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_EXIT(EFI_SUCCESS);
124 event->notify_function(event, event->notify_context);
125 EFI_ENTRY("returning from notification function");
129 static efi_status_t efi_unsupported(const char *funcname)
131 debug("EFI: App called into unimplemented function %s\n", funcname);
132 return EFI_EXIT(EFI_UNSUPPORTED);
135 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
137 EFI_ENTRY("0x%zx", new_tpl);
141 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
143 EFI_ENTRY("0x%zx", old_tpl);
144 EFI_EXIT(efi_unsupported(__func__));
147 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
153 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
154 r = efi_allocate_pages(type, memory_type, pages, memory);
158 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
163 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
164 r = efi_free_pages(memory, pages);
168 static efi_status_t EFIAPI efi_get_memory_map_ext(
169 unsigned long *memory_map_size,
170 struct efi_mem_desc *memory_map,
171 unsigned long *map_key,
172 unsigned long *descriptor_size,
173 uint32_t *descriptor_version)
177 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
178 map_key, descriptor_size, descriptor_version);
179 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
180 descriptor_size, descriptor_version);
184 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
190 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
191 r = efi_allocate_pool(pool_type, size, buffer);
195 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
199 EFI_ENTRY("%p", buffer);
200 r = efi_free_pool(buffer);
205 * Our event capabilities are very limited. Only a small limited
206 * number of events is allowed to coexist.
208 static struct efi_event efi_events[16];
210 efi_status_t efi_create_event(enum efi_event_type type, UINTN notify_tpl,
211 void (EFIAPI *notify_function) (
212 struct efi_event *event,
214 void *notify_context, struct efi_event **event)
219 return EFI_INVALID_PARAMETER;
221 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
222 return EFI_INVALID_PARAMETER;
224 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
225 notify_function == NULL)
226 return EFI_INVALID_PARAMETER;
228 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
229 if (efi_events[i].type)
231 efi_events[i].type = type;
232 efi_events[i].notify_tpl = notify_tpl;
233 efi_events[i].notify_function = notify_function;
234 efi_events[i].notify_context = notify_context;
235 /* Disable timers on bootup */
236 efi_events[i].trigger_next = -1ULL;
237 efi_events[i].signaled = 0;
238 *event = &efi_events[i];
241 return EFI_OUT_OF_RESOURCES;
244 static efi_status_t EFIAPI efi_create_event_ext(
245 enum efi_event_type type, UINTN notify_tpl,
246 void (EFIAPI *notify_function) (
247 struct efi_event *event,
249 void *notify_context, struct efi_event **event)
251 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
253 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
254 notify_context, event));
259 * Our timers have to work without interrupts, so we check whenever keyboard
260 * input or disk accesses happen if enough time elapsed for it to fire.
262 void efi_timer_check(void)
265 u64 now = timer_get_us();
267 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
268 if (!efi_events[i].type ||
269 !(efi_events[i].type & EVT_TIMER) ||
270 efi_events[i].trigger_type == EFI_TIMER_STOP ||
271 now < efi_events[i].trigger_next)
273 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
274 efi_events[i].trigger_next +=
275 efi_events[i].trigger_time;
276 efi_events[i].signaled = 0;
278 efi_signal_event(&efi_events[i]);
283 efi_status_t efi_set_timer(struct efi_event *event, int type,
284 uint64_t trigger_time)
289 * The parameter defines a multiple of 100ns.
290 * We use multiples of 1000ns. So divide by 10.
292 trigger_time = efi_div10(trigger_time);
294 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
295 if (event != &efi_events[i])
298 if (!(event->type & EVT_TIMER))
302 event->trigger_next = -1ULL;
304 case EFI_TIMER_PERIODIC:
305 case EFI_TIMER_RELATIVE:
306 event->trigger_next =
307 timer_get_us() + trigger_time;
310 return EFI_INVALID_PARAMETER;
312 event->trigger_type = type;
313 event->trigger_time = trigger_time;
316 return EFI_INVALID_PARAMETER;
319 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, int type,
320 uint64_t trigger_time)
322 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
323 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
326 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
327 struct efi_event **event,
328 unsigned long *index)
332 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
334 /* Check parameters */
335 if (!num_events || !event)
336 return EFI_EXIT(EFI_INVALID_PARAMETER);
337 for (i = 0; i < num_events; ++i) {
338 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
339 if (event[i] == &efi_events[j])
342 return EFI_EXIT(EFI_INVALID_PARAMETER);
344 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
345 return EFI_EXIT(EFI_INVALID_PARAMETER);
348 /* Wait for signal */
350 for (i = 0; i < num_events; ++i) {
351 if (event[i]->signaled)
354 /* Allow events to occur. */
360 * Reset the signal which is passed to the caller to allow periodic
363 event[i]->signaled = 0;
367 return EFI_EXIT(EFI_SUCCESS);
370 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
374 EFI_ENTRY("%p", event);
375 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
376 if (event != &efi_events[i])
378 efi_signal_event(event);
381 return EFI_EXIT(EFI_SUCCESS);
384 static efi_status_t EFIAPI efi_close_event(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 event->trigger_next = -1ULL;
394 return EFI_EXIT(EFI_SUCCESS);
397 return EFI_EXIT(EFI_INVALID_PARAMETER);
400 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
404 EFI_ENTRY("%p", event);
406 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
407 if (event != &efi_events[i])
409 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
412 return EFI_EXIT(EFI_SUCCESS);
413 return EFI_EXIT(EFI_NOT_READY);
415 return EFI_EXIT(EFI_INVALID_PARAMETER);
418 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
419 efi_guid_t *protocol, int protocol_interface_type,
420 void *protocol_interface)
422 struct list_head *lhandle;
426 if (!handle || !protocol ||
427 protocol_interface_type != EFI_NATIVE_INTERFACE) {
428 r = EFI_INVALID_PARAMETER;
432 /* Create new handle if requested. */
434 r = EFI_OUT_OF_RESOURCES;
438 list_for_each(lhandle, &efi_obj_list) {
439 struct efi_object *efiobj;
440 efiobj = list_entry(lhandle, struct efi_object, link);
442 if (efiobj->handle != *handle)
444 /* Check if protocol is already installed on the handle. */
445 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
446 struct efi_handler *handler = &efiobj->protocols[i];
450 if (!guidcmp(handler->guid, protocol)) {
451 r = EFI_INVALID_PARAMETER;
455 /* Install protocol in first empty slot. */
456 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
457 struct efi_handler *handler = &efiobj->protocols[i];
462 handler->guid = protocol;
463 handler->protocol_interface = protocol_interface;
467 r = EFI_OUT_OF_RESOURCES;
470 r = EFI_INVALID_PARAMETER;
475 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
476 efi_guid_t *protocol, int protocol_interface_type,
477 void *protocol_interface)
479 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
482 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
483 protocol_interface_type,
484 protocol_interface));
487 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
488 efi_guid_t *protocol, void *old_interface,
491 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
493 return EFI_EXIT(EFI_ACCESS_DENIED);
496 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
497 efi_guid_t *protocol, void *protocol_interface)
499 struct list_head *lhandle;
501 efi_status_t r = EFI_NOT_FOUND;
503 if (!handle || !protocol) {
504 r = EFI_INVALID_PARAMETER;
508 list_for_each(lhandle, &efi_obj_list) {
509 struct efi_object *efiobj;
510 efiobj = list_entry(lhandle, struct efi_object, link);
512 if (efiobj->handle != handle)
515 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
516 struct efi_handler *handler = &efiobj->protocols[i];
517 const efi_guid_t *hprotocol = handler->guid;
521 if (!guidcmp(hprotocol, protocol)) {
522 if (handler->protocol_interface) {
523 r = EFI_ACCESS_DENIED;
537 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
538 efi_guid_t *protocol, void *protocol_interface)
540 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
542 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
543 protocol_interface));
546 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
547 struct efi_event *event,
550 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
551 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
554 static int efi_search(enum efi_locate_search_type search_type,
555 efi_guid_t *protocol, void *search_key,
556 struct efi_object *efiobj)
560 switch (search_type) {
563 case by_register_notify:
566 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
567 const efi_guid_t *guid = efiobj->protocols[i].guid;
568 if (guid && !guidcmp(guid, protocol))
577 static efi_status_t EFIAPI efi_locate_handle(
578 enum efi_locate_search_type search_type,
579 efi_guid_t *protocol, void *search_key,
580 unsigned long *buffer_size, efi_handle_t *buffer)
582 struct list_head *lhandle;
583 unsigned long size = 0;
585 /* Count how much space we need */
586 list_for_each(lhandle, &efi_obj_list) {
587 struct efi_object *efiobj;
588 efiobj = list_entry(lhandle, struct efi_object, link);
589 if (!efi_search(search_type, protocol, search_key, efiobj)) {
590 size += sizeof(void*);
594 if (*buffer_size < size) {
596 return EFI_BUFFER_TOO_SMALL;
599 /* Then fill the array */
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 *(buffer++) = efiobj->handle;
612 static efi_status_t EFIAPI efi_locate_handle_ext(
613 enum efi_locate_search_type search_type,
614 efi_guid_t *protocol, void *search_key,
615 unsigned long *buffer_size, efi_handle_t *buffer)
617 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
618 buffer_size, buffer);
620 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
621 buffer_size, buffer));
624 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
625 struct efi_device_path **device_path,
626 efi_handle_t *device)
628 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
629 return EFI_EXIT(EFI_NOT_FOUND);
632 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
636 /* Check for guid override */
637 for (i = 0; i < systab.nr_tables; i++) {
638 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
639 efi_conf_table[i].table = table;
644 /* No override, check for overflow */
645 if (i >= ARRAY_SIZE(efi_conf_table))
646 return EFI_OUT_OF_RESOURCES;
648 /* Add a new entry */
649 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
650 efi_conf_table[i].table = table;
651 systab.nr_tables = i + 1;
656 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
659 EFI_ENTRY("%p, %p", guid, table);
660 return EFI_EXIT(efi_install_configuration_table(guid, table));
663 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
664 efi_handle_t parent_image,
665 struct efi_device_path *file_path,
667 unsigned long source_size,
668 efi_handle_t *image_handle)
670 static struct efi_object loaded_image_info_obj = {
673 .guid = &efi_guid_loaded_image,
677 struct efi_loaded_image *info;
678 struct efi_object *obj;
680 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
681 file_path, source_buffer, source_size, image_handle);
682 info = malloc(sizeof(*info));
683 loaded_image_info_obj.protocols[0].protocol_interface = info;
684 obj = malloc(sizeof(loaded_image_info_obj));
685 memset(info, 0, sizeof(*info));
686 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
688 info->file_path = file_path;
689 info->reserved = efi_load_pe(source_buffer, info);
690 if (!info->reserved) {
693 return EFI_EXIT(EFI_UNSUPPORTED);
696 *image_handle = info;
697 list_add_tail(&obj->link, &efi_obj_list);
699 return EFI_EXIT(EFI_SUCCESS);
702 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
703 unsigned long *exit_data_size,
706 ulong (*entry)(void *image_handle, struct efi_system_table *st);
707 struct efi_loaded_image *info = image_handle;
709 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
710 entry = info->reserved;
712 efi_is_direct_boot = false;
714 /* call the image! */
715 if (setjmp(&info->exit_jmp)) {
716 /* We returned from the child image */
717 return EFI_EXIT(info->exit_status);
720 entry(image_handle, &systab);
722 /* Should usually never get here */
723 return EFI_EXIT(EFI_SUCCESS);
726 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
727 efi_status_t exit_status, unsigned long exit_data_size,
730 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
732 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
733 exit_data_size, exit_data);
735 loaded_image_info->exit_status = exit_status;
736 longjmp(&loaded_image_info->exit_jmp, 1);
738 panic("EFI application exited");
741 static struct efi_object *efi_search_obj(void *handle)
743 struct list_head *lhandle;
745 list_for_each(lhandle, &efi_obj_list) {
746 struct efi_object *efiobj;
747 efiobj = list_entry(lhandle, struct efi_object, link);
748 if (efiobj->handle == handle)
755 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
757 struct efi_object *efiobj;
759 EFI_ENTRY("%p", image_handle);
760 efiobj = efi_search_obj(image_handle);
762 list_del(&efiobj->link);
764 return EFI_EXIT(EFI_SUCCESS);
767 static void efi_exit_caches(void)
769 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
771 * Grub on 32bit ARM needs to have caches disabled before jumping into
772 * a zImage, but does not know of all cache layers. Give it a hand.
774 if (efi_is_direct_boot)
775 cleanup_before_linux();
779 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
780 unsigned long map_key)
782 EFI_ENTRY("%p, %ld", image_handle, map_key);
784 board_quiesce_devices();
786 /* Fix up caches for EFI payloads if necessary */
789 /* This stops all lingering devices */
790 bootm_disable_interrupts();
792 /* Give the payload some time to boot */
795 return EFI_EXIT(EFI_SUCCESS);
798 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
800 static uint64_t mono = 0;
801 EFI_ENTRY("%p", count);
803 return EFI_EXIT(EFI_SUCCESS);
806 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
808 EFI_ENTRY("%ld", microseconds);
809 udelay(microseconds);
810 return EFI_EXIT(EFI_SUCCESS);
813 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
814 uint64_t watchdog_code,
815 unsigned long data_size,
816 uint16_t *watchdog_data)
818 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
819 data_size, watchdog_data);
820 return EFI_EXIT(efi_unsupported(__func__));
823 static efi_status_t EFIAPI efi_connect_controller(
824 efi_handle_t controller_handle,
825 efi_handle_t *driver_image_handle,
826 struct efi_device_path *remain_device_path,
829 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
830 remain_device_path, recursive);
831 return EFI_EXIT(EFI_NOT_FOUND);
834 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
835 void *driver_image_handle,
838 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
840 return EFI_EXIT(EFI_INVALID_PARAMETER);
843 static efi_status_t EFIAPI efi_close_protocol(void *handle,
844 efi_guid_t *protocol,
846 void *controller_handle)
848 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
850 return EFI_EXIT(EFI_NOT_FOUND);
853 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
854 efi_guid_t *protocol,
855 struct efi_open_protocol_info_entry **entry_buffer,
856 unsigned long *entry_count)
858 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
860 return EFI_EXIT(EFI_NOT_FOUND);
863 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
864 efi_guid_t ***protocol_buffer,
865 unsigned long *protocol_buffer_count)
867 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
868 protocol_buffer_count);
869 *protocol_buffer_count = 0;
870 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
873 static efi_status_t EFIAPI efi_locate_handle_buffer(
874 enum efi_locate_search_type search_type,
875 efi_guid_t *protocol, void *search_key,
876 unsigned long *no_handles, efi_handle_t **buffer)
879 unsigned long buffer_size = 0;
881 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
884 if (!no_handles || !buffer) {
885 r = EFI_INVALID_PARAMETER;
890 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
892 if (r != EFI_BUFFER_TOO_SMALL)
894 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
896 if (r != EFI_SUCCESS)
898 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
900 if (r == EFI_SUCCESS)
901 *no_handles = buffer_size / sizeof(void *);
906 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
908 void **protocol_interface)
910 struct list_head *lhandle;
913 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
915 if (!protocol || !protocol_interface)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 list_for_each(lhandle, &efi_obj_list) {
919 struct efi_object *efiobj;
921 efiobj = list_entry(lhandle, struct efi_object, link);
922 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
923 struct efi_handler *handler = &efiobj->protocols[i];
927 if (!guidcmp(handler->guid, protocol)) {
928 *protocol_interface =
929 handler->protocol_interface;
930 return EFI_EXIT(EFI_SUCCESS);
934 *protocol_interface = NULL;
936 return EFI_EXIT(EFI_NOT_FOUND);
939 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
942 EFI_ENTRY("%p", handle);
945 efi_guid_t *protocol;
946 void *protocol_interface;
947 efi_status_t r = EFI_SUCCESS;
951 return EFI_EXIT(EFI_INVALID_PARAMETER);
953 va_start(argptr, handle);
955 protocol = va_arg(argptr, efi_guid_t*);
958 protocol_interface = va_arg(argptr, void*);
959 r = efi_install_protocol_interface(handle, protocol,
960 EFI_NATIVE_INTERFACE,
962 if (r != EFI_SUCCESS)
967 if (r == EFI_SUCCESS)
970 /* If an error occured undo all changes. */
971 va_start(argptr, handle);
973 protocol = va_arg(argptr, efi_guid_t*);
974 protocol_interface = va_arg(argptr, void*);
975 efi_uninstall_protocol_interface(handle, protocol,
983 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
986 EFI_ENTRY("%p", handle);
987 return EFI_EXIT(EFI_INVALID_PARAMETER);
990 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
991 unsigned long data_size,
994 EFI_ENTRY("%p, %ld", data, data_size);
995 *crc32_p = crc32(0, data, data_size);
996 return EFI_EXIT(EFI_SUCCESS);
999 static void EFIAPI efi_copy_mem(void *destination, void *source,
1000 unsigned long length)
1002 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1003 memcpy(destination, source, length);
1006 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1008 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1009 memset(buffer, value, size);
1012 static efi_status_t EFIAPI efi_open_protocol(
1013 void *handle, efi_guid_t *protocol,
1014 void **protocol_interface, void *agent_handle,
1015 void *controller_handle, uint32_t attributes)
1017 struct list_head *lhandle;
1019 efi_status_t r = EFI_INVALID_PARAMETER;
1021 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1022 protocol_interface, agent_handle, controller_handle,
1025 if (!handle || !protocol ||
1026 (!protocol_interface && attributes !=
1027 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1031 switch (attributes) {
1032 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1033 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1034 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1036 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1037 if (controller_handle == handle)
1039 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1040 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1041 if (controller_handle == NULL)
1043 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1044 if (agent_handle == NULL)
1051 list_for_each(lhandle, &efi_obj_list) {
1052 struct efi_object *efiobj;
1053 efiobj = list_entry(lhandle, struct efi_object, link);
1055 if (efiobj->handle != handle)
1058 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1059 struct efi_handler *handler = &efiobj->protocols[i];
1060 const efi_guid_t *hprotocol = handler->guid;
1063 if (!guidcmp(hprotocol, protocol)) {
1065 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1066 *protocol_interface =
1067 handler->protocol_interface;
1077 r = EFI_UNSUPPORTED;
1082 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1083 efi_guid_t *protocol,
1084 void **protocol_interface)
1086 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1087 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1090 static const struct efi_boot_services efi_boot_services = {
1092 .headersize = sizeof(struct efi_table_hdr),
1094 .raise_tpl = efi_raise_tpl,
1095 .restore_tpl = efi_restore_tpl,
1096 .allocate_pages = efi_allocate_pages_ext,
1097 .free_pages = efi_free_pages_ext,
1098 .get_memory_map = efi_get_memory_map_ext,
1099 .allocate_pool = efi_allocate_pool_ext,
1100 .free_pool = efi_free_pool_ext,
1101 .create_event = efi_create_event_ext,
1102 .set_timer = efi_set_timer_ext,
1103 .wait_for_event = efi_wait_for_event,
1104 .signal_event = efi_signal_event_ext,
1105 .close_event = efi_close_event,
1106 .check_event = efi_check_event,
1107 .install_protocol_interface = efi_install_protocol_interface_ext,
1108 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1109 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1110 .handle_protocol = efi_handle_protocol,
1112 .register_protocol_notify = efi_register_protocol_notify,
1113 .locate_handle = efi_locate_handle_ext,
1114 .locate_device_path = efi_locate_device_path,
1115 .install_configuration_table = efi_install_configuration_table_ext,
1116 .load_image = efi_load_image,
1117 .start_image = efi_start_image,
1119 .unload_image = efi_unload_image,
1120 .exit_boot_services = efi_exit_boot_services,
1121 .get_next_monotonic_count = efi_get_next_monotonic_count,
1123 .set_watchdog_timer = efi_set_watchdog_timer,
1124 .connect_controller = efi_connect_controller,
1125 .disconnect_controller = efi_disconnect_controller,
1126 .open_protocol = efi_open_protocol,
1127 .close_protocol = efi_close_protocol,
1128 .open_protocol_information = efi_open_protocol_information,
1129 .protocols_per_handle = efi_protocols_per_handle,
1130 .locate_handle_buffer = efi_locate_handle_buffer,
1131 .locate_protocol = efi_locate_protocol,
1132 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1133 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1134 .calculate_crc32 = efi_calculate_crc32,
1135 .copy_mem = efi_copy_mem,
1136 .set_mem = efi_set_mem,
1140 static uint16_t __efi_runtime_data firmware_vendor[] =
1141 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1143 struct efi_system_table __efi_runtime_data systab = {
1145 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1146 .revision = 0x20005, /* 2.5 */
1147 .headersize = sizeof(struct efi_table_hdr),
1149 .fw_vendor = (long)firmware_vendor,
1150 .con_in = (void*)&efi_con_in,
1151 .con_out = (void*)&efi_con_out,
1152 .std_err = (void*)&efi_con_out,
1153 .runtime = (void*)&efi_runtime_services,
1154 .boottime = (void*)&efi_boot_services,
1156 .tables = (void*)efi_conf_table,