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)
165 if (event->type & EVT_NOTIFY_SIGNAL) {
166 EFI_CALL(event->notify_function(event, event->notify_context));
170 static efi_status_t efi_unsupported(const char *funcname)
172 debug("EFI: App called into unimplemented function %s\n", funcname);
173 return EFI_EXIT(EFI_UNSUPPORTED);
176 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
178 EFI_ENTRY("0x%zx", new_tpl);
182 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
184 EFI_ENTRY("0x%zx", old_tpl);
185 efi_unsupported(__func__);
188 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
194 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
195 r = efi_allocate_pages(type, memory_type, pages, memory);
199 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
204 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
205 r = efi_free_pages(memory, pages);
209 static efi_status_t EFIAPI efi_get_memory_map_ext(
210 unsigned long *memory_map_size,
211 struct efi_mem_desc *memory_map,
212 unsigned long *map_key,
213 unsigned long *descriptor_size,
214 uint32_t *descriptor_version)
218 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
219 map_key, descriptor_size, descriptor_version);
220 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
221 descriptor_size, descriptor_version);
225 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
231 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
232 r = efi_allocate_pool(pool_type, size, buffer);
236 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
240 EFI_ENTRY("%p", buffer);
241 r = efi_free_pool(buffer);
246 * Our event capabilities are very limited. Only a small limited
247 * number of events is allowed to coexist.
249 static struct efi_event efi_events[16];
251 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
252 void (EFIAPI *notify_function) (
253 struct efi_event *event,
255 void *notify_context, struct efi_event **event)
260 return EFI_INVALID_PARAMETER;
262 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
263 return EFI_INVALID_PARAMETER;
265 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
266 notify_function == NULL)
267 return EFI_INVALID_PARAMETER;
269 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
270 if (efi_events[i].type)
272 efi_events[i].type = type;
273 efi_events[i].notify_tpl = notify_tpl;
274 efi_events[i].notify_function = notify_function;
275 efi_events[i].notify_context = notify_context;
276 /* Disable timers on bootup */
277 efi_events[i].trigger_next = -1ULL;
278 efi_events[i].signaled = 0;
279 *event = &efi_events[i];
282 return EFI_OUT_OF_RESOURCES;
285 static efi_status_t EFIAPI efi_create_event_ext(
286 uint32_t type, UINTN notify_tpl,
287 void (EFIAPI *notify_function) (
288 struct efi_event *event,
290 void *notify_context, struct efi_event **event)
292 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
294 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
295 notify_context, event));
300 * Our timers have to work without interrupts, so we check whenever keyboard
301 * input or disk accesses happen if enough time elapsed for it to fire.
303 void efi_timer_check(void)
306 u64 now = timer_get_us();
308 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
309 if (!efi_events[i].type ||
310 !(efi_events[i].type & EVT_TIMER) ||
311 efi_events[i].trigger_type == EFI_TIMER_STOP ||
312 now < efi_events[i].trigger_next)
314 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
315 efi_events[i].trigger_next +=
316 efi_events[i].trigger_time;
317 efi_events[i].signaled = 0;
319 efi_signal_event(&efi_events[i]);
324 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
325 uint64_t trigger_time)
330 * The parameter defines a multiple of 100ns.
331 * We use multiples of 1000ns. So divide by 10.
333 trigger_time = efi_div10(trigger_time);
335 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
336 if (event != &efi_events[i])
339 if (!(event->type & EVT_TIMER))
343 event->trigger_next = -1ULL;
345 case EFI_TIMER_PERIODIC:
346 case EFI_TIMER_RELATIVE:
347 event->trigger_next =
348 timer_get_us() + trigger_time;
351 return EFI_INVALID_PARAMETER;
353 event->trigger_type = type;
354 event->trigger_time = trigger_time;
357 return EFI_INVALID_PARAMETER;
360 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
361 enum efi_timer_delay type,
362 uint64_t trigger_time)
364 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
365 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
368 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
369 struct efi_event **event,
370 unsigned long *index)
374 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
376 /* Check parameters */
377 if (!num_events || !event)
378 return EFI_EXIT(EFI_INVALID_PARAMETER);
379 for (i = 0; i < num_events; ++i) {
380 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
381 if (event[i] == &efi_events[j])
384 return EFI_EXIT(EFI_INVALID_PARAMETER);
386 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
387 return EFI_EXIT(EFI_INVALID_PARAMETER);
390 /* Wait for signal */
392 for (i = 0; i < num_events; ++i) {
393 if (event[i]->signaled)
396 /* Allow events to occur. */
402 * Reset the signal which is passed to the caller to allow periodic
405 event[i]->signaled = 0;
409 return EFI_EXIT(EFI_SUCCESS);
412 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
416 EFI_ENTRY("%p", event);
417 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
418 if (event != &efi_events[i])
420 efi_signal_event(event);
423 return EFI_EXIT(EFI_SUCCESS);
426 static efi_status_t EFIAPI efi_close_event(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]) {
434 event->trigger_next = -1ULL;
436 return EFI_EXIT(EFI_SUCCESS);
439 return EFI_EXIT(EFI_INVALID_PARAMETER);
442 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
446 EFI_ENTRY("%p", event);
448 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
449 if (event != &efi_events[i])
451 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
454 return EFI_EXIT(EFI_SUCCESS);
455 return EFI_EXIT(EFI_NOT_READY);
457 return EFI_EXIT(EFI_INVALID_PARAMETER);
460 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
461 efi_guid_t *protocol, int protocol_interface_type,
462 void *protocol_interface)
464 struct list_head *lhandle;
468 if (!handle || !protocol ||
469 protocol_interface_type != EFI_NATIVE_INTERFACE) {
470 r = EFI_INVALID_PARAMETER;
474 /* Create new handle if requested. */
476 r = EFI_OUT_OF_RESOURCES;
480 list_for_each(lhandle, &efi_obj_list) {
481 struct efi_object *efiobj;
482 efiobj = list_entry(lhandle, struct efi_object, link);
484 if (efiobj->handle != *handle)
486 /* Check if protocol is already installed on the handle. */
487 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
488 struct efi_handler *handler = &efiobj->protocols[i];
492 if (!guidcmp(handler->guid, protocol)) {
493 r = EFI_INVALID_PARAMETER;
497 /* Install protocol in first empty slot. */
498 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
499 struct efi_handler *handler = &efiobj->protocols[i];
504 handler->guid = protocol;
505 handler->protocol_interface = protocol_interface;
509 r = EFI_OUT_OF_RESOURCES;
512 r = EFI_INVALID_PARAMETER;
517 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
518 efi_guid_t *protocol, int protocol_interface_type,
519 void *protocol_interface)
521 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
524 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
525 protocol_interface_type,
526 protocol_interface));
529 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
530 efi_guid_t *protocol, void *old_interface,
533 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
535 return EFI_EXIT(EFI_ACCESS_DENIED);
538 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
539 efi_guid_t *protocol, void *protocol_interface)
541 struct list_head *lhandle;
543 efi_status_t r = EFI_NOT_FOUND;
545 if (!handle || !protocol) {
546 r = EFI_INVALID_PARAMETER;
550 list_for_each(lhandle, &efi_obj_list) {
551 struct efi_object *efiobj;
552 efiobj = list_entry(lhandle, struct efi_object, link);
554 if (efiobj->handle != handle)
557 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
558 struct efi_handler *handler = &efiobj->protocols[i];
559 const efi_guid_t *hprotocol = handler->guid;
563 if (!guidcmp(hprotocol, protocol)) {
564 if (handler->protocol_interface) {
565 r = EFI_ACCESS_DENIED;
579 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
580 efi_guid_t *protocol, void *protocol_interface)
582 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
584 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
585 protocol_interface));
588 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
589 struct efi_event *event,
592 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
593 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
596 static int efi_search(enum efi_locate_search_type search_type,
597 efi_guid_t *protocol, void *search_key,
598 struct efi_object *efiobj)
602 switch (search_type) {
605 case by_register_notify:
608 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
609 const efi_guid_t *guid = efiobj->protocols[i].guid;
610 if (guid && !guidcmp(guid, protocol))
619 static efi_status_t efi_locate_handle(
620 enum efi_locate_search_type search_type,
621 efi_guid_t *protocol, void *search_key,
622 unsigned long *buffer_size, efi_handle_t *buffer)
624 struct list_head *lhandle;
625 unsigned long size = 0;
627 /* Count how much space we need */
628 list_for_each(lhandle, &efi_obj_list) {
629 struct efi_object *efiobj;
630 efiobj = list_entry(lhandle, struct efi_object, link);
631 if (!efi_search(search_type, protocol, search_key, efiobj)) {
632 size += sizeof(void*);
636 if (*buffer_size < size) {
638 return EFI_BUFFER_TOO_SMALL;
643 return EFI_NOT_FOUND;
645 /* Then fill the array */
646 list_for_each(lhandle, &efi_obj_list) {
647 struct efi_object *efiobj;
648 efiobj = list_entry(lhandle, struct efi_object, link);
649 if (!efi_search(search_type, protocol, search_key, efiobj)) {
650 *(buffer++) = efiobj->handle;
657 static efi_status_t EFIAPI efi_locate_handle_ext(
658 enum efi_locate_search_type search_type,
659 efi_guid_t *protocol, void *search_key,
660 unsigned long *buffer_size, efi_handle_t *buffer)
662 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
663 buffer_size, buffer);
665 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
666 buffer_size, buffer));
669 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
670 struct efi_device_path **device_path,
671 efi_handle_t *device)
673 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
674 return EFI_EXIT(EFI_NOT_FOUND);
677 /* Collapses configuration table entries, removing index i */
678 static void efi_remove_configuration_table(int i)
680 struct efi_configuration_table *this = &efi_conf_table[i];
681 struct efi_configuration_table *next = &efi_conf_table[i+1];
682 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
684 memmove(this, next, (ulong)end - (ulong)next);
688 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
692 /* Check for guid override */
693 for (i = 0; i < systab.nr_tables; i++) {
694 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
696 efi_conf_table[i].table = table;
698 efi_remove_configuration_table(i);
704 return EFI_NOT_FOUND;
706 /* No override, check for overflow */
707 if (i >= ARRAY_SIZE(efi_conf_table))
708 return EFI_OUT_OF_RESOURCES;
710 /* Add a new entry */
711 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
712 efi_conf_table[i].table = table;
713 systab.nr_tables = i + 1;
718 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
721 EFI_ENTRY("%p, %p", guid, table);
722 return EFI_EXIT(efi_install_configuration_table(guid, table));
725 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
726 efi_handle_t parent_image,
727 struct efi_device_path *file_path,
729 unsigned long source_size,
730 efi_handle_t *image_handle)
732 static struct efi_object loaded_image_info_obj = {
735 .guid = &efi_guid_loaded_image,
739 struct efi_loaded_image *info;
740 struct efi_object *obj;
742 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
743 file_path, source_buffer, source_size, image_handle);
744 info = malloc(sizeof(*info));
745 loaded_image_info_obj.protocols[0].protocol_interface = info;
746 obj = malloc(sizeof(loaded_image_info_obj));
747 memset(info, 0, sizeof(*info));
748 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
750 info->file_path = file_path;
751 info->reserved = efi_load_pe(source_buffer, info);
752 if (!info->reserved) {
755 return EFI_EXIT(EFI_UNSUPPORTED);
758 *image_handle = info;
759 list_add_tail(&obj->link, &efi_obj_list);
761 return EFI_EXIT(EFI_SUCCESS);
764 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
765 unsigned long *exit_data_size,
768 ulong (*entry)(void *image_handle, struct efi_system_table *st);
769 struct efi_loaded_image *info = image_handle;
771 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
772 entry = info->reserved;
774 efi_is_direct_boot = false;
776 /* call the image! */
777 if (setjmp(&info->exit_jmp)) {
778 /* We returned from the child image */
779 return EFI_EXIT(info->exit_status);
784 entry(image_handle, &systab);
788 /* Should usually never get here */
789 return EFI_EXIT(EFI_SUCCESS);
792 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
793 efi_status_t exit_status, unsigned long exit_data_size,
796 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
798 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
799 exit_data_size, exit_data);
801 /* Make sure entry/exit counts for EFI world cross-overs match */
805 * But longjmp out with the U-Boot gd, not the application's, as
806 * the other end is a setjmp call inside EFI context.
810 loaded_image_info->exit_status = exit_status;
811 longjmp(&loaded_image_info->exit_jmp, 1);
813 panic("EFI application exited");
816 static struct efi_object *efi_search_obj(void *handle)
818 struct list_head *lhandle;
820 list_for_each(lhandle, &efi_obj_list) {
821 struct efi_object *efiobj;
822 efiobj = list_entry(lhandle, struct efi_object, link);
823 if (efiobj->handle == handle)
830 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
832 struct efi_object *efiobj;
834 EFI_ENTRY("%p", image_handle);
835 efiobj = efi_search_obj(image_handle);
837 list_del(&efiobj->link);
839 return EFI_EXIT(EFI_SUCCESS);
842 static void efi_exit_caches(void)
844 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
846 * Grub on 32bit ARM needs to have caches disabled before jumping into
847 * a zImage, but does not know of all cache layers. Give it a hand.
849 if (efi_is_direct_boot)
850 cleanup_before_linux();
854 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
855 unsigned long map_key)
857 EFI_ENTRY("%p, %ld", image_handle, map_key);
859 board_quiesce_devices();
861 /* Fix up caches for EFI payloads if necessary */
864 /* This stops all lingering devices */
865 bootm_disable_interrupts();
867 /* Give the payload some time to boot */
870 return EFI_EXIT(EFI_SUCCESS);
873 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
875 static uint64_t mono = 0;
876 EFI_ENTRY("%p", count);
878 return EFI_EXIT(EFI_SUCCESS);
881 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
883 EFI_ENTRY("%ld", microseconds);
884 udelay(microseconds);
885 return EFI_EXIT(EFI_SUCCESS);
888 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
889 uint64_t watchdog_code,
890 unsigned long data_size,
891 uint16_t *watchdog_data)
893 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
894 data_size, watchdog_data);
895 return efi_unsupported(__func__);
898 static efi_status_t EFIAPI efi_connect_controller(
899 efi_handle_t controller_handle,
900 efi_handle_t *driver_image_handle,
901 struct efi_device_path *remain_device_path,
904 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
905 remain_device_path, recursive);
906 return EFI_EXIT(EFI_NOT_FOUND);
909 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
910 void *driver_image_handle,
913 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
915 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 static efi_status_t EFIAPI efi_close_protocol(void *handle,
919 efi_guid_t *protocol,
921 void *controller_handle)
923 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
925 return EFI_EXIT(EFI_NOT_FOUND);
928 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
929 efi_guid_t *protocol,
930 struct efi_open_protocol_info_entry **entry_buffer,
931 unsigned long *entry_count)
933 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
935 return EFI_EXIT(EFI_NOT_FOUND);
938 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
939 efi_guid_t ***protocol_buffer,
940 unsigned long *protocol_buffer_count)
942 unsigned long buffer_size;
943 struct efi_object *efiobj;
945 struct list_head *lhandle;
948 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
949 protocol_buffer_count);
951 if (!handle || !protocol_buffer || !protocol_buffer_count)
952 return EFI_EXIT(EFI_INVALID_PARAMETER);
954 *protocol_buffer = NULL;
955 *protocol_buffer_count = 0;
956 list_for_each(lhandle, &efi_obj_list) {
957 efiobj = list_entry(lhandle, struct efi_object, link);
959 if (efiobj->handle != handle)
962 /* Count protocols */
963 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
964 if (efiobj->protocols[i].guid)
965 ++*protocol_buffer_count;
968 if (*protocol_buffer_count) {
969 buffer_size = sizeof(efi_guid_t *) *
970 *protocol_buffer_count;
971 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
973 (void **)protocol_buffer);
974 if (r != EFI_SUCCESS)
977 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
978 if (efiobj->protocols[i].guid) {
979 (*protocol_buffer)[j] = (void *)
980 efiobj->protocols[i].guid;
988 return EFI_EXIT(EFI_SUCCESS);
991 static efi_status_t EFIAPI efi_locate_handle_buffer(
992 enum efi_locate_search_type search_type,
993 efi_guid_t *protocol, void *search_key,
994 unsigned long *no_handles, efi_handle_t **buffer)
997 unsigned long buffer_size = 0;
999 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1000 no_handles, buffer);
1002 if (!no_handles || !buffer) {
1003 r = EFI_INVALID_PARAMETER;
1008 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1010 if (r != EFI_BUFFER_TOO_SMALL)
1012 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1014 if (r != EFI_SUCCESS)
1016 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1018 if (r == EFI_SUCCESS)
1019 *no_handles = buffer_size / sizeof(void *);
1024 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1026 void **protocol_interface)
1028 struct list_head *lhandle;
1031 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
1033 if (!protocol || !protocol_interface)
1034 return EFI_EXIT(EFI_INVALID_PARAMETER);
1036 EFI_PRINT_GUID("protocol", protocol);
1038 list_for_each(lhandle, &efi_obj_list) {
1039 struct efi_object *efiobj;
1041 efiobj = list_entry(lhandle, struct efi_object, link);
1042 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1043 struct efi_handler *handler = &efiobj->protocols[i];
1047 if (!guidcmp(handler->guid, protocol)) {
1048 *protocol_interface =
1049 handler->protocol_interface;
1050 return EFI_EXIT(EFI_SUCCESS);
1054 *protocol_interface = NULL;
1056 return EFI_EXIT(EFI_NOT_FOUND);
1059 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1062 EFI_ENTRY("%p", handle);
1065 efi_guid_t *protocol;
1066 void *protocol_interface;
1067 efi_status_t r = EFI_SUCCESS;
1071 return EFI_EXIT(EFI_INVALID_PARAMETER);
1073 va_start(argptr, handle);
1075 protocol = va_arg(argptr, efi_guid_t*);
1078 protocol_interface = va_arg(argptr, void*);
1079 r = efi_install_protocol_interface(handle, protocol,
1080 EFI_NATIVE_INTERFACE,
1081 protocol_interface);
1082 if (r != EFI_SUCCESS)
1087 if (r == EFI_SUCCESS)
1090 /* If an error occured undo all changes. */
1091 va_start(argptr, handle);
1093 protocol = va_arg(argptr, efi_guid_t*);
1094 protocol_interface = va_arg(argptr, void*);
1095 efi_uninstall_protocol_interface(handle, protocol,
1096 protocol_interface);
1103 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1106 EFI_ENTRY("%p", handle);
1107 return EFI_EXIT(EFI_INVALID_PARAMETER);
1110 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1111 unsigned long data_size,
1114 EFI_ENTRY("%p, %ld", data, data_size);
1115 *crc32_p = crc32(0, data, data_size);
1116 return EFI_EXIT(EFI_SUCCESS);
1119 static void EFIAPI efi_copy_mem(void *destination, void *source,
1120 unsigned long length)
1122 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1123 memcpy(destination, source, length);
1126 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1128 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1129 memset(buffer, value, size);
1132 static efi_status_t EFIAPI efi_open_protocol(
1133 void *handle, efi_guid_t *protocol,
1134 void **protocol_interface, void *agent_handle,
1135 void *controller_handle, uint32_t attributes)
1137 struct list_head *lhandle;
1139 efi_status_t r = EFI_INVALID_PARAMETER;
1141 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1142 protocol_interface, agent_handle, controller_handle,
1145 if (!handle || !protocol ||
1146 (!protocol_interface && attributes !=
1147 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1151 EFI_PRINT_GUID("protocol", protocol);
1153 switch (attributes) {
1154 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1155 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1156 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1158 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1159 if (controller_handle == handle)
1161 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1162 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1163 if (controller_handle == NULL)
1165 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1166 if (agent_handle == NULL)
1173 list_for_each(lhandle, &efi_obj_list) {
1174 struct efi_object *efiobj;
1175 efiobj = list_entry(lhandle, struct efi_object, link);
1177 if (efiobj->handle != handle)
1180 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1181 struct efi_handler *handler = &efiobj->protocols[i];
1182 const efi_guid_t *hprotocol = handler->guid;
1185 if (!guidcmp(hprotocol, protocol)) {
1187 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1188 *protocol_interface =
1189 handler->protocol_interface;
1199 r = EFI_UNSUPPORTED;
1204 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1205 efi_guid_t *protocol,
1206 void **protocol_interface)
1208 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1209 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1212 static const struct efi_boot_services efi_boot_services = {
1214 .headersize = sizeof(struct efi_table_hdr),
1216 .raise_tpl = efi_raise_tpl,
1217 .restore_tpl = efi_restore_tpl,
1218 .allocate_pages = efi_allocate_pages_ext,
1219 .free_pages = efi_free_pages_ext,
1220 .get_memory_map = efi_get_memory_map_ext,
1221 .allocate_pool = efi_allocate_pool_ext,
1222 .free_pool = efi_free_pool_ext,
1223 .create_event = efi_create_event_ext,
1224 .set_timer = efi_set_timer_ext,
1225 .wait_for_event = efi_wait_for_event,
1226 .signal_event = efi_signal_event_ext,
1227 .close_event = efi_close_event,
1228 .check_event = efi_check_event,
1229 .install_protocol_interface = efi_install_protocol_interface_ext,
1230 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1231 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1232 .handle_protocol = efi_handle_protocol,
1234 .register_protocol_notify = efi_register_protocol_notify,
1235 .locate_handle = efi_locate_handle_ext,
1236 .locate_device_path = efi_locate_device_path,
1237 .install_configuration_table = efi_install_configuration_table_ext,
1238 .load_image = efi_load_image,
1239 .start_image = efi_start_image,
1241 .unload_image = efi_unload_image,
1242 .exit_boot_services = efi_exit_boot_services,
1243 .get_next_monotonic_count = efi_get_next_monotonic_count,
1245 .set_watchdog_timer = efi_set_watchdog_timer,
1246 .connect_controller = efi_connect_controller,
1247 .disconnect_controller = efi_disconnect_controller,
1248 .open_protocol = efi_open_protocol,
1249 .close_protocol = efi_close_protocol,
1250 .open_protocol_information = efi_open_protocol_information,
1251 .protocols_per_handle = efi_protocols_per_handle,
1252 .locate_handle_buffer = efi_locate_handle_buffer,
1253 .locate_protocol = efi_locate_protocol,
1254 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1255 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1256 .calculate_crc32 = efi_calculate_crc32,
1257 .copy_mem = efi_copy_mem,
1258 .set_mem = efi_set_mem,
1262 static uint16_t __efi_runtime_data firmware_vendor[] =
1263 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1265 struct efi_system_table __efi_runtime_data systab = {
1267 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1268 .revision = 0x20005, /* 2.5 */
1269 .headersize = sizeof(struct efi_table_hdr),
1271 .fw_vendor = (long)firmware_vendor,
1272 .con_in = (void*)&efi_con_in,
1273 .con_out = (void*)&efi_con_out,
1274 .std_err = (void*)&efi_con_out,
1275 .runtime = (void*)&efi_runtime_services,
1276 .boottime = (void*)&efi_boot_services,
1278 .tables = (void*)efi_conf_table,