blob: fd35ffa359b76335237d870f2d625a5859a17d18 [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
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100281 /*
282 * Lowering the TPL may have made queued events eligible for execution.
283 */
284 efi_timer_check();
285
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200286 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100287}
288
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200289/*
290 * Allocate memory pages.
291 *
292 * This function implements the AllocatePages service.
293 * See the Unified Extensible Firmware Interface (UEFI) specification
294 * for details.
295 *
296 * @type type of allocation to be performed
297 * @memory_type usage type of the allocated memory
298 * @pages number of pages to be allocated
299 * @memory allocated memory
300 * @return status code
301 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900302static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100303 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900304 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100305{
306 efi_status_t r;
307
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100308 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100309 r = efi_allocate_pages(type, memory_type, pages, memory);
310 return EFI_EXIT(r);
311}
312
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200313/*
314 * Free memory pages.
315 *
316 * This function implements the FreePages service.
317 * See the Unified Extensible Firmware Interface (UEFI) specification
318 * for details.
319 *
320 * @memory start of the memory area to be freed
321 * @pages number of pages to be freed
322 * @return status code
323 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900324static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100325 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100326{
327 efi_status_t r;
328
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100329 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100330 r = efi_free_pages(memory, pages);
331 return EFI_EXIT(r);
332}
333
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200334/*
335 * Get map describing memory usage.
336 *
337 * This function implements the GetMemoryMap service.
338 * See the Unified Extensible Firmware Interface (UEFI) specification
339 * for details.
340 *
341 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
342 * on exit the size of the copied memory map
343 * @memory_map buffer to which the memory map is written
344 * @map_key key for the memory map
345 * @descriptor_size size of an individual memory descriptor
346 * @descriptor_version version number of the memory descriptor structure
347 * @return status code
348 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900349static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100350 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900351 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100352 efi_uintn_t *map_key,
353 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900354 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100355{
356 efi_status_t r;
357
358 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
359 map_key, descriptor_size, descriptor_version);
360 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
361 descriptor_size, descriptor_version);
362 return EFI_EXIT(r);
363}
364
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200365/*
366 * Allocate memory from pool.
367 *
368 * This function implements the AllocatePool service.
369 * See the Unified Extensible Firmware Interface (UEFI) specification
370 * for details.
371 *
372 * @pool_type type of the pool from which memory is to be allocated
373 * @size number of bytes to be allocated
374 * @buffer allocated memory
375 * @return status code
376 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200377static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100378 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200379 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100380{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100381 efi_status_t r;
382
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100383 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200384 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100385 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100386}
387
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200388/*
389 * Free memory from pool.
390 *
391 * This function implements the FreePool service.
392 * See the Unified Extensible Firmware Interface (UEFI) specification
393 * for details.
394 *
395 * @buffer start of memory to be freed
396 * @return status code
397 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200398static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100399{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100400 efi_status_t r;
401
402 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200403 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100404 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100405}
406
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200407/*
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100408 * Add a new object to the object list.
409 *
410 * The protocols list is initialized.
411 * The object handle is set.
412 *
413 * @obj object to be added
414 */
415void efi_add_handle(struct efi_object *obj)
416{
417 if (!obj)
418 return;
419 INIT_LIST_HEAD(&obj->protocols);
420 obj->handle = obj;
421 list_add_tail(&obj->link, &efi_obj_list);
422}
423
424/*
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200425 * Create handle.
426 *
427 * @handle new handle
428 * @return status code
429 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100430efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200431{
432 struct efi_object *obj;
433 efi_status_t r;
434
435 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
436 sizeof(struct efi_object),
437 (void **)&obj);
438 if (r != EFI_SUCCESS)
439 return r;
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100440 efi_add_handle(obj);
441 *handle = obj->handle;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200442 return r;
443}
444
Alexander Grafc15d9212016-03-04 01:09:59 +0100445/*
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100446 * Find a protocol on a handle.
447 *
448 * @handle handle
449 * @protocol_guid GUID of the protocol
450 * @handler reference to the protocol
451 * @return status code
452 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100453efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100454 const efi_guid_t *protocol_guid,
455 struct efi_handler **handler)
456{
457 struct efi_object *efiobj;
458 struct list_head *lhandle;
459
460 if (!handle || !protocol_guid)
461 return EFI_INVALID_PARAMETER;
462 efiobj = efi_search_obj(handle);
463 if (!efiobj)
464 return EFI_INVALID_PARAMETER;
465 list_for_each(lhandle, &efiobj->protocols) {
466 struct efi_handler *protocol;
467
468 protocol = list_entry(lhandle, struct efi_handler, link);
469 if (!guidcmp(protocol->guid, protocol_guid)) {
470 if (handler)
471 *handler = protocol;
472 return EFI_SUCCESS;
473 }
474 }
475 return EFI_NOT_FOUND;
476}
477
478/*
479 * Delete protocol from a handle.
480 *
481 * @handle handle from which the protocol shall be deleted
482 * @protocol GUID of the protocol to be deleted
483 * @protocol_interface interface of the protocol implementation
484 * @return status code
485 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100486efi_status_t efi_remove_protocol(const efi_handle_t handle,
487 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100488 void *protocol_interface)
489{
490 struct efi_handler *handler;
491 efi_status_t ret;
492
493 ret = efi_search_protocol(handle, protocol, &handler);
494 if (ret != EFI_SUCCESS)
495 return ret;
496 if (guidcmp(handler->guid, protocol))
497 return EFI_INVALID_PARAMETER;
498 list_del(&handler->link);
499 free(handler);
500 return EFI_SUCCESS;
501}
502
503/*
504 * Delete all protocols from a handle.
505 *
506 * @handle handle from which the protocols shall be deleted
507 * @return status code
508 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100509efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100510{
511 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100512 struct efi_handler *protocol;
513 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100514
515 efiobj = efi_search_obj(handle);
516 if (!efiobj)
517 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100518 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100519 efi_status_t ret;
520
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100521 ret = efi_remove_protocol(handle, protocol->guid,
522 protocol->protocol_interface);
523 if (ret != EFI_SUCCESS)
524 return ret;
525 }
526 return EFI_SUCCESS;
527}
528
529/*
530 * Delete handle.
531 *
532 * @handle handle to delete
533 */
534void efi_delete_handle(struct efi_object *obj)
535{
536 if (!obj)
537 return;
538 efi_remove_all_protocols(obj->handle);
539 list_del(&obj->link);
540 free(obj);
541}
542
543/*
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100544 * Check if a pointer is a valid event.
545 *
546 * @event pointer to check
547 * @return status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100548 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100549static efi_status_t efi_is_event(const struct efi_event *event)
550{
551 const struct efi_event *evt;
552
553 if (!event)
554 return EFI_INVALID_PARAMETER;
555 list_for_each_entry(evt, &efi_events, link) {
556 if (evt == event)
557 return EFI_SUCCESS;
558 }
559 return EFI_INVALID_PARAMETER;
560}
Alexander Grafc15d9212016-03-04 01:09:59 +0100561
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200562/*
563 * Create an event.
564 *
565 * This function is used inside U-Boot code to create an event.
566 *
567 * For the API function implementing the CreateEvent service see
568 * efi_create_event_ext.
569 *
570 * @type type of the event to create
571 * @notify_tpl task priority level of the event
572 * @notify_function notification function of the event
573 * @notify_context pointer passed to the notification function
574 * @event created event
575 * @return status code
576 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100577efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200578 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200579 struct efi_event *event,
580 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100581 void *notify_context, efi_guid_t *group,
582 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100583{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100584 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200585
Jonathan Gray7758b212017-03-12 19:26:07 +1100586 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200587 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100588
589 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200590 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100591
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100592 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
Jonathan Gray7758b212017-03-12 19:26:07 +1100593 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200594 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100595
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100596 evt = calloc(1, sizeof(struct efi_event));
597 if (!evt)
598 return EFI_OUT_OF_RESOURCES;
599 evt->type = type;
600 evt->notify_tpl = notify_tpl;
601 evt->notify_function = notify_function;
602 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100603 evt->group = group;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100604 /* Disable timers on bootup */
605 evt->trigger_next = -1ULL;
606 evt->is_queued = false;
607 evt->is_signaled = false;
608 list_add_tail(&evt->link, &efi_events);
609 *event = evt;
610 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100611}
612
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200613/*
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100614 * Create an event in a group.
615 *
616 * This function implements the CreateEventEx service.
617 * See the Unified Extensible Firmware Interface (UEFI) specification
618 * for details.
619 * TODO: Support event groups
620 *
621 * @type type of the event to create
622 * @notify_tpl task priority level of the event
623 * @notify_function notification function of the event
624 * @notify_context pointer passed to the notification function
625 * @event created event
626 * @event_group event group
627 * @return status code
628 */
629efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
630 void (EFIAPI *notify_function) (
631 struct efi_event *event,
632 void *context),
633 void *notify_context,
634 efi_guid_t *event_group,
635 struct efi_event **event)
636{
637 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
638 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100639 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100640 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100641}
642
643/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200644 * Create an event.
645 *
646 * This function implements the CreateEvent service.
647 * See the Unified Extensible Firmware Interface (UEFI) specification
648 * for details.
649 *
650 * @type type of the event to create
651 * @notify_tpl task priority level of the event
652 * @notify_function notification function of the event
653 * @notify_context pointer passed to the notification function
654 * @event created event
655 * @return status code
656 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200657static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100658 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200659 void (EFIAPI *notify_function) (
660 struct efi_event *event,
661 void *context),
662 void *notify_context, struct efi_event **event)
663{
664 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
665 notify_context);
666 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100667 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200668}
669
Alexander Grafc15d9212016-03-04 01:09:59 +0100670/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200671 * Check if a timer event has occurred or a queued notification function should
672 * be called.
673 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100674 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200675 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100676 */
677void efi_timer_check(void)
678{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100679 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100680 u64 now = timer_get_us();
681
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100682 list_for_each_entry(evt, &efi_events, link) {
683 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100684 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100685 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200686 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100687 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200688 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100689 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200690 break;
691 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100692 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200693 break;
694 default:
695 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200696 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100697 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100698 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100699 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100700 WATCHDOG_RESET();
701}
702
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200703/*
704 * Set the trigger time for a timer event or stop the event.
705 *
706 * This is the function for internal usage in U-Boot. For the API function
707 * implementing the SetTimer service see efi_set_timer_ext.
708 *
709 * @event event for which the timer is set
710 * @type type of the timer
711 * @trigger_time trigger period in multiples of 100ns
712 * @return status code
713 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200714efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200715 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100716{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100717 /* Check that the event is valid */
718 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
719 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100720
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200721 /*
722 * The parameter defines a multiple of 100ns.
723 * We use multiples of 1000ns. So divide by 10.
724 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200725 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100726
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100727 switch (type) {
728 case EFI_TIMER_STOP:
729 event->trigger_next = -1ULL;
730 break;
731 case EFI_TIMER_PERIODIC:
732 case EFI_TIMER_RELATIVE:
733 event->trigger_next = timer_get_us() + trigger_time;
734 break;
735 default:
736 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100737 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100738 event->trigger_type = type;
739 event->trigger_time = trigger_time;
740 event->is_signaled = false;
741 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100742}
743
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200744/*
745 * Set the trigger time for a timer event or stop the event.
746 *
747 * This function implements the SetTimer service.
748 * See the Unified Extensible Firmware Interface (UEFI) specification
749 * for details.
750 *
751 * @event event for which the timer is set
752 * @type type of the timer
753 * @trigger_time trigger period in multiples of 100ns
754 * @return status code
755 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200756static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
757 enum efi_timer_delay type,
758 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200759{
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100760 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200761 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
762}
763
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200764/*
765 * Wait for events to be signaled.
766 *
767 * This function implements the WaitForEvent service.
768 * See the Unified Extensible Firmware Interface (UEFI) specification
769 * for details.
770 *
771 * @num_events number of events to be waited for
772 * @events events to be waited for
773 * @index index of the event that was signaled
774 * @return status code
775 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100776static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200777 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100778 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100779{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100780 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100781
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100782 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100783
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200784 /* Check parameters */
785 if (!num_events || !event)
786 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200787 /* Check TPL */
788 if (efi_tpl != TPL_APPLICATION)
789 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200790 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100791 if (efi_is_event(event[i]) != EFI_SUCCESS)
792 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200793 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
794 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200795 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100796 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200797 }
798
799 /* Wait for signal */
800 for (;;) {
801 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200802 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200803 goto out;
804 }
805 /* Allow events to occur. */
806 efi_timer_check();
807 }
808
809out:
810 /*
811 * Reset the signal which is passed to the caller to allow periodic
812 * events to occur.
813 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200814 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200815 if (index)
816 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100817
818 return EFI_EXIT(EFI_SUCCESS);
819}
820
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200821/*
822 * Signal an EFI event.
823 *
824 * This function implements the SignalEvent service.
825 * See the Unified Extensible Firmware Interface (UEFI) specification
826 * for details.
827 *
828 * This functions sets the signaled state of the event and queues the
829 * notification function for execution.
830 *
831 * @event event to signal
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +0200832 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200833 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200834static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100835{
836 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100837 if (efi_is_event(event) != EFI_SUCCESS)
838 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100839 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100840 return EFI_EXIT(EFI_SUCCESS);
841}
842
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200843/*
844 * Close an EFI event.
845 *
846 * This function implements the CloseEvent service.
847 * See the Unified Extensible Firmware Interface (UEFI) specification
848 * for details.
849 *
850 * @event event to close
851 * @return status code
852 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200853static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100854{
855 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100856 if (efi_is_event(event) != EFI_SUCCESS)
857 return EFI_EXIT(EFI_INVALID_PARAMETER);
858 list_del(&event->link);
859 free(event);
860 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100861}
862
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200863/*
864 * Check if an event is signaled.
865 *
866 * This function implements the CheckEvent service.
867 * See the Unified Extensible Firmware Interface (UEFI) specification
868 * for details.
869 *
Heinrich Schuchardtf375f372018-02-18 11:32:02 +0100870 * If an event is not signaled yet, the notification function is queued.
871 * The signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200872 *
873 * @event event to check
874 * @return status code
875 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200876static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100877{
878 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200879 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100880 if (efi_is_event(event) != EFI_SUCCESS ||
881 event->type & EVT_NOTIFY_SIGNAL)
882 return EFI_EXIT(EFI_INVALID_PARAMETER);
883 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100884 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100885 if (event->is_signaled) {
886 event->is_signaled = false;
887 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200888 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100889 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100890}
891
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200892/*
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200893 * Find the internal EFI object for a handle.
894 *
895 * @handle handle to find
896 * @return EFI object
897 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100898struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200899{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100900 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200901
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100902 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200903 if (efiobj->handle == handle)
904 return efiobj;
905 }
906
907 return NULL;
908}
909
910/*
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100911 * Create open protocol info entry and add it to a protocol.
912 *
913 * @handler handler of a protocol
914 * @return open protocol info entry
915 */
916static struct efi_open_protocol_info_entry *efi_create_open_info(
917 struct efi_handler *handler)
918{
919 struct efi_open_protocol_info_item *item;
920
921 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
922 if (!item)
923 return NULL;
924 /* Append the item to the open protocol info list. */
925 list_add_tail(&item->link, &handler->open_infos);
926
927 return &item->info;
928}
929
930/*
931 * Remove an open protocol info entry from a protocol.
932 *
933 * @handler handler of a protocol
934 * @return status code
935 */
936static efi_status_t efi_delete_open_info(
937 struct efi_open_protocol_info_item *item)
938{
939 list_del(&item->link);
940 free(item);
941 return EFI_SUCCESS;
942}
943
944/*
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200945 * Install new protocol on a handle.
946 *
947 * @handle handle on which the protocol shall be installed
948 * @protocol GUID of the protocol to be installed
949 * @protocol_interface interface of the protocol implementation
950 * @return status code
951 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100952efi_status_t efi_add_protocol(const efi_handle_t handle,
953 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200954 void *protocol_interface)
955{
956 struct efi_object *efiobj;
957 struct efi_handler *handler;
958 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200959
960 efiobj = efi_search_obj(handle);
961 if (!efiobj)
962 return EFI_INVALID_PARAMETER;
963 ret = efi_search_protocol(handle, protocol, NULL);
964 if (ret != EFI_NOT_FOUND)
965 return EFI_INVALID_PARAMETER;
966 handler = calloc(1, sizeof(struct efi_handler));
967 if (!handler)
968 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100969 handler->guid = protocol;
970 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100971 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100972 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +0100973 if (!guidcmp(&efi_guid_device_path, protocol))
974 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100975 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200976}
977
978/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200979 * Install protocol interface.
980 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100981 * This function implements the InstallProtocolInterface service.
982 * See the Unified Extensible Firmware Interface (UEFI) specification
983 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200984 *
985 * @handle handle on which the protocol shall be installed
986 * @protocol GUID of the protocol to be installed
987 * @protocol_interface_type type of the interface to be installed,
988 * always EFI_NATIVE_INTERFACE
989 * @protocol_interface interface of the protocol implementation
990 * @return status code
991 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100992static efi_status_t EFIAPI efi_install_protocol_interface(
993 void **handle, const efi_guid_t *protocol,
994 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100995{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200996 efi_status_t r;
997
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100998 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
999 protocol_interface);
1000
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001001 if (!handle || !protocol ||
1002 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1003 r = EFI_INVALID_PARAMETER;
1004 goto out;
1005 }
1006
1007 /* Create new handle if requested. */
1008 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001009 r = efi_create_handle(handle);
1010 if (r != EFI_SUCCESS)
1011 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001012 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1013 *handle);
1014 } else {
1015 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1016 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001017 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001018 /* Add new protocol */
1019 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001020out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001021 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001022}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001023
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001024/*
1025 * Reinstall protocol interface.
1026 *
1027 * This function implements the ReinstallProtocolInterface service.
1028 * See the Unified Extensible Firmware Interface (UEFI) specification
1029 * for details.
1030 *
1031 * @handle handle on which the protocol shall be
1032 * reinstalled
1033 * @protocol GUID of the protocol to be installed
1034 * @old_interface interface to be removed
1035 * @new_interface interface to be installed
1036 * @return status code
1037 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001038static efi_status_t EFIAPI efi_reinstall_protocol_interface(
1039 efi_handle_t handle, const efi_guid_t *protocol,
1040 void *old_interface, void *new_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001041{
Rob Clark238f88c2017-09-13 18:05:41 -04001042 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01001043 new_interface);
1044 return EFI_EXIT(EFI_ACCESS_DENIED);
1045}
1046
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001047/*
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001048 * Get all drivers associated to a controller.
1049 * The allocated buffer has to be freed with free().
1050 *
1051 * @efiobj handle of the controller
1052 * @protocol protocol guid (optional)
1053 * @number_of_drivers number of child controllers
1054 * @driver_handle_buffer handles of the the drivers
1055 * @return status code
1056 */
1057static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1058 const efi_guid_t *protocol,
1059 efi_uintn_t *number_of_drivers,
1060 efi_handle_t **driver_handle_buffer)
1061{
1062 struct efi_handler *handler;
1063 struct efi_open_protocol_info_item *item;
1064 efi_uintn_t count = 0, i;
1065 bool duplicate;
1066
1067 /* Count all driver associations */
1068 list_for_each_entry(handler, &efiobj->protocols, link) {
1069 if (protocol && guidcmp(handler->guid, protocol))
1070 continue;
1071 list_for_each_entry(item, &handler->open_infos, link) {
1072 if (item->info.attributes &
1073 EFI_OPEN_PROTOCOL_BY_DRIVER)
1074 ++count;
1075 }
1076 }
1077 /*
1078 * Create buffer. In case of duplicate driver assignments the buffer
1079 * will be too large. But that does not harm.
1080 */
1081 *number_of_drivers = 0;
1082 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1083 if (!*driver_handle_buffer)
1084 return EFI_OUT_OF_RESOURCES;
1085 /* Collect unique driver handles */
1086 list_for_each_entry(handler, &efiobj->protocols, link) {
1087 if (protocol && guidcmp(handler->guid, protocol))
1088 continue;
1089 list_for_each_entry(item, &handler->open_infos, link) {
1090 if (item->info.attributes &
1091 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1092 /* Check this is a new driver */
1093 duplicate = false;
1094 for (i = 0; i < *number_of_drivers; ++i) {
1095 if ((*driver_handle_buffer)[i] ==
1096 item->info.agent_handle)
1097 duplicate = true;
1098 }
1099 /* Copy handle to buffer */
1100 if (!duplicate) {
1101 i = (*number_of_drivers)++;
1102 (*driver_handle_buffer)[i] =
1103 item->info.agent_handle;
1104 }
1105 }
1106 }
1107 }
1108 return EFI_SUCCESS;
1109}
1110
1111/*
1112 * Disconnect all drivers from a controller.
1113 *
1114 * This function implements the DisconnectController service.
1115 * See the Unified Extensible Firmware Interface (UEFI) specification
1116 * for details.
1117 *
1118 * @efiobj handle of the controller
1119 * @protocol protocol guid (optional)
1120 * @child_handle handle of the child to destroy
1121 * @return status code
1122 */
1123static efi_status_t efi_disconnect_all_drivers(
1124 struct efi_object *efiobj,
1125 const efi_guid_t *protocol,
1126 efi_handle_t child_handle)
1127{
1128 efi_uintn_t number_of_drivers;
1129 efi_handle_t *driver_handle_buffer;
1130 efi_status_t r, ret;
1131
1132 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1133 &driver_handle_buffer);
1134 if (ret != EFI_SUCCESS)
1135 return ret;
1136
1137 ret = EFI_NOT_FOUND;
1138 while (number_of_drivers) {
1139 r = EFI_CALL(efi_disconnect_controller(
1140 efiobj->handle,
1141 driver_handle_buffer[--number_of_drivers],
1142 child_handle));
1143 if (r == EFI_SUCCESS)
1144 ret = r;
1145 }
1146 free(driver_handle_buffer);
1147 return ret;
1148}
1149
1150/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001151 * Uninstall protocol interface.
1152 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001153 * This function implements the UninstallProtocolInterface service.
1154 * See the Unified Extensible Firmware Interface (UEFI) specification
1155 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001156 *
1157 * @handle handle from which the protocol shall be removed
1158 * @protocol GUID of the protocol to be removed
1159 * @protocol_interface interface to be removed
1160 * @return status code
1161 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001162static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001163 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001164 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001165{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001166 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001167 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001168 struct efi_open_protocol_info_item *item;
1169 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001170 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001171
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001172 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1173
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001174 /* Check handle */
1175 efiobj = efi_search_obj(handle);
1176 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001177 r = EFI_INVALID_PARAMETER;
1178 goto out;
1179 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001180 /* Find the protocol on the handle */
1181 r = efi_search_protocol(handle, protocol, &handler);
1182 if (r != EFI_SUCCESS)
1183 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001184 /* Disconnect controllers */
1185 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1186 if (!list_empty(&handler->open_infos)) {
1187 r = EFI_ACCESS_DENIED;
1188 goto out;
1189 }
1190 /* Close protocol */
1191 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1192 if (item->info.attributes ==
1193 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1194 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1195 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1196 list_del(&item->link);
1197 }
1198 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001199 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001200 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001201 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001202 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001203out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001204 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001205}
1206
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001207/*
1208 * Register an event for notification when a protocol is installed.
1209 *
1210 * This function implements the RegisterProtocolNotify service.
1211 * See the Unified Extensible Firmware Interface (UEFI) specification
1212 * for details.
1213 *
1214 * @protocol GUID of the protocol whose installation shall be
1215 * notified
1216 * @event event to be signaled upon installation of the protocol
1217 * @registration key for retrieving the registration information
1218 * @return status code
1219 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001220static efi_status_t EFIAPI efi_register_protocol_notify(
1221 const efi_guid_t *protocol,
1222 struct efi_event *event,
1223 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001224{
Rob Clark238f88c2017-09-13 18:05:41 -04001225 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001226 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1227}
1228
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001229/*
1230 * Determine if an EFI handle implements a protocol.
1231 *
1232 * See the documentation of the LocateHandle service in the UEFI specification.
1233 *
1234 * @search_type selection criterion
1235 * @protocol GUID of the protocol
1236 * @search_key registration key
1237 * @efiobj handle
1238 * @return 0 if the handle implements the protocol
1239 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001240static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001241 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001242 struct efi_object *efiobj)
1243{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001244 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001245
1246 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001247 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001248 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001249 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001250 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001251 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001252 case BY_PROTOCOL:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001253 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1254 return (ret != EFI_SUCCESS);
1255 default:
1256 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001257 return -1;
1258 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001259}
1260
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001261/*
1262 * Locate handles implementing a protocol.
1263 *
1264 * This function is meant for U-Boot internal calls. For the API implementation
1265 * of the LocateHandle service see efi_locate_handle_ext.
1266 *
1267 * @search_type selection criterion
1268 * @protocol GUID of the protocol
1269 * @search_key registration key
1270 * @buffer_size size of the buffer to receive the handles in bytes
1271 * @buffer buffer to receive the relevant handles
1272 * @return status code
1273 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001274static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001275 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001276 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001277 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001278{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001279 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001280 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001281
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001282 /* Check parameters */
1283 switch (search_type) {
1284 case ALL_HANDLES:
1285 break;
1286 case BY_REGISTER_NOTIFY:
1287 if (!search_key)
1288 return EFI_INVALID_PARAMETER;
1289 /* RegisterProtocolNotify is not implemented yet */
1290 return EFI_UNSUPPORTED;
1291 case BY_PROTOCOL:
1292 if (!protocol)
1293 return EFI_INVALID_PARAMETER;
1294 break;
1295 default:
1296 return EFI_INVALID_PARAMETER;
1297 }
1298
1299 /*
1300 * efi_locate_handle_buffer uses this function for
1301 * the calculation of the necessary buffer size.
1302 * So do not require a buffer for buffersize == 0.
1303 */
1304 if (!buffer_size || (*buffer_size && !buffer))
1305 return EFI_INVALID_PARAMETER;
1306
Alexander Grafc15d9212016-03-04 01:09:59 +01001307 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001308 list_for_each_entry(efiobj, &efi_obj_list, link) {
1309 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001310 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001311 }
1312
1313 if (*buffer_size < size) {
1314 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001315 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001316 }
1317
Rob Clarkcdee3372017-08-06 14:10:07 -04001318 *buffer_size = size;
1319 if (size == 0)
1320 return EFI_NOT_FOUND;
1321
Alexander Grafc15d9212016-03-04 01:09:59 +01001322 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001323 list_for_each_entry(efiobj, &efi_obj_list, link) {
1324 if (!efi_search(search_type, protocol, search_key, efiobj))
1325 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001326 }
1327
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001328 return EFI_SUCCESS;
1329}
1330
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001331/*
1332 * Locate handles implementing a protocol.
1333 *
1334 * This function implements the LocateHandle service.
1335 * See the Unified Extensible Firmware Interface (UEFI) specification
1336 * for details.
1337 *
1338 * @search_type selection criterion
1339 * @protocol GUID of the protocol
1340 * @search_key registration key
1341 * @buffer_size size of the buffer to receive the handles in bytes
1342 * @buffer buffer to receive the relevant handles
1343 * @return 0 if the handle implements the protocol
1344 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001345static efi_status_t EFIAPI efi_locate_handle_ext(
1346 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001347 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001348 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001349{
Rob Clark238f88c2017-09-13 18:05:41 -04001350 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001351 buffer_size, buffer);
1352
1353 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1354 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001355}
1356
Alexander Graffe3366f2017-07-26 13:41:04 +02001357/* Collapses configuration table entries, removing index i */
1358static void efi_remove_configuration_table(int i)
1359{
1360 struct efi_configuration_table *this = &efi_conf_table[i];
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001361 struct efi_configuration_table *next = &efi_conf_table[i + 1];
Alexander Graffe3366f2017-07-26 13:41:04 +02001362 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1363
1364 memmove(this, next, (ulong)end - (ulong)next);
1365 systab.nr_tables--;
1366}
1367
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001368/*
1369 * Adds, updates, or removes a configuration table.
1370 *
1371 * This function is used for internal calls. For the API implementation of the
1372 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1373 *
1374 * @guid GUID of the installed table
1375 * @table table to be installed
1376 * @return status code
1377 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001378efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1379 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001380{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001381 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001382 int i;
1383
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001384 if (!guid)
1385 return EFI_INVALID_PARAMETER;
1386
Alexander Grafc15d9212016-03-04 01:09:59 +01001387 /* Check for guid override */
1388 for (i = 0; i < systab.nr_tables; i++) {
1389 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001390 if (table)
1391 efi_conf_table[i].table = table;
1392 else
1393 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001394 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001395 }
1396 }
1397
Alexander Graffe3366f2017-07-26 13:41:04 +02001398 if (!table)
1399 return EFI_NOT_FOUND;
1400
Alexander Grafc15d9212016-03-04 01:09:59 +01001401 /* No override, check for overflow */
1402 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001403 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001404
1405 /* Add a new entry */
1406 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1407 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001408 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001409
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001410out:
1411 /* Notify that the configuration table was changed */
1412 list_for_each_entry(evt, &efi_events, link) {
1413 if (evt->group && !guidcmp(evt->group, guid)) {
1414 efi_signal_event(evt, false);
1415 break;
1416 }
1417 }
1418
Alexander Grafc5c11632016-08-19 01:23:24 +02001419 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001420}
1421
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001422/*
1423 * Adds, updates, or removes a configuration table.
1424 *
1425 * This function implements the InstallConfigurationTable service.
1426 * See the Unified Extensible Firmware Interface (UEFI) specification
1427 * for details.
1428 *
1429 * @guid GUID of the installed table
1430 * @table table to be installed
1431 * @return status code
1432 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001433static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1434 void *table)
1435{
Rob Clark238f88c2017-09-13 18:05:41 -04001436 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001437 return EFI_EXIT(efi_install_configuration_table(guid, table));
1438}
1439
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001440/*
1441 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001442 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001443 *
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001444 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001445 * image
1446 * @obj internal object associated with the loaded image
1447 * @device_path device path of the loaded image
1448 * @file_path file path of the loaded image
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001449 * @return status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001450 */
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001451efi_status_t efi_setup_loaded_image(
1452 struct efi_loaded_image *info, struct efi_object *obj,
1453 struct efi_device_path *device_path,
1454 struct efi_device_path *file_path)
Rob Clarkf8db9222017-09-13 18:05:33 -04001455{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001456 efi_status_t ret;
1457
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001458 /* Add internal object to object list */
1459 efi_add_handle(obj);
1460 /* efi_exit() assumes that the handle points to the info */
Rob Clarkf8db9222017-09-13 18:05:33 -04001461 obj->handle = info;
1462
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001463 info->file_path = file_path;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001464
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001465 if (device_path) {
1466 info->device_handle = efi_dp_find_obj(device_path, NULL);
1467 /*
1468 * When asking for the device path interface, return
1469 * bootefi_device_path
1470 */
1471 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1472 device_path);
1473 if (ret != EFI_SUCCESS)
1474 goto failure;
1475 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001476
1477 /*
1478 * When asking for the loaded_image interface, just
1479 * return handle which points to loaded_image_info
1480 */
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001481 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1482 if (ret != EFI_SUCCESS)
1483 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001484
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001485 ret = efi_add_protocol(obj->handle,
1486 &efi_guid_device_path_to_text_protocol,
1487 (void *)&efi_device_path_to_text);
1488 if (ret != EFI_SUCCESS)
1489 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001490
Leif Lindholmb6e6fdc2018-03-09 17:43:21 +01001491 ret = efi_add_protocol(obj->handle,
1492 &efi_guid_device_path_utilities_protocol,
1493 (void *)&efi_device_path_utilities);
1494 if (ret != EFI_SUCCESS)
1495 goto failure;
1496
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001497 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001498failure:
1499 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001500 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001501}
1502
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001503/*
1504 * Load an image using a file path.
1505 *
1506 * @file_path the path of the image to load
1507 * @buffer buffer containing the loaded image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001508 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001509 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001510efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1511 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001512{
1513 struct efi_file_info *info = NULL;
1514 struct efi_file_handle *f;
1515 static efi_status_t ret;
1516 uint64_t bs;
1517
1518 f = efi_file_from_path(file_path);
1519 if (!f)
1520 return EFI_DEVICE_ERROR;
1521
1522 bs = 0;
1523 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1524 &bs, info));
1525 if (ret == EFI_BUFFER_TOO_SMALL) {
1526 info = malloc(bs);
1527 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1528 &bs, info));
1529 }
1530 if (ret != EFI_SUCCESS)
1531 goto error;
1532
1533 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1534 if (ret)
1535 goto error;
1536
1537 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1538
1539error:
1540 free(info);
1541 EFI_CALL(f->close(f));
1542
1543 if (ret != EFI_SUCCESS) {
1544 efi_free_pool(*buffer);
1545 *buffer = NULL;
1546 }
1547
1548 return ret;
1549}
1550
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001551/*
1552 * Load an EFI image into memory.
1553 *
1554 * This function implements the LoadImage service.
1555 * See the Unified Extensible Firmware Interface (UEFI) specification
1556 * for details.
1557 *
1558 * @boot_policy true for request originating from the boot manager
Heinrich Schuchardt091cf432018-01-24 19:21:36 +01001559 * @parent_image the caller's image handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001560 * @file_path the path of the image to load
1561 * @source_buffer memory location from which the image is installed
1562 * @source_size size of the memory area from which the image is
1563 * installed
1564 * @image_handle handle for the newly installed image
1565 * @return status code
1566 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001567static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1568 efi_handle_t parent_image,
1569 struct efi_device_path *file_path,
1570 void *source_buffer,
1571 unsigned long source_size,
1572 efi_handle_t *image_handle)
1573{
Alexander Grafc15d9212016-03-04 01:09:59 +01001574 struct efi_loaded_image *info;
1575 struct efi_object *obj;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001576 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001577
Heinrich Schuchardtba784332018-01-19 20:24:40 +01001578 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001579 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001580
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001581 if (!image_handle || !parent_image) {
1582 ret = EFI_INVALID_PARAMETER;
1583 goto error;
1584 }
1585
1586 if (!source_buffer && !file_path) {
1587 ret = EFI_NOT_FOUND;
1588 goto error;
1589 }
1590
Rob Clark857a1222017-09-13 18:05:35 -04001591 info = calloc(1, sizeof(*info));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001592 if (!info) {
1593 ret = EFI_OUT_OF_RESOURCES;
1594 goto error;
1595 }
Rob Clark857a1222017-09-13 18:05:35 -04001596 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001597 if (!obj) {
1598 free(info);
1599 ret = EFI_OUT_OF_RESOURCES;
1600 goto error;
1601 }
Rob Clark857a1222017-09-13 18:05:35 -04001602
1603 if (!source_buffer) {
1604 struct efi_device_path *dp, *fp;
Rob Clark857a1222017-09-13 18:05:35 -04001605
Rob Clarkc84c1102017-09-13 18:05:38 -04001606 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001607 if (ret != EFI_SUCCESS)
1608 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001609 /*
1610 * split file_path which contains both the device and
1611 * file parts:
1612 */
1613 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001614 ret = efi_setup_loaded_image(info, obj, dp, fp);
1615 if (ret != EFI_SUCCESS)
1616 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001617 } else {
1618 /* In this case, file_path is the "device" path, ie.
1619 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1620 */
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001621 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1622 if (ret != EFI_SUCCESS)
1623 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001624 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001625 info->reserved = efi_load_pe(source_buffer, info);
1626 if (!info->reserved) {
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001627 ret = EFI_UNSUPPORTED;
1628 goto failure;
Alexander Grafc15d9212016-03-04 01:09:59 +01001629 }
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001630 info->system_table = &systab;
1631 info->parent_handle = parent_image;
Heinrich Schuchardt754d4972017-11-26 14:05:22 +01001632 *image_handle = obj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001633 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001634failure:
1635 free(info);
1636 efi_delete_handle(obj);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001637error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001638 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001639}
1640
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001641/*
1642 * Call the entry point of an image.
1643 *
1644 * This function implements the StartImage service.
1645 * See the Unified Extensible Firmware Interface (UEFI) specification
1646 * for details.
1647 *
1648 * @image_handle handle of the image
1649 * @exit_data_size size of the buffer
1650 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001651 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001652 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001653static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1654 unsigned long *exit_data_size,
1655 s16 **exit_data)
1656{
Alexander Graf54d8cdf2018-01-24 00:18:08 +01001657 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1658 struct efi_system_table *st);
Alexander Grafc15d9212016-03-04 01:09:59 +01001659 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001660 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001661
1662 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1663 entry = info->reserved;
1664
1665 efi_is_direct_boot = false;
1666
1667 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001668 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001669 /*
1670 * We called the entry point of the child image with EFI_CALL
1671 * in the lines below. The child image called the Exit() boot
1672 * service efi_exit() which executed the long jump that brought
1673 * us to the current line. This implies that the second half
1674 * of the EFI_CALL macro has not been executed.
1675 */
1676#ifdef CONFIG_ARM
1677 /*
1678 * efi_exit() called efi_restore_gd(). We have to undo this
1679 * otherwise __efi_entry_check() will put the wrong value into
1680 * app_gd.
1681 */
1682 gd = app_gd;
1683#endif
1684 /*
1685 * To get ready to call EFI_EXIT below we have to execute the
1686 * missed out steps of EFI_CALL.
1687 */
1688 assert(__efi_entry_check());
1689 debug("%sEFI: %lu returned by started image\n",
1690 __efi_nesting_dec(),
1691 (unsigned long)((uintptr_t)info->exit_status &
1692 ~EFI_ERROR_MASK));
Alexander Graf988c0662016-05-20 23:28:23 +02001693 return EFI_EXIT(info->exit_status);
1694 }
1695
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001696 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001697
Alexander Grafeff317c2018-01-26 00:47:53 +01001698 /*
1699 * Usually UEFI applications call Exit() instead of returning.
1700 * But because the world doesn not consist of ponies and unicorns,
1701 * we're happy to emulate that behavior on behalf of a payload
1702 * that forgot.
1703 */
1704 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001705}
1706
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001707/*
1708 * Leave an EFI application or driver.
1709 *
1710 * This function implements the Exit service.
1711 * See the Unified Extensible Firmware Interface (UEFI) specification
1712 * for details.
1713 *
1714 * @image_handle handle of the application or driver that is exiting
1715 * @exit_status status code
1716 * @exit_data_size size of the buffer in bytes
1717 * @exit_data buffer with data describing an error
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001718 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001719 */
Alexander Graf988c0662016-05-20 23:28:23 +02001720static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001721 efi_status_t exit_status,
1722 unsigned long exit_data_size,
1723 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001724{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001725 /*
1726 * We require that the handle points to the original loaded
1727 * image protocol interface.
1728 *
1729 * For getting the longjmp address this is safer than locating
1730 * the protocol because the protocol may have been reinstalled
1731 * pointing to another memory location.
1732 *
1733 * TODO: We should call the unload procedure of the loaded
1734 * image protocol.
1735 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001736 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001737
Alexander Grafc15d9212016-03-04 01:09:59 +01001738 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1739 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001740
Alexander Graf87e787a2017-09-03 14:14:17 +02001741 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001742 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001743
Alexander Graf87e787a2017-09-03 14:14:17 +02001744 /*
1745 * But longjmp out with the U-Boot gd, not the application's, as
1746 * the other end is a setjmp call inside EFI context.
1747 */
1748 efi_restore_gd();
1749
Alexander Graf988c0662016-05-20 23:28:23 +02001750 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001751 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001752
1753 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001754}
1755
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001756/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001757 * Unload an EFI image.
1758 *
1759 * This function implements the UnloadImage service.
1760 * See the Unified Extensible Firmware Interface (UEFI) specification
1761 * for details.
1762 *
1763 * @image_handle handle of the image to be unloaded
1764 * @return status code
1765 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001766static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001767{
1768 struct efi_object *efiobj;
1769
1770 EFI_ENTRY("%p", image_handle);
1771 efiobj = efi_search_obj(image_handle);
1772 if (efiobj)
1773 list_del(&efiobj->link);
1774
1775 return EFI_EXIT(EFI_SUCCESS);
1776}
1777
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001778/*
1779 * Fix up caches for EFI payloads if necessary.
1780 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001781static void efi_exit_caches(void)
1782{
1783#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1784 /*
1785 * Grub on 32bit ARM needs to have caches disabled before jumping into
1786 * a zImage, but does not know of all cache layers. Give it a hand.
1787 */
1788 if (efi_is_direct_boot)
1789 cleanup_before_linux();
1790#endif
1791}
1792
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001793/*
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001794 * Stop all boot services.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001795 *
1796 * This function implements the ExitBootServices service.
1797 * See the Unified Extensible Firmware Interface (UEFI) specification
1798 * for details.
1799 *
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001800 * All timer events are disabled.
1801 * For exit boot services events the notification function is called.
1802 * The boot services are disabled in the system table.
1803 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001804 * @image_handle handle of the loaded image
1805 * @map_key key of the memory map
1806 * @return status code
1807 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001808static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001809 unsigned long map_key)
1810{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001811 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001812
Alexander Grafc15d9212016-03-04 01:09:59 +01001813 EFI_ENTRY("%p, %ld", image_handle, map_key);
1814
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001815 /* Make sure that notification functions are not called anymore */
1816 efi_tpl = TPL_HIGH_LEVEL;
1817
1818 /* Check if ExitBootServices has already been called */
1819 if (!systab.boottime)
1820 return EFI_EXIT(EFI_SUCCESS);
1821
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001822 /* Add related events to the event group */
1823 list_for_each_entry(evt, &efi_events, link) {
1824 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1825 evt->group = &efi_guid_event_group_exit_boot_services;
1826 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001827 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001828 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001829 if (evt->group &&
1830 !guidcmp(evt->group,
1831 &efi_guid_event_group_exit_boot_services)) {
1832 efi_signal_event(evt, false);
1833 break;
1834 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001835 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001836
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001837 /* TODO Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001838
Alexander Graf2ebeb442016-11-17 01:02:57 +01001839 board_quiesce_devices();
1840
Alexander Grafc15d9212016-03-04 01:09:59 +01001841 /* Fix up caches for EFI payloads if necessary */
1842 efi_exit_caches();
1843
1844 /* This stops all lingering devices */
1845 bootm_disable_interrupts();
1846
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001847 /* Disable boottime services */
1848 systab.con_in_handle = NULL;
1849 systab.con_in = NULL;
1850 systab.con_out_handle = NULL;
1851 systab.con_out = NULL;
1852 systab.stderr_handle = NULL;
1853 systab.std_err = NULL;
1854 systab.boottime = NULL;
1855
1856 /* Recalculate CRC32 */
1857 systab.hdr.crc32 = 0;
1858 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1859 sizeof(struct efi_system_table));
1860
Alexander Grafc15d9212016-03-04 01:09:59 +01001861 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001862 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001863 WATCHDOG_RESET();
1864
1865 return EFI_EXIT(EFI_SUCCESS);
1866}
1867
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001868/*
1869 * Get next value of the counter.
1870 *
1871 * This function implements the NextMonotonicCount service.
1872 * See the Unified Extensible Firmware Interface (UEFI) specification
1873 * for details.
1874 *
1875 * @count returned value of the counter
1876 * @return status code
1877 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001878static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1879{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001880 static uint64_t mono;
1881
Alexander Grafc15d9212016-03-04 01:09:59 +01001882 EFI_ENTRY("%p", count);
1883 *count = mono++;
1884 return EFI_EXIT(EFI_SUCCESS);
1885}
1886
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001887/*
1888 * Sleep.
1889 *
1890 * This function implements the Stall sercive.
1891 * See the Unified Extensible Firmware Interface (UEFI) specification
1892 * for details.
1893 *
1894 * @microseconds period to sleep in microseconds
1895 * @return status code
1896 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001897static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1898{
1899 EFI_ENTRY("%ld", microseconds);
1900 udelay(microseconds);
1901 return EFI_EXIT(EFI_SUCCESS);
1902}
1903
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001904/*
1905 * Reset the watchdog timer.
1906 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001907 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001908 * See the Unified Extensible Firmware Interface (UEFI) specification
1909 * for details.
1910 *
1911 * @timeout seconds before reset by watchdog
1912 * @watchdog_code code to be logged when resetting
1913 * @data_size size of buffer in bytes
1914 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001915 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001916 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001917static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1918 uint64_t watchdog_code,
1919 unsigned long data_size,
1920 uint16_t *watchdog_data)
1921{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001922 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001923 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001924 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001925}
1926
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001927/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001928 * Close a protocol.
1929 *
1930 * This function implements the CloseProtocol service.
1931 * See the Unified Extensible Firmware Interface (UEFI) specification
1932 * for details.
1933 *
1934 * @handle handle on which the protocol shall be closed
1935 * @protocol GUID of the protocol to close
1936 * @agent_handle handle of the driver
1937 * @controller_handle handle of the controller
1938 * @return status code
1939 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001940static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001941 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001942 efi_handle_t agent_handle,
1943 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001944{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001945 struct efi_handler *handler;
1946 struct efi_open_protocol_info_item *item;
1947 struct efi_open_protocol_info_item *pos;
1948 efi_status_t r;
1949
Rob Clark238f88c2017-09-13 18:05:41 -04001950 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001951 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001952
1953 if (!agent_handle) {
1954 r = EFI_INVALID_PARAMETER;
1955 goto out;
1956 }
1957 r = efi_search_protocol(handle, protocol, &handler);
1958 if (r != EFI_SUCCESS)
1959 goto out;
1960
1961 r = EFI_NOT_FOUND;
1962 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1963 if (item->info.agent_handle == agent_handle &&
1964 item->info.controller_handle == controller_handle) {
1965 efi_delete_open_info(item);
1966 r = EFI_SUCCESS;
1967 break;
1968 }
1969 }
1970out:
1971 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001972}
1973
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001974/*
1975 * Provide information about then open status of a protocol on a handle
1976 *
1977 * This function implements the OpenProtocolInformation service.
1978 * See the Unified Extensible Firmware Interface (UEFI) specification
1979 * for details.
1980 *
1981 * @handle handle for which the information shall be retrieved
1982 * @protocol GUID of the protocol
1983 * @entry_buffer buffer to receive the open protocol information
1984 * @entry_count number of entries available in the buffer
1985 * @return status code
1986 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001987static efi_status_t EFIAPI efi_open_protocol_information(
1988 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001989 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001990 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001991{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001992 unsigned long buffer_size;
1993 unsigned long count;
1994 struct efi_handler *handler;
1995 struct efi_open_protocol_info_item *item;
1996 efi_status_t r;
1997
Rob Clark238f88c2017-09-13 18:05:41 -04001998 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001999 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002000
2001 /* Check parameters */
2002 if (!entry_buffer) {
2003 r = EFI_INVALID_PARAMETER;
2004 goto out;
2005 }
2006 r = efi_search_protocol(handle, protocol, &handler);
2007 if (r != EFI_SUCCESS)
2008 goto out;
2009
2010 /* Count entries */
2011 count = 0;
2012 list_for_each_entry(item, &handler->open_infos, link) {
2013 if (item->info.open_count)
2014 ++count;
2015 }
2016 *entry_count = count;
2017 *entry_buffer = NULL;
2018 if (!count) {
2019 r = EFI_SUCCESS;
2020 goto out;
2021 }
2022
2023 /* Copy entries */
2024 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2025 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2026 (void **)entry_buffer);
2027 if (r != EFI_SUCCESS)
2028 goto out;
2029 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2030 if (item->info.open_count)
2031 (*entry_buffer)[--count] = item->info;
2032 }
2033out:
2034 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002035}
2036
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002037/*
2038 * Get protocols installed on a handle.
2039 *
2040 * This function implements the ProtocolsPerHandleService.
2041 * See the Unified Extensible Firmware Interface (UEFI) specification
2042 * for details.
2043 *
2044 * @handle handle for which the information is retrieved
2045 * @protocol_buffer buffer with protocol GUIDs
2046 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02002047 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002048 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002049static efi_status_t EFIAPI efi_protocols_per_handle(
2050 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002051 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002052{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002053 unsigned long buffer_size;
2054 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002055 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002056 efi_status_t r;
2057
Alexander Grafc15d9212016-03-04 01:09:59 +01002058 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2059 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002060
2061 if (!handle || !protocol_buffer || !protocol_buffer_count)
2062 return EFI_EXIT(EFI_INVALID_PARAMETER);
2063
2064 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002065 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002066
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002067 efiobj = efi_search_obj(handle);
2068 if (!efiobj)
2069 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002070
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002071 /* Count protocols */
2072 list_for_each(protocol_handle, &efiobj->protocols) {
2073 ++*protocol_buffer_count;
2074 }
2075
2076 /* Copy guids */
2077 if (*protocol_buffer_count) {
2078 size_t j = 0;
2079
2080 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2081 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2082 (void **)protocol_buffer);
2083 if (r != EFI_SUCCESS)
2084 return EFI_EXIT(r);
2085 list_for_each(protocol_handle, &efiobj->protocols) {
2086 struct efi_handler *protocol;
2087
2088 protocol = list_entry(protocol_handle,
2089 struct efi_handler, link);
2090 (*protocol_buffer)[j] = (void *)protocol->guid;
2091 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002092 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002093 }
2094
2095 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002096}
2097
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002098/*
2099 * Locate handles implementing a protocol.
2100 *
2101 * This function implements the LocateHandleBuffer service.
2102 * See the Unified Extensible Firmware Interface (UEFI) specification
2103 * for details.
2104 *
2105 * @search_type selection criterion
2106 * @protocol GUID of the protocol
2107 * @search_key registration key
2108 * @no_handles number of returned handles
2109 * @buffer buffer with the returned handles
2110 * @return status code
2111 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002112static efi_status_t EFIAPI efi_locate_handle_buffer(
2113 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002114 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002115 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002116{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002117 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002118 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002119
Rob Clark238f88c2017-09-13 18:05:41 -04002120 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002121 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002122
2123 if (!no_handles || !buffer) {
2124 r = EFI_INVALID_PARAMETER;
2125 goto out;
2126 }
2127 *no_handles = 0;
2128 *buffer = NULL;
2129 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2130 *buffer);
2131 if (r != EFI_BUFFER_TOO_SMALL)
2132 goto out;
2133 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2134 (void **)buffer);
2135 if (r != EFI_SUCCESS)
2136 goto out;
2137 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2138 *buffer);
2139 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002140 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002141out:
2142 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002143}
2144
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002145/*
2146 * Find an interface implementing a protocol.
2147 *
2148 * This function implements the LocateProtocol service.
2149 * See the Unified Extensible Firmware Interface (UEFI) specification
2150 * for details.
2151 *
2152 * @protocol GUID of the protocol
2153 * @registration registration key passed to the notification function
2154 * @protocol_interface interface implementing the protocol
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02002155 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002156 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002157static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002158 void *registration,
2159 void **protocol_interface)
2160{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002161 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002162 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002163
Rob Clark238f88c2017-09-13 18:05:41 -04002164 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002165
2166 if (!protocol || !protocol_interface)
2167 return EFI_EXIT(EFI_INVALID_PARAMETER);
2168
2169 list_for_each(lhandle, &efi_obj_list) {
2170 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002171 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002172
2173 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002174
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002175 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2176 if (ret == EFI_SUCCESS) {
2177 *protocol_interface = handler->protocol_interface;
2178 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002179 }
2180 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002181 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002182
2183 return EFI_EXIT(EFI_NOT_FOUND);
2184}
2185
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002186/*
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002187 * Get the device path and handle of an device implementing a protocol.
2188 *
2189 * This function implements the LocateDevicePath service.
2190 * See the Unified Extensible Firmware Interface (UEFI) specification
2191 * for details.
2192 *
2193 * @protocol GUID of the protocol
2194 * @device_path device path
2195 * @device handle of the device
2196 * @return status code
2197 */
2198static efi_status_t EFIAPI efi_locate_device_path(
2199 const efi_guid_t *protocol,
2200 struct efi_device_path **device_path,
2201 efi_handle_t *device)
2202{
2203 struct efi_device_path *dp;
2204 size_t i;
2205 struct efi_handler *handler;
2206 efi_handle_t *handles;
2207 size_t len, len_dp;
2208 size_t len_best = 0;
2209 efi_uintn_t no_handles;
2210 u8 *remainder;
2211 efi_status_t ret;
2212
2213 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2214
2215 if (!protocol || !device_path || !*device_path || !device) {
2216 ret = EFI_INVALID_PARAMETER;
2217 goto out;
2218 }
2219
2220 /* Find end of device path */
2221 len = efi_dp_size(*device_path);
2222
2223 /* Get all handles implementing the protocol */
2224 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2225 &no_handles, &handles));
2226 if (ret != EFI_SUCCESS)
2227 goto out;
2228
2229 for (i = 0; i < no_handles; ++i) {
2230 /* Find the device path protocol */
2231 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2232 &handler);
2233 if (ret != EFI_SUCCESS)
2234 continue;
2235 dp = (struct efi_device_path *)handler->protocol_interface;
2236 len_dp = efi_dp_size(dp);
2237 /*
2238 * This handle can only be a better fit
2239 * if its device path length is longer than the best fit and
2240 * if its device path length is shorter of equal the searched
2241 * device path.
2242 */
2243 if (len_dp <= len_best || len_dp > len)
2244 continue;
2245 /* Check if dp is a subpath of device_path */
2246 if (memcmp(*device_path, dp, len_dp))
2247 continue;
2248 *device = handles[i];
2249 len_best = len_dp;
2250 }
2251 if (len_best) {
2252 remainder = (u8 *)*device_path + len_best;
2253 *device_path = (struct efi_device_path *)remainder;
2254 ret = EFI_SUCCESS;
2255 } else {
2256 ret = EFI_NOT_FOUND;
2257 }
2258out:
2259 return EFI_EXIT(ret);
2260}
2261
2262/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002263 * Install multiple protocol interfaces.
2264 *
2265 * This function implements the MultipleProtocolInterfaces service.
2266 * See the Unified Extensible Firmware Interface (UEFI) specification
2267 * for details.
2268 *
2269 * @handle handle on which the protocol interfaces shall be installed
2270 * @... NULL terminated argument list with pairs of protocol GUIDS and
2271 * interfaces
2272 * @return status code
2273 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002274static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2275 void **handle, ...)
2276{
2277 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002278
2279 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002280 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002281 void *protocol_interface;
2282 efi_status_t r = EFI_SUCCESS;
2283 int i = 0;
2284
2285 if (!handle)
2286 return EFI_EXIT(EFI_INVALID_PARAMETER);
2287
2288 va_start(argptr, handle);
2289 for (;;) {
2290 protocol = va_arg(argptr, efi_guid_t*);
2291 if (!protocol)
2292 break;
2293 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002294 r = EFI_CALL(efi_install_protocol_interface(
2295 handle, protocol,
2296 EFI_NATIVE_INTERFACE,
2297 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002298 if (r != EFI_SUCCESS)
2299 break;
2300 i++;
2301 }
2302 va_end(argptr);
2303 if (r == EFI_SUCCESS)
2304 return EFI_EXIT(r);
2305
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002306 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002307 va_start(argptr, handle);
2308 for (; i; --i) {
2309 protocol = va_arg(argptr, efi_guid_t*);
2310 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002311 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2312 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002313 }
2314 va_end(argptr);
2315
2316 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002317}
2318
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002319/*
2320 * Uninstall multiple protocol interfaces.
2321 *
2322 * This function implements the UninstallMultipleProtocolInterfaces service.
2323 * See the Unified Extensible Firmware Interface (UEFI) specification
2324 * for details.
2325 *
2326 * @handle handle from which the protocol interfaces shall be removed
2327 * @... NULL terminated argument list with pairs of protocol GUIDS and
2328 * interfaces
2329 * @return status code
2330 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002331static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2332 void *handle, ...)
2333{
2334 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002335
2336 va_list argptr;
2337 const efi_guid_t *protocol;
2338 void *protocol_interface;
2339 efi_status_t r = EFI_SUCCESS;
2340 size_t i = 0;
2341
2342 if (!handle)
2343 return EFI_EXIT(EFI_INVALID_PARAMETER);
2344
2345 va_start(argptr, handle);
2346 for (;;) {
2347 protocol = va_arg(argptr, efi_guid_t*);
2348 if (!protocol)
2349 break;
2350 protocol_interface = va_arg(argptr, void*);
2351 r = EFI_CALL(efi_uninstall_protocol_interface(
2352 handle, protocol,
2353 protocol_interface));
2354 if (r != EFI_SUCCESS)
2355 break;
2356 i++;
2357 }
2358 va_end(argptr);
2359 if (r == EFI_SUCCESS)
2360 return EFI_EXIT(r);
2361
2362 /* If an error occurred undo all changes. */
2363 va_start(argptr, handle);
2364 for (; i; --i) {
2365 protocol = va_arg(argptr, efi_guid_t*);
2366 protocol_interface = va_arg(argptr, void*);
2367 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2368 EFI_NATIVE_INTERFACE,
2369 protocol_interface));
2370 }
2371 va_end(argptr);
2372
2373 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002374}
2375
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002376/*
2377 * Calculate cyclic redundancy code.
2378 *
2379 * This function implements the CalculateCrc32 service.
2380 * See the Unified Extensible Firmware Interface (UEFI) specification
2381 * for details.
2382 *
2383 * @data buffer with data
2384 * @data_size size of buffer in bytes
2385 * @crc32_p cyclic redundancy code
2386 * @return status code
2387 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002388static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2389 unsigned long data_size,
2390 uint32_t *crc32_p)
2391{
2392 EFI_ENTRY("%p, %ld", data, data_size);
2393 *crc32_p = crc32(0, data, data_size);
2394 return EFI_EXIT(EFI_SUCCESS);
2395}
2396
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002397/*
2398 * Copy memory.
2399 *
2400 * This function implements the CopyMem service.
2401 * See the Unified Extensible Firmware Interface (UEFI) specification
2402 * for details.
2403 *
2404 * @destination destination of the copy operation
2405 * @source source of the copy operation
2406 * @length number of bytes to copy
2407 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002408static void EFIAPI efi_copy_mem(void *destination, const void *source,
2409 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002410{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002411 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01002412 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002413 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002414}
2415
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002416/*
2417 * Fill memory with a byte value.
2418 *
2419 * This function implements the SetMem service.
2420 * See the Unified Extensible Firmware Interface (UEFI) specification
2421 * for details.
2422 *
2423 * @buffer buffer to fill
2424 * @size size of buffer in bytes
2425 * @value byte to copy to the buffer
2426 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002427static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002428{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002429 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002430 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002431 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002432}
2433
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002434/*
2435 * Open protocol interface on a handle.
2436 *
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002437 * @handler handler of a protocol
2438 * @protocol_interface interface implementing the protocol
2439 * @agent_handle handle of the driver
2440 * @controller_handle handle of the controller
2441 * @attributes attributes indicating how to open the protocol
2442 * @return status code
2443 */
2444static efi_status_t efi_protocol_open(
2445 struct efi_handler *handler,
2446 void **protocol_interface, void *agent_handle,
2447 void *controller_handle, uint32_t attributes)
2448{
2449 struct efi_open_protocol_info_item *item;
2450 struct efi_open_protocol_info_entry *match = NULL;
2451 bool opened_by_driver = false;
2452 bool opened_exclusive = false;
2453
2454 /* If there is no agent, only return the interface */
2455 if (!agent_handle)
2456 goto out;
2457
2458 /* For TEST_PROTOCOL ignore interface attribute */
2459 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2460 *protocol_interface = NULL;
2461
2462 /*
2463 * Check if the protocol is already opened by a driver with the same
2464 * attributes or opened exclusively
2465 */
2466 list_for_each_entry(item, &handler->open_infos, link) {
2467 if (item->info.agent_handle == agent_handle) {
2468 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2469 (item->info.attributes == attributes))
2470 return EFI_ALREADY_STARTED;
2471 }
2472 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2473 opened_exclusive = true;
2474 }
2475
2476 /* Only one controller can open the protocol exclusively */
2477 if (opened_exclusive && attributes &
2478 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2479 return EFI_ACCESS_DENIED;
2480
2481 /* Prepare exclusive opening */
2482 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2483 /* Try to disconnect controllers */
2484 list_for_each_entry(item, &handler->open_infos, link) {
2485 if (item->info.attributes ==
2486 EFI_OPEN_PROTOCOL_BY_DRIVER)
2487 EFI_CALL(efi_disconnect_controller(
2488 item->info.controller_handle,
2489 item->info.agent_handle,
2490 NULL));
2491 }
2492 opened_by_driver = false;
2493 /* Check if all controllers are disconnected */
2494 list_for_each_entry(item, &handler->open_infos, link) {
2495 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2496 opened_by_driver = true;
2497 }
2498 /* Only one controller can be conncected */
2499 if (opened_by_driver)
2500 return EFI_ACCESS_DENIED;
2501 }
2502
2503 /* Find existing entry */
2504 list_for_each_entry(item, &handler->open_infos, link) {
2505 if (item->info.agent_handle == agent_handle &&
2506 item->info.controller_handle == controller_handle)
2507 match = &item->info;
2508 }
2509 /* None found, create one */
2510 if (!match) {
2511 match = efi_create_open_info(handler);
2512 if (!match)
2513 return EFI_OUT_OF_RESOURCES;
2514 }
2515
2516 match->agent_handle = agent_handle;
2517 match->controller_handle = controller_handle;
2518 match->attributes = attributes;
2519 match->open_count++;
2520
2521out:
2522 /* For TEST_PROTOCOL ignore interface attribute. */
2523 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2524 *protocol_interface = handler->protocol_interface;
2525
2526 return EFI_SUCCESS;
2527}
2528
2529/*
2530 * Open protocol interface on a handle.
2531 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002532 * This function implements the OpenProtocol interface.
2533 * See the Unified Extensible Firmware Interface (UEFI) specification
2534 * for details.
2535 *
2536 * @handle handle on which the protocol shall be opened
2537 * @protocol GUID of the protocol
2538 * @protocol_interface interface implementing the protocol
2539 * @agent_handle handle of the driver
2540 * @controller_handle handle of the controller
2541 * @attributes attributes indicating how to open the protocol
2542 * @return status code
2543 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002544static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002545 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002546 void **protocol_interface, void *agent_handle,
2547 void *controller_handle, uint32_t attributes)
2548{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002549 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002550 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002551
Rob Clark238f88c2017-09-13 18:05:41 -04002552 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002553 protocol_interface, agent_handle, controller_handle,
2554 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002555
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002556 if (!handle || !protocol ||
2557 (!protocol_interface && attributes !=
2558 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002559 goto out;
2560 }
2561
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002562 switch (attributes) {
2563 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2564 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2565 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2566 break;
2567 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2568 if (controller_handle == handle)
2569 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002570 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002571 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2572 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002573 /* Check that the controller handle is valid */
2574 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002575 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002576 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002577 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002578 /* Check that the agent handle is valid */
2579 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002580 goto out;
2581 break;
2582 default:
2583 goto out;
2584 }
2585
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002586 r = efi_search_protocol(handle, protocol, &handler);
2587 if (r != EFI_SUCCESS)
2588 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002589
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002590 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2591 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002592out:
2593 return EFI_EXIT(r);
2594}
2595
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002596/*
2597 * Get interface of a protocol on a handle.
2598 *
2599 * This function implements the HandleProtocol service.
2600 * See the Unified Extensible Firmware Interface (UEFI) specification
2601 * for details.
2602 *
2603 * @handle handle on which the protocol shall be opened
2604 * @protocol GUID of the protocol
2605 * @protocol_interface interface implementing the protocol
2606 * @return status code
2607 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002608static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002609 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002610 void **protocol_interface)
2611{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002612 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2613 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002614}
2615
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002616static efi_status_t efi_bind_controller(
2617 efi_handle_t controller_handle,
2618 efi_handle_t driver_image_handle,
2619 struct efi_device_path *remain_device_path)
2620{
2621 struct efi_driver_binding_protocol *binding_protocol;
2622 efi_status_t r;
2623
2624 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2625 &efi_guid_driver_binding_protocol,
2626 (void **)&binding_protocol,
2627 driver_image_handle, NULL,
2628 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2629 if (r != EFI_SUCCESS)
2630 return r;
2631 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2632 controller_handle,
2633 remain_device_path));
2634 if (r == EFI_SUCCESS)
2635 r = EFI_CALL(binding_protocol->start(binding_protocol,
2636 controller_handle,
2637 remain_device_path));
2638 EFI_CALL(efi_close_protocol(driver_image_handle,
2639 &efi_guid_driver_binding_protocol,
2640 driver_image_handle, NULL));
2641 return r;
2642}
2643
2644static efi_status_t efi_connect_single_controller(
2645 efi_handle_t controller_handle,
2646 efi_handle_t *driver_image_handle,
2647 struct efi_device_path *remain_device_path)
2648{
2649 efi_handle_t *buffer;
2650 size_t count;
2651 size_t i;
2652 efi_status_t r;
2653 size_t connected = 0;
2654
2655 /* Get buffer with all handles with driver binding protocol */
2656 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2657 &efi_guid_driver_binding_protocol,
2658 NULL, &count, &buffer));
2659 if (r != EFI_SUCCESS)
2660 return r;
2661
2662 /* Context Override */
2663 if (driver_image_handle) {
2664 for (; *driver_image_handle; ++driver_image_handle) {
2665 for (i = 0; i < count; ++i) {
2666 if (buffer[i] == *driver_image_handle) {
2667 buffer[i] = NULL;
2668 r = efi_bind_controller(
2669 controller_handle,
2670 *driver_image_handle,
2671 remain_device_path);
2672 /*
2673 * For drivers that do not support the
2674 * controller or are already connected
2675 * we receive an error code here.
2676 */
2677 if (r == EFI_SUCCESS)
2678 ++connected;
2679 }
2680 }
2681 }
2682 }
2683
2684 /*
2685 * TODO: Some overrides are not yet implemented:
2686 * - Platform Driver Override
2687 * - Driver Family Override Search
2688 * - Bus Specific Driver Override
2689 */
2690
2691 /* Driver Binding Search */
2692 for (i = 0; i < count; ++i) {
2693 if (buffer[i]) {
2694 r = efi_bind_controller(controller_handle,
2695 buffer[i],
2696 remain_device_path);
2697 if (r == EFI_SUCCESS)
2698 ++connected;
2699 }
2700 }
2701
2702 efi_free_pool(buffer);
2703 if (!connected)
2704 return EFI_NOT_FOUND;
2705 return EFI_SUCCESS;
2706}
2707
2708/*
2709 * Connect a controller to a driver.
2710 *
2711 * This function implements the ConnectController service.
2712 * See the Unified Extensible Firmware Interface (UEFI) specification
2713 * for details.
2714 *
2715 * First all driver binding protocol handles are tried for binding drivers.
2716 * Afterwards all handles that have openened a protocol of the controller
2717 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2718 *
2719 * @controller_handle handle of the controller
2720 * @driver_image_handle handle of the driver
2721 * @remain_device_path device path of a child controller
2722 * @recursive true to connect all child controllers
2723 * @return status code
2724 */
2725static efi_status_t EFIAPI efi_connect_controller(
2726 efi_handle_t controller_handle,
2727 efi_handle_t *driver_image_handle,
2728 struct efi_device_path *remain_device_path,
2729 bool recursive)
2730{
2731 efi_status_t r;
2732 efi_status_t ret = EFI_NOT_FOUND;
2733 struct efi_object *efiobj;
2734
2735 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2736 remain_device_path, recursive);
2737
2738 efiobj = efi_search_obj(controller_handle);
2739 if (!efiobj) {
2740 ret = EFI_INVALID_PARAMETER;
2741 goto out;
2742 }
2743
2744 r = efi_connect_single_controller(controller_handle,
2745 driver_image_handle,
2746 remain_device_path);
2747 if (r == EFI_SUCCESS)
2748 ret = EFI_SUCCESS;
2749 if (recursive) {
2750 struct efi_handler *handler;
2751 struct efi_open_protocol_info_item *item;
2752
2753 list_for_each_entry(handler, &efiobj->protocols, link) {
2754 list_for_each_entry(item, &handler->open_infos, link) {
2755 if (item->info.attributes &
2756 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2757 r = EFI_CALL(efi_connect_controller(
2758 item->info.controller_handle,
2759 driver_image_handle,
2760 remain_device_path,
2761 recursive));
2762 if (r == EFI_SUCCESS)
2763 ret = EFI_SUCCESS;
2764 }
2765 }
2766 }
2767 }
2768 /* Check for child controller specified by end node */
2769 if (ret != EFI_SUCCESS && remain_device_path &&
2770 remain_device_path->type == DEVICE_PATH_TYPE_END)
2771 ret = EFI_SUCCESS;
2772out:
2773 return EFI_EXIT(ret);
2774}
2775
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002776/*
2777 * Get all child controllers associated to a driver.
2778 * The allocated buffer has to be freed with free().
2779 *
2780 * @efiobj handle of the controller
2781 * @driver_handle handle of the driver
2782 * @number_of_children number of child controllers
2783 * @child_handle_buffer handles of the the child controllers
2784 */
2785static efi_status_t efi_get_child_controllers(
2786 struct efi_object *efiobj,
2787 efi_handle_t driver_handle,
2788 efi_uintn_t *number_of_children,
2789 efi_handle_t **child_handle_buffer)
2790{
2791 struct efi_handler *handler;
2792 struct efi_open_protocol_info_item *item;
2793 efi_uintn_t count = 0, i;
2794 bool duplicate;
2795
2796 /* Count all child controller associations */
2797 list_for_each_entry(handler, &efiobj->protocols, link) {
2798 list_for_each_entry(item, &handler->open_infos, link) {
2799 if (item->info.agent_handle == driver_handle &&
2800 item->info.attributes &
2801 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2802 ++count;
2803 }
2804 }
2805 /*
2806 * Create buffer. In case of duplicate child controller assignments
2807 * the buffer will be too large. But that does not harm.
2808 */
2809 *number_of_children = 0;
2810 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2811 if (!*child_handle_buffer)
2812 return EFI_OUT_OF_RESOURCES;
2813 /* Copy unique child handles */
2814 list_for_each_entry(handler, &efiobj->protocols, link) {
2815 list_for_each_entry(item, &handler->open_infos, link) {
2816 if (item->info.agent_handle == driver_handle &&
2817 item->info.attributes &
2818 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2819 /* Check this is a new child controller */
2820 duplicate = false;
2821 for (i = 0; i < *number_of_children; ++i) {
2822 if ((*child_handle_buffer)[i] ==
2823 item->info.controller_handle)
2824 duplicate = true;
2825 }
2826 /* Copy handle to buffer */
2827 if (!duplicate) {
2828 i = (*number_of_children)++;
2829 (*child_handle_buffer)[i] =
2830 item->info.controller_handle;
2831 }
2832 }
2833 }
2834 }
2835 return EFI_SUCCESS;
2836}
2837
2838/*
2839 * Disconnect a controller from a driver.
2840 *
2841 * This function implements the DisconnectController service.
2842 * See the Unified Extensible Firmware Interface (UEFI) specification
2843 * for details.
2844 *
2845 * @controller_handle handle of the controller
2846 * @driver_image_handle handle of the driver
2847 * @child_handle handle of the child to destroy
2848 * @return status code
2849 */
2850static efi_status_t EFIAPI efi_disconnect_controller(
2851 efi_handle_t controller_handle,
2852 efi_handle_t driver_image_handle,
2853 efi_handle_t child_handle)
2854{
2855 struct efi_driver_binding_protocol *binding_protocol;
2856 efi_handle_t *child_handle_buffer = NULL;
2857 size_t number_of_children = 0;
2858 efi_status_t r;
2859 size_t stop_count = 0;
2860 struct efi_object *efiobj;
2861
2862 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2863 child_handle);
2864
2865 efiobj = efi_search_obj(controller_handle);
2866 if (!efiobj) {
2867 r = EFI_INVALID_PARAMETER;
2868 goto out;
2869 }
2870
2871 if (child_handle && !efi_search_obj(child_handle)) {
2872 r = EFI_INVALID_PARAMETER;
2873 goto out;
2874 }
2875
2876 /* If no driver handle is supplied, disconnect all drivers */
2877 if (!driver_image_handle) {
2878 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2879 goto out;
2880 }
2881
2882 /* Create list of child handles */
2883 if (child_handle) {
2884 number_of_children = 1;
2885 child_handle_buffer = &child_handle;
2886 } else {
2887 efi_get_child_controllers(efiobj,
2888 driver_image_handle,
2889 &number_of_children,
2890 &child_handle_buffer);
2891 }
2892
2893 /* Get the driver binding protocol */
2894 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2895 &efi_guid_driver_binding_protocol,
2896 (void **)&binding_protocol,
2897 driver_image_handle, NULL,
2898 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2899 if (r != EFI_SUCCESS)
2900 goto out;
2901 /* Remove the children */
2902 if (number_of_children) {
2903 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2904 controller_handle,
2905 number_of_children,
2906 child_handle_buffer));
2907 if (r == EFI_SUCCESS)
2908 ++stop_count;
2909 }
2910 /* Remove the driver */
2911 if (!child_handle)
2912 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2913 controller_handle,
2914 0, NULL));
2915 if (r == EFI_SUCCESS)
2916 ++stop_count;
2917 EFI_CALL(efi_close_protocol(driver_image_handle,
2918 &efi_guid_driver_binding_protocol,
2919 driver_image_handle, NULL));
2920
2921 if (stop_count)
2922 r = EFI_SUCCESS;
2923 else
2924 r = EFI_NOT_FOUND;
2925out:
2926 if (!child_handle)
2927 free(child_handle_buffer);
2928 return EFI_EXIT(r);
2929}
2930
Alexander Grafc15d9212016-03-04 01:09:59 +01002931static const struct efi_boot_services efi_boot_services = {
2932 .hdr = {
2933 .headersize = sizeof(struct efi_table_hdr),
2934 },
2935 .raise_tpl = efi_raise_tpl,
2936 .restore_tpl = efi_restore_tpl,
2937 .allocate_pages = efi_allocate_pages_ext,
2938 .free_pages = efi_free_pages_ext,
2939 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02002940 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02002941 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02002942 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02002943 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002944 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02002945 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002946 .close_event = efi_close_event,
2947 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002948 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002949 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002950 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002951 .handle_protocol = efi_handle_protocol,
2952 .reserved = NULL,
2953 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02002954 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002955 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02002956 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002957 .load_image = efi_load_image,
2958 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02002959 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01002960 .unload_image = efi_unload_image,
2961 .exit_boot_services = efi_exit_boot_services,
2962 .get_next_monotonic_count = efi_get_next_monotonic_count,
2963 .stall = efi_stall,
2964 .set_watchdog_timer = efi_set_watchdog_timer,
2965 .connect_controller = efi_connect_controller,
2966 .disconnect_controller = efi_disconnect_controller,
2967 .open_protocol = efi_open_protocol,
2968 .close_protocol = efi_close_protocol,
2969 .open_protocol_information = efi_open_protocol_information,
2970 .protocols_per_handle = efi_protocols_per_handle,
2971 .locate_handle_buffer = efi_locate_handle_buffer,
2972 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002973 .install_multiple_protocol_interfaces =
2974 efi_install_multiple_protocol_interfaces,
2975 .uninstall_multiple_protocol_interfaces =
2976 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01002977 .calculate_crc32 = efi_calculate_crc32,
2978 .copy_mem = efi_copy_mem,
2979 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01002980 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01002981};
2982
Heinrich Schuchardt3579b692017-12-11 20:10:20 +01002983static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01002984
Alexander Graf393dd912016-10-14 13:45:30 +02002985struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01002986 .hdr = {
2987 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt7d495df2018-02-05 18:04:21 +01002988 .revision = 2 << 16 | 70, /* 2.7 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002989 .headersize = sizeof(struct efi_table_hdr),
2990 },
2991 .fw_vendor = (long)firmware_vendor,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002992 .con_in = (void *)&efi_con_in,
2993 .con_out = (void *)&efi_con_out,
2994 .std_err = (void *)&efi_con_out,
2995 .runtime = (void *)&efi_runtime_services,
2996 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01002997 .nr_tables = 0,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002998 .tables = (void *)efi_conf_table,
Alexander Grafc15d9212016-03-04 01:09:59 +01002999};