blob: 1ff0568d47474b854437a9abf4894b0451e6ba9d [file] [log] [blame]
Alexander Grafc15d9212016-03-04 01:09:59 +01001/*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Grafc15d9212016-03-04 01:09:59 +01009#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010011#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040012#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <malloc.h>
14#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <u-boot/crc.h>
17#include <bootm.h>
18#include <inttypes.h>
19#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020025
Alexander Grafc15d9212016-03-04 01:09:59 +010026/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010030LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010031
Alexander Grafc15d9212016-03-04 01:09:59 +010032/*
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
37 */
38static bool efi_is_direct_boot = true;
39
40/*
41 * EFI can pass arbitrary additional "tables" containing vendor specific
42 * information to the payload. One such table is the FDT table which contains
43 * a pointer to a flattened device tree blob.
44 *
45 * In most cases we want to pass an FDT to the payload, so reserve one slot of
46 * config table space for it. The pointer gets populated by do_bootefi_exec().
47 */
Alexander Graf393dd912016-10-14 13:45:30 +020048static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafc15d9212016-03-04 01:09:59 +010049
Simon Glasscdfe6962016-09-25 15:27:35 -060050#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010051/*
52 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
53 * fixed when compiling U-Boot. However, the payload does not know about that
54 * restriction so we need to manually swap its and our view of that register on
55 * EFI callback entry/exit.
56 */
57static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060058#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010059
Rob Clark86789d52017-07-27 08:04:18 -040060static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040061static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010062/* GUID of the device tree table */
63const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010064/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
65const efi_guid_t efi_guid_driver_binding_protocol =
66 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040067
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010068/* event group ExitBootServices() invoked */
69const efi_guid_t efi_guid_event_group_exit_boot_services =
70 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
71/* event group SetVirtualAddressMap() invoked */
72const efi_guid_t efi_guid_event_group_virtual_address_change =
73 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
74/* event group memory map changed */
75const efi_guid_t efi_guid_event_group_memory_map_change =
76 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
77/* event group boot manager about to boot */
78const efi_guid_t efi_guid_event_group_ready_to_boot =
79 EFI_EVENT_GROUP_READY_TO_BOOT;
80/* event group ResetSystem() invoked (before ExitBootServices) */
81const efi_guid_t efi_guid_event_group_reset_system =
82 EFI_EVENT_GROUP_RESET_SYSTEM;
83
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010084static efi_status_t EFIAPI efi_disconnect_controller(
85 efi_handle_t controller_handle,
86 efi_handle_t driver_image_handle,
87 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010088
Rob Clark86789d52017-07-27 08:04:18 -040089/* Called on every callback entry */
90int __efi_entry_check(void)
91{
92 int ret = entry_count++ == 0;
93#ifdef CONFIG_ARM
94 assert(efi_gd);
95 app_gd = gd;
96 gd = efi_gd;
97#endif
98 return ret;
99}
100
101/* Called on every callback exit */
102int __efi_exit_check(void)
103{
104 int ret = --entry_count == 0;
105#ifdef CONFIG_ARM
106 gd = app_gd;
107#endif
108 return ret;
109}
110
Alexander Grafc15d9212016-03-04 01:09:59 +0100111/* Called from do_bootefi_exec() */
112void efi_save_gd(void)
113{
Simon Glasscdfe6962016-09-25 15:27:35 -0600114#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100115 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600116#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100117}
118
Rob Clark86789d52017-07-27 08:04:18 -0400119/*
120 * Special case handler for error/abort that just forces things back
121 * to u-boot world so we can dump out an abort msg, without any care
122 * about returning back to UEFI world.
123 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100124void efi_restore_gd(void)
125{
Simon Glasscdfe6962016-09-25 15:27:35 -0600126#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100127 /* Only restore if we're already in EFI context */
128 if (!efi_gd)
129 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100130 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600131#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100132}
133
Rob Clarke7896c32017-07-27 08:04:19 -0400134/*
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100135 * Return a string for indenting with two spaces per level. A maximum of ten
136 * indent levels is supported. Higher indent levels will be truncated.
137 *
138 * @level indent level
139 * @return indent string
Rob Clarke7896c32017-07-27 08:04:19 -0400140 */
141static const char *indent_string(int level)
142{
143 const char *indent = " ";
144 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100145
Rob Clarke7896c32017-07-27 08:04:19 -0400146 level = min(max, level * 2);
147 return &indent[max - level];
148}
149
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200150const char *__efi_nesting(void)
151{
152 return indent_string(nesting_level);
153}
154
Rob Clarke7896c32017-07-27 08:04:19 -0400155const char *__efi_nesting_inc(void)
156{
157 return indent_string(nesting_level++);
158}
159
160const char *__efi_nesting_dec(void)
161{
162 return indent_string(--nesting_level);
163}
164
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200165/*
166 * Queue an EFI event.
167 *
168 * This function queues the notification function of the event for future
169 * execution.
170 *
171 * The notification function is called if the task priority level of the
172 * event is higher than the current task priority level.
173 *
174 * For the SignalEvent service see efi_signal_event_ext.
175 *
176 * @event event to signal
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100177 * @check_tpl check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200178 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100179static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200180{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200181 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200182 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200183 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100184 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200185 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200186 EFI_CALL_VOID(event->notify_function(event,
187 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200188 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200189 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200190}
191
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200192/*
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100193 * Signal an EFI event.
194 *
195 * This function signals an event. If the event belongs to an event group
196 * all events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
197 * their notification function is queued.
198 *
199 * For the SignalEvent service see efi_signal_event_ext.
200 *
201 * @event event to signal
202 * @check_tpl check the TPL level
203 */
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
236/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200237 * Raise the task priority level.
238 *
239 * This function implements the RaiseTpl service.
240 * See the Unified Extensible Firmware Interface (UEFI) specification
241 * for details.
242 *
243 * @new_tpl new value of the task priority level
244 * @return old value of the task priority level
245 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100246static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100247{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100248 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200249
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200250 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200251
252 if (new_tpl < efi_tpl)
253 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
254 efi_tpl = new_tpl;
255 if (efi_tpl > TPL_HIGH_LEVEL)
256 efi_tpl = TPL_HIGH_LEVEL;
257
258 EFI_EXIT(EFI_SUCCESS);
259 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100260}
261
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200262/*
263 * Lower the task priority level.
264 *
265 * This function implements the RestoreTpl service.
266 * See the Unified Extensible Firmware Interface (UEFI) specification
267 * for details.
268 *
269 * @old_tpl value of the task priority level to be restored
270 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100271static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100272{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200273 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200274
275 if (old_tpl > efi_tpl)
276 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
277 efi_tpl = old_tpl;
278 if (efi_tpl > TPL_HIGH_LEVEL)
279 efi_tpl = TPL_HIGH_LEVEL;
280
281 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100282}
283
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200284/*
285 * Allocate memory pages.
286 *
287 * This function implements the AllocatePages service.
288 * See the Unified Extensible Firmware Interface (UEFI) specification
289 * for details.
290 *
291 * @type type of allocation to be performed
292 * @memory_type usage type of the allocated memory
293 * @pages number of pages to be allocated
294 * @memory allocated memory
295 * @return status code
296 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900297static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100298 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900299 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100300{
301 efi_status_t r;
302
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100303 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100304 r = efi_allocate_pages(type, memory_type, pages, memory);
305 return EFI_EXIT(r);
306}
307
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200308/*
309 * Free memory pages.
310 *
311 * This function implements the FreePages service.
312 * See the Unified Extensible Firmware Interface (UEFI) specification
313 * for details.
314 *
315 * @memory start of the memory area to be freed
316 * @pages number of pages to be freed
317 * @return status code
318 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900319static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100320 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100321{
322 efi_status_t r;
323
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100324 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100325 r = efi_free_pages(memory, pages);
326 return EFI_EXIT(r);
327}
328
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200329/*
330 * Get map describing memory usage.
331 *
332 * This function implements the GetMemoryMap service.
333 * See the Unified Extensible Firmware Interface (UEFI) specification
334 * for details.
335 *
336 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
337 * on exit the size of the copied memory map
338 * @memory_map buffer to which the memory map is written
339 * @map_key key for the memory map
340 * @descriptor_size size of an individual memory descriptor
341 * @descriptor_version version number of the memory descriptor structure
342 * @return status code
343 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900344static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100345 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900346 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100347 efi_uintn_t *map_key,
348 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900349 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100350{
351 efi_status_t r;
352
353 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
354 map_key, descriptor_size, descriptor_version);
355 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
356 descriptor_size, descriptor_version);
357 return EFI_EXIT(r);
358}
359
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200360/*
361 * Allocate memory from pool.
362 *
363 * This function implements the AllocatePool service.
364 * See the Unified Extensible Firmware Interface (UEFI) specification
365 * for details.
366 *
367 * @pool_type type of the pool from which memory is to be allocated
368 * @size number of bytes to be allocated
369 * @buffer allocated memory
370 * @return status code
371 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200372static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100373 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200374 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100375{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100376 efi_status_t r;
377
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100378 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200379 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100380 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100381}
382
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200383/*
384 * Free memory from pool.
385 *
386 * This function implements the FreePool service.
387 * See the Unified Extensible Firmware Interface (UEFI) specification
388 * for details.
389 *
390 * @buffer start of memory to be freed
391 * @return status code
392 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200393static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100394{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100395 efi_status_t r;
396
397 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200398 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100399 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100400}
401
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200402/*
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100403 * Add a new object to the object list.
404 *
405 * The protocols list is initialized.
406 * The object handle is set.
407 *
408 * @obj object to be added
409 */
410void efi_add_handle(struct efi_object *obj)
411{
412 if (!obj)
413 return;
414 INIT_LIST_HEAD(&obj->protocols);
415 obj->handle = obj;
416 list_add_tail(&obj->link, &efi_obj_list);
417}
418
419/*
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200420 * Create handle.
421 *
422 * @handle new handle
423 * @return status code
424 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100425efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200426{
427 struct efi_object *obj;
428 efi_status_t r;
429
430 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
431 sizeof(struct efi_object),
432 (void **)&obj);
433 if (r != EFI_SUCCESS)
434 return r;
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100435 efi_add_handle(obj);
436 *handle = obj->handle;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200437 return r;
438}
439
Alexander Grafc15d9212016-03-04 01:09:59 +0100440/*
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100441 * Find a protocol on a handle.
442 *
443 * @handle handle
444 * @protocol_guid GUID of the protocol
445 * @handler reference to the protocol
446 * @return status code
447 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100448efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100449 const efi_guid_t *protocol_guid,
450 struct efi_handler **handler)
451{
452 struct efi_object *efiobj;
453 struct list_head *lhandle;
454
455 if (!handle || !protocol_guid)
456 return EFI_INVALID_PARAMETER;
457 efiobj = efi_search_obj(handle);
458 if (!efiobj)
459 return EFI_INVALID_PARAMETER;
460 list_for_each(lhandle, &efiobj->protocols) {
461 struct efi_handler *protocol;
462
463 protocol = list_entry(lhandle, struct efi_handler, link);
464 if (!guidcmp(protocol->guid, protocol_guid)) {
465 if (handler)
466 *handler = protocol;
467 return EFI_SUCCESS;
468 }
469 }
470 return EFI_NOT_FOUND;
471}
472
473/*
474 * Delete protocol from a handle.
475 *
476 * @handle handle from which the protocol shall be deleted
477 * @protocol GUID of the protocol to be deleted
478 * @protocol_interface interface of the protocol implementation
479 * @return status code
480 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100481efi_status_t efi_remove_protocol(const efi_handle_t handle,
482 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100483 void *protocol_interface)
484{
485 struct efi_handler *handler;
486 efi_status_t ret;
487
488 ret = efi_search_protocol(handle, protocol, &handler);
489 if (ret != EFI_SUCCESS)
490 return ret;
491 if (guidcmp(handler->guid, protocol))
492 return EFI_INVALID_PARAMETER;
493 list_del(&handler->link);
494 free(handler);
495 return EFI_SUCCESS;
496}
497
498/*
499 * Delete all protocols from a handle.
500 *
501 * @handle handle from which the protocols shall be deleted
502 * @return status code
503 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100504efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100505{
506 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100507 struct efi_handler *protocol;
508 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100509
510 efiobj = efi_search_obj(handle);
511 if (!efiobj)
512 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100513 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100514 efi_status_t ret;
515
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100516 ret = efi_remove_protocol(handle, protocol->guid,
517 protocol->protocol_interface);
518 if (ret != EFI_SUCCESS)
519 return ret;
520 }
521 return EFI_SUCCESS;
522}
523
524/*
525 * Delete handle.
526 *
527 * @handle handle to delete
528 */
529void efi_delete_handle(struct efi_object *obj)
530{
531 if (!obj)
532 return;
533 efi_remove_all_protocols(obj->handle);
534 list_del(&obj->link);
535 free(obj);
536}
537
538/*
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100539 * Check if a pointer is a valid event.
540 *
541 * @event pointer to check
542 * @return status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100543 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100544static efi_status_t efi_is_event(const struct efi_event *event)
545{
546 const struct efi_event *evt;
547
548 if (!event)
549 return EFI_INVALID_PARAMETER;
550 list_for_each_entry(evt, &efi_events, link) {
551 if (evt == event)
552 return EFI_SUCCESS;
553 }
554 return EFI_INVALID_PARAMETER;
555}
Alexander Grafc15d9212016-03-04 01:09:59 +0100556
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200557/*
558 * Create an event.
559 *
560 * This function is used inside U-Boot code to create an event.
561 *
562 * For the API function implementing the CreateEvent service see
563 * efi_create_event_ext.
564 *
565 * @type type of the event to create
566 * @notify_tpl task priority level of the event
567 * @notify_function notification function of the event
568 * @notify_context pointer passed to the notification function
569 * @event created event
570 * @return status code
571 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100572efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200573 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200574 struct efi_event *event,
575 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100576 void *notify_context, efi_guid_t *group,
577 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100578{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100579 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200580
Jonathan Gray7758b212017-03-12 19:26:07 +1100581 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200582 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100583
584 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200585 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100586
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100587 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
Jonathan Gray7758b212017-03-12 19:26:07 +1100588 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200589 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100590
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100591 evt = calloc(1, sizeof(struct efi_event));
592 if (!evt)
593 return EFI_OUT_OF_RESOURCES;
594 evt->type = type;
595 evt->notify_tpl = notify_tpl;
596 evt->notify_function = notify_function;
597 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100598 evt->group = group;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100599 /* Disable timers on bootup */
600 evt->trigger_next = -1ULL;
601 evt->is_queued = false;
602 evt->is_signaled = false;
603 list_add_tail(&evt->link, &efi_events);
604 *event = evt;
605 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100606}
607
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200608/*
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100609 * Create an event in a group.
610 *
611 * This function implements the CreateEventEx service.
612 * See the Unified Extensible Firmware Interface (UEFI) specification
613 * for details.
614 * TODO: Support event groups
615 *
616 * @type type of the event to create
617 * @notify_tpl task priority level of the event
618 * @notify_function notification function of the event
619 * @notify_context pointer passed to the notification function
620 * @event created event
621 * @event_group event group
622 * @return status code
623 */
624efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
625 void (EFIAPI *notify_function) (
626 struct efi_event *event,
627 void *context),
628 void *notify_context,
629 efi_guid_t *event_group,
630 struct efi_event **event)
631{
632 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
633 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100634 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100635 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100636}
637
638/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200639 * Create an event.
640 *
641 * This function implements the CreateEvent service.
642 * See the Unified Extensible Firmware Interface (UEFI) specification
643 * for details.
644 *
645 * @type type of the event to create
646 * @notify_tpl task priority level of the event
647 * @notify_function notification function of the event
648 * @notify_context pointer passed to the notification function
649 * @event created event
650 * @return status code
651 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200652static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100653 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200654 void (EFIAPI *notify_function) (
655 struct efi_event *event,
656 void *context),
657 void *notify_context, struct efi_event **event)
658{
659 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
660 notify_context);
661 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100662 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200663}
664
Alexander Grafc15d9212016-03-04 01:09:59 +0100665/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200666 * Check if a timer event has occurred or a queued notification function should
667 * be called.
668 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100669 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200670 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100671 */
672void efi_timer_check(void)
673{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100674 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100675 u64 now = timer_get_us();
676
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100677 list_for_each_entry(evt, &efi_events, link) {
678 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100679 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100680 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200681 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100682 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200683 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100684 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200685 break;
686 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100687 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200688 break;
689 default:
690 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200691 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100692 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100693 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100694 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100695 WATCHDOG_RESET();
696}
697
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200698/*
699 * Set the trigger time for a timer event or stop the event.
700 *
701 * This is the function for internal usage in U-Boot. For the API function
702 * implementing the SetTimer service see efi_set_timer_ext.
703 *
704 * @event event for which the timer is set
705 * @type type of the timer
706 * @trigger_time trigger period in multiples of 100ns
707 * @return status code
708 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200709efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200710 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100711{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100712 /* Check that the event is valid */
713 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
714 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100715
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200716 /*
717 * The parameter defines a multiple of 100ns.
718 * We use multiples of 1000ns. So divide by 10.
719 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200720 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100721
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100722 switch (type) {
723 case EFI_TIMER_STOP:
724 event->trigger_next = -1ULL;
725 break;
726 case EFI_TIMER_PERIODIC:
727 case EFI_TIMER_RELATIVE:
728 event->trigger_next = timer_get_us() + trigger_time;
729 break;
730 default:
731 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100732 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100733 event->trigger_type = type;
734 event->trigger_time = trigger_time;
735 event->is_signaled = false;
736 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100737}
738
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200739/*
740 * Set the trigger time for a timer event or stop the event.
741 *
742 * This function implements the SetTimer service.
743 * See the Unified Extensible Firmware Interface (UEFI) specification
744 * for details.
745 *
746 * @event event for which the timer is set
747 * @type type of the timer
748 * @trigger_time trigger period in multiples of 100ns
749 * @return status code
750 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200751static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
752 enum efi_timer_delay type,
753 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200754{
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100755 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200756 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
757}
758
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200759/*
760 * Wait for events to be signaled.
761 *
762 * This function implements the WaitForEvent service.
763 * See the Unified Extensible Firmware Interface (UEFI) specification
764 * for details.
765 *
766 * @num_events number of events to be waited for
767 * @events events to be waited for
768 * @index index of the event that was signaled
769 * @return status code
770 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100771static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200772 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100773 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100774{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100775 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100776
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100777 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100778
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200779 /* Check parameters */
780 if (!num_events || !event)
781 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200782 /* Check TPL */
783 if (efi_tpl != TPL_APPLICATION)
784 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200785 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100786 if (efi_is_event(event[i]) != EFI_SUCCESS)
787 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200788 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
789 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200790 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100791 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200792 }
793
794 /* Wait for signal */
795 for (;;) {
796 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200797 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200798 goto out;
799 }
800 /* Allow events to occur. */
801 efi_timer_check();
802 }
803
804out:
805 /*
806 * Reset the signal which is passed to the caller to allow periodic
807 * events to occur.
808 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200809 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200810 if (index)
811 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100812
813 return EFI_EXIT(EFI_SUCCESS);
814}
815
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200816/*
817 * Signal an EFI event.
818 *
819 * This function implements the SignalEvent service.
820 * See the Unified Extensible Firmware Interface (UEFI) specification
821 * for details.
822 *
823 * This functions sets the signaled state of the event and queues the
824 * notification function for execution.
825 *
826 * @event event to signal
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +0200827 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200828 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200829static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100830{
831 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100832 if (efi_is_event(event) != EFI_SUCCESS)
833 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100834 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100835 return EFI_EXIT(EFI_SUCCESS);
836}
837
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200838/*
839 * Close an EFI event.
840 *
841 * This function implements the CloseEvent service.
842 * See the Unified Extensible Firmware Interface (UEFI) specification
843 * for details.
844 *
845 * @event event to close
846 * @return status code
847 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200848static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100849{
850 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100851 if (efi_is_event(event) != EFI_SUCCESS)
852 return EFI_EXIT(EFI_INVALID_PARAMETER);
853 list_del(&event->link);
854 free(event);
855 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100856}
857
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200858/*
859 * Check if an event is signaled.
860 *
861 * This function implements the CheckEvent service.
862 * See the Unified Extensible Firmware Interface (UEFI) specification
863 * for details.
864 *
Heinrich Schuchardtf375f372018-02-18 11:32:02 +0100865 * If an event is not signaled yet, the notification function is queued.
866 * The signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200867 *
868 * @event event to check
869 * @return status code
870 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200871static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100872{
873 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200874 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100875 if (efi_is_event(event) != EFI_SUCCESS ||
876 event->type & EVT_NOTIFY_SIGNAL)
877 return EFI_EXIT(EFI_INVALID_PARAMETER);
878 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100879 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100880 if (event->is_signaled) {
881 event->is_signaled = false;
882 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200883 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100884 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100885}
886
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200887/*
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200888 * Find the internal EFI object for a handle.
889 *
890 * @handle handle to find
891 * @return EFI object
892 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100893struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200894{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100895 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200896
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100897 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200898 if (efiobj->handle == handle)
899 return efiobj;
900 }
901
902 return NULL;
903}
904
905/*
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100906 * Create open protocol info entry and add it to a protocol.
907 *
908 * @handler handler of a protocol
909 * @return open protocol info entry
910 */
911static struct efi_open_protocol_info_entry *efi_create_open_info(
912 struct efi_handler *handler)
913{
914 struct efi_open_protocol_info_item *item;
915
916 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
917 if (!item)
918 return NULL;
919 /* Append the item to the open protocol info list. */
920 list_add_tail(&item->link, &handler->open_infos);
921
922 return &item->info;
923}
924
925/*
926 * Remove an open protocol info entry from a protocol.
927 *
928 * @handler handler of a protocol
929 * @return status code
930 */
931static efi_status_t efi_delete_open_info(
932 struct efi_open_protocol_info_item *item)
933{
934 list_del(&item->link);
935 free(item);
936 return EFI_SUCCESS;
937}
938
939/*
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200940 * Install new protocol on a handle.
941 *
942 * @handle handle on which the protocol shall be installed
943 * @protocol GUID of the protocol to be installed
944 * @protocol_interface interface of the protocol implementation
945 * @return status code
946 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100947efi_status_t efi_add_protocol(const efi_handle_t handle,
948 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200949 void *protocol_interface)
950{
951 struct efi_object *efiobj;
952 struct efi_handler *handler;
953 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200954
955 efiobj = efi_search_obj(handle);
956 if (!efiobj)
957 return EFI_INVALID_PARAMETER;
958 ret = efi_search_protocol(handle, protocol, NULL);
959 if (ret != EFI_NOT_FOUND)
960 return EFI_INVALID_PARAMETER;
961 handler = calloc(1, sizeof(struct efi_handler));
962 if (!handler)
963 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100964 handler->guid = protocol;
965 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100966 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100967 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +0100968 if (!guidcmp(&efi_guid_device_path, protocol))
969 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100970 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200971}
972
973/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200974 * Install protocol interface.
975 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100976 * This function implements the InstallProtocolInterface service.
977 * See the Unified Extensible Firmware Interface (UEFI) specification
978 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200979 *
980 * @handle handle on which the protocol shall be installed
981 * @protocol GUID of the protocol to be installed
982 * @protocol_interface_type type of the interface to be installed,
983 * always EFI_NATIVE_INTERFACE
984 * @protocol_interface interface of the protocol implementation
985 * @return status code
986 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100987static efi_status_t EFIAPI efi_install_protocol_interface(
988 void **handle, const efi_guid_t *protocol,
989 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100990{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200991 efi_status_t r;
992
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100993 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
994 protocol_interface);
995
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200996 if (!handle || !protocol ||
997 protocol_interface_type != EFI_NATIVE_INTERFACE) {
998 r = EFI_INVALID_PARAMETER;
999 goto out;
1000 }
1001
1002 /* Create new handle if requested. */
1003 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001004 r = efi_create_handle(handle);
1005 if (r != EFI_SUCCESS)
1006 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001007 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1008 *handle);
1009 } else {
1010 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1011 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001012 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001013 /* Add new protocol */
1014 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001015out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001016 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001017}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001018
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001019/*
1020 * Reinstall protocol interface.
1021 *
1022 * This function implements the ReinstallProtocolInterface service.
1023 * See the Unified Extensible Firmware Interface (UEFI) specification
1024 * for details.
1025 *
1026 * @handle handle on which the protocol shall be
1027 * reinstalled
1028 * @protocol GUID of the protocol to be installed
1029 * @old_interface interface to be removed
1030 * @new_interface interface to be installed
1031 * @return status code
1032 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001033static efi_status_t EFIAPI efi_reinstall_protocol_interface(
1034 efi_handle_t handle, const efi_guid_t *protocol,
1035 void *old_interface, void *new_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001036{
Rob Clark238f88c2017-09-13 18:05:41 -04001037 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01001038 new_interface);
1039 return EFI_EXIT(EFI_ACCESS_DENIED);
1040}
1041
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001042/*
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001043 * Get all drivers associated to a controller.
1044 * The allocated buffer has to be freed with free().
1045 *
1046 * @efiobj handle of the controller
1047 * @protocol protocol guid (optional)
1048 * @number_of_drivers number of child controllers
1049 * @driver_handle_buffer handles of the the drivers
1050 * @return status code
1051 */
1052static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1053 const efi_guid_t *protocol,
1054 efi_uintn_t *number_of_drivers,
1055 efi_handle_t **driver_handle_buffer)
1056{
1057 struct efi_handler *handler;
1058 struct efi_open_protocol_info_item *item;
1059 efi_uintn_t count = 0, i;
1060 bool duplicate;
1061
1062 /* Count all driver associations */
1063 list_for_each_entry(handler, &efiobj->protocols, link) {
1064 if (protocol && guidcmp(handler->guid, protocol))
1065 continue;
1066 list_for_each_entry(item, &handler->open_infos, link) {
1067 if (item->info.attributes &
1068 EFI_OPEN_PROTOCOL_BY_DRIVER)
1069 ++count;
1070 }
1071 }
1072 /*
1073 * Create buffer. In case of duplicate driver assignments the buffer
1074 * will be too large. But that does not harm.
1075 */
1076 *number_of_drivers = 0;
1077 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1078 if (!*driver_handle_buffer)
1079 return EFI_OUT_OF_RESOURCES;
1080 /* Collect unique driver handles */
1081 list_for_each_entry(handler, &efiobj->protocols, link) {
1082 if (protocol && guidcmp(handler->guid, protocol))
1083 continue;
1084 list_for_each_entry(item, &handler->open_infos, link) {
1085 if (item->info.attributes &
1086 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1087 /* Check this is a new driver */
1088 duplicate = false;
1089 for (i = 0; i < *number_of_drivers; ++i) {
1090 if ((*driver_handle_buffer)[i] ==
1091 item->info.agent_handle)
1092 duplicate = true;
1093 }
1094 /* Copy handle to buffer */
1095 if (!duplicate) {
1096 i = (*number_of_drivers)++;
1097 (*driver_handle_buffer)[i] =
1098 item->info.agent_handle;
1099 }
1100 }
1101 }
1102 }
1103 return EFI_SUCCESS;
1104}
1105
1106/*
1107 * Disconnect all drivers from a controller.
1108 *
1109 * This function implements the DisconnectController service.
1110 * See the Unified Extensible Firmware Interface (UEFI) specification
1111 * for details.
1112 *
1113 * @efiobj handle of the controller
1114 * @protocol protocol guid (optional)
1115 * @child_handle handle of the child to destroy
1116 * @return status code
1117 */
1118static efi_status_t efi_disconnect_all_drivers(
1119 struct efi_object *efiobj,
1120 const efi_guid_t *protocol,
1121 efi_handle_t child_handle)
1122{
1123 efi_uintn_t number_of_drivers;
1124 efi_handle_t *driver_handle_buffer;
1125 efi_status_t r, ret;
1126
1127 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1128 &driver_handle_buffer);
1129 if (ret != EFI_SUCCESS)
1130 return ret;
1131
1132 ret = EFI_NOT_FOUND;
1133 while (number_of_drivers) {
1134 r = EFI_CALL(efi_disconnect_controller(
1135 efiobj->handle,
1136 driver_handle_buffer[--number_of_drivers],
1137 child_handle));
1138 if (r == EFI_SUCCESS)
1139 ret = r;
1140 }
1141 free(driver_handle_buffer);
1142 return ret;
1143}
1144
1145/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001146 * Uninstall protocol interface.
1147 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001148 * This function implements the UninstallProtocolInterface service.
1149 * See the Unified Extensible Firmware Interface (UEFI) specification
1150 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001151 *
1152 * @handle handle from which the protocol shall be removed
1153 * @protocol GUID of the protocol to be removed
1154 * @protocol_interface interface to be removed
1155 * @return status code
1156 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001157static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001158 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001159 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001160{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001161 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001162 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001163 struct efi_open_protocol_info_item *item;
1164 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001165 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001166
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001167 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1168
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001169 /* Check handle */
1170 efiobj = efi_search_obj(handle);
1171 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001172 r = EFI_INVALID_PARAMETER;
1173 goto out;
1174 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001175 /* Find the protocol on the handle */
1176 r = efi_search_protocol(handle, protocol, &handler);
1177 if (r != EFI_SUCCESS)
1178 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001179 /* Disconnect controllers */
1180 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1181 if (!list_empty(&handler->open_infos)) {
1182 r = EFI_ACCESS_DENIED;
1183 goto out;
1184 }
1185 /* Close protocol */
1186 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1187 if (item->info.attributes ==
1188 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1189 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1190 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1191 list_del(&item->link);
1192 }
1193 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001194 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001195 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001196 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001197 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001198out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001199 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001200}
1201
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001202/*
1203 * Register an event for notification when a protocol is installed.
1204 *
1205 * This function implements the RegisterProtocolNotify service.
1206 * See the Unified Extensible Firmware Interface (UEFI) specification
1207 * for details.
1208 *
1209 * @protocol GUID of the protocol whose installation shall be
1210 * notified
1211 * @event event to be signaled upon installation of the protocol
1212 * @registration key for retrieving the registration information
1213 * @return status code
1214 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001215static efi_status_t EFIAPI efi_register_protocol_notify(
1216 const efi_guid_t *protocol,
1217 struct efi_event *event,
1218 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001219{
Rob Clark238f88c2017-09-13 18:05:41 -04001220 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001221 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1222}
1223
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001224/*
1225 * Determine if an EFI handle implements a protocol.
1226 *
1227 * See the documentation of the LocateHandle service in the UEFI specification.
1228 *
1229 * @search_type selection criterion
1230 * @protocol GUID of the protocol
1231 * @search_key registration key
1232 * @efiobj handle
1233 * @return 0 if the handle implements the protocol
1234 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001235static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001236 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001237 struct efi_object *efiobj)
1238{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001239 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001240
1241 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001242 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001243 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001244 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001245 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001246 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001247 case BY_PROTOCOL:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001248 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1249 return (ret != EFI_SUCCESS);
1250 default:
1251 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001252 return -1;
1253 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001254}
1255
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001256/*
1257 * Locate handles implementing a protocol.
1258 *
1259 * This function is meant for U-Boot internal calls. For the API implementation
1260 * of the LocateHandle service see efi_locate_handle_ext.
1261 *
1262 * @search_type selection criterion
1263 * @protocol GUID of the protocol
1264 * @search_key registration key
1265 * @buffer_size size of the buffer to receive the handles in bytes
1266 * @buffer buffer to receive the relevant handles
1267 * @return status code
1268 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001269static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001270 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001271 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001272 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001273{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001274 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001275 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001276
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001277 /* Check parameters */
1278 switch (search_type) {
1279 case ALL_HANDLES:
1280 break;
1281 case BY_REGISTER_NOTIFY:
1282 if (!search_key)
1283 return EFI_INVALID_PARAMETER;
1284 /* RegisterProtocolNotify is not implemented yet */
1285 return EFI_UNSUPPORTED;
1286 case BY_PROTOCOL:
1287 if (!protocol)
1288 return EFI_INVALID_PARAMETER;
1289 break;
1290 default:
1291 return EFI_INVALID_PARAMETER;
1292 }
1293
1294 /*
1295 * efi_locate_handle_buffer uses this function for
1296 * the calculation of the necessary buffer size.
1297 * So do not require a buffer for buffersize == 0.
1298 */
1299 if (!buffer_size || (*buffer_size && !buffer))
1300 return EFI_INVALID_PARAMETER;
1301
Alexander Grafc15d9212016-03-04 01:09:59 +01001302 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001303 list_for_each_entry(efiobj, &efi_obj_list, link) {
1304 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001305 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001306 }
1307
1308 if (*buffer_size < size) {
1309 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001310 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001311 }
1312
Rob Clarkcdee3372017-08-06 14:10:07 -04001313 *buffer_size = size;
1314 if (size == 0)
1315 return EFI_NOT_FOUND;
1316
Alexander Grafc15d9212016-03-04 01:09:59 +01001317 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001318 list_for_each_entry(efiobj, &efi_obj_list, link) {
1319 if (!efi_search(search_type, protocol, search_key, efiobj))
1320 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001321 }
1322
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001323 return EFI_SUCCESS;
1324}
1325
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001326/*
1327 * Locate handles implementing a protocol.
1328 *
1329 * This function implements the LocateHandle service.
1330 * See the Unified Extensible Firmware Interface (UEFI) specification
1331 * for details.
1332 *
1333 * @search_type selection criterion
1334 * @protocol GUID of the protocol
1335 * @search_key registration key
1336 * @buffer_size size of the buffer to receive the handles in bytes
1337 * @buffer buffer to receive the relevant handles
1338 * @return 0 if the handle implements the protocol
1339 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001340static efi_status_t EFIAPI efi_locate_handle_ext(
1341 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001342 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001343 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001344{
Rob Clark238f88c2017-09-13 18:05:41 -04001345 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001346 buffer_size, buffer);
1347
1348 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1349 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001350}
1351
Alexander Graffe3366f2017-07-26 13:41:04 +02001352/* Collapses configuration table entries, removing index i */
1353static void efi_remove_configuration_table(int i)
1354{
1355 struct efi_configuration_table *this = &efi_conf_table[i];
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001356 struct efi_configuration_table *next = &efi_conf_table[i + 1];
Alexander Graffe3366f2017-07-26 13:41:04 +02001357 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1358
1359 memmove(this, next, (ulong)end - (ulong)next);
1360 systab.nr_tables--;
1361}
1362
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001363/*
1364 * Adds, updates, or removes a configuration table.
1365 *
1366 * This function is used for internal calls. For the API implementation of the
1367 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1368 *
1369 * @guid GUID of the installed table
1370 * @table table to be installed
1371 * @return status code
1372 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001373efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1374 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001375{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001376 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001377 int i;
1378
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001379 if (!guid)
1380 return EFI_INVALID_PARAMETER;
1381
Alexander Grafc15d9212016-03-04 01:09:59 +01001382 /* Check for guid override */
1383 for (i = 0; i < systab.nr_tables; i++) {
1384 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001385 if (table)
1386 efi_conf_table[i].table = table;
1387 else
1388 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001389 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001390 }
1391 }
1392
Alexander Graffe3366f2017-07-26 13:41:04 +02001393 if (!table)
1394 return EFI_NOT_FOUND;
1395
Alexander Grafc15d9212016-03-04 01:09:59 +01001396 /* No override, check for overflow */
1397 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001398 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001399
1400 /* Add a new entry */
1401 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1402 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001403 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001404
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001405out:
1406 /* Notify that the configuration table was changed */
1407 list_for_each_entry(evt, &efi_events, link) {
1408 if (evt->group && !guidcmp(evt->group, guid)) {
1409 efi_signal_event(evt, false);
1410 break;
1411 }
1412 }
1413
Alexander Grafc5c11632016-08-19 01:23:24 +02001414 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001415}
1416
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001417/*
1418 * Adds, updates, or removes a configuration table.
1419 *
1420 * This function implements the InstallConfigurationTable service.
1421 * See the Unified Extensible Firmware Interface (UEFI) specification
1422 * for details.
1423 *
1424 * @guid GUID of the installed table
1425 * @table table to be installed
1426 * @return status code
1427 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001428static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1429 void *table)
1430{
Rob Clark238f88c2017-09-13 18:05:41 -04001431 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001432 return EFI_EXIT(efi_install_configuration_table(guid, table));
1433}
1434
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001435/*
1436 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001437 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001438 *
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001439 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001440 * image
1441 * @obj internal object associated with the loaded image
1442 * @device_path device path of the loaded image
1443 * @file_path file path of the loaded image
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001444 * @return status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001445 */
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001446efi_status_t efi_setup_loaded_image(
1447 struct efi_loaded_image *info, struct efi_object *obj,
1448 struct efi_device_path *device_path,
1449 struct efi_device_path *file_path)
Rob Clarkf8db9222017-09-13 18:05:33 -04001450{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001451 efi_status_t ret;
1452
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001453 /* Add internal object to object list */
1454 efi_add_handle(obj);
1455 /* efi_exit() assumes that the handle points to the info */
Rob Clarkf8db9222017-09-13 18:05:33 -04001456 obj->handle = info;
1457
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001458 info->file_path = file_path;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001459
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001460 if (device_path) {
1461 info->device_handle = efi_dp_find_obj(device_path, NULL);
1462 /*
1463 * When asking for the device path interface, return
1464 * bootefi_device_path
1465 */
1466 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1467 device_path);
1468 if (ret != EFI_SUCCESS)
1469 goto failure;
1470 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001471
1472 /*
1473 * When asking for the loaded_image interface, just
1474 * return handle which points to loaded_image_info
1475 */
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001476 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1477 if (ret != EFI_SUCCESS)
1478 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001479
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001480 ret = efi_add_protocol(obj->handle,
1481 &efi_guid_device_path_to_text_protocol,
1482 (void *)&efi_device_path_to_text);
1483 if (ret != EFI_SUCCESS)
1484 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001485
Leif Lindholmb6e6fdc2018-03-09 17:43:21 +01001486 ret = efi_add_protocol(obj->handle,
1487 &efi_guid_device_path_utilities_protocol,
1488 (void *)&efi_device_path_utilities);
1489 if (ret != EFI_SUCCESS)
1490 goto failure;
1491
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001492 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001493failure:
1494 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001495 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001496}
1497
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001498/*
1499 * Load an image using a file path.
1500 *
1501 * @file_path the path of the image to load
1502 * @buffer buffer containing the loaded image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001503 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001504 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001505efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1506 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001507{
1508 struct efi_file_info *info = NULL;
1509 struct efi_file_handle *f;
1510 static efi_status_t ret;
1511 uint64_t bs;
1512
1513 f = efi_file_from_path(file_path);
1514 if (!f)
1515 return EFI_DEVICE_ERROR;
1516
1517 bs = 0;
1518 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1519 &bs, info));
1520 if (ret == EFI_BUFFER_TOO_SMALL) {
1521 info = malloc(bs);
1522 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1523 &bs, info));
1524 }
1525 if (ret != EFI_SUCCESS)
1526 goto error;
1527
1528 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1529 if (ret)
1530 goto error;
1531
1532 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1533
1534error:
1535 free(info);
1536 EFI_CALL(f->close(f));
1537
1538 if (ret != EFI_SUCCESS) {
1539 efi_free_pool(*buffer);
1540 *buffer = NULL;
1541 }
1542
1543 return ret;
1544}
1545
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001546/*
1547 * Load an EFI image into memory.
1548 *
1549 * This function implements the LoadImage service.
1550 * See the Unified Extensible Firmware Interface (UEFI) specification
1551 * for details.
1552 *
1553 * @boot_policy true for request originating from the boot manager
Heinrich Schuchardt091cf432018-01-24 19:21:36 +01001554 * @parent_image the caller's image handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001555 * @file_path the path of the image to load
1556 * @source_buffer memory location from which the image is installed
1557 * @source_size size of the memory area from which the image is
1558 * installed
1559 * @image_handle handle for the newly installed image
1560 * @return status code
1561 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001562static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1563 efi_handle_t parent_image,
1564 struct efi_device_path *file_path,
1565 void *source_buffer,
1566 unsigned long source_size,
1567 efi_handle_t *image_handle)
1568{
Alexander Grafc15d9212016-03-04 01:09:59 +01001569 struct efi_loaded_image *info;
1570 struct efi_object *obj;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001571 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001572
Heinrich Schuchardtba784332018-01-19 20:24:40 +01001573 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001574 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001575
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001576 if (!image_handle || !parent_image) {
1577 ret = EFI_INVALID_PARAMETER;
1578 goto error;
1579 }
1580
1581 if (!source_buffer && !file_path) {
1582 ret = EFI_NOT_FOUND;
1583 goto error;
1584 }
1585
Rob Clark857a1222017-09-13 18:05:35 -04001586 info = calloc(1, sizeof(*info));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001587 if (!info) {
1588 ret = EFI_OUT_OF_RESOURCES;
1589 goto error;
1590 }
Rob Clark857a1222017-09-13 18:05:35 -04001591 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001592 if (!obj) {
1593 free(info);
1594 ret = EFI_OUT_OF_RESOURCES;
1595 goto error;
1596 }
Rob Clark857a1222017-09-13 18:05:35 -04001597
1598 if (!source_buffer) {
1599 struct efi_device_path *dp, *fp;
Rob Clark857a1222017-09-13 18:05:35 -04001600
Rob Clarkc84c1102017-09-13 18:05:38 -04001601 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001602 if (ret != EFI_SUCCESS)
1603 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001604 /*
1605 * split file_path which contains both the device and
1606 * file parts:
1607 */
1608 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001609 ret = efi_setup_loaded_image(info, obj, dp, fp);
1610 if (ret != EFI_SUCCESS)
1611 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001612 } else {
1613 /* In this case, file_path is the "device" path, ie.
1614 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1615 */
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001616 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1617 if (ret != EFI_SUCCESS)
1618 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001619 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001620 info->reserved = efi_load_pe(source_buffer, info);
1621 if (!info->reserved) {
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001622 ret = EFI_UNSUPPORTED;
1623 goto failure;
Alexander Grafc15d9212016-03-04 01:09:59 +01001624 }
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001625 info->system_table = &systab;
1626 info->parent_handle = parent_image;
Heinrich Schuchardt754d4972017-11-26 14:05:22 +01001627 *image_handle = obj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001628 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001629failure:
1630 free(info);
1631 efi_delete_handle(obj);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001632error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001633 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001634}
1635
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001636/*
1637 * Call the entry point of an image.
1638 *
1639 * This function implements the StartImage service.
1640 * See the Unified Extensible Firmware Interface (UEFI) specification
1641 * for details.
1642 *
1643 * @image_handle handle of the image
1644 * @exit_data_size size of the buffer
1645 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001646 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001647 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001648static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1649 unsigned long *exit_data_size,
1650 s16 **exit_data)
1651{
Alexander Graf54d8cdf2018-01-24 00:18:08 +01001652 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1653 struct efi_system_table *st);
Alexander Grafc15d9212016-03-04 01:09:59 +01001654 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001655 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001656
1657 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1658 entry = info->reserved;
1659
1660 efi_is_direct_boot = false;
1661
1662 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001663 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001664 /*
1665 * We called the entry point of the child image with EFI_CALL
1666 * in the lines below. The child image called the Exit() boot
1667 * service efi_exit() which executed the long jump that brought
1668 * us to the current line. This implies that the second half
1669 * of the EFI_CALL macro has not been executed.
1670 */
1671#ifdef CONFIG_ARM
1672 /*
1673 * efi_exit() called efi_restore_gd(). We have to undo this
1674 * otherwise __efi_entry_check() will put the wrong value into
1675 * app_gd.
1676 */
1677 gd = app_gd;
1678#endif
1679 /*
1680 * To get ready to call EFI_EXIT below we have to execute the
1681 * missed out steps of EFI_CALL.
1682 */
1683 assert(__efi_entry_check());
1684 debug("%sEFI: %lu returned by started image\n",
1685 __efi_nesting_dec(),
1686 (unsigned long)((uintptr_t)info->exit_status &
1687 ~EFI_ERROR_MASK));
Alexander Graf988c0662016-05-20 23:28:23 +02001688 return EFI_EXIT(info->exit_status);
1689 }
1690
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001691 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001692
Alexander Grafeff317c2018-01-26 00:47:53 +01001693 /*
1694 * Usually UEFI applications call Exit() instead of returning.
1695 * But because the world doesn not consist of ponies and unicorns,
1696 * we're happy to emulate that behavior on behalf of a payload
1697 * that forgot.
1698 */
1699 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001700}
1701
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001702/*
1703 * Leave an EFI application or driver.
1704 *
1705 * This function implements the Exit service.
1706 * See the Unified Extensible Firmware Interface (UEFI) specification
1707 * for details.
1708 *
1709 * @image_handle handle of the application or driver that is exiting
1710 * @exit_status status code
1711 * @exit_data_size size of the buffer in bytes
1712 * @exit_data buffer with data describing an error
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001713 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001714 */
Alexander Graf988c0662016-05-20 23:28:23 +02001715static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001716 efi_status_t exit_status,
1717 unsigned long exit_data_size,
1718 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001719{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001720 /*
1721 * We require that the handle points to the original loaded
1722 * image protocol interface.
1723 *
1724 * For getting the longjmp address this is safer than locating
1725 * the protocol because the protocol may have been reinstalled
1726 * pointing to another memory location.
1727 *
1728 * TODO: We should call the unload procedure of the loaded
1729 * image protocol.
1730 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001731 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001732
Alexander Grafc15d9212016-03-04 01:09:59 +01001733 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1734 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001735
Alexander Graf87e787a2017-09-03 14:14:17 +02001736 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001737 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001738
Alexander Graf87e787a2017-09-03 14:14:17 +02001739 /*
1740 * But longjmp out with the U-Boot gd, not the application's, as
1741 * the other end is a setjmp call inside EFI context.
1742 */
1743 efi_restore_gd();
1744
Alexander Graf988c0662016-05-20 23:28:23 +02001745 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001746 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001747
1748 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001749}
1750
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001751/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001752 * Unload an EFI image.
1753 *
1754 * This function implements the UnloadImage service.
1755 * See the Unified Extensible Firmware Interface (UEFI) specification
1756 * for details.
1757 *
1758 * @image_handle handle of the image to be unloaded
1759 * @return status code
1760 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001761static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001762{
1763 struct efi_object *efiobj;
1764
1765 EFI_ENTRY("%p", image_handle);
1766 efiobj = efi_search_obj(image_handle);
1767 if (efiobj)
1768 list_del(&efiobj->link);
1769
1770 return EFI_EXIT(EFI_SUCCESS);
1771}
1772
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001773/*
1774 * Fix up caches for EFI payloads if necessary.
1775 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001776static void efi_exit_caches(void)
1777{
1778#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1779 /*
1780 * Grub on 32bit ARM needs to have caches disabled before jumping into
1781 * a zImage, but does not know of all cache layers. Give it a hand.
1782 */
1783 if (efi_is_direct_boot)
1784 cleanup_before_linux();
1785#endif
1786}
1787
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001788/*
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001789 * Stop all boot services.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001790 *
1791 * This function implements the ExitBootServices service.
1792 * See the Unified Extensible Firmware Interface (UEFI) specification
1793 * for details.
1794 *
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001795 * All timer events are disabled.
1796 * For exit boot services events the notification function is called.
1797 * The boot services are disabled in the system table.
1798 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001799 * @image_handle handle of the loaded image
1800 * @map_key key of the memory map
1801 * @return status code
1802 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001803static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001804 unsigned long map_key)
1805{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001806 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001807
Alexander Grafc15d9212016-03-04 01:09:59 +01001808 EFI_ENTRY("%p, %ld", image_handle, map_key);
1809
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001810 /* Make sure that notification functions are not called anymore */
1811 efi_tpl = TPL_HIGH_LEVEL;
1812
1813 /* Check if ExitBootServices has already been called */
1814 if (!systab.boottime)
1815 return EFI_EXIT(EFI_SUCCESS);
1816
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001817 /* Add related events to the event group */
1818 list_for_each_entry(evt, &efi_events, link) {
1819 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1820 evt->group = &efi_guid_event_group_exit_boot_services;
1821 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001822 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001823 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001824 if (evt->group &&
1825 !guidcmp(evt->group,
1826 &efi_guid_event_group_exit_boot_services)) {
1827 efi_signal_event(evt, false);
1828 break;
1829 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001830 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001831
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001832 /* TODO Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001833
Alexander Graf2ebeb442016-11-17 01:02:57 +01001834 board_quiesce_devices();
1835
Alexander Grafc15d9212016-03-04 01:09:59 +01001836 /* Fix up caches for EFI payloads if necessary */
1837 efi_exit_caches();
1838
1839 /* This stops all lingering devices */
1840 bootm_disable_interrupts();
1841
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001842 /* Disable boottime services */
1843 systab.con_in_handle = NULL;
1844 systab.con_in = NULL;
1845 systab.con_out_handle = NULL;
1846 systab.con_out = NULL;
1847 systab.stderr_handle = NULL;
1848 systab.std_err = NULL;
1849 systab.boottime = NULL;
1850
1851 /* Recalculate CRC32 */
1852 systab.hdr.crc32 = 0;
1853 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1854 sizeof(struct efi_system_table));
1855
Alexander Grafc15d9212016-03-04 01:09:59 +01001856 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001857 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001858 WATCHDOG_RESET();
1859
1860 return EFI_EXIT(EFI_SUCCESS);
1861}
1862
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001863/*
1864 * Get next value of the counter.
1865 *
1866 * This function implements the NextMonotonicCount service.
1867 * See the Unified Extensible Firmware Interface (UEFI) specification
1868 * for details.
1869 *
1870 * @count returned value of the counter
1871 * @return status code
1872 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001873static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1874{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001875 static uint64_t mono;
1876
Alexander Grafc15d9212016-03-04 01:09:59 +01001877 EFI_ENTRY("%p", count);
1878 *count = mono++;
1879 return EFI_EXIT(EFI_SUCCESS);
1880}
1881
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001882/*
1883 * Sleep.
1884 *
1885 * This function implements the Stall sercive.
1886 * See the Unified Extensible Firmware Interface (UEFI) specification
1887 * for details.
1888 *
1889 * @microseconds period to sleep in microseconds
1890 * @return status code
1891 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001892static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1893{
1894 EFI_ENTRY("%ld", microseconds);
1895 udelay(microseconds);
1896 return EFI_EXIT(EFI_SUCCESS);
1897}
1898
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001899/*
1900 * Reset the watchdog timer.
1901 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001902 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001903 * See the Unified Extensible Firmware Interface (UEFI) specification
1904 * for details.
1905 *
1906 * @timeout seconds before reset by watchdog
1907 * @watchdog_code code to be logged when resetting
1908 * @data_size size of buffer in bytes
1909 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001910 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001911 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001912static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1913 uint64_t watchdog_code,
1914 unsigned long data_size,
1915 uint16_t *watchdog_data)
1916{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001917 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001918 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001919 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001920}
1921
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001922/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001923 * Close a protocol.
1924 *
1925 * This function implements the CloseProtocol service.
1926 * See the Unified Extensible Firmware Interface (UEFI) specification
1927 * for details.
1928 *
1929 * @handle handle on which the protocol shall be closed
1930 * @protocol GUID of the protocol to close
1931 * @agent_handle handle of the driver
1932 * @controller_handle handle of the controller
1933 * @return status code
1934 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001935static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001936 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001937 efi_handle_t agent_handle,
1938 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001939{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001940 struct efi_handler *handler;
1941 struct efi_open_protocol_info_item *item;
1942 struct efi_open_protocol_info_item *pos;
1943 efi_status_t r;
1944
Rob Clark238f88c2017-09-13 18:05:41 -04001945 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001946 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001947
1948 if (!agent_handle) {
1949 r = EFI_INVALID_PARAMETER;
1950 goto out;
1951 }
1952 r = efi_search_protocol(handle, protocol, &handler);
1953 if (r != EFI_SUCCESS)
1954 goto out;
1955
1956 r = EFI_NOT_FOUND;
1957 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1958 if (item->info.agent_handle == agent_handle &&
1959 item->info.controller_handle == controller_handle) {
1960 efi_delete_open_info(item);
1961 r = EFI_SUCCESS;
1962 break;
1963 }
1964 }
1965out:
1966 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001967}
1968
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001969/*
1970 * Provide information about then open status of a protocol on a handle
1971 *
1972 * This function implements the OpenProtocolInformation service.
1973 * See the Unified Extensible Firmware Interface (UEFI) specification
1974 * for details.
1975 *
1976 * @handle handle for which the information shall be retrieved
1977 * @protocol GUID of the protocol
1978 * @entry_buffer buffer to receive the open protocol information
1979 * @entry_count number of entries available in the buffer
1980 * @return status code
1981 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001982static efi_status_t EFIAPI efi_open_protocol_information(
1983 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001984 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001985 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001986{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001987 unsigned long buffer_size;
1988 unsigned long count;
1989 struct efi_handler *handler;
1990 struct efi_open_protocol_info_item *item;
1991 efi_status_t r;
1992
Rob Clark238f88c2017-09-13 18:05:41 -04001993 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001994 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001995
1996 /* Check parameters */
1997 if (!entry_buffer) {
1998 r = EFI_INVALID_PARAMETER;
1999 goto out;
2000 }
2001 r = efi_search_protocol(handle, protocol, &handler);
2002 if (r != EFI_SUCCESS)
2003 goto out;
2004
2005 /* Count entries */
2006 count = 0;
2007 list_for_each_entry(item, &handler->open_infos, link) {
2008 if (item->info.open_count)
2009 ++count;
2010 }
2011 *entry_count = count;
2012 *entry_buffer = NULL;
2013 if (!count) {
2014 r = EFI_SUCCESS;
2015 goto out;
2016 }
2017
2018 /* Copy entries */
2019 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2020 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2021 (void **)entry_buffer);
2022 if (r != EFI_SUCCESS)
2023 goto out;
2024 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2025 if (item->info.open_count)
2026 (*entry_buffer)[--count] = item->info;
2027 }
2028out:
2029 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002030}
2031
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002032/*
2033 * Get protocols installed on a handle.
2034 *
2035 * This function implements the ProtocolsPerHandleService.
2036 * See the Unified Extensible Firmware Interface (UEFI) specification
2037 * for details.
2038 *
2039 * @handle handle for which the information is retrieved
2040 * @protocol_buffer buffer with protocol GUIDs
2041 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02002042 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002043 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002044static efi_status_t EFIAPI efi_protocols_per_handle(
2045 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002046 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002047{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002048 unsigned long buffer_size;
2049 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002050 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002051 efi_status_t r;
2052
Alexander Grafc15d9212016-03-04 01:09:59 +01002053 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2054 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002055
2056 if (!handle || !protocol_buffer || !protocol_buffer_count)
2057 return EFI_EXIT(EFI_INVALID_PARAMETER);
2058
2059 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002060 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002061
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002062 efiobj = efi_search_obj(handle);
2063 if (!efiobj)
2064 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002065
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002066 /* Count protocols */
2067 list_for_each(protocol_handle, &efiobj->protocols) {
2068 ++*protocol_buffer_count;
2069 }
2070
2071 /* Copy guids */
2072 if (*protocol_buffer_count) {
2073 size_t j = 0;
2074
2075 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2076 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2077 (void **)protocol_buffer);
2078 if (r != EFI_SUCCESS)
2079 return EFI_EXIT(r);
2080 list_for_each(protocol_handle, &efiobj->protocols) {
2081 struct efi_handler *protocol;
2082
2083 protocol = list_entry(protocol_handle,
2084 struct efi_handler, link);
2085 (*protocol_buffer)[j] = (void *)protocol->guid;
2086 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002087 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002088 }
2089
2090 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002091}
2092
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002093/*
2094 * Locate handles implementing a protocol.
2095 *
2096 * This function implements the LocateHandleBuffer service.
2097 * See the Unified Extensible Firmware Interface (UEFI) specification
2098 * for details.
2099 *
2100 * @search_type selection criterion
2101 * @protocol GUID of the protocol
2102 * @search_key registration key
2103 * @no_handles number of returned handles
2104 * @buffer buffer with the returned handles
2105 * @return status code
2106 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002107static efi_status_t EFIAPI efi_locate_handle_buffer(
2108 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002109 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002110 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002111{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002112 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002113 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002114
Rob Clark238f88c2017-09-13 18:05:41 -04002115 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002116 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002117
2118 if (!no_handles || !buffer) {
2119 r = EFI_INVALID_PARAMETER;
2120 goto out;
2121 }
2122 *no_handles = 0;
2123 *buffer = NULL;
2124 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2125 *buffer);
2126 if (r != EFI_BUFFER_TOO_SMALL)
2127 goto out;
2128 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2129 (void **)buffer);
2130 if (r != EFI_SUCCESS)
2131 goto out;
2132 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2133 *buffer);
2134 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002135 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002136out:
2137 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002138}
2139
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002140/*
2141 * Find an interface implementing a protocol.
2142 *
2143 * This function implements the LocateProtocol service.
2144 * See the Unified Extensible Firmware Interface (UEFI) specification
2145 * for details.
2146 *
2147 * @protocol GUID of the protocol
2148 * @registration registration key passed to the notification function
2149 * @protocol_interface interface implementing the protocol
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02002150 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002151 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002152static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002153 void *registration,
2154 void **protocol_interface)
2155{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002156 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002157 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002158
Rob Clark238f88c2017-09-13 18:05:41 -04002159 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002160
2161 if (!protocol || !protocol_interface)
2162 return EFI_EXIT(EFI_INVALID_PARAMETER);
2163
2164 list_for_each(lhandle, &efi_obj_list) {
2165 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002166 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002167
2168 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002169
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002170 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2171 if (ret == EFI_SUCCESS) {
2172 *protocol_interface = handler->protocol_interface;
2173 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002174 }
2175 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002176 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002177
2178 return EFI_EXIT(EFI_NOT_FOUND);
2179}
2180
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002181/*
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002182 * Get the device path and handle of an device implementing a protocol.
2183 *
2184 * This function implements the LocateDevicePath service.
2185 * See the Unified Extensible Firmware Interface (UEFI) specification
2186 * for details.
2187 *
2188 * @protocol GUID of the protocol
2189 * @device_path device path
2190 * @device handle of the device
2191 * @return status code
2192 */
2193static efi_status_t EFIAPI efi_locate_device_path(
2194 const efi_guid_t *protocol,
2195 struct efi_device_path **device_path,
2196 efi_handle_t *device)
2197{
2198 struct efi_device_path *dp;
2199 size_t i;
2200 struct efi_handler *handler;
2201 efi_handle_t *handles;
2202 size_t len, len_dp;
2203 size_t len_best = 0;
2204 efi_uintn_t no_handles;
2205 u8 *remainder;
2206 efi_status_t ret;
2207
2208 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2209
2210 if (!protocol || !device_path || !*device_path || !device) {
2211 ret = EFI_INVALID_PARAMETER;
2212 goto out;
2213 }
2214
2215 /* Find end of device path */
2216 len = efi_dp_size(*device_path);
2217
2218 /* Get all handles implementing the protocol */
2219 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2220 &no_handles, &handles));
2221 if (ret != EFI_SUCCESS)
2222 goto out;
2223
2224 for (i = 0; i < no_handles; ++i) {
2225 /* Find the device path protocol */
2226 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2227 &handler);
2228 if (ret != EFI_SUCCESS)
2229 continue;
2230 dp = (struct efi_device_path *)handler->protocol_interface;
2231 len_dp = efi_dp_size(dp);
2232 /*
2233 * This handle can only be a better fit
2234 * if its device path length is longer than the best fit and
2235 * if its device path length is shorter of equal the searched
2236 * device path.
2237 */
2238 if (len_dp <= len_best || len_dp > len)
2239 continue;
2240 /* Check if dp is a subpath of device_path */
2241 if (memcmp(*device_path, dp, len_dp))
2242 continue;
2243 *device = handles[i];
2244 len_best = len_dp;
2245 }
2246 if (len_best) {
2247 remainder = (u8 *)*device_path + len_best;
2248 *device_path = (struct efi_device_path *)remainder;
2249 ret = EFI_SUCCESS;
2250 } else {
2251 ret = EFI_NOT_FOUND;
2252 }
2253out:
2254 return EFI_EXIT(ret);
2255}
2256
2257/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002258 * Install multiple protocol interfaces.
2259 *
2260 * This function implements the MultipleProtocolInterfaces service.
2261 * See the Unified Extensible Firmware Interface (UEFI) specification
2262 * for details.
2263 *
2264 * @handle handle on which the protocol interfaces shall be installed
2265 * @... NULL terminated argument list with pairs of protocol GUIDS and
2266 * interfaces
2267 * @return status code
2268 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002269static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2270 void **handle, ...)
2271{
2272 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002273
2274 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002275 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002276 void *protocol_interface;
2277 efi_status_t r = EFI_SUCCESS;
2278 int i = 0;
2279
2280 if (!handle)
2281 return EFI_EXIT(EFI_INVALID_PARAMETER);
2282
2283 va_start(argptr, handle);
2284 for (;;) {
2285 protocol = va_arg(argptr, efi_guid_t*);
2286 if (!protocol)
2287 break;
2288 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002289 r = EFI_CALL(efi_install_protocol_interface(
2290 handle, protocol,
2291 EFI_NATIVE_INTERFACE,
2292 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002293 if (r != EFI_SUCCESS)
2294 break;
2295 i++;
2296 }
2297 va_end(argptr);
2298 if (r == EFI_SUCCESS)
2299 return EFI_EXIT(r);
2300
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002301 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002302 va_start(argptr, handle);
2303 for (; i; --i) {
2304 protocol = va_arg(argptr, efi_guid_t*);
2305 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002306 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2307 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002308 }
2309 va_end(argptr);
2310
2311 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002312}
2313
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002314/*
2315 * Uninstall multiple protocol interfaces.
2316 *
2317 * This function implements the UninstallMultipleProtocolInterfaces service.
2318 * See the Unified Extensible Firmware Interface (UEFI) specification
2319 * for details.
2320 *
2321 * @handle handle from which the protocol interfaces shall be removed
2322 * @... NULL terminated argument list with pairs of protocol GUIDS and
2323 * interfaces
2324 * @return status code
2325 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002326static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2327 void *handle, ...)
2328{
2329 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002330
2331 va_list argptr;
2332 const efi_guid_t *protocol;
2333 void *protocol_interface;
2334 efi_status_t r = EFI_SUCCESS;
2335 size_t i = 0;
2336
2337 if (!handle)
2338 return EFI_EXIT(EFI_INVALID_PARAMETER);
2339
2340 va_start(argptr, handle);
2341 for (;;) {
2342 protocol = va_arg(argptr, efi_guid_t*);
2343 if (!protocol)
2344 break;
2345 protocol_interface = va_arg(argptr, void*);
2346 r = EFI_CALL(efi_uninstall_protocol_interface(
2347 handle, protocol,
2348 protocol_interface));
2349 if (r != EFI_SUCCESS)
2350 break;
2351 i++;
2352 }
2353 va_end(argptr);
2354 if (r == EFI_SUCCESS)
2355 return EFI_EXIT(r);
2356
2357 /* If an error occurred undo all changes. */
2358 va_start(argptr, handle);
2359 for (; i; --i) {
2360 protocol = va_arg(argptr, efi_guid_t*);
2361 protocol_interface = va_arg(argptr, void*);
2362 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2363 EFI_NATIVE_INTERFACE,
2364 protocol_interface));
2365 }
2366 va_end(argptr);
2367
2368 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002369}
2370
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002371/*
2372 * Calculate cyclic redundancy code.
2373 *
2374 * This function implements the CalculateCrc32 service.
2375 * See the Unified Extensible Firmware Interface (UEFI) specification
2376 * for details.
2377 *
2378 * @data buffer with data
2379 * @data_size size of buffer in bytes
2380 * @crc32_p cyclic redundancy code
2381 * @return status code
2382 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002383static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2384 unsigned long data_size,
2385 uint32_t *crc32_p)
2386{
2387 EFI_ENTRY("%p, %ld", data, data_size);
2388 *crc32_p = crc32(0, data, data_size);
2389 return EFI_EXIT(EFI_SUCCESS);
2390}
2391
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002392/*
2393 * Copy memory.
2394 *
2395 * This function implements the CopyMem service.
2396 * See the Unified Extensible Firmware Interface (UEFI) specification
2397 * for details.
2398 *
2399 * @destination destination of the copy operation
2400 * @source source of the copy operation
2401 * @length number of bytes to copy
2402 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002403static void EFIAPI efi_copy_mem(void *destination, const void *source,
2404 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002405{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002406 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01002407 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002408 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002409}
2410
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002411/*
2412 * Fill memory with a byte value.
2413 *
2414 * This function implements the SetMem service.
2415 * See the Unified Extensible Firmware Interface (UEFI) specification
2416 * for details.
2417 *
2418 * @buffer buffer to fill
2419 * @size size of buffer in bytes
2420 * @value byte to copy to the buffer
2421 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002422static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002423{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002424 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002425 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002426 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002427}
2428
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002429/*
2430 * Open protocol interface on a handle.
2431 *
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002432 * @handler handler of a protocol
2433 * @protocol_interface interface implementing the protocol
2434 * @agent_handle handle of the driver
2435 * @controller_handle handle of the controller
2436 * @attributes attributes indicating how to open the protocol
2437 * @return status code
2438 */
2439static efi_status_t efi_protocol_open(
2440 struct efi_handler *handler,
2441 void **protocol_interface, void *agent_handle,
2442 void *controller_handle, uint32_t attributes)
2443{
2444 struct efi_open_protocol_info_item *item;
2445 struct efi_open_protocol_info_entry *match = NULL;
2446 bool opened_by_driver = false;
2447 bool opened_exclusive = false;
2448
2449 /* If there is no agent, only return the interface */
2450 if (!agent_handle)
2451 goto out;
2452
2453 /* For TEST_PROTOCOL ignore interface attribute */
2454 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2455 *protocol_interface = NULL;
2456
2457 /*
2458 * Check if the protocol is already opened by a driver with the same
2459 * attributes or opened exclusively
2460 */
2461 list_for_each_entry(item, &handler->open_infos, link) {
2462 if (item->info.agent_handle == agent_handle) {
2463 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2464 (item->info.attributes == attributes))
2465 return EFI_ALREADY_STARTED;
2466 }
2467 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2468 opened_exclusive = true;
2469 }
2470
2471 /* Only one controller can open the protocol exclusively */
2472 if (opened_exclusive && attributes &
2473 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2474 return EFI_ACCESS_DENIED;
2475
2476 /* Prepare exclusive opening */
2477 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2478 /* Try to disconnect controllers */
2479 list_for_each_entry(item, &handler->open_infos, link) {
2480 if (item->info.attributes ==
2481 EFI_OPEN_PROTOCOL_BY_DRIVER)
2482 EFI_CALL(efi_disconnect_controller(
2483 item->info.controller_handle,
2484 item->info.agent_handle,
2485 NULL));
2486 }
2487 opened_by_driver = false;
2488 /* Check if all controllers are disconnected */
2489 list_for_each_entry(item, &handler->open_infos, link) {
2490 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2491 opened_by_driver = true;
2492 }
2493 /* Only one controller can be conncected */
2494 if (opened_by_driver)
2495 return EFI_ACCESS_DENIED;
2496 }
2497
2498 /* Find existing entry */
2499 list_for_each_entry(item, &handler->open_infos, link) {
2500 if (item->info.agent_handle == agent_handle &&
2501 item->info.controller_handle == controller_handle)
2502 match = &item->info;
2503 }
2504 /* None found, create one */
2505 if (!match) {
2506 match = efi_create_open_info(handler);
2507 if (!match)
2508 return EFI_OUT_OF_RESOURCES;
2509 }
2510
2511 match->agent_handle = agent_handle;
2512 match->controller_handle = controller_handle;
2513 match->attributes = attributes;
2514 match->open_count++;
2515
2516out:
2517 /* For TEST_PROTOCOL ignore interface attribute. */
2518 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2519 *protocol_interface = handler->protocol_interface;
2520
2521 return EFI_SUCCESS;
2522}
2523
2524/*
2525 * Open protocol interface on a handle.
2526 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002527 * This function implements the OpenProtocol interface.
2528 * See the Unified Extensible Firmware Interface (UEFI) specification
2529 * for details.
2530 *
2531 * @handle handle on which the protocol shall be opened
2532 * @protocol GUID of the protocol
2533 * @protocol_interface interface implementing the protocol
2534 * @agent_handle handle of the driver
2535 * @controller_handle handle of the controller
2536 * @attributes attributes indicating how to open the protocol
2537 * @return status code
2538 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002539static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002540 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002541 void **protocol_interface, void *agent_handle,
2542 void *controller_handle, uint32_t attributes)
2543{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002544 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002545 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002546
Rob Clark238f88c2017-09-13 18:05:41 -04002547 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002548 protocol_interface, agent_handle, controller_handle,
2549 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002550
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002551 if (!handle || !protocol ||
2552 (!protocol_interface && attributes !=
2553 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002554 goto out;
2555 }
2556
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002557 switch (attributes) {
2558 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2559 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2560 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2561 break;
2562 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2563 if (controller_handle == handle)
2564 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002565 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002566 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2567 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002568 /* Check that the controller handle is valid */
2569 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002570 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002571 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002572 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002573 /* Check that the agent handle is valid */
2574 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002575 goto out;
2576 break;
2577 default:
2578 goto out;
2579 }
2580
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002581 r = efi_search_protocol(handle, protocol, &handler);
2582 if (r != EFI_SUCCESS)
2583 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002584
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002585 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2586 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002587out:
2588 return EFI_EXIT(r);
2589}
2590
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002591/*
2592 * Get interface of a protocol on a handle.
2593 *
2594 * This function implements the HandleProtocol service.
2595 * See the Unified Extensible Firmware Interface (UEFI) specification
2596 * for details.
2597 *
2598 * @handle handle on which the protocol shall be opened
2599 * @protocol GUID of the protocol
2600 * @protocol_interface interface implementing the protocol
2601 * @return status code
2602 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002603static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002604 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002605 void **protocol_interface)
2606{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002607 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2608 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002609}
2610
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002611static efi_status_t efi_bind_controller(
2612 efi_handle_t controller_handle,
2613 efi_handle_t driver_image_handle,
2614 struct efi_device_path *remain_device_path)
2615{
2616 struct efi_driver_binding_protocol *binding_protocol;
2617 efi_status_t r;
2618
2619 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2620 &efi_guid_driver_binding_protocol,
2621 (void **)&binding_protocol,
2622 driver_image_handle, NULL,
2623 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2624 if (r != EFI_SUCCESS)
2625 return r;
2626 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2627 controller_handle,
2628 remain_device_path));
2629 if (r == EFI_SUCCESS)
2630 r = EFI_CALL(binding_protocol->start(binding_protocol,
2631 controller_handle,
2632 remain_device_path));
2633 EFI_CALL(efi_close_protocol(driver_image_handle,
2634 &efi_guid_driver_binding_protocol,
2635 driver_image_handle, NULL));
2636 return r;
2637}
2638
2639static efi_status_t efi_connect_single_controller(
2640 efi_handle_t controller_handle,
2641 efi_handle_t *driver_image_handle,
2642 struct efi_device_path *remain_device_path)
2643{
2644 efi_handle_t *buffer;
2645 size_t count;
2646 size_t i;
2647 efi_status_t r;
2648 size_t connected = 0;
2649
2650 /* Get buffer with all handles with driver binding protocol */
2651 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2652 &efi_guid_driver_binding_protocol,
2653 NULL, &count, &buffer));
2654 if (r != EFI_SUCCESS)
2655 return r;
2656
2657 /* Context Override */
2658 if (driver_image_handle) {
2659 for (; *driver_image_handle; ++driver_image_handle) {
2660 for (i = 0; i < count; ++i) {
2661 if (buffer[i] == *driver_image_handle) {
2662 buffer[i] = NULL;
2663 r = efi_bind_controller(
2664 controller_handle,
2665 *driver_image_handle,
2666 remain_device_path);
2667 /*
2668 * For drivers that do not support the
2669 * controller or are already connected
2670 * we receive an error code here.
2671 */
2672 if (r == EFI_SUCCESS)
2673 ++connected;
2674 }
2675 }
2676 }
2677 }
2678
2679 /*
2680 * TODO: Some overrides are not yet implemented:
2681 * - Platform Driver Override
2682 * - Driver Family Override Search
2683 * - Bus Specific Driver Override
2684 */
2685
2686 /* Driver Binding Search */
2687 for (i = 0; i < count; ++i) {
2688 if (buffer[i]) {
2689 r = efi_bind_controller(controller_handle,
2690 buffer[i],
2691 remain_device_path);
2692 if (r == EFI_SUCCESS)
2693 ++connected;
2694 }
2695 }
2696
2697 efi_free_pool(buffer);
2698 if (!connected)
2699 return EFI_NOT_FOUND;
2700 return EFI_SUCCESS;
2701}
2702
2703/*
2704 * Connect a controller to a driver.
2705 *
2706 * This function implements the ConnectController service.
2707 * See the Unified Extensible Firmware Interface (UEFI) specification
2708 * for details.
2709 *
2710 * First all driver binding protocol handles are tried for binding drivers.
2711 * Afterwards all handles that have openened a protocol of the controller
2712 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2713 *
2714 * @controller_handle handle of the controller
2715 * @driver_image_handle handle of the driver
2716 * @remain_device_path device path of a child controller
2717 * @recursive true to connect all child controllers
2718 * @return status code
2719 */
2720static efi_status_t EFIAPI efi_connect_controller(
2721 efi_handle_t controller_handle,
2722 efi_handle_t *driver_image_handle,
2723 struct efi_device_path *remain_device_path,
2724 bool recursive)
2725{
2726 efi_status_t r;
2727 efi_status_t ret = EFI_NOT_FOUND;
2728 struct efi_object *efiobj;
2729
2730 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2731 remain_device_path, recursive);
2732
2733 efiobj = efi_search_obj(controller_handle);
2734 if (!efiobj) {
2735 ret = EFI_INVALID_PARAMETER;
2736 goto out;
2737 }
2738
2739 r = efi_connect_single_controller(controller_handle,
2740 driver_image_handle,
2741 remain_device_path);
2742 if (r == EFI_SUCCESS)
2743 ret = EFI_SUCCESS;
2744 if (recursive) {
2745 struct efi_handler *handler;
2746 struct efi_open_protocol_info_item *item;
2747
2748 list_for_each_entry(handler, &efiobj->protocols, link) {
2749 list_for_each_entry(item, &handler->open_infos, link) {
2750 if (item->info.attributes &
2751 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2752 r = EFI_CALL(efi_connect_controller(
2753 item->info.controller_handle,
2754 driver_image_handle,
2755 remain_device_path,
2756 recursive));
2757 if (r == EFI_SUCCESS)
2758 ret = EFI_SUCCESS;
2759 }
2760 }
2761 }
2762 }
2763 /* Check for child controller specified by end node */
2764 if (ret != EFI_SUCCESS && remain_device_path &&
2765 remain_device_path->type == DEVICE_PATH_TYPE_END)
2766 ret = EFI_SUCCESS;
2767out:
2768 return EFI_EXIT(ret);
2769}
2770
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002771/*
2772 * Get all child controllers associated to a driver.
2773 * The allocated buffer has to be freed with free().
2774 *
2775 * @efiobj handle of the controller
2776 * @driver_handle handle of the driver
2777 * @number_of_children number of child controllers
2778 * @child_handle_buffer handles of the the child controllers
2779 */
2780static efi_status_t efi_get_child_controllers(
2781 struct efi_object *efiobj,
2782 efi_handle_t driver_handle,
2783 efi_uintn_t *number_of_children,
2784 efi_handle_t **child_handle_buffer)
2785{
2786 struct efi_handler *handler;
2787 struct efi_open_protocol_info_item *item;
2788 efi_uintn_t count = 0, i;
2789 bool duplicate;
2790
2791 /* Count all child controller associations */
2792 list_for_each_entry(handler, &efiobj->protocols, link) {
2793 list_for_each_entry(item, &handler->open_infos, link) {
2794 if (item->info.agent_handle == driver_handle &&
2795 item->info.attributes &
2796 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2797 ++count;
2798 }
2799 }
2800 /*
2801 * Create buffer. In case of duplicate child controller assignments
2802 * the buffer will be too large. But that does not harm.
2803 */
2804 *number_of_children = 0;
2805 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2806 if (!*child_handle_buffer)
2807 return EFI_OUT_OF_RESOURCES;
2808 /* Copy unique child handles */
2809 list_for_each_entry(handler, &efiobj->protocols, link) {
2810 list_for_each_entry(item, &handler->open_infos, link) {
2811 if (item->info.agent_handle == driver_handle &&
2812 item->info.attributes &
2813 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2814 /* Check this is a new child controller */
2815 duplicate = false;
2816 for (i = 0; i < *number_of_children; ++i) {
2817 if ((*child_handle_buffer)[i] ==
2818 item->info.controller_handle)
2819 duplicate = true;
2820 }
2821 /* Copy handle to buffer */
2822 if (!duplicate) {
2823 i = (*number_of_children)++;
2824 (*child_handle_buffer)[i] =
2825 item->info.controller_handle;
2826 }
2827 }
2828 }
2829 }
2830 return EFI_SUCCESS;
2831}
2832
2833/*
2834 * Disconnect a controller from a driver.
2835 *
2836 * This function implements the DisconnectController service.
2837 * See the Unified Extensible Firmware Interface (UEFI) specification
2838 * for details.
2839 *
2840 * @controller_handle handle of the controller
2841 * @driver_image_handle handle of the driver
2842 * @child_handle handle of the child to destroy
2843 * @return status code
2844 */
2845static efi_status_t EFIAPI efi_disconnect_controller(
2846 efi_handle_t controller_handle,
2847 efi_handle_t driver_image_handle,
2848 efi_handle_t child_handle)
2849{
2850 struct efi_driver_binding_protocol *binding_protocol;
2851 efi_handle_t *child_handle_buffer = NULL;
2852 size_t number_of_children = 0;
2853 efi_status_t r;
2854 size_t stop_count = 0;
2855 struct efi_object *efiobj;
2856
2857 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2858 child_handle);
2859
2860 efiobj = efi_search_obj(controller_handle);
2861 if (!efiobj) {
2862 r = EFI_INVALID_PARAMETER;
2863 goto out;
2864 }
2865
2866 if (child_handle && !efi_search_obj(child_handle)) {
2867 r = EFI_INVALID_PARAMETER;
2868 goto out;
2869 }
2870
2871 /* If no driver handle is supplied, disconnect all drivers */
2872 if (!driver_image_handle) {
2873 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2874 goto out;
2875 }
2876
2877 /* Create list of child handles */
2878 if (child_handle) {
2879 number_of_children = 1;
2880 child_handle_buffer = &child_handle;
2881 } else {
2882 efi_get_child_controllers(efiobj,
2883 driver_image_handle,
2884 &number_of_children,
2885 &child_handle_buffer);
2886 }
2887
2888 /* Get the driver binding protocol */
2889 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2890 &efi_guid_driver_binding_protocol,
2891 (void **)&binding_protocol,
2892 driver_image_handle, NULL,
2893 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2894 if (r != EFI_SUCCESS)
2895 goto out;
2896 /* Remove the children */
2897 if (number_of_children) {
2898 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2899 controller_handle,
2900 number_of_children,
2901 child_handle_buffer));
2902 if (r == EFI_SUCCESS)
2903 ++stop_count;
2904 }
2905 /* Remove the driver */
2906 if (!child_handle)
2907 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2908 controller_handle,
2909 0, NULL));
2910 if (r == EFI_SUCCESS)
2911 ++stop_count;
2912 EFI_CALL(efi_close_protocol(driver_image_handle,
2913 &efi_guid_driver_binding_protocol,
2914 driver_image_handle, NULL));
2915
2916 if (stop_count)
2917 r = EFI_SUCCESS;
2918 else
2919 r = EFI_NOT_FOUND;
2920out:
2921 if (!child_handle)
2922 free(child_handle_buffer);
2923 return EFI_EXIT(r);
2924}
2925
Alexander Grafc15d9212016-03-04 01:09:59 +01002926static const struct efi_boot_services efi_boot_services = {
2927 .hdr = {
2928 .headersize = sizeof(struct efi_table_hdr),
2929 },
2930 .raise_tpl = efi_raise_tpl,
2931 .restore_tpl = efi_restore_tpl,
2932 .allocate_pages = efi_allocate_pages_ext,
2933 .free_pages = efi_free_pages_ext,
2934 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02002935 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02002936 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02002937 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02002938 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002939 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02002940 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002941 .close_event = efi_close_event,
2942 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002943 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002944 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002945 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002946 .handle_protocol = efi_handle_protocol,
2947 .reserved = NULL,
2948 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02002949 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002950 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02002951 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002952 .load_image = efi_load_image,
2953 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02002954 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01002955 .unload_image = efi_unload_image,
2956 .exit_boot_services = efi_exit_boot_services,
2957 .get_next_monotonic_count = efi_get_next_monotonic_count,
2958 .stall = efi_stall,
2959 .set_watchdog_timer = efi_set_watchdog_timer,
2960 .connect_controller = efi_connect_controller,
2961 .disconnect_controller = efi_disconnect_controller,
2962 .open_protocol = efi_open_protocol,
2963 .close_protocol = efi_close_protocol,
2964 .open_protocol_information = efi_open_protocol_information,
2965 .protocols_per_handle = efi_protocols_per_handle,
2966 .locate_handle_buffer = efi_locate_handle_buffer,
2967 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002968 .install_multiple_protocol_interfaces =
2969 efi_install_multiple_protocol_interfaces,
2970 .uninstall_multiple_protocol_interfaces =
2971 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01002972 .calculate_crc32 = efi_calculate_crc32,
2973 .copy_mem = efi_copy_mem,
2974 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01002975 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01002976};
2977
Heinrich Schuchardt3579b692017-12-11 20:10:20 +01002978static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01002979
Alexander Graf393dd912016-10-14 13:45:30 +02002980struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01002981 .hdr = {
2982 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt7d495df2018-02-05 18:04:21 +01002983 .revision = 2 << 16 | 70, /* 2.7 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002984 .headersize = sizeof(struct efi_table_hdr),
2985 },
2986 .fw_vendor = (long)firmware_vendor,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002987 .con_in = (void *)&efi_con_in,
2988 .con_out = (void *)&efi_con_out,
2989 .std_err = (void *)&efi_con_out,
2990 .runtime = (void *)&efi_runtime_services,
2991 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01002992 .nr_tables = 0,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002993 .tables = (void *)efi_conf_table,
Alexander Grafc15d9212016-03-04 01:09:59 +01002994};