2 * EFI application boot time services
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
9 /* #define DEBUG_EFI */
12 #include <efi_loader.h>
14 #include <asm/global_data.h>
15 #include <libfdt_env.h>
16 #include <u-boot/crc.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* This list contains all the EFI objects our payload has access to */
24 LIST_HEAD(efi_obj_list);
27 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
28 * we need to do trickery with caches. Since we don't want to break the EFI
29 * aware boot path, only apply hacks when loading exiting directly (breaking
30 * direct Linux EFI booting along the way - oh well).
32 static bool efi_is_direct_boot = true;
35 * EFI can pass arbitrary additional "tables" containing vendor specific
36 * information to the payload. One such table is the FDT table which contains
37 * a pointer to a flattened device tree blob.
39 * In most cases we want to pass an FDT to the payload, so reserve one slot of
40 * config table space for it. The pointer gets populated by do_bootefi_exec().
42 static struct efi_configuration_table EFI_RUNTIME_DATA efi_conf_table[1];
45 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
46 * fixed when compiling U-Boot. However, the payload does not know about that
47 * restriction so we need to manually swap its and our view of that register on
48 * EFI callback entry/exit.
50 static volatile void *efi_gd, *app_gd;
52 /* Called from do_bootefi_exec() */
53 void efi_save_gd(void)
58 /* Called on every callback entry */
59 void efi_restore_gd(void)
61 /* Only restore if we're already in EFI context */
70 /* Called on every callback exit */
71 efi_status_t efi_exit_func(efi_status_t ret)
77 static efi_status_t efi_unsupported(const char *funcname)
80 printf("EFI: App called into unimplemented function %s\n", funcname);
82 return EFI_EXIT(EFI_UNSUPPORTED);
85 static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
87 return memcmp(g1, g2, sizeof(efi_guid_t));
90 static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl)
92 EFI_ENTRY("0x%lx", new_tpl);
96 static void EFIAPI efi_restore_tpl(unsigned long old_tpl)
98 EFI_ENTRY("0x%lx", old_tpl);
99 EFI_EXIT(efi_unsupported(__func__));
102 efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
108 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
109 r = efi_allocate_pages(type, memory_type, pages, memory);
113 efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, unsigned long pages)
117 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
118 r = efi_free_pages(memory, pages);
122 efi_status_t EFIAPI efi_get_memory_map_ext(unsigned long *memory_map_size,
123 struct efi_mem_desc *memory_map,
124 unsigned long *map_key,
125 unsigned long *descriptor_size,
126 uint32_t *descriptor_version)
130 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
131 map_key, descriptor_size, descriptor_version);
132 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
133 descriptor_size, descriptor_version);
137 static efi_status_t EFIAPI efi_allocate_pool(int pool_type, unsigned long size,
142 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
143 r = efi_allocate_pages(0, pool_type, (size + 0xfff) >> 12, (void*)buffer);
147 static efi_status_t EFIAPI efi_free_pool(void *buffer)
151 EFI_ENTRY("%p", buffer);
152 r = efi_free_pages((ulong)buffer, 0);
157 * Our event capabilities are very limited. Only support a single
158 * event to exist, so we don't need to maintain lists.
161 enum efi_event_type type;
165 unsigned long notify_tpl;
166 void (*notify_function) (void *event, void *context);
167 void *notify_context;
169 /* Disable timers on bootup */
170 .trigger_next = -1ULL,
173 static efi_status_t EFIAPI efi_create_event(
174 enum efi_event_type type, ulong notify_tpl,
175 void (*notify_function) (void *event, void *context),
176 void *notify_context, void **event)
178 EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function,
180 if (efi_event.notify_function) {
181 /* We only support one event at a time */
182 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
185 efi_event.type = type;
186 efi_event.notify_tpl = notify_tpl;
187 efi_event.notify_function = notify_function;
188 efi_event.notify_context = notify_context;
191 return EFI_EXIT(EFI_SUCCESS);
195 * Our timers have to work without interrupts, so we check whenever keyboard
196 * input or disk accesses happen if enough time elapsed for it to fire.
198 void efi_timer_check(void)
200 u64 now = timer_get_us();
202 if (now >= efi_event.trigger_next) {
204 if (efi_event.trigger_type == EFI_TIMER_PERIODIC)
205 efi_event.trigger_next += efi_event.trigger_time / 10;
206 efi_event.notify_function(&efi_event, efi_event.notify_context);
212 static efi_status_t EFIAPI efi_set_timer(void *event, int type,
213 uint64_t trigger_time)
215 /* We don't have 64bit division available everywhere, so limit timer
216 * distances to 32bit bits. */
217 u32 trigger32 = trigger_time;
219 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
221 if (trigger32 < trigger_time) {
222 printf("WARNING: Truncating timer from %"PRIx64" to %x\n",
223 trigger_time, trigger32);
226 if (event != &efi_event) {
227 /* We only support one event at a time */
228 return EFI_EXIT(EFI_INVALID_PARAMETER);
233 efi_event.trigger_next = -1ULL;
235 case EFI_TIMER_PERIODIC:
236 case EFI_TIMER_RELATIVE:
237 efi_event.trigger_next = timer_get_us() + (trigger32 / 10);
240 return EFI_EXIT(EFI_INVALID_PARAMETER);
242 efi_event.trigger_type = type;
243 efi_event.trigger_time = trigger_time;
245 return EFI_EXIT(EFI_SUCCESS);
248 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
249 void *event, unsigned long *index)
253 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
255 now = timer_get_us();
256 while (now < efi_event.trigger_next) { }
259 return EFI_EXIT(EFI_SUCCESS);
262 static efi_status_t EFIAPI efi_signal_event(void *event)
264 EFI_ENTRY("%p", event);
265 return EFI_EXIT(EFI_SUCCESS);
268 static efi_status_t EFIAPI efi_close_event(void *event)
270 EFI_ENTRY("%p", event);
271 efi_event.trigger_next = -1ULL;
272 return EFI_EXIT(EFI_SUCCESS);
275 static efi_status_t EFIAPI efi_check_event(void *event)
277 EFI_ENTRY("%p", event);
278 return EFI_EXIT(EFI_NOT_READY);
281 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
282 efi_guid_t *protocol, int protocol_interface_type,
283 void *protocol_interface)
285 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
287 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
289 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
290 efi_guid_t *protocol, void *old_interface,
293 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
295 return EFI_EXIT(EFI_ACCESS_DENIED);
298 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
299 efi_guid_t *protocol, void *protocol_interface)
301 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
302 return EFI_EXIT(EFI_NOT_FOUND);
305 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
309 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
310 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
313 static int efi_search(enum efi_locate_search_type search_type,
314 efi_guid_t *protocol, void *search_key,
315 struct efi_object *efiobj)
319 switch (search_type) {
322 case by_register_notify:
325 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
326 const efi_guid_t *guid = efiobj->protocols[i].guid;
327 if (guid && !guidcmp(guid, protocol))
336 static efi_status_t EFIAPI efi_locate_handle(
337 enum efi_locate_search_type search_type,
338 efi_guid_t *protocol, void *search_key,
339 unsigned long *buffer_size, efi_handle_t *buffer)
341 struct list_head *lhandle;
342 unsigned long size = 0;
344 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
345 buffer_size, buffer);
347 /* Count how much space we need */
348 list_for_each(lhandle, &efi_obj_list) {
349 struct efi_object *efiobj;
350 efiobj = list_entry(lhandle, struct efi_object, link);
351 if (!efi_search(search_type, protocol, search_key, efiobj)) {
352 size += sizeof(void*);
356 if (*buffer_size < size) {
358 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
361 /* Then fill the array */
362 list_for_each(lhandle, &efi_obj_list) {
363 struct efi_object *efiobj;
364 efiobj = list_entry(lhandle, struct efi_object, link);
365 if (!efi_search(search_type, protocol, search_key, efiobj)) {
366 *(buffer++) = efiobj->handle;
371 return EFI_EXIT(EFI_SUCCESS);
374 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
375 struct efi_device_path **device_path,
376 efi_handle_t *device)
378 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
379 return EFI_EXIT(EFI_NOT_FOUND);
382 static efi_status_t EFIAPI efi_install_configuration_table(efi_guid_t *guid,
387 EFI_ENTRY("%p, %p", guid, table);
389 /* Check for guid override */
390 for (i = 0; i < systab.nr_tables; i++) {
391 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
392 efi_conf_table[i].table = table;
393 return EFI_EXIT(EFI_SUCCESS);
397 /* No override, check for overflow */
398 if (i >= ARRAY_SIZE(efi_conf_table))
399 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
401 /* Add a new entry */
402 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
403 efi_conf_table[i].table = table;
404 systab.nr_tables = i;
406 return EFI_EXIT(EFI_SUCCESS);
409 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
410 efi_handle_t parent_image,
411 struct efi_device_path *file_path,
413 unsigned long source_size,
414 efi_handle_t *image_handle)
416 static struct efi_object loaded_image_info_obj = {
419 .guid = &efi_guid_loaded_image,
420 .open = &efi_return_handle,
424 struct efi_loaded_image *info;
425 struct efi_object *obj;
427 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
428 file_path, source_buffer, source_size, image_handle);
429 info = malloc(sizeof(*info));
430 obj = malloc(sizeof(loaded_image_info_obj));
431 memset(info, 0, sizeof(*info));
432 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
434 info->file_path = file_path;
435 info->reserved = efi_load_pe(source_buffer, info);
436 if (!info->reserved) {
439 return EFI_EXIT(EFI_UNSUPPORTED);
442 *image_handle = info;
443 list_add_tail(&obj->link, &efi_obj_list);
445 return EFI_EXIT(EFI_SUCCESS);
448 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
449 unsigned long *exit_data_size,
452 ulong (*entry)(void *image_handle, struct efi_system_table *st);
453 struct efi_loaded_image *info = image_handle;
455 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
456 entry = info->reserved;
458 efi_is_direct_boot = false;
460 /* call the image! */
461 if (setjmp(&info->exit_jmp)) {
462 /* We returned from the child image */
463 return EFI_EXIT(info->exit_status);
466 entry(image_handle, &systab);
468 /* Should usually never get here */
469 return EFI_EXIT(EFI_SUCCESS);
472 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
473 efi_status_t exit_status, unsigned long exit_data_size,
476 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
478 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
479 exit_data_size, exit_data);
481 loaded_image_info->exit_status = exit_status;
482 longjmp(&loaded_image_info->exit_jmp);
484 panic("EFI application exited");
487 static struct efi_object *efi_search_obj(void *handle)
489 struct list_head *lhandle;
491 list_for_each(lhandle, &efi_obj_list) {
492 struct efi_object *efiobj;
493 efiobj = list_entry(lhandle, struct efi_object, link);
494 if (efiobj->handle == handle)
501 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
503 struct efi_object *efiobj;
505 EFI_ENTRY("%p", image_handle);
506 efiobj = efi_search_obj(image_handle);
508 list_del(&efiobj->link);
510 return EFI_EXIT(EFI_SUCCESS);
513 static void efi_exit_caches(void)
515 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
517 * Grub on 32bit ARM needs to have caches disabled before jumping into
518 * a zImage, but does not know of all cache layers. Give it a hand.
520 if (efi_is_direct_boot)
521 cleanup_before_linux();
525 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
526 unsigned long map_key)
528 EFI_ENTRY("%p, %ld", image_handle, map_key);
530 /* Fix up caches for EFI payloads if necessary */
533 /* This stops all lingering devices */
534 bootm_disable_interrupts();
536 /* Give the payload some time to boot */
539 return EFI_EXIT(EFI_SUCCESS);
542 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
544 static uint64_t mono = 0;
545 EFI_ENTRY("%p", count);
547 return EFI_EXIT(EFI_SUCCESS);
550 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
552 EFI_ENTRY("%ld", microseconds);
553 udelay(microseconds);
554 return EFI_EXIT(EFI_SUCCESS);
557 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
558 uint64_t watchdog_code,
559 unsigned long data_size,
560 uint16_t *watchdog_data)
562 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
563 data_size, watchdog_data);
564 return EFI_EXIT(efi_unsupported(__func__));
567 static efi_status_t EFIAPI efi_connect_controller(
568 efi_handle_t controller_handle,
569 efi_handle_t *driver_image_handle,
570 struct efi_device_path *remain_device_path,
573 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
574 remain_device_path, recursive);
575 return EFI_EXIT(EFI_NOT_FOUND);
578 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
579 void *driver_image_handle,
582 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
584 return EFI_EXIT(EFI_INVALID_PARAMETER);
587 static efi_status_t EFIAPI efi_close_protocol(void *handle,
588 efi_guid_t *protocol,
590 void *controller_handle)
592 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
594 return EFI_EXIT(EFI_NOT_FOUND);
597 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
598 efi_guid_t *protocol,
599 struct efi_open_protocol_info_entry **entry_buffer,
600 unsigned long *entry_count)
602 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
604 return EFI_EXIT(EFI_NOT_FOUND);
607 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
608 efi_guid_t ***protocol_buffer,
609 unsigned long *protocol_buffer_count)
611 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
612 protocol_buffer_count);
613 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
616 static efi_status_t EFIAPI efi_locate_handle_buffer(
617 enum efi_locate_search_type search_type,
618 efi_guid_t *protocol, void *search_key,
619 unsigned long *no_handles, efi_handle_t **buffer)
621 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
623 return EFI_EXIT(EFI_NOT_FOUND);
626 static struct efi_class_map efi_class_maps[] = {
628 .guid = &efi_guid_console_control,
629 .interface = &efi_console_control
633 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
635 void **protocol_interface)
639 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
640 for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) {
641 struct efi_class_map *curmap = &efi_class_maps[i];
642 if (!guidcmp(protocol, curmap->guid)) {
643 *protocol_interface = (void*)curmap->interface;
644 return EFI_EXIT(EFI_SUCCESS);
648 return EFI_EXIT(EFI_NOT_FOUND);
651 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
654 EFI_ENTRY("%p", handle);
655 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
658 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
661 EFI_ENTRY("%p", handle);
662 return EFI_EXIT(EFI_INVALID_PARAMETER);
665 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
666 unsigned long data_size,
669 EFI_ENTRY("%p, %ld", data, data_size);
670 *crc32_p = crc32(0, data, data_size);
671 return EFI_EXIT(EFI_SUCCESS);
674 static void EFIAPI efi_copy_mem(void *destination, void *source,
675 unsigned long length)
677 EFI_ENTRY("%p, %p, %ld", destination, source, length);
678 memcpy(destination, source, length);
681 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
683 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
684 memset(buffer, value, size);
687 static efi_status_t EFIAPI efi_open_protocol(
688 void *handle, efi_guid_t *protocol,
689 void **protocol_interface, void *agent_handle,
690 void *controller_handle, uint32_t attributes)
692 struct list_head *lhandle;
694 efi_status_t r = EFI_UNSUPPORTED;
696 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
697 protocol_interface, agent_handle, controller_handle,
699 list_for_each(lhandle, &efi_obj_list) {
700 struct efi_object *efiobj;
701 efiobj = list_entry(lhandle, struct efi_object, link);
703 if (efiobj->handle != handle)
706 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
707 struct efi_handler *handler = &efiobj->protocols[i];
708 const efi_guid_t *hprotocol = handler->guid;
711 if (!guidcmp(hprotocol, protocol)) {
712 r = handler->open(handle, protocol,
713 protocol_interface, agent_handle,
714 controller_handle, attributes);
724 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
725 efi_guid_t *protocol,
726 void **protocol_interface)
728 return efi_open_protocol(handle, protocol, protocol_interface,
732 static const struct efi_boot_services efi_boot_services = {
734 .headersize = sizeof(struct efi_table_hdr),
736 .raise_tpl = efi_raise_tpl,
737 .restore_tpl = efi_restore_tpl,
738 .allocate_pages = efi_allocate_pages_ext,
739 .free_pages = efi_free_pages_ext,
740 .get_memory_map = efi_get_memory_map_ext,
741 .allocate_pool = efi_allocate_pool,
742 .free_pool = efi_free_pool,
743 .create_event = efi_create_event,
744 .set_timer = efi_set_timer,
745 .wait_for_event = efi_wait_for_event,
746 .signal_event = efi_signal_event,
747 .close_event = efi_close_event,
748 .check_event = efi_check_event,
749 .install_protocol_interface = efi_install_protocol_interface,
750 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
751 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
752 .handle_protocol = efi_handle_protocol,
754 .register_protocol_notify = efi_register_protocol_notify,
755 .locate_handle = efi_locate_handle,
756 .locate_device_path = efi_locate_device_path,
757 .install_configuration_table = efi_install_configuration_table,
758 .load_image = efi_load_image,
759 .start_image = efi_start_image,
761 .unload_image = efi_unload_image,
762 .exit_boot_services = efi_exit_boot_services,
763 .get_next_monotonic_count = efi_get_next_monotonic_count,
765 .set_watchdog_timer = efi_set_watchdog_timer,
766 .connect_controller = efi_connect_controller,
767 .disconnect_controller = efi_disconnect_controller,
768 .open_protocol = efi_open_protocol,
769 .close_protocol = efi_close_protocol,
770 .open_protocol_information = efi_open_protocol_information,
771 .protocols_per_handle = efi_protocols_per_handle,
772 .locate_handle_buffer = efi_locate_handle_buffer,
773 .locate_protocol = efi_locate_protocol,
774 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
775 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
776 .calculate_crc32 = efi_calculate_crc32,
777 .copy_mem = efi_copy_mem,
778 .set_mem = efi_set_mem,
782 static uint16_t EFI_RUNTIME_DATA firmware_vendor[] =
783 { 'D','a','s',' ','U','-','b','o','o','t',0 };
785 struct efi_system_table EFI_RUNTIME_DATA systab = {
787 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
788 .revision = 0x20005, /* 2.5 */
789 .headersize = sizeof(struct efi_table_hdr),
791 .fw_vendor = (long)firmware_vendor,
792 .con_in = (void*)&efi_con_in,
793 .con_out = (void*)&efi_con_out,
794 .std_err = (void*)&efi_con_out,
795 .runtime = (void*)&efi_runtime_services,
796 .boottime = (void*)&efi_boot_services,
798 .tables = (void*)efi_conf_table,