xref: /openbmc/u-boot/lib/efi_loader/efi_boottime.c (revision 66c433ed4342e5761ee9b048c85fe47d31130b2e)
1f739fcd8STom Rini // SPDX-License-Identifier: GPL-2.0+
2bee91169SAlexander Graf /*
3bee91169SAlexander Graf  *  EFI application boot time services
4bee91169SAlexander Graf  *
5bee91169SAlexander Graf  *  Copyright (c) 2016 Alexander Graf
6bee91169SAlexander Graf  */
7bee91169SAlexander Graf 
8bee91169SAlexander Graf #include <common.h>
97d963323SHeinrich Schuchardt #include <div64.h>
10bee91169SAlexander Graf #include <efi_loader.h>
11ad644e7cSRob Clark #include <environment.h>
12bee91169SAlexander Graf #include <malloc.h>
13b08c8c48SMasahiro Yamada #include <linux/libfdt_env.h>
14bee91169SAlexander Graf #include <u-boot/crc.h>
15bee91169SAlexander Graf #include <bootm.h>
16bee91169SAlexander Graf #include <watchdog.h>
17bee91169SAlexander Graf 
18bee91169SAlexander Graf DECLARE_GLOBAL_DATA_PTR;
19bee91169SAlexander Graf 
2032f4b2f8SHeinrich Schuchardt /* Task priority level */
21152cade3SHeinrich Schuchardt static efi_uintn_t efi_tpl = TPL_APPLICATION;
2232f4b2f8SHeinrich Schuchardt 
23bee91169SAlexander Graf /* This list contains all the EFI objects our payload has access to */
24bee91169SAlexander Graf LIST_HEAD(efi_obj_list);
25bee91169SAlexander Graf 
2643bce442SHeinrich Schuchardt /* List of all events */
27b095f3c8SHeinrich Schuchardt LIST_HEAD(efi_events);
2843bce442SHeinrich Schuchardt 
29f31239acSAlexander Graf /*
30f31239acSAlexander Graf  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31f31239acSAlexander Graf  * we need to do trickery with caches. Since we don't want to break the EFI
32f31239acSAlexander Graf  * aware boot path, only apply hacks when loading exiting directly (breaking
33f31239acSAlexander Graf  * direct Linux EFI booting along the way - oh well).
34f31239acSAlexander Graf  */
35f31239acSAlexander Graf static bool efi_is_direct_boot = true;
36f31239acSAlexander Graf 
3765e4c0b1SSimon Glass #ifdef CONFIG_ARM
38bee91169SAlexander Graf /*
39bee91169SAlexander Graf  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40bee91169SAlexander Graf  * fixed when compiling U-Boot. However, the payload does not know about that
41bee91169SAlexander Graf  * restriction so we need to manually swap its and our view of that register on
42bee91169SAlexander Graf  * EFI callback entry/exit.
43bee91169SAlexander Graf  */
44bee91169SAlexander Graf static volatile void *efi_gd, *app_gd;
4565e4c0b1SSimon Glass #endif
46bee91169SAlexander Graf 
47*914df75bSHeinrich Schuchardt /* 1 if inside U-Boot code, 0 if inside EFI payload code */
48*914df75bSHeinrich Schuchardt static int entry_count = 1;
49af65db85SRob Clark static int nesting_level;
50bc4f9133SHeinrich Schuchardt /* GUID of the device tree table */
51bc4f9133SHeinrich Schuchardt const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
52f0959dbeSHeinrich Schuchardt /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
53f0959dbeSHeinrich Schuchardt const efi_guid_t efi_guid_driver_binding_protocol =
54f0959dbeSHeinrich Schuchardt 			EFI_DRIVER_BINDING_PROTOCOL_GUID;
55c160d2f5SRob Clark 
56a3a28f5fSHeinrich Schuchardt /* event group ExitBootServices() invoked */
57a3a28f5fSHeinrich Schuchardt const efi_guid_t efi_guid_event_group_exit_boot_services =
58a3a28f5fSHeinrich Schuchardt 			EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
59a3a28f5fSHeinrich Schuchardt /* event group SetVirtualAddressMap() invoked */
60a3a28f5fSHeinrich Schuchardt const efi_guid_t efi_guid_event_group_virtual_address_change =
61a3a28f5fSHeinrich Schuchardt 			EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
62a3a28f5fSHeinrich Schuchardt /* event group memory map changed */
63a3a28f5fSHeinrich Schuchardt const efi_guid_t efi_guid_event_group_memory_map_change =
64a3a28f5fSHeinrich Schuchardt 			EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
65a3a28f5fSHeinrich Schuchardt /* event group boot manager about to boot */
66a3a28f5fSHeinrich Schuchardt const efi_guid_t efi_guid_event_group_ready_to_boot =
67a3a28f5fSHeinrich Schuchardt 			EFI_EVENT_GROUP_READY_TO_BOOT;
68a3a28f5fSHeinrich Schuchardt /* event group ResetSystem() invoked (before ExitBootServices) */
69a3a28f5fSHeinrich Schuchardt const efi_guid_t efi_guid_event_group_reset_system =
70a3a28f5fSHeinrich Schuchardt 			EFI_EVENT_GROUP_RESET_SYSTEM;
71a3a28f5fSHeinrich Schuchardt 
722074f700SHeinrich Schuchardt static efi_status_t EFIAPI efi_disconnect_controller(
732074f700SHeinrich Schuchardt 					efi_handle_t controller_handle,
742074f700SHeinrich Schuchardt 					efi_handle_t driver_image_handle,
752074f700SHeinrich Schuchardt 					efi_handle_t child_handle);
763f9b0042SHeinrich Schuchardt 
77c160d2f5SRob Clark /* Called on every callback entry */
__efi_entry_check(void)78c160d2f5SRob Clark int __efi_entry_check(void)
79c160d2f5SRob Clark {
80c160d2f5SRob Clark 	int ret = entry_count++ == 0;
81c160d2f5SRob Clark #ifdef CONFIG_ARM
82c160d2f5SRob Clark 	assert(efi_gd);
83c160d2f5SRob Clark 	app_gd = gd;
84c160d2f5SRob Clark 	gd = efi_gd;
85c160d2f5SRob Clark #endif
86c160d2f5SRob Clark 	return ret;
87c160d2f5SRob Clark }
88c160d2f5SRob Clark 
89c160d2f5SRob Clark /* Called on every callback exit */
__efi_exit_check(void)90c160d2f5SRob Clark int __efi_exit_check(void)
91c160d2f5SRob Clark {
92c160d2f5SRob Clark 	int ret = --entry_count == 0;
93c160d2f5SRob Clark #ifdef CONFIG_ARM
94c160d2f5SRob Clark 	gd = app_gd;
95c160d2f5SRob Clark #endif
96c160d2f5SRob Clark 	return ret;
97c160d2f5SRob Clark }
98c160d2f5SRob Clark 
99bee91169SAlexander Graf /* Called from do_bootefi_exec() */
efi_save_gd(void)100bee91169SAlexander Graf void efi_save_gd(void)
101bee91169SAlexander Graf {
10265e4c0b1SSimon Glass #ifdef CONFIG_ARM
103bee91169SAlexander Graf 	efi_gd = gd;
10465e4c0b1SSimon Glass #endif
105bee91169SAlexander Graf }
106bee91169SAlexander Graf 
107c160d2f5SRob Clark /*
10878a88f79SMario Six  * Special case handler for error/abort that just forces things back to u-boot
109b72aaa87SHeinrich Schuchardt  * world so we can dump out an abort message, without any care about returning
110b72aaa87SHeinrich Schuchardt  * back to UEFI world.
111c160d2f5SRob Clark  */
efi_restore_gd(void)112bee91169SAlexander Graf void efi_restore_gd(void)
113bee91169SAlexander Graf {
11465e4c0b1SSimon Glass #ifdef CONFIG_ARM
115bee91169SAlexander Graf 	/* Only restore if we're already in EFI context */
116bee91169SAlexander Graf 	if (!efi_gd)
117bee91169SAlexander Graf 		return;
118bee91169SAlexander Graf 	gd = efi_gd;
11965e4c0b1SSimon Glass #endif
120bee91169SAlexander Graf }
121bee91169SAlexander Graf 
1226b03cd10SHeinrich Schuchardt /**
12378a88f79SMario Six  * indent_string() - returns a string for indenting with two spaces per level
12478a88f79SMario Six  * @level: indent level
125c8df80c5SHeinrich Schuchardt  *
1266b03cd10SHeinrich Schuchardt  * A maximum of ten indent levels is supported. Higher indent levels will be
1276b03cd10SHeinrich Schuchardt  * truncated.
1286b03cd10SHeinrich Schuchardt  *
12978a88f79SMario Six  * Return: A string for indenting with two spaces per level is
1306b03cd10SHeinrich Schuchardt  *         returned.
131af65db85SRob Clark  */
indent_string(int level)132af65db85SRob Clark static const char *indent_string(int level)
133af65db85SRob Clark {
134af65db85SRob Clark 	const char *indent = "                    ";
135af65db85SRob Clark 	const int max = strlen(indent);
136ab9efa97SHeinrich Schuchardt 
137af65db85SRob Clark 	level = min(max, level * 2);
138af65db85SRob Clark 	return &indent[max - level];
139af65db85SRob Clark }
140af65db85SRob Clark 
__efi_nesting(void)141ae0bd3a9SHeinrich Schuchardt const char *__efi_nesting(void)
142ae0bd3a9SHeinrich Schuchardt {
143ae0bd3a9SHeinrich Schuchardt 	return indent_string(nesting_level);
144ae0bd3a9SHeinrich Schuchardt }
145ae0bd3a9SHeinrich Schuchardt 
__efi_nesting_inc(void)146af65db85SRob Clark const char *__efi_nesting_inc(void)
147af65db85SRob Clark {
148af65db85SRob Clark 	return indent_string(nesting_level++);
149af65db85SRob Clark }
150af65db85SRob Clark 
__efi_nesting_dec(void)151af65db85SRob Clark const char *__efi_nesting_dec(void)
152af65db85SRob Clark {
153af65db85SRob Clark 	return indent_string(--nesting_level);
154af65db85SRob Clark }
155af65db85SRob Clark 
1566b03cd10SHeinrich Schuchardt /**
15778a88f79SMario Six  * efi_queue_event() - queue an EFI event
15878a88f79SMario Six  * @event:     event to signal
15978a88f79SMario Six  * @check_tpl: check the TPL level
160332468f7SHeinrich Schuchardt  *
161332468f7SHeinrich Schuchardt  * This function queues the notification function of the event for future
162332468f7SHeinrich Schuchardt  * execution.
163332468f7SHeinrich Schuchardt  *
16478a88f79SMario Six  * The notification function is called if the task priority level of the event
16578a88f79SMario Six  * is higher than the current task priority level.
166332468f7SHeinrich Schuchardt  *
167332468f7SHeinrich Schuchardt  * For the SignalEvent service see efi_signal_event_ext.
168332468f7SHeinrich Schuchardt  *
169332468f7SHeinrich Schuchardt  */
efi_queue_event(struct efi_event * event,bool check_tpl)170b095f3c8SHeinrich Schuchardt static void efi_queue_event(struct efi_event *event, bool check_tpl)
171c6841592Sxypron.glpk@gmx.de {
172ca62a4f5SHeinrich Schuchardt 	if (event->notify_function) {
173e190e897SHeinrich Schuchardt 		event->is_queued = true;
17432f4b2f8SHeinrich Schuchardt 		/* Check TPL */
1759bc9664dSHeinrich Schuchardt 		if (check_tpl && efi_tpl >= event->notify_tpl)
17632f4b2f8SHeinrich Schuchardt 			return;
177ea630ce9SHeinrich Schuchardt 		EFI_CALL_VOID(event->notify_function(event,
178ea630ce9SHeinrich Schuchardt 						     event->notify_context));
179c6841592Sxypron.glpk@gmx.de 	}
180e190e897SHeinrich Schuchardt 	event->is_queued = false;
181c6841592Sxypron.glpk@gmx.de }
182c6841592Sxypron.glpk@gmx.de 
1836b03cd10SHeinrich Schuchardt /**
18421b3edfcSHeinrich Schuchardt  * is_valid_tpl() - check if the task priority level is valid
18521b3edfcSHeinrich Schuchardt  *
18621b3edfcSHeinrich Schuchardt  * @tpl:		TPL level to check
187b72aaa87SHeinrich Schuchardt  * Return:		status code
18821b3edfcSHeinrich Schuchardt  */
is_valid_tpl(efi_uintn_t tpl)18921b3edfcSHeinrich Schuchardt efi_status_t is_valid_tpl(efi_uintn_t tpl)
19021b3edfcSHeinrich Schuchardt {
19121b3edfcSHeinrich Schuchardt 	switch (tpl) {
19221b3edfcSHeinrich Schuchardt 	case TPL_APPLICATION:
19321b3edfcSHeinrich Schuchardt 	case TPL_CALLBACK:
19421b3edfcSHeinrich Schuchardt 	case TPL_NOTIFY:
19521b3edfcSHeinrich Schuchardt 	case TPL_HIGH_LEVEL:
19621b3edfcSHeinrich Schuchardt 		return EFI_SUCCESS;
19721b3edfcSHeinrich Schuchardt 	default:
19821b3edfcSHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
19921b3edfcSHeinrich Schuchardt 	}
20021b3edfcSHeinrich Schuchardt }
20121b3edfcSHeinrich Schuchardt 
20221b3edfcSHeinrich Schuchardt /**
20378a88f79SMario Six  * efi_signal_event() - signal an EFI event
20478a88f79SMario Six  * @event:     event to signal
20578a88f79SMario Six  * @check_tpl: check the TPL level
206b095f3c8SHeinrich Schuchardt  *
20778a88f79SMario Six  * This function signals an event. If the event belongs to an event group all
20878a88f79SMario Six  * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
209b095f3c8SHeinrich Schuchardt  * their notification function is queued.
210b095f3c8SHeinrich Schuchardt  *
211b095f3c8SHeinrich Schuchardt  * For the SignalEvent service see efi_signal_event_ext.
212b095f3c8SHeinrich Schuchardt  */
efi_signal_event(struct efi_event * event,bool check_tpl)213b095f3c8SHeinrich Schuchardt void efi_signal_event(struct efi_event *event, bool check_tpl)
214b095f3c8SHeinrich Schuchardt {
215b095f3c8SHeinrich Schuchardt 	if (event->group) {
216b095f3c8SHeinrich Schuchardt 		struct efi_event *evt;
217b095f3c8SHeinrich Schuchardt 
218b095f3c8SHeinrich Schuchardt 		/*
219b095f3c8SHeinrich Schuchardt 		 * The signaled state has to set before executing any
220b095f3c8SHeinrich Schuchardt 		 * notification function
221b095f3c8SHeinrich Schuchardt 		 */
222b095f3c8SHeinrich Schuchardt 		list_for_each_entry(evt, &efi_events, link) {
223b095f3c8SHeinrich Schuchardt 			if (!evt->group || guidcmp(evt->group, event->group))
224b095f3c8SHeinrich Schuchardt 				continue;
225b095f3c8SHeinrich Schuchardt 			if (evt->is_signaled)
226b095f3c8SHeinrich Schuchardt 				continue;
227b095f3c8SHeinrich Schuchardt 			evt->is_signaled = true;
228b095f3c8SHeinrich Schuchardt 			if (evt->type & EVT_NOTIFY_SIGNAL &&
229b095f3c8SHeinrich Schuchardt 			    evt->notify_function)
230b095f3c8SHeinrich Schuchardt 				evt->is_queued = true;
231b095f3c8SHeinrich Schuchardt 		}
232b095f3c8SHeinrich Schuchardt 		list_for_each_entry(evt, &efi_events, link) {
233b095f3c8SHeinrich Schuchardt 			if (!evt->group || guidcmp(evt->group, event->group))
234b095f3c8SHeinrich Schuchardt 				continue;
235b095f3c8SHeinrich Schuchardt 			if (evt->is_queued)
236b095f3c8SHeinrich Schuchardt 				efi_queue_event(evt, check_tpl);
237b095f3c8SHeinrich Schuchardt 		}
238b095f3c8SHeinrich Schuchardt 	} else if (!event->is_signaled) {
239b095f3c8SHeinrich Schuchardt 		event->is_signaled = true;
240b095f3c8SHeinrich Schuchardt 		if (event->type & EVT_NOTIFY_SIGNAL)
241b095f3c8SHeinrich Schuchardt 			efi_queue_event(event, check_tpl);
242b095f3c8SHeinrich Schuchardt 	}
243b095f3c8SHeinrich Schuchardt }
244b095f3c8SHeinrich Schuchardt 
2456b03cd10SHeinrich Schuchardt /**
24678a88f79SMario Six  * efi_raise_tpl() - raise the task priority level
24778a88f79SMario Six  * @new_tpl: new value of the task priority level
248332468f7SHeinrich Schuchardt  *
249332468f7SHeinrich Schuchardt  * This function implements the RaiseTpl service.
250332468f7SHeinrich Schuchardt  *
25178a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
25278a88f79SMario Six  * details.
25378a88f79SMario Six  *
25478a88f79SMario Six  * Return: old value of the task priority level
255332468f7SHeinrich Schuchardt  */
efi_raise_tpl(efi_uintn_t new_tpl)256152cade3SHeinrich Schuchardt static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
257bee91169SAlexander Graf {
258152cade3SHeinrich Schuchardt 	efi_uintn_t old_tpl = efi_tpl;
25932f4b2f8SHeinrich Schuchardt 
260503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", new_tpl);
26132f4b2f8SHeinrich Schuchardt 
26232f4b2f8SHeinrich Schuchardt 	if (new_tpl < efi_tpl)
26332f4b2f8SHeinrich Schuchardt 		debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
26432f4b2f8SHeinrich Schuchardt 	efi_tpl = new_tpl;
26532f4b2f8SHeinrich Schuchardt 	if (efi_tpl > TPL_HIGH_LEVEL)
26632f4b2f8SHeinrich Schuchardt 		efi_tpl = TPL_HIGH_LEVEL;
26732f4b2f8SHeinrich Schuchardt 
26832f4b2f8SHeinrich Schuchardt 	EFI_EXIT(EFI_SUCCESS);
26932f4b2f8SHeinrich Schuchardt 	return old_tpl;
270bee91169SAlexander Graf }
271bee91169SAlexander Graf 
2726b03cd10SHeinrich Schuchardt /**
27378a88f79SMario Six  * efi_restore_tpl() - lower the task priority level
27478a88f79SMario Six  * @old_tpl: value of the task priority level to be restored
275332468f7SHeinrich Schuchardt  *
276332468f7SHeinrich Schuchardt  * This function implements the RestoreTpl service.
277332468f7SHeinrich Schuchardt  *
27878a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
27978a88f79SMario Six  * details.
280332468f7SHeinrich Schuchardt  */
efi_restore_tpl(efi_uintn_t old_tpl)281152cade3SHeinrich Schuchardt static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
282bee91169SAlexander Graf {
283503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", old_tpl);
28432f4b2f8SHeinrich Schuchardt 
28532f4b2f8SHeinrich Schuchardt 	if (old_tpl > efi_tpl)
28632f4b2f8SHeinrich Schuchardt 		debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
28732f4b2f8SHeinrich Schuchardt 	efi_tpl = old_tpl;
28832f4b2f8SHeinrich Schuchardt 	if (efi_tpl > TPL_HIGH_LEVEL)
28932f4b2f8SHeinrich Schuchardt 		efi_tpl = TPL_HIGH_LEVEL;
29032f4b2f8SHeinrich Schuchardt 
2910f7fcc72SHeinrich Schuchardt 	/*
2920f7fcc72SHeinrich Schuchardt 	 * Lowering the TPL may have made queued events eligible for execution.
2930f7fcc72SHeinrich Schuchardt 	 */
2940f7fcc72SHeinrich Schuchardt 	efi_timer_check();
2950f7fcc72SHeinrich Schuchardt 
29632f4b2f8SHeinrich Schuchardt 	EFI_EXIT(EFI_SUCCESS);
297bee91169SAlexander Graf }
298bee91169SAlexander Graf 
2996b03cd10SHeinrich Schuchardt /**
30078a88f79SMario Six  * efi_allocate_pages_ext() - allocate memory pages
3016b03cd10SHeinrich Schuchardt  * @type:        type of allocation to be performed
3026b03cd10SHeinrich Schuchardt  * @memory_type: usage type of the allocated memory
3036b03cd10SHeinrich Schuchardt  * @pages:       number of pages to be allocated
3046b03cd10SHeinrich Schuchardt  * @memory:      allocated memory
30578a88f79SMario Six  *
30678a88f79SMario Six  * This function implements the AllocatePages service.
30778a88f79SMario Six  *
30878a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
30978a88f79SMario Six  * details.
31078a88f79SMario Six  *
31178a88f79SMario Six  * Return: status code
312332468f7SHeinrich Schuchardt  */
efi_allocate_pages_ext(int type,int memory_type,efi_uintn_t pages,uint64_t * memory)3136e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
314f5a2a938SHeinrich Schuchardt 						  efi_uintn_t pages,
315bee91169SAlexander Graf 						  uint64_t *memory)
316bee91169SAlexander Graf {
317bee91169SAlexander Graf 	efi_status_t r;
318bee91169SAlexander Graf 
319f5a2a938SHeinrich Schuchardt 	EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
320bee91169SAlexander Graf 	r = efi_allocate_pages(type, memory_type, pages, memory);
321bee91169SAlexander Graf 	return EFI_EXIT(r);
322bee91169SAlexander Graf }
323bee91169SAlexander Graf 
3246b03cd10SHeinrich Schuchardt /**
32578a88f79SMario Six  * efi_free_pages_ext() - Free memory pages.
3266b03cd10SHeinrich Schuchardt  * @memory: start of the memory area to be freed
3276b03cd10SHeinrich Schuchardt  * @pages:  number of pages to be freed
32878a88f79SMario Six  *
32978a88f79SMario Six  * This function implements the FreePages service.
33078a88f79SMario Six  *
33178a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
33278a88f79SMario Six  * details.
33378a88f79SMario Six  *
33478a88f79SMario Six  * Return: status code
335332468f7SHeinrich Schuchardt  */
efi_free_pages_ext(uint64_t memory,efi_uintn_t pages)3366e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
337f5a2a938SHeinrich Schuchardt 					      efi_uintn_t pages)
338bee91169SAlexander Graf {
339bee91169SAlexander Graf 	efi_status_t r;
340bee91169SAlexander Graf 
341dee37fc9SMasahiro Yamada 	EFI_ENTRY("%llx, 0x%zx", memory, pages);
342bee91169SAlexander Graf 	r = efi_free_pages(memory, pages);
343bee91169SAlexander Graf 	return EFI_EXIT(r);
344bee91169SAlexander Graf }
345bee91169SAlexander Graf 
3466b03cd10SHeinrich Schuchardt /**
34778a88f79SMario Six  * efi_get_memory_map_ext() - get map describing memory usage
3486b03cd10SHeinrich Schuchardt  * @memory_map_size:    on entry the size, in bytes, of the memory map buffer,
349332468f7SHeinrich Schuchardt  *                      on exit the size of the copied memory map
3506b03cd10SHeinrich Schuchardt  * @memory_map:         buffer to which the memory map is written
3516b03cd10SHeinrich Schuchardt  * @map_key:            key for the memory map
3526b03cd10SHeinrich Schuchardt  * @descriptor_size:    size of an individual memory descriptor
3536b03cd10SHeinrich Schuchardt  * @descriptor_version: version number of the memory descriptor structure
35478a88f79SMario Six  *
35578a88f79SMario Six  * This function implements the GetMemoryMap service.
35678a88f79SMario Six  *
35778a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
35878a88f79SMario Six  * details.
35978a88f79SMario Six  *
36078a88f79SMario Six  * Return: status code
361332468f7SHeinrich Schuchardt  */
efi_get_memory_map_ext(efi_uintn_t * memory_map_size,struct efi_mem_desc * memory_map,efi_uintn_t * map_key,efi_uintn_t * descriptor_size,uint32_t * descriptor_version)3626e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
363f5a2a938SHeinrich Schuchardt 					efi_uintn_t *memory_map_size,
364bee91169SAlexander Graf 					struct efi_mem_desc *memory_map,
365f5a2a938SHeinrich Schuchardt 					efi_uintn_t *map_key,
366f5a2a938SHeinrich Schuchardt 					efi_uintn_t *descriptor_size,
367bee91169SAlexander Graf 					uint32_t *descriptor_version)
368bee91169SAlexander Graf {
369bee91169SAlexander Graf 	efi_status_t r;
370bee91169SAlexander Graf 
371bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
372bee91169SAlexander Graf 		  map_key, descriptor_size, descriptor_version);
373bee91169SAlexander Graf 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
374bee91169SAlexander Graf 			       descriptor_size, descriptor_version);
375bee91169SAlexander Graf 	return EFI_EXIT(r);
376bee91169SAlexander Graf }
377bee91169SAlexander Graf 
3786b03cd10SHeinrich Schuchardt /**
37978a88f79SMario Six  * efi_allocate_pool_ext() - allocate memory from pool
3806b03cd10SHeinrich Schuchardt  * @pool_type: type of the pool from which memory is to be allocated
3816b03cd10SHeinrich Schuchardt  * @size:      number of bytes to be allocated
3826b03cd10SHeinrich Schuchardt  * @buffer:    allocated memory
38378a88f79SMario Six  *
38478a88f79SMario Six  * This function implements the AllocatePool service.
38578a88f79SMario Six  *
38678a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
38778a88f79SMario Six  * details.
38878a88f79SMario Six  *
38978a88f79SMario Six  * Return: status code
390332468f7SHeinrich Schuchardt  */
efi_allocate_pool_ext(int pool_type,efi_uintn_t size,void ** buffer)391ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
392f5a2a938SHeinrich Schuchardt 						 efi_uintn_t size,
393bee91169SAlexander Graf 						 void **buffer)
394bee91169SAlexander Graf {
3951cd29f0aSAlexander Graf 	efi_status_t r;
3961cd29f0aSAlexander Graf 
397f5a2a938SHeinrich Schuchardt 	EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
398ead1274bSStefan Brüns 	r = efi_allocate_pool(pool_type, size, buffer);
3991cd29f0aSAlexander Graf 	return EFI_EXIT(r);
400bee91169SAlexander Graf }
401bee91169SAlexander Graf 
4026b03cd10SHeinrich Schuchardt /**
40378a88f79SMario Six  * efi_free_pool_ext() - free memory from pool
40478a88f79SMario Six  * @buffer: start of memory to be freed
405332468f7SHeinrich Schuchardt  *
406332468f7SHeinrich Schuchardt  * This function implements the FreePool service.
407332468f7SHeinrich Schuchardt  *
40878a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
40978a88f79SMario Six  * details.
41078a88f79SMario Six  *
41178a88f79SMario Six  * Return: status code
412332468f7SHeinrich Schuchardt  */
efi_free_pool_ext(void * buffer)41342417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
414bee91169SAlexander Graf {
4151cd29f0aSAlexander Graf 	efi_status_t r;
4161cd29f0aSAlexander Graf 
4171cd29f0aSAlexander Graf 	EFI_ENTRY("%p", buffer);
41842417bc8SStefan Brüns 	r = efi_free_pool(buffer);
4191cd29f0aSAlexander Graf 	return EFI_EXIT(r);
420bee91169SAlexander Graf }
421bee91169SAlexander Graf 
4226b03cd10SHeinrich Schuchardt /**
42378a88f79SMario Six  * efi_add_handle() - add a new object to the object list
4246b03cd10SHeinrich Schuchardt  * @obj: object to be added
42578a88f79SMario Six  *
42678a88f79SMario Six  * The protocols list is initialized. The object handle is set.
42744549d62SHeinrich Schuchardt  */
efi_add_handle(efi_handle_t handle)428fae0118eSHeinrich Schuchardt void efi_add_handle(efi_handle_t handle)
42944549d62SHeinrich Schuchardt {
430fae0118eSHeinrich Schuchardt 	if (!handle)
43144549d62SHeinrich Schuchardt 		return;
432fae0118eSHeinrich Schuchardt 	INIT_LIST_HEAD(&handle->protocols);
433fae0118eSHeinrich Schuchardt 	list_add_tail(&handle->link, &efi_obj_list);
43444549d62SHeinrich Schuchardt }
43544549d62SHeinrich Schuchardt 
4366b03cd10SHeinrich Schuchardt /**
43778a88f79SMario Six  * efi_create_handle() - create handle
4386b03cd10SHeinrich Schuchardt  * @handle: new handle
43978a88f79SMario Six  *
44078a88f79SMario Six  * Return: status code
4412edab5e2SHeinrich Schuchardt  */
efi_create_handle(efi_handle_t * handle)4422074f700SHeinrich Schuchardt efi_status_t efi_create_handle(efi_handle_t *handle)
4433cc6e3feSHeinrich Schuchardt {
4443cc6e3feSHeinrich Schuchardt 	struct efi_object *obj;
4453cc6e3feSHeinrich Schuchardt 
446d29e7824SHeinrich Schuchardt 	obj = calloc(1, sizeof(struct efi_object));
447d29e7824SHeinrich Schuchardt 	if (!obj)
448d29e7824SHeinrich Schuchardt 		return EFI_OUT_OF_RESOURCES;
449d29e7824SHeinrich Schuchardt 
45044549d62SHeinrich Schuchardt 	efi_add_handle(obj);
451fae0118eSHeinrich Schuchardt 	*handle = obj;
452d29e7824SHeinrich Schuchardt 
453d29e7824SHeinrich Schuchardt 	return EFI_SUCCESS;
4543cc6e3feSHeinrich Schuchardt }
4553cc6e3feSHeinrich Schuchardt 
4566b03cd10SHeinrich Schuchardt /**
45778a88f79SMario Six  * efi_search_protocol() - find a protocol on a handle.
4586b03cd10SHeinrich Schuchardt  * @handle:        handle
4596b03cd10SHeinrich Schuchardt  * @protocol_guid: GUID of the protocol
4606b03cd10SHeinrich Schuchardt  * @handler:       reference to the protocol
46178a88f79SMario Six  *
46278a88f79SMario Six  * Return: status code
463678e03a0SHeinrich Schuchardt  */
efi_search_protocol(const efi_handle_t handle,const efi_guid_t * protocol_guid,struct efi_handler ** handler)4642074f700SHeinrich Schuchardt efi_status_t efi_search_protocol(const efi_handle_t handle,
465678e03a0SHeinrich Schuchardt 				 const efi_guid_t *protocol_guid,
466678e03a0SHeinrich Schuchardt 				 struct efi_handler **handler)
467678e03a0SHeinrich Schuchardt {
468678e03a0SHeinrich Schuchardt 	struct efi_object *efiobj;
469678e03a0SHeinrich Schuchardt 	struct list_head *lhandle;
470678e03a0SHeinrich Schuchardt 
471678e03a0SHeinrich Schuchardt 	if (!handle || !protocol_guid)
472678e03a0SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
473678e03a0SHeinrich Schuchardt 	efiobj = efi_search_obj(handle);
474678e03a0SHeinrich Schuchardt 	if (!efiobj)
475678e03a0SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
476678e03a0SHeinrich Schuchardt 	list_for_each(lhandle, &efiobj->protocols) {
477678e03a0SHeinrich Schuchardt 		struct efi_handler *protocol;
478678e03a0SHeinrich Schuchardt 
479678e03a0SHeinrich Schuchardt 		protocol = list_entry(lhandle, struct efi_handler, link);
480678e03a0SHeinrich Schuchardt 		if (!guidcmp(protocol->guid, protocol_guid)) {
481678e03a0SHeinrich Schuchardt 			if (handler)
482678e03a0SHeinrich Schuchardt 				*handler = protocol;
483678e03a0SHeinrich Schuchardt 			return EFI_SUCCESS;
484678e03a0SHeinrich Schuchardt 		}
485678e03a0SHeinrich Schuchardt 	}
486678e03a0SHeinrich Schuchardt 	return EFI_NOT_FOUND;
487678e03a0SHeinrich Schuchardt }
488678e03a0SHeinrich Schuchardt 
4896b03cd10SHeinrich Schuchardt /**
49078a88f79SMario Six  * efi_remove_protocol() - delete protocol from a handle
4916b03cd10SHeinrich Schuchardt  * @handle:             handle from which the protocol shall be deleted
4926b03cd10SHeinrich Schuchardt  * @protocol:           GUID of the protocol to be deleted
4936b03cd10SHeinrich Schuchardt  * @protocol_interface: interface of the protocol implementation
49478a88f79SMario Six  *
49578a88f79SMario Six  * Return: status code
496678e03a0SHeinrich Schuchardt  */
efi_remove_protocol(const efi_handle_t handle,const efi_guid_t * protocol,void * protocol_interface)4972074f700SHeinrich Schuchardt efi_status_t efi_remove_protocol(const efi_handle_t handle,
4982074f700SHeinrich Schuchardt 				 const efi_guid_t *protocol,
499678e03a0SHeinrich Schuchardt 				 void *protocol_interface)
500678e03a0SHeinrich Schuchardt {
501678e03a0SHeinrich Schuchardt 	struct efi_handler *handler;
502678e03a0SHeinrich Schuchardt 	efi_status_t ret;
503678e03a0SHeinrich Schuchardt 
504678e03a0SHeinrich Schuchardt 	ret = efi_search_protocol(handle, protocol, &handler);
505678e03a0SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
506678e03a0SHeinrich Schuchardt 		return ret;
507678e03a0SHeinrich Schuchardt 	if (guidcmp(handler->guid, protocol))
508678e03a0SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
5091f470e17SHeinrich Schuchardt 	if (handler->protocol_interface != protocol_interface)
5101f470e17SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
511678e03a0SHeinrich Schuchardt 	list_del(&handler->link);
512678e03a0SHeinrich Schuchardt 	free(handler);
513678e03a0SHeinrich Schuchardt 	return EFI_SUCCESS;
514678e03a0SHeinrich Schuchardt }
515678e03a0SHeinrich Schuchardt 
5166b03cd10SHeinrich Schuchardt /**
51778a88f79SMario Six  * efi_remove_all_protocols() - delete all protocols from a handle
5186b03cd10SHeinrich Schuchardt  * @handle: handle from which the protocols shall be deleted
51978a88f79SMario Six  *
52078a88f79SMario Six  * Return: status code
521678e03a0SHeinrich Schuchardt  */
efi_remove_all_protocols(const efi_handle_t handle)5222074f700SHeinrich Schuchardt efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
523678e03a0SHeinrich Schuchardt {
524678e03a0SHeinrich Schuchardt 	struct efi_object *efiobj;
52532e6fed6SHeinrich Schuchardt 	struct efi_handler *protocol;
52632e6fed6SHeinrich Schuchardt 	struct efi_handler *pos;
527678e03a0SHeinrich Schuchardt 
528678e03a0SHeinrich Schuchardt 	efiobj = efi_search_obj(handle);
529678e03a0SHeinrich Schuchardt 	if (!efiobj)
530678e03a0SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
53132e6fed6SHeinrich Schuchardt 	list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
532678e03a0SHeinrich Schuchardt 		efi_status_t ret;
533678e03a0SHeinrich Schuchardt 
534678e03a0SHeinrich Schuchardt 		ret = efi_remove_protocol(handle, protocol->guid,
535678e03a0SHeinrich Schuchardt 					  protocol->protocol_interface);
536678e03a0SHeinrich Schuchardt 		if (ret != EFI_SUCCESS)
537678e03a0SHeinrich Schuchardt 			return ret;
538678e03a0SHeinrich Schuchardt 	}
539678e03a0SHeinrich Schuchardt 	return EFI_SUCCESS;
540678e03a0SHeinrich Schuchardt }
541678e03a0SHeinrich Schuchardt 
5426b03cd10SHeinrich Schuchardt /**
54378a88f79SMario Six  * efi_delete_handle() - delete handle
544678e03a0SHeinrich Schuchardt  *
5456b03cd10SHeinrich Schuchardt  * @obj: handle to delete
546678e03a0SHeinrich Schuchardt  */
efi_delete_handle(efi_handle_t handle)547fae0118eSHeinrich Schuchardt void efi_delete_handle(efi_handle_t handle)
548678e03a0SHeinrich Schuchardt {
549fae0118eSHeinrich Schuchardt 	if (!handle)
550678e03a0SHeinrich Schuchardt 		return;
551fae0118eSHeinrich Schuchardt 	efi_remove_all_protocols(handle);
552fae0118eSHeinrich Schuchardt 	list_del(&handle->link);
553fae0118eSHeinrich Schuchardt 	free(handle);
554678e03a0SHeinrich Schuchardt }
555678e03a0SHeinrich Schuchardt 
5566b03cd10SHeinrich Schuchardt /**
55778a88f79SMario Six  * efi_is_event() - check if a pointer is a valid event
5586b03cd10SHeinrich Schuchardt  * @event: pointer to check
55978a88f79SMario Six  *
56078a88f79SMario Six  * Return: status code
561bee91169SAlexander Graf  */
efi_is_event(const struct efi_event * event)56243bce442SHeinrich Schuchardt static efi_status_t efi_is_event(const struct efi_event *event)
56343bce442SHeinrich Schuchardt {
56443bce442SHeinrich Schuchardt 	const struct efi_event *evt;
56543bce442SHeinrich Schuchardt 
56643bce442SHeinrich Schuchardt 	if (!event)
56743bce442SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
56843bce442SHeinrich Schuchardt 	list_for_each_entry(evt, &efi_events, link) {
56943bce442SHeinrich Schuchardt 		if (evt == event)
57043bce442SHeinrich Schuchardt 			return EFI_SUCCESS;
57143bce442SHeinrich Schuchardt 	}
57243bce442SHeinrich Schuchardt 	return EFI_INVALID_PARAMETER;
57343bce442SHeinrich Schuchardt }
574bee91169SAlexander Graf 
5756b03cd10SHeinrich Schuchardt /**
57678a88f79SMario Six  * efi_create_event() - create an event
5776b03cd10SHeinrich Schuchardt  * @type:            type of the event to create
5786b03cd10SHeinrich Schuchardt  * @notify_tpl:      task priority level of the event
5796b03cd10SHeinrich Schuchardt  * @notify_function: notification function of the event
5806b03cd10SHeinrich Schuchardt  * @notify_context:  pointer passed to the notification function
5816b03cd10SHeinrich Schuchardt  * @group:           event group
5826b03cd10SHeinrich Schuchardt  * @event:           created event
58378a88f79SMario Six  *
58478a88f79SMario Six  * This function is used inside U-Boot code to create an event.
58578a88f79SMario Six  *
58678a88f79SMario Six  * For the API function implementing the CreateEvent service see
58778a88f79SMario Six  * efi_create_event_ext.
58878a88f79SMario Six  *
58978a88f79SMario Six  * Return: status code
590332468f7SHeinrich Schuchardt  */
efi_create_event(uint32_t type,efi_uintn_t notify_tpl,void (EFIAPI * notify_function)(struct efi_event * event,void * context),void * notify_context,efi_guid_t * group,struct efi_event ** event)591152cade3SHeinrich Schuchardt efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
5922fd945feSxypron.glpk@gmx.de 			      void (EFIAPI *notify_function) (
5932fd945feSxypron.glpk@gmx.de 					struct efi_event *event,
594e275458cSSimon Glass 					void *context),
595b095f3c8SHeinrich Schuchardt 			      void *notify_context, efi_guid_t *group,
596b095f3c8SHeinrich Schuchardt 			      struct efi_event **event)
597bee91169SAlexander Graf {
59843bce442SHeinrich Schuchardt 	struct efi_event *evt;
599c6841592Sxypron.glpk@gmx.de 
600a95343b8SJonathan Gray 	if (event == NULL)
60149deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
602a95343b8SJonathan Gray 
60321b3edfcSHeinrich Schuchardt 	switch (type) {
60421b3edfcSHeinrich Schuchardt 	case 0:
60521b3edfcSHeinrich Schuchardt 	case EVT_TIMER:
60621b3edfcSHeinrich Schuchardt 	case EVT_NOTIFY_SIGNAL:
60721b3edfcSHeinrich Schuchardt 	case EVT_TIMER | EVT_NOTIFY_SIGNAL:
60821b3edfcSHeinrich Schuchardt 	case EVT_NOTIFY_WAIT:
60921b3edfcSHeinrich Schuchardt 	case EVT_TIMER | EVT_NOTIFY_WAIT:
61021b3edfcSHeinrich Schuchardt 	case EVT_SIGNAL_EXIT_BOOT_SERVICES:
61121b3edfcSHeinrich Schuchardt 	case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
61221b3edfcSHeinrich Schuchardt 		break;
61321b3edfcSHeinrich Schuchardt 	default:
61449deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
61521b3edfcSHeinrich Schuchardt 	}
616a95343b8SJonathan Gray 
6173748ed90SAKASHI Takahiro 	if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
6183748ed90SAKASHI Takahiro 	    (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
61949deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
620a95343b8SJonathan Gray 
62143bce442SHeinrich Schuchardt 	evt = calloc(1, sizeof(struct efi_event));
62243bce442SHeinrich Schuchardt 	if (!evt)
62349deb455Sxypron.glpk@gmx.de 		return EFI_OUT_OF_RESOURCES;
62443bce442SHeinrich Schuchardt 	evt->type = type;
62543bce442SHeinrich Schuchardt 	evt->notify_tpl = notify_tpl;
62643bce442SHeinrich Schuchardt 	evt->notify_function = notify_function;
62743bce442SHeinrich Schuchardt 	evt->notify_context = notify_context;
628b095f3c8SHeinrich Schuchardt 	evt->group = group;
62943bce442SHeinrich Schuchardt 	/* Disable timers on boot up */
63043bce442SHeinrich Schuchardt 	evt->trigger_next = -1ULL;
63143bce442SHeinrich Schuchardt 	evt->is_queued = false;
63243bce442SHeinrich Schuchardt 	evt->is_signaled = false;
63343bce442SHeinrich Schuchardt 	list_add_tail(&evt->link, &efi_events);
63443bce442SHeinrich Schuchardt 	*event = evt;
63543bce442SHeinrich Schuchardt 	return EFI_SUCCESS;
636c6841592Sxypron.glpk@gmx.de }
637bee91169SAlexander Graf 
638332468f7SHeinrich Schuchardt /*
63978a88f79SMario Six  * efi_create_event_ex() - create an event in a group
6406b03cd10SHeinrich Schuchardt  * @type:            type of the event to create
6416b03cd10SHeinrich Schuchardt  * @notify_tpl:      task priority level of the event
6426b03cd10SHeinrich Schuchardt  * @notify_function: notification function of the event
6436b03cd10SHeinrich Schuchardt  * @notify_context:  pointer passed to the notification function
6446b03cd10SHeinrich Schuchardt  * @event:           created event
6456b03cd10SHeinrich Schuchardt  * @event_group:     event group
64678a88f79SMario Six  *
64778a88f79SMario Six  * This function implements the CreateEventEx service.
64878a88f79SMario Six  *
64978a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
65078a88f79SMario Six  * details.
65178a88f79SMario Six  *
65278a88f79SMario Six  * Return: status code
6539f0930e5SHeinrich Schuchardt  */
efi_create_event_ex(uint32_t type,efi_uintn_t notify_tpl,void (EFIAPI * notify_function)(struct efi_event * event,void * context),void * notify_context,efi_guid_t * event_group,struct efi_event ** event)6549f0930e5SHeinrich Schuchardt efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
6559f0930e5SHeinrich Schuchardt 					void (EFIAPI *notify_function) (
6569f0930e5SHeinrich Schuchardt 							struct efi_event *event,
6579f0930e5SHeinrich Schuchardt 							void *context),
6589f0930e5SHeinrich Schuchardt 					void *notify_context,
6599f0930e5SHeinrich Schuchardt 					efi_guid_t *event_group,
6609f0930e5SHeinrich Schuchardt 					struct efi_event **event)
6619f0930e5SHeinrich Schuchardt {
6629f0930e5SHeinrich Schuchardt 	EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
6639f0930e5SHeinrich Schuchardt 		  notify_context, event_group);
6649f0930e5SHeinrich Schuchardt 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
665b095f3c8SHeinrich Schuchardt 					 notify_context, event_group, event));
6669f0930e5SHeinrich Schuchardt }
6679f0930e5SHeinrich Schuchardt 
6686b03cd10SHeinrich Schuchardt /**
66978a88f79SMario Six  * efi_create_event_ext() - create an event
6706b03cd10SHeinrich Schuchardt  * @type:            type of the event to create
6716b03cd10SHeinrich Schuchardt  * @notify_tpl:      task priority level of the event
6726b03cd10SHeinrich Schuchardt  * @notify_function: notification function of the event
6736b03cd10SHeinrich Schuchardt  * @notify_context:  pointer passed to the notification function
6746b03cd10SHeinrich Schuchardt  * @event:           created event
67578a88f79SMario Six  *
67678a88f79SMario Six  * This function implements the CreateEvent service.
67778a88f79SMario Six  *
67878a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
67978a88f79SMario Six  * details.
68078a88f79SMario Six  *
68178a88f79SMario Six  * Return: status code
682332468f7SHeinrich Schuchardt  */
efi_create_event_ext(uint32_t type,efi_uintn_t notify_tpl,void (EFIAPI * notify_function)(struct efi_event * event,void * context),void * notify_context,struct efi_event ** event)68349deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
684152cade3SHeinrich Schuchardt 			uint32_t type, efi_uintn_t notify_tpl,
68549deb455Sxypron.glpk@gmx.de 			void (EFIAPI *notify_function) (
68649deb455Sxypron.glpk@gmx.de 					struct efi_event *event,
68749deb455Sxypron.glpk@gmx.de 					void *context),
68849deb455Sxypron.glpk@gmx.de 			void *notify_context, struct efi_event **event)
68949deb455Sxypron.glpk@gmx.de {
69049deb455Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
69149deb455Sxypron.glpk@gmx.de 		  notify_context);
69249deb455Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
693b095f3c8SHeinrich Schuchardt 					 notify_context, NULL, event));
69449deb455Sxypron.glpk@gmx.de }
69549deb455Sxypron.glpk@gmx.de 
6966b03cd10SHeinrich Schuchardt /**
69778a88f79SMario Six  * efi_timer_check() - check if a timer event has occurred
6986b03cd10SHeinrich Schuchardt  *
699332468f7SHeinrich Schuchardt  * Check if a timer event has occurred or a queued notification function should
700332468f7SHeinrich Schuchardt  * be called.
701332468f7SHeinrich Schuchardt  *
702bee91169SAlexander Graf  * Our timers have to work without interrupts, so we check whenever keyboard
703332468f7SHeinrich Schuchardt  * input or disk accesses happen if enough time elapsed for them to fire.
704bee91169SAlexander Graf  */
efi_timer_check(void)705bee91169SAlexander Graf void efi_timer_check(void)
706bee91169SAlexander Graf {
70743bce442SHeinrich Schuchardt 	struct efi_event *evt;
708bee91169SAlexander Graf 	u64 now = timer_get_us();
709bee91169SAlexander Graf 
71043bce442SHeinrich Schuchardt 	list_for_each_entry(evt, &efi_events, link) {
71143bce442SHeinrich Schuchardt 		if (evt->is_queued)
712b095f3c8SHeinrich Schuchardt 			efi_queue_event(evt, true);
71343bce442SHeinrich Schuchardt 		if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
714ca62a4f5SHeinrich Schuchardt 			continue;
71543bce442SHeinrich Schuchardt 		switch (evt->trigger_type) {
716ca62a4f5SHeinrich Schuchardt 		case EFI_TIMER_RELATIVE:
71743bce442SHeinrich Schuchardt 			evt->trigger_type = EFI_TIMER_STOP;
718ca62a4f5SHeinrich Schuchardt 			break;
719ca62a4f5SHeinrich Schuchardt 		case EFI_TIMER_PERIODIC:
72043bce442SHeinrich Schuchardt 			evt->trigger_next += evt->trigger_time;
721ca62a4f5SHeinrich Schuchardt 			break;
722ca62a4f5SHeinrich Schuchardt 		default:
723ca62a4f5SHeinrich Schuchardt 			continue;
724bee91169SAlexander Graf 		}
725b095f3c8SHeinrich Schuchardt 		evt->is_signaled = false;
72643bce442SHeinrich Schuchardt 		efi_signal_event(evt, true);
727c6841592Sxypron.glpk@gmx.de 	}
728bee91169SAlexander Graf 	WATCHDOG_RESET();
729bee91169SAlexander Graf }
730bee91169SAlexander Graf 
7316b03cd10SHeinrich Schuchardt /**
73278a88f79SMario Six  * efi_set_timer() - set the trigger time for a timer event or stop the event
73378a88f79SMario Six  * @event:        event for which the timer is set
73478a88f79SMario Six  * @type:         type of the timer
73578a88f79SMario Six  * @trigger_time: trigger period in multiples of 100 ns
736332468f7SHeinrich Schuchardt  *
737332468f7SHeinrich Schuchardt  * This is the function for internal usage in U-Boot. For the API function
738332468f7SHeinrich Schuchardt  * implementing the SetTimer service see efi_set_timer_ext.
739332468f7SHeinrich Schuchardt  *
74078a88f79SMario Six  * Return: status code
741332468f7SHeinrich Schuchardt  */
efi_set_timer(struct efi_event * event,enum efi_timer_delay type,uint64_t trigger_time)742b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
743bee91169SAlexander Graf 			   uint64_t trigger_time)
744bee91169SAlexander Graf {
74543bce442SHeinrich Schuchardt 	/* Check that the event is valid */
74643bce442SHeinrich Schuchardt 	if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
74743bce442SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
748bee91169SAlexander Graf 
7498787b02eSxypron.glpk@gmx.de 	/*
7508787b02eSxypron.glpk@gmx.de 	 * The parameter defines a multiple of 100 ns.
7518787b02eSxypron.glpk@gmx.de 	 * We use multiples of 1000 ns. So divide by 10.
7528787b02eSxypron.glpk@gmx.de 	 */
7537d963323SHeinrich Schuchardt 	do_div(trigger_time, 10);
754bee91169SAlexander Graf 
755bee91169SAlexander Graf 	switch (type) {
756bee91169SAlexander Graf 	case EFI_TIMER_STOP:
757c6841592Sxypron.glpk@gmx.de 		event->trigger_next = -1ULL;
758bee91169SAlexander Graf 		break;
759bee91169SAlexander Graf 	case EFI_TIMER_PERIODIC:
760bee91169SAlexander Graf 	case EFI_TIMER_RELATIVE:
76143bce442SHeinrich Schuchardt 		event->trigger_next = timer_get_us() + trigger_time;
762bee91169SAlexander Graf 		break;
763bee91169SAlexander Graf 	default:
764bfc72462Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
765bee91169SAlexander Graf 	}
766c6841592Sxypron.glpk@gmx.de 	event->trigger_type = type;
767c6841592Sxypron.glpk@gmx.de 	event->trigger_time = trigger_time;
768e190e897SHeinrich Schuchardt 	event->is_signaled = false;
769bfc72462Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
770bee91169SAlexander Graf }
771bfc72462Sxypron.glpk@gmx.de 
7726b03cd10SHeinrich Schuchardt /**
77378a88f79SMario Six  * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
77478a88f79SMario Six  *                       event
7756b03cd10SHeinrich Schuchardt  * @event:        event for which the timer is set
7766b03cd10SHeinrich Schuchardt  * @type:         type of the timer
7776b03cd10SHeinrich Schuchardt  * @trigger_time: trigger period in multiples of 100 ns
77878a88f79SMario Six  *
77978a88f79SMario Six  * This function implements the SetTimer service.
78078a88f79SMario Six  *
78178a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
78278a88f79SMario Six  * details.
78378a88f79SMario Six  *
78478a88f79SMario Six  *
78578a88f79SMario Six  * Return: status code
786332468f7SHeinrich Schuchardt  */
efi_set_timer_ext(struct efi_event * event,enum efi_timer_delay type,uint64_t trigger_time)787b521d29eSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
788b521d29eSxypron.glpk@gmx.de 					     enum efi_timer_delay type,
789bfc72462Sxypron.glpk@gmx.de 					     uint64_t trigger_time)
790bfc72462Sxypron.glpk@gmx.de {
791dee37fc9SMasahiro Yamada 	EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
792bfc72462Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
793c6841592Sxypron.glpk@gmx.de }
794bee91169SAlexander Graf 
7956b03cd10SHeinrich Schuchardt /**
79678a88f79SMario Six  * efi_wait_for_event() - wait for events to be signaled
7976b03cd10SHeinrich Schuchardt  * @num_events: number of events to be waited for
7986b03cd10SHeinrich Schuchardt  * @event:      events to be waited for
7996b03cd10SHeinrich Schuchardt  * @index:      index of the event that was signaled
80078a88f79SMario Six  *
80178a88f79SMario Six  * This function implements the WaitForEvent service.
80278a88f79SMario Six  *
80378a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
80478a88f79SMario Six  * details.
80578a88f79SMario Six  *
80678a88f79SMario Six  * Return: status code
807332468f7SHeinrich Schuchardt  */
efi_wait_for_event(efi_uintn_t num_events,struct efi_event ** event,efi_uintn_t * index)808f5a2a938SHeinrich Schuchardt static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
8092fd945feSxypron.glpk@gmx.de 					      struct efi_event **event,
810f5a2a938SHeinrich Schuchardt 					      efi_uintn_t *index)
811bee91169SAlexander Graf {
81243bce442SHeinrich Schuchardt 	int i;
813bee91169SAlexander Graf 
814f5a2a938SHeinrich Schuchardt 	EFI_ENTRY("%zd, %p, %p", num_events, event, index);
815bee91169SAlexander Graf 
816c6841592Sxypron.glpk@gmx.de 	/* Check parameters */
817c6841592Sxypron.glpk@gmx.de 	if (!num_events || !event)
818c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
81932f4b2f8SHeinrich Schuchardt 	/* Check TPL */
82032f4b2f8SHeinrich Schuchardt 	if (efi_tpl != TPL_APPLICATION)
82132f4b2f8SHeinrich Schuchardt 		return EFI_EXIT(EFI_UNSUPPORTED);
822c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < num_events; ++i) {
82343bce442SHeinrich Schuchardt 		if (efi_is_event(event[i]) != EFI_SUCCESS)
824c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
825c6841592Sxypron.glpk@gmx.de 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
826c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
827e190e897SHeinrich Schuchardt 		if (!event[i]->is_signaled)
828b095f3c8SHeinrich Schuchardt 			efi_queue_event(event[i], true);
829c6841592Sxypron.glpk@gmx.de 	}
830c6841592Sxypron.glpk@gmx.de 
831c6841592Sxypron.glpk@gmx.de 	/* Wait for signal */
832c6841592Sxypron.glpk@gmx.de 	for (;;) {
833c6841592Sxypron.glpk@gmx.de 		for (i = 0; i < num_events; ++i) {
834e190e897SHeinrich Schuchardt 			if (event[i]->is_signaled)
835c6841592Sxypron.glpk@gmx.de 				goto out;
836c6841592Sxypron.glpk@gmx.de 		}
837c6841592Sxypron.glpk@gmx.de 		/* Allow events to occur. */
838bee91169SAlexander Graf 		efi_timer_check();
839c6841592Sxypron.glpk@gmx.de 	}
840c6841592Sxypron.glpk@gmx.de 
841c6841592Sxypron.glpk@gmx.de out:
842c6841592Sxypron.glpk@gmx.de 	/*
843c6841592Sxypron.glpk@gmx.de 	 * Reset the signal which is passed to the caller to allow periodic
844c6841592Sxypron.glpk@gmx.de 	 * events to occur.
845c6841592Sxypron.glpk@gmx.de 	 */
846e190e897SHeinrich Schuchardt 	event[i]->is_signaled = false;
847c6841592Sxypron.glpk@gmx.de 	if (index)
848c6841592Sxypron.glpk@gmx.de 		*index = i;
849bee91169SAlexander Graf 
850bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
851bee91169SAlexander Graf }
852bee91169SAlexander Graf 
8536b03cd10SHeinrich Schuchardt /**
85478a88f79SMario Six  * efi_signal_event_ext() - signal an EFI event
85578a88f79SMario Six  * @event: event to signal
856332468f7SHeinrich Schuchardt  *
857332468f7SHeinrich Schuchardt  * This function implements the SignalEvent service.
85878a88f79SMario Six  *
85978a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
86078a88f79SMario Six  * details.
861332468f7SHeinrich Schuchardt  *
862332468f7SHeinrich Schuchardt  * This functions sets the signaled state of the event and queues the
863332468f7SHeinrich Schuchardt  * notification function for execution.
864332468f7SHeinrich Schuchardt  *
86578a88f79SMario Six  * Return: status code
866332468f7SHeinrich Schuchardt  */
efi_signal_event_ext(struct efi_event * event)867c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
868bee91169SAlexander Graf {
869bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
87043bce442SHeinrich Schuchardt 	if (efi_is_event(event) != EFI_SUCCESS)
87143bce442SHeinrich Schuchardt 		return EFI_EXIT(EFI_INVALID_PARAMETER);
8729bc9664dSHeinrich Schuchardt 	efi_signal_event(event, true);
873bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
874bee91169SAlexander Graf }
875bee91169SAlexander Graf 
8766b03cd10SHeinrich Schuchardt /**
87778a88f79SMario Six  * efi_close_event() - close an EFI event
87878a88f79SMario Six  * @event: event to close
879332468f7SHeinrich Schuchardt  *
880332468f7SHeinrich Schuchardt  * This function implements the CloseEvent service.
881332468f7SHeinrich Schuchardt  *
88278a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
88378a88f79SMario Six  * details.
88478a88f79SMario Six  *
88578a88f79SMario Six  * Return: status code
886332468f7SHeinrich Schuchardt  */
efi_close_event(struct efi_event * event)8872fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
888bee91169SAlexander Graf {
889bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
89043bce442SHeinrich Schuchardt 	if (efi_is_event(event) != EFI_SUCCESS)
891c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
89243bce442SHeinrich Schuchardt 	list_del(&event->link);
89343bce442SHeinrich Schuchardt 	free(event);
89443bce442SHeinrich Schuchardt 	return EFI_EXIT(EFI_SUCCESS);
895c6841592Sxypron.glpk@gmx.de }
896bee91169SAlexander Graf 
8976b03cd10SHeinrich Schuchardt /**
89878a88f79SMario Six  * efi_check_event() - check if an event is signaled
89978a88f79SMario Six  * @event: event to check
900332468f7SHeinrich Schuchardt  *
901332468f7SHeinrich Schuchardt  * This function implements the CheckEvent service.
902332468f7SHeinrich Schuchardt  *
90378a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
90478a88f79SMario Six  * details.
905332468f7SHeinrich Schuchardt  *
90678a88f79SMario Six  * If an event is not signaled yet, the notification function is queued. The
90778a88f79SMario Six  * signaled state is cleared.
90878a88f79SMario Six  *
90978a88f79SMario Six  * Return: status code
910332468f7SHeinrich Schuchardt  */
efi_check_event(struct efi_event * event)9112fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
912bee91169SAlexander Graf {
913bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
914c6841592Sxypron.glpk@gmx.de 	efi_timer_check();
91543bce442SHeinrich Schuchardt 	if (efi_is_event(event) != EFI_SUCCESS ||
91643bce442SHeinrich Schuchardt 	    event->type & EVT_NOTIFY_SIGNAL)
91743bce442SHeinrich Schuchardt 		return EFI_EXIT(EFI_INVALID_PARAMETER);
918e190e897SHeinrich Schuchardt 	if (!event->is_signaled)
919b095f3c8SHeinrich Schuchardt 		efi_queue_event(event, true);
9207069515eSHeinrich Schuchardt 	if (event->is_signaled) {
9217069515eSHeinrich Schuchardt 		event->is_signaled = false;
922c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_SUCCESS);
9237069515eSHeinrich Schuchardt 	}
924bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_READY);
925bee91169SAlexander Graf }
926bee91169SAlexander Graf 
9276b03cd10SHeinrich Schuchardt /**
92878a88f79SMario Six  * efi_search_obj() - find the internal EFI object for a handle
9296b03cd10SHeinrich Schuchardt  * @handle: handle to find
93078a88f79SMario Six  *
93178a88f79SMario Six  * Return: EFI object
9327b9f8ad7SHeinrich Schuchardt  */
efi_search_obj(const efi_handle_t handle)9332074f700SHeinrich Schuchardt struct efi_object *efi_search_obj(const efi_handle_t handle)
9347b9f8ad7SHeinrich Schuchardt {
9357b9f8ad7SHeinrich Schuchardt 	struct efi_object *efiobj;
9367b9f8ad7SHeinrich Schuchardt 
9371b68153aSHeinrich Schuchardt 	list_for_each_entry(efiobj, &efi_obj_list, link) {
938fae0118eSHeinrich Schuchardt 		if (efiobj == handle)
9397b9f8ad7SHeinrich Schuchardt 			return efiobj;
9407b9f8ad7SHeinrich Schuchardt 	}
9417b9f8ad7SHeinrich Schuchardt 
9427b9f8ad7SHeinrich Schuchardt 	return NULL;
9437b9f8ad7SHeinrich Schuchardt }
9447b9f8ad7SHeinrich Schuchardt 
9456b03cd10SHeinrich Schuchardt /**
94678a88f79SMario Six  * efi_open_protocol_info_entry() - create open protocol info entry and add it
9476b03cd10SHeinrich Schuchardt  *                                  to a protocol
9486b03cd10SHeinrich Schuchardt  * @handler: handler of a protocol
94978a88f79SMario Six  *
95078a88f79SMario Six  * Return: open protocol info entry
951fe1599daSHeinrich Schuchardt  */
efi_create_open_info(struct efi_handler * handler)952fe1599daSHeinrich Schuchardt static struct efi_open_protocol_info_entry *efi_create_open_info(
953fe1599daSHeinrich Schuchardt 			struct efi_handler *handler)
954fe1599daSHeinrich Schuchardt {
955fe1599daSHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
956fe1599daSHeinrich Schuchardt 
957fe1599daSHeinrich Schuchardt 	item = calloc(1, sizeof(struct efi_open_protocol_info_item));
958fe1599daSHeinrich Schuchardt 	if (!item)
959fe1599daSHeinrich Schuchardt 		return NULL;
960fe1599daSHeinrich Schuchardt 	/* Append the item to the open protocol info list. */
961fe1599daSHeinrich Schuchardt 	list_add_tail(&item->link, &handler->open_infos);
962fe1599daSHeinrich Schuchardt 
963fe1599daSHeinrich Schuchardt 	return &item->info;
964fe1599daSHeinrich Schuchardt }
965fe1599daSHeinrich Schuchardt 
9666b03cd10SHeinrich Schuchardt /**
96778a88f79SMario Six  * efi_delete_open_info() - remove an open protocol info entry from a protocol
9686b03cd10SHeinrich Schuchardt  * @item: open protocol info entry to delete
96978a88f79SMario Six  *
97078a88f79SMario Six  * Return: status code
971fe1599daSHeinrich Schuchardt  */
efi_delete_open_info(struct efi_open_protocol_info_item * item)972fe1599daSHeinrich Schuchardt static efi_status_t efi_delete_open_info(
973fe1599daSHeinrich Schuchardt 			struct efi_open_protocol_info_item *item)
974fe1599daSHeinrich Schuchardt {
975fe1599daSHeinrich Schuchardt 	list_del(&item->link);
976fe1599daSHeinrich Schuchardt 	free(item);
977fe1599daSHeinrich Schuchardt 	return EFI_SUCCESS;
978fe1599daSHeinrich Schuchardt }
979fe1599daSHeinrich Schuchardt 
9806b03cd10SHeinrich Schuchardt /**
98178a88f79SMario Six  * efi_add_protocol() - install new protocol on a handle
9826b03cd10SHeinrich Schuchardt  * @handle:             handle on which the protocol shall be installed
9836b03cd10SHeinrich Schuchardt  * @protocol:           GUID of the protocol to be installed
9846b03cd10SHeinrich Schuchardt  * @protocol_interface: interface of the protocol implementation
98578a88f79SMario Six  *
98678a88f79SMario Six  * Return: status code
9873f79a2b5SHeinrich Schuchardt  */
efi_add_protocol(const efi_handle_t handle,const efi_guid_t * protocol,void * protocol_interface)9882074f700SHeinrich Schuchardt efi_status_t efi_add_protocol(const efi_handle_t handle,
9892074f700SHeinrich Schuchardt 			      const efi_guid_t *protocol,
9903f79a2b5SHeinrich Schuchardt 			      void *protocol_interface)
9913f79a2b5SHeinrich Schuchardt {
9923f79a2b5SHeinrich Schuchardt 	struct efi_object *efiobj;
9933f79a2b5SHeinrich Schuchardt 	struct efi_handler *handler;
9943f79a2b5SHeinrich Schuchardt 	efi_status_t ret;
9953f79a2b5SHeinrich Schuchardt 
9963f79a2b5SHeinrich Schuchardt 	efiobj = efi_search_obj(handle);
9973f79a2b5SHeinrich Schuchardt 	if (!efiobj)
9983f79a2b5SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
9993f79a2b5SHeinrich Schuchardt 	ret = efi_search_protocol(handle, protocol, NULL);
10003f79a2b5SHeinrich Schuchardt 	if (ret != EFI_NOT_FOUND)
10013f79a2b5SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
10023f79a2b5SHeinrich Schuchardt 	handler = calloc(1, sizeof(struct efi_handler));
10033f79a2b5SHeinrich Schuchardt 	if (!handler)
10043f79a2b5SHeinrich Schuchardt 		return EFI_OUT_OF_RESOURCES;
10053f79a2b5SHeinrich Schuchardt 	handler->guid = protocol;
10063f79a2b5SHeinrich Schuchardt 	handler->protocol_interface = protocol_interface;
1007fe1599daSHeinrich Schuchardt 	INIT_LIST_HEAD(&handler->open_infos);
100869fb6b1aSHeinrich Schuchardt 	list_add_tail(&handler->link, &efiobj->protocols);
1009d5504144SHeinrich Schuchardt 	if (!guidcmp(&efi_guid_device_path, protocol))
1010d5504144SHeinrich Schuchardt 		EFI_PRINT("installed device path '%pD'\n", protocol_interface);
10113f79a2b5SHeinrich Schuchardt 	return EFI_SUCCESS;
10123f79a2b5SHeinrich Schuchardt }
10133f79a2b5SHeinrich Schuchardt 
10146b03cd10SHeinrich Schuchardt /**
101578a88f79SMario Six  * efi_install_protocol_interface() - install protocol interface
10166b03cd10SHeinrich Schuchardt  * @handle:                  handle on which the protocol shall be installed
10176b03cd10SHeinrich Schuchardt  * @protocol:                GUID of the protocol to be installed
10186b03cd10SHeinrich Schuchardt  * @protocol_interface_type: type of the interface to be installed,
1019332468f7SHeinrich Schuchardt  *                           always EFI_NATIVE_INTERFACE
10206b03cd10SHeinrich Schuchardt  * @protocol_interface:      interface of the protocol implementation
102178a88f79SMario Six  *
102278a88f79SMario Six  * This function implements the InstallProtocolInterface service.
102378a88f79SMario Six  *
102478a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
102578a88f79SMario Six  * details.
102678a88f79SMario Six  *
102778a88f79SMario Six  * Return: status code
1028332468f7SHeinrich Schuchardt  */
efi_install_protocol_interface(efi_handle_t * handle,const efi_guid_t * protocol,int protocol_interface_type,void * protocol_interface)10291760ef57SHeinrich Schuchardt static efi_status_t EFIAPI efi_install_protocol_interface(
1030faea1041SHeinrich Schuchardt 			efi_handle_t *handle, const efi_guid_t *protocol,
10311760ef57SHeinrich Schuchardt 			int protocol_interface_type, void *protocol_interface)
1032bee91169SAlexander Graf {
1033e0549f8aSxypron.glpk@gmx.de 	efi_status_t r;
1034e0549f8aSxypron.glpk@gmx.de 
10351760ef57SHeinrich Schuchardt 	EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
10361760ef57SHeinrich Schuchardt 		  protocol_interface);
10371760ef57SHeinrich Schuchardt 
1038e0549f8aSxypron.glpk@gmx.de 	if (!handle || !protocol ||
1039e0549f8aSxypron.glpk@gmx.de 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
1040e0549f8aSxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
1041e0549f8aSxypron.glpk@gmx.de 		goto out;
1042bee91169SAlexander Graf 	}
1043e0549f8aSxypron.glpk@gmx.de 
1044e0549f8aSxypron.glpk@gmx.de 	/* Create new handle if requested. */
1045e0549f8aSxypron.glpk@gmx.de 	if (!*handle) {
10463cc6e3feSHeinrich Schuchardt 		r = efi_create_handle(handle);
10473cc6e3feSHeinrich Schuchardt 		if (r != EFI_SUCCESS)
1048e0549f8aSxypron.glpk@gmx.de 			goto out;
1049af1408e0SHeinrich Schuchardt 		debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1050af1408e0SHeinrich Schuchardt 		      *handle);
1051af1408e0SHeinrich Schuchardt 	} else {
1052af1408e0SHeinrich Schuchardt 		debug("%sEFI: handle %p\n", indent_string(nesting_level),
1053af1408e0SHeinrich Schuchardt 		      *handle);
1054e0549f8aSxypron.glpk@gmx.de 	}
10551202530dSHeinrich Schuchardt 	/* Add new protocol */
10561202530dSHeinrich Schuchardt 	r = efi_add_protocol(*handle, protocol, protocol_interface);
1057e0549f8aSxypron.glpk@gmx.de out:
10581760ef57SHeinrich Schuchardt 	return EFI_EXIT(r);
1059e0549f8aSxypron.glpk@gmx.de }
1060e0549f8aSxypron.glpk@gmx.de 
10616b03cd10SHeinrich Schuchardt /**
106278a88f79SMario Six  * efi_get_drivers() - get all drivers associated to a controller
1063fae0118eSHeinrich Schuchardt  * @handle:               handle of the controller
1064b72aaa87SHeinrich Schuchardt  * @protocol:             protocol GUID (optional)
10656b03cd10SHeinrich Schuchardt  * @number_of_drivers:    number of child controllers
10666b03cd10SHeinrich Schuchardt  * @driver_handle_buffer: handles of the the drivers
106778a88f79SMario Six  *
106878a88f79SMario Six  * The allocated buffer has to be freed with free().
106978a88f79SMario Six  *
107078a88f79SMario Six  * Return: status code
10713f9b0042SHeinrich Schuchardt  */
efi_get_drivers(efi_handle_t handle,const efi_guid_t * protocol,efi_uintn_t * number_of_drivers,efi_handle_t ** driver_handle_buffer)1072fae0118eSHeinrich Schuchardt static efi_status_t efi_get_drivers(efi_handle_t handle,
10733f9b0042SHeinrich Schuchardt 				    const efi_guid_t *protocol,
10743f9b0042SHeinrich Schuchardt 				    efi_uintn_t *number_of_drivers,
10753f9b0042SHeinrich Schuchardt 				    efi_handle_t **driver_handle_buffer)
10763f9b0042SHeinrich Schuchardt {
10773f9b0042SHeinrich Schuchardt 	struct efi_handler *handler;
10783f9b0042SHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
10793f9b0042SHeinrich Schuchardt 	efi_uintn_t count = 0, i;
10803f9b0042SHeinrich Schuchardt 	bool duplicate;
10813f9b0042SHeinrich Schuchardt 
10823f9b0042SHeinrich Schuchardt 	/* Count all driver associations */
1083fae0118eSHeinrich Schuchardt 	list_for_each_entry(handler, &handle->protocols, link) {
10843f9b0042SHeinrich Schuchardt 		if (protocol && guidcmp(handler->guid, protocol))
10853f9b0042SHeinrich Schuchardt 			continue;
10863f9b0042SHeinrich Schuchardt 		list_for_each_entry(item, &handler->open_infos, link) {
10873f9b0042SHeinrich Schuchardt 			if (item->info.attributes &
10883f9b0042SHeinrich Schuchardt 			    EFI_OPEN_PROTOCOL_BY_DRIVER)
10893f9b0042SHeinrich Schuchardt 				++count;
10903f9b0042SHeinrich Schuchardt 		}
10913f9b0042SHeinrich Schuchardt 	}
10923f9b0042SHeinrich Schuchardt 	/*
10933f9b0042SHeinrich Schuchardt 	 * Create buffer. In case of duplicate driver assignments the buffer
10943f9b0042SHeinrich Schuchardt 	 * will be too large. But that does not harm.
10953f9b0042SHeinrich Schuchardt 	 */
10963f9b0042SHeinrich Schuchardt 	*number_of_drivers = 0;
10973f9b0042SHeinrich Schuchardt 	*driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
10983f9b0042SHeinrich Schuchardt 	if (!*driver_handle_buffer)
10993f9b0042SHeinrich Schuchardt 		return EFI_OUT_OF_RESOURCES;
11003f9b0042SHeinrich Schuchardt 	/* Collect unique driver handles */
1101fae0118eSHeinrich Schuchardt 	list_for_each_entry(handler, &handle->protocols, link) {
11023f9b0042SHeinrich Schuchardt 		if (protocol && guidcmp(handler->guid, protocol))
11033f9b0042SHeinrich Schuchardt 			continue;
11043f9b0042SHeinrich Schuchardt 		list_for_each_entry(item, &handler->open_infos, link) {
11053f9b0042SHeinrich Schuchardt 			if (item->info.attributes &
11063f9b0042SHeinrich Schuchardt 			    EFI_OPEN_PROTOCOL_BY_DRIVER) {
11073f9b0042SHeinrich Schuchardt 				/* Check this is a new driver */
11083f9b0042SHeinrich Schuchardt 				duplicate = false;
11093f9b0042SHeinrich Schuchardt 				for (i = 0; i < *number_of_drivers; ++i) {
11103f9b0042SHeinrich Schuchardt 					if ((*driver_handle_buffer)[i] ==
11113f9b0042SHeinrich Schuchardt 					    item->info.agent_handle)
11123f9b0042SHeinrich Schuchardt 						duplicate = true;
11133f9b0042SHeinrich Schuchardt 				}
11143f9b0042SHeinrich Schuchardt 				/* Copy handle to buffer */
11153f9b0042SHeinrich Schuchardt 				if (!duplicate) {
11163f9b0042SHeinrich Schuchardt 					i = (*number_of_drivers)++;
11173f9b0042SHeinrich Schuchardt 					(*driver_handle_buffer)[i] =
11183f9b0042SHeinrich Schuchardt 						item->info.agent_handle;
11193f9b0042SHeinrich Schuchardt 				}
11203f9b0042SHeinrich Schuchardt 			}
11213f9b0042SHeinrich Schuchardt 		}
11223f9b0042SHeinrich Schuchardt 	}
11233f9b0042SHeinrich Schuchardt 	return EFI_SUCCESS;
11243f9b0042SHeinrich Schuchardt }
11253f9b0042SHeinrich Schuchardt 
11266b03cd10SHeinrich Schuchardt /**
112778a88f79SMario Six  * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1128fae0118eSHeinrich Schuchardt  * @handle:       handle of the controller
1129b72aaa87SHeinrich Schuchardt  * @protocol:     protocol GUID (optional)
11306b03cd10SHeinrich Schuchardt  * @child_handle: handle of the child to destroy
113178a88f79SMario Six  *
113278a88f79SMario Six  * This function implements the DisconnectController service.
113378a88f79SMario Six  *
113478a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
113578a88f79SMario Six  * details.
113678a88f79SMario Six  *
113778a88f79SMario Six  * Return: status code
11383f9b0042SHeinrich Schuchardt  */
efi_disconnect_all_drivers(efi_handle_t handle,const efi_guid_t * protocol,efi_handle_t child_handle)1139fae0118eSHeinrich Schuchardt static efi_status_t efi_disconnect_all_drivers
1140fae0118eSHeinrich Schuchardt 				(efi_handle_t handle,
11413f9b0042SHeinrich Schuchardt 				 const efi_guid_t *protocol,
11423f9b0042SHeinrich Schuchardt 				 efi_handle_t child_handle)
11433f9b0042SHeinrich Schuchardt {
11443f9b0042SHeinrich Schuchardt 	efi_uintn_t number_of_drivers;
11453f9b0042SHeinrich Schuchardt 	efi_handle_t *driver_handle_buffer;
11463f9b0042SHeinrich Schuchardt 	efi_status_t r, ret;
11473f9b0042SHeinrich Schuchardt 
1148fae0118eSHeinrich Schuchardt 	ret = efi_get_drivers(handle, protocol, &number_of_drivers,
11493f9b0042SHeinrich Schuchardt 			      &driver_handle_buffer);
11503f9b0042SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
11513f9b0042SHeinrich Schuchardt 		return ret;
11523f9b0042SHeinrich Schuchardt 
11533f9b0042SHeinrich Schuchardt 	ret = EFI_NOT_FOUND;
11543f9b0042SHeinrich Schuchardt 	while (number_of_drivers) {
11553f9b0042SHeinrich Schuchardt 		r = EFI_CALL(efi_disconnect_controller(
1156fae0118eSHeinrich Schuchardt 				handle,
11573f9b0042SHeinrich Schuchardt 				driver_handle_buffer[--number_of_drivers],
11583f9b0042SHeinrich Schuchardt 				child_handle));
11593f9b0042SHeinrich Schuchardt 		if (r == EFI_SUCCESS)
11603f9b0042SHeinrich Schuchardt 			ret = r;
11613f9b0042SHeinrich Schuchardt 	}
11623f9b0042SHeinrich Schuchardt 	free(driver_handle_buffer);
11633f9b0042SHeinrich Schuchardt 	return ret;
11643f9b0042SHeinrich Schuchardt }
11653f9b0042SHeinrich Schuchardt 
11666b03cd10SHeinrich Schuchardt /**
11679b47f13bSHeinrich Schuchardt  * efi_uninstall_protocol() - uninstall protocol interface
11689b47f13bSHeinrich Schuchardt  *
11696b03cd10SHeinrich Schuchardt  * @handle:             handle from which the protocol shall be removed
11706b03cd10SHeinrich Schuchardt  * @protocol:           GUID of the protocol to be removed
11716b03cd10SHeinrich Schuchardt  * @protocol_interface: interface to be removed
117278a88f79SMario Six  *
11739b47f13bSHeinrich Schuchardt  * This function DOES NOT delete a handle without installed protocol.
117478a88f79SMario Six  *
117578a88f79SMario Six  * Return: status code
1176332468f7SHeinrich Schuchardt  */
efi_uninstall_protocol(efi_handle_t handle,const efi_guid_t * protocol,void * protocol_interface)11779b47f13bSHeinrich Schuchardt static efi_status_t efi_uninstall_protocol
11789b47f13bSHeinrich Schuchardt 			(efi_handle_t handle, const efi_guid_t *protocol,
1179cd534083SHeinrich Schuchardt 			 void *protocol_interface)
1180bee91169SAlexander Graf {
1181ad97373bSHeinrich Schuchardt 	struct efi_object *efiobj;
11825810511dSHeinrich Schuchardt 	struct efi_handler *handler;
1183ad97373bSHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
1184ad97373bSHeinrich Schuchardt 	struct efi_open_protocol_info_item *pos;
11855810511dSHeinrich Schuchardt 	efi_status_t r;
11864b6ed0d7Sxypron.glpk@gmx.de 
1187ad97373bSHeinrich Schuchardt 	/* Check handle */
1188ad97373bSHeinrich Schuchardt 	efiobj = efi_search_obj(handle);
1189ad97373bSHeinrich Schuchardt 	if (!efiobj) {
11904b6ed0d7Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
11914b6ed0d7Sxypron.glpk@gmx.de 		goto out;
11924b6ed0d7Sxypron.glpk@gmx.de 	}
11935810511dSHeinrich Schuchardt 	/* Find the protocol on the handle */
11945810511dSHeinrich Schuchardt 	r = efi_search_protocol(handle, protocol, &handler);
11955810511dSHeinrich Schuchardt 	if (r != EFI_SUCCESS)
11965810511dSHeinrich Schuchardt 		goto out;
1197ad97373bSHeinrich Schuchardt 	/* Disconnect controllers */
1198ad97373bSHeinrich Schuchardt 	efi_disconnect_all_drivers(efiobj, protocol, NULL);
1199ad97373bSHeinrich Schuchardt 	if (!list_empty(&handler->open_infos)) {
12004b6ed0d7Sxypron.glpk@gmx.de 		r =  EFI_ACCESS_DENIED;
1201ad97373bSHeinrich Schuchardt 		goto out;
12024b6ed0d7Sxypron.glpk@gmx.de 	}
1203ad97373bSHeinrich Schuchardt 	/* Close protocol */
1204ad97373bSHeinrich Schuchardt 	list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1205ad97373bSHeinrich Schuchardt 		if (item->info.attributes ==
1206ad97373bSHeinrich Schuchardt 			EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1207ad97373bSHeinrich Schuchardt 		    item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1208ad97373bSHeinrich Schuchardt 		    item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1209ad97373bSHeinrich Schuchardt 			list_del(&item->link);
1210ad97373bSHeinrich Schuchardt 	}
1211ad97373bSHeinrich Schuchardt 	if (!list_empty(&handler->open_infos)) {
1212ad97373bSHeinrich Schuchardt 		r =  EFI_ACCESS_DENIED;
1213ad97373bSHeinrich Schuchardt 		goto out;
1214ad97373bSHeinrich Schuchardt 	}
1215ad97373bSHeinrich Schuchardt 	r = efi_remove_protocol(handle, protocol, protocol_interface);
12164b6ed0d7Sxypron.glpk@gmx.de out:
12179b47f13bSHeinrich Schuchardt 	return r;
12189b47f13bSHeinrich Schuchardt }
12199b47f13bSHeinrich Schuchardt 
12209b47f13bSHeinrich Schuchardt /**
12219b47f13bSHeinrich Schuchardt  * efi_uninstall_protocol_interface() - uninstall protocol interface
12229b47f13bSHeinrich Schuchardt  * @handle:             handle from which the protocol shall be removed
12239b47f13bSHeinrich Schuchardt  * @protocol:           GUID of the protocol to be removed
12249b47f13bSHeinrich Schuchardt  * @protocol_interface: interface to be removed
12259b47f13bSHeinrich Schuchardt  *
12269b47f13bSHeinrich Schuchardt  * This function implements the UninstallProtocolInterface service.
12279b47f13bSHeinrich Schuchardt  *
12289b47f13bSHeinrich Schuchardt  * See the Unified Extensible Firmware Interface (UEFI) specification for
12299b47f13bSHeinrich Schuchardt  * details.
12309b47f13bSHeinrich Schuchardt  *
12319b47f13bSHeinrich Schuchardt  * Return: status code
12329b47f13bSHeinrich Schuchardt  */
efi_uninstall_protocol_interface(efi_handle_t handle,const efi_guid_t * protocol,void * protocol_interface)12339b47f13bSHeinrich Schuchardt static efi_status_t EFIAPI efi_uninstall_protocol_interface
12349b47f13bSHeinrich Schuchardt 			(efi_handle_t handle, const efi_guid_t *protocol,
12359b47f13bSHeinrich Schuchardt 			 void *protocol_interface)
12369b47f13bSHeinrich Schuchardt {
12379b47f13bSHeinrich Schuchardt 	efi_status_t ret;
12389b47f13bSHeinrich Schuchardt 
12399b47f13bSHeinrich Schuchardt 	EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
12409b47f13bSHeinrich Schuchardt 
12419b47f13bSHeinrich Schuchardt 	ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
12429b47f13bSHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
12439b47f13bSHeinrich Schuchardt 		goto out;
12449b47f13bSHeinrich Schuchardt 
12459b47f13bSHeinrich Schuchardt 	/* If the last protocol has been removed, delete the handle. */
12469b47f13bSHeinrich Schuchardt 	if (list_empty(&handle->protocols)) {
12479b47f13bSHeinrich Schuchardt 		list_del(&handle->link);
12489b47f13bSHeinrich Schuchardt 		free(handle);
12499b47f13bSHeinrich Schuchardt 	}
12509b47f13bSHeinrich Schuchardt out:
12519b47f13bSHeinrich Schuchardt 	return EFI_EXIT(ret);
1252bee91169SAlexander Graf }
1253bee91169SAlexander Graf 
12546b03cd10SHeinrich Schuchardt /**
125578a88f79SMario Six  * efi_register_protocol_notify() - register an event for notification when a
12566b03cd10SHeinrich Schuchardt  *                                  protocol is installed.
125778a88f79SMario Six  * @protocol:     GUID of the protocol whose installation shall be notified
125878a88f79SMario Six  * @event:        event to be signaled upon installation of the protocol
125978a88f79SMario Six  * @registration: key for retrieving the registration information
1260332468f7SHeinrich Schuchardt  *
1261332468f7SHeinrich Schuchardt  * This function implements the RegisterProtocolNotify service.
1262332468f7SHeinrich Schuchardt  * See the Unified Extensible Firmware Interface (UEFI) specification
1263332468f7SHeinrich Schuchardt  * for details.
1264332468f7SHeinrich Schuchardt  *
126578a88f79SMario Six  * Return: status code
1266332468f7SHeinrich Schuchardt  */
efi_register_protocol_notify(const efi_guid_t * protocol,struct efi_event * event,void ** registration)12675a9682d0SHeinrich Schuchardt static efi_status_t EFIAPI efi_register_protocol_notify(
12685a9682d0SHeinrich Schuchardt 						const efi_guid_t *protocol,
12692fd945feSxypron.glpk@gmx.de 						struct efi_event *event,
1270bee91169SAlexander Graf 						void **registration)
1271bee91169SAlexander Graf {
1272778e6af8SRob Clark 	EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1273bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1274bee91169SAlexander Graf }
1275bee91169SAlexander Graf 
12766b03cd10SHeinrich Schuchardt /**
127778a88f79SMario Six  * efi_search() - determine if an EFI handle implements a protocol
12786b03cd10SHeinrich Schuchardt  * @search_type: selection criterion
12796b03cd10SHeinrich Schuchardt  * @protocol:    GUID of the protocol
12806b03cd10SHeinrich Schuchardt  * @search_key:  registration key
1281fae0118eSHeinrich Schuchardt  * @handle:      handle
128278a88f79SMario Six  *
128378a88f79SMario Six  * See the documentation of the LocateHandle service in the UEFI specification.
128478a88f79SMario Six  *
128578a88f79SMario Six  * Return: 0 if the handle implements the protocol
1286332468f7SHeinrich Schuchardt  */
efi_search(enum efi_locate_search_type search_type,const efi_guid_t * protocol,void * search_key,efi_handle_t handle)1287bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
12885a9682d0SHeinrich Schuchardt 		      const efi_guid_t *protocol, void *search_key,
1289fae0118eSHeinrich Schuchardt 		      efi_handle_t handle)
1290bee91169SAlexander Graf {
129142cf1242SHeinrich Schuchardt 	efi_status_t ret;
1292bee91169SAlexander Graf 
1293bee91169SAlexander Graf 	switch (search_type) {
12949f0770ffSHeinrich Schuchardt 	case ALL_HANDLES:
1295bee91169SAlexander Graf 		return 0;
12969f0770ffSHeinrich Schuchardt 	case BY_REGISTER_NOTIFY:
129742cf1242SHeinrich Schuchardt 		/* TODO: RegisterProtocolNotify is not implemented yet */
1298bee91169SAlexander Graf 		return -1;
12999f0770ffSHeinrich Schuchardt 	case BY_PROTOCOL:
1300fae0118eSHeinrich Schuchardt 		ret = efi_search_protocol(handle, protocol, NULL);
130142cf1242SHeinrich Schuchardt 		return (ret != EFI_SUCCESS);
130242cf1242SHeinrich Schuchardt 	default:
130342cf1242SHeinrich Schuchardt 		/* Invalid search type */
1304bee91169SAlexander Graf 		return -1;
1305bee91169SAlexander Graf 	}
1306bee91169SAlexander Graf }
1307bee91169SAlexander Graf 
13086b03cd10SHeinrich Schuchardt /**
130978a88f79SMario Six  * efi_locate_handle() - locate handles implementing a protocol
13106b03cd10SHeinrich Schuchardt  * @search_type: selection criterion
13116b03cd10SHeinrich Schuchardt  * @protocol:    GUID of the protocol
13126b03cd10SHeinrich Schuchardt  * @search_key: registration key
13136b03cd10SHeinrich Schuchardt  * @buffer_size: size of the buffer to receive the handles in bytes
13146b03cd10SHeinrich Schuchardt  * @buffer:      buffer to receive the relevant handles
131578a88f79SMario Six  *
131678a88f79SMario Six  * This function is meant for U-Boot internal calls. For the API implementation
131778a88f79SMario Six  * of the LocateHandle service see efi_locate_handle_ext.
131878a88f79SMario Six  *
131978a88f79SMario Six  * Return: status code
1320332468f7SHeinrich Schuchardt  */
efi_locate_handle(enum efi_locate_search_type search_type,const efi_guid_t * protocol,void * search_key,efi_uintn_t * buffer_size,efi_handle_t * buffer)1321ebf199b9Sxypron.glpk@gmx.de static efi_status_t efi_locate_handle(
1322bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
13235a9682d0SHeinrich Schuchardt 			const efi_guid_t *protocol, void *search_key,
1324f5a2a938SHeinrich Schuchardt 			efi_uintn_t *buffer_size, efi_handle_t *buffer)
1325bee91169SAlexander Graf {
1326caf864e4SHeinrich Schuchardt 	struct efi_object *efiobj;
1327f5a2a938SHeinrich Schuchardt 	efi_uintn_t size = 0;
1328bee91169SAlexander Graf 
1329caf864e4SHeinrich Schuchardt 	/* Check parameters */
1330caf864e4SHeinrich Schuchardt 	switch (search_type) {
1331caf864e4SHeinrich Schuchardt 	case ALL_HANDLES:
1332caf864e4SHeinrich Schuchardt 		break;
1333caf864e4SHeinrich Schuchardt 	case BY_REGISTER_NOTIFY:
1334caf864e4SHeinrich Schuchardt 		if (!search_key)
1335caf864e4SHeinrich Schuchardt 			return EFI_INVALID_PARAMETER;
1336caf864e4SHeinrich Schuchardt 		/* RegisterProtocolNotify is not implemented yet */
1337caf864e4SHeinrich Schuchardt 		return EFI_UNSUPPORTED;
1338caf864e4SHeinrich Schuchardt 	case BY_PROTOCOL:
1339caf864e4SHeinrich Schuchardt 		if (!protocol)
1340caf864e4SHeinrich Schuchardt 			return EFI_INVALID_PARAMETER;
1341caf864e4SHeinrich Schuchardt 		break;
1342caf864e4SHeinrich Schuchardt 	default:
1343caf864e4SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
1344bee91169SAlexander Graf 	}
1345caf864e4SHeinrich Schuchardt 
1346caf864e4SHeinrich Schuchardt 	/*
1347caf864e4SHeinrich Schuchardt 	 * efi_locate_handle_buffer uses this function for
1348caf864e4SHeinrich Schuchardt 	 * the calculation of the necessary buffer size.
1349caf864e4SHeinrich Schuchardt 	 * So do not require a buffer for buffersize == 0.
1350caf864e4SHeinrich Schuchardt 	 */
1351caf864e4SHeinrich Schuchardt 	if (!buffer_size || (*buffer_size && !buffer))
1352caf864e4SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
1353caf864e4SHeinrich Schuchardt 
1354caf864e4SHeinrich Schuchardt 	/* Count how much space we need */
1355caf864e4SHeinrich Schuchardt 	list_for_each_entry(efiobj, &efi_obj_list, link) {
1356caf864e4SHeinrich Schuchardt 		if (!efi_search(search_type, protocol, search_key, efiobj))
1357caf864e4SHeinrich Schuchardt 			size += sizeof(void *);
1358bee91169SAlexander Graf 	}
1359bee91169SAlexander Graf 
1360bee91169SAlexander Graf 	if (*buffer_size < size) {
1361bee91169SAlexander Graf 		*buffer_size = size;
136226329584Sxypron.glpk@gmx.de 		return EFI_BUFFER_TOO_SMALL;
1363bee91169SAlexander Graf 	}
1364bee91169SAlexander Graf 
1365796a78cbSRob Clark 	*buffer_size = size;
1366796a78cbSRob Clark 	if (size == 0)
1367796a78cbSRob Clark 		return EFI_NOT_FOUND;
1368796a78cbSRob Clark 
1369bee91169SAlexander Graf 	/* Then fill the array */
1370caf864e4SHeinrich Schuchardt 	list_for_each_entry(efiobj, &efi_obj_list, link) {
1371caf864e4SHeinrich Schuchardt 		if (!efi_search(search_type, protocol, search_key, efiobj))
1372fae0118eSHeinrich Schuchardt 			*buffer++ = efiobj;
1373bee91169SAlexander Graf 	}
1374bee91169SAlexander Graf 
137526329584Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
137626329584Sxypron.glpk@gmx.de }
137726329584Sxypron.glpk@gmx.de 
13786b03cd10SHeinrich Schuchardt /**
137978a88f79SMario Six  * efi_locate_handle_ext() - locate handles implementing a protocol.
13806b03cd10SHeinrich Schuchardt  * @search_type: selection criterion
13816b03cd10SHeinrich Schuchardt  * @protocol:    GUID of the protocol
13826b03cd10SHeinrich Schuchardt  * @search_key:  registration key
13836b03cd10SHeinrich Schuchardt  * @buffer_size: size of the buffer to receive the handles in bytes
13846b03cd10SHeinrich Schuchardt  * @buffer:      buffer to receive the relevant handles
138578a88f79SMario Six  *
138678a88f79SMario Six  * This function implements the LocateHandle service.
138778a88f79SMario Six  *
138878a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
138978a88f79SMario Six  * details.
139078a88f79SMario Six  *
139178a88f79SMario Six  * Return: 0 if the handle implements the protocol
1392332468f7SHeinrich Schuchardt  */
efi_locate_handle_ext(enum efi_locate_search_type search_type,const efi_guid_t * protocol,void * search_key,efi_uintn_t * buffer_size,efi_handle_t * buffer)139326329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
139426329584Sxypron.glpk@gmx.de 			enum efi_locate_search_type search_type,
13955a9682d0SHeinrich Schuchardt 			const efi_guid_t *protocol, void *search_key,
1396f5a2a938SHeinrich Schuchardt 			efi_uintn_t *buffer_size, efi_handle_t *buffer)
139726329584Sxypron.glpk@gmx.de {
1398778e6af8SRob Clark 	EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
139926329584Sxypron.glpk@gmx.de 		  buffer_size, buffer);
140026329584Sxypron.glpk@gmx.de 
140126329584Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
140226329584Sxypron.glpk@gmx.de 			buffer_size, buffer));
1403bee91169SAlexander Graf }
1404bee91169SAlexander Graf 
14056b03cd10SHeinrich Schuchardt /**
140678a88f79SMario Six  * efi_remove_configuration_table() - collapses configuration table entries,
14076b03cd10SHeinrich Schuchardt  *                                    removing index i
14086b03cd10SHeinrich Schuchardt  *
14096b03cd10SHeinrich Schuchardt  * @i: index of the table entry to be removed
14106b03cd10SHeinrich Schuchardt  */
efi_remove_configuration_table(int i)1411d98cdf6aSAlexander Graf static void efi_remove_configuration_table(int i)
1412d98cdf6aSAlexander Graf {
14134182a129SHeinrich Schuchardt 	struct efi_configuration_table *this = &systab.tables[i];
14144182a129SHeinrich Schuchardt 	struct efi_configuration_table *next = &systab.tables[i + 1];
14154182a129SHeinrich Schuchardt 	struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1416d98cdf6aSAlexander Graf 
1417d98cdf6aSAlexander Graf 	memmove(this, next, (ulong)end - (ulong)next);
1418d98cdf6aSAlexander Graf 	systab.nr_tables--;
1419d98cdf6aSAlexander Graf }
1420d98cdf6aSAlexander Graf 
14216b03cd10SHeinrich Schuchardt /**
142278a88f79SMario Six  * efi_install_configuration_table() - adds, updates, or removes a
142378a88f79SMario Six  *                                     configuration table
142478a88f79SMario Six  * @guid:  GUID of the installed table
142578a88f79SMario Six  * @table: table to be installed
1426332468f7SHeinrich Schuchardt  *
1427332468f7SHeinrich Schuchardt  * This function is used for internal calls. For the API implementation of the
1428332468f7SHeinrich Schuchardt  * InstallConfigurationTable service see efi_install_configuration_table_ext.
1429332468f7SHeinrich Schuchardt  *
143078a88f79SMario Six  * Return: status code
1431332468f7SHeinrich Schuchardt  */
efi_install_configuration_table(const efi_guid_t * guid,void * table)1432ab9efa97SHeinrich Schuchardt efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1433ab9efa97SHeinrich Schuchardt 					     void *table)
1434bee91169SAlexander Graf {
1435b095f3c8SHeinrich Schuchardt 	struct efi_event *evt;
1436bee91169SAlexander Graf 	int i;
1437bee91169SAlexander Graf 
1438eb68b4efSHeinrich Schuchardt 	if (!guid)
1439eb68b4efSHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
1440eb68b4efSHeinrich Schuchardt 
1441b72aaa87SHeinrich Schuchardt 	/* Check for GUID override */
1442bee91169SAlexander Graf 	for (i = 0; i < systab.nr_tables; i++) {
14434182a129SHeinrich Schuchardt 		if (!guidcmp(guid, &systab.tables[i].guid)) {
1444d98cdf6aSAlexander Graf 			if (table)
14454182a129SHeinrich Schuchardt 				systab.tables[i].table = table;
1446d98cdf6aSAlexander Graf 			else
1447d98cdf6aSAlexander Graf 				efi_remove_configuration_table(i);
1448b095f3c8SHeinrich Schuchardt 			goto out;
1449bee91169SAlexander Graf 		}
1450bee91169SAlexander Graf 	}
1451bee91169SAlexander Graf 
1452d98cdf6aSAlexander Graf 	if (!table)
1453d98cdf6aSAlexander Graf 		return EFI_NOT_FOUND;
1454d98cdf6aSAlexander Graf 
1455bee91169SAlexander Graf 	/* No override, check for overflow */
14564182a129SHeinrich Schuchardt 	if (i >= EFI_MAX_CONFIGURATION_TABLES)
1457488bf12dSAlexander Graf 		return EFI_OUT_OF_RESOURCES;
1458bee91169SAlexander Graf 
1459bee91169SAlexander Graf 	/* Add a new entry */
14604182a129SHeinrich Schuchardt 	memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
14614182a129SHeinrich Schuchardt 	systab.tables[i].table = table;
1462aba5e919SAlexander Graf 	systab.nr_tables = i + 1;
1463bee91169SAlexander Graf 
1464b095f3c8SHeinrich Schuchardt out:
1465b72aaa87SHeinrich Schuchardt 	/* systab.nr_tables may have changed. So we need to update the CRC32 */
146655d8ee3bSHeinrich Schuchardt 	efi_update_table_header_crc32(&systab.hdr);
146755d8ee3bSHeinrich Schuchardt 
1468b095f3c8SHeinrich Schuchardt 	/* Notify that the configuration table was changed */
1469b095f3c8SHeinrich Schuchardt 	list_for_each_entry(evt, &efi_events, link) {
1470b095f3c8SHeinrich Schuchardt 		if (evt->group && !guidcmp(evt->group, guid)) {
1471b095f3c8SHeinrich Schuchardt 			efi_signal_event(evt, false);
1472b095f3c8SHeinrich Schuchardt 			break;
1473b095f3c8SHeinrich Schuchardt 		}
1474b095f3c8SHeinrich Schuchardt 	}
1475b095f3c8SHeinrich Schuchardt 
1476488bf12dSAlexander Graf 	return EFI_SUCCESS;
1477488bf12dSAlexander Graf }
1478488bf12dSAlexander Graf 
14796b03cd10SHeinrich Schuchardt /**
148078a88f79SMario Six  * efi_install_configuration_table_ex() - Adds, updates, or removes a
14816b03cd10SHeinrich Schuchardt  *                                        configuration table.
14826b03cd10SHeinrich Schuchardt  * @guid:  GUID of the installed table
14836b03cd10SHeinrich Schuchardt  * @table: table to be installed
148478a88f79SMario Six  *
148578a88f79SMario Six  * This function implements the InstallConfigurationTable service.
148678a88f79SMario Six  *
148778a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
148878a88f79SMario Six  * details.
148978a88f79SMario Six  *
149078a88f79SMario Six  * Return: status code
1491332468f7SHeinrich Schuchardt  */
efi_install_configuration_table_ext(efi_guid_t * guid,void * table)1492488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1493488bf12dSAlexander Graf 							       void *table)
1494488bf12dSAlexander Graf {
1495778e6af8SRob Clark 	EFI_ENTRY("%pUl, %p", guid, table);
1496488bf12dSAlexander Graf 	return EFI_EXIT(efi_install_configuration_table(guid, table));
1497bee91169SAlexander Graf }
1498bee91169SAlexander Graf 
14996b03cd10SHeinrich Schuchardt /**
150078a88f79SMario Six  * efi_setup_loaded_image() - initialize a loaded image
15016b03cd10SHeinrich Schuchardt  *
15026b03cd10SHeinrich Schuchardt  * Initialize a loaded_image_info and loaded_image_info object with correct
150395c5553eSRob Clark  * protocols, boot-device, etc.
1504332468f7SHeinrich Schuchardt  *
150516112f9fSHeinrich Schuchardt  * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
150616112f9fSHeinrich Schuchardt  * code is returned.
150716112f9fSHeinrich Schuchardt  *
150816112f9fSHeinrich Schuchardt  * @device_path:	device path of the loaded image
150916112f9fSHeinrich Schuchardt  * @file_path:		file path of the loaded image
151016112f9fSHeinrich Schuchardt  * @handle_ptr:		handle of the loaded image
151116112f9fSHeinrich Schuchardt  * @info_ptr:		loaded image protocol
151278a88f79SMario Six  * Return:		status code
151395c5553eSRob Clark  */
efi_setup_loaded_image(struct efi_device_path * device_path,struct efi_device_path * file_path,struct efi_loaded_image_obj ** handle_ptr,struct efi_loaded_image ** info_ptr)1514c982874eSHeinrich Schuchardt efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1515c982874eSHeinrich Schuchardt 				    struct efi_device_path *file_path,
1516c982874eSHeinrich Schuchardt 				    struct efi_loaded_image_obj **handle_ptr,
1517c982874eSHeinrich Schuchardt 				    struct efi_loaded_image **info_ptr)
151895c5553eSRob Clark {
151948b66230SHeinrich Schuchardt 	efi_status_t ret;
152016112f9fSHeinrich Schuchardt 	struct efi_loaded_image *info = NULL;
152116112f9fSHeinrich Schuchardt 	struct efi_loaded_image_obj *obj = NULL;
152216112f9fSHeinrich Schuchardt 
152316112f9fSHeinrich Schuchardt 	/* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
152416112f9fSHeinrich Schuchardt 	*handle_ptr = NULL;
152516112f9fSHeinrich Schuchardt 	*info_ptr = NULL;
1526c982874eSHeinrich Schuchardt 
1527c982874eSHeinrich Schuchardt 	info = calloc(1, sizeof(*info));
1528c982874eSHeinrich Schuchardt 	if (!info)
1529c982874eSHeinrich Schuchardt 		return EFI_OUT_OF_RESOURCES;
1530c982874eSHeinrich Schuchardt 	obj = calloc(1, sizeof(*obj));
1531c982874eSHeinrich Schuchardt 	if (!obj) {
1532c982874eSHeinrich Schuchardt 		free(info);
1533c982874eSHeinrich Schuchardt 		return EFI_OUT_OF_RESOURCES;
1534c982874eSHeinrich Schuchardt 	}
153548b66230SHeinrich Schuchardt 
153644549d62SHeinrich Schuchardt 	/* Add internal object to object list */
1537d39646a3SHeinrich Schuchardt 	efi_add_handle(&obj->header);
1538c982874eSHeinrich Schuchardt 
153995147313SHeinrich Schuchardt 	info->revision =  EFI_LOADED_IMAGE_PROTOCOL_REVISION;
154095c5553eSRob Clark 	info->file_path = file_path;
15417e1effceSHeinrich Schuchardt 	info->system_table = &systab;
154295c5553eSRob Clark 
15437df5af6fSHeinrich Schuchardt 	if (device_path) {
15447df5af6fSHeinrich Schuchardt 		info->device_handle = efi_dp_find_obj(device_path, NULL);
154548b66230SHeinrich Schuchardt 		/*
154648b66230SHeinrich Schuchardt 		 * When asking for the device path interface, return
154748b66230SHeinrich Schuchardt 		 * bootefi_device_path
154848b66230SHeinrich Schuchardt 		 */
1549d39646a3SHeinrich Schuchardt 		ret = efi_add_protocol(&obj->header,
1550c982874eSHeinrich Schuchardt 				       &efi_guid_device_path, device_path);
155148b66230SHeinrich Schuchardt 		if (ret != EFI_SUCCESS)
155248b66230SHeinrich Schuchardt 			goto failure;
15537df5af6fSHeinrich Schuchardt 	}
155448b66230SHeinrich Schuchardt 
155548b66230SHeinrich Schuchardt 	/*
155648b66230SHeinrich Schuchardt 	 * When asking for the loaded_image interface, just
155748b66230SHeinrich Schuchardt 	 * return handle which points to loaded_image_info
155848b66230SHeinrich Schuchardt 	 */
1559d39646a3SHeinrich Schuchardt 	ret = efi_add_protocol(&obj->header,
1560c982874eSHeinrich Schuchardt 			       &efi_guid_loaded_image, info);
156148b66230SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
156248b66230SHeinrich Schuchardt 		goto failure;
156348b66230SHeinrich Schuchardt 
15645fbb2895SAlexander Graf #if CONFIG_IS_ENABLED(EFI_LOADER_HII)
1565c9bfb222SLeif Lindholm 	ret = efi_add_protocol(&obj->header,
1566c9bfb222SLeif Lindholm 			       &efi_guid_hii_string_protocol,
1567c9bfb222SLeif Lindholm 			       (void *)&efi_hii_string);
1568c9bfb222SLeif Lindholm 	if (ret != EFI_SUCCESS)
1569c9bfb222SLeif Lindholm 		goto failure;
1570c9bfb222SLeif Lindholm 
1571c9bfb222SLeif Lindholm 	ret = efi_add_protocol(&obj->header,
1572c9bfb222SLeif Lindholm 			       &efi_guid_hii_database_protocol,
1573c9bfb222SLeif Lindholm 			       (void *)&efi_hii_database);
1574c9bfb222SLeif Lindholm 	if (ret != EFI_SUCCESS)
1575c9bfb222SLeif Lindholm 		goto failure;
1576c9bfb222SLeif Lindholm 
1577cb728e51SAKASHI Takahiro 	ret = efi_add_protocol(&obj->header,
1578cb728e51SAKASHI Takahiro 			       &efi_guid_hii_config_routing_protocol,
1579cb728e51SAKASHI Takahiro 			       (void *)&efi_hii_config_routing);
1580cb728e51SAKASHI Takahiro 	if (ret != EFI_SUCCESS)
1581cb728e51SAKASHI Takahiro 		goto failure;
15825fbb2895SAlexander Graf #endif
1583cb728e51SAKASHI Takahiro 
158416112f9fSHeinrich Schuchardt 	*info_ptr = info;
158516112f9fSHeinrich Schuchardt 	*handle_ptr = obj;
158616112f9fSHeinrich Schuchardt 
158756d92888SHeinrich Schuchardt 	return ret;
158848b66230SHeinrich Schuchardt failure:
158948b66230SHeinrich Schuchardt 	printf("ERROR: Failure to install protocols for loaded image\n");
159016112f9fSHeinrich Schuchardt 	efi_delete_handle(&obj->header);
159116112f9fSHeinrich Schuchardt 	free(info);
159256d92888SHeinrich Schuchardt 	return ret;
159395c5553eSRob Clark }
159495c5553eSRob Clark 
15956b03cd10SHeinrich Schuchardt /**
159678a88f79SMario Six  * efi_load_image_from_path() - load an image using a file path
15970e18f584SHeinrich Schuchardt  *
15980e18f584SHeinrich Schuchardt  * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
15990e18f584SHeinrich Schuchardt  * callers obligation to update the memory type as needed.
16000e18f584SHeinrich Schuchardt  *
16016b03cd10SHeinrich Schuchardt  * @file_path:	the path of the image to load
16026b03cd10SHeinrich Schuchardt  * @buffer:	buffer containing the loaded image
16030e18f584SHeinrich Schuchardt  * @size:	size of the loaded image
160478a88f79SMario Six  * Return:	status code
1605332468f7SHeinrich Schuchardt  */
efi_load_image_from_path(struct efi_device_path * file_path,void ** buffer,efi_uintn_t * size)16069975fe96SRob Clark efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
16070e18f584SHeinrich Schuchardt 				      void **buffer, efi_uintn_t *size)
1608838ee4b4SRob Clark {
1609838ee4b4SRob Clark 	struct efi_file_info *info = NULL;
1610838ee4b4SRob Clark 	struct efi_file_handle *f;
1611838ee4b4SRob Clark 	static efi_status_t ret;
16120e18f584SHeinrich Schuchardt 	u64 addr;
1613b6dd5777SHeinrich Schuchardt 	efi_uintn_t bs;
1614838ee4b4SRob Clark 
16150e18f584SHeinrich Schuchardt 	/* In case of failure nothing is returned */
16160e18f584SHeinrich Schuchardt 	*buffer = NULL;
16170e18f584SHeinrich Schuchardt 	*size = 0;
16180e18f584SHeinrich Schuchardt 
16190e18f584SHeinrich Schuchardt 	/* Open file */
1620838ee4b4SRob Clark 	f = efi_file_from_path(file_path);
1621838ee4b4SRob Clark 	if (!f)
1622838ee4b4SRob Clark 		return EFI_DEVICE_ERROR;
1623838ee4b4SRob Clark 
16240e18f584SHeinrich Schuchardt 	/* Get file size */
1625838ee4b4SRob Clark 	bs = 0;
1626838ee4b4SRob Clark 	EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1627838ee4b4SRob Clark 				  &bs, info));
16280e18f584SHeinrich Schuchardt 	if (ret != EFI_BUFFER_TOO_SMALL) {
16290e18f584SHeinrich Schuchardt 		ret =  EFI_DEVICE_ERROR;
16300e18f584SHeinrich Schuchardt 		goto error;
1631838ee4b4SRob Clark 	}
16320e18f584SHeinrich Schuchardt 
16330e18f584SHeinrich Schuchardt 	info = malloc(bs);
16340e18f584SHeinrich Schuchardt 	EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
16350e18f584SHeinrich Schuchardt 				  info));
1636838ee4b4SRob Clark 	if (ret != EFI_SUCCESS)
1637838ee4b4SRob Clark 		goto error;
1638838ee4b4SRob Clark 
16390e18f584SHeinrich Schuchardt 	/*
16400e18f584SHeinrich Schuchardt 	 * When reading the file we do not yet know if it contains an
16410e18f584SHeinrich Schuchardt 	 * application, a boottime driver, or a runtime driver. So here we
16420e18f584SHeinrich Schuchardt 	 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
16430e18f584SHeinrich Schuchardt 	 * update the reservation according to the image type.
16440e18f584SHeinrich Schuchardt 	 */
1645b6dd5777SHeinrich Schuchardt 	bs = info->file_size;
16460e18f584SHeinrich Schuchardt 	ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
16470e18f584SHeinrich Schuchardt 				 EFI_BOOT_SERVICES_DATA,
16480e18f584SHeinrich Schuchardt 				 efi_size_in_pages(bs), &addr);
1649838ee4b4SRob Clark 	if (ret != EFI_SUCCESS) {
16500e18f584SHeinrich Schuchardt 		ret = EFI_OUT_OF_RESOURCES;
16510e18f584SHeinrich Schuchardt 		goto error;
1652838ee4b4SRob Clark 	}
1653838ee4b4SRob Clark 
16540e18f584SHeinrich Schuchardt 	/* Read file */
16550e18f584SHeinrich Schuchardt 	EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
16560e18f584SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
16570e18f584SHeinrich Schuchardt 		efi_free_pages(addr, efi_size_in_pages(bs));
16580e18f584SHeinrich Schuchardt 	*buffer = (void *)(uintptr_t)addr;
16590e18f584SHeinrich Schuchardt 	*size = bs;
16600e18f584SHeinrich Schuchardt error:
16610e18f584SHeinrich Schuchardt 	EFI_CALL(f->close(f));
16620e18f584SHeinrich Schuchardt 	free(info);
1663838ee4b4SRob Clark 	return ret;
1664838ee4b4SRob Clark }
1665838ee4b4SRob Clark 
16666b03cd10SHeinrich Schuchardt /**
166778a88f79SMario Six  * efi_load_image() - load an EFI image into memory
16686b03cd10SHeinrich Schuchardt  * @boot_policy:   true for request originating from the boot manager
16696b03cd10SHeinrich Schuchardt  * @parent_image:  the caller's image handle
16706b03cd10SHeinrich Schuchardt  * @file_path:     the path of the image to load
16716b03cd10SHeinrich Schuchardt  * @source_buffer: memory location from which the image is installed
167278a88f79SMario Six  * @source_size:   size of the memory area from which the image is installed
16736b03cd10SHeinrich Schuchardt  * @image_handle:  handle for the newly installed image
167478a88f79SMario Six  *
167578a88f79SMario Six  * This function implements the LoadImage service.
167678a88f79SMario Six  *
167778a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification
167878a88f79SMario Six  * for details.
167978a88f79SMario Six  *
168078a88f79SMario Six  * Return: status code
1681332468f7SHeinrich Schuchardt  */
efi_load_image(bool boot_policy,efi_handle_t parent_image,struct efi_device_path * file_path,void * source_buffer,efi_uintn_t source_size,efi_handle_t * image_handle)1682bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1683bee91169SAlexander Graf 					  efi_handle_t parent_image,
1684bee91169SAlexander Graf 					  struct efi_device_path *file_path,
1685bee91169SAlexander Graf 					  void *source_buffer,
16867fb96a10SHeinrich Schuchardt 					  efi_uintn_t source_size,
1687bee91169SAlexander Graf 					  efi_handle_t *image_handle)
1688bee91169SAlexander Graf {
16890e18f584SHeinrich Schuchardt 	struct efi_device_path *dp, *fp;
16901c3b2f4aSTom Rini 	struct efi_loaded_image *info = NULL;
1691c982874eSHeinrich Schuchardt 	struct efi_loaded_image_obj **image_obj =
1692c982874eSHeinrich Schuchardt 		(struct efi_loaded_image_obj **)image_handle;
1693b9b17598SHeinrich Schuchardt 	efi_status_t ret;
1694bee91169SAlexander Graf 
16957fb96a10SHeinrich Schuchardt 	EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1696bee91169SAlexander Graf 		  file_path, source_buffer, source_size, image_handle);
1697838ee4b4SRob Clark 
169828a4fd46SHeinrich Schuchardt 	if (!image_handle || !parent_image) {
169928a4fd46SHeinrich Schuchardt 		ret = EFI_INVALID_PARAMETER;
170028a4fd46SHeinrich Schuchardt 		goto error;
170128a4fd46SHeinrich Schuchardt 	}
170228a4fd46SHeinrich Schuchardt 
170328a4fd46SHeinrich Schuchardt 	if (!source_buffer && !file_path) {
170428a4fd46SHeinrich Schuchardt 		ret = EFI_NOT_FOUND;
170528a4fd46SHeinrich Schuchardt 		goto error;
170628a4fd46SHeinrich Schuchardt 	}
170728a4fd46SHeinrich Schuchardt 
1708838ee4b4SRob Clark 	if (!source_buffer) {
17090e18f584SHeinrich Schuchardt 		ret = efi_load_image_from_path(file_path, &source_buffer,
17100e18f584SHeinrich Schuchardt 					       &source_size);
1711b9b17598SHeinrich Schuchardt 		if (ret != EFI_SUCCESS)
17120e18f584SHeinrich Schuchardt 			goto error;
1713838ee4b4SRob Clark 		/*
1714838ee4b4SRob Clark 		 * split file_path which contains both the device and
1715838ee4b4SRob Clark 		 * file parts:
1716838ee4b4SRob Clark 		 */
1717838ee4b4SRob Clark 		efi_dp_split_file_path(file_path, &dp, &fp);
1718838ee4b4SRob Clark 	} else {
1719b72aaa87SHeinrich Schuchardt 		/* In this case, file_path is the "device" path, i.e.
1720838ee4b4SRob Clark 		 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1721838ee4b4SRob Clark 		 */
17220e18f584SHeinrich Schuchardt 		u64 addr;
17230e18f584SHeinrich Schuchardt 		void *dest_buffer;
17240e18f584SHeinrich Schuchardt 
17250e18f584SHeinrich Schuchardt 		ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
17260e18f584SHeinrich Schuchardt 					 EFI_RUNTIME_SERVICES_CODE,
17270e18f584SHeinrich Schuchardt 					 efi_size_in_pages(source_size), &addr);
1728b9b17598SHeinrich Schuchardt 		if (ret != EFI_SUCCESS)
1729c982874eSHeinrich Schuchardt 			goto error;
17300e18f584SHeinrich Schuchardt 		dest_buffer = (void *)(uintptr_t)addr;
17310e18f584SHeinrich Schuchardt 		memcpy(dest_buffer, source_buffer, source_size);
17320e18f584SHeinrich Schuchardt 		source_buffer = dest_buffer;
17330e18f584SHeinrich Schuchardt 
17340e18f584SHeinrich Schuchardt 		dp = file_path;
17350e18f584SHeinrich Schuchardt 		fp = NULL;
1736838ee4b4SRob Clark 	}
17370e18f584SHeinrich Schuchardt 	ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
17380e18f584SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
17390e18f584SHeinrich Schuchardt 		goto error_invalid_image;
17408f7e2b29SHeinrich Schuchardt 	ret = efi_load_pe(*image_obj, source_buffer, info);
17418f7e2b29SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
17420e18f584SHeinrich Schuchardt 		goto error_invalid_image;
17430e18f584SHeinrich Schuchardt 	/* Update the type of the allocated memory */
17440e18f584SHeinrich Schuchardt 	efi_add_memory_map((uintptr_t)source_buffer,
17450e18f584SHeinrich Schuchardt 			   efi_size_in_pages(source_size),
17460e18f584SHeinrich Schuchardt 			   info->image_code_type, false);
174732fc2ac3SHeinrich Schuchardt 	info->system_table = &systab;
174832fc2ac3SHeinrich Schuchardt 	info->parent_handle = parent_image;
1749bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
17500e18f584SHeinrich Schuchardt error_invalid_image:
17510e18f584SHeinrich Schuchardt 	/* The image is invalid. Release all associated resources. */
17520e18f584SHeinrich Schuchardt 	efi_free_pages((uintptr_t)source_buffer,
17530e18f584SHeinrich Schuchardt 		       efi_size_in_pages(source_size));
1754c982874eSHeinrich Schuchardt 	efi_delete_handle(*image_handle);
1755c982874eSHeinrich Schuchardt 	*image_handle = NULL;
1756b9b17598SHeinrich Schuchardt 	free(info);
175728a4fd46SHeinrich Schuchardt error:
1758b9b17598SHeinrich Schuchardt 	return EFI_EXIT(ret);
1759bee91169SAlexander Graf }
1760bee91169SAlexander Graf 
17616b03cd10SHeinrich Schuchardt /**
1762b72aaa87SHeinrich Schuchardt  * efi_start_image() - call the entry point of an image
17636b03cd10SHeinrich Schuchardt  * @image_handle:   handle of the image
17646b03cd10SHeinrich Schuchardt  * @exit_data_size: size of the buffer
17656b03cd10SHeinrich Schuchardt  * @exit_data:      buffer to receive the exit data of the called image
176678a88f79SMario Six  *
176778a88f79SMario Six  * This function implements the StartImage service.
176878a88f79SMario Six  *
176978a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
177078a88f79SMario Six  * details.
177178a88f79SMario Six  *
177278a88f79SMario Six  * Return: status code
1773332468f7SHeinrich Schuchardt  */
efi_start_image(efi_handle_t image_handle,efi_uintn_t * exit_data_size,u16 ** exit_data)1774f69d63faSHeinrich Schuchardt efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1775cc8e3417SHeinrich Schuchardt 				    efi_uintn_t *exit_data_size,
1776cc8e3417SHeinrich Schuchardt 				    u16 **exit_data)
1777bee91169SAlexander Graf {
1778c982874eSHeinrich Schuchardt 	struct efi_loaded_image_obj *image_obj =
1779c982874eSHeinrich Schuchardt 		(struct efi_loaded_image_obj *)image_handle;
1780727a1afbSHeinrich Schuchardt 	efi_status_t ret;
1781bee91169SAlexander Graf 
1782bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1783bee91169SAlexander Graf 
1784f31239acSAlexander Graf 	efi_is_direct_boot = false;
1785f31239acSAlexander Graf 
1786bee91169SAlexander Graf 	/* call the image! */
1787c982874eSHeinrich Schuchardt 	if (setjmp(&image_obj->exit_jmp)) {
1788727a1afbSHeinrich Schuchardt 		/*
1789727a1afbSHeinrich Schuchardt 		 * We called the entry point of the child image with EFI_CALL
1790727a1afbSHeinrich Schuchardt 		 * in the lines below. The child image called the Exit() boot
1791727a1afbSHeinrich Schuchardt 		 * service efi_exit() which executed the long jump that brought
1792727a1afbSHeinrich Schuchardt 		 * us to the current line. This implies that the second half
1793727a1afbSHeinrich Schuchardt 		 * of the EFI_CALL macro has not been executed.
1794727a1afbSHeinrich Schuchardt 		 */
1795727a1afbSHeinrich Schuchardt #ifdef CONFIG_ARM
1796727a1afbSHeinrich Schuchardt 		/*
1797727a1afbSHeinrich Schuchardt 		 * efi_exit() called efi_restore_gd(). We have to undo this
1798727a1afbSHeinrich Schuchardt 		 * otherwise __efi_entry_check() will put the wrong value into
1799727a1afbSHeinrich Schuchardt 		 * app_gd.
1800727a1afbSHeinrich Schuchardt 		 */
1801727a1afbSHeinrich Schuchardt 		gd = app_gd;
1802727a1afbSHeinrich Schuchardt #endif
1803727a1afbSHeinrich Schuchardt 		/*
1804727a1afbSHeinrich Schuchardt 		 * To get ready to call EFI_EXIT below we have to execute the
1805727a1afbSHeinrich Schuchardt 		 * missed out steps of EFI_CALL.
1806727a1afbSHeinrich Schuchardt 		 */
1807727a1afbSHeinrich Schuchardt 		assert(__efi_entry_check());
1808727a1afbSHeinrich Schuchardt 		debug("%sEFI: %lu returned by started image\n",
1809727a1afbSHeinrich Schuchardt 		      __efi_nesting_dec(),
1810c982874eSHeinrich Schuchardt 		      (unsigned long)((uintptr_t)image_obj->exit_status &
1811727a1afbSHeinrich Schuchardt 				      ~EFI_ERROR_MASK));
1812c982874eSHeinrich Schuchardt 		return EFI_EXIT(image_obj->exit_status);
1813a86aeaf2SAlexander Graf 	}
1814a86aeaf2SAlexander Graf 
1815c982874eSHeinrich Schuchardt 	ret = EFI_CALL(image_obj->entry(image_handle, &systab));
1816bee91169SAlexander Graf 
181756672bf5SAlexander Graf 	/*
181856672bf5SAlexander Graf 	 * Usually UEFI applications call Exit() instead of returning.
1819b72aaa87SHeinrich Schuchardt 	 * But because the world doesn't consist of ponies and unicorns,
182056672bf5SAlexander Graf 	 * we're happy to emulate that behavior on behalf of a payload
182156672bf5SAlexander Graf 	 * that forgot.
182256672bf5SAlexander Graf 	 */
182356672bf5SAlexander Graf 	return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1824bee91169SAlexander Graf }
1825bee91169SAlexander Graf 
18266b03cd10SHeinrich Schuchardt /**
182778a88f79SMario Six  * efi_exit() - leave an EFI application or driver
18286b03cd10SHeinrich Schuchardt  * @image_handle:   handle of the application or driver that is exiting
18296b03cd10SHeinrich Schuchardt  * @exit_status:    status code
18306b03cd10SHeinrich Schuchardt  * @exit_data_size: size of the buffer in bytes
18316b03cd10SHeinrich Schuchardt  * @exit_data:      buffer with data describing an error
183278a88f79SMario Six  *
183378a88f79SMario Six  * This function implements the Exit service.
183478a88f79SMario Six  *
183578a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
183678a88f79SMario Six  * details.
183778a88f79SMario Six  *
183878a88f79SMario Six  * Return: status code
1839332468f7SHeinrich Schuchardt  */
efi_exit(efi_handle_t image_handle,efi_status_t exit_status,efi_uintn_t exit_data_size,u16 * exit_data)1840a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1841ab9efa97SHeinrich Schuchardt 				    efi_status_t exit_status,
1842cc8e3417SHeinrich Schuchardt 				    efi_uintn_t exit_data_size,
1843cc8e3417SHeinrich Schuchardt 				    u16 *exit_data)
1844bee91169SAlexander Graf {
184544549d62SHeinrich Schuchardt 	/*
184644549d62SHeinrich Schuchardt 	 * TODO: We should call the unload procedure of the loaded
184744549d62SHeinrich Schuchardt 	 *	 image protocol.
184844549d62SHeinrich Schuchardt 	 */
1849c982874eSHeinrich Schuchardt 	struct efi_loaded_image_obj *image_obj =
1850c982874eSHeinrich Schuchardt 		(struct efi_loaded_image_obj *)image_handle;
1851a86aeaf2SAlexander Graf 
1852cc8e3417SHeinrich Schuchardt 	EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
1853bee91169SAlexander Graf 		  exit_data_size, exit_data);
1854a86aeaf2SAlexander Graf 
1855a148920eSAlexander Graf 	/* Make sure entry/exit counts for EFI world cross-overs match */
1856727a1afbSHeinrich Schuchardt 	EFI_EXIT(exit_status);
1857da94073bSHeinrich Schuchardt 
1858a148920eSAlexander Graf 	/*
1859a148920eSAlexander Graf 	 * But longjmp out with the U-Boot gd, not the application's, as
1860a148920eSAlexander Graf 	 * the other end is a setjmp call inside EFI context.
1861a148920eSAlexander Graf 	 */
1862a148920eSAlexander Graf 	efi_restore_gd();
1863a148920eSAlexander Graf 
1864c982874eSHeinrich Schuchardt 	image_obj->exit_status = exit_status;
1865c982874eSHeinrich Schuchardt 	longjmp(&image_obj->exit_jmp, 1);
1866a86aeaf2SAlexander Graf 
1867a86aeaf2SAlexander Graf 	panic("EFI application exited");
1868bee91169SAlexander Graf }
1869bee91169SAlexander Graf 
18706b03cd10SHeinrich Schuchardt /**
187178a88f79SMario Six  * efi_unload_image() - unload an EFI image
187278a88f79SMario Six  * @image_handle: handle of the image to be unloaded
1873332468f7SHeinrich Schuchardt  *
1874332468f7SHeinrich Schuchardt  * This function implements the UnloadImage service.
1875332468f7SHeinrich Schuchardt  *
187678a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
187778a88f79SMario Six  * details.
187878a88f79SMario Six  *
187978a88f79SMario Six  * Return: status code
1880332468f7SHeinrich Schuchardt  */
efi_unload_image(efi_handle_t image_handle)18812074f700SHeinrich Schuchardt static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1882bee91169SAlexander Graf {
1883bee91169SAlexander Graf 	struct efi_object *efiobj;
1884bee91169SAlexander Graf 
1885bee91169SAlexander Graf 	EFI_ENTRY("%p", image_handle);
1886bee91169SAlexander Graf 	efiobj = efi_search_obj(image_handle);
1887bee91169SAlexander Graf 	if (efiobj)
1888bee91169SAlexander Graf 		list_del(&efiobj->link);
1889bee91169SAlexander Graf 
1890bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
1891bee91169SAlexander Graf }
1892bee91169SAlexander Graf 
18936b03cd10SHeinrich Schuchardt /**
1894f31239acSAlexander Graf  * efi_exit_caches() - fix up caches for EFI payloads if necessary
1895f31239acSAlexander Graf  */
efi_exit_caches(void)1896f31239acSAlexander Graf static void efi_exit_caches(void)
1897f31239acSAlexander Graf {
1898f31239acSAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1899f31239acSAlexander Graf 	/*
1900f31239acSAlexander Graf 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
1901f31239acSAlexander Graf 	 * a zImage, but does not know of all cache layers. Give it a hand.
1902f31239acSAlexander Graf 	 */
1903f31239acSAlexander Graf 	if (efi_is_direct_boot)
1904f31239acSAlexander Graf 		cleanup_before_linux();
1905f31239acSAlexander Graf #endif
1906f31239acSAlexander Graf }
1907f31239acSAlexander Graf 
1908f31239acSAlexander Graf /**
190978a88f79SMario Six  * efi_exit_boot_services() - stop all boot services
191078a88f79SMario Six  * @image_handle: handle of the loaded image
191178a88f79SMario Six  * @map_key:      key of the memory map
1912332468f7SHeinrich Schuchardt  *
1913332468f7SHeinrich Schuchardt  * This function implements the ExitBootServices service.
191478a88f79SMario Six  *
1915332468f7SHeinrich Schuchardt  * See the Unified Extensible Firmware Interface (UEFI) specification
1916332468f7SHeinrich Schuchardt  * for details.
1917332468f7SHeinrich Schuchardt  *
191878a88f79SMario Six  * All timer events are disabled. For exit boot services events the
191978a88f79SMario Six  * notification function is called. The boot services are disabled in the
192078a88f79SMario Six  * system table.
1921cc20ed03SHeinrich Schuchardt  *
192278a88f79SMario Six  * Return: status code
1923332468f7SHeinrich Schuchardt  */
efi_exit_boot_services(efi_handle_t image_handle,unsigned long map_key)19242074f700SHeinrich Schuchardt static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1925bee91169SAlexander Graf 						  unsigned long map_key)
1926bee91169SAlexander Graf {
192743bce442SHeinrich Schuchardt 	struct efi_event *evt;
1928152a263cSHeinrich Schuchardt 
1929bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", image_handle, map_key);
1930bee91169SAlexander Graf 
19311fcb7ea2SHeinrich Schuchardt 	/* Check that the caller has read the current memory map */
19321fcb7ea2SHeinrich Schuchardt 	if (map_key != efi_memory_map_key)
19331fcb7ea2SHeinrich Schuchardt 		return EFI_INVALID_PARAMETER;
19341fcb7ea2SHeinrich Schuchardt 
1935cc20ed03SHeinrich Schuchardt 	/* Make sure that notification functions are not called anymore */
1936cc20ed03SHeinrich Schuchardt 	efi_tpl = TPL_HIGH_LEVEL;
1937cc20ed03SHeinrich Schuchardt 
1938cc20ed03SHeinrich Schuchardt 	/* Check if ExitBootServices has already been called */
1939cc20ed03SHeinrich Schuchardt 	if (!systab.boottime)
1940cc20ed03SHeinrich Schuchardt 		return EFI_EXIT(EFI_SUCCESS);
1941cc20ed03SHeinrich Schuchardt 
1942b095f3c8SHeinrich Schuchardt 	/* Add related events to the event group */
1943b095f3c8SHeinrich Schuchardt 	list_for_each_entry(evt, &efi_events, link) {
1944b095f3c8SHeinrich Schuchardt 		if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1945b095f3c8SHeinrich Schuchardt 			evt->group = &efi_guid_event_group_exit_boot_services;
1946b095f3c8SHeinrich Schuchardt 	}
1947152a263cSHeinrich Schuchardt 	/* Notify that ExitBootServices is invoked. */
194843bce442SHeinrich Schuchardt 	list_for_each_entry(evt, &efi_events, link) {
1949b095f3c8SHeinrich Schuchardt 		if (evt->group &&
1950b095f3c8SHeinrich Schuchardt 		    !guidcmp(evt->group,
1951b095f3c8SHeinrich Schuchardt 			     &efi_guid_event_group_exit_boot_services)) {
195243bce442SHeinrich Schuchardt 			efi_signal_event(evt, false);
1953b095f3c8SHeinrich Schuchardt 			break;
1954b095f3c8SHeinrich Schuchardt 		}
1955152a263cSHeinrich Schuchardt 	}
1956152a263cSHeinrich Schuchardt 
1957b72aaa87SHeinrich Schuchardt 	/* TODO: Should persist EFI variables here */
1958ad644e7cSRob Clark 
1959b7b8410aSAlexander Graf 	board_quiesce_devices();
1960b7b8410aSAlexander Graf 
1961f31239acSAlexander Graf 	/* Fix up caches for EFI payloads if necessary */
1962f31239acSAlexander Graf 	efi_exit_caches();
1963f31239acSAlexander Graf 
1964bee91169SAlexander Graf 	/* This stops all lingering devices */
1965bee91169SAlexander Graf 	bootm_disable_interrupts();
1966bee91169SAlexander Graf 
1967cc20ed03SHeinrich Schuchardt 	/* Disable boot time services */
1968cc20ed03SHeinrich Schuchardt 	systab.con_in_handle = NULL;
1969cc20ed03SHeinrich Schuchardt 	systab.con_in = NULL;
1970cc20ed03SHeinrich Schuchardt 	systab.con_out_handle = NULL;
1971cc20ed03SHeinrich Schuchardt 	systab.con_out = NULL;
1972cc20ed03SHeinrich Schuchardt 	systab.stderr_handle = NULL;
1973cc20ed03SHeinrich Schuchardt 	systab.std_err = NULL;
1974cc20ed03SHeinrich Schuchardt 	systab.boottime = NULL;
1975cc20ed03SHeinrich Schuchardt 
1976cc20ed03SHeinrich Schuchardt 	/* Recalculate CRC32 */
1977640adadfSHeinrich Schuchardt 	efi_update_table_header_crc32(&systab.hdr);
1978cc20ed03SHeinrich Schuchardt 
1979bee91169SAlexander Graf 	/* Give the payload some time to boot */
1980b3d60900SHeinrich Schuchardt 	efi_set_watchdog(0);
1981bee91169SAlexander Graf 	WATCHDOG_RESET();
1982bee91169SAlexander Graf 
1983bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
1984bee91169SAlexander Graf }
1985bee91169SAlexander Graf 
19866b03cd10SHeinrich Schuchardt /**
198778a88f79SMario Six  * efi_get_next_monotonic_count() - get next value of the counter
198878a88f79SMario Six  * @count: returned value of the counter
1989332468f7SHeinrich Schuchardt  *
1990332468f7SHeinrich Schuchardt  * This function implements the NextMonotonicCount service.
1991332468f7SHeinrich Schuchardt  *
199278a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
199378a88f79SMario Six  * details.
199478a88f79SMario Six  *
199578a88f79SMario Six  * Return: status code
1996332468f7SHeinrich Schuchardt  */
efi_get_next_monotonic_count(uint64_t * count)1997bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1998bee91169SAlexander Graf {
1999ab9efa97SHeinrich Schuchardt 	static uint64_t mono;
2000ab9efa97SHeinrich Schuchardt 
2001bee91169SAlexander Graf 	EFI_ENTRY("%p", count);
2002bee91169SAlexander Graf 	*count = mono++;
2003bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
2004bee91169SAlexander Graf }
2005bee91169SAlexander Graf 
20066b03cd10SHeinrich Schuchardt /**
200778a88f79SMario Six  * efi_stall() - sleep
20086b03cd10SHeinrich Schuchardt  * @microseconds: period to sleep in microseconds
200978a88f79SMario Six  *
201078a88f79SMario Six  * This function implements the Stall service.
201178a88f79SMario Six  *
201278a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
201378a88f79SMario Six  * details.
201478a88f79SMario Six  *
201578a88f79SMario Six  * Return:  status code
2016332468f7SHeinrich Schuchardt  */
efi_stall(unsigned long microseconds)2017bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2018bee91169SAlexander Graf {
2019bee91169SAlexander Graf 	EFI_ENTRY("%ld", microseconds);
2020bee91169SAlexander Graf 	udelay(microseconds);
2021bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
2022bee91169SAlexander Graf }
2023bee91169SAlexander Graf 
20246b03cd10SHeinrich Schuchardt /**
202578a88f79SMario Six  * efi_set_watchdog_timer() - reset the watchdog timer
20266b03cd10SHeinrich Schuchardt  * @timeout:       seconds before reset by watchdog
20276b03cd10SHeinrich Schuchardt  * @watchdog_code: code to be logged when resetting
20286b03cd10SHeinrich Schuchardt  * @data_size:     size of buffer in bytes
20296b03cd10SHeinrich Schuchardt  * @watchdog_data: buffer with data describing the reset reason
203078a88f79SMario Six  *
203178a88f79SMario Six  * This function implements the SetWatchdogTimer service.
203278a88f79SMario Six  *
203378a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
203478a88f79SMario Six  * details.
203578a88f79SMario Six  *
203678a88f79SMario Six  * Return: status code
2037332468f7SHeinrich Schuchardt  */
efi_set_watchdog_timer(unsigned long timeout,uint64_t watchdog_code,unsigned long data_size,uint16_t * watchdog_data)2038bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2039bee91169SAlexander Graf 						  uint64_t watchdog_code,
2040bee91169SAlexander Graf 						  unsigned long data_size,
2041bee91169SAlexander Graf 						  uint16_t *watchdog_data)
2042bee91169SAlexander Graf {
2043dee37fc9SMasahiro Yamada 	EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2044bee91169SAlexander Graf 		  data_size, watchdog_data);
2045b3d60900SHeinrich Schuchardt 	return EFI_EXIT(efi_set_watchdog(timeout));
2046bee91169SAlexander Graf }
2047bee91169SAlexander Graf 
20486b03cd10SHeinrich Schuchardt /**
204978a88f79SMario Six  * efi_close_protocol() - close a protocol
20506b03cd10SHeinrich Schuchardt  * @handle:            handle on which the protocol shall be closed
20516b03cd10SHeinrich Schuchardt  * @protocol:          GUID of the protocol to close
20526b03cd10SHeinrich Schuchardt  * @agent_handle:      handle of the driver
20536b03cd10SHeinrich Schuchardt  * @controller_handle: handle of the controller
205478a88f79SMario Six  *
205578a88f79SMario Six  * This function implements the CloseProtocol service.
205678a88f79SMario Six  *
205778a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
205878a88f79SMario Six  * details.
205978a88f79SMario Six  *
206078a88f79SMario Six  * Return: status code
2061332468f7SHeinrich Schuchardt  */
efi_close_protocol(efi_handle_t handle,const efi_guid_t * protocol,efi_handle_t agent_handle,efi_handle_t controller_handle)20622074f700SHeinrich Schuchardt static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
20635a9682d0SHeinrich Schuchardt 					      const efi_guid_t *protocol,
20642074f700SHeinrich Schuchardt 					      efi_handle_t agent_handle,
20652074f700SHeinrich Schuchardt 					      efi_handle_t controller_handle)
2066bee91169SAlexander Graf {
20673b8a489cSHeinrich Schuchardt 	struct efi_handler *handler;
20683b8a489cSHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
20693b8a489cSHeinrich Schuchardt 	struct efi_open_protocol_info_item *pos;
20703b8a489cSHeinrich Schuchardt 	efi_status_t r;
20713b8a489cSHeinrich Schuchardt 
2072778e6af8SRob Clark 	EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2073bee91169SAlexander Graf 		  controller_handle);
20743b8a489cSHeinrich Schuchardt 
20753b8a489cSHeinrich Schuchardt 	if (!agent_handle) {
20763b8a489cSHeinrich Schuchardt 		r = EFI_INVALID_PARAMETER;
20773b8a489cSHeinrich Schuchardt 		goto out;
20783b8a489cSHeinrich Schuchardt 	}
20793b8a489cSHeinrich Schuchardt 	r = efi_search_protocol(handle, protocol, &handler);
20803b8a489cSHeinrich Schuchardt 	if (r != EFI_SUCCESS)
20813b8a489cSHeinrich Schuchardt 		goto out;
20823b8a489cSHeinrich Schuchardt 
20833b8a489cSHeinrich Schuchardt 	r = EFI_NOT_FOUND;
20843b8a489cSHeinrich Schuchardt 	list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
20853b8a489cSHeinrich Schuchardt 		if (item->info.agent_handle == agent_handle &&
20863b8a489cSHeinrich Schuchardt 		    item->info.controller_handle == controller_handle) {
20873b8a489cSHeinrich Schuchardt 			efi_delete_open_info(item);
20883b8a489cSHeinrich Schuchardt 			r = EFI_SUCCESS;
20893b8a489cSHeinrich Schuchardt 			break;
20903b8a489cSHeinrich Schuchardt 		}
20913b8a489cSHeinrich Schuchardt 	}
20923b8a489cSHeinrich Schuchardt out:
20933b8a489cSHeinrich Schuchardt 	return EFI_EXIT(r);
2094bee91169SAlexander Graf }
2095bee91169SAlexander Graf 
20966b03cd10SHeinrich Schuchardt /**
209778a88f79SMario Six  * efi_open_protocol_information() - provide information about then open status
20986b03cd10SHeinrich Schuchardt  *                                   of a protocol on a handle
20996b03cd10SHeinrich Schuchardt  * @handle:       handle for which the information shall be retrieved
21006b03cd10SHeinrich Schuchardt  * @protocol:     GUID of the protocol
21016b03cd10SHeinrich Schuchardt  * @entry_buffer: buffer to receive the open protocol information
21026b03cd10SHeinrich Schuchardt  * @entry_count:  number of entries available in the buffer
210378a88f79SMario Six  *
210478a88f79SMario Six  * This function implements the OpenProtocolInformation service.
210578a88f79SMario Six  *
210678a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
210778a88f79SMario Six  * details.
210878a88f79SMario Six  *
210978a88f79SMario Six  * Return: status code
2110332468f7SHeinrich Schuchardt  */
efi_open_protocol_information(efi_handle_t handle,const efi_guid_t * protocol,struct efi_open_protocol_info_entry ** entry_buffer,efi_uintn_t * entry_count)2111ab9efa97SHeinrich Schuchardt static efi_status_t EFIAPI efi_open_protocol_information(
2112ab9efa97SHeinrich Schuchardt 			efi_handle_t handle, const efi_guid_t *protocol,
2113bee91169SAlexander Graf 			struct efi_open_protocol_info_entry **entry_buffer,
2114f5a2a938SHeinrich Schuchardt 			efi_uintn_t *entry_count)
2115bee91169SAlexander Graf {
2116e3fbbc36SHeinrich Schuchardt 	unsigned long buffer_size;
2117e3fbbc36SHeinrich Schuchardt 	unsigned long count;
2118e3fbbc36SHeinrich Schuchardt 	struct efi_handler *handler;
2119e3fbbc36SHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
2120e3fbbc36SHeinrich Schuchardt 	efi_status_t r;
2121e3fbbc36SHeinrich Schuchardt 
2122778e6af8SRob Clark 	EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2123bee91169SAlexander Graf 		  entry_count);
2124e3fbbc36SHeinrich Schuchardt 
2125e3fbbc36SHeinrich Schuchardt 	/* Check parameters */
2126e3fbbc36SHeinrich Schuchardt 	if (!entry_buffer) {
2127e3fbbc36SHeinrich Schuchardt 		r = EFI_INVALID_PARAMETER;
2128e3fbbc36SHeinrich Schuchardt 		goto out;
2129e3fbbc36SHeinrich Schuchardt 	}
2130e3fbbc36SHeinrich Schuchardt 	r = efi_search_protocol(handle, protocol, &handler);
2131e3fbbc36SHeinrich Schuchardt 	if (r != EFI_SUCCESS)
2132e3fbbc36SHeinrich Schuchardt 		goto out;
2133e3fbbc36SHeinrich Schuchardt 
2134e3fbbc36SHeinrich Schuchardt 	/* Count entries */
2135e3fbbc36SHeinrich Schuchardt 	count = 0;
2136e3fbbc36SHeinrich Schuchardt 	list_for_each_entry(item, &handler->open_infos, link) {
2137e3fbbc36SHeinrich Schuchardt 		if (item->info.open_count)
2138e3fbbc36SHeinrich Schuchardt 			++count;
2139e3fbbc36SHeinrich Schuchardt 	}
2140e3fbbc36SHeinrich Schuchardt 	*entry_count = count;
2141e3fbbc36SHeinrich Schuchardt 	*entry_buffer = NULL;
2142e3fbbc36SHeinrich Schuchardt 	if (!count) {
2143e3fbbc36SHeinrich Schuchardt 		r = EFI_SUCCESS;
2144e3fbbc36SHeinrich Schuchardt 		goto out;
2145e3fbbc36SHeinrich Schuchardt 	}
2146e3fbbc36SHeinrich Schuchardt 
2147e3fbbc36SHeinrich Schuchardt 	/* Copy entries */
2148e3fbbc36SHeinrich Schuchardt 	buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2149eee6530eSHeinrich Schuchardt 	r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2150e3fbbc36SHeinrich Schuchardt 			      (void **)entry_buffer);
2151e3fbbc36SHeinrich Schuchardt 	if (r != EFI_SUCCESS)
2152e3fbbc36SHeinrich Schuchardt 		goto out;
2153e3fbbc36SHeinrich Schuchardt 	list_for_each_entry_reverse(item, &handler->open_infos, link) {
2154e3fbbc36SHeinrich Schuchardt 		if (item->info.open_count)
2155e3fbbc36SHeinrich Schuchardt 			(*entry_buffer)[--count] = item->info;
2156e3fbbc36SHeinrich Schuchardt 	}
2157e3fbbc36SHeinrich Schuchardt out:
2158e3fbbc36SHeinrich Schuchardt 	return EFI_EXIT(r);
2159bee91169SAlexander Graf }
2160bee91169SAlexander Graf 
21616b03cd10SHeinrich Schuchardt /**
216278a88f79SMario Six  * efi_protocols_per_handle() - get protocols installed on a handle
21636b03cd10SHeinrich Schuchardt  * @handle:                handle for which the information is retrieved
21646b03cd10SHeinrich Schuchardt  * @protocol_buffer:       buffer with protocol GUIDs
21656b03cd10SHeinrich Schuchardt  * @protocol_buffer_count: number of entries in the buffer
216678a88f79SMario Six  *
216778a88f79SMario Six  * This function implements the ProtocolsPerHandleService.
216878a88f79SMario Six  *
216978a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
217078a88f79SMario Six  * details.
217178a88f79SMario Six  *
217278a88f79SMario Six  * Return: status code
2173332468f7SHeinrich Schuchardt  */
efi_protocols_per_handle(efi_handle_t handle,efi_guid_t *** protocol_buffer,efi_uintn_t * protocol_buffer_count)21742074f700SHeinrich Schuchardt static efi_status_t EFIAPI efi_protocols_per_handle(
21752074f700SHeinrich Schuchardt 			efi_handle_t handle, efi_guid_t ***protocol_buffer,
2176f5a2a938SHeinrich Schuchardt 			efi_uintn_t *protocol_buffer_count)
2177bee91169SAlexander Graf {
2178c0ebfc86Sxypron.glpk@gmx.de 	unsigned long buffer_size;
2179c0ebfc86Sxypron.glpk@gmx.de 	struct efi_object *efiobj;
218069fb6b1aSHeinrich Schuchardt 	struct list_head *protocol_handle;
2181c0ebfc86Sxypron.glpk@gmx.de 	efi_status_t r;
2182c0ebfc86Sxypron.glpk@gmx.de 
2183bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2184bee91169SAlexander Graf 		  protocol_buffer_count);
2185c0ebfc86Sxypron.glpk@gmx.de 
2186c0ebfc86Sxypron.glpk@gmx.de 	if (!handle || !protocol_buffer || !protocol_buffer_count)
2187c0ebfc86Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
2188c0ebfc86Sxypron.glpk@gmx.de 
2189c0ebfc86Sxypron.glpk@gmx.de 	*protocol_buffer = NULL;
2190661c8327SRob Clark 	*protocol_buffer_count = 0;
2191c0ebfc86Sxypron.glpk@gmx.de 
219269fb6b1aSHeinrich Schuchardt 	efiobj = efi_search_obj(handle);
219369fb6b1aSHeinrich Schuchardt 	if (!efiobj)
219469fb6b1aSHeinrich Schuchardt 		return EFI_EXIT(EFI_INVALID_PARAMETER);
2195c0ebfc86Sxypron.glpk@gmx.de 
2196c0ebfc86Sxypron.glpk@gmx.de 	/* Count protocols */
219769fb6b1aSHeinrich Schuchardt 	list_for_each(protocol_handle, &efiobj->protocols) {
2198c0ebfc86Sxypron.glpk@gmx.de 		++*protocol_buffer_count;
2199c0ebfc86Sxypron.glpk@gmx.de 	}
220069fb6b1aSHeinrich Schuchardt 
2201b72aaa87SHeinrich Schuchardt 	/* Copy GUIDs */
2202c0ebfc86Sxypron.glpk@gmx.de 	if (*protocol_buffer_count) {
220369fb6b1aSHeinrich Schuchardt 		size_t j = 0;
220469fb6b1aSHeinrich Schuchardt 
220569fb6b1aSHeinrich Schuchardt 		buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2206eee6530eSHeinrich Schuchardt 		r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2207c0ebfc86Sxypron.glpk@gmx.de 				      (void **)protocol_buffer);
2208c0ebfc86Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
2209c0ebfc86Sxypron.glpk@gmx.de 			return EFI_EXIT(r);
221069fb6b1aSHeinrich Schuchardt 		list_for_each(protocol_handle, &efiobj->protocols) {
221169fb6b1aSHeinrich Schuchardt 			struct efi_handler *protocol;
221269fb6b1aSHeinrich Schuchardt 
221369fb6b1aSHeinrich Schuchardt 			protocol = list_entry(protocol_handle,
221469fb6b1aSHeinrich Schuchardt 					      struct efi_handler, link);
221569fb6b1aSHeinrich Schuchardt 			(*protocol_buffer)[j] = (void *)protocol->guid;
2216c0ebfc86Sxypron.glpk@gmx.de 			++j;
2217c0ebfc86Sxypron.glpk@gmx.de 		}
2218c0ebfc86Sxypron.glpk@gmx.de 	}
2219c0ebfc86Sxypron.glpk@gmx.de 
2220c0ebfc86Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_SUCCESS);
2221bee91169SAlexander Graf }
2222bee91169SAlexander Graf 
22236b03cd10SHeinrich Schuchardt /**
222478a88f79SMario Six  * efi_locate_handle_buffer() - locate handles implementing a protocol
22256b03cd10SHeinrich Schuchardt  * @search_type: selection criterion
22266b03cd10SHeinrich Schuchardt  * @protocol:    GUID of the protocol
22276b03cd10SHeinrich Schuchardt  * @search_key:  registration key
22286b03cd10SHeinrich Schuchardt  * @no_handles:  number of returned handles
22296b03cd10SHeinrich Schuchardt  * @buffer:      buffer with the returned handles
223078a88f79SMario Six  *
223178a88f79SMario Six  * This function implements the LocateHandleBuffer service.
223278a88f79SMario Six  *
223378a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
223478a88f79SMario Six  * details.
223578a88f79SMario Six  *
223678a88f79SMario Six  * Return: status code
2237332468f7SHeinrich Schuchardt  */
efi_locate_handle_buffer(enum efi_locate_search_type search_type,const efi_guid_t * protocol,void * search_key,efi_uintn_t * no_handles,efi_handle_t ** buffer)2238bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
2239bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
22405a9682d0SHeinrich Schuchardt 			const efi_guid_t *protocol, void *search_key,
2241f5a2a938SHeinrich Schuchardt 			efi_uintn_t *no_handles, efi_handle_t **buffer)
2242bee91169SAlexander Graf {
2243c2e703f9Sxypron.glpk@gmx.de 	efi_status_t r;
2244f5a2a938SHeinrich Schuchardt 	efi_uintn_t buffer_size = 0;
2245c2e703f9Sxypron.glpk@gmx.de 
2246778e6af8SRob Clark 	EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2247bee91169SAlexander Graf 		  no_handles, buffer);
2248c2e703f9Sxypron.glpk@gmx.de 
2249c2e703f9Sxypron.glpk@gmx.de 	if (!no_handles || !buffer) {
2250c2e703f9Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
2251c2e703f9Sxypron.glpk@gmx.de 		goto out;
2252c2e703f9Sxypron.glpk@gmx.de 	}
2253c2e703f9Sxypron.glpk@gmx.de 	*no_handles = 0;
2254c2e703f9Sxypron.glpk@gmx.de 	*buffer = NULL;
2255c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2256c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
2257c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_BUFFER_TOO_SMALL)
2258c2e703f9Sxypron.glpk@gmx.de 		goto out;
2259eee6530eSHeinrich Schuchardt 	r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2260c2e703f9Sxypron.glpk@gmx.de 			      (void **)buffer);
2261c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
2262c2e703f9Sxypron.glpk@gmx.de 		goto out;
2263c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2264c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
2265c2e703f9Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
22662074f700SHeinrich Schuchardt 		*no_handles = buffer_size / sizeof(efi_handle_t);
2267c2e703f9Sxypron.glpk@gmx.de out:
2268c2e703f9Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
2269bee91169SAlexander Graf }
2270bee91169SAlexander Graf 
22716b03cd10SHeinrich Schuchardt /**
227278a88f79SMario Six  * efi_locate_protocol() - find an interface implementing a protocol
22736b03cd10SHeinrich Schuchardt  * @protocol:           GUID of the protocol
22746b03cd10SHeinrich Schuchardt  * @registration:       registration key passed to the notification function
22756b03cd10SHeinrich Schuchardt  * @protocol_interface: interface implementing the protocol
227678a88f79SMario Six  *
227778a88f79SMario Six  * This function implements the LocateProtocol service.
227878a88f79SMario Six  *
227978a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
228078a88f79SMario Six  * details.
228178a88f79SMario Six  *
228278a88f79SMario Six  * Return: status code
2283332468f7SHeinrich Schuchardt  */
efi_locate_protocol(const efi_guid_t * protocol,void * registration,void ** protocol_interface)22845a9682d0SHeinrich Schuchardt static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2285bee91169SAlexander Graf 					       void *registration,
2286bee91169SAlexander Graf 					       void **protocol_interface)
2287bee91169SAlexander Graf {
228888adae5eSxypron.glpk@gmx.de 	struct list_head *lhandle;
22899172cd91SHeinrich Schuchardt 	efi_status_t ret;
2290bee91169SAlexander Graf 
2291778e6af8SRob Clark 	EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
229288adae5eSxypron.glpk@gmx.de 
229388adae5eSxypron.glpk@gmx.de 	if (!protocol || !protocol_interface)
229488adae5eSxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
229588adae5eSxypron.glpk@gmx.de 
229688adae5eSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
229788adae5eSxypron.glpk@gmx.de 		struct efi_object *efiobj;
22989172cd91SHeinrich Schuchardt 		struct efi_handler *handler;
229988adae5eSxypron.glpk@gmx.de 
230088adae5eSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
230188adae5eSxypron.glpk@gmx.de 
2302fae0118eSHeinrich Schuchardt 		ret = efi_search_protocol(efiobj, protocol, &handler);
23039172cd91SHeinrich Schuchardt 		if (ret == EFI_SUCCESS) {
23049172cd91SHeinrich Schuchardt 			*protocol_interface = handler->protocol_interface;
2305bee91169SAlexander Graf 			return EFI_EXIT(EFI_SUCCESS);
2306bee91169SAlexander Graf 		}
2307bee91169SAlexander Graf 	}
230888adae5eSxypron.glpk@gmx.de 	*protocol_interface = NULL;
2309bee91169SAlexander Graf 
2310bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
2311bee91169SAlexander Graf }
2312bee91169SAlexander Graf 
23136b03cd10SHeinrich Schuchardt /**
231478a88f79SMario Six  * efi_locate_device_path() - Get the device path and handle of an device
23156b03cd10SHeinrich Schuchardt  *                            implementing a protocol
23166b03cd10SHeinrich Schuchardt  * @protocol:    GUID of the protocol
23176b03cd10SHeinrich Schuchardt  * @device_path: device path
23186b03cd10SHeinrich Schuchardt  * @device:      handle of the device
231978a88f79SMario Six  *
232078a88f79SMario Six  * This function implements the LocateDevicePath service.
232178a88f79SMario Six  *
232278a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
232378a88f79SMario Six  * details.
232478a88f79SMario Six  *
232578a88f79SMario Six  * Return: status code
2326ae2c85c1SHeinrich Schuchardt  */
efi_locate_device_path(const efi_guid_t * protocol,struct efi_device_path ** device_path,efi_handle_t * device)2327ae2c85c1SHeinrich Schuchardt static efi_status_t EFIAPI efi_locate_device_path(
2328ae2c85c1SHeinrich Schuchardt 			const efi_guid_t *protocol,
2329ae2c85c1SHeinrich Schuchardt 			struct efi_device_path **device_path,
2330ae2c85c1SHeinrich Schuchardt 			efi_handle_t *device)
2331ae2c85c1SHeinrich Schuchardt {
2332ae2c85c1SHeinrich Schuchardt 	struct efi_device_path *dp;
2333ae2c85c1SHeinrich Schuchardt 	size_t i;
2334ae2c85c1SHeinrich Schuchardt 	struct efi_handler *handler;
2335ae2c85c1SHeinrich Schuchardt 	efi_handle_t *handles;
2336ae2c85c1SHeinrich Schuchardt 	size_t len, len_dp;
2337ae2c85c1SHeinrich Schuchardt 	size_t len_best = 0;
2338ae2c85c1SHeinrich Schuchardt 	efi_uintn_t no_handles;
2339ae2c85c1SHeinrich Schuchardt 	u8 *remainder;
2340ae2c85c1SHeinrich Schuchardt 	efi_status_t ret;
2341ae2c85c1SHeinrich Schuchardt 
2342ae2c85c1SHeinrich Schuchardt 	EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2343ae2c85c1SHeinrich Schuchardt 
2344ae2c85c1SHeinrich Schuchardt 	if (!protocol || !device_path || !*device_path || !device) {
2345ae2c85c1SHeinrich Schuchardt 		ret = EFI_INVALID_PARAMETER;
2346ae2c85c1SHeinrich Schuchardt 		goto out;
2347ae2c85c1SHeinrich Schuchardt 	}
2348ae2c85c1SHeinrich Schuchardt 
2349ae2c85c1SHeinrich Schuchardt 	/* Find end of device path */
2350f6dd3f35SHeinrich Schuchardt 	len = efi_dp_instance_size(*device_path);
2351ae2c85c1SHeinrich Schuchardt 
2352ae2c85c1SHeinrich Schuchardt 	/* Get all handles implementing the protocol */
2353ae2c85c1SHeinrich Schuchardt 	ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2354ae2c85c1SHeinrich Schuchardt 						&no_handles, &handles));
2355ae2c85c1SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
2356ae2c85c1SHeinrich Schuchardt 		goto out;
2357ae2c85c1SHeinrich Schuchardt 
2358ae2c85c1SHeinrich Schuchardt 	for (i = 0; i < no_handles; ++i) {
2359ae2c85c1SHeinrich Schuchardt 		/* Find the device path protocol */
2360ae2c85c1SHeinrich Schuchardt 		ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2361ae2c85c1SHeinrich Schuchardt 					  &handler);
2362ae2c85c1SHeinrich Schuchardt 		if (ret != EFI_SUCCESS)
2363ae2c85c1SHeinrich Schuchardt 			continue;
2364ae2c85c1SHeinrich Schuchardt 		dp = (struct efi_device_path *)handler->protocol_interface;
2365f6dd3f35SHeinrich Schuchardt 		len_dp = efi_dp_instance_size(dp);
2366ae2c85c1SHeinrich Schuchardt 		/*
2367ae2c85c1SHeinrich Schuchardt 		 * This handle can only be a better fit
2368ae2c85c1SHeinrich Schuchardt 		 * if its device path length is longer than the best fit and
2369ae2c85c1SHeinrich Schuchardt 		 * if its device path length is shorter of equal the searched
2370ae2c85c1SHeinrich Schuchardt 		 * device path.
2371ae2c85c1SHeinrich Schuchardt 		 */
2372ae2c85c1SHeinrich Schuchardt 		if (len_dp <= len_best || len_dp > len)
2373ae2c85c1SHeinrich Schuchardt 			continue;
2374ae2c85c1SHeinrich Schuchardt 		/* Check if dp is a subpath of device_path */
2375ae2c85c1SHeinrich Schuchardt 		if (memcmp(*device_path, dp, len_dp))
2376ae2c85c1SHeinrich Schuchardt 			continue;
2377ae2c85c1SHeinrich Schuchardt 		*device = handles[i];
2378ae2c85c1SHeinrich Schuchardt 		len_best = len_dp;
2379ae2c85c1SHeinrich Schuchardt 	}
2380ae2c85c1SHeinrich Schuchardt 	if (len_best) {
2381ae2c85c1SHeinrich Schuchardt 		remainder = (u8 *)*device_path + len_best;
2382ae2c85c1SHeinrich Schuchardt 		*device_path = (struct efi_device_path *)remainder;
2383ae2c85c1SHeinrich Schuchardt 		ret = EFI_SUCCESS;
2384ae2c85c1SHeinrich Schuchardt 	} else {
2385ae2c85c1SHeinrich Schuchardt 		ret = EFI_NOT_FOUND;
2386ae2c85c1SHeinrich Schuchardt 	}
2387ae2c85c1SHeinrich Schuchardt out:
2388ae2c85c1SHeinrich Schuchardt 	return EFI_EXIT(ret);
2389ae2c85c1SHeinrich Schuchardt }
2390ae2c85c1SHeinrich Schuchardt 
23916b03cd10SHeinrich Schuchardt /**
239278a88f79SMario Six  * efi_install_multiple_protocol_interfaces() - Install multiple protocol
239378a88f79SMario Six  *                                              interfaces
239478a88f79SMario Six  * @handle: handle on which the protocol interfaces shall be installed
239578a88f79SMario Six  * @...:    NULL terminated argument list with pairs of protocol GUIDS and
239678a88f79SMario Six  *          interfaces
2397332468f7SHeinrich Schuchardt  *
2398332468f7SHeinrich Schuchardt  * This function implements the MultipleProtocolInterfaces service.
2399332468f7SHeinrich Schuchardt  *
240078a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
240178a88f79SMario Six  * details.
240278a88f79SMario Six  *
240378a88f79SMario Six  * Return: status code
2404332468f7SHeinrich Schuchardt  */
efi_install_multiple_protocol_interfaces(efi_handle_t * handle,...)2405faea1041SHeinrich Schuchardt static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2406faea1041SHeinrich Schuchardt 				(efi_handle_t *handle, ...)
2407bee91169SAlexander Graf {
2408bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
240958b83586Sxypron.glpk@gmx.de 
2410beb077a2SAlexander Graf 	efi_va_list argptr;
24115a9682d0SHeinrich Schuchardt 	const efi_guid_t *protocol;
241258b83586Sxypron.glpk@gmx.de 	void *protocol_interface;
241358b83586Sxypron.glpk@gmx.de 	efi_status_t r = EFI_SUCCESS;
241458b83586Sxypron.glpk@gmx.de 	int i = 0;
241558b83586Sxypron.glpk@gmx.de 
241658b83586Sxypron.glpk@gmx.de 	if (!handle)
241758b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
241858b83586Sxypron.glpk@gmx.de 
2419beb077a2SAlexander Graf 	efi_va_start(argptr, handle);
242058b83586Sxypron.glpk@gmx.de 	for (;;) {
2421beb077a2SAlexander Graf 		protocol = efi_va_arg(argptr, efi_guid_t*);
242258b83586Sxypron.glpk@gmx.de 		if (!protocol)
242358b83586Sxypron.glpk@gmx.de 			break;
2424beb077a2SAlexander Graf 		protocol_interface = efi_va_arg(argptr, void*);
24251760ef57SHeinrich Schuchardt 		r = EFI_CALL(efi_install_protocol_interface(
24261760ef57SHeinrich Schuchardt 						handle, protocol,
242758b83586Sxypron.glpk@gmx.de 						EFI_NATIVE_INTERFACE,
24281760ef57SHeinrich Schuchardt 						protocol_interface));
242958b83586Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
243058b83586Sxypron.glpk@gmx.de 			break;
243158b83586Sxypron.glpk@gmx.de 		i++;
243258b83586Sxypron.glpk@gmx.de 	}
2433beb077a2SAlexander Graf 	efi_va_end(argptr);
243458b83586Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
243558b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(r);
243658b83586Sxypron.glpk@gmx.de 
243762471e46SHeinrich Schuchardt 	/* If an error occurred undo all changes. */
2438beb077a2SAlexander Graf 	efi_va_start(argptr, handle);
243958b83586Sxypron.glpk@gmx.de 	for (; i; --i) {
2440beb077a2SAlexander Graf 		protocol = efi_va_arg(argptr, efi_guid_t*);
2441beb077a2SAlexander Graf 		protocol_interface = efi_va_arg(argptr, void*);
2442faea1041SHeinrich Schuchardt 		EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2443cd534083SHeinrich Schuchardt 							  protocol_interface));
244458b83586Sxypron.glpk@gmx.de 	}
2445beb077a2SAlexander Graf 	efi_va_end(argptr);
244658b83586Sxypron.glpk@gmx.de 
244758b83586Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
2448bee91169SAlexander Graf }
2449bee91169SAlexander Graf 
24506b03cd10SHeinrich Schuchardt /**
245178a88f79SMario Six  * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
245278a88f79SMario Six  *                                                interfaces
245378a88f79SMario Six  * @handle: handle from which the protocol interfaces shall be removed
245478a88f79SMario Six  * @...:    NULL terminated argument list with pairs of protocol GUIDS and
24556b03cd10SHeinrich Schuchardt  *          interfaces
2456332468f7SHeinrich Schuchardt  *
2457332468f7SHeinrich Schuchardt  * This function implements the UninstallMultipleProtocolInterfaces service.
2458332468f7SHeinrich Schuchardt  *
245978a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
246078a88f79SMario Six  * details.
246178a88f79SMario Six  *
246278a88f79SMario Six  * Return: status code
2463332468f7SHeinrich Schuchardt  */
efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle,...)2464bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2465faea1041SHeinrich Schuchardt 			efi_handle_t handle, ...)
2466bee91169SAlexander Graf {
2467bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
2468843ce54cSHeinrich Schuchardt 
2469beb077a2SAlexander Graf 	efi_va_list argptr;
2470843ce54cSHeinrich Schuchardt 	const efi_guid_t *protocol;
2471843ce54cSHeinrich Schuchardt 	void *protocol_interface;
2472843ce54cSHeinrich Schuchardt 	efi_status_t r = EFI_SUCCESS;
2473843ce54cSHeinrich Schuchardt 	size_t i = 0;
2474843ce54cSHeinrich Schuchardt 
2475843ce54cSHeinrich Schuchardt 	if (!handle)
2476bee91169SAlexander Graf 		return EFI_EXIT(EFI_INVALID_PARAMETER);
2477843ce54cSHeinrich Schuchardt 
2478beb077a2SAlexander Graf 	efi_va_start(argptr, handle);
2479843ce54cSHeinrich Schuchardt 	for (;;) {
2480beb077a2SAlexander Graf 		protocol = efi_va_arg(argptr, efi_guid_t*);
2481843ce54cSHeinrich Schuchardt 		if (!protocol)
2482843ce54cSHeinrich Schuchardt 			break;
2483beb077a2SAlexander Graf 		protocol_interface = efi_va_arg(argptr, void*);
24849b47f13bSHeinrich Schuchardt 		r = efi_uninstall_protocol(handle, protocol,
24859b47f13bSHeinrich Schuchardt 					   protocol_interface);
2486843ce54cSHeinrich Schuchardt 		if (r != EFI_SUCCESS)
2487843ce54cSHeinrich Schuchardt 			break;
2488843ce54cSHeinrich Schuchardt 		i++;
2489843ce54cSHeinrich Schuchardt 	}
2490beb077a2SAlexander Graf 	efi_va_end(argptr);
24919b47f13bSHeinrich Schuchardt 	if (r == EFI_SUCCESS) {
24929b47f13bSHeinrich Schuchardt 		/* If the last protocol has been removed, delete the handle. */
24939b47f13bSHeinrich Schuchardt 		if (list_empty(&handle->protocols)) {
24949b47f13bSHeinrich Schuchardt 			list_del(&handle->link);
24959b47f13bSHeinrich Schuchardt 			free(handle);
24969b47f13bSHeinrich Schuchardt 		}
2497843ce54cSHeinrich Schuchardt 		return EFI_EXIT(r);
24989b47f13bSHeinrich Schuchardt 	}
2499843ce54cSHeinrich Schuchardt 
2500843ce54cSHeinrich Schuchardt 	/* If an error occurred undo all changes. */
2501beb077a2SAlexander Graf 	efi_va_start(argptr, handle);
2502843ce54cSHeinrich Schuchardt 	for (; i; --i) {
2503beb077a2SAlexander Graf 		protocol = efi_va_arg(argptr, efi_guid_t*);
2504beb077a2SAlexander Graf 		protocol_interface = efi_va_arg(argptr, void*);
2505843ce54cSHeinrich Schuchardt 		EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2506843ce54cSHeinrich Schuchardt 							EFI_NATIVE_INTERFACE,
2507843ce54cSHeinrich Schuchardt 							protocol_interface));
2508843ce54cSHeinrich Schuchardt 	}
2509beb077a2SAlexander Graf 	efi_va_end(argptr);
2510843ce54cSHeinrich Schuchardt 
2511e2373021SHeinrich Schuchardt 	/* In case of an error always return EFI_INVALID_PARAMETER */
2512e2373021SHeinrich Schuchardt 	return EFI_EXIT(EFI_INVALID_PARAMETER);
2513bee91169SAlexander Graf }
2514bee91169SAlexander Graf 
25156b03cd10SHeinrich Schuchardt /**
251678a88f79SMario Six  * efi_calculate_crc32() - calculate cyclic redundancy code
25176b03cd10SHeinrich Schuchardt  * @data:      buffer with data
25186b03cd10SHeinrich Schuchardt  * @data_size: size of buffer in bytes
25196b03cd10SHeinrich Schuchardt  * @crc32_p:   cyclic redundancy code
252078a88f79SMario Six  *
252178a88f79SMario Six  * This function implements the CalculateCrc32 service.
252278a88f79SMario Six  *
252378a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
252478a88f79SMario Six  * details.
252578a88f79SMario Six  *
252678a88f79SMario Six  * Return: status code
2527332468f7SHeinrich Schuchardt  */
efi_calculate_crc32(const void * data,efi_uintn_t data_size,u32 * crc32_p)25288aa8360eSHeinrich Schuchardt static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
25298aa8360eSHeinrich Schuchardt 					       efi_uintn_t data_size,
25308aa8360eSHeinrich Schuchardt 					       u32 *crc32_p)
2531bee91169SAlexander Graf {
25328aa8360eSHeinrich Schuchardt 	EFI_ENTRY("%p, %zu", data, data_size);
2533bee91169SAlexander Graf 	*crc32_p = crc32(0, data, data_size);
2534bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
2535bee91169SAlexander Graf }
2536bee91169SAlexander Graf 
25376b03cd10SHeinrich Schuchardt /**
253878a88f79SMario Six  * efi_copy_mem() - copy memory
25396b03cd10SHeinrich Schuchardt  * @destination: destination of the copy operation
25406b03cd10SHeinrich Schuchardt  * @source:      source of the copy operation
25416b03cd10SHeinrich Schuchardt  * @length:      number of bytes to copy
254278a88f79SMario Six  *
254378a88f79SMario Six  * This function implements the CopyMem service.
254478a88f79SMario Six  *
254578a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
254678a88f79SMario Six  * details.
2547332468f7SHeinrich Schuchardt  */
efi_copy_mem(void * destination,const void * source,size_t length)2548fc05a959SHeinrich Schuchardt static void EFIAPI efi_copy_mem(void *destination, const void *source,
2549fc05a959SHeinrich Schuchardt 				size_t length)
2550bee91169SAlexander Graf {
2551fc05a959SHeinrich Schuchardt 	EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
25520bc81a71SHeinrich Schuchardt 	memmove(destination, source, length);
2553f7c78176SHeinrich Schuchardt 	EFI_EXIT(EFI_SUCCESS);
2554bee91169SAlexander Graf }
2555bee91169SAlexander Graf 
25566b03cd10SHeinrich Schuchardt /**
255778a88f79SMario Six  * efi_set_mem() - Fill memory with a byte value.
25586b03cd10SHeinrich Schuchardt  * @buffer: buffer to fill
25596b03cd10SHeinrich Schuchardt  * @size:   size of buffer in bytes
25606b03cd10SHeinrich Schuchardt  * @value:  byte to copy to the buffer
256178a88f79SMario Six  *
256278a88f79SMario Six  * This function implements the SetMem service.
256378a88f79SMario Six  *
256478a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
256578a88f79SMario Six  * details.
2566332468f7SHeinrich Schuchardt  */
efi_set_mem(void * buffer,size_t size,uint8_t value)2567fc05a959SHeinrich Schuchardt static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2568bee91169SAlexander Graf {
2569fc05a959SHeinrich Schuchardt 	EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2570bee91169SAlexander Graf 	memset(buffer, value, size);
2571f7c78176SHeinrich Schuchardt 	EFI_EXIT(EFI_SUCCESS);
2572bee91169SAlexander Graf }
2573bee91169SAlexander Graf 
25746b03cd10SHeinrich Schuchardt /**
257578a88f79SMario Six  * efi_protocol_open() - open protocol interface on a handle
25766b03cd10SHeinrich Schuchardt  * @handler:            handler of a protocol
25776b03cd10SHeinrich Schuchardt  * @protocol_interface: interface implementing the protocol
25786b03cd10SHeinrich Schuchardt  * @agent_handle:       handle of the driver
25796b03cd10SHeinrich Schuchardt  * @controller_handle:  handle of the controller
25806b03cd10SHeinrich Schuchardt  * @attributes:         attributes indicating how to open the protocol
258178a88f79SMario Six  *
258278a88f79SMario Six  * Return: status code
2583191a41ccSHeinrich Schuchardt  */
efi_protocol_open(struct efi_handler * handler,void ** protocol_interface,void * agent_handle,void * controller_handle,uint32_t attributes)2584191a41ccSHeinrich Schuchardt static efi_status_t efi_protocol_open(
2585191a41ccSHeinrich Schuchardt 			struct efi_handler *handler,
2586191a41ccSHeinrich Schuchardt 			void **protocol_interface, void *agent_handle,
2587191a41ccSHeinrich Schuchardt 			void *controller_handle, uint32_t attributes)
2588191a41ccSHeinrich Schuchardt {
2589191a41ccSHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
2590191a41ccSHeinrich Schuchardt 	struct efi_open_protocol_info_entry *match = NULL;
2591191a41ccSHeinrich Schuchardt 	bool opened_by_driver = false;
2592191a41ccSHeinrich Schuchardt 	bool opened_exclusive = false;
2593191a41ccSHeinrich Schuchardt 
2594191a41ccSHeinrich Schuchardt 	/* If there is no agent, only return the interface */
2595191a41ccSHeinrich Schuchardt 	if (!agent_handle)
2596191a41ccSHeinrich Schuchardt 		goto out;
2597191a41ccSHeinrich Schuchardt 
2598191a41ccSHeinrich Schuchardt 	/* For TEST_PROTOCOL ignore interface attribute */
2599191a41ccSHeinrich Schuchardt 	if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2600191a41ccSHeinrich Schuchardt 		*protocol_interface = NULL;
2601191a41ccSHeinrich Schuchardt 
2602191a41ccSHeinrich Schuchardt 	/*
2603191a41ccSHeinrich Schuchardt 	 * Check if the protocol is already opened by a driver with the same
2604191a41ccSHeinrich Schuchardt 	 * attributes or opened exclusively
2605191a41ccSHeinrich Schuchardt 	 */
2606191a41ccSHeinrich Schuchardt 	list_for_each_entry(item, &handler->open_infos, link) {
2607191a41ccSHeinrich Schuchardt 		if (item->info.agent_handle == agent_handle) {
2608191a41ccSHeinrich Schuchardt 			if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2609191a41ccSHeinrich Schuchardt 			    (item->info.attributes == attributes))
2610191a41ccSHeinrich Schuchardt 				return EFI_ALREADY_STARTED;
2611191a41ccSHeinrich Schuchardt 		}
2612191a41ccSHeinrich Schuchardt 		if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2613191a41ccSHeinrich Schuchardt 			opened_exclusive = true;
2614191a41ccSHeinrich Schuchardt 	}
2615191a41ccSHeinrich Schuchardt 
2616191a41ccSHeinrich Schuchardt 	/* Only one controller can open the protocol exclusively */
2617191a41ccSHeinrich Schuchardt 	if (opened_exclusive && attributes &
2618191a41ccSHeinrich Schuchardt 	    (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2619191a41ccSHeinrich Schuchardt 		return EFI_ACCESS_DENIED;
2620191a41ccSHeinrich Schuchardt 
2621191a41ccSHeinrich Schuchardt 	/* Prepare exclusive opening */
2622191a41ccSHeinrich Schuchardt 	if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2623191a41ccSHeinrich Schuchardt 		/* Try to disconnect controllers */
2624191a41ccSHeinrich Schuchardt 		list_for_each_entry(item, &handler->open_infos, link) {
2625191a41ccSHeinrich Schuchardt 			if (item->info.attributes ==
2626191a41ccSHeinrich Schuchardt 					EFI_OPEN_PROTOCOL_BY_DRIVER)
2627191a41ccSHeinrich Schuchardt 				EFI_CALL(efi_disconnect_controller(
2628191a41ccSHeinrich Schuchardt 						item->info.controller_handle,
2629191a41ccSHeinrich Schuchardt 						item->info.agent_handle,
2630191a41ccSHeinrich Schuchardt 						NULL));
2631191a41ccSHeinrich Schuchardt 		}
2632191a41ccSHeinrich Schuchardt 		opened_by_driver = false;
2633191a41ccSHeinrich Schuchardt 		/* Check if all controllers are disconnected */
2634191a41ccSHeinrich Schuchardt 		list_for_each_entry(item, &handler->open_infos, link) {
2635191a41ccSHeinrich Schuchardt 			if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2636191a41ccSHeinrich Schuchardt 				opened_by_driver = true;
2637191a41ccSHeinrich Schuchardt 		}
26384f37fa47SHeinrich Schuchardt 		/* Only one controller can be connected */
2639191a41ccSHeinrich Schuchardt 		if (opened_by_driver)
2640191a41ccSHeinrich Schuchardt 			return EFI_ACCESS_DENIED;
2641191a41ccSHeinrich Schuchardt 	}
2642191a41ccSHeinrich Schuchardt 
2643191a41ccSHeinrich Schuchardt 	/* Find existing entry */
2644191a41ccSHeinrich Schuchardt 	list_for_each_entry(item, &handler->open_infos, link) {
2645191a41ccSHeinrich Schuchardt 		if (item->info.agent_handle == agent_handle &&
2646191a41ccSHeinrich Schuchardt 		    item->info.controller_handle == controller_handle)
2647191a41ccSHeinrich Schuchardt 			match = &item->info;
2648191a41ccSHeinrich Schuchardt 	}
2649191a41ccSHeinrich Schuchardt 	/* None found, create one */
2650191a41ccSHeinrich Schuchardt 	if (!match) {
2651191a41ccSHeinrich Schuchardt 		match = efi_create_open_info(handler);
2652191a41ccSHeinrich Schuchardt 		if (!match)
2653191a41ccSHeinrich Schuchardt 			return EFI_OUT_OF_RESOURCES;
2654191a41ccSHeinrich Schuchardt 	}
2655191a41ccSHeinrich Schuchardt 
2656191a41ccSHeinrich Schuchardt 	match->agent_handle = agent_handle;
2657191a41ccSHeinrich Schuchardt 	match->controller_handle = controller_handle;
2658191a41ccSHeinrich Schuchardt 	match->attributes = attributes;
2659191a41ccSHeinrich Schuchardt 	match->open_count++;
2660191a41ccSHeinrich Schuchardt 
2661191a41ccSHeinrich Schuchardt out:
2662191a41ccSHeinrich Schuchardt 	/* For TEST_PROTOCOL ignore interface attribute. */
2663191a41ccSHeinrich Schuchardt 	if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2664191a41ccSHeinrich Schuchardt 		*protocol_interface = handler->protocol_interface;
2665191a41ccSHeinrich Schuchardt 
2666191a41ccSHeinrich Schuchardt 	return EFI_SUCCESS;
2667191a41ccSHeinrich Schuchardt }
2668191a41ccSHeinrich Schuchardt 
26696b03cd10SHeinrich Schuchardt /**
267078a88f79SMario Six  * efi_open_protocol() - open protocol interface on a handle
26716b03cd10SHeinrich Schuchardt  * @handle:             handle on which the protocol shall be opened
26726b03cd10SHeinrich Schuchardt  * @protocol:           GUID of the protocol
26736b03cd10SHeinrich Schuchardt  * @protocol_interface: interface implementing the protocol
26746b03cd10SHeinrich Schuchardt  * @agent_handle:       handle of the driver
26756b03cd10SHeinrich Schuchardt  * @controller_handle:  handle of the controller
26766b03cd10SHeinrich Schuchardt  * @attributes:         attributes indicating how to open the protocol
267778a88f79SMario Six  *
267878a88f79SMario Six  * This function implements the OpenProtocol interface.
267978a88f79SMario Six  *
268078a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
268178a88f79SMario Six  * details.
268278a88f79SMario Six  *
268378a88f79SMario Six  * Return: status code
2684332468f7SHeinrich Schuchardt  */
efi_open_protocol(efi_handle_t handle,const efi_guid_t * protocol,void ** protocol_interface,efi_handle_t agent_handle,efi_handle_t controller_handle,uint32_t attributes)2685faea1041SHeinrich Schuchardt static efi_status_t EFIAPI efi_open_protocol
2686faea1041SHeinrich Schuchardt 			(efi_handle_t handle, const efi_guid_t *protocol,
2687faea1041SHeinrich Schuchardt 			 void **protocol_interface, efi_handle_t agent_handle,
2688faea1041SHeinrich Schuchardt 			 efi_handle_t controller_handle, uint32_t attributes)
2689bee91169SAlexander Graf {
269080286e8fSHeinrich Schuchardt 	struct efi_handler *handler;
269169baec67Sxypron.glpk@gmx.de 	efi_status_t r = EFI_INVALID_PARAMETER;
2692bee91169SAlexander Graf 
2693778e6af8SRob Clark 	EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2694bee91169SAlexander Graf 		  protocol_interface, agent_handle, controller_handle,
2695bee91169SAlexander Graf 		  attributes);
2696b5349f74Sxypron.glpk@gmx.de 
269769baec67Sxypron.glpk@gmx.de 	if (!handle || !protocol ||
269869baec67Sxypron.glpk@gmx.de 	    (!protocol_interface && attributes !=
269969baec67Sxypron.glpk@gmx.de 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
270069baec67Sxypron.glpk@gmx.de 		goto out;
270169baec67Sxypron.glpk@gmx.de 	}
270269baec67Sxypron.glpk@gmx.de 
270369baec67Sxypron.glpk@gmx.de 	switch (attributes) {
270469baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
270569baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
270669baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
270769baec67Sxypron.glpk@gmx.de 		break;
270869baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
270969baec67Sxypron.glpk@gmx.de 		if (controller_handle == handle)
271069baec67Sxypron.glpk@gmx.de 			goto out;
2711191a41ccSHeinrich Schuchardt 		/* fall-through */
271269baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
271369baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2714191a41ccSHeinrich Schuchardt 		/* Check that the controller handle is valid */
2715191a41ccSHeinrich Schuchardt 		if (!efi_search_obj(controller_handle))
271669baec67Sxypron.glpk@gmx.de 			goto out;
2717191a41ccSHeinrich Schuchardt 		/* fall-through */
271869baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2719191a41ccSHeinrich Schuchardt 		/* Check that the agent handle is valid */
2720191a41ccSHeinrich Schuchardt 		if (!efi_search_obj(agent_handle))
272169baec67Sxypron.glpk@gmx.de 			goto out;
272269baec67Sxypron.glpk@gmx.de 		break;
272369baec67Sxypron.glpk@gmx.de 	default:
2724b5349f74Sxypron.glpk@gmx.de 		goto out;
2725b5349f74Sxypron.glpk@gmx.de 	}
2726b5349f74Sxypron.glpk@gmx.de 
272780286e8fSHeinrich Schuchardt 	r = efi_search_protocol(handle, protocol, &handler);
272880286e8fSHeinrich Schuchardt 	if (r != EFI_SUCCESS)
2729bee91169SAlexander Graf 		goto out;
2730bee91169SAlexander Graf 
2731191a41ccSHeinrich Schuchardt 	r = efi_protocol_open(handler, protocol_interface, agent_handle,
2732191a41ccSHeinrich Schuchardt 			      controller_handle, attributes);
2733bee91169SAlexander Graf out:
2734bee91169SAlexander Graf 	return EFI_EXIT(r);
2735bee91169SAlexander Graf }
2736bee91169SAlexander Graf 
27376b03cd10SHeinrich Schuchardt /**
273878a88f79SMario Six  * efi_handle_protocol() - get interface of a protocol on a handle
27396b03cd10SHeinrich Schuchardt  * @handle:             handle on which the protocol shall be opened
27406b03cd10SHeinrich Schuchardt  * @protocol:           GUID of the protocol
27416b03cd10SHeinrich Schuchardt  * @protocol_interface: interface implementing the protocol
274278a88f79SMario Six  *
274378a88f79SMario Six  * This function implements the HandleProtocol service.
274478a88f79SMario Six  *
274578a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
274678a88f79SMario Six  * details.
274778a88f79SMario Six  *
274878a88f79SMario Six  * Return: status code
2749332468f7SHeinrich Schuchardt  */
efi_handle_protocol(efi_handle_t handle,const efi_guid_t * protocol,void ** protocol_interface)27502074f700SHeinrich Schuchardt static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
27515a9682d0SHeinrich Schuchardt 					       const efi_guid_t *protocol,
2752bee91169SAlexander Graf 					       void **protocol_interface)
2753bee91169SAlexander Graf {
27548e1d329fSxypron.glpk@gmx.de 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
27558e1d329fSxypron.glpk@gmx.de 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2756bee91169SAlexander Graf }
2757bee91169SAlexander Graf 
27586b03cd10SHeinrich Schuchardt /**
275978a88f79SMario Six  * efi_bind_controller() - bind a single driver to a controller
27606b03cd10SHeinrich Schuchardt  * @controller_handle:   controller handle
27616b03cd10SHeinrich Schuchardt  * @driver_image_handle: driver handle
27626b03cd10SHeinrich Schuchardt  * @remain_device_path:  remaining path
276378a88f79SMario Six  *
276478a88f79SMario Six  * Return: status code
27656b03cd10SHeinrich Schuchardt  */
efi_bind_controller(efi_handle_t controller_handle,efi_handle_t driver_image_handle,struct efi_device_path * remain_device_path)2766f0959dbeSHeinrich Schuchardt static efi_status_t efi_bind_controller(
2767f0959dbeSHeinrich Schuchardt 			efi_handle_t controller_handle,
2768f0959dbeSHeinrich Schuchardt 			efi_handle_t driver_image_handle,
2769f0959dbeSHeinrich Schuchardt 			struct efi_device_path *remain_device_path)
2770f0959dbeSHeinrich Schuchardt {
2771f0959dbeSHeinrich Schuchardt 	struct efi_driver_binding_protocol *binding_protocol;
2772f0959dbeSHeinrich Schuchardt 	efi_status_t r;
2773f0959dbeSHeinrich Schuchardt 
2774f0959dbeSHeinrich Schuchardt 	r = EFI_CALL(efi_open_protocol(driver_image_handle,
2775f0959dbeSHeinrich Schuchardt 				       &efi_guid_driver_binding_protocol,
2776f0959dbeSHeinrich Schuchardt 				       (void **)&binding_protocol,
2777f0959dbeSHeinrich Schuchardt 				       driver_image_handle, NULL,
2778f0959dbeSHeinrich Schuchardt 				       EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2779f0959dbeSHeinrich Schuchardt 	if (r != EFI_SUCCESS)
2780f0959dbeSHeinrich Schuchardt 		return r;
2781f0959dbeSHeinrich Schuchardt 	r = EFI_CALL(binding_protocol->supported(binding_protocol,
2782f0959dbeSHeinrich Schuchardt 						 controller_handle,
2783f0959dbeSHeinrich Schuchardt 						 remain_device_path));
2784f0959dbeSHeinrich Schuchardt 	if (r == EFI_SUCCESS)
2785f0959dbeSHeinrich Schuchardt 		r = EFI_CALL(binding_protocol->start(binding_protocol,
2786f0959dbeSHeinrich Schuchardt 						     controller_handle,
2787f0959dbeSHeinrich Schuchardt 						     remain_device_path));
2788f0959dbeSHeinrich Schuchardt 	EFI_CALL(efi_close_protocol(driver_image_handle,
2789f0959dbeSHeinrich Schuchardt 				    &efi_guid_driver_binding_protocol,
2790f0959dbeSHeinrich Schuchardt 				    driver_image_handle, NULL));
2791f0959dbeSHeinrich Schuchardt 	return r;
2792f0959dbeSHeinrich Schuchardt }
2793f0959dbeSHeinrich Schuchardt 
27946b03cd10SHeinrich Schuchardt /**
279578a88f79SMario Six  * efi_connect_single_controller() - connect a single driver to a controller
27966b03cd10SHeinrich Schuchardt  * @controller_handle:   controller
27976b03cd10SHeinrich Schuchardt  * @driver_image_handle: driver
2798b72aaa87SHeinrich Schuchardt  * @remain_device_path:  remaining path
279978a88f79SMario Six  *
280078a88f79SMario Six  * Return: status code
28016b03cd10SHeinrich Schuchardt  */
efi_connect_single_controller(efi_handle_t controller_handle,efi_handle_t * driver_image_handle,struct efi_device_path * remain_device_path)2802f0959dbeSHeinrich Schuchardt static efi_status_t efi_connect_single_controller(
2803f0959dbeSHeinrich Schuchardt 			efi_handle_t controller_handle,
2804f0959dbeSHeinrich Schuchardt 			efi_handle_t *driver_image_handle,
2805f0959dbeSHeinrich Schuchardt 			struct efi_device_path *remain_device_path)
2806f0959dbeSHeinrich Schuchardt {
2807f0959dbeSHeinrich Schuchardt 	efi_handle_t *buffer;
2808f0959dbeSHeinrich Schuchardt 	size_t count;
2809f0959dbeSHeinrich Schuchardt 	size_t i;
2810f0959dbeSHeinrich Schuchardt 	efi_status_t r;
2811f0959dbeSHeinrich Schuchardt 	size_t connected = 0;
2812f0959dbeSHeinrich Schuchardt 
2813f0959dbeSHeinrich Schuchardt 	/* Get buffer with all handles with driver binding protocol */
2814f0959dbeSHeinrich Schuchardt 	r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2815f0959dbeSHeinrich Schuchardt 					      &efi_guid_driver_binding_protocol,
2816f0959dbeSHeinrich Schuchardt 					      NULL, &count, &buffer));
2817f0959dbeSHeinrich Schuchardt 	if (r != EFI_SUCCESS)
2818f0959dbeSHeinrich Schuchardt 		return r;
2819f0959dbeSHeinrich Schuchardt 
2820f0959dbeSHeinrich Schuchardt 	/*  Context Override */
2821f0959dbeSHeinrich Schuchardt 	if (driver_image_handle) {
2822f0959dbeSHeinrich Schuchardt 		for (; *driver_image_handle; ++driver_image_handle) {
2823f0959dbeSHeinrich Schuchardt 			for (i = 0; i < count; ++i) {
2824f0959dbeSHeinrich Schuchardt 				if (buffer[i] == *driver_image_handle) {
2825f0959dbeSHeinrich Schuchardt 					buffer[i] = NULL;
2826f0959dbeSHeinrich Schuchardt 					r = efi_bind_controller(
2827f0959dbeSHeinrich Schuchardt 							controller_handle,
2828f0959dbeSHeinrich Schuchardt 							*driver_image_handle,
2829f0959dbeSHeinrich Schuchardt 							remain_device_path);
2830f0959dbeSHeinrich Schuchardt 					/*
2831f0959dbeSHeinrich Schuchardt 					 * For drivers that do not support the
2832f0959dbeSHeinrich Schuchardt 					 * controller or are already connected
2833f0959dbeSHeinrich Schuchardt 					 * we receive an error code here.
2834f0959dbeSHeinrich Schuchardt 					 */
2835f0959dbeSHeinrich Schuchardt 					if (r == EFI_SUCCESS)
2836f0959dbeSHeinrich Schuchardt 						++connected;
2837f0959dbeSHeinrich Schuchardt 				}
2838f0959dbeSHeinrich Schuchardt 			}
2839f0959dbeSHeinrich Schuchardt 		}
2840f0959dbeSHeinrich Schuchardt 	}
2841f0959dbeSHeinrich Schuchardt 
2842f0959dbeSHeinrich Schuchardt 	/*
2843f0959dbeSHeinrich Schuchardt 	 * TODO: Some overrides are not yet implemented:
2844f0959dbeSHeinrich Schuchardt 	 * - Platform Driver Override
2845f0959dbeSHeinrich Schuchardt 	 * - Driver Family Override Search
2846f0959dbeSHeinrich Schuchardt 	 * - Bus Specific Driver Override
2847f0959dbeSHeinrich Schuchardt 	 */
2848f0959dbeSHeinrich Schuchardt 
2849f0959dbeSHeinrich Schuchardt 	/* Driver Binding Search */
2850f0959dbeSHeinrich Schuchardt 	for (i = 0; i < count; ++i) {
2851f0959dbeSHeinrich Schuchardt 		if (buffer[i]) {
2852f0959dbeSHeinrich Schuchardt 			r = efi_bind_controller(controller_handle,
2853f0959dbeSHeinrich Schuchardt 						buffer[i],
2854f0959dbeSHeinrich Schuchardt 						remain_device_path);
2855f0959dbeSHeinrich Schuchardt 			if (r == EFI_SUCCESS)
2856f0959dbeSHeinrich Schuchardt 				++connected;
2857f0959dbeSHeinrich Schuchardt 		}
2858f0959dbeSHeinrich Schuchardt 	}
2859f0959dbeSHeinrich Schuchardt 
2860f0959dbeSHeinrich Schuchardt 	efi_free_pool(buffer);
2861f0959dbeSHeinrich Schuchardt 	if (!connected)
2862f0959dbeSHeinrich Schuchardt 		return EFI_NOT_FOUND;
2863f0959dbeSHeinrich Schuchardt 	return EFI_SUCCESS;
2864f0959dbeSHeinrich Schuchardt }
2865f0959dbeSHeinrich Schuchardt 
28666b03cd10SHeinrich Schuchardt /**
286778a88f79SMario Six  * efi_connect_controller() - connect a controller to a driver
286878a88f79SMario Six  * @controller_handle:   handle of the controller
286978a88f79SMario Six  * @driver_image_handle: handle of the driver
287078a88f79SMario Six  * @remain_device_path:  device path of a child controller
287178a88f79SMario Six  * @recursive:           true to connect all child controllers
2872f0959dbeSHeinrich Schuchardt  *
2873f0959dbeSHeinrich Schuchardt  * This function implements the ConnectController service.
287478a88f79SMario Six  *
287578a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
287678a88f79SMario Six  * details.
2877f0959dbeSHeinrich Schuchardt  *
2878f0959dbeSHeinrich Schuchardt  * First all driver binding protocol handles are tried for binding drivers.
2879b72aaa87SHeinrich Schuchardt  * Afterwards all handles that have opened a protocol of the controller
2880f0959dbeSHeinrich Schuchardt  * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2881f0959dbeSHeinrich Schuchardt  *
288278a88f79SMario Six  * Return: status code
2883f0959dbeSHeinrich Schuchardt  */
efi_connect_controller(efi_handle_t controller_handle,efi_handle_t * driver_image_handle,struct efi_device_path * remain_device_path,bool recursive)2884f0959dbeSHeinrich Schuchardt static efi_status_t EFIAPI efi_connect_controller(
2885f0959dbeSHeinrich Schuchardt 			efi_handle_t controller_handle,
2886f0959dbeSHeinrich Schuchardt 			efi_handle_t *driver_image_handle,
2887f0959dbeSHeinrich Schuchardt 			struct efi_device_path *remain_device_path,
2888f0959dbeSHeinrich Schuchardt 			bool recursive)
2889f0959dbeSHeinrich Schuchardt {
2890f0959dbeSHeinrich Schuchardt 	efi_status_t r;
2891f0959dbeSHeinrich Schuchardt 	efi_status_t ret = EFI_NOT_FOUND;
2892f0959dbeSHeinrich Schuchardt 	struct efi_object *efiobj;
2893f0959dbeSHeinrich Schuchardt 
2894d178836bSHeinrich Schuchardt 	EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
2895f0959dbeSHeinrich Schuchardt 		  remain_device_path, recursive);
2896f0959dbeSHeinrich Schuchardt 
2897f0959dbeSHeinrich Schuchardt 	efiobj = efi_search_obj(controller_handle);
2898f0959dbeSHeinrich Schuchardt 	if (!efiobj) {
2899f0959dbeSHeinrich Schuchardt 		ret = EFI_INVALID_PARAMETER;
2900f0959dbeSHeinrich Schuchardt 		goto out;
2901f0959dbeSHeinrich Schuchardt 	}
2902f0959dbeSHeinrich Schuchardt 
2903f0959dbeSHeinrich Schuchardt 	r = efi_connect_single_controller(controller_handle,
2904f0959dbeSHeinrich Schuchardt 					  driver_image_handle,
2905f0959dbeSHeinrich Schuchardt 					  remain_device_path);
2906f0959dbeSHeinrich Schuchardt 	if (r == EFI_SUCCESS)
2907f0959dbeSHeinrich Schuchardt 		ret = EFI_SUCCESS;
2908f0959dbeSHeinrich Schuchardt 	if (recursive) {
2909f0959dbeSHeinrich Schuchardt 		struct efi_handler *handler;
2910f0959dbeSHeinrich Schuchardt 		struct efi_open_protocol_info_item *item;
2911f0959dbeSHeinrich Schuchardt 
2912f0959dbeSHeinrich Schuchardt 		list_for_each_entry(handler, &efiobj->protocols, link) {
2913f0959dbeSHeinrich Schuchardt 			list_for_each_entry(item, &handler->open_infos, link) {
2914f0959dbeSHeinrich Schuchardt 				if (item->info.attributes &
2915f0959dbeSHeinrich Schuchardt 				    EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2916f0959dbeSHeinrich Schuchardt 					r = EFI_CALL(efi_connect_controller(
2917f0959dbeSHeinrich Schuchardt 						item->info.controller_handle,
2918f0959dbeSHeinrich Schuchardt 						driver_image_handle,
2919f0959dbeSHeinrich Schuchardt 						remain_device_path,
2920f0959dbeSHeinrich Schuchardt 						recursive));
2921f0959dbeSHeinrich Schuchardt 					if (r == EFI_SUCCESS)
2922f0959dbeSHeinrich Schuchardt 						ret = EFI_SUCCESS;
2923f0959dbeSHeinrich Schuchardt 				}
2924f0959dbeSHeinrich Schuchardt 			}
2925f0959dbeSHeinrich Schuchardt 		}
2926f0959dbeSHeinrich Schuchardt 	}
2927f0959dbeSHeinrich Schuchardt 	/*  Check for child controller specified by end node */
2928f0959dbeSHeinrich Schuchardt 	if (ret != EFI_SUCCESS && remain_device_path &&
2929f0959dbeSHeinrich Schuchardt 	    remain_device_path->type == DEVICE_PATH_TYPE_END)
2930f0959dbeSHeinrich Schuchardt 		ret = EFI_SUCCESS;
2931f0959dbeSHeinrich Schuchardt out:
2932f0959dbeSHeinrich Schuchardt 	return EFI_EXIT(ret);
2933f0959dbeSHeinrich Schuchardt }
2934f0959dbeSHeinrich Schuchardt 
29356b03cd10SHeinrich Schuchardt /**
293678a88f79SMario Six  * efi_reinstall_protocol_interface() - reinstall protocol interface
293778a88f79SMario Six  * @handle:        handle on which the protocol shall be reinstalled
293878a88f79SMario Six  * @protocol:      GUID of the protocol to be installed
293978a88f79SMario Six  * @old_interface: interface to be removed
294078a88f79SMario Six  * @new_interface: interface to be installed
2941e861a120SHeinrich Schuchardt  *
2942e861a120SHeinrich Schuchardt  * This function implements the ReinstallProtocolInterface service.
294378a88f79SMario Six  *
294478a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
294578a88f79SMario Six  * details.
2946e861a120SHeinrich Schuchardt  *
2947e861a120SHeinrich Schuchardt  * The old interface is uninstalled. The new interface is installed.
2948e861a120SHeinrich Schuchardt  * Drivers are connected.
2949e861a120SHeinrich Schuchardt  *
295078a88f79SMario Six  * Return: status code
2951e861a120SHeinrich Schuchardt  */
efi_reinstall_protocol_interface(efi_handle_t handle,const efi_guid_t * protocol,void * old_interface,void * new_interface)2952e861a120SHeinrich Schuchardt static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2953e861a120SHeinrich Schuchardt 			efi_handle_t handle, const efi_guid_t *protocol,
2954e861a120SHeinrich Schuchardt 			void *old_interface, void *new_interface)
2955e861a120SHeinrich Schuchardt {
2956e861a120SHeinrich Schuchardt 	efi_status_t ret;
2957e861a120SHeinrich Schuchardt 
2958e861a120SHeinrich Schuchardt 	EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2959e861a120SHeinrich Schuchardt 		  new_interface);
29609b47f13bSHeinrich Schuchardt 
29619b47f13bSHeinrich Schuchardt 	/* Uninstall protocol but do not delete handle */
29629b47f13bSHeinrich Schuchardt 	ret = efi_uninstall_protocol(handle, protocol, old_interface);
2963e861a120SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
2964e861a120SHeinrich Schuchardt 		goto out;
29659b47f13bSHeinrich Schuchardt 
29669b47f13bSHeinrich Schuchardt 	/* Install the new protocol */
29679b47f13bSHeinrich Schuchardt 	ret = efi_add_protocol(handle, protocol, new_interface);
29689b47f13bSHeinrich Schuchardt 	/*
29699b47f13bSHeinrich Schuchardt 	 * The UEFI spec does not specify what should happen to the handle
29709b47f13bSHeinrich Schuchardt 	 * if in case of an error no protocol interface remains on the handle.
29719b47f13bSHeinrich Schuchardt 	 * So let's do nothing here.
29729b47f13bSHeinrich Schuchardt 	 */
2973e861a120SHeinrich Schuchardt 	if (ret != EFI_SUCCESS)
2974e861a120SHeinrich Schuchardt 		goto out;
2975e861a120SHeinrich Schuchardt 	/*
2976e861a120SHeinrich Schuchardt 	 * The returned status code has to be ignored.
2977e861a120SHeinrich Schuchardt 	 * Do not create an error if no suitable driver for the handle exists.
2978e861a120SHeinrich Schuchardt 	 */
2979e861a120SHeinrich Schuchardt 	EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2980e861a120SHeinrich Schuchardt out:
2981e861a120SHeinrich Schuchardt 	return EFI_EXIT(ret);
2982e861a120SHeinrich Schuchardt }
2983e861a120SHeinrich Schuchardt 
29846b03cd10SHeinrich Schuchardt /**
298578a88f79SMario Six  * efi_get_child_controllers() - get all child controllers associated to a driver
29866b03cd10SHeinrich Schuchardt  * @efiobj:              handle of the controller
29876b03cd10SHeinrich Schuchardt  * @driver_handle:       handle of the driver
29886b03cd10SHeinrich Schuchardt  * @number_of_children:  number of child controllers
29896b03cd10SHeinrich Schuchardt  * @child_handle_buffer: handles of the the child controllers
299078a88f79SMario Six  *
299178a88f79SMario Six  * The allocated buffer has to be freed with free().
299278a88f79SMario Six  *
299378a88f79SMario Six  * Return: status code
29943f9b0042SHeinrich Schuchardt  */
efi_get_child_controllers(struct efi_object * efiobj,efi_handle_t driver_handle,efi_uintn_t * number_of_children,efi_handle_t ** child_handle_buffer)29953f9b0042SHeinrich Schuchardt static efi_status_t efi_get_child_controllers(
29963f9b0042SHeinrich Schuchardt 				struct efi_object *efiobj,
29973f9b0042SHeinrich Schuchardt 				efi_handle_t driver_handle,
29983f9b0042SHeinrich Schuchardt 				efi_uintn_t *number_of_children,
29993f9b0042SHeinrich Schuchardt 				efi_handle_t **child_handle_buffer)
30003f9b0042SHeinrich Schuchardt {
30013f9b0042SHeinrich Schuchardt 	struct efi_handler *handler;
30023f9b0042SHeinrich Schuchardt 	struct efi_open_protocol_info_item *item;
30033f9b0042SHeinrich Schuchardt 	efi_uintn_t count = 0, i;
30043f9b0042SHeinrich Schuchardt 	bool duplicate;
30053f9b0042SHeinrich Schuchardt 
30063f9b0042SHeinrich Schuchardt 	/* Count all child controller associations */
30073f9b0042SHeinrich Schuchardt 	list_for_each_entry(handler, &efiobj->protocols, link) {
30083f9b0042SHeinrich Schuchardt 		list_for_each_entry(item, &handler->open_infos, link) {
30093f9b0042SHeinrich Schuchardt 			if (item->info.agent_handle == driver_handle &&
30103f9b0042SHeinrich Schuchardt 			    item->info.attributes &
30113f9b0042SHeinrich Schuchardt 			    EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
30123f9b0042SHeinrich Schuchardt 				++count;
30133f9b0042SHeinrich Schuchardt 		}
30143f9b0042SHeinrich Schuchardt 	}
30153f9b0042SHeinrich Schuchardt 	/*
30163f9b0042SHeinrich Schuchardt 	 * Create buffer. In case of duplicate child controller assignments
30173f9b0042SHeinrich Schuchardt 	 * the buffer will be too large. But that does not harm.
30183f9b0042SHeinrich Schuchardt 	 */
30193f9b0042SHeinrich Schuchardt 	*number_of_children = 0;
30203f9b0042SHeinrich Schuchardt 	*child_handle_buffer = calloc(count, sizeof(efi_handle_t));
30213f9b0042SHeinrich Schuchardt 	if (!*child_handle_buffer)
30223f9b0042SHeinrich Schuchardt 		return EFI_OUT_OF_RESOURCES;
30233f9b0042SHeinrich Schuchardt 	/* Copy unique child handles */
30243f9b0042SHeinrich Schuchardt 	list_for_each_entry(handler, &efiobj->protocols, link) {
30253f9b0042SHeinrich Schuchardt 		list_for_each_entry(item, &handler->open_infos, link) {
30263f9b0042SHeinrich Schuchardt 			if (item->info.agent_handle == driver_handle &&
30273f9b0042SHeinrich Schuchardt 			    item->info.attributes &
30283f9b0042SHeinrich Schuchardt 			    EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
30293f9b0042SHeinrich Schuchardt 				/* Check this is a new child controller */
30303f9b0042SHeinrich Schuchardt 				duplicate = false;
30313f9b0042SHeinrich Schuchardt 				for (i = 0; i < *number_of_children; ++i) {
30323f9b0042SHeinrich Schuchardt 					if ((*child_handle_buffer)[i] ==
30333f9b0042SHeinrich Schuchardt 					    item->info.controller_handle)
30343f9b0042SHeinrich Schuchardt 						duplicate = true;
30353f9b0042SHeinrich Schuchardt 				}
30363f9b0042SHeinrich Schuchardt 				/* Copy handle to buffer */
30373f9b0042SHeinrich Schuchardt 				if (!duplicate) {
30383f9b0042SHeinrich Schuchardt 					i = (*number_of_children)++;
30393f9b0042SHeinrich Schuchardt 					(*child_handle_buffer)[i] =
30403f9b0042SHeinrich Schuchardt 						item->info.controller_handle;
30413f9b0042SHeinrich Schuchardt 				}
30423f9b0042SHeinrich Schuchardt 			}
30433f9b0042SHeinrich Schuchardt 		}
30443f9b0042SHeinrich Schuchardt 	}
30453f9b0042SHeinrich Schuchardt 	return EFI_SUCCESS;
30463f9b0042SHeinrich Schuchardt }
30473f9b0042SHeinrich Schuchardt 
30486b03cd10SHeinrich Schuchardt /**
304978a88f79SMario Six  * efi_disconnect_controller() - disconnect a controller from a driver
30506b03cd10SHeinrich Schuchardt  * @controller_handle:   handle of the controller
30516b03cd10SHeinrich Schuchardt  * @driver_image_handle: handle of the driver
30526b03cd10SHeinrich Schuchardt  * @child_handle:        handle of the child to destroy
305378a88f79SMario Six  *
305478a88f79SMario Six  * This function implements the DisconnectController service.
305578a88f79SMario Six  *
305678a88f79SMario Six  * See the Unified Extensible Firmware Interface (UEFI) specification for
305778a88f79SMario Six  * details.
305878a88f79SMario Six  *
305978a88f79SMario Six  * Return: status code
30603f9b0042SHeinrich Schuchardt  */
efi_disconnect_controller(efi_handle_t controller_handle,efi_handle_t driver_image_handle,efi_handle_t child_handle)30613f9b0042SHeinrich Schuchardt static efi_status_t EFIAPI efi_disconnect_controller(
30623f9b0042SHeinrich Schuchardt 				efi_handle_t controller_handle,
30633f9b0042SHeinrich Schuchardt 				efi_handle_t driver_image_handle,
30643f9b0042SHeinrich Schuchardt 				efi_handle_t child_handle)
30653f9b0042SHeinrich Schuchardt {
30663f9b0042SHeinrich Schuchardt 	struct efi_driver_binding_protocol *binding_protocol;
30673f9b0042SHeinrich Schuchardt 	efi_handle_t *child_handle_buffer = NULL;
30683f9b0042SHeinrich Schuchardt 	size_t number_of_children = 0;
30693f9b0042SHeinrich Schuchardt 	efi_status_t r;
30703f9b0042SHeinrich Schuchardt 	size_t stop_count = 0;
30713f9b0042SHeinrich Schuchardt 	struct efi_object *efiobj;
30723f9b0042SHeinrich Schuchardt 
30733f9b0042SHeinrich Schuchardt 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
30743f9b0042SHeinrich Schuchardt 		  child_handle);
30753f9b0042SHeinrich Schuchardt 
30763f9b0042SHeinrich Schuchardt 	efiobj = efi_search_obj(controller_handle);
30773f9b0042SHeinrich Schuchardt 	if (!efiobj) {
30783f9b0042SHeinrich Schuchardt 		r = EFI_INVALID_PARAMETER;
30793f9b0042SHeinrich Schuchardt 		goto out;
30803f9b0042SHeinrich Schuchardt 	}
30813f9b0042SHeinrich Schuchardt 
30823f9b0042SHeinrich Schuchardt 	if (child_handle && !efi_search_obj(child_handle)) {
30833f9b0042SHeinrich Schuchardt 		r = EFI_INVALID_PARAMETER;
30843f9b0042SHeinrich Schuchardt 		goto out;
30853f9b0042SHeinrich Schuchardt 	}
30863f9b0042SHeinrich Schuchardt 
30873f9b0042SHeinrich Schuchardt 	/* If no driver handle is supplied, disconnect all drivers */
30883f9b0042SHeinrich Schuchardt 	if (!driver_image_handle) {
30893f9b0042SHeinrich Schuchardt 		r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
30903f9b0042SHeinrich Schuchardt 		goto out;
30913f9b0042SHeinrich Schuchardt 	}
30923f9b0042SHeinrich Schuchardt 
30933f9b0042SHeinrich Schuchardt 	/* Create list of child handles */
30943f9b0042SHeinrich Schuchardt 	if (child_handle) {
30953f9b0042SHeinrich Schuchardt 		number_of_children = 1;
30963f9b0042SHeinrich Schuchardt 		child_handle_buffer = &child_handle;
30973f9b0042SHeinrich Schuchardt 	} else {
30983f9b0042SHeinrich Schuchardt 		efi_get_child_controllers(efiobj,
30993f9b0042SHeinrich Schuchardt 					  driver_image_handle,
31003f9b0042SHeinrich Schuchardt 					  &number_of_children,
31013f9b0042SHeinrich Schuchardt 					  &child_handle_buffer);
31023f9b0042SHeinrich Schuchardt 	}
31033f9b0042SHeinrich Schuchardt 
31043f9b0042SHeinrich Schuchardt 	/* Get the driver binding protocol */
31053f9b0042SHeinrich Schuchardt 	r = EFI_CALL(efi_open_protocol(driver_image_handle,
31063f9b0042SHeinrich Schuchardt 				       &efi_guid_driver_binding_protocol,
31073f9b0042SHeinrich Schuchardt 				       (void **)&binding_protocol,
31083f9b0042SHeinrich Schuchardt 				       driver_image_handle, NULL,
31093f9b0042SHeinrich Schuchardt 				       EFI_OPEN_PROTOCOL_GET_PROTOCOL));
31103f9b0042SHeinrich Schuchardt 	if (r != EFI_SUCCESS)
31113f9b0042SHeinrich Schuchardt 		goto out;
31123f9b0042SHeinrich Schuchardt 	/* Remove the children */
31133f9b0042SHeinrich Schuchardt 	if (number_of_children) {
31143f9b0042SHeinrich Schuchardt 		r = EFI_CALL(binding_protocol->stop(binding_protocol,
31153f9b0042SHeinrich Schuchardt 						    controller_handle,
31163f9b0042SHeinrich Schuchardt 						    number_of_children,
31173f9b0042SHeinrich Schuchardt 						    child_handle_buffer));
31183f9b0042SHeinrich Schuchardt 		if (r == EFI_SUCCESS)
31193f9b0042SHeinrich Schuchardt 			++stop_count;
31203f9b0042SHeinrich Schuchardt 	}
31213f9b0042SHeinrich Schuchardt 	/* Remove the driver */
31223f9b0042SHeinrich Schuchardt 	if (!child_handle)
31233f9b0042SHeinrich Schuchardt 		r = EFI_CALL(binding_protocol->stop(binding_protocol,
31243f9b0042SHeinrich Schuchardt 						    controller_handle,
31253f9b0042SHeinrich Schuchardt 						    0, NULL));
31263f9b0042SHeinrich Schuchardt 	if (r == EFI_SUCCESS)
31273f9b0042SHeinrich Schuchardt 		++stop_count;
31283f9b0042SHeinrich Schuchardt 	EFI_CALL(efi_close_protocol(driver_image_handle,
31293f9b0042SHeinrich Schuchardt 				    &efi_guid_driver_binding_protocol,
31303f9b0042SHeinrich Schuchardt 				    driver_image_handle, NULL));
31313f9b0042SHeinrich Schuchardt 
31323f9b0042SHeinrich Schuchardt 	if (stop_count)
31333f9b0042SHeinrich Schuchardt 		r = EFI_SUCCESS;
31343f9b0042SHeinrich Schuchardt 	else
31353f9b0042SHeinrich Schuchardt 		r = EFI_NOT_FOUND;
31363f9b0042SHeinrich Schuchardt out:
31373f9b0042SHeinrich Schuchardt 	if (!child_handle)
31383f9b0042SHeinrich Schuchardt 		free(child_handle_buffer);
31393f9b0042SHeinrich Schuchardt 	return EFI_EXIT(r);
31403f9b0042SHeinrich Schuchardt }
31413f9b0042SHeinrich Schuchardt 
3142640adadfSHeinrich Schuchardt static struct efi_boot_services efi_boot_services = {
3143bee91169SAlexander Graf 	.hdr = {
3144112f2430SHeinrich Schuchardt 		.signature = EFI_BOOT_SERVICES_SIGNATURE,
3145112f2430SHeinrich Schuchardt 		.revision = EFI_SPECIFICATION_VERSION,
314671c846abSHeinrich Schuchardt 		.headersize = sizeof(struct efi_boot_services),
3147bee91169SAlexander Graf 	},
3148bee91169SAlexander Graf 	.raise_tpl = efi_raise_tpl,
3149bee91169SAlexander Graf 	.restore_tpl = efi_restore_tpl,
3150bee91169SAlexander Graf 	.allocate_pages = efi_allocate_pages_ext,
3151bee91169SAlexander Graf 	.free_pages = efi_free_pages_ext,
3152bee91169SAlexander Graf 	.get_memory_map = efi_get_memory_map_ext,
3153ead1274bSStefan Brüns 	.allocate_pool = efi_allocate_pool_ext,
315442417bc8SStefan Brüns 	.free_pool = efi_free_pool_ext,
315549deb455Sxypron.glpk@gmx.de 	.create_event = efi_create_event_ext,
3156bfc72462Sxypron.glpk@gmx.de 	.set_timer = efi_set_timer_ext,
3157bee91169SAlexander Graf 	.wait_for_event = efi_wait_for_event,
3158c6841592Sxypron.glpk@gmx.de 	.signal_event = efi_signal_event_ext,
3159bee91169SAlexander Graf 	.close_event = efi_close_event,
3160bee91169SAlexander Graf 	.check_event = efi_check_event,
31611760ef57SHeinrich Schuchardt 	.install_protocol_interface = efi_install_protocol_interface,
3162bee91169SAlexander Graf 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
3163cd534083SHeinrich Schuchardt 	.uninstall_protocol_interface = efi_uninstall_protocol_interface,
3164bee91169SAlexander Graf 	.handle_protocol = efi_handle_protocol,
3165bee91169SAlexander Graf 	.reserved = NULL,
3166bee91169SAlexander Graf 	.register_protocol_notify = efi_register_protocol_notify,
316726329584Sxypron.glpk@gmx.de 	.locate_handle = efi_locate_handle_ext,
3168bee91169SAlexander Graf 	.locate_device_path = efi_locate_device_path,
3169488bf12dSAlexander Graf 	.install_configuration_table = efi_install_configuration_table_ext,
3170bee91169SAlexander Graf 	.load_image = efi_load_image,
3171bee91169SAlexander Graf 	.start_image = efi_start_image,
3172a86aeaf2SAlexander Graf 	.exit = efi_exit,
3173bee91169SAlexander Graf 	.unload_image = efi_unload_image,
3174bee91169SAlexander Graf 	.exit_boot_services = efi_exit_boot_services,
3175bee91169SAlexander Graf 	.get_next_monotonic_count = efi_get_next_monotonic_count,
3176bee91169SAlexander Graf 	.stall = efi_stall,
3177bee91169SAlexander Graf 	.set_watchdog_timer = efi_set_watchdog_timer,
3178bee91169SAlexander Graf 	.connect_controller = efi_connect_controller,
3179bee91169SAlexander Graf 	.disconnect_controller = efi_disconnect_controller,
3180bee91169SAlexander Graf 	.open_protocol = efi_open_protocol,
3181bee91169SAlexander Graf 	.close_protocol = efi_close_protocol,
3182bee91169SAlexander Graf 	.open_protocol_information = efi_open_protocol_information,
3183bee91169SAlexander Graf 	.protocols_per_handle = efi_protocols_per_handle,
3184bee91169SAlexander Graf 	.locate_handle_buffer = efi_locate_handle_buffer,
3185bee91169SAlexander Graf 	.locate_protocol = efi_locate_protocol,
3186ab9efa97SHeinrich Schuchardt 	.install_multiple_protocol_interfaces =
3187ab9efa97SHeinrich Schuchardt 			efi_install_multiple_protocol_interfaces,
3188ab9efa97SHeinrich Schuchardt 	.uninstall_multiple_protocol_interfaces =
3189ab9efa97SHeinrich Schuchardt 			efi_uninstall_multiple_protocol_interfaces,
3190bee91169SAlexander Graf 	.calculate_crc32 = efi_calculate_crc32,
3191bee91169SAlexander Graf 	.copy_mem = efi_copy_mem,
3192bee91169SAlexander Graf 	.set_mem = efi_set_mem,
31939f0930e5SHeinrich Schuchardt 	.create_event_ex = efi_create_event_ex,
3194bee91169SAlexander Graf };
3195bee91169SAlexander Graf 
31960b386537SHeinrich Schuchardt static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3197bee91169SAlexander Graf 
31983c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
3199bee91169SAlexander Graf 	.hdr = {
3200bee91169SAlexander Graf 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
3201112f2430SHeinrich Schuchardt 		.revision = EFI_SPECIFICATION_VERSION,
320271c846abSHeinrich Schuchardt 		.headersize = sizeof(struct efi_system_table),
3203bee91169SAlexander Graf 	},
32040b386537SHeinrich Schuchardt 	.fw_vendor = firmware_vendor,
32050b386537SHeinrich Schuchardt 	.fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3206bee91169SAlexander Graf 	.con_in = (void *)&efi_con_in,
3207bee91169SAlexander Graf 	.con_out = (void *)&efi_con_out,
3208bee91169SAlexander Graf 	.std_err = (void *)&efi_con_out,
3209bee91169SAlexander Graf 	.runtime = (void *)&efi_runtime_services,
3210bee91169SAlexander Graf 	.boottime = (void *)&efi_boot_services,
3211bee91169SAlexander Graf 	.nr_tables = 0,
32124182a129SHeinrich Schuchardt 	.tables = NULL,
3213bee91169SAlexander Graf };
3214640adadfSHeinrich Schuchardt 
3215640adadfSHeinrich Schuchardt /**
3216640adadfSHeinrich Schuchardt  * efi_initialize_system_table() - Initialize system table
3217640adadfSHeinrich Schuchardt  *
32180414359aSHeinrich Schuchardt  * Return:	status code
3219640adadfSHeinrich Schuchardt  */
efi_initialize_system_table(void)3220640adadfSHeinrich Schuchardt efi_status_t efi_initialize_system_table(void)
3221640adadfSHeinrich Schuchardt {
32224182a129SHeinrich Schuchardt 	efi_status_t ret;
32234182a129SHeinrich Schuchardt 
32244182a129SHeinrich Schuchardt 	/* Allocate configuration table array */
32254182a129SHeinrich Schuchardt 	ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
32264182a129SHeinrich Schuchardt 				EFI_MAX_CONFIGURATION_TABLES *
32274182a129SHeinrich Schuchardt 				sizeof(struct efi_configuration_table),
32284182a129SHeinrich Schuchardt 				(void **)&systab.tables);
32294182a129SHeinrich Schuchardt 
3230b72aaa87SHeinrich Schuchardt 	/* Set CRC32 field in table headers */
3231640adadfSHeinrich Schuchardt 	efi_update_table_header_crc32(&systab.hdr);
3232640adadfSHeinrich Schuchardt 	efi_update_table_header_crc32(&efi_runtime_services.hdr);
3233640adadfSHeinrich Schuchardt 	efi_update_table_header_crc32(&efi_boot_services.hdr);
32344182a129SHeinrich Schuchardt 
32354182a129SHeinrich Schuchardt 	return ret;
3236640adadfSHeinrich Schuchardt }
3237