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);
31 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
32 * we need to do trickery with caches. Since we don't want to break the EFI
33 * aware boot path, only apply hacks when loading exiting directly (breaking
34 * direct Linux EFI booting along the way - oh well).
36 static bool efi_is_direct_boot = true;
39 * EFI can pass arbitrary additional "tables" containing vendor specific
40 * information to the payload. One such table is the FDT table which contains
41 * a pointer to a flattened device tree blob.
43 * In most cases we want to pass an FDT to the payload, so reserve one slot of
44 * config table space for it. The pointer gets populated by do_bootefi_exec().
46 static struct efi_configuration_table __efi_runtime_data efi_conf_table[16];
50 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
51 * fixed when compiling U-Boot. However, the payload does not know about that
52 * restriction so we need to manually swap its and our view of that register on
53 * EFI callback entry/exit.
55 static volatile void *efi_gd, *app_gd;
58 static int entry_count;
59 static int nesting_level;
60 /* GUID of the device tree table */
61 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
62 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63 const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
66 /* event group ExitBootServices() invoked */
67 const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69 /* event group SetVirtualAddressMap() invoked */
70 const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72 /* event group memory map changed */
73 const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75 /* event group boot manager about to boot */
76 const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78 /* event group ResetSystem() invoked (before ExitBootServices) */
79 const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
82 static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
87 /* Called on every callback entry */
88 int __efi_entry_check(void)
90 int ret = entry_count++ == 0;
99 /* Called on every callback exit */
100 int __efi_exit_check(void)
102 int ret = --entry_count == 0;
109 /* Called from do_bootefi_exec() */
110 void efi_save_gd(void)
118 * Special case handler for error/abort that just forces things back to u-boot
119 * world so we can dump out an abort msg, without any care about returning back
122 void efi_restore_gd(void)
125 /* Only restore if we're already in EFI context */
133 * indent_string() - returns a string for indenting with two spaces per level
134 * @level: indent level
136 * A maximum of ten indent levels is supported. Higher indent levels will be
139 * Return: A string for indenting with two spaces per level is
142 static const char *indent_string(int level)
144 const char *indent = " ";
145 const int max = strlen(indent);
147 level = min(max, level * 2);
148 return &indent[max - level];
151 const char *__efi_nesting(void)
153 return indent_string(nesting_level);
156 const char *__efi_nesting_inc(void)
158 return indent_string(nesting_level++);
161 const char *__efi_nesting_dec(void)
163 return indent_string(--nesting_level);
167 * efi_queue_event() - queue an EFI event
168 * @event: event to signal
169 * @check_tpl: check the TPL level
171 * This function queues the notification function of the event for future
174 * The notification function is called if the task priority level of the event
175 * is higher than the current task priority level.
177 * For the SignalEvent service see efi_signal_event_ext.
180 static void efi_queue_event(struct efi_event *event, bool check_tpl)
182 if (event->notify_function) {
183 event->is_queued = true;
185 if (check_tpl && efi_tpl >= event->notify_tpl)
187 EFI_CALL_VOID(event->notify_function(event,
188 event->notify_context));
190 event->is_queued = false;
194 * efi_signal_event() - signal an EFI event
195 * @event: event to signal
196 * @check_tpl: check the TPL level
198 * This function signals an event. If the event belongs to an event group all
199 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
200 * their notification function is queued.
202 * For the SignalEvent service see efi_signal_event_ext.
204 void efi_signal_event(struct efi_event *event, bool check_tpl)
207 struct efi_event *evt;
210 * The signaled state has to set before executing any
211 * notification function
213 list_for_each_entry(evt, &efi_events, link) {
214 if (!evt->group || guidcmp(evt->group, event->group))
216 if (evt->is_signaled)
218 evt->is_signaled = true;
219 if (evt->type & EVT_NOTIFY_SIGNAL &&
220 evt->notify_function)
221 evt->is_queued = true;
223 list_for_each_entry(evt, &efi_events, link) {
224 if (!evt->group || guidcmp(evt->group, event->group))
227 efi_queue_event(evt, check_tpl);
229 } else if (!event->is_signaled) {
230 event->is_signaled = true;
231 if (event->type & EVT_NOTIFY_SIGNAL)
232 efi_queue_event(event, check_tpl);
237 * efi_raise_tpl() - raise the task priority level
238 * @new_tpl: new value of the task priority level
240 * This function implements the RaiseTpl service.
242 * See the Unified Extensible Firmware Interface (UEFI) specification for
245 * Return: old value of the task priority level
247 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
249 efi_uintn_t old_tpl = efi_tpl;
251 EFI_ENTRY("0x%zx", new_tpl);
253 if (new_tpl < efi_tpl)
254 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
256 if (efi_tpl > TPL_HIGH_LEVEL)
257 efi_tpl = TPL_HIGH_LEVEL;
259 EFI_EXIT(EFI_SUCCESS);
264 * efi_restore_tpl() - lower the task priority level
265 * @old_tpl: value of the task priority level to be restored
267 * This function implements the RestoreTpl service.
269 * See the Unified Extensible Firmware Interface (UEFI) specification for
272 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
274 EFI_ENTRY("0x%zx", old_tpl);
276 if (old_tpl > efi_tpl)
277 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
279 if (efi_tpl > TPL_HIGH_LEVEL)
280 efi_tpl = TPL_HIGH_LEVEL;
283 * Lowering the TPL may have made queued events eligible for execution.
287 EFI_EXIT(EFI_SUCCESS);
291 * efi_allocate_pages_ext() - allocate memory pages
292 * @type: type of allocation to be performed
293 * @memory_type: usage type of the allocated memory
294 * @pages: number of pages to be allocated
295 * @memory: allocated memory
297 * This function implements the AllocatePages service.
299 * See the Unified Extensible Firmware Interface (UEFI) specification for
302 * Return: status code
304 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
310 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
311 r = efi_allocate_pages(type, memory_type, pages, memory);
316 * efi_free_pages_ext() - Free memory pages.
317 * @memory: start of the memory area to be freed
318 * @pages: number of pages to be freed
320 * This function implements the FreePages service.
322 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * Return: status code
327 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
332 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
333 r = efi_free_pages(memory, pages);
338 * efi_get_memory_map_ext() - get map describing memory usage
339 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
340 * on exit the size of the copied memory map
341 * @memory_map: buffer to which the memory map is written
342 * @map_key: key for the memory map
343 * @descriptor_size: size of an individual memory descriptor
344 * @descriptor_version: version number of the memory descriptor structure
346 * This function implements the GetMemoryMap service.
348 * See the Unified Extensible Firmware Interface (UEFI) specification for
351 * Return: status code
353 static efi_status_t EFIAPI efi_get_memory_map_ext(
354 efi_uintn_t *memory_map_size,
355 struct efi_mem_desc *memory_map,
356 efi_uintn_t *map_key,
357 efi_uintn_t *descriptor_size,
358 uint32_t *descriptor_version)
362 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
363 map_key, descriptor_size, descriptor_version);
364 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
365 descriptor_size, descriptor_version);
370 * efi_allocate_pool_ext() - allocate memory from pool
371 * @pool_type: type of the pool from which memory is to be allocated
372 * @size: number of bytes to be allocated
373 * @buffer: allocated memory
375 * This function implements the AllocatePool service.
377 * See the Unified Extensible Firmware Interface (UEFI) specification for
380 * Return: status code
382 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
388 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
389 r = efi_allocate_pool(pool_type, size, buffer);
394 * efi_free_pool_ext() - free memory from pool
395 * @buffer: start of memory to be freed
397 * This function implements the FreePool service.
399 * See the Unified Extensible Firmware Interface (UEFI) specification for
402 * Return: status code
404 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
408 EFI_ENTRY("%p", buffer);
409 r = efi_free_pool(buffer);
414 * efi_add_handle() - add a new object to the object list
415 * @obj: object to be added
417 * The protocols list is initialized. The object handle is set.
419 void efi_add_handle(struct efi_object *obj)
423 INIT_LIST_HEAD(&obj->protocols);
425 list_add_tail(&obj->link, &efi_obj_list);
429 * efi_create_handle() - create handle
430 * @handle: new handle
432 * Return: status code
434 efi_status_t efi_create_handle(efi_handle_t *handle)
436 struct efi_object *obj;
438 obj = calloc(1, sizeof(struct efi_object));
440 return EFI_OUT_OF_RESOURCES;
443 *handle = obj->handle;
449 * efi_search_protocol() - find a protocol on a handle.
451 * @protocol_guid: GUID of the protocol
452 * @handler: reference to the protocol
454 * Return: status code
456 efi_status_t efi_search_protocol(const efi_handle_t handle,
457 const efi_guid_t *protocol_guid,
458 struct efi_handler **handler)
460 struct efi_object *efiobj;
461 struct list_head *lhandle;
463 if (!handle || !protocol_guid)
464 return EFI_INVALID_PARAMETER;
465 efiobj = efi_search_obj(handle);
467 return EFI_INVALID_PARAMETER;
468 list_for_each(lhandle, &efiobj->protocols) {
469 struct efi_handler *protocol;
471 protocol = list_entry(lhandle, struct efi_handler, link);
472 if (!guidcmp(protocol->guid, protocol_guid)) {
478 return EFI_NOT_FOUND;
482 * efi_remove_protocol() - delete protocol from a handle
483 * @handle: handle from which the protocol shall be deleted
484 * @protocol: GUID of the protocol to be deleted
485 * @protocol_interface: interface of the protocol implementation
487 * Return: status code
489 efi_status_t efi_remove_protocol(const efi_handle_t handle,
490 const efi_guid_t *protocol,
491 void *protocol_interface)
493 struct efi_handler *handler;
496 ret = efi_search_protocol(handle, protocol, &handler);
497 if (ret != EFI_SUCCESS)
499 if (guidcmp(handler->guid, protocol))
500 return EFI_INVALID_PARAMETER;
501 if (handler->protocol_interface != protocol_interface)
502 return EFI_INVALID_PARAMETER;
503 list_del(&handler->link);
509 * efi_remove_all_protocols() - delete all protocols from a handle
510 * @handle: handle from which the protocols shall be deleted
512 * Return: status code
514 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
516 struct efi_object *efiobj;
517 struct efi_handler *protocol;
518 struct efi_handler *pos;
520 efiobj = efi_search_obj(handle);
522 return EFI_INVALID_PARAMETER;
523 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
526 ret = efi_remove_protocol(handle, protocol->guid,
527 protocol->protocol_interface);
528 if (ret != EFI_SUCCESS)
535 * efi_delete_handle() - delete handle
537 * @obj: handle to delete
539 void efi_delete_handle(struct efi_object *obj)
543 efi_remove_all_protocols(obj->handle);
544 list_del(&obj->link);
549 * efi_is_event() - check if a pointer is a valid event
550 * @event: pointer to check
552 * Return: status code
554 static efi_status_t efi_is_event(const struct efi_event *event)
556 const struct efi_event *evt;
559 return EFI_INVALID_PARAMETER;
560 list_for_each_entry(evt, &efi_events, link) {
564 return EFI_INVALID_PARAMETER;
568 * efi_create_event() - create an event
569 * @type: type of the event to create
570 * @notify_tpl: task priority level of the event
571 * @notify_function: notification function of the event
572 * @notify_context: pointer passed to the notification function
573 * @group: event group
574 * @event: created event
576 * This function is used inside U-Boot code to create an event.
578 * For the API function implementing the CreateEvent service see
579 * efi_create_event_ext.
581 * Return: status code
583 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
584 void (EFIAPI *notify_function) (
585 struct efi_event *event,
587 void *notify_context, efi_guid_t *group,
588 struct efi_event **event)
590 struct efi_event *evt;
593 return EFI_INVALID_PARAMETER;
595 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
596 return EFI_INVALID_PARAMETER;
598 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
599 notify_function == NULL)
600 return EFI_INVALID_PARAMETER;
602 evt = calloc(1, sizeof(struct efi_event));
604 return EFI_OUT_OF_RESOURCES;
606 evt->notify_tpl = notify_tpl;
607 evt->notify_function = notify_function;
608 evt->notify_context = notify_context;
610 /* Disable timers on bootup */
611 evt->trigger_next = -1ULL;
612 evt->is_queued = false;
613 evt->is_signaled = false;
614 list_add_tail(&evt->link, &efi_events);
620 * efi_create_event_ex() - create an event in a group
621 * @type: type of the event to create
622 * @notify_tpl: task priority level of the event
623 * @notify_function: notification function of the event
624 * @notify_context: pointer passed to the notification function
625 * @event: created event
626 * @event_group: event group
628 * This function implements the CreateEventEx service.
630 * See the Unified Extensible Firmware Interface (UEFI) specification for
633 * Return: status code
635 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
636 void (EFIAPI *notify_function) (
637 struct efi_event *event,
639 void *notify_context,
640 efi_guid_t *event_group,
641 struct efi_event **event)
643 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
644 notify_context, event_group);
645 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
646 notify_context, event_group, event));
650 * efi_create_event_ext() - create an event
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
657 * This function implements the CreateEvent service.
659 * See the Unified Extensible Firmware Interface (UEFI) specification for
662 * Return: status code
664 static efi_status_t EFIAPI efi_create_event_ext(
665 uint32_t type, efi_uintn_t notify_tpl,
666 void (EFIAPI *notify_function) (
667 struct efi_event *event,
669 void *notify_context, struct efi_event **event)
671 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
673 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
674 notify_context, NULL, event));
678 * efi_timer_check() - check if a timer event has occurred
680 * Check if a timer event has occurred or a queued notification function should
683 * Our timers have to work without interrupts, so we check whenever keyboard
684 * input or disk accesses happen if enough time elapsed for them to fire.
686 void efi_timer_check(void)
688 struct efi_event *evt;
689 u64 now = timer_get_us();
691 list_for_each_entry(evt, &efi_events, link) {
693 efi_queue_event(evt, true);
694 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
696 switch (evt->trigger_type) {
697 case EFI_TIMER_RELATIVE:
698 evt->trigger_type = EFI_TIMER_STOP;
700 case EFI_TIMER_PERIODIC:
701 evt->trigger_next += evt->trigger_time;
706 evt->is_signaled = false;
707 efi_signal_event(evt, true);
713 * efi_set_timer() - set the trigger time for a timer event or stop the event
714 * @event: event for which the timer is set
715 * @type: type of the timer
716 * @trigger_time: trigger period in multiples of 100ns
718 * This is the function for internal usage in U-Boot. For the API function
719 * implementing the SetTimer service see efi_set_timer_ext.
721 * Return: status code
723 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
724 uint64_t trigger_time)
726 /* Check that the event is valid */
727 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
728 return EFI_INVALID_PARAMETER;
731 * The parameter defines a multiple of 100ns.
732 * We use multiples of 1000ns. So divide by 10.
734 do_div(trigger_time, 10);
738 event->trigger_next = -1ULL;
740 case EFI_TIMER_PERIODIC:
741 case EFI_TIMER_RELATIVE:
742 event->trigger_next = timer_get_us() + trigger_time;
745 return EFI_INVALID_PARAMETER;
747 event->trigger_type = type;
748 event->trigger_time = trigger_time;
749 event->is_signaled = false;
754 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
756 * @event: event for which the timer is set
757 * @type: type of the timer
758 * @trigger_time: trigger period in multiples of 100ns
760 * This function implements the SetTimer service.
762 * See the Unified Extensible Firmware Interface (UEFI) specification for
766 * Return: status code
768 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
769 enum efi_timer_delay type,
770 uint64_t trigger_time)
772 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
773 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
777 * efi_wait_for_event() - wait for events to be signaled
778 * @num_events: number of events to be waited for
779 * @event: events to be waited for
780 * @index: index of the event that was signaled
782 * This function implements the WaitForEvent service.
784 * See the Unified Extensible Firmware Interface (UEFI) specification for
787 * Return: status code
789 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
790 struct efi_event **event,
795 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
797 /* Check parameters */
798 if (!num_events || !event)
799 return EFI_EXIT(EFI_INVALID_PARAMETER);
801 if (efi_tpl != TPL_APPLICATION)
802 return EFI_EXIT(EFI_UNSUPPORTED);
803 for (i = 0; i < num_events; ++i) {
804 if (efi_is_event(event[i]) != EFI_SUCCESS)
805 return EFI_EXIT(EFI_INVALID_PARAMETER);
806 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
807 return EFI_EXIT(EFI_INVALID_PARAMETER);
808 if (!event[i]->is_signaled)
809 efi_queue_event(event[i], true);
812 /* Wait for signal */
814 for (i = 0; i < num_events; ++i) {
815 if (event[i]->is_signaled)
818 /* Allow events to occur. */
824 * Reset the signal which is passed to the caller to allow periodic
827 event[i]->is_signaled = false;
831 return EFI_EXIT(EFI_SUCCESS);
835 * efi_signal_event_ext() - signal an EFI event
836 * @event: event to signal
838 * This function implements the SignalEvent service.
840 * See the Unified Extensible Firmware Interface (UEFI) specification for
843 * This functions sets the signaled state of the event and queues the
844 * notification function for execution.
846 * Return: status code
848 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
850 EFI_ENTRY("%p", event);
851 if (efi_is_event(event) != EFI_SUCCESS)
852 return EFI_EXIT(EFI_INVALID_PARAMETER);
853 efi_signal_event(event, true);
854 return EFI_EXIT(EFI_SUCCESS);
858 * efi_close_event() - close an EFI event
859 * @event: event to close
861 * This function implements the CloseEvent service.
863 * See the Unified Extensible Firmware Interface (UEFI) specification for
866 * Return: status code
868 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
870 EFI_ENTRY("%p", event);
871 if (efi_is_event(event) != EFI_SUCCESS)
872 return EFI_EXIT(EFI_INVALID_PARAMETER);
873 list_del(&event->link);
875 return EFI_EXIT(EFI_SUCCESS);
879 * efi_check_event() - check if an event is signaled
880 * @event: event to check
882 * This function implements the CheckEvent service.
884 * See the Unified Extensible Firmware Interface (UEFI) specification for
887 * If an event is not signaled yet, the notification function is queued. The
888 * signaled state is cleared.
890 * Return: status code
892 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
894 EFI_ENTRY("%p", event);
896 if (efi_is_event(event) != EFI_SUCCESS ||
897 event->type & EVT_NOTIFY_SIGNAL)
898 return EFI_EXIT(EFI_INVALID_PARAMETER);
899 if (!event->is_signaled)
900 efi_queue_event(event, true);
901 if (event->is_signaled) {
902 event->is_signaled = false;
903 return EFI_EXIT(EFI_SUCCESS);
905 return EFI_EXIT(EFI_NOT_READY);
909 * efi_search_obj() - find the internal EFI object for a handle
910 * @handle: handle to find
914 struct efi_object *efi_search_obj(const efi_handle_t handle)
916 struct efi_object *efiobj;
918 list_for_each_entry(efiobj, &efi_obj_list, link) {
919 if (efiobj->handle == handle)
927 * efi_open_protocol_info_entry() - create open protocol info entry and add it
929 * @handler: handler of a protocol
931 * Return: open protocol info entry
933 static struct efi_open_protocol_info_entry *efi_create_open_info(
934 struct efi_handler *handler)
936 struct efi_open_protocol_info_item *item;
938 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
941 /* Append the item to the open protocol info list. */
942 list_add_tail(&item->link, &handler->open_infos);
948 * efi_delete_open_info() - remove an open protocol info entry from a protocol
949 * @item: open protocol info entry to delete
951 * Return: status code
953 static efi_status_t efi_delete_open_info(
954 struct efi_open_protocol_info_item *item)
956 list_del(&item->link);
962 * efi_add_protocol() - install new protocol on a handle
963 * @handle: handle on which the protocol shall be installed
964 * @protocol: GUID of the protocol to be installed
965 * @protocol_interface: interface of the protocol implementation
967 * Return: status code
969 efi_status_t efi_add_protocol(const efi_handle_t handle,
970 const efi_guid_t *protocol,
971 void *protocol_interface)
973 struct efi_object *efiobj;
974 struct efi_handler *handler;
977 efiobj = efi_search_obj(handle);
979 return EFI_INVALID_PARAMETER;
980 ret = efi_search_protocol(handle, protocol, NULL);
981 if (ret != EFI_NOT_FOUND)
982 return EFI_INVALID_PARAMETER;
983 handler = calloc(1, sizeof(struct efi_handler));
985 return EFI_OUT_OF_RESOURCES;
986 handler->guid = protocol;
987 handler->protocol_interface = protocol_interface;
988 INIT_LIST_HEAD(&handler->open_infos);
989 list_add_tail(&handler->link, &efiobj->protocols);
990 if (!guidcmp(&efi_guid_device_path, protocol))
991 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
996 * efi_install_protocol_interface() - install protocol interface
997 * @handle: handle on which the protocol shall be installed
998 * @protocol: GUID of the protocol to be installed
999 * @protocol_interface_type: type of the interface to be installed,
1000 * always EFI_NATIVE_INTERFACE
1001 * @protocol_interface: interface of the protocol implementation
1003 * This function implements the InstallProtocolInterface service.
1005 * See the Unified Extensible Firmware Interface (UEFI) specification for
1008 * Return: status code
1010 static efi_status_t EFIAPI efi_install_protocol_interface(
1011 void **handle, const efi_guid_t *protocol,
1012 int protocol_interface_type, void *protocol_interface)
1016 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1017 protocol_interface);
1019 if (!handle || !protocol ||
1020 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1021 r = EFI_INVALID_PARAMETER;
1025 /* Create new handle if requested. */
1027 r = efi_create_handle(handle);
1028 if (r != EFI_SUCCESS)
1030 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1033 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1036 /* Add new protocol */
1037 r = efi_add_protocol(*handle, protocol, protocol_interface);
1043 * efi_get_drivers() - get all drivers associated to a controller
1044 * @efiobj: handle of the controller
1045 * @protocol: protocol guid (optional)
1046 * @number_of_drivers: number of child controllers
1047 * @driver_handle_buffer: handles of the the drivers
1049 * The allocated buffer has to be freed with free().
1051 * Return: status code
1053 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1054 const efi_guid_t *protocol,
1055 efi_uintn_t *number_of_drivers,
1056 efi_handle_t **driver_handle_buffer)
1058 struct efi_handler *handler;
1059 struct efi_open_protocol_info_item *item;
1060 efi_uintn_t count = 0, i;
1063 /* Count all driver associations */
1064 list_for_each_entry(handler, &efiobj->protocols, link) {
1065 if (protocol && guidcmp(handler->guid, protocol))
1067 list_for_each_entry(item, &handler->open_infos, link) {
1068 if (item->info.attributes &
1069 EFI_OPEN_PROTOCOL_BY_DRIVER)
1074 * Create buffer. In case of duplicate driver assignments the buffer
1075 * will be too large. But that does not harm.
1077 *number_of_drivers = 0;
1078 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1079 if (!*driver_handle_buffer)
1080 return EFI_OUT_OF_RESOURCES;
1081 /* Collect unique driver handles */
1082 list_for_each_entry(handler, &efiobj->protocols, link) {
1083 if (protocol && guidcmp(handler->guid, protocol))
1085 list_for_each_entry(item, &handler->open_infos, link) {
1086 if (item->info.attributes &
1087 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1088 /* Check this is a new driver */
1090 for (i = 0; i < *number_of_drivers; ++i) {
1091 if ((*driver_handle_buffer)[i] ==
1092 item->info.agent_handle)
1095 /* Copy handle to buffer */
1097 i = (*number_of_drivers)++;
1098 (*driver_handle_buffer)[i] =
1099 item->info.agent_handle;
1108 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1109 * @efiobj: handle of the controller
1110 * @protocol: protocol guid (optional)
1111 * @child_handle: handle of the child to destroy
1113 * This function implements the DisconnectController service.
1115 * See the Unified Extensible Firmware Interface (UEFI) specification for
1118 * Return: status code
1120 static efi_status_t efi_disconnect_all_drivers(
1121 struct efi_object *efiobj,
1122 const efi_guid_t *protocol,
1123 efi_handle_t child_handle)
1125 efi_uintn_t number_of_drivers;
1126 efi_handle_t *driver_handle_buffer;
1127 efi_status_t r, ret;
1129 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1130 &driver_handle_buffer);
1131 if (ret != EFI_SUCCESS)
1134 ret = EFI_NOT_FOUND;
1135 while (number_of_drivers) {
1136 r = EFI_CALL(efi_disconnect_controller(
1138 driver_handle_buffer[--number_of_drivers],
1140 if (r == EFI_SUCCESS)
1143 free(driver_handle_buffer);
1148 * efi_uninstall_protocol_interface() - uninstall protocol interface
1149 * @handle: handle from which the protocol shall be removed
1150 * @protocol: GUID of the protocol to be removed
1151 * @protocol_interface: interface to be removed
1153 * This function implements the UninstallProtocolInterface service.
1155 * See the Unified Extensible Firmware Interface (UEFI) specification for
1158 * Return: status code
1160 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1161 efi_handle_t handle, const efi_guid_t *protocol,
1162 void *protocol_interface)
1164 struct efi_object *efiobj;
1165 struct efi_handler *handler;
1166 struct efi_open_protocol_info_item *item;
1167 struct efi_open_protocol_info_item *pos;
1170 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1173 efiobj = efi_search_obj(handle);
1175 r = EFI_INVALID_PARAMETER;
1178 /* Find the protocol on the handle */
1179 r = efi_search_protocol(handle, protocol, &handler);
1180 if (r != EFI_SUCCESS)
1182 /* Disconnect controllers */
1183 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1184 if (!list_empty(&handler->open_infos)) {
1185 r = EFI_ACCESS_DENIED;
1188 /* Close protocol */
1189 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1190 if (item->info.attributes ==
1191 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1192 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1193 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1194 list_del(&item->link);
1196 if (!list_empty(&handler->open_infos)) {
1197 r = EFI_ACCESS_DENIED;
1200 r = efi_remove_protocol(handle, protocol, protocol_interface);
1206 * efi_register_protocol_notify() - register an event for notification when a
1207 * protocol is installed.
1208 * @protocol: GUID of the protocol whose installation shall be notified
1209 * @event: event to be signaled upon installation of the protocol
1210 * @registration: key for retrieving the registration information
1212 * This function implements the RegisterProtocolNotify service.
1213 * See the Unified Extensible Firmware Interface (UEFI) specification
1216 * Return: status code
1218 static efi_status_t EFIAPI efi_register_protocol_notify(
1219 const efi_guid_t *protocol,
1220 struct efi_event *event,
1221 void **registration)
1223 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1224 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1228 * efi_search() - determine if an EFI handle implements a protocol
1229 * @search_type: selection criterion
1230 * @protocol: GUID of the protocol
1231 * @search_key: registration key
1234 * See the documentation of the LocateHandle service in the UEFI specification.
1236 * Return: 0 if the handle implements the protocol
1238 static int efi_search(enum efi_locate_search_type search_type,
1239 const efi_guid_t *protocol, void *search_key,
1240 struct efi_object *efiobj)
1244 switch (search_type) {
1247 case BY_REGISTER_NOTIFY:
1248 /* TODO: RegisterProtocolNotify is not implemented yet */
1251 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1252 return (ret != EFI_SUCCESS);
1254 /* Invalid search type */
1260 * efi_locate_handle() - locate handles implementing a protocol
1261 * @search_type: selection criterion
1262 * @protocol: GUID of the protocol
1263 * @search_key: registration key
1264 * @buffer_size: size of the buffer to receive the handles in bytes
1265 * @buffer: buffer to receive the relevant handles
1267 * This function is meant for U-Boot internal calls. For the API implementation
1268 * of the LocateHandle service see efi_locate_handle_ext.
1270 * Return: status code
1272 static efi_status_t efi_locate_handle(
1273 enum efi_locate_search_type search_type,
1274 const efi_guid_t *protocol, void *search_key,
1275 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1277 struct efi_object *efiobj;
1278 efi_uintn_t size = 0;
1280 /* Check parameters */
1281 switch (search_type) {
1284 case BY_REGISTER_NOTIFY:
1286 return EFI_INVALID_PARAMETER;
1287 /* RegisterProtocolNotify is not implemented yet */
1288 return EFI_UNSUPPORTED;
1291 return EFI_INVALID_PARAMETER;
1294 return EFI_INVALID_PARAMETER;
1298 * efi_locate_handle_buffer uses this function for
1299 * the calculation of the necessary buffer size.
1300 * So do not require a buffer for buffersize == 0.
1302 if (!buffer_size || (*buffer_size && !buffer))
1303 return EFI_INVALID_PARAMETER;
1305 /* Count how much space we need */
1306 list_for_each_entry(efiobj, &efi_obj_list, link) {
1307 if (!efi_search(search_type, protocol, search_key, efiobj))
1308 size += sizeof(void *);
1311 if (*buffer_size < size) {
1312 *buffer_size = size;
1313 return EFI_BUFFER_TOO_SMALL;
1316 *buffer_size = size;
1318 return EFI_NOT_FOUND;
1320 /* Then fill the array */
1321 list_for_each_entry(efiobj, &efi_obj_list, link) {
1322 if (!efi_search(search_type, protocol, search_key, efiobj))
1323 *buffer++ = efiobj->handle;
1330 * efi_locate_handle_ext() - locate handles implementing a protocol.
1331 * @search_type: selection criterion
1332 * @protocol: GUID of the protocol
1333 * @search_key: registration key
1334 * @buffer_size: size of the buffer to receive the handles in bytes
1335 * @buffer: buffer to receive the relevant handles
1337 * This function implements the LocateHandle service.
1339 * See the Unified Extensible Firmware Interface (UEFI) specification for
1342 * Return: 0 if the handle implements the protocol
1344 static efi_status_t EFIAPI efi_locate_handle_ext(
1345 enum efi_locate_search_type search_type,
1346 const efi_guid_t *protocol, void *search_key,
1347 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1349 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1350 buffer_size, buffer);
1352 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1353 buffer_size, buffer));
1357 * efi_remove_configuration_table() - collapses configuration table entries,
1360 * @i: index of the table entry to be removed
1362 static void efi_remove_configuration_table(int i)
1364 struct efi_configuration_table *this = &efi_conf_table[i];
1365 struct efi_configuration_table *next = &efi_conf_table[i + 1];
1366 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1368 memmove(this, next, (ulong)end - (ulong)next);
1373 * efi_install_configuration_table() - adds, updates, or removes a
1374 * configuration table
1375 * @guid: GUID of the installed table
1376 * @table: table to be installed
1378 * This function is used for internal calls. For the API implementation of the
1379 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1381 * Return: status code
1383 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1386 struct efi_event *evt;
1390 return EFI_INVALID_PARAMETER;
1392 /* Check for guid override */
1393 for (i = 0; i < systab.nr_tables; i++) {
1394 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1396 efi_conf_table[i].table = table;
1398 efi_remove_configuration_table(i);
1404 return EFI_NOT_FOUND;
1406 /* No override, check for overflow */
1407 if (i >= ARRAY_SIZE(efi_conf_table))
1408 return EFI_OUT_OF_RESOURCES;
1410 /* Add a new entry */
1411 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1412 efi_conf_table[i].table = table;
1413 systab.nr_tables = i + 1;
1416 /* Notify that the configuration table was changed */
1417 list_for_each_entry(evt, &efi_events, link) {
1418 if (evt->group && !guidcmp(evt->group, guid)) {
1419 efi_signal_event(evt, false);
1428 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1429 * configuration table.
1430 * @guid: GUID of the installed table
1431 * @table: table to be installed
1433 * This function implements the InstallConfigurationTable service.
1435 * See the Unified Extensible Firmware Interface (UEFI) specification for
1438 * Return: status code
1440 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1443 EFI_ENTRY("%pUl, %p", guid, table);
1444 return EFI_EXIT(efi_install_configuration_table(guid, table));
1448 * efi_setup_loaded_image() - initialize a loaded image
1449 * @info: loaded image info to be passed to the entry point of the image
1450 * @obj: internal object associated with the loaded image
1451 * @device_path: device path of the loaded image
1452 * @file_path: file path of the loaded image
1454 * Initialize a loaded_image_info and loaded_image_info object with correct
1455 * protocols, boot-device, etc.
1457 * Return: status code
1459 efi_status_t efi_setup_loaded_image(
1460 struct efi_loaded_image *info, struct efi_object *obj,
1461 struct efi_device_path *device_path,
1462 struct efi_device_path *file_path)
1466 /* Add internal object to object list */
1467 efi_add_handle(obj);
1468 /* efi_exit() assumes that the handle points to the info */
1471 info->file_path = file_path;
1474 info->device_handle = efi_dp_find_obj(device_path, NULL);
1476 * When asking for the device path interface, return
1477 * bootefi_device_path
1479 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1481 if (ret != EFI_SUCCESS)
1486 * When asking for the loaded_image interface, just
1487 * return handle which points to loaded_image_info
1489 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1490 if (ret != EFI_SUCCESS)
1493 ret = efi_add_protocol(obj->handle,
1494 &efi_guid_device_path_to_text_protocol,
1495 (void *)&efi_device_path_to_text);
1496 if (ret != EFI_SUCCESS)
1499 ret = efi_add_protocol(obj->handle,
1500 &efi_guid_device_path_utilities_protocol,
1501 (void *)&efi_device_path_utilities);
1502 if (ret != EFI_SUCCESS)
1507 printf("ERROR: Failure to install protocols for loaded image\n");
1512 * efi_load_image_from_path() - load an image using a file path
1513 * @file_path: the path of the image to load
1514 * @buffer: buffer containing the loaded image
1516 * Return: status code
1518 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1521 struct efi_file_info *info = NULL;
1522 struct efi_file_handle *f;
1523 static efi_status_t ret;
1526 f = efi_file_from_path(file_path);
1528 return EFI_DEVICE_ERROR;
1531 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1533 if (ret == EFI_BUFFER_TOO_SMALL) {
1535 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1538 if (ret != EFI_SUCCESS)
1541 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1545 bs = info->file_size;
1546 EFI_CALL(ret = f->read(f, &bs, *buffer));
1550 EFI_CALL(f->close(f));
1552 if (ret != EFI_SUCCESS) {
1553 efi_free_pool(*buffer);
1561 * efi_load_image() - load an EFI image into memory
1562 * @boot_policy: true for request originating from the boot manager
1563 * @parent_image: the caller's image handle
1564 * @file_path: the path of the image to load
1565 * @source_buffer: memory location from which the image is installed
1566 * @source_size: size of the memory area from which the image is installed
1567 * @image_handle: handle for the newly installed image
1569 * This function implements the LoadImage service.
1571 * See the Unified Extensible Firmware Interface (UEFI) specification
1574 * Return: status code
1576 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1577 efi_handle_t parent_image,
1578 struct efi_device_path *file_path,
1579 void *source_buffer,
1580 efi_uintn_t source_size,
1581 efi_handle_t *image_handle)
1583 struct efi_loaded_image *info;
1584 struct efi_object *obj;
1587 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1588 file_path, source_buffer, source_size, image_handle);
1590 if (!image_handle || !parent_image) {
1591 ret = EFI_INVALID_PARAMETER;
1595 if (!source_buffer && !file_path) {
1596 ret = EFI_NOT_FOUND;
1600 info = calloc(1, sizeof(*info));
1602 ret = EFI_OUT_OF_RESOURCES;
1605 obj = calloc(1, sizeof(*obj));
1608 ret = EFI_OUT_OF_RESOURCES;
1612 if (!source_buffer) {
1613 struct efi_device_path *dp, *fp;
1615 ret = efi_load_image_from_path(file_path, &source_buffer);
1616 if (ret != EFI_SUCCESS)
1619 * split file_path which contains both the device and
1622 efi_dp_split_file_path(file_path, &dp, &fp);
1623 ret = efi_setup_loaded_image(info, obj, dp, fp);
1624 if (ret != EFI_SUCCESS)
1627 /* In this case, file_path is the "device" path, ie.
1628 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1630 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1631 if (ret != EFI_SUCCESS)
1634 info->reserved = efi_load_pe(source_buffer, info);
1635 if (!info->reserved) {
1636 ret = EFI_UNSUPPORTED;
1639 info->system_table = &systab;
1640 info->parent_handle = parent_image;
1641 *image_handle = obj->handle;
1642 return EFI_EXIT(EFI_SUCCESS);
1645 efi_delete_handle(obj);
1647 return EFI_EXIT(ret);
1651 * efi_start_image() - dall the entry point of an image
1652 * @image_handle: handle of the image
1653 * @exit_data_size: size of the buffer
1654 * @exit_data: buffer to receive the exit data of the called image
1656 * This function implements the StartImage service.
1658 * See the Unified Extensible Firmware Interface (UEFI) specification for
1661 * Return: status code
1663 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1664 unsigned long *exit_data_size,
1667 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1668 struct efi_system_table *st);
1669 struct efi_loaded_image *info = image_handle;
1672 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1673 entry = info->reserved;
1675 efi_is_direct_boot = false;
1677 /* call the image! */
1678 if (setjmp(&info->exit_jmp)) {
1680 * We called the entry point of the child image with EFI_CALL
1681 * in the lines below. The child image called the Exit() boot
1682 * service efi_exit() which executed the long jump that brought
1683 * us to the current line. This implies that the second half
1684 * of the EFI_CALL macro has not been executed.
1688 * efi_exit() called efi_restore_gd(). We have to undo this
1689 * otherwise __efi_entry_check() will put the wrong value into
1695 * To get ready to call EFI_EXIT below we have to execute the
1696 * missed out steps of EFI_CALL.
1698 assert(__efi_entry_check());
1699 debug("%sEFI: %lu returned by started image\n",
1700 __efi_nesting_dec(),
1701 (unsigned long)((uintptr_t)info->exit_status &
1703 return EFI_EXIT(info->exit_status);
1706 ret = EFI_CALL(entry(image_handle, &systab));
1709 * Usually UEFI applications call Exit() instead of returning.
1710 * But because the world doesn not consist of ponies and unicorns,
1711 * we're happy to emulate that behavior on behalf of a payload
1714 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1718 * efi_exit() - leave an EFI application or driver
1719 * @image_handle: handle of the application or driver that is exiting
1720 * @exit_status: status code
1721 * @exit_data_size: size of the buffer in bytes
1722 * @exit_data: buffer with data describing an error
1724 * This function implements the Exit service.
1726 * See the Unified Extensible Firmware Interface (UEFI) specification for
1729 * Return: status code
1731 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1732 efi_status_t exit_status,
1733 unsigned long exit_data_size,
1737 * We require that the handle points to the original loaded
1738 * image protocol interface.
1740 * For getting the longjmp address this is safer than locating
1741 * the protocol because the protocol may have been reinstalled
1742 * pointing to another memory location.
1744 * TODO: We should call the unload procedure of the loaded
1747 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
1749 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1750 exit_data_size, exit_data);
1752 /* Make sure entry/exit counts for EFI world cross-overs match */
1753 EFI_EXIT(exit_status);
1756 * But longjmp out with the U-Boot gd, not the application's, as
1757 * the other end is a setjmp call inside EFI context.
1761 loaded_image_info->exit_status = exit_status;
1762 longjmp(&loaded_image_info->exit_jmp, 1);
1764 panic("EFI application exited");
1768 * efi_unload_image() - unload an EFI image
1769 * @image_handle: handle of the image to be unloaded
1771 * This function implements the UnloadImage service.
1773 * See the Unified Extensible Firmware Interface (UEFI) specification for
1776 * Return: status code
1778 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1780 struct efi_object *efiobj;
1782 EFI_ENTRY("%p", image_handle);
1783 efiobj = efi_search_obj(image_handle);
1785 list_del(&efiobj->link);
1787 return EFI_EXIT(EFI_SUCCESS);
1791 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1793 static void efi_exit_caches(void)
1795 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1797 * Grub on 32bit ARM needs to have caches disabled before jumping into
1798 * a zImage, but does not know of all cache layers. Give it a hand.
1800 if (efi_is_direct_boot)
1801 cleanup_before_linux();
1806 * efi_exit_boot_services() - stop all boot services
1807 * @image_handle: handle of the loaded image
1808 * @map_key: key of the memory map
1810 * This function implements the ExitBootServices service.
1812 * See the Unified Extensible Firmware Interface (UEFI) specification
1815 * All timer events are disabled. For exit boot services events the
1816 * notification function is called. The boot services are disabled in the
1819 * Return: status code
1821 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1822 unsigned long map_key)
1824 struct efi_event *evt;
1826 EFI_ENTRY("%p, %ld", image_handle, map_key);
1828 /* Make sure that notification functions are not called anymore */
1829 efi_tpl = TPL_HIGH_LEVEL;
1831 /* Check if ExitBootServices has already been called */
1832 if (!systab.boottime)
1833 return EFI_EXIT(EFI_SUCCESS);
1835 /* Add related events to the event group */
1836 list_for_each_entry(evt, &efi_events, link) {
1837 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1838 evt->group = &efi_guid_event_group_exit_boot_services;
1840 /* Notify that ExitBootServices is invoked. */
1841 list_for_each_entry(evt, &efi_events, link) {
1843 !guidcmp(evt->group,
1844 &efi_guid_event_group_exit_boot_services)) {
1845 efi_signal_event(evt, false);
1850 /* TODO Should persist EFI variables here */
1852 board_quiesce_devices();
1854 /* Fix up caches for EFI payloads if necessary */
1857 /* This stops all lingering devices */
1858 bootm_disable_interrupts();
1860 /* Disable boottime services */
1861 systab.con_in_handle = NULL;
1862 systab.con_in = NULL;
1863 systab.con_out_handle = NULL;
1864 systab.con_out = NULL;
1865 systab.stderr_handle = NULL;
1866 systab.std_err = NULL;
1867 systab.boottime = NULL;
1869 /* Recalculate CRC32 */
1870 systab.hdr.crc32 = 0;
1871 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1872 sizeof(struct efi_system_table));
1874 /* Give the payload some time to boot */
1875 efi_set_watchdog(0);
1878 return EFI_EXIT(EFI_SUCCESS);
1882 * efi_get_next_monotonic_count() - get next value of the counter
1883 * @count: returned value of the counter
1885 * This function implements the NextMonotonicCount service.
1887 * See the Unified Extensible Firmware Interface (UEFI) specification for
1890 * Return: status code
1892 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1894 static uint64_t mono;
1896 EFI_ENTRY("%p", count);
1898 return EFI_EXIT(EFI_SUCCESS);
1902 * efi_stall() - sleep
1903 * @microseconds: period to sleep in microseconds
1905 * This function implements the Stall service.
1907 * See the Unified Extensible Firmware Interface (UEFI) specification for
1910 * Return: status code
1912 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1914 EFI_ENTRY("%ld", microseconds);
1915 udelay(microseconds);
1916 return EFI_EXIT(EFI_SUCCESS);
1920 * efi_set_watchdog_timer() - reset the watchdog timer
1921 * @timeout: seconds before reset by watchdog
1922 * @watchdog_code: code to be logged when resetting
1923 * @data_size: size of buffer in bytes
1924 * @watchdog_data: buffer with data describing the reset reason
1926 * This function implements the SetWatchdogTimer service.
1928 * See the Unified Extensible Firmware Interface (UEFI) specification for
1931 * Return: status code
1933 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1934 uint64_t watchdog_code,
1935 unsigned long data_size,
1936 uint16_t *watchdog_data)
1938 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
1939 data_size, watchdog_data);
1940 return EFI_EXIT(efi_set_watchdog(timeout));
1944 * efi_close_protocol() - close a protocol
1945 * @handle: handle on which the protocol shall be closed
1946 * @protocol: GUID of the protocol to close
1947 * @agent_handle: handle of the driver
1948 * @controller_handle: handle of the controller
1950 * This function implements the CloseProtocol service.
1952 * See the Unified Extensible Firmware Interface (UEFI) specification for
1955 * Return: status code
1957 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1958 const efi_guid_t *protocol,
1959 efi_handle_t agent_handle,
1960 efi_handle_t controller_handle)
1962 struct efi_handler *handler;
1963 struct efi_open_protocol_info_item *item;
1964 struct efi_open_protocol_info_item *pos;
1967 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1970 if (!agent_handle) {
1971 r = EFI_INVALID_PARAMETER;
1974 r = efi_search_protocol(handle, protocol, &handler);
1975 if (r != EFI_SUCCESS)
1979 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1980 if (item->info.agent_handle == agent_handle &&
1981 item->info.controller_handle == controller_handle) {
1982 efi_delete_open_info(item);
1992 * efi_open_protocol_information() - provide information about then open status
1993 * of a protocol on a handle
1994 * @handle: handle for which the information shall be retrieved
1995 * @protocol: GUID of the protocol
1996 * @entry_buffer: buffer to receive the open protocol information
1997 * @entry_count: number of entries available in the buffer
1999 * This function implements the OpenProtocolInformation service.
2001 * See the Unified Extensible Firmware Interface (UEFI) specification for
2004 * Return: status code
2006 static efi_status_t EFIAPI efi_open_protocol_information(
2007 efi_handle_t handle, const efi_guid_t *protocol,
2008 struct efi_open_protocol_info_entry **entry_buffer,
2009 efi_uintn_t *entry_count)
2011 unsigned long buffer_size;
2012 unsigned long count;
2013 struct efi_handler *handler;
2014 struct efi_open_protocol_info_item *item;
2017 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2020 /* Check parameters */
2021 if (!entry_buffer) {
2022 r = EFI_INVALID_PARAMETER;
2025 r = efi_search_protocol(handle, protocol, &handler);
2026 if (r != EFI_SUCCESS)
2031 list_for_each_entry(item, &handler->open_infos, link) {
2032 if (item->info.open_count)
2035 *entry_count = count;
2036 *entry_buffer = NULL;
2043 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2044 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2045 (void **)entry_buffer);
2046 if (r != EFI_SUCCESS)
2048 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2049 if (item->info.open_count)
2050 (*entry_buffer)[--count] = item->info;
2057 * efi_protocols_per_handle() - get protocols installed on a handle
2058 * @handle: handle for which the information is retrieved
2059 * @protocol_buffer: buffer with protocol GUIDs
2060 * @protocol_buffer_count: number of entries in the buffer
2062 * This function implements the ProtocolsPerHandleService.
2064 * See the Unified Extensible Firmware Interface (UEFI) specification for
2067 * Return: status code
2069 static efi_status_t EFIAPI efi_protocols_per_handle(
2070 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2071 efi_uintn_t *protocol_buffer_count)
2073 unsigned long buffer_size;
2074 struct efi_object *efiobj;
2075 struct list_head *protocol_handle;
2078 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2079 protocol_buffer_count);
2081 if (!handle || !protocol_buffer || !protocol_buffer_count)
2082 return EFI_EXIT(EFI_INVALID_PARAMETER);
2084 *protocol_buffer = NULL;
2085 *protocol_buffer_count = 0;
2087 efiobj = efi_search_obj(handle);
2089 return EFI_EXIT(EFI_INVALID_PARAMETER);
2091 /* Count protocols */
2092 list_for_each(protocol_handle, &efiobj->protocols) {
2093 ++*protocol_buffer_count;
2097 if (*protocol_buffer_count) {
2100 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2101 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2102 (void **)protocol_buffer);
2103 if (r != EFI_SUCCESS)
2105 list_for_each(protocol_handle, &efiobj->protocols) {
2106 struct efi_handler *protocol;
2108 protocol = list_entry(protocol_handle,
2109 struct efi_handler, link);
2110 (*protocol_buffer)[j] = (void *)protocol->guid;
2115 return EFI_EXIT(EFI_SUCCESS);
2119 * efi_locate_handle_buffer() - locate handles implementing a protocol
2120 * @search_type: selection criterion
2121 * @protocol: GUID of the protocol
2122 * @search_key: registration key
2123 * @no_handles: number of returned handles
2124 * @buffer: buffer with the returned handles
2126 * This function implements the LocateHandleBuffer service.
2128 * See the Unified Extensible Firmware Interface (UEFI) specification for
2131 * Return: status code
2133 static efi_status_t EFIAPI efi_locate_handle_buffer(
2134 enum efi_locate_search_type search_type,
2135 const efi_guid_t *protocol, void *search_key,
2136 efi_uintn_t *no_handles, efi_handle_t **buffer)
2139 efi_uintn_t buffer_size = 0;
2141 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2142 no_handles, buffer);
2144 if (!no_handles || !buffer) {
2145 r = EFI_INVALID_PARAMETER;
2150 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2152 if (r != EFI_BUFFER_TOO_SMALL)
2154 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2156 if (r != EFI_SUCCESS)
2158 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2160 if (r == EFI_SUCCESS)
2161 *no_handles = buffer_size / sizeof(efi_handle_t);
2167 * efi_locate_protocol() - find an interface implementing a protocol
2168 * @protocol: GUID of the protocol
2169 * @registration: registration key passed to the notification function
2170 * @protocol_interface: interface implementing the protocol
2172 * This function implements the LocateProtocol service.
2174 * See the Unified Extensible Firmware Interface (UEFI) specification for
2177 * Return: status code
2179 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2181 void **protocol_interface)
2183 struct list_head *lhandle;
2186 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2188 if (!protocol || !protocol_interface)
2189 return EFI_EXIT(EFI_INVALID_PARAMETER);
2191 list_for_each(lhandle, &efi_obj_list) {
2192 struct efi_object *efiobj;
2193 struct efi_handler *handler;
2195 efiobj = list_entry(lhandle, struct efi_object, link);
2197 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2198 if (ret == EFI_SUCCESS) {
2199 *protocol_interface = handler->protocol_interface;
2200 return EFI_EXIT(EFI_SUCCESS);
2203 *protocol_interface = NULL;
2205 return EFI_EXIT(EFI_NOT_FOUND);
2209 * efi_locate_device_path() - Get the device path and handle of an device
2210 * implementing a protocol
2211 * @protocol: GUID of the protocol
2212 * @device_path: device path
2213 * @device: handle of the device
2215 * This function implements the LocateDevicePath service.
2217 * See the Unified Extensible Firmware Interface (UEFI) specification for
2220 * Return: status code
2222 static efi_status_t EFIAPI efi_locate_device_path(
2223 const efi_guid_t *protocol,
2224 struct efi_device_path **device_path,
2225 efi_handle_t *device)
2227 struct efi_device_path *dp;
2229 struct efi_handler *handler;
2230 efi_handle_t *handles;
2232 size_t len_best = 0;
2233 efi_uintn_t no_handles;
2237 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2239 if (!protocol || !device_path || !*device_path || !device) {
2240 ret = EFI_INVALID_PARAMETER;
2244 /* Find end of device path */
2245 len = efi_dp_instance_size(*device_path);
2247 /* Get all handles implementing the protocol */
2248 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2249 &no_handles, &handles));
2250 if (ret != EFI_SUCCESS)
2253 for (i = 0; i < no_handles; ++i) {
2254 /* Find the device path protocol */
2255 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2257 if (ret != EFI_SUCCESS)
2259 dp = (struct efi_device_path *)handler->protocol_interface;
2260 len_dp = efi_dp_instance_size(dp);
2262 * This handle can only be a better fit
2263 * if its device path length is longer than the best fit and
2264 * if its device path length is shorter of equal the searched
2267 if (len_dp <= len_best || len_dp > len)
2269 /* Check if dp is a subpath of device_path */
2270 if (memcmp(*device_path, dp, len_dp))
2272 *device = handles[i];
2276 remainder = (u8 *)*device_path + len_best;
2277 *device_path = (struct efi_device_path *)remainder;
2280 ret = EFI_NOT_FOUND;
2283 return EFI_EXIT(ret);
2287 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2289 * @handle: handle on which the protocol interfaces shall be installed
2290 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2293 * This function implements the MultipleProtocolInterfaces service.
2295 * See the Unified Extensible Firmware Interface (UEFI) specification for
2298 * Return: status code
2300 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2303 EFI_ENTRY("%p", handle);
2306 const efi_guid_t *protocol;
2307 void *protocol_interface;
2308 efi_status_t r = EFI_SUCCESS;
2312 return EFI_EXIT(EFI_INVALID_PARAMETER);
2314 va_start(argptr, handle);
2316 protocol = va_arg(argptr, efi_guid_t*);
2319 protocol_interface = va_arg(argptr, void*);
2320 r = EFI_CALL(efi_install_protocol_interface(
2322 EFI_NATIVE_INTERFACE,
2323 protocol_interface));
2324 if (r != EFI_SUCCESS)
2329 if (r == EFI_SUCCESS)
2332 /* If an error occurred undo all changes. */
2333 va_start(argptr, handle);
2335 protocol = va_arg(argptr, efi_guid_t*);
2336 protocol_interface = va_arg(argptr, void*);
2337 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2338 protocol_interface));
2346 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2348 * @handle: handle from which the protocol interfaces shall be removed
2349 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2352 * This function implements the UninstallMultipleProtocolInterfaces service.
2354 * See the Unified Extensible Firmware Interface (UEFI) specification for
2357 * Return: status code
2359 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2362 EFI_ENTRY("%p", handle);
2365 const efi_guid_t *protocol;
2366 void *protocol_interface;
2367 efi_status_t r = EFI_SUCCESS;
2371 return EFI_EXIT(EFI_INVALID_PARAMETER);
2373 va_start(argptr, handle);
2375 protocol = va_arg(argptr, efi_guid_t*);
2378 protocol_interface = va_arg(argptr, void*);
2379 r = EFI_CALL(efi_uninstall_protocol_interface(
2381 protocol_interface));
2382 if (r != EFI_SUCCESS)
2387 if (r == EFI_SUCCESS)
2390 /* If an error occurred undo all changes. */
2391 va_start(argptr, handle);
2393 protocol = va_arg(argptr, efi_guid_t*);
2394 protocol_interface = va_arg(argptr, void*);
2395 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2396 EFI_NATIVE_INTERFACE,
2397 protocol_interface));
2405 * efi_calculate_crc32() - calculate cyclic redundancy code
2406 * @data: buffer with data
2407 * @data_size: size of buffer in bytes
2408 * @crc32_p: cyclic redundancy code
2410 * This function implements the CalculateCrc32 service.
2412 * See the Unified Extensible Firmware Interface (UEFI) specification for
2415 * Return: status code
2417 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2418 unsigned long data_size,
2421 EFI_ENTRY("%p, %ld", data, data_size);
2422 *crc32_p = crc32(0, data, data_size);
2423 return EFI_EXIT(EFI_SUCCESS);
2427 * efi_copy_mem() - copy memory
2428 * @destination: destination of the copy operation
2429 * @source: source of the copy operation
2430 * @length: number of bytes to copy
2432 * This function implements the CopyMem service.
2434 * See the Unified Extensible Firmware Interface (UEFI) specification for
2437 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2440 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2441 memcpy(destination, source, length);
2442 EFI_EXIT(EFI_SUCCESS);
2446 * efi_set_mem() - Fill memory with a byte value.
2447 * @buffer: buffer to fill
2448 * @size: size of buffer in bytes
2449 * @value: byte to copy to the buffer
2451 * This function implements the SetMem service.
2453 * See the Unified Extensible Firmware Interface (UEFI) specification for
2456 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2458 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2459 memset(buffer, value, size);
2460 EFI_EXIT(EFI_SUCCESS);
2464 * efi_protocol_open() - open protocol interface on a handle
2465 * @handler: handler of a protocol
2466 * @protocol_interface: interface implementing the protocol
2467 * @agent_handle: handle of the driver
2468 * @controller_handle: handle of the controller
2469 * @attributes: attributes indicating how to open the protocol
2471 * Return: status code
2473 static efi_status_t efi_protocol_open(
2474 struct efi_handler *handler,
2475 void **protocol_interface, void *agent_handle,
2476 void *controller_handle, uint32_t attributes)
2478 struct efi_open_protocol_info_item *item;
2479 struct efi_open_protocol_info_entry *match = NULL;
2480 bool opened_by_driver = false;
2481 bool opened_exclusive = false;
2483 /* If there is no agent, only return the interface */
2487 /* For TEST_PROTOCOL ignore interface attribute */
2488 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2489 *protocol_interface = NULL;
2492 * Check if the protocol is already opened by a driver with the same
2493 * attributes or opened exclusively
2495 list_for_each_entry(item, &handler->open_infos, link) {
2496 if (item->info.agent_handle == agent_handle) {
2497 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2498 (item->info.attributes == attributes))
2499 return EFI_ALREADY_STARTED;
2501 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2502 opened_exclusive = true;
2505 /* Only one controller can open the protocol exclusively */
2506 if (opened_exclusive && attributes &
2507 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2508 return EFI_ACCESS_DENIED;
2510 /* Prepare exclusive opening */
2511 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2512 /* Try to disconnect controllers */
2513 list_for_each_entry(item, &handler->open_infos, link) {
2514 if (item->info.attributes ==
2515 EFI_OPEN_PROTOCOL_BY_DRIVER)
2516 EFI_CALL(efi_disconnect_controller(
2517 item->info.controller_handle,
2518 item->info.agent_handle,
2521 opened_by_driver = false;
2522 /* Check if all controllers are disconnected */
2523 list_for_each_entry(item, &handler->open_infos, link) {
2524 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2525 opened_by_driver = true;
2527 /* Only one controller can be conncected */
2528 if (opened_by_driver)
2529 return EFI_ACCESS_DENIED;
2532 /* Find existing entry */
2533 list_for_each_entry(item, &handler->open_infos, link) {
2534 if (item->info.agent_handle == agent_handle &&
2535 item->info.controller_handle == controller_handle)
2536 match = &item->info;
2538 /* None found, create one */
2540 match = efi_create_open_info(handler);
2542 return EFI_OUT_OF_RESOURCES;
2545 match->agent_handle = agent_handle;
2546 match->controller_handle = controller_handle;
2547 match->attributes = attributes;
2548 match->open_count++;
2551 /* For TEST_PROTOCOL ignore interface attribute. */
2552 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2553 *protocol_interface = handler->protocol_interface;
2559 * efi_open_protocol() - open protocol interface on a handle
2560 * @handle: handle on which the protocol shall be opened
2561 * @protocol: GUID of the protocol
2562 * @protocol_interface: interface implementing the protocol
2563 * @agent_handle: handle of the driver
2564 * @controller_handle: handle of the controller
2565 * @attributes: attributes indicating how to open the protocol
2567 * This function implements the OpenProtocol interface.
2569 * See the Unified Extensible Firmware Interface (UEFI) specification for
2572 * Return: status code
2574 static efi_status_t EFIAPI efi_open_protocol(
2575 void *handle, const efi_guid_t *protocol,
2576 void **protocol_interface, void *agent_handle,
2577 void *controller_handle, uint32_t attributes)
2579 struct efi_handler *handler;
2580 efi_status_t r = EFI_INVALID_PARAMETER;
2582 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2583 protocol_interface, agent_handle, controller_handle,
2586 if (!handle || !protocol ||
2587 (!protocol_interface && attributes !=
2588 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2592 switch (attributes) {
2593 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2594 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2595 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2597 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2598 if (controller_handle == handle)
2601 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2602 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2603 /* Check that the controller handle is valid */
2604 if (!efi_search_obj(controller_handle))
2607 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2608 /* Check that the agent handle is valid */
2609 if (!efi_search_obj(agent_handle))
2616 r = efi_search_protocol(handle, protocol, &handler);
2617 if (r != EFI_SUCCESS)
2620 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2621 controller_handle, attributes);
2627 * efi_handle_protocol() - get interface of a protocol on a handle
2628 * @handle: handle on which the protocol shall be opened
2629 * @protocol: GUID of the protocol
2630 * @protocol_interface: interface implementing the protocol
2632 * This function implements the HandleProtocol service.
2634 * See the Unified Extensible Firmware Interface (UEFI) specification for
2637 * Return: status code
2639 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2640 const efi_guid_t *protocol,
2641 void **protocol_interface)
2643 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2644 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2648 * efi_bind_controller() - bind a single driver to a controller
2649 * @controller_handle: controller handle
2650 * @driver_image_handle: driver handle
2651 * @remain_device_path: remaining path
2653 * Return: status code
2655 static efi_status_t efi_bind_controller(
2656 efi_handle_t controller_handle,
2657 efi_handle_t driver_image_handle,
2658 struct efi_device_path *remain_device_path)
2660 struct efi_driver_binding_protocol *binding_protocol;
2663 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2664 &efi_guid_driver_binding_protocol,
2665 (void **)&binding_protocol,
2666 driver_image_handle, NULL,
2667 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2668 if (r != EFI_SUCCESS)
2670 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2672 remain_device_path));
2673 if (r == EFI_SUCCESS)
2674 r = EFI_CALL(binding_protocol->start(binding_protocol,
2676 remain_device_path));
2677 EFI_CALL(efi_close_protocol(driver_image_handle,
2678 &efi_guid_driver_binding_protocol,
2679 driver_image_handle, NULL));
2684 * efi_connect_single_controller() - connect a single driver to a controller
2685 * @controller_handle: controller
2686 * @driver_image_handle: driver
2687 * @remain_device_path: remainting path
2689 * Return: status code
2691 static efi_status_t efi_connect_single_controller(
2692 efi_handle_t controller_handle,
2693 efi_handle_t *driver_image_handle,
2694 struct efi_device_path *remain_device_path)
2696 efi_handle_t *buffer;
2700 size_t connected = 0;
2702 /* Get buffer with all handles with driver binding protocol */
2703 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2704 &efi_guid_driver_binding_protocol,
2705 NULL, &count, &buffer));
2706 if (r != EFI_SUCCESS)
2709 /* Context Override */
2710 if (driver_image_handle) {
2711 for (; *driver_image_handle; ++driver_image_handle) {
2712 for (i = 0; i < count; ++i) {
2713 if (buffer[i] == *driver_image_handle) {
2715 r = efi_bind_controller(
2717 *driver_image_handle,
2718 remain_device_path);
2720 * For drivers that do not support the
2721 * controller or are already connected
2722 * we receive an error code here.
2724 if (r == EFI_SUCCESS)
2732 * TODO: Some overrides are not yet implemented:
2733 * - Platform Driver Override
2734 * - Driver Family Override Search
2735 * - Bus Specific Driver Override
2738 /* Driver Binding Search */
2739 for (i = 0; i < count; ++i) {
2741 r = efi_bind_controller(controller_handle,
2743 remain_device_path);
2744 if (r == EFI_SUCCESS)
2749 efi_free_pool(buffer);
2751 return EFI_NOT_FOUND;
2756 * efi_connect_controller() - connect a controller to a driver
2757 * @controller_handle: handle of the controller
2758 * @driver_image_handle: handle of the driver
2759 * @remain_device_path: device path of a child controller
2760 * @recursive: true to connect all child controllers
2762 * This function implements the ConnectController service.
2764 * See the Unified Extensible Firmware Interface (UEFI) specification for
2767 * First all driver binding protocol handles are tried for binding drivers.
2768 * Afterwards all handles that have openened a protocol of the controller
2769 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2771 * Return: status code
2773 static efi_status_t EFIAPI efi_connect_controller(
2774 efi_handle_t controller_handle,
2775 efi_handle_t *driver_image_handle,
2776 struct efi_device_path *remain_device_path,
2780 efi_status_t ret = EFI_NOT_FOUND;
2781 struct efi_object *efiobj;
2783 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2784 remain_device_path, recursive);
2786 efiobj = efi_search_obj(controller_handle);
2788 ret = EFI_INVALID_PARAMETER;
2792 r = efi_connect_single_controller(controller_handle,
2793 driver_image_handle,
2794 remain_device_path);
2795 if (r == EFI_SUCCESS)
2798 struct efi_handler *handler;
2799 struct efi_open_protocol_info_item *item;
2801 list_for_each_entry(handler, &efiobj->protocols, link) {
2802 list_for_each_entry(item, &handler->open_infos, link) {
2803 if (item->info.attributes &
2804 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2805 r = EFI_CALL(efi_connect_controller(
2806 item->info.controller_handle,
2807 driver_image_handle,
2810 if (r == EFI_SUCCESS)
2816 /* Check for child controller specified by end node */
2817 if (ret != EFI_SUCCESS && remain_device_path &&
2818 remain_device_path->type == DEVICE_PATH_TYPE_END)
2821 return EFI_EXIT(ret);
2825 * efi_reinstall_protocol_interface() - reinstall protocol interface
2826 * @handle: handle on which the protocol shall be reinstalled
2827 * @protocol: GUID of the protocol to be installed
2828 * @old_interface: interface to be removed
2829 * @new_interface: interface to be installed
2831 * This function implements the ReinstallProtocolInterface service.
2833 * See the Unified Extensible Firmware Interface (UEFI) specification for
2836 * The old interface is uninstalled. The new interface is installed.
2837 * Drivers are connected.
2839 * Return: status code
2841 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2842 efi_handle_t handle, const efi_guid_t *protocol,
2843 void *old_interface, void *new_interface)
2847 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2849 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2851 if (ret != EFI_SUCCESS)
2853 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2854 EFI_NATIVE_INTERFACE,
2856 if (ret != EFI_SUCCESS)
2859 * The returned status code has to be ignored.
2860 * Do not create an error if no suitable driver for the handle exists.
2862 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2864 return EFI_EXIT(ret);
2868 * efi_get_child_controllers() - get all child controllers associated to a driver
2869 * @efiobj: handle of the controller
2870 * @driver_handle: handle of the driver
2871 * @number_of_children: number of child controllers
2872 * @child_handle_buffer: handles of the the child controllers
2874 * The allocated buffer has to be freed with free().
2876 * Return: status code
2878 static efi_status_t efi_get_child_controllers(
2879 struct efi_object *efiobj,
2880 efi_handle_t driver_handle,
2881 efi_uintn_t *number_of_children,
2882 efi_handle_t **child_handle_buffer)
2884 struct efi_handler *handler;
2885 struct efi_open_protocol_info_item *item;
2886 efi_uintn_t count = 0, i;
2889 /* Count all child controller associations */
2890 list_for_each_entry(handler, &efiobj->protocols, link) {
2891 list_for_each_entry(item, &handler->open_infos, link) {
2892 if (item->info.agent_handle == driver_handle &&
2893 item->info.attributes &
2894 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2899 * Create buffer. In case of duplicate child controller assignments
2900 * the buffer will be too large. But that does not harm.
2902 *number_of_children = 0;
2903 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2904 if (!*child_handle_buffer)
2905 return EFI_OUT_OF_RESOURCES;
2906 /* Copy unique child handles */
2907 list_for_each_entry(handler, &efiobj->protocols, link) {
2908 list_for_each_entry(item, &handler->open_infos, link) {
2909 if (item->info.agent_handle == driver_handle &&
2910 item->info.attributes &
2911 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2912 /* Check this is a new child controller */
2914 for (i = 0; i < *number_of_children; ++i) {
2915 if ((*child_handle_buffer)[i] ==
2916 item->info.controller_handle)
2919 /* Copy handle to buffer */
2921 i = (*number_of_children)++;
2922 (*child_handle_buffer)[i] =
2923 item->info.controller_handle;
2932 * efi_disconnect_controller() - disconnect a controller from a driver
2933 * @controller_handle: handle of the controller
2934 * @driver_image_handle: handle of the driver
2935 * @child_handle: handle of the child to destroy
2937 * This function implements the DisconnectController service.
2939 * See the Unified Extensible Firmware Interface (UEFI) specification for
2942 * Return: status code
2944 static efi_status_t EFIAPI efi_disconnect_controller(
2945 efi_handle_t controller_handle,
2946 efi_handle_t driver_image_handle,
2947 efi_handle_t child_handle)
2949 struct efi_driver_binding_protocol *binding_protocol;
2950 efi_handle_t *child_handle_buffer = NULL;
2951 size_t number_of_children = 0;
2953 size_t stop_count = 0;
2954 struct efi_object *efiobj;
2956 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2959 efiobj = efi_search_obj(controller_handle);
2961 r = EFI_INVALID_PARAMETER;
2965 if (child_handle && !efi_search_obj(child_handle)) {
2966 r = EFI_INVALID_PARAMETER;
2970 /* If no driver handle is supplied, disconnect all drivers */
2971 if (!driver_image_handle) {
2972 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2976 /* Create list of child handles */
2978 number_of_children = 1;
2979 child_handle_buffer = &child_handle;
2981 efi_get_child_controllers(efiobj,
2982 driver_image_handle,
2983 &number_of_children,
2984 &child_handle_buffer);
2987 /* Get the driver binding protocol */
2988 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2989 &efi_guid_driver_binding_protocol,
2990 (void **)&binding_protocol,
2991 driver_image_handle, NULL,
2992 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2993 if (r != EFI_SUCCESS)
2995 /* Remove the children */
2996 if (number_of_children) {
2997 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3000 child_handle_buffer));
3001 if (r == EFI_SUCCESS)
3004 /* Remove the driver */
3006 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3009 if (r == EFI_SUCCESS)
3011 EFI_CALL(efi_close_protocol(driver_image_handle,
3012 &efi_guid_driver_binding_protocol,
3013 driver_image_handle, NULL));
3021 free(child_handle_buffer);
3025 static const struct efi_boot_services efi_boot_services = {
3027 .headersize = sizeof(struct efi_table_hdr),
3029 .raise_tpl = efi_raise_tpl,
3030 .restore_tpl = efi_restore_tpl,
3031 .allocate_pages = efi_allocate_pages_ext,
3032 .free_pages = efi_free_pages_ext,
3033 .get_memory_map = efi_get_memory_map_ext,
3034 .allocate_pool = efi_allocate_pool_ext,
3035 .free_pool = efi_free_pool_ext,
3036 .create_event = efi_create_event_ext,
3037 .set_timer = efi_set_timer_ext,
3038 .wait_for_event = efi_wait_for_event,
3039 .signal_event = efi_signal_event_ext,
3040 .close_event = efi_close_event,
3041 .check_event = efi_check_event,
3042 .install_protocol_interface = efi_install_protocol_interface,
3043 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3044 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3045 .handle_protocol = efi_handle_protocol,
3047 .register_protocol_notify = efi_register_protocol_notify,
3048 .locate_handle = efi_locate_handle_ext,
3049 .locate_device_path = efi_locate_device_path,
3050 .install_configuration_table = efi_install_configuration_table_ext,
3051 .load_image = efi_load_image,
3052 .start_image = efi_start_image,
3054 .unload_image = efi_unload_image,
3055 .exit_boot_services = efi_exit_boot_services,
3056 .get_next_monotonic_count = efi_get_next_monotonic_count,
3058 .set_watchdog_timer = efi_set_watchdog_timer,
3059 .connect_controller = efi_connect_controller,
3060 .disconnect_controller = efi_disconnect_controller,
3061 .open_protocol = efi_open_protocol,
3062 .close_protocol = efi_close_protocol,
3063 .open_protocol_information = efi_open_protocol_information,
3064 .protocols_per_handle = efi_protocols_per_handle,
3065 .locate_handle_buffer = efi_locate_handle_buffer,
3066 .locate_protocol = efi_locate_protocol,
3067 .install_multiple_protocol_interfaces =
3068 efi_install_multiple_protocol_interfaces,
3069 .uninstall_multiple_protocol_interfaces =
3070 efi_uninstall_multiple_protocol_interfaces,
3071 .calculate_crc32 = efi_calculate_crc32,
3072 .copy_mem = efi_copy_mem,
3073 .set_mem = efi_set_mem,
3074 .create_event_ex = efi_create_event_ex,
3077 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3079 struct efi_system_table __efi_runtime_data systab = {
3081 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3082 .revision = 2 << 16 | 70, /* 2.7 */
3083 .headersize = sizeof(struct efi_table_hdr),
3085 .fw_vendor = (long)firmware_vendor,
3086 .con_in = (void *)&efi_con_in,
3087 .con_out = (void *)&efi_con_out,
3088 .std_err = (void *)&efi_con_out,
3089 .runtime = (void *)&efi_runtime_services,
3090 .boottime = (void *)&efi_boot_services,
3092 .tables = (void *)efi_conf_table,