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[2];
44 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45 * fixed when compiling U-Boot. However, the payload does not know about that
46 * restriction so we need to manually swap its and our view of that register on
47 * EFI callback entry/exit.
49 static volatile void *efi_gd, *app_gd;
52 static int entry_count;
53 static int nesting_level;
55 /* Called on every callback entry */
56 int __efi_entry_check(void)
58 int ret = entry_count++ == 0;
67 /* Called on every callback exit */
68 int __efi_exit_check(void)
70 int ret = --entry_count == 0;
77 /* Called from do_bootefi_exec() */
78 void efi_save_gd(void)
86 * Special case handler for error/abort that just forces things back
87 * to u-boot world so we can dump out an abort msg, without any care
88 * about returning back to UEFI world.
90 void efi_restore_gd(void)
93 /* Only restore if we're already in EFI context */
101 * Two spaces per indent level, maxing out at 10.. which ought to be
102 * enough for anyone ;-)
104 static const char *indent_string(int level)
106 const char *indent = " ";
107 const int max = strlen(indent);
108 level = min(max, level * 2);
109 return &indent[max - level];
112 const char *__efi_nesting(void)
114 return indent_string(nesting_level);
117 const char *__efi_nesting_inc(void)
119 return indent_string(nesting_level++);
122 const char *__efi_nesting_dec(void)
124 return indent_string(--nesting_level);
128 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
130 #define EFI_HIGH32(a) (a >> 32)
133 * 64bit division by 10 implemented as multiplication by 1 / 10
135 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
137 #define EFI_TENTH 0x199999999999999A
138 static u64 efi_div10(u64 a)
144 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
145 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
146 rem = EFI_LOW32(prod);
147 ret += EFI_HIGH32(prod);
148 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
149 rem += EFI_LOW32(prod);
150 ret += EFI_HIGH32(prod);
151 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
152 rem += EFI_HIGH32(prod);
153 ret += EFI_HIGH32(rem);
154 /* Round to nearest integer */
155 if (rem >= (1 << 31))
160 void efi_signal_event(struct efi_event *event)
162 if (event->notify_function) {
164 /* Put missing TPL check here */
165 EFI_CALL_VOID(event->notify_function(event,
166 event->notify_context));
171 static efi_status_t efi_unsupported(const char *funcname)
173 debug("EFI: App called into unimplemented function %s\n", funcname);
174 return EFI_EXIT(EFI_UNSUPPORTED);
177 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
179 EFI_ENTRY("0x%zx", new_tpl);
183 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
185 EFI_ENTRY("0x%zx", old_tpl);
186 efi_unsupported(__func__);
189 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
195 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
196 r = efi_allocate_pages(type, memory_type, pages, memory);
200 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
205 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
206 r = efi_free_pages(memory, pages);
210 static efi_status_t EFIAPI efi_get_memory_map_ext(
211 unsigned long *memory_map_size,
212 struct efi_mem_desc *memory_map,
213 unsigned long *map_key,
214 unsigned long *descriptor_size,
215 uint32_t *descriptor_version)
219 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
220 map_key, descriptor_size, descriptor_version);
221 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
222 descriptor_size, descriptor_version);
226 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
232 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
233 r = efi_allocate_pool(pool_type, size, buffer);
237 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
241 EFI_ENTRY("%p", buffer);
242 r = efi_free_pool(buffer);
247 * Our event capabilities are very limited. Only a small limited
248 * number of events is allowed to coexist.
250 static struct efi_event efi_events[16];
252 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
253 void (EFIAPI *notify_function) (
254 struct efi_event *event,
256 void *notify_context, struct efi_event **event)
261 return EFI_INVALID_PARAMETER;
263 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
264 return EFI_INVALID_PARAMETER;
266 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
267 notify_function == NULL)
268 return EFI_INVALID_PARAMETER;
270 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
271 if (efi_events[i].type)
273 efi_events[i].type = type;
274 efi_events[i].notify_tpl = notify_tpl;
275 efi_events[i].notify_function = notify_function;
276 efi_events[i].notify_context = notify_context;
277 /* Disable timers on bootup */
278 efi_events[i].trigger_next = -1ULL;
279 efi_events[i].queued = 0;
280 efi_events[i].signaled = 0;
281 *event = &efi_events[i];
284 return EFI_OUT_OF_RESOURCES;
287 static efi_status_t EFIAPI efi_create_event_ext(
288 uint32_t type, UINTN notify_tpl,
289 void (EFIAPI *notify_function) (
290 struct efi_event *event,
292 void *notify_context, struct efi_event **event)
294 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
296 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
297 notify_context, event));
302 * Our timers have to work without interrupts, so we check whenever keyboard
303 * input or disk accesses happen if enough time elapsed for it to fire.
305 void efi_timer_check(void)
308 u64 now = timer_get_us();
310 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
311 if (!efi_events[i].type)
313 if (efi_events[i].queued)
314 efi_signal_event(&efi_events[i]);
315 if (!(efi_events[i].type & EVT_TIMER) ||
316 now < efi_events[i].trigger_next)
318 switch (efi_events[i].trigger_type) {
319 case EFI_TIMER_RELATIVE:
320 efi_events[i].trigger_type = EFI_TIMER_STOP;
322 case EFI_TIMER_PERIODIC:
323 efi_events[i].trigger_next +=
324 efi_events[i].trigger_time;
329 efi_events[i].signaled = 1;
330 efi_signal_event(&efi_events[i]);
335 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
336 uint64_t trigger_time)
341 * The parameter defines a multiple of 100ns.
342 * We use multiples of 1000ns. So divide by 10.
344 trigger_time = efi_div10(trigger_time);
346 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
347 if (event != &efi_events[i])
350 if (!(event->type & EVT_TIMER))
354 event->trigger_next = -1ULL;
356 case EFI_TIMER_PERIODIC:
357 case EFI_TIMER_RELATIVE:
358 event->trigger_next =
359 timer_get_us() + trigger_time;
362 return EFI_INVALID_PARAMETER;
364 event->trigger_type = type;
365 event->trigger_time = trigger_time;
368 return EFI_INVALID_PARAMETER;
371 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
372 enum efi_timer_delay type,
373 uint64_t trigger_time)
375 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
376 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
379 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
380 struct efi_event **event,
381 unsigned long *index)
385 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
387 /* Check parameters */
388 if (!num_events || !event)
389 return EFI_EXIT(EFI_INVALID_PARAMETER);
390 /* Put missing TPL check here */
391 for (i = 0; i < num_events; ++i) {
392 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
393 if (event[i] == &efi_events[j])
396 return EFI_EXIT(EFI_INVALID_PARAMETER);
398 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
399 return EFI_EXIT(EFI_INVALID_PARAMETER);
400 if (!event[i]->signaled)
401 efi_signal_event(event[i]);
404 /* Wait for signal */
406 for (i = 0; i < num_events; ++i) {
407 if (event[i]->signaled)
410 /* Allow events to occur. */
416 * Reset the signal which is passed to the caller to allow periodic
419 event[i]->signaled = 0;
423 return EFI_EXIT(EFI_SUCCESS);
426 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
430 EFI_ENTRY("%p", event);
431 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
432 if (event != &efi_events[i])
437 if (event->type & EVT_NOTIFY_SIGNAL)
438 efi_signal_event(event);
441 return EFI_EXIT(EFI_SUCCESS);
444 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
448 EFI_ENTRY("%p", event);
449 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
450 if (event == &efi_events[i]) {
452 event->trigger_next = -1ULL;
455 return EFI_EXIT(EFI_SUCCESS);
458 return EFI_EXIT(EFI_INVALID_PARAMETER);
461 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
465 EFI_ENTRY("%p", event);
467 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
468 if (event != &efi_events[i])
470 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
472 if (!event->signaled)
473 efi_signal_event(event);
475 return EFI_EXIT(EFI_SUCCESS);
476 return EFI_EXIT(EFI_NOT_READY);
478 return EFI_EXIT(EFI_INVALID_PARAMETER);
481 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
482 efi_guid_t *protocol, int protocol_interface_type,
483 void *protocol_interface)
485 struct list_head *lhandle;
489 if (!handle || !protocol ||
490 protocol_interface_type != EFI_NATIVE_INTERFACE) {
491 r = EFI_INVALID_PARAMETER;
495 /* Create new handle if requested. */
497 r = EFI_OUT_OF_RESOURCES;
501 list_for_each(lhandle, &efi_obj_list) {
502 struct efi_object *efiobj;
503 efiobj = list_entry(lhandle, struct efi_object, link);
505 if (efiobj->handle != *handle)
507 /* Check if protocol is already installed on the handle. */
508 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
509 struct efi_handler *handler = &efiobj->protocols[i];
513 if (!guidcmp(handler->guid, protocol)) {
514 r = EFI_INVALID_PARAMETER;
518 /* Install protocol in first empty slot. */
519 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
520 struct efi_handler *handler = &efiobj->protocols[i];
525 handler->guid = protocol;
526 handler->protocol_interface = protocol_interface;
530 r = EFI_OUT_OF_RESOURCES;
533 r = EFI_INVALID_PARAMETER;
538 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
539 efi_guid_t *protocol, int protocol_interface_type,
540 void *protocol_interface)
542 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
545 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
546 protocol_interface_type,
547 protocol_interface));
550 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
551 efi_guid_t *protocol, void *old_interface,
554 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
556 return EFI_EXIT(EFI_ACCESS_DENIED);
559 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
560 efi_guid_t *protocol, void *protocol_interface)
562 struct list_head *lhandle;
564 efi_status_t r = EFI_NOT_FOUND;
566 if (!handle || !protocol) {
567 r = EFI_INVALID_PARAMETER;
571 list_for_each(lhandle, &efi_obj_list) {
572 struct efi_object *efiobj;
573 efiobj = list_entry(lhandle, struct efi_object, link);
575 if (efiobj->handle != handle)
578 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
579 struct efi_handler *handler = &efiobj->protocols[i];
580 const efi_guid_t *hprotocol = handler->guid;
584 if (!guidcmp(hprotocol, protocol)) {
585 if (handler->protocol_interface) {
586 r = EFI_ACCESS_DENIED;
600 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
601 efi_guid_t *protocol, void *protocol_interface)
603 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
605 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
606 protocol_interface));
609 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
610 struct efi_event *event,
613 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
614 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
617 static int efi_search(enum efi_locate_search_type search_type,
618 efi_guid_t *protocol, void *search_key,
619 struct efi_object *efiobj)
623 switch (search_type) {
626 case by_register_notify:
629 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
630 const efi_guid_t *guid = efiobj->protocols[i].guid;
631 if (guid && !guidcmp(guid, protocol))
640 static efi_status_t efi_locate_handle(
641 enum efi_locate_search_type search_type,
642 efi_guid_t *protocol, void *search_key,
643 unsigned long *buffer_size, efi_handle_t *buffer)
645 struct list_head *lhandle;
646 unsigned long size = 0;
648 /* Count how much space we need */
649 list_for_each(lhandle, &efi_obj_list) {
650 struct efi_object *efiobj;
651 efiobj = list_entry(lhandle, struct efi_object, link);
652 if (!efi_search(search_type, protocol, search_key, efiobj)) {
653 size += sizeof(void*);
657 if (*buffer_size < size) {
659 return EFI_BUFFER_TOO_SMALL;
664 return EFI_NOT_FOUND;
666 /* Then fill the array */
667 list_for_each(lhandle, &efi_obj_list) {
668 struct efi_object *efiobj;
669 efiobj = list_entry(lhandle, struct efi_object, link);
670 if (!efi_search(search_type, protocol, search_key, efiobj)) {
671 *(buffer++) = efiobj->handle;
678 static efi_status_t EFIAPI efi_locate_handle_ext(
679 enum efi_locate_search_type search_type,
680 efi_guid_t *protocol, void *search_key,
681 unsigned long *buffer_size, efi_handle_t *buffer)
683 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
684 buffer_size, buffer);
686 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
687 buffer_size, buffer));
690 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
691 struct efi_device_path **device_path,
692 efi_handle_t *device)
694 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
695 return EFI_EXIT(EFI_NOT_FOUND);
698 /* Collapses configuration table entries, removing index i */
699 static void efi_remove_configuration_table(int i)
701 struct efi_configuration_table *this = &efi_conf_table[i];
702 struct efi_configuration_table *next = &efi_conf_table[i+1];
703 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
705 memmove(this, next, (ulong)end - (ulong)next);
709 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
713 /* Check for guid override */
714 for (i = 0; i < systab.nr_tables; i++) {
715 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
717 efi_conf_table[i].table = table;
719 efi_remove_configuration_table(i);
725 return EFI_NOT_FOUND;
727 /* No override, check for overflow */
728 if (i >= ARRAY_SIZE(efi_conf_table))
729 return EFI_OUT_OF_RESOURCES;
731 /* Add a new entry */
732 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
733 efi_conf_table[i].table = table;
734 systab.nr_tables = i + 1;
739 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
742 EFI_ENTRY("%p, %p", guid, table);
743 return EFI_EXIT(efi_install_configuration_table(guid, table));
746 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
747 efi_handle_t parent_image,
748 struct efi_device_path *file_path,
750 unsigned long source_size,
751 efi_handle_t *image_handle)
753 static struct efi_object loaded_image_info_obj = {
756 .guid = &efi_guid_loaded_image,
760 struct efi_loaded_image *info;
761 struct efi_object *obj;
763 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
764 file_path, source_buffer, source_size, image_handle);
765 info = malloc(sizeof(*info));
766 loaded_image_info_obj.protocols[0].protocol_interface = info;
767 obj = malloc(sizeof(loaded_image_info_obj));
768 memset(info, 0, sizeof(*info));
769 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
771 info->file_path = file_path;
772 info->reserved = efi_load_pe(source_buffer, info);
773 if (!info->reserved) {
776 return EFI_EXIT(EFI_UNSUPPORTED);
779 *image_handle = info;
780 list_add_tail(&obj->link, &efi_obj_list);
782 return EFI_EXIT(EFI_SUCCESS);
785 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
786 unsigned long *exit_data_size,
789 ulong (*entry)(void *image_handle, struct efi_system_table *st);
790 struct efi_loaded_image *info = image_handle;
792 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
793 entry = info->reserved;
795 efi_is_direct_boot = false;
797 /* call the image! */
798 if (setjmp(&info->exit_jmp)) {
799 /* We returned from the child image */
800 return EFI_EXIT(info->exit_status);
805 entry(image_handle, &systab);
809 /* Should usually never get here */
810 return EFI_EXIT(EFI_SUCCESS);
813 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
814 efi_status_t exit_status, unsigned long exit_data_size,
817 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
819 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
820 exit_data_size, exit_data);
822 /* Make sure entry/exit counts for EFI world cross-overs match */
826 * But longjmp out with the U-Boot gd, not the application's, as
827 * the other end is a setjmp call inside EFI context.
831 loaded_image_info->exit_status = exit_status;
832 longjmp(&loaded_image_info->exit_jmp, 1);
834 panic("EFI application exited");
837 static struct efi_object *efi_search_obj(void *handle)
839 struct list_head *lhandle;
841 list_for_each(lhandle, &efi_obj_list) {
842 struct efi_object *efiobj;
843 efiobj = list_entry(lhandle, struct efi_object, link);
844 if (efiobj->handle == handle)
851 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
853 struct efi_object *efiobj;
855 EFI_ENTRY("%p", image_handle);
856 efiobj = efi_search_obj(image_handle);
858 list_del(&efiobj->link);
860 return EFI_EXIT(EFI_SUCCESS);
863 static void efi_exit_caches(void)
865 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
867 * Grub on 32bit ARM needs to have caches disabled before jumping into
868 * a zImage, but does not know of all cache layers. Give it a hand.
870 if (efi_is_direct_boot)
871 cleanup_before_linux();
875 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
876 unsigned long map_key)
878 EFI_ENTRY("%p, %ld", image_handle, map_key);
880 board_quiesce_devices();
882 /* Fix up caches for EFI payloads if necessary */
885 /* This stops all lingering devices */
886 bootm_disable_interrupts();
888 /* Give the payload some time to boot */
891 return EFI_EXIT(EFI_SUCCESS);
894 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
896 static uint64_t mono = 0;
897 EFI_ENTRY("%p", count);
899 return EFI_EXIT(EFI_SUCCESS);
902 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
904 EFI_ENTRY("%ld", microseconds);
905 udelay(microseconds);
906 return EFI_EXIT(EFI_SUCCESS);
909 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
910 uint64_t watchdog_code,
911 unsigned long data_size,
912 uint16_t *watchdog_data)
914 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
915 data_size, watchdog_data);
916 return efi_unsupported(__func__);
919 static efi_status_t EFIAPI efi_connect_controller(
920 efi_handle_t controller_handle,
921 efi_handle_t *driver_image_handle,
922 struct efi_device_path *remain_device_path,
925 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
926 remain_device_path, recursive);
927 return EFI_EXIT(EFI_NOT_FOUND);
930 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
931 void *driver_image_handle,
934 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
936 return EFI_EXIT(EFI_INVALID_PARAMETER);
939 static efi_status_t EFIAPI efi_close_protocol(void *handle,
940 efi_guid_t *protocol,
942 void *controller_handle)
944 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
946 return EFI_EXIT(EFI_NOT_FOUND);
949 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
950 efi_guid_t *protocol,
951 struct efi_open_protocol_info_entry **entry_buffer,
952 unsigned long *entry_count)
954 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
956 return EFI_EXIT(EFI_NOT_FOUND);
959 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
960 efi_guid_t ***protocol_buffer,
961 unsigned long *protocol_buffer_count)
963 unsigned long buffer_size;
964 struct efi_object *efiobj;
966 struct list_head *lhandle;
969 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
970 protocol_buffer_count);
972 if (!handle || !protocol_buffer || !protocol_buffer_count)
973 return EFI_EXIT(EFI_INVALID_PARAMETER);
975 *protocol_buffer = NULL;
976 *protocol_buffer_count = 0;
977 list_for_each(lhandle, &efi_obj_list) {
978 efiobj = list_entry(lhandle, struct efi_object, link);
980 if (efiobj->handle != handle)
983 /* Count protocols */
984 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
985 if (efiobj->protocols[i].guid)
986 ++*protocol_buffer_count;
989 if (*protocol_buffer_count) {
990 buffer_size = sizeof(efi_guid_t *) *
991 *protocol_buffer_count;
992 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
994 (void **)protocol_buffer);
995 if (r != EFI_SUCCESS)
998 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
999 if (efiobj->protocols[i].guid) {
1000 (*protocol_buffer)[j] = (void *)
1001 efiobj->protocols[i].guid;
1009 return EFI_EXIT(EFI_SUCCESS);
1012 static efi_status_t EFIAPI efi_locate_handle_buffer(
1013 enum efi_locate_search_type search_type,
1014 efi_guid_t *protocol, void *search_key,
1015 unsigned long *no_handles, efi_handle_t **buffer)
1018 unsigned long buffer_size = 0;
1020 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1021 no_handles, buffer);
1023 if (!no_handles || !buffer) {
1024 r = EFI_INVALID_PARAMETER;
1029 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1031 if (r != EFI_BUFFER_TOO_SMALL)
1033 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1035 if (r != EFI_SUCCESS)
1037 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1039 if (r == EFI_SUCCESS)
1040 *no_handles = buffer_size / sizeof(void *);
1045 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1047 void **protocol_interface)
1049 struct list_head *lhandle;
1052 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
1054 if (!protocol || !protocol_interface)
1055 return EFI_EXIT(EFI_INVALID_PARAMETER);
1057 EFI_PRINT_GUID("protocol", protocol);
1059 list_for_each(lhandle, &efi_obj_list) {
1060 struct efi_object *efiobj;
1062 efiobj = list_entry(lhandle, struct efi_object, link);
1063 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1064 struct efi_handler *handler = &efiobj->protocols[i];
1068 if (!guidcmp(handler->guid, protocol)) {
1069 *protocol_interface =
1070 handler->protocol_interface;
1071 return EFI_EXIT(EFI_SUCCESS);
1075 *protocol_interface = NULL;
1077 return EFI_EXIT(EFI_NOT_FOUND);
1080 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1083 EFI_ENTRY("%p", handle);
1086 efi_guid_t *protocol;
1087 void *protocol_interface;
1088 efi_status_t r = EFI_SUCCESS;
1092 return EFI_EXIT(EFI_INVALID_PARAMETER);
1094 va_start(argptr, handle);
1096 protocol = va_arg(argptr, efi_guid_t*);
1099 protocol_interface = va_arg(argptr, void*);
1100 r = efi_install_protocol_interface(handle, protocol,
1101 EFI_NATIVE_INTERFACE,
1102 protocol_interface);
1103 if (r != EFI_SUCCESS)
1108 if (r == EFI_SUCCESS)
1111 /* If an error occured undo all changes. */
1112 va_start(argptr, handle);
1114 protocol = va_arg(argptr, efi_guid_t*);
1115 protocol_interface = va_arg(argptr, void*);
1116 efi_uninstall_protocol_interface(handle, protocol,
1117 protocol_interface);
1124 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1127 EFI_ENTRY("%p", handle);
1128 return EFI_EXIT(EFI_INVALID_PARAMETER);
1131 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1132 unsigned long data_size,
1135 EFI_ENTRY("%p, %ld", data, data_size);
1136 *crc32_p = crc32(0, data, data_size);
1137 return EFI_EXIT(EFI_SUCCESS);
1140 static void EFIAPI efi_copy_mem(void *destination, void *source,
1141 unsigned long length)
1143 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1144 memcpy(destination, source, length);
1147 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1149 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1150 memset(buffer, value, size);
1153 static efi_status_t EFIAPI efi_open_protocol(
1154 void *handle, efi_guid_t *protocol,
1155 void **protocol_interface, void *agent_handle,
1156 void *controller_handle, uint32_t attributes)
1158 struct list_head *lhandle;
1160 efi_status_t r = EFI_INVALID_PARAMETER;
1162 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1163 protocol_interface, agent_handle, controller_handle,
1166 if (!handle || !protocol ||
1167 (!protocol_interface && attributes !=
1168 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1172 EFI_PRINT_GUID("protocol", protocol);
1174 switch (attributes) {
1175 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1176 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1177 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1179 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1180 if (controller_handle == handle)
1182 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1183 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1184 if (controller_handle == NULL)
1186 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1187 if (agent_handle == NULL)
1194 list_for_each(lhandle, &efi_obj_list) {
1195 struct efi_object *efiobj;
1196 efiobj = list_entry(lhandle, struct efi_object, link);
1198 if (efiobj->handle != handle)
1201 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1202 struct efi_handler *handler = &efiobj->protocols[i];
1203 const efi_guid_t *hprotocol = handler->guid;
1206 if (!guidcmp(hprotocol, protocol)) {
1208 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1209 *protocol_interface =
1210 handler->protocol_interface;
1220 r = EFI_UNSUPPORTED;
1225 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1226 efi_guid_t *protocol,
1227 void **protocol_interface)
1229 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1230 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1233 static const struct efi_boot_services efi_boot_services = {
1235 .headersize = sizeof(struct efi_table_hdr),
1237 .raise_tpl = efi_raise_tpl,
1238 .restore_tpl = efi_restore_tpl,
1239 .allocate_pages = efi_allocate_pages_ext,
1240 .free_pages = efi_free_pages_ext,
1241 .get_memory_map = efi_get_memory_map_ext,
1242 .allocate_pool = efi_allocate_pool_ext,
1243 .free_pool = efi_free_pool_ext,
1244 .create_event = efi_create_event_ext,
1245 .set_timer = efi_set_timer_ext,
1246 .wait_for_event = efi_wait_for_event,
1247 .signal_event = efi_signal_event_ext,
1248 .close_event = efi_close_event,
1249 .check_event = efi_check_event,
1250 .install_protocol_interface = efi_install_protocol_interface_ext,
1251 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1252 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1253 .handle_protocol = efi_handle_protocol,
1255 .register_protocol_notify = efi_register_protocol_notify,
1256 .locate_handle = efi_locate_handle_ext,
1257 .locate_device_path = efi_locate_device_path,
1258 .install_configuration_table = efi_install_configuration_table_ext,
1259 .load_image = efi_load_image,
1260 .start_image = efi_start_image,
1262 .unload_image = efi_unload_image,
1263 .exit_boot_services = efi_exit_boot_services,
1264 .get_next_monotonic_count = efi_get_next_monotonic_count,
1266 .set_watchdog_timer = efi_set_watchdog_timer,
1267 .connect_controller = efi_connect_controller,
1268 .disconnect_controller = efi_disconnect_controller,
1269 .open_protocol = efi_open_protocol,
1270 .close_protocol = efi_close_protocol,
1271 .open_protocol_information = efi_open_protocol_information,
1272 .protocols_per_handle = efi_protocols_per_handle,
1273 .locate_handle_buffer = efi_locate_handle_buffer,
1274 .locate_protocol = efi_locate_protocol,
1275 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1276 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1277 .calculate_crc32 = efi_calculate_crc32,
1278 .copy_mem = efi_copy_mem,
1279 .set_mem = efi_set_mem,
1283 static uint16_t __efi_runtime_data firmware_vendor[] =
1284 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1286 struct efi_system_table __efi_runtime_data systab = {
1288 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1289 .revision = 0x20005, /* 2.5 */
1290 .headersize = sizeof(struct efi_table_hdr),
1292 .fw_vendor = (long)firmware_vendor,
1293 .con_in = (void*)&efi_con_in,
1294 .con_out = (void*)&efi_con_out,
1295 .std_err = (void*)&efi_con_out,
1296 .runtime = (void*)&efi_runtime_services,
1297 .boottime = (void*)&efi_boot_services,
1299 .tables = (void*)efi_conf_table,