blob: 9440816f624818afe8c029490a7fd6a86dc99c8c [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafbee91162016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafbee91162016-03-04 01:09:59 +01006 */
7
Alexander Grafbee91162016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Alexander Grafbee91162016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020022
Alexander Grafbee91162016-03-04 01:09:59 +010023/* This list contains all the EFI objects our payload has access to */
24LIST_HEAD(efi_obj_list);
25
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010028
Simon Glass65e4c0b2016-09-25 15:27:35 -060029#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010030/*
31 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
32 * fixed when compiling U-Boot. However, the payload does not know about that
33 * restriction so we need to manually swap its and our view of that register on
34 * EFI callback entry/exit.
35 */
36static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060037#endif
Alexander Grafbee91162016-03-04 01:09:59 +010038
Rob Clarkc160d2f2017-07-27 08:04:18 -040039static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040040static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010041/* GUID of the device tree table */
42const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010043/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
44const efi_guid_t efi_guid_driver_binding_protocol =
45 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040046
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010047/* event group ExitBootServices() invoked */
48const efi_guid_t efi_guid_event_group_exit_boot_services =
49 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
50/* event group SetVirtualAddressMap() invoked */
51const efi_guid_t efi_guid_event_group_virtual_address_change =
52 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
53/* event group memory map changed */
54const efi_guid_t efi_guid_event_group_memory_map_change =
55 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
56/* event group boot manager about to boot */
57const efi_guid_t efi_guid_event_group_ready_to_boot =
58 EFI_EVENT_GROUP_READY_TO_BOOT;
59/* event group ResetSystem() invoked (before ExitBootServices) */
60const efi_guid_t efi_guid_event_group_reset_system =
61 EFI_EVENT_GROUP_RESET_SYSTEM;
62
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010063static efi_status_t EFIAPI efi_disconnect_controller(
64 efi_handle_t controller_handle,
65 efi_handle_t driver_image_handle,
66 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010067
Rob Clarkc160d2f2017-07-27 08:04:18 -040068/* Called on every callback entry */
69int __efi_entry_check(void)
70{
71 int ret = entry_count++ == 0;
72#ifdef CONFIG_ARM
73 assert(efi_gd);
74 app_gd = gd;
75 gd = efi_gd;
76#endif
77 return ret;
78}
79
80/* Called on every callback exit */
81int __efi_exit_check(void)
82{
83 int ret = --entry_count == 0;
84#ifdef CONFIG_ARM
85 gd = app_gd;
86#endif
87 return ret;
88}
89
Alexander Grafbee91162016-03-04 01:09:59 +010090/* Called from do_bootefi_exec() */
91void efi_save_gd(void)
92{
Simon Glass65e4c0b2016-09-25 15:27:35 -060093#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010094 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060095#endif
Alexander Grafbee91162016-03-04 01:09:59 +010096}
97
Rob Clarkc160d2f2017-07-27 08:04:18 -040098/*
Mario Six78a88f72018-07-10 08:40:17 +020099 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200100 * world so we can dump out an abort message, without any care about returning
101 * back to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400102 */
Alexander Grafbee91162016-03-04 01:09:59 +0100103void efi_restore_gd(void)
104{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600105#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100106 /* Only restore if we're already in EFI context */
107 if (!efi_gd)
108 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100109 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600110#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100111}
112
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200113/**
Mario Six78a88f72018-07-10 08:40:17 +0200114 * indent_string() - returns a string for indenting with two spaces per level
115 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100116 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200117 * A maximum of ten indent levels is supported. Higher indent levels will be
118 * truncated.
119 *
Mario Six78a88f72018-07-10 08:40:17 +0200120 * Return: A string for indenting with two spaces per level is
121 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400122 */
123static const char *indent_string(int level)
124{
125 const char *indent = " ";
126 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100127
Rob Clarkaf65db82017-07-27 08:04:19 -0400128 level = min(max, level * 2);
129 return &indent[max - level];
130}
131
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200132const char *__efi_nesting(void)
133{
134 return indent_string(nesting_level);
135}
136
Rob Clarkaf65db82017-07-27 08:04:19 -0400137const char *__efi_nesting_inc(void)
138{
139 return indent_string(nesting_level++);
140}
141
142const char *__efi_nesting_dec(void)
143{
144 return indent_string(--nesting_level);
145}
146
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200147/**
Mario Six78a88f72018-07-10 08:40:17 +0200148 * efi_queue_event() - queue an EFI event
149 * @event: event to signal
150 * @check_tpl: check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200151 *
152 * This function queues the notification function of the event for future
153 * execution.
154 *
Mario Six78a88f72018-07-10 08:40:17 +0200155 * The notification function is called if the task priority level of the event
156 * is higher than the current task priority level.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200157 *
158 * For the SignalEvent service see efi_signal_event_ext.
159 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200160 */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100161static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200162{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200163 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200164 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200165 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100166 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200167 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200168 EFI_CALL_VOID(event->notify_function(event,
169 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200170 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200171 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200172}
173
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200174/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200175 * is_valid_tpl() - check if the task priority level is valid
176 *
177 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200178 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200179 */
180efi_status_t is_valid_tpl(efi_uintn_t tpl)
181{
182 switch (tpl) {
183 case TPL_APPLICATION:
184 case TPL_CALLBACK:
185 case TPL_NOTIFY:
186 case TPL_HIGH_LEVEL:
187 return EFI_SUCCESS;
188 default:
189 return EFI_INVALID_PARAMETER;
190 }
191}
192
193/**
Mario Six78a88f72018-07-10 08:40:17 +0200194 * efi_signal_event() - signal an EFI event
195 * @event: event to signal
196 * @check_tpl: check the TPL level
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100197 *
Mario Six78a88f72018-07-10 08:40:17 +0200198 * This function signals an event. If the event belongs to an event group all
199 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100200 * their notification function is queued.
201 *
202 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100203 */
204void efi_signal_event(struct efi_event *event, bool check_tpl)
205{
206 if (event->group) {
207 struct efi_event *evt;
208
209 /*
210 * The signaled state has to set before executing any
211 * notification function
212 */
213 list_for_each_entry(evt, &efi_events, link) {
214 if (!evt->group || guidcmp(evt->group, event->group))
215 continue;
216 if (evt->is_signaled)
217 continue;
218 evt->is_signaled = true;
219 if (evt->type & EVT_NOTIFY_SIGNAL &&
220 evt->notify_function)
221 evt->is_queued = true;
222 }
223 list_for_each_entry(evt, &efi_events, link) {
224 if (!evt->group || guidcmp(evt->group, event->group))
225 continue;
226 if (evt->is_queued)
227 efi_queue_event(evt, check_tpl);
228 }
229 } else if (!event->is_signaled) {
230 event->is_signaled = true;
231 if (event->type & EVT_NOTIFY_SIGNAL)
232 efi_queue_event(event, check_tpl);
233 }
234}
235
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200236/**
Mario Six78a88f72018-07-10 08:40:17 +0200237 * efi_raise_tpl() - raise the task priority level
238 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200239 *
240 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200241 *
Mario Six78a88f72018-07-10 08:40:17 +0200242 * See the Unified Extensible Firmware Interface (UEFI) specification for
243 * details.
244 *
245 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200246 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100247static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100248{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100249 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200250
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200251 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200252
253 if (new_tpl < efi_tpl)
254 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
255 efi_tpl = new_tpl;
256 if (efi_tpl > TPL_HIGH_LEVEL)
257 efi_tpl = TPL_HIGH_LEVEL;
258
259 EFI_EXIT(EFI_SUCCESS);
260 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100261}
262
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200263/**
Mario Six78a88f72018-07-10 08:40:17 +0200264 * efi_restore_tpl() - lower the task priority level
265 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200266 *
267 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200268 *
Mario Six78a88f72018-07-10 08:40:17 +0200269 * See the Unified Extensible Firmware Interface (UEFI) specification for
270 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200271 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100272static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100273{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200274 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200275
276 if (old_tpl > efi_tpl)
277 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
278 efi_tpl = old_tpl;
279 if (efi_tpl > TPL_HIGH_LEVEL)
280 efi_tpl = TPL_HIGH_LEVEL;
281
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100282 /*
283 * Lowering the TPL may have made queued events eligible for execution.
284 */
285 efi_timer_check();
286
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200287 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100288}
289
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200290/**
Mario Six78a88f72018-07-10 08:40:17 +0200291 * efi_allocate_pages_ext() - allocate memory pages
292 * @type: type of allocation to be performed
293 * @memory_type: usage type of the allocated memory
294 * @pages: number of pages to be allocated
295 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200296 *
297 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200298 *
Mario Six78a88f72018-07-10 08:40:17 +0200299 * See the Unified Extensible Firmware Interface (UEFI) specification for
300 * details.
301 *
302 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200303 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900304static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100305 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900306 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100307{
308 efi_status_t r;
309
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100310 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100311 r = efi_allocate_pages(type, memory_type, pages, memory);
312 return EFI_EXIT(r);
313}
314
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200315/**
Mario Six78a88f72018-07-10 08:40:17 +0200316 * efi_free_pages_ext() - Free memory pages.
317 * @memory: start of the memory area to be freed
318 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200319 *
320 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200321 *
Mario Six78a88f72018-07-10 08:40:17 +0200322 * See the Unified Extensible Firmware Interface (UEFI) specification for
323 * details.
324 *
325 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200326 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900327static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100328 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100329{
330 efi_status_t r;
331
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900332 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100333 r = efi_free_pages(memory, pages);
334 return EFI_EXIT(r);
335}
336
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200337/**
Mario Six78a88f72018-07-10 08:40:17 +0200338 * efi_get_memory_map_ext() - get map describing memory usage
339 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
340 * on exit the size of the copied memory map
341 * @memory_map: buffer to which the memory map is written
342 * @map_key: key for the memory map
343 * @descriptor_size: size of an individual memory descriptor
344 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200345 *
346 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200347 *
Mario Six78a88f72018-07-10 08:40:17 +0200348 * See the Unified Extensible Firmware Interface (UEFI) specification for
349 * details.
350 *
351 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200352 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900353static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100354 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900355 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100356 efi_uintn_t *map_key,
357 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900358 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100359{
360 efi_status_t r;
361
362 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
363 map_key, descriptor_size, descriptor_version);
364 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
365 descriptor_size, descriptor_version);
366 return EFI_EXIT(r);
367}
368
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200369/**
Mario Six78a88f72018-07-10 08:40:17 +0200370 * efi_allocate_pool_ext() - allocate memory from pool
371 * @pool_type: type of the pool from which memory is to be allocated
372 * @size: number of bytes to be allocated
373 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200374 *
375 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200376 *
Mario Six78a88f72018-07-10 08:40:17 +0200377 * See the Unified Extensible Firmware Interface (UEFI) specification for
378 * details.
379 *
380 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200381 */
Stefan Brünsead12742016-10-09 22:17:18 +0200382static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100383 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200384 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100385{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100386 efi_status_t r;
387
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100388 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200389 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100390 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100391}
392
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200393/**
Mario Six78a88f72018-07-10 08:40:17 +0200394 * efi_free_pool_ext() - free memory from pool
395 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200396 *
397 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200398 *
Mario Six78a88f72018-07-10 08:40:17 +0200399 * See the Unified Extensible Firmware Interface (UEFI) specification for
400 * details.
401 *
402 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200403 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200404static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100405{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100406 efi_status_t r;
407
408 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200409 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100410 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100411}
412
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200413/**
Mario Six78a88f72018-07-10 08:40:17 +0200414 * efi_add_handle() - add a new object to the object list
415 * @obj: object to be added
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100416 *
Mario Six78a88f72018-07-10 08:40:17 +0200417 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100418 */
419void efi_add_handle(struct efi_object *obj)
420{
421 if (!obj)
422 return;
423 INIT_LIST_HEAD(&obj->protocols);
424 obj->handle = obj;
425 list_add_tail(&obj->link, &efi_obj_list);
426}
427
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200428/**
Mario Six78a88f72018-07-10 08:40:17 +0200429 * efi_create_handle() - create handle
430 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200431 *
Mario Six78a88f72018-07-10 08:40:17 +0200432 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200433 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100434efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200435{
436 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200437
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200438 obj = calloc(1, sizeof(struct efi_object));
439 if (!obj)
440 return EFI_OUT_OF_RESOURCES;
441
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100442 efi_add_handle(obj);
443 *handle = obj->handle;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200444
445 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200446}
447
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200448/**
Mario Six78a88f72018-07-10 08:40:17 +0200449 * efi_search_protocol() - find a protocol on a handle.
450 * @handle: handle
451 * @protocol_guid: GUID of the protocol
452 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100453 *
Mario Six78a88f72018-07-10 08:40:17 +0200454 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100455 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100456efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100457 const efi_guid_t *protocol_guid,
458 struct efi_handler **handler)
459{
460 struct efi_object *efiobj;
461 struct list_head *lhandle;
462
463 if (!handle || !protocol_guid)
464 return EFI_INVALID_PARAMETER;
465 efiobj = efi_search_obj(handle);
466 if (!efiobj)
467 return EFI_INVALID_PARAMETER;
468 list_for_each(lhandle, &efiobj->protocols) {
469 struct efi_handler *protocol;
470
471 protocol = list_entry(lhandle, struct efi_handler, link);
472 if (!guidcmp(protocol->guid, protocol_guid)) {
473 if (handler)
474 *handler = protocol;
475 return EFI_SUCCESS;
476 }
477 }
478 return EFI_NOT_FOUND;
479}
480
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200481/**
Mario Six78a88f72018-07-10 08:40:17 +0200482 * efi_remove_protocol() - delete protocol from a handle
483 * @handle: handle from which the protocol shall be deleted
484 * @protocol: GUID of the protocol to be deleted
485 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100486 *
Mario Six78a88f72018-07-10 08:40:17 +0200487 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100488 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100489efi_status_t efi_remove_protocol(const efi_handle_t handle,
490 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100491 void *protocol_interface)
492{
493 struct efi_handler *handler;
494 efi_status_t ret;
495
496 ret = efi_search_protocol(handle, protocol, &handler);
497 if (ret != EFI_SUCCESS)
498 return ret;
499 if (guidcmp(handler->guid, protocol))
500 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200501 if (handler->protocol_interface != protocol_interface)
502 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100503 list_del(&handler->link);
504 free(handler);
505 return EFI_SUCCESS;
506}
507
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200508/**
Mario Six78a88f72018-07-10 08:40:17 +0200509 * efi_remove_all_protocols() - delete all protocols from a handle
510 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100511 *
Mario Six78a88f72018-07-10 08:40:17 +0200512 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100513 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100514efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100515{
516 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100517 struct efi_handler *protocol;
518 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100519
520 efiobj = efi_search_obj(handle);
521 if (!efiobj)
522 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100523 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100524 efi_status_t ret;
525
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100526 ret = efi_remove_protocol(handle, protocol->guid,
527 protocol->protocol_interface);
528 if (ret != EFI_SUCCESS)
529 return ret;
530 }
531 return EFI_SUCCESS;
532}
533
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200534/**
Mario Six78a88f72018-07-10 08:40:17 +0200535 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100536 *
Mario Six78a88f72018-07-10 08:40:17 +0200537 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100538 */
539void efi_delete_handle(struct efi_object *obj)
540{
541 if (!obj)
542 return;
543 efi_remove_all_protocols(obj->handle);
544 list_del(&obj->link);
545 free(obj);
546}
547
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200548/**
Mario Six78a88f72018-07-10 08:40:17 +0200549 * efi_is_event() - check if a pointer is a valid event
550 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100551 *
Mario Six78a88f72018-07-10 08:40:17 +0200552 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100553 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100554static efi_status_t efi_is_event(const struct efi_event *event)
555{
556 const struct efi_event *evt;
557
558 if (!event)
559 return EFI_INVALID_PARAMETER;
560 list_for_each_entry(evt, &efi_events, link) {
561 if (evt == event)
562 return EFI_SUCCESS;
563 }
564 return EFI_INVALID_PARAMETER;
565}
Alexander Grafbee91162016-03-04 01:09:59 +0100566
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200567/**
Mario Six78a88f72018-07-10 08:40:17 +0200568 * efi_create_event() - create an event
569 * @type: type of the event to create
570 * @notify_tpl: task priority level of the event
571 * @notify_function: notification function of the event
572 * @notify_context: pointer passed to the notification function
573 * @group: event group
574 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200575 *
576 * This function is used inside U-Boot code to create an event.
577 *
578 * For the API function implementing the CreateEvent service see
579 * efi_create_event_ext.
580 *
Mario Six78a88f72018-07-10 08:40:17 +0200581 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200582 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100583efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200584 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200585 struct efi_event *event,
586 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100587 void *notify_context, efi_guid_t *group,
588 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100589{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100590 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200591
Jonathan Graya95343b2017-03-12 19:26:07 +1100592 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200593 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100594
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200595 switch (type) {
596 case 0:
597 case EVT_TIMER:
598 case EVT_NOTIFY_SIGNAL:
599 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
600 case EVT_NOTIFY_WAIT:
601 case EVT_TIMER | EVT_NOTIFY_WAIT:
602 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
603 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
604 break;
605 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200606 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200607 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100608
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900609 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
610 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200611 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100612
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100613 evt = calloc(1, sizeof(struct efi_event));
614 if (!evt)
615 return EFI_OUT_OF_RESOURCES;
616 evt->type = type;
617 evt->notify_tpl = notify_tpl;
618 evt->notify_function = notify_function;
619 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100620 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200621 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100622 evt->trigger_next = -1ULL;
623 evt->is_queued = false;
624 evt->is_signaled = false;
625 list_add_tail(&evt->link, &efi_events);
626 *event = evt;
627 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100628}
629
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200630/*
Mario Six78a88f72018-07-10 08:40:17 +0200631 * efi_create_event_ex() - create an event in a group
632 * @type: type of the event to create
633 * @notify_tpl: task priority level of the event
634 * @notify_function: notification function of the event
635 * @notify_context: pointer passed to the notification function
636 * @event: created event
637 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100638 *
639 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100640 *
Mario Six78a88f72018-07-10 08:40:17 +0200641 * See the Unified Extensible Firmware Interface (UEFI) specification for
642 * details.
643 *
644 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100645 */
646efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
647 void (EFIAPI *notify_function) (
648 struct efi_event *event,
649 void *context),
650 void *notify_context,
651 efi_guid_t *event_group,
652 struct efi_event **event)
653{
654 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
655 notify_context, event_group);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100656 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100657 notify_context, event_group, event));
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100658}
659
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200660/**
Mario Six78a88f72018-07-10 08:40:17 +0200661 * efi_create_event_ext() - create an event
662 * @type: type of the event to create
663 * @notify_tpl: task priority level of the event
664 * @notify_function: notification function of the event
665 * @notify_context: pointer passed to the notification function
666 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200667 *
668 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200669 *
Mario Six78a88f72018-07-10 08:40:17 +0200670 * See the Unified Extensible Firmware Interface (UEFI) specification for
671 * details.
672 *
673 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200674 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200675static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100676 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200677 void (EFIAPI *notify_function) (
678 struct efi_event *event,
679 void *context),
680 void *notify_context, struct efi_event **event)
681{
682 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
683 notify_context);
684 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100685 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200686}
687
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200688/**
Mario Six78a88f72018-07-10 08:40:17 +0200689 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200690 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200691 * Check if a timer event has occurred or a queued notification function should
692 * be called.
693 *
Alexander Grafbee91162016-03-04 01:09:59 +0100694 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200695 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100696 */
697void efi_timer_check(void)
698{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100699 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100700 u64 now = timer_get_us();
701
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100702 list_for_each_entry(evt, &efi_events, link) {
703 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100704 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100705 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200706 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100707 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200708 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100709 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200710 break;
711 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100712 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200713 break;
714 default:
715 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200716 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100717 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100718 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100719 }
Alexander Grafbee91162016-03-04 01:09:59 +0100720 WATCHDOG_RESET();
721}
722
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200723/**
Mario Six78a88f72018-07-10 08:40:17 +0200724 * efi_set_timer() - set the trigger time for a timer event or stop the event
725 * @event: event for which the timer is set
726 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200727 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200728 *
729 * This is the function for internal usage in U-Boot. For the API function
730 * implementing the SetTimer service see efi_set_timer_ext.
731 *
Mario Six78a88f72018-07-10 08:40:17 +0200732 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200733 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200734efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200735 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100736{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100737 /* Check that the event is valid */
738 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
739 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100740
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200741 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200742 * The parameter defines a multiple of 100 ns.
743 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200744 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200745 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100746
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100747 switch (type) {
748 case EFI_TIMER_STOP:
749 event->trigger_next = -1ULL;
750 break;
751 case EFI_TIMER_PERIODIC:
752 case EFI_TIMER_RELATIVE:
753 event->trigger_next = timer_get_us() + trigger_time;
754 break;
755 default:
756 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100757 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100758 event->trigger_type = type;
759 event->trigger_time = trigger_time;
760 event->is_signaled = false;
761 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200762}
763
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200764/**
Mario Six78a88f72018-07-10 08:40:17 +0200765 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
766 * event
767 * @event: event for which the timer is set
768 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200769 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200770 *
771 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200772 *
Mario Six78a88f72018-07-10 08:40:17 +0200773 * See the Unified Extensible Firmware Interface (UEFI) specification for
774 * details.
775 *
776 *
777 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200778 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200779static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
780 enum efi_timer_delay type,
781 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200782{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900783 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200784 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100785}
786
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200787/**
Mario Six78a88f72018-07-10 08:40:17 +0200788 * efi_wait_for_event() - wait for events to be signaled
789 * @num_events: number of events to be waited for
790 * @event: events to be waited for
791 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200792 *
793 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200794 *
Mario Six78a88f72018-07-10 08:40:17 +0200795 * See the Unified Extensible Firmware Interface (UEFI) specification for
796 * details.
797 *
798 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200799 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100800static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200801 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100802 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100803{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100804 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100805
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100806 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100807
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200808 /* Check parameters */
809 if (!num_events || !event)
810 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200811 /* Check TPL */
812 if (efi_tpl != TPL_APPLICATION)
813 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200814 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100815 if (efi_is_event(event[i]) != EFI_SUCCESS)
816 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200817 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
818 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200819 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100820 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200821 }
822
823 /* Wait for signal */
824 for (;;) {
825 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200826 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200827 goto out;
828 }
829 /* Allow events to occur. */
830 efi_timer_check();
831 }
832
833out:
834 /*
835 * Reset the signal which is passed to the caller to allow periodic
836 * events to occur.
837 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200838 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200839 if (index)
840 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100841
842 return EFI_EXIT(EFI_SUCCESS);
843}
844
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200845/**
Mario Six78a88f72018-07-10 08:40:17 +0200846 * efi_signal_event_ext() - signal an EFI event
847 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200848 *
849 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200850 *
851 * See the Unified Extensible Firmware Interface (UEFI) specification for
852 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200853 *
854 * This functions sets the signaled state of the event and queues the
855 * notification function for execution.
856 *
Mario Six78a88f72018-07-10 08:40:17 +0200857 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200858 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200859static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100860{
861 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100862 if (efi_is_event(event) != EFI_SUCCESS)
863 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100864 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100865 return EFI_EXIT(EFI_SUCCESS);
866}
867
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200868/**
Mario Six78a88f72018-07-10 08:40:17 +0200869 * efi_close_event() - close an EFI event
870 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200871 *
872 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200873 *
Mario Six78a88f72018-07-10 08:40:17 +0200874 * See the Unified Extensible Firmware Interface (UEFI) specification for
875 * details.
876 *
877 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200878 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200879static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100880{
881 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100882 if (efi_is_event(event) != EFI_SUCCESS)
883 return EFI_EXIT(EFI_INVALID_PARAMETER);
884 list_del(&event->link);
885 free(event);
886 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100887}
888
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200889/**
Mario Six78a88f72018-07-10 08:40:17 +0200890 * efi_check_event() - check if an event is signaled
891 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200892 *
893 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200894 *
Mario Six78a88f72018-07-10 08:40:17 +0200895 * See the Unified Extensible Firmware Interface (UEFI) specification for
896 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200897 *
Mario Six78a88f72018-07-10 08:40:17 +0200898 * If an event is not signaled yet, the notification function is queued. The
899 * signaled state is cleared.
900 *
901 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200902 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200903static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100904{
905 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200906 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100907 if (efi_is_event(event) != EFI_SUCCESS ||
908 event->type & EVT_NOTIFY_SIGNAL)
909 return EFI_EXIT(EFI_INVALID_PARAMETER);
910 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100911 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100912 if (event->is_signaled) {
913 event->is_signaled = false;
914 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200915 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100916 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100917}
918
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200919/**
Mario Six78a88f72018-07-10 08:40:17 +0200920 * efi_search_obj() - find the internal EFI object for a handle
921 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200922 *
Mario Six78a88f72018-07-10 08:40:17 +0200923 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200924 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100925struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200926{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100927 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200928
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100929 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200930 if (efiobj->handle == handle)
931 return efiobj;
932 }
933
934 return NULL;
935}
936
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200937/**
Mario Six78a88f72018-07-10 08:40:17 +0200938 * efi_open_protocol_info_entry() - create open protocol info entry and add it
939 * to a protocol
940 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100941 *
Mario Six78a88f72018-07-10 08:40:17 +0200942 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100943 */
944static struct efi_open_protocol_info_entry *efi_create_open_info(
945 struct efi_handler *handler)
946{
947 struct efi_open_protocol_info_item *item;
948
949 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
950 if (!item)
951 return NULL;
952 /* Append the item to the open protocol info list. */
953 list_add_tail(&item->link, &handler->open_infos);
954
955 return &item->info;
956}
957
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200958/**
Mario Six78a88f72018-07-10 08:40:17 +0200959 * efi_delete_open_info() - remove an open protocol info entry from a protocol
960 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100961 *
Mario Six78a88f72018-07-10 08:40:17 +0200962 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100963 */
964static efi_status_t efi_delete_open_info(
965 struct efi_open_protocol_info_item *item)
966{
967 list_del(&item->link);
968 free(item);
969 return EFI_SUCCESS;
970}
971
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200972/**
Mario Six78a88f72018-07-10 08:40:17 +0200973 * efi_add_protocol() - install new protocol on a handle
974 * @handle: handle on which the protocol shall be installed
975 * @protocol: GUID of the protocol to be installed
976 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200977 *
Mario Six78a88f72018-07-10 08:40:17 +0200978 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200979 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100980efi_status_t efi_add_protocol(const efi_handle_t handle,
981 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200982 void *protocol_interface)
983{
984 struct efi_object *efiobj;
985 struct efi_handler *handler;
986 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200987
988 efiobj = efi_search_obj(handle);
989 if (!efiobj)
990 return EFI_INVALID_PARAMETER;
991 ret = efi_search_protocol(handle, protocol, NULL);
992 if (ret != EFI_NOT_FOUND)
993 return EFI_INVALID_PARAMETER;
994 handler = calloc(1, sizeof(struct efi_handler));
995 if (!handler)
996 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100997 handler->guid = protocol;
998 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100999 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001000 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001001 if (!guidcmp(&efi_guid_device_path, protocol))
1002 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001003 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001004}
1005
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001006/**
Mario Six78a88f72018-07-10 08:40:17 +02001007 * efi_install_protocol_interface() - install protocol interface
1008 * @handle: handle on which the protocol shall be installed
1009 * @protocol: GUID of the protocol to be installed
1010 * @protocol_interface_type: type of the interface to be installed,
1011 * always EFI_NATIVE_INTERFACE
1012 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001013 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001014 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001015 *
Mario Six78a88f72018-07-10 08:40:17 +02001016 * See the Unified Extensible Firmware Interface (UEFI) specification for
1017 * details.
1018 *
1019 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001020 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001021static efi_status_t EFIAPI efi_install_protocol_interface(
1022 void **handle, const efi_guid_t *protocol,
1023 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001024{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001025 efi_status_t r;
1026
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001027 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1028 protocol_interface);
1029
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001030 if (!handle || !protocol ||
1031 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1032 r = EFI_INVALID_PARAMETER;
1033 goto out;
1034 }
1035
1036 /* Create new handle if requested. */
1037 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001038 r = efi_create_handle(handle);
1039 if (r != EFI_SUCCESS)
1040 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001041 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1042 *handle);
1043 } else {
1044 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1045 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001046 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001047 /* Add new protocol */
1048 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001049out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001050 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001051}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001052
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001053/**
Mario Six78a88f72018-07-10 08:40:17 +02001054 * efi_get_drivers() - get all drivers associated to a controller
1055 * @efiobj: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001056 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001057 * @number_of_drivers: number of child controllers
1058 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001059 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001060 * The allocated buffer has to be freed with free().
1061 *
Mario Six78a88f72018-07-10 08:40:17 +02001062 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001063 */
1064static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1065 const efi_guid_t *protocol,
1066 efi_uintn_t *number_of_drivers,
1067 efi_handle_t **driver_handle_buffer)
1068{
1069 struct efi_handler *handler;
1070 struct efi_open_protocol_info_item *item;
1071 efi_uintn_t count = 0, i;
1072 bool duplicate;
1073
1074 /* Count all driver associations */
1075 list_for_each_entry(handler, &efiobj->protocols, link) {
1076 if (protocol && guidcmp(handler->guid, protocol))
1077 continue;
1078 list_for_each_entry(item, &handler->open_infos, link) {
1079 if (item->info.attributes &
1080 EFI_OPEN_PROTOCOL_BY_DRIVER)
1081 ++count;
1082 }
1083 }
1084 /*
1085 * Create buffer. In case of duplicate driver assignments the buffer
1086 * will be too large. But that does not harm.
1087 */
1088 *number_of_drivers = 0;
1089 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1090 if (!*driver_handle_buffer)
1091 return EFI_OUT_OF_RESOURCES;
1092 /* Collect unique driver handles */
1093 list_for_each_entry(handler, &efiobj->protocols, link) {
1094 if (protocol && guidcmp(handler->guid, protocol))
1095 continue;
1096 list_for_each_entry(item, &handler->open_infos, link) {
1097 if (item->info.attributes &
1098 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1099 /* Check this is a new driver */
1100 duplicate = false;
1101 for (i = 0; i < *number_of_drivers; ++i) {
1102 if ((*driver_handle_buffer)[i] ==
1103 item->info.agent_handle)
1104 duplicate = true;
1105 }
1106 /* Copy handle to buffer */
1107 if (!duplicate) {
1108 i = (*number_of_drivers)++;
1109 (*driver_handle_buffer)[i] =
1110 item->info.agent_handle;
1111 }
1112 }
1113 }
1114 }
1115 return EFI_SUCCESS;
1116}
1117
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001118/**
Mario Six78a88f72018-07-10 08:40:17 +02001119 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1120 * @efiobj: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001121 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001122 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001123 *
1124 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001125 *
Mario Six78a88f72018-07-10 08:40:17 +02001126 * See the Unified Extensible Firmware Interface (UEFI) specification for
1127 * details.
1128 *
1129 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001130 */
1131static efi_status_t efi_disconnect_all_drivers(
1132 struct efi_object *efiobj,
1133 const efi_guid_t *protocol,
1134 efi_handle_t child_handle)
1135{
1136 efi_uintn_t number_of_drivers;
1137 efi_handle_t *driver_handle_buffer;
1138 efi_status_t r, ret;
1139
1140 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1141 &driver_handle_buffer);
1142 if (ret != EFI_SUCCESS)
1143 return ret;
1144
1145 ret = EFI_NOT_FOUND;
1146 while (number_of_drivers) {
1147 r = EFI_CALL(efi_disconnect_controller(
1148 efiobj->handle,
1149 driver_handle_buffer[--number_of_drivers],
1150 child_handle));
1151 if (r == EFI_SUCCESS)
1152 ret = r;
1153 }
1154 free(driver_handle_buffer);
1155 return ret;
1156}
1157
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001158/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001159 * efi_uninstall_protocol() - uninstall protocol interface
1160 *
Mario Six78a88f72018-07-10 08:40:17 +02001161 * @handle: handle from which the protocol shall be removed
1162 * @protocol: GUID of the protocol to be removed
1163 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001164 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001165 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001166 *
1167 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001168 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001169static efi_status_t efi_uninstall_protocol
1170 (efi_handle_t handle, const efi_guid_t *protocol,
1171 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001172{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001173 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001174 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001175 struct efi_open_protocol_info_item *item;
1176 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001177 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001178
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001179 /* Check handle */
1180 efiobj = efi_search_obj(handle);
1181 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001182 r = EFI_INVALID_PARAMETER;
1183 goto out;
1184 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001185 /* Find the protocol on the handle */
1186 r = efi_search_protocol(handle, protocol, &handler);
1187 if (r != EFI_SUCCESS)
1188 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001189 /* Disconnect controllers */
1190 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1191 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001192 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001193 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001194 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001195 /* Close protocol */
1196 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1197 if (item->info.attributes ==
1198 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1199 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1200 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1201 list_del(&item->link);
1202 }
1203 if (!list_empty(&handler->open_infos)) {
1204 r = EFI_ACCESS_DENIED;
1205 goto out;
1206 }
1207 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001208out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001209 return r;
1210}
1211
1212/**
1213 * efi_uninstall_protocol_interface() - uninstall protocol interface
1214 * @handle: handle from which the protocol shall be removed
1215 * @protocol: GUID of the protocol to be removed
1216 * @protocol_interface: interface to be removed
1217 *
1218 * This function implements the UninstallProtocolInterface service.
1219 *
1220 * See the Unified Extensible Firmware Interface (UEFI) specification for
1221 * details.
1222 *
1223 * Return: status code
1224 */
1225static efi_status_t EFIAPI efi_uninstall_protocol_interface
1226 (efi_handle_t handle, const efi_guid_t *protocol,
1227 void *protocol_interface)
1228{
1229 efi_status_t ret;
1230
1231 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1232
1233 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1234 if (ret != EFI_SUCCESS)
1235 goto out;
1236
1237 /* If the last protocol has been removed, delete the handle. */
1238 if (list_empty(&handle->protocols)) {
1239 list_del(&handle->link);
1240 free(handle);
1241 }
1242out:
1243 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001244}
1245
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001246/**
Mario Six78a88f72018-07-10 08:40:17 +02001247 * efi_register_protocol_notify() - register an event for notification when a
1248 * protocol is installed.
1249 * @protocol: GUID of the protocol whose installation shall be notified
1250 * @event: event to be signaled upon installation of the protocol
1251 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001252 *
1253 * This function implements the RegisterProtocolNotify service.
1254 * See the Unified Extensible Firmware Interface (UEFI) specification
1255 * for details.
1256 *
Mario Six78a88f72018-07-10 08:40:17 +02001257 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001258 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001259static efi_status_t EFIAPI efi_register_protocol_notify(
1260 const efi_guid_t *protocol,
1261 struct efi_event *event,
1262 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001263{
Rob Clark778e6af2017-09-13 18:05:41 -04001264 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001265 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1266}
1267
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001268/**
Mario Six78a88f72018-07-10 08:40:17 +02001269 * efi_search() - determine if an EFI handle implements a protocol
1270 * @search_type: selection criterion
1271 * @protocol: GUID of the protocol
1272 * @search_key: registration key
1273 * @efiobj: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001274 *
1275 * See the documentation of the LocateHandle service in the UEFI specification.
1276 *
Mario Six78a88f72018-07-10 08:40:17 +02001277 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001278 */
Alexander Grafbee91162016-03-04 01:09:59 +01001279static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001280 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001281 struct efi_object *efiobj)
1282{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001283 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001284
1285 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001286 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001287 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001288 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001289 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001290 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001291 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001292 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1293 return (ret != EFI_SUCCESS);
1294 default:
1295 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001296 return -1;
1297 }
Alexander Grafbee91162016-03-04 01:09:59 +01001298}
1299
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001300/**
Mario Six78a88f72018-07-10 08:40:17 +02001301 * efi_locate_handle() - locate handles implementing a protocol
1302 * @search_type: selection criterion
1303 * @protocol: GUID of the protocol
1304 * @search_key: registration key
1305 * @buffer_size: size of the buffer to receive the handles in bytes
1306 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001307 *
1308 * This function is meant for U-Boot internal calls. For the API implementation
1309 * of the LocateHandle service see efi_locate_handle_ext.
1310 *
Mario Six78a88f72018-07-10 08:40:17 +02001311 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001312 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001313static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001314 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001315 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001316 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001317{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001318 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001319 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001320
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001321 /* Check parameters */
1322 switch (search_type) {
1323 case ALL_HANDLES:
1324 break;
1325 case BY_REGISTER_NOTIFY:
1326 if (!search_key)
1327 return EFI_INVALID_PARAMETER;
1328 /* RegisterProtocolNotify is not implemented yet */
1329 return EFI_UNSUPPORTED;
1330 case BY_PROTOCOL:
1331 if (!protocol)
1332 return EFI_INVALID_PARAMETER;
1333 break;
1334 default:
1335 return EFI_INVALID_PARAMETER;
1336 }
1337
1338 /*
1339 * efi_locate_handle_buffer uses this function for
1340 * the calculation of the necessary buffer size.
1341 * So do not require a buffer for buffersize == 0.
1342 */
1343 if (!buffer_size || (*buffer_size && !buffer))
1344 return EFI_INVALID_PARAMETER;
1345
Alexander Grafbee91162016-03-04 01:09:59 +01001346 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001347 list_for_each_entry(efiobj, &efi_obj_list, link) {
1348 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001349 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001350 }
1351
1352 if (*buffer_size < size) {
1353 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001354 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001355 }
1356
Rob Clark796a78c2017-08-06 14:10:07 -04001357 *buffer_size = size;
1358 if (size == 0)
1359 return EFI_NOT_FOUND;
1360
Alexander Grafbee91162016-03-04 01:09:59 +01001361 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001362 list_for_each_entry(efiobj, &efi_obj_list, link) {
1363 if (!efi_search(search_type, protocol, search_key, efiobj))
1364 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001365 }
1366
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001367 return EFI_SUCCESS;
1368}
1369
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001370/**
Mario Six78a88f72018-07-10 08:40:17 +02001371 * efi_locate_handle_ext() - locate handles implementing a protocol.
1372 * @search_type: selection criterion
1373 * @protocol: GUID of the protocol
1374 * @search_key: registration key
1375 * @buffer_size: size of the buffer to receive the handles in bytes
1376 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001377 *
1378 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001379 *
Mario Six78a88f72018-07-10 08:40:17 +02001380 * See the Unified Extensible Firmware Interface (UEFI) specification for
1381 * details.
1382 *
1383 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001384 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001385static efi_status_t EFIAPI efi_locate_handle_ext(
1386 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001387 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001388 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001389{
Rob Clark778e6af2017-09-13 18:05:41 -04001390 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001391 buffer_size, buffer);
1392
1393 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1394 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001395}
1396
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001397/**
Mario Six78a88f72018-07-10 08:40:17 +02001398 * efi_remove_configuration_table() - collapses configuration table entries,
1399 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001400 *
Mario Six78a88f72018-07-10 08:40:17 +02001401 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001402 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001403static void efi_remove_configuration_table(int i)
1404{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001405 struct efi_configuration_table *this = &systab.tables[i];
1406 struct efi_configuration_table *next = &systab.tables[i + 1];
1407 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001408
1409 memmove(this, next, (ulong)end - (ulong)next);
1410 systab.nr_tables--;
1411}
1412
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001413/**
Mario Six78a88f72018-07-10 08:40:17 +02001414 * efi_install_configuration_table() - adds, updates, or removes a
1415 * configuration table
1416 * @guid: GUID of the installed table
1417 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001418 *
1419 * This function is used for internal calls. For the API implementation of the
1420 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1421 *
Mario Six78a88f72018-07-10 08:40:17 +02001422 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001423 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001424efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1425 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001426{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001427 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001428 int i;
1429
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001430 if (!guid)
1431 return EFI_INVALID_PARAMETER;
1432
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001433 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001434 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001435 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001436 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001437 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001438 else
1439 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001440 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001441 }
1442 }
1443
Alexander Grafd98cdf62017-07-26 13:41:04 +02001444 if (!table)
1445 return EFI_NOT_FOUND;
1446
Alexander Grafbee91162016-03-04 01:09:59 +01001447 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001448 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001449 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001450
1451 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001452 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1453 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001454 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001455
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001456out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001457 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001458 efi_update_table_header_crc32(&systab.hdr);
1459
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001460 /* Notify that the configuration table was changed */
1461 list_for_each_entry(evt, &efi_events, link) {
1462 if (evt->group && !guidcmp(evt->group, guid)) {
1463 efi_signal_event(evt, false);
1464 break;
1465 }
1466 }
1467
Alexander Graf488bf122016-08-19 01:23:24 +02001468 return EFI_SUCCESS;
1469}
1470
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001471/**
Mario Six78a88f72018-07-10 08:40:17 +02001472 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1473 * configuration table.
1474 * @guid: GUID of the installed table
1475 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001476 *
1477 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001478 *
Mario Six78a88f72018-07-10 08:40:17 +02001479 * See the Unified Extensible Firmware Interface (UEFI) specification for
1480 * details.
1481 *
1482 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001483 */
Alexander Graf488bf122016-08-19 01:23:24 +02001484static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1485 void *table)
1486{
Rob Clark778e6af2017-09-13 18:05:41 -04001487 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001488 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001489}
1490
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001491/**
Mario Six78a88f72018-07-10 08:40:17 +02001492 * efi_setup_loaded_image() - initialize a loaded image
1493 * @info: loaded image info to be passed to the entry point of the image
1494 * @obj: internal object associated with the loaded image
1495 * @device_path: device path of the loaded image
1496 * @file_path: file path of the loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001497 *
1498 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001499 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001500 *
Mario Six78a88f72018-07-10 08:40:17 +02001501 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001502 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001503efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1504 struct efi_device_path *file_path,
1505 struct efi_loaded_image_obj **handle_ptr,
1506 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001507{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001508 efi_status_t ret;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001509 struct efi_loaded_image *info;
1510 struct efi_loaded_image_obj *obj;
1511
1512 info = calloc(1, sizeof(*info));
1513 if (!info)
1514 return EFI_OUT_OF_RESOURCES;
1515 obj = calloc(1, sizeof(*obj));
1516 if (!obj) {
1517 free(info);
1518 return EFI_OUT_OF_RESOURCES;
1519 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001520
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001521 /* Add internal object to object list */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001522 efi_add_handle(&obj->parent);
1523
1524 if (info_ptr)
1525 *info_ptr = info;
1526 if (handle_ptr)
1527 *handle_ptr = obj;
Rob Clark95c55532017-09-13 18:05:33 -04001528
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001529 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001530 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001531 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001532
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001533 if (device_path) {
1534 info->device_handle = efi_dp_find_obj(device_path, NULL);
1535 /*
1536 * When asking for the device path interface, return
1537 * bootefi_device_path
1538 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001539 ret = efi_add_protocol(obj->parent.handle,
1540 &efi_guid_device_path, device_path);
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001541 if (ret != EFI_SUCCESS)
1542 goto failure;
1543 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001544
1545 /*
1546 * When asking for the loaded_image interface, just
1547 * return handle which points to loaded_image_info
1548 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001549 ret = efi_add_protocol(obj->parent.handle,
1550 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001551 if (ret != EFI_SUCCESS)
1552 goto failure;
1553
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001554 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001555failure:
1556 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001557 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001558}
1559
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001560/**
Mario Six78a88f72018-07-10 08:40:17 +02001561 * efi_load_image_from_path() - load an image using a file path
1562 * @file_path: the path of the image to load
1563 * @buffer: buffer containing the loaded image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001564 *
Mario Six78a88f72018-07-10 08:40:17 +02001565 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001566 */
Rob Clark9975fe92017-09-13 18:05:38 -04001567efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1568 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001569{
1570 struct efi_file_info *info = NULL;
1571 struct efi_file_handle *f;
1572 static efi_status_t ret;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001573 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001574
1575 f = efi_file_from_path(file_path);
1576 if (!f)
1577 return EFI_DEVICE_ERROR;
1578
1579 bs = 0;
1580 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1581 &bs, info));
1582 if (ret == EFI_BUFFER_TOO_SMALL) {
1583 info = malloc(bs);
1584 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1585 &bs, info));
1586 }
1587 if (ret != EFI_SUCCESS)
1588 goto error;
1589
1590 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1591 if (ret)
1592 goto error;
1593
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001594 bs = info->file_size;
1595 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark838ee4b2017-09-13 18:05:35 -04001596
1597error:
1598 free(info);
1599 EFI_CALL(f->close(f));
1600
1601 if (ret != EFI_SUCCESS) {
1602 efi_free_pool(*buffer);
1603 *buffer = NULL;
1604 }
1605
1606 return ret;
1607}
1608
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001609/**
Mario Six78a88f72018-07-10 08:40:17 +02001610 * efi_load_image() - load an EFI image into memory
1611 * @boot_policy: true for request originating from the boot manager
1612 * @parent_image: the caller's image handle
1613 * @file_path: the path of the image to load
1614 * @source_buffer: memory location from which the image is installed
1615 * @source_size: size of the memory area from which the image is installed
1616 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001617 *
1618 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001619 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001620 * See the Unified Extensible Firmware Interface (UEFI) specification
1621 * for details.
1622 *
Mario Six78a88f72018-07-10 08:40:17 +02001623 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001624 */
Alexander Grafbee91162016-03-04 01:09:59 +01001625static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1626 efi_handle_t parent_image,
1627 struct efi_device_path *file_path,
1628 void *source_buffer,
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001629 efi_uintn_t source_size,
Alexander Grafbee91162016-03-04 01:09:59 +01001630 efi_handle_t *image_handle)
1631{
Tom Rini1c3b2f42018-09-30 10:38:15 -04001632 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001633 struct efi_loaded_image_obj **image_obj =
1634 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001635 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001636
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001637 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001638 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001639
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001640 if (!image_handle || !parent_image) {
1641 ret = EFI_INVALID_PARAMETER;
1642 goto error;
1643 }
1644
1645 if (!source_buffer && !file_path) {
1646 ret = EFI_NOT_FOUND;
1647 goto error;
1648 }
1649
Rob Clark838ee4b2017-09-13 18:05:35 -04001650 if (!source_buffer) {
1651 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001652
Rob Clark9975fe92017-09-13 18:05:38 -04001653 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001654 if (ret != EFI_SUCCESS)
1655 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001656 /*
1657 * split file_path which contains both the device and
1658 * file parts:
1659 */
1660 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001661 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001662 if (ret != EFI_SUCCESS)
1663 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001664 } else {
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001665 /* In this case, file_path is the "device" path, i.e.
Rob Clark838ee4b2017-09-13 18:05:35 -04001666 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1667 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001668 ret = efi_setup_loaded_image(file_path, NULL, image_obj, &info);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001669 if (ret != EFI_SUCCESS)
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001670 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001671 }
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001672 (*image_obj)->entry = efi_load_pe(*image_obj, source_buffer, info);
1673 if (!(*image_obj)->entry) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001674 ret = EFI_UNSUPPORTED;
1675 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001676 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001677 info->system_table = &systab;
1678 info->parent_handle = parent_image;
Alexander Grafbee91162016-03-04 01:09:59 +01001679 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001680failure:
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001681 efi_delete_handle(*image_handle);
1682 *image_handle = NULL;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001683 free(info);
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001684error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001685 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001686}
1687
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001688/**
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001689 * efi_start_image() - call the entry point of an image
Mario Six78a88f72018-07-10 08:40:17 +02001690 * @image_handle: handle of the image
1691 * @exit_data_size: size of the buffer
1692 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001693 *
1694 * This function implements the StartImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001695 *
Mario Six78a88f72018-07-10 08:40:17 +02001696 * See the Unified Extensible Firmware Interface (UEFI) specification for
1697 * details.
1698 *
1699 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001700 */
Alexander Grafbee91162016-03-04 01:09:59 +01001701static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1702 unsigned long *exit_data_size,
1703 s16 **exit_data)
1704{
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001705 struct efi_loaded_image_obj *image_obj =
1706 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001707 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001708
1709 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
Alexander Grafbee91162016-03-04 01:09:59 +01001710
Alexander Grafbee91162016-03-04 01:09:59 +01001711 /* call the image! */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001712 if (setjmp(&image_obj->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001713 /*
1714 * We called the entry point of the child image with EFI_CALL
1715 * in the lines below. The child image called the Exit() boot
1716 * service efi_exit() which executed the long jump that brought
1717 * us to the current line. This implies that the second half
1718 * of the EFI_CALL macro has not been executed.
1719 */
1720#ifdef CONFIG_ARM
1721 /*
1722 * efi_exit() called efi_restore_gd(). We have to undo this
1723 * otherwise __efi_entry_check() will put the wrong value into
1724 * app_gd.
1725 */
1726 gd = app_gd;
1727#endif
1728 /*
1729 * To get ready to call EFI_EXIT below we have to execute the
1730 * missed out steps of EFI_CALL.
1731 */
1732 assert(__efi_entry_check());
1733 debug("%sEFI: %lu returned by started image\n",
1734 __efi_nesting_dec(),
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001735 (unsigned long)((uintptr_t)image_obj->exit_status &
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001736 ~EFI_ERROR_MASK));
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001737 return EFI_EXIT(image_obj->exit_status);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001738 }
1739
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001740 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001741
Alexander Graf56672bf2018-01-26 00:47:53 +01001742 /*
1743 * Usually UEFI applications call Exit() instead of returning.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001744 * But because the world doesn't consist of ponies and unicorns,
Alexander Graf56672bf2018-01-26 00:47:53 +01001745 * we're happy to emulate that behavior on behalf of a payload
1746 * that forgot.
1747 */
1748 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafbee91162016-03-04 01:09:59 +01001749}
1750
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001751/**
Mario Six78a88f72018-07-10 08:40:17 +02001752 * efi_exit() - leave an EFI application or driver
1753 * @image_handle: handle of the application or driver that is exiting
1754 * @exit_status: status code
1755 * @exit_data_size: size of the buffer in bytes
1756 * @exit_data: buffer with data describing an error
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001757 *
1758 * This function implements the Exit service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001759 *
Mario Six78a88f72018-07-10 08:40:17 +02001760 * See the Unified Extensible Firmware Interface (UEFI) specification for
1761 * details.
1762 *
1763 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001764 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001765static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001766 efi_status_t exit_status,
1767 unsigned long exit_data_size,
1768 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001769{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001770 /*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001771 * TODO: We should call the unload procedure of the loaded
1772 * image protocol.
1773 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001774 struct efi_loaded_image_obj *image_obj =
1775 (struct efi_loaded_image_obj *)image_handle;
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001776
Alexander Grafbee91162016-03-04 01:09:59 +01001777 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1778 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001779
Alexander Grafa1489202017-09-03 14:14:17 +02001780 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001781 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001782
Alexander Grafa1489202017-09-03 14:14:17 +02001783 /*
1784 * But longjmp out with the U-Boot gd, not the application's, as
1785 * the other end is a setjmp call inside EFI context.
1786 */
1787 efi_restore_gd();
1788
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001789 image_obj->exit_status = exit_status;
1790 longjmp(&image_obj->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001791
1792 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001793}
1794
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001795/**
Mario Six78a88f72018-07-10 08:40:17 +02001796 * efi_unload_image() - unload an EFI image
1797 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001798 *
1799 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001800 *
Mario Six78a88f72018-07-10 08:40:17 +02001801 * See the Unified Extensible Firmware Interface (UEFI) specification for
1802 * details.
1803 *
1804 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001805 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001806static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001807{
1808 struct efi_object *efiobj;
1809
1810 EFI_ENTRY("%p", image_handle);
1811 efiobj = efi_search_obj(image_handle);
1812 if (efiobj)
1813 list_del(&efiobj->link);
1814
1815 return EFI_EXIT(EFI_SUCCESS);
1816}
1817
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001818/**
Mario Six78a88f72018-07-10 08:40:17 +02001819 * efi_exit_boot_services() - stop all boot services
1820 * @image_handle: handle of the loaded image
1821 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001822 *
1823 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001824 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001825 * See the Unified Extensible Firmware Interface (UEFI) specification
1826 * for details.
1827 *
Mario Six78a88f72018-07-10 08:40:17 +02001828 * All timer events are disabled. For exit boot services events the
1829 * notification function is called. The boot services are disabled in the
1830 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001831 *
Mario Six78a88f72018-07-10 08:40:17 +02001832 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001833 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001834static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001835 unsigned long map_key)
1836{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001837 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001838
Alexander Grafbee91162016-03-04 01:09:59 +01001839 EFI_ENTRY("%p, %ld", image_handle, map_key);
1840
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001841 /* Check that the caller has read the current memory map */
1842 if (map_key != efi_memory_map_key)
1843 return EFI_INVALID_PARAMETER;
1844
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001845 /* Make sure that notification functions are not called anymore */
1846 efi_tpl = TPL_HIGH_LEVEL;
1847
1848 /* Check if ExitBootServices has already been called */
1849 if (!systab.boottime)
1850 return EFI_EXIT(EFI_SUCCESS);
1851
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001852 /* Add related events to the event group */
1853 list_for_each_entry(evt, &efi_events, link) {
1854 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1855 evt->group = &efi_guid_event_group_exit_boot_services;
1856 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001857 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001858 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001859 if (evt->group &&
1860 !guidcmp(evt->group,
1861 &efi_guid_event_group_exit_boot_services)) {
1862 efi_signal_event(evt, false);
1863 break;
1864 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001865 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001866
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001867 /* TODO: Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001868
Alexander Grafb7b84102016-11-17 01:02:57 +01001869 board_quiesce_devices();
1870
Alexander Grafbee91162016-03-04 01:09:59 +01001871 /* This stops all lingering devices */
1872 bootm_disable_interrupts();
1873
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001874 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001875 systab.con_in_handle = NULL;
1876 systab.con_in = NULL;
1877 systab.con_out_handle = NULL;
1878 systab.con_out = NULL;
1879 systab.stderr_handle = NULL;
1880 systab.std_err = NULL;
1881 systab.boottime = NULL;
1882
1883 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001884 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001885
Alexander Grafbee91162016-03-04 01:09:59 +01001886 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001887 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001888 WATCHDOG_RESET();
1889
1890 return EFI_EXIT(EFI_SUCCESS);
1891}
1892
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001893/**
Mario Six78a88f72018-07-10 08:40:17 +02001894 * efi_get_next_monotonic_count() - get next value of the counter
1895 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001896 *
1897 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001898 *
Mario Six78a88f72018-07-10 08:40:17 +02001899 * See the Unified Extensible Firmware Interface (UEFI) specification for
1900 * details.
1901 *
1902 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001903 */
Alexander Grafbee91162016-03-04 01:09:59 +01001904static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1905{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001906 static uint64_t mono;
1907
Alexander Grafbee91162016-03-04 01:09:59 +01001908 EFI_ENTRY("%p", count);
1909 *count = mono++;
1910 return EFI_EXIT(EFI_SUCCESS);
1911}
1912
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001913/**
Mario Six78a88f72018-07-10 08:40:17 +02001914 * efi_stall() - sleep
1915 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001916 *
Mario Six78a88f72018-07-10 08:40:17 +02001917 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001918 *
Mario Six78a88f72018-07-10 08:40:17 +02001919 * See the Unified Extensible Firmware Interface (UEFI) specification for
1920 * details.
1921 *
1922 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001923 */
Alexander Grafbee91162016-03-04 01:09:59 +01001924static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1925{
1926 EFI_ENTRY("%ld", microseconds);
1927 udelay(microseconds);
1928 return EFI_EXIT(EFI_SUCCESS);
1929}
1930
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001931/**
Mario Six78a88f72018-07-10 08:40:17 +02001932 * efi_set_watchdog_timer() - reset the watchdog timer
1933 * @timeout: seconds before reset by watchdog
1934 * @watchdog_code: code to be logged when resetting
1935 * @data_size: size of buffer in bytes
1936 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001937 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001938 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001939 *
Mario Six78a88f72018-07-10 08:40:17 +02001940 * See the Unified Extensible Firmware Interface (UEFI) specification for
1941 * details.
1942 *
1943 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001944 */
Alexander Grafbee91162016-03-04 01:09:59 +01001945static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1946 uint64_t watchdog_code,
1947 unsigned long data_size,
1948 uint16_t *watchdog_data)
1949{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001950 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01001951 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001952 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001953}
1954
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001955/**
Mario Six78a88f72018-07-10 08:40:17 +02001956 * efi_close_protocol() - close a protocol
1957 * @handle: handle on which the protocol shall be closed
1958 * @protocol: GUID of the protocol to close
1959 * @agent_handle: handle of the driver
1960 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001961 *
1962 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001963 *
Mario Six78a88f72018-07-10 08:40:17 +02001964 * See the Unified Extensible Firmware Interface (UEFI) specification for
1965 * details.
1966 *
1967 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001968 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001969static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001970 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001971 efi_handle_t agent_handle,
1972 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001973{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001974 struct efi_handler *handler;
1975 struct efi_open_protocol_info_item *item;
1976 struct efi_open_protocol_info_item *pos;
1977 efi_status_t r;
1978
Rob Clark778e6af2017-09-13 18:05:41 -04001979 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001980 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001981
1982 if (!agent_handle) {
1983 r = EFI_INVALID_PARAMETER;
1984 goto out;
1985 }
1986 r = efi_search_protocol(handle, protocol, &handler);
1987 if (r != EFI_SUCCESS)
1988 goto out;
1989
1990 r = EFI_NOT_FOUND;
1991 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1992 if (item->info.agent_handle == agent_handle &&
1993 item->info.controller_handle == controller_handle) {
1994 efi_delete_open_info(item);
1995 r = EFI_SUCCESS;
1996 break;
1997 }
1998 }
1999out:
2000 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002001}
2002
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002003/**
Mario Six78a88f72018-07-10 08:40:17 +02002004 * efi_open_protocol_information() - provide information about then open status
2005 * of a protocol on a handle
2006 * @handle: handle for which the information shall be retrieved
2007 * @protocol: GUID of the protocol
2008 * @entry_buffer: buffer to receive the open protocol information
2009 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002010 *
2011 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002012 *
Mario Six78a88f72018-07-10 08:40:17 +02002013 * See the Unified Extensible Firmware Interface (UEFI) specification for
2014 * details.
2015 *
2016 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002017 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002018static efi_status_t EFIAPI efi_open_protocol_information(
2019 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002020 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002021 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002022{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002023 unsigned long buffer_size;
2024 unsigned long count;
2025 struct efi_handler *handler;
2026 struct efi_open_protocol_info_item *item;
2027 efi_status_t r;
2028
Rob Clark778e6af2017-09-13 18:05:41 -04002029 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002030 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002031
2032 /* Check parameters */
2033 if (!entry_buffer) {
2034 r = EFI_INVALID_PARAMETER;
2035 goto out;
2036 }
2037 r = efi_search_protocol(handle, protocol, &handler);
2038 if (r != EFI_SUCCESS)
2039 goto out;
2040
2041 /* Count entries */
2042 count = 0;
2043 list_for_each_entry(item, &handler->open_infos, link) {
2044 if (item->info.open_count)
2045 ++count;
2046 }
2047 *entry_count = count;
2048 *entry_buffer = NULL;
2049 if (!count) {
2050 r = EFI_SUCCESS;
2051 goto out;
2052 }
2053
2054 /* Copy entries */
2055 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002056 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002057 (void **)entry_buffer);
2058 if (r != EFI_SUCCESS)
2059 goto out;
2060 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2061 if (item->info.open_count)
2062 (*entry_buffer)[--count] = item->info;
2063 }
2064out:
2065 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002066}
2067
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002068/**
Mario Six78a88f72018-07-10 08:40:17 +02002069 * efi_protocols_per_handle() - get protocols installed on a handle
2070 * @handle: handle for which the information is retrieved
2071 * @protocol_buffer: buffer with protocol GUIDs
2072 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002073 *
2074 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002075 *
Mario Six78a88f72018-07-10 08:40:17 +02002076 * See the Unified Extensible Firmware Interface (UEFI) specification for
2077 * details.
2078 *
2079 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002080 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002081static efi_status_t EFIAPI efi_protocols_per_handle(
2082 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002083 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002084{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002085 unsigned long buffer_size;
2086 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002087 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002088 efi_status_t r;
2089
Alexander Grafbee91162016-03-04 01:09:59 +01002090 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2091 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002092
2093 if (!handle || !protocol_buffer || !protocol_buffer_count)
2094 return EFI_EXIT(EFI_INVALID_PARAMETER);
2095
2096 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002097 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002098
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002099 efiobj = efi_search_obj(handle);
2100 if (!efiobj)
2101 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002102
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002103 /* Count protocols */
2104 list_for_each(protocol_handle, &efiobj->protocols) {
2105 ++*protocol_buffer_count;
2106 }
2107
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002108 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002109 if (*protocol_buffer_count) {
2110 size_t j = 0;
2111
2112 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002113 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002114 (void **)protocol_buffer);
2115 if (r != EFI_SUCCESS)
2116 return EFI_EXIT(r);
2117 list_for_each(protocol_handle, &efiobj->protocols) {
2118 struct efi_handler *protocol;
2119
2120 protocol = list_entry(protocol_handle,
2121 struct efi_handler, link);
2122 (*protocol_buffer)[j] = (void *)protocol->guid;
2123 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002124 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002125 }
2126
2127 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002128}
2129
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002130/**
Mario Six78a88f72018-07-10 08:40:17 +02002131 * efi_locate_handle_buffer() - locate handles implementing a protocol
2132 * @search_type: selection criterion
2133 * @protocol: GUID of the protocol
2134 * @search_key: registration key
2135 * @no_handles: number of returned handles
2136 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002137 *
2138 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002139 *
Mario Six78a88f72018-07-10 08:40:17 +02002140 * See the Unified Extensible Firmware Interface (UEFI) specification for
2141 * details.
2142 *
2143 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002144 */
Alexander Grafbee91162016-03-04 01:09:59 +01002145static efi_status_t EFIAPI efi_locate_handle_buffer(
2146 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002147 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002148 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002149{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002150 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002151 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002152
Rob Clark778e6af2017-09-13 18:05:41 -04002153 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002154 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002155
2156 if (!no_handles || !buffer) {
2157 r = EFI_INVALID_PARAMETER;
2158 goto out;
2159 }
2160 *no_handles = 0;
2161 *buffer = NULL;
2162 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2163 *buffer);
2164 if (r != EFI_BUFFER_TOO_SMALL)
2165 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002166 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002167 (void **)buffer);
2168 if (r != EFI_SUCCESS)
2169 goto out;
2170 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2171 *buffer);
2172 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002173 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002174out:
2175 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002176}
2177
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002178/**
Mario Six78a88f72018-07-10 08:40:17 +02002179 * efi_locate_protocol() - find an interface implementing a protocol
2180 * @protocol: GUID of the protocol
2181 * @registration: registration key passed to the notification function
2182 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002183 *
2184 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002185 *
Mario Six78a88f72018-07-10 08:40:17 +02002186 * See the Unified Extensible Firmware Interface (UEFI) specification for
2187 * details.
2188 *
2189 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002190 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002191static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002192 void *registration,
2193 void **protocol_interface)
2194{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002195 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002196 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002197
Rob Clark778e6af2017-09-13 18:05:41 -04002198 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002199
2200 if (!protocol || !protocol_interface)
2201 return EFI_EXIT(EFI_INVALID_PARAMETER);
2202
2203 list_for_each(lhandle, &efi_obj_list) {
2204 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002205 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002206
2207 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002208
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002209 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2210 if (ret == EFI_SUCCESS) {
2211 *protocol_interface = handler->protocol_interface;
2212 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002213 }
2214 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002215 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002216
2217 return EFI_EXIT(EFI_NOT_FOUND);
2218}
2219
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002220/**
Mario Six78a88f72018-07-10 08:40:17 +02002221 * efi_locate_device_path() - Get the device path and handle of an device
2222 * implementing a protocol
2223 * @protocol: GUID of the protocol
2224 * @device_path: device path
2225 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002226 *
2227 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002228 *
Mario Six78a88f72018-07-10 08:40:17 +02002229 * See the Unified Extensible Firmware Interface (UEFI) specification for
2230 * details.
2231 *
2232 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002233 */
2234static efi_status_t EFIAPI efi_locate_device_path(
2235 const efi_guid_t *protocol,
2236 struct efi_device_path **device_path,
2237 efi_handle_t *device)
2238{
2239 struct efi_device_path *dp;
2240 size_t i;
2241 struct efi_handler *handler;
2242 efi_handle_t *handles;
2243 size_t len, len_dp;
2244 size_t len_best = 0;
2245 efi_uintn_t no_handles;
2246 u8 *remainder;
2247 efi_status_t ret;
2248
2249 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2250
2251 if (!protocol || !device_path || !*device_path || !device) {
2252 ret = EFI_INVALID_PARAMETER;
2253 goto out;
2254 }
2255
2256 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002257 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002258
2259 /* Get all handles implementing the protocol */
2260 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2261 &no_handles, &handles));
2262 if (ret != EFI_SUCCESS)
2263 goto out;
2264
2265 for (i = 0; i < no_handles; ++i) {
2266 /* Find the device path protocol */
2267 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2268 &handler);
2269 if (ret != EFI_SUCCESS)
2270 continue;
2271 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002272 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002273 /*
2274 * This handle can only be a better fit
2275 * if its device path length is longer than the best fit and
2276 * if its device path length is shorter of equal the searched
2277 * device path.
2278 */
2279 if (len_dp <= len_best || len_dp > len)
2280 continue;
2281 /* Check if dp is a subpath of device_path */
2282 if (memcmp(*device_path, dp, len_dp))
2283 continue;
2284 *device = handles[i];
2285 len_best = len_dp;
2286 }
2287 if (len_best) {
2288 remainder = (u8 *)*device_path + len_best;
2289 *device_path = (struct efi_device_path *)remainder;
2290 ret = EFI_SUCCESS;
2291 } else {
2292 ret = EFI_NOT_FOUND;
2293 }
2294out:
2295 return EFI_EXIT(ret);
2296}
2297
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002298/**
Mario Six78a88f72018-07-10 08:40:17 +02002299 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2300 * interfaces
2301 * @handle: handle on which the protocol interfaces shall be installed
2302 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2303 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002304 *
2305 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002306 *
Mario Six78a88f72018-07-10 08:40:17 +02002307 * See the Unified Extensible Firmware Interface (UEFI) specification for
2308 * details.
2309 *
2310 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002311 */
Alexander Grafbee91162016-03-04 01:09:59 +01002312static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2313 void **handle, ...)
2314{
2315 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002316
Alexander Grafbeb077a2018-06-18 17:23:05 +02002317 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002318 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002319 void *protocol_interface;
2320 efi_status_t r = EFI_SUCCESS;
2321 int i = 0;
2322
2323 if (!handle)
2324 return EFI_EXIT(EFI_INVALID_PARAMETER);
2325
Alexander Grafbeb077a2018-06-18 17:23:05 +02002326 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002327 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002328 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002329 if (!protocol)
2330 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002331 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002332 r = EFI_CALL(efi_install_protocol_interface(
2333 handle, protocol,
2334 EFI_NATIVE_INTERFACE,
2335 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002336 if (r != EFI_SUCCESS)
2337 break;
2338 i++;
2339 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002340 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002341 if (r == EFI_SUCCESS)
2342 return EFI_EXIT(r);
2343
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002344 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002345 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002346 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002347 protocol = efi_va_arg(argptr, efi_guid_t*);
2348 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002349 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2350 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002351 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002352 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002353
2354 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002355}
2356
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002357/**
Mario Six78a88f72018-07-10 08:40:17 +02002358 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2359 * interfaces
2360 * @handle: handle from which the protocol interfaces shall be removed
2361 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2362 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002363 *
2364 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002365 *
Mario Six78a88f72018-07-10 08:40:17 +02002366 * See the Unified Extensible Firmware Interface (UEFI) specification for
2367 * details.
2368 *
2369 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002370 */
Alexander Grafbee91162016-03-04 01:09:59 +01002371static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2372 void *handle, ...)
2373{
2374 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002375
Alexander Grafbeb077a2018-06-18 17:23:05 +02002376 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002377 const efi_guid_t *protocol;
2378 void *protocol_interface;
2379 efi_status_t r = EFI_SUCCESS;
2380 size_t i = 0;
2381
2382 if (!handle)
2383 return EFI_EXIT(EFI_INVALID_PARAMETER);
2384
Alexander Grafbeb077a2018-06-18 17:23:05 +02002385 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002386 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002387 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002388 if (!protocol)
2389 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002390 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002391 r = efi_uninstall_protocol(handle, protocol,
2392 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002393 if (r != EFI_SUCCESS)
2394 break;
2395 i++;
2396 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002397 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002398 if (r == EFI_SUCCESS) {
2399 /* If the last protocol has been removed, delete the handle. */
2400 if (list_empty(&handle->protocols)) {
2401 list_del(&handle->link);
2402 free(handle);
2403 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002404 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002405 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002406
2407 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002408 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002409 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002410 protocol = efi_va_arg(argptr, efi_guid_t*);
2411 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002412 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2413 EFI_NATIVE_INTERFACE,
2414 protocol_interface));
2415 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002416 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002417
2418 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002419}
2420
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002421/**
Mario Six78a88f72018-07-10 08:40:17 +02002422 * efi_calculate_crc32() - calculate cyclic redundancy code
2423 * @data: buffer with data
2424 * @data_size: size of buffer in bytes
2425 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002426 *
2427 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002428 *
Mario Six78a88f72018-07-10 08:40:17 +02002429 * See the Unified Extensible Firmware Interface (UEFI) specification for
2430 * details.
2431 *
2432 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002433 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002434static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2435 efi_uintn_t data_size,
2436 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002437{
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002438 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafbee91162016-03-04 01:09:59 +01002439 *crc32_p = crc32(0, data, data_size);
2440 return EFI_EXIT(EFI_SUCCESS);
2441}
2442
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002443/**
Mario Six78a88f72018-07-10 08:40:17 +02002444 * efi_copy_mem() - copy memory
2445 * @destination: destination of the copy operation
2446 * @source: source of the copy operation
2447 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002448 *
2449 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002450 *
Mario Six78a88f72018-07-10 08:40:17 +02002451 * See the Unified Extensible Firmware Interface (UEFI) specification for
2452 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002453 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002454static void EFIAPI efi_copy_mem(void *destination, const void *source,
2455 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002456{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002457 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002458 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002459 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002460}
2461
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002462/**
Mario Six78a88f72018-07-10 08:40:17 +02002463 * efi_set_mem() - Fill memory with a byte value.
2464 * @buffer: buffer to fill
2465 * @size: size of buffer in bytes
2466 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002467 *
2468 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002469 *
Mario Six78a88f72018-07-10 08:40:17 +02002470 * See the Unified Extensible Firmware Interface (UEFI) specification for
2471 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002472 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002473static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002474{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002475 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002476 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002477 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002478}
2479
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002480/**
Mario Six78a88f72018-07-10 08:40:17 +02002481 * efi_protocol_open() - open protocol interface on a handle
2482 * @handler: handler of a protocol
2483 * @protocol_interface: interface implementing the protocol
2484 * @agent_handle: handle of the driver
2485 * @controller_handle: handle of the controller
2486 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002487 *
Mario Six78a88f72018-07-10 08:40:17 +02002488 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002489 */
2490static efi_status_t efi_protocol_open(
2491 struct efi_handler *handler,
2492 void **protocol_interface, void *agent_handle,
2493 void *controller_handle, uint32_t attributes)
2494{
2495 struct efi_open_protocol_info_item *item;
2496 struct efi_open_protocol_info_entry *match = NULL;
2497 bool opened_by_driver = false;
2498 bool opened_exclusive = false;
2499
2500 /* If there is no agent, only return the interface */
2501 if (!agent_handle)
2502 goto out;
2503
2504 /* For TEST_PROTOCOL ignore interface attribute */
2505 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2506 *protocol_interface = NULL;
2507
2508 /*
2509 * Check if the protocol is already opened by a driver with the same
2510 * attributes or opened exclusively
2511 */
2512 list_for_each_entry(item, &handler->open_infos, link) {
2513 if (item->info.agent_handle == agent_handle) {
2514 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2515 (item->info.attributes == attributes))
2516 return EFI_ALREADY_STARTED;
2517 }
2518 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2519 opened_exclusive = true;
2520 }
2521
2522 /* Only one controller can open the protocol exclusively */
2523 if (opened_exclusive && attributes &
2524 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2525 return EFI_ACCESS_DENIED;
2526
2527 /* Prepare exclusive opening */
2528 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2529 /* Try to disconnect controllers */
2530 list_for_each_entry(item, &handler->open_infos, link) {
2531 if (item->info.attributes ==
2532 EFI_OPEN_PROTOCOL_BY_DRIVER)
2533 EFI_CALL(efi_disconnect_controller(
2534 item->info.controller_handle,
2535 item->info.agent_handle,
2536 NULL));
2537 }
2538 opened_by_driver = false;
2539 /* Check if all controllers are disconnected */
2540 list_for_each_entry(item, &handler->open_infos, link) {
2541 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2542 opened_by_driver = true;
2543 }
Heinrich Schuchardt4f37fa42018-09-30 13:40:43 +02002544 /* Only one controller can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002545 if (opened_by_driver)
2546 return EFI_ACCESS_DENIED;
2547 }
2548
2549 /* Find existing entry */
2550 list_for_each_entry(item, &handler->open_infos, link) {
2551 if (item->info.agent_handle == agent_handle &&
2552 item->info.controller_handle == controller_handle)
2553 match = &item->info;
2554 }
2555 /* None found, create one */
2556 if (!match) {
2557 match = efi_create_open_info(handler);
2558 if (!match)
2559 return EFI_OUT_OF_RESOURCES;
2560 }
2561
2562 match->agent_handle = agent_handle;
2563 match->controller_handle = controller_handle;
2564 match->attributes = attributes;
2565 match->open_count++;
2566
2567out:
2568 /* For TEST_PROTOCOL ignore interface attribute. */
2569 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2570 *protocol_interface = handler->protocol_interface;
2571
2572 return EFI_SUCCESS;
2573}
2574
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002575/**
Mario Six78a88f72018-07-10 08:40:17 +02002576 * efi_open_protocol() - open protocol interface on a handle
2577 * @handle: handle on which the protocol shall be opened
2578 * @protocol: GUID of the protocol
2579 * @protocol_interface: interface implementing the protocol
2580 * @agent_handle: handle of the driver
2581 * @controller_handle: handle of the controller
2582 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002583 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002584 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002585 *
Mario Six78a88f72018-07-10 08:40:17 +02002586 * See the Unified Extensible Firmware Interface (UEFI) specification for
2587 * details.
2588 *
2589 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002590 */
Alexander Grafbee91162016-03-04 01:09:59 +01002591static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002592 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002593 void **protocol_interface, void *agent_handle,
2594 void *controller_handle, uint32_t attributes)
2595{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002596 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002597 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002598
Rob Clark778e6af2017-09-13 18:05:41 -04002599 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002600 protocol_interface, agent_handle, controller_handle,
2601 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002602
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002603 if (!handle || !protocol ||
2604 (!protocol_interface && attributes !=
2605 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2606 goto out;
2607 }
2608
2609 switch (attributes) {
2610 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2611 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2612 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2613 break;
2614 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2615 if (controller_handle == handle)
2616 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002617 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002618 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2619 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002620 /* Check that the controller handle is valid */
2621 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002622 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002623 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002624 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002625 /* Check that the agent handle is valid */
2626 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002627 goto out;
2628 break;
2629 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002630 goto out;
2631 }
2632
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002633 r = efi_search_protocol(handle, protocol, &handler);
2634 if (r != EFI_SUCCESS)
2635 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002636
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002637 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2638 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002639out:
2640 return EFI_EXIT(r);
2641}
2642
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002643/**
Mario Six78a88f72018-07-10 08:40:17 +02002644 * efi_handle_protocol() - get interface of a protocol on a handle
2645 * @handle: handle on which the protocol shall be opened
2646 * @protocol: GUID of the protocol
2647 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002648 *
2649 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002650 *
Mario Six78a88f72018-07-10 08:40:17 +02002651 * See the Unified Extensible Firmware Interface (UEFI) specification for
2652 * details.
2653 *
2654 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002655 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002656static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002657 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002658 void **protocol_interface)
2659{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002660 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2661 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002662}
2663
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002664/**
Mario Six78a88f72018-07-10 08:40:17 +02002665 * efi_bind_controller() - bind a single driver to a controller
2666 * @controller_handle: controller handle
2667 * @driver_image_handle: driver handle
2668 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002669 *
Mario Six78a88f72018-07-10 08:40:17 +02002670 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002671 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002672static efi_status_t efi_bind_controller(
2673 efi_handle_t controller_handle,
2674 efi_handle_t driver_image_handle,
2675 struct efi_device_path *remain_device_path)
2676{
2677 struct efi_driver_binding_protocol *binding_protocol;
2678 efi_status_t r;
2679
2680 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2681 &efi_guid_driver_binding_protocol,
2682 (void **)&binding_protocol,
2683 driver_image_handle, NULL,
2684 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2685 if (r != EFI_SUCCESS)
2686 return r;
2687 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2688 controller_handle,
2689 remain_device_path));
2690 if (r == EFI_SUCCESS)
2691 r = EFI_CALL(binding_protocol->start(binding_protocol,
2692 controller_handle,
2693 remain_device_path));
2694 EFI_CALL(efi_close_protocol(driver_image_handle,
2695 &efi_guid_driver_binding_protocol,
2696 driver_image_handle, NULL));
2697 return r;
2698}
2699
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002700/**
Mario Six78a88f72018-07-10 08:40:17 +02002701 * efi_connect_single_controller() - connect a single driver to a controller
2702 * @controller_handle: controller
2703 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002704 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002705 *
Mario Six78a88f72018-07-10 08:40:17 +02002706 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002707 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002708static efi_status_t efi_connect_single_controller(
2709 efi_handle_t controller_handle,
2710 efi_handle_t *driver_image_handle,
2711 struct efi_device_path *remain_device_path)
2712{
2713 efi_handle_t *buffer;
2714 size_t count;
2715 size_t i;
2716 efi_status_t r;
2717 size_t connected = 0;
2718
2719 /* Get buffer with all handles with driver binding protocol */
2720 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2721 &efi_guid_driver_binding_protocol,
2722 NULL, &count, &buffer));
2723 if (r != EFI_SUCCESS)
2724 return r;
2725
2726 /* Context Override */
2727 if (driver_image_handle) {
2728 for (; *driver_image_handle; ++driver_image_handle) {
2729 for (i = 0; i < count; ++i) {
2730 if (buffer[i] == *driver_image_handle) {
2731 buffer[i] = NULL;
2732 r = efi_bind_controller(
2733 controller_handle,
2734 *driver_image_handle,
2735 remain_device_path);
2736 /*
2737 * For drivers that do not support the
2738 * controller or are already connected
2739 * we receive an error code here.
2740 */
2741 if (r == EFI_SUCCESS)
2742 ++connected;
2743 }
2744 }
2745 }
2746 }
2747
2748 /*
2749 * TODO: Some overrides are not yet implemented:
2750 * - Platform Driver Override
2751 * - Driver Family Override Search
2752 * - Bus Specific Driver Override
2753 */
2754
2755 /* Driver Binding Search */
2756 for (i = 0; i < count; ++i) {
2757 if (buffer[i]) {
2758 r = efi_bind_controller(controller_handle,
2759 buffer[i],
2760 remain_device_path);
2761 if (r == EFI_SUCCESS)
2762 ++connected;
2763 }
2764 }
2765
2766 efi_free_pool(buffer);
2767 if (!connected)
2768 return EFI_NOT_FOUND;
2769 return EFI_SUCCESS;
2770}
2771
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002772/**
Mario Six78a88f72018-07-10 08:40:17 +02002773 * efi_connect_controller() - connect a controller to a driver
2774 * @controller_handle: handle of the controller
2775 * @driver_image_handle: handle of the driver
2776 * @remain_device_path: device path of a child controller
2777 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002778 *
2779 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002780 *
2781 * See the Unified Extensible Firmware Interface (UEFI) specification for
2782 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002783 *
2784 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002785 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002786 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2787 *
Mario Six78a88f72018-07-10 08:40:17 +02002788 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002789 */
2790static efi_status_t EFIAPI efi_connect_controller(
2791 efi_handle_t controller_handle,
2792 efi_handle_t *driver_image_handle,
2793 struct efi_device_path *remain_device_path,
2794 bool recursive)
2795{
2796 efi_status_t r;
2797 efi_status_t ret = EFI_NOT_FOUND;
2798 struct efi_object *efiobj;
2799
2800 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2801 remain_device_path, recursive);
2802
2803 efiobj = efi_search_obj(controller_handle);
2804 if (!efiobj) {
2805 ret = EFI_INVALID_PARAMETER;
2806 goto out;
2807 }
2808
2809 r = efi_connect_single_controller(controller_handle,
2810 driver_image_handle,
2811 remain_device_path);
2812 if (r == EFI_SUCCESS)
2813 ret = EFI_SUCCESS;
2814 if (recursive) {
2815 struct efi_handler *handler;
2816 struct efi_open_protocol_info_item *item;
2817
2818 list_for_each_entry(handler, &efiobj->protocols, link) {
2819 list_for_each_entry(item, &handler->open_infos, link) {
2820 if (item->info.attributes &
2821 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2822 r = EFI_CALL(efi_connect_controller(
2823 item->info.controller_handle,
2824 driver_image_handle,
2825 remain_device_path,
2826 recursive));
2827 if (r == EFI_SUCCESS)
2828 ret = EFI_SUCCESS;
2829 }
2830 }
2831 }
2832 }
2833 /* Check for child controller specified by end node */
2834 if (ret != EFI_SUCCESS && remain_device_path &&
2835 remain_device_path->type == DEVICE_PATH_TYPE_END)
2836 ret = EFI_SUCCESS;
2837out:
2838 return EFI_EXIT(ret);
2839}
2840
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002841/**
Mario Six78a88f72018-07-10 08:40:17 +02002842 * efi_reinstall_protocol_interface() - reinstall protocol interface
2843 * @handle: handle on which the protocol shall be reinstalled
2844 * @protocol: GUID of the protocol to be installed
2845 * @old_interface: interface to be removed
2846 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002847 *
2848 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02002849 *
2850 * See the Unified Extensible Firmware Interface (UEFI) specification for
2851 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002852 *
2853 * The old interface is uninstalled. The new interface is installed.
2854 * Drivers are connected.
2855 *
Mario Six78a88f72018-07-10 08:40:17 +02002856 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002857 */
2858static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2859 efi_handle_t handle, const efi_guid_t *protocol,
2860 void *old_interface, void *new_interface)
2861{
2862 efi_status_t ret;
2863
2864 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2865 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002866
2867 /* Uninstall protocol but do not delete handle */
2868 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002869 if (ret != EFI_SUCCESS)
2870 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002871
2872 /* Install the new protocol */
2873 ret = efi_add_protocol(handle, protocol, new_interface);
2874 /*
2875 * The UEFI spec does not specify what should happen to the handle
2876 * if in case of an error no protocol interface remains on the handle.
2877 * So let's do nothing here.
2878 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002879 if (ret != EFI_SUCCESS)
2880 goto out;
2881 /*
2882 * The returned status code has to be ignored.
2883 * Do not create an error if no suitable driver for the handle exists.
2884 */
2885 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2886out:
2887 return EFI_EXIT(ret);
2888}
2889
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002890/**
Mario Six78a88f72018-07-10 08:40:17 +02002891 * efi_get_child_controllers() - get all child controllers associated to a driver
2892 * @efiobj: handle of the controller
2893 * @driver_handle: handle of the driver
2894 * @number_of_children: number of child controllers
2895 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002896 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002897 * The allocated buffer has to be freed with free().
2898 *
Mario Six78a88f72018-07-10 08:40:17 +02002899 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002900 */
2901static efi_status_t efi_get_child_controllers(
2902 struct efi_object *efiobj,
2903 efi_handle_t driver_handle,
2904 efi_uintn_t *number_of_children,
2905 efi_handle_t **child_handle_buffer)
2906{
2907 struct efi_handler *handler;
2908 struct efi_open_protocol_info_item *item;
2909 efi_uintn_t count = 0, i;
2910 bool duplicate;
2911
2912 /* Count all child controller associations */
2913 list_for_each_entry(handler, &efiobj->protocols, link) {
2914 list_for_each_entry(item, &handler->open_infos, link) {
2915 if (item->info.agent_handle == driver_handle &&
2916 item->info.attributes &
2917 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2918 ++count;
2919 }
2920 }
2921 /*
2922 * Create buffer. In case of duplicate child controller assignments
2923 * the buffer will be too large. But that does not harm.
2924 */
2925 *number_of_children = 0;
2926 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2927 if (!*child_handle_buffer)
2928 return EFI_OUT_OF_RESOURCES;
2929 /* Copy unique child handles */
2930 list_for_each_entry(handler, &efiobj->protocols, link) {
2931 list_for_each_entry(item, &handler->open_infos, link) {
2932 if (item->info.agent_handle == driver_handle &&
2933 item->info.attributes &
2934 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2935 /* Check this is a new child controller */
2936 duplicate = false;
2937 for (i = 0; i < *number_of_children; ++i) {
2938 if ((*child_handle_buffer)[i] ==
2939 item->info.controller_handle)
2940 duplicate = true;
2941 }
2942 /* Copy handle to buffer */
2943 if (!duplicate) {
2944 i = (*number_of_children)++;
2945 (*child_handle_buffer)[i] =
2946 item->info.controller_handle;
2947 }
2948 }
2949 }
2950 }
2951 return EFI_SUCCESS;
2952}
2953
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002954/**
Mario Six78a88f72018-07-10 08:40:17 +02002955 * efi_disconnect_controller() - disconnect a controller from a driver
2956 * @controller_handle: handle of the controller
2957 * @driver_image_handle: handle of the driver
2958 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002959 *
2960 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002961 *
Mario Six78a88f72018-07-10 08:40:17 +02002962 * See the Unified Extensible Firmware Interface (UEFI) specification for
2963 * details.
2964 *
2965 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002966 */
2967static efi_status_t EFIAPI efi_disconnect_controller(
2968 efi_handle_t controller_handle,
2969 efi_handle_t driver_image_handle,
2970 efi_handle_t child_handle)
2971{
2972 struct efi_driver_binding_protocol *binding_protocol;
2973 efi_handle_t *child_handle_buffer = NULL;
2974 size_t number_of_children = 0;
2975 efi_status_t r;
2976 size_t stop_count = 0;
2977 struct efi_object *efiobj;
2978
2979 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2980 child_handle);
2981
2982 efiobj = efi_search_obj(controller_handle);
2983 if (!efiobj) {
2984 r = EFI_INVALID_PARAMETER;
2985 goto out;
2986 }
2987
2988 if (child_handle && !efi_search_obj(child_handle)) {
2989 r = EFI_INVALID_PARAMETER;
2990 goto out;
2991 }
2992
2993 /* If no driver handle is supplied, disconnect all drivers */
2994 if (!driver_image_handle) {
2995 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2996 goto out;
2997 }
2998
2999 /* Create list of child handles */
3000 if (child_handle) {
3001 number_of_children = 1;
3002 child_handle_buffer = &child_handle;
3003 } else {
3004 efi_get_child_controllers(efiobj,
3005 driver_image_handle,
3006 &number_of_children,
3007 &child_handle_buffer);
3008 }
3009
3010 /* Get the driver binding protocol */
3011 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3012 &efi_guid_driver_binding_protocol,
3013 (void **)&binding_protocol,
3014 driver_image_handle, NULL,
3015 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3016 if (r != EFI_SUCCESS)
3017 goto out;
3018 /* Remove the children */
3019 if (number_of_children) {
3020 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3021 controller_handle,
3022 number_of_children,
3023 child_handle_buffer));
3024 if (r == EFI_SUCCESS)
3025 ++stop_count;
3026 }
3027 /* Remove the driver */
3028 if (!child_handle)
3029 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3030 controller_handle,
3031 0, NULL));
3032 if (r == EFI_SUCCESS)
3033 ++stop_count;
3034 EFI_CALL(efi_close_protocol(driver_image_handle,
3035 &efi_guid_driver_binding_protocol,
3036 driver_image_handle, NULL));
3037
3038 if (stop_count)
3039 r = EFI_SUCCESS;
3040 else
3041 r = EFI_NOT_FOUND;
3042out:
3043 if (!child_handle)
3044 free(child_handle_buffer);
3045 return EFI_EXIT(r);
3046}
3047
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003048static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003049 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003050 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3051 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003052 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003053 },
3054 .raise_tpl = efi_raise_tpl,
3055 .restore_tpl = efi_restore_tpl,
3056 .allocate_pages = efi_allocate_pages_ext,
3057 .free_pages = efi_free_pages_ext,
3058 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003059 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003060 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003061 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003062 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003063 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003064 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003065 .close_event = efi_close_event,
3066 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003067 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003068 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003069 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003070 .handle_protocol = efi_handle_protocol,
3071 .reserved = NULL,
3072 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003073 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003074 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003075 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003076 .load_image = efi_load_image,
3077 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003078 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003079 .unload_image = efi_unload_image,
3080 .exit_boot_services = efi_exit_boot_services,
3081 .get_next_monotonic_count = efi_get_next_monotonic_count,
3082 .stall = efi_stall,
3083 .set_watchdog_timer = efi_set_watchdog_timer,
3084 .connect_controller = efi_connect_controller,
3085 .disconnect_controller = efi_disconnect_controller,
3086 .open_protocol = efi_open_protocol,
3087 .close_protocol = efi_close_protocol,
3088 .open_protocol_information = efi_open_protocol_information,
3089 .protocols_per_handle = efi_protocols_per_handle,
3090 .locate_handle_buffer = efi_locate_handle_buffer,
3091 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003092 .install_multiple_protocol_interfaces =
3093 efi_install_multiple_protocol_interfaces,
3094 .uninstall_multiple_protocol_interfaces =
3095 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003096 .calculate_crc32 = efi_calculate_crc32,
3097 .copy_mem = efi_copy_mem,
3098 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003099 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003100};
3101
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003102static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003103
Alexander Graf3c63db92016-10-14 13:45:30 +02003104struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003105 .hdr = {
3106 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003107 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003108 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003109 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003110 .fw_vendor = firmware_vendor,
3111 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003112 .con_in = (void *)&efi_con_in,
3113 .con_out = (void *)&efi_con_out,
3114 .std_err = (void *)&efi_con_out,
3115 .runtime = (void *)&efi_runtime_services,
3116 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003117 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003118 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003119};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003120
3121/**
3122 * efi_initialize_system_table() - Initialize system table
3123 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003124 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003125 */
3126efi_status_t efi_initialize_system_table(void)
3127{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003128 efi_status_t ret;
3129
3130 /* Allocate configuration table array */
3131 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3132 EFI_MAX_CONFIGURATION_TABLES *
3133 sizeof(struct efi_configuration_table),
3134 (void **)&systab.tables);
3135
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003136 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003137 efi_update_table_header_crc32(&systab.hdr);
3138 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3139 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003140
3141 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003142}