xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_boottime.c (revision 8787b02e32dc8bcf7ed432adcb0d246c5c844985)
1bee91169SAlexander Graf /*
2bee91169SAlexander Graf  *  EFI application boot time services
3bee91169SAlexander Graf  *
4bee91169SAlexander Graf  *  Copyright (c) 2016 Alexander Graf
5bee91169SAlexander Graf  *
6bee91169SAlexander Graf  *  SPDX-License-Identifier:     GPL-2.0+
7bee91169SAlexander Graf  */
8bee91169SAlexander Graf 
9bee91169SAlexander Graf #include <common.h>
10bee91169SAlexander Graf #include <efi_loader.h>
11bee91169SAlexander Graf #include <malloc.h>
12bee91169SAlexander Graf #include <asm/global_data.h>
13bee91169SAlexander Graf #include <libfdt_env.h>
14bee91169SAlexander Graf #include <u-boot/crc.h>
15bee91169SAlexander Graf #include <bootm.h>
16bee91169SAlexander Graf #include <inttypes.h>
17bee91169SAlexander Graf #include <watchdog.h>
18bee91169SAlexander Graf 
19bee91169SAlexander Graf DECLARE_GLOBAL_DATA_PTR;
20bee91169SAlexander Graf 
21bee91169SAlexander Graf /* This list contains all the EFI objects our payload has access to */
22bee91169SAlexander Graf LIST_HEAD(efi_obj_list);
23bee91169SAlexander Graf 
24bee91169SAlexander Graf /*
25bee91169SAlexander Graf  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26bee91169SAlexander Graf  * we need to do trickery with caches. Since we don't want to break the EFI
27bee91169SAlexander Graf  * aware boot path, only apply hacks when loading exiting directly (breaking
28bee91169SAlexander Graf  * direct Linux EFI booting along the way - oh well).
29bee91169SAlexander Graf  */
30bee91169SAlexander Graf static bool efi_is_direct_boot = true;
31bee91169SAlexander Graf 
32bee91169SAlexander Graf /*
33bee91169SAlexander Graf  * EFI can pass arbitrary additional "tables" containing vendor specific
34bee91169SAlexander Graf  * information to the payload. One such table is the FDT table which contains
35bee91169SAlexander Graf  * a pointer to a flattened device tree blob.
36bee91169SAlexander Graf  *
37bee91169SAlexander Graf  * In most cases we want to pass an FDT to the payload, so reserve one slot of
38bee91169SAlexander Graf  * config table space for it. The pointer gets populated by do_bootefi_exec().
39bee91169SAlexander Graf  */
403c63db9cSAlexander Graf static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
41bee91169SAlexander Graf 
4265e4c0b1SSimon Glass #ifdef CONFIG_ARM
43bee91169SAlexander Graf /*
44bee91169SAlexander Graf  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45bee91169SAlexander Graf  * fixed when compiling U-Boot. However, the payload does not know about that
46bee91169SAlexander Graf  * restriction so we need to manually swap its and our view of that register on
47bee91169SAlexander Graf  * EFI callback entry/exit.
48bee91169SAlexander Graf  */
49bee91169SAlexander Graf static volatile void *efi_gd, *app_gd;
5065e4c0b1SSimon Glass #endif
51bee91169SAlexander Graf 
52bee91169SAlexander Graf /* Called from do_bootefi_exec() */
53bee91169SAlexander Graf void efi_save_gd(void)
54bee91169SAlexander Graf {
5565e4c0b1SSimon Glass #ifdef CONFIG_ARM
56bee91169SAlexander Graf 	efi_gd = gd;
5765e4c0b1SSimon Glass #endif
58bee91169SAlexander Graf }
59bee91169SAlexander Graf 
60bee91169SAlexander Graf /* Called on every callback entry */
61bee91169SAlexander Graf void efi_restore_gd(void)
62bee91169SAlexander Graf {
6365e4c0b1SSimon Glass #ifdef CONFIG_ARM
64bee91169SAlexander Graf 	/* Only restore if we're already in EFI context */
65bee91169SAlexander Graf 	if (!efi_gd)
66bee91169SAlexander Graf 		return;
67bee91169SAlexander Graf 
68bee91169SAlexander Graf 	if (gd != efi_gd)
69bee91169SAlexander Graf 		app_gd = gd;
70bee91169SAlexander Graf 	gd = efi_gd;
7165e4c0b1SSimon Glass #endif
72bee91169SAlexander Graf }
73bee91169SAlexander Graf 
74bee91169SAlexander Graf /* Called on every callback exit */
75bee91169SAlexander Graf efi_status_t efi_exit_func(efi_status_t ret)
76bee91169SAlexander Graf {
7765e4c0b1SSimon Glass #ifdef CONFIG_ARM
78bee91169SAlexander Graf 	gd = app_gd;
7965e4c0b1SSimon Glass #endif
8065e4c0b1SSimon Glass 
81bee91169SAlexander Graf 	return ret;
82bee91169SAlexander Graf }
83bee91169SAlexander Graf 
84*8787b02eSxypron.glpk@gmx.de /* Low 32 bit */
85*8787b02eSxypron.glpk@gmx.de #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
86*8787b02eSxypron.glpk@gmx.de /* High 32 bit */
87*8787b02eSxypron.glpk@gmx.de #define EFI_HIGH32(a) (a >> 32)
88*8787b02eSxypron.glpk@gmx.de 
89*8787b02eSxypron.glpk@gmx.de /*
90*8787b02eSxypron.glpk@gmx.de  * 64bit division by 10 implemented as multiplication by 1 / 10
91*8787b02eSxypron.glpk@gmx.de  *
92*8787b02eSxypron.glpk@gmx.de  * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
93*8787b02eSxypron.glpk@gmx.de  */
94*8787b02eSxypron.glpk@gmx.de #define EFI_TENTH 0x199999999999999A
95*8787b02eSxypron.glpk@gmx.de static u64 efi_div10(u64 a)
96*8787b02eSxypron.glpk@gmx.de {
97*8787b02eSxypron.glpk@gmx.de 	u64 prod;
98*8787b02eSxypron.glpk@gmx.de 	u64 rem;
99*8787b02eSxypron.glpk@gmx.de 	u64 ret;
100*8787b02eSxypron.glpk@gmx.de 
101*8787b02eSxypron.glpk@gmx.de 	ret  = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
102*8787b02eSxypron.glpk@gmx.de 	prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
103*8787b02eSxypron.glpk@gmx.de 	rem  = EFI_LOW32(prod);
104*8787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
105*8787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
106*8787b02eSxypron.glpk@gmx.de 	rem += EFI_LOW32(prod);
107*8787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
108*8787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
109*8787b02eSxypron.glpk@gmx.de 	rem += EFI_HIGH32(prod);
110*8787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(rem);
111*8787b02eSxypron.glpk@gmx.de 	/* Round to nearest integer */
112*8787b02eSxypron.glpk@gmx.de 	if (rem >= (1 << 31))
113*8787b02eSxypron.glpk@gmx.de 		++ret;
114*8787b02eSxypron.glpk@gmx.de 	return ret;
115*8787b02eSxypron.glpk@gmx.de }
116*8787b02eSxypron.glpk@gmx.de 
11791be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event)
118c6841592Sxypron.glpk@gmx.de {
119c6841592Sxypron.glpk@gmx.de 	if (event->signaled)
120c6841592Sxypron.glpk@gmx.de 		return;
121c6841592Sxypron.glpk@gmx.de 	event->signaled = 1;
122c6841592Sxypron.glpk@gmx.de 	if (event->type & EVT_NOTIFY_SIGNAL) {
123c6841592Sxypron.glpk@gmx.de 		EFI_EXIT(EFI_SUCCESS);
124c6841592Sxypron.glpk@gmx.de 		event->notify_function(event, event->notify_context);
125c6841592Sxypron.glpk@gmx.de 		EFI_ENTRY("returning from notification function");
126c6841592Sxypron.glpk@gmx.de 	}
127c6841592Sxypron.glpk@gmx.de }
128c6841592Sxypron.glpk@gmx.de 
129bee91169SAlexander Graf static efi_status_t efi_unsupported(const char *funcname)
130bee91169SAlexander Graf {
131edcef3baSAlexander Graf 	debug("EFI: App called into unimplemented function %s\n", funcname);
132bee91169SAlexander Graf 	return EFI_EXIT(EFI_UNSUPPORTED);
133bee91169SAlexander Graf }
134bee91169SAlexander Graf 
135bee91169SAlexander Graf static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
136bee91169SAlexander Graf {
137bee91169SAlexander Graf 	return memcmp(g1, g2, sizeof(efi_guid_t));
138bee91169SAlexander Graf }
139bee91169SAlexander Graf 
140503f2695Sxypron.glpk@gmx.de static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
141bee91169SAlexander Graf {
142503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", new_tpl);
143bee91169SAlexander Graf 	return EFI_EXIT(0);
144bee91169SAlexander Graf }
145bee91169SAlexander Graf 
146503f2695Sxypron.glpk@gmx.de static void EFIAPI efi_restore_tpl(UINTN old_tpl)
147bee91169SAlexander Graf {
148503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", old_tpl);
149bee91169SAlexander Graf 	EFI_EXIT(efi_unsupported(__func__));
150bee91169SAlexander Graf }
151bee91169SAlexander Graf 
1526e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
153bee91169SAlexander Graf 						  unsigned long pages,
154bee91169SAlexander Graf 						  uint64_t *memory)
155bee91169SAlexander Graf {
156bee91169SAlexander Graf 	efi_status_t r;
157bee91169SAlexander Graf 
158bee91169SAlexander Graf 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
159bee91169SAlexander Graf 	r = efi_allocate_pages(type, memory_type, pages, memory);
160bee91169SAlexander Graf 	return EFI_EXIT(r);
161bee91169SAlexander Graf }
162bee91169SAlexander Graf 
1636e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
1646e0bf8d8SMasahiro Yamada 					      unsigned long pages)
165bee91169SAlexander Graf {
166bee91169SAlexander Graf 	efi_status_t r;
167bee91169SAlexander Graf 
168bee91169SAlexander Graf 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
169bee91169SAlexander Graf 	r = efi_free_pages(memory, pages);
170bee91169SAlexander Graf 	return EFI_EXIT(r);
171bee91169SAlexander Graf }
172bee91169SAlexander Graf 
1736e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
1746e0bf8d8SMasahiro Yamada 					unsigned long *memory_map_size,
175bee91169SAlexander Graf 					struct efi_mem_desc *memory_map,
176bee91169SAlexander Graf 					unsigned long *map_key,
177bee91169SAlexander Graf 					unsigned long *descriptor_size,
178bee91169SAlexander Graf 					uint32_t *descriptor_version)
179bee91169SAlexander Graf {
180bee91169SAlexander Graf 	efi_status_t r;
181bee91169SAlexander Graf 
182bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
183bee91169SAlexander Graf 		  map_key, descriptor_size, descriptor_version);
184bee91169SAlexander Graf 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
185bee91169SAlexander Graf 			       descriptor_size, descriptor_version);
186bee91169SAlexander Graf 	return EFI_EXIT(r);
187bee91169SAlexander Graf }
188bee91169SAlexander Graf 
189ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
190ead1274bSStefan Brüns 						 unsigned long size,
191bee91169SAlexander Graf 						 void **buffer)
192bee91169SAlexander Graf {
1931cd29f0aSAlexander Graf 	efi_status_t r;
1941cd29f0aSAlexander Graf 
1951cd29f0aSAlexander Graf 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
196ead1274bSStefan Brüns 	r = efi_allocate_pool(pool_type, size, buffer);
1971cd29f0aSAlexander Graf 	return EFI_EXIT(r);
198bee91169SAlexander Graf }
199bee91169SAlexander Graf 
20042417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
201bee91169SAlexander Graf {
2021cd29f0aSAlexander Graf 	efi_status_t r;
2031cd29f0aSAlexander Graf 
2041cd29f0aSAlexander Graf 	EFI_ENTRY("%p", buffer);
20542417bc8SStefan Brüns 	r = efi_free_pool(buffer);
2061cd29f0aSAlexander Graf 	return EFI_EXIT(r);
207bee91169SAlexander Graf }
208bee91169SAlexander Graf 
209bee91169SAlexander Graf /*
210c6841592Sxypron.glpk@gmx.de  * Our event capabilities are very limited. Only a small limited
211c6841592Sxypron.glpk@gmx.de  * number of events is allowed to coexist.
212bee91169SAlexander Graf  */
213c6841592Sxypron.glpk@gmx.de static struct efi_event efi_events[16];
214bee91169SAlexander Graf 
21549deb455Sxypron.glpk@gmx.de efi_status_t efi_create_event(enum efi_event_type type, UINTN notify_tpl,
2162fd945feSxypron.glpk@gmx.de 			      void (EFIAPI *notify_function) (
2172fd945feSxypron.glpk@gmx.de 					struct efi_event *event,
218e275458cSSimon Glass 					void *context),
2192fd945feSxypron.glpk@gmx.de 			      void *notify_context, struct efi_event **event)
220bee91169SAlexander Graf {
221c6841592Sxypron.glpk@gmx.de 	int i;
222c6841592Sxypron.glpk@gmx.de 
223a95343b8SJonathan Gray 	if (event == NULL)
22449deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
225a95343b8SJonathan Gray 
226a95343b8SJonathan Gray 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
22749deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
228a95343b8SJonathan Gray 
229a95343b8SJonathan Gray 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
230a95343b8SJonathan Gray 	    notify_function == NULL)
23149deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
232a95343b8SJonathan Gray 
233c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
234c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].type)
235c6841592Sxypron.glpk@gmx.de 			continue;
236c6841592Sxypron.glpk@gmx.de 		efi_events[i].type = type;
237c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_tpl = notify_tpl;
238c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_function = notify_function;
239c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_context = notify_context;
240c6841592Sxypron.glpk@gmx.de 		/* Disable timers on bootup */
241c6841592Sxypron.glpk@gmx.de 		efi_events[i].trigger_next = -1ULL;
242c6841592Sxypron.glpk@gmx.de 		efi_events[i].signaled = 0;
243c6841592Sxypron.glpk@gmx.de 		*event = &efi_events[i];
24449deb455Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
245bee91169SAlexander Graf 	}
24649deb455Sxypron.glpk@gmx.de 	return EFI_OUT_OF_RESOURCES;
247c6841592Sxypron.glpk@gmx.de }
248bee91169SAlexander Graf 
24949deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
25049deb455Sxypron.glpk@gmx.de 			enum efi_event_type type, UINTN notify_tpl,
25149deb455Sxypron.glpk@gmx.de 			void (EFIAPI *notify_function) (
25249deb455Sxypron.glpk@gmx.de 					struct efi_event *event,
25349deb455Sxypron.glpk@gmx.de 					void *context),
25449deb455Sxypron.glpk@gmx.de 			void *notify_context, struct efi_event **event)
25549deb455Sxypron.glpk@gmx.de {
25649deb455Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
25749deb455Sxypron.glpk@gmx.de 		  notify_context);
25849deb455Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
25949deb455Sxypron.glpk@gmx.de 					 notify_context, event));
26049deb455Sxypron.glpk@gmx.de }
26149deb455Sxypron.glpk@gmx.de 
26249deb455Sxypron.glpk@gmx.de 
263bee91169SAlexander Graf /*
264bee91169SAlexander Graf  * Our timers have to work without interrupts, so we check whenever keyboard
265bee91169SAlexander Graf  * input or disk accesses happen if enough time elapsed for it to fire.
266bee91169SAlexander Graf  */
267bee91169SAlexander Graf void efi_timer_check(void)
268bee91169SAlexander Graf {
269c6841592Sxypron.glpk@gmx.de 	int i;
270bee91169SAlexander Graf 	u64 now = timer_get_us();
271bee91169SAlexander Graf 
272c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
273c6841592Sxypron.glpk@gmx.de 		if (!efi_events[i].type ||
274c6841592Sxypron.glpk@gmx.de 		    !(efi_events[i].type & EVT_TIMER) ||
275c6841592Sxypron.glpk@gmx.de 		    efi_events[i].trigger_type == EFI_TIMER_STOP ||
276c6841592Sxypron.glpk@gmx.de 		    now < efi_events[i].trigger_next)
277c6841592Sxypron.glpk@gmx.de 			continue;
278c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
279c6841592Sxypron.glpk@gmx.de 			efi_events[i].trigger_next +=
280*8787b02eSxypron.glpk@gmx.de 				efi_events[i].trigger_time;
281c6841592Sxypron.glpk@gmx.de 			efi_events[i].signaled = 0;
282bee91169SAlexander Graf 		}
283c6841592Sxypron.glpk@gmx.de 		efi_signal_event(&efi_events[i]);
284c6841592Sxypron.glpk@gmx.de 	}
285bee91169SAlexander Graf 	WATCHDOG_RESET();
286bee91169SAlexander Graf }
287bee91169SAlexander Graf 
288bfc72462Sxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, int type,
289bee91169SAlexander Graf 			   uint64_t trigger_time)
290bee91169SAlexander Graf {
291c6841592Sxypron.glpk@gmx.de 	int i;
292bee91169SAlexander Graf 
293*8787b02eSxypron.glpk@gmx.de 	/*
294*8787b02eSxypron.glpk@gmx.de 	 * The parameter defines a multiple of 100ns.
295*8787b02eSxypron.glpk@gmx.de 	 * We use multiples of 1000ns. So divide by 10.
296*8787b02eSxypron.glpk@gmx.de 	 */
297*8787b02eSxypron.glpk@gmx.de 	trigger_time = efi_div10(trigger_time);
298bee91169SAlexander Graf 
299c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
300c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
301c6841592Sxypron.glpk@gmx.de 			continue;
302bee91169SAlexander Graf 
303c6841592Sxypron.glpk@gmx.de 		if (!(event->type & EVT_TIMER))
304c6841592Sxypron.glpk@gmx.de 			break;
305bee91169SAlexander Graf 		switch (type) {
306bee91169SAlexander Graf 		case EFI_TIMER_STOP:
307c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
308bee91169SAlexander Graf 			break;
309bee91169SAlexander Graf 		case EFI_TIMER_PERIODIC:
310bee91169SAlexander Graf 		case EFI_TIMER_RELATIVE:
311c6841592Sxypron.glpk@gmx.de 			event->trigger_next =
312*8787b02eSxypron.glpk@gmx.de 				timer_get_us() + trigger_time;
313bee91169SAlexander Graf 			break;
314bee91169SAlexander Graf 		default:
315bfc72462Sxypron.glpk@gmx.de 			return EFI_INVALID_PARAMETER;
316bee91169SAlexander Graf 		}
317c6841592Sxypron.glpk@gmx.de 		event->trigger_type = type;
318c6841592Sxypron.glpk@gmx.de 		event->trigger_time = trigger_time;
319bfc72462Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
320bee91169SAlexander Graf 	}
321bfc72462Sxypron.glpk@gmx.de 	return EFI_INVALID_PARAMETER;
322bfc72462Sxypron.glpk@gmx.de }
323bfc72462Sxypron.glpk@gmx.de 
324bfc72462Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, int type,
325bfc72462Sxypron.glpk@gmx.de 					 uint64_t trigger_time)
326bfc72462Sxypron.glpk@gmx.de {
327bfc72462Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
328bfc72462Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
329c6841592Sxypron.glpk@gmx.de }
330bee91169SAlexander Graf 
331bee91169SAlexander Graf static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
3322fd945feSxypron.glpk@gmx.de 					      struct efi_event **event,
3332fd945feSxypron.glpk@gmx.de 					      unsigned long *index)
334bee91169SAlexander Graf {
335c6841592Sxypron.glpk@gmx.de 	int i, j;
336bee91169SAlexander Graf 
337bee91169SAlexander Graf 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
338bee91169SAlexander Graf 
339c6841592Sxypron.glpk@gmx.de 	/* Check parameters */
340c6841592Sxypron.glpk@gmx.de 	if (!num_events || !event)
341c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
342c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < num_events; ++i) {
343c6841592Sxypron.glpk@gmx.de 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
344c6841592Sxypron.glpk@gmx.de 			if (event[i] == &efi_events[j])
345c6841592Sxypron.glpk@gmx.de 				goto known_event;
346c6841592Sxypron.glpk@gmx.de 		}
347c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
348c6841592Sxypron.glpk@gmx.de known_event:
349c6841592Sxypron.glpk@gmx.de 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
350c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
351c6841592Sxypron.glpk@gmx.de 	}
352c6841592Sxypron.glpk@gmx.de 
353c6841592Sxypron.glpk@gmx.de 	/* Wait for signal */
354c6841592Sxypron.glpk@gmx.de 	for (;;) {
355c6841592Sxypron.glpk@gmx.de 		for (i = 0; i < num_events; ++i) {
356c6841592Sxypron.glpk@gmx.de 			if (event[i]->signaled)
357c6841592Sxypron.glpk@gmx.de 				goto out;
358c6841592Sxypron.glpk@gmx.de 		}
359c6841592Sxypron.glpk@gmx.de 		/* Allow events to occur. */
360bee91169SAlexander Graf 		efi_timer_check();
361c6841592Sxypron.glpk@gmx.de 	}
362c6841592Sxypron.glpk@gmx.de 
363c6841592Sxypron.glpk@gmx.de out:
364c6841592Sxypron.glpk@gmx.de 	/*
365c6841592Sxypron.glpk@gmx.de 	 * Reset the signal which is passed to the caller to allow periodic
366c6841592Sxypron.glpk@gmx.de 	 * events to occur.
367c6841592Sxypron.glpk@gmx.de 	 */
368c6841592Sxypron.glpk@gmx.de 	event[i]->signaled = 0;
369c6841592Sxypron.glpk@gmx.de 	if (index)
370c6841592Sxypron.glpk@gmx.de 		*index = i;
371bee91169SAlexander Graf 
372bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
373bee91169SAlexander Graf }
374bee91169SAlexander Graf 
375c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
376bee91169SAlexander Graf {
377c6841592Sxypron.glpk@gmx.de 	int i;
378c6841592Sxypron.glpk@gmx.de 
379bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
380c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
381c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
382c6841592Sxypron.glpk@gmx.de 			continue;
383c6841592Sxypron.glpk@gmx.de 		efi_signal_event(event);
384c6841592Sxypron.glpk@gmx.de 		break;
385c6841592Sxypron.glpk@gmx.de 	}
386bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
387bee91169SAlexander Graf }
388bee91169SAlexander Graf 
3892fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
390bee91169SAlexander Graf {
391c6841592Sxypron.glpk@gmx.de 	int i;
392c6841592Sxypron.glpk@gmx.de 
393bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
394c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
395c6841592Sxypron.glpk@gmx.de 		if (event == &efi_events[i]) {
396c6841592Sxypron.glpk@gmx.de 			event->type = 0;
397c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
398c6841592Sxypron.glpk@gmx.de 			event->signaled = 0;
399bee91169SAlexander Graf 			return EFI_EXIT(EFI_SUCCESS);
400bee91169SAlexander Graf 		}
401c6841592Sxypron.glpk@gmx.de 	}
402c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
403c6841592Sxypron.glpk@gmx.de }
404bee91169SAlexander Graf 
4052fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
406bee91169SAlexander Graf {
407c6841592Sxypron.glpk@gmx.de 	int i;
408c6841592Sxypron.glpk@gmx.de 
409bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
410c6841592Sxypron.glpk@gmx.de 	efi_timer_check();
411c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
412c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
413c6841592Sxypron.glpk@gmx.de 			continue;
414c6841592Sxypron.glpk@gmx.de 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
415c6841592Sxypron.glpk@gmx.de 			break;
416c6841592Sxypron.glpk@gmx.de 		if (event->signaled)
417c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_SUCCESS);
418bee91169SAlexander Graf 		return EFI_EXIT(EFI_NOT_READY);
419bee91169SAlexander Graf 	}
420c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
421c6841592Sxypron.glpk@gmx.de }
422bee91169SAlexander Graf 
423bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
424bee91169SAlexander Graf 			efi_guid_t *protocol, int protocol_interface_type,
425bee91169SAlexander Graf 			void *protocol_interface)
426bee91169SAlexander Graf {
427e0549f8aSxypron.glpk@gmx.de 	struct list_head *lhandle;
428e0549f8aSxypron.glpk@gmx.de 	int i;
429e0549f8aSxypron.glpk@gmx.de 	efi_status_t r;
430e0549f8aSxypron.glpk@gmx.de 
431e0549f8aSxypron.glpk@gmx.de 	if (!handle || !protocol ||
432e0549f8aSxypron.glpk@gmx.de 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
433e0549f8aSxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
434e0549f8aSxypron.glpk@gmx.de 		goto out;
435bee91169SAlexander Graf 	}
436e0549f8aSxypron.glpk@gmx.de 
437e0549f8aSxypron.glpk@gmx.de 	/* Create new handle if requested. */
438e0549f8aSxypron.glpk@gmx.de 	if (!*handle) {
439e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
440e0549f8aSxypron.glpk@gmx.de 		goto out;
441e0549f8aSxypron.glpk@gmx.de 	}
442e0549f8aSxypron.glpk@gmx.de 	/* Find object. */
443e0549f8aSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
444e0549f8aSxypron.glpk@gmx.de 		struct efi_object *efiobj;
445e0549f8aSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
446e0549f8aSxypron.glpk@gmx.de 
447e0549f8aSxypron.glpk@gmx.de 		if (efiobj->handle != *handle)
448e0549f8aSxypron.glpk@gmx.de 			continue;
449e0549f8aSxypron.glpk@gmx.de 		/* Check if protocol is already installed on the handle. */
450e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
451e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
452e0549f8aSxypron.glpk@gmx.de 
453e0549f8aSxypron.glpk@gmx.de 			if (!handler->guid)
454e0549f8aSxypron.glpk@gmx.de 				continue;
455e0549f8aSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
456e0549f8aSxypron.glpk@gmx.de 				r = EFI_INVALID_PARAMETER;
457e0549f8aSxypron.glpk@gmx.de 				goto out;
458e0549f8aSxypron.glpk@gmx.de 			}
459e0549f8aSxypron.glpk@gmx.de 		}
460e0549f8aSxypron.glpk@gmx.de 		/* Install protocol in first empty slot. */
461e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
462e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
463e0549f8aSxypron.glpk@gmx.de 
464e0549f8aSxypron.glpk@gmx.de 			if (handler->guid)
465e0549f8aSxypron.glpk@gmx.de 				continue;
466e0549f8aSxypron.glpk@gmx.de 
467e0549f8aSxypron.glpk@gmx.de 			handler->guid = protocol;
468e0549f8aSxypron.glpk@gmx.de 			handler->protocol_interface = protocol_interface;
469e0549f8aSxypron.glpk@gmx.de 			r = EFI_SUCCESS;
470e0549f8aSxypron.glpk@gmx.de 			goto out;
471e0549f8aSxypron.glpk@gmx.de 		}
472e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
473e0549f8aSxypron.glpk@gmx.de 		goto out;
474e0549f8aSxypron.glpk@gmx.de 	}
475e0549f8aSxypron.glpk@gmx.de 	r = EFI_INVALID_PARAMETER;
476e0549f8aSxypron.glpk@gmx.de out:
4778bee5a3cSxypron.glpk@gmx.de 	return r;
4788bee5a3cSxypron.glpk@gmx.de }
4798bee5a3cSxypron.glpk@gmx.de 
4808bee5a3cSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
4818bee5a3cSxypron.glpk@gmx.de 			efi_guid_t *protocol, int protocol_interface_type,
4828bee5a3cSxypron.glpk@gmx.de 			void *protocol_interface)
4838bee5a3cSxypron.glpk@gmx.de {
4848bee5a3cSxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
4858bee5a3cSxypron.glpk@gmx.de 		  protocol_interface);
4868bee5a3cSxypron.glpk@gmx.de 
4878bee5a3cSxypron.glpk@gmx.de 	return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
4888bee5a3cSxypron.glpk@gmx.de 						       protocol_interface_type,
4898bee5a3cSxypron.glpk@gmx.de 						       protocol_interface));
490e0549f8aSxypron.glpk@gmx.de }
491e0549f8aSxypron.glpk@gmx.de 
492bee91169SAlexander Graf static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
493bee91169SAlexander Graf 			efi_guid_t *protocol, void *old_interface,
494bee91169SAlexander Graf 			void *new_interface)
495bee91169SAlexander Graf {
496bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
497bee91169SAlexander Graf 		  new_interface);
498bee91169SAlexander Graf 	return EFI_EXIT(EFI_ACCESS_DENIED);
499bee91169SAlexander Graf }
500bee91169SAlexander Graf 
501bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
502bee91169SAlexander Graf 			efi_guid_t *protocol, void *protocol_interface)
503bee91169SAlexander Graf {
5044b6ed0d7Sxypron.glpk@gmx.de 	struct list_head *lhandle;
5054b6ed0d7Sxypron.glpk@gmx.de 	int i;
5064b6ed0d7Sxypron.glpk@gmx.de 	efi_status_t r = EFI_NOT_FOUND;
5074b6ed0d7Sxypron.glpk@gmx.de 
5084b6ed0d7Sxypron.glpk@gmx.de 	if (!handle || !protocol) {
5094b6ed0d7Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
5104b6ed0d7Sxypron.glpk@gmx.de 		goto out;
5114b6ed0d7Sxypron.glpk@gmx.de 	}
5124b6ed0d7Sxypron.glpk@gmx.de 
5134b6ed0d7Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
5144b6ed0d7Sxypron.glpk@gmx.de 		struct efi_object *efiobj;
5154b6ed0d7Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
5164b6ed0d7Sxypron.glpk@gmx.de 
5174b6ed0d7Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
5184b6ed0d7Sxypron.glpk@gmx.de 			continue;
5194b6ed0d7Sxypron.glpk@gmx.de 
5204b6ed0d7Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
5214b6ed0d7Sxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
5224b6ed0d7Sxypron.glpk@gmx.de 			const efi_guid_t *hprotocol = handler->guid;
5234b6ed0d7Sxypron.glpk@gmx.de 
5244b6ed0d7Sxypron.glpk@gmx.de 			if (!hprotocol)
5254b6ed0d7Sxypron.glpk@gmx.de 				continue;
5264b6ed0d7Sxypron.glpk@gmx.de 			if (!guidcmp(hprotocol, protocol)) {
5274b6ed0d7Sxypron.glpk@gmx.de 				if (handler->protocol_interface) {
5284b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_ACCESS_DENIED;
5294b6ed0d7Sxypron.glpk@gmx.de 				} else {
5304b6ed0d7Sxypron.glpk@gmx.de 					handler->guid = 0;
5314b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_SUCCESS;
5324b6ed0d7Sxypron.glpk@gmx.de 				}
5334b6ed0d7Sxypron.glpk@gmx.de 				goto out;
5344b6ed0d7Sxypron.glpk@gmx.de 			}
5354b6ed0d7Sxypron.glpk@gmx.de 		}
5364b6ed0d7Sxypron.glpk@gmx.de 	}
5374b6ed0d7Sxypron.glpk@gmx.de 
5384b6ed0d7Sxypron.glpk@gmx.de out:
5393d8e1456Sxypron.glpk@gmx.de 	return r;
5403d8e1456Sxypron.glpk@gmx.de }
5413d8e1456Sxypron.glpk@gmx.de 
5423d8e1456Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
5433d8e1456Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *protocol_interface)
5443d8e1456Sxypron.glpk@gmx.de {
5453d8e1456Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
5463d8e1456Sxypron.glpk@gmx.de 
5473d8e1456Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
5483d8e1456Sxypron.glpk@gmx.de 							 protocol_interface));
549bee91169SAlexander Graf }
550bee91169SAlexander Graf 
551bee91169SAlexander Graf static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
5522fd945feSxypron.glpk@gmx.de 							struct efi_event *event,
553bee91169SAlexander Graf 							void **registration)
554bee91169SAlexander Graf {
555bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
556bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
557bee91169SAlexander Graf }
558bee91169SAlexander Graf 
559bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
560bee91169SAlexander Graf 		      efi_guid_t *protocol, void *search_key,
561bee91169SAlexander Graf 		      struct efi_object *efiobj)
562bee91169SAlexander Graf {
563bee91169SAlexander Graf 	int i;
564bee91169SAlexander Graf 
565bee91169SAlexander Graf 	switch (search_type) {
566bee91169SAlexander Graf 	case all_handles:
567bee91169SAlexander Graf 		return 0;
568bee91169SAlexander Graf 	case by_register_notify:
569bee91169SAlexander Graf 		return -1;
570bee91169SAlexander Graf 	case by_protocol:
571bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
572bee91169SAlexander Graf 			const efi_guid_t *guid = efiobj->protocols[i].guid;
573bee91169SAlexander Graf 			if (guid && !guidcmp(guid, protocol))
574bee91169SAlexander Graf 				return 0;
575bee91169SAlexander Graf 		}
576bee91169SAlexander Graf 		return -1;
577bee91169SAlexander Graf 	}
578bee91169SAlexander Graf 
579bee91169SAlexander Graf 	return -1;
580bee91169SAlexander Graf }
581bee91169SAlexander Graf 
582bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle(
583bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
584bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
585bee91169SAlexander Graf 			unsigned long *buffer_size, efi_handle_t *buffer)
586bee91169SAlexander Graf {
587bee91169SAlexander Graf 	struct list_head *lhandle;
588bee91169SAlexander Graf 	unsigned long size = 0;
589bee91169SAlexander Graf 
590bee91169SAlexander Graf 	/* Count how much space we need */
591bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
592bee91169SAlexander Graf 		struct efi_object *efiobj;
593bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
594bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
595bee91169SAlexander Graf 			size += sizeof(void*);
596bee91169SAlexander Graf 		}
597bee91169SAlexander Graf 	}
598bee91169SAlexander Graf 
599bee91169SAlexander Graf 	if (*buffer_size < size) {
600bee91169SAlexander Graf 		*buffer_size = size;
60126329584Sxypron.glpk@gmx.de 		return EFI_BUFFER_TOO_SMALL;
602bee91169SAlexander Graf 	}
603bee91169SAlexander Graf 
604bee91169SAlexander Graf 	/* Then fill the array */
605bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
606bee91169SAlexander Graf 		struct efi_object *efiobj;
607bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
608bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
609bee91169SAlexander Graf 			*(buffer++) = efiobj->handle;
610bee91169SAlexander Graf 		}
611bee91169SAlexander Graf 	}
612bee91169SAlexander Graf 
613bee91169SAlexander Graf 	*buffer_size = size;
61426329584Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
61526329584Sxypron.glpk@gmx.de }
61626329584Sxypron.glpk@gmx.de 
61726329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
61826329584Sxypron.glpk@gmx.de 			enum efi_locate_search_type search_type,
61926329584Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *search_key,
62026329584Sxypron.glpk@gmx.de 			unsigned long *buffer_size, efi_handle_t *buffer)
62126329584Sxypron.glpk@gmx.de {
62226329584Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
62326329584Sxypron.glpk@gmx.de 		  buffer_size, buffer);
62426329584Sxypron.glpk@gmx.de 
62526329584Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
62626329584Sxypron.glpk@gmx.de 			buffer_size, buffer));
627bee91169SAlexander Graf }
628bee91169SAlexander Graf 
629bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
630bee91169SAlexander Graf 			struct efi_device_path **device_path,
631bee91169SAlexander Graf 			efi_handle_t *device)
632bee91169SAlexander Graf {
633bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
634bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
635bee91169SAlexander Graf }
636bee91169SAlexander Graf 
637488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
638bee91169SAlexander Graf {
639bee91169SAlexander Graf 	int i;
640bee91169SAlexander Graf 
641bee91169SAlexander Graf 	/* Check for guid override */
642bee91169SAlexander Graf 	for (i = 0; i < systab.nr_tables; i++) {
643bee91169SAlexander Graf 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
644bee91169SAlexander Graf 			efi_conf_table[i].table = table;
645488bf12dSAlexander Graf 			return EFI_SUCCESS;
646bee91169SAlexander Graf 		}
647bee91169SAlexander Graf 	}
648bee91169SAlexander Graf 
649bee91169SAlexander Graf 	/* No override, check for overflow */
650bee91169SAlexander Graf 	if (i >= ARRAY_SIZE(efi_conf_table))
651488bf12dSAlexander Graf 		return EFI_OUT_OF_RESOURCES;
652bee91169SAlexander Graf 
653bee91169SAlexander Graf 	/* Add a new entry */
654bee91169SAlexander Graf 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
655bee91169SAlexander Graf 	efi_conf_table[i].table = table;
656aba5e919SAlexander Graf 	systab.nr_tables = i + 1;
657bee91169SAlexander Graf 
658488bf12dSAlexander Graf 	return EFI_SUCCESS;
659488bf12dSAlexander Graf }
660488bf12dSAlexander Graf 
661488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
662488bf12dSAlexander Graf 							       void *table)
663488bf12dSAlexander Graf {
664488bf12dSAlexander Graf 	EFI_ENTRY("%p, %p", guid, table);
665488bf12dSAlexander Graf 	return EFI_EXIT(efi_install_configuration_table(guid, table));
666bee91169SAlexander Graf }
667bee91169SAlexander Graf 
668bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
669bee91169SAlexander Graf 					  efi_handle_t parent_image,
670bee91169SAlexander Graf 					  struct efi_device_path *file_path,
671bee91169SAlexander Graf 					  void *source_buffer,
672bee91169SAlexander Graf 					  unsigned long source_size,
673bee91169SAlexander Graf 					  efi_handle_t *image_handle)
674bee91169SAlexander Graf {
675bee91169SAlexander Graf 	static struct efi_object loaded_image_info_obj = {
676bee91169SAlexander Graf 		.protocols = {
677bee91169SAlexander Graf 			{
678bee91169SAlexander Graf 				.guid = &efi_guid_loaded_image,
679bee91169SAlexander Graf 			},
680bee91169SAlexander Graf 		},
681bee91169SAlexander Graf 	};
682bee91169SAlexander Graf 	struct efi_loaded_image *info;
683bee91169SAlexander Graf 	struct efi_object *obj;
684bee91169SAlexander Graf 
685bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
686bee91169SAlexander Graf 		  file_path, source_buffer, source_size, image_handle);
687bee91169SAlexander Graf 	info = malloc(sizeof(*info));
688b5349f74Sxypron.glpk@gmx.de 	loaded_image_info_obj.protocols[0].protocol_interface = info;
689bee91169SAlexander Graf 	obj = malloc(sizeof(loaded_image_info_obj));
690bee91169SAlexander Graf 	memset(info, 0, sizeof(*info));
691bee91169SAlexander Graf 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
692bee91169SAlexander Graf 	obj->handle = info;
693bee91169SAlexander Graf 	info->file_path = file_path;
694bee91169SAlexander Graf 	info->reserved = efi_load_pe(source_buffer, info);
695bee91169SAlexander Graf 	if (!info->reserved) {
696bee91169SAlexander Graf 		free(info);
697bee91169SAlexander Graf 		free(obj);
698bee91169SAlexander Graf 		return EFI_EXIT(EFI_UNSUPPORTED);
699bee91169SAlexander Graf 	}
700bee91169SAlexander Graf 
701bee91169SAlexander Graf 	*image_handle = info;
702bee91169SAlexander Graf 	list_add_tail(&obj->link, &efi_obj_list);
703bee91169SAlexander Graf 
704bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
705bee91169SAlexander Graf }
706bee91169SAlexander Graf 
707bee91169SAlexander Graf static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
708bee91169SAlexander Graf 					   unsigned long *exit_data_size,
709bee91169SAlexander Graf 					   s16 **exit_data)
710bee91169SAlexander Graf {
711bee91169SAlexander Graf 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
712bee91169SAlexander Graf 	struct efi_loaded_image *info = image_handle;
713bee91169SAlexander Graf 
714bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
715bee91169SAlexander Graf 	entry = info->reserved;
716bee91169SAlexander Graf 
717bee91169SAlexander Graf 	efi_is_direct_boot = false;
718bee91169SAlexander Graf 
719bee91169SAlexander Graf 	/* call the image! */
720a86aeaf2SAlexander Graf 	if (setjmp(&info->exit_jmp)) {
721a86aeaf2SAlexander Graf 		/* We returned from the child image */
722a86aeaf2SAlexander Graf 		return EFI_EXIT(info->exit_status);
723a86aeaf2SAlexander Graf 	}
724a86aeaf2SAlexander Graf 
725bee91169SAlexander Graf 	entry(image_handle, &systab);
726bee91169SAlexander Graf 
727bee91169SAlexander Graf 	/* Should usually never get here */
728bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
729bee91169SAlexander Graf }
730bee91169SAlexander Graf 
731a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
732a86aeaf2SAlexander Graf 			efi_status_t exit_status, unsigned long exit_data_size,
733a86aeaf2SAlexander Graf 			int16_t *exit_data)
734bee91169SAlexander Graf {
735a86aeaf2SAlexander Graf 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
736a86aeaf2SAlexander Graf 
737bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
738bee91169SAlexander Graf 		  exit_data_size, exit_data);
739a86aeaf2SAlexander Graf 
740a86aeaf2SAlexander Graf 	loaded_image_info->exit_status = exit_status;
741692fcdd8SAlexander Graf 	longjmp(&loaded_image_info->exit_jmp, 1);
742a86aeaf2SAlexander Graf 
743a86aeaf2SAlexander Graf 	panic("EFI application exited");
744bee91169SAlexander Graf }
745bee91169SAlexander Graf 
746bee91169SAlexander Graf static struct efi_object *efi_search_obj(void *handle)
747bee91169SAlexander Graf {
748bee91169SAlexander Graf 	struct list_head *lhandle;
749bee91169SAlexander Graf 
750bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
751bee91169SAlexander Graf 		struct efi_object *efiobj;
752bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
753bee91169SAlexander Graf 		if (efiobj->handle == handle)
754bee91169SAlexander Graf 			return efiobj;
755bee91169SAlexander Graf 	}
756bee91169SAlexander Graf 
757bee91169SAlexander Graf 	return NULL;
758bee91169SAlexander Graf }
759bee91169SAlexander Graf 
760bee91169SAlexander Graf static efi_status_t EFIAPI efi_unload_image(void *image_handle)
761bee91169SAlexander Graf {
762bee91169SAlexander Graf 	struct efi_object *efiobj;
763bee91169SAlexander Graf 
764bee91169SAlexander Graf 	EFI_ENTRY("%p", image_handle);
765bee91169SAlexander Graf 	efiobj = efi_search_obj(image_handle);
766bee91169SAlexander Graf 	if (efiobj)
767bee91169SAlexander Graf 		list_del(&efiobj->link);
768bee91169SAlexander Graf 
769bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
770bee91169SAlexander Graf }
771bee91169SAlexander Graf 
772bee91169SAlexander Graf static void efi_exit_caches(void)
773bee91169SAlexander Graf {
774bee91169SAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
775bee91169SAlexander Graf 	/*
776bee91169SAlexander Graf 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
777bee91169SAlexander Graf 	 * a zImage, but does not know of all cache layers. Give it a hand.
778bee91169SAlexander Graf 	 */
779bee91169SAlexander Graf 	if (efi_is_direct_boot)
780bee91169SAlexander Graf 		cleanup_before_linux();
781bee91169SAlexander Graf #endif
782bee91169SAlexander Graf }
783bee91169SAlexander Graf 
784bee91169SAlexander Graf static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
785bee91169SAlexander Graf 						  unsigned long map_key)
786bee91169SAlexander Graf {
787bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", image_handle, map_key);
788bee91169SAlexander Graf 
789b7b8410aSAlexander Graf 	board_quiesce_devices();
790b7b8410aSAlexander Graf 
791bee91169SAlexander Graf 	/* Fix up caches for EFI payloads if necessary */
792bee91169SAlexander Graf 	efi_exit_caches();
793bee91169SAlexander Graf 
794bee91169SAlexander Graf 	/* This stops all lingering devices */
795bee91169SAlexander Graf 	bootm_disable_interrupts();
796bee91169SAlexander Graf 
797bee91169SAlexander Graf 	/* Give the payload some time to boot */
798bee91169SAlexander Graf 	WATCHDOG_RESET();
799bee91169SAlexander Graf 
800bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
801bee91169SAlexander Graf }
802bee91169SAlexander Graf 
803bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
804bee91169SAlexander Graf {
805bee91169SAlexander Graf 	static uint64_t mono = 0;
806bee91169SAlexander Graf 	EFI_ENTRY("%p", count);
807bee91169SAlexander Graf 	*count = mono++;
808bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
809bee91169SAlexander Graf }
810bee91169SAlexander Graf 
811bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
812bee91169SAlexander Graf {
813bee91169SAlexander Graf 	EFI_ENTRY("%ld", microseconds);
814bee91169SAlexander Graf 	udelay(microseconds);
815bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
816bee91169SAlexander Graf }
817bee91169SAlexander Graf 
818bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
819bee91169SAlexander Graf 						  uint64_t watchdog_code,
820bee91169SAlexander Graf 						  unsigned long data_size,
821bee91169SAlexander Graf 						  uint16_t *watchdog_data)
822bee91169SAlexander Graf {
823bee91169SAlexander Graf 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
824bee91169SAlexander Graf 		  data_size, watchdog_data);
825bee91169SAlexander Graf 	return EFI_EXIT(efi_unsupported(__func__));
826bee91169SAlexander Graf }
827bee91169SAlexander Graf 
828bee91169SAlexander Graf static efi_status_t EFIAPI efi_connect_controller(
829bee91169SAlexander Graf 			efi_handle_t controller_handle,
830bee91169SAlexander Graf 			efi_handle_t *driver_image_handle,
831bee91169SAlexander Graf 			struct efi_device_path *remain_device_path,
832bee91169SAlexander Graf 			bool recursive)
833bee91169SAlexander Graf {
834bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
835bee91169SAlexander Graf 		  remain_device_path, recursive);
836bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
837bee91169SAlexander Graf }
838bee91169SAlexander Graf 
839bee91169SAlexander Graf static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
840bee91169SAlexander Graf 						     void *driver_image_handle,
841bee91169SAlexander Graf 						     void *child_handle)
842bee91169SAlexander Graf {
843bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
844bee91169SAlexander Graf 		  child_handle);
845bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
846bee91169SAlexander Graf }
847bee91169SAlexander Graf 
848bee91169SAlexander Graf static efi_status_t EFIAPI efi_close_protocol(void *handle,
849bee91169SAlexander Graf 					      efi_guid_t *protocol,
850bee91169SAlexander Graf 					      void *agent_handle,
851bee91169SAlexander Graf 					      void *controller_handle)
852bee91169SAlexander Graf {
853bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
854bee91169SAlexander Graf 		  controller_handle);
855bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
856bee91169SAlexander Graf }
857bee91169SAlexander Graf 
858bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
859bee91169SAlexander Graf 			efi_guid_t *protocol,
860bee91169SAlexander Graf 			struct efi_open_protocol_info_entry **entry_buffer,
861bee91169SAlexander Graf 			unsigned long *entry_count)
862bee91169SAlexander Graf {
863bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
864bee91169SAlexander Graf 		  entry_count);
865bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
866bee91169SAlexander Graf }
867bee91169SAlexander Graf 
868bee91169SAlexander Graf static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
869bee91169SAlexander Graf 			efi_guid_t ***protocol_buffer,
870bee91169SAlexander Graf 			unsigned long *protocol_buffer_count)
871bee91169SAlexander Graf {
872bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
873bee91169SAlexander Graf 		  protocol_buffer_count);
874bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
875bee91169SAlexander Graf }
876bee91169SAlexander Graf 
877bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
878bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
879bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
880bee91169SAlexander Graf 			unsigned long *no_handles, efi_handle_t **buffer)
881bee91169SAlexander Graf {
882c2e703f9Sxypron.glpk@gmx.de 	efi_status_t r;
883c2e703f9Sxypron.glpk@gmx.de 	unsigned long buffer_size = 0;
884c2e703f9Sxypron.glpk@gmx.de 
885bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
886bee91169SAlexander Graf 		  no_handles, buffer);
887c2e703f9Sxypron.glpk@gmx.de 
888c2e703f9Sxypron.glpk@gmx.de 	if (!no_handles || !buffer) {
889c2e703f9Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
890c2e703f9Sxypron.glpk@gmx.de 		goto out;
891c2e703f9Sxypron.glpk@gmx.de 	}
892c2e703f9Sxypron.glpk@gmx.de 	*no_handles = 0;
893c2e703f9Sxypron.glpk@gmx.de 	*buffer = NULL;
894c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
895c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
896c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_BUFFER_TOO_SMALL)
897c2e703f9Sxypron.glpk@gmx.de 		goto out;
898c2e703f9Sxypron.glpk@gmx.de 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
899c2e703f9Sxypron.glpk@gmx.de 			      (void **)buffer);
900c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
901c2e703f9Sxypron.glpk@gmx.de 		goto out;
902c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
903c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
904c2e703f9Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
905c2e703f9Sxypron.glpk@gmx.de 		*no_handles = buffer_size / sizeof(void *);
906c2e703f9Sxypron.glpk@gmx.de out:
907c2e703f9Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
908bee91169SAlexander Graf }
909bee91169SAlexander Graf 
910bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
911bee91169SAlexander Graf 					       void *registration,
912bee91169SAlexander Graf 					       void **protocol_interface)
913bee91169SAlexander Graf {
91488adae5eSxypron.glpk@gmx.de 	struct list_head *lhandle;
915bee91169SAlexander Graf 	int i;
916bee91169SAlexander Graf 
917bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
91888adae5eSxypron.glpk@gmx.de 
91988adae5eSxypron.glpk@gmx.de 	if (!protocol || !protocol_interface)
92088adae5eSxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
92188adae5eSxypron.glpk@gmx.de 
92288adae5eSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
92388adae5eSxypron.glpk@gmx.de 		struct efi_object *efiobj;
92488adae5eSxypron.glpk@gmx.de 
92588adae5eSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
92688adae5eSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
92788adae5eSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
92888adae5eSxypron.glpk@gmx.de 
92988adae5eSxypron.glpk@gmx.de 			if (!handler->guid)
93088adae5eSxypron.glpk@gmx.de 				continue;
93188adae5eSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
93288adae5eSxypron.glpk@gmx.de 				*protocol_interface =
93388adae5eSxypron.glpk@gmx.de 					handler->protocol_interface;
934bee91169SAlexander Graf 				return EFI_EXIT(EFI_SUCCESS);
935bee91169SAlexander Graf 			}
936bee91169SAlexander Graf 		}
93788adae5eSxypron.glpk@gmx.de 	}
93888adae5eSxypron.glpk@gmx.de 	*protocol_interface = NULL;
939bee91169SAlexander Graf 
940bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
941bee91169SAlexander Graf }
942bee91169SAlexander Graf 
943bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
944bee91169SAlexander Graf 			void **handle, ...)
945bee91169SAlexander Graf {
946bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
94758b83586Sxypron.glpk@gmx.de 
94858b83586Sxypron.glpk@gmx.de 	va_list argptr;
94958b83586Sxypron.glpk@gmx.de 	efi_guid_t *protocol;
95058b83586Sxypron.glpk@gmx.de 	void *protocol_interface;
95158b83586Sxypron.glpk@gmx.de 	efi_status_t r = EFI_SUCCESS;
95258b83586Sxypron.glpk@gmx.de 	int i = 0;
95358b83586Sxypron.glpk@gmx.de 
95458b83586Sxypron.glpk@gmx.de 	if (!handle)
95558b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
95658b83586Sxypron.glpk@gmx.de 
95758b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
95858b83586Sxypron.glpk@gmx.de 	for (;;) {
95958b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
96058b83586Sxypron.glpk@gmx.de 		if (!protocol)
96158b83586Sxypron.glpk@gmx.de 			break;
96258b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
96358b83586Sxypron.glpk@gmx.de 		r = efi_install_protocol_interface(handle, protocol,
96458b83586Sxypron.glpk@gmx.de 						   EFI_NATIVE_INTERFACE,
96558b83586Sxypron.glpk@gmx.de 						   protocol_interface);
96658b83586Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
96758b83586Sxypron.glpk@gmx.de 			break;
96858b83586Sxypron.glpk@gmx.de 		i++;
96958b83586Sxypron.glpk@gmx.de 	}
97058b83586Sxypron.glpk@gmx.de 	va_end(argptr);
97158b83586Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
97258b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(r);
97358b83586Sxypron.glpk@gmx.de 
97458b83586Sxypron.glpk@gmx.de 	/* If an error occured undo all changes. */
97558b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
97658b83586Sxypron.glpk@gmx.de 	for (; i; --i) {
97758b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
97858b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
97958b83586Sxypron.glpk@gmx.de 		efi_uninstall_protocol_interface(handle, protocol,
98058b83586Sxypron.glpk@gmx.de 						 protocol_interface);
98158b83586Sxypron.glpk@gmx.de 	}
98258b83586Sxypron.glpk@gmx.de 	va_end(argptr);
98358b83586Sxypron.glpk@gmx.de 
98458b83586Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
985bee91169SAlexander Graf }
986bee91169SAlexander Graf 
987bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
988bee91169SAlexander Graf 			void *handle, ...)
989bee91169SAlexander Graf {
990bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
991bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
992bee91169SAlexander Graf }
993bee91169SAlexander Graf 
994bee91169SAlexander Graf static efi_status_t EFIAPI efi_calculate_crc32(void *data,
995bee91169SAlexander Graf 					       unsigned long data_size,
996bee91169SAlexander Graf 					       uint32_t *crc32_p)
997bee91169SAlexander Graf {
998bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", data, data_size);
999bee91169SAlexander Graf 	*crc32_p = crc32(0, data, data_size);
1000bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
1001bee91169SAlexander Graf }
1002bee91169SAlexander Graf 
1003bee91169SAlexander Graf static void EFIAPI efi_copy_mem(void *destination, void *source,
1004bee91169SAlexander Graf 				unsigned long length)
1005bee91169SAlexander Graf {
1006bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
1007bee91169SAlexander Graf 	memcpy(destination, source, length);
1008bee91169SAlexander Graf }
1009bee91169SAlexander Graf 
1010bee91169SAlexander Graf static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1011bee91169SAlexander Graf {
1012bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1013bee91169SAlexander Graf 	memset(buffer, value, size);
1014bee91169SAlexander Graf }
1015bee91169SAlexander Graf 
1016bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol(
1017bee91169SAlexander Graf 			void *handle, efi_guid_t *protocol,
1018bee91169SAlexander Graf 			void **protocol_interface, void *agent_handle,
1019bee91169SAlexander Graf 			void *controller_handle, uint32_t attributes)
1020bee91169SAlexander Graf {
1021bee91169SAlexander Graf 	struct list_head *lhandle;
1022bee91169SAlexander Graf 	int i;
102369baec67Sxypron.glpk@gmx.de 	efi_status_t r = EFI_INVALID_PARAMETER;
1024bee91169SAlexander Graf 
1025bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1026bee91169SAlexander Graf 		  protocol_interface, agent_handle, controller_handle,
1027bee91169SAlexander Graf 		  attributes);
1028b5349f74Sxypron.glpk@gmx.de 
102969baec67Sxypron.glpk@gmx.de 	if (!handle || !protocol ||
103069baec67Sxypron.glpk@gmx.de 	    (!protocol_interface && attributes !=
103169baec67Sxypron.glpk@gmx.de 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
103269baec67Sxypron.glpk@gmx.de 		goto out;
103369baec67Sxypron.glpk@gmx.de 	}
103469baec67Sxypron.glpk@gmx.de 
103569baec67Sxypron.glpk@gmx.de 	switch (attributes) {
103669baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
103769baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
103869baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
103969baec67Sxypron.glpk@gmx.de 		break;
104069baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
104169baec67Sxypron.glpk@gmx.de 		if (controller_handle == handle)
104269baec67Sxypron.glpk@gmx.de 			goto out;
104369baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
104469baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
104569baec67Sxypron.glpk@gmx.de 		if (controller_handle == NULL)
104669baec67Sxypron.glpk@gmx.de 			goto out;
104769baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
104869baec67Sxypron.glpk@gmx.de 		if (agent_handle == NULL)
104969baec67Sxypron.glpk@gmx.de 			goto out;
105069baec67Sxypron.glpk@gmx.de 		break;
105169baec67Sxypron.glpk@gmx.de 	default:
1052b5349f74Sxypron.glpk@gmx.de 		goto out;
1053b5349f74Sxypron.glpk@gmx.de 	}
1054b5349f74Sxypron.glpk@gmx.de 
1055bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
1056bee91169SAlexander Graf 		struct efi_object *efiobj;
1057bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
1058bee91169SAlexander Graf 
1059bee91169SAlexander Graf 		if (efiobj->handle != handle)
1060bee91169SAlexander Graf 			continue;
1061bee91169SAlexander Graf 
1062bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1063bee91169SAlexander Graf 			struct efi_handler *handler = &efiobj->protocols[i];
1064bee91169SAlexander Graf 			const efi_guid_t *hprotocol = handler->guid;
1065bee91169SAlexander Graf 			if (!hprotocol)
10664b6ed0d7Sxypron.glpk@gmx.de 				continue;
1067bee91169SAlexander Graf 			if (!guidcmp(hprotocol, protocol)) {
1068b5349f74Sxypron.glpk@gmx.de 				if (attributes !=
1069b5349f74Sxypron.glpk@gmx.de 				    EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1070b5349f74Sxypron.glpk@gmx.de 					*protocol_interface =
1071b5349f74Sxypron.glpk@gmx.de 						handler->protocol_interface;
1072b5349f74Sxypron.glpk@gmx.de 				}
1073b5349f74Sxypron.glpk@gmx.de 				r = EFI_SUCCESS;
1074bee91169SAlexander Graf 				goto out;
1075bee91169SAlexander Graf 			}
1076bee91169SAlexander Graf 		}
107769baec67Sxypron.glpk@gmx.de 		goto unsupported;
1078bee91169SAlexander Graf 	}
1079bee91169SAlexander Graf 
108069baec67Sxypron.glpk@gmx.de unsupported:
108169baec67Sxypron.glpk@gmx.de 	r = EFI_UNSUPPORTED;
1082bee91169SAlexander Graf out:
1083bee91169SAlexander Graf 	return EFI_EXIT(r);
1084bee91169SAlexander Graf }
1085bee91169SAlexander Graf 
1086bee91169SAlexander Graf static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1087bee91169SAlexander Graf 					       efi_guid_t *protocol,
1088bee91169SAlexander Graf 					       void **protocol_interface)
1089bee91169SAlexander Graf {
10908e1d329fSxypron.glpk@gmx.de 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
10918e1d329fSxypron.glpk@gmx.de 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1092bee91169SAlexander Graf }
1093bee91169SAlexander Graf 
1094bee91169SAlexander Graf static const struct efi_boot_services efi_boot_services = {
1095bee91169SAlexander Graf 	.hdr = {
1096bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1097bee91169SAlexander Graf 	},
1098bee91169SAlexander Graf 	.raise_tpl = efi_raise_tpl,
1099bee91169SAlexander Graf 	.restore_tpl = efi_restore_tpl,
1100bee91169SAlexander Graf 	.allocate_pages = efi_allocate_pages_ext,
1101bee91169SAlexander Graf 	.free_pages = efi_free_pages_ext,
1102bee91169SAlexander Graf 	.get_memory_map = efi_get_memory_map_ext,
1103ead1274bSStefan Brüns 	.allocate_pool = efi_allocate_pool_ext,
110442417bc8SStefan Brüns 	.free_pool = efi_free_pool_ext,
110549deb455Sxypron.glpk@gmx.de 	.create_event = efi_create_event_ext,
1106bfc72462Sxypron.glpk@gmx.de 	.set_timer = efi_set_timer_ext,
1107bee91169SAlexander Graf 	.wait_for_event = efi_wait_for_event,
1108c6841592Sxypron.glpk@gmx.de 	.signal_event = efi_signal_event_ext,
1109bee91169SAlexander Graf 	.close_event = efi_close_event,
1110bee91169SAlexander Graf 	.check_event = efi_check_event,
11118bee5a3cSxypron.glpk@gmx.de 	.install_protocol_interface = efi_install_protocol_interface_ext,
1112bee91169SAlexander Graf 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
11133d8e1456Sxypron.glpk@gmx.de 	.uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1114bee91169SAlexander Graf 	.handle_protocol = efi_handle_protocol,
1115bee91169SAlexander Graf 	.reserved = NULL,
1116bee91169SAlexander Graf 	.register_protocol_notify = efi_register_protocol_notify,
111726329584Sxypron.glpk@gmx.de 	.locate_handle = efi_locate_handle_ext,
1118bee91169SAlexander Graf 	.locate_device_path = efi_locate_device_path,
1119488bf12dSAlexander Graf 	.install_configuration_table = efi_install_configuration_table_ext,
1120bee91169SAlexander Graf 	.load_image = efi_load_image,
1121bee91169SAlexander Graf 	.start_image = efi_start_image,
1122a86aeaf2SAlexander Graf 	.exit = efi_exit,
1123bee91169SAlexander Graf 	.unload_image = efi_unload_image,
1124bee91169SAlexander Graf 	.exit_boot_services = efi_exit_boot_services,
1125bee91169SAlexander Graf 	.get_next_monotonic_count = efi_get_next_monotonic_count,
1126bee91169SAlexander Graf 	.stall = efi_stall,
1127bee91169SAlexander Graf 	.set_watchdog_timer = efi_set_watchdog_timer,
1128bee91169SAlexander Graf 	.connect_controller = efi_connect_controller,
1129bee91169SAlexander Graf 	.disconnect_controller = efi_disconnect_controller,
1130bee91169SAlexander Graf 	.open_protocol = efi_open_protocol,
1131bee91169SAlexander Graf 	.close_protocol = efi_close_protocol,
1132bee91169SAlexander Graf 	.open_protocol_information = efi_open_protocol_information,
1133bee91169SAlexander Graf 	.protocols_per_handle = efi_protocols_per_handle,
1134bee91169SAlexander Graf 	.locate_handle_buffer = efi_locate_handle_buffer,
1135bee91169SAlexander Graf 	.locate_protocol = efi_locate_protocol,
1136bee91169SAlexander Graf 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1137bee91169SAlexander Graf 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1138bee91169SAlexander Graf 	.calculate_crc32 = efi_calculate_crc32,
1139bee91169SAlexander Graf 	.copy_mem = efi_copy_mem,
1140bee91169SAlexander Graf 	.set_mem = efi_set_mem,
1141bee91169SAlexander Graf };
1142bee91169SAlexander Graf 
1143bee91169SAlexander Graf 
11443c63db9cSAlexander Graf static uint16_t __efi_runtime_data firmware_vendor[] =
1145bee91169SAlexander Graf 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
1146bee91169SAlexander Graf 
11473c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
1148bee91169SAlexander Graf 	.hdr = {
1149bee91169SAlexander Graf 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
1150bee91169SAlexander Graf 		.revision = 0x20005, /* 2.5 */
1151bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1152bee91169SAlexander Graf 	},
1153bee91169SAlexander Graf 	.fw_vendor = (long)firmware_vendor,
1154bee91169SAlexander Graf 	.con_in = (void*)&efi_con_in,
1155bee91169SAlexander Graf 	.con_out = (void*)&efi_con_out,
1156bee91169SAlexander Graf 	.std_err = (void*)&efi_con_out,
1157bee91169SAlexander Graf 	.runtime = (void*)&efi_runtime_services,
1158bee91169SAlexander Graf 	.boottime = (void*)&efi_boot_services,
1159bee91169SAlexander Graf 	.nr_tables = 0,
1160bee91169SAlexander Graf 	.tables = (void*)efi_conf_table,
1161bee91169SAlexander Graf };
1162