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 /* Called from do_bootefi_exec() */
53 void efi_save_gd(void)
60 /* Called on every callback entry */
61 void efi_restore_gd(void)
64 /* Only restore if we're already in EFI context */
74 /* Called on every callback exit */
75 efi_status_t efi_exit_func(efi_status_t ret)
85 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
87 #define EFI_HIGH32(a) (a >> 32)
90 * 64bit division by 10 implemented as multiplication by 1 / 10
92 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
94 #define EFI_TENTH 0x199999999999999A
95 static u64 efi_div10(u64 a)
101 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
102 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
103 rem = EFI_LOW32(prod);
104 ret += EFI_HIGH32(prod);
105 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
106 rem += EFI_LOW32(prod);
107 ret += EFI_HIGH32(prod);
108 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
109 rem += EFI_HIGH32(prod);
110 ret += EFI_HIGH32(rem);
111 /* Round to nearest integer */
112 if (rem >= (1 << 31))
117 void efi_signal_event(struct efi_event *event)
122 if (event->type & EVT_NOTIFY_SIGNAL) {
123 EFI_CALL(event->notify_function(event, event->notify_context));
127 static efi_status_t efi_unsupported(const char *funcname)
129 debug("EFI: App called into unimplemented function %s\n", funcname);
130 return EFI_EXIT(EFI_UNSUPPORTED);
133 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
135 EFI_ENTRY("0x%zx", new_tpl);
139 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
141 EFI_ENTRY("0x%zx", old_tpl);
142 efi_unsupported(__func__);
145 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
151 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
152 r = efi_allocate_pages(type, memory_type, pages, memory);
156 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
161 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
162 r = efi_free_pages(memory, pages);
166 static efi_status_t EFIAPI efi_get_memory_map_ext(
167 unsigned long *memory_map_size,
168 struct efi_mem_desc *memory_map,
169 unsigned long *map_key,
170 unsigned long *descriptor_size,
171 uint32_t *descriptor_version)
175 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
176 map_key, descriptor_size, descriptor_version);
177 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
178 descriptor_size, descriptor_version);
182 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
188 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
189 r = efi_allocate_pool(pool_type, size, buffer);
193 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
197 EFI_ENTRY("%p", buffer);
198 r = efi_free_pool(buffer);
203 * Our event capabilities are very limited. Only a small limited
204 * number of events is allowed to coexist.
206 static struct efi_event efi_events[16];
208 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
209 void (EFIAPI *notify_function) (
210 struct efi_event *event,
212 void *notify_context, struct efi_event **event)
217 return EFI_INVALID_PARAMETER;
219 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
220 return EFI_INVALID_PARAMETER;
222 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
223 notify_function == NULL)
224 return EFI_INVALID_PARAMETER;
226 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
227 if (efi_events[i].type)
229 efi_events[i].type = type;
230 efi_events[i].notify_tpl = notify_tpl;
231 efi_events[i].notify_function = notify_function;
232 efi_events[i].notify_context = notify_context;
233 /* Disable timers on bootup */
234 efi_events[i].trigger_next = -1ULL;
235 efi_events[i].signaled = 0;
236 *event = &efi_events[i];
239 return EFI_OUT_OF_RESOURCES;
242 static efi_status_t EFIAPI efi_create_event_ext(
243 uint32_t type, UINTN notify_tpl,
244 void (EFIAPI *notify_function) (
245 struct efi_event *event,
247 void *notify_context, struct efi_event **event)
249 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
251 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
252 notify_context, event));
257 * Our timers have to work without interrupts, so we check whenever keyboard
258 * input or disk accesses happen if enough time elapsed for it to fire.
260 void efi_timer_check(void)
263 u64 now = timer_get_us();
265 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
266 if (!efi_events[i].type ||
267 !(efi_events[i].type & EVT_TIMER) ||
268 efi_events[i].trigger_type == EFI_TIMER_STOP ||
269 now < efi_events[i].trigger_next)
271 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
272 efi_events[i].trigger_next +=
273 efi_events[i].trigger_time;
274 efi_events[i].signaled = 0;
276 efi_signal_event(&efi_events[i]);
281 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
282 uint64_t trigger_time)
287 * The parameter defines a multiple of 100ns.
288 * We use multiples of 1000ns. So divide by 10.
290 trigger_time = efi_div10(trigger_time);
292 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
293 if (event != &efi_events[i])
296 if (!(event->type & EVT_TIMER))
300 event->trigger_next = -1ULL;
302 case EFI_TIMER_PERIODIC:
303 case EFI_TIMER_RELATIVE:
304 event->trigger_next =
305 timer_get_us() + trigger_time;
308 return EFI_INVALID_PARAMETER;
310 event->trigger_type = type;
311 event->trigger_time = trigger_time;
314 return EFI_INVALID_PARAMETER;
317 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
318 enum efi_timer_delay type,
319 uint64_t trigger_time)
321 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
322 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
325 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
326 struct efi_event **event,
327 unsigned long *index)
331 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
333 /* Check parameters */
334 if (!num_events || !event)
335 return EFI_EXIT(EFI_INVALID_PARAMETER);
336 for (i = 0; i < num_events; ++i) {
337 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
338 if (event[i] == &efi_events[j])
341 return EFI_EXIT(EFI_INVALID_PARAMETER);
343 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
344 return EFI_EXIT(EFI_INVALID_PARAMETER);
347 /* Wait for signal */
349 for (i = 0; i < num_events; ++i) {
350 if (event[i]->signaled)
353 /* Allow events to occur. */
359 * Reset the signal which is passed to the caller to allow periodic
362 event[i]->signaled = 0;
366 return EFI_EXIT(EFI_SUCCESS);
369 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
373 EFI_ENTRY("%p", event);
374 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
375 if (event != &efi_events[i])
377 efi_signal_event(event);
380 return EFI_EXIT(EFI_SUCCESS);
383 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
387 EFI_ENTRY("%p", event);
388 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
389 if (event == &efi_events[i]) {
391 event->trigger_next = -1ULL;
393 return EFI_EXIT(EFI_SUCCESS);
396 return EFI_EXIT(EFI_INVALID_PARAMETER);
399 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
403 EFI_ENTRY("%p", event);
405 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
406 if (event != &efi_events[i])
408 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
411 return EFI_EXIT(EFI_SUCCESS);
412 return EFI_EXIT(EFI_NOT_READY);
414 return EFI_EXIT(EFI_INVALID_PARAMETER);
417 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
418 efi_guid_t *protocol, int protocol_interface_type,
419 void *protocol_interface)
421 struct list_head *lhandle;
425 if (!handle || !protocol ||
426 protocol_interface_type != EFI_NATIVE_INTERFACE) {
427 r = EFI_INVALID_PARAMETER;
431 /* Create new handle if requested. */
433 r = EFI_OUT_OF_RESOURCES;
437 list_for_each(lhandle, &efi_obj_list) {
438 struct efi_object *efiobj;
439 efiobj = list_entry(lhandle, struct efi_object, link);
441 if (efiobj->handle != *handle)
443 /* Check if protocol is already installed on the handle. */
444 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
445 struct efi_handler *handler = &efiobj->protocols[i];
449 if (!guidcmp(handler->guid, protocol)) {
450 r = EFI_INVALID_PARAMETER;
454 /* Install protocol in first empty slot. */
455 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
456 struct efi_handler *handler = &efiobj->protocols[i];
461 handler->guid = protocol;
462 handler->protocol_interface = protocol_interface;
466 r = EFI_OUT_OF_RESOURCES;
469 r = EFI_INVALID_PARAMETER;
474 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
475 efi_guid_t *protocol, int protocol_interface_type,
476 void *protocol_interface)
478 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
481 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
482 protocol_interface_type,
483 protocol_interface));
486 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
487 efi_guid_t *protocol, void *old_interface,
490 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
492 return EFI_EXIT(EFI_ACCESS_DENIED);
495 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
496 efi_guid_t *protocol, void *protocol_interface)
498 struct list_head *lhandle;
500 efi_status_t r = EFI_NOT_FOUND;
502 if (!handle || !protocol) {
503 r = EFI_INVALID_PARAMETER;
507 list_for_each(lhandle, &efi_obj_list) {
508 struct efi_object *efiobj;
509 efiobj = list_entry(lhandle, struct efi_object, link);
511 if (efiobj->handle != handle)
514 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
515 struct efi_handler *handler = &efiobj->protocols[i];
516 const efi_guid_t *hprotocol = handler->guid;
520 if (!guidcmp(hprotocol, protocol)) {
521 if (handler->protocol_interface) {
522 r = EFI_ACCESS_DENIED;
536 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
537 efi_guid_t *protocol, void *protocol_interface)
539 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
541 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
542 protocol_interface));
545 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
546 struct efi_event *event,
549 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
550 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
553 static int efi_search(enum efi_locate_search_type search_type,
554 efi_guid_t *protocol, void *search_key,
555 struct efi_object *efiobj)
559 switch (search_type) {
562 case by_register_notify:
565 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
566 const efi_guid_t *guid = efiobj->protocols[i].guid;
567 if (guid && !guidcmp(guid, protocol))
576 static efi_status_t EFIAPI efi_locate_handle(
577 enum efi_locate_search_type search_type,
578 efi_guid_t *protocol, void *search_key,
579 unsigned long *buffer_size, efi_handle_t *buffer)
581 struct list_head *lhandle;
582 unsigned long size = 0;
584 /* Count how much space we need */
585 list_for_each(lhandle, &efi_obj_list) {
586 struct efi_object *efiobj;
587 efiobj = list_entry(lhandle, struct efi_object, link);
588 if (!efi_search(search_type, protocol, search_key, efiobj)) {
589 size += sizeof(void*);
593 if (*buffer_size < size) {
595 return EFI_BUFFER_TOO_SMALL;
598 /* Then fill the array */
599 list_for_each(lhandle, &efi_obj_list) {
600 struct efi_object *efiobj;
601 efiobj = list_entry(lhandle, struct efi_object, link);
602 if (!efi_search(search_type, protocol, search_key, efiobj)) {
603 *(buffer++) = efiobj->handle;
611 static efi_status_t EFIAPI efi_locate_handle_ext(
612 enum efi_locate_search_type search_type,
613 efi_guid_t *protocol, void *search_key,
614 unsigned long *buffer_size, efi_handle_t *buffer)
616 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
617 buffer_size, buffer);
619 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
620 buffer_size, buffer));
623 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
624 struct efi_device_path **device_path,
625 efi_handle_t *device)
627 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
628 return EFI_EXIT(EFI_NOT_FOUND);
631 /* Collapses configuration table entries, removing index i */
632 static void efi_remove_configuration_table(int i)
634 struct efi_configuration_table *this = &efi_conf_table[i];
635 struct efi_configuration_table *next = &efi_conf_table[i+1];
636 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
638 memmove(this, next, (ulong)end - (ulong)next);
642 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
646 /* Check for guid override */
647 for (i = 0; i < systab.nr_tables; i++) {
648 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
650 efi_conf_table[i].table = table;
652 efi_remove_configuration_table(i);
658 return EFI_NOT_FOUND;
660 /* No override, check for overflow */
661 if (i >= ARRAY_SIZE(efi_conf_table))
662 return EFI_OUT_OF_RESOURCES;
664 /* Add a new entry */
665 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
666 efi_conf_table[i].table = table;
667 systab.nr_tables = i + 1;
672 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
675 EFI_ENTRY("%p, %p", guid, table);
676 return EFI_EXIT(efi_install_configuration_table(guid, table));
679 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
680 efi_handle_t parent_image,
681 struct efi_device_path *file_path,
683 unsigned long source_size,
684 efi_handle_t *image_handle)
686 static struct efi_object loaded_image_info_obj = {
689 .guid = &efi_guid_loaded_image,
693 struct efi_loaded_image *info;
694 struct efi_object *obj;
696 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
697 file_path, source_buffer, source_size, image_handle);
698 info = malloc(sizeof(*info));
699 loaded_image_info_obj.protocols[0].protocol_interface = info;
700 obj = malloc(sizeof(loaded_image_info_obj));
701 memset(info, 0, sizeof(*info));
702 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
704 info->file_path = file_path;
705 info->reserved = efi_load_pe(source_buffer, info);
706 if (!info->reserved) {
709 return EFI_EXIT(EFI_UNSUPPORTED);
712 *image_handle = info;
713 list_add_tail(&obj->link, &efi_obj_list);
715 return EFI_EXIT(EFI_SUCCESS);
718 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
719 unsigned long *exit_data_size,
722 ulong (*entry)(void *image_handle, struct efi_system_table *st);
723 struct efi_loaded_image *info = image_handle;
725 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
726 entry = info->reserved;
728 efi_is_direct_boot = false;
730 /* call the image! */
731 if (setjmp(&info->exit_jmp)) {
732 /* We returned from the child image */
733 return EFI_EXIT(info->exit_status);
736 entry(image_handle, &systab);
738 /* Should usually never get here */
739 return EFI_EXIT(EFI_SUCCESS);
742 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
743 efi_status_t exit_status, unsigned long exit_data_size,
746 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
748 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
749 exit_data_size, exit_data);
751 loaded_image_info->exit_status = exit_status;
752 longjmp(&loaded_image_info->exit_jmp, 1);
754 panic("EFI application exited");
757 static struct efi_object *efi_search_obj(void *handle)
759 struct list_head *lhandle;
761 list_for_each(lhandle, &efi_obj_list) {
762 struct efi_object *efiobj;
763 efiobj = list_entry(lhandle, struct efi_object, link);
764 if (efiobj->handle == handle)
771 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
773 struct efi_object *efiobj;
775 EFI_ENTRY("%p", image_handle);
776 efiobj = efi_search_obj(image_handle);
778 list_del(&efiobj->link);
780 return EFI_EXIT(EFI_SUCCESS);
783 static void efi_exit_caches(void)
785 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
787 * Grub on 32bit ARM needs to have caches disabled before jumping into
788 * a zImage, but does not know of all cache layers. Give it a hand.
790 if (efi_is_direct_boot)
791 cleanup_before_linux();
795 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
796 unsigned long map_key)
798 EFI_ENTRY("%p, %ld", image_handle, map_key);
800 board_quiesce_devices();
802 /* Fix up caches for EFI payloads if necessary */
805 /* This stops all lingering devices */
806 bootm_disable_interrupts();
808 /* Give the payload some time to boot */
811 return EFI_EXIT(EFI_SUCCESS);
814 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
816 static uint64_t mono = 0;
817 EFI_ENTRY("%p", count);
819 return EFI_EXIT(EFI_SUCCESS);
822 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
824 EFI_ENTRY("%ld", microseconds);
825 udelay(microseconds);
826 return EFI_EXIT(EFI_SUCCESS);
829 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
830 uint64_t watchdog_code,
831 unsigned long data_size,
832 uint16_t *watchdog_data)
834 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
835 data_size, watchdog_data);
836 return efi_unsupported(__func__);
839 static efi_status_t EFIAPI efi_connect_controller(
840 efi_handle_t controller_handle,
841 efi_handle_t *driver_image_handle,
842 struct efi_device_path *remain_device_path,
845 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
846 remain_device_path, recursive);
847 return EFI_EXIT(EFI_NOT_FOUND);
850 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
851 void *driver_image_handle,
854 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
856 return EFI_EXIT(EFI_INVALID_PARAMETER);
859 static efi_status_t EFIAPI efi_close_protocol(void *handle,
860 efi_guid_t *protocol,
862 void *controller_handle)
864 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
866 return EFI_EXIT(EFI_NOT_FOUND);
869 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
870 efi_guid_t *protocol,
871 struct efi_open_protocol_info_entry **entry_buffer,
872 unsigned long *entry_count)
874 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
876 return EFI_EXIT(EFI_NOT_FOUND);
879 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
880 efi_guid_t ***protocol_buffer,
881 unsigned long *protocol_buffer_count)
883 unsigned long buffer_size;
884 struct efi_object *efiobj;
886 struct list_head *lhandle;
889 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
890 protocol_buffer_count);
892 if (!handle || !protocol_buffer || !protocol_buffer_count)
893 return EFI_EXIT(EFI_INVALID_PARAMETER);
895 *protocol_buffer = NULL;
896 *protocol_buffer_count = 0;
897 list_for_each(lhandle, &efi_obj_list) {
898 efiobj = list_entry(lhandle, struct efi_object, link);
900 if (efiobj->handle != handle)
903 /* Count protocols */
904 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
905 if (efiobj->protocols[i].guid)
906 ++*protocol_buffer_count;
909 if (*protocol_buffer_count) {
910 buffer_size = sizeof(efi_guid_t *) *
911 *protocol_buffer_count;
912 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
914 (void **)protocol_buffer);
915 if (r != EFI_SUCCESS)
918 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
919 if (efiobj->protocols[i].guid) {
920 (*protocol_buffer)[j] = (void *)
921 efiobj->protocols[i].guid;
929 return EFI_EXIT(EFI_SUCCESS);
932 static efi_status_t EFIAPI efi_locate_handle_buffer(
933 enum efi_locate_search_type search_type,
934 efi_guid_t *protocol, void *search_key,
935 unsigned long *no_handles, efi_handle_t **buffer)
938 unsigned long buffer_size = 0;
940 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
943 if (!no_handles || !buffer) {
944 r = EFI_INVALID_PARAMETER;
949 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
951 if (r != EFI_BUFFER_TOO_SMALL)
953 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
955 if (r != EFI_SUCCESS)
957 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
959 if (r == EFI_SUCCESS)
960 *no_handles = buffer_size / sizeof(void *);
965 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
967 void **protocol_interface)
969 struct list_head *lhandle;
972 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
974 if (!protocol || !protocol_interface)
975 return EFI_EXIT(EFI_INVALID_PARAMETER);
977 list_for_each(lhandle, &efi_obj_list) {
978 struct efi_object *efiobj;
980 efiobj = list_entry(lhandle, struct efi_object, link);
981 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
982 struct efi_handler *handler = &efiobj->protocols[i];
986 if (!guidcmp(handler->guid, protocol)) {
987 *protocol_interface =
988 handler->protocol_interface;
989 return EFI_EXIT(EFI_SUCCESS);
993 *protocol_interface = NULL;
995 return EFI_EXIT(EFI_NOT_FOUND);
998 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1001 EFI_ENTRY("%p", handle);
1004 efi_guid_t *protocol;
1005 void *protocol_interface;
1006 efi_status_t r = EFI_SUCCESS;
1010 return EFI_EXIT(EFI_INVALID_PARAMETER);
1012 va_start(argptr, handle);
1014 protocol = va_arg(argptr, efi_guid_t*);
1017 protocol_interface = va_arg(argptr, void*);
1018 r = efi_install_protocol_interface(handle, protocol,
1019 EFI_NATIVE_INTERFACE,
1020 protocol_interface);
1021 if (r != EFI_SUCCESS)
1026 if (r == EFI_SUCCESS)
1029 /* If an error occured undo all changes. */
1030 va_start(argptr, handle);
1032 protocol = va_arg(argptr, efi_guid_t*);
1033 protocol_interface = va_arg(argptr, void*);
1034 efi_uninstall_protocol_interface(handle, protocol,
1035 protocol_interface);
1042 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1045 EFI_ENTRY("%p", handle);
1046 return EFI_EXIT(EFI_INVALID_PARAMETER);
1049 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1050 unsigned long data_size,
1053 EFI_ENTRY("%p, %ld", data, data_size);
1054 *crc32_p = crc32(0, data, data_size);
1055 return EFI_EXIT(EFI_SUCCESS);
1058 static void EFIAPI efi_copy_mem(void *destination, void *source,
1059 unsigned long length)
1061 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1062 memcpy(destination, source, length);
1065 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1067 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1068 memset(buffer, value, size);
1071 static efi_status_t EFIAPI efi_open_protocol(
1072 void *handle, efi_guid_t *protocol,
1073 void **protocol_interface, void *agent_handle,
1074 void *controller_handle, uint32_t attributes)
1076 struct list_head *lhandle;
1078 efi_status_t r = EFI_INVALID_PARAMETER;
1080 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1081 protocol_interface, agent_handle, controller_handle,
1084 if (!handle || !protocol ||
1085 (!protocol_interface && attributes !=
1086 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1090 switch (attributes) {
1091 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1092 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1093 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1095 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1096 if (controller_handle == handle)
1098 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1099 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1100 if (controller_handle == NULL)
1102 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1103 if (agent_handle == NULL)
1110 list_for_each(lhandle, &efi_obj_list) {
1111 struct efi_object *efiobj;
1112 efiobj = list_entry(lhandle, struct efi_object, link);
1114 if (efiobj->handle != handle)
1117 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1118 struct efi_handler *handler = &efiobj->protocols[i];
1119 const efi_guid_t *hprotocol = handler->guid;
1122 if (!guidcmp(hprotocol, protocol)) {
1124 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1125 *protocol_interface =
1126 handler->protocol_interface;
1136 r = EFI_UNSUPPORTED;
1141 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1142 efi_guid_t *protocol,
1143 void **protocol_interface)
1145 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1146 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1149 static const struct efi_boot_services efi_boot_services = {
1151 .headersize = sizeof(struct efi_table_hdr),
1153 .raise_tpl = efi_raise_tpl,
1154 .restore_tpl = efi_restore_tpl,
1155 .allocate_pages = efi_allocate_pages_ext,
1156 .free_pages = efi_free_pages_ext,
1157 .get_memory_map = efi_get_memory_map_ext,
1158 .allocate_pool = efi_allocate_pool_ext,
1159 .free_pool = efi_free_pool_ext,
1160 .create_event = efi_create_event_ext,
1161 .set_timer = efi_set_timer_ext,
1162 .wait_for_event = efi_wait_for_event,
1163 .signal_event = efi_signal_event_ext,
1164 .close_event = efi_close_event,
1165 .check_event = efi_check_event,
1166 .install_protocol_interface = efi_install_protocol_interface_ext,
1167 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1168 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1169 .handle_protocol = efi_handle_protocol,
1171 .register_protocol_notify = efi_register_protocol_notify,
1172 .locate_handle = efi_locate_handle_ext,
1173 .locate_device_path = efi_locate_device_path,
1174 .install_configuration_table = efi_install_configuration_table_ext,
1175 .load_image = efi_load_image,
1176 .start_image = efi_start_image,
1178 .unload_image = efi_unload_image,
1179 .exit_boot_services = efi_exit_boot_services,
1180 .get_next_monotonic_count = efi_get_next_monotonic_count,
1182 .set_watchdog_timer = efi_set_watchdog_timer,
1183 .connect_controller = efi_connect_controller,
1184 .disconnect_controller = efi_disconnect_controller,
1185 .open_protocol = efi_open_protocol,
1186 .close_protocol = efi_close_protocol,
1187 .open_protocol_information = efi_open_protocol_information,
1188 .protocols_per_handle = efi_protocols_per_handle,
1189 .locate_handle_buffer = efi_locate_handle_buffer,
1190 .locate_protocol = efi_locate_protocol,
1191 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1192 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1193 .calculate_crc32 = efi_calculate_crc32,
1194 .copy_mem = efi_copy_mem,
1195 .set_mem = efi_set_mem,
1199 static uint16_t __efi_runtime_data firmware_vendor[] =
1200 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1202 struct efi_system_table __efi_runtime_data systab = {
1204 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1205 .revision = 0x20005, /* 2.5 */
1206 .headersize = sizeof(struct efi_table_hdr),
1208 .fw_vendor = (long)firmware_vendor,
1209 .con_in = (void*)&efi_con_in,
1210 .con_out = (void*)&efi_con_out,
1211 .std_err = (void*)&efi_con_out,
1212 .runtime = (void*)&efi_runtime_services,
1213 .boottime = (void*)&efi_boot_services,
1215 .tables = (void*)efi_conf_table,