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);
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
35 static bool efi_is_direct_boot = true;
39 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40 * fixed when compiling U-Boot. However, the payload does not know about that
41 * restriction so we need to manually swap its and our view of that register on
42 * EFI callback entry/exit.
44 static volatile void *efi_gd, *app_gd;
47 static int entry_count;
48 static int nesting_level;
49 /* GUID of the device tree table */
50 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
51 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
52 const efi_guid_t efi_guid_driver_binding_protocol =
53 EFI_DRIVER_BINDING_PROTOCOL_GUID;
55 /* event group ExitBootServices() invoked */
56 const efi_guid_t efi_guid_event_group_exit_boot_services =
57 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
58 /* event group SetVirtualAddressMap() invoked */
59 const efi_guid_t efi_guid_event_group_virtual_address_change =
60 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
61 /* event group memory map changed */
62 const efi_guid_t efi_guid_event_group_memory_map_change =
63 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
64 /* event group boot manager about to boot */
65 const efi_guid_t efi_guid_event_group_ready_to_boot =
66 EFI_EVENT_GROUP_READY_TO_BOOT;
67 /* event group ResetSystem() invoked (before ExitBootServices) */
68 const efi_guid_t efi_guid_event_group_reset_system =
69 EFI_EVENT_GROUP_RESET_SYSTEM;
71 static efi_status_t EFIAPI efi_disconnect_controller(
72 efi_handle_t controller_handle,
73 efi_handle_t driver_image_handle,
74 efi_handle_t child_handle);
76 /* Called on every callback entry */
77 int __efi_entry_check(void)
79 int ret = entry_count++ == 0;
88 /* Called on every callback exit */
89 int __efi_exit_check(void)
91 int ret = --entry_count == 0;
98 /* Called from do_bootefi_exec() */
99 void efi_save_gd(void)
107 * Special case handler for error/abort that just forces things back to u-boot
108 * world so we can dump out an abort message, without any care about returning
109 * back to UEFI world.
111 void efi_restore_gd(void)
114 /* Only restore if we're already in EFI context */
122 * indent_string() - returns a string for indenting with two spaces per level
123 * @level: indent level
125 * A maximum of ten indent levels is supported. Higher indent levels will be
128 * Return: A string for indenting with two spaces per level is
131 static const char *indent_string(int level)
133 const char *indent = " ";
134 const int max = strlen(indent);
136 level = min(max, level * 2);
137 return &indent[max - level];
140 const char *__efi_nesting(void)
142 return indent_string(nesting_level);
145 const char *__efi_nesting_inc(void)
147 return indent_string(nesting_level++);
150 const char *__efi_nesting_dec(void)
152 return indent_string(--nesting_level);
156 * efi_queue_event() - queue an EFI event
157 * @event: event to signal
158 * @check_tpl: check the TPL level
160 * This function queues the notification function of the event for future
163 * The notification function is called if the task priority level of the event
164 * is higher than the current task priority level.
166 * For the SignalEvent service see efi_signal_event_ext.
169 static void efi_queue_event(struct efi_event *event, bool check_tpl)
171 if (event->notify_function) {
172 event->is_queued = true;
174 if (check_tpl && efi_tpl >= event->notify_tpl)
176 EFI_CALL_VOID(event->notify_function(event,
177 event->notify_context));
179 event->is_queued = false;
183 * is_valid_tpl() - check if the task priority level is valid
185 * @tpl: TPL level to check
186 * Return: status code
188 efi_status_t is_valid_tpl(efi_uintn_t tpl)
191 case TPL_APPLICATION:
197 return EFI_INVALID_PARAMETER;
202 * efi_signal_event() - signal an EFI event
203 * @event: event to signal
204 * @check_tpl: check the TPL level
206 * This function signals an event. If the event belongs to an event group all
207 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
208 * their notification function is queued.
210 * For the SignalEvent service see efi_signal_event_ext.
212 void efi_signal_event(struct efi_event *event, bool check_tpl)
215 struct efi_event *evt;
218 * The signaled state has to set before executing any
219 * notification function
221 list_for_each_entry(evt, &efi_events, link) {
222 if (!evt->group || guidcmp(evt->group, event->group))
224 if (evt->is_signaled)
226 evt->is_signaled = true;
227 if (evt->type & EVT_NOTIFY_SIGNAL &&
228 evt->notify_function)
229 evt->is_queued = true;
231 list_for_each_entry(evt, &efi_events, link) {
232 if (!evt->group || guidcmp(evt->group, event->group))
235 efi_queue_event(evt, check_tpl);
237 } else if (!event->is_signaled) {
238 event->is_signaled = true;
239 if (event->type & EVT_NOTIFY_SIGNAL)
240 efi_queue_event(event, check_tpl);
245 * efi_raise_tpl() - raise the task priority level
246 * @new_tpl: new value of the task priority level
248 * This function implements the RaiseTpl service.
250 * See the Unified Extensible Firmware Interface (UEFI) specification for
253 * Return: old value of the task priority level
255 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
257 efi_uintn_t old_tpl = efi_tpl;
259 EFI_ENTRY("0x%zx", new_tpl);
261 if (new_tpl < efi_tpl)
262 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
264 if (efi_tpl > TPL_HIGH_LEVEL)
265 efi_tpl = TPL_HIGH_LEVEL;
267 EFI_EXIT(EFI_SUCCESS);
272 * efi_restore_tpl() - lower the task priority level
273 * @old_tpl: value of the task priority level to be restored
275 * This function implements the RestoreTpl service.
277 * See the Unified Extensible Firmware Interface (UEFI) specification for
280 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
282 EFI_ENTRY("0x%zx", old_tpl);
284 if (old_tpl > efi_tpl)
285 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
287 if (efi_tpl > TPL_HIGH_LEVEL)
288 efi_tpl = TPL_HIGH_LEVEL;
291 * Lowering the TPL may have made queued events eligible for execution.
295 EFI_EXIT(EFI_SUCCESS);
299 * efi_allocate_pages_ext() - allocate memory pages
300 * @type: type of allocation to be performed
301 * @memory_type: usage type of the allocated memory
302 * @pages: number of pages to be allocated
303 * @memory: allocated memory
305 * This function implements the AllocatePages service.
307 * See the Unified Extensible Firmware Interface (UEFI) specification for
310 * Return: status code
312 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
318 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
319 r = efi_allocate_pages(type, memory_type, pages, memory);
324 * efi_free_pages_ext() - Free memory pages.
325 * @memory: start of the memory area to be freed
326 * @pages: number of pages to be freed
328 * This function implements the FreePages service.
330 * See the Unified Extensible Firmware Interface (UEFI) specification for
333 * Return: status code
335 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
340 EFI_ENTRY("%llx, 0x%zx", memory, pages);
341 r = efi_free_pages(memory, pages);
346 * efi_get_memory_map_ext() - get map describing memory usage
347 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
348 * on exit the size of the copied memory map
349 * @memory_map: buffer to which the memory map is written
350 * @map_key: key for the memory map
351 * @descriptor_size: size of an individual memory descriptor
352 * @descriptor_version: version number of the memory descriptor structure
354 * This function implements the GetMemoryMap service.
356 * See the Unified Extensible Firmware Interface (UEFI) specification for
359 * Return: status code
361 static efi_status_t EFIAPI efi_get_memory_map_ext(
362 efi_uintn_t *memory_map_size,
363 struct efi_mem_desc *memory_map,
364 efi_uintn_t *map_key,
365 efi_uintn_t *descriptor_size,
366 uint32_t *descriptor_version)
370 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
371 map_key, descriptor_size, descriptor_version);
372 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
373 descriptor_size, descriptor_version);
378 * efi_allocate_pool_ext() - allocate memory from pool
379 * @pool_type: type of the pool from which memory is to be allocated
380 * @size: number of bytes to be allocated
381 * @buffer: allocated memory
383 * This function implements the AllocatePool service.
385 * See the Unified Extensible Firmware Interface (UEFI) specification for
388 * Return: status code
390 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
396 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
397 r = efi_allocate_pool(pool_type, size, buffer);
402 * efi_free_pool_ext() - free memory from pool
403 * @buffer: start of memory to be freed
405 * This function implements the FreePool service.
407 * See the Unified Extensible Firmware Interface (UEFI) specification for
410 * Return: status code
412 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
416 EFI_ENTRY("%p", buffer);
417 r = efi_free_pool(buffer);
422 * efi_add_handle() - add a new object to the object list
423 * @obj: object to be added
425 * The protocols list is initialized. The object handle is set.
427 void efi_add_handle(struct efi_object *obj)
431 INIT_LIST_HEAD(&obj->protocols);
433 list_add_tail(&obj->link, &efi_obj_list);
437 * efi_create_handle() - create handle
438 * @handle: new handle
440 * Return: status code
442 efi_status_t efi_create_handle(efi_handle_t *handle)
444 struct efi_object *obj;
446 obj = calloc(1, sizeof(struct efi_object));
448 return EFI_OUT_OF_RESOURCES;
451 *handle = obj->handle;
457 * efi_search_protocol() - find a protocol on a handle.
459 * @protocol_guid: GUID of the protocol
460 * @handler: reference to the protocol
462 * Return: status code
464 efi_status_t efi_search_protocol(const efi_handle_t handle,
465 const efi_guid_t *protocol_guid,
466 struct efi_handler **handler)
468 struct efi_object *efiobj;
469 struct list_head *lhandle;
471 if (!handle || !protocol_guid)
472 return EFI_INVALID_PARAMETER;
473 efiobj = efi_search_obj(handle);
475 return EFI_INVALID_PARAMETER;
476 list_for_each(lhandle, &efiobj->protocols) {
477 struct efi_handler *protocol;
479 protocol = list_entry(lhandle, struct efi_handler, link);
480 if (!guidcmp(protocol->guid, protocol_guid)) {
486 return EFI_NOT_FOUND;
490 * efi_remove_protocol() - delete protocol from a handle
491 * @handle: handle from which the protocol shall be deleted
492 * @protocol: GUID of the protocol to be deleted
493 * @protocol_interface: interface of the protocol implementation
495 * Return: status code
497 efi_status_t efi_remove_protocol(const efi_handle_t handle,
498 const efi_guid_t *protocol,
499 void *protocol_interface)
501 struct efi_handler *handler;
504 ret = efi_search_protocol(handle, protocol, &handler);
505 if (ret != EFI_SUCCESS)
507 if (guidcmp(handler->guid, protocol))
508 return EFI_INVALID_PARAMETER;
509 if (handler->protocol_interface != protocol_interface)
510 return EFI_INVALID_PARAMETER;
511 list_del(&handler->link);
517 * efi_remove_all_protocols() - delete all protocols from a handle
518 * @handle: handle from which the protocols shall be deleted
520 * Return: status code
522 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
524 struct efi_object *efiobj;
525 struct efi_handler *protocol;
526 struct efi_handler *pos;
528 efiobj = efi_search_obj(handle);
530 return EFI_INVALID_PARAMETER;
531 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
534 ret = efi_remove_protocol(handle, protocol->guid,
535 protocol->protocol_interface);
536 if (ret != EFI_SUCCESS)
543 * efi_delete_handle() - delete handle
545 * @obj: handle to delete
547 void efi_delete_handle(struct efi_object *obj)
551 efi_remove_all_protocols(obj->handle);
552 list_del(&obj->link);
557 * efi_is_event() - check if a pointer is a valid event
558 * @event: pointer to check
560 * Return: status code
562 static efi_status_t efi_is_event(const struct efi_event *event)
564 const struct efi_event *evt;
567 return EFI_INVALID_PARAMETER;
568 list_for_each_entry(evt, &efi_events, link) {
572 return EFI_INVALID_PARAMETER;
576 * efi_create_event() - create an event
577 * @type: type of the event to create
578 * @notify_tpl: task priority level of the event
579 * @notify_function: notification function of the event
580 * @notify_context: pointer passed to the notification function
581 * @group: event group
582 * @event: created event
584 * This function is used inside U-Boot code to create an event.
586 * For the API function implementing the CreateEvent service see
587 * efi_create_event_ext.
589 * Return: status code
591 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
592 void (EFIAPI *notify_function) (
593 struct efi_event *event,
595 void *notify_context, efi_guid_t *group,
596 struct efi_event **event)
598 struct efi_event *evt;
601 return EFI_INVALID_PARAMETER;
606 case EVT_NOTIFY_SIGNAL:
607 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
608 case EVT_NOTIFY_WAIT:
609 case EVT_TIMER | EVT_NOTIFY_WAIT:
610 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
611 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
614 return EFI_INVALID_PARAMETER;
617 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
618 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
619 return EFI_INVALID_PARAMETER;
621 evt = calloc(1, sizeof(struct efi_event));
623 return EFI_OUT_OF_RESOURCES;
625 evt->notify_tpl = notify_tpl;
626 evt->notify_function = notify_function;
627 evt->notify_context = notify_context;
629 /* Disable timers on boot up */
630 evt->trigger_next = -1ULL;
631 evt->is_queued = false;
632 evt->is_signaled = false;
633 list_add_tail(&evt->link, &efi_events);
639 * efi_create_event_ex() - create an event in a group
640 * @type: type of the event to create
641 * @notify_tpl: task priority level of the event
642 * @notify_function: notification function of the event
643 * @notify_context: pointer passed to the notification function
644 * @event: created event
645 * @event_group: event group
647 * This function implements the CreateEventEx service.
649 * See the Unified Extensible Firmware Interface (UEFI) specification for
652 * Return: status code
654 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
655 void (EFIAPI *notify_function) (
656 struct efi_event *event,
658 void *notify_context,
659 efi_guid_t *event_group,
660 struct efi_event **event)
662 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
663 notify_context, event_group);
664 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
665 notify_context, event_group, event));
669 * efi_create_event_ext() - create an event
670 * @type: type of the event to create
671 * @notify_tpl: task priority level of the event
672 * @notify_function: notification function of the event
673 * @notify_context: pointer passed to the notification function
674 * @event: created event
676 * This function implements the CreateEvent service.
678 * See the Unified Extensible Firmware Interface (UEFI) specification for
681 * Return: status code
683 static efi_status_t EFIAPI efi_create_event_ext(
684 uint32_t type, efi_uintn_t notify_tpl,
685 void (EFIAPI *notify_function) (
686 struct efi_event *event,
688 void *notify_context, struct efi_event **event)
690 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
692 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
693 notify_context, NULL, event));
697 * efi_timer_check() - check if a timer event has occurred
699 * Check if a timer event has occurred or a queued notification function should
702 * Our timers have to work without interrupts, so we check whenever keyboard
703 * input or disk accesses happen if enough time elapsed for them to fire.
705 void efi_timer_check(void)
707 struct efi_event *evt;
708 u64 now = timer_get_us();
710 list_for_each_entry(evt, &efi_events, link) {
712 efi_queue_event(evt, true);
713 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
715 switch (evt->trigger_type) {
716 case EFI_TIMER_RELATIVE:
717 evt->trigger_type = EFI_TIMER_STOP;
719 case EFI_TIMER_PERIODIC:
720 evt->trigger_next += evt->trigger_time;
725 evt->is_signaled = false;
726 efi_signal_event(evt, true);
732 * efi_set_timer() - set the trigger time for a timer event or stop the event
733 * @event: event for which the timer is set
734 * @type: type of the timer
735 * @trigger_time: trigger period in multiples of 100 ns
737 * This is the function for internal usage in U-Boot. For the API function
738 * implementing the SetTimer service see efi_set_timer_ext.
740 * Return: status code
742 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
743 uint64_t trigger_time)
745 /* Check that the event is valid */
746 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
747 return EFI_INVALID_PARAMETER;
750 * The parameter defines a multiple of 100 ns.
751 * We use multiples of 1000 ns. So divide by 10.
753 do_div(trigger_time, 10);
757 event->trigger_next = -1ULL;
759 case EFI_TIMER_PERIODIC:
760 case EFI_TIMER_RELATIVE:
761 event->trigger_next = timer_get_us() + trigger_time;
764 return EFI_INVALID_PARAMETER;
766 event->trigger_type = type;
767 event->trigger_time = trigger_time;
768 event->is_signaled = false;
773 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
775 * @event: event for which the timer is set
776 * @type: type of the timer
777 * @trigger_time: trigger period in multiples of 100 ns
779 * This function implements the SetTimer service.
781 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * Return: status code
787 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
788 enum efi_timer_delay type,
789 uint64_t trigger_time)
791 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
792 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
796 * efi_wait_for_event() - wait for events to be signaled
797 * @num_events: number of events to be waited for
798 * @event: events to be waited for
799 * @index: index of the event that was signaled
801 * This function implements the WaitForEvent service.
803 * See the Unified Extensible Firmware Interface (UEFI) specification for
806 * Return: status code
808 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
809 struct efi_event **event,
814 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
816 /* Check parameters */
817 if (!num_events || !event)
818 return EFI_EXIT(EFI_INVALID_PARAMETER);
820 if (efi_tpl != TPL_APPLICATION)
821 return EFI_EXIT(EFI_UNSUPPORTED);
822 for (i = 0; i < num_events; ++i) {
823 if (efi_is_event(event[i]) != EFI_SUCCESS)
824 return EFI_EXIT(EFI_INVALID_PARAMETER);
825 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
826 return EFI_EXIT(EFI_INVALID_PARAMETER);
827 if (!event[i]->is_signaled)
828 efi_queue_event(event[i], true);
831 /* Wait for signal */
833 for (i = 0; i < num_events; ++i) {
834 if (event[i]->is_signaled)
837 /* Allow events to occur. */
843 * Reset the signal which is passed to the caller to allow periodic
846 event[i]->is_signaled = false;
850 return EFI_EXIT(EFI_SUCCESS);
854 * efi_signal_event_ext() - signal an EFI event
855 * @event: event to signal
857 * This function implements the SignalEvent service.
859 * See the Unified Extensible Firmware Interface (UEFI) specification for
862 * This functions sets the signaled state of the event and queues the
863 * notification function for execution.
865 * Return: status code
867 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
869 EFI_ENTRY("%p", event);
870 if (efi_is_event(event) != EFI_SUCCESS)
871 return EFI_EXIT(EFI_INVALID_PARAMETER);
872 efi_signal_event(event, true);
873 return EFI_EXIT(EFI_SUCCESS);
877 * efi_close_event() - close an EFI event
878 * @event: event to close
880 * This function implements the CloseEvent service.
882 * See the Unified Extensible Firmware Interface (UEFI) specification for
885 * Return: status code
887 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
889 EFI_ENTRY("%p", event);
890 if (efi_is_event(event) != EFI_SUCCESS)
891 return EFI_EXIT(EFI_INVALID_PARAMETER);
892 list_del(&event->link);
894 return EFI_EXIT(EFI_SUCCESS);
898 * efi_check_event() - check if an event is signaled
899 * @event: event to check
901 * This function implements the CheckEvent service.
903 * See the Unified Extensible Firmware Interface (UEFI) specification for
906 * If an event is not signaled yet, the notification function is queued. The
907 * signaled state is cleared.
909 * Return: status code
911 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
913 EFI_ENTRY("%p", event);
915 if (efi_is_event(event) != EFI_SUCCESS ||
916 event->type & EVT_NOTIFY_SIGNAL)
917 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 if (!event->is_signaled)
919 efi_queue_event(event, true);
920 if (event->is_signaled) {
921 event->is_signaled = false;
922 return EFI_EXIT(EFI_SUCCESS);
924 return EFI_EXIT(EFI_NOT_READY);
928 * efi_search_obj() - find the internal EFI object for a handle
929 * @handle: handle to find
933 struct efi_object *efi_search_obj(const efi_handle_t handle)
935 struct efi_object *efiobj;
937 list_for_each_entry(efiobj, &efi_obj_list, link) {
938 if (efiobj->handle == handle)
946 * efi_open_protocol_info_entry() - create open protocol info entry and add it
948 * @handler: handler of a protocol
950 * Return: open protocol info entry
952 static struct efi_open_protocol_info_entry *efi_create_open_info(
953 struct efi_handler *handler)
955 struct efi_open_protocol_info_item *item;
957 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
960 /* Append the item to the open protocol info list. */
961 list_add_tail(&item->link, &handler->open_infos);
967 * efi_delete_open_info() - remove an open protocol info entry from a protocol
968 * @item: open protocol info entry to delete
970 * Return: status code
972 static efi_status_t efi_delete_open_info(
973 struct efi_open_protocol_info_item *item)
975 list_del(&item->link);
981 * efi_add_protocol() - install new protocol on a handle
982 * @handle: handle on which the protocol shall be installed
983 * @protocol: GUID of the protocol to be installed
984 * @protocol_interface: interface of the protocol implementation
986 * Return: status code
988 efi_status_t efi_add_protocol(const efi_handle_t handle,
989 const efi_guid_t *protocol,
990 void *protocol_interface)
992 struct efi_object *efiobj;
993 struct efi_handler *handler;
996 efiobj = efi_search_obj(handle);
998 return EFI_INVALID_PARAMETER;
999 ret = efi_search_protocol(handle, protocol, NULL);
1000 if (ret != EFI_NOT_FOUND)
1001 return EFI_INVALID_PARAMETER;
1002 handler = calloc(1, sizeof(struct efi_handler));
1004 return EFI_OUT_OF_RESOURCES;
1005 handler->guid = protocol;
1006 handler->protocol_interface = protocol_interface;
1007 INIT_LIST_HEAD(&handler->open_infos);
1008 list_add_tail(&handler->link, &efiobj->protocols);
1009 if (!guidcmp(&efi_guid_device_path, protocol))
1010 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1015 * efi_install_protocol_interface() - install protocol interface
1016 * @handle: handle on which the protocol shall be installed
1017 * @protocol: GUID of the protocol to be installed
1018 * @protocol_interface_type: type of the interface to be installed,
1019 * always EFI_NATIVE_INTERFACE
1020 * @protocol_interface: interface of the protocol implementation
1022 * This function implements the InstallProtocolInterface service.
1024 * See the Unified Extensible Firmware Interface (UEFI) specification for
1027 * Return: status code
1029 static efi_status_t EFIAPI efi_install_protocol_interface(
1030 void **handle, const efi_guid_t *protocol,
1031 int protocol_interface_type, void *protocol_interface)
1035 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1036 protocol_interface);
1038 if (!handle || !protocol ||
1039 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1040 r = EFI_INVALID_PARAMETER;
1044 /* Create new handle if requested. */
1046 r = efi_create_handle(handle);
1047 if (r != EFI_SUCCESS)
1049 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1052 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1055 /* Add new protocol */
1056 r = efi_add_protocol(*handle, protocol, protocol_interface);
1062 * efi_get_drivers() - get all drivers associated to a controller
1063 * @efiobj: handle of the controller
1064 * @protocol: protocol GUID (optional)
1065 * @number_of_drivers: number of child controllers
1066 * @driver_handle_buffer: handles of the the drivers
1068 * The allocated buffer has to be freed with free().
1070 * Return: status code
1072 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1073 const efi_guid_t *protocol,
1074 efi_uintn_t *number_of_drivers,
1075 efi_handle_t **driver_handle_buffer)
1077 struct efi_handler *handler;
1078 struct efi_open_protocol_info_item *item;
1079 efi_uintn_t count = 0, i;
1082 /* Count all driver associations */
1083 list_for_each_entry(handler, &efiobj->protocols, link) {
1084 if (protocol && guidcmp(handler->guid, protocol))
1086 list_for_each_entry(item, &handler->open_infos, link) {
1087 if (item->info.attributes &
1088 EFI_OPEN_PROTOCOL_BY_DRIVER)
1093 * Create buffer. In case of duplicate driver assignments the buffer
1094 * will be too large. But that does not harm.
1096 *number_of_drivers = 0;
1097 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1098 if (!*driver_handle_buffer)
1099 return EFI_OUT_OF_RESOURCES;
1100 /* Collect unique driver handles */
1101 list_for_each_entry(handler, &efiobj->protocols, link) {
1102 if (protocol && guidcmp(handler->guid, protocol))
1104 list_for_each_entry(item, &handler->open_infos, link) {
1105 if (item->info.attributes &
1106 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1107 /* Check this is a new driver */
1109 for (i = 0; i < *number_of_drivers; ++i) {
1110 if ((*driver_handle_buffer)[i] ==
1111 item->info.agent_handle)
1114 /* Copy handle to buffer */
1116 i = (*number_of_drivers)++;
1117 (*driver_handle_buffer)[i] =
1118 item->info.agent_handle;
1127 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1128 * @efiobj: handle of the controller
1129 * @protocol: protocol GUID (optional)
1130 * @child_handle: handle of the child to destroy
1132 * This function implements the DisconnectController service.
1134 * See the Unified Extensible Firmware Interface (UEFI) specification for
1137 * Return: status code
1139 static efi_status_t efi_disconnect_all_drivers(
1140 struct efi_object *efiobj,
1141 const efi_guid_t *protocol,
1142 efi_handle_t child_handle)
1144 efi_uintn_t number_of_drivers;
1145 efi_handle_t *driver_handle_buffer;
1146 efi_status_t r, ret;
1148 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1149 &driver_handle_buffer);
1150 if (ret != EFI_SUCCESS)
1153 ret = EFI_NOT_FOUND;
1154 while (number_of_drivers) {
1155 r = EFI_CALL(efi_disconnect_controller(
1157 driver_handle_buffer[--number_of_drivers],
1159 if (r == EFI_SUCCESS)
1162 free(driver_handle_buffer);
1167 * efi_uninstall_protocol_interface() - uninstall protocol interface
1168 * @handle: handle from which the protocol shall be removed
1169 * @protocol: GUID of the protocol to be removed
1170 * @protocol_interface: interface to be removed
1172 * This function implements the UninstallProtocolInterface service.
1174 * See the Unified Extensible Firmware Interface (UEFI) specification for
1177 * Return: status code
1179 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1180 efi_handle_t handle, const efi_guid_t *protocol,
1181 void *protocol_interface)
1183 struct efi_object *efiobj;
1184 struct efi_handler *handler;
1185 struct efi_open_protocol_info_item *item;
1186 struct efi_open_protocol_info_item *pos;
1189 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1192 efiobj = efi_search_obj(handle);
1194 r = EFI_INVALID_PARAMETER;
1197 /* Find the protocol on the handle */
1198 r = efi_search_protocol(handle, protocol, &handler);
1199 if (r != EFI_SUCCESS)
1201 /* Disconnect controllers */
1202 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1203 if (!list_empty(&handler->open_infos)) {
1204 r = EFI_ACCESS_DENIED;
1207 /* Close protocol */
1208 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1209 if (item->info.attributes ==
1210 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1211 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1212 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1213 list_del(&item->link);
1215 if (!list_empty(&handler->open_infos)) {
1216 r = EFI_ACCESS_DENIED;
1219 r = efi_remove_protocol(handle, protocol, protocol_interface);
1225 * efi_register_protocol_notify() - register an event for notification when a
1226 * protocol is installed.
1227 * @protocol: GUID of the protocol whose installation shall be notified
1228 * @event: event to be signaled upon installation of the protocol
1229 * @registration: key for retrieving the registration information
1231 * This function implements the RegisterProtocolNotify service.
1232 * See the Unified Extensible Firmware Interface (UEFI) specification
1235 * Return: status code
1237 static efi_status_t EFIAPI efi_register_protocol_notify(
1238 const efi_guid_t *protocol,
1239 struct efi_event *event,
1240 void **registration)
1242 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1243 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1247 * efi_search() - determine if an EFI handle implements a protocol
1248 * @search_type: selection criterion
1249 * @protocol: GUID of the protocol
1250 * @search_key: registration key
1253 * See the documentation of the LocateHandle service in the UEFI specification.
1255 * Return: 0 if the handle implements the protocol
1257 static int efi_search(enum efi_locate_search_type search_type,
1258 const efi_guid_t *protocol, void *search_key,
1259 struct efi_object *efiobj)
1263 switch (search_type) {
1266 case BY_REGISTER_NOTIFY:
1267 /* TODO: RegisterProtocolNotify is not implemented yet */
1270 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1271 return (ret != EFI_SUCCESS);
1273 /* Invalid search type */
1279 * efi_locate_handle() - locate handles implementing a protocol
1280 * @search_type: selection criterion
1281 * @protocol: GUID of the protocol
1282 * @search_key: registration key
1283 * @buffer_size: size of the buffer to receive the handles in bytes
1284 * @buffer: buffer to receive the relevant handles
1286 * This function is meant for U-Boot internal calls. For the API implementation
1287 * of the LocateHandle service see efi_locate_handle_ext.
1289 * Return: status code
1291 static efi_status_t efi_locate_handle(
1292 enum efi_locate_search_type search_type,
1293 const efi_guid_t *protocol, void *search_key,
1294 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1296 struct efi_object *efiobj;
1297 efi_uintn_t size = 0;
1299 /* Check parameters */
1300 switch (search_type) {
1303 case BY_REGISTER_NOTIFY:
1305 return EFI_INVALID_PARAMETER;
1306 /* RegisterProtocolNotify is not implemented yet */
1307 return EFI_UNSUPPORTED;
1310 return EFI_INVALID_PARAMETER;
1313 return EFI_INVALID_PARAMETER;
1317 * efi_locate_handle_buffer uses this function for
1318 * the calculation of the necessary buffer size.
1319 * So do not require a buffer for buffersize == 0.
1321 if (!buffer_size || (*buffer_size && !buffer))
1322 return EFI_INVALID_PARAMETER;
1324 /* Count how much space we need */
1325 list_for_each_entry(efiobj, &efi_obj_list, link) {
1326 if (!efi_search(search_type, protocol, search_key, efiobj))
1327 size += sizeof(void *);
1330 if (*buffer_size < size) {
1331 *buffer_size = size;
1332 return EFI_BUFFER_TOO_SMALL;
1335 *buffer_size = size;
1337 return EFI_NOT_FOUND;
1339 /* Then fill the array */
1340 list_for_each_entry(efiobj, &efi_obj_list, link) {
1341 if (!efi_search(search_type, protocol, search_key, efiobj))
1342 *buffer++ = efiobj->handle;
1349 * efi_locate_handle_ext() - locate handles implementing a protocol.
1350 * @search_type: selection criterion
1351 * @protocol: GUID of the protocol
1352 * @search_key: registration key
1353 * @buffer_size: size of the buffer to receive the handles in bytes
1354 * @buffer: buffer to receive the relevant handles
1356 * This function implements the LocateHandle service.
1358 * See the Unified Extensible Firmware Interface (UEFI) specification for
1361 * Return: 0 if the handle implements the protocol
1363 static efi_status_t EFIAPI efi_locate_handle_ext(
1364 enum efi_locate_search_type search_type,
1365 const efi_guid_t *protocol, void *search_key,
1366 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1368 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1369 buffer_size, buffer);
1371 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1372 buffer_size, buffer));
1376 * efi_remove_configuration_table() - collapses configuration table entries,
1379 * @i: index of the table entry to be removed
1381 static void efi_remove_configuration_table(int i)
1383 struct efi_configuration_table *this = &systab.tables[i];
1384 struct efi_configuration_table *next = &systab.tables[i + 1];
1385 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1387 memmove(this, next, (ulong)end - (ulong)next);
1392 * efi_install_configuration_table() - adds, updates, or removes a
1393 * configuration table
1394 * @guid: GUID of the installed table
1395 * @table: table to be installed
1397 * This function is used for internal calls. For the API implementation of the
1398 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1400 * Return: status code
1402 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1405 struct efi_event *evt;
1409 return EFI_INVALID_PARAMETER;
1411 /* Check for GUID override */
1412 for (i = 0; i < systab.nr_tables; i++) {
1413 if (!guidcmp(guid, &systab.tables[i].guid)) {
1415 systab.tables[i].table = table;
1417 efi_remove_configuration_table(i);
1423 return EFI_NOT_FOUND;
1425 /* No override, check for overflow */
1426 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1427 return EFI_OUT_OF_RESOURCES;
1429 /* Add a new entry */
1430 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1431 systab.tables[i].table = table;
1432 systab.nr_tables = i + 1;
1435 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1436 efi_update_table_header_crc32(&systab.hdr);
1438 /* Notify that the configuration table was changed */
1439 list_for_each_entry(evt, &efi_events, link) {
1440 if (evt->group && !guidcmp(evt->group, guid)) {
1441 efi_signal_event(evt, false);
1450 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1451 * configuration table.
1452 * @guid: GUID of the installed table
1453 * @table: table to be installed
1455 * This function implements the InstallConfigurationTable service.
1457 * See the Unified Extensible Firmware Interface (UEFI) specification for
1460 * Return: status code
1462 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1465 EFI_ENTRY("%pUl, %p", guid, table);
1466 return EFI_EXIT(efi_install_configuration_table(guid, table));
1470 * efi_setup_loaded_image() - initialize a loaded image
1471 * @info: loaded image info to be passed to the entry point of the image
1472 * @obj: internal object associated with the loaded image
1473 * @device_path: device path of the loaded image
1474 * @file_path: file path of the loaded image
1476 * Initialize a loaded_image_info and loaded_image_info object with correct
1477 * protocols, boot-device, etc.
1479 * Return: status code
1481 efi_status_t efi_setup_loaded_image(
1482 struct efi_loaded_image *info, struct efi_object *obj,
1483 struct efi_device_path *device_path,
1484 struct efi_device_path *file_path)
1488 /* Add internal object to object list */
1489 efi_add_handle(obj);
1490 /* efi_exit() assumes that the handle points to the info */
1493 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1494 info->file_path = file_path;
1495 info->system_table = &systab;
1498 info->device_handle = efi_dp_find_obj(device_path, NULL);
1500 * When asking for the device path interface, return
1501 * bootefi_device_path
1503 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1505 if (ret != EFI_SUCCESS)
1510 * When asking for the loaded_image interface, just
1511 * return handle which points to loaded_image_info
1513 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1514 if (ret != EFI_SUCCESS)
1519 printf("ERROR: Failure to install protocols for loaded image\n");
1524 * efi_load_image_from_path() - load an image using a file path
1525 * @file_path: the path of the image to load
1526 * @buffer: buffer containing the loaded image
1528 * Return: status code
1530 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1533 struct efi_file_info *info = NULL;
1534 struct efi_file_handle *f;
1535 static efi_status_t ret;
1538 f = efi_file_from_path(file_path);
1540 return EFI_DEVICE_ERROR;
1543 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1545 if (ret == EFI_BUFFER_TOO_SMALL) {
1547 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1550 if (ret != EFI_SUCCESS)
1553 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1557 bs = info->file_size;
1558 EFI_CALL(ret = f->read(f, &bs, *buffer));
1562 EFI_CALL(f->close(f));
1564 if (ret != EFI_SUCCESS) {
1565 efi_free_pool(*buffer);
1573 * efi_load_image() - load an EFI image into memory
1574 * @boot_policy: true for request originating from the boot manager
1575 * @parent_image: the caller's image handle
1576 * @file_path: the path of the image to load
1577 * @source_buffer: memory location from which the image is installed
1578 * @source_size: size of the memory area from which the image is installed
1579 * @image_handle: handle for the newly installed image
1581 * This function implements the LoadImage service.
1583 * See the Unified Extensible Firmware Interface (UEFI) specification
1586 * Return: status code
1588 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1589 efi_handle_t parent_image,
1590 struct efi_device_path *file_path,
1591 void *source_buffer,
1592 efi_uintn_t source_size,
1593 efi_handle_t *image_handle)
1595 struct efi_loaded_image *info;
1596 struct efi_object *obj;
1599 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1600 file_path, source_buffer, source_size, image_handle);
1602 if (!image_handle || !parent_image) {
1603 ret = EFI_INVALID_PARAMETER;
1607 if (!source_buffer && !file_path) {
1608 ret = EFI_NOT_FOUND;
1612 info = calloc(1, sizeof(*info));
1614 ret = EFI_OUT_OF_RESOURCES;
1617 obj = calloc(1, sizeof(*obj));
1620 ret = EFI_OUT_OF_RESOURCES;
1624 if (!source_buffer) {
1625 struct efi_device_path *dp, *fp;
1627 ret = efi_load_image_from_path(file_path, &source_buffer);
1628 if (ret != EFI_SUCCESS)
1631 * split file_path which contains both the device and
1634 efi_dp_split_file_path(file_path, &dp, &fp);
1635 ret = efi_setup_loaded_image(info, obj, dp, fp);
1636 if (ret != EFI_SUCCESS)
1639 /* In this case, file_path is the "device" path, i.e.
1640 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1642 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1643 if (ret != EFI_SUCCESS)
1646 info->reserved = efi_load_pe(source_buffer, info);
1647 if (!info->reserved) {
1648 ret = EFI_UNSUPPORTED;
1651 info->system_table = &systab;
1652 info->parent_handle = parent_image;
1653 *image_handle = obj->handle;
1654 return EFI_EXIT(EFI_SUCCESS);
1657 efi_delete_handle(obj);
1659 return EFI_EXIT(ret);
1663 * efi_start_image() - call the entry point of an image
1664 * @image_handle: handle of the image
1665 * @exit_data_size: size of the buffer
1666 * @exit_data: buffer to receive the exit data of the called image
1668 * This function implements the StartImage service.
1670 * See the Unified Extensible Firmware Interface (UEFI) specification for
1673 * Return: status code
1675 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1676 unsigned long *exit_data_size,
1679 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1680 struct efi_system_table *st);
1681 struct efi_loaded_image *info = image_handle;
1684 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1685 entry = info->reserved;
1687 efi_is_direct_boot = false;
1689 /* call the image! */
1690 if (setjmp(&info->exit_jmp)) {
1692 * We called the entry point of the child image with EFI_CALL
1693 * in the lines below. The child image called the Exit() boot
1694 * service efi_exit() which executed the long jump that brought
1695 * us to the current line. This implies that the second half
1696 * of the EFI_CALL macro has not been executed.
1700 * efi_exit() called efi_restore_gd(). We have to undo this
1701 * otherwise __efi_entry_check() will put the wrong value into
1707 * To get ready to call EFI_EXIT below we have to execute the
1708 * missed out steps of EFI_CALL.
1710 assert(__efi_entry_check());
1711 debug("%sEFI: %lu returned by started image\n",
1712 __efi_nesting_dec(),
1713 (unsigned long)((uintptr_t)info->exit_status &
1715 return EFI_EXIT(info->exit_status);
1718 ret = EFI_CALL(entry(image_handle, &systab));
1721 * Usually UEFI applications call Exit() instead of returning.
1722 * But because the world doesn't consist of ponies and unicorns,
1723 * we're happy to emulate that behavior on behalf of a payload
1726 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1730 * efi_exit() - leave an EFI application or driver
1731 * @image_handle: handle of the application or driver that is exiting
1732 * @exit_status: status code
1733 * @exit_data_size: size of the buffer in bytes
1734 * @exit_data: buffer with data describing an error
1736 * This function implements the Exit service.
1738 * See the Unified Extensible Firmware Interface (UEFI) specification for
1741 * Return: status code
1743 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1744 efi_status_t exit_status,
1745 unsigned long exit_data_size,
1749 * We require that the handle points to the original loaded
1750 * image protocol interface.
1752 * For getting the longjmp address this is safer than locating
1753 * the protocol because the protocol may have been reinstalled
1754 * pointing to another memory location.
1756 * TODO: We should call the unload procedure of the loaded
1759 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
1761 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1762 exit_data_size, exit_data);
1764 /* Make sure entry/exit counts for EFI world cross-overs match */
1765 EFI_EXIT(exit_status);
1768 * But longjmp out with the U-Boot gd, not the application's, as
1769 * the other end is a setjmp call inside EFI context.
1773 loaded_image_info->exit_status = exit_status;
1774 longjmp(&loaded_image_info->exit_jmp, 1);
1776 panic("EFI application exited");
1780 * efi_unload_image() - unload an EFI image
1781 * @image_handle: handle of the image to be unloaded
1783 * This function implements the UnloadImage service.
1785 * See the Unified Extensible Firmware Interface (UEFI) specification for
1788 * Return: status code
1790 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1792 struct efi_object *efiobj;
1794 EFI_ENTRY("%p", image_handle);
1795 efiobj = efi_search_obj(image_handle);
1797 list_del(&efiobj->link);
1799 return EFI_EXIT(EFI_SUCCESS);
1803 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1805 static void efi_exit_caches(void)
1807 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1809 * Grub on 32bit ARM needs to have caches disabled before jumping into
1810 * a zImage, but does not know of all cache layers. Give it a hand.
1812 if (efi_is_direct_boot)
1813 cleanup_before_linux();
1818 * efi_exit_boot_services() - stop all boot services
1819 * @image_handle: handle of the loaded image
1820 * @map_key: key of the memory map
1822 * This function implements the ExitBootServices service.
1824 * See the Unified Extensible Firmware Interface (UEFI) specification
1827 * All timer events are disabled. For exit boot services events the
1828 * notification function is called. The boot services are disabled in the
1831 * Return: status code
1833 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1834 unsigned long map_key)
1836 struct efi_event *evt;
1838 EFI_ENTRY("%p, %ld", image_handle, map_key);
1840 /* Check that the caller has read the current memory map */
1841 if (map_key != efi_memory_map_key)
1842 return EFI_INVALID_PARAMETER;
1844 /* Make sure that notification functions are not called anymore */
1845 efi_tpl = TPL_HIGH_LEVEL;
1847 /* Check if ExitBootServices has already been called */
1848 if (!systab.boottime)
1849 return EFI_EXIT(EFI_SUCCESS);
1851 /* Add related events to the event group */
1852 list_for_each_entry(evt, &efi_events, link) {
1853 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1854 evt->group = &efi_guid_event_group_exit_boot_services;
1856 /* Notify that ExitBootServices is invoked. */
1857 list_for_each_entry(evt, &efi_events, link) {
1859 !guidcmp(evt->group,
1860 &efi_guid_event_group_exit_boot_services)) {
1861 efi_signal_event(evt, false);
1866 /* TODO: Should persist EFI variables here */
1868 board_quiesce_devices();
1870 /* Fix up caches for EFI payloads if necessary */
1873 /* This stops all lingering devices */
1874 bootm_disable_interrupts();
1876 /* Disable boot time services */
1877 systab.con_in_handle = NULL;
1878 systab.con_in = NULL;
1879 systab.con_out_handle = NULL;
1880 systab.con_out = NULL;
1881 systab.stderr_handle = NULL;
1882 systab.std_err = NULL;
1883 systab.boottime = NULL;
1885 /* Recalculate CRC32 */
1886 efi_update_table_header_crc32(&systab.hdr);
1888 /* Give the payload some time to boot */
1889 efi_set_watchdog(0);
1892 return EFI_EXIT(EFI_SUCCESS);
1896 * efi_get_next_monotonic_count() - get next value of the counter
1897 * @count: returned value of the counter
1899 * This function implements the NextMonotonicCount service.
1901 * See the Unified Extensible Firmware Interface (UEFI) specification for
1904 * Return: status code
1906 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1908 static uint64_t mono;
1910 EFI_ENTRY("%p", count);
1912 return EFI_EXIT(EFI_SUCCESS);
1916 * efi_stall() - sleep
1917 * @microseconds: period to sleep in microseconds
1919 * This function implements the Stall service.
1921 * See the Unified Extensible Firmware Interface (UEFI) specification for
1924 * Return: status code
1926 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1928 EFI_ENTRY("%ld", microseconds);
1929 udelay(microseconds);
1930 return EFI_EXIT(EFI_SUCCESS);
1934 * efi_set_watchdog_timer() - reset the watchdog timer
1935 * @timeout: seconds before reset by watchdog
1936 * @watchdog_code: code to be logged when resetting
1937 * @data_size: size of buffer in bytes
1938 * @watchdog_data: buffer with data describing the reset reason
1940 * This function implements the SetWatchdogTimer service.
1942 * See the Unified Extensible Firmware Interface (UEFI) specification for
1945 * Return: status code
1947 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1948 uint64_t watchdog_code,
1949 unsigned long data_size,
1950 uint16_t *watchdog_data)
1952 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1953 data_size, watchdog_data);
1954 return EFI_EXIT(efi_set_watchdog(timeout));
1958 * efi_close_protocol() - close a protocol
1959 * @handle: handle on which the protocol shall be closed
1960 * @protocol: GUID of the protocol to close
1961 * @agent_handle: handle of the driver
1962 * @controller_handle: handle of the controller
1964 * This function implements the CloseProtocol service.
1966 * See the Unified Extensible Firmware Interface (UEFI) specification for
1969 * Return: status code
1971 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1972 const efi_guid_t *protocol,
1973 efi_handle_t agent_handle,
1974 efi_handle_t controller_handle)
1976 struct efi_handler *handler;
1977 struct efi_open_protocol_info_item *item;
1978 struct efi_open_protocol_info_item *pos;
1981 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1984 if (!agent_handle) {
1985 r = EFI_INVALID_PARAMETER;
1988 r = efi_search_protocol(handle, protocol, &handler);
1989 if (r != EFI_SUCCESS)
1993 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1994 if (item->info.agent_handle == agent_handle &&
1995 item->info.controller_handle == controller_handle) {
1996 efi_delete_open_info(item);
2006 * efi_open_protocol_information() - provide information about then open status
2007 * of a protocol on a handle
2008 * @handle: handle for which the information shall be retrieved
2009 * @protocol: GUID of the protocol
2010 * @entry_buffer: buffer to receive the open protocol information
2011 * @entry_count: number of entries available in the buffer
2013 * This function implements the OpenProtocolInformation service.
2015 * See the Unified Extensible Firmware Interface (UEFI) specification for
2018 * Return: status code
2020 static efi_status_t EFIAPI efi_open_protocol_information(
2021 efi_handle_t handle, const efi_guid_t *protocol,
2022 struct efi_open_protocol_info_entry **entry_buffer,
2023 efi_uintn_t *entry_count)
2025 unsigned long buffer_size;
2026 unsigned long count;
2027 struct efi_handler *handler;
2028 struct efi_open_protocol_info_item *item;
2031 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2034 /* Check parameters */
2035 if (!entry_buffer) {
2036 r = EFI_INVALID_PARAMETER;
2039 r = efi_search_protocol(handle, protocol, &handler);
2040 if (r != EFI_SUCCESS)
2045 list_for_each_entry(item, &handler->open_infos, link) {
2046 if (item->info.open_count)
2049 *entry_count = count;
2050 *entry_buffer = NULL;
2057 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2058 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2059 (void **)entry_buffer);
2060 if (r != EFI_SUCCESS)
2062 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2063 if (item->info.open_count)
2064 (*entry_buffer)[--count] = item->info;
2071 * efi_protocols_per_handle() - get protocols installed on a handle
2072 * @handle: handle for which the information is retrieved
2073 * @protocol_buffer: buffer with protocol GUIDs
2074 * @protocol_buffer_count: number of entries in the buffer
2076 * This function implements the ProtocolsPerHandleService.
2078 * See the Unified Extensible Firmware Interface (UEFI) specification for
2081 * Return: status code
2083 static efi_status_t EFIAPI efi_protocols_per_handle(
2084 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2085 efi_uintn_t *protocol_buffer_count)
2087 unsigned long buffer_size;
2088 struct efi_object *efiobj;
2089 struct list_head *protocol_handle;
2092 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2093 protocol_buffer_count);
2095 if (!handle || !protocol_buffer || !protocol_buffer_count)
2096 return EFI_EXIT(EFI_INVALID_PARAMETER);
2098 *protocol_buffer = NULL;
2099 *protocol_buffer_count = 0;
2101 efiobj = efi_search_obj(handle);
2103 return EFI_EXIT(EFI_INVALID_PARAMETER);
2105 /* Count protocols */
2106 list_for_each(protocol_handle, &efiobj->protocols) {
2107 ++*protocol_buffer_count;
2111 if (*protocol_buffer_count) {
2114 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2115 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2116 (void **)protocol_buffer);
2117 if (r != EFI_SUCCESS)
2119 list_for_each(protocol_handle, &efiobj->protocols) {
2120 struct efi_handler *protocol;
2122 protocol = list_entry(protocol_handle,
2123 struct efi_handler, link);
2124 (*protocol_buffer)[j] = (void *)protocol->guid;
2129 return EFI_EXIT(EFI_SUCCESS);
2133 * efi_locate_handle_buffer() - locate handles implementing a protocol
2134 * @search_type: selection criterion
2135 * @protocol: GUID of the protocol
2136 * @search_key: registration key
2137 * @no_handles: number of returned handles
2138 * @buffer: buffer with the returned handles
2140 * This function implements the LocateHandleBuffer service.
2142 * See the Unified Extensible Firmware Interface (UEFI) specification for
2145 * Return: status code
2147 static efi_status_t EFIAPI efi_locate_handle_buffer(
2148 enum efi_locate_search_type search_type,
2149 const efi_guid_t *protocol, void *search_key,
2150 efi_uintn_t *no_handles, efi_handle_t **buffer)
2153 efi_uintn_t buffer_size = 0;
2155 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2156 no_handles, buffer);
2158 if (!no_handles || !buffer) {
2159 r = EFI_INVALID_PARAMETER;
2164 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2166 if (r != EFI_BUFFER_TOO_SMALL)
2168 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2170 if (r != EFI_SUCCESS)
2172 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2174 if (r == EFI_SUCCESS)
2175 *no_handles = buffer_size / sizeof(efi_handle_t);
2181 * efi_locate_protocol() - find an interface implementing a protocol
2182 * @protocol: GUID of the protocol
2183 * @registration: registration key passed to the notification function
2184 * @protocol_interface: interface implementing the protocol
2186 * This function implements the LocateProtocol service.
2188 * See the Unified Extensible Firmware Interface (UEFI) specification for
2191 * Return: status code
2193 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2195 void **protocol_interface)
2197 struct list_head *lhandle;
2200 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2202 if (!protocol || !protocol_interface)
2203 return EFI_EXIT(EFI_INVALID_PARAMETER);
2205 list_for_each(lhandle, &efi_obj_list) {
2206 struct efi_object *efiobj;
2207 struct efi_handler *handler;
2209 efiobj = list_entry(lhandle, struct efi_object, link);
2211 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2212 if (ret == EFI_SUCCESS) {
2213 *protocol_interface = handler->protocol_interface;
2214 return EFI_EXIT(EFI_SUCCESS);
2217 *protocol_interface = NULL;
2219 return EFI_EXIT(EFI_NOT_FOUND);
2223 * efi_locate_device_path() - Get the device path and handle of an device
2224 * implementing a protocol
2225 * @protocol: GUID of the protocol
2226 * @device_path: device path
2227 * @device: handle of the device
2229 * This function implements the LocateDevicePath service.
2231 * See the Unified Extensible Firmware Interface (UEFI) specification for
2234 * Return: status code
2236 static efi_status_t EFIAPI efi_locate_device_path(
2237 const efi_guid_t *protocol,
2238 struct efi_device_path **device_path,
2239 efi_handle_t *device)
2241 struct efi_device_path *dp;
2243 struct efi_handler *handler;
2244 efi_handle_t *handles;
2246 size_t len_best = 0;
2247 efi_uintn_t no_handles;
2251 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2253 if (!protocol || !device_path || !*device_path || !device) {
2254 ret = EFI_INVALID_PARAMETER;
2258 /* Find end of device path */
2259 len = efi_dp_instance_size(*device_path);
2261 /* Get all handles implementing the protocol */
2262 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2263 &no_handles, &handles));
2264 if (ret != EFI_SUCCESS)
2267 for (i = 0; i < no_handles; ++i) {
2268 /* Find the device path protocol */
2269 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2271 if (ret != EFI_SUCCESS)
2273 dp = (struct efi_device_path *)handler->protocol_interface;
2274 len_dp = efi_dp_instance_size(dp);
2276 * This handle can only be a better fit
2277 * if its device path length is longer than the best fit and
2278 * if its device path length is shorter of equal the searched
2281 if (len_dp <= len_best || len_dp > len)
2283 /* Check if dp is a subpath of device_path */
2284 if (memcmp(*device_path, dp, len_dp))
2286 *device = handles[i];
2290 remainder = (u8 *)*device_path + len_best;
2291 *device_path = (struct efi_device_path *)remainder;
2294 ret = EFI_NOT_FOUND;
2297 return EFI_EXIT(ret);
2301 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2303 * @handle: handle on which the protocol interfaces shall be installed
2304 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2307 * This function implements the MultipleProtocolInterfaces service.
2309 * See the Unified Extensible Firmware Interface (UEFI) specification for
2312 * Return: status code
2314 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2317 EFI_ENTRY("%p", handle);
2320 const efi_guid_t *protocol;
2321 void *protocol_interface;
2322 efi_status_t r = EFI_SUCCESS;
2326 return EFI_EXIT(EFI_INVALID_PARAMETER);
2328 efi_va_start(argptr, handle);
2330 protocol = efi_va_arg(argptr, efi_guid_t*);
2333 protocol_interface = efi_va_arg(argptr, void*);
2334 r = EFI_CALL(efi_install_protocol_interface(
2336 EFI_NATIVE_INTERFACE,
2337 protocol_interface));
2338 if (r != EFI_SUCCESS)
2343 if (r == EFI_SUCCESS)
2346 /* If an error occurred undo all changes. */
2347 efi_va_start(argptr, handle);
2349 protocol = efi_va_arg(argptr, efi_guid_t*);
2350 protocol_interface = efi_va_arg(argptr, void*);
2351 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2352 protocol_interface));
2360 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2362 * @handle: handle from which the protocol interfaces shall be removed
2363 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2366 * This function implements the UninstallMultipleProtocolInterfaces service.
2368 * See the Unified Extensible Firmware Interface (UEFI) specification for
2371 * Return: status code
2373 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2376 EFI_ENTRY("%p", handle);
2379 const efi_guid_t *protocol;
2380 void *protocol_interface;
2381 efi_status_t r = EFI_SUCCESS;
2385 return EFI_EXIT(EFI_INVALID_PARAMETER);
2387 efi_va_start(argptr, handle);
2389 protocol = efi_va_arg(argptr, efi_guid_t*);
2392 protocol_interface = efi_va_arg(argptr, void*);
2393 r = EFI_CALL(efi_uninstall_protocol_interface(
2395 protocol_interface));
2396 if (r != EFI_SUCCESS)
2401 if (r == EFI_SUCCESS)
2404 /* If an error occurred undo all changes. */
2405 efi_va_start(argptr, handle);
2407 protocol = efi_va_arg(argptr, efi_guid_t*);
2408 protocol_interface = efi_va_arg(argptr, void*);
2409 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2410 EFI_NATIVE_INTERFACE,
2411 protocol_interface));
2419 * efi_calculate_crc32() - calculate cyclic redundancy code
2420 * @data: buffer with data
2421 * @data_size: size of buffer in bytes
2422 * @crc32_p: cyclic redundancy code
2424 * This function implements the CalculateCrc32 service.
2426 * See the Unified Extensible Firmware Interface (UEFI) specification for
2429 * Return: status code
2431 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2432 efi_uintn_t data_size,
2435 EFI_ENTRY("%p, %zu", data, data_size);
2436 *crc32_p = crc32(0, data, data_size);
2437 return EFI_EXIT(EFI_SUCCESS);
2441 * efi_copy_mem() - copy memory
2442 * @destination: destination of the copy operation
2443 * @source: source of the copy operation
2444 * @length: number of bytes to copy
2446 * This function implements the CopyMem service.
2448 * See the Unified Extensible Firmware Interface (UEFI) specification for
2451 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2454 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2455 memcpy(destination, source, length);
2456 EFI_EXIT(EFI_SUCCESS);
2460 * efi_set_mem() - Fill memory with a byte value.
2461 * @buffer: buffer to fill
2462 * @size: size of buffer in bytes
2463 * @value: byte to copy to the buffer
2465 * This function implements the SetMem service.
2467 * See the Unified Extensible Firmware Interface (UEFI) specification for
2470 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2472 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2473 memset(buffer, value, size);
2474 EFI_EXIT(EFI_SUCCESS);
2478 * efi_protocol_open() - open protocol interface on a handle
2479 * @handler: handler of a protocol
2480 * @protocol_interface: interface implementing the protocol
2481 * @agent_handle: handle of the driver
2482 * @controller_handle: handle of the controller
2483 * @attributes: attributes indicating how to open the protocol
2485 * Return: status code
2487 static efi_status_t efi_protocol_open(
2488 struct efi_handler *handler,
2489 void **protocol_interface, void *agent_handle,
2490 void *controller_handle, uint32_t attributes)
2492 struct efi_open_protocol_info_item *item;
2493 struct efi_open_protocol_info_entry *match = NULL;
2494 bool opened_by_driver = false;
2495 bool opened_exclusive = false;
2497 /* If there is no agent, only return the interface */
2501 /* For TEST_PROTOCOL ignore interface attribute */
2502 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2503 *protocol_interface = NULL;
2506 * Check if the protocol is already opened by a driver with the same
2507 * attributes or opened exclusively
2509 list_for_each_entry(item, &handler->open_infos, link) {
2510 if (item->info.agent_handle == agent_handle) {
2511 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2512 (item->info.attributes == attributes))
2513 return EFI_ALREADY_STARTED;
2515 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2516 opened_exclusive = true;
2519 /* Only one controller can open the protocol exclusively */
2520 if (opened_exclusive && attributes &
2521 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2522 return EFI_ACCESS_DENIED;
2524 /* Prepare exclusive opening */
2525 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2526 /* Try to disconnect controllers */
2527 list_for_each_entry(item, &handler->open_infos, link) {
2528 if (item->info.attributes ==
2529 EFI_OPEN_PROTOCOL_BY_DRIVER)
2530 EFI_CALL(efi_disconnect_controller(
2531 item->info.controller_handle,
2532 item->info.agent_handle,
2535 opened_by_driver = false;
2536 /* Check if all controllers are disconnected */
2537 list_for_each_entry(item, &handler->open_infos, link) {
2538 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2539 opened_by_driver = true;
2541 /* Only one controller can be conncected */
2542 if (opened_by_driver)
2543 return EFI_ACCESS_DENIED;
2546 /* Find existing entry */
2547 list_for_each_entry(item, &handler->open_infos, link) {
2548 if (item->info.agent_handle == agent_handle &&
2549 item->info.controller_handle == controller_handle)
2550 match = &item->info;
2552 /* None found, create one */
2554 match = efi_create_open_info(handler);
2556 return EFI_OUT_OF_RESOURCES;
2559 match->agent_handle = agent_handle;
2560 match->controller_handle = controller_handle;
2561 match->attributes = attributes;
2562 match->open_count++;
2565 /* For TEST_PROTOCOL ignore interface attribute. */
2566 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2567 *protocol_interface = handler->protocol_interface;
2573 * efi_open_protocol() - open protocol interface on a handle
2574 * @handle: handle on which the protocol shall be opened
2575 * @protocol: GUID of the protocol
2576 * @protocol_interface: interface implementing the protocol
2577 * @agent_handle: handle of the driver
2578 * @controller_handle: handle of the controller
2579 * @attributes: attributes indicating how to open the protocol
2581 * This function implements the OpenProtocol interface.
2583 * See the Unified Extensible Firmware Interface (UEFI) specification for
2586 * Return: status code
2588 static efi_status_t EFIAPI efi_open_protocol(
2589 void *handle, const efi_guid_t *protocol,
2590 void **protocol_interface, void *agent_handle,
2591 void *controller_handle, uint32_t attributes)
2593 struct efi_handler *handler;
2594 efi_status_t r = EFI_INVALID_PARAMETER;
2596 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2597 protocol_interface, agent_handle, controller_handle,
2600 if (!handle || !protocol ||
2601 (!protocol_interface && attributes !=
2602 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2606 switch (attributes) {
2607 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2608 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2609 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2611 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2612 if (controller_handle == handle)
2615 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2616 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2617 /* Check that the controller handle is valid */
2618 if (!efi_search_obj(controller_handle))
2621 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2622 /* Check that the agent handle is valid */
2623 if (!efi_search_obj(agent_handle))
2630 r = efi_search_protocol(handle, protocol, &handler);
2631 if (r != EFI_SUCCESS)
2634 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2635 controller_handle, attributes);
2641 * efi_handle_protocol() - get interface of a protocol on a handle
2642 * @handle: handle on which the protocol shall be opened
2643 * @protocol: GUID of the protocol
2644 * @protocol_interface: interface implementing the protocol
2646 * This function implements the HandleProtocol service.
2648 * See the Unified Extensible Firmware Interface (UEFI) specification for
2651 * Return: status code
2653 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2654 const efi_guid_t *protocol,
2655 void **protocol_interface)
2657 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2658 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2662 * efi_bind_controller() - bind a single driver to a controller
2663 * @controller_handle: controller handle
2664 * @driver_image_handle: driver handle
2665 * @remain_device_path: remaining path
2667 * Return: status code
2669 static efi_status_t efi_bind_controller(
2670 efi_handle_t controller_handle,
2671 efi_handle_t driver_image_handle,
2672 struct efi_device_path *remain_device_path)
2674 struct efi_driver_binding_protocol *binding_protocol;
2677 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2678 &efi_guid_driver_binding_protocol,
2679 (void **)&binding_protocol,
2680 driver_image_handle, NULL,
2681 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2682 if (r != EFI_SUCCESS)
2684 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2686 remain_device_path));
2687 if (r == EFI_SUCCESS)
2688 r = EFI_CALL(binding_protocol->start(binding_protocol,
2690 remain_device_path));
2691 EFI_CALL(efi_close_protocol(driver_image_handle,
2692 &efi_guid_driver_binding_protocol,
2693 driver_image_handle, NULL));
2698 * efi_connect_single_controller() - connect a single driver to a controller
2699 * @controller_handle: controller
2700 * @driver_image_handle: driver
2701 * @remain_device_path: remaining path
2703 * Return: status code
2705 static efi_status_t efi_connect_single_controller(
2706 efi_handle_t controller_handle,
2707 efi_handle_t *driver_image_handle,
2708 struct efi_device_path *remain_device_path)
2710 efi_handle_t *buffer;
2714 size_t connected = 0;
2716 /* Get buffer with all handles with driver binding protocol */
2717 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2718 &efi_guid_driver_binding_protocol,
2719 NULL, &count, &buffer));
2720 if (r != EFI_SUCCESS)
2723 /* Context Override */
2724 if (driver_image_handle) {
2725 for (; *driver_image_handle; ++driver_image_handle) {
2726 for (i = 0; i < count; ++i) {
2727 if (buffer[i] == *driver_image_handle) {
2729 r = efi_bind_controller(
2731 *driver_image_handle,
2732 remain_device_path);
2734 * For drivers that do not support the
2735 * controller or are already connected
2736 * we receive an error code here.
2738 if (r == EFI_SUCCESS)
2746 * TODO: Some overrides are not yet implemented:
2747 * - Platform Driver Override
2748 * - Driver Family Override Search
2749 * - Bus Specific Driver Override
2752 /* Driver Binding Search */
2753 for (i = 0; i < count; ++i) {
2755 r = efi_bind_controller(controller_handle,
2757 remain_device_path);
2758 if (r == EFI_SUCCESS)
2763 efi_free_pool(buffer);
2765 return EFI_NOT_FOUND;
2770 * efi_connect_controller() - connect a controller to a driver
2771 * @controller_handle: handle of the controller
2772 * @driver_image_handle: handle of the driver
2773 * @remain_device_path: device path of a child controller
2774 * @recursive: true to connect all child controllers
2776 * This function implements the ConnectController service.
2778 * See the Unified Extensible Firmware Interface (UEFI) specification for
2781 * First all driver binding protocol handles are tried for binding drivers.
2782 * Afterwards all handles that have opened a protocol of the controller
2783 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2785 * Return: status code
2787 static efi_status_t EFIAPI efi_connect_controller(
2788 efi_handle_t controller_handle,
2789 efi_handle_t *driver_image_handle,
2790 struct efi_device_path *remain_device_path,
2794 efi_status_t ret = EFI_NOT_FOUND;
2795 struct efi_object *efiobj;
2797 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2798 remain_device_path, recursive);
2800 efiobj = efi_search_obj(controller_handle);
2802 ret = EFI_INVALID_PARAMETER;
2806 r = efi_connect_single_controller(controller_handle,
2807 driver_image_handle,
2808 remain_device_path);
2809 if (r == EFI_SUCCESS)
2812 struct efi_handler *handler;
2813 struct efi_open_protocol_info_item *item;
2815 list_for_each_entry(handler, &efiobj->protocols, link) {
2816 list_for_each_entry(item, &handler->open_infos, link) {
2817 if (item->info.attributes &
2818 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2819 r = EFI_CALL(efi_connect_controller(
2820 item->info.controller_handle,
2821 driver_image_handle,
2824 if (r == EFI_SUCCESS)
2830 /* Check for child controller specified by end node */
2831 if (ret != EFI_SUCCESS && remain_device_path &&
2832 remain_device_path->type == DEVICE_PATH_TYPE_END)
2835 return EFI_EXIT(ret);
2839 * efi_reinstall_protocol_interface() - reinstall protocol interface
2840 * @handle: handle on which the protocol shall be reinstalled
2841 * @protocol: GUID of the protocol to be installed
2842 * @old_interface: interface to be removed
2843 * @new_interface: interface to be installed
2845 * This function implements the ReinstallProtocolInterface service.
2847 * See the Unified Extensible Firmware Interface (UEFI) specification for
2850 * The old interface is uninstalled. The new interface is installed.
2851 * Drivers are connected.
2853 * Return: status code
2855 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2856 efi_handle_t handle, const efi_guid_t *protocol,
2857 void *old_interface, void *new_interface)
2861 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2863 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2865 if (ret != EFI_SUCCESS)
2867 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2868 EFI_NATIVE_INTERFACE,
2870 if (ret != EFI_SUCCESS)
2873 * The returned status code has to be ignored.
2874 * Do not create an error if no suitable driver for the handle exists.
2876 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2878 return EFI_EXIT(ret);
2882 * efi_get_child_controllers() - get all child controllers associated to a driver
2883 * @efiobj: handle of the controller
2884 * @driver_handle: handle of the driver
2885 * @number_of_children: number of child controllers
2886 * @child_handle_buffer: handles of the the child controllers
2888 * The allocated buffer has to be freed with free().
2890 * Return: status code
2892 static efi_status_t efi_get_child_controllers(
2893 struct efi_object *efiobj,
2894 efi_handle_t driver_handle,
2895 efi_uintn_t *number_of_children,
2896 efi_handle_t **child_handle_buffer)
2898 struct efi_handler *handler;
2899 struct efi_open_protocol_info_item *item;
2900 efi_uintn_t count = 0, i;
2903 /* Count all child controller associations */
2904 list_for_each_entry(handler, &efiobj->protocols, link) {
2905 list_for_each_entry(item, &handler->open_infos, link) {
2906 if (item->info.agent_handle == driver_handle &&
2907 item->info.attributes &
2908 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2913 * Create buffer. In case of duplicate child controller assignments
2914 * the buffer will be too large. But that does not harm.
2916 *number_of_children = 0;
2917 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2918 if (!*child_handle_buffer)
2919 return EFI_OUT_OF_RESOURCES;
2920 /* Copy unique child handles */
2921 list_for_each_entry(handler, &efiobj->protocols, link) {
2922 list_for_each_entry(item, &handler->open_infos, link) {
2923 if (item->info.agent_handle == driver_handle &&
2924 item->info.attributes &
2925 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2926 /* Check this is a new child controller */
2928 for (i = 0; i < *number_of_children; ++i) {
2929 if ((*child_handle_buffer)[i] ==
2930 item->info.controller_handle)
2933 /* Copy handle to buffer */
2935 i = (*number_of_children)++;
2936 (*child_handle_buffer)[i] =
2937 item->info.controller_handle;
2946 * efi_disconnect_controller() - disconnect a controller from a driver
2947 * @controller_handle: handle of the controller
2948 * @driver_image_handle: handle of the driver
2949 * @child_handle: handle of the child to destroy
2951 * This function implements the DisconnectController service.
2953 * See the Unified Extensible Firmware Interface (UEFI) specification for
2956 * Return: status code
2958 static efi_status_t EFIAPI efi_disconnect_controller(
2959 efi_handle_t controller_handle,
2960 efi_handle_t driver_image_handle,
2961 efi_handle_t child_handle)
2963 struct efi_driver_binding_protocol *binding_protocol;
2964 efi_handle_t *child_handle_buffer = NULL;
2965 size_t number_of_children = 0;
2967 size_t stop_count = 0;
2968 struct efi_object *efiobj;
2970 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2973 efiobj = efi_search_obj(controller_handle);
2975 r = EFI_INVALID_PARAMETER;
2979 if (child_handle && !efi_search_obj(child_handle)) {
2980 r = EFI_INVALID_PARAMETER;
2984 /* If no driver handle is supplied, disconnect all drivers */
2985 if (!driver_image_handle) {
2986 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2990 /* Create list of child handles */
2992 number_of_children = 1;
2993 child_handle_buffer = &child_handle;
2995 efi_get_child_controllers(efiobj,
2996 driver_image_handle,
2997 &number_of_children,
2998 &child_handle_buffer);
3001 /* Get the driver binding protocol */
3002 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3003 &efi_guid_driver_binding_protocol,
3004 (void **)&binding_protocol,
3005 driver_image_handle, NULL,
3006 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3007 if (r != EFI_SUCCESS)
3009 /* Remove the children */
3010 if (number_of_children) {
3011 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3014 child_handle_buffer));
3015 if (r == EFI_SUCCESS)
3018 /* Remove the driver */
3020 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3023 if (r == EFI_SUCCESS)
3025 EFI_CALL(efi_close_protocol(driver_image_handle,
3026 &efi_guid_driver_binding_protocol,
3027 driver_image_handle, NULL));
3035 free(child_handle_buffer);
3039 static struct efi_boot_services efi_boot_services = {
3041 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3042 .revision = EFI_SPECIFICATION_VERSION,
3043 .headersize = sizeof(struct efi_boot_services),
3045 .raise_tpl = efi_raise_tpl,
3046 .restore_tpl = efi_restore_tpl,
3047 .allocate_pages = efi_allocate_pages_ext,
3048 .free_pages = efi_free_pages_ext,
3049 .get_memory_map = efi_get_memory_map_ext,
3050 .allocate_pool = efi_allocate_pool_ext,
3051 .free_pool = efi_free_pool_ext,
3052 .create_event = efi_create_event_ext,
3053 .set_timer = efi_set_timer_ext,
3054 .wait_for_event = efi_wait_for_event,
3055 .signal_event = efi_signal_event_ext,
3056 .close_event = efi_close_event,
3057 .check_event = efi_check_event,
3058 .install_protocol_interface = efi_install_protocol_interface,
3059 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3060 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3061 .handle_protocol = efi_handle_protocol,
3063 .register_protocol_notify = efi_register_protocol_notify,
3064 .locate_handle = efi_locate_handle_ext,
3065 .locate_device_path = efi_locate_device_path,
3066 .install_configuration_table = efi_install_configuration_table_ext,
3067 .load_image = efi_load_image,
3068 .start_image = efi_start_image,
3070 .unload_image = efi_unload_image,
3071 .exit_boot_services = efi_exit_boot_services,
3072 .get_next_monotonic_count = efi_get_next_monotonic_count,
3074 .set_watchdog_timer = efi_set_watchdog_timer,
3075 .connect_controller = efi_connect_controller,
3076 .disconnect_controller = efi_disconnect_controller,
3077 .open_protocol = efi_open_protocol,
3078 .close_protocol = efi_close_protocol,
3079 .open_protocol_information = efi_open_protocol_information,
3080 .protocols_per_handle = efi_protocols_per_handle,
3081 .locate_handle_buffer = efi_locate_handle_buffer,
3082 .locate_protocol = efi_locate_protocol,
3083 .install_multiple_protocol_interfaces =
3084 efi_install_multiple_protocol_interfaces,
3085 .uninstall_multiple_protocol_interfaces =
3086 efi_uninstall_multiple_protocol_interfaces,
3087 .calculate_crc32 = efi_calculate_crc32,
3088 .copy_mem = efi_copy_mem,
3089 .set_mem = efi_set_mem,
3090 .create_event_ex = efi_create_event_ex,
3093 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3095 struct efi_system_table __efi_runtime_data systab = {
3097 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3098 .revision = EFI_SPECIFICATION_VERSION,
3099 .headersize = sizeof(struct efi_system_table),
3101 .fw_vendor = firmware_vendor,
3102 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3103 .con_in = (void *)&efi_con_in,
3104 .con_out = (void *)&efi_con_out,
3105 .std_err = (void *)&efi_con_out,
3106 .runtime = (void *)&efi_runtime_services,
3107 .boottime = (void *)&efi_boot_services,
3113 * efi_initialize_system_table() - Initialize system table
3115 * Return: status code
3117 efi_status_t efi_initialize_system_table(void)
3121 /* Allocate configuration table array */
3122 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3123 EFI_MAX_CONFIGURATION_TABLES *
3124 sizeof(struct efi_configuration_table),
3125 (void **)&systab.tables);
3127 /* Set CRC32 field in table headers */
3128 efi_update_table_header_crc32(&systab.hdr);
3129 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3130 efi_update_table_header_crc32(&efi_boot_services.hdr);