1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
11 #include <environment.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* Task priority level */
22 static efi_uintn_t efi_tpl = TPL_APPLICATION;
24 /* This list contains all the EFI objects our payload has access to */
25 LIST_HEAD(efi_obj_list);
27 /* List of all events */
28 LIST_HEAD(efi_events);
30 /* List of all events registered by RegisterProtocolNotify() */
31 LIST_HEAD(efi_register_notify_events);
33 /* Handle of the currently executing image */
34 static efi_handle_t current_image;
37 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
38 * we need to do trickery with caches. Since we don't want to break the EFI
39 * aware boot path, only apply hacks when loading exiting directly (breaking
40 * direct Linux EFI booting along the way - oh well).
42 static bool efi_is_direct_boot = true;
46 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
51 static volatile void *efi_gd, *app_gd;
54 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
55 static int entry_count = 1;
56 static int nesting_level;
57 /* GUID of the device tree table */
58 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
59 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60 const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
63 /* event group ExitBootServices() invoked */
64 const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66 /* event group SetVirtualAddressMap() invoked */
67 const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69 /* event group memory map changed */
70 const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72 /* event group boot manager about to boot */
73 const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75 /* event group ResetSystem() invoked (before ExitBootServices) */
76 const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
79 static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
84 /* Called on every callback entry */
85 int __efi_entry_check(void)
87 int ret = entry_count++ == 0;
96 /* Called on every callback exit */
97 int __efi_exit_check(void)
99 int ret = --entry_count == 0;
106 /* Called from do_bootefi_exec() */
107 void efi_save_gd(void)
115 * Special case handler for error/abort that just forces things back to u-boot
116 * world so we can dump out an abort message, without any care about returning
117 * back to UEFI world.
119 void efi_restore_gd(void)
122 /* Only restore if we're already in EFI context */
130 * indent_string() - returns a string for indenting with two spaces per level
131 * @level: indent level
133 * A maximum of ten indent levels is supported. Higher indent levels will be
136 * Return: A string for indenting with two spaces per level is
139 static const char *indent_string(int level)
141 const char *indent = " ";
142 const int max = strlen(indent);
144 level = min(max, level * 2);
145 return &indent[max - level];
148 const char *__efi_nesting(void)
150 return indent_string(nesting_level);
153 const char *__efi_nesting_inc(void)
155 return indent_string(nesting_level++);
158 const char *__efi_nesting_dec(void)
160 return indent_string(--nesting_level);
164 * efi_queue_event() - queue an EFI event
165 * @event: event to signal
166 * @check_tpl: check the TPL level
168 * This function queues the notification function of the event for future
171 * The notification function is called if the task priority level of the event
172 * is higher than the current task priority level.
174 * For the SignalEvent service see efi_signal_event_ext.
177 static void efi_queue_event(struct efi_event *event, bool check_tpl)
179 if (event->notify_function) {
180 event->is_queued = true;
182 if (check_tpl && efi_tpl >= event->notify_tpl)
184 event->is_queued = false;
185 EFI_CALL_VOID(event->notify_function(event,
186 event->notify_context));
188 event->is_queued = false;
193 * is_valid_tpl() - check if the task priority level is valid
195 * @tpl: TPL level to check
196 * Return: status code
198 efi_status_t is_valid_tpl(efi_uintn_t tpl)
201 case TPL_APPLICATION:
207 return EFI_INVALID_PARAMETER;
212 * efi_signal_event() - signal an EFI event
213 * @event: event to signal
214 * @check_tpl: check the TPL level
216 * This function signals an event. If the event belongs to an event group all
217 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
218 * their notification function is queued.
220 * For the SignalEvent service see efi_signal_event_ext.
222 void efi_signal_event(struct efi_event *event, bool check_tpl)
224 if (event->is_signaled)
227 struct efi_event *evt;
230 * The signaled state has to set before executing any
231 * notification function
233 list_for_each_entry(evt, &efi_events, link) {
234 if (!evt->group || guidcmp(evt->group, event->group))
236 if (evt->is_signaled)
238 evt->is_signaled = true;
239 if (evt->type & EVT_NOTIFY_SIGNAL &&
240 evt->notify_function)
241 evt->is_queued = true;
243 list_for_each_entry(evt, &efi_events, link) {
244 if (!evt->group || guidcmp(evt->group, event->group))
247 efi_queue_event(evt, check_tpl);
250 event->is_signaled = true;
251 if (event->type & EVT_NOTIFY_SIGNAL)
252 efi_queue_event(event, check_tpl);
257 * efi_raise_tpl() - raise the task priority level
258 * @new_tpl: new value of the task priority level
260 * This function implements the RaiseTpl service.
262 * See the Unified Extensible Firmware Interface (UEFI) specification for
265 * Return: old value of the task priority level
267 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
269 efi_uintn_t old_tpl = efi_tpl;
271 EFI_ENTRY("0x%zx", new_tpl);
273 if (new_tpl < efi_tpl)
274 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
276 if (efi_tpl > TPL_HIGH_LEVEL)
277 efi_tpl = TPL_HIGH_LEVEL;
279 EFI_EXIT(EFI_SUCCESS);
284 * efi_restore_tpl() - lower the task priority level
285 * @old_tpl: value of the task priority level to be restored
287 * This function implements the RestoreTpl service.
289 * See the Unified Extensible Firmware Interface (UEFI) specification for
292 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
294 EFI_ENTRY("0x%zx", old_tpl);
296 if (old_tpl > efi_tpl)
297 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
299 if (efi_tpl > TPL_HIGH_LEVEL)
300 efi_tpl = TPL_HIGH_LEVEL;
303 * Lowering the TPL may have made queued events eligible for execution.
307 EFI_EXIT(EFI_SUCCESS);
311 * efi_allocate_pages_ext() - allocate memory pages
312 * @type: type of allocation to be performed
313 * @memory_type: usage type of the allocated memory
314 * @pages: number of pages to be allocated
315 * @memory: allocated memory
317 * This function implements the AllocatePages service.
319 * See the Unified Extensible Firmware Interface (UEFI) specification for
322 * Return: status code
324 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
330 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
331 r = efi_allocate_pages(type, memory_type, pages, memory);
336 * efi_free_pages_ext() - Free memory pages.
337 * @memory: start of the memory area to be freed
338 * @pages: number of pages to be freed
340 * This function implements the FreePages service.
342 * See the Unified Extensible Firmware Interface (UEFI) specification for
345 * Return: status code
347 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
352 EFI_ENTRY("%llx, 0x%zx", memory, pages);
353 r = efi_free_pages(memory, pages);
358 * efi_get_memory_map_ext() - get map describing memory usage
359 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
360 * on exit the size of the copied memory map
361 * @memory_map: buffer to which the memory map is written
362 * @map_key: key for the memory map
363 * @descriptor_size: size of an individual memory descriptor
364 * @descriptor_version: version number of the memory descriptor structure
366 * This function implements the GetMemoryMap service.
368 * See the Unified Extensible Firmware Interface (UEFI) specification for
371 * Return: status code
373 static efi_status_t EFIAPI efi_get_memory_map_ext(
374 efi_uintn_t *memory_map_size,
375 struct efi_mem_desc *memory_map,
376 efi_uintn_t *map_key,
377 efi_uintn_t *descriptor_size,
378 uint32_t *descriptor_version)
382 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
383 map_key, descriptor_size, descriptor_version);
384 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
385 descriptor_size, descriptor_version);
390 * efi_allocate_pool_ext() - allocate memory from pool
391 * @pool_type: type of the pool from which memory is to be allocated
392 * @size: number of bytes to be allocated
393 * @buffer: allocated memory
395 * This function implements the AllocatePool service.
397 * See the Unified Extensible Firmware Interface (UEFI) specification for
400 * Return: status code
402 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
408 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
409 r = efi_allocate_pool(pool_type, size, buffer);
414 * efi_free_pool_ext() - free memory from pool
415 * @buffer: start of memory to be freed
417 * This function implements the FreePool service.
419 * See the Unified Extensible Firmware Interface (UEFI) specification for
422 * Return: status code
424 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
428 EFI_ENTRY("%p", buffer);
429 r = efi_free_pool(buffer);
434 * efi_add_handle() - add a new handle to the object list
436 * @handle: handle to be added
438 * The protocols list is initialized. The handle is added to the list of known
441 void efi_add_handle(efi_handle_t handle)
445 INIT_LIST_HEAD(&handle->protocols);
446 list_add_tail(&handle->link, &efi_obj_list);
450 * efi_create_handle() - create handle
451 * @handle: new handle
453 * Return: status code
455 efi_status_t efi_create_handle(efi_handle_t *handle)
457 struct efi_object *obj;
459 obj = calloc(1, sizeof(struct efi_object));
461 return EFI_OUT_OF_RESOURCES;
470 * efi_search_protocol() - find a protocol on a handle.
472 * @protocol_guid: GUID of the protocol
473 * @handler: reference to the protocol
475 * Return: status code
477 efi_status_t efi_search_protocol(const efi_handle_t handle,
478 const efi_guid_t *protocol_guid,
479 struct efi_handler **handler)
481 struct efi_object *efiobj;
482 struct list_head *lhandle;
484 if (!handle || !protocol_guid)
485 return EFI_INVALID_PARAMETER;
486 efiobj = efi_search_obj(handle);
488 return EFI_INVALID_PARAMETER;
489 list_for_each(lhandle, &efiobj->protocols) {
490 struct efi_handler *protocol;
492 protocol = list_entry(lhandle, struct efi_handler, link);
493 if (!guidcmp(protocol->guid, protocol_guid)) {
499 return EFI_NOT_FOUND;
503 * efi_remove_protocol() - delete protocol from a handle
504 * @handle: handle from which the protocol shall be deleted
505 * @protocol: GUID of the protocol to be deleted
506 * @protocol_interface: interface of the protocol implementation
508 * Return: status code
510 efi_status_t efi_remove_protocol(const efi_handle_t handle,
511 const efi_guid_t *protocol,
512 void *protocol_interface)
514 struct efi_handler *handler;
517 ret = efi_search_protocol(handle, protocol, &handler);
518 if (ret != EFI_SUCCESS)
520 if (handler->protocol_interface != protocol_interface)
521 return EFI_NOT_FOUND;
522 list_del(&handler->link);
528 * efi_remove_all_protocols() - delete all protocols from a handle
529 * @handle: handle from which the protocols shall be deleted
531 * Return: status code
533 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
535 struct efi_object *efiobj;
536 struct efi_handler *protocol;
537 struct efi_handler *pos;
539 efiobj = efi_search_obj(handle);
541 return EFI_INVALID_PARAMETER;
542 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
545 ret = efi_remove_protocol(handle, protocol->guid,
546 protocol->protocol_interface);
547 if (ret != EFI_SUCCESS)
554 * efi_delete_handle() - delete handle
556 * @obj: handle to delete
558 void efi_delete_handle(efi_handle_t handle)
562 efi_remove_all_protocols(handle);
563 list_del(&handle->link);
568 * efi_is_event() - check if a pointer is a valid event
569 * @event: pointer to check
571 * Return: status code
573 static efi_status_t efi_is_event(const struct efi_event *event)
575 const struct efi_event *evt;
578 return EFI_INVALID_PARAMETER;
579 list_for_each_entry(evt, &efi_events, link) {
583 return EFI_INVALID_PARAMETER;
587 * efi_create_event() - create an event
588 * @type: type of the event to create
589 * @notify_tpl: task priority level of the event
590 * @notify_function: notification function of the event
591 * @notify_context: pointer passed to the notification function
592 * @group: event group
593 * @event: created event
595 * This function is used inside U-Boot code to create an event.
597 * For the API function implementing the CreateEvent service see
598 * efi_create_event_ext.
600 * Return: status code
602 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
603 void (EFIAPI *notify_function) (
604 struct efi_event *event,
606 void *notify_context, efi_guid_t *group,
607 struct efi_event **event)
609 struct efi_event *evt;
612 return EFI_INVALID_PARAMETER;
617 case EVT_NOTIFY_SIGNAL:
618 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
619 case EVT_NOTIFY_WAIT:
620 case EVT_TIMER | EVT_NOTIFY_WAIT:
621 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
622 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
625 return EFI_INVALID_PARAMETER;
628 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
629 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
630 return EFI_INVALID_PARAMETER;
632 evt = calloc(1, sizeof(struct efi_event));
634 return EFI_OUT_OF_RESOURCES;
636 evt->notify_tpl = notify_tpl;
637 evt->notify_function = notify_function;
638 evt->notify_context = notify_context;
640 /* Disable timers on boot up */
641 evt->trigger_next = -1ULL;
642 evt->is_queued = false;
643 evt->is_signaled = false;
644 list_add_tail(&evt->link, &efi_events);
650 * efi_create_event_ex() - create an event in a group
651 * @type: type of the event to create
652 * @notify_tpl: task priority level of the event
653 * @notify_function: notification function of the event
654 * @notify_context: pointer passed to the notification function
655 * @event: created event
656 * @event_group: event group
658 * This function implements the CreateEventEx service.
660 * See the Unified Extensible Firmware Interface (UEFI) specification for
663 * Return: status code
665 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
666 void (EFIAPI *notify_function) (
667 struct efi_event *event,
669 void *notify_context,
670 efi_guid_t *event_group,
671 struct efi_event **event)
675 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
676 notify_context, event_group);
679 * The allowable input parameters are the same as in CreateEvent()
680 * except for the following two disallowed event types.
683 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
684 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
685 ret = EFI_INVALID_PARAMETER;
689 ret = efi_create_event(type, notify_tpl, notify_function,
690 notify_context, event_group, event);
692 return EFI_EXIT(ret);
696 * efi_create_event_ext() - create an event
697 * @type: type of the event to create
698 * @notify_tpl: task priority level of the event
699 * @notify_function: notification function of the event
700 * @notify_context: pointer passed to the notification function
701 * @event: created event
703 * This function implements the CreateEvent service.
705 * See the Unified Extensible Firmware Interface (UEFI) specification for
708 * Return: status code
710 static efi_status_t EFIAPI efi_create_event_ext(
711 uint32_t type, efi_uintn_t notify_tpl,
712 void (EFIAPI *notify_function) (
713 struct efi_event *event,
715 void *notify_context, struct efi_event **event)
717 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
719 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
720 notify_context, NULL, event));
724 * efi_timer_check() - check if a timer event has occurred
726 * Check if a timer event has occurred or a queued notification function should
729 * Our timers have to work without interrupts, so we check whenever keyboard
730 * input or disk accesses happen if enough time elapsed for them to fire.
732 void efi_timer_check(void)
734 struct efi_event *evt;
735 u64 now = timer_get_us();
737 list_for_each_entry(evt, &efi_events, link) {
739 efi_queue_event(evt, true);
740 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
742 switch (evt->trigger_type) {
743 case EFI_TIMER_RELATIVE:
744 evt->trigger_type = EFI_TIMER_STOP;
746 case EFI_TIMER_PERIODIC:
747 evt->trigger_next += evt->trigger_time;
752 evt->is_signaled = false;
753 efi_signal_event(evt, true);
759 * efi_set_timer() - set the trigger time for a timer event or stop the event
760 * @event: event for which the timer is set
761 * @type: type of the timer
762 * @trigger_time: trigger period in multiples of 100 ns
764 * This is the function for internal usage in U-Boot. For the API function
765 * implementing the SetTimer service see efi_set_timer_ext.
767 * Return: status code
769 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
770 uint64_t trigger_time)
772 /* Check that the event is valid */
773 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
774 return EFI_INVALID_PARAMETER;
777 * The parameter defines a multiple of 100 ns.
778 * We use multiples of 1000 ns. So divide by 10.
780 do_div(trigger_time, 10);
784 event->trigger_next = -1ULL;
786 case EFI_TIMER_PERIODIC:
787 case EFI_TIMER_RELATIVE:
788 event->trigger_next = timer_get_us() + trigger_time;
791 return EFI_INVALID_PARAMETER;
793 event->trigger_type = type;
794 event->trigger_time = trigger_time;
795 event->is_signaled = false;
800 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
802 * @event: event for which the timer is set
803 * @type: type of the timer
804 * @trigger_time: trigger period in multiples of 100 ns
806 * This function implements the SetTimer service.
808 * See the Unified Extensible Firmware Interface (UEFI) specification for
812 * Return: status code
814 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
815 enum efi_timer_delay type,
816 uint64_t trigger_time)
818 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
819 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
823 * efi_wait_for_event() - wait for events to be signaled
824 * @num_events: number of events to be waited for
825 * @event: events to be waited for
826 * @index: index of the event that was signaled
828 * This function implements the WaitForEvent service.
830 * See the Unified Extensible Firmware Interface (UEFI) specification for
833 * Return: status code
835 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
836 struct efi_event **event,
841 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
843 /* Check parameters */
844 if (!num_events || !event)
845 return EFI_EXIT(EFI_INVALID_PARAMETER);
847 if (efi_tpl != TPL_APPLICATION)
848 return EFI_EXIT(EFI_UNSUPPORTED);
849 for (i = 0; i < num_events; ++i) {
850 if (efi_is_event(event[i]) != EFI_SUCCESS)
851 return EFI_EXIT(EFI_INVALID_PARAMETER);
852 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
853 return EFI_EXIT(EFI_INVALID_PARAMETER);
854 if (!event[i]->is_signaled)
855 efi_queue_event(event[i], true);
858 /* Wait for signal */
860 for (i = 0; i < num_events; ++i) {
861 if (event[i]->is_signaled)
864 /* Allow events to occur. */
870 * Reset the signal which is passed to the caller to allow periodic
873 event[i]->is_signaled = false;
877 return EFI_EXIT(EFI_SUCCESS);
881 * efi_signal_event_ext() - signal an EFI event
882 * @event: event to signal
884 * This function implements the SignalEvent service.
886 * See the Unified Extensible Firmware Interface (UEFI) specification for
889 * This functions sets the signaled state of the event and queues the
890 * notification function for execution.
892 * Return: status code
894 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
896 EFI_ENTRY("%p", event);
897 if (efi_is_event(event) != EFI_SUCCESS)
898 return EFI_EXIT(EFI_INVALID_PARAMETER);
899 efi_signal_event(event, true);
900 return EFI_EXIT(EFI_SUCCESS);
904 * efi_close_event() - close an EFI event
905 * @event: event to close
907 * This function implements the CloseEvent service.
909 * See the Unified Extensible Firmware Interface (UEFI) specification for
912 * Return: status code
914 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
916 struct efi_register_notify_event *item, *next;
918 EFI_ENTRY("%p", event);
919 if (efi_is_event(event) != EFI_SUCCESS)
920 return EFI_EXIT(EFI_INVALID_PARAMETER);
922 /* Remove protocol notify registrations for the event */
923 list_for_each_entry_safe(item, next, &efi_register_notify_events,
925 if (event == item->event) {
926 struct efi_protocol_notification *hitem, *hnext;
928 /* Remove signaled handles */
929 list_for_each_entry_safe(hitem, hnext, &item->handles,
931 list_del(&hitem->link);
934 list_del(&item->link);
939 list_del(&event->link);
941 return EFI_EXIT(EFI_SUCCESS);
945 * efi_check_event() - check if an event is signaled
946 * @event: event to check
948 * This function implements the CheckEvent service.
950 * See the Unified Extensible Firmware Interface (UEFI) specification for
953 * If an event is not signaled yet, the notification function is queued. The
954 * signaled state is cleared.
956 * Return: status code
958 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
960 EFI_ENTRY("%p", event);
962 if (efi_is_event(event) != EFI_SUCCESS ||
963 event->type & EVT_NOTIFY_SIGNAL)
964 return EFI_EXIT(EFI_INVALID_PARAMETER);
965 if (!event->is_signaled)
966 efi_queue_event(event, true);
967 if (event->is_signaled) {
968 event->is_signaled = false;
969 return EFI_EXIT(EFI_SUCCESS);
971 return EFI_EXIT(EFI_NOT_READY);
975 * efi_search_obj() - find the internal EFI object for a handle
976 * @handle: handle to find
980 struct efi_object *efi_search_obj(const efi_handle_t handle)
982 struct efi_object *efiobj;
987 list_for_each_entry(efiobj, &efi_obj_list, link) {
988 if (efiobj == handle)
995 * efi_open_protocol_info_entry() - create open protocol info entry and add it
997 * @handler: handler of a protocol
999 * Return: open protocol info entry
1001 static struct efi_open_protocol_info_entry *efi_create_open_info(
1002 struct efi_handler *handler)
1004 struct efi_open_protocol_info_item *item;
1006 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1009 /* Append the item to the open protocol info list. */
1010 list_add_tail(&item->link, &handler->open_infos);
1016 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1017 * @item: open protocol info entry to delete
1019 * Return: status code
1021 static efi_status_t efi_delete_open_info(
1022 struct efi_open_protocol_info_item *item)
1024 list_del(&item->link);
1030 * efi_add_protocol() - install new protocol on a handle
1031 * @handle: handle on which the protocol shall be installed
1032 * @protocol: GUID of the protocol to be installed
1033 * @protocol_interface: interface of the protocol implementation
1035 * Return: status code
1037 efi_status_t efi_add_protocol(const efi_handle_t handle,
1038 const efi_guid_t *protocol,
1039 void *protocol_interface)
1041 struct efi_object *efiobj;
1042 struct efi_handler *handler;
1044 struct efi_register_notify_event *event;
1046 efiobj = efi_search_obj(handle);
1048 return EFI_INVALID_PARAMETER;
1049 ret = efi_search_protocol(handle, protocol, NULL);
1050 if (ret != EFI_NOT_FOUND)
1051 return EFI_INVALID_PARAMETER;
1052 handler = calloc(1, sizeof(struct efi_handler));
1054 return EFI_OUT_OF_RESOURCES;
1055 handler->guid = protocol;
1056 handler->protocol_interface = protocol_interface;
1057 INIT_LIST_HEAD(&handler->open_infos);
1058 list_add_tail(&handler->link, &efiobj->protocols);
1060 /* Notify registered events */
1061 list_for_each_entry(event, &efi_register_notify_events, link) {
1062 if (!guidcmp(protocol, &event->protocol)) {
1063 struct efi_protocol_notification *notif;
1065 notif = calloc(1, sizeof(*notif));
1067 list_del(&handler->link);
1069 return EFI_OUT_OF_RESOURCES;
1071 notif->handle = handle;
1072 list_add_tail(¬if->link, &event->handles);
1073 event->event->is_signaled = false;
1074 efi_signal_event(event->event, true);
1078 if (!guidcmp(&efi_guid_device_path, protocol))
1079 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1084 * efi_install_protocol_interface() - install protocol interface
1085 * @handle: handle on which the protocol shall be installed
1086 * @protocol: GUID of the protocol to be installed
1087 * @protocol_interface_type: type of the interface to be installed,
1088 * always EFI_NATIVE_INTERFACE
1089 * @protocol_interface: interface of the protocol implementation
1091 * This function implements the InstallProtocolInterface service.
1093 * See the Unified Extensible Firmware Interface (UEFI) specification for
1096 * Return: status code
1098 static efi_status_t EFIAPI efi_install_protocol_interface(
1099 efi_handle_t *handle, const efi_guid_t *protocol,
1100 int protocol_interface_type, void *protocol_interface)
1104 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1105 protocol_interface);
1107 if (!handle || !protocol ||
1108 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1109 r = EFI_INVALID_PARAMETER;
1113 /* Create new handle if requested. */
1115 r = efi_create_handle(handle);
1116 if (r != EFI_SUCCESS)
1118 EFI_PRINT("new handle %p\n", *handle);
1120 EFI_PRINT("handle %p\n", *handle);
1122 /* Add new protocol */
1123 r = efi_add_protocol(*handle, protocol, protocol_interface);
1129 * efi_get_drivers() - get all drivers associated to a controller
1130 * @handle: handle of the controller
1131 * @protocol: protocol GUID (optional)
1132 * @number_of_drivers: number of child controllers
1133 * @driver_handle_buffer: handles of the the drivers
1135 * The allocated buffer has to be freed with free().
1137 * Return: status code
1139 static efi_status_t efi_get_drivers(efi_handle_t handle,
1140 const efi_guid_t *protocol,
1141 efi_uintn_t *number_of_drivers,
1142 efi_handle_t **driver_handle_buffer)
1144 struct efi_handler *handler;
1145 struct efi_open_protocol_info_item *item;
1146 efi_uintn_t count = 0, i;
1149 /* Count all driver associations */
1150 list_for_each_entry(handler, &handle->protocols, link) {
1151 if (protocol && guidcmp(handler->guid, protocol))
1153 list_for_each_entry(item, &handler->open_infos, link) {
1154 if (item->info.attributes &
1155 EFI_OPEN_PROTOCOL_BY_DRIVER)
1159 *number_of_drivers = 0;
1161 *driver_handle_buffer = NULL;
1165 * Create buffer. In case of duplicate driver assignments the buffer
1166 * will be too large. But that does not harm.
1168 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1169 if (!*driver_handle_buffer)
1170 return EFI_OUT_OF_RESOURCES;
1171 /* Collect unique driver handles */
1172 list_for_each_entry(handler, &handle->protocols, link) {
1173 if (protocol && guidcmp(handler->guid, protocol))
1175 list_for_each_entry(item, &handler->open_infos, link) {
1176 if (item->info.attributes &
1177 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1178 /* Check this is a new driver */
1180 for (i = 0; i < *number_of_drivers; ++i) {
1181 if ((*driver_handle_buffer)[i] ==
1182 item->info.agent_handle)
1185 /* Copy handle to buffer */
1187 i = (*number_of_drivers)++;
1188 (*driver_handle_buffer)[i] =
1189 item->info.agent_handle;
1198 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1199 * @handle: handle of the controller
1200 * @protocol: protocol GUID (optional)
1201 * @child_handle: handle of the child to destroy
1203 * This function implements the DisconnectController service.
1205 * See the Unified Extensible Firmware Interface (UEFI) specification for
1208 * Return: status code
1210 static efi_status_t efi_disconnect_all_drivers
1211 (efi_handle_t handle,
1212 const efi_guid_t *protocol,
1213 efi_handle_t child_handle)
1215 efi_uintn_t number_of_drivers;
1216 efi_handle_t *driver_handle_buffer;
1217 efi_status_t r, ret;
1219 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1220 &driver_handle_buffer);
1221 if (ret != EFI_SUCCESS)
1223 if (!number_of_drivers)
1225 ret = EFI_NOT_FOUND;
1226 while (number_of_drivers) {
1227 r = EFI_CALL(efi_disconnect_controller(
1229 driver_handle_buffer[--number_of_drivers],
1231 if (r == EFI_SUCCESS)
1234 free(driver_handle_buffer);
1239 * efi_uninstall_protocol() - uninstall protocol interface
1241 * @handle: handle from which the protocol shall be removed
1242 * @protocol: GUID of the protocol to be removed
1243 * @protocol_interface: interface to be removed
1245 * This function DOES NOT delete a handle without installed protocol.
1247 * Return: status code
1249 static efi_status_t efi_uninstall_protocol
1250 (efi_handle_t handle, const efi_guid_t *protocol,
1251 void *protocol_interface)
1253 struct efi_object *efiobj;
1254 struct efi_handler *handler;
1255 struct efi_open_protocol_info_item *item;
1256 struct efi_open_protocol_info_item *pos;
1260 efiobj = efi_search_obj(handle);
1262 r = EFI_INVALID_PARAMETER;
1265 /* Find the protocol on the handle */
1266 r = efi_search_protocol(handle, protocol, &handler);
1267 if (r != EFI_SUCCESS)
1269 /* Disconnect controllers */
1270 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1271 /* Close protocol */
1272 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1273 if (item->info.attributes ==
1274 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1275 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1276 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1277 list_del(&item->link);
1279 if (!list_empty(&handler->open_infos)) {
1280 r = EFI_ACCESS_DENIED;
1283 r = efi_remove_protocol(handle, protocol, protocol_interface);
1289 * efi_uninstall_protocol_interface() - uninstall protocol interface
1290 * @handle: handle from which the protocol shall be removed
1291 * @protocol: GUID of the protocol to be removed
1292 * @protocol_interface: interface to be removed
1294 * This function implements the UninstallProtocolInterface service.
1296 * See the Unified Extensible Firmware Interface (UEFI) specification for
1299 * Return: status code
1301 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1302 (efi_handle_t handle, const efi_guid_t *protocol,
1303 void *protocol_interface)
1307 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1309 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1310 if (ret != EFI_SUCCESS)
1313 /* If the last protocol has been removed, delete the handle. */
1314 if (list_empty(&handle->protocols)) {
1315 list_del(&handle->link);
1319 return EFI_EXIT(ret);
1323 * efi_register_protocol_notify() - register an event for notification when a
1324 * protocol is installed.
1325 * @protocol: GUID of the protocol whose installation shall be notified
1326 * @event: event to be signaled upon installation of the protocol
1327 * @registration: key for retrieving the registration information
1329 * This function implements the RegisterProtocolNotify service.
1330 * See the Unified Extensible Firmware Interface (UEFI) specification
1333 * Return: status code
1335 static efi_status_t EFIAPI efi_register_protocol_notify(
1336 const efi_guid_t *protocol,
1337 struct efi_event *event,
1338 void **registration)
1340 struct efi_register_notify_event *item;
1341 efi_status_t ret = EFI_SUCCESS;
1343 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1345 if (!protocol || !event || !registration) {
1346 ret = EFI_INVALID_PARAMETER;
1350 item = calloc(1, sizeof(struct efi_register_notify_event));
1352 ret = EFI_OUT_OF_RESOURCES;
1356 item->event = event;
1357 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
1358 INIT_LIST_HEAD(&item->handles);
1360 list_add_tail(&item->link, &efi_register_notify_events);
1362 *registration = item;
1364 return EFI_EXIT(ret);
1368 * efi_search() - determine if an EFI handle implements a protocol
1369 * @search_type: selection criterion
1370 * @protocol: GUID of the protocol
1371 * @search_key: registration key
1374 * See the documentation of the LocateHandle service in the UEFI specification.
1376 * Return: 0 if the handle implements the protocol
1378 static int efi_search(enum efi_locate_search_type search_type,
1379 const efi_guid_t *protocol, efi_handle_t handle)
1383 switch (search_type) {
1387 ret = efi_search_protocol(handle, protocol, NULL);
1388 return (ret != EFI_SUCCESS);
1390 /* Invalid search type */
1396 * efi_check_register_notify_event() - check if registration key is valid
1398 * Check that a pointer is a valid registration key as returned by
1399 * RegisterProtocolNotify().
1401 * @key: registration key
1402 * Return: valid registration key or NULL
1404 static struct efi_register_notify_event *efi_check_register_notify_event
1407 struct efi_register_notify_event *event;
1409 list_for_each_entry(event, &efi_register_notify_events, link) {
1410 if (event == (struct efi_register_notify_event *)key)
1417 * efi_locate_handle() - locate handles implementing a protocol
1419 * @search_type: selection criterion
1420 * @protocol: GUID of the protocol
1421 * @search_key: registration key
1422 * @buffer_size: size of the buffer to receive the handles in bytes
1423 * @buffer: buffer to receive the relevant handles
1425 * This function is meant for U-Boot internal calls. For the API implementation
1426 * of the LocateHandle service see efi_locate_handle_ext.
1428 * Return: status code
1430 static efi_status_t efi_locate_handle(
1431 enum efi_locate_search_type search_type,
1432 const efi_guid_t *protocol, void *search_key,
1433 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1435 struct efi_object *efiobj;
1436 efi_uintn_t size = 0;
1437 struct efi_register_notify_event *event;
1438 struct efi_protocol_notification *handle = NULL;
1440 /* Check parameters */
1441 switch (search_type) {
1444 case BY_REGISTER_NOTIFY:
1446 return EFI_INVALID_PARAMETER;
1447 /* Check that the registration key is valid */
1448 event = efi_check_register_notify_event(search_key);
1450 return EFI_INVALID_PARAMETER;
1454 return EFI_INVALID_PARAMETER;
1457 return EFI_INVALID_PARAMETER;
1460 /* Count how much space we need */
1461 if (search_type == BY_REGISTER_NOTIFY) {
1462 if (list_empty(&event->handles))
1463 return EFI_NOT_FOUND;
1464 handle = list_first_entry(&event->handles,
1465 struct efi_protocol_notification,
1467 efiobj = handle->handle;
1468 size += sizeof(void *);
1470 list_for_each_entry(efiobj, &efi_obj_list, link) {
1471 if (!efi_search(search_type, protocol, efiobj))
1472 size += sizeof(void *);
1475 return EFI_NOT_FOUND;
1479 return EFI_INVALID_PARAMETER;
1481 if (*buffer_size < size) {
1482 *buffer_size = size;
1483 return EFI_BUFFER_TOO_SMALL;
1486 *buffer_size = size;
1488 /* The buffer size is sufficient but there is no buffer */
1490 return EFI_INVALID_PARAMETER;
1492 /* Then fill the array */
1493 if (search_type == BY_REGISTER_NOTIFY) {
1495 list_del(&handle->link);
1497 list_for_each_entry(efiobj, &efi_obj_list, link) {
1498 if (!efi_search(search_type, protocol, efiobj))
1507 * efi_locate_handle_ext() - locate handles implementing a protocol.
1508 * @search_type: selection criterion
1509 * @protocol: GUID of the protocol
1510 * @search_key: registration key
1511 * @buffer_size: size of the buffer to receive the handles in bytes
1512 * @buffer: buffer to receive the relevant handles
1514 * This function implements the LocateHandle service.
1516 * See the Unified Extensible Firmware Interface (UEFI) specification for
1519 * Return: 0 if the handle implements the protocol
1521 static efi_status_t EFIAPI efi_locate_handle_ext(
1522 enum efi_locate_search_type search_type,
1523 const efi_guid_t *protocol, void *search_key,
1524 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1526 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1527 buffer_size, buffer);
1529 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1530 buffer_size, buffer));
1534 * efi_remove_configuration_table() - collapses configuration table entries,
1537 * @i: index of the table entry to be removed
1539 static void efi_remove_configuration_table(int i)
1541 struct efi_configuration_table *this = &systab.tables[i];
1542 struct efi_configuration_table *next = &systab.tables[i + 1];
1543 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1545 memmove(this, next, (ulong)end - (ulong)next);
1550 * efi_install_configuration_table() - adds, updates, or removes a
1551 * configuration table
1552 * @guid: GUID of the installed table
1553 * @table: table to be installed
1555 * This function is used for internal calls. For the API implementation of the
1556 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1558 * Return: status code
1560 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1563 struct efi_event *evt;
1567 return EFI_INVALID_PARAMETER;
1569 /* Check for GUID override */
1570 for (i = 0; i < systab.nr_tables; i++) {
1571 if (!guidcmp(guid, &systab.tables[i].guid)) {
1573 systab.tables[i].table = table;
1575 efi_remove_configuration_table(i);
1581 return EFI_NOT_FOUND;
1583 /* No override, check for overflow */
1584 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1585 return EFI_OUT_OF_RESOURCES;
1587 /* Add a new entry */
1588 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1589 systab.tables[i].table = table;
1590 systab.nr_tables = i + 1;
1593 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1594 efi_update_table_header_crc32(&systab.hdr);
1596 /* Notify that the configuration table was changed */
1597 list_for_each_entry(evt, &efi_events, link) {
1598 if (evt->group && !guidcmp(evt->group, guid)) {
1599 efi_signal_event(evt, false);
1608 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1609 * configuration table.
1610 * @guid: GUID of the installed table
1611 * @table: table to be installed
1613 * This function implements the InstallConfigurationTable service.
1615 * See the Unified Extensible Firmware Interface (UEFI) specification for
1618 * Return: status code
1620 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1623 EFI_ENTRY("%pUl, %p", guid, table);
1624 return EFI_EXIT(efi_install_configuration_table(guid, table));
1628 * efi_setup_loaded_image() - initialize a loaded image
1630 * Initialize a loaded_image_info and loaded_image_info object with correct
1631 * protocols, boot-device, etc.
1633 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1636 * @device_path: device path of the loaded image
1637 * @file_path: file path of the loaded image
1638 * @handle_ptr: handle of the loaded image
1639 * @info_ptr: loaded image protocol
1640 * Return: status code
1642 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1643 struct efi_device_path *file_path,
1644 struct efi_loaded_image_obj **handle_ptr,
1645 struct efi_loaded_image **info_ptr)
1648 struct efi_loaded_image *info = NULL;
1649 struct efi_loaded_image_obj *obj = NULL;
1650 struct efi_device_path *dp;
1652 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1656 info = calloc(1, sizeof(*info));
1658 return EFI_OUT_OF_RESOURCES;
1659 obj = calloc(1, sizeof(*obj));
1662 return EFI_OUT_OF_RESOURCES;
1664 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1666 /* Add internal object to object list */
1667 efi_add_handle(&obj->header);
1669 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1670 info->file_path = file_path;
1671 info->system_table = &systab;
1674 info->device_handle = efi_dp_find_obj(device_path, NULL);
1676 dp = efi_dp_append(device_path, file_path);
1678 ret = EFI_OUT_OF_RESOURCES;
1684 ret = efi_add_protocol(&obj->header,
1685 &efi_guid_loaded_image_device_path, dp);
1686 if (ret != EFI_SUCCESS)
1690 * When asking for the loaded_image interface, just
1691 * return handle which points to loaded_image_info
1693 ret = efi_add_protocol(&obj->header,
1694 &efi_guid_loaded_image, info);
1695 if (ret != EFI_SUCCESS)
1703 printf("ERROR: Failure to install protocols for loaded image\n");
1704 efi_delete_handle(&obj->header);
1710 * efi_load_image_from_path() - load an image using a file path
1712 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1713 * callers obligation to update the memory type as needed.
1715 * @file_path: the path of the image to load
1716 * @buffer: buffer containing the loaded image
1717 * @size: size of the loaded image
1718 * Return: status code
1721 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1722 void **buffer, efi_uintn_t *size)
1724 struct efi_file_info *info = NULL;
1725 struct efi_file_handle *f;
1726 static efi_status_t ret;
1730 /* In case of failure nothing is returned */
1735 f = efi_file_from_path(file_path);
1737 return EFI_DEVICE_ERROR;
1741 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1743 if (ret != EFI_BUFFER_TOO_SMALL) {
1744 ret = EFI_DEVICE_ERROR;
1749 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1751 if (ret != EFI_SUCCESS)
1755 * When reading the file we do not yet know if it contains an
1756 * application, a boottime driver, or a runtime driver. So here we
1757 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1758 * update the reservation according to the image type.
1760 bs = info->file_size;
1761 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1762 EFI_BOOT_SERVICES_DATA,
1763 efi_size_in_pages(bs), &addr);
1764 if (ret != EFI_SUCCESS) {
1765 ret = EFI_OUT_OF_RESOURCES;
1770 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1771 if (ret != EFI_SUCCESS)
1772 efi_free_pages(addr, efi_size_in_pages(bs));
1773 *buffer = (void *)(uintptr_t)addr;
1776 EFI_CALL(f->close(f));
1782 * efi_load_image() - load an EFI image into memory
1783 * @boot_policy: true for request originating from the boot manager
1784 * @parent_image: the caller's image handle
1785 * @file_path: the path of the image to load
1786 * @source_buffer: memory location from which the image is installed
1787 * @source_size: size of the memory area from which the image is installed
1788 * @image_handle: handle for the newly installed image
1790 * This function implements the LoadImage service.
1792 * See the Unified Extensible Firmware Interface (UEFI) specification
1795 * Return: status code
1797 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1798 efi_handle_t parent_image,
1799 struct efi_device_path *file_path,
1800 void *source_buffer,
1801 efi_uintn_t source_size,
1802 efi_handle_t *image_handle)
1804 struct efi_device_path *dp, *fp;
1805 struct efi_loaded_image *info = NULL;
1806 struct efi_loaded_image_obj **image_obj =
1807 (struct efi_loaded_image_obj **)image_handle;
1811 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1812 file_path, source_buffer, source_size, image_handle);
1814 if (!image_handle || !efi_search_obj(parent_image)) {
1815 ret = EFI_INVALID_PARAMETER;
1819 if (!source_buffer && !file_path) {
1820 ret = EFI_NOT_FOUND;
1823 /* The parent image handle must refer to a loaded image */
1824 if (!parent_image->type) {
1825 ret = EFI_INVALID_PARAMETER;
1829 if (!source_buffer) {
1830 ret = efi_load_image_from_path(file_path, &dest_buffer,
1832 if (ret != EFI_SUCCESS)
1836 ret = EFI_LOAD_ERROR;
1839 dest_buffer = source_buffer;
1841 /* split file_path which contains both the device and file parts */
1842 efi_dp_split_file_path(file_path, &dp, &fp);
1843 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1844 if (ret == EFI_SUCCESS)
1845 ret = efi_load_pe(*image_obj, dest_buffer, info);
1847 /* Release buffer to which file was loaded */
1848 efi_free_pages((uintptr_t)dest_buffer,
1849 efi_size_in_pages(source_size));
1850 if (ret == EFI_SUCCESS) {
1851 info->system_table = &systab;
1852 info->parent_handle = parent_image;
1854 /* The image is invalid. Release all associated resources. */
1855 efi_delete_handle(*image_handle);
1856 *image_handle = NULL;
1860 return EFI_EXIT(ret);
1864 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1866 static void efi_exit_caches(void)
1868 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1870 * Grub on 32bit ARM needs to have caches disabled before jumping into
1871 * a zImage, but does not know of all cache layers. Give it a hand.
1873 if (efi_is_direct_boot)
1874 cleanup_before_linux();
1879 * efi_exit_boot_services() - stop all boot services
1880 * @image_handle: handle of the loaded image
1881 * @map_key: key of the memory map
1883 * This function implements the ExitBootServices service.
1885 * See the Unified Extensible Firmware Interface (UEFI) specification
1888 * All timer events are disabled. For exit boot services events the
1889 * notification function is called. The boot services are disabled in the
1892 * Return: status code
1894 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1895 efi_uintn_t map_key)
1897 struct efi_event *evt;
1899 EFI_ENTRY("%p, %zx", image_handle, map_key);
1901 /* Check that the caller has read the current memory map */
1902 if (map_key != efi_memory_map_key)
1903 return EFI_INVALID_PARAMETER;
1905 /* Make sure that notification functions are not called anymore */
1906 efi_tpl = TPL_HIGH_LEVEL;
1908 /* Check if ExitBootServices has already been called */
1909 if (!systab.boottime)
1910 return EFI_EXIT(EFI_SUCCESS);
1912 /* Add related events to the event group */
1913 list_for_each_entry(evt, &efi_events, link) {
1914 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1915 evt->group = &efi_guid_event_group_exit_boot_services;
1917 /* Notify that ExitBootServices is invoked. */
1918 list_for_each_entry(evt, &efi_events, link) {
1920 !guidcmp(evt->group,
1921 &efi_guid_event_group_exit_boot_services)) {
1922 efi_signal_event(evt, false);
1927 /* TODO: Should persist EFI variables here */
1929 board_quiesce_devices();
1931 /* Fix up caches for EFI payloads if necessary */
1934 /* This stops all lingering devices */
1935 bootm_disable_interrupts();
1937 /* Disable boot time services */
1938 systab.con_in_handle = NULL;
1939 systab.con_in = NULL;
1940 systab.con_out_handle = NULL;
1941 systab.con_out = NULL;
1942 systab.stderr_handle = NULL;
1943 systab.std_err = NULL;
1944 systab.boottime = NULL;
1946 /* Recalculate CRC32 */
1947 efi_update_table_header_crc32(&systab.hdr);
1949 /* Give the payload some time to boot */
1950 efi_set_watchdog(0);
1953 return EFI_EXIT(EFI_SUCCESS);
1957 * efi_get_next_monotonic_count() - get next value of the counter
1958 * @count: returned value of the counter
1960 * This function implements the NextMonotonicCount service.
1962 * See the Unified Extensible Firmware Interface (UEFI) specification for
1965 * Return: status code
1967 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1969 static uint64_t mono;
1972 EFI_ENTRY("%p", count);
1974 ret = EFI_INVALID_PARAMETER;
1980 return EFI_EXIT(ret);
1984 * efi_stall() - sleep
1985 * @microseconds: period to sleep in microseconds
1987 * This function implements the Stall service.
1989 * See the Unified Extensible Firmware Interface (UEFI) specification for
1992 * Return: status code
1994 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1998 EFI_ENTRY("%ld", microseconds);
2000 end_tick = get_ticks() + usec_to_tick(microseconds);
2001 while (get_ticks() < end_tick)
2004 return EFI_EXIT(EFI_SUCCESS);
2008 * efi_set_watchdog_timer() - reset the watchdog timer
2009 * @timeout: seconds before reset by watchdog
2010 * @watchdog_code: code to be logged when resetting
2011 * @data_size: size of buffer in bytes
2012 * @watchdog_data: buffer with data describing the reset reason
2014 * This function implements the SetWatchdogTimer service.
2016 * See the Unified Extensible Firmware Interface (UEFI) specification for
2019 * Return: status code
2021 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2022 uint64_t watchdog_code,
2023 unsigned long data_size,
2024 uint16_t *watchdog_data)
2026 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2027 data_size, watchdog_data);
2028 return EFI_EXIT(efi_set_watchdog(timeout));
2032 * efi_close_protocol() - close a protocol
2033 * @handle: handle on which the protocol shall be closed
2034 * @protocol: GUID of the protocol to close
2035 * @agent_handle: handle of the driver
2036 * @controller_handle: handle of the controller
2038 * This function implements the CloseProtocol service.
2040 * See the Unified Extensible Firmware Interface (UEFI) specification for
2043 * Return: status code
2045 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2046 const efi_guid_t *protocol,
2047 efi_handle_t agent_handle,
2048 efi_handle_t controller_handle)
2050 struct efi_handler *handler;
2051 struct efi_open_protocol_info_item *item;
2052 struct efi_open_protocol_info_item *pos;
2055 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2058 if (!efi_search_obj(agent_handle) ||
2059 (controller_handle && !efi_search_obj(controller_handle))) {
2060 r = EFI_INVALID_PARAMETER;
2063 r = efi_search_protocol(handle, protocol, &handler);
2064 if (r != EFI_SUCCESS)
2068 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2069 if (item->info.agent_handle == agent_handle &&
2070 item->info.controller_handle == controller_handle) {
2071 efi_delete_open_info(item);
2080 * efi_open_protocol_information() - provide information about then open status
2081 * of a protocol on a handle
2082 * @handle: handle for which the information shall be retrieved
2083 * @protocol: GUID of the protocol
2084 * @entry_buffer: buffer to receive the open protocol information
2085 * @entry_count: number of entries available in the buffer
2087 * This function implements the OpenProtocolInformation service.
2089 * See the Unified Extensible Firmware Interface (UEFI) specification for
2092 * Return: status code
2094 static efi_status_t EFIAPI efi_open_protocol_information(
2095 efi_handle_t handle, const efi_guid_t *protocol,
2096 struct efi_open_protocol_info_entry **entry_buffer,
2097 efi_uintn_t *entry_count)
2099 unsigned long buffer_size;
2100 unsigned long count;
2101 struct efi_handler *handler;
2102 struct efi_open_protocol_info_item *item;
2105 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2108 /* Check parameters */
2109 if (!entry_buffer) {
2110 r = EFI_INVALID_PARAMETER;
2113 r = efi_search_protocol(handle, protocol, &handler);
2114 if (r != EFI_SUCCESS)
2119 list_for_each_entry(item, &handler->open_infos, link) {
2120 if (item->info.open_count)
2123 *entry_count = count;
2124 *entry_buffer = NULL;
2131 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2132 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2133 (void **)entry_buffer);
2134 if (r != EFI_SUCCESS)
2136 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2137 if (item->info.open_count)
2138 (*entry_buffer)[--count] = item->info;
2145 * efi_protocols_per_handle() - get protocols installed on a handle
2146 * @handle: handle for which the information is retrieved
2147 * @protocol_buffer: buffer with protocol GUIDs
2148 * @protocol_buffer_count: number of entries in the buffer
2150 * This function implements the ProtocolsPerHandleService.
2152 * See the Unified Extensible Firmware Interface (UEFI) specification for
2155 * Return: status code
2157 static efi_status_t EFIAPI efi_protocols_per_handle(
2158 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2159 efi_uintn_t *protocol_buffer_count)
2161 unsigned long buffer_size;
2162 struct efi_object *efiobj;
2163 struct list_head *protocol_handle;
2166 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2167 protocol_buffer_count);
2169 if (!handle || !protocol_buffer || !protocol_buffer_count)
2170 return EFI_EXIT(EFI_INVALID_PARAMETER);
2172 *protocol_buffer = NULL;
2173 *protocol_buffer_count = 0;
2175 efiobj = efi_search_obj(handle);
2177 return EFI_EXIT(EFI_INVALID_PARAMETER);
2179 /* Count protocols */
2180 list_for_each(protocol_handle, &efiobj->protocols) {
2181 ++*protocol_buffer_count;
2185 if (*protocol_buffer_count) {
2188 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2189 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2190 (void **)protocol_buffer);
2191 if (r != EFI_SUCCESS)
2193 list_for_each(protocol_handle, &efiobj->protocols) {
2194 struct efi_handler *protocol;
2196 protocol = list_entry(protocol_handle,
2197 struct efi_handler, link);
2198 (*protocol_buffer)[j] = (void *)protocol->guid;
2203 return EFI_EXIT(EFI_SUCCESS);
2207 * efi_locate_handle_buffer() - locate handles implementing a protocol
2208 * @search_type: selection criterion
2209 * @protocol: GUID of the protocol
2210 * @search_key: registration key
2211 * @no_handles: number of returned handles
2212 * @buffer: buffer with the returned handles
2214 * This function implements the LocateHandleBuffer service.
2216 * See the Unified Extensible Firmware Interface (UEFI) specification for
2219 * Return: status code
2221 static efi_status_t EFIAPI efi_locate_handle_buffer(
2222 enum efi_locate_search_type search_type,
2223 const efi_guid_t *protocol, void *search_key,
2224 efi_uintn_t *no_handles, efi_handle_t **buffer)
2227 efi_uintn_t buffer_size = 0;
2229 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2230 no_handles, buffer);
2232 if (!no_handles || !buffer) {
2233 r = EFI_INVALID_PARAMETER;
2238 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2240 if (r != EFI_BUFFER_TOO_SMALL)
2242 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2244 if (r != EFI_SUCCESS)
2246 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2248 if (r == EFI_SUCCESS)
2249 *no_handles = buffer_size / sizeof(efi_handle_t);
2255 * efi_locate_protocol() - find an interface implementing a protocol
2256 * @protocol: GUID of the protocol
2257 * @registration: registration key passed to the notification function
2258 * @protocol_interface: interface implementing the protocol
2260 * This function implements the LocateProtocol service.
2262 * See the Unified Extensible Firmware Interface (UEFI) specification for
2265 * Return: status code
2267 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2269 void **protocol_interface)
2271 struct efi_handler *handler;
2273 struct efi_object *efiobj;
2275 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2278 * The UEFI spec explicitly requires a protocol even if a registration
2279 * key is provided. This differs from the logic in LocateHandle().
2281 if (!protocol || !protocol_interface)
2282 return EFI_EXIT(EFI_INVALID_PARAMETER);
2285 struct efi_register_notify_event *event;
2286 struct efi_protocol_notification *handle;
2288 event = efi_check_register_notify_event(registration);
2290 return EFI_EXIT(EFI_INVALID_PARAMETER);
2292 * The UEFI spec requires to return EFI_NOT_FOUND if no
2293 * protocol instance matches protocol and registration.
2294 * So let's do the same for a mismatch between protocol and
2297 if (guidcmp(&event->protocol, protocol))
2299 if (list_empty(&event->handles))
2301 handle = list_first_entry(&event->handles,
2302 struct efi_protocol_notification,
2304 efiobj = handle->handle;
2305 list_del(&handle->link);
2307 ret = efi_search_protocol(efiobj, protocol, &handler);
2308 if (ret == EFI_SUCCESS)
2311 list_for_each_entry(efiobj, &efi_obj_list, link) {
2312 ret = efi_search_protocol(efiobj, protocol, &handler);
2313 if (ret == EFI_SUCCESS)
2318 *protocol_interface = NULL;
2319 return EFI_EXIT(EFI_NOT_FOUND);
2321 *protocol_interface = handler->protocol_interface;
2322 return EFI_EXIT(EFI_SUCCESS);
2326 * efi_locate_device_path() - Get the device path and handle of an device
2327 * implementing a protocol
2328 * @protocol: GUID of the protocol
2329 * @device_path: device path
2330 * @device: handle of the device
2332 * This function implements the LocateDevicePath service.
2334 * See the Unified Extensible Firmware Interface (UEFI) specification for
2337 * Return: status code
2339 static efi_status_t EFIAPI efi_locate_device_path(
2340 const efi_guid_t *protocol,
2341 struct efi_device_path **device_path,
2342 efi_handle_t *device)
2344 struct efi_device_path *dp;
2346 struct efi_handler *handler;
2347 efi_handle_t *handles;
2349 size_t len_best = 0;
2350 efi_uintn_t no_handles;
2354 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2356 if (!protocol || !device_path || !*device_path) {
2357 ret = EFI_INVALID_PARAMETER;
2361 /* Find end of device path */
2362 len = efi_dp_instance_size(*device_path);
2364 /* Get all handles implementing the protocol */
2365 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2366 &no_handles, &handles));
2367 if (ret != EFI_SUCCESS)
2370 for (i = 0; i < no_handles; ++i) {
2371 /* Find the device path protocol */
2372 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2374 if (ret != EFI_SUCCESS)
2376 dp = (struct efi_device_path *)handler->protocol_interface;
2377 len_dp = efi_dp_instance_size(dp);
2379 * This handle can only be a better fit
2380 * if its device path length is longer than the best fit and
2381 * if its device path length is shorter of equal the searched
2384 if (len_dp <= len_best || len_dp > len)
2386 /* Check if dp is a subpath of device_path */
2387 if (memcmp(*device_path, dp, len_dp))
2390 ret = EFI_INVALID_PARAMETER;
2393 *device = handles[i];
2397 remainder = (u8 *)*device_path + len_best;
2398 *device_path = (struct efi_device_path *)remainder;
2401 ret = EFI_NOT_FOUND;
2404 return EFI_EXIT(ret);
2408 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2410 * @handle: handle on which the protocol interfaces shall be installed
2411 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2414 * This function implements the MultipleProtocolInterfaces service.
2416 * See the Unified Extensible Firmware Interface (UEFI) specification for
2419 * Return: status code
2421 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2422 (efi_handle_t *handle, ...)
2424 EFI_ENTRY("%p", handle);
2427 const efi_guid_t *protocol;
2428 void *protocol_interface;
2429 efi_handle_t old_handle;
2430 efi_status_t r = EFI_SUCCESS;
2434 return EFI_EXIT(EFI_INVALID_PARAMETER);
2436 efi_va_start(argptr, handle);
2438 protocol = efi_va_arg(argptr, efi_guid_t*);
2441 protocol_interface = efi_va_arg(argptr, void*);
2442 /* Check that a device path has not been installed before */
2443 if (!guidcmp(protocol, &efi_guid_device_path)) {
2444 struct efi_device_path *dp = protocol_interface;
2446 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2448 if (r == EFI_SUCCESS &&
2449 dp->type == DEVICE_PATH_TYPE_END) {
2450 EFI_PRINT("Path %pD already installed\n",
2451 protocol_interface);
2452 r = EFI_ALREADY_STARTED;
2456 r = EFI_CALL(efi_install_protocol_interface(
2458 EFI_NATIVE_INTERFACE,
2459 protocol_interface));
2460 if (r != EFI_SUCCESS)
2465 if (r == EFI_SUCCESS)
2468 /* If an error occurred undo all changes. */
2469 efi_va_start(argptr, handle);
2471 protocol = efi_va_arg(argptr, efi_guid_t*);
2472 protocol_interface = efi_va_arg(argptr, void*);
2473 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2474 protocol_interface));
2482 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2484 * @handle: handle from which the protocol interfaces shall be removed
2485 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2488 * This function implements the UninstallMultipleProtocolInterfaces service.
2490 * See the Unified Extensible Firmware Interface (UEFI) specification for
2493 * Return: status code
2495 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2496 efi_handle_t handle, ...)
2498 EFI_ENTRY("%p", handle);
2501 const efi_guid_t *protocol;
2502 void *protocol_interface;
2503 efi_status_t r = EFI_SUCCESS;
2507 return EFI_EXIT(EFI_INVALID_PARAMETER);
2509 efi_va_start(argptr, handle);
2511 protocol = efi_va_arg(argptr, efi_guid_t*);
2514 protocol_interface = efi_va_arg(argptr, void*);
2515 r = efi_uninstall_protocol(handle, protocol,
2516 protocol_interface);
2517 if (r != EFI_SUCCESS)
2522 if (r == EFI_SUCCESS) {
2523 /* If the last protocol has been removed, delete the handle. */
2524 if (list_empty(&handle->protocols)) {
2525 list_del(&handle->link);
2531 /* If an error occurred undo all changes. */
2532 efi_va_start(argptr, handle);
2534 protocol = efi_va_arg(argptr, efi_guid_t*);
2535 protocol_interface = efi_va_arg(argptr, void*);
2536 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2537 EFI_NATIVE_INTERFACE,
2538 protocol_interface));
2542 /* In case of an error always return EFI_INVALID_PARAMETER */
2543 return EFI_EXIT(EFI_INVALID_PARAMETER);
2547 * efi_calculate_crc32() - calculate cyclic redundancy code
2548 * @data: buffer with data
2549 * @data_size: size of buffer in bytes
2550 * @crc32_p: cyclic redundancy code
2552 * This function implements the CalculateCrc32 service.
2554 * See the Unified Extensible Firmware Interface (UEFI) specification for
2557 * Return: status code
2559 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2560 efi_uintn_t data_size,
2563 efi_status_t ret = EFI_SUCCESS;
2565 EFI_ENTRY("%p, %zu", data, data_size);
2566 if (!data || !data_size || !crc32_p) {
2567 ret = EFI_INVALID_PARAMETER;
2570 *crc32_p = crc32(0, data, data_size);
2572 return EFI_EXIT(ret);
2576 * efi_copy_mem() - copy memory
2577 * @destination: destination of the copy operation
2578 * @source: source of the copy operation
2579 * @length: number of bytes to copy
2581 * This function implements the CopyMem service.
2583 * See the Unified Extensible Firmware Interface (UEFI) specification for
2586 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2589 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2590 memmove(destination, source, length);
2591 EFI_EXIT(EFI_SUCCESS);
2595 * efi_set_mem() - Fill memory with a byte value.
2596 * @buffer: buffer to fill
2597 * @size: size of buffer in bytes
2598 * @value: byte to copy to the buffer
2600 * This function implements the SetMem service.
2602 * See the Unified Extensible Firmware Interface (UEFI) specification for
2605 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2607 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2608 memset(buffer, value, size);
2609 EFI_EXIT(EFI_SUCCESS);
2613 * efi_protocol_open() - open protocol interface on a handle
2614 * @handler: handler of a protocol
2615 * @protocol_interface: interface implementing the protocol
2616 * @agent_handle: handle of the driver
2617 * @controller_handle: handle of the controller
2618 * @attributes: attributes indicating how to open the protocol
2620 * Return: status code
2622 static efi_status_t efi_protocol_open(
2623 struct efi_handler *handler,
2624 void **protocol_interface, void *agent_handle,
2625 void *controller_handle, uint32_t attributes)
2627 struct efi_open_protocol_info_item *item;
2628 struct efi_open_protocol_info_entry *match = NULL;
2629 bool opened_by_driver = false;
2630 bool opened_exclusive = false;
2632 /* If there is no agent, only return the interface */
2636 /* For TEST_PROTOCOL ignore interface attribute */
2637 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2638 *protocol_interface = NULL;
2641 * Check if the protocol is already opened by a driver with the same
2642 * attributes or opened exclusively
2644 list_for_each_entry(item, &handler->open_infos, link) {
2645 if (item->info.agent_handle == agent_handle) {
2646 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2647 (item->info.attributes == attributes))
2648 return EFI_ALREADY_STARTED;
2650 if (item->info.attributes &
2651 EFI_OPEN_PROTOCOL_BY_DRIVER)
2652 opened_by_driver = true;
2654 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2655 opened_exclusive = true;
2658 /* Only one controller can open the protocol exclusively */
2659 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2660 if (opened_exclusive)
2661 return EFI_ACCESS_DENIED;
2662 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2663 if (opened_exclusive || opened_by_driver)
2664 return EFI_ACCESS_DENIED;
2667 /* Prepare exclusive opening */
2668 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2669 /* Try to disconnect controllers */
2671 opened_by_driver = false;
2672 list_for_each_entry(item, &handler->open_infos, link) {
2675 if (item->info.attributes ==
2676 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2677 ret = EFI_CALL(efi_disconnect_controller(
2678 item->info.controller_handle,
2679 item->info.agent_handle,
2681 if (ret == EFI_SUCCESS)
2683 * Child controllers may have been
2684 * removed from the open_infos list. So
2685 * let's restart the loop.
2687 goto disconnect_next;
2689 opened_by_driver = true;
2692 /* Only one driver can be connected */
2693 if (opened_by_driver)
2694 return EFI_ACCESS_DENIED;
2697 /* Find existing entry */
2698 list_for_each_entry(item, &handler->open_infos, link) {
2699 if (item->info.agent_handle == agent_handle &&
2700 item->info.controller_handle == controller_handle &&
2701 item->info.attributes == attributes)
2702 match = &item->info;
2704 /* None found, create one */
2706 match = efi_create_open_info(handler);
2708 return EFI_OUT_OF_RESOURCES;
2711 match->agent_handle = agent_handle;
2712 match->controller_handle = controller_handle;
2713 match->attributes = attributes;
2714 match->open_count++;
2717 /* For TEST_PROTOCOL ignore interface attribute. */
2718 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2719 *protocol_interface = handler->protocol_interface;
2725 * efi_open_protocol() - open protocol interface on a handle
2726 * @handle: handle on which the protocol shall be opened
2727 * @protocol: GUID of the protocol
2728 * @protocol_interface: interface implementing the protocol
2729 * @agent_handle: handle of the driver
2730 * @controller_handle: handle of the controller
2731 * @attributes: attributes indicating how to open the protocol
2733 * This function implements the OpenProtocol interface.
2735 * See the Unified Extensible Firmware Interface (UEFI) specification for
2738 * Return: status code
2740 static efi_status_t EFIAPI efi_open_protocol
2741 (efi_handle_t handle, const efi_guid_t *protocol,
2742 void **protocol_interface, efi_handle_t agent_handle,
2743 efi_handle_t controller_handle, uint32_t attributes)
2745 struct efi_handler *handler;
2746 efi_status_t r = EFI_INVALID_PARAMETER;
2748 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2749 protocol_interface, agent_handle, controller_handle,
2752 if (!handle || !protocol ||
2753 (!protocol_interface && attributes !=
2754 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2758 switch (attributes) {
2759 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2760 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2761 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2763 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2764 if (controller_handle == handle)
2767 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2768 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2769 /* Check that the controller handle is valid */
2770 if (!efi_search_obj(controller_handle))
2773 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2774 /* Check that the agent handle is valid */
2775 if (!efi_search_obj(agent_handle))
2782 r = efi_search_protocol(handle, protocol, &handler);
2787 r = EFI_UNSUPPORTED;
2793 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2794 controller_handle, attributes);
2800 * efi_start_image() - call the entry point of an image
2801 * @image_handle: handle of the image
2802 * @exit_data_size: size of the buffer
2803 * @exit_data: buffer to receive the exit data of the called image
2805 * This function implements the StartImage service.
2807 * See the Unified Extensible Firmware Interface (UEFI) specification for
2810 * Return: status code
2812 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2813 efi_uintn_t *exit_data_size,
2816 struct efi_loaded_image_obj *image_obj =
2817 (struct efi_loaded_image_obj *)image_handle;
2820 efi_handle_t parent_image = current_image;
2822 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2824 /* Check parameters */
2825 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2827 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2828 if (ret != EFI_SUCCESS)
2829 return EFI_EXIT(EFI_INVALID_PARAMETER);
2831 efi_is_direct_boot = false;
2833 image_obj->exit_data_size = exit_data_size;
2834 image_obj->exit_data = exit_data;
2836 /* call the image! */
2837 if (setjmp(&image_obj->exit_jmp)) {
2839 * We called the entry point of the child image with EFI_CALL
2840 * in the lines below. The child image called the Exit() boot
2841 * service efi_exit() which executed the long jump that brought
2842 * us to the current line. This implies that the second half
2843 * of the EFI_CALL macro has not been executed.
2847 * efi_exit() called efi_restore_gd(). We have to undo this
2848 * otherwise __efi_entry_check() will put the wrong value into
2854 * To get ready to call EFI_EXIT below we have to execute the
2855 * missed out steps of EFI_CALL.
2857 assert(__efi_entry_check());
2858 EFI_PRINT("%lu returned by started image\n",
2859 (unsigned long)((uintptr_t)image_obj->exit_status &
2861 current_image = parent_image;
2862 return EFI_EXIT(image_obj->exit_status);
2865 current_image = image_handle;
2866 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2867 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2868 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2871 * Usually UEFI applications call Exit() instead of returning.
2872 * But because the world doesn't consist of ponies and unicorns,
2873 * we're happy to emulate that behavior on behalf of a payload
2876 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2880 * efi_delete_image() - delete loaded image from memory)
2882 * @image_obj: handle of the loaded image
2883 * @loaded_image_protocol: loaded image protocol
2885 static efi_status_t efi_delete_image
2886 (struct efi_loaded_image_obj *image_obj,
2887 struct efi_loaded_image *loaded_image_protocol)
2889 struct efi_object *efiobj;
2890 efi_status_t r, ret = EFI_SUCCESS;
2893 list_for_each_entry(efiobj, &efi_obj_list, link) {
2894 struct efi_handler *protocol;
2896 list_for_each_entry(protocol, &efiobj->protocols, link) {
2897 struct efi_open_protocol_info_item *info;
2899 list_for_each_entry(info, &protocol->open_infos, link) {
2900 if (info->info.agent_handle !=
2901 (efi_handle_t)image_obj)
2903 r = EFI_CALL(efi_close_protocol
2904 (efiobj, protocol->guid,
2905 info->info.agent_handle,
2906 info->info.controller_handle
2908 if (r != EFI_SUCCESS)
2911 * Closing protocols may results in further
2912 * items being deleted. To play it safe loop
2913 * over all elements again.
2920 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2921 efi_size_in_pages(loaded_image_protocol->image_size));
2922 efi_delete_handle(&image_obj->header);
2928 * efi_unload_image() - unload an EFI image
2929 * @image_handle: handle of the image to be unloaded
2931 * This function implements the UnloadImage service.
2933 * See the Unified Extensible Firmware Interface (UEFI) specification for
2936 * Return: status code
2938 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2940 efi_status_t ret = EFI_SUCCESS;
2941 struct efi_object *efiobj;
2942 struct efi_loaded_image *loaded_image_protocol;
2944 EFI_ENTRY("%p", image_handle);
2946 efiobj = efi_search_obj(image_handle);
2948 ret = EFI_INVALID_PARAMETER;
2951 /* Find the loaded image protocol */
2952 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2953 (void **)&loaded_image_protocol,
2955 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2956 if (ret != EFI_SUCCESS) {
2957 ret = EFI_INVALID_PARAMETER;
2960 switch (efiobj->type) {
2961 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2962 /* Call the unload function */
2963 if (!loaded_image_protocol->unload) {
2964 ret = EFI_UNSUPPORTED;
2967 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2968 if (ret != EFI_SUCCESS)
2971 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2974 ret = EFI_INVALID_PARAMETER;
2977 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2978 loaded_image_protocol);
2980 return EFI_EXIT(ret);
2984 * efi_update_exit_data() - fill exit data parameters of StartImage()
2986 * @image_obj image handle
2987 * @exit_data_size size of the exit data buffer
2988 * @exit_data buffer with data returned by UEFI payload
2989 * Return: status code
2991 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2992 efi_uintn_t exit_data_size,
2998 * If exit_data is not provided to StartImage(), exit_data_size must be
3001 if (!image_obj->exit_data)
3003 if (image_obj->exit_data_size)
3004 *image_obj->exit_data_size = exit_data_size;
3005 if (exit_data_size && exit_data) {
3006 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3008 (void **)image_obj->exit_data);
3009 if (ret != EFI_SUCCESS)
3011 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3013 image_obj->exit_data = NULL;
3019 * efi_exit() - leave an EFI application or driver
3020 * @image_handle: handle of the application or driver that is exiting
3021 * @exit_status: status code
3022 * @exit_data_size: size of the buffer in bytes
3023 * @exit_data: buffer with data describing an error
3025 * This function implements the Exit service.
3027 * See the Unified Extensible Firmware Interface (UEFI) specification for
3030 * Return: status code
3032 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3033 efi_status_t exit_status,
3034 efi_uintn_t exit_data_size,
3038 * TODO: We should call the unload procedure of the loaded
3042 struct efi_loaded_image *loaded_image_protocol;
3043 struct efi_loaded_image_obj *image_obj =
3044 (struct efi_loaded_image_obj *)image_handle;
3046 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3047 exit_data_size, exit_data);
3049 /* Check parameters */
3050 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3051 (void **)&loaded_image_protocol,
3053 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3054 if (ret != EFI_SUCCESS) {
3055 ret = EFI_INVALID_PARAMETER;
3059 /* Unloading of unstarted images */
3060 switch (image_obj->header.type) {
3061 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3063 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3064 efi_delete_image(image_obj, loaded_image_protocol);
3068 /* Handle does not refer to loaded image */
3069 ret = EFI_INVALID_PARAMETER;
3072 /* A started image can only be unloaded it is the last one started. */
3073 if (image_handle != current_image) {
3074 ret = EFI_INVALID_PARAMETER;
3078 /* Exit data is only foreseen in case of failure. */
3079 if (exit_status != EFI_SUCCESS) {
3080 ret = efi_update_exit_data(image_obj, exit_data_size,
3082 /* Exiting has priority. Don't return error to caller. */
3083 if (ret != EFI_SUCCESS)
3084 EFI_PRINT("%s: out of memory\n", __func__);
3086 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3087 exit_status != EFI_SUCCESS)
3088 efi_delete_image(image_obj, loaded_image_protocol);
3090 /* Make sure entry/exit counts for EFI world cross-overs match */
3091 EFI_EXIT(exit_status);
3094 * But longjmp out with the U-Boot gd, not the application's, as
3095 * the other end is a setjmp call inside EFI context.
3099 image_obj->exit_status = exit_status;
3100 longjmp(&image_obj->exit_jmp, 1);
3102 panic("EFI application exited");
3104 return EFI_EXIT(ret);
3108 * efi_handle_protocol() - get interface of a protocol on a handle
3109 * @handle: handle on which the protocol shall be opened
3110 * @protocol: GUID of the protocol
3111 * @protocol_interface: interface implementing the protocol
3113 * This function implements the HandleProtocol service.
3115 * See the Unified Extensible Firmware Interface (UEFI) specification for
3118 * Return: status code
3120 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3121 const efi_guid_t *protocol,
3122 void **protocol_interface)
3124 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3125 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3129 * efi_bind_controller() - bind a single driver to a controller
3130 * @controller_handle: controller handle
3131 * @driver_image_handle: driver handle
3132 * @remain_device_path: remaining path
3134 * Return: status code
3136 static efi_status_t efi_bind_controller(
3137 efi_handle_t controller_handle,
3138 efi_handle_t driver_image_handle,
3139 struct efi_device_path *remain_device_path)
3141 struct efi_driver_binding_protocol *binding_protocol;
3144 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3145 &efi_guid_driver_binding_protocol,
3146 (void **)&binding_protocol,
3147 driver_image_handle, NULL,
3148 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3149 if (r != EFI_SUCCESS)
3151 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3153 remain_device_path));
3154 if (r == EFI_SUCCESS)
3155 r = EFI_CALL(binding_protocol->start(binding_protocol,
3157 remain_device_path));
3158 EFI_CALL(efi_close_protocol(driver_image_handle,
3159 &efi_guid_driver_binding_protocol,
3160 driver_image_handle, NULL));
3165 * efi_connect_single_controller() - connect a single driver to a controller
3166 * @controller_handle: controller
3167 * @driver_image_handle: driver
3168 * @remain_device_path: remaining path
3170 * Return: status code
3172 static efi_status_t efi_connect_single_controller(
3173 efi_handle_t controller_handle,
3174 efi_handle_t *driver_image_handle,
3175 struct efi_device_path *remain_device_path)
3177 efi_handle_t *buffer;
3181 size_t connected = 0;
3183 /* Get buffer with all handles with driver binding protocol */
3184 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3185 &efi_guid_driver_binding_protocol,
3186 NULL, &count, &buffer));
3187 if (r != EFI_SUCCESS)
3190 /* Context Override */
3191 if (driver_image_handle) {
3192 for (; *driver_image_handle; ++driver_image_handle) {
3193 for (i = 0; i < count; ++i) {
3194 if (buffer[i] == *driver_image_handle) {
3196 r = efi_bind_controller(
3198 *driver_image_handle,
3199 remain_device_path);
3201 * For drivers that do not support the
3202 * controller or are already connected
3203 * we receive an error code here.
3205 if (r == EFI_SUCCESS)
3213 * TODO: Some overrides are not yet implemented:
3214 * - Platform Driver Override
3215 * - Driver Family Override Search
3216 * - Bus Specific Driver Override
3219 /* Driver Binding Search */
3220 for (i = 0; i < count; ++i) {
3222 r = efi_bind_controller(controller_handle,
3224 remain_device_path);
3225 if (r == EFI_SUCCESS)
3230 efi_free_pool(buffer);
3232 return EFI_NOT_FOUND;
3237 * efi_connect_controller() - connect a controller to a driver
3238 * @controller_handle: handle of the controller
3239 * @driver_image_handle: handle of the driver
3240 * @remain_device_path: device path of a child controller
3241 * @recursive: true to connect all child controllers
3243 * This function implements the ConnectController service.
3245 * See the Unified Extensible Firmware Interface (UEFI) specification for
3248 * First all driver binding protocol handles are tried for binding drivers.
3249 * Afterwards all handles that have opened a protocol of the controller
3250 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3252 * Return: status code
3254 static efi_status_t EFIAPI efi_connect_controller(
3255 efi_handle_t controller_handle,
3256 efi_handle_t *driver_image_handle,
3257 struct efi_device_path *remain_device_path,
3261 efi_status_t ret = EFI_NOT_FOUND;
3262 struct efi_object *efiobj;
3264 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3265 remain_device_path, recursive);
3267 efiobj = efi_search_obj(controller_handle);
3269 ret = EFI_INVALID_PARAMETER;
3273 r = efi_connect_single_controller(controller_handle,
3274 driver_image_handle,
3275 remain_device_path);
3276 if (r == EFI_SUCCESS)
3279 struct efi_handler *handler;
3280 struct efi_open_protocol_info_item *item;
3282 list_for_each_entry(handler, &efiobj->protocols, link) {
3283 list_for_each_entry(item, &handler->open_infos, link) {
3284 if (item->info.attributes &
3285 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3286 r = EFI_CALL(efi_connect_controller(
3287 item->info.controller_handle,
3288 driver_image_handle,
3291 if (r == EFI_SUCCESS)
3297 /* Check for child controller specified by end node */
3298 if (ret != EFI_SUCCESS && remain_device_path &&
3299 remain_device_path->type == DEVICE_PATH_TYPE_END)
3302 return EFI_EXIT(ret);
3306 * efi_reinstall_protocol_interface() - reinstall protocol interface
3307 * @handle: handle on which the protocol shall be reinstalled
3308 * @protocol: GUID of the protocol to be installed
3309 * @old_interface: interface to be removed
3310 * @new_interface: interface to be installed
3312 * This function implements the ReinstallProtocolInterface service.
3314 * See the Unified Extensible Firmware Interface (UEFI) specification for
3317 * The old interface is uninstalled. The new interface is installed.
3318 * Drivers are connected.
3320 * Return: status code
3322 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3323 efi_handle_t handle, const efi_guid_t *protocol,
3324 void *old_interface, void *new_interface)
3328 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3331 /* Uninstall protocol but do not delete handle */
3332 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3333 if (ret != EFI_SUCCESS)
3336 /* Install the new protocol */
3337 ret = efi_add_protocol(handle, protocol, new_interface);
3339 * The UEFI spec does not specify what should happen to the handle
3340 * if in case of an error no protocol interface remains on the handle.
3341 * So let's do nothing here.
3343 if (ret != EFI_SUCCESS)
3346 * The returned status code has to be ignored.
3347 * Do not create an error if no suitable driver for the handle exists.
3349 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3351 return EFI_EXIT(ret);
3355 * efi_get_child_controllers() - get all child controllers associated to a driver
3356 * @efiobj: handle of the controller
3357 * @driver_handle: handle of the driver
3358 * @number_of_children: number of child controllers
3359 * @child_handle_buffer: handles of the the child controllers
3361 * The allocated buffer has to be freed with free().
3363 * Return: status code
3365 static efi_status_t efi_get_child_controllers(
3366 struct efi_object *efiobj,
3367 efi_handle_t driver_handle,
3368 efi_uintn_t *number_of_children,
3369 efi_handle_t **child_handle_buffer)
3371 struct efi_handler *handler;
3372 struct efi_open_protocol_info_item *item;
3373 efi_uintn_t count = 0, i;
3376 /* Count all child controller associations */
3377 list_for_each_entry(handler, &efiobj->protocols, link) {
3378 list_for_each_entry(item, &handler->open_infos, link) {
3379 if (item->info.agent_handle == driver_handle &&
3380 item->info.attributes &
3381 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3386 * Create buffer. In case of duplicate child controller assignments
3387 * the buffer will be too large. But that does not harm.
3389 *number_of_children = 0;
3390 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3391 if (!*child_handle_buffer)
3392 return EFI_OUT_OF_RESOURCES;
3393 /* Copy unique child handles */
3394 list_for_each_entry(handler, &efiobj->protocols, link) {
3395 list_for_each_entry(item, &handler->open_infos, link) {
3396 if (item->info.agent_handle == driver_handle &&
3397 item->info.attributes &
3398 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3399 /* Check this is a new child controller */
3401 for (i = 0; i < *number_of_children; ++i) {
3402 if ((*child_handle_buffer)[i] ==
3403 item->info.controller_handle)
3406 /* Copy handle to buffer */
3408 i = (*number_of_children)++;
3409 (*child_handle_buffer)[i] =
3410 item->info.controller_handle;
3419 * efi_disconnect_controller() - disconnect a controller from a driver
3420 * @controller_handle: handle of the controller
3421 * @driver_image_handle: handle of the driver
3422 * @child_handle: handle of the child to destroy
3424 * This function implements the DisconnectController service.
3426 * See the Unified Extensible Firmware Interface (UEFI) specification for
3429 * Return: status code
3431 static efi_status_t EFIAPI efi_disconnect_controller(
3432 efi_handle_t controller_handle,
3433 efi_handle_t driver_image_handle,
3434 efi_handle_t child_handle)
3436 struct efi_driver_binding_protocol *binding_protocol;
3437 efi_handle_t *child_handle_buffer = NULL;
3438 size_t number_of_children = 0;
3440 size_t stop_count = 0;
3441 struct efi_object *efiobj;
3443 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3446 efiobj = efi_search_obj(controller_handle);
3448 r = EFI_INVALID_PARAMETER;
3452 if (child_handle && !efi_search_obj(child_handle)) {
3453 r = EFI_INVALID_PARAMETER;
3457 /* If no driver handle is supplied, disconnect all drivers */
3458 if (!driver_image_handle) {
3459 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3463 /* Create list of child handles */
3465 number_of_children = 1;
3466 child_handle_buffer = &child_handle;
3468 efi_get_child_controllers(efiobj,
3469 driver_image_handle,
3470 &number_of_children,
3471 &child_handle_buffer);
3474 /* Get the driver binding protocol */
3475 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3476 &efi_guid_driver_binding_protocol,
3477 (void **)&binding_protocol,
3478 driver_image_handle, NULL,
3479 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3480 if (r != EFI_SUCCESS)
3482 /* Remove the children */
3483 if (number_of_children) {
3484 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3487 child_handle_buffer));
3488 if (r == EFI_SUCCESS)
3491 /* Remove the driver */
3493 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3496 if (r == EFI_SUCCESS)
3498 EFI_CALL(efi_close_protocol(driver_image_handle,
3499 &efi_guid_driver_binding_protocol,
3500 driver_image_handle, NULL));
3508 free(child_handle_buffer);
3512 static struct efi_boot_services efi_boot_services = {
3514 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3515 .revision = EFI_SPECIFICATION_VERSION,
3516 .headersize = sizeof(struct efi_boot_services),
3518 .raise_tpl = efi_raise_tpl,
3519 .restore_tpl = efi_restore_tpl,
3520 .allocate_pages = efi_allocate_pages_ext,
3521 .free_pages = efi_free_pages_ext,
3522 .get_memory_map = efi_get_memory_map_ext,
3523 .allocate_pool = efi_allocate_pool_ext,
3524 .free_pool = efi_free_pool_ext,
3525 .create_event = efi_create_event_ext,
3526 .set_timer = efi_set_timer_ext,
3527 .wait_for_event = efi_wait_for_event,
3528 .signal_event = efi_signal_event_ext,
3529 .close_event = efi_close_event,
3530 .check_event = efi_check_event,
3531 .install_protocol_interface = efi_install_protocol_interface,
3532 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3533 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3534 .handle_protocol = efi_handle_protocol,
3536 .register_protocol_notify = efi_register_protocol_notify,
3537 .locate_handle = efi_locate_handle_ext,
3538 .locate_device_path = efi_locate_device_path,
3539 .install_configuration_table = efi_install_configuration_table_ext,
3540 .load_image = efi_load_image,
3541 .start_image = efi_start_image,
3543 .unload_image = efi_unload_image,
3544 .exit_boot_services = efi_exit_boot_services,
3545 .get_next_monotonic_count = efi_get_next_monotonic_count,
3547 .set_watchdog_timer = efi_set_watchdog_timer,
3548 .connect_controller = efi_connect_controller,
3549 .disconnect_controller = efi_disconnect_controller,
3550 .open_protocol = efi_open_protocol,
3551 .close_protocol = efi_close_protocol,
3552 .open_protocol_information = efi_open_protocol_information,
3553 .protocols_per_handle = efi_protocols_per_handle,
3554 .locate_handle_buffer = efi_locate_handle_buffer,
3555 .locate_protocol = efi_locate_protocol,
3556 .install_multiple_protocol_interfaces =
3557 efi_install_multiple_protocol_interfaces,
3558 .uninstall_multiple_protocol_interfaces =
3559 efi_uninstall_multiple_protocol_interfaces,
3560 .calculate_crc32 = efi_calculate_crc32,
3561 .copy_mem = efi_copy_mem,
3562 .set_mem = efi_set_mem,
3563 .create_event_ex = efi_create_event_ex,
3566 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3568 struct efi_system_table __efi_runtime_data systab = {
3570 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3571 .revision = EFI_SPECIFICATION_VERSION,
3572 .headersize = sizeof(struct efi_system_table),
3574 .fw_vendor = firmware_vendor,
3575 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3576 .con_in = (void *)&efi_con_in,
3577 .con_out = (void *)&efi_con_out,
3578 .std_err = (void *)&efi_con_out,
3579 .runtime = (void *)&efi_runtime_services,
3580 .boottime = (void *)&efi_boot_services,
3586 * efi_initialize_system_table() - Initialize system table
3588 * Return: status code
3590 efi_status_t efi_initialize_system_table(void)
3594 /* Allocate configuration table array */
3595 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3596 EFI_MAX_CONFIGURATION_TABLES *
3597 sizeof(struct efi_configuration_table),
3598 (void **)&systab.tables);
3600 /* Set CRC32 field in table headers */
3601 efi_update_table_header_crc32(&systab.hdr);
3602 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3603 efi_update_table_header_crc32(&efi_boot_services.hdr);