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_inc(void)
114 return indent_string(nesting_level++);
117 const char *__efi_nesting_dec(void)
119 return indent_string(--nesting_level);
123 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
125 #define EFI_HIGH32(a) (a >> 32)
128 * 64bit division by 10 implemented as multiplication by 1 / 10
130 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
132 #define EFI_TENTH 0x199999999999999A
133 static u64 efi_div10(u64 a)
139 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
140 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
141 rem = EFI_LOW32(prod);
142 ret += EFI_HIGH32(prod);
143 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
144 rem += EFI_LOW32(prod);
145 ret += EFI_HIGH32(prod);
146 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
147 rem += EFI_HIGH32(prod);
148 ret += EFI_HIGH32(rem);
149 /* Round to nearest integer */
150 if (rem >= (1 << 31))
155 void efi_signal_event(struct efi_event *event)
160 if (event->type & EVT_NOTIFY_SIGNAL) {
161 EFI_CALL(event->notify_function(event, event->notify_context));
165 static efi_status_t efi_unsupported(const char *funcname)
167 debug("EFI: App called into unimplemented function %s\n", funcname);
168 return EFI_EXIT(EFI_UNSUPPORTED);
171 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
173 EFI_ENTRY("0x%zx", new_tpl);
177 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
179 EFI_ENTRY("0x%zx", old_tpl);
180 efi_unsupported(__func__);
183 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
189 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
190 r = efi_allocate_pages(type, memory_type, pages, memory);
194 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
199 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
200 r = efi_free_pages(memory, pages);
204 static efi_status_t EFIAPI efi_get_memory_map_ext(
205 unsigned long *memory_map_size,
206 struct efi_mem_desc *memory_map,
207 unsigned long *map_key,
208 unsigned long *descriptor_size,
209 uint32_t *descriptor_version)
213 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
214 map_key, descriptor_size, descriptor_version);
215 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
216 descriptor_size, descriptor_version);
220 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
226 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
227 r = efi_allocate_pool(pool_type, size, buffer);
231 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
235 EFI_ENTRY("%p", buffer);
236 r = efi_free_pool(buffer);
241 * Our event capabilities are very limited. Only a small limited
242 * number of events is allowed to coexist.
244 static struct efi_event efi_events[16];
246 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
247 void (EFIAPI *notify_function) (
248 struct efi_event *event,
250 void *notify_context, struct efi_event **event)
255 return EFI_INVALID_PARAMETER;
257 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
258 return EFI_INVALID_PARAMETER;
260 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
261 notify_function == NULL)
262 return EFI_INVALID_PARAMETER;
264 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
265 if (efi_events[i].type)
267 efi_events[i].type = type;
268 efi_events[i].notify_tpl = notify_tpl;
269 efi_events[i].notify_function = notify_function;
270 efi_events[i].notify_context = notify_context;
271 /* Disable timers on bootup */
272 efi_events[i].trigger_next = -1ULL;
273 efi_events[i].signaled = 0;
274 *event = &efi_events[i];
277 return EFI_OUT_OF_RESOURCES;
280 static efi_status_t EFIAPI efi_create_event_ext(
281 uint32_t type, UINTN notify_tpl,
282 void (EFIAPI *notify_function) (
283 struct efi_event *event,
285 void *notify_context, struct efi_event **event)
287 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
289 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
290 notify_context, event));
295 * Our timers have to work without interrupts, so we check whenever keyboard
296 * input or disk accesses happen if enough time elapsed for it to fire.
298 void efi_timer_check(void)
301 u64 now = timer_get_us();
303 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
304 if (!efi_events[i].type ||
305 !(efi_events[i].type & EVT_TIMER) ||
306 efi_events[i].trigger_type == EFI_TIMER_STOP ||
307 now < efi_events[i].trigger_next)
309 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
310 efi_events[i].trigger_next +=
311 efi_events[i].trigger_time;
312 efi_events[i].signaled = 0;
314 efi_signal_event(&efi_events[i]);
319 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
320 uint64_t trigger_time)
325 * The parameter defines a multiple of 100ns.
326 * We use multiples of 1000ns. So divide by 10.
328 trigger_time = efi_div10(trigger_time);
330 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
331 if (event != &efi_events[i])
334 if (!(event->type & EVT_TIMER))
338 event->trigger_next = -1ULL;
340 case EFI_TIMER_PERIODIC:
341 case EFI_TIMER_RELATIVE:
342 event->trigger_next =
343 timer_get_us() + trigger_time;
346 return EFI_INVALID_PARAMETER;
348 event->trigger_type = type;
349 event->trigger_time = trigger_time;
352 return EFI_INVALID_PARAMETER;
355 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
356 enum efi_timer_delay type,
357 uint64_t trigger_time)
359 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
360 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
363 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
364 struct efi_event **event,
365 unsigned long *index)
369 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
371 /* Check parameters */
372 if (!num_events || !event)
373 return EFI_EXIT(EFI_INVALID_PARAMETER);
374 for (i = 0; i < num_events; ++i) {
375 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
376 if (event[i] == &efi_events[j])
379 return EFI_EXIT(EFI_INVALID_PARAMETER);
381 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
382 return EFI_EXIT(EFI_INVALID_PARAMETER);
385 /* Wait for signal */
387 for (i = 0; i < num_events; ++i) {
388 if (event[i]->signaled)
391 /* Allow events to occur. */
397 * Reset the signal which is passed to the caller to allow periodic
400 event[i]->signaled = 0;
404 return EFI_EXIT(EFI_SUCCESS);
407 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
411 EFI_ENTRY("%p", event);
412 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
413 if (event != &efi_events[i])
415 efi_signal_event(event);
418 return EFI_EXIT(EFI_SUCCESS);
421 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
425 EFI_ENTRY("%p", event);
426 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
427 if (event == &efi_events[i]) {
429 event->trigger_next = -1ULL;
431 return EFI_EXIT(EFI_SUCCESS);
434 return EFI_EXIT(EFI_INVALID_PARAMETER);
437 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
441 EFI_ENTRY("%p", event);
443 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
444 if (event != &efi_events[i])
446 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
449 return EFI_EXIT(EFI_SUCCESS);
450 return EFI_EXIT(EFI_NOT_READY);
452 return EFI_EXIT(EFI_INVALID_PARAMETER);
455 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
456 efi_guid_t *protocol, int protocol_interface_type,
457 void *protocol_interface)
459 struct list_head *lhandle;
463 if (!handle || !protocol ||
464 protocol_interface_type != EFI_NATIVE_INTERFACE) {
465 r = EFI_INVALID_PARAMETER;
469 /* Create new handle if requested. */
471 r = EFI_OUT_OF_RESOURCES;
475 list_for_each(lhandle, &efi_obj_list) {
476 struct efi_object *efiobj;
477 efiobj = list_entry(lhandle, struct efi_object, link);
479 if (efiobj->handle != *handle)
481 /* Check if protocol is already installed on the handle. */
482 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
483 struct efi_handler *handler = &efiobj->protocols[i];
487 if (!guidcmp(handler->guid, protocol)) {
488 r = EFI_INVALID_PARAMETER;
492 /* Install protocol in first empty slot. */
493 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
494 struct efi_handler *handler = &efiobj->protocols[i];
499 handler->guid = protocol;
500 handler->protocol_interface = protocol_interface;
504 r = EFI_OUT_OF_RESOURCES;
507 r = EFI_INVALID_PARAMETER;
512 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
513 efi_guid_t *protocol, int protocol_interface_type,
514 void *protocol_interface)
516 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
519 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
520 protocol_interface_type,
521 protocol_interface));
524 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
525 efi_guid_t *protocol, void *old_interface,
528 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
530 return EFI_EXIT(EFI_ACCESS_DENIED);
533 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
534 efi_guid_t *protocol, void *protocol_interface)
536 struct list_head *lhandle;
538 efi_status_t r = EFI_NOT_FOUND;
540 if (!handle || !protocol) {
541 r = EFI_INVALID_PARAMETER;
545 list_for_each(lhandle, &efi_obj_list) {
546 struct efi_object *efiobj;
547 efiobj = list_entry(lhandle, struct efi_object, link);
549 if (efiobj->handle != handle)
552 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
553 struct efi_handler *handler = &efiobj->protocols[i];
554 const efi_guid_t *hprotocol = handler->guid;
558 if (!guidcmp(hprotocol, protocol)) {
559 if (handler->protocol_interface) {
560 r = EFI_ACCESS_DENIED;
574 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
575 efi_guid_t *protocol, void *protocol_interface)
577 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
579 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
580 protocol_interface));
583 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
584 struct efi_event *event,
587 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
588 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
591 static int efi_search(enum efi_locate_search_type search_type,
592 efi_guid_t *protocol, void *search_key,
593 struct efi_object *efiobj)
597 switch (search_type) {
600 case by_register_notify:
603 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
604 const efi_guid_t *guid = efiobj->protocols[i].guid;
605 if (guid && !guidcmp(guid, protocol))
614 static efi_status_t efi_locate_handle(
615 enum efi_locate_search_type search_type,
616 efi_guid_t *protocol, void *search_key,
617 unsigned long *buffer_size, efi_handle_t *buffer)
619 struct list_head *lhandle;
620 unsigned long size = 0;
622 /* Count how much space we need */
623 list_for_each(lhandle, &efi_obj_list) {
624 struct efi_object *efiobj;
625 efiobj = list_entry(lhandle, struct efi_object, link);
626 if (!efi_search(search_type, protocol, search_key, efiobj)) {
627 size += sizeof(void*);
631 if (*buffer_size < size) {
633 return EFI_BUFFER_TOO_SMALL;
638 return EFI_NOT_FOUND;
640 /* Then fill the array */
641 list_for_each(lhandle, &efi_obj_list) {
642 struct efi_object *efiobj;
643 efiobj = list_entry(lhandle, struct efi_object, link);
644 if (!efi_search(search_type, protocol, search_key, efiobj)) {
645 *(buffer++) = efiobj->handle;
652 static efi_status_t EFIAPI efi_locate_handle_ext(
653 enum efi_locate_search_type search_type,
654 efi_guid_t *protocol, void *search_key,
655 unsigned long *buffer_size, efi_handle_t *buffer)
657 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
658 buffer_size, buffer);
660 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
661 buffer_size, buffer));
664 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
665 struct efi_device_path **device_path,
666 efi_handle_t *device)
668 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
669 return EFI_EXIT(EFI_NOT_FOUND);
672 /* Collapses configuration table entries, removing index i */
673 static void efi_remove_configuration_table(int i)
675 struct efi_configuration_table *this = &efi_conf_table[i];
676 struct efi_configuration_table *next = &efi_conf_table[i+1];
677 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
679 memmove(this, next, (ulong)end - (ulong)next);
683 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
687 /* Check for guid override */
688 for (i = 0; i < systab.nr_tables; i++) {
689 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
691 efi_conf_table[i].table = table;
693 efi_remove_configuration_table(i);
699 return EFI_NOT_FOUND;
701 /* No override, check for overflow */
702 if (i >= ARRAY_SIZE(efi_conf_table))
703 return EFI_OUT_OF_RESOURCES;
705 /* Add a new entry */
706 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
707 efi_conf_table[i].table = table;
708 systab.nr_tables = i + 1;
713 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
716 EFI_ENTRY("%p, %p", guid, table);
717 return EFI_EXIT(efi_install_configuration_table(guid, table));
720 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
721 efi_handle_t parent_image,
722 struct efi_device_path *file_path,
724 unsigned long source_size,
725 efi_handle_t *image_handle)
727 static struct efi_object loaded_image_info_obj = {
730 .guid = &efi_guid_loaded_image,
734 struct efi_loaded_image *info;
735 struct efi_object *obj;
737 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
738 file_path, source_buffer, source_size, image_handle);
739 info = malloc(sizeof(*info));
740 loaded_image_info_obj.protocols[0].protocol_interface = info;
741 obj = malloc(sizeof(loaded_image_info_obj));
742 memset(info, 0, sizeof(*info));
743 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
745 info->file_path = file_path;
746 info->reserved = efi_load_pe(source_buffer, info);
747 if (!info->reserved) {
750 return EFI_EXIT(EFI_UNSUPPORTED);
753 *image_handle = info;
754 list_add_tail(&obj->link, &efi_obj_list);
756 return EFI_EXIT(EFI_SUCCESS);
759 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
760 unsigned long *exit_data_size,
763 ulong (*entry)(void *image_handle, struct efi_system_table *st);
764 struct efi_loaded_image *info = image_handle;
766 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
767 entry = info->reserved;
769 efi_is_direct_boot = false;
771 /* call the image! */
772 if (setjmp(&info->exit_jmp)) {
773 /* We returned from the child image */
774 return EFI_EXIT(info->exit_status);
779 entry(image_handle, &systab);
783 /* Should usually never get here */
784 return EFI_EXIT(EFI_SUCCESS);
787 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
788 efi_status_t exit_status, unsigned long exit_data_size,
791 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
793 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
794 exit_data_size, exit_data);
796 loaded_image_info->exit_status = exit_status;
797 longjmp(&loaded_image_info->exit_jmp, 1);
799 panic("EFI application exited");
802 static struct efi_object *efi_search_obj(void *handle)
804 struct list_head *lhandle;
806 list_for_each(lhandle, &efi_obj_list) {
807 struct efi_object *efiobj;
808 efiobj = list_entry(lhandle, struct efi_object, link);
809 if (efiobj->handle == handle)
816 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
818 struct efi_object *efiobj;
820 EFI_ENTRY("%p", image_handle);
821 efiobj = efi_search_obj(image_handle);
823 list_del(&efiobj->link);
825 return EFI_EXIT(EFI_SUCCESS);
828 static void efi_exit_caches(void)
830 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
832 * Grub on 32bit ARM needs to have caches disabled before jumping into
833 * a zImage, but does not know of all cache layers. Give it a hand.
835 if (efi_is_direct_boot)
836 cleanup_before_linux();
840 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
841 unsigned long map_key)
843 EFI_ENTRY("%p, %ld", image_handle, map_key);
845 board_quiesce_devices();
847 /* Fix up caches for EFI payloads if necessary */
850 /* This stops all lingering devices */
851 bootm_disable_interrupts();
853 /* Give the payload some time to boot */
856 return EFI_EXIT(EFI_SUCCESS);
859 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
861 static uint64_t mono = 0;
862 EFI_ENTRY("%p", count);
864 return EFI_EXIT(EFI_SUCCESS);
867 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
869 EFI_ENTRY("%ld", microseconds);
870 udelay(microseconds);
871 return EFI_EXIT(EFI_SUCCESS);
874 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
875 uint64_t watchdog_code,
876 unsigned long data_size,
877 uint16_t *watchdog_data)
879 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
880 data_size, watchdog_data);
881 return efi_unsupported(__func__);
884 static efi_status_t EFIAPI efi_connect_controller(
885 efi_handle_t controller_handle,
886 efi_handle_t *driver_image_handle,
887 struct efi_device_path *remain_device_path,
890 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
891 remain_device_path, recursive);
892 return EFI_EXIT(EFI_NOT_FOUND);
895 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
896 void *driver_image_handle,
899 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
904 static efi_status_t EFIAPI efi_close_protocol(void *handle,
905 efi_guid_t *protocol,
907 void *controller_handle)
909 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
911 return EFI_EXIT(EFI_NOT_FOUND);
914 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
915 efi_guid_t *protocol,
916 struct efi_open_protocol_info_entry **entry_buffer,
917 unsigned long *entry_count)
919 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
921 return EFI_EXIT(EFI_NOT_FOUND);
924 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
925 efi_guid_t ***protocol_buffer,
926 unsigned long *protocol_buffer_count)
928 unsigned long buffer_size;
929 struct efi_object *efiobj;
931 struct list_head *lhandle;
934 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
935 protocol_buffer_count);
937 if (!handle || !protocol_buffer || !protocol_buffer_count)
938 return EFI_EXIT(EFI_INVALID_PARAMETER);
940 *protocol_buffer = NULL;
941 *protocol_buffer_count = 0;
942 list_for_each(lhandle, &efi_obj_list) {
943 efiobj = list_entry(lhandle, struct efi_object, link);
945 if (efiobj->handle != handle)
948 /* Count protocols */
949 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
950 if (efiobj->protocols[i].guid)
951 ++*protocol_buffer_count;
954 if (*protocol_buffer_count) {
955 buffer_size = sizeof(efi_guid_t *) *
956 *protocol_buffer_count;
957 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
959 (void **)protocol_buffer);
960 if (r != EFI_SUCCESS)
963 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
964 if (efiobj->protocols[i].guid) {
965 (*protocol_buffer)[j] = (void *)
966 efiobj->protocols[i].guid;
974 return EFI_EXIT(EFI_SUCCESS);
977 static efi_status_t EFIAPI efi_locate_handle_buffer(
978 enum efi_locate_search_type search_type,
979 efi_guid_t *protocol, void *search_key,
980 unsigned long *no_handles, efi_handle_t **buffer)
983 unsigned long buffer_size = 0;
985 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
988 if (!no_handles || !buffer) {
989 r = EFI_INVALID_PARAMETER;
994 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
996 if (r != EFI_BUFFER_TOO_SMALL)
998 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1000 if (r != EFI_SUCCESS)
1002 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1004 if (r == EFI_SUCCESS)
1005 *no_handles = buffer_size / sizeof(void *);
1010 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1012 void **protocol_interface)
1014 struct list_head *lhandle;
1017 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
1019 if (!protocol || !protocol_interface)
1020 return EFI_EXIT(EFI_INVALID_PARAMETER);
1022 list_for_each(lhandle, &efi_obj_list) {
1023 struct efi_object *efiobj;
1025 efiobj = list_entry(lhandle, struct efi_object, link);
1026 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1027 struct efi_handler *handler = &efiobj->protocols[i];
1031 if (!guidcmp(handler->guid, protocol)) {
1032 *protocol_interface =
1033 handler->protocol_interface;
1034 return EFI_EXIT(EFI_SUCCESS);
1038 *protocol_interface = NULL;
1040 return EFI_EXIT(EFI_NOT_FOUND);
1043 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1046 EFI_ENTRY("%p", handle);
1049 efi_guid_t *protocol;
1050 void *protocol_interface;
1051 efi_status_t r = EFI_SUCCESS;
1055 return EFI_EXIT(EFI_INVALID_PARAMETER);
1057 va_start(argptr, handle);
1059 protocol = va_arg(argptr, efi_guid_t*);
1062 protocol_interface = va_arg(argptr, void*);
1063 r = efi_install_protocol_interface(handle, protocol,
1064 EFI_NATIVE_INTERFACE,
1065 protocol_interface);
1066 if (r != EFI_SUCCESS)
1071 if (r == EFI_SUCCESS)
1074 /* If an error occured undo all changes. */
1075 va_start(argptr, handle);
1077 protocol = va_arg(argptr, efi_guid_t*);
1078 protocol_interface = va_arg(argptr, void*);
1079 efi_uninstall_protocol_interface(handle, protocol,
1080 protocol_interface);
1087 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1090 EFI_ENTRY("%p", handle);
1091 return EFI_EXIT(EFI_INVALID_PARAMETER);
1094 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1095 unsigned long data_size,
1098 EFI_ENTRY("%p, %ld", data, data_size);
1099 *crc32_p = crc32(0, data, data_size);
1100 return EFI_EXIT(EFI_SUCCESS);
1103 static void EFIAPI efi_copy_mem(void *destination, void *source,
1104 unsigned long length)
1106 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1107 memcpy(destination, source, length);
1110 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1112 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1113 memset(buffer, value, size);
1116 static efi_status_t EFIAPI efi_open_protocol(
1117 void *handle, efi_guid_t *protocol,
1118 void **protocol_interface, void *agent_handle,
1119 void *controller_handle, uint32_t attributes)
1121 struct list_head *lhandle;
1123 efi_status_t r = EFI_INVALID_PARAMETER;
1125 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1126 protocol_interface, agent_handle, controller_handle,
1129 if (!handle || !protocol ||
1130 (!protocol_interface && attributes !=
1131 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1135 switch (attributes) {
1136 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1137 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1138 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1140 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1141 if (controller_handle == handle)
1143 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1144 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1145 if (controller_handle == NULL)
1147 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1148 if (agent_handle == NULL)
1155 list_for_each(lhandle, &efi_obj_list) {
1156 struct efi_object *efiobj;
1157 efiobj = list_entry(lhandle, struct efi_object, link);
1159 if (efiobj->handle != handle)
1162 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1163 struct efi_handler *handler = &efiobj->protocols[i];
1164 const efi_guid_t *hprotocol = handler->guid;
1167 if (!guidcmp(hprotocol, protocol)) {
1169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1170 *protocol_interface =
1171 handler->protocol_interface;
1181 r = EFI_UNSUPPORTED;
1186 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1187 efi_guid_t *protocol,
1188 void **protocol_interface)
1190 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1191 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1194 static const struct efi_boot_services efi_boot_services = {
1196 .headersize = sizeof(struct efi_table_hdr),
1198 .raise_tpl = efi_raise_tpl,
1199 .restore_tpl = efi_restore_tpl,
1200 .allocate_pages = efi_allocate_pages_ext,
1201 .free_pages = efi_free_pages_ext,
1202 .get_memory_map = efi_get_memory_map_ext,
1203 .allocate_pool = efi_allocate_pool_ext,
1204 .free_pool = efi_free_pool_ext,
1205 .create_event = efi_create_event_ext,
1206 .set_timer = efi_set_timer_ext,
1207 .wait_for_event = efi_wait_for_event,
1208 .signal_event = efi_signal_event_ext,
1209 .close_event = efi_close_event,
1210 .check_event = efi_check_event,
1211 .install_protocol_interface = efi_install_protocol_interface_ext,
1212 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1213 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1214 .handle_protocol = efi_handle_protocol,
1216 .register_protocol_notify = efi_register_protocol_notify,
1217 .locate_handle = efi_locate_handle_ext,
1218 .locate_device_path = efi_locate_device_path,
1219 .install_configuration_table = efi_install_configuration_table_ext,
1220 .load_image = efi_load_image,
1221 .start_image = efi_start_image,
1223 .unload_image = efi_unload_image,
1224 .exit_boot_services = efi_exit_boot_services,
1225 .get_next_monotonic_count = efi_get_next_monotonic_count,
1227 .set_watchdog_timer = efi_set_watchdog_timer,
1228 .connect_controller = efi_connect_controller,
1229 .disconnect_controller = efi_disconnect_controller,
1230 .open_protocol = efi_open_protocol,
1231 .close_protocol = efi_close_protocol,
1232 .open_protocol_information = efi_open_protocol_information,
1233 .protocols_per_handle = efi_protocols_per_handle,
1234 .locate_handle_buffer = efi_locate_handle_buffer,
1235 .locate_protocol = efi_locate_protocol,
1236 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1237 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1238 .calculate_crc32 = efi_calculate_crc32,
1239 .copy_mem = efi_copy_mem,
1240 .set_mem = efi_set_mem,
1244 static uint16_t __efi_runtime_data firmware_vendor[] =
1245 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1247 struct efi_system_table __efi_runtime_data systab = {
1249 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1250 .revision = 0x20005, /* 2.5 */
1251 .headersize = sizeof(struct efi_table_hdr),
1253 .fw_vendor = (long)firmware_vendor,
1254 .con_in = (void*)&efi_con_in,
1255 .con_out = (void*)&efi_con_out,
1256 .std_err = (void*)&efi_con_out,
1257 .runtime = (void*)&efi_runtime_services,
1258 .boottime = (void*)&efi_boot_services,
1260 .tables = (void*)efi_conf_table,