2 * EFI application boot time services
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <efi_loader.h>
12 #include <asm/global_data.h>
13 #include <libfdt_env.h>
14 #include <u-boot/crc.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* Task priority level */
22 static UINTN efi_tpl = TPL_APPLICATION;
24 /* This list contains all the EFI objects our payload has access to */
25 LIST_HEAD(efi_obj_list);
28 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
29 * we need to do trickery with caches. Since we don't want to break the EFI
30 * aware boot path, only apply hacks when loading exiting directly (breaking
31 * direct Linux EFI booting along the way - oh well).
33 static bool efi_is_direct_boot = true;
36 * EFI can pass arbitrary additional "tables" containing vendor specific
37 * information to the payload. One such table is the FDT table which contains
38 * a pointer to a flattened device tree blob.
40 * In most cases we want to pass an FDT to the payload, so reserve one slot of
41 * config table space for it. The pointer gets populated by do_bootefi_exec().
43 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
47 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
48 * fixed when compiling U-Boot. However, the payload does not know about that
49 * restriction so we need to manually swap its and our view of that register on
50 * EFI callback entry/exit.
52 static volatile void *efi_gd, *app_gd;
55 static int entry_count;
56 static int nesting_level;
58 /* Called on every callback entry */
59 int __efi_entry_check(void)
61 int ret = entry_count++ == 0;
70 /* Called on every callback exit */
71 int __efi_exit_check(void)
73 int ret = --entry_count == 0;
80 /* Called from do_bootefi_exec() */
81 void efi_save_gd(void)
89 * Special case handler for error/abort that just forces things back
90 * to u-boot world so we can dump out an abort msg, without any care
91 * about returning back to UEFI world.
93 void efi_restore_gd(void)
96 /* Only restore if we're already in EFI context */
104 * Two spaces per indent level, maxing out at 10.. which ought to be
105 * enough for anyone ;-)
107 static const char *indent_string(int level)
109 const char *indent = " ";
110 const int max = strlen(indent);
111 level = min(max, level * 2);
112 return &indent[max - level];
115 const char *__efi_nesting(void)
117 return indent_string(nesting_level);
120 const char *__efi_nesting_inc(void)
122 return indent_string(nesting_level++);
125 const char *__efi_nesting_dec(void)
127 return indent_string(--nesting_level);
131 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
133 #define EFI_HIGH32(a) (a >> 32)
136 * 64bit division by 10 implemented as multiplication by 1 / 10
138 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
140 #define EFI_TENTH 0x199999999999999A
141 static u64 efi_div10(u64 a)
147 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
148 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
149 rem = EFI_LOW32(prod);
150 ret += EFI_HIGH32(prod);
151 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
152 rem += EFI_LOW32(prod);
153 ret += EFI_HIGH32(prod);
154 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
155 rem += EFI_HIGH32(prod);
156 ret += EFI_HIGH32(rem);
157 /* Round to nearest integer */
158 if (rem >= (1 << 31))
163 void efi_signal_event(struct efi_event *event)
165 if (event->notify_function) {
168 if (efi_tpl >= event->notify_tpl)
170 EFI_CALL_VOID(event->notify_function(event,
171 event->notify_context));
176 static efi_status_t efi_unsupported(const char *funcname)
178 debug("EFI: App called into unimplemented function %s\n", funcname);
179 return EFI_EXIT(EFI_UNSUPPORTED);
182 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
184 UINTN old_tpl = efi_tpl;
186 EFI_ENTRY("0x%zx", new_tpl);
188 if (new_tpl < efi_tpl)
189 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
191 if (efi_tpl > TPL_HIGH_LEVEL)
192 efi_tpl = TPL_HIGH_LEVEL;
194 EFI_EXIT(EFI_SUCCESS);
198 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
200 EFI_ENTRY("0x%zx", old_tpl);
202 if (old_tpl > efi_tpl)
203 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
205 if (efi_tpl > TPL_HIGH_LEVEL)
206 efi_tpl = TPL_HIGH_LEVEL;
208 EFI_EXIT(EFI_SUCCESS);
211 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
217 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
218 r = efi_allocate_pages(type, memory_type, pages, memory);
222 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
227 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
228 r = efi_free_pages(memory, pages);
232 static efi_status_t EFIAPI efi_get_memory_map_ext(
233 unsigned long *memory_map_size,
234 struct efi_mem_desc *memory_map,
235 unsigned long *map_key,
236 unsigned long *descriptor_size,
237 uint32_t *descriptor_version)
241 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
242 map_key, descriptor_size, descriptor_version);
243 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
244 descriptor_size, descriptor_version);
248 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
254 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
255 r = efi_allocate_pool(pool_type, size, buffer);
259 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
263 EFI_ENTRY("%p", buffer);
264 r = efi_free_pool(buffer);
269 * Our event capabilities are very limited. Only a small limited
270 * number of events is allowed to coexist.
272 static struct efi_event efi_events[16];
274 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
275 void (EFIAPI *notify_function) (
276 struct efi_event *event,
278 void *notify_context, struct efi_event **event)
283 return EFI_INVALID_PARAMETER;
285 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
286 return EFI_INVALID_PARAMETER;
288 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
289 notify_function == NULL)
290 return EFI_INVALID_PARAMETER;
292 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
293 if (efi_events[i].type)
295 efi_events[i].type = type;
296 efi_events[i].notify_tpl = notify_tpl;
297 efi_events[i].notify_function = notify_function;
298 efi_events[i].notify_context = notify_context;
299 /* Disable timers on bootup */
300 efi_events[i].trigger_next = -1ULL;
301 efi_events[i].queued = 0;
302 efi_events[i].signaled = 0;
303 *event = &efi_events[i];
306 return EFI_OUT_OF_RESOURCES;
309 static efi_status_t EFIAPI efi_create_event_ext(
310 uint32_t type, UINTN notify_tpl,
311 void (EFIAPI *notify_function) (
312 struct efi_event *event,
314 void *notify_context, struct efi_event **event)
316 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
318 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
319 notify_context, event));
324 * Our timers have to work without interrupts, so we check whenever keyboard
325 * input or disk accesses happen if enough time elapsed for it to fire.
327 void efi_timer_check(void)
330 u64 now = timer_get_us();
332 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
333 if (!efi_events[i].type)
335 if (efi_events[i].queued)
336 efi_signal_event(&efi_events[i]);
337 if (!(efi_events[i].type & EVT_TIMER) ||
338 now < efi_events[i].trigger_next)
340 switch (efi_events[i].trigger_type) {
341 case EFI_TIMER_RELATIVE:
342 efi_events[i].trigger_type = EFI_TIMER_STOP;
344 case EFI_TIMER_PERIODIC:
345 efi_events[i].trigger_next +=
346 efi_events[i].trigger_time;
351 efi_events[i].signaled = 1;
352 efi_signal_event(&efi_events[i]);
357 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
358 uint64_t trigger_time)
363 * The parameter defines a multiple of 100ns.
364 * We use multiples of 1000ns. So divide by 10.
366 trigger_time = efi_div10(trigger_time);
368 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
369 if (event != &efi_events[i])
372 if (!(event->type & EVT_TIMER))
376 event->trigger_next = -1ULL;
378 case EFI_TIMER_PERIODIC:
379 case EFI_TIMER_RELATIVE:
380 event->trigger_next =
381 timer_get_us() + trigger_time;
384 return EFI_INVALID_PARAMETER;
386 event->trigger_type = type;
387 event->trigger_time = trigger_time;
391 return EFI_INVALID_PARAMETER;
394 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
395 enum efi_timer_delay type,
396 uint64_t trigger_time)
398 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
399 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
402 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
403 struct efi_event **event,
404 unsigned long *index)
408 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
410 /* Check parameters */
411 if (!num_events || !event)
412 return EFI_EXIT(EFI_INVALID_PARAMETER);
414 if (efi_tpl != TPL_APPLICATION)
415 return EFI_EXIT(EFI_UNSUPPORTED);
416 for (i = 0; i < num_events; ++i) {
417 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
418 if (event[i] == &efi_events[j])
421 return EFI_EXIT(EFI_INVALID_PARAMETER);
423 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
424 return EFI_EXIT(EFI_INVALID_PARAMETER);
425 if (!event[i]->signaled)
426 efi_signal_event(event[i]);
429 /* Wait for signal */
431 for (i = 0; i < num_events; ++i) {
432 if (event[i]->signaled)
435 /* Allow events to occur. */
441 * Reset the signal which is passed to the caller to allow periodic
444 event[i]->signaled = 0;
448 return EFI_EXIT(EFI_SUCCESS);
451 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
455 EFI_ENTRY("%p", event);
456 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
457 if (event != &efi_events[i])
462 if (event->type & EVT_NOTIFY_SIGNAL)
463 efi_signal_event(event);
466 return EFI_EXIT(EFI_SUCCESS);
469 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
473 EFI_ENTRY("%p", event);
474 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
475 if (event == &efi_events[i]) {
477 event->trigger_next = -1ULL;
480 return EFI_EXIT(EFI_SUCCESS);
483 return EFI_EXIT(EFI_INVALID_PARAMETER);
486 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
490 EFI_ENTRY("%p", event);
492 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
493 if (event != &efi_events[i])
495 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
497 if (!event->signaled)
498 efi_signal_event(event);
500 return EFI_EXIT(EFI_SUCCESS);
501 return EFI_EXIT(EFI_NOT_READY);
503 return EFI_EXIT(EFI_INVALID_PARAMETER);
506 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
507 efi_guid_t *protocol, int protocol_interface_type,
508 void *protocol_interface)
510 struct list_head *lhandle;
514 if (!handle || !protocol ||
515 protocol_interface_type != EFI_NATIVE_INTERFACE) {
516 r = EFI_INVALID_PARAMETER;
520 /* Create new handle if requested. */
522 r = EFI_OUT_OF_RESOURCES;
526 list_for_each(lhandle, &efi_obj_list) {
527 struct efi_object *efiobj;
528 efiobj = list_entry(lhandle, struct efi_object, link);
530 if (efiobj->handle != *handle)
532 /* Check if protocol is already installed on the handle. */
533 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
534 struct efi_handler *handler = &efiobj->protocols[i];
538 if (!guidcmp(handler->guid, protocol)) {
539 r = EFI_INVALID_PARAMETER;
543 /* Install protocol in first empty slot. */
544 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
545 struct efi_handler *handler = &efiobj->protocols[i];
550 handler->guid = protocol;
551 handler->protocol_interface = protocol_interface;
555 r = EFI_OUT_OF_RESOURCES;
558 r = EFI_INVALID_PARAMETER;
563 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
564 efi_guid_t *protocol, int protocol_interface_type,
565 void *protocol_interface)
567 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
570 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
571 protocol_interface_type,
572 protocol_interface));
575 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
576 efi_guid_t *protocol, void *old_interface,
579 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
581 return EFI_EXIT(EFI_ACCESS_DENIED);
584 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
585 efi_guid_t *protocol, void *protocol_interface)
587 struct list_head *lhandle;
589 efi_status_t r = EFI_NOT_FOUND;
591 if (!handle || !protocol) {
592 r = EFI_INVALID_PARAMETER;
596 list_for_each(lhandle, &efi_obj_list) {
597 struct efi_object *efiobj;
598 efiobj = list_entry(lhandle, struct efi_object, link);
600 if (efiobj->handle != handle)
603 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
604 struct efi_handler *handler = &efiobj->protocols[i];
605 const efi_guid_t *hprotocol = handler->guid;
609 if (!guidcmp(hprotocol, protocol)) {
610 if (handler->protocol_interface) {
611 r = EFI_ACCESS_DENIED;
625 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
626 efi_guid_t *protocol, void *protocol_interface)
628 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
630 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
631 protocol_interface));
634 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
635 struct efi_event *event,
638 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
639 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
642 static int efi_search(enum efi_locate_search_type search_type,
643 efi_guid_t *protocol, void *search_key,
644 struct efi_object *efiobj)
648 switch (search_type) {
651 case by_register_notify:
654 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
655 const efi_guid_t *guid = efiobj->protocols[i].guid;
656 if (guid && !guidcmp(guid, protocol))
665 static efi_status_t efi_locate_handle(
666 enum efi_locate_search_type search_type,
667 efi_guid_t *protocol, void *search_key,
668 unsigned long *buffer_size, efi_handle_t *buffer)
670 struct list_head *lhandle;
671 unsigned long size = 0;
673 /* Count how much space we need */
674 list_for_each(lhandle, &efi_obj_list) {
675 struct efi_object *efiobj;
676 efiobj = list_entry(lhandle, struct efi_object, link);
677 if (!efi_search(search_type, protocol, search_key, efiobj)) {
678 size += sizeof(void*);
682 if (*buffer_size < size) {
684 return EFI_BUFFER_TOO_SMALL;
689 return EFI_NOT_FOUND;
691 /* Then fill the array */
692 list_for_each(lhandle, &efi_obj_list) {
693 struct efi_object *efiobj;
694 efiobj = list_entry(lhandle, struct efi_object, link);
695 if (!efi_search(search_type, protocol, search_key, efiobj)) {
696 *(buffer++) = efiobj->handle;
703 static efi_status_t EFIAPI efi_locate_handle_ext(
704 enum efi_locate_search_type search_type,
705 efi_guid_t *protocol, void *search_key,
706 unsigned long *buffer_size, efi_handle_t *buffer)
708 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
709 buffer_size, buffer);
711 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
712 buffer_size, buffer));
715 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
716 struct efi_device_path **device_path,
717 efi_handle_t *device)
719 struct efi_object *efiobj;
721 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
723 efiobj = efi_dp_find_obj(*device_path, device_path);
725 return EFI_EXIT(EFI_NOT_FOUND);
727 *device = efiobj->handle;
729 return EFI_EXIT(EFI_SUCCESS);
732 /* Collapses configuration table entries, removing index i */
733 static void efi_remove_configuration_table(int i)
735 struct efi_configuration_table *this = &efi_conf_table[i];
736 struct efi_configuration_table *next = &efi_conf_table[i+1];
737 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
739 memmove(this, next, (ulong)end - (ulong)next);
743 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
747 /* Check for guid override */
748 for (i = 0; i < systab.nr_tables; i++) {
749 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
751 efi_conf_table[i].table = table;
753 efi_remove_configuration_table(i);
759 return EFI_NOT_FOUND;
761 /* No override, check for overflow */
762 if (i >= ARRAY_SIZE(efi_conf_table))
763 return EFI_OUT_OF_RESOURCES;
765 /* Add a new entry */
766 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
767 efi_conf_table[i].table = table;
768 systab.nr_tables = i + 1;
773 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
776 EFI_ENTRY("%p, %p", guid, table);
777 return EFI_EXIT(efi_install_configuration_table(guid, table));
780 /* Initialize a loaded_image_info + loaded_image_info object with correct
781 * protocols, boot-device, etc.
783 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
784 struct efi_device_path *device_path,
785 struct efi_device_path *file_path)
790 * When asking for the device path interface, return
791 * bootefi_device_path
793 obj->protocols[0].guid = &efi_guid_device_path;
794 obj->protocols[0].protocol_interface = device_path;
797 * When asking for the loaded_image interface, just
798 * return handle which points to loaded_image_info
800 obj->protocols[1].guid = &efi_guid_loaded_image;
801 obj->protocols[1].protocol_interface = info;
803 obj->protocols[2].guid = &efi_guid_console_control;
804 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
806 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
807 obj->protocols[3].protocol_interface =
808 (void *)&efi_device_path_to_text;
810 info->file_path = file_path;
811 info->device_handle = efi_dp_find_obj(device_path, NULL);
813 list_add_tail(&obj->link, &efi_obj_list);
816 static efi_status_t load_image_from_path(struct efi_device_path *file_path,
819 struct efi_file_info *info = NULL;
820 struct efi_file_handle *f;
821 static efi_status_t ret;
824 f = efi_file_from_path(file_path);
826 return EFI_DEVICE_ERROR;
829 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
831 if (ret == EFI_BUFFER_TOO_SMALL) {
833 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
836 if (ret != EFI_SUCCESS)
839 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
843 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
847 EFI_CALL(f->close(f));
849 if (ret != EFI_SUCCESS) {
850 efi_free_pool(*buffer);
857 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
858 efi_handle_t parent_image,
859 struct efi_device_path *file_path,
861 unsigned long source_size,
862 efi_handle_t *image_handle)
864 struct efi_loaded_image *info;
865 struct efi_object *obj;
867 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
868 file_path, source_buffer, source_size, image_handle);
870 info = calloc(1, sizeof(*info));
871 obj = calloc(1, sizeof(*obj));
873 if (!source_buffer) {
874 struct efi_device_path *dp, *fp;
877 ret = load_image_from_path(file_path, &source_buffer);
878 if (ret != EFI_SUCCESS) {
881 return EFI_EXIT(ret);
885 * split file_path which contains both the device and
888 efi_dp_split_file_path(file_path, &dp, &fp);
890 efi_setup_loaded_image(info, obj, dp, fp);
892 /* In this case, file_path is the "device" path, ie.
893 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
895 efi_setup_loaded_image(info, obj, file_path, NULL);
898 info->reserved = efi_load_pe(source_buffer, info);
899 if (!info->reserved) {
902 return EFI_EXIT(EFI_UNSUPPORTED);
905 *image_handle = info;
907 return EFI_EXIT(EFI_SUCCESS);
910 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
911 unsigned long *exit_data_size,
914 ulong (*entry)(void *image_handle, struct efi_system_table *st);
915 struct efi_loaded_image *info = image_handle;
917 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
918 entry = info->reserved;
920 efi_is_direct_boot = false;
922 /* call the image! */
923 if (setjmp(&info->exit_jmp)) {
924 /* We returned from the child image */
925 return EFI_EXIT(info->exit_status);
930 entry(image_handle, &systab);
934 /* Should usually never get here */
935 return EFI_EXIT(EFI_SUCCESS);
938 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
939 efi_status_t exit_status, unsigned long exit_data_size,
942 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
944 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
945 exit_data_size, exit_data);
947 /* Make sure entry/exit counts for EFI world cross-overs match */
951 * But longjmp out with the U-Boot gd, not the application's, as
952 * the other end is a setjmp call inside EFI context.
956 loaded_image_info->exit_status = exit_status;
957 longjmp(&loaded_image_info->exit_jmp, 1);
959 panic("EFI application exited");
962 static struct efi_object *efi_search_obj(void *handle)
964 struct list_head *lhandle;
966 list_for_each(lhandle, &efi_obj_list) {
967 struct efi_object *efiobj;
968 efiobj = list_entry(lhandle, struct efi_object, link);
969 if (efiobj->handle == handle)
976 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
978 struct efi_object *efiobj;
980 EFI_ENTRY("%p", image_handle);
981 efiobj = efi_search_obj(image_handle);
983 list_del(&efiobj->link);
985 return EFI_EXIT(EFI_SUCCESS);
988 static void efi_exit_caches(void)
990 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
992 * Grub on 32bit ARM needs to have caches disabled before jumping into
993 * a zImage, but does not know of all cache layers. Give it a hand.
995 if (efi_is_direct_boot)
996 cleanup_before_linux();
1000 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1001 unsigned long map_key)
1005 EFI_ENTRY("%p, %ld", image_handle, map_key);
1007 /* Notify that ExitBootServices is invoked. */
1008 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1009 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1011 efi_signal_event(&efi_events[i]);
1013 /* Make sure that notification functions are not called anymore */
1014 efi_tpl = TPL_HIGH_LEVEL;
1016 board_quiesce_devices();
1018 /* Fix up caches for EFI payloads if necessary */
1021 /* This stops all lingering devices */
1022 bootm_disable_interrupts();
1024 /* Give the payload some time to boot */
1027 return EFI_EXIT(EFI_SUCCESS);
1030 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1032 static uint64_t mono = 0;
1033 EFI_ENTRY("%p", count);
1035 return EFI_EXIT(EFI_SUCCESS);
1038 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1040 EFI_ENTRY("%ld", microseconds);
1041 udelay(microseconds);
1042 return EFI_EXIT(EFI_SUCCESS);
1045 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1046 uint64_t watchdog_code,
1047 unsigned long data_size,
1048 uint16_t *watchdog_data)
1050 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1051 data_size, watchdog_data);
1052 return efi_unsupported(__func__);
1055 static efi_status_t EFIAPI efi_connect_controller(
1056 efi_handle_t controller_handle,
1057 efi_handle_t *driver_image_handle,
1058 struct efi_device_path *remain_device_path,
1061 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1062 remain_device_path, recursive);
1063 return EFI_EXIT(EFI_NOT_FOUND);
1066 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1067 void *driver_image_handle,
1070 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1072 return EFI_EXIT(EFI_INVALID_PARAMETER);
1075 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1076 efi_guid_t *protocol,
1078 void *controller_handle)
1080 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
1082 return EFI_EXIT(EFI_NOT_FOUND);
1085 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1086 efi_guid_t *protocol,
1087 struct efi_open_protocol_info_entry **entry_buffer,
1088 unsigned long *entry_count)
1090 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
1092 return EFI_EXIT(EFI_NOT_FOUND);
1095 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1096 efi_guid_t ***protocol_buffer,
1097 unsigned long *protocol_buffer_count)
1099 unsigned long buffer_size;
1100 struct efi_object *efiobj;
1102 struct list_head *lhandle;
1105 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1106 protocol_buffer_count);
1108 if (!handle || !protocol_buffer || !protocol_buffer_count)
1109 return EFI_EXIT(EFI_INVALID_PARAMETER);
1111 *protocol_buffer = NULL;
1112 *protocol_buffer_count = 0;
1113 list_for_each(lhandle, &efi_obj_list) {
1114 efiobj = list_entry(lhandle, struct efi_object, link);
1116 if (efiobj->handle != handle)
1119 /* Count protocols */
1120 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1121 if (efiobj->protocols[i].guid)
1122 ++*protocol_buffer_count;
1125 if (*protocol_buffer_count) {
1126 buffer_size = sizeof(efi_guid_t *) *
1127 *protocol_buffer_count;
1128 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1130 (void **)protocol_buffer);
1131 if (r != EFI_SUCCESS)
1134 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1135 if (efiobj->protocols[i].guid) {
1136 (*protocol_buffer)[j] = (void *)
1137 efiobj->protocols[i].guid;
1145 return EFI_EXIT(EFI_SUCCESS);
1148 static efi_status_t EFIAPI efi_locate_handle_buffer(
1149 enum efi_locate_search_type search_type,
1150 efi_guid_t *protocol, void *search_key,
1151 unsigned long *no_handles, efi_handle_t **buffer)
1154 unsigned long buffer_size = 0;
1156 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1157 no_handles, buffer);
1159 if (!no_handles || !buffer) {
1160 r = EFI_INVALID_PARAMETER;
1165 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1167 if (r != EFI_BUFFER_TOO_SMALL)
1169 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1171 if (r != EFI_SUCCESS)
1173 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1175 if (r == EFI_SUCCESS)
1176 *no_handles = buffer_size / sizeof(void *);
1181 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1183 void **protocol_interface)
1185 struct list_head *lhandle;
1188 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
1190 if (!protocol || !protocol_interface)
1191 return EFI_EXIT(EFI_INVALID_PARAMETER);
1193 EFI_PRINT_GUID("protocol", protocol);
1195 list_for_each(lhandle, &efi_obj_list) {
1196 struct efi_object *efiobj;
1198 efiobj = list_entry(lhandle, struct efi_object, link);
1199 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1200 struct efi_handler *handler = &efiobj->protocols[i];
1204 if (!guidcmp(handler->guid, protocol)) {
1205 *protocol_interface =
1206 handler->protocol_interface;
1207 return EFI_EXIT(EFI_SUCCESS);
1211 *protocol_interface = NULL;
1213 return EFI_EXIT(EFI_NOT_FOUND);
1216 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1219 EFI_ENTRY("%p", handle);
1222 efi_guid_t *protocol;
1223 void *protocol_interface;
1224 efi_status_t r = EFI_SUCCESS;
1228 return EFI_EXIT(EFI_INVALID_PARAMETER);
1230 va_start(argptr, handle);
1232 protocol = va_arg(argptr, efi_guid_t*);
1235 protocol_interface = va_arg(argptr, void*);
1236 r = efi_install_protocol_interface(handle, protocol,
1237 EFI_NATIVE_INTERFACE,
1238 protocol_interface);
1239 if (r != EFI_SUCCESS)
1244 if (r == EFI_SUCCESS)
1247 /* If an error occured undo all changes. */
1248 va_start(argptr, handle);
1250 protocol = va_arg(argptr, efi_guid_t*);
1251 protocol_interface = va_arg(argptr, void*);
1252 efi_uninstall_protocol_interface(handle, protocol,
1253 protocol_interface);
1260 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1263 EFI_ENTRY("%p", handle);
1264 return EFI_EXIT(EFI_INVALID_PARAMETER);
1267 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1268 unsigned long data_size,
1271 EFI_ENTRY("%p, %ld", data, data_size);
1272 *crc32_p = crc32(0, data, data_size);
1273 return EFI_EXIT(EFI_SUCCESS);
1276 static void EFIAPI efi_copy_mem(void *destination, void *source,
1277 unsigned long length)
1279 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1280 memcpy(destination, source, length);
1283 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1285 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1286 memset(buffer, value, size);
1289 static efi_status_t EFIAPI efi_open_protocol(
1290 void *handle, efi_guid_t *protocol,
1291 void **protocol_interface, void *agent_handle,
1292 void *controller_handle, uint32_t attributes)
1294 struct list_head *lhandle;
1296 efi_status_t r = EFI_INVALID_PARAMETER;
1298 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1299 protocol_interface, agent_handle, controller_handle,
1302 if (!handle || !protocol ||
1303 (!protocol_interface && attributes !=
1304 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1308 EFI_PRINT_GUID("protocol", protocol);
1310 switch (attributes) {
1311 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1312 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1313 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1315 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1316 if (controller_handle == handle)
1318 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1319 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1320 if (controller_handle == NULL)
1322 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1323 if (agent_handle == NULL)
1330 list_for_each(lhandle, &efi_obj_list) {
1331 struct efi_object *efiobj;
1332 efiobj = list_entry(lhandle, struct efi_object, link);
1334 if (efiobj->handle != handle)
1337 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1338 struct efi_handler *handler = &efiobj->protocols[i];
1339 const efi_guid_t *hprotocol = handler->guid;
1342 if (!guidcmp(hprotocol, protocol)) {
1344 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1345 *protocol_interface =
1346 handler->protocol_interface;
1356 r = EFI_UNSUPPORTED;
1361 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1362 efi_guid_t *protocol,
1363 void **protocol_interface)
1365 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1366 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1369 static const struct efi_boot_services efi_boot_services = {
1371 .headersize = sizeof(struct efi_table_hdr),
1373 .raise_tpl = efi_raise_tpl,
1374 .restore_tpl = efi_restore_tpl,
1375 .allocate_pages = efi_allocate_pages_ext,
1376 .free_pages = efi_free_pages_ext,
1377 .get_memory_map = efi_get_memory_map_ext,
1378 .allocate_pool = efi_allocate_pool_ext,
1379 .free_pool = efi_free_pool_ext,
1380 .create_event = efi_create_event_ext,
1381 .set_timer = efi_set_timer_ext,
1382 .wait_for_event = efi_wait_for_event,
1383 .signal_event = efi_signal_event_ext,
1384 .close_event = efi_close_event,
1385 .check_event = efi_check_event,
1386 .install_protocol_interface = efi_install_protocol_interface_ext,
1387 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1388 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1389 .handle_protocol = efi_handle_protocol,
1391 .register_protocol_notify = efi_register_protocol_notify,
1392 .locate_handle = efi_locate_handle_ext,
1393 .locate_device_path = efi_locate_device_path,
1394 .install_configuration_table = efi_install_configuration_table_ext,
1395 .load_image = efi_load_image,
1396 .start_image = efi_start_image,
1398 .unload_image = efi_unload_image,
1399 .exit_boot_services = efi_exit_boot_services,
1400 .get_next_monotonic_count = efi_get_next_monotonic_count,
1402 .set_watchdog_timer = efi_set_watchdog_timer,
1403 .connect_controller = efi_connect_controller,
1404 .disconnect_controller = efi_disconnect_controller,
1405 .open_protocol = efi_open_protocol,
1406 .close_protocol = efi_close_protocol,
1407 .open_protocol_information = efi_open_protocol_information,
1408 .protocols_per_handle = efi_protocols_per_handle,
1409 .locate_handle_buffer = efi_locate_handle_buffer,
1410 .locate_protocol = efi_locate_protocol,
1411 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1412 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1413 .calculate_crc32 = efi_calculate_crc32,
1414 .copy_mem = efi_copy_mem,
1415 .set_mem = efi_set_mem,
1419 static uint16_t __efi_runtime_data firmware_vendor[] =
1420 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1422 struct efi_system_table __efi_runtime_data systab = {
1424 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1425 .revision = 0x20005, /* 2.5 */
1426 .headersize = sizeof(struct efi_table_hdr),
1428 .fw_vendor = (long)firmware_vendor,
1429 .con_in = (void*)&efi_con_in,
1430 .con_out = (void*)&efi_con_out,
1431 .std_err = (void*)&efi_con_out,
1432 .runtime = (void*)&efi_runtime_services,
1433 .boottime = (void*)&efi_boot_services,
1435 .tables = (void*)efi_conf_table,