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;
54 /* Called on every callback entry */
55 int __efi_entry_check(void)
57 int ret = entry_count++ == 0;
66 /* Called on every callback exit */
67 int __efi_exit_check(void)
69 int ret = --entry_count == 0;
76 /* Called from do_bootefi_exec() */
77 void efi_save_gd(void)
85 * Special case handler for error/abort that just forces things back
86 * to u-boot world so we can dump out an abort msg, without any care
87 * about returning back to UEFI world.
89 void efi_restore_gd(void)
92 /* Only restore if we're already in EFI context */
100 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
102 #define EFI_HIGH32(a) (a >> 32)
105 * 64bit division by 10 implemented as multiplication by 1 / 10
107 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
109 #define EFI_TENTH 0x199999999999999A
110 static u64 efi_div10(u64 a)
116 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
117 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
118 rem = EFI_LOW32(prod);
119 ret += EFI_HIGH32(prod);
120 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
121 rem += EFI_LOW32(prod);
122 ret += EFI_HIGH32(prod);
123 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
124 rem += EFI_HIGH32(prod);
125 ret += EFI_HIGH32(rem);
126 /* Round to nearest integer */
127 if (rem >= (1 << 31))
132 void efi_signal_event(struct efi_event *event)
137 if (event->type & EVT_NOTIFY_SIGNAL) {
138 EFI_CALL(event->notify_function(event, event->notify_context));
142 static efi_status_t efi_unsupported(const char *funcname)
144 debug("EFI: App called into unimplemented function %s\n", funcname);
145 return EFI_EXIT(EFI_UNSUPPORTED);
148 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
150 EFI_ENTRY("0x%zx", new_tpl);
154 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
156 EFI_ENTRY("0x%zx", old_tpl);
157 efi_unsupported(__func__);
160 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
166 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
167 r = efi_allocate_pages(type, memory_type, pages, memory);
171 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
176 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
177 r = efi_free_pages(memory, pages);
181 static efi_status_t EFIAPI efi_get_memory_map_ext(
182 unsigned long *memory_map_size,
183 struct efi_mem_desc *memory_map,
184 unsigned long *map_key,
185 unsigned long *descriptor_size,
186 uint32_t *descriptor_version)
190 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
191 map_key, descriptor_size, descriptor_version);
192 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
193 descriptor_size, descriptor_version);
197 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
203 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
204 r = efi_allocate_pool(pool_type, size, buffer);
208 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
212 EFI_ENTRY("%p", buffer);
213 r = efi_free_pool(buffer);
218 * Our event capabilities are very limited. Only a small limited
219 * number of events is allowed to coexist.
221 static struct efi_event efi_events[16];
223 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
224 void (EFIAPI *notify_function) (
225 struct efi_event *event,
227 void *notify_context, struct efi_event **event)
232 return EFI_INVALID_PARAMETER;
234 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
235 return EFI_INVALID_PARAMETER;
237 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
238 notify_function == NULL)
239 return EFI_INVALID_PARAMETER;
241 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
242 if (efi_events[i].type)
244 efi_events[i].type = type;
245 efi_events[i].notify_tpl = notify_tpl;
246 efi_events[i].notify_function = notify_function;
247 efi_events[i].notify_context = notify_context;
248 /* Disable timers on bootup */
249 efi_events[i].trigger_next = -1ULL;
250 efi_events[i].signaled = 0;
251 *event = &efi_events[i];
254 return EFI_OUT_OF_RESOURCES;
257 static efi_status_t EFIAPI efi_create_event_ext(
258 uint32_t type, UINTN notify_tpl,
259 void (EFIAPI *notify_function) (
260 struct efi_event *event,
262 void *notify_context, struct efi_event **event)
264 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
266 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
267 notify_context, event));
272 * Our timers have to work without interrupts, so we check whenever keyboard
273 * input or disk accesses happen if enough time elapsed for it to fire.
275 void efi_timer_check(void)
278 u64 now = timer_get_us();
280 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
281 if (!efi_events[i].type ||
282 !(efi_events[i].type & EVT_TIMER) ||
283 efi_events[i].trigger_type == EFI_TIMER_STOP ||
284 now < efi_events[i].trigger_next)
286 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
287 efi_events[i].trigger_next +=
288 efi_events[i].trigger_time;
289 efi_events[i].signaled = 0;
291 efi_signal_event(&efi_events[i]);
296 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
297 uint64_t trigger_time)
302 * The parameter defines a multiple of 100ns.
303 * We use multiples of 1000ns. So divide by 10.
305 trigger_time = efi_div10(trigger_time);
307 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
308 if (event != &efi_events[i])
311 if (!(event->type & EVT_TIMER))
315 event->trigger_next = -1ULL;
317 case EFI_TIMER_PERIODIC:
318 case EFI_TIMER_RELATIVE:
319 event->trigger_next =
320 timer_get_us() + trigger_time;
323 return EFI_INVALID_PARAMETER;
325 event->trigger_type = type;
326 event->trigger_time = trigger_time;
329 return EFI_INVALID_PARAMETER;
332 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
333 enum efi_timer_delay type,
334 uint64_t trigger_time)
336 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
337 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
340 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
341 struct efi_event **event,
342 unsigned long *index)
346 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
348 /* Check parameters */
349 if (!num_events || !event)
350 return EFI_EXIT(EFI_INVALID_PARAMETER);
351 for (i = 0; i < num_events; ++i) {
352 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
353 if (event[i] == &efi_events[j])
356 return EFI_EXIT(EFI_INVALID_PARAMETER);
358 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
359 return EFI_EXIT(EFI_INVALID_PARAMETER);
362 /* Wait for signal */
364 for (i = 0; i < num_events; ++i) {
365 if (event[i]->signaled)
368 /* Allow events to occur. */
374 * Reset the signal which is passed to the caller to allow periodic
377 event[i]->signaled = 0;
381 return EFI_EXIT(EFI_SUCCESS);
384 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
388 EFI_ENTRY("%p", event);
389 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
390 if (event != &efi_events[i])
392 efi_signal_event(event);
395 return EFI_EXIT(EFI_SUCCESS);
398 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
402 EFI_ENTRY("%p", event);
403 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
404 if (event == &efi_events[i]) {
406 event->trigger_next = -1ULL;
408 return EFI_EXIT(EFI_SUCCESS);
411 return EFI_EXIT(EFI_INVALID_PARAMETER);
414 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
418 EFI_ENTRY("%p", event);
420 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
421 if (event != &efi_events[i])
423 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
426 return EFI_EXIT(EFI_SUCCESS);
427 return EFI_EXIT(EFI_NOT_READY);
429 return EFI_EXIT(EFI_INVALID_PARAMETER);
432 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
433 efi_guid_t *protocol, int protocol_interface_type,
434 void *protocol_interface)
436 struct list_head *lhandle;
440 if (!handle || !protocol ||
441 protocol_interface_type != EFI_NATIVE_INTERFACE) {
442 r = EFI_INVALID_PARAMETER;
446 /* Create new handle if requested. */
448 r = EFI_OUT_OF_RESOURCES;
452 list_for_each(lhandle, &efi_obj_list) {
453 struct efi_object *efiobj;
454 efiobj = list_entry(lhandle, struct efi_object, link);
456 if (efiobj->handle != *handle)
458 /* Check if protocol is already installed on the handle. */
459 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
460 struct efi_handler *handler = &efiobj->protocols[i];
464 if (!guidcmp(handler->guid, protocol)) {
465 r = EFI_INVALID_PARAMETER;
469 /* Install protocol in first empty slot. */
470 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
471 struct efi_handler *handler = &efiobj->protocols[i];
476 handler->guid = protocol;
477 handler->protocol_interface = protocol_interface;
481 r = EFI_OUT_OF_RESOURCES;
484 r = EFI_INVALID_PARAMETER;
489 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
490 efi_guid_t *protocol, int protocol_interface_type,
491 void *protocol_interface)
493 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
496 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
497 protocol_interface_type,
498 protocol_interface));
501 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
502 efi_guid_t *protocol, void *old_interface,
505 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
507 return EFI_EXIT(EFI_ACCESS_DENIED);
510 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
511 efi_guid_t *protocol, void *protocol_interface)
513 struct list_head *lhandle;
515 efi_status_t r = EFI_NOT_FOUND;
517 if (!handle || !protocol) {
518 r = EFI_INVALID_PARAMETER;
522 list_for_each(lhandle, &efi_obj_list) {
523 struct efi_object *efiobj;
524 efiobj = list_entry(lhandle, struct efi_object, link);
526 if (efiobj->handle != handle)
529 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
530 struct efi_handler *handler = &efiobj->protocols[i];
531 const efi_guid_t *hprotocol = handler->guid;
535 if (!guidcmp(hprotocol, protocol)) {
536 if (handler->protocol_interface) {
537 r = EFI_ACCESS_DENIED;
551 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
552 efi_guid_t *protocol, void *protocol_interface)
554 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
556 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
557 protocol_interface));
560 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
561 struct efi_event *event,
564 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
565 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
568 static int efi_search(enum efi_locate_search_type search_type,
569 efi_guid_t *protocol, void *search_key,
570 struct efi_object *efiobj)
574 switch (search_type) {
577 case by_register_notify:
580 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
581 const efi_guid_t *guid = efiobj->protocols[i].guid;
582 if (guid && !guidcmp(guid, protocol))
591 static efi_status_t EFIAPI efi_locate_handle(
592 enum efi_locate_search_type search_type,
593 efi_guid_t *protocol, void *search_key,
594 unsigned long *buffer_size, efi_handle_t *buffer)
596 struct list_head *lhandle;
597 unsigned long size = 0;
599 /* Count how much space we need */
600 list_for_each(lhandle, &efi_obj_list) {
601 struct efi_object *efiobj;
602 efiobj = list_entry(lhandle, struct efi_object, link);
603 if (!efi_search(search_type, protocol, search_key, efiobj)) {
604 size += sizeof(void*);
608 if (*buffer_size < size) {
610 return EFI_BUFFER_TOO_SMALL;
613 /* Then fill the array */
614 list_for_each(lhandle, &efi_obj_list) {
615 struct efi_object *efiobj;
616 efiobj = list_entry(lhandle, struct efi_object, link);
617 if (!efi_search(search_type, protocol, search_key, efiobj)) {
618 *(buffer++) = efiobj->handle;
626 static efi_status_t EFIAPI efi_locate_handle_ext(
627 enum efi_locate_search_type search_type,
628 efi_guid_t *protocol, void *search_key,
629 unsigned long *buffer_size, efi_handle_t *buffer)
631 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
632 buffer_size, buffer);
634 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
635 buffer_size, buffer));
638 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
639 struct efi_device_path **device_path,
640 efi_handle_t *device)
642 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
643 return EFI_EXIT(EFI_NOT_FOUND);
646 /* Collapses configuration table entries, removing index i */
647 static void efi_remove_configuration_table(int i)
649 struct efi_configuration_table *this = &efi_conf_table[i];
650 struct efi_configuration_table *next = &efi_conf_table[i+1];
651 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
653 memmove(this, next, (ulong)end - (ulong)next);
657 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
661 /* Check for guid override */
662 for (i = 0; i < systab.nr_tables; i++) {
663 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
665 efi_conf_table[i].table = table;
667 efi_remove_configuration_table(i);
673 return EFI_NOT_FOUND;
675 /* No override, check for overflow */
676 if (i >= ARRAY_SIZE(efi_conf_table))
677 return EFI_OUT_OF_RESOURCES;
679 /* Add a new entry */
680 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
681 efi_conf_table[i].table = table;
682 systab.nr_tables = i + 1;
687 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
690 EFI_ENTRY("%p, %p", guid, table);
691 return EFI_EXIT(efi_install_configuration_table(guid, table));
694 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
695 efi_handle_t parent_image,
696 struct efi_device_path *file_path,
698 unsigned long source_size,
699 efi_handle_t *image_handle)
701 static struct efi_object loaded_image_info_obj = {
704 .guid = &efi_guid_loaded_image,
708 struct efi_loaded_image *info;
709 struct efi_object *obj;
711 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
712 file_path, source_buffer, source_size, image_handle);
713 info = malloc(sizeof(*info));
714 loaded_image_info_obj.protocols[0].protocol_interface = info;
715 obj = malloc(sizeof(loaded_image_info_obj));
716 memset(info, 0, sizeof(*info));
717 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
719 info->file_path = file_path;
720 info->reserved = efi_load_pe(source_buffer, info);
721 if (!info->reserved) {
724 return EFI_EXIT(EFI_UNSUPPORTED);
727 *image_handle = info;
728 list_add_tail(&obj->link, &efi_obj_list);
730 return EFI_EXIT(EFI_SUCCESS);
733 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
734 unsigned long *exit_data_size,
737 ulong (*entry)(void *image_handle, struct efi_system_table *st);
738 struct efi_loaded_image *info = image_handle;
740 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
741 entry = info->reserved;
743 efi_is_direct_boot = false;
745 /* call the image! */
746 if (setjmp(&info->exit_jmp)) {
747 /* We returned from the child image */
748 return EFI_EXIT(info->exit_status);
752 entry(image_handle, &systab);
755 /* Should usually never get here */
756 return EFI_EXIT(EFI_SUCCESS);
759 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
760 efi_status_t exit_status, unsigned long exit_data_size,
763 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
765 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
766 exit_data_size, exit_data);
768 loaded_image_info->exit_status = exit_status;
769 longjmp(&loaded_image_info->exit_jmp, 1);
771 panic("EFI application exited");
774 static struct efi_object *efi_search_obj(void *handle)
776 struct list_head *lhandle;
778 list_for_each(lhandle, &efi_obj_list) {
779 struct efi_object *efiobj;
780 efiobj = list_entry(lhandle, struct efi_object, link);
781 if (efiobj->handle == handle)
788 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
790 struct efi_object *efiobj;
792 EFI_ENTRY("%p", image_handle);
793 efiobj = efi_search_obj(image_handle);
795 list_del(&efiobj->link);
797 return EFI_EXIT(EFI_SUCCESS);
800 static void efi_exit_caches(void)
802 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
804 * Grub on 32bit ARM needs to have caches disabled before jumping into
805 * a zImage, but does not know of all cache layers. Give it a hand.
807 if (efi_is_direct_boot)
808 cleanup_before_linux();
812 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
813 unsigned long map_key)
815 EFI_ENTRY("%p, %ld", image_handle, map_key);
817 board_quiesce_devices();
819 /* Fix up caches for EFI payloads if necessary */
822 /* This stops all lingering devices */
823 bootm_disable_interrupts();
825 /* Give the payload some time to boot */
828 return EFI_EXIT(EFI_SUCCESS);
831 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
833 static uint64_t mono = 0;
834 EFI_ENTRY("%p", count);
836 return EFI_EXIT(EFI_SUCCESS);
839 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
841 EFI_ENTRY("%ld", microseconds);
842 udelay(microseconds);
843 return EFI_EXIT(EFI_SUCCESS);
846 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
847 uint64_t watchdog_code,
848 unsigned long data_size,
849 uint16_t *watchdog_data)
851 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
852 data_size, watchdog_data);
853 return efi_unsupported(__func__);
856 static efi_status_t EFIAPI efi_connect_controller(
857 efi_handle_t controller_handle,
858 efi_handle_t *driver_image_handle,
859 struct efi_device_path *remain_device_path,
862 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
863 remain_device_path, recursive);
864 return EFI_EXIT(EFI_NOT_FOUND);
867 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
868 void *driver_image_handle,
871 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
873 return EFI_EXIT(EFI_INVALID_PARAMETER);
876 static efi_status_t EFIAPI efi_close_protocol(void *handle,
877 efi_guid_t *protocol,
879 void *controller_handle)
881 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
883 return EFI_EXIT(EFI_NOT_FOUND);
886 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
887 efi_guid_t *protocol,
888 struct efi_open_protocol_info_entry **entry_buffer,
889 unsigned long *entry_count)
891 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
893 return EFI_EXIT(EFI_NOT_FOUND);
896 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
897 efi_guid_t ***protocol_buffer,
898 unsigned long *protocol_buffer_count)
900 unsigned long buffer_size;
901 struct efi_object *efiobj;
903 struct list_head *lhandle;
906 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
907 protocol_buffer_count);
909 if (!handle || !protocol_buffer || !protocol_buffer_count)
910 return EFI_EXIT(EFI_INVALID_PARAMETER);
912 *protocol_buffer = NULL;
913 *protocol_buffer_count = 0;
914 list_for_each(lhandle, &efi_obj_list) {
915 efiobj = list_entry(lhandle, struct efi_object, link);
917 if (efiobj->handle != handle)
920 /* Count protocols */
921 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
922 if (efiobj->protocols[i].guid)
923 ++*protocol_buffer_count;
926 if (*protocol_buffer_count) {
927 buffer_size = sizeof(efi_guid_t *) *
928 *protocol_buffer_count;
929 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
931 (void **)protocol_buffer);
932 if (r != EFI_SUCCESS)
935 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
936 if (efiobj->protocols[i].guid) {
937 (*protocol_buffer)[j] = (void *)
938 efiobj->protocols[i].guid;
946 return EFI_EXIT(EFI_SUCCESS);
949 static efi_status_t EFIAPI efi_locate_handle_buffer(
950 enum efi_locate_search_type search_type,
951 efi_guid_t *protocol, void *search_key,
952 unsigned long *no_handles, efi_handle_t **buffer)
955 unsigned long buffer_size = 0;
957 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
960 if (!no_handles || !buffer) {
961 r = EFI_INVALID_PARAMETER;
966 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
968 if (r != EFI_BUFFER_TOO_SMALL)
970 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
972 if (r != EFI_SUCCESS)
974 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
976 if (r == EFI_SUCCESS)
977 *no_handles = buffer_size / sizeof(void *);
982 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
984 void **protocol_interface)
986 struct list_head *lhandle;
989 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
991 if (!protocol || !protocol_interface)
992 return EFI_EXIT(EFI_INVALID_PARAMETER);
994 list_for_each(lhandle, &efi_obj_list) {
995 struct efi_object *efiobj;
997 efiobj = list_entry(lhandle, struct efi_object, link);
998 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
999 struct efi_handler *handler = &efiobj->protocols[i];
1003 if (!guidcmp(handler->guid, protocol)) {
1004 *protocol_interface =
1005 handler->protocol_interface;
1006 return EFI_EXIT(EFI_SUCCESS);
1010 *protocol_interface = NULL;
1012 return EFI_EXIT(EFI_NOT_FOUND);
1015 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1018 EFI_ENTRY("%p", handle);
1021 efi_guid_t *protocol;
1022 void *protocol_interface;
1023 efi_status_t r = EFI_SUCCESS;
1027 return EFI_EXIT(EFI_INVALID_PARAMETER);
1029 va_start(argptr, handle);
1031 protocol = va_arg(argptr, efi_guid_t*);
1034 protocol_interface = va_arg(argptr, void*);
1035 r = efi_install_protocol_interface(handle, protocol,
1036 EFI_NATIVE_INTERFACE,
1037 protocol_interface);
1038 if (r != EFI_SUCCESS)
1043 if (r == EFI_SUCCESS)
1046 /* If an error occured undo all changes. */
1047 va_start(argptr, handle);
1049 protocol = va_arg(argptr, efi_guid_t*);
1050 protocol_interface = va_arg(argptr, void*);
1051 efi_uninstall_protocol_interface(handle, protocol,
1052 protocol_interface);
1059 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1062 EFI_ENTRY("%p", handle);
1063 return EFI_EXIT(EFI_INVALID_PARAMETER);
1066 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1067 unsigned long data_size,
1070 EFI_ENTRY("%p, %ld", data, data_size);
1071 *crc32_p = crc32(0, data, data_size);
1072 return EFI_EXIT(EFI_SUCCESS);
1075 static void EFIAPI efi_copy_mem(void *destination, void *source,
1076 unsigned long length)
1078 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1079 memcpy(destination, source, length);
1082 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1084 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1085 memset(buffer, value, size);
1088 static efi_status_t EFIAPI efi_open_protocol(
1089 void *handle, efi_guid_t *protocol,
1090 void **protocol_interface, void *agent_handle,
1091 void *controller_handle, uint32_t attributes)
1093 struct list_head *lhandle;
1095 efi_status_t r = EFI_INVALID_PARAMETER;
1097 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1098 protocol_interface, agent_handle, controller_handle,
1101 if (!handle || !protocol ||
1102 (!protocol_interface && attributes !=
1103 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1107 switch (attributes) {
1108 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1109 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1110 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1112 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1113 if (controller_handle == handle)
1115 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1116 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1117 if (controller_handle == NULL)
1119 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1120 if (agent_handle == NULL)
1127 list_for_each(lhandle, &efi_obj_list) {
1128 struct efi_object *efiobj;
1129 efiobj = list_entry(lhandle, struct efi_object, link);
1131 if (efiobj->handle != handle)
1134 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1135 struct efi_handler *handler = &efiobj->protocols[i];
1136 const efi_guid_t *hprotocol = handler->guid;
1139 if (!guidcmp(hprotocol, protocol)) {
1141 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1142 *protocol_interface =
1143 handler->protocol_interface;
1153 r = EFI_UNSUPPORTED;
1158 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1159 efi_guid_t *protocol,
1160 void **protocol_interface)
1162 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1163 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1166 static const struct efi_boot_services efi_boot_services = {
1168 .headersize = sizeof(struct efi_table_hdr),
1170 .raise_tpl = efi_raise_tpl,
1171 .restore_tpl = efi_restore_tpl,
1172 .allocate_pages = efi_allocate_pages_ext,
1173 .free_pages = efi_free_pages_ext,
1174 .get_memory_map = efi_get_memory_map_ext,
1175 .allocate_pool = efi_allocate_pool_ext,
1176 .free_pool = efi_free_pool_ext,
1177 .create_event = efi_create_event_ext,
1178 .set_timer = efi_set_timer_ext,
1179 .wait_for_event = efi_wait_for_event,
1180 .signal_event = efi_signal_event_ext,
1181 .close_event = efi_close_event,
1182 .check_event = efi_check_event,
1183 .install_protocol_interface = efi_install_protocol_interface_ext,
1184 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1185 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1186 .handle_protocol = efi_handle_protocol,
1188 .register_protocol_notify = efi_register_protocol_notify,
1189 .locate_handle = efi_locate_handle_ext,
1190 .locate_device_path = efi_locate_device_path,
1191 .install_configuration_table = efi_install_configuration_table_ext,
1192 .load_image = efi_load_image,
1193 .start_image = efi_start_image,
1195 .unload_image = efi_unload_image,
1196 .exit_boot_services = efi_exit_boot_services,
1197 .get_next_monotonic_count = efi_get_next_monotonic_count,
1199 .set_watchdog_timer = efi_set_watchdog_timer,
1200 .connect_controller = efi_connect_controller,
1201 .disconnect_controller = efi_disconnect_controller,
1202 .open_protocol = efi_open_protocol,
1203 .close_protocol = efi_close_protocol,
1204 .open_protocol_information = efi_open_protocol_information,
1205 .protocols_per_handle = efi_protocols_per_handle,
1206 .locate_handle_buffer = efi_locate_handle_buffer,
1207 .locate_protocol = efi_locate_protocol,
1208 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1209 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1210 .calculate_crc32 = efi_calculate_crc32,
1211 .copy_mem = efi_copy_mem,
1212 .set_mem = efi_set_mem,
1216 static uint16_t __efi_runtime_data firmware_vendor[] =
1217 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1219 struct efi_system_table __efi_runtime_data systab = {
1221 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1222 .revision = 0x20005, /* 2.5 */
1223 .headersize = sizeof(struct efi_table_hdr),
1225 .fw_vendor = (long)firmware_vendor,
1226 .con_in = (void*)&efi_con_in,
1227 .con_out = (void*)&efi_con_out,
1228 .std_err = (void*)&efi_con_out,
1229 .runtime = (void*)&efi_runtime_services,
1230 .boottime = (void*)&efi_boot_services,
1232 .tables = (void*)efi_conf_table,