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>
18 DECLARE_GLOBAL_DATA_PTR;
20 /* Task priority level */
21 static efi_uintn_t efi_tpl = TPL_APPLICATION;
23 /* This list contains all the EFI objects our payload has access to */
24 LIST_HEAD(efi_obj_list);
26 /* List of all events */
27 LIST_HEAD(efi_events);
31 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
32 * fixed when compiling U-Boot. However, the payload does not know about that
33 * restriction so we need to manually swap its and our view of that register on
34 * EFI callback entry/exit.
36 static volatile void *efi_gd, *app_gd;
39 static int entry_count;
40 static int nesting_level;
41 /* GUID of the device tree table */
42 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
43 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
44 const efi_guid_t efi_guid_driver_binding_protocol =
45 EFI_DRIVER_BINDING_PROTOCOL_GUID;
47 /* event group ExitBootServices() invoked */
48 const efi_guid_t efi_guid_event_group_exit_boot_services =
49 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
50 /* event group SetVirtualAddressMap() invoked */
51 const efi_guid_t efi_guid_event_group_virtual_address_change =
52 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
53 /* event group memory map changed */
54 const efi_guid_t efi_guid_event_group_memory_map_change =
55 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
56 /* event group boot manager about to boot */
57 const efi_guid_t efi_guid_event_group_ready_to_boot =
58 EFI_EVENT_GROUP_READY_TO_BOOT;
59 /* event group ResetSystem() invoked (before ExitBootServices) */
60 const efi_guid_t efi_guid_event_group_reset_system =
61 EFI_EVENT_GROUP_RESET_SYSTEM;
63 static efi_status_t EFIAPI efi_disconnect_controller(
64 efi_handle_t controller_handle,
65 efi_handle_t driver_image_handle,
66 efi_handle_t child_handle);
68 /* Called on every callback entry */
69 int __efi_entry_check(void)
71 int ret = entry_count++ == 0;
80 /* Called on every callback exit */
81 int __efi_exit_check(void)
83 int ret = --entry_count == 0;
90 /* Called from do_bootefi_exec() */
91 void efi_save_gd(void)
99 * Special case handler for error/abort that just forces things back to u-boot
100 * world so we can dump out an abort message, without any care about returning
101 * back to UEFI world.
103 void efi_restore_gd(void)
106 /* Only restore if we're already in EFI context */
114 * indent_string() - returns a string for indenting with two spaces per level
115 * @level: indent level
117 * A maximum of ten indent levels is supported. Higher indent levels will be
120 * Return: A string for indenting with two spaces per level is
123 static const char *indent_string(int level)
125 const char *indent = " ";
126 const int max = strlen(indent);
128 level = min(max, level * 2);
129 return &indent[max - level];
132 const char *__efi_nesting(void)
134 return indent_string(nesting_level);
137 const char *__efi_nesting_inc(void)
139 return indent_string(nesting_level++);
142 const char *__efi_nesting_dec(void)
144 return indent_string(--nesting_level);
148 * efi_queue_event() - queue an EFI event
149 * @event: event to signal
150 * @check_tpl: check the TPL level
152 * This function queues the notification function of the event for future
155 * The notification function is called if the task priority level of the event
156 * is higher than the current task priority level.
158 * For the SignalEvent service see efi_signal_event_ext.
161 static void efi_queue_event(struct efi_event *event, bool check_tpl)
163 if (event->notify_function) {
164 event->is_queued = true;
166 if (check_tpl && efi_tpl >= event->notify_tpl)
168 EFI_CALL_VOID(event->notify_function(event,
169 event->notify_context));
171 event->is_queued = false;
175 * is_valid_tpl() - check if the task priority level is valid
177 * @tpl: TPL level to check
178 * Return: status code
180 efi_status_t is_valid_tpl(efi_uintn_t tpl)
183 case TPL_APPLICATION:
189 return EFI_INVALID_PARAMETER;
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("%llx, 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;
598 case EVT_NOTIFY_SIGNAL:
599 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
600 case EVT_NOTIFY_WAIT:
601 case EVT_TIMER | EVT_NOTIFY_WAIT:
602 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
603 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
606 return EFI_INVALID_PARAMETER;
609 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
610 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
611 return EFI_INVALID_PARAMETER;
613 evt = calloc(1, sizeof(struct efi_event));
615 return EFI_OUT_OF_RESOURCES;
617 evt->notify_tpl = notify_tpl;
618 evt->notify_function = notify_function;
619 evt->notify_context = notify_context;
621 /* Disable timers on boot up */
622 evt->trigger_next = -1ULL;
623 evt->is_queued = false;
624 evt->is_signaled = false;
625 list_add_tail(&evt->link, &efi_events);
631 * efi_create_event_ex() - create an event in a group
632 * @type: type of the event to create
633 * @notify_tpl: task priority level of the event
634 * @notify_function: notification function of the event
635 * @notify_context: pointer passed to the notification function
636 * @event: created event
637 * @event_group: event group
639 * This function implements the CreateEventEx service.
641 * See the Unified Extensible Firmware Interface (UEFI) specification for
644 * Return: status code
646 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
647 void (EFIAPI *notify_function) (
648 struct efi_event *event,
650 void *notify_context,
651 efi_guid_t *event_group,
652 struct efi_event **event)
654 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
655 notify_context, event_group);
656 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
657 notify_context, event_group, event));
661 * efi_create_event_ext() - create an event
662 * @type: type of the event to create
663 * @notify_tpl: task priority level of the event
664 * @notify_function: notification function of the event
665 * @notify_context: pointer passed to the notification function
666 * @event: created event
668 * This function implements the CreateEvent service.
670 * See the Unified Extensible Firmware Interface (UEFI) specification for
673 * Return: status code
675 static efi_status_t EFIAPI efi_create_event_ext(
676 uint32_t type, efi_uintn_t notify_tpl,
677 void (EFIAPI *notify_function) (
678 struct efi_event *event,
680 void *notify_context, struct efi_event **event)
682 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
684 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
685 notify_context, NULL, event));
689 * efi_timer_check() - check if a timer event has occurred
691 * Check if a timer event has occurred or a queued notification function should
694 * Our timers have to work without interrupts, so we check whenever keyboard
695 * input or disk accesses happen if enough time elapsed for them to fire.
697 void efi_timer_check(void)
699 struct efi_event *evt;
700 u64 now = timer_get_us();
702 list_for_each_entry(evt, &efi_events, link) {
704 efi_queue_event(evt, true);
705 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
707 switch (evt->trigger_type) {
708 case EFI_TIMER_RELATIVE:
709 evt->trigger_type = EFI_TIMER_STOP;
711 case EFI_TIMER_PERIODIC:
712 evt->trigger_next += evt->trigger_time;
717 evt->is_signaled = false;
718 efi_signal_event(evt, true);
724 * efi_set_timer() - set the trigger time for a timer event or stop the event
725 * @event: event for which the timer is set
726 * @type: type of the timer
727 * @trigger_time: trigger period in multiples of 100 ns
729 * This is the function for internal usage in U-Boot. For the API function
730 * implementing the SetTimer service see efi_set_timer_ext.
732 * Return: status code
734 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
735 uint64_t trigger_time)
737 /* Check that the event is valid */
738 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
739 return EFI_INVALID_PARAMETER;
742 * The parameter defines a multiple of 100 ns.
743 * We use multiples of 1000 ns. So divide by 10.
745 do_div(trigger_time, 10);
749 event->trigger_next = -1ULL;
751 case EFI_TIMER_PERIODIC:
752 case EFI_TIMER_RELATIVE:
753 event->trigger_next = timer_get_us() + trigger_time;
756 return EFI_INVALID_PARAMETER;
758 event->trigger_type = type;
759 event->trigger_time = trigger_time;
760 event->is_signaled = false;
765 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
767 * @event: event for which the timer is set
768 * @type: type of the timer
769 * @trigger_time: trigger period in multiples of 100 ns
771 * This function implements the SetTimer service.
773 * See the Unified Extensible Firmware Interface (UEFI) specification for
777 * Return: status code
779 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
780 enum efi_timer_delay type,
781 uint64_t trigger_time)
783 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
784 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
788 * efi_wait_for_event() - wait for events to be signaled
789 * @num_events: number of events to be waited for
790 * @event: events to be waited for
791 * @index: index of the event that was signaled
793 * This function implements the WaitForEvent service.
795 * See the Unified Extensible Firmware Interface (UEFI) specification for
798 * Return: status code
800 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
801 struct efi_event **event,
806 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
808 /* Check parameters */
809 if (!num_events || !event)
810 return EFI_EXIT(EFI_INVALID_PARAMETER);
812 if (efi_tpl != TPL_APPLICATION)
813 return EFI_EXIT(EFI_UNSUPPORTED);
814 for (i = 0; i < num_events; ++i) {
815 if (efi_is_event(event[i]) != EFI_SUCCESS)
816 return EFI_EXIT(EFI_INVALID_PARAMETER);
817 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
818 return EFI_EXIT(EFI_INVALID_PARAMETER);
819 if (!event[i]->is_signaled)
820 efi_queue_event(event[i], true);
823 /* Wait for signal */
825 for (i = 0; i < num_events; ++i) {
826 if (event[i]->is_signaled)
829 /* Allow events to occur. */
835 * Reset the signal which is passed to the caller to allow periodic
838 event[i]->is_signaled = false;
842 return EFI_EXIT(EFI_SUCCESS);
846 * efi_signal_event_ext() - signal an EFI event
847 * @event: event to signal
849 * This function implements the SignalEvent service.
851 * See the Unified Extensible Firmware Interface (UEFI) specification for
854 * This functions sets the signaled state of the event and queues the
855 * notification function for execution.
857 * Return: status code
859 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
861 EFI_ENTRY("%p", event);
862 if (efi_is_event(event) != EFI_SUCCESS)
863 return EFI_EXIT(EFI_INVALID_PARAMETER);
864 efi_signal_event(event, true);
865 return EFI_EXIT(EFI_SUCCESS);
869 * efi_close_event() - close an EFI event
870 * @event: event to close
872 * This function implements the CloseEvent service.
874 * See the Unified Extensible Firmware Interface (UEFI) specification for
877 * Return: status code
879 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
881 EFI_ENTRY("%p", event);
882 if (efi_is_event(event) != EFI_SUCCESS)
883 return EFI_EXIT(EFI_INVALID_PARAMETER);
884 list_del(&event->link);
886 return EFI_EXIT(EFI_SUCCESS);
890 * efi_check_event() - check if an event is signaled
891 * @event: event to check
893 * This function implements the CheckEvent service.
895 * See the Unified Extensible Firmware Interface (UEFI) specification for
898 * If an event is not signaled yet, the notification function is queued. The
899 * signaled state is cleared.
901 * Return: status code
903 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
905 EFI_ENTRY("%p", event);
907 if (efi_is_event(event) != EFI_SUCCESS ||
908 event->type & EVT_NOTIFY_SIGNAL)
909 return EFI_EXIT(EFI_INVALID_PARAMETER);
910 if (!event->is_signaled)
911 efi_queue_event(event, true);
912 if (event->is_signaled) {
913 event->is_signaled = false;
914 return EFI_EXIT(EFI_SUCCESS);
916 return EFI_EXIT(EFI_NOT_READY);
920 * efi_search_obj() - find the internal EFI object for a handle
921 * @handle: handle to find
925 struct efi_object *efi_search_obj(const efi_handle_t handle)
927 struct efi_object *efiobj;
929 list_for_each_entry(efiobj, &efi_obj_list, link) {
930 if (efiobj->handle == handle)
938 * efi_open_protocol_info_entry() - create open protocol info entry and add it
940 * @handler: handler of a protocol
942 * Return: open protocol info entry
944 static struct efi_open_protocol_info_entry *efi_create_open_info(
945 struct efi_handler *handler)
947 struct efi_open_protocol_info_item *item;
949 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
952 /* Append the item to the open protocol info list. */
953 list_add_tail(&item->link, &handler->open_infos);
959 * efi_delete_open_info() - remove an open protocol info entry from a protocol
960 * @item: open protocol info entry to delete
962 * Return: status code
964 static efi_status_t efi_delete_open_info(
965 struct efi_open_protocol_info_item *item)
967 list_del(&item->link);
973 * efi_add_protocol() - install new protocol on a handle
974 * @handle: handle on which the protocol shall be installed
975 * @protocol: GUID of the protocol to be installed
976 * @protocol_interface: interface of the protocol implementation
978 * Return: status code
980 efi_status_t efi_add_protocol(const efi_handle_t handle,
981 const efi_guid_t *protocol,
982 void *protocol_interface)
984 struct efi_object *efiobj;
985 struct efi_handler *handler;
988 efiobj = efi_search_obj(handle);
990 return EFI_INVALID_PARAMETER;
991 ret = efi_search_protocol(handle, protocol, NULL);
992 if (ret != EFI_NOT_FOUND)
993 return EFI_INVALID_PARAMETER;
994 handler = calloc(1, sizeof(struct efi_handler));
996 return EFI_OUT_OF_RESOURCES;
997 handler->guid = protocol;
998 handler->protocol_interface = protocol_interface;
999 INIT_LIST_HEAD(&handler->open_infos);
1000 list_add_tail(&handler->link, &efiobj->protocols);
1001 if (!guidcmp(&efi_guid_device_path, protocol))
1002 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1007 * efi_install_protocol_interface() - install protocol interface
1008 * @handle: handle on which the protocol shall be installed
1009 * @protocol: GUID of the protocol to be installed
1010 * @protocol_interface_type: type of the interface to be installed,
1011 * always EFI_NATIVE_INTERFACE
1012 * @protocol_interface: interface of the protocol implementation
1014 * This function implements the InstallProtocolInterface service.
1016 * See the Unified Extensible Firmware Interface (UEFI) specification for
1019 * Return: status code
1021 static efi_status_t EFIAPI efi_install_protocol_interface(
1022 efi_handle_t *handle, const efi_guid_t *protocol,
1023 int protocol_interface_type, void *protocol_interface)
1027 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1028 protocol_interface);
1030 if (!handle || !protocol ||
1031 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1032 r = EFI_INVALID_PARAMETER;
1036 /* Create new handle if requested. */
1038 r = efi_create_handle(handle);
1039 if (r != EFI_SUCCESS)
1041 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1044 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1047 /* Add new protocol */
1048 r = efi_add_protocol(*handle, protocol, protocol_interface);
1054 * efi_get_drivers() - get all drivers associated to a controller
1055 * @efiobj: handle of the controller
1056 * @protocol: protocol GUID (optional)
1057 * @number_of_drivers: number of child controllers
1058 * @driver_handle_buffer: handles of the the drivers
1060 * The allocated buffer has to be freed with free().
1062 * Return: status code
1064 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1065 const efi_guid_t *protocol,
1066 efi_uintn_t *number_of_drivers,
1067 efi_handle_t **driver_handle_buffer)
1069 struct efi_handler *handler;
1070 struct efi_open_protocol_info_item *item;
1071 efi_uintn_t count = 0, i;
1074 /* Count all driver associations */
1075 list_for_each_entry(handler, &efiobj->protocols, link) {
1076 if (protocol && guidcmp(handler->guid, protocol))
1078 list_for_each_entry(item, &handler->open_infos, link) {
1079 if (item->info.attributes &
1080 EFI_OPEN_PROTOCOL_BY_DRIVER)
1085 * Create buffer. In case of duplicate driver assignments the buffer
1086 * will be too large. But that does not harm.
1088 *number_of_drivers = 0;
1089 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1090 if (!*driver_handle_buffer)
1091 return EFI_OUT_OF_RESOURCES;
1092 /* Collect unique driver handles */
1093 list_for_each_entry(handler, &efiobj->protocols, link) {
1094 if (protocol && guidcmp(handler->guid, protocol))
1096 list_for_each_entry(item, &handler->open_infos, link) {
1097 if (item->info.attributes &
1098 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1099 /* Check this is a new driver */
1101 for (i = 0; i < *number_of_drivers; ++i) {
1102 if ((*driver_handle_buffer)[i] ==
1103 item->info.agent_handle)
1106 /* Copy handle to buffer */
1108 i = (*number_of_drivers)++;
1109 (*driver_handle_buffer)[i] =
1110 item->info.agent_handle;
1119 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1120 * @efiobj: handle of the controller
1121 * @protocol: protocol GUID (optional)
1122 * @child_handle: handle of the child to destroy
1124 * This function implements the DisconnectController service.
1126 * See the Unified Extensible Firmware Interface (UEFI) specification for
1129 * Return: status code
1131 static efi_status_t efi_disconnect_all_drivers(
1132 struct efi_object *efiobj,
1133 const efi_guid_t *protocol,
1134 efi_handle_t child_handle)
1136 efi_uintn_t number_of_drivers;
1137 efi_handle_t *driver_handle_buffer;
1138 efi_status_t r, ret;
1140 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1141 &driver_handle_buffer);
1142 if (ret != EFI_SUCCESS)
1145 ret = EFI_NOT_FOUND;
1146 while (number_of_drivers) {
1147 r = EFI_CALL(efi_disconnect_controller(
1149 driver_handle_buffer[--number_of_drivers],
1151 if (r == EFI_SUCCESS)
1154 free(driver_handle_buffer);
1159 * efi_uninstall_protocol() - uninstall protocol interface
1161 * @handle: handle from which the protocol shall be removed
1162 * @protocol: GUID of the protocol to be removed
1163 * @protocol_interface: interface to be removed
1165 * This function DOES NOT delete a handle without installed protocol.
1167 * Return: status code
1169 static efi_status_t efi_uninstall_protocol
1170 (efi_handle_t handle, const efi_guid_t *protocol,
1171 void *protocol_interface)
1173 struct efi_object *efiobj;
1174 struct efi_handler *handler;
1175 struct efi_open_protocol_info_item *item;
1176 struct efi_open_protocol_info_item *pos;
1180 efiobj = efi_search_obj(handle);
1182 r = EFI_INVALID_PARAMETER;
1185 /* Find the protocol on the handle */
1186 r = efi_search_protocol(handle, protocol, &handler);
1187 if (r != EFI_SUCCESS)
1189 /* Disconnect controllers */
1190 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1191 if (!list_empty(&handler->open_infos)) {
1192 r = EFI_ACCESS_DENIED;
1195 /* Close protocol */
1196 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1197 if (item->info.attributes ==
1198 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1199 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1200 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1201 list_del(&item->link);
1203 if (!list_empty(&handler->open_infos)) {
1204 r = EFI_ACCESS_DENIED;
1207 r = efi_remove_protocol(handle, protocol, protocol_interface);
1213 * efi_uninstall_protocol_interface() - uninstall protocol interface
1214 * @handle: handle from which the protocol shall be removed
1215 * @protocol: GUID of the protocol to be removed
1216 * @protocol_interface: interface to be removed
1218 * This function implements the UninstallProtocolInterface service.
1220 * See the Unified Extensible Firmware Interface (UEFI) specification for
1223 * Return: status code
1225 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1226 (efi_handle_t handle, const efi_guid_t *protocol,
1227 void *protocol_interface)
1231 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1233 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1234 if (ret != EFI_SUCCESS)
1237 /* If the last protocol has been removed, delete the handle. */
1238 if (list_empty(&handle->protocols)) {
1239 list_del(&handle->link);
1243 return EFI_EXIT(ret);
1247 * efi_register_protocol_notify() - register an event for notification when a
1248 * protocol is installed.
1249 * @protocol: GUID of the protocol whose installation shall be notified
1250 * @event: event to be signaled upon installation of the protocol
1251 * @registration: key for retrieving the registration information
1253 * This function implements the RegisterProtocolNotify service.
1254 * See the Unified Extensible Firmware Interface (UEFI) specification
1257 * Return: status code
1259 static efi_status_t EFIAPI efi_register_protocol_notify(
1260 const efi_guid_t *protocol,
1261 struct efi_event *event,
1262 void **registration)
1264 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1265 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1269 * efi_search() - determine if an EFI handle implements a protocol
1270 * @search_type: selection criterion
1271 * @protocol: GUID of the protocol
1272 * @search_key: registration key
1275 * See the documentation of the LocateHandle service in the UEFI specification.
1277 * Return: 0 if the handle implements the protocol
1279 static int efi_search(enum efi_locate_search_type search_type,
1280 const efi_guid_t *protocol, void *search_key,
1281 struct efi_object *efiobj)
1285 switch (search_type) {
1288 case BY_REGISTER_NOTIFY:
1289 /* TODO: RegisterProtocolNotify is not implemented yet */
1292 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1293 return (ret != EFI_SUCCESS);
1295 /* Invalid search type */
1301 * efi_locate_handle() - locate handles implementing a protocol
1302 * @search_type: selection criterion
1303 * @protocol: GUID of the protocol
1304 * @search_key: registration key
1305 * @buffer_size: size of the buffer to receive the handles in bytes
1306 * @buffer: buffer to receive the relevant handles
1308 * This function is meant for U-Boot internal calls. For the API implementation
1309 * of the LocateHandle service see efi_locate_handle_ext.
1311 * Return: status code
1313 static efi_status_t efi_locate_handle(
1314 enum efi_locate_search_type search_type,
1315 const efi_guid_t *protocol, void *search_key,
1316 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1318 struct efi_object *efiobj;
1319 efi_uintn_t size = 0;
1321 /* Check parameters */
1322 switch (search_type) {
1325 case BY_REGISTER_NOTIFY:
1327 return EFI_INVALID_PARAMETER;
1328 /* RegisterProtocolNotify is not implemented yet */
1329 return EFI_UNSUPPORTED;
1332 return EFI_INVALID_PARAMETER;
1335 return EFI_INVALID_PARAMETER;
1339 * efi_locate_handle_buffer uses this function for
1340 * the calculation of the necessary buffer size.
1341 * So do not require a buffer for buffersize == 0.
1343 if (!buffer_size || (*buffer_size && !buffer))
1344 return EFI_INVALID_PARAMETER;
1346 /* Count how much space we need */
1347 list_for_each_entry(efiobj, &efi_obj_list, link) {
1348 if (!efi_search(search_type, protocol, search_key, efiobj))
1349 size += sizeof(void *);
1352 if (*buffer_size < size) {
1353 *buffer_size = size;
1354 return EFI_BUFFER_TOO_SMALL;
1357 *buffer_size = size;
1359 return EFI_NOT_FOUND;
1361 /* Then fill the array */
1362 list_for_each_entry(efiobj, &efi_obj_list, link) {
1363 if (!efi_search(search_type, protocol, search_key, efiobj))
1364 *buffer++ = efiobj->handle;
1371 * efi_locate_handle_ext() - locate handles implementing a protocol.
1372 * @search_type: selection criterion
1373 * @protocol: GUID of the protocol
1374 * @search_key: registration key
1375 * @buffer_size: size of the buffer to receive the handles in bytes
1376 * @buffer: buffer to receive the relevant handles
1378 * This function implements the LocateHandle service.
1380 * See the Unified Extensible Firmware Interface (UEFI) specification for
1383 * Return: 0 if the handle implements the protocol
1385 static efi_status_t EFIAPI efi_locate_handle_ext(
1386 enum efi_locate_search_type search_type,
1387 const efi_guid_t *protocol, void *search_key,
1388 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1390 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1391 buffer_size, buffer);
1393 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1394 buffer_size, buffer));
1398 * efi_remove_configuration_table() - collapses configuration table entries,
1401 * @i: index of the table entry to be removed
1403 static void efi_remove_configuration_table(int i)
1405 struct efi_configuration_table *this = &systab.tables[i];
1406 struct efi_configuration_table *next = &systab.tables[i + 1];
1407 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1409 memmove(this, next, (ulong)end - (ulong)next);
1414 * efi_install_configuration_table() - adds, updates, or removes a
1415 * configuration table
1416 * @guid: GUID of the installed table
1417 * @table: table to be installed
1419 * This function is used for internal calls. For the API implementation of the
1420 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1422 * Return: status code
1424 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1427 struct efi_event *evt;
1431 return EFI_INVALID_PARAMETER;
1433 /* Check for GUID override */
1434 for (i = 0; i < systab.nr_tables; i++) {
1435 if (!guidcmp(guid, &systab.tables[i].guid)) {
1437 systab.tables[i].table = table;
1439 efi_remove_configuration_table(i);
1445 return EFI_NOT_FOUND;
1447 /* No override, check for overflow */
1448 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1449 return EFI_OUT_OF_RESOURCES;
1451 /* Add a new entry */
1452 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1453 systab.tables[i].table = table;
1454 systab.nr_tables = i + 1;
1457 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1458 efi_update_table_header_crc32(&systab.hdr);
1460 /* Notify that the configuration table was changed */
1461 list_for_each_entry(evt, &efi_events, link) {
1462 if (evt->group && !guidcmp(evt->group, guid)) {
1463 efi_signal_event(evt, false);
1472 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1473 * configuration table.
1474 * @guid: GUID of the installed table
1475 * @table: table to be installed
1477 * This function implements the InstallConfigurationTable service.
1479 * See the Unified Extensible Firmware Interface (UEFI) specification for
1482 * Return: status code
1484 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1487 EFI_ENTRY("%pUl, %p", guid, table);
1488 return EFI_EXIT(efi_install_configuration_table(guid, table));
1492 * efi_setup_loaded_image() - initialize a loaded image
1493 * @info: loaded image info to be passed to the entry point of the image
1494 * @obj: internal object associated with the loaded image
1495 * @device_path: device path of the loaded image
1496 * @file_path: file path of the loaded image
1498 * Initialize a loaded_image_info and loaded_image_info object with correct
1499 * protocols, boot-device, etc.
1501 * Return: status code
1503 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1504 struct efi_device_path *file_path,
1505 struct efi_loaded_image_obj **handle_ptr,
1506 struct efi_loaded_image **info_ptr)
1509 struct efi_loaded_image *info;
1510 struct efi_loaded_image_obj *obj;
1512 info = calloc(1, sizeof(*info));
1514 return EFI_OUT_OF_RESOURCES;
1515 obj = calloc(1, sizeof(*obj));
1518 return EFI_OUT_OF_RESOURCES;
1521 /* Add internal object to object list */
1522 efi_add_handle(&obj->parent);
1529 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1530 info->file_path = file_path;
1531 info->system_table = &systab;
1534 info->device_handle = efi_dp_find_obj(device_path, NULL);
1536 * When asking for the device path interface, return
1537 * bootefi_device_path
1539 ret = efi_add_protocol(obj->parent.handle,
1540 &efi_guid_device_path, device_path);
1541 if (ret != EFI_SUCCESS)
1546 * When asking for the loaded_image interface, just
1547 * return handle which points to loaded_image_info
1549 ret = efi_add_protocol(obj->parent.handle,
1550 &efi_guid_loaded_image, info);
1551 if (ret != EFI_SUCCESS)
1556 printf("ERROR: Failure to install protocols for loaded image\n");
1561 * efi_load_image_from_path() - load an image using a file path
1562 * @file_path: the path of the image to load
1563 * @buffer: buffer containing the loaded image
1565 * Return: status code
1567 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1570 struct efi_file_info *info = NULL;
1571 struct efi_file_handle *f;
1572 static efi_status_t ret;
1575 f = efi_file_from_path(file_path);
1577 return EFI_DEVICE_ERROR;
1580 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1582 if (ret == EFI_BUFFER_TOO_SMALL) {
1584 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1587 if (ret != EFI_SUCCESS)
1590 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1594 bs = info->file_size;
1595 EFI_CALL(ret = f->read(f, &bs, *buffer));
1599 EFI_CALL(f->close(f));
1601 if (ret != EFI_SUCCESS) {
1602 efi_free_pool(*buffer);
1610 * efi_load_image() - load an EFI image into memory
1611 * @boot_policy: true for request originating from the boot manager
1612 * @parent_image: the caller's image handle
1613 * @file_path: the path of the image to load
1614 * @source_buffer: memory location from which the image is installed
1615 * @source_size: size of the memory area from which the image is installed
1616 * @image_handle: handle for the newly installed image
1618 * This function implements the LoadImage service.
1620 * See the Unified Extensible Firmware Interface (UEFI) specification
1623 * Return: status code
1625 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1626 efi_handle_t parent_image,
1627 struct efi_device_path *file_path,
1628 void *source_buffer,
1629 efi_uintn_t source_size,
1630 efi_handle_t *image_handle)
1632 struct efi_loaded_image *info = NULL;
1633 struct efi_loaded_image_obj **image_obj =
1634 (struct efi_loaded_image_obj **)image_handle;
1637 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1638 file_path, source_buffer, source_size, image_handle);
1640 if (!image_handle || !parent_image) {
1641 ret = EFI_INVALID_PARAMETER;
1645 if (!source_buffer && !file_path) {
1646 ret = EFI_NOT_FOUND;
1650 if (!source_buffer) {
1651 struct efi_device_path *dp, *fp;
1653 ret = efi_load_image_from_path(file_path, &source_buffer);
1654 if (ret != EFI_SUCCESS)
1657 * split file_path which contains both the device and
1660 efi_dp_split_file_path(file_path, &dp, &fp);
1661 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1662 if (ret != EFI_SUCCESS)
1665 /* In this case, file_path is the "device" path, i.e.
1666 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1668 ret = efi_setup_loaded_image(file_path, NULL, image_obj, &info);
1669 if (ret != EFI_SUCCESS)
1672 (*image_obj)->entry = efi_load_pe(*image_obj, source_buffer, info);
1673 if (!(*image_obj)->entry) {
1674 ret = EFI_UNSUPPORTED;
1677 info->system_table = &systab;
1678 info->parent_handle = parent_image;
1679 return EFI_EXIT(EFI_SUCCESS);
1681 efi_delete_handle(*image_handle);
1682 *image_handle = NULL;
1685 return EFI_EXIT(ret);
1689 * efi_start_image() - call the entry point of an image
1690 * @image_handle: handle of the image
1691 * @exit_data_size: size of the buffer
1692 * @exit_data: buffer to receive the exit data of the called image
1694 * This function implements the StartImage service.
1696 * See the Unified Extensible Firmware Interface (UEFI) specification for
1699 * Return: status code
1701 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1702 unsigned long *exit_data_size,
1705 struct efi_loaded_image_obj *image_obj =
1706 (struct efi_loaded_image_obj *)image_handle;
1709 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1711 /* call the image! */
1712 if (setjmp(&image_obj->exit_jmp)) {
1714 * We called the entry point of the child image with EFI_CALL
1715 * in the lines below. The child image called the Exit() boot
1716 * service efi_exit() which executed the long jump that brought
1717 * us to the current line. This implies that the second half
1718 * of the EFI_CALL macro has not been executed.
1722 * efi_exit() called efi_restore_gd(). We have to undo this
1723 * otherwise __efi_entry_check() will put the wrong value into
1729 * To get ready to call EFI_EXIT below we have to execute the
1730 * missed out steps of EFI_CALL.
1732 assert(__efi_entry_check());
1733 debug("%sEFI: %lu returned by started image\n",
1734 __efi_nesting_dec(),
1735 (unsigned long)((uintptr_t)image_obj->exit_status &
1737 return EFI_EXIT(image_obj->exit_status);
1740 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
1743 * Usually UEFI applications call Exit() instead of returning.
1744 * But because the world doesn't consist of ponies and unicorns,
1745 * we're happy to emulate that behavior on behalf of a payload
1748 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1752 * efi_exit() - leave an EFI application or driver
1753 * @image_handle: handle of the application or driver that is exiting
1754 * @exit_status: status code
1755 * @exit_data_size: size of the buffer in bytes
1756 * @exit_data: buffer with data describing an error
1758 * This function implements the Exit service.
1760 * See the Unified Extensible Firmware Interface (UEFI) specification for
1763 * Return: status code
1765 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1766 efi_status_t exit_status,
1767 unsigned long exit_data_size,
1771 * TODO: We should call the unload procedure of the loaded
1774 struct efi_loaded_image_obj *image_obj =
1775 (struct efi_loaded_image_obj *)image_handle;
1777 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1778 exit_data_size, exit_data);
1780 /* Make sure entry/exit counts for EFI world cross-overs match */
1781 EFI_EXIT(exit_status);
1784 * But longjmp out with the U-Boot gd, not the application's, as
1785 * the other end is a setjmp call inside EFI context.
1789 image_obj->exit_status = exit_status;
1790 longjmp(&image_obj->exit_jmp, 1);
1792 panic("EFI application exited");
1796 * efi_unload_image() - unload an EFI image
1797 * @image_handle: handle of the image to be unloaded
1799 * This function implements the UnloadImage service.
1801 * See the Unified Extensible Firmware Interface (UEFI) specification for
1804 * Return: status code
1806 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1808 struct efi_object *efiobj;
1810 EFI_ENTRY("%p", image_handle);
1811 efiobj = efi_search_obj(image_handle);
1813 list_del(&efiobj->link);
1815 return EFI_EXIT(EFI_SUCCESS);
1819 * efi_exit_boot_services() - stop all boot services
1820 * @image_handle: handle of the loaded image
1821 * @map_key: key of the memory map
1823 * This function implements the ExitBootServices service.
1825 * See the Unified Extensible Firmware Interface (UEFI) specification
1828 * All timer events are disabled. For exit boot services events the
1829 * notification function is called. The boot services are disabled in the
1832 * Return: status code
1834 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1835 unsigned long map_key)
1837 struct efi_event *evt;
1839 EFI_ENTRY("%p, %ld", image_handle, map_key);
1841 /* Check that the caller has read the current memory map */
1842 if (map_key != efi_memory_map_key)
1843 return EFI_INVALID_PARAMETER;
1845 /* Make sure that notification functions are not called anymore */
1846 efi_tpl = TPL_HIGH_LEVEL;
1848 /* Check if ExitBootServices has already been called */
1849 if (!systab.boottime)
1850 return EFI_EXIT(EFI_SUCCESS);
1852 /* Add related events to the event group */
1853 list_for_each_entry(evt, &efi_events, link) {
1854 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1855 evt->group = &efi_guid_event_group_exit_boot_services;
1857 /* Notify that ExitBootServices is invoked. */
1858 list_for_each_entry(evt, &efi_events, link) {
1860 !guidcmp(evt->group,
1861 &efi_guid_event_group_exit_boot_services)) {
1862 efi_signal_event(evt, false);
1867 /* TODO: Should persist EFI variables here */
1869 board_quiesce_devices();
1871 /* This stops all lingering devices */
1872 bootm_disable_interrupts();
1874 /* Disable boot time services */
1875 systab.con_in_handle = NULL;
1876 systab.con_in = NULL;
1877 systab.con_out_handle = NULL;
1878 systab.con_out = NULL;
1879 systab.stderr_handle = NULL;
1880 systab.std_err = NULL;
1881 systab.boottime = NULL;
1883 /* Recalculate CRC32 */
1884 efi_update_table_header_crc32(&systab.hdr);
1886 /* Give the payload some time to boot */
1887 efi_set_watchdog(0);
1890 return EFI_EXIT(EFI_SUCCESS);
1894 * efi_get_next_monotonic_count() - get next value of the counter
1895 * @count: returned value of the counter
1897 * This function implements the NextMonotonicCount service.
1899 * See the Unified Extensible Firmware Interface (UEFI) specification for
1902 * Return: status code
1904 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1906 static uint64_t mono;
1908 EFI_ENTRY("%p", count);
1910 return EFI_EXIT(EFI_SUCCESS);
1914 * efi_stall() - sleep
1915 * @microseconds: period to sleep in microseconds
1917 * This function implements the Stall service.
1919 * See the Unified Extensible Firmware Interface (UEFI) specification for
1922 * Return: status code
1924 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1926 EFI_ENTRY("%ld", microseconds);
1927 udelay(microseconds);
1928 return EFI_EXIT(EFI_SUCCESS);
1932 * efi_set_watchdog_timer() - reset the watchdog timer
1933 * @timeout: seconds before reset by watchdog
1934 * @watchdog_code: code to be logged when resetting
1935 * @data_size: size of buffer in bytes
1936 * @watchdog_data: buffer with data describing the reset reason
1938 * This function implements the SetWatchdogTimer service.
1940 * See the Unified Extensible Firmware Interface (UEFI) specification for
1943 * Return: status code
1945 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1946 uint64_t watchdog_code,
1947 unsigned long data_size,
1948 uint16_t *watchdog_data)
1950 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1951 data_size, watchdog_data);
1952 return EFI_EXIT(efi_set_watchdog(timeout));
1956 * efi_close_protocol() - close a protocol
1957 * @handle: handle on which the protocol shall be closed
1958 * @protocol: GUID of the protocol to close
1959 * @agent_handle: handle of the driver
1960 * @controller_handle: handle of the controller
1962 * This function implements the CloseProtocol service.
1964 * See the Unified Extensible Firmware Interface (UEFI) specification for
1967 * Return: status code
1969 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1970 const efi_guid_t *protocol,
1971 efi_handle_t agent_handle,
1972 efi_handle_t controller_handle)
1974 struct efi_handler *handler;
1975 struct efi_open_protocol_info_item *item;
1976 struct efi_open_protocol_info_item *pos;
1979 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1982 if (!agent_handle) {
1983 r = EFI_INVALID_PARAMETER;
1986 r = efi_search_protocol(handle, protocol, &handler);
1987 if (r != EFI_SUCCESS)
1991 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1992 if (item->info.agent_handle == agent_handle &&
1993 item->info.controller_handle == controller_handle) {
1994 efi_delete_open_info(item);
2004 * efi_open_protocol_information() - provide information about then open status
2005 * of a protocol on a handle
2006 * @handle: handle for which the information shall be retrieved
2007 * @protocol: GUID of the protocol
2008 * @entry_buffer: buffer to receive the open protocol information
2009 * @entry_count: number of entries available in the buffer
2011 * This function implements the OpenProtocolInformation service.
2013 * See the Unified Extensible Firmware Interface (UEFI) specification for
2016 * Return: status code
2018 static efi_status_t EFIAPI efi_open_protocol_information(
2019 efi_handle_t handle, const efi_guid_t *protocol,
2020 struct efi_open_protocol_info_entry **entry_buffer,
2021 efi_uintn_t *entry_count)
2023 unsigned long buffer_size;
2024 unsigned long count;
2025 struct efi_handler *handler;
2026 struct efi_open_protocol_info_item *item;
2029 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2032 /* Check parameters */
2033 if (!entry_buffer) {
2034 r = EFI_INVALID_PARAMETER;
2037 r = efi_search_protocol(handle, protocol, &handler);
2038 if (r != EFI_SUCCESS)
2043 list_for_each_entry(item, &handler->open_infos, link) {
2044 if (item->info.open_count)
2047 *entry_count = count;
2048 *entry_buffer = NULL;
2055 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2056 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2057 (void **)entry_buffer);
2058 if (r != EFI_SUCCESS)
2060 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2061 if (item->info.open_count)
2062 (*entry_buffer)[--count] = item->info;
2069 * efi_protocols_per_handle() - get protocols installed on a handle
2070 * @handle: handle for which the information is retrieved
2071 * @protocol_buffer: buffer with protocol GUIDs
2072 * @protocol_buffer_count: number of entries in the buffer
2074 * This function implements the ProtocolsPerHandleService.
2076 * See the Unified Extensible Firmware Interface (UEFI) specification for
2079 * Return: status code
2081 static efi_status_t EFIAPI efi_protocols_per_handle(
2082 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2083 efi_uintn_t *protocol_buffer_count)
2085 unsigned long buffer_size;
2086 struct efi_object *efiobj;
2087 struct list_head *protocol_handle;
2090 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2091 protocol_buffer_count);
2093 if (!handle || !protocol_buffer || !protocol_buffer_count)
2094 return EFI_EXIT(EFI_INVALID_PARAMETER);
2096 *protocol_buffer = NULL;
2097 *protocol_buffer_count = 0;
2099 efiobj = efi_search_obj(handle);
2101 return EFI_EXIT(EFI_INVALID_PARAMETER);
2103 /* Count protocols */
2104 list_for_each(protocol_handle, &efiobj->protocols) {
2105 ++*protocol_buffer_count;
2109 if (*protocol_buffer_count) {
2112 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2113 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2114 (void **)protocol_buffer);
2115 if (r != EFI_SUCCESS)
2117 list_for_each(protocol_handle, &efiobj->protocols) {
2118 struct efi_handler *protocol;
2120 protocol = list_entry(protocol_handle,
2121 struct efi_handler, link);
2122 (*protocol_buffer)[j] = (void *)protocol->guid;
2127 return EFI_EXIT(EFI_SUCCESS);
2131 * efi_locate_handle_buffer() - locate handles implementing a protocol
2132 * @search_type: selection criterion
2133 * @protocol: GUID of the protocol
2134 * @search_key: registration key
2135 * @no_handles: number of returned handles
2136 * @buffer: buffer with the returned handles
2138 * This function implements the LocateHandleBuffer service.
2140 * See the Unified Extensible Firmware Interface (UEFI) specification for
2143 * Return: status code
2145 static efi_status_t EFIAPI efi_locate_handle_buffer(
2146 enum efi_locate_search_type search_type,
2147 const efi_guid_t *protocol, void *search_key,
2148 efi_uintn_t *no_handles, efi_handle_t **buffer)
2151 efi_uintn_t buffer_size = 0;
2153 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2154 no_handles, buffer);
2156 if (!no_handles || !buffer) {
2157 r = EFI_INVALID_PARAMETER;
2162 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2164 if (r != EFI_BUFFER_TOO_SMALL)
2166 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2168 if (r != EFI_SUCCESS)
2170 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2172 if (r == EFI_SUCCESS)
2173 *no_handles = buffer_size / sizeof(efi_handle_t);
2179 * efi_locate_protocol() - find an interface implementing a protocol
2180 * @protocol: GUID of the protocol
2181 * @registration: registration key passed to the notification function
2182 * @protocol_interface: interface implementing the protocol
2184 * This function implements the LocateProtocol service.
2186 * See the Unified Extensible Firmware Interface (UEFI) specification for
2189 * Return: status code
2191 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2193 void **protocol_interface)
2195 struct list_head *lhandle;
2198 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2200 if (!protocol || !protocol_interface)
2201 return EFI_EXIT(EFI_INVALID_PARAMETER);
2203 list_for_each(lhandle, &efi_obj_list) {
2204 struct efi_object *efiobj;
2205 struct efi_handler *handler;
2207 efiobj = list_entry(lhandle, struct efi_object, link);
2209 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2210 if (ret == EFI_SUCCESS) {
2211 *protocol_interface = handler->protocol_interface;
2212 return EFI_EXIT(EFI_SUCCESS);
2215 *protocol_interface = NULL;
2217 return EFI_EXIT(EFI_NOT_FOUND);
2221 * efi_locate_device_path() - Get the device path and handle of an device
2222 * implementing a protocol
2223 * @protocol: GUID of the protocol
2224 * @device_path: device path
2225 * @device: handle of the device
2227 * This function implements the LocateDevicePath service.
2229 * See the Unified Extensible Firmware Interface (UEFI) specification for
2232 * Return: status code
2234 static efi_status_t EFIAPI efi_locate_device_path(
2235 const efi_guid_t *protocol,
2236 struct efi_device_path **device_path,
2237 efi_handle_t *device)
2239 struct efi_device_path *dp;
2241 struct efi_handler *handler;
2242 efi_handle_t *handles;
2244 size_t len_best = 0;
2245 efi_uintn_t no_handles;
2249 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2251 if (!protocol || !device_path || !*device_path || !device) {
2252 ret = EFI_INVALID_PARAMETER;
2256 /* Find end of device path */
2257 len = efi_dp_instance_size(*device_path);
2259 /* Get all handles implementing the protocol */
2260 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2261 &no_handles, &handles));
2262 if (ret != EFI_SUCCESS)
2265 for (i = 0; i < no_handles; ++i) {
2266 /* Find the device path protocol */
2267 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2269 if (ret != EFI_SUCCESS)
2271 dp = (struct efi_device_path *)handler->protocol_interface;
2272 len_dp = efi_dp_instance_size(dp);
2274 * This handle can only be a better fit
2275 * if its device path length is longer than the best fit and
2276 * if its device path length is shorter of equal the searched
2279 if (len_dp <= len_best || len_dp > len)
2281 /* Check if dp is a subpath of device_path */
2282 if (memcmp(*device_path, dp, len_dp))
2284 *device = handles[i];
2288 remainder = (u8 *)*device_path + len_best;
2289 *device_path = (struct efi_device_path *)remainder;
2292 ret = EFI_NOT_FOUND;
2295 return EFI_EXIT(ret);
2299 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2301 * @handle: handle on which the protocol interfaces shall be installed
2302 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2305 * This function implements the MultipleProtocolInterfaces service.
2307 * See the Unified Extensible Firmware Interface (UEFI) specification for
2310 * Return: status code
2312 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2313 (efi_handle_t *handle, ...)
2315 EFI_ENTRY("%p", handle);
2318 const efi_guid_t *protocol;
2319 void *protocol_interface;
2320 efi_status_t r = EFI_SUCCESS;
2324 return EFI_EXIT(EFI_INVALID_PARAMETER);
2326 efi_va_start(argptr, handle);
2328 protocol = efi_va_arg(argptr, efi_guid_t*);
2331 protocol_interface = efi_va_arg(argptr, void*);
2332 r = EFI_CALL(efi_install_protocol_interface(
2334 EFI_NATIVE_INTERFACE,
2335 protocol_interface));
2336 if (r != EFI_SUCCESS)
2341 if (r == EFI_SUCCESS)
2344 /* If an error occurred undo all changes. */
2345 efi_va_start(argptr, handle);
2347 protocol = efi_va_arg(argptr, efi_guid_t*);
2348 protocol_interface = efi_va_arg(argptr, void*);
2349 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2350 protocol_interface));
2358 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2360 * @handle: handle from which the protocol interfaces shall be removed
2361 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2364 * This function implements the UninstallMultipleProtocolInterfaces service.
2366 * See the Unified Extensible Firmware Interface (UEFI) specification for
2369 * Return: status code
2371 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2372 efi_handle_t handle, ...)
2374 EFI_ENTRY("%p", handle);
2377 const efi_guid_t *protocol;
2378 void *protocol_interface;
2379 efi_status_t r = EFI_SUCCESS;
2383 return EFI_EXIT(EFI_INVALID_PARAMETER);
2385 efi_va_start(argptr, handle);
2387 protocol = efi_va_arg(argptr, efi_guid_t*);
2390 protocol_interface = efi_va_arg(argptr, void*);
2391 r = efi_uninstall_protocol(handle, protocol,
2392 protocol_interface);
2393 if (r != EFI_SUCCESS)
2398 if (r == EFI_SUCCESS) {
2399 /* If the last protocol has been removed, delete the handle. */
2400 if (list_empty(&handle->protocols)) {
2401 list_del(&handle->link);
2407 /* If an error occurred undo all changes. */
2408 efi_va_start(argptr, handle);
2410 protocol = efi_va_arg(argptr, efi_guid_t*);
2411 protocol_interface = efi_va_arg(argptr, void*);
2412 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2413 EFI_NATIVE_INTERFACE,
2414 protocol_interface));
2422 * efi_calculate_crc32() - calculate cyclic redundancy code
2423 * @data: buffer with data
2424 * @data_size: size of buffer in bytes
2425 * @crc32_p: cyclic redundancy code
2427 * This function implements the CalculateCrc32 service.
2429 * See the Unified Extensible Firmware Interface (UEFI) specification for
2432 * Return: status code
2434 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2435 efi_uintn_t data_size,
2438 EFI_ENTRY("%p, %zu", data, data_size);
2439 *crc32_p = crc32(0, data, data_size);
2440 return EFI_EXIT(EFI_SUCCESS);
2444 * efi_copy_mem() - copy memory
2445 * @destination: destination of the copy operation
2446 * @source: source of the copy operation
2447 * @length: number of bytes to copy
2449 * This function implements the CopyMem service.
2451 * See the Unified Extensible Firmware Interface (UEFI) specification for
2454 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2457 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2458 memcpy(destination, source, length);
2459 EFI_EXIT(EFI_SUCCESS);
2463 * efi_set_mem() - Fill memory with a byte value.
2464 * @buffer: buffer to fill
2465 * @size: size of buffer in bytes
2466 * @value: byte to copy to the buffer
2468 * This function implements the SetMem service.
2470 * See the Unified Extensible Firmware Interface (UEFI) specification for
2473 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2475 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2476 memset(buffer, value, size);
2477 EFI_EXIT(EFI_SUCCESS);
2481 * efi_protocol_open() - open protocol interface on a handle
2482 * @handler: handler of a protocol
2483 * @protocol_interface: interface implementing the protocol
2484 * @agent_handle: handle of the driver
2485 * @controller_handle: handle of the controller
2486 * @attributes: attributes indicating how to open the protocol
2488 * Return: status code
2490 static efi_status_t efi_protocol_open(
2491 struct efi_handler *handler,
2492 void **protocol_interface, void *agent_handle,
2493 void *controller_handle, uint32_t attributes)
2495 struct efi_open_protocol_info_item *item;
2496 struct efi_open_protocol_info_entry *match = NULL;
2497 bool opened_by_driver = false;
2498 bool opened_exclusive = false;
2500 /* If there is no agent, only return the interface */
2504 /* For TEST_PROTOCOL ignore interface attribute */
2505 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2506 *protocol_interface = NULL;
2509 * Check if the protocol is already opened by a driver with the same
2510 * attributes or opened exclusively
2512 list_for_each_entry(item, &handler->open_infos, link) {
2513 if (item->info.agent_handle == agent_handle) {
2514 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2515 (item->info.attributes == attributes))
2516 return EFI_ALREADY_STARTED;
2518 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2519 opened_exclusive = true;
2522 /* Only one controller can open the protocol exclusively */
2523 if (opened_exclusive && attributes &
2524 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2525 return EFI_ACCESS_DENIED;
2527 /* Prepare exclusive opening */
2528 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2529 /* Try to disconnect controllers */
2530 list_for_each_entry(item, &handler->open_infos, link) {
2531 if (item->info.attributes ==
2532 EFI_OPEN_PROTOCOL_BY_DRIVER)
2533 EFI_CALL(efi_disconnect_controller(
2534 item->info.controller_handle,
2535 item->info.agent_handle,
2538 opened_by_driver = false;
2539 /* Check if all controllers are disconnected */
2540 list_for_each_entry(item, &handler->open_infos, link) {
2541 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2542 opened_by_driver = true;
2544 /* Only one controller can be connected */
2545 if (opened_by_driver)
2546 return EFI_ACCESS_DENIED;
2549 /* Find existing entry */
2550 list_for_each_entry(item, &handler->open_infos, link) {
2551 if (item->info.agent_handle == agent_handle &&
2552 item->info.controller_handle == controller_handle)
2553 match = &item->info;
2555 /* None found, create one */
2557 match = efi_create_open_info(handler);
2559 return EFI_OUT_OF_RESOURCES;
2562 match->agent_handle = agent_handle;
2563 match->controller_handle = controller_handle;
2564 match->attributes = attributes;
2565 match->open_count++;
2568 /* For TEST_PROTOCOL ignore interface attribute. */
2569 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2570 *protocol_interface = handler->protocol_interface;
2576 * efi_open_protocol() - open protocol interface on a handle
2577 * @handle: handle on which the protocol shall be opened
2578 * @protocol: GUID of the protocol
2579 * @protocol_interface: interface implementing the protocol
2580 * @agent_handle: handle of the driver
2581 * @controller_handle: handle of the controller
2582 * @attributes: attributes indicating how to open the protocol
2584 * This function implements the OpenProtocol interface.
2586 * See the Unified Extensible Firmware Interface (UEFI) specification for
2589 * Return: status code
2591 static efi_status_t EFIAPI efi_open_protocol
2592 (efi_handle_t handle, const efi_guid_t *protocol,
2593 void **protocol_interface, efi_handle_t agent_handle,
2594 efi_handle_t controller_handle, uint32_t attributes)
2596 struct efi_handler *handler;
2597 efi_status_t r = EFI_INVALID_PARAMETER;
2599 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2600 protocol_interface, agent_handle, controller_handle,
2603 if (!handle || !protocol ||
2604 (!protocol_interface && attributes !=
2605 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2609 switch (attributes) {
2610 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2611 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2612 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2614 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2615 if (controller_handle == handle)
2618 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2619 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2620 /* Check that the controller handle is valid */
2621 if (!efi_search_obj(controller_handle))
2624 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2625 /* Check that the agent handle is valid */
2626 if (!efi_search_obj(agent_handle))
2633 r = efi_search_protocol(handle, protocol, &handler);
2634 if (r != EFI_SUCCESS)
2637 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2638 controller_handle, attributes);
2644 * efi_handle_protocol() - get interface of a protocol on a handle
2645 * @handle: handle on which the protocol shall be opened
2646 * @protocol: GUID of the protocol
2647 * @protocol_interface: interface implementing the protocol
2649 * This function implements the HandleProtocol service.
2651 * See the Unified Extensible Firmware Interface (UEFI) specification for
2654 * Return: status code
2656 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2657 const efi_guid_t *protocol,
2658 void **protocol_interface)
2660 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2661 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2665 * efi_bind_controller() - bind a single driver to a controller
2666 * @controller_handle: controller handle
2667 * @driver_image_handle: driver handle
2668 * @remain_device_path: remaining path
2670 * Return: status code
2672 static efi_status_t efi_bind_controller(
2673 efi_handle_t controller_handle,
2674 efi_handle_t driver_image_handle,
2675 struct efi_device_path *remain_device_path)
2677 struct efi_driver_binding_protocol *binding_protocol;
2680 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2681 &efi_guid_driver_binding_protocol,
2682 (void **)&binding_protocol,
2683 driver_image_handle, NULL,
2684 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2685 if (r != EFI_SUCCESS)
2687 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2689 remain_device_path));
2690 if (r == EFI_SUCCESS)
2691 r = EFI_CALL(binding_protocol->start(binding_protocol,
2693 remain_device_path));
2694 EFI_CALL(efi_close_protocol(driver_image_handle,
2695 &efi_guid_driver_binding_protocol,
2696 driver_image_handle, NULL));
2701 * efi_connect_single_controller() - connect a single driver to a controller
2702 * @controller_handle: controller
2703 * @driver_image_handle: driver
2704 * @remain_device_path: remaining path
2706 * Return: status code
2708 static efi_status_t efi_connect_single_controller(
2709 efi_handle_t controller_handle,
2710 efi_handle_t *driver_image_handle,
2711 struct efi_device_path *remain_device_path)
2713 efi_handle_t *buffer;
2717 size_t connected = 0;
2719 /* Get buffer with all handles with driver binding protocol */
2720 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2721 &efi_guid_driver_binding_protocol,
2722 NULL, &count, &buffer));
2723 if (r != EFI_SUCCESS)
2726 /* Context Override */
2727 if (driver_image_handle) {
2728 for (; *driver_image_handle; ++driver_image_handle) {
2729 for (i = 0; i < count; ++i) {
2730 if (buffer[i] == *driver_image_handle) {
2732 r = efi_bind_controller(
2734 *driver_image_handle,
2735 remain_device_path);
2737 * For drivers that do not support the
2738 * controller or are already connected
2739 * we receive an error code here.
2741 if (r == EFI_SUCCESS)
2749 * TODO: Some overrides are not yet implemented:
2750 * - Platform Driver Override
2751 * - Driver Family Override Search
2752 * - Bus Specific Driver Override
2755 /* Driver Binding Search */
2756 for (i = 0; i < count; ++i) {
2758 r = efi_bind_controller(controller_handle,
2760 remain_device_path);
2761 if (r == EFI_SUCCESS)
2766 efi_free_pool(buffer);
2768 return EFI_NOT_FOUND;
2773 * efi_connect_controller() - connect a controller to a driver
2774 * @controller_handle: handle of the controller
2775 * @driver_image_handle: handle of the driver
2776 * @remain_device_path: device path of a child controller
2777 * @recursive: true to connect all child controllers
2779 * This function implements the ConnectController service.
2781 * See the Unified Extensible Firmware Interface (UEFI) specification for
2784 * First all driver binding protocol handles are tried for binding drivers.
2785 * Afterwards all handles that have opened a protocol of the controller
2786 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2788 * Return: status code
2790 static efi_status_t EFIAPI efi_connect_controller(
2791 efi_handle_t controller_handle,
2792 efi_handle_t *driver_image_handle,
2793 struct efi_device_path *remain_device_path,
2797 efi_status_t ret = EFI_NOT_FOUND;
2798 struct efi_object *efiobj;
2800 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2801 remain_device_path, recursive);
2803 efiobj = efi_search_obj(controller_handle);
2805 ret = EFI_INVALID_PARAMETER;
2809 r = efi_connect_single_controller(controller_handle,
2810 driver_image_handle,
2811 remain_device_path);
2812 if (r == EFI_SUCCESS)
2815 struct efi_handler *handler;
2816 struct efi_open_protocol_info_item *item;
2818 list_for_each_entry(handler, &efiobj->protocols, link) {
2819 list_for_each_entry(item, &handler->open_infos, link) {
2820 if (item->info.attributes &
2821 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2822 r = EFI_CALL(efi_connect_controller(
2823 item->info.controller_handle,
2824 driver_image_handle,
2827 if (r == EFI_SUCCESS)
2833 /* Check for child controller specified by end node */
2834 if (ret != EFI_SUCCESS && remain_device_path &&
2835 remain_device_path->type == DEVICE_PATH_TYPE_END)
2838 return EFI_EXIT(ret);
2842 * efi_reinstall_protocol_interface() - reinstall protocol interface
2843 * @handle: handle on which the protocol shall be reinstalled
2844 * @protocol: GUID of the protocol to be installed
2845 * @old_interface: interface to be removed
2846 * @new_interface: interface to be installed
2848 * This function implements the ReinstallProtocolInterface service.
2850 * See the Unified Extensible Firmware Interface (UEFI) specification for
2853 * The old interface is uninstalled. The new interface is installed.
2854 * Drivers are connected.
2856 * Return: status code
2858 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2859 efi_handle_t handle, const efi_guid_t *protocol,
2860 void *old_interface, void *new_interface)
2864 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2867 /* Uninstall protocol but do not delete handle */
2868 ret = efi_uninstall_protocol(handle, protocol, old_interface);
2869 if (ret != EFI_SUCCESS)
2872 /* Install the new protocol */
2873 ret = efi_add_protocol(handle, protocol, new_interface);
2875 * The UEFI spec does not specify what should happen to the handle
2876 * if in case of an error no protocol interface remains on the handle.
2877 * So let's do nothing here.
2879 if (ret != EFI_SUCCESS)
2882 * The returned status code has to be ignored.
2883 * Do not create an error if no suitable driver for the handle exists.
2885 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2887 return EFI_EXIT(ret);
2891 * efi_get_child_controllers() - get all child controllers associated to a driver
2892 * @efiobj: handle of the controller
2893 * @driver_handle: handle of the driver
2894 * @number_of_children: number of child controllers
2895 * @child_handle_buffer: handles of the the child controllers
2897 * The allocated buffer has to be freed with free().
2899 * Return: status code
2901 static efi_status_t efi_get_child_controllers(
2902 struct efi_object *efiobj,
2903 efi_handle_t driver_handle,
2904 efi_uintn_t *number_of_children,
2905 efi_handle_t **child_handle_buffer)
2907 struct efi_handler *handler;
2908 struct efi_open_protocol_info_item *item;
2909 efi_uintn_t count = 0, i;
2912 /* Count all child controller associations */
2913 list_for_each_entry(handler, &efiobj->protocols, link) {
2914 list_for_each_entry(item, &handler->open_infos, link) {
2915 if (item->info.agent_handle == driver_handle &&
2916 item->info.attributes &
2917 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2922 * Create buffer. In case of duplicate child controller assignments
2923 * the buffer will be too large. But that does not harm.
2925 *number_of_children = 0;
2926 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2927 if (!*child_handle_buffer)
2928 return EFI_OUT_OF_RESOURCES;
2929 /* Copy unique child handles */
2930 list_for_each_entry(handler, &efiobj->protocols, link) {
2931 list_for_each_entry(item, &handler->open_infos, link) {
2932 if (item->info.agent_handle == driver_handle &&
2933 item->info.attributes &
2934 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2935 /* Check this is a new child controller */
2937 for (i = 0; i < *number_of_children; ++i) {
2938 if ((*child_handle_buffer)[i] ==
2939 item->info.controller_handle)
2942 /* Copy handle to buffer */
2944 i = (*number_of_children)++;
2945 (*child_handle_buffer)[i] =
2946 item->info.controller_handle;
2955 * efi_disconnect_controller() - disconnect a controller from a driver
2956 * @controller_handle: handle of the controller
2957 * @driver_image_handle: handle of the driver
2958 * @child_handle: handle of the child to destroy
2960 * This function implements the DisconnectController service.
2962 * See the Unified Extensible Firmware Interface (UEFI) specification for
2965 * Return: status code
2967 static efi_status_t EFIAPI efi_disconnect_controller(
2968 efi_handle_t controller_handle,
2969 efi_handle_t driver_image_handle,
2970 efi_handle_t child_handle)
2972 struct efi_driver_binding_protocol *binding_protocol;
2973 efi_handle_t *child_handle_buffer = NULL;
2974 size_t number_of_children = 0;
2976 size_t stop_count = 0;
2977 struct efi_object *efiobj;
2979 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2982 efiobj = efi_search_obj(controller_handle);
2984 r = EFI_INVALID_PARAMETER;
2988 if (child_handle && !efi_search_obj(child_handle)) {
2989 r = EFI_INVALID_PARAMETER;
2993 /* If no driver handle is supplied, disconnect all drivers */
2994 if (!driver_image_handle) {
2995 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2999 /* Create list of child handles */
3001 number_of_children = 1;
3002 child_handle_buffer = &child_handle;
3004 efi_get_child_controllers(efiobj,
3005 driver_image_handle,
3006 &number_of_children,
3007 &child_handle_buffer);
3010 /* Get the driver binding protocol */
3011 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3012 &efi_guid_driver_binding_protocol,
3013 (void **)&binding_protocol,
3014 driver_image_handle, NULL,
3015 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3016 if (r != EFI_SUCCESS)
3018 /* Remove the children */
3019 if (number_of_children) {
3020 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3023 child_handle_buffer));
3024 if (r == EFI_SUCCESS)
3027 /* Remove the driver */
3029 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3032 if (r == EFI_SUCCESS)
3034 EFI_CALL(efi_close_protocol(driver_image_handle,
3035 &efi_guid_driver_binding_protocol,
3036 driver_image_handle, NULL));
3044 free(child_handle_buffer);
3048 static struct efi_boot_services efi_boot_services = {
3050 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3051 .revision = EFI_SPECIFICATION_VERSION,
3052 .headersize = sizeof(struct efi_boot_services),
3054 .raise_tpl = efi_raise_tpl,
3055 .restore_tpl = efi_restore_tpl,
3056 .allocate_pages = efi_allocate_pages_ext,
3057 .free_pages = efi_free_pages_ext,
3058 .get_memory_map = efi_get_memory_map_ext,
3059 .allocate_pool = efi_allocate_pool_ext,
3060 .free_pool = efi_free_pool_ext,
3061 .create_event = efi_create_event_ext,
3062 .set_timer = efi_set_timer_ext,
3063 .wait_for_event = efi_wait_for_event,
3064 .signal_event = efi_signal_event_ext,
3065 .close_event = efi_close_event,
3066 .check_event = efi_check_event,
3067 .install_protocol_interface = efi_install_protocol_interface,
3068 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3069 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3070 .handle_protocol = efi_handle_protocol,
3072 .register_protocol_notify = efi_register_protocol_notify,
3073 .locate_handle = efi_locate_handle_ext,
3074 .locate_device_path = efi_locate_device_path,
3075 .install_configuration_table = efi_install_configuration_table_ext,
3076 .load_image = efi_load_image,
3077 .start_image = efi_start_image,
3079 .unload_image = efi_unload_image,
3080 .exit_boot_services = efi_exit_boot_services,
3081 .get_next_monotonic_count = efi_get_next_monotonic_count,
3083 .set_watchdog_timer = efi_set_watchdog_timer,
3084 .connect_controller = efi_connect_controller,
3085 .disconnect_controller = efi_disconnect_controller,
3086 .open_protocol = efi_open_protocol,
3087 .close_protocol = efi_close_protocol,
3088 .open_protocol_information = efi_open_protocol_information,
3089 .protocols_per_handle = efi_protocols_per_handle,
3090 .locate_handle_buffer = efi_locate_handle_buffer,
3091 .locate_protocol = efi_locate_protocol,
3092 .install_multiple_protocol_interfaces =
3093 efi_install_multiple_protocol_interfaces,
3094 .uninstall_multiple_protocol_interfaces =
3095 efi_uninstall_multiple_protocol_interfaces,
3096 .calculate_crc32 = efi_calculate_crc32,
3097 .copy_mem = efi_copy_mem,
3098 .set_mem = efi_set_mem,
3099 .create_event_ex = efi_create_event_ex,
3102 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3104 struct efi_system_table __efi_runtime_data systab = {
3106 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3107 .revision = EFI_SPECIFICATION_VERSION,
3108 .headersize = sizeof(struct efi_system_table),
3110 .fw_vendor = firmware_vendor,
3111 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3112 .con_in = (void *)&efi_con_in,
3113 .con_out = (void *)&efi_con_out,
3114 .std_err = (void *)&efi_con_out,
3115 .runtime = (void *)&efi_runtime_services,
3116 .boottime = (void *)&efi_boot_services,
3122 * efi_initialize_system_table() - Initialize system table
3124 * Return: status code
3126 efi_status_t efi_initialize_system_table(void)
3130 /* Allocate configuration table array */
3131 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3132 EFI_MAX_CONFIGURATION_TABLES *
3133 sizeof(struct efi_configuration_table),
3134 (void **)&systab.tables);
3136 /* Set CRC32 field in table headers */
3137 efi_update_table_header_crc32(&systab.hdr);
3138 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3139 efi_update_table_header_crc32(&efi_boot_services.hdr);