xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_boottime.c (revision 0e00a84cdedf7a1949486746225b35984b351eca)
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>
13*0e00a84cSMasahiro Yamada #include <linux/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 
52c160d2f5SRob Clark static int entry_count;
53af65db85SRob Clark static int nesting_level;
54c160d2f5SRob Clark 
55c160d2f5SRob Clark /* Called on every callback entry */
56c160d2f5SRob Clark int __efi_entry_check(void)
57c160d2f5SRob Clark {
58c160d2f5SRob Clark 	int ret = entry_count++ == 0;
59c160d2f5SRob Clark #ifdef CONFIG_ARM
60c160d2f5SRob Clark 	assert(efi_gd);
61c160d2f5SRob Clark 	app_gd = gd;
62c160d2f5SRob Clark 	gd = efi_gd;
63c160d2f5SRob Clark #endif
64c160d2f5SRob Clark 	return ret;
65c160d2f5SRob Clark }
66c160d2f5SRob Clark 
67c160d2f5SRob Clark /* Called on every callback exit */
68c160d2f5SRob Clark int __efi_exit_check(void)
69c160d2f5SRob Clark {
70c160d2f5SRob Clark 	int ret = --entry_count == 0;
71c160d2f5SRob Clark #ifdef CONFIG_ARM
72c160d2f5SRob Clark 	gd = app_gd;
73c160d2f5SRob Clark #endif
74c160d2f5SRob Clark 	return ret;
75c160d2f5SRob Clark }
76c160d2f5SRob Clark 
77bee91169SAlexander Graf /* Called from do_bootefi_exec() */
78bee91169SAlexander Graf void efi_save_gd(void)
79bee91169SAlexander Graf {
8065e4c0b1SSimon Glass #ifdef CONFIG_ARM
81bee91169SAlexander Graf 	efi_gd = gd;
8265e4c0b1SSimon Glass #endif
83bee91169SAlexander Graf }
84bee91169SAlexander Graf 
85c160d2f5SRob Clark /*
86c160d2f5SRob Clark  * Special case handler for error/abort that just forces things back
87c160d2f5SRob Clark  * to u-boot world so we can dump out an abort msg, without any care
88c160d2f5SRob Clark  * about returning back to UEFI world.
89c160d2f5SRob Clark  */
90bee91169SAlexander Graf void efi_restore_gd(void)
91bee91169SAlexander Graf {
9265e4c0b1SSimon Glass #ifdef CONFIG_ARM
93bee91169SAlexander Graf 	/* Only restore if we're already in EFI context */
94bee91169SAlexander Graf 	if (!efi_gd)
95bee91169SAlexander Graf 		return;
96bee91169SAlexander Graf 	gd = efi_gd;
9765e4c0b1SSimon Glass #endif
98bee91169SAlexander Graf }
99bee91169SAlexander Graf 
100af65db85SRob Clark /*
101af65db85SRob Clark  * Two spaces per indent level, maxing out at 10.. which ought to be
102af65db85SRob Clark  * enough for anyone ;-)
103af65db85SRob Clark  */
104af65db85SRob Clark static const char *indent_string(int level)
105af65db85SRob Clark {
106af65db85SRob Clark 	const char *indent = "                    ";
107af65db85SRob Clark 	const int max = strlen(indent);
108af65db85SRob Clark 	level = min(max, level * 2);
109af65db85SRob Clark 	return &indent[max - level];
110af65db85SRob Clark }
111af65db85SRob Clark 
112af65db85SRob Clark const char *__efi_nesting_inc(void)
113af65db85SRob Clark {
114af65db85SRob Clark 	return indent_string(nesting_level++);
115af65db85SRob Clark }
116af65db85SRob Clark 
117af65db85SRob Clark const char *__efi_nesting_dec(void)
118af65db85SRob Clark {
119af65db85SRob Clark 	return indent_string(--nesting_level);
120af65db85SRob Clark }
121af65db85SRob Clark 
1228787b02eSxypron.glpk@gmx.de /* Low 32 bit */
1238787b02eSxypron.glpk@gmx.de #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
1248787b02eSxypron.glpk@gmx.de /* High 32 bit */
1258787b02eSxypron.glpk@gmx.de #define EFI_HIGH32(a) (a >> 32)
1268787b02eSxypron.glpk@gmx.de 
1278787b02eSxypron.glpk@gmx.de /*
1288787b02eSxypron.glpk@gmx.de  * 64bit division by 10 implemented as multiplication by 1 / 10
1298787b02eSxypron.glpk@gmx.de  *
1308787b02eSxypron.glpk@gmx.de  * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
1318787b02eSxypron.glpk@gmx.de  */
1328787b02eSxypron.glpk@gmx.de #define EFI_TENTH 0x199999999999999A
1338787b02eSxypron.glpk@gmx.de static u64 efi_div10(u64 a)
1348787b02eSxypron.glpk@gmx.de {
1358787b02eSxypron.glpk@gmx.de 	u64 prod;
1368787b02eSxypron.glpk@gmx.de 	u64 rem;
1378787b02eSxypron.glpk@gmx.de 	u64 ret;
1388787b02eSxypron.glpk@gmx.de 
1398787b02eSxypron.glpk@gmx.de 	ret  = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
1408787b02eSxypron.glpk@gmx.de 	prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
1418787b02eSxypron.glpk@gmx.de 	rem  = EFI_LOW32(prod);
1428787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
1438787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
1448787b02eSxypron.glpk@gmx.de 	rem += EFI_LOW32(prod);
1458787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
1468787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
1478787b02eSxypron.glpk@gmx.de 	rem += EFI_HIGH32(prod);
1488787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(rem);
1498787b02eSxypron.glpk@gmx.de 	/* Round to nearest integer */
1508787b02eSxypron.glpk@gmx.de 	if (rem >= (1 << 31))
1518787b02eSxypron.glpk@gmx.de 		++ret;
1528787b02eSxypron.glpk@gmx.de 	return ret;
1538787b02eSxypron.glpk@gmx.de }
1548787b02eSxypron.glpk@gmx.de 
15591be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event)
156c6841592Sxypron.glpk@gmx.de {
157c6841592Sxypron.glpk@gmx.de 	if (event->signaled)
158c6841592Sxypron.glpk@gmx.de 		return;
159c6841592Sxypron.glpk@gmx.de 	event->signaled = 1;
160c6841592Sxypron.glpk@gmx.de 	if (event->type & EVT_NOTIFY_SIGNAL) {
161a095aadfSRob Clark 		EFI_CALL(event->notify_function(event, event->notify_context));
162c6841592Sxypron.glpk@gmx.de 	}
163c6841592Sxypron.glpk@gmx.de }
164c6841592Sxypron.glpk@gmx.de 
165bee91169SAlexander Graf static efi_status_t efi_unsupported(const char *funcname)
166bee91169SAlexander Graf {
167edcef3baSAlexander Graf 	debug("EFI: App called into unimplemented function %s\n", funcname);
168bee91169SAlexander Graf 	return EFI_EXIT(EFI_UNSUPPORTED);
169bee91169SAlexander Graf }
170bee91169SAlexander Graf 
171503f2695Sxypron.glpk@gmx.de static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
172bee91169SAlexander Graf {
173503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", new_tpl);
174bee91169SAlexander Graf 	return EFI_EXIT(0);
175bee91169SAlexander Graf }
176bee91169SAlexander Graf 
177503f2695Sxypron.glpk@gmx.de static void EFIAPI efi_restore_tpl(UINTN old_tpl)
178bee91169SAlexander Graf {
179503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", old_tpl);
180b5104821SRob Clark 	efi_unsupported(__func__);
181bee91169SAlexander Graf }
182bee91169SAlexander Graf 
1836e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
184bee91169SAlexander Graf 						  unsigned long pages,
185bee91169SAlexander Graf 						  uint64_t *memory)
186bee91169SAlexander Graf {
187bee91169SAlexander Graf 	efi_status_t r;
188bee91169SAlexander Graf 
189bee91169SAlexander Graf 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
190bee91169SAlexander Graf 	r = efi_allocate_pages(type, memory_type, pages, memory);
191bee91169SAlexander Graf 	return EFI_EXIT(r);
192bee91169SAlexander Graf }
193bee91169SAlexander Graf 
1946e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
1956e0bf8d8SMasahiro Yamada 					      unsigned long pages)
196bee91169SAlexander Graf {
197bee91169SAlexander Graf 	efi_status_t r;
198bee91169SAlexander Graf 
199bee91169SAlexander Graf 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
200bee91169SAlexander Graf 	r = efi_free_pages(memory, pages);
201bee91169SAlexander Graf 	return EFI_EXIT(r);
202bee91169SAlexander Graf }
203bee91169SAlexander Graf 
2046e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
2056e0bf8d8SMasahiro Yamada 					unsigned long *memory_map_size,
206bee91169SAlexander Graf 					struct efi_mem_desc *memory_map,
207bee91169SAlexander Graf 					unsigned long *map_key,
208bee91169SAlexander Graf 					unsigned long *descriptor_size,
209bee91169SAlexander Graf 					uint32_t *descriptor_version)
210bee91169SAlexander Graf {
211bee91169SAlexander Graf 	efi_status_t r;
212bee91169SAlexander Graf 
213bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
214bee91169SAlexander Graf 		  map_key, descriptor_size, descriptor_version);
215bee91169SAlexander Graf 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
216bee91169SAlexander Graf 			       descriptor_size, descriptor_version);
217bee91169SAlexander Graf 	return EFI_EXIT(r);
218bee91169SAlexander Graf }
219bee91169SAlexander Graf 
220ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
221ead1274bSStefan Brüns 						 unsigned long size,
222bee91169SAlexander Graf 						 void **buffer)
223bee91169SAlexander Graf {
2241cd29f0aSAlexander Graf 	efi_status_t r;
2251cd29f0aSAlexander Graf 
2261cd29f0aSAlexander Graf 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
227ead1274bSStefan Brüns 	r = efi_allocate_pool(pool_type, size, buffer);
2281cd29f0aSAlexander Graf 	return EFI_EXIT(r);
229bee91169SAlexander Graf }
230bee91169SAlexander Graf 
23142417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
232bee91169SAlexander Graf {
2331cd29f0aSAlexander Graf 	efi_status_t r;
2341cd29f0aSAlexander Graf 
2351cd29f0aSAlexander Graf 	EFI_ENTRY("%p", buffer);
23642417bc8SStefan Brüns 	r = efi_free_pool(buffer);
2371cd29f0aSAlexander Graf 	return EFI_EXIT(r);
238bee91169SAlexander Graf }
239bee91169SAlexander Graf 
240bee91169SAlexander Graf /*
241c6841592Sxypron.glpk@gmx.de  * Our event capabilities are very limited. Only a small limited
242c6841592Sxypron.glpk@gmx.de  * number of events is allowed to coexist.
243bee91169SAlexander Graf  */
244c6841592Sxypron.glpk@gmx.de static struct efi_event efi_events[16];
245bee91169SAlexander Graf 
246b521d29eSxypron.glpk@gmx.de efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
2472fd945feSxypron.glpk@gmx.de 			      void (EFIAPI *notify_function) (
2482fd945feSxypron.glpk@gmx.de 					struct efi_event *event,
249e275458cSSimon Glass 					void *context),
2502fd945feSxypron.glpk@gmx.de 			      void *notify_context, struct efi_event **event)
251bee91169SAlexander Graf {
252c6841592Sxypron.glpk@gmx.de 	int i;
253c6841592Sxypron.glpk@gmx.de 
254a95343b8SJonathan Gray 	if (event == NULL)
25549deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
256a95343b8SJonathan Gray 
257a95343b8SJonathan Gray 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
25849deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
259a95343b8SJonathan Gray 
260a95343b8SJonathan Gray 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
261a95343b8SJonathan Gray 	    notify_function == NULL)
26249deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
263a95343b8SJonathan Gray 
264c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
265c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].type)
266c6841592Sxypron.glpk@gmx.de 			continue;
267c6841592Sxypron.glpk@gmx.de 		efi_events[i].type = type;
268c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_tpl = notify_tpl;
269c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_function = notify_function;
270c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_context = notify_context;
271c6841592Sxypron.glpk@gmx.de 		/* Disable timers on bootup */
272c6841592Sxypron.glpk@gmx.de 		efi_events[i].trigger_next = -1ULL;
273c6841592Sxypron.glpk@gmx.de 		efi_events[i].signaled = 0;
274c6841592Sxypron.glpk@gmx.de 		*event = &efi_events[i];
27549deb455Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
276bee91169SAlexander Graf 	}
27749deb455Sxypron.glpk@gmx.de 	return EFI_OUT_OF_RESOURCES;
278c6841592Sxypron.glpk@gmx.de }
279bee91169SAlexander Graf 
28049deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
281b521d29eSxypron.glpk@gmx.de 			uint32_t type, UINTN notify_tpl,
28249deb455Sxypron.glpk@gmx.de 			void (EFIAPI *notify_function) (
28349deb455Sxypron.glpk@gmx.de 					struct efi_event *event,
28449deb455Sxypron.glpk@gmx.de 					void *context),
28549deb455Sxypron.glpk@gmx.de 			void *notify_context, struct efi_event **event)
28649deb455Sxypron.glpk@gmx.de {
28749deb455Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
28849deb455Sxypron.glpk@gmx.de 		  notify_context);
28949deb455Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
29049deb455Sxypron.glpk@gmx.de 					 notify_context, event));
29149deb455Sxypron.glpk@gmx.de }
29249deb455Sxypron.glpk@gmx.de 
29349deb455Sxypron.glpk@gmx.de 
294bee91169SAlexander Graf /*
295bee91169SAlexander Graf  * Our timers have to work without interrupts, so we check whenever keyboard
296bee91169SAlexander Graf  * input or disk accesses happen if enough time elapsed for it to fire.
297bee91169SAlexander Graf  */
298bee91169SAlexander Graf void efi_timer_check(void)
299bee91169SAlexander Graf {
300c6841592Sxypron.glpk@gmx.de 	int i;
301bee91169SAlexander Graf 	u64 now = timer_get_us();
302bee91169SAlexander Graf 
303c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
304c6841592Sxypron.glpk@gmx.de 		if (!efi_events[i].type ||
305c6841592Sxypron.glpk@gmx.de 		    !(efi_events[i].type & EVT_TIMER) ||
306c6841592Sxypron.glpk@gmx.de 		    efi_events[i].trigger_type == EFI_TIMER_STOP ||
307c6841592Sxypron.glpk@gmx.de 		    now < efi_events[i].trigger_next)
308c6841592Sxypron.glpk@gmx.de 			continue;
309c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
310c6841592Sxypron.glpk@gmx.de 			efi_events[i].trigger_next +=
3118787b02eSxypron.glpk@gmx.de 				efi_events[i].trigger_time;
312c6841592Sxypron.glpk@gmx.de 			efi_events[i].signaled = 0;
313bee91169SAlexander Graf 		}
314c6841592Sxypron.glpk@gmx.de 		efi_signal_event(&efi_events[i]);
315c6841592Sxypron.glpk@gmx.de 	}
316bee91169SAlexander Graf 	WATCHDOG_RESET();
317bee91169SAlexander Graf }
318bee91169SAlexander Graf 
319b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
320bee91169SAlexander Graf 			   uint64_t trigger_time)
321bee91169SAlexander Graf {
322c6841592Sxypron.glpk@gmx.de 	int i;
323bee91169SAlexander Graf 
3248787b02eSxypron.glpk@gmx.de 	/*
3258787b02eSxypron.glpk@gmx.de 	 * The parameter defines a multiple of 100ns.
3268787b02eSxypron.glpk@gmx.de 	 * We use multiples of 1000ns. So divide by 10.
3278787b02eSxypron.glpk@gmx.de 	 */
3288787b02eSxypron.glpk@gmx.de 	trigger_time = efi_div10(trigger_time);
329bee91169SAlexander Graf 
330c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
331c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
332c6841592Sxypron.glpk@gmx.de 			continue;
333bee91169SAlexander Graf 
334c6841592Sxypron.glpk@gmx.de 		if (!(event->type & EVT_TIMER))
335c6841592Sxypron.glpk@gmx.de 			break;
336bee91169SAlexander Graf 		switch (type) {
337bee91169SAlexander Graf 		case EFI_TIMER_STOP:
338c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
339bee91169SAlexander Graf 			break;
340bee91169SAlexander Graf 		case EFI_TIMER_PERIODIC:
341bee91169SAlexander Graf 		case EFI_TIMER_RELATIVE:
342c6841592Sxypron.glpk@gmx.de 			event->trigger_next =
3438787b02eSxypron.glpk@gmx.de 				timer_get_us() + trigger_time;
344bee91169SAlexander Graf 			break;
345bee91169SAlexander Graf 		default:
346bfc72462Sxypron.glpk@gmx.de 			return EFI_INVALID_PARAMETER;
347bee91169SAlexander Graf 		}
348c6841592Sxypron.glpk@gmx.de 		event->trigger_type = type;
349c6841592Sxypron.glpk@gmx.de 		event->trigger_time = trigger_time;
350bfc72462Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
351bee91169SAlexander Graf 	}
352bfc72462Sxypron.glpk@gmx.de 	return EFI_INVALID_PARAMETER;
353bfc72462Sxypron.glpk@gmx.de }
354bfc72462Sxypron.glpk@gmx.de 
355b521d29eSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
356b521d29eSxypron.glpk@gmx.de 					     enum efi_timer_delay type,
357bfc72462Sxypron.glpk@gmx.de 					     uint64_t trigger_time)
358bfc72462Sxypron.glpk@gmx.de {
359bfc72462Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
360bfc72462Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
361c6841592Sxypron.glpk@gmx.de }
362bee91169SAlexander Graf 
363bee91169SAlexander Graf static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
3642fd945feSxypron.glpk@gmx.de 					      struct efi_event **event,
3652fd945feSxypron.glpk@gmx.de 					      unsigned long *index)
366bee91169SAlexander Graf {
367c6841592Sxypron.glpk@gmx.de 	int i, j;
368bee91169SAlexander Graf 
369bee91169SAlexander Graf 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
370bee91169SAlexander Graf 
371c6841592Sxypron.glpk@gmx.de 	/* Check parameters */
372c6841592Sxypron.glpk@gmx.de 	if (!num_events || !event)
373c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
374c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < num_events; ++i) {
375c6841592Sxypron.glpk@gmx.de 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
376c6841592Sxypron.glpk@gmx.de 			if (event[i] == &efi_events[j])
377c6841592Sxypron.glpk@gmx.de 				goto known_event;
378c6841592Sxypron.glpk@gmx.de 		}
379c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
380c6841592Sxypron.glpk@gmx.de known_event:
381c6841592Sxypron.glpk@gmx.de 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
382c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
383c6841592Sxypron.glpk@gmx.de 	}
384c6841592Sxypron.glpk@gmx.de 
385c6841592Sxypron.glpk@gmx.de 	/* Wait for signal */
386c6841592Sxypron.glpk@gmx.de 	for (;;) {
387c6841592Sxypron.glpk@gmx.de 		for (i = 0; i < num_events; ++i) {
388c6841592Sxypron.glpk@gmx.de 			if (event[i]->signaled)
389c6841592Sxypron.glpk@gmx.de 				goto out;
390c6841592Sxypron.glpk@gmx.de 		}
391c6841592Sxypron.glpk@gmx.de 		/* Allow events to occur. */
392bee91169SAlexander Graf 		efi_timer_check();
393c6841592Sxypron.glpk@gmx.de 	}
394c6841592Sxypron.glpk@gmx.de 
395c6841592Sxypron.glpk@gmx.de out:
396c6841592Sxypron.glpk@gmx.de 	/*
397c6841592Sxypron.glpk@gmx.de 	 * Reset the signal which is passed to the caller to allow periodic
398c6841592Sxypron.glpk@gmx.de 	 * events to occur.
399c6841592Sxypron.glpk@gmx.de 	 */
400c6841592Sxypron.glpk@gmx.de 	event[i]->signaled = 0;
401c6841592Sxypron.glpk@gmx.de 	if (index)
402c6841592Sxypron.glpk@gmx.de 		*index = i;
403bee91169SAlexander Graf 
404bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
405bee91169SAlexander Graf }
406bee91169SAlexander Graf 
407c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
408bee91169SAlexander Graf {
409c6841592Sxypron.glpk@gmx.de 	int i;
410c6841592Sxypron.glpk@gmx.de 
411bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
412c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
413c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
414c6841592Sxypron.glpk@gmx.de 			continue;
415c6841592Sxypron.glpk@gmx.de 		efi_signal_event(event);
416c6841592Sxypron.glpk@gmx.de 		break;
417c6841592Sxypron.glpk@gmx.de 	}
418bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
419bee91169SAlexander Graf }
420bee91169SAlexander Graf 
4212fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
422bee91169SAlexander Graf {
423c6841592Sxypron.glpk@gmx.de 	int i;
424c6841592Sxypron.glpk@gmx.de 
425bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
426c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
427c6841592Sxypron.glpk@gmx.de 		if (event == &efi_events[i]) {
428c6841592Sxypron.glpk@gmx.de 			event->type = 0;
429c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
430c6841592Sxypron.glpk@gmx.de 			event->signaled = 0;
431bee91169SAlexander Graf 			return EFI_EXIT(EFI_SUCCESS);
432bee91169SAlexander Graf 		}
433c6841592Sxypron.glpk@gmx.de 	}
434c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
435c6841592Sxypron.glpk@gmx.de }
436bee91169SAlexander Graf 
4372fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
438bee91169SAlexander Graf {
439c6841592Sxypron.glpk@gmx.de 	int i;
440c6841592Sxypron.glpk@gmx.de 
441bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
442c6841592Sxypron.glpk@gmx.de 	efi_timer_check();
443c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
444c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
445c6841592Sxypron.glpk@gmx.de 			continue;
446c6841592Sxypron.glpk@gmx.de 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
447c6841592Sxypron.glpk@gmx.de 			break;
448c6841592Sxypron.glpk@gmx.de 		if (event->signaled)
449c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_SUCCESS);
450bee91169SAlexander Graf 		return EFI_EXIT(EFI_NOT_READY);
451bee91169SAlexander Graf 	}
452c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
453c6841592Sxypron.glpk@gmx.de }
454bee91169SAlexander Graf 
455bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
456bee91169SAlexander Graf 			efi_guid_t *protocol, int protocol_interface_type,
457bee91169SAlexander Graf 			void *protocol_interface)
458bee91169SAlexander Graf {
459e0549f8aSxypron.glpk@gmx.de 	struct list_head *lhandle;
460e0549f8aSxypron.glpk@gmx.de 	int i;
461e0549f8aSxypron.glpk@gmx.de 	efi_status_t r;
462e0549f8aSxypron.glpk@gmx.de 
463e0549f8aSxypron.glpk@gmx.de 	if (!handle || !protocol ||
464e0549f8aSxypron.glpk@gmx.de 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
465e0549f8aSxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
466e0549f8aSxypron.glpk@gmx.de 		goto out;
467bee91169SAlexander Graf 	}
468e0549f8aSxypron.glpk@gmx.de 
469e0549f8aSxypron.glpk@gmx.de 	/* Create new handle if requested. */
470e0549f8aSxypron.glpk@gmx.de 	if (!*handle) {
471e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
472e0549f8aSxypron.glpk@gmx.de 		goto out;
473e0549f8aSxypron.glpk@gmx.de 	}
474e0549f8aSxypron.glpk@gmx.de 	/* Find object. */
475e0549f8aSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
476e0549f8aSxypron.glpk@gmx.de 		struct efi_object *efiobj;
477e0549f8aSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
478e0549f8aSxypron.glpk@gmx.de 
479e0549f8aSxypron.glpk@gmx.de 		if (efiobj->handle != *handle)
480e0549f8aSxypron.glpk@gmx.de 			continue;
481e0549f8aSxypron.glpk@gmx.de 		/* Check if protocol is already installed on the handle. */
482e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
483e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
484e0549f8aSxypron.glpk@gmx.de 
485e0549f8aSxypron.glpk@gmx.de 			if (!handler->guid)
486e0549f8aSxypron.glpk@gmx.de 				continue;
487e0549f8aSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
488e0549f8aSxypron.glpk@gmx.de 				r = EFI_INVALID_PARAMETER;
489e0549f8aSxypron.glpk@gmx.de 				goto out;
490e0549f8aSxypron.glpk@gmx.de 			}
491e0549f8aSxypron.glpk@gmx.de 		}
492e0549f8aSxypron.glpk@gmx.de 		/* Install protocol in first empty slot. */
493e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
494e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
495e0549f8aSxypron.glpk@gmx.de 
496e0549f8aSxypron.glpk@gmx.de 			if (handler->guid)
497e0549f8aSxypron.glpk@gmx.de 				continue;
498e0549f8aSxypron.glpk@gmx.de 
499e0549f8aSxypron.glpk@gmx.de 			handler->guid = protocol;
500e0549f8aSxypron.glpk@gmx.de 			handler->protocol_interface = protocol_interface;
501e0549f8aSxypron.glpk@gmx.de 			r = EFI_SUCCESS;
502e0549f8aSxypron.glpk@gmx.de 			goto out;
503e0549f8aSxypron.glpk@gmx.de 		}
504e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
505e0549f8aSxypron.glpk@gmx.de 		goto out;
506e0549f8aSxypron.glpk@gmx.de 	}
507e0549f8aSxypron.glpk@gmx.de 	r = EFI_INVALID_PARAMETER;
508e0549f8aSxypron.glpk@gmx.de out:
5098bee5a3cSxypron.glpk@gmx.de 	return r;
5108bee5a3cSxypron.glpk@gmx.de }
5118bee5a3cSxypron.glpk@gmx.de 
5128bee5a3cSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
5138bee5a3cSxypron.glpk@gmx.de 			efi_guid_t *protocol, int protocol_interface_type,
5148bee5a3cSxypron.glpk@gmx.de 			void *protocol_interface)
5158bee5a3cSxypron.glpk@gmx.de {
5168bee5a3cSxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
5178bee5a3cSxypron.glpk@gmx.de 		  protocol_interface);
5188bee5a3cSxypron.glpk@gmx.de 
5198bee5a3cSxypron.glpk@gmx.de 	return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
5208bee5a3cSxypron.glpk@gmx.de 						       protocol_interface_type,
5218bee5a3cSxypron.glpk@gmx.de 						       protocol_interface));
522e0549f8aSxypron.glpk@gmx.de }
523e0549f8aSxypron.glpk@gmx.de 
524bee91169SAlexander Graf static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
525bee91169SAlexander Graf 			efi_guid_t *protocol, void *old_interface,
526bee91169SAlexander Graf 			void *new_interface)
527bee91169SAlexander Graf {
528bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
529bee91169SAlexander Graf 		  new_interface);
530bee91169SAlexander Graf 	return EFI_EXIT(EFI_ACCESS_DENIED);
531bee91169SAlexander Graf }
532bee91169SAlexander Graf 
533bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
534bee91169SAlexander Graf 			efi_guid_t *protocol, void *protocol_interface)
535bee91169SAlexander Graf {
5364b6ed0d7Sxypron.glpk@gmx.de 	struct list_head *lhandle;
5374b6ed0d7Sxypron.glpk@gmx.de 	int i;
5384b6ed0d7Sxypron.glpk@gmx.de 	efi_status_t r = EFI_NOT_FOUND;
5394b6ed0d7Sxypron.glpk@gmx.de 
5404b6ed0d7Sxypron.glpk@gmx.de 	if (!handle || !protocol) {
5414b6ed0d7Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
5424b6ed0d7Sxypron.glpk@gmx.de 		goto out;
5434b6ed0d7Sxypron.glpk@gmx.de 	}
5444b6ed0d7Sxypron.glpk@gmx.de 
5454b6ed0d7Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
5464b6ed0d7Sxypron.glpk@gmx.de 		struct efi_object *efiobj;
5474b6ed0d7Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
5484b6ed0d7Sxypron.glpk@gmx.de 
5494b6ed0d7Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
5504b6ed0d7Sxypron.glpk@gmx.de 			continue;
5514b6ed0d7Sxypron.glpk@gmx.de 
5524b6ed0d7Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
5534b6ed0d7Sxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
5544b6ed0d7Sxypron.glpk@gmx.de 			const efi_guid_t *hprotocol = handler->guid;
5554b6ed0d7Sxypron.glpk@gmx.de 
5564b6ed0d7Sxypron.glpk@gmx.de 			if (!hprotocol)
5574b6ed0d7Sxypron.glpk@gmx.de 				continue;
5584b6ed0d7Sxypron.glpk@gmx.de 			if (!guidcmp(hprotocol, protocol)) {
5594b6ed0d7Sxypron.glpk@gmx.de 				if (handler->protocol_interface) {
5604b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_ACCESS_DENIED;
5614b6ed0d7Sxypron.glpk@gmx.de 				} else {
5624b6ed0d7Sxypron.glpk@gmx.de 					handler->guid = 0;
5634b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_SUCCESS;
5644b6ed0d7Sxypron.glpk@gmx.de 				}
5654b6ed0d7Sxypron.glpk@gmx.de 				goto out;
5664b6ed0d7Sxypron.glpk@gmx.de 			}
5674b6ed0d7Sxypron.glpk@gmx.de 		}
5684b6ed0d7Sxypron.glpk@gmx.de 	}
5694b6ed0d7Sxypron.glpk@gmx.de 
5704b6ed0d7Sxypron.glpk@gmx.de out:
5713d8e1456Sxypron.glpk@gmx.de 	return r;
5723d8e1456Sxypron.glpk@gmx.de }
5733d8e1456Sxypron.glpk@gmx.de 
5743d8e1456Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
5753d8e1456Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *protocol_interface)
5763d8e1456Sxypron.glpk@gmx.de {
5773d8e1456Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
5783d8e1456Sxypron.glpk@gmx.de 
5793d8e1456Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
5803d8e1456Sxypron.glpk@gmx.de 							 protocol_interface));
581bee91169SAlexander Graf }
582bee91169SAlexander Graf 
583bee91169SAlexander Graf static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
5842fd945feSxypron.glpk@gmx.de 							struct efi_event *event,
585bee91169SAlexander Graf 							void **registration)
586bee91169SAlexander Graf {
587bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
588bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
589bee91169SAlexander Graf }
590bee91169SAlexander Graf 
591bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
592bee91169SAlexander Graf 		      efi_guid_t *protocol, void *search_key,
593bee91169SAlexander Graf 		      struct efi_object *efiobj)
594bee91169SAlexander Graf {
595bee91169SAlexander Graf 	int i;
596bee91169SAlexander Graf 
597bee91169SAlexander Graf 	switch (search_type) {
598bee91169SAlexander Graf 	case all_handles:
599bee91169SAlexander Graf 		return 0;
600bee91169SAlexander Graf 	case by_register_notify:
601bee91169SAlexander Graf 		return -1;
602bee91169SAlexander Graf 	case by_protocol:
603bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
604bee91169SAlexander Graf 			const efi_guid_t *guid = efiobj->protocols[i].guid;
605bee91169SAlexander Graf 			if (guid && !guidcmp(guid, protocol))
606bee91169SAlexander Graf 				return 0;
607bee91169SAlexander Graf 		}
608bee91169SAlexander Graf 		return -1;
609bee91169SAlexander Graf 	}
610bee91169SAlexander Graf 
611bee91169SAlexander Graf 	return -1;
612bee91169SAlexander Graf }
613bee91169SAlexander Graf 
614ebf199b9Sxypron.glpk@gmx.de static efi_status_t efi_locate_handle(
615bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
616bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
617bee91169SAlexander Graf 			unsigned long *buffer_size, efi_handle_t *buffer)
618bee91169SAlexander Graf {
619bee91169SAlexander Graf 	struct list_head *lhandle;
620bee91169SAlexander Graf 	unsigned long size = 0;
621bee91169SAlexander Graf 
622bee91169SAlexander Graf 	/* Count how much space we need */
623bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
624bee91169SAlexander Graf 		struct efi_object *efiobj;
625bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
626bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
627bee91169SAlexander Graf 			size += sizeof(void*);
628bee91169SAlexander Graf 		}
629bee91169SAlexander Graf 	}
630bee91169SAlexander Graf 
631bee91169SAlexander Graf 	if (*buffer_size < size) {
632bee91169SAlexander Graf 		*buffer_size = size;
63326329584Sxypron.glpk@gmx.de 		return EFI_BUFFER_TOO_SMALL;
634bee91169SAlexander Graf 	}
635bee91169SAlexander Graf 
636796a78cbSRob Clark 	*buffer_size = size;
637796a78cbSRob Clark 	if (size == 0)
638796a78cbSRob Clark 		return EFI_NOT_FOUND;
639796a78cbSRob Clark 
640bee91169SAlexander Graf 	/* Then fill the array */
641bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
642bee91169SAlexander Graf 		struct efi_object *efiobj;
643bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
644bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
645bee91169SAlexander Graf 			*(buffer++) = efiobj->handle;
646bee91169SAlexander Graf 		}
647bee91169SAlexander Graf 	}
648bee91169SAlexander Graf 
64926329584Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
65026329584Sxypron.glpk@gmx.de }
65126329584Sxypron.glpk@gmx.de 
65226329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
65326329584Sxypron.glpk@gmx.de 			enum efi_locate_search_type search_type,
65426329584Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *search_key,
65526329584Sxypron.glpk@gmx.de 			unsigned long *buffer_size, efi_handle_t *buffer)
65626329584Sxypron.glpk@gmx.de {
65726329584Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
65826329584Sxypron.glpk@gmx.de 		  buffer_size, buffer);
65926329584Sxypron.glpk@gmx.de 
66026329584Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
66126329584Sxypron.glpk@gmx.de 			buffer_size, buffer));
662bee91169SAlexander Graf }
663bee91169SAlexander Graf 
664bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
665bee91169SAlexander Graf 			struct efi_device_path **device_path,
666bee91169SAlexander Graf 			efi_handle_t *device)
667bee91169SAlexander Graf {
668bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
669bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
670bee91169SAlexander Graf }
671bee91169SAlexander Graf 
672d98cdf6aSAlexander Graf /* Collapses configuration table entries, removing index i */
673d98cdf6aSAlexander Graf static void efi_remove_configuration_table(int i)
674d98cdf6aSAlexander Graf {
675d98cdf6aSAlexander Graf 	struct efi_configuration_table *this = &efi_conf_table[i];
676d98cdf6aSAlexander Graf 	struct efi_configuration_table *next = &efi_conf_table[i+1];
677d98cdf6aSAlexander Graf 	struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
678d98cdf6aSAlexander Graf 
679d98cdf6aSAlexander Graf 	memmove(this, next, (ulong)end - (ulong)next);
680d98cdf6aSAlexander Graf 	systab.nr_tables--;
681d98cdf6aSAlexander Graf }
682d98cdf6aSAlexander Graf 
683488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
684bee91169SAlexander Graf {
685bee91169SAlexander Graf 	int i;
686bee91169SAlexander Graf 
687bee91169SAlexander Graf 	/* Check for guid override */
688bee91169SAlexander Graf 	for (i = 0; i < systab.nr_tables; i++) {
689bee91169SAlexander Graf 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
690d98cdf6aSAlexander Graf 			if (table)
691bee91169SAlexander Graf 				efi_conf_table[i].table = table;
692d98cdf6aSAlexander Graf 			else
693d98cdf6aSAlexander Graf 				efi_remove_configuration_table(i);
694488bf12dSAlexander Graf 			return EFI_SUCCESS;
695bee91169SAlexander Graf 		}
696bee91169SAlexander Graf 	}
697bee91169SAlexander Graf 
698d98cdf6aSAlexander Graf 	if (!table)
699d98cdf6aSAlexander Graf 		return EFI_NOT_FOUND;
700d98cdf6aSAlexander Graf 
701bee91169SAlexander Graf 	/* No override, check for overflow */
702bee91169SAlexander Graf 	if (i >= ARRAY_SIZE(efi_conf_table))
703488bf12dSAlexander Graf 		return EFI_OUT_OF_RESOURCES;
704bee91169SAlexander Graf 
705bee91169SAlexander Graf 	/* Add a new entry */
706bee91169SAlexander Graf 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
707bee91169SAlexander Graf 	efi_conf_table[i].table = table;
708aba5e919SAlexander Graf 	systab.nr_tables = i + 1;
709bee91169SAlexander Graf 
710488bf12dSAlexander Graf 	return EFI_SUCCESS;
711488bf12dSAlexander Graf }
712488bf12dSAlexander Graf 
713488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
714488bf12dSAlexander Graf 							       void *table)
715488bf12dSAlexander Graf {
716488bf12dSAlexander Graf 	EFI_ENTRY("%p, %p", guid, table);
717488bf12dSAlexander Graf 	return EFI_EXIT(efi_install_configuration_table(guid, table));
718bee91169SAlexander Graf }
719bee91169SAlexander Graf 
720bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
721bee91169SAlexander Graf 					  efi_handle_t parent_image,
722bee91169SAlexander Graf 					  struct efi_device_path *file_path,
723bee91169SAlexander Graf 					  void *source_buffer,
724bee91169SAlexander Graf 					  unsigned long source_size,
725bee91169SAlexander Graf 					  efi_handle_t *image_handle)
726bee91169SAlexander Graf {
727bee91169SAlexander Graf 	static struct efi_object loaded_image_info_obj = {
728bee91169SAlexander Graf 		.protocols = {
729bee91169SAlexander Graf 			{
730bee91169SAlexander Graf 				.guid = &efi_guid_loaded_image,
731bee91169SAlexander Graf 			},
732bee91169SAlexander Graf 		},
733bee91169SAlexander Graf 	};
734bee91169SAlexander Graf 	struct efi_loaded_image *info;
735bee91169SAlexander Graf 	struct efi_object *obj;
736bee91169SAlexander Graf 
737bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
738bee91169SAlexander Graf 		  file_path, source_buffer, source_size, image_handle);
739bee91169SAlexander Graf 	info = malloc(sizeof(*info));
740b5349f74Sxypron.glpk@gmx.de 	loaded_image_info_obj.protocols[0].protocol_interface = info;
741bee91169SAlexander Graf 	obj = malloc(sizeof(loaded_image_info_obj));
742bee91169SAlexander Graf 	memset(info, 0, sizeof(*info));
743bee91169SAlexander Graf 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
744bee91169SAlexander Graf 	obj->handle = info;
745bee91169SAlexander Graf 	info->file_path = file_path;
746bee91169SAlexander Graf 	info->reserved = efi_load_pe(source_buffer, info);
747bee91169SAlexander Graf 	if (!info->reserved) {
748bee91169SAlexander Graf 		free(info);
749bee91169SAlexander Graf 		free(obj);
750bee91169SAlexander Graf 		return EFI_EXIT(EFI_UNSUPPORTED);
751bee91169SAlexander Graf 	}
752bee91169SAlexander Graf 
753bee91169SAlexander Graf 	*image_handle = info;
754bee91169SAlexander Graf 	list_add_tail(&obj->link, &efi_obj_list);
755bee91169SAlexander Graf 
756bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
757bee91169SAlexander Graf }
758bee91169SAlexander Graf 
759bee91169SAlexander Graf static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
760bee91169SAlexander Graf 					   unsigned long *exit_data_size,
761bee91169SAlexander Graf 					   s16 **exit_data)
762bee91169SAlexander Graf {
763bee91169SAlexander Graf 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
764bee91169SAlexander Graf 	struct efi_loaded_image *info = image_handle;
765bee91169SAlexander Graf 
766bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
767bee91169SAlexander Graf 	entry = info->reserved;
768bee91169SAlexander Graf 
769bee91169SAlexander Graf 	efi_is_direct_boot = false;
770bee91169SAlexander Graf 
771bee91169SAlexander Graf 	/* call the image! */
772a86aeaf2SAlexander Graf 	if (setjmp(&info->exit_jmp)) {
773a86aeaf2SAlexander Graf 		/* We returned from the child image */
774a86aeaf2SAlexander Graf 		return EFI_EXIT(info->exit_status);
775a86aeaf2SAlexander Graf 	}
776a86aeaf2SAlexander Graf 
777af65db85SRob Clark 	__efi_nesting_dec();
778c160d2f5SRob Clark 	__efi_exit_check();
779bee91169SAlexander Graf 	entry(image_handle, &systab);
780c160d2f5SRob Clark 	__efi_entry_check();
781af65db85SRob Clark 	__efi_nesting_inc();
782bee91169SAlexander Graf 
783bee91169SAlexander Graf 	/* Should usually never get here */
784bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
785bee91169SAlexander Graf }
786bee91169SAlexander Graf 
787a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
788a86aeaf2SAlexander Graf 			efi_status_t exit_status, unsigned long exit_data_size,
789a86aeaf2SAlexander Graf 			int16_t *exit_data)
790bee91169SAlexander Graf {
791a86aeaf2SAlexander Graf 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
792a86aeaf2SAlexander Graf 
793bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
794bee91169SAlexander Graf 		  exit_data_size, exit_data);
795a86aeaf2SAlexander Graf 
796a86aeaf2SAlexander Graf 	loaded_image_info->exit_status = exit_status;
797692fcdd8SAlexander Graf 	longjmp(&loaded_image_info->exit_jmp, 1);
798a86aeaf2SAlexander Graf 
799a86aeaf2SAlexander Graf 	panic("EFI application exited");
800bee91169SAlexander Graf }
801bee91169SAlexander Graf 
802bee91169SAlexander Graf static struct efi_object *efi_search_obj(void *handle)
803bee91169SAlexander Graf {
804bee91169SAlexander Graf 	struct list_head *lhandle;
805bee91169SAlexander Graf 
806bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
807bee91169SAlexander Graf 		struct efi_object *efiobj;
808bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
809bee91169SAlexander Graf 		if (efiobj->handle == handle)
810bee91169SAlexander Graf 			return efiobj;
811bee91169SAlexander Graf 	}
812bee91169SAlexander Graf 
813bee91169SAlexander Graf 	return NULL;
814bee91169SAlexander Graf }
815bee91169SAlexander Graf 
816bee91169SAlexander Graf static efi_status_t EFIAPI efi_unload_image(void *image_handle)
817bee91169SAlexander Graf {
818bee91169SAlexander Graf 	struct efi_object *efiobj;
819bee91169SAlexander Graf 
820bee91169SAlexander Graf 	EFI_ENTRY("%p", image_handle);
821bee91169SAlexander Graf 	efiobj = efi_search_obj(image_handle);
822bee91169SAlexander Graf 	if (efiobj)
823bee91169SAlexander Graf 		list_del(&efiobj->link);
824bee91169SAlexander Graf 
825bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
826bee91169SAlexander Graf }
827bee91169SAlexander Graf 
828bee91169SAlexander Graf static void efi_exit_caches(void)
829bee91169SAlexander Graf {
830bee91169SAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
831bee91169SAlexander Graf 	/*
832bee91169SAlexander Graf 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
833bee91169SAlexander Graf 	 * a zImage, but does not know of all cache layers. Give it a hand.
834bee91169SAlexander Graf 	 */
835bee91169SAlexander Graf 	if (efi_is_direct_boot)
836bee91169SAlexander Graf 		cleanup_before_linux();
837bee91169SAlexander Graf #endif
838bee91169SAlexander Graf }
839bee91169SAlexander Graf 
840bee91169SAlexander Graf static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
841bee91169SAlexander Graf 						  unsigned long map_key)
842bee91169SAlexander Graf {
843bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", image_handle, map_key);
844bee91169SAlexander Graf 
845b7b8410aSAlexander Graf 	board_quiesce_devices();
846b7b8410aSAlexander Graf 
847bee91169SAlexander Graf 	/* Fix up caches for EFI payloads if necessary */
848bee91169SAlexander Graf 	efi_exit_caches();
849bee91169SAlexander Graf 
850bee91169SAlexander Graf 	/* This stops all lingering devices */
851bee91169SAlexander Graf 	bootm_disable_interrupts();
852bee91169SAlexander Graf 
853bee91169SAlexander Graf 	/* Give the payload some time to boot */
854bee91169SAlexander Graf 	WATCHDOG_RESET();
855bee91169SAlexander Graf 
856bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
857bee91169SAlexander Graf }
858bee91169SAlexander Graf 
859bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
860bee91169SAlexander Graf {
861bee91169SAlexander Graf 	static uint64_t mono = 0;
862bee91169SAlexander Graf 	EFI_ENTRY("%p", count);
863bee91169SAlexander Graf 	*count = mono++;
864bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
865bee91169SAlexander Graf }
866bee91169SAlexander Graf 
867bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
868bee91169SAlexander Graf {
869bee91169SAlexander Graf 	EFI_ENTRY("%ld", microseconds);
870bee91169SAlexander Graf 	udelay(microseconds);
871bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
872bee91169SAlexander Graf }
873bee91169SAlexander Graf 
874bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
875bee91169SAlexander Graf 						  uint64_t watchdog_code,
876bee91169SAlexander Graf 						  unsigned long data_size,
877bee91169SAlexander Graf 						  uint16_t *watchdog_data)
878bee91169SAlexander Graf {
879bee91169SAlexander Graf 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
880bee91169SAlexander Graf 		  data_size, watchdog_data);
881b5104821SRob Clark 	return efi_unsupported(__func__);
882bee91169SAlexander Graf }
883bee91169SAlexander Graf 
884bee91169SAlexander Graf static efi_status_t EFIAPI efi_connect_controller(
885bee91169SAlexander Graf 			efi_handle_t controller_handle,
886bee91169SAlexander Graf 			efi_handle_t *driver_image_handle,
887bee91169SAlexander Graf 			struct efi_device_path *remain_device_path,
888bee91169SAlexander Graf 			bool recursive)
889bee91169SAlexander Graf {
890bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
891bee91169SAlexander Graf 		  remain_device_path, recursive);
892bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
893bee91169SAlexander Graf }
894bee91169SAlexander Graf 
895bee91169SAlexander Graf static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
896bee91169SAlexander Graf 						     void *driver_image_handle,
897bee91169SAlexander Graf 						     void *child_handle)
898bee91169SAlexander Graf {
899bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
900bee91169SAlexander Graf 		  child_handle);
901bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
902bee91169SAlexander Graf }
903bee91169SAlexander Graf 
904bee91169SAlexander Graf static efi_status_t EFIAPI efi_close_protocol(void *handle,
905bee91169SAlexander Graf 					      efi_guid_t *protocol,
906bee91169SAlexander Graf 					      void *agent_handle,
907bee91169SAlexander Graf 					      void *controller_handle)
908bee91169SAlexander Graf {
909bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
910bee91169SAlexander Graf 		  controller_handle);
911bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
912bee91169SAlexander Graf }
913bee91169SAlexander Graf 
914bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
915bee91169SAlexander Graf 			efi_guid_t *protocol,
916bee91169SAlexander Graf 			struct efi_open_protocol_info_entry **entry_buffer,
917bee91169SAlexander Graf 			unsigned long *entry_count)
918bee91169SAlexander Graf {
919bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
920bee91169SAlexander Graf 		  entry_count);
921bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
922bee91169SAlexander Graf }
923bee91169SAlexander Graf 
924bee91169SAlexander Graf static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
925bee91169SAlexander Graf 			efi_guid_t ***protocol_buffer,
926bee91169SAlexander Graf 			unsigned long *protocol_buffer_count)
927bee91169SAlexander Graf {
928c0ebfc86Sxypron.glpk@gmx.de 	unsigned long buffer_size;
929c0ebfc86Sxypron.glpk@gmx.de 	struct efi_object *efiobj;
930c0ebfc86Sxypron.glpk@gmx.de 	unsigned long i, j;
931c0ebfc86Sxypron.glpk@gmx.de 	struct list_head *lhandle;
932c0ebfc86Sxypron.glpk@gmx.de 	efi_status_t r;
933c0ebfc86Sxypron.glpk@gmx.de 
934bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
935bee91169SAlexander Graf 		  protocol_buffer_count);
936c0ebfc86Sxypron.glpk@gmx.de 
937c0ebfc86Sxypron.glpk@gmx.de 	if (!handle || !protocol_buffer || !protocol_buffer_count)
938c0ebfc86Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
939c0ebfc86Sxypron.glpk@gmx.de 
940c0ebfc86Sxypron.glpk@gmx.de 	*protocol_buffer = NULL;
941661c8327SRob Clark 	*protocol_buffer_count = 0;
942c0ebfc86Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
943c0ebfc86Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
944c0ebfc86Sxypron.glpk@gmx.de 
945c0ebfc86Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
946c0ebfc86Sxypron.glpk@gmx.de 			continue;
947c0ebfc86Sxypron.glpk@gmx.de 
948c0ebfc86Sxypron.glpk@gmx.de 		/* Count protocols */
949c0ebfc86Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
950c0ebfc86Sxypron.glpk@gmx.de 			if (efiobj->protocols[i].guid)
951c0ebfc86Sxypron.glpk@gmx.de 				++*protocol_buffer_count;
952c0ebfc86Sxypron.glpk@gmx.de 		}
953c0ebfc86Sxypron.glpk@gmx.de 		/* Copy guids */
954c0ebfc86Sxypron.glpk@gmx.de 		if (*protocol_buffer_count) {
955c0ebfc86Sxypron.glpk@gmx.de 			buffer_size = sizeof(efi_guid_t *) *
956c0ebfc86Sxypron.glpk@gmx.de 					*protocol_buffer_count;
957c0ebfc86Sxypron.glpk@gmx.de 			r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
958c0ebfc86Sxypron.glpk@gmx.de 					      buffer_size,
959c0ebfc86Sxypron.glpk@gmx.de 					      (void **)protocol_buffer);
960c0ebfc86Sxypron.glpk@gmx.de 			if (r != EFI_SUCCESS)
961c0ebfc86Sxypron.glpk@gmx.de 				return EFI_EXIT(r);
962c0ebfc86Sxypron.glpk@gmx.de 			j = 0;
963c0ebfc86Sxypron.glpk@gmx.de 			for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
964c0ebfc86Sxypron.glpk@gmx.de 				if (efiobj->protocols[i].guid) {
965c0ebfc86Sxypron.glpk@gmx.de 					(*protocol_buffer)[j] = (void *)
966c0ebfc86Sxypron.glpk@gmx.de 						efiobj->protocols[i].guid;
967c0ebfc86Sxypron.glpk@gmx.de 					++j;
968c0ebfc86Sxypron.glpk@gmx.de 				}
969c0ebfc86Sxypron.glpk@gmx.de 			}
970c0ebfc86Sxypron.glpk@gmx.de 		}
971c0ebfc86Sxypron.glpk@gmx.de 		break;
972c0ebfc86Sxypron.glpk@gmx.de 	}
973c0ebfc86Sxypron.glpk@gmx.de 
974c0ebfc86Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_SUCCESS);
975bee91169SAlexander Graf }
976bee91169SAlexander Graf 
977bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
978bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
979bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
980bee91169SAlexander Graf 			unsigned long *no_handles, efi_handle_t **buffer)
981bee91169SAlexander Graf {
982c2e703f9Sxypron.glpk@gmx.de 	efi_status_t r;
983c2e703f9Sxypron.glpk@gmx.de 	unsigned long buffer_size = 0;
984c2e703f9Sxypron.glpk@gmx.de 
985bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
986bee91169SAlexander Graf 		  no_handles, buffer);
987c2e703f9Sxypron.glpk@gmx.de 
988c2e703f9Sxypron.glpk@gmx.de 	if (!no_handles || !buffer) {
989c2e703f9Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
990c2e703f9Sxypron.glpk@gmx.de 		goto out;
991c2e703f9Sxypron.glpk@gmx.de 	}
992c2e703f9Sxypron.glpk@gmx.de 	*no_handles = 0;
993c2e703f9Sxypron.glpk@gmx.de 	*buffer = NULL;
994c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
995c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
996c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_BUFFER_TOO_SMALL)
997c2e703f9Sxypron.glpk@gmx.de 		goto out;
998c2e703f9Sxypron.glpk@gmx.de 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
999c2e703f9Sxypron.glpk@gmx.de 			      (void **)buffer);
1000c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
1001c2e703f9Sxypron.glpk@gmx.de 		goto out;
1002c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1003c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
1004c2e703f9Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
1005c2e703f9Sxypron.glpk@gmx.de 		*no_handles = buffer_size / sizeof(void *);
1006c2e703f9Sxypron.glpk@gmx.de out:
1007c2e703f9Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
1008bee91169SAlexander Graf }
1009bee91169SAlexander Graf 
1010bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1011bee91169SAlexander Graf 					       void *registration,
1012bee91169SAlexander Graf 					       void **protocol_interface)
1013bee91169SAlexander Graf {
101488adae5eSxypron.glpk@gmx.de 	struct list_head *lhandle;
1015bee91169SAlexander Graf 	int i;
1016bee91169SAlexander Graf 
1017bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
101888adae5eSxypron.glpk@gmx.de 
101988adae5eSxypron.glpk@gmx.de 	if (!protocol || !protocol_interface)
102088adae5eSxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
102188adae5eSxypron.glpk@gmx.de 
102288adae5eSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
102388adae5eSxypron.glpk@gmx.de 		struct efi_object *efiobj;
102488adae5eSxypron.glpk@gmx.de 
102588adae5eSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
102688adae5eSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
102788adae5eSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
102888adae5eSxypron.glpk@gmx.de 
102988adae5eSxypron.glpk@gmx.de 			if (!handler->guid)
103088adae5eSxypron.glpk@gmx.de 				continue;
103188adae5eSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
103288adae5eSxypron.glpk@gmx.de 				*protocol_interface =
103388adae5eSxypron.glpk@gmx.de 					handler->protocol_interface;
1034bee91169SAlexander Graf 				return EFI_EXIT(EFI_SUCCESS);
1035bee91169SAlexander Graf 			}
1036bee91169SAlexander Graf 		}
103788adae5eSxypron.glpk@gmx.de 	}
103888adae5eSxypron.glpk@gmx.de 	*protocol_interface = NULL;
1039bee91169SAlexander Graf 
1040bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
1041bee91169SAlexander Graf }
1042bee91169SAlexander Graf 
1043bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1044bee91169SAlexander Graf 			void **handle, ...)
1045bee91169SAlexander Graf {
1046bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
104758b83586Sxypron.glpk@gmx.de 
104858b83586Sxypron.glpk@gmx.de 	va_list argptr;
104958b83586Sxypron.glpk@gmx.de 	efi_guid_t *protocol;
105058b83586Sxypron.glpk@gmx.de 	void *protocol_interface;
105158b83586Sxypron.glpk@gmx.de 	efi_status_t r = EFI_SUCCESS;
105258b83586Sxypron.glpk@gmx.de 	int i = 0;
105358b83586Sxypron.glpk@gmx.de 
105458b83586Sxypron.glpk@gmx.de 	if (!handle)
105558b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
105658b83586Sxypron.glpk@gmx.de 
105758b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
105858b83586Sxypron.glpk@gmx.de 	for (;;) {
105958b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
106058b83586Sxypron.glpk@gmx.de 		if (!protocol)
106158b83586Sxypron.glpk@gmx.de 			break;
106258b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
106358b83586Sxypron.glpk@gmx.de 		r = efi_install_protocol_interface(handle, protocol,
106458b83586Sxypron.glpk@gmx.de 						   EFI_NATIVE_INTERFACE,
106558b83586Sxypron.glpk@gmx.de 						   protocol_interface);
106658b83586Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
106758b83586Sxypron.glpk@gmx.de 			break;
106858b83586Sxypron.glpk@gmx.de 		i++;
106958b83586Sxypron.glpk@gmx.de 	}
107058b83586Sxypron.glpk@gmx.de 	va_end(argptr);
107158b83586Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
107258b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(r);
107358b83586Sxypron.glpk@gmx.de 
107458b83586Sxypron.glpk@gmx.de 	/* If an error occured undo all changes. */
107558b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
107658b83586Sxypron.glpk@gmx.de 	for (; i; --i) {
107758b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
107858b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
107958b83586Sxypron.glpk@gmx.de 		efi_uninstall_protocol_interface(handle, protocol,
108058b83586Sxypron.glpk@gmx.de 						 protocol_interface);
108158b83586Sxypron.glpk@gmx.de 	}
108258b83586Sxypron.glpk@gmx.de 	va_end(argptr);
108358b83586Sxypron.glpk@gmx.de 
108458b83586Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
1085bee91169SAlexander Graf }
1086bee91169SAlexander Graf 
1087bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1088bee91169SAlexander Graf 			void *handle, ...)
1089bee91169SAlexander Graf {
1090bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
1091bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
1092bee91169SAlexander Graf }
1093bee91169SAlexander Graf 
1094bee91169SAlexander Graf static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1095bee91169SAlexander Graf 					       unsigned long data_size,
1096bee91169SAlexander Graf 					       uint32_t *crc32_p)
1097bee91169SAlexander Graf {
1098bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", data, data_size);
1099bee91169SAlexander Graf 	*crc32_p = crc32(0, data, data_size);
1100bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
1101bee91169SAlexander Graf }
1102bee91169SAlexander Graf 
1103bee91169SAlexander Graf static void EFIAPI efi_copy_mem(void *destination, void *source,
1104bee91169SAlexander Graf 				unsigned long length)
1105bee91169SAlexander Graf {
1106bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
1107bee91169SAlexander Graf 	memcpy(destination, source, length);
1108bee91169SAlexander Graf }
1109bee91169SAlexander Graf 
1110bee91169SAlexander Graf static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1111bee91169SAlexander Graf {
1112bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1113bee91169SAlexander Graf 	memset(buffer, value, size);
1114bee91169SAlexander Graf }
1115bee91169SAlexander Graf 
1116bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol(
1117bee91169SAlexander Graf 			void *handle, efi_guid_t *protocol,
1118bee91169SAlexander Graf 			void **protocol_interface, void *agent_handle,
1119bee91169SAlexander Graf 			void *controller_handle, uint32_t attributes)
1120bee91169SAlexander Graf {
1121bee91169SAlexander Graf 	struct list_head *lhandle;
1122bee91169SAlexander Graf 	int i;
112369baec67Sxypron.glpk@gmx.de 	efi_status_t r = EFI_INVALID_PARAMETER;
1124bee91169SAlexander Graf 
1125bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1126bee91169SAlexander Graf 		  protocol_interface, agent_handle, controller_handle,
1127bee91169SAlexander Graf 		  attributes);
1128b5349f74Sxypron.glpk@gmx.de 
112969baec67Sxypron.glpk@gmx.de 	if (!handle || !protocol ||
113069baec67Sxypron.glpk@gmx.de 	    (!protocol_interface && attributes !=
113169baec67Sxypron.glpk@gmx.de 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
113269baec67Sxypron.glpk@gmx.de 		goto out;
113369baec67Sxypron.glpk@gmx.de 	}
113469baec67Sxypron.glpk@gmx.de 
113569baec67Sxypron.glpk@gmx.de 	switch (attributes) {
113669baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
113769baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
113869baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
113969baec67Sxypron.glpk@gmx.de 		break;
114069baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
114169baec67Sxypron.glpk@gmx.de 		if (controller_handle == handle)
114269baec67Sxypron.glpk@gmx.de 			goto out;
114369baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
114469baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
114569baec67Sxypron.glpk@gmx.de 		if (controller_handle == NULL)
114669baec67Sxypron.glpk@gmx.de 			goto out;
114769baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
114869baec67Sxypron.glpk@gmx.de 		if (agent_handle == NULL)
114969baec67Sxypron.glpk@gmx.de 			goto out;
115069baec67Sxypron.glpk@gmx.de 		break;
115169baec67Sxypron.glpk@gmx.de 	default:
1152b5349f74Sxypron.glpk@gmx.de 		goto out;
1153b5349f74Sxypron.glpk@gmx.de 	}
1154b5349f74Sxypron.glpk@gmx.de 
1155bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
1156bee91169SAlexander Graf 		struct efi_object *efiobj;
1157bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
1158bee91169SAlexander Graf 
1159bee91169SAlexander Graf 		if (efiobj->handle != handle)
1160bee91169SAlexander Graf 			continue;
1161bee91169SAlexander Graf 
1162bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1163bee91169SAlexander Graf 			struct efi_handler *handler = &efiobj->protocols[i];
1164bee91169SAlexander Graf 			const efi_guid_t *hprotocol = handler->guid;
1165bee91169SAlexander Graf 			if (!hprotocol)
11664b6ed0d7Sxypron.glpk@gmx.de 				continue;
1167bee91169SAlexander Graf 			if (!guidcmp(hprotocol, protocol)) {
1168b5349f74Sxypron.glpk@gmx.de 				if (attributes !=
1169b5349f74Sxypron.glpk@gmx.de 				    EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1170b5349f74Sxypron.glpk@gmx.de 					*protocol_interface =
1171b5349f74Sxypron.glpk@gmx.de 						handler->protocol_interface;
1172b5349f74Sxypron.glpk@gmx.de 				}
1173b5349f74Sxypron.glpk@gmx.de 				r = EFI_SUCCESS;
1174bee91169SAlexander Graf 				goto out;
1175bee91169SAlexander Graf 			}
1176bee91169SAlexander Graf 		}
117769baec67Sxypron.glpk@gmx.de 		goto unsupported;
1178bee91169SAlexander Graf 	}
1179bee91169SAlexander Graf 
118069baec67Sxypron.glpk@gmx.de unsupported:
118169baec67Sxypron.glpk@gmx.de 	r = EFI_UNSUPPORTED;
1182bee91169SAlexander Graf out:
1183bee91169SAlexander Graf 	return EFI_EXIT(r);
1184bee91169SAlexander Graf }
1185bee91169SAlexander Graf 
1186bee91169SAlexander Graf static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1187bee91169SAlexander Graf 					       efi_guid_t *protocol,
1188bee91169SAlexander Graf 					       void **protocol_interface)
1189bee91169SAlexander Graf {
11908e1d329fSxypron.glpk@gmx.de 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
11918e1d329fSxypron.glpk@gmx.de 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1192bee91169SAlexander Graf }
1193bee91169SAlexander Graf 
1194bee91169SAlexander Graf static const struct efi_boot_services efi_boot_services = {
1195bee91169SAlexander Graf 	.hdr = {
1196bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1197bee91169SAlexander Graf 	},
1198bee91169SAlexander Graf 	.raise_tpl = efi_raise_tpl,
1199bee91169SAlexander Graf 	.restore_tpl = efi_restore_tpl,
1200bee91169SAlexander Graf 	.allocate_pages = efi_allocate_pages_ext,
1201bee91169SAlexander Graf 	.free_pages = efi_free_pages_ext,
1202bee91169SAlexander Graf 	.get_memory_map = efi_get_memory_map_ext,
1203ead1274bSStefan Brüns 	.allocate_pool = efi_allocate_pool_ext,
120442417bc8SStefan Brüns 	.free_pool = efi_free_pool_ext,
120549deb455Sxypron.glpk@gmx.de 	.create_event = efi_create_event_ext,
1206bfc72462Sxypron.glpk@gmx.de 	.set_timer = efi_set_timer_ext,
1207bee91169SAlexander Graf 	.wait_for_event = efi_wait_for_event,
1208c6841592Sxypron.glpk@gmx.de 	.signal_event = efi_signal_event_ext,
1209bee91169SAlexander Graf 	.close_event = efi_close_event,
1210bee91169SAlexander Graf 	.check_event = efi_check_event,
12118bee5a3cSxypron.glpk@gmx.de 	.install_protocol_interface = efi_install_protocol_interface_ext,
1212bee91169SAlexander Graf 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
12133d8e1456Sxypron.glpk@gmx.de 	.uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1214bee91169SAlexander Graf 	.handle_protocol = efi_handle_protocol,
1215bee91169SAlexander Graf 	.reserved = NULL,
1216bee91169SAlexander Graf 	.register_protocol_notify = efi_register_protocol_notify,
121726329584Sxypron.glpk@gmx.de 	.locate_handle = efi_locate_handle_ext,
1218bee91169SAlexander Graf 	.locate_device_path = efi_locate_device_path,
1219488bf12dSAlexander Graf 	.install_configuration_table = efi_install_configuration_table_ext,
1220bee91169SAlexander Graf 	.load_image = efi_load_image,
1221bee91169SAlexander Graf 	.start_image = efi_start_image,
1222a86aeaf2SAlexander Graf 	.exit = efi_exit,
1223bee91169SAlexander Graf 	.unload_image = efi_unload_image,
1224bee91169SAlexander Graf 	.exit_boot_services = efi_exit_boot_services,
1225bee91169SAlexander Graf 	.get_next_monotonic_count = efi_get_next_monotonic_count,
1226bee91169SAlexander Graf 	.stall = efi_stall,
1227bee91169SAlexander Graf 	.set_watchdog_timer = efi_set_watchdog_timer,
1228bee91169SAlexander Graf 	.connect_controller = efi_connect_controller,
1229bee91169SAlexander Graf 	.disconnect_controller = efi_disconnect_controller,
1230bee91169SAlexander Graf 	.open_protocol = efi_open_protocol,
1231bee91169SAlexander Graf 	.close_protocol = efi_close_protocol,
1232bee91169SAlexander Graf 	.open_protocol_information = efi_open_protocol_information,
1233bee91169SAlexander Graf 	.protocols_per_handle = efi_protocols_per_handle,
1234bee91169SAlexander Graf 	.locate_handle_buffer = efi_locate_handle_buffer,
1235bee91169SAlexander Graf 	.locate_protocol = efi_locate_protocol,
1236bee91169SAlexander Graf 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1237bee91169SAlexander Graf 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1238bee91169SAlexander Graf 	.calculate_crc32 = efi_calculate_crc32,
1239bee91169SAlexander Graf 	.copy_mem = efi_copy_mem,
1240bee91169SAlexander Graf 	.set_mem = efi_set_mem,
1241bee91169SAlexander Graf };
1242bee91169SAlexander Graf 
1243bee91169SAlexander Graf 
12443c63db9cSAlexander Graf static uint16_t __efi_runtime_data firmware_vendor[] =
1245bee91169SAlexander Graf 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
1246bee91169SAlexander Graf 
12473c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
1248bee91169SAlexander Graf 	.hdr = {
1249bee91169SAlexander Graf 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
1250bee91169SAlexander Graf 		.revision = 0x20005, /* 2.5 */
1251bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1252bee91169SAlexander Graf 	},
1253bee91169SAlexander Graf 	.fw_vendor = (long)firmware_vendor,
1254bee91169SAlexander Graf 	.con_in = (void*)&efi_con_in,
1255bee91169SAlexander Graf 	.con_out = (void*)&efi_con_out,
1256bee91169SAlexander Graf 	.std_err = (void*)&efi_con_out,
1257bee91169SAlexander Graf 	.runtime = (void*)&efi_runtime_services,
1258bee91169SAlexander Graf 	.boottime = (void*)&efi_boot_services,
1259bee91169SAlexander Graf 	.nr_tables = 0,
1260bee91169SAlexander Graf 	.tables = (void*)efi_conf_table,
1261bee91169SAlexander Graf };
1262