2 * EFI application boot time services
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
12 #include <environment.h>
14 #include <asm/global_data.h>
15 #include <libfdt_env.h>
16 #include <u-boot/crc.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* Task priority level */
24 static efi_uintn_t efi_tpl = TPL_APPLICATION;
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list);
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
35 static bool efi_is_direct_boot = true;
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
54 static volatile void *efi_gd, *app_gd;
57 static int entry_count;
58 static int nesting_level;
60 /* Called on every callback entry */
61 int __efi_entry_check(void)
63 int ret = entry_count++ == 0;
72 /* Called on every callback exit */
73 int __efi_exit_check(void)
75 int ret = --entry_count == 0;
82 /* Called from do_bootefi_exec() */
83 void efi_save_gd(void)
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
95 void efi_restore_gd(void)
98 /* Only restore if we're already in EFI context */
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
109 static const char *indent_string(int level)
111 const char *indent = " ";
112 const int max = strlen(indent);
113 level = min(max, level * 2);
114 return &indent[max - level];
117 const char *__efi_nesting(void)
119 return indent_string(nesting_level);
122 const char *__efi_nesting_inc(void)
124 return indent_string(nesting_level++);
127 const char *__efi_nesting_dec(void)
129 return indent_string(--nesting_level);
133 * Queue an EFI event.
135 * This function queues the notification function of the event for future
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
141 * For the SignalEvent service see efi_signal_event_ext.
143 * @event event to signal
145 void efi_signal_event(struct efi_event *event)
147 if (event->notify_function) {
148 event->is_queued = true;
150 if (efi_tpl >= event->notify_tpl)
152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
155 event->is_queued = false;
159 * Raise the task priority level.
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
168 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
170 efi_uintn_t old_tpl = efi_tpl;
172 EFI_ENTRY("0x%zx", new_tpl);
174 if (new_tpl < efi_tpl)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
177 if (efi_tpl > TPL_HIGH_LEVEL)
178 efi_tpl = TPL_HIGH_LEVEL;
180 EFI_EXIT(EFI_SUCCESS);
185 * Lower the task priority level.
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
191 * @old_tpl value of the task priority level to be restored
193 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
195 EFI_ENTRY("0x%zx", old_tpl);
197 if (old_tpl > efi_tpl)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
200 if (efi_tpl > TPL_HIGH_LEVEL)
201 efi_tpl = TPL_HIGH_LEVEL;
203 EFI_EXIT(EFI_SUCCESS);
207 * Allocate memory pages.
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
219 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
226 r = efi_allocate_pages(type, memory_type, pages, memory);
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
241 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
247 r = efi_free_pages(memory, pages);
252 * Get map describing memory usage.
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
266 static efi_status_t EFIAPI efi_get_memory_map_ext(
267 efi_uintn_t *memory_map_size,
268 struct efi_mem_desc *memory_map,
269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
271 uint32_t *descriptor_version)
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 map_key, descriptor_size, descriptor_version);
277 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 descriptor_size, descriptor_version);
283 * Allocate memory from pool.
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
294 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
301 r = efi_allocate_pool(pool_type, size, buffer);
306 * Free memory from pool.
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
312 * @buffer start of memory to be freed
313 * @return status code
315 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
319 EFI_ENTRY("%p", buffer);
320 r = efi_free_pool(buffer);
328 * @return status code
330 efi_status_t efi_create_handle(void **handle)
332 struct efi_object *obj;
335 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
336 sizeof(struct efi_object),
338 if (r != EFI_SUCCESS)
340 memset(obj, 0, sizeof(struct efi_object));
342 list_add_tail(&obj->link, &efi_obj_list);
348 * Our event capabilities are very limited. Only a small limited
349 * number of events is allowed to coexist.
351 static struct efi_event efi_events[16];
356 * This function is used inside U-Boot code to create an event.
358 * For the API function implementing the CreateEvent service see
359 * efi_create_event_ext.
361 * @type type of the event to create
362 * @notify_tpl task priority level of the event
363 * @notify_function notification function of the event
364 * @notify_context pointer passed to the notification function
365 * @event created event
366 * @return status code
368 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
369 void (EFIAPI *notify_function) (
370 struct efi_event *event,
372 void *notify_context, struct efi_event **event)
377 return EFI_INVALID_PARAMETER;
379 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
380 return EFI_INVALID_PARAMETER;
382 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
383 notify_function == NULL)
384 return EFI_INVALID_PARAMETER;
386 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
387 if (efi_events[i].type)
389 efi_events[i].type = type;
390 efi_events[i].notify_tpl = notify_tpl;
391 efi_events[i].notify_function = notify_function;
392 efi_events[i].notify_context = notify_context;
393 /* Disable timers on bootup */
394 efi_events[i].trigger_next = -1ULL;
395 efi_events[i].is_queued = false;
396 efi_events[i].is_signaled = false;
397 *event = &efi_events[i];
400 return EFI_OUT_OF_RESOURCES;
406 * This function implements the CreateEvent service.
407 * See the Unified Extensible Firmware Interface (UEFI) specification
410 * @type type of the event to create
411 * @notify_tpl task priority level of the event
412 * @notify_function notification function of the event
413 * @notify_context pointer passed to the notification function
414 * @event created event
415 * @return status code
417 static efi_status_t EFIAPI efi_create_event_ext(
418 uint32_t type, efi_uintn_t notify_tpl,
419 void (EFIAPI *notify_function) (
420 struct efi_event *event,
422 void *notify_context, struct efi_event **event)
424 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
426 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
427 notify_context, event));
432 * Check if a timer event has occurred or a queued notification function should
435 * Our timers have to work without interrupts, so we check whenever keyboard
436 * input or disk accesses happen if enough time elapsed for them to fire.
438 void efi_timer_check(void)
441 u64 now = timer_get_us();
443 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
444 if (!efi_events[i].type)
446 if (efi_events[i].is_queued)
447 efi_signal_event(&efi_events[i]);
448 if (!(efi_events[i].type & EVT_TIMER) ||
449 now < efi_events[i].trigger_next)
451 switch (efi_events[i].trigger_type) {
452 case EFI_TIMER_RELATIVE:
453 efi_events[i].trigger_type = EFI_TIMER_STOP;
455 case EFI_TIMER_PERIODIC:
456 efi_events[i].trigger_next +=
457 efi_events[i].trigger_time;
462 efi_events[i].is_signaled = true;
463 efi_signal_event(&efi_events[i]);
469 * Set the trigger time for a timer event or stop the event.
471 * This is the function for internal usage in U-Boot. For the API function
472 * implementing the SetTimer service see efi_set_timer_ext.
474 * @event event for which the timer is set
475 * @type type of the timer
476 * @trigger_time trigger period in multiples of 100ns
477 * @return status code
479 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
480 uint64_t trigger_time)
485 * The parameter defines a multiple of 100ns.
486 * We use multiples of 1000ns. So divide by 10.
488 do_div(trigger_time, 10);
490 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
491 if (event != &efi_events[i])
494 if (!(event->type & EVT_TIMER))
498 event->trigger_next = -1ULL;
500 case EFI_TIMER_PERIODIC:
501 case EFI_TIMER_RELATIVE:
502 event->trigger_next =
503 timer_get_us() + trigger_time;
506 return EFI_INVALID_PARAMETER;
508 event->trigger_type = type;
509 event->trigger_time = trigger_time;
510 event->is_signaled = false;
513 return EFI_INVALID_PARAMETER;
517 * Set the trigger time for a timer event or stop the event.
519 * This function implements the SetTimer service.
520 * See the Unified Extensible Firmware Interface (UEFI) specification
523 * @event event for which the timer is set
524 * @type type of the timer
525 * @trigger_time trigger period in multiples of 100ns
526 * @return status code
528 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
529 enum efi_timer_delay type,
530 uint64_t trigger_time)
532 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
533 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
537 * Wait for events to be signaled.
539 * This function implements the WaitForEvent service.
540 * See the Unified Extensible Firmware Interface (UEFI) specification
543 * @num_events number of events to be waited for
544 * @events events to be waited for
545 * @index index of the event that was signaled
546 * @return status code
548 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
549 struct efi_event **event,
554 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
556 /* Check parameters */
557 if (!num_events || !event)
558 return EFI_EXIT(EFI_INVALID_PARAMETER);
560 if (efi_tpl != TPL_APPLICATION)
561 return EFI_EXIT(EFI_UNSUPPORTED);
562 for (i = 0; i < num_events; ++i) {
563 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
564 if (event[i] == &efi_events[j])
567 return EFI_EXIT(EFI_INVALID_PARAMETER);
569 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
570 return EFI_EXIT(EFI_INVALID_PARAMETER);
571 if (!event[i]->is_signaled)
572 efi_signal_event(event[i]);
575 /* Wait for signal */
577 for (i = 0; i < num_events; ++i) {
578 if (event[i]->is_signaled)
581 /* Allow events to occur. */
587 * Reset the signal which is passed to the caller to allow periodic
590 event[i]->is_signaled = false;
594 return EFI_EXIT(EFI_SUCCESS);
598 * Signal an EFI event.
600 * This function implements the SignalEvent service.
601 * See the Unified Extensible Firmware Interface (UEFI) specification
604 * This functions sets the signaled state of the event and queues the
605 * notification function for execution.
607 * @event event to signal
608 * @return status code
610 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
614 EFI_ENTRY("%p", event);
615 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
616 if (event != &efi_events[i])
618 if (event->is_signaled)
620 event->is_signaled = true;
621 if (event->type & EVT_NOTIFY_SIGNAL)
622 efi_signal_event(event);
625 return EFI_EXIT(EFI_SUCCESS);
629 * Close an EFI event.
631 * This function implements the CloseEvent service.
632 * See the Unified Extensible Firmware Interface (UEFI) specification
635 * @event event to close
636 * @return status code
638 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
642 EFI_ENTRY("%p", event);
643 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
644 if (event == &efi_events[i]) {
646 event->trigger_next = -1ULL;
647 event->is_queued = false;
648 event->is_signaled = false;
649 return EFI_EXIT(EFI_SUCCESS);
652 return EFI_EXIT(EFI_INVALID_PARAMETER);
656 * Check if an event is signaled.
658 * This function implements the CheckEvent service.
659 * See the Unified Extensible Firmware Interface (UEFI) specification
662 * If an event is not signaled yet the notification function is queued.
664 * @event event to check
665 * @return status code
667 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
671 EFI_ENTRY("%p", event);
673 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
674 if (event != &efi_events[i])
676 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
678 if (!event->is_signaled)
679 efi_signal_event(event);
680 if (event->is_signaled)
681 return EFI_EXIT(EFI_SUCCESS);
682 return EFI_EXIT(EFI_NOT_READY);
684 return EFI_EXIT(EFI_INVALID_PARAMETER);
688 * Find the internal EFI object for a handle.
690 * @handle handle to find
693 struct efi_object *efi_search_obj(const void *handle)
695 struct efi_object *efiobj;
697 list_for_each_entry(efiobj, &efi_obj_list, link) {
698 if (efiobj->handle == handle)
706 * Find a protocol on a handle.
709 * @protocol_guid GUID of the protocol
710 * @handler reference to the protocol
711 * @return status code
713 efi_status_t efi_search_protocol(const void *handle,
714 const efi_guid_t *protocol_guid,
715 struct efi_handler **handler)
717 struct efi_object *efiobj;
719 struct efi_handler *protocol;
721 if (!handle || !protocol_guid)
722 return EFI_INVALID_PARAMETER;
723 efiobj = efi_search_obj(handle);
725 return EFI_INVALID_PARAMETER;
726 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
727 protocol = &efiobj->protocols[i];
730 if (!guidcmp(protocol->guid, protocol_guid)) {
736 return EFI_NOT_FOUND;
740 * Install new protocol on a handle.
742 * @handle handle on which the protocol shall be installed
743 * @protocol GUID of the protocol to be installed
744 * @protocol_interface interface of the protocol implementation
745 * @return status code
747 efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
748 void *protocol_interface)
750 struct efi_object *efiobj;
751 struct efi_handler *handler;
755 efiobj = efi_search_obj(handle);
757 return EFI_INVALID_PARAMETER;
758 ret = efi_search_protocol(handle, protocol, NULL);
759 if (ret != EFI_NOT_FOUND)
760 return EFI_INVALID_PARAMETER;
761 handler = calloc(1, sizeof(struct efi_handler));
763 return EFI_OUT_OF_RESOURCES;
764 /* Install protocol in first empty slot. */
765 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
766 handler = &efiobj->protocols[i];
769 handler->guid = protocol;
770 handler->protocol_interface = protocol_interface;
773 return EFI_OUT_OF_RESOURCES;
777 * Delete protocol from a handle.
779 * @handle handle from which the protocol shall be deleted
780 * @protocol GUID of the protocol to be deleted
781 * @protocol_interface interface of the protocol implementation
782 * @return status code
784 efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
785 void *protocol_interface)
787 struct efi_handler *handler;
790 ret = efi_search_protocol(handle, protocol, &handler);
791 if (ret != EFI_SUCCESS)
793 if (handler->protocol_interface != protocol_interface)
794 return EFI_NOT_FOUND;
795 handler->guid = NULL;
796 handler->protocol_interface = NULL;
801 * Delete all protocols from a handle.
803 * @handle handle from which the protocols shall be deleted
804 * @return status code
806 efi_status_t efi_remove_all_protocols(const void *handle)
808 struct efi_object *efiobj;
809 struct efi_handler *handler;
812 efiobj = efi_search_obj(handle);
814 return EFI_INVALID_PARAMETER;
816 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
817 handler = &efiobj->protocols[i];
818 handler->guid = NULL;
819 handler->protocol_interface = NULL;
825 * Install protocol interface.
827 * This function implements the InstallProtocolInterface service.
828 * See the Unified Extensible Firmware Interface (UEFI) specification
831 * @handle handle on which the protocol shall be installed
832 * @protocol GUID of the protocol to be installed
833 * @protocol_interface_type type of the interface to be installed,
834 * always EFI_NATIVE_INTERFACE
835 * @protocol_interface interface of the protocol implementation
836 * @return status code
838 static efi_status_t EFIAPI efi_install_protocol_interface(
839 void **handle, const efi_guid_t *protocol,
840 int protocol_interface_type, void *protocol_interface)
844 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
847 if (!handle || !protocol ||
848 protocol_interface_type != EFI_NATIVE_INTERFACE) {
849 r = EFI_INVALID_PARAMETER;
853 /* Create new handle if requested. */
855 r = efi_create_handle(handle);
856 if (r != EFI_SUCCESS)
858 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
861 debug("%sEFI: handle %p\n", indent_string(nesting_level),
864 /* Add new protocol */
865 r = efi_add_protocol(*handle, protocol, protocol_interface);
871 * Reinstall protocol interface.
873 * This function implements the ReinstallProtocolInterface service.
874 * See the Unified Extensible Firmware Interface (UEFI) specification
877 * @handle handle on which the protocol shall be
879 * @protocol GUID of the protocol to be installed
880 * @old_interface interface to be removed
881 * @new_interface interface to be installed
882 * @return status code
884 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
885 const efi_guid_t *protocol, void *old_interface,
888 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
890 return EFI_EXIT(EFI_ACCESS_DENIED);
894 * Uninstall protocol interface.
896 * This function implements the UninstallProtocolInterface service.
897 * See the Unified Extensible Firmware Interface (UEFI) specification
900 * @handle handle from which the protocol shall be removed
901 * @protocol GUID of the protocol to be removed
902 * @protocol_interface interface to be removed
903 * @return status code
905 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
906 void *handle, const efi_guid_t *protocol,
907 void *protocol_interface)
909 struct efi_handler *handler;
912 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
914 if (!handle || !protocol) {
915 r = EFI_INVALID_PARAMETER;
919 /* Find the protocol on the handle */
920 r = efi_search_protocol(handle, protocol, &handler);
921 if (r != EFI_SUCCESS)
923 if (handler->protocol_interface) {
924 /* TODO disconnect controllers */
925 r = EFI_ACCESS_DENIED;
927 r = efi_remove_protocol(handle, protocol, protocol_interface);
934 * Register an event for notification when a protocol is installed.
936 * This function implements the RegisterProtocolNotify service.
937 * See the Unified Extensible Firmware Interface (UEFI) specification
940 * @protocol GUID of the protocol whose installation shall be
942 * @event event to be signaled upon installation of the protocol
943 * @registration key for retrieving the registration information
944 * @return status code
946 static efi_status_t EFIAPI efi_register_protocol_notify(
947 const efi_guid_t *protocol,
948 struct efi_event *event,
951 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
952 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
956 * Determine if an EFI handle implements a protocol.
958 * See the documentation of the LocateHandle service in the UEFI specification.
960 * @search_type selection criterion
961 * @protocol GUID of the protocol
962 * @search_key registration key
964 * @return 0 if the handle implements the protocol
966 static int efi_search(enum efi_locate_search_type search_type,
967 const efi_guid_t *protocol, void *search_key,
968 struct efi_object *efiobj)
972 switch (search_type) {
975 case BY_REGISTER_NOTIFY:
976 /* TODO: RegisterProtocolNotify is not implemented yet */
979 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
980 return (ret != EFI_SUCCESS);
982 /* Invalid search type */
988 * Locate handles implementing a protocol.
990 * This function is meant for U-Boot internal calls. For the API implementation
991 * of the LocateHandle service see efi_locate_handle_ext.
993 * @search_type selection criterion
994 * @protocol GUID of the protocol
995 * @search_key registration key
996 * @buffer_size size of the buffer to receive the handles in bytes
997 * @buffer buffer to receive the relevant handles
998 * @return status code
1000 static efi_status_t efi_locate_handle(
1001 enum efi_locate_search_type search_type,
1002 const efi_guid_t *protocol, void *search_key,
1003 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1005 struct efi_object *efiobj;
1006 efi_uintn_t size = 0;
1008 /* Check parameters */
1009 switch (search_type) {
1012 case BY_REGISTER_NOTIFY:
1014 return EFI_INVALID_PARAMETER;
1015 /* RegisterProtocolNotify is not implemented yet */
1016 return EFI_UNSUPPORTED;
1019 return EFI_INVALID_PARAMETER;
1022 return EFI_INVALID_PARAMETER;
1026 * efi_locate_handle_buffer uses this function for
1027 * the calculation of the necessary buffer size.
1028 * So do not require a buffer for buffersize == 0.
1030 if (!buffer_size || (*buffer_size && !buffer))
1031 return EFI_INVALID_PARAMETER;
1033 /* Count how much space we need */
1034 list_for_each_entry(efiobj, &efi_obj_list, link) {
1035 if (!efi_search(search_type, protocol, search_key, efiobj))
1036 size += sizeof(void*);
1039 if (*buffer_size < size) {
1040 *buffer_size = size;
1041 return EFI_BUFFER_TOO_SMALL;
1044 *buffer_size = size;
1046 return EFI_NOT_FOUND;
1048 /* Then fill the array */
1049 list_for_each_entry(efiobj, &efi_obj_list, link) {
1050 if (!efi_search(search_type, protocol, search_key, efiobj))
1051 *buffer++ = efiobj->handle;
1058 * Locate handles implementing a protocol.
1060 * This function implements the LocateHandle service.
1061 * See the Unified Extensible Firmware Interface (UEFI) specification
1064 * @search_type selection criterion
1065 * @protocol GUID of the protocol
1066 * @search_key registration key
1067 * @buffer_size size of the buffer to receive the handles in bytes
1068 * @buffer buffer to receive the relevant handles
1069 * @return 0 if the handle implements the protocol
1071 static efi_status_t EFIAPI efi_locate_handle_ext(
1072 enum efi_locate_search_type search_type,
1073 const efi_guid_t *protocol, void *search_key,
1074 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1076 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1077 buffer_size, buffer);
1079 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1080 buffer_size, buffer));
1084 * Get the device path and handle of an device implementing a protocol.
1086 * This function implements the LocateDevicePath service.
1087 * See the Unified Extensible Firmware Interface (UEFI) specification
1090 * @protocol GUID of the protocol
1091 * @device_path device path
1092 * @device handle of the device
1093 * @return status code
1095 static efi_status_t EFIAPI efi_locate_device_path(
1096 const efi_guid_t *protocol,
1097 struct efi_device_path **device_path,
1098 efi_handle_t *device)
1100 struct efi_object *efiobj;
1102 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1104 efiobj = efi_dp_find_obj(*device_path, device_path);
1106 return EFI_EXIT(EFI_NOT_FOUND);
1108 *device = efiobj->handle;
1110 return EFI_EXIT(EFI_SUCCESS);
1113 /* Collapses configuration table entries, removing index i */
1114 static void efi_remove_configuration_table(int i)
1116 struct efi_configuration_table *this = &efi_conf_table[i];
1117 struct efi_configuration_table *next = &efi_conf_table[i+1];
1118 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1120 memmove(this, next, (ulong)end - (ulong)next);
1125 * Adds, updates, or removes a configuration table.
1127 * This function is used for internal calls. For the API implementation of the
1128 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1130 * @guid GUID of the installed table
1131 * @table table to be installed
1132 * @return status code
1134 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1138 /* Check for guid override */
1139 for (i = 0; i < systab.nr_tables; i++) {
1140 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1142 efi_conf_table[i].table = table;
1144 efi_remove_configuration_table(i);
1150 return EFI_NOT_FOUND;
1152 /* No override, check for overflow */
1153 if (i >= ARRAY_SIZE(efi_conf_table))
1154 return EFI_OUT_OF_RESOURCES;
1156 /* Add a new entry */
1157 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1158 efi_conf_table[i].table = table;
1159 systab.nr_tables = i + 1;
1165 * Adds, updates, or removes a configuration table.
1167 * This function implements the InstallConfigurationTable service.
1168 * See the Unified Extensible Firmware Interface (UEFI) specification
1171 * @guid GUID of the installed table
1172 * @table table to be installed
1173 * @return status code
1175 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1178 EFI_ENTRY("%pUl, %p", guid, table);
1179 return EFI_EXIT(efi_install_configuration_table(guid, table));
1183 * Initialize a loaded_image_info + loaded_image_info object with correct
1184 * protocols, boot-device, etc.
1186 * @info loaded image info to be passed to the entry point of the
1188 * @obj internal object associated with the loaded image
1189 * @device_path device path of the loaded image
1190 * @file_path file path of the loaded image
1192 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1193 struct efi_device_path *device_path,
1194 struct efi_device_path *file_path)
1199 * When asking for the device path interface, return
1200 * bootefi_device_path
1202 obj->protocols[0].guid = &efi_guid_device_path;
1203 obj->protocols[0].protocol_interface = device_path;
1206 * When asking for the loaded_image interface, just
1207 * return handle which points to loaded_image_info
1209 obj->protocols[1].guid = &efi_guid_loaded_image;
1210 obj->protocols[1].protocol_interface = info;
1212 obj->protocols[2].guid = &efi_guid_console_control;
1213 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1215 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1216 obj->protocols[3].protocol_interface =
1217 (void *)&efi_device_path_to_text;
1219 info->file_path = file_path;
1221 info->device_handle = efi_dp_find_obj(device_path, NULL);
1223 list_add_tail(&obj->link, &efi_obj_list);
1227 * Load an image using a file path.
1229 * @file_path the path of the image to load
1230 * @buffer buffer containing the loaded image
1231 * @return status code
1233 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1236 struct efi_file_info *info = NULL;
1237 struct efi_file_handle *f;
1238 static efi_status_t ret;
1241 f = efi_file_from_path(file_path);
1243 return EFI_DEVICE_ERROR;
1246 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1248 if (ret == EFI_BUFFER_TOO_SMALL) {
1250 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1253 if (ret != EFI_SUCCESS)
1256 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1260 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1264 EFI_CALL(f->close(f));
1266 if (ret != EFI_SUCCESS) {
1267 efi_free_pool(*buffer);
1275 * Load an EFI image into memory.
1277 * This function implements the LoadImage service.
1278 * See the Unified Extensible Firmware Interface (UEFI) specification
1281 * @boot_policy true for request originating from the boot manager
1282 * @parent_image the calles's image handle
1283 * @file_path the path of the image to load
1284 * @source_buffer memory location from which the image is installed
1285 * @source_size size of the memory area from which the image is
1287 * @image_handle handle for the newly installed image
1288 * @return status code
1290 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1291 efi_handle_t parent_image,
1292 struct efi_device_path *file_path,
1293 void *source_buffer,
1294 unsigned long source_size,
1295 efi_handle_t *image_handle)
1297 struct efi_loaded_image *info;
1298 struct efi_object *obj;
1300 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1301 file_path, source_buffer, source_size, image_handle);
1303 info = calloc(1, sizeof(*info));
1304 obj = calloc(1, sizeof(*obj));
1306 if (!source_buffer) {
1307 struct efi_device_path *dp, *fp;
1310 ret = efi_load_image_from_path(file_path, &source_buffer);
1311 if (ret != EFI_SUCCESS) {
1314 return EFI_EXIT(ret);
1318 * split file_path which contains both the device and
1321 efi_dp_split_file_path(file_path, &dp, &fp);
1323 efi_setup_loaded_image(info, obj, dp, fp);
1325 /* In this case, file_path is the "device" path, ie.
1326 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1328 efi_setup_loaded_image(info, obj, file_path, NULL);
1331 info->reserved = efi_load_pe(source_buffer, info);
1332 if (!info->reserved) {
1335 return EFI_EXIT(EFI_UNSUPPORTED);
1338 info->system_table = &systab;
1339 info->parent_handle = parent_image;
1340 *image_handle = info;
1342 return EFI_EXIT(EFI_SUCCESS);
1346 * Call the entry point of an image.
1348 * This function implements the StartImage service.
1349 * See the Unified Extensible Firmware Interface (UEFI) specification
1352 * @image_handle handle of the image
1353 * @exit_data_size size of the buffer
1354 * @exit_data buffer to receive the exit data of the called image
1355 * @return status code
1357 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1358 unsigned long *exit_data_size,
1361 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1362 struct efi_loaded_image *info = image_handle;
1364 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1365 entry = info->reserved;
1367 efi_is_direct_boot = false;
1369 /* call the image! */
1370 if (setjmp(&info->exit_jmp)) {
1371 /* We returned from the child image */
1372 return EFI_EXIT(info->exit_status);
1375 __efi_nesting_dec();
1377 entry(image_handle, &systab);
1378 __efi_entry_check();
1379 __efi_nesting_inc();
1381 /* Should usually never get here */
1382 return EFI_EXIT(EFI_SUCCESS);
1386 * Leave an EFI application or driver.
1388 * This function implements the Exit service.
1389 * See the Unified Extensible Firmware Interface (UEFI) specification
1392 * @image_handle handle of the application or driver that is exiting
1393 * @exit_status status code
1394 * @exit_data_size size of the buffer in bytes
1395 * @exit_data buffer with data describing an error
1396 * @return status code
1398 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1399 efi_status_t exit_status, unsigned long exit_data_size,
1402 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1404 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1405 exit_data_size, exit_data);
1407 /* Make sure entry/exit counts for EFI world cross-overs match */
1411 * But longjmp out with the U-Boot gd, not the application's, as
1412 * the other end is a setjmp call inside EFI context.
1416 loaded_image_info->exit_status = exit_status;
1417 longjmp(&loaded_image_info->exit_jmp, 1);
1419 panic("EFI application exited");
1423 * Unload an EFI image.
1425 * This function implements the UnloadImage service.
1426 * See the Unified Extensible Firmware Interface (UEFI) specification
1429 * @image_handle handle of the image to be unloaded
1430 * @return status code
1432 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1434 struct efi_object *efiobj;
1436 EFI_ENTRY("%p", image_handle);
1437 efiobj = efi_search_obj(image_handle);
1439 list_del(&efiobj->link);
1441 return EFI_EXIT(EFI_SUCCESS);
1445 * Fix up caches for EFI payloads if necessary.
1447 static void efi_exit_caches(void)
1449 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1451 * Grub on 32bit ARM needs to have caches disabled before jumping into
1452 * a zImage, but does not know of all cache layers. Give it a hand.
1454 if (efi_is_direct_boot)
1455 cleanup_before_linux();
1460 * Stop boot services.
1462 * This function implements the ExitBootServices service.
1463 * See the Unified Extensible Firmware Interface (UEFI) specification
1466 * @image_handle handle of the loaded image
1467 * @map_key key of the memory map
1468 * @return status code
1470 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1471 unsigned long map_key)
1475 EFI_ENTRY("%p, %ld", image_handle, map_key);
1477 /* Notify that ExitBootServices is invoked. */
1478 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1479 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1481 efi_signal_event(&efi_events[i]);
1483 /* Make sure that notification functions are not called anymore */
1484 efi_tpl = TPL_HIGH_LEVEL;
1486 /* XXX Should persist EFI variables here */
1488 board_quiesce_devices();
1490 /* Fix up caches for EFI payloads if necessary */
1493 /* This stops all lingering devices */
1494 bootm_disable_interrupts();
1496 /* Give the payload some time to boot */
1497 efi_set_watchdog(0);
1500 return EFI_EXIT(EFI_SUCCESS);
1504 * Get next value of the counter.
1506 * This function implements the NextMonotonicCount service.
1507 * See the Unified Extensible Firmware Interface (UEFI) specification
1510 * @count returned value of the counter
1511 * @return status code
1513 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1515 static uint64_t mono = 0;
1516 EFI_ENTRY("%p", count);
1518 return EFI_EXIT(EFI_SUCCESS);
1524 * This function implements the Stall sercive.
1525 * See the Unified Extensible Firmware Interface (UEFI) specification
1528 * @microseconds period to sleep in microseconds
1529 * @return status code
1531 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1533 EFI_ENTRY("%ld", microseconds);
1534 udelay(microseconds);
1535 return EFI_EXIT(EFI_SUCCESS);
1539 * Reset the watchdog timer.
1541 * This function implements the SetWatchdogTimer service.
1542 * See the Unified Extensible Firmware Interface (UEFI) specification
1545 * @timeout seconds before reset by watchdog
1546 * @watchdog_code code to be logged when resetting
1547 * @data_size size of buffer in bytes
1548 * @watchdog_data buffer with data describing the reset reason
1549 * @return status code
1551 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1552 uint64_t watchdog_code,
1553 unsigned long data_size,
1554 uint16_t *watchdog_data)
1556 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1557 data_size, watchdog_data);
1558 return EFI_EXIT(efi_set_watchdog(timeout));
1562 * Connect a controller to a driver.
1564 * This function implements the ConnectController service.
1565 * See the Unified Extensible Firmware Interface (UEFI) specification
1568 * @controller_handle handle of the controller
1569 * @driver_image_handle handle of the driver
1570 * @remain_device_path device path of a child controller
1571 * @recursive true to connect all child controllers
1572 * @return status code
1574 static efi_status_t EFIAPI efi_connect_controller(
1575 efi_handle_t controller_handle,
1576 efi_handle_t *driver_image_handle,
1577 struct efi_device_path *remain_device_path,
1580 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1581 remain_device_path, recursive);
1582 return EFI_EXIT(EFI_NOT_FOUND);
1586 * Disconnect a controller from a driver.
1588 * This function implements the DisconnectController service.
1589 * See the Unified Extensible Firmware Interface (UEFI) specification
1592 * @controller_handle handle of the controller
1593 * @driver_image_handle handle of the driver
1594 * @child_handle handle of the child to destroy
1595 * @return status code
1597 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1598 void *driver_image_handle,
1601 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1603 return EFI_EXIT(EFI_INVALID_PARAMETER);
1609 * This function implements the CloseProtocol service.
1610 * See the Unified Extensible Firmware Interface (UEFI) specification
1613 * @handle handle on which the protocol shall be closed
1614 * @protocol GUID of the protocol to close
1615 * @agent_handle handle of the driver
1616 * @controller_handle handle of the controller
1617 * @return status code
1619 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1620 const efi_guid_t *protocol,
1622 void *controller_handle)
1624 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1626 return EFI_EXIT(EFI_NOT_FOUND);
1630 * Provide information about then open status of a protocol on a handle
1632 * This function implements the OpenProtocolInformation service.
1633 * See the Unified Extensible Firmware Interface (UEFI) specification
1636 * @handle handle for which the information shall be retrieved
1637 * @protocol GUID of the protocol
1638 * @entry_buffer buffer to receive the open protocol information
1639 * @entry_count number of entries available in the buffer
1640 * @return status code
1642 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1643 const efi_guid_t *protocol,
1644 struct efi_open_protocol_info_entry **entry_buffer,
1645 efi_uintn_t *entry_count)
1647 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1649 return EFI_EXIT(EFI_NOT_FOUND);
1653 * Get protocols installed on a handle.
1655 * This function implements the ProtocolsPerHandleService.
1656 * See the Unified Extensible Firmware Interface (UEFI) specification
1659 * @handle handle for which the information is retrieved
1660 * @protocol_buffer buffer with protocol GUIDs
1661 * @protocol_buffer_count number of entries in the buffer
1662 * @return status code
1664 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1665 efi_guid_t ***protocol_buffer,
1666 efi_uintn_t *protocol_buffer_count)
1668 unsigned long buffer_size;
1669 struct efi_object *efiobj;
1671 struct list_head *lhandle;
1674 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1675 protocol_buffer_count);
1677 if (!handle || !protocol_buffer || !protocol_buffer_count)
1678 return EFI_EXIT(EFI_INVALID_PARAMETER);
1680 *protocol_buffer = NULL;
1681 *protocol_buffer_count = 0;
1682 list_for_each(lhandle, &efi_obj_list) {
1683 efiobj = list_entry(lhandle, struct efi_object, link);
1685 if (efiobj->handle != handle)
1688 /* Count protocols */
1689 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1690 if (efiobj->protocols[i].guid)
1691 ++*protocol_buffer_count;
1694 if (*protocol_buffer_count) {
1695 buffer_size = sizeof(efi_guid_t *) *
1696 *protocol_buffer_count;
1697 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1699 (void **)protocol_buffer);
1700 if (r != EFI_SUCCESS)
1703 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1704 if (efiobj->protocols[i].guid) {
1705 (*protocol_buffer)[j] = (void *)
1706 efiobj->protocols[i].guid;
1714 return EFI_EXIT(EFI_SUCCESS);
1718 * Locate handles implementing a protocol.
1720 * This function implements the LocateHandleBuffer service.
1721 * See the Unified Extensible Firmware Interface (UEFI) specification
1724 * @search_type selection criterion
1725 * @protocol GUID of the protocol
1726 * @search_key registration key
1727 * @no_handles number of returned handles
1728 * @buffer buffer with the returned handles
1729 * @return status code
1731 static efi_status_t EFIAPI efi_locate_handle_buffer(
1732 enum efi_locate_search_type search_type,
1733 const efi_guid_t *protocol, void *search_key,
1734 efi_uintn_t *no_handles, efi_handle_t **buffer)
1737 efi_uintn_t buffer_size = 0;
1739 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1740 no_handles, buffer);
1742 if (!no_handles || !buffer) {
1743 r = EFI_INVALID_PARAMETER;
1748 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1750 if (r != EFI_BUFFER_TOO_SMALL)
1752 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1754 if (r != EFI_SUCCESS)
1756 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1758 if (r == EFI_SUCCESS)
1759 *no_handles = buffer_size / sizeof(void *);
1765 * Find an interface implementing a protocol.
1767 * This function implements the LocateProtocol service.
1768 * See the Unified Extensible Firmware Interface (UEFI) specification
1771 * @protocol GUID of the protocol
1772 * @registration registration key passed to the notification function
1773 * @protocol_interface interface implementing the protocol
1774 * @return status code
1776 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1778 void **protocol_interface)
1780 struct list_head *lhandle;
1783 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1785 if (!protocol || !protocol_interface)
1786 return EFI_EXIT(EFI_INVALID_PARAMETER);
1788 EFI_PRINT_GUID("protocol", protocol);
1790 list_for_each(lhandle, &efi_obj_list) {
1791 struct efi_object *efiobj;
1793 efiobj = list_entry(lhandle, struct efi_object, link);
1794 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1795 struct efi_handler *handler = &efiobj->protocols[i];
1799 if (!guidcmp(handler->guid, protocol)) {
1800 *protocol_interface =
1801 handler->protocol_interface;
1802 return EFI_EXIT(EFI_SUCCESS);
1806 *protocol_interface = NULL;
1808 return EFI_EXIT(EFI_NOT_FOUND);
1812 * Install multiple protocol interfaces.
1814 * This function implements the MultipleProtocolInterfaces service.
1815 * See the Unified Extensible Firmware Interface (UEFI) specification
1818 * @handle handle on which the protocol interfaces shall be installed
1819 * @... NULL terminated argument list with pairs of protocol GUIDS and
1821 * @return status code
1823 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1826 EFI_ENTRY("%p", handle);
1829 const efi_guid_t *protocol;
1830 void *protocol_interface;
1831 efi_status_t r = EFI_SUCCESS;
1835 return EFI_EXIT(EFI_INVALID_PARAMETER);
1837 va_start(argptr, handle);
1839 protocol = va_arg(argptr, efi_guid_t*);
1842 protocol_interface = va_arg(argptr, void*);
1843 r = EFI_CALL(efi_install_protocol_interface(
1845 EFI_NATIVE_INTERFACE,
1846 protocol_interface));
1847 if (r != EFI_SUCCESS)
1852 if (r == EFI_SUCCESS)
1855 /* If an error occurred undo all changes. */
1856 va_start(argptr, handle);
1858 protocol = va_arg(argptr, efi_guid_t*);
1859 protocol_interface = va_arg(argptr, void*);
1860 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1861 protocol_interface));
1869 * Uninstall multiple protocol interfaces.
1871 * This function implements the UninstallMultipleProtocolInterfaces service.
1872 * See the Unified Extensible Firmware Interface (UEFI) specification
1875 * @handle handle from which the protocol interfaces shall be removed
1876 * @... NULL terminated argument list with pairs of protocol GUIDS and
1878 * @return status code
1880 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1883 EFI_ENTRY("%p", handle);
1886 const efi_guid_t *protocol;
1887 void *protocol_interface;
1888 efi_status_t r = EFI_SUCCESS;
1892 return EFI_EXIT(EFI_INVALID_PARAMETER);
1894 va_start(argptr, handle);
1896 protocol = va_arg(argptr, efi_guid_t*);
1899 protocol_interface = va_arg(argptr, void*);
1900 r = EFI_CALL(efi_uninstall_protocol_interface(
1902 protocol_interface));
1903 if (r != EFI_SUCCESS)
1908 if (r == EFI_SUCCESS)
1911 /* If an error occurred undo all changes. */
1912 va_start(argptr, handle);
1914 protocol = va_arg(argptr, efi_guid_t*);
1915 protocol_interface = va_arg(argptr, void*);
1916 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1917 EFI_NATIVE_INTERFACE,
1918 protocol_interface));
1926 * Calculate cyclic redundancy code.
1928 * This function implements the CalculateCrc32 service.
1929 * See the Unified Extensible Firmware Interface (UEFI) specification
1932 * @data buffer with data
1933 * @data_size size of buffer in bytes
1934 * @crc32_p cyclic redundancy code
1935 * @return status code
1937 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1938 unsigned long data_size,
1941 EFI_ENTRY("%p, %ld", data, data_size);
1942 *crc32_p = crc32(0, data, data_size);
1943 return EFI_EXIT(EFI_SUCCESS);
1949 * This function implements the CopyMem service.
1950 * See the Unified Extensible Firmware Interface (UEFI) specification
1953 * @destination destination of the copy operation
1954 * @source source of the copy operation
1955 * @length number of bytes to copy
1957 static void EFIAPI efi_copy_mem(void *destination, const void *source,
1960 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
1961 memcpy(destination, source, length);
1962 EFI_EXIT(EFI_SUCCESS);
1966 * Fill memory with a byte value.
1968 * This function implements the SetMem service.
1969 * See the Unified Extensible Firmware Interface (UEFI) specification
1972 * @buffer buffer to fill
1973 * @size size of buffer in bytes
1974 * @value byte to copy to the buffer
1976 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
1978 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
1979 memset(buffer, value, size);
1980 EFI_EXIT(EFI_SUCCESS);
1984 * Open protocol interface on a handle.
1986 * This function implements the OpenProtocol interface.
1987 * See the Unified Extensible Firmware Interface (UEFI) specification
1990 * @handle handle on which the protocol shall be opened
1991 * @protocol GUID of the protocol
1992 * @protocol_interface interface implementing the protocol
1993 * @agent_handle handle of the driver
1994 * @controller_handle handle of the controller
1995 * @attributes attributes indicating how to open the protocol
1996 * @return status code
1998 static efi_status_t EFIAPI efi_open_protocol(
1999 void *handle, const efi_guid_t *protocol,
2000 void **protocol_interface, void *agent_handle,
2001 void *controller_handle, uint32_t attributes)
2003 struct list_head *lhandle;
2005 efi_status_t r = EFI_INVALID_PARAMETER;
2007 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2008 protocol_interface, agent_handle, controller_handle,
2011 if (!handle || !protocol ||
2012 (!protocol_interface && attributes !=
2013 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2017 EFI_PRINT_GUID("protocol", protocol);
2019 switch (attributes) {
2020 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2021 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2022 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2024 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2025 if (controller_handle == handle)
2027 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2028 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2029 if (controller_handle == NULL)
2031 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2032 if (agent_handle == NULL)
2039 list_for_each(lhandle, &efi_obj_list) {
2040 struct efi_object *efiobj;
2041 efiobj = list_entry(lhandle, struct efi_object, link);
2043 if (efiobj->handle != handle)
2046 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
2047 struct efi_handler *handler = &efiobj->protocols[i];
2048 const efi_guid_t *hprotocol = handler->guid;
2051 if (!guidcmp(hprotocol, protocol)) {
2053 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
2054 *protocol_interface =
2055 handler->protocol_interface;
2065 r = EFI_UNSUPPORTED;
2071 * Get interface of a protocol on a handle.
2073 * This function implements the HandleProtocol service.
2074 * See the Unified Extensible Firmware Interface (UEFI) specification
2077 * @handle handle on which the protocol shall be opened
2078 * @protocol GUID of the protocol
2079 * @protocol_interface interface implementing the protocol
2080 * @return status code
2082 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2083 const efi_guid_t *protocol,
2084 void **protocol_interface)
2086 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2087 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2090 static const struct efi_boot_services efi_boot_services = {
2092 .headersize = sizeof(struct efi_table_hdr),
2094 .raise_tpl = efi_raise_tpl,
2095 .restore_tpl = efi_restore_tpl,
2096 .allocate_pages = efi_allocate_pages_ext,
2097 .free_pages = efi_free_pages_ext,
2098 .get_memory_map = efi_get_memory_map_ext,
2099 .allocate_pool = efi_allocate_pool_ext,
2100 .free_pool = efi_free_pool_ext,
2101 .create_event = efi_create_event_ext,
2102 .set_timer = efi_set_timer_ext,
2103 .wait_for_event = efi_wait_for_event,
2104 .signal_event = efi_signal_event_ext,
2105 .close_event = efi_close_event,
2106 .check_event = efi_check_event,
2107 .install_protocol_interface = efi_install_protocol_interface,
2108 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2109 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2110 .handle_protocol = efi_handle_protocol,
2112 .register_protocol_notify = efi_register_protocol_notify,
2113 .locate_handle = efi_locate_handle_ext,
2114 .locate_device_path = efi_locate_device_path,
2115 .install_configuration_table = efi_install_configuration_table_ext,
2116 .load_image = efi_load_image,
2117 .start_image = efi_start_image,
2119 .unload_image = efi_unload_image,
2120 .exit_boot_services = efi_exit_boot_services,
2121 .get_next_monotonic_count = efi_get_next_monotonic_count,
2123 .set_watchdog_timer = efi_set_watchdog_timer,
2124 .connect_controller = efi_connect_controller,
2125 .disconnect_controller = efi_disconnect_controller,
2126 .open_protocol = efi_open_protocol,
2127 .close_protocol = efi_close_protocol,
2128 .open_protocol_information = efi_open_protocol_information,
2129 .protocols_per_handle = efi_protocols_per_handle,
2130 .locate_handle_buffer = efi_locate_handle_buffer,
2131 .locate_protocol = efi_locate_protocol,
2132 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2133 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2134 .calculate_crc32 = efi_calculate_crc32,
2135 .copy_mem = efi_copy_mem,
2136 .set_mem = efi_set_mem,
2140 static uint16_t __efi_runtime_data firmware_vendor[] =
2141 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2143 struct efi_system_table __efi_runtime_data systab = {
2145 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2146 .revision = 0x20005, /* 2.5 */
2147 .headersize = sizeof(struct efi_table_hdr),
2149 .fw_vendor = (long)firmware_vendor,
2150 .con_in = (void*)&efi_con_in,
2151 .con_out = (void*)&efi_con_out,
2152 .std_err = (void*)&efi_con_out,
2153 .runtime = (void*)&efi_runtime_services,
2154 .boottime = (void*)&efi_boot_services,
2156 .tables = (void*)efi_conf_table,