blob: 02bc9fdcf056c6cd68178be049663ddc94326bfa [file] [log] [blame]
Alexander Grafbee91162016-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 Grafbee91162016-03-04 01:09:59 +01009#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +020010#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010011#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040012#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010013#include <malloc.h>
14#include <asm/global_data.h>
15#include <libfdt_env.h>
16#include <u-boot/crc.h>
17#include <bootm.h>
18#include <inttypes.h>
19#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020025
Alexander Grafbee91162016-03-04 01:09:59 +010026/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
29/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
37/*
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
41 *
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
44 */
Alexander Graf3c63db92016-10-14 13:45:30 +020045static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafbee91162016-03-04 01:09:59 +010046
Simon Glass65e4c0b2016-09-25 15:27:35 -060047#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010048/*
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
54static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060055#endif
Alexander Grafbee91162016-03-04 01:09:59 +010056
Rob Clarkc160d2f2017-07-27 08:04:18 -040057static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040058static int nesting_level;
Rob Clarkc160d2f2017-07-27 08:04:18 -040059
60/* Called on every callback entry */
61int __efi_entry_check(void)
62{
63 int ret = entry_count++ == 0;
64#ifdef CONFIG_ARM
65 assert(efi_gd);
66 app_gd = gd;
67 gd = efi_gd;
68#endif
69 return ret;
70}
71
72/* Called on every callback exit */
73int __efi_exit_check(void)
74{
75 int ret = --entry_count == 0;
76#ifdef CONFIG_ARM
77 gd = app_gd;
78#endif
79 return ret;
80}
81
Alexander Grafbee91162016-03-04 01:09:59 +010082/* Called from do_bootefi_exec() */
83void efi_save_gd(void)
84{
Simon Glass65e4c0b2016-09-25 15:27:35 -060085#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010086 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060087#endif
Alexander Grafbee91162016-03-04 01:09:59 +010088}
89
Rob Clarkc160d2f2017-07-27 08:04:18 -040090/*
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
94 */
Alexander Grafbee91162016-03-04 01:09:59 +010095void efi_restore_gd(void)
96{
Simon Glass65e4c0b2016-09-25 15:27:35 -060097#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010098 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100101 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600102#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100103}
104
Rob Clarkaf65db82017-07-27 08:04:19 -0400105/*
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
108 */
109static const char *indent_string(int level)
110{
111 const char *indent = " ";
112 const int max = strlen(indent);
113 level = min(max, level * 2);
114 return &indent[max - level];
115}
116
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200117const char *__efi_nesting(void)
118{
119 return indent_string(nesting_level);
120}
121
Rob Clarkaf65db82017-07-27 08:04:19 -0400122const char *__efi_nesting_inc(void)
123{
124 return indent_string(nesting_level++);
125}
126
127const char *__efi_nesting_dec(void)
128{
129 return indent_string(--nesting_level);
130}
131
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200132/*
133 * Queue an EFI event.
134 *
135 * This function queues the notification function of the event for future
136 * execution.
137 *
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
140 *
141 * For the SignalEvent service see efi_signal_event_ext.
142 *
143 * @event event to signal
144 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200145void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200146{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200147 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200148 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200154 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200155 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200156}
157
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200158/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200159 * Raise the task priority level.
160 *
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
163 * for details.
164 *
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
167 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100168static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100169{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100170 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200171
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200172 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200173
174 if (new_tpl < efi_tpl)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176 efi_tpl = new_tpl;
177 if (efi_tpl > TPL_HIGH_LEVEL)
178 efi_tpl = TPL_HIGH_LEVEL;
179
180 EFI_EXIT(EFI_SUCCESS);
181 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100182}
183
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200184/*
185 * Lower the task priority level.
186 *
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
189 * for details.
190 *
191 * @old_tpl value of the task priority level to be restored
192 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100193static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100194{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200195 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200196
197 if (old_tpl > efi_tpl)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199 efi_tpl = old_tpl;
200 if (efi_tpl > TPL_HIGH_LEVEL)
201 efi_tpl = TPL_HIGH_LEVEL;
202
203 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100204}
205
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200206/*
207 * Allocate memory pages.
208 *
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
211 * for details.
212 *
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
218 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900219static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100220 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900221 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100222{
223 efi_status_t r;
224
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228}
229
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200230/*
231 * Free memory pages.
232 *
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
235 * for details.
236 *
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
240 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900241static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100242 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100243{
244 efi_status_t r;
245
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249}
250
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200251/*
252 * Get map describing memory usage.
253 *
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
256 * for details.
257 *
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
265 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900266static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100267 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900268 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900271 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100272{
273 efi_status_t r;
274
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 map_key, descriptor_size, descriptor_version);
277 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 descriptor_size, descriptor_version);
279 return EFI_EXIT(r);
280}
281
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200282/*
283 * Allocate memory from pool.
284 *
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
287 * for details.
288 *
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
293 */
Stefan Brünsead12742016-10-09 22:17:18 +0200294static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100295 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200296 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100297{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100298 efi_status_t r;
299
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200301 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100302 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100303}
304
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200305/*
306 * Free memory from pool.
307 *
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
310 * for details.
311 *
312 * @buffer start of memory to be freed
313 * @return status code
314 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200315static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100316{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200320 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100321 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100322}
323
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200324/*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100325 * Add a new object to the object list.
326 *
327 * The protocols list is initialized.
328 * The object handle is set.
329 *
330 * @obj object to be added
331 */
332void efi_add_handle(struct efi_object *obj)
333{
334 if (!obj)
335 return;
336 INIT_LIST_HEAD(&obj->protocols);
337 obj->handle = obj;
338 list_add_tail(&obj->link, &efi_obj_list);
339}
340
341/*
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200342 * Create handle.
343 *
344 * @handle new handle
345 * @return status code
346 */
347efi_status_t efi_create_handle(void **handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200348{
349 struct efi_object *obj;
350 efi_status_t r;
351
352 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
353 sizeof(struct efi_object),
354 (void **)&obj);
355 if (r != EFI_SUCCESS)
356 return r;
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100357 efi_add_handle(obj);
358 *handle = obj->handle;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200359 return r;
360}
361
Alexander Grafbee91162016-03-04 01:09:59 +0100362/*
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100363 * Find a protocol on a handle.
364 *
365 * @handle handle
366 * @protocol_guid GUID of the protocol
367 * @handler reference to the protocol
368 * @return status code
369 */
370efi_status_t efi_search_protocol(const void *handle,
371 const efi_guid_t *protocol_guid,
372 struct efi_handler **handler)
373{
374 struct efi_object *efiobj;
375 struct list_head *lhandle;
376
377 if (!handle || !protocol_guid)
378 return EFI_INVALID_PARAMETER;
379 efiobj = efi_search_obj(handle);
380 if (!efiobj)
381 return EFI_INVALID_PARAMETER;
382 list_for_each(lhandle, &efiobj->protocols) {
383 struct efi_handler *protocol;
384
385 protocol = list_entry(lhandle, struct efi_handler, link);
386 if (!guidcmp(protocol->guid, protocol_guid)) {
387 if (handler)
388 *handler = protocol;
389 return EFI_SUCCESS;
390 }
391 }
392 return EFI_NOT_FOUND;
393}
394
395/*
396 * Delete protocol from a handle.
397 *
398 * @handle handle from which the protocol shall be deleted
399 * @protocol GUID of the protocol to be deleted
400 * @protocol_interface interface of the protocol implementation
401 * @return status code
402 */
403efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
404 void *protocol_interface)
405{
406 struct efi_handler *handler;
407 efi_status_t ret;
408
409 ret = efi_search_protocol(handle, protocol, &handler);
410 if (ret != EFI_SUCCESS)
411 return ret;
412 if (guidcmp(handler->guid, protocol))
413 return EFI_INVALID_PARAMETER;
414 list_del(&handler->link);
415 free(handler);
416 return EFI_SUCCESS;
417}
418
419/*
420 * Delete all protocols from a handle.
421 *
422 * @handle handle from which the protocols shall be deleted
423 * @return status code
424 */
425efi_status_t efi_remove_all_protocols(const void *handle)
426{
427 struct efi_object *efiobj;
428 struct list_head *lhandle;
429 struct list_head *pos;
430
431 efiobj = efi_search_obj(handle);
432 if (!efiobj)
433 return EFI_INVALID_PARAMETER;
434 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
435 struct efi_handler *protocol;
436 efi_status_t ret;
437
438 protocol = list_entry(lhandle, struct efi_handler, link);
439
440 ret = efi_remove_protocol(handle, protocol->guid,
441 protocol->protocol_interface);
442 if (ret != EFI_SUCCESS)
443 return ret;
444 }
445 return EFI_SUCCESS;
446}
447
448/*
449 * Delete handle.
450 *
451 * @handle handle to delete
452 */
453void efi_delete_handle(struct efi_object *obj)
454{
455 if (!obj)
456 return;
457 efi_remove_all_protocols(obj->handle);
458 list_del(&obj->link);
459 free(obj);
460}
461
462/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200463 * Our event capabilities are very limited. Only a small limited
464 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100465 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200466static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100467
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200468/*
469 * Create an event.
470 *
471 * This function is used inside U-Boot code to create an event.
472 *
473 * For the API function implementing the CreateEvent service see
474 * efi_create_event_ext.
475 *
476 * @type type of the event to create
477 * @notify_tpl task priority level of the event
478 * @notify_function notification function of the event
479 * @notify_context pointer passed to the notification function
480 * @event created event
481 * @return status code
482 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100483efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200484 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200485 struct efi_event *event,
486 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200487 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100488{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200489 int i;
490
Jonathan Graya95343b2017-03-12 19:26:07 +1100491 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200492 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100493
494 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200495 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100496
497 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
498 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200499 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100500
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200501 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
502 if (efi_events[i].type)
503 continue;
504 efi_events[i].type = type;
505 efi_events[i].notify_tpl = notify_tpl;
506 efi_events[i].notify_function = notify_function;
507 efi_events[i].notify_context = notify_context;
508 /* Disable timers on bootup */
509 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200510 efi_events[i].is_queued = false;
511 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200512 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200513 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200514 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200515 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100516}
517
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200518/*
519 * Create an event.
520 *
521 * This function implements the CreateEvent service.
522 * See the Unified Extensible Firmware Interface (UEFI) specification
523 * for details.
524 *
525 * @type type of the event to create
526 * @notify_tpl task priority level of the event
527 * @notify_function notification function of the event
528 * @notify_context pointer passed to the notification function
529 * @event created event
530 * @return status code
531 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200532static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100533 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200534 void (EFIAPI *notify_function) (
535 struct efi_event *event,
536 void *context),
537 void *notify_context, struct efi_event **event)
538{
539 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
540 notify_context);
541 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
542 notify_context, event));
543}
544
545
Alexander Grafbee91162016-03-04 01:09:59 +0100546/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200547 * Check if a timer event has occurred or a queued notification function should
548 * be called.
549 *
Alexander Grafbee91162016-03-04 01:09:59 +0100550 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200551 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100552 */
553void efi_timer_check(void)
554{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200555 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100556 u64 now = timer_get_us();
557
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200558 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200559 if (!efi_events[i].type)
560 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200561 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200562 efi_signal_event(&efi_events[i]);
563 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200564 now < efi_events[i].trigger_next)
565 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200566 switch (efi_events[i].trigger_type) {
567 case EFI_TIMER_RELATIVE:
568 efi_events[i].trigger_type = EFI_TIMER_STOP;
569 break;
570 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200571 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200572 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200573 break;
574 default:
575 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200576 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200577 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200578 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100579 }
Alexander Grafbee91162016-03-04 01:09:59 +0100580 WATCHDOG_RESET();
581}
582
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200583/*
584 * Set the trigger time for a timer event or stop the event.
585 *
586 * This is the function for internal usage in U-Boot. For the API function
587 * implementing the SetTimer service see efi_set_timer_ext.
588 *
589 * @event event for which the timer is set
590 * @type type of the timer
591 * @trigger_time trigger period in multiples of 100ns
592 * @return status code
593 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200594efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200595 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100596{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200597 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100598
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200599 /*
600 * The parameter defines a multiple of 100ns.
601 * We use multiples of 1000ns. So divide by 10.
602 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200603 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100604
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200605 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
606 if (event != &efi_events[i])
607 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100608
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200609 if (!(event->type & EVT_TIMER))
610 break;
611 switch (type) {
612 case EFI_TIMER_STOP:
613 event->trigger_next = -1ULL;
614 break;
615 case EFI_TIMER_PERIODIC:
616 case EFI_TIMER_RELATIVE:
617 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200618 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200619 break;
620 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200621 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200622 }
623 event->trigger_type = type;
624 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200625 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200626 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100627 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200628 return EFI_INVALID_PARAMETER;
629}
630
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200631/*
632 * Set the trigger time for a timer event or stop the event.
633 *
634 * This function implements the SetTimer service.
635 * See the Unified Extensible Firmware Interface (UEFI) specification
636 * for details.
637 *
638 * @event event for which the timer is set
639 * @type type of the timer
640 * @trigger_time trigger period in multiples of 100ns
641 * @return status code
642 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200643static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
644 enum efi_timer_delay type,
645 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200646{
647 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
648 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100649}
650
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200651/*
652 * Wait for events to be signaled.
653 *
654 * This function implements the WaitForEvent service.
655 * See the Unified Extensible Firmware Interface (UEFI) specification
656 * for details.
657 *
658 * @num_events number of events to be waited for
659 * @events events to be waited for
660 * @index index of the event that was signaled
661 * @return status code
662 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100663static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200664 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100665 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100666{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200667 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100668
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100669 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100670
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200671 /* Check parameters */
672 if (!num_events || !event)
673 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200674 /* Check TPL */
675 if (efi_tpl != TPL_APPLICATION)
676 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200677 for (i = 0; i < num_events; ++i) {
678 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
679 if (event[i] == &efi_events[j])
680 goto known_event;
681 }
682 return EFI_EXIT(EFI_INVALID_PARAMETER);
683known_event:
684 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
685 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200686 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200687 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200688 }
689
690 /* Wait for signal */
691 for (;;) {
692 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200693 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200694 goto out;
695 }
696 /* Allow events to occur. */
697 efi_timer_check();
698 }
699
700out:
701 /*
702 * Reset the signal which is passed to the caller to allow periodic
703 * events to occur.
704 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200705 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200706 if (index)
707 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100708
709 return EFI_EXIT(EFI_SUCCESS);
710}
711
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200712/*
713 * Signal an EFI event.
714 *
715 * This function implements the SignalEvent service.
716 * See the Unified Extensible Firmware Interface (UEFI) specification
717 * for details.
718 *
719 * This functions sets the signaled state of the event and queues the
720 * notification function for execution.
721 *
722 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200723 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200724 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200725static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100726{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200727 int i;
728
Alexander Grafbee91162016-03-04 01:09:59 +0100729 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200730 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
731 if (event != &efi_events[i])
732 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200733 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200734 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200735 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200736 if (event->type & EVT_NOTIFY_SIGNAL)
737 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200738 break;
739 }
Alexander Grafbee91162016-03-04 01:09:59 +0100740 return EFI_EXIT(EFI_SUCCESS);
741}
742
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200743/*
744 * Close an EFI event.
745 *
746 * This function implements the CloseEvent service.
747 * See the Unified Extensible Firmware Interface (UEFI) specification
748 * for details.
749 *
750 * @event event to close
751 * @return status code
752 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200753static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100754{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200755 int i;
756
Alexander Grafbee91162016-03-04 01:09:59 +0100757 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200758 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
759 if (event == &efi_events[i]) {
760 event->type = 0;
761 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200762 event->is_queued = false;
763 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200764 return EFI_EXIT(EFI_SUCCESS);
765 }
766 }
767 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100768}
769
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200770/*
771 * Check if an event is signaled.
772 *
773 * This function implements the CheckEvent service.
774 * See the Unified Extensible Firmware Interface (UEFI) specification
775 * for details.
776 *
777 * If an event is not signaled yet the notification function is queued.
778 *
779 * @event event to check
780 * @return status code
781 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200782static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100783{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200784 int i;
785
Alexander Grafbee91162016-03-04 01:09:59 +0100786 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200787 efi_timer_check();
788 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
789 if (event != &efi_events[i])
790 continue;
791 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
792 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200793 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200794 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200795 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200796 return EFI_EXIT(EFI_SUCCESS);
797 return EFI_EXIT(EFI_NOT_READY);
798 }
799 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100800}
801
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200802/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200803 * Find the internal EFI object for a handle.
804 *
805 * @handle handle to find
806 * @return EFI object
807 */
Heinrich Schuchardt085d07c2017-10-26 19:25:50 +0200808struct efi_object *efi_search_obj(const void *handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200809{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100810 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200811
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100812 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200813 if (efiobj->handle == handle)
814 return efiobj;
815 }
816
817 return NULL;
818}
819
820/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200821 * Install new protocol on a handle.
822 *
823 * @handle handle on which the protocol shall be installed
824 * @protocol GUID of the protocol to be installed
825 * @protocol_interface interface of the protocol implementation
826 * @return status code
827 */
828efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
829 void *protocol_interface)
830{
831 struct efi_object *efiobj;
832 struct efi_handler *handler;
833 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200834
835 efiobj = efi_search_obj(handle);
836 if (!efiobj)
837 return EFI_INVALID_PARAMETER;
838 ret = efi_search_protocol(handle, protocol, NULL);
839 if (ret != EFI_NOT_FOUND)
840 return EFI_INVALID_PARAMETER;
841 handler = calloc(1, sizeof(struct efi_handler));
842 if (!handler)
843 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100844 handler->guid = protocol;
845 handler->protocol_interface = protocol_interface;
846 list_add_tail(&handler->link, &efiobj->protocols);
847 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200848}
849
850/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200851 * Install protocol interface.
852 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100853 * This function implements the InstallProtocolInterface service.
854 * See the Unified Extensible Firmware Interface (UEFI) specification
855 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200856 *
857 * @handle handle on which the protocol shall be installed
858 * @protocol GUID of the protocol to be installed
859 * @protocol_interface_type type of the interface to be installed,
860 * always EFI_NATIVE_INTERFACE
861 * @protocol_interface interface of the protocol implementation
862 * @return status code
863 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100864static efi_status_t EFIAPI efi_install_protocol_interface(
865 void **handle, const efi_guid_t *protocol,
866 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100867{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200868 efi_status_t r;
869
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100870 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
871 protocol_interface);
872
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200873 if (!handle || !protocol ||
874 protocol_interface_type != EFI_NATIVE_INTERFACE) {
875 r = EFI_INVALID_PARAMETER;
876 goto out;
877 }
878
879 /* Create new handle if requested. */
880 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200881 r = efi_create_handle(handle);
882 if (r != EFI_SUCCESS)
883 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200884 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
885 *handle);
886 } else {
887 debug("%sEFI: handle %p\n", indent_string(nesting_level),
888 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200889 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +0200890 /* Add new protocol */
891 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200892out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100893 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100894}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200895
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200896/*
897 * Reinstall protocol interface.
898 *
899 * This function implements the ReinstallProtocolInterface service.
900 * See the Unified Extensible Firmware Interface (UEFI) specification
901 * for details.
902 *
903 * @handle handle on which the protocol shall be
904 * reinstalled
905 * @protocol GUID of the protocol to be installed
906 * @old_interface interface to be removed
907 * @new_interface interface to be installed
908 * @return status code
909 */
Alexander Grafbee91162016-03-04 01:09:59 +0100910static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200911 const efi_guid_t *protocol, void *old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100912 void *new_interface)
913{
Rob Clark778e6af2017-09-13 18:05:41 -0400914 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100915 new_interface);
916 return EFI_EXIT(EFI_ACCESS_DENIED);
917}
918
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200919/*
920 * Uninstall protocol interface.
921 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100922 * This function implements the UninstallProtocolInterface service.
923 * See the Unified Extensible Firmware Interface (UEFI) specification
924 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200925 *
926 * @handle handle from which the protocol shall be removed
927 * @protocol GUID of the protocol to be removed
928 * @protocol_interface interface to be removed
929 * @return status code
930 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100931static efi_status_t EFIAPI efi_uninstall_protocol_interface(
932 void *handle, const efi_guid_t *protocol,
933 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100934{
Heinrich Schuchardt58105112017-10-26 19:25:56 +0200935 struct efi_handler *handler;
936 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200937
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100938 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
939
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200940 if (!handle || !protocol) {
941 r = EFI_INVALID_PARAMETER;
942 goto out;
943 }
944
Heinrich Schuchardt58105112017-10-26 19:25:56 +0200945 /* Find the protocol on the handle */
946 r = efi_search_protocol(handle, protocol, &handler);
947 if (r != EFI_SUCCESS)
948 goto out;
949 if (handler->protocol_interface) {
950 /* TODO disconnect controllers */
951 r = EFI_ACCESS_DENIED;
952 } else {
953 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200954 }
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200955out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100956 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100957}
958
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200959/*
960 * Register an event for notification when a protocol is installed.
961 *
962 * This function implements the RegisterProtocolNotify service.
963 * See the Unified Extensible Firmware Interface (UEFI) specification
964 * for details.
965 *
966 * @protocol GUID of the protocol whose installation shall be
967 * notified
968 * @event event to be signaled upon installation of the protocol
969 * @registration key for retrieving the registration information
970 * @return status code
971 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200972static efi_status_t EFIAPI efi_register_protocol_notify(
973 const efi_guid_t *protocol,
974 struct efi_event *event,
975 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +0100976{
Rob Clark778e6af2017-09-13 18:05:41 -0400977 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +0100978 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
979}
980
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200981/*
982 * Determine if an EFI handle implements a protocol.
983 *
984 * See the documentation of the LocateHandle service in the UEFI specification.
985 *
986 * @search_type selection criterion
987 * @protocol GUID of the protocol
988 * @search_key registration key
989 * @efiobj handle
990 * @return 0 if the handle implements the protocol
991 */
Alexander Grafbee91162016-03-04 01:09:59 +0100992static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200993 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +0100994 struct efi_object *efiobj)
995{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200996 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +0100997
998 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100999 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001000 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001001 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001002 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001003 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001004 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001005 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1006 return (ret != EFI_SUCCESS);
1007 default:
1008 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001009 return -1;
1010 }
Alexander Grafbee91162016-03-04 01:09:59 +01001011}
1012
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001013/*
1014 * Locate handles implementing a protocol.
1015 *
1016 * This function is meant for U-Boot internal calls. For the API implementation
1017 * of the LocateHandle service see efi_locate_handle_ext.
1018 *
1019 * @search_type selection criterion
1020 * @protocol GUID of the protocol
1021 * @search_key registration key
1022 * @buffer_size size of the buffer to receive the handles in bytes
1023 * @buffer buffer to receive the relevant handles
1024 * @return status code
1025 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001026static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001027 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001028 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001029 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001030{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001031 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001032 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001033
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001034 /* Check parameters */
1035 switch (search_type) {
1036 case ALL_HANDLES:
1037 break;
1038 case BY_REGISTER_NOTIFY:
1039 if (!search_key)
1040 return EFI_INVALID_PARAMETER;
1041 /* RegisterProtocolNotify is not implemented yet */
1042 return EFI_UNSUPPORTED;
1043 case BY_PROTOCOL:
1044 if (!protocol)
1045 return EFI_INVALID_PARAMETER;
1046 break;
1047 default:
1048 return EFI_INVALID_PARAMETER;
1049 }
1050
1051 /*
1052 * efi_locate_handle_buffer uses this function for
1053 * the calculation of the necessary buffer size.
1054 * So do not require a buffer for buffersize == 0.
1055 */
1056 if (!buffer_size || (*buffer_size && !buffer))
1057 return EFI_INVALID_PARAMETER;
1058
Alexander Grafbee91162016-03-04 01:09:59 +01001059 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001060 list_for_each_entry(efiobj, &efi_obj_list, link) {
1061 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001062 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001063 }
1064
1065 if (*buffer_size < size) {
1066 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001067 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001068 }
1069
Rob Clark796a78c2017-08-06 14:10:07 -04001070 *buffer_size = size;
1071 if (size == 0)
1072 return EFI_NOT_FOUND;
1073
Alexander Grafbee91162016-03-04 01:09:59 +01001074 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001075 list_for_each_entry(efiobj, &efi_obj_list, link) {
1076 if (!efi_search(search_type, protocol, search_key, efiobj))
1077 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001078 }
1079
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001080 return EFI_SUCCESS;
1081}
1082
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001083/*
1084 * Locate handles implementing a protocol.
1085 *
1086 * This function implements the LocateHandle service.
1087 * See the Unified Extensible Firmware Interface (UEFI) specification
1088 * for details.
1089 *
1090 * @search_type selection criterion
1091 * @protocol GUID of the protocol
1092 * @search_key registration key
1093 * @buffer_size size of the buffer to receive the handles in bytes
1094 * @buffer buffer to receive the relevant handles
1095 * @return 0 if the handle implements the protocol
1096 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001097static efi_status_t EFIAPI efi_locate_handle_ext(
1098 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001099 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001100 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001101{
Rob Clark778e6af2017-09-13 18:05:41 -04001102 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001103 buffer_size, buffer);
1104
1105 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1106 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001107}
1108
Alexander Grafd98cdf62017-07-26 13:41:04 +02001109/* Collapses configuration table entries, removing index i */
1110static void efi_remove_configuration_table(int i)
1111{
1112 struct efi_configuration_table *this = &efi_conf_table[i];
1113 struct efi_configuration_table *next = &efi_conf_table[i+1];
1114 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1115
1116 memmove(this, next, (ulong)end - (ulong)next);
1117 systab.nr_tables--;
1118}
1119
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001120/*
1121 * Adds, updates, or removes a configuration table.
1122 *
1123 * This function is used for internal calls. For the API implementation of the
1124 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1125 *
1126 * @guid GUID of the installed table
1127 * @table table to be installed
1128 * @return status code
1129 */
Alexander Graf488bf122016-08-19 01:23:24 +02001130efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001131{
1132 int i;
1133
Alexander Grafbee91162016-03-04 01:09:59 +01001134 /* Check for guid override */
1135 for (i = 0; i < systab.nr_tables; i++) {
1136 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001137 if (table)
1138 efi_conf_table[i].table = table;
1139 else
1140 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001141 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001142 }
1143 }
1144
Alexander Grafd98cdf62017-07-26 13:41:04 +02001145 if (!table)
1146 return EFI_NOT_FOUND;
1147
Alexander Grafbee91162016-03-04 01:09:59 +01001148 /* No override, check for overflow */
1149 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001150 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001151
1152 /* Add a new entry */
1153 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1154 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001155 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001156
Alexander Graf488bf122016-08-19 01:23:24 +02001157 return EFI_SUCCESS;
1158}
1159
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001160/*
1161 * Adds, updates, or removes a configuration table.
1162 *
1163 * This function implements the InstallConfigurationTable service.
1164 * See the Unified Extensible Firmware Interface (UEFI) specification
1165 * for details.
1166 *
1167 * @guid GUID of the installed table
1168 * @table table to be installed
1169 * @return status code
1170 */
Alexander Graf488bf122016-08-19 01:23:24 +02001171static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1172 void *table)
1173{
Rob Clark778e6af2017-09-13 18:05:41 -04001174 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001175 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001176}
1177
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001178/*
1179 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001180 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001181 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001182 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001183 * image
1184 * @obj internal object associated with the loaded image
1185 * @device_path device path of the loaded image
1186 * @file_path file path of the loaded image
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001187 * @return status code
Rob Clark95c55532017-09-13 18:05:33 -04001188 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001189efi_status_t efi_setup_loaded_image(
1190 struct efi_loaded_image *info, struct efi_object *obj,
1191 struct efi_device_path *device_path,
1192 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001193{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001194 efi_status_t ret;
1195
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001196 /* Add internal object to object list */
1197 efi_add_handle(obj);
1198 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001199 obj->handle = info;
1200
Rob Clark95c55532017-09-13 18:05:33 -04001201 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001202 if (device_path)
1203 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001204
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001205 /*
1206 * When asking for the device path interface, return
1207 * bootefi_device_path
1208 */
1209 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1210 if (ret != EFI_SUCCESS)
1211 goto failure;
1212
1213 /*
1214 * When asking for the loaded_image interface, just
1215 * return handle which points to loaded_image_info
1216 */
1217 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1218 if (ret != EFI_SUCCESS)
1219 goto failure;
1220
1221 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1222 (void *)&efi_console_control);
1223 if (ret != EFI_SUCCESS)
1224 goto failure;
1225
1226 ret = efi_add_protocol(obj->handle,
1227 &efi_guid_device_path_to_text_protocol,
1228 (void *)&efi_device_path_to_text);
1229 if (ret != EFI_SUCCESS)
1230 goto failure;
1231
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001232 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001233failure:
1234 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001235 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001236}
1237
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001238/*
1239 * Load an image using a file path.
1240 *
1241 * @file_path the path of the image to load
1242 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001243 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001244 */
Rob Clark9975fe92017-09-13 18:05:38 -04001245efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1246 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001247{
1248 struct efi_file_info *info = NULL;
1249 struct efi_file_handle *f;
1250 static efi_status_t ret;
1251 uint64_t bs;
1252
1253 f = efi_file_from_path(file_path);
1254 if (!f)
1255 return EFI_DEVICE_ERROR;
1256
1257 bs = 0;
1258 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1259 &bs, info));
1260 if (ret == EFI_BUFFER_TOO_SMALL) {
1261 info = malloc(bs);
1262 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1263 &bs, info));
1264 }
1265 if (ret != EFI_SUCCESS)
1266 goto error;
1267
1268 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1269 if (ret)
1270 goto error;
1271
1272 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1273
1274error:
1275 free(info);
1276 EFI_CALL(f->close(f));
1277
1278 if (ret != EFI_SUCCESS) {
1279 efi_free_pool(*buffer);
1280 *buffer = NULL;
1281 }
1282
1283 return ret;
1284}
1285
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001286/*
1287 * Load an EFI image into memory.
1288 *
1289 * This function implements the LoadImage service.
1290 * See the Unified Extensible Firmware Interface (UEFI) specification
1291 * for details.
1292 *
1293 * @boot_policy true for request originating from the boot manager
1294 * @parent_image the calles's image handle
1295 * @file_path the path of the image to load
1296 * @source_buffer memory location from which the image is installed
1297 * @source_size size of the memory area from which the image is
1298 * installed
1299 * @image_handle handle for the newly installed image
1300 * @return status code
1301 */
Alexander Grafbee91162016-03-04 01:09:59 +01001302static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1303 efi_handle_t parent_image,
1304 struct efi_device_path *file_path,
1305 void *source_buffer,
1306 unsigned long source_size,
1307 efi_handle_t *image_handle)
1308{
Alexander Grafbee91162016-03-04 01:09:59 +01001309 struct efi_loaded_image *info;
1310 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001311 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001312
1313 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1314 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001315
1316 info = calloc(1, sizeof(*info));
1317 obj = calloc(1, sizeof(*obj));
1318
1319 if (!source_buffer) {
1320 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001321
Rob Clark9975fe92017-09-13 18:05:38 -04001322 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001323 if (ret != EFI_SUCCESS)
1324 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001325 /*
1326 * split file_path which contains both the device and
1327 * file parts:
1328 */
1329 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001330 ret = efi_setup_loaded_image(info, obj, dp, fp);
1331 if (ret != EFI_SUCCESS)
1332 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001333 } else {
1334 /* In this case, file_path is the "device" path, ie.
1335 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1336 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001337 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1338 if (ret != EFI_SUCCESS)
1339 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001340 }
Alexander Grafbee91162016-03-04 01:09:59 +01001341 info->reserved = efi_load_pe(source_buffer, info);
1342 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001343 ret = EFI_UNSUPPORTED;
1344 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001345 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001346 info->system_table = &systab;
1347 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001348 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001349 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001350failure:
1351 free(info);
1352 efi_delete_handle(obj);
1353 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001354}
1355
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001356/*
1357 * Call the entry point of an image.
1358 *
1359 * This function implements the StartImage service.
1360 * See the Unified Extensible Firmware Interface (UEFI) specification
1361 * for details.
1362 *
1363 * @image_handle handle of the image
1364 * @exit_data_size size of the buffer
1365 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001366 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001367 */
Alexander Grafbee91162016-03-04 01:09:59 +01001368static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1369 unsigned long *exit_data_size,
1370 s16 **exit_data)
1371{
1372 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1373 struct efi_loaded_image *info = image_handle;
1374
1375 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1376 entry = info->reserved;
1377
1378 efi_is_direct_boot = false;
1379
1380 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001381 if (setjmp(&info->exit_jmp)) {
1382 /* We returned from the child image */
1383 return EFI_EXIT(info->exit_status);
1384 }
1385
Rob Clarkaf65db82017-07-27 08:04:19 -04001386 __efi_nesting_dec();
Rob Clarkc160d2f2017-07-27 08:04:18 -04001387 __efi_exit_check();
Alexander Grafbee91162016-03-04 01:09:59 +01001388 entry(image_handle, &systab);
Rob Clarkc160d2f2017-07-27 08:04:18 -04001389 __efi_entry_check();
Rob Clarkaf65db82017-07-27 08:04:19 -04001390 __efi_nesting_inc();
Alexander Grafbee91162016-03-04 01:09:59 +01001391
1392 /* Should usually never get here */
1393 return EFI_EXIT(EFI_SUCCESS);
1394}
1395
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001396/*
1397 * Leave an EFI application or driver.
1398 *
1399 * This function implements the Exit service.
1400 * See the Unified Extensible Firmware Interface (UEFI) specification
1401 * for details.
1402 *
1403 * @image_handle handle of the application or driver that is exiting
1404 * @exit_status status code
1405 * @exit_data_size size of the buffer in bytes
1406 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001407 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001408 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001409static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1410 efi_status_t exit_status, unsigned long exit_data_size,
1411 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001412{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001413 /*
1414 * We require that the handle points to the original loaded
1415 * image protocol interface.
1416 *
1417 * For getting the longjmp address this is safer than locating
1418 * the protocol because the protocol may have been reinstalled
1419 * pointing to another memory location.
1420 *
1421 * TODO: We should call the unload procedure of the loaded
1422 * image protocol.
1423 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001424 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1425
Alexander Grafbee91162016-03-04 01:09:59 +01001426 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1427 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001428
Alexander Grafa1489202017-09-03 14:14:17 +02001429 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001430 __efi_exit_check();
1431
Alexander Grafa1489202017-09-03 14:14:17 +02001432 /*
1433 * But longjmp out with the U-Boot gd, not the application's, as
1434 * the other end is a setjmp call inside EFI context.
1435 */
1436 efi_restore_gd();
1437
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001438 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001439 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001440
1441 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001442}
1443
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001444/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001445 * Unload an EFI image.
1446 *
1447 * This function implements the UnloadImage service.
1448 * See the Unified Extensible Firmware Interface (UEFI) specification
1449 * for details.
1450 *
1451 * @image_handle handle of the image to be unloaded
1452 * @return status code
1453 */
Alexander Grafbee91162016-03-04 01:09:59 +01001454static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1455{
1456 struct efi_object *efiobj;
1457
1458 EFI_ENTRY("%p", image_handle);
1459 efiobj = efi_search_obj(image_handle);
1460 if (efiobj)
1461 list_del(&efiobj->link);
1462
1463 return EFI_EXIT(EFI_SUCCESS);
1464}
1465
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001466/*
1467 * Fix up caches for EFI payloads if necessary.
1468 */
Alexander Grafbee91162016-03-04 01:09:59 +01001469static void efi_exit_caches(void)
1470{
1471#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1472 /*
1473 * Grub on 32bit ARM needs to have caches disabled before jumping into
1474 * a zImage, but does not know of all cache layers. Give it a hand.
1475 */
1476 if (efi_is_direct_boot)
1477 cleanup_before_linux();
1478#endif
1479}
1480
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001481/*
1482 * Stop boot services.
1483 *
1484 * This function implements the ExitBootServices service.
1485 * See the Unified Extensible Firmware Interface (UEFI) specification
1486 * for details.
1487 *
1488 * @image_handle handle of the loaded image
1489 * @map_key key of the memory map
1490 * @return status code
1491 */
Alexander Grafbee91162016-03-04 01:09:59 +01001492static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1493 unsigned long map_key)
1494{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001495 int i;
1496
Alexander Grafbee91162016-03-04 01:09:59 +01001497 EFI_ENTRY("%p, %ld", image_handle, map_key);
1498
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001499 /* Notify that ExitBootServices is invoked. */
1500 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1501 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1502 continue;
1503 efi_signal_event(&efi_events[i]);
1504 }
1505 /* Make sure that notification functions are not called anymore */
1506 efi_tpl = TPL_HIGH_LEVEL;
1507
Alexander Graf40583562017-10-19 23:23:50 +02001508 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001509
Alexander Grafb7b84102016-11-17 01:02:57 +01001510 board_quiesce_devices();
1511
Alexander Grafbee91162016-03-04 01:09:59 +01001512 /* Fix up caches for EFI payloads if necessary */
1513 efi_exit_caches();
1514
1515 /* This stops all lingering devices */
1516 bootm_disable_interrupts();
1517
1518 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001519 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001520 WATCHDOG_RESET();
1521
1522 return EFI_EXIT(EFI_SUCCESS);
1523}
1524
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001525/*
1526 * Get next value of the counter.
1527 *
1528 * This function implements the NextMonotonicCount service.
1529 * See the Unified Extensible Firmware Interface (UEFI) specification
1530 * for details.
1531 *
1532 * @count returned value of the counter
1533 * @return status code
1534 */
Alexander Grafbee91162016-03-04 01:09:59 +01001535static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1536{
1537 static uint64_t mono = 0;
1538 EFI_ENTRY("%p", count);
1539 *count = mono++;
1540 return EFI_EXIT(EFI_SUCCESS);
1541}
1542
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001543/*
1544 * Sleep.
1545 *
1546 * This function implements the Stall sercive.
1547 * See the Unified Extensible Firmware Interface (UEFI) specification
1548 * for details.
1549 *
1550 * @microseconds period to sleep in microseconds
1551 * @return status code
1552 */
Alexander Grafbee91162016-03-04 01:09:59 +01001553static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1554{
1555 EFI_ENTRY("%ld", microseconds);
1556 udelay(microseconds);
1557 return EFI_EXIT(EFI_SUCCESS);
1558}
1559
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001560/*
1561 * Reset the watchdog timer.
1562 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001563 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001564 * See the Unified Extensible Firmware Interface (UEFI) specification
1565 * for details.
1566 *
1567 * @timeout seconds before reset by watchdog
1568 * @watchdog_code code to be logged when resetting
1569 * @data_size size of buffer in bytes
1570 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001571 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001572 */
Alexander Grafbee91162016-03-04 01:09:59 +01001573static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1574 uint64_t watchdog_code,
1575 unsigned long data_size,
1576 uint16_t *watchdog_data)
1577{
1578 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1579 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001580 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001581}
1582
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001583/*
1584 * Connect a controller to a driver.
1585 *
1586 * This function implements the ConnectController service.
1587 * See the Unified Extensible Firmware Interface (UEFI) specification
1588 * for details.
1589 *
1590 * @controller_handle handle of the controller
1591 * @driver_image_handle handle of the driver
1592 * @remain_device_path device path of a child controller
1593 * @recursive true to connect all child controllers
1594 * @return status code
1595 */
Alexander Grafbee91162016-03-04 01:09:59 +01001596static efi_status_t EFIAPI efi_connect_controller(
1597 efi_handle_t controller_handle,
1598 efi_handle_t *driver_image_handle,
1599 struct efi_device_path *remain_device_path,
1600 bool recursive)
1601{
1602 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1603 remain_device_path, recursive);
1604 return EFI_EXIT(EFI_NOT_FOUND);
1605}
1606
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001607/*
1608 * Disconnect a controller from a driver.
1609 *
1610 * This function implements the DisconnectController service.
1611 * See the Unified Extensible Firmware Interface (UEFI) specification
1612 * for details.
1613 *
1614 * @controller_handle handle of the controller
1615 * @driver_image_handle handle of the driver
1616 * @child_handle handle of the child to destroy
1617 * @return status code
1618 */
Alexander Grafbee91162016-03-04 01:09:59 +01001619static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1620 void *driver_image_handle,
1621 void *child_handle)
1622{
1623 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1624 child_handle);
1625 return EFI_EXIT(EFI_INVALID_PARAMETER);
1626}
1627
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001628/*
1629 * Close a protocol.
1630 *
1631 * This function implements the CloseProtocol service.
1632 * See the Unified Extensible Firmware Interface (UEFI) specification
1633 * for details.
1634 *
1635 * @handle handle on which the protocol shall be closed
1636 * @protocol GUID of the protocol to close
1637 * @agent_handle handle of the driver
1638 * @controller_handle handle of the controller
1639 * @return status code
1640 */
Alexander Grafbee91162016-03-04 01:09:59 +01001641static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001642 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001643 void *agent_handle,
1644 void *controller_handle)
1645{
Rob Clark778e6af2017-09-13 18:05:41 -04001646 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001647 controller_handle);
1648 return EFI_EXIT(EFI_NOT_FOUND);
1649}
1650
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001651/*
1652 * Provide information about then open status of a protocol on a handle
1653 *
1654 * This function implements the OpenProtocolInformation service.
1655 * See the Unified Extensible Firmware Interface (UEFI) specification
1656 * for details.
1657 *
1658 * @handle handle for which the information shall be retrieved
1659 * @protocol GUID of the protocol
1660 * @entry_buffer buffer to receive the open protocol information
1661 * @entry_count number of entries available in the buffer
1662 * @return status code
1663 */
Alexander Grafbee91162016-03-04 01:09:59 +01001664static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001665 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001666 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001667 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001668{
Rob Clark778e6af2017-09-13 18:05:41 -04001669 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001670 entry_count);
1671 return EFI_EXIT(EFI_NOT_FOUND);
1672}
1673
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001674/*
1675 * Get protocols installed on a handle.
1676 *
1677 * This function implements the ProtocolsPerHandleService.
1678 * See the Unified Extensible Firmware Interface (UEFI) specification
1679 * for details.
1680 *
1681 * @handle handle for which the information is retrieved
1682 * @protocol_buffer buffer with protocol GUIDs
1683 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001684 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001685 */
Alexander Grafbee91162016-03-04 01:09:59 +01001686static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1687 efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001688 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001689{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001690 unsigned long buffer_size;
1691 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001692 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001693 efi_status_t r;
1694
Alexander Grafbee91162016-03-04 01:09:59 +01001695 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1696 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001697
1698 if (!handle || !protocol_buffer || !protocol_buffer_count)
1699 return EFI_EXIT(EFI_INVALID_PARAMETER);
1700
1701 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001702 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001703
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001704 efiobj = efi_search_obj(handle);
1705 if (!efiobj)
1706 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001707
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001708 /* Count protocols */
1709 list_for_each(protocol_handle, &efiobj->protocols) {
1710 ++*protocol_buffer_count;
1711 }
1712
1713 /* Copy guids */
1714 if (*protocol_buffer_count) {
1715 size_t j = 0;
1716
1717 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1718 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1719 (void **)protocol_buffer);
1720 if (r != EFI_SUCCESS)
1721 return EFI_EXIT(r);
1722 list_for_each(protocol_handle, &efiobj->protocols) {
1723 struct efi_handler *protocol;
1724
1725 protocol = list_entry(protocol_handle,
1726 struct efi_handler, link);
1727 (*protocol_buffer)[j] = (void *)protocol->guid;
1728 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001729 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001730 }
1731
1732 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001733}
1734
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001735/*
1736 * Locate handles implementing a protocol.
1737 *
1738 * This function implements the LocateHandleBuffer service.
1739 * See the Unified Extensible Firmware Interface (UEFI) specification
1740 * for details.
1741 *
1742 * @search_type selection criterion
1743 * @protocol GUID of the protocol
1744 * @search_key registration key
1745 * @no_handles number of returned handles
1746 * @buffer buffer with the returned handles
1747 * @return status code
1748 */
Alexander Grafbee91162016-03-04 01:09:59 +01001749static efi_status_t EFIAPI efi_locate_handle_buffer(
1750 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001751 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001752 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001753{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001754 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001755 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001756
Rob Clark778e6af2017-09-13 18:05:41 -04001757 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001758 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001759
1760 if (!no_handles || !buffer) {
1761 r = EFI_INVALID_PARAMETER;
1762 goto out;
1763 }
1764 *no_handles = 0;
1765 *buffer = NULL;
1766 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1767 *buffer);
1768 if (r != EFI_BUFFER_TOO_SMALL)
1769 goto out;
1770 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1771 (void **)buffer);
1772 if (r != EFI_SUCCESS)
1773 goto out;
1774 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1775 *buffer);
1776 if (r == EFI_SUCCESS)
1777 *no_handles = buffer_size / sizeof(void *);
1778out:
1779 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001780}
1781
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001782/*
1783 * Find an interface implementing a protocol.
1784 *
1785 * This function implements the LocateProtocol service.
1786 * See the Unified Extensible Firmware Interface (UEFI) specification
1787 * for details.
1788 *
1789 * @protocol GUID of the protocol
1790 * @registration registration key passed to the notification function
1791 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001792 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001793 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001794static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001795 void *registration,
1796 void **protocol_interface)
1797{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001798 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001799 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001800
Rob Clark778e6af2017-09-13 18:05:41 -04001801 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001802
1803 if (!protocol || !protocol_interface)
1804 return EFI_EXIT(EFI_INVALID_PARAMETER);
1805
1806 list_for_each(lhandle, &efi_obj_list) {
1807 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001808 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001809
1810 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001811
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001812 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1813 if (ret == EFI_SUCCESS) {
1814 *protocol_interface = handler->protocol_interface;
1815 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001816 }
1817 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001818 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001819
1820 return EFI_EXIT(EFI_NOT_FOUND);
1821}
1822
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001823/*
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01001824 * Get the device path and handle of an device implementing a protocol.
1825 *
1826 * This function implements the LocateDevicePath service.
1827 * See the Unified Extensible Firmware Interface (UEFI) specification
1828 * for details.
1829 *
1830 * @protocol GUID of the protocol
1831 * @device_path device path
1832 * @device handle of the device
1833 * @return status code
1834 */
1835static efi_status_t EFIAPI efi_locate_device_path(
1836 const efi_guid_t *protocol,
1837 struct efi_device_path **device_path,
1838 efi_handle_t *device)
1839{
1840 struct efi_device_path *dp;
1841 size_t i;
1842 struct efi_handler *handler;
1843 efi_handle_t *handles;
1844 size_t len, len_dp;
1845 size_t len_best = 0;
1846 efi_uintn_t no_handles;
1847 u8 *remainder;
1848 efi_status_t ret;
1849
1850 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1851
1852 if (!protocol || !device_path || !*device_path || !device) {
1853 ret = EFI_INVALID_PARAMETER;
1854 goto out;
1855 }
1856
1857 /* Find end of device path */
1858 len = efi_dp_size(*device_path);
1859
1860 /* Get all handles implementing the protocol */
1861 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1862 &no_handles, &handles));
1863 if (ret != EFI_SUCCESS)
1864 goto out;
1865
1866 for (i = 0; i < no_handles; ++i) {
1867 /* Find the device path protocol */
1868 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1869 &handler);
1870 if (ret != EFI_SUCCESS)
1871 continue;
1872 dp = (struct efi_device_path *)handler->protocol_interface;
1873 len_dp = efi_dp_size(dp);
1874 /*
1875 * This handle can only be a better fit
1876 * if its device path length is longer than the best fit and
1877 * if its device path length is shorter of equal the searched
1878 * device path.
1879 */
1880 if (len_dp <= len_best || len_dp > len)
1881 continue;
1882 /* Check if dp is a subpath of device_path */
1883 if (memcmp(*device_path, dp, len_dp))
1884 continue;
1885 *device = handles[i];
1886 len_best = len_dp;
1887 }
1888 if (len_best) {
1889 remainder = (u8 *)*device_path + len_best;
1890 *device_path = (struct efi_device_path *)remainder;
1891 ret = EFI_SUCCESS;
1892 } else {
1893 ret = EFI_NOT_FOUND;
1894 }
1895out:
1896 return EFI_EXIT(ret);
1897}
1898
1899/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001900 * Install multiple protocol interfaces.
1901 *
1902 * This function implements the MultipleProtocolInterfaces service.
1903 * See the Unified Extensible Firmware Interface (UEFI) specification
1904 * for details.
1905 *
1906 * @handle handle on which the protocol interfaces shall be installed
1907 * @... NULL terminated argument list with pairs of protocol GUIDS and
1908 * interfaces
1909 * @return status code
1910 */
Alexander Grafbee91162016-03-04 01:09:59 +01001911static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1912 void **handle, ...)
1913{
1914 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001915
1916 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001917 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001918 void *protocol_interface;
1919 efi_status_t r = EFI_SUCCESS;
1920 int i = 0;
1921
1922 if (!handle)
1923 return EFI_EXIT(EFI_INVALID_PARAMETER);
1924
1925 va_start(argptr, handle);
1926 for (;;) {
1927 protocol = va_arg(argptr, efi_guid_t*);
1928 if (!protocol)
1929 break;
1930 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001931 r = EFI_CALL(efi_install_protocol_interface(
1932 handle, protocol,
1933 EFI_NATIVE_INTERFACE,
1934 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001935 if (r != EFI_SUCCESS)
1936 break;
1937 i++;
1938 }
1939 va_end(argptr);
1940 if (r == EFI_SUCCESS)
1941 return EFI_EXIT(r);
1942
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02001943 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001944 va_start(argptr, handle);
1945 for (; i; --i) {
1946 protocol = va_arg(argptr, efi_guid_t*);
1947 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001948 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1949 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001950 }
1951 va_end(argptr);
1952
1953 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001954}
1955
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001956/*
1957 * Uninstall multiple protocol interfaces.
1958 *
1959 * This function implements the UninstallMultipleProtocolInterfaces service.
1960 * See the Unified Extensible Firmware Interface (UEFI) specification
1961 * for details.
1962 *
1963 * @handle handle from which the protocol interfaces shall be removed
1964 * @... NULL terminated argument list with pairs of protocol GUIDS and
1965 * interfaces
1966 * @return status code
1967 */
Alexander Grafbee91162016-03-04 01:09:59 +01001968static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1969 void *handle, ...)
1970{
1971 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02001972
1973 va_list argptr;
1974 const efi_guid_t *protocol;
1975 void *protocol_interface;
1976 efi_status_t r = EFI_SUCCESS;
1977 size_t i = 0;
1978
1979 if (!handle)
1980 return EFI_EXIT(EFI_INVALID_PARAMETER);
1981
1982 va_start(argptr, handle);
1983 for (;;) {
1984 protocol = va_arg(argptr, efi_guid_t*);
1985 if (!protocol)
1986 break;
1987 protocol_interface = va_arg(argptr, void*);
1988 r = EFI_CALL(efi_uninstall_protocol_interface(
1989 handle, protocol,
1990 protocol_interface));
1991 if (r != EFI_SUCCESS)
1992 break;
1993 i++;
1994 }
1995 va_end(argptr);
1996 if (r == EFI_SUCCESS)
1997 return EFI_EXIT(r);
1998
1999 /* If an error occurred undo all changes. */
2000 va_start(argptr, handle);
2001 for (; i; --i) {
2002 protocol = va_arg(argptr, efi_guid_t*);
2003 protocol_interface = va_arg(argptr, void*);
2004 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2005 EFI_NATIVE_INTERFACE,
2006 protocol_interface));
2007 }
2008 va_end(argptr);
2009
2010 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002011}
2012
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002013/*
2014 * Calculate cyclic redundancy code.
2015 *
2016 * This function implements the CalculateCrc32 service.
2017 * See the Unified Extensible Firmware Interface (UEFI) specification
2018 * for details.
2019 *
2020 * @data buffer with data
2021 * @data_size size of buffer in bytes
2022 * @crc32_p cyclic redundancy code
2023 * @return status code
2024 */
Alexander Grafbee91162016-03-04 01:09:59 +01002025static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2026 unsigned long data_size,
2027 uint32_t *crc32_p)
2028{
2029 EFI_ENTRY("%p, %ld", data, data_size);
2030 *crc32_p = crc32(0, data, data_size);
2031 return EFI_EXIT(EFI_SUCCESS);
2032}
2033
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002034/*
2035 * Copy memory.
2036 *
2037 * This function implements the CopyMem service.
2038 * See the Unified Extensible Firmware Interface (UEFI) specification
2039 * for details.
2040 *
2041 * @destination destination of the copy operation
2042 * @source source of the copy operation
2043 * @length number of bytes to copy
2044 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002045static void EFIAPI efi_copy_mem(void *destination, const void *source,
2046 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002047{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002048 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002049 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002050 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002051}
2052
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002053/*
2054 * Fill memory with a byte value.
2055 *
2056 * This function implements the SetMem service.
2057 * See the Unified Extensible Firmware Interface (UEFI) specification
2058 * for details.
2059 *
2060 * @buffer buffer to fill
2061 * @size size of buffer in bytes
2062 * @value byte to copy to the buffer
2063 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002064static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002065{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002066 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002067 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002068 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002069}
2070
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002071/*
2072 * Open protocol interface on a handle.
2073 *
2074 * This function implements the OpenProtocol interface.
2075 * See the Unified Extensible Firmware Interface (UEFI) specification
2076 * for details.
2077 *
2078 * @handle handle on which the protocol shall be opened
2079 * @protocol GUID of the protocol
2080 * @protocol_interface interface implementing the protocol
2081 * @agent_handle handle of the driver
2082 * @controller_handle handle of the controller
2083 * @attributes attributes indicating how to open the protocol
2084 * @return status code
2085 */
Alexander Grafbee91162016-03-04 01:09:59 +01002086static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002087 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002088 void **protocol_interface, void *agent_handle,
2089 void *controller_handle, uint32_t attributes)
2090{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002091 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002092 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002093
Rob Clark778e6af2017-09-13 18:05:41 -04002094 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002095 protocol_interface, agent_handle, controller_handle,
2096 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002097
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002098 if (!handle || !protocol ||
2099 (!protocol_interface && attributes !=
2100 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2101 goto out;
2102 }
2103
2104 switch (attributes) {
2105 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2106 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2107 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2108 break;
2109 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2110 if (controller_handle == handle)
2111 goto out;
2112 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2113 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2114 if (controller_handle == NULL)
2115 goto out;
2116 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2117 if (agent_handle == NULL)
2118 goto out;
2119 break;
2120 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002121 goto out;
2122 }
2123
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002124 r = efi_search_protocol(handle, protocol, &handler);
2125 if (r != EFI_SUCCESS)
2126 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002127
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002128 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2129 *protocol_interface = handler->protocol_interface;
Alexander Grafbee91162016-03-04 01:09:59 +01002130out:
2131 return EFI_EXIT(r);
2132}
2133
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002134/*
2135 * Get interface of a protocol on a handle.
2136 *
2137 * This function implements the HandleProtocol service.
2138 * See the Unified Extensible Firmware Interface (UEFI) specification
2139 * for details.
2140 *
2141 * @handle handle on which the protocol shall be opened
2142 * @protocol GUID of the protocol
2143 * @protocol_interface interface implementing the protocol
2144 * @return status code
2145 */
Alexander Grafbee91162016-03-04 01:09:59 +01002146static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002147 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002148 void **protocol_interface)
2149{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002150 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2151 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002152}
2153
2154static const struct efi_boot_services efi_boot_services = {
2155 .hdr = {
2156 .headersize = sizeof(struct efi_table_hdr),
2157 },
2158 .raise_tpl = efi_raise_tpl,
2159 .restore_tpl = efi_restore_tpl,
2160 .allocate_pages = efi_allocate_pages_ext,
2161 .free_pages = efi_free_pages_ext,
2162 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002163 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002164 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002165 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002166 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002167 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002168 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002169 .close_event = efi_close_event,
2170 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002171 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002172 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002173 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002174 .handle_protocol = efi_handle_protocol,
2175 .reserved = NULL,
2176 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002177 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002178 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002179 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002180 .load_image = efi_load_image,
2181 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002182 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002183 .unload_image = efi_unload_image,
2184 .exit_boot_services = efi_exit_boot_services,
2185 .get_next_monotonic_count = efi_get_next_monotonic_count,
2186 .stall = efi_stall,
2187 .set_watchdog_timer = efi_set_watchdog_timer,
2188 .connect_controller = efi_connect_controller,
2189 .disconnect_controller = efi_disconnect_controller,
2190 .open_protocol = efi_open_protocol,
2191 .close_protocol = efi_close_protocol,
2192 .open_protocol_information = efi_open_protocol_information,
2193 .protocols_per_handle = efi_protocols_per_handle,
2194 .locate_handle_buffer = efi_locate_handle_buffer,
2195 .locate_protocol = efi_locate_protocol,
2196 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2197 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2198 .calculate_crc32 = efi_calculate_crc32,
2199 .copy_mem = efi_copy_mem,
2200 .set_mem = efi_set_mem,
2201};
2202
2203
Heinrich Schuchardt05b6f562017-12-11 20:10:20 +01002204static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01002205
Alexander Graf3c63db92016-10-14 13:45:30 +02002206struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002207 .hdr = {
2208 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2209 .revision = 0x20005, /* 2.5 */
2210 .headersize = sizeof(struct efi_table_hdr),
2211 },
2212 .fw_vendor = (long)firmware_vendor,
2213 .con_in = (void*)&efi_con_in,
2214 .con_out = (void*)&efi_con_out,
2215 .std_err = (void*)&efi_con_out,
2216 .runtime = (void*)&efi_runtime_services,
2217 .boottime = (void*)&efi_boot_services,
2218 .nr_tables = 0,
2219 .tables = (void*)efi_conf_table,
2220};