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 /* This list contains all the EFI objects our payload has access to */
22 LIST_HEAD(efi_obj_list);
25 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26 * we need to do trickery with caches. Since we don't want to break the EFI
27 * aware boot path, only apply hacks when loading exiting directly (breaking
28 * direct Linux EFI booting along the way - oh well).
30 static bool efi_is_direct_boot = true;
33 * EFI can pass arbitrary additional "tables" containing vendor specific
34 * information to the payload. One such table is the FDT table which contains
35 * a pointer to a flattened device tree blob.
37 * In most cases we want to pass an FDT to the payload, so reserve one slot of
38 * config table space for it. The pointer gets populated by do_bootefi_exec().
40 static struct efi_configuration_table EFI_RUNTIME_DATA efi_conf_table[1];
43 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
44 * fixed when compiling U-Boot. However, the payload does not know about that
45 * restriction so we need to manually swap its and our view of that register on
46 * EFI callback entry/exit.
48 static volatile void *efi_gd, *app_gd;
50 /* Called from do_bootefi_exec() */
51 void efi_save_gd(void)
56 /* Called on every callback entry */
57 void efi_restore_gd(void)
59 /* Only restore if we're already in EFI context */
68 /* Called on every callback exit */
69 efi_status_t efi_exit_func(efi_status_t ret)
75 static efi_status_t efi_unsupported(const char *funcname)
77 debug("EFI: App called into unimplemented function %s\n", funcname);
78 return EFI_EXIT(EFI_UNSUPPORTED);
81 static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
83 return memcmp(g1, g2, sizeof(efi_guid_t));
86 static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl)
88 EFI_ENTRY("0x%lx", new_tpl);
92 static void EFIAPI efi_restore_tpl(unsigned long old_tpl)
94 EFI_ENTRY("0x%lx", old_tpl);
95 EFI_EXIT(efi_unsupported(__func__));
98 efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
104 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
105 r = efi_allocate_pages(type, memory_type, pages, memory);
109 efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, unsigned long pages)
113 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
114 r = efi_free_pages(memory, pages);
118 efi_status_t EFIAPI efi_get_memory_map_ext(unsigned long *memory_map_size,
119 struct efi_mem_desc *memory_map,
120 unsigned long *map_key,
121 unsigned long *descriptor_size,
122 uint32_t *descriptor_version)
126 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
127 map_key, descriptor_size, descriptor_version);
128 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
129 descriptor_size, descriptor_version);
133 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
139 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
140 r = efi_allocate_pool(pool_type, size, buffer);
144 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
148 EFI_ENTRY("%p", buffer);
149 r = efi_free_pool(buffer);
154 * Our event capabilities are very limited. Only support a single
155 * event to exist, so we don't need to maintain lists.
158 enum efi_event_type type;
162 unsigned long notify_tpl;
163 void (*notify_function) (void *event, void *context);
164 void *notify_context;
166 /* Disable timers on bootup */
167 .trigger_next = -1ULL,
170 static efi_status_t EFIAPI efi_create_event(
171 enum efi_event_type type, ulong notify_tpl,
172 void (*notify_function) (void *event, void *context),
173 void *notify_context, void **event)
175 EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function,
177 if (efi_event.notify_function) {
178 /* We only support one event at a time */
179 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
182 efi_event.type = type;
183 efi_event.notify_tpl = notify_tpl;
184 efi_event.notify_function = notify_function;
185 efi_event.notify_context = notify_context;
188 return EFI_EXIT(EFI_SUCCESS);
192 * Our timers have to work without interrupts, so we check whenever keyboard
193 * input or disk accesses happen if enough time elapsed for it to fire.
195 void efi_timer_check(void)
197 u64 now = timer_get_us();
199 if (now >= efi_event.trigger_next) {
201 if (efi_event.trigger_type == EFI_TIMER_PERIODIC)
202 efi_event.trigger_next += efi_event.trigger_time / 10;
203 efi_event.notify_function(&efi_event, efi_event.notify_context);
209 static efi_status_t EFIAPI efi_set_timer(void *event, int type,
210 uint64_t trigger_time)
212 /* We don't have 64bit division available everywhere, so limit timer
213 * distances to 32bit bits. */
214 u32 trigger32 = trigger_time;
216 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
218 if (trigger32 < trigger_time) {
219 printf("WARNING: Truncating timer from %"PRIx64" to %x\n",
220 trigger_time, trigger32);
223 if (event != &efi_event) {
224 /* We only support one event at a time */
225 return EFI_EXIT(EFI_INVALID_PARAMETER);
230 efi_event.trigger_next = -1ULL;
232 case EFI_TIMER_PERIODIC:
233 case EFI_TIMER_RELATIVE:
234 efi_event.trigger_next = timer_get_us() + (trigger32 / 10);
237 return EFI_EXIT(EFI_INVALID_PARAMETER);
239 efi_event.trigger_type = type;
240 efi_event.trigger_time = trigger_time;
242 return EFI_EXIT(EFI_SUCCESS);
245 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
246 void *event, unsigned long *index)
250 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
252 now = timer_get_us();
253 while (now < efi_event.trigger_next) { }
256 return EFI_EXIT(EFI_SUCCESS);
259 static efi_status_t EFIAPI efi_signal_event(void *event)
261 EFI_ENTRY("%p", event);
262 return EFI_EXIT(EFI_SUCCESS);
265 static efi_status_t EFIAPI efi_close_event(void *event)
267 EFI_ENTRY("%p", event);
268 efi_event.trigger_next = -1ULL;
269 return EFI_EXIT(EFI_SUCCESS);
272 static efi_status_t EFIAPI efi_check_event(void *event)
274 EFI_ENTRY("%p", event);
275 return EFI_EXIT(EFI_NOT_READY);
278 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
279 efi_guid_t *protocol, int protocol_interface_type,
280 void *protocol_interface)
282 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
284 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
286 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
287 efi_guid_t *protocol, void *old_interface,
290 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
292 return EFI_EXIT(EFI_ACCESS_DENIED);
295 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
296 efi_guid_t *protocol, void *protocol_interface)
298 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
299 return EFI_EXIT(EFI_NOT_FOUND);
302 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
306 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
307 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
310 static int efi_search(enum efi_locate_search_type search_type,
311 efi_guid_t *protocol, void *search_key,
312 struct efi_object *efiobj)
316 switch (search_type) {
319 case by_register_notify:
322 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
323 const efi_guid_t *guid = efiobj->protocols[i].guid;
324 if (guid && !guidcmp(guid, protocol))
333 static efi_status_t EFIAPI efi_locate_handle(
334 enum efi_locate_search_type search_type,
335 efi_guid_t *protocol, void *search_key,
336 unsigned long *buffer_size, efi_handle_t *buffer)
338 struct list_head *lhandle;
339 unsigned long size = 0;
341 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
342 buffer_size, buffer);
344 /* Count how much space we need */
345 list_for_each(lhandle, &efi_obj_list) {
346 struct efi_object *efiobj;
347 efiobj = list_entry(lhandle, struct efi_object, link);
348 if (!efi_search(search_type, protocol, search_key, efiobj)) {
349 size += sizeof(void*);
353 if (*buffer_size < size) {
355 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
358 /* Then fill the array */
359 list_for_each(lhandle, &efi_obj_list) {
360 struct efi_object *efiobj;
361 efiobj = list_entry(lhandle, struct efi_object, link);
362 if (!efi_search(search_type, protocol, search_key, efiobj)) {
363 *(buffer++) = efiobj->handle;
368 return EFI_EXIT(EFI_SUCCESS);
371 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
372 struct efi_device_path **device_path,
373 efi_handle_t *device)
375 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
376 return EFI_EXIT(EFI_NOT_FOUND);
379 static efi_status_t EFIAPI efi_install_configuration_table(efi_guid_t *guid,
384 EFI_ENTRY("%p, %p", guid, table);
386 /* Check for guid override */
387 for (i = 0; i < systab.nr_tables; i++) {
388 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
389 efi_conf_table[i].table = table;
390 return EFI_EXIT(EFI_SUCCESS);
394 /* No override, check for overflow */
395 if (i >= ARRAY_SIZE(efi_conf_table))
396 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
398 /* Add a new entry */
399 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
400 efi_conf_table[i].table = table;
401 systab.nr_tables = i;
403 return EFI_EXIT(EFI_SUCCESS);
406 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
407 efi_handle_t parent_image,
408 struct efi_device_path *file_path,
410 unsigned long source_size,
411 efi_handle_t *image_handle)
413 static struct efi_object loaded_image_info_obj = {
416 .guid = &efi_guid_loaded_image,
417 .open = &efi_return_handle,
421 struct efi_loaded_image *info;
422 struct efi_object *obj;
424 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
425 file_path, source_buffer, source_size, image_handle);
426 info = malloc(sizeof(*info));
427 obj = malloc(sizeof(loaded_image_info_obj));
428 memset(info, 0, sizeof(*info));
429 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
431 info->file_path = file_path;
432 info->reserved = efi_load_pe(source_buffer, info);
433 if (!info->reserved) {
436 return EFI_EXIT(EFI_UNSUPPORTED);
439 *image_handle = info;
440 list_add_tail(&obj->link, &efi_obj_list);
442 return EFI_EXIT(EFI_SUCCESS);
445 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
446 unsigned long *exit_data_size,
449 ulong (*entry)(void *image_handle, struct efi_system_table *st);
450 struct efi_loaded_image *info = image_handle;
452 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
453 entry = info->reserved;
455 efi_is_direct_boot = false;
457 /* call the image! */
458 if (setjmp(&info->exit_jmp)) {
459 /* We returned from the child image */
460 return EFI_EXIT(info->exit_status);
463 entry(image_handle, &systab);
465 /* Should usually never get here */
466 return EFI_EXIT(EFI_SUCCESS);
469 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
470 efi_status_t exit_status, unsigned long exit_data_size,
473 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
475 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
476 exit_data_size, exit_data);
478 loaded_image_info->exit_status = exit_status;
479 longjmp(&loaded_image_info->exit_jmp, 1);
481 panic("EFI application exited");
484 static struct efi_object *efi_search_obj(void *handle)
486 struct list_head *lhandle;
488 list_for_each(lhandle, &efi_obj_list) {
489 struct efi_object *efiobj;
490 efiobj = list_entry(lhandle, struct efi_object, link);
491 if (efiobj->handle == handle)
498 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
500 struct efi_object *efiobj;
502 EFI_ENTRY("%p", image_handle);
503 efiobj = efi_search_obj(image_handle);
505 list_del(&efiobj->link);
507 return EFI_EXIT(EFI_SUCCESS);
510 static void efi_exit_caches(void)
512 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
514 * Grub on 32bit ARM needs to have caches disabled before jumping into
515 * a zImage, but does not know of all cache layers. Give it a hand.
517 if (efi_is_direct_boot)
518 cleanup_before_linux();
522 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
523 unsigned long map_key)
525 EFI_ENTRY("%p, %ld", image_handle, map_key);
527 /* Fix up caches for EFI payloads if necessary */
530 /* This stops all lingering devices */
531 bootm_disable_interrupts();
533 /* Give the payload some time to boot */
536 return EFI_EXIT(EFI_SUCCESS);
539 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
541 static uint64_t mono = 0;
542 EFI_ENTRY("%p", count);
544 return EFI_EXIT(EFI_SUCCESS);
547 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
549 EFI_ENTRY("%ld", microseconds);
550 udelay(microseconds);
551 return EFI_EXIT(EFI_SUCCESS);
554 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
555 uint64_t watchdog_code,
556 unsigned long data_size,
557 uint16_t *watchdog_data)
559 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
560 data_size, watchdog_data);
561 return EFI_EXIT(efi_unsupported(__func__));
564 static efi_status_t EFIAPI efi_connect_controller(
565 efi_handle_t controller_handle,
566 efi_handle_t *driver_image_handle,
567 struct efi_device_path *remain_device_path,
570 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
571 remain_device_path, recursive);
572 return EFI_EXIT(EFI_NOT_FOUND);
575 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
576 void *driver_image_handle,
579 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
581 return EFI_EXIT(EFI_INVALID_PARAMETER);
584 static efi_status_t EFIAPI efi_close_protocol(void *handle,
585 efi_guid_t *protocol,
587 void *controller_handle)
589 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
591 return EFI_EXIT(EFI_NOT_FOUND);
594 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
595 efi_guid_t *protocol,
596 struct efi_open_protocol_info_entry **entry_buffer,
597 unsigned long *entry_count)
599 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
601 return EFI_EXIT(EFI_NOT_FOUND);
604 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
605 efi_guid_t ***protocol_buffer,
606 unsigned long *protocol_buffer_count)
608 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
609 protocol_buffer_count);
610 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
613 static efi_status_t EFIAPI efi_locate_handle_buffer(
614 enum efi_locate_search_type search_type,
615 efi_guid_t *protocol, void *search_key,
616 unsigned long *no_handles, efi_handle_t **buffer)
618 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
620 return EFI_EXIT(EFI_NOT_FOUND);
623 static struct efi_class_map efi_class_maps[] = {
625 .guid = &efi_guid_console_control,
626 .interface = &efi_console_control
630 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
632 void **protocol_interface)
636 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
637 for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) {
638 struct efi_class_map *curmap = &efi_class_maps[i];
639 if (!guidcmp(protocol, curmap->guid)) {
640 *protocol_interface = (void*)curmap->interface;
641 return EFI_EXIT(EFI_SUCCESS);
645 return EFI_EXIT(EFI_NOT_FOUND);
648 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
651 EFI_ENTRY("%p", handle);
652 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
655 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
658 EFI_ENTRY("%p", handle);
659 return EFI_EXIT(EFI_INVALID_PARAMETER);
662 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
663 unsigned long data_size,
666 EFI_ENTRY("%p, %ld", data, data_size);
667 *crc32_p = crc32(0, data, data_size);
668 return EFI_EXIT(EFI_SUCCESS);
671 static void EFIAPI efi_copy_mem(void *destination, void *source,
672 unsigned long length)
674 EFI_ENTRY("%p, %p, %ld", destination, source, length);
675 memcpy(destination, source, length);
678 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
680 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
681 memset(buffer, value, size);
684 static efi_status_t EFIAPI efi_open_protocol(
685 void *handle, efi_guid_t *protocol,
686 void **protocol_interface, void *agent_handle,
687 void *controller_handle, uint32_t attributes)
689 struct list_head *lhandle;
691 efi_status_t r = EFI_UNSUPPORTED;
693 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
694 protocol_interface, agent_handle, controller_handle,
696 list_for_each(lhandle, &efi_obj_list) {
697 struct efi_object *efiobj;
698 efiobj = list_entry(lhandle, struct efi_object, link);
700 if (efiobj->handle != handle)
703 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
704 struct efi_handler *handler = &efiobj->protocols[i];
705 const efi_guid_t *hprotocol = handler->guid;
708 if (!guidcmp(hprotocol, protocol)) {
709 r = handler->open(handle, protocol,
710 protocol_interface, agent_handle,
711 controller_handle, attributes);
721 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
722 efi_guid_t *protocol,
723 void **protocol_interface)
725 return efi_open_protocol(handle, protocol, protocol_interface,
729 static const struct efi_boot_services efi_boot_services = {
731 .headersize = sizeof(struct efi_table_hdr),
733 .raise_tpl = efi_raise_tpl,
734 .restore_tpl = efi_restore_tpl,
735 .allocate_pages = efi_allocate_pages_ext,
736 .free_pages = efi_free_pages_ext,
737 .get_memory_map = efi_get_memory_map_ext,
738 .allocate_pool = efi_allocate_pool_ext,
739 .free_pool = efi_free_pool_ext,
740 .create_event = efi_create_event,
741 .set_timer = efi_set_timer,
742 .wait_for_event = efi_wait_for_event,
743 .signal_event = efi_signal_event,
744 .close_event = efi_close_event,
745 .check_event = efi_check_event,
746 .install_protocol_interface = efi_install_protocol_interface,
747 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
748 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
749 .handle_protocol = efi_handle_protocol,
751 .register_protocol_notify = efi_register_protocol_notify,
752 .locate_handle = efi_locate_handle,
753 .locate_device_path = efi_locate_device_path,
754 .install_configuration_table = efi_install_configuration_table,
755 .load_image = efi_load_image,
756 .start_image = efi_start_image,
758 .unload_image = efi_unload_image,
759 .exit_boot_services = efi_exit_boot_services,
760 .get_next_monotonic_count = efi_get_next_monotonic_count,
762 .set_watchdog_timer = efi_set_watchdog_timer,
763 .connect_controller = efi_connect_controller,
764 .disconnect_controller = efi_disconnect_controller,
765 .open_protocol = efi_open_protocol,
766 .close_protocol = efi_close_protocol,
767 .open_protocol_information = efi_open_protocol_information,
768 .protocols_per_handle = efi_protocols_per_handle,
769 .locate_handle_buffer = efi_locate_handle_buffer,
770 .locate_protocol = efi_locate_protocol,
771 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
772 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
773 .calculate_crc32 = efi_calculate_crc32,
774 .copy_mem = efi_copy_mem,
775 .set_mem = efi_set_mem,
779 static uint16_t EFI_RUNTIME_DATA firmware_vendor[] =
780 { 'D','a','s',' ','U','-','b','o','o','t',0 };
782 struct efi_system_table EFI_RUNTIME_DATA systab = {
784 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
785 .revision = 0x20005, /* 2.5 */
786 .headersize = sizeof(struct efi_table_hdr),
788 .fw_vendor = (long)firmware_vendor,
789 .con_in = (void*)&efi_con_in,
790 .con_out = (void*)&efi_con_out,
791 .std_err = (void*)&efi_con_out,
792 .runtime = (void*)&efi_runtime_services,
793 .boottime = (void*)&efi_boot_services,
795 .tables = (void*)efi_conf_table,