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 UINTN 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(UINTN new_tpl)
170 UINTN 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(UINTN 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%lx, %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%lx", 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 unsigned long *memory_map_size,
268 struct efi_mem_desc *memory_map,
269 unsigned long *map_key,
270 unsigned long *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, %ld, %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);
324 static efi_status_t efi_create_handle(void **handle)
326 struct efi_object *obj;
329 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
330 sizeof(struct efi_object),
332 if (r != EFI_SUCCESS)
334 memset(obj, 0, sizeof(struct efi_object));
336 list_add_tail(&obj->link, &efi_obj_list);
342 * Our event capabilities are very limited. Only a small limited
343 * number of events is allowed to coexist.
345 static struct efi_event efi_events[16];
350 * This function is used inside U-Boot code to create an event.
352 * For the API function implementing the CreateEvent service see
353 * efi_create_event_ext.
355 * @type type of the event to create
356 * @notify_tpl task priority level of the event
357 * @notify_function notification function of the event
358 * @notify_context pointer passed to the notification function
359 * @event created event
360 * @return status code
362 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
363 void (EFIAPI *notify_function) (
364 struct efi_event *event,
366 void *notify_context, struct efi_event **event)
371 return EFI_INVALID_PARAMETER;
373 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
374 return EFI_INVALID_PARAMETER;
376 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
377 notify_function == NULL)
378 return EFI_INVALID_PARAMETER;
380 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
381 if (efi_events[i].type)
383 efi_events[i].type = type;
384 efi_events[i].notify_tpl = notify_tpl;
385 efi_events[i].notify_function = notify_function;
386 efi_events[i].notify_context = notify_context;
387 /* Disable timers on bootup */
388 efi_events[i].trigger_next = -1ULL;
389 efi_events[i].is_queued = false;
390 efi_events[i].is_signaled = false;
391 *event = &efi_events[i];
394 return EFI_OUT_OF_RESOURCES;
400 * This function implements the CreateEvent service.
401 * See the Unified Extensible Firmware Interface (UEFI) specification
404 * @type type of the event to create
405 * @notify_tpl task priority level of the event
406 * @notify_function notification function of the event
407 * @notify_context pointer passed to the notification function
408 * @event created event
409 * @return status code
411 static efi_status_t EFIAPI efi_create_event_ext(
412 uint32_t type, UINTN notify_tpl,
413 void (EFIAPI *notify_function) (
414 struct efi_event *event,
416 void *notify_context, struct efi_event **event)
418 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
420 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
421 notify_context, event));
426 * Check if a timer event has occurred or a queued notification function should
429 * Our timers have to work without interrupts, so we check whenever keyboard
430 * input or disk accesses happen if enough time elapsed for them to fire.
432 void efi_timer_check(void)
435 u64 now = timer_get_us();
437 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
438 if (!efi_events[i].type)
440 if (efi_events[i].is_queued)
441 efi_signal_event(&efi_events[i]);
442 if (!(efi_events[i].type & EVT_TIMER) ||
443 now < efi_events[i].trigger_next)
445 switch (efi_events[i].trigger_type) {
446 case EFI_TIMER_RELATIVE:
447 efi_events[i].trigger_type = EFI_TIMER_STOP;
449 case EFI_TIMER_PERIODIC:
450 efi_events[i].trigger_next +=
451 efi_events[i].trigger_time;
456 efi_events[i].is_signaled = true;
457 efi_signal_event(&efi_events[i]);
463 * Set the trigger time for a timer event or stop the event.
465 * This is the function for internal usage in U-Boot. For the API function
466 * implementing the SetTimer service see efi_set_timer_ext.
468 * @event event for which the timer is set
469 * @type type of the timer
470 * @trigger_time trigger period in multiples of 100ns
471 * @return status code
473 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
474 uint64_t trigger_time)
479 * The parameter defines a multiple of 100ns.
480 * We use multiples of 1000ns. So divide by 10.
482 do_div(trigger_time, 10);
484 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
485 if (event != &efi_events[i])
488 if (!(event->type & EVT_TIMER))
492 event->trigger_next = -1ULL;
494 case EFI_TIMER_PERIODIC:
495 case EFI_TIMER_RELATIVE:
496 event->trigger_next =
497 timer_get_us() + trigger_time;
500 return EFI_INVALID_PARAMETER;
502 event->trigger_type = type;
503 event->trigger_time = trigger_time;
504 event->is_signaled = false;
507 return EFI_INVALID_PARAMETER;
511 * Set the trigger time for a timer event or stop the event.
513 * This function implements the SetTimer service.
514 * See the Unified Extensible Firmware Interface (UEFI) specification
517 * @event event for which the timer is set
518 * @type type of the timer
519 * @trigger_time trigger period in multiples of 100ns
520 * @return status code
522 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
523 enum efi_timer_delay type,
524 uint64_t trigger_time)
526 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
527 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
531 * Wait for events to be signaled.
533 * This function implements the WaitForEvent service.
534 * See the Unified Extensible Firmware Interface (UEFI) specification
537 * @num_events number of events to be waited for
538 * @events events to be waited for
539 * @index index of the event that was signaled
540 * @return status code
542 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
543 struct efi_event **event,
548 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
550 /* Check parameters */
551 if (!num_events || !event)
552 return EFI_EXIT(EFI_INVALID_PARAMETER);
554 if (efi_tpl != TPL_APPLICATION)
555 return EFI_EXIT(EFI_UNSUPPORTED);
556 for (i = 0; i < num_events; ++i) {
557 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
558 if (event[i] == &efi_events[j])
561 return EFI_EXIT(EFI_INVALID_PARAMETER);
563 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
564 return EFI_EXIT(EFI_INVALID_PARAMETER);
565 if (!event[i]->is_signaled)
566 efi_signal_event(event[i]);
569 /* Wait for signal */
571 for (i = 0; i < num_events; ++i) {
572 if (event[i]->is_signaled)
575 /* Allow events to occur. */
581 * Reset the signal which is passed to the caller to allow periodic
584 event[i]->is_signaled = false;
588 return EFI_EXIT(EFI_SUCCESS);
592 * Signal an EFI event.
594 * This function implements the SignalEvent service.
595 * See the Unified Extensible Firmware Interface (UEFI) specification
598 * This functions sets the signaled state of the event and queues the
599 * notification function for execution.
601 * @event event to signal
602 * @return status code
604 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
608 EFI_ENTRY("%p", event);
609 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
610 if (event != &efi_events[i])
612 if (event->is_signaled)
614 event->is_signaled = true;
615 if (event->type & EVT_NOTIFY_SIGNAL)
616 efi_signal_event(event);
619 return EFI_EXIT(EFI_SUCCESS);
623 * Close an EFI event.
625 * This function implements the CloseEvent service.
626 * See the Unified Extensible Firmware Interface (UEFI) specification
629 * @event event to close
630 * @return status code
632 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
636 EFI_ENTRY("%p", event);
637 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
638 if (event == &efi_events[i]) {
640 event->trigger_next = -1ULL;
641 event->is_queued = false;
642 event->is_signaled = false;
643 return EFI_EXIT(EFI_SUCCESS);
646 return EFI_EXIT(EFI_INVALID_PARAMETER);
650 * Check if an event is signaled.
652 * This function implements the CheckEvent service.
653 * See the Unified Extensible Firmware Interface (UEFI) specification
656 * If an event is not signaled yet the notification function is queued.
658 * @event event to check
659 * @return status code
661 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
665 EFI_ENTRY("%p", event);
667 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
668 if (event != &efi_events[i])
670 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
672 if (!event->is_signaled)
673 efi_signal_event(event);
674 if (event->is_signaled)
675 return EFI_EXIT(EFI_SUCCESS);
676 return EFI_EXIT(EFI_NOT_READY);
678 return EFI_EXIT(EFI_INVALID_PARAMETER);
682 * Find the internal EFI object for a handle.
684 * @handle handle to find
687 static struct efi_object *efi_search_obj(void *handle)
689 struct list_head *lhandle;
691 list_for_each(lhandle, &efi_obj_list) {
692 struct efi_object *efiobj;
694 efiobj = list_entry(lhandle, struct efi_object, link);
695 if (efiobj->handle == handle)
703 * Install protocol interface.
705 * This function implements the InstallProtocolInterface service.
706 * See the Unified Extensible Firmware Interface (UEFI) specification
709 * @handle handle on which the protocol shall be installed
710 * @protocol GUID of the protocol to be installed
711 * @protocol_interface_type type of the interface to be installed,
712 * always EFI_NATIVE_INTERFACE
713 * @protocol_interface interface of the protocol implementation
714 * @return status code
716 static efi_status_t EFIAPI efi_install_protocol_interface(
717 void **handle, const efi_guid_t *protocol,
718 int protocol_interface_type, void *protocol_interface)
720 struct list_head *lhandle;
724 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
727 if (!handle || !protocol ||
728 protocol_interface_type != EFI_NATIVE_INTERFACE) {
729 r = EFI_INVALID_PARAMETER;
733 /* Create new handle if requested. */
735 r = efi_create_handle(handle);
736 if (r != EFI_SUCCESS)
740 list_for_each(lhandle, &efi_obj_list) {
741 struct efi_object *efiobj;
742 efiobj = list_entry(lhandle, struct efi_object, link);
744 if (efiobj->handle != *handle)
746 /* Check if protocol is already installed on the handle. */
747 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
748 struct efi_handler *handler = &efiobj->protocols[i];
752 if (!guidcmp(handler->guid, protocol)) {
753 r = EFI_INVALID_PARAMETER;
757 /* Install protocol in first empty slot. */
758 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
759 struct efi_handler *handler = &efiobj->protocols[i];
764 handler->guid = protocol;
765 handler->protocol_interface = protocol_interface;
769 r = EFI_OUT_OF_RESOURCES;
772 r = EFI_INVALID_PARAMETER;
778 * Reinstall protocol interface.
780 * This function implements the ReinstallProtocolInterface service.
781 * See the Unified Extensible Firmware Interface (UEFI) specification
784 * @handle handle on which the protocol shall be
786 * @protocol GUID of the protocol to be installed
787 * @old_interface interface to be removed
788 * @new_interface interface to be installed
789 * @return status code
791 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
792 const efi_guid_t *protocol, void *old_interface,
795 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
797 return EFI_EXIT(EFI_ACCESS_DENIED);
801 * Uninstall protocol interface.
803 * This is the function for internal calls. For the API implementation of the
804 * UninstallProtocolInterface service see function
805 * efi_uninstall_protocol_interface_ext.
807 * @handle handle from which the protocol shall be removed
808 * @protocol GUID of the protocol to be removed
809 * @protocol_interface interface to be removed
810 * @return status code
812 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
813 const efi_guid_t *protocol, void *protocol_interface)
815 struct list_head *lhandle;
817 efi_status_t r = EFI_NOT_FOUND;
819 if (!handle || !protocol) {
820 r = EFI_INVALID_PARAMETER;
824 list_for_each(lhandle, &efi_obj_list) {
825 struct efi_object *efiobj;
826 efiobj = list_entry(lhandle, struct efi_object, link);
828 if (efiobj->handle != handle)
831 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
832 struct efi_handler *handler = &efiobj->protocols[i];
833 const efi_guid_t *hprotocol = handler->guid;
837 if (!guidcmp(hprotocol, protocol)) {
838 if (handler->protocol_interface) {
839 r = EFI_ACCESS_DENIED;
854 * Uninstall protocol interface.
856 * This function implements the UninstallProtocolInterface service.
857 * See the Unified Extensible Firmware Interface (UEFI) specification
860 * @handle handle from which the protocol shall be removed
861 * @protocol GUID of the protocol to be removed
862 * @protocol_interface interface to be removed
863 * @return status code
865 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
866 const efi_guid_t *protocol, void *protocol_interface)
868 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
870 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
871 protocol_interface));
875 * Register an event for notification when a protocol is installed.
877 * This function implements the RegisterProtocolNotify service.
878 * See the Unified Extensible Firmware Interface (UEFI) specification
881 * @protocol GUID of the protocol whose installation shall be
883 * @event event to be signaled upon installation of the protocol
884 * @registration key for retrieving the registration information
885 * @return status code
887 static efi_status_t EFIAPI efi_register_protocol_notify(
888 const efi_guid_t *protocol,
889 struct efi_event *event,
892 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
893 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
897 * Determine if an EFI handle implements a protocol.
899 * See the documentation of the LocateHandle service in the UEFI specification.
901 * @search_type selection criterion
902 * @protocol GUID of the protocol
903 * @search_key registration key
905 * @return 0 if the handle implements the protocol
907 static int efi_search(enum efi_locate_search_type search_type,
908 const efi_guid_t *protocol, void *search_key,
909 struct efi_object *efiobj)
913 switch (search_type) {
916 case BY_REGISTER_NOTIFY:
919 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
920 const efi_guid_t *guid = efiobj->protocols[i].guid;
921 if (guid && !guidcmp(guid, protocol))
931 * Locate handles implementing a protocol.
933 * This function is meant for U-Boot internal calls. For the API implementation
934 * of the LocateHandle service see efi_locate_handle_ext.
936 * @search_type selection criterion
937 * @protocol GUID of the protocol
938 * @search_key registration key
939 * @buffer_size size of the buffer to receive the handles in bytes
940 * @buffer buffer to receive the relevant handles
941 * @return status code
943 static efi_status_t efi_locate_handle(
944 enum efi_locate_search_type search_type,
945 const efi_guid_t *protocol, void *search_key,
946 unsigned long *buffer_size, efi_handle_t *buffer)
948 struct list_head *lhandle;
949 unsigned long size = 0;
951 /* Count how much space we need */
952 list_for_each(lhandle, &efi_obj_list) {
953 struct efi_object *efiobj;
954 efiobj = list_entry(lhandle, struct efi_object, link);
955 if (!efi_search(search_type, protocol, search_key, efiobj)) {
956 size += sizeof(void*);
960 if (*buffer_size < size) {
962 return EFI_BUFFER_TOO_SMALL;
967 return EFI_NOT_FOUND;
969 /* Then fill the array */
970 list_for_each(lhandle, &efi_obj_list) {
971 struct efi_object *efiobj;
972 efiobj = list_entry(lhandle, struct efi_object, link);
973 if (!efi_search(search_type, protocol, search_key, efiobj)) {
974 *(buffer++) = efiobj->handle;
982 * Locate handles implementing a protocol.
984 * This function implements the LocateHandle service.
985 * See the Unified Extensible Firmware Interface (UEFI) specification
988 * @search_type selection criterion
989 * @protocol GUID of the protocol
990 * @search_key registration key
991 * @buffer_size size of the buffer to receive the handles in bytes
992 * @buffer buffer to receive the relevant handles
993 * @return 0 if the handle implements the protocol
995 static efi_status_t EFIAPI efi_locate_handle_ext(
996 enum efi_locate_search_type search_type,
997 const efi_guid_t *protocol, void *search_key,
998 unsigned long *buffer_size, efi_handle_t *buffer)
1000 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1001 buffer_size, buffer);
1003 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1004 buffer_size, buffer));
1008 * Get the device path and handle of an device implementing a protocol.
1010 * This function implements the LocateDevicePath service.
1011 * See the Unified Extensible Firmware Interface (UEFI) specification
1014 * @protocol GUID of the protocol
1015 * @device_path device path
1016 * @device handle of the device
1017 * @return status code
1019 static efi_status_t EFIAPI efi_locate_device_path(
1020 const efi_guid_t *protocol,
1021 struct efi_device_path **device_path,
1022 efi_handle_t *device)
1024 struct efi_object *efiobj;
1026 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1028 efiobj = efi_dp_find_obj(*device_path, device_path);
1030 return EFI_EXIT(EFI_NOT_FOUND);
1032 *device = efiobj->handle;
1034 return EFI_EXIT(EFI_SUCCESS);
1037 /* Collapses configuration table entries, removing index i */
1038 static void efi_remove_configuration_table(int i)
1040 struct efi_configuration_table *this = &efi_conf_table[i];
1041 struct efi_configuration_table *next = &efi_conf_table[i+1];
1042 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1044 memmove(this, next, (ulong)end - (ulong)next);
1049 * Adds, updates, or removes a configuration table.
1051 * This function is used for internal calls. For the API implementation of the
1052 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1054 * @guid GUID of the installed table
1055 * @table table to be installed
1056 * @return status code
1058 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1062 /* Check for guid override */
1063 for (i = 0; i < systab.nr_tables; i++) {
1064 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1066 efi_conf_table[i].table = table;
1068 efi_remove_configuration_table(i);
1074 return EFI_NOT_FOUND;
1076 /* No override, check for overflow */
1077 if (i >= ARRAY_SIZE(efi_conf_table))
1078 return EFI_OUT_OF_RESOURCES;
1080 /* Add a new entry */
1081 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1082 efi_conf_table[i].table = table;
1083 systab.nr_tables = i + 1;
1089 * Adds, updates, or removes a configuration table.
1091 * This function implements the InstallConfigurationTable service.
1092 * See the Unified Extensible Firmware Interface (UEFI) specification
1095 * @guid GUID of the installed table
1096 * @table table to be installed
1097 * @return status code
1099 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1102 EFI_ENTRY("%pUl, %p", guid, table);
1103 return EFI_EXIT(efi_install_configuration_table(guid, table));
1107 * Initialize a loaded_image_info + loaded_image_info object with correct
1108 * protocols, boot-device, etc.
1110 * @info loaded image info to be passed to the entry point of the
1112 * @obj internal object associated with the loaded image
1113 * @device_path device path of the loaded image
1114 * @file_path file path of the loaded image
1116 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1117 struct efi_device_path *device_path,
1118 struct efi_device_path *file_path)
1123 * When asking for the device path interface, return
1124 * bootefi_device_path
1126 obj->protocols[0].guid = &efi_guid_device_path;
1127 obj->protocols[0].protocol_interface = device_path;
1130 * When asking for the loaded_image interface, just
1131 * return handle which points to loaded_image_info
1133 obj->protocols[1].guid = &efi_guid_loaded_image;
1134 obj->protocols[1].protocol_interface = info;
1136 obj->protocols[2].guid = &efi_guid_console_control;
1137 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1139 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1140 obj->protocols[3].protocol_interface =
1141 (void *)&efi_device_path_to_text;
1143 info->file_path = file_path;
1145 info->device_handle = efi_dp_find_obj(device_path, NULL);
1147 list_add_tail(&obj->link, &efi_obj_list);
1151 * Load an image using a file path.
1153 * @file_path the path of the image to load
1154 * @buffer buffer containing the loaded image
1155 * @return status code
1157 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1160 struct efi_file_info *info = NULL;
1161 struct efi_file_handle *f;
1162 static efi_status_t ret;
1165 f = efi_file_from_path(file_path);
1167 return EFI_DEVICE_ERROR;
1170 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1172 if (ret == EFI_BUFFER_TOO_SMALL) {
1174 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1177 if (ret != EFI_SUCCESS)
1180 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1184 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1188 EFI_CALL(f->close(f));
1190 if (ret != EFI_SUCCESS) {
1191 efi_free_pool(*buffer);
1199 * Load an EFI image into memory.
1201 * This function implements the LoadImage service.
1202 * See the Unified Extensible Firmware Interface (UEFI) specification
1205 * @boot_policy true for request originating from the boot manager
1206 * @parent_image the calles's image handle
1207 * @file_path the path of the image to load
1208 * @source_buffer memory location from which the image is installed
1209 * @source_size size of the memory area from which the image is
1211 * @image_handle handle for the newly installed image
1212 * @return status code
1214 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1215 efi_handle_t parent_image,
1216 struct efi_device_path *file_path,
1217 void *source_buffer,
1218 unsigned long source_size,
1219 efi_handle_t *image_handle)
1221 struct efi_loaded_image *info;
1222 struct efi_object *obj;
1224 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1225 file_path, source_buffer, source_size, image_handle);
1227 info = calloc(1, sizeof(*info));
1228 obj = calloc(1, sizeof(*obj));
1230 if (!source_buffer) {
1231 struct efi_device_path *dp, *fp;
1234 ret = efi_load_image_from_path(file_path, &source_buffer);
1235 if (ret != EFI_SUCCESS) {
1238 return EFI_EXIT(ret);
1242 * split file_path which contains both the device and
1245 efi_dp_split_file_path(file_path, &dp, &fp);
1247 efi_setup_loaded_image(info, obj, dp, fp);
1249 /* In this case, file_path is the "device" path, ie.
1250 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1252 efi_setup_loaded_image(info, obj, file_path, NULL);
1255 info->reserved = efi_load_pe(source_buffer, info);
1256 if (!info->reserved) {
1259 return EFI_EXIT(EFI_UNSUPPORTED);
1262 info->system_table = &systab;
1263 info->parent_handle = parent_image;
1264 *image_handle = info;
1266 return EFI_EXIT(EFI_SUCCESS);
1270 * Call the entry point of an image.
1272 * This function implements the StartImage service.
1273 * See the Unified Extensible Firmware Interface (UEFI) specification
1276 * @image_handle handle of the image
1277 * @exit_data_size size of the buffer
1278 * @exit_data buffer to receive the exit data of the called image
1279 * @return status code
1281 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1282 unsigned long *exit_data_size,
1285 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1286 struct efi_loaded_image *info = image_handle;
1288 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1289 entry = info->reserved;
1291 efi_is_direct_boot = false;
1293 /* call the image! */
1294 if (setjmp(&info->exit_jmp)) {
1295 /* We returned from the child image */
1296 return EFI_EXIT(info->exit_status);
1299 __efi_nesting_dec();
1301 entry(image_handle, &systab);
1302 __efi_entry_check();
1303 __efi_nesting_inc();
1305 /* Should usually never get here */
1306 return EFI_EXIT(EFI_SUCCESS);
1310 * Leave an EFI application or driver.
1312 * This function implements the Exit service.
1313 * See the Unified Extensible Firmware Interface (UEFI) specification
1316 * @image_handle handle of the application or driver that is exiting
1317 * @exit_status status code
1318 * @exit_data_size size of the buffer in bytes
1319 * @exit_data buffer with data describing an error
1320 * @return status code
1322 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1323 efi_status_t exit_status, unsigned long exit_data_size,
1326 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1328 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1329 exit_data_size, exit_data);
1331 /* Make sure entry/exit counts for EFI world cross-overs match */
1335 * But longjmp out with the U-Boot gd, not the application's, as
1336 * the other end is a setjmp call inside EFI context.
1340 loaded_image_info->exit_status = exit_status;
1341 longjmp(&loaded_image_info->exit_jmp, 1);
1343 panic("EFI application exited");
1347 * Unload an EFI image.
1349 * This function implements the UnloadImage service.
1350 * See the Unified Extensible Firmware Interface (UEFI) specification
1353 * @image_handle handle of the image to be unloaded
1354 * @return status code
1356 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1358 struct efi_object *efiobj;
1360 EFI_ENTRY("%p", image_handle);
1361 efiobj = efi_search_obj(image_handle);
1363 list_del(&efiobj->link);
1365 return EFI_EXIT(EFI_SUCCESS);
1369 * Fix up caches for EFI payloads if necessary.
1371 static void efi_exit_caches(void)
1373 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1375 * Grub on 32bit ARM needs to have caches disabled before jumping into
1376 * a zImage, but does not know of all cache layers. Give it a hand.
1378 if (efi_is_direct_boot)
1379 cleanup_before_linux();
1384 * Stop boot services.
1386 * This function implements the ExitBootServices service.
1387 * See the Unified Extensible Firmware Interface (UEFI) specification
1390 * @image_handle handle of the loaded image
1391 * @map_key key of the memory map
1392 * @return status code
1394 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1395 unsigned long map_key)
1399 EFI_ENTRY("%p, %ld", image_handle, map_key);
1401 /* Notify that ExitBootServices is invoked. */
1402 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1403 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1405 efi_signal_event(&efi_events[i]);
1407 /* Make sure that notification functions are not called anymore */
1408 efi_tpl = TPL_HIGH_LEVEL;
1410 /* XXX Should persist EFI variables here */
1412 board_quiesce_devices();
1414 /* Fix up caches for EFI payloads if necessary */
1417 /* This stops all lingering devices */
1418 bootm_disable_interrupts();
1420 /* Give the payload some time to boot */
1421 efi_set_watchdog(0);
1424 return EFI_EXIT(EFI_SUCCESS);
1428 * Get next value of the counter.
1430 * This function implements the NextMonotonicCount service.
1431 * See the Unified Extensible Firmware Interface (UEFI) specification
1434 * @count returned value of the counter
1435 * @return status code
1437 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1439 static uint64_t mono = 0;
1440 EFI_ENTRY("%p", count);
1442 return EFI_EXIT(EFI_SUCCESS);
1448 * This function implements the Stall sercive.
1449 * See the Unified Extensible Firmware Interface (UEFI) specification
1452 * @microseconds period to sleep in microseconds
1453 * @return status code
1455 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1457 EFI_ENTRY("%ld", microseconds);
1458 udelay(microseconds);
1459 return EFI_EXIT(EFI_SUCCESS);
1463 * Reset the watchdog timer.
1465 * This function implements the SetWatchdogTimer service.
1466 * See the Unified Extensible Firmware Interface (UEFI) specification
1469 * @timeout seconds before reset by watchdog
1470 * @watchdog_code code to be logged when resetting
1471 * @data_size size of buffer in bytes
1472 * @watchdog_data buffer with data describing the reset reason
1473 * @return status code
1475 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1476 uint64_t watchdog_code,
1477 unsigned long data_size,
1478 uint16_t *watchdog_data)
1480 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1481 data_size, watchdog_data);
1482 return EFI_EXIT(efi_set_watchdog(timeout));
1486 * Connect a controller to a driver.
1488 * This function implements the ConnectController service.
1489 * See the Unified Extensible Firmware Interface (UEFI) specification
1492 * @controller_handle handle of the controller
1493 * @driver_image_handle handle of the driver
1494 * @remain_device_path device path of a child controller
1495 * @recursive true to connect all child controllers
1496 * @return status code
1498 static efi_status_t EFIAPI efi_connect_controller(
1499 efi_handle_t controller_handle,
1500 efi_handle_t *driver_image_handle,
1501 struct efi_device_path *remain_device_path,
1504 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1505 remain_device_path, recursive);
1506 return EFI_EXIT(EFI_NOT_FOUND);
1510 * Disconnect a controller from a driver.
1512 * This function implements the DisconnectController service.
1513 * See the Unified Extensible Firmware Interface (UEFI) specification
1516 * @controller_handle handle of the controller
1517 * @driver_image_handle handle of the driver
1518 * @child_handle handle of the child to destroy
1519 * @return status code
1521 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1522 void *driver_image_handle,
1525 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1527 return EFI_EXIT(EFI_INVALID_PARAMETER);
1533 * This function implements the CloseProtocol service.
1534 * See the Unified Extensible Firmware Interface (UEFI) specification
1537 * @handle handle on which the protocol shall be closed
1538 * @protocol GUID of the protocol to close
1539 * @agent_handle handle of the driver
1540 * @controller_handle handle of the controller
1541 * @return status code
1543 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1544 const efi_guid_t *protocol,
1546 void *controller_handle)
1548 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1550 return EFI_EXIT(EFI_NOT_FOUND);
1554 * Provide information about then open status of a protocol on a handle
1556 * This function implements the OpenProtocolInformation service.
1557 * See the Unified Extensible Firmware Interface (UEFI) specification
1560 * @handle handle for which the information shall be retrieved
1561 * @protocol GUID of the protocol
1562 * @entry_buffer buffer to receive the open protocol information
1563 * @entry_count number of entries available in the buffer
1564 * @return status code
1566 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1567 const efi_guid_t *protocol,
1568 struct efi_open_protocol_info_entry **entry_buffer,
1569 unsigned long *entry_count)
1571 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1573 return EFI_EXIT(EFI_NOT_FOUND);
1577 * Get protocols installed on a handle.
1579 * This function implements the ProtocolsPerHandleService.
1580 * See the Unified Extensible Firmware Interface (UEFI) specification
1583 * @handle handle for which the information is retrieved
1584 * @protocol_buffer buffer with protocol GUIDs
1585 * @protocol_buffer_count number of entries in the buffer
1586 * @return status code
1588 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1589 efi_guid_t ***protocol_buffer,
1590 unsigned long *protocol_buffer_count)
1592 unsigned long buffer_size;
1593 struct efi_object *efiobj;
1595 struct list_head *lhandle;
1598 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1599 protocol_buffer_count);
1601 if (!handle || !protocol_buffer || !protocol_buffer_count)
1602 return EFI_EXIT(EFI_INVALID_PARAMETER);
1604 *protocol_buffer = NULL;
1605 *protocol_buffer_count = 0;
1606 list_for_each(lhandle, &efi_obj_list) {
1607 efiobj = list_entry(lhandle, struct efi_object, link);
1609 if (efiobj->handle != handle)
1612 /* Count protocols */
1613 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1614 if (efiobj->protocols[i].guid)
1615 ++*protocol_buffer_count;
1618 if (*protocol_buffer_count) {
1619 buffer_size = sizeof(efi_guid_t *) *
1620 *protocol_buffer_count;
1621 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1623 (void **)protocol_buffer);
1624 if (r != EFI_SUCCESS)
1627 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1628 if (efiobj->protocols[i].guid) {
1629 (*protocol_buffer)[j] = (void *)
1630 efiobj->protocols[i].guid;
1638 return EFI_EXIT(EFI_SUCCESS);
1642 * Locate handles implementing a protocol.
1644 * This function implements the LocateHandleBuffer service.
1645 * See the Unified Extensible Firmware Interface (UEFI) specification
1648 * @search_type selection criterion
1649 * @protocol GUID of the protocol
1650 * @search_key registration key
1651 * @no_handles number of returned handles
1652 * @buffer buffer with the returned handles
1653 * @return status code
1655 static efi_status_t EFIAPI efi_locate_handle_buffer(
1656 enum efi_locate_search_type search_type,
1657 const efi_guid_t *protocol, void *search_key,
1658 unsigned long *no_handles, efi_handle_t **buffer)
1661 unsigned long buffer_size = 0;
1663 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1664 no_handles, buffer);
1666 if (!no_handles || !buffer) {
1667 r = EFI_INVALID_PARAMETER;
1672 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1674 if (r != EFI_BUFFER_TOO_SMALL)
1676 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1678 if (r != EFI_SUCCESS)
1680 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1682 if (r == EFI_SUCCESS)
1683 *no_handles = buffer_size / sizeof(void *);
1689 * Find an interface implementing a protocol.
1691 * This function implements the LocateProtocol service.
1692 * See the Unified Extensible Firmware Interface (UEFI) specification
1695 * @protocol GUID of the protocol
1696 * @registration registration key passed to the notification function
1697 * @protocol_interface interface implementing the protocol
1698 * @return status code
1700 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1702 void **protocol_interface)
1704 struct list_head *lhandle;
1707 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1709 if (!protocol || !protocol_interface)
1710 return EFI_EXIT(EFI_INVALID_PARAMETER);
1712 EFI_PRINT_GUID("protocol", protocol);
1714 list_for_each(lhandle, &efi_obj_list) {
1715 struct efi_object *efiobj;
1717 efiobj = list_entry(lhandle, struct efi_object, link);
1718 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1719 struct efi_handler *handler = &efiobj->protocols[i];
1723 if (!guidcmp(handler->guid, protocol)) {
1724 *protocol_interface =
1725 handler->protocol_interface;
1726 return EFI_EXIT(EFI_SUCCESS);
1730 *protocol_interface = NULL;
1732 return EFI_EXIT(EFI_NOT_FOUND);
1736 * Install multiple protocol interfaces.
1738 * This function implements the MultipleProtocolInterfaces service.
1739 * See the Unified Extensible Firmware Interface (UEFI) specification
1742 * @handle handle on which the protocol interfaces shall be installed
1743 * @... NULL terminated argument list with pairs of protocol GUIDS and
1745 * @return status code
1747 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1750 EFI_ENTRY("%p", handle);
1753 const efi_guid_t *protocol;
1754 void *protocol_interface;
1755 efi_status_t r = EFI_SUCCESS;
1759 return EFI_EXIT(EFI_INVALID_PARAMETER);
1761 va_start(argptr, handle);
1763 protocol = va_arg(argptr, efi_guid_t*);
1766 protocol_interface = va_arg(argptr, void*);
1767 r = EFI_CALL(efi_install_protocol_interface(
1769 EFI_NATIVE_INTERFACE,
1770 protocol_interface));
1771 if (r != EFI_SUCCESS)
1776 if (r == EFI_SUCCESS)
1779 /* If an error occured undo all changes. */
1780 va_start(argptr, handle);
1782 protocol = va_arg(argptr, efi_guid_t*);
1783 protocol_interface = va_arg(argptr, void*);
1784 efi_uninstall_protocol_interface(handle, protocol,
1785 protocol_interface);
1793 * Uninstall multiple protocol interfaces.
1795 * This function implements the UninstallMultipleProtocolInterfaces service.
1796 * See the Unified Extensible Firmware Interface (UEFI) specification
1799 * @handle handle from which the protocol interfaces shall be removed
1800 * @... NULL terminated argument list with pairs of protocol GUIDS and
1802 * @return status code
1804 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1807 EFI_ENTRY("%p", handle);
1808 return EFI_EXIT(EFI_INVALID_PARAMETER);
1812 * Calculate cyclic redundancy code.
1814 * This function implements the CalculateCrc32 service.
1815 * See the Unified Extensible Firmware Interface (UEFI) specification
1818 * @data buffer with data
1819 * @data_size size of buffer in bytes
1820 * @crc32_p cyclic redundancy code
1821 * @return status code
1823 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1824 unsigned long data_size,
1827 EFI_ENTRY("%p, %ld", data, data_size);
1828 *crc32_p = crc32(0, data, data_size);
1829 return EFI_EXIT(EFI_SUCCESS);
1835 * This function implements the CopyMem service.
1836 * See the Unified Extensible Firmware Interface (UEFI) specification
1839 * @destination destination of the copy operation
1840 * @source source of the copy operation
1841 * @length number of bytes to copy
1843 static void EFIAPI efi_copy_mem(void *destination, const void *source,
1846 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
1847 memcpy(destination, source, length);
1848 EFI_EXIT(EFI_SUCCESS);
1852 * Fill memory with a byte value.
1854 * This function implements the SetMem service.
1855 * See the Unified Extensible Firmware Interface (UEFI) specification
1858 * @buffer buffer to fill
1859 * @size size of buffer in bytes
1860 * @value byte to copy to the buffer
1862 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
1864 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
1865 memset(buffer, value, size);
1866 EFI_EXIT(EFI_SUCCESS);
1870 * Open protocol interface on a handle.
1872 * This function implements the OpenProtocol interface.
1873 * See the Unified Extensible Firmware Interface (UEFI) specification
1876 * @handle handle on which the protocol shall be opened
1877 * @protocol GUID of the protocol
1878 * @protocol_interface interface implementing the protocol
1879 * @agent_handle handle of the driver
1880 * @controller_handle handle of the controller
1881 * @attributes attributes indicating how to open the protocol
1882 * @return status code
1884 static efi_status_t EFIAPI efi_open_protocol(
1885 void *handle, const efi_guid_t *protocol,
1886 void **protocol_interface, void *agent_handle,
1887 void *controller_handle, uint32_t attributes)
1889 struct list_head *lhandle;
1891 efi_status_t r = EFI_INVALID_PARAMETER;
1893 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
1894 protocol_interface, agent_handle, controller_handle,
1897 if (!handle || !protocol ||
1898 (!protocol_interface && attributes !=
1899 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1903 EFI_PRINT_GUID("protocol", protocol);
1905 switch (attributes) {
1906 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1907 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1908 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1910 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1911 if (controller_handle == handle)
1913 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1914 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1915 if (controller_handle == NULL)
1917 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1918 if (agent_handle == NULL)
1925 list_for_each(lhandle, &efi_obj_list) {
1926 struct efi_object *efiobj;
1927 efiobj = list_entry(lhandle, struct efi_object, link);
1929 if (efiobj->handle != handle)
1932 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1933 struct efi_handler *handler = &efiobj->protocols[i];
1934 const efi_guid_t *hprotocol = handler->guid;
1937 if (!guidcmp(hprotocol, protocol)) {
1939 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1940 *protocol_interface =
1941 handler->protocol_interface;
1951 r = EFI_UNSUPPORTED;
1957 * Get interface of a protocol on a handle.
1959 * This function implements the HandleProtocol service.
1960 * See the Unified Extensible Firmware Interface (UEFI) specification
1963 * @handle handle on which the protocol shall be opened
1964 * @protocol GUID of the protocol
1965 * @protocol_interface interface implementing the protocol
1966 * @return status code
1968 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1969 const efi_guid_t *protocol,
1970 void **protocol_interface)
1972 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1973 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1976 static const struct efi_boot_services efi_boot_services = {
1978 .headersize = sizeof(struct efi_table_hdr),
1980 .raise_tpl = efi_raise_tpl,
1981 .restore_tpl = efi_restore_tpl,
1982 .allocate_pages = efi_allocate_pages_ext,
1983 .free_pages = efi_free_pages_ext,
1984 .get_memory_map = efi_get_memory_map_ext,
1985 .allocate_pool = efi_allocate_pool_ext,
1986 .free_pool = efi_free_pool_ext,
1987 .create_event = efi_create_event_ext,
1988 .set_timer = efi_set_timer_ext,
1989 .wait_for_event = efi_wait_for_event,
1990 .signal_event = efi_signal_event_ext,
1991 .close_event = efi_close_event,
1992 .check_event = efi_check_event,
1993 .install_protocol_interface = efi_install_protocol_interface,
1994 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1995 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1996 .handle_protocol = efi_handle_protocol,
1998 .register_protocol_notify = efi_register_protocol_notify,
1999 .locate_handle = efi_locate_handle_ext,
2000 .locate_device_path = efi_locate_device_path,
2001 .install_configuration_table = efi_install_configuration_table_ext,
2002 .load_image = efi_load_image,
2003 .start_image = efi_start_image,
2005 .unload_image = efi_unload_image,
2006 .exit_boot_services = efi_exit_boot_services,
2007 .get_next_monotonic_count = efi_get_next_monotonic_count,
2009 .set_watchdog_timer = efi_set_watchdog_timer,
2010 .connect_controller = efi_connect_controller,
2011 .disconnect_controller = efi_disconnect_controller,
2012 .open_protocol = efi_open_protocol,
2013 .close_protocol = efi_close_protocol,
2014 .open_protocol_information = efi_open_protocol_information,
2015 .protocols_per_handle = efi_protocols_per_handle,
2016 .locate_handle_buffer = efi_locate_handle_buffer,
2017 .locate_protocol = efi_locate_protocol,
2018 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2019 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2020 .calculate_crc32 = efi_calculate_crc32,
2021 .copy_mem = efi_copy_mem,
2022 .set_mem = efi_set_mem,
2026 static uint16_t __efi_runtime_data firmware_vendor[] =
2027 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2029 struct efi_system_table __efi_runtime_data systab = {
2031 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2032 .revision = 0x20005, /* 2.5 */
2033 .headersize = sizeof(struct efi_table_hdr),
2035 .fw_vendor = (long)firmware_vendor,
2036 .con_in = (void*)&efi_con_in,
2037 .con_out = (void*)&efi_con_out,
2038 .std_err = (void*)&efi_con_out,
2039 .runtime = (void*)&efi_runtime_services,
2040 .boottime = (void*)&efi_boot_services,
2042 .tables = (void*)efi_conf_table,