xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_boottime.c (revision c160d2f5ec9298d545a6e0fab0a68cc1a3e93759)
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 
52*c160d2f5SRob Clark static int entry_count;
53*c160d2f5SRob Clark 
54*c160d2f5SRob Clark /* Called on every callback entry */
55*c160d2f5SRob Clark int __efi_entry_check(void)
56*c160d2f5SRob Clark {
57*c160d2f5SRob Clark 	int ret = entry_count++ == 0;
58*c160d2f5SRob Clark #ifdef CONFIG_ARM
59*c160d2f5SRob Clark 	assert(efi_gd);
60*c160d2f5SRob Clark 	app_gd = gd;
61*c160d2f5SRob Clark 	gd = efi_gd;
62*c160d2f5SRob Clark #endif
63*c160d2f5SRob Clark 	return ret;
64*c160d2f5SRob Clark }
65*c160d2f5SRob Clark 
66*c160d2f5SRob Clark /* Called on every callback exit */
67*c160d2f5SRob Clark int __efi_exit_check(void)
68*c160d2f5SRob Clark {
69*c160d2f5SRob Clark 	int ret = --entry_count == 0;
70*c160d2f5SRob Clark #ifdef CONFIG_ARM
71*c160d2f5SRob Clark 	gd = app_gd;
72*c160d2f5SRob Clark #endif
73*c160d2f5SRob Clark 	return ret;
74*c160d2f5SRob Clark }
75*c160d2f5SRob Clark 
76bee91169SAlexander Graf /* Called from do_bootefi_exec() */
77bee91169SAlexander Graf void efi_save_gd(void)
78bee91169SAlexander Graf {
7965e4c0b1SSimon Glass #ifdef CONFIG_ARM
80bee91169SAlexander Graf 	efi_gd = gd;
8165e4c0b1SSimon Glass #endif
82bee91169SAlexander Graf }
83bee91169SAlexander Graf 
84*c160d2f5SRob Clark /*
85*c160d2f5SRob Clark  * Special case handler for error/abort that just forces things back
86*c160d2f5SRob Clark  * to u-boot world so we can dump out an abort msg, without any care
87*c160d2f5SRob Clark  * about returning back to UEFI world.
88*c160d2f5SRob Clark  */
89bee91169SAlexander Graf void efi_restore_gd(void)
90bee91169SAlexander Graf {
9165e4c0b1SSimon Glass #ifdef CONFIG_ARM
92bee91169SAlexander Graf 	/* Only restore if we're already in EFI context */
93bee91169SAlexander Graf 	if (!efi_gd)
94bee91169SAlexander Graf 		return;
95bee91169SAlexander Graf 	gd = efi_gd;
9665e4c0b1SSimon Glass #endif
97bee91169SAlexander Graf }
98bee91169SAlexander Graf 
998787b02eSxypron.glpk@gmx.de /* Low 32 bit */
1008787b02eSxypron.glpk@gmx.de #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
1018787b02eSxypron.glpk@gmx.de /* High 32 bit */
1028787b02eSxypron.glpk@gmx.de #define EFI_HIGH32(a) (a >> 32)
1038787b02eSxypron.glpk@gmx.de 
1048787b02eSxypron.glpk@gmx.de /*
1058787b02eSxypron.glpk@gmx.de  * 64bit division by 10 implemented as multiplication by 1 / 10
1068787b02eSxypron.glpk@gmx.de  *
1078787b02eSxypron.glpk@gmx.de  * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
1088787b02eSxypron.glpk@gmx.de  */
1098787b02eSxypron.glpk@gmx.de #define EFI_TENTH 0x199999999999999A
1108787b02eSxypron.glpk@gmx.de static u64 efi_div10(u64 a)
1118787b02eSxypron.glpk@gmx.de {
1128787b02eSxypron.glpk@gmx.de 	u64 prod;
1138787b02eSxypron.glpk@gmx.de 	u64 rem;
1148787b02eSxypron.glpk@gmx.de 	u64 ret;
1158787b02eSxypron.glpk@gmx.de 
1168787b02eSxypron.glpk@gmx.de 	ret  = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
1178787b02eSxypron.glpk@gmx.de 	prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
1188787b02eSxypron.glpk@gmx.de 	rem  = EFI_LOW32(prod);
1198787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
1208787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
1218787b02eSxypron.glpk@gmx.de 	rem += EFI_LOW32(prod);
1228787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
1238787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
1248787b02eSxypron.glpk@gmx.de 	rem += EFI_HIGH32(prod);
1258787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(rem);
1268787b02eSxypron.glpk@gmx.de 	/* Round to nearest integer */
1278787b02eSxypron.glpk@gmx.de 	if (rem >= (1 << 31))
1288787b02eSxypron.glpk@gmx.de 		++ret;
1298787b02eSxypron.glpk@gmx.de 	return ret;
1308787b02eSxypron.glpk@gmx.de }
1318787b02eSxypron.glpk@gmx.de 
13291be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event)
133c6841592Sxypron.glpk@gmx.de {
134c6841592Sxypron.glpk@gmx.de 	if (event->signaled)
135c6841592Sxypron.glpk@gmx.de 		return;
136c6841592Sxypron.glpk@gmx.de 	event->signaled = 1;
137c6841592Sxypron.glpk@gmx.de 	if (event->type & EVT_NOTIFY_SIGNAL) {
138a095aadfSRob Clark 		EFI_CALL(event->notify_function(event, event->notify_context));
139c6841592Sxypron.glpk@gmx.de 	}
140c6841592Sxypron.glpk@gmx.de }
141c6841592Sxypron.glpk@gmx.de 
142bee91169SAlexander Graf static efi_status_t efi_unsupported(const char *funcname)
143bee91169SAlexander Graf {
144edcef3baSAlexander Graf 	debug("EFI: App called into unimplemented function %s\n", funcname);
145bee91169SAlexander Graf 	return EFI_EXIT(EFI_UNSUPPORTED);
146bee91169SAlexander Graf }
147bee91169SAlexander Graf 
148503f2695Sxypron.glpk@gmx.de static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
149bee91169SAlexander Graf {
150503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", new_tpl);
151bee91169SAlexander Graf 	return EFI_EXIT(0);
152bee91169SAlexander Graf }
153bee91169SAlexander Graf 
154503f2695Sxypron.glpk@gmx.de static void EFIAPI efi_restore_tpl(UINTN old_tpl)
155bee91169SAlexander Graf {
156503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", old_tpl);
157b5104821SRob Clark 	efi_unsupported(__func__);
158bee91169SAlexander Graf }
159bee91169SAlexander Graf 
1606e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
161bee91169SAlexander Graf 						  unsigned long pages,
162bee91169SAlexander Graf 						  uint64_t *memory)
163bee91169SAlexander Graf {
164bee91169SAlexander Graf 	efi_status_t r;
165bee91169SAlexander Graf 
166bee91169SAlexander Graf 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
167bee91169SAlexander Graf 	r = efi_allocate_pages(type, memory_type, pages, memory);
168bee91169SAlexander Graf 	return EFI_EXIT(r);
169bee91169SAlexander Graf }
170bee91169SAlexander Graf 
1716e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
1726e0bf8d8SMasahiro Yamada 					      unsigned long pages)
173bee91169SAlexander Graf {
174bee91169SAlexander Graf 	efi_status_t r;
175bee91169SAlexander Graf 
176bee91169SAlexander Graf 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
177bee91169SAlexander Graf 	r = efi_free_pages(memory, pages);
178bee91169SAlexander Graf 	return EFI_EXIT(r);
179bee91169SAlexander Graf }
180bee91169SAlexander Graf 
1816e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
1826e0bf8d8SMasahiro Yamada 					unsigned long *memory_map_size,
183bee91169SAlexander Graf 					struct efi_mem_desc *memory_map,
184bee91169SAlexander Graf 					unsigned long *map_key,
185bee91169SAlexander Graf 					unsigned long *descriptor_size,
186bee91169SAlexander Graf 					uint32_t *descriptor_version)
187bee91169SAlexander Graf {
188bee91169SAlexander Graf 	efi_status_t r;
189bee91169SAlexander Graf 
190bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
191bee91169SAlexander Graf 		  map_key, descriptor_size, descriptor_version);
192bee91169SAlexander Graf 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
193bee91169SAlexander Graf 			       descriptor_size, descriptor_version);
194bee91169SAlexander Graf 	return EFI_EXIT(r);
195bee91169SAlexander Graf }
196bee91169SAlexander Graf 
197ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
198ead1274bSStefan Brüns 						 unsigned long size,
199bee91169SAlexander Graf 						 void **buffer)
200bee91169SAlexander Graf {
2011cd29f0aSAlexander Graf 	efi_status_t r;
2021cd29f0aSAlexander Graf 
2031cd29f0aSAlexander Graf 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
204ead1274bSStefan Brüns 	r = efi_allocate_pool(pool_type, size, buffer);
2051cd29f0aSAlexander Graf 	return EFI_EXIT(r);
206bee91169SAlexander Graf }
207bee91169SAlexander Graf 
20842417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
209bee91169SAlexander Graf {
2101cd29f0aSAlexander Graf 	efi_status_t r;
2111cd29f0aSAlexander Graf 
2121cd29f0aSAlexander Graf 	EFI_ENTRY("%p", buffer);
21342417bc8SStefan Brüns 	r = efi_free_pool(buffer);
2141cd29f0aSAlexander Graf 	return EFI_EXIT(r);
215bee91169SAlexander Graf }
216bee91169SAlexander Graf 
217bee91169SAlexander Graf /*
218c6841592Sxypron.glpk@gmx.de  * Our event capabilities are very limited. Only a small limited
219c6841592Sxypron.glpk@gmx.de  * number of events is allowed to coexist.
220bee91169SAlexander Graf  */
221c6841592Sxypron.glpk@gmx.de static struct efi_event efi_events[16];
222bee91169SAlexander Graf 
223b521d29eSxypron.glpk@gmx.de efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
2242fd945feSxypron.glpk@gmx.de 			      void (EFIAPI *notify_function) (
2252fd945feSxypron.glpk@gmx.de 					struct efi_event *event,
226e275458cSSimon Glass 					void *context),
2272fd945feSxypron.glpk@gmx.de 			      void *notify_context, struct efi_event **event)
228bee91169SAlexander Graf {
229c6841592Sxypron.glpk@gmx.de 	int i;
230c6841592Sxypron.glpk@gmx.de 
231a95343b8SJonathan Gray 	if (event == NULL)
23249deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
233a95343b8SJonathan Gray 
234a95343b8SJonathan Gray 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
23549deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
236a95343b8SJonathan Gray 
237a95343b8SJonathan Gray 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
238a95343b8SJonathan Gray 	    notify_function == NULL)
23949deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
240a95343b8SJonathan Gray 
241c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
242c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].type)
243c6841592Sxypron.glpk@gmx.de 			continue;
244c6841592Sxypron.glpk@gmx.de 		efi_events[i].type = type;
245c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_tpl = notify_tpl;
246c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_function = notify_function;
247c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_context = notify_context;
248c6841592Sxypron.glpk@gmx.de 		/* Disable timers on bootup */
249c6841592Sxypron.glpk@gmx.de 		efi_events[i].trigger_next = -1ULL;
250c6841592Sxypron.glpk@gmx.de 		efi_events[i].signaled = 0;
251c6841592Sxypron.glpk@gmx.de 		*event = &efi_events[i];
25249deb455Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
253bee91169SAlexander Graf 	}
25449deb455Sxypron.glpk@gmx.de 	return EFI_OUT_OF_RESOURCES;
255c6841592Sxypron.glpk@gmx.de }
256bee91169SAlexander Graf 
25749deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
258b521d29eSxypron.glpk@gmx.de 			uint32_t type, UINTN notify_tpl,
25949deb455Sxypron.glpk@gmx.de 			void (EFIAPI *notify_function) (
26049deb455Sxypron.glpk@gmx.de 					struct efi_event *event,
26149deb455Sxypron.glpk@gmx.de 					void *context),
26249deb455Sxypron.glpk@gmx.de 			void *notify_context, struct efi_event **event)
26349deb455Sxypron.glpk@gmx.de {
26449deb455Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
26549deb455Sxypron.glpk@gmx.de 		  notify_context);
26649deb455Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
26749deb455Sxypron.glpk@gmx.de 					 notify_context, event));
26849deb455Sxypron.glpk@gmx.de }
26949deb455Sxypron.glpk@gmx.de 
27049deb455Sxypron.glpk@gmx.de 
271bee91169SAlexander Graf /*
272bee91169SAlexander Graf  * Our timers have to work without interrupts, so we check whenever keyboard
273bee91169SAlexander Graf  * input or disk accesses happen if enough time elapsed for it to fire.
274bee91169SAlexander Graf  */
275bee91169SAlexander Graf void efi_timer_check(void)
276bee91169SAlexander Graf {
277c6841592Sxypron.glpk@gmx.de 	int i;
278bee91169SAlexander Graf 	u64 now = timer_get_us();
279bee91169SAlexander Graf 
280c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
281c6841592Sxypron.glpk@gmx.de 		if (!efi_events[i].type ||
282c6841592Sxypron.glpk@gmx.de 		    !(efi_events[i].type & EVT_TIMER) ||
283c6841592Sxypron.glpk@gmx.de 		    efi_events[i].trigger_type == EFI_TIMER_STOP ||
284c6841592Sxypron.glpk@gmx.de 		    now < efi_events[i].trigger_next)
285c6841592Sxypron.glpk@gmx.de 			continue;
286c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
287c6841592Sxypron.glpk@gmx.de 			efi_events[i].trigger_next +=
2888787b02eSxypron.glpk@gmx.de 				efi_events[i].trigger_time;
289c6841592Sxypron.glpk@gmx.de 			efi_events[i].signaled = 0;
290bee91169SAlexander Graf 		}
291c6841592Sxypron.glpk@gmx.de 		efi_signal_event(&efi_events[i]);
292c6841592Sxypron.glpk@gmx.de 	}
293bee91169SAlexander Graf 	WATCHDOG_RESET();
294bee91169SAlexander Graf }
295bee91169SAlexander Graf 
296b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
297bee91169SAlexander Graf 			   uint64_t trigger_time)
298bee91169SAlexander Graf {
299c6841592Sxypron.glpk@gmx.de 	int i;
300bee91169SAlexander Graf 
3018787b02eSxypron.glpk@gmx.de 	/*
3028787b02eSxypron.glpk@gmx.de 	 * The parameter defines a multiple of 100ns.
3038787b02eSxypron.glpk@gmx.de 	 * We use multiples of 1000ns. So divide by 10.
3048787b02eSxypron.glpk@gmx.de 	 */
3058787b02eSxypron.glpk@gmx.de 	trigger_time = efi_div10(trigger_time);
306bee91169SAlexander Graf 
307c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
308c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
309c6841592Sxypron.glpk@gmx.de 			continue;
310bee91169SAlexander Graf 
311c6841592Sxypron.glpk@gmx.de 		if (!(event->type & EVT_TIMER))
312c6841592Sxypron.glpk@gmx.de 			break;
313bee91169SAlexander Graf 		switch (type) {
314bee91169SAlexander Graf 		case EFI_TIMER_STOP:
315c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
316bee91169SAlexander Graf 			break;
317bee91169SAlexander Graf 		case EFI_TIMER_PERIODIC:
318bee91169SAlexander Graf 		case EFI_TIMER_RELATIVE:
319c6841592Sxypron.glpk@gmx.de 			event->trigger_next =
3208787b02eSxypron.glpk@gmx.de 				timer_get_us() + trigger_time;
321bee91169SAlexander Graf 			break;
322bee91169SAlexander Graf 		default:
323bfc72462Sxypron.glpk@gmx.de 			return EFI_INVALID_PARAMETER;
324bee91169SAlexander Graf 		}
325c6841592Sxypron.glpk@gmx.de 		event->trigger_type = type;
326c6841592Sxypron.glpk@gmx.de 		event->trigger_time = trigger_time;
327bfc72462Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
328bee91169SAlexander Graf 	}
329bfc72462Sxypron.glpk@gmx.de 	return EFI_INVALID_PARAMETER;
330bfc72462Sxypron.glpk@gmx.de }
331bfc72462Sxypron.glpk@gmx.de 
332b521d29eSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
333b521d29eSxypron.glpk@gmx.de 					     enum efi_timer_delay type,
334bfc72462Sxypron.glpk@gmx.de 					     uint64_t trigger_time)
335bfc72462Sxypron.glpk@gmx.de {
336bfc72462Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
337bfc72462Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
338c6841592Sxypron.glpk@gmx.de }
339bee91169SAlexander Graf 
340bee91169SAlexander Graf static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
3412fd945feSxypron.glpk@gmx.de 					      struct efi_event **event,
3422fd945feSxypron.glpk@gmx.de 					      unsigned long *index)
343bee91169SAlexander Graf {
344c6841592Sxypron.glpk@gmx.de 	int i, j;
345bee91169SAlexander Graf 
346bee91169SAlexander Graf 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
347bee91169SAlexander Graf 
348c6841592Sxypron.glpk@gmx.de 	/* Check parameters */
349c6841592Sxypron.glpk@gmx.de 	if (!num_events || !event)
350c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
351c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < num_events; ++i) {
352c6841592Sxypron.glpk@gmx.de 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
353c6841592Sxypron.glpk@gmx.de 			if (event[i] == &efi_events[j])
354c6841592Sxypron.glpk@gmx.de 				goto known_event;
355c6841592Sxypron.glpk@gmx.de 		}
356c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
357c6841592Sxypron.glpk@gmx.de known_event:
358c6841592Sxypron.glpk@gmx.de 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
359c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
360c6841592Sxypron.glpk@gmx.de 	}
361c6841592Sxypron.glpk@gmx.de 
362c6841592Sxypron.glpk@gmx.de 	/* Wait for signal */
363c6841592Sxypron.glpk@gmx.de 	for (;;) {
364c6841592Sxypron.glpk@gmx.de 		for (i = 0; i < num_events; ++i) {
365c6841592Sxypron.glpk@gmx.de 			if (event[i]->signaled)
366c6841592Sxypron.glpk@gmx.de 				goto out;
367c6841592Sxypron.glpk@gmx.de 		}
368c6841592Sxypron.glpk@gmx.de 		/* Allow events to occur. */
369bee91169SAlexander Graf 		efi_timer_check();
370c6841592Sxypron.glpk@gmx.de 	}
371c6841592Sxypron.glpk@gmx.de 
372c6841592Sxypron.glpk@gmx.de out:
373c6841592Sxypron.glpk@gmx.de 	/*
374c6841592Sxypron.glpk@gmx.de 	 * Reset the signal which is passed to the caller to allow periodic
375c6841592Sxypron.glpk@gmx.de 	 * events to occur.
376c6841592Sxypron.glpk@gmx.de 	 */
377c6841592Sxypron.glpk@gmx.de 	event[i]->signaled = 0;
378c6841592Sxypron.glpk@gmx.de 	if (index)
379c6841592Sxypron.glpk@gmx.de 		*index = i;
380bee91169SAlexander Graf 
381bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
382bee91169SAlexander Graf }
383bee91169SAlexander Graf 
384c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
385bee91169SAlexander Graf {
386c6841592Sxypron.glpk@gmx.de 	int i;
387c6841592Sxypron.glpk@gmx.de 
388bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
389c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
390c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
391c6841592Sxypron.glpk@gmx.de 			continue;
392c6841592Sxypron.glpk@gmx.de 		efi_signal_event(event);
393c6841592Sxypron.glpk@gmx.de 		break;
394c6841592Sxypron.glpk@gmx.de 	}
395bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
396bee91169SAlexander Graf }
397bee91169SAlexander Graf 
3982fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
399bee91169SAlexander Graf {
400c6841592Sxypron.glpk@gmx.de 	int i;
401c6841592Sxypron.glpk@gmx.de 
402bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
403c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
404c6841592Sxypron.glpk@gmx.de 		if (event == &efi_events[i]) {
405c6841592Sxypron.glpk@gmx.de 			event->type = 0;
406c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
407c6841592Sxypron.glpk@gmx.de 			event->signaled = 0;
408bee91169SAlexander Graf 			return EFI_EXIT(EFI_SUCCESS);
409bee91169SAlexander Graf 		}
410c6841592Sxypron.glpk@gmx.de 	}
411c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
412c6841592Sxypron.glpk@gmx.de }
413bee91169SAlexander Graf 
4142fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
415bee91169SAlexander Graf {
416c6841592Sxypron.glpk@gmx.de 	int i;
417c6841592Sxypron.glpk@gmx.de 
418bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
419c6841592Sxypron.glpk@gmx.de 	efi_timer_check();
420c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
421c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
422c6841592Sxypron.glpk@gmx.de 			continue;
423c6841592Sxypron.glpk@gmx.de 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
424c6841592Sxypron.glpk@gmx.de 			break;
425c6841592Sxypron.glpk@gmx.de 		if (event->signaled)
426c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_SUCCESS);
427bee91169SAlexander Graf 		return EFI_EXIT(EFI_NOT_READY);
428bee91169SAlexander Graf 	}
429c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
430c6841592Sxypron.glpk@gmx.de }
431bee91169SAlexander Graf 
432bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
433bee91169SAlexander Graf 			efi_guid_t *protocol, int protocol_interface_type,
434bee91169SAlexander Graf 			void *protocol_interface)
435bee91169SAlexander Graf {
436e0549f8aSxypron.glpk@gmx.de 	struct list_head *lhandle;
437e0549f8aSxypron.glpk@gmx.de 	int i;
438e0549f8aSxypron.glpk@gmx.de 	efi_status_t r;
439e0549f8aSxypron.glpk@gmx.de 
440e0549f8aSxypron.glpk@gmx.de 	if (!handle || !protocol ||
441e0549f8aSxypron.glpk@gmx.de 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
442e0549f8aSxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
443e0549f8aSxypron.glpk@gmx.de 		goto out;
444bee91169SAlexander Graf 	}
445e0549f8aSxypron.glpk@gmx.de 
446e0549f8aSxypron.glpk@gmx.de 	/* Create new handle if requested. */
447e0549f8aSxypron.glpk@gmx.de 	if (!*handle) {
448e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
449e0549f8aSxypron.glpk@gmx.de 		goto out;
450e0549f8aSxypron.glpk@gmx.de 	}
451e0549f8aSxypron.glpk@gmx.de 	/* Find object. */
452e0549f8aSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
453e0549f8aSxypron.glpk@gmx.de 		struct efi_object *efiobj;
454e0549f8aSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
455e0549f8aSxypron.glpk@gmx.de 
456e0549f8aSxypron.glpk@gmx.de 		if (efiobj->handle != *handle)
457e0549f8aSxypron.glpk@gmx.de 			continue;
458e0549f8aSxypron.glpk@gmx.de 		/* Check if protocol is already installed on the handle. */
459e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
460e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
461e0549f8aSxypron.glpk@gmx.de 
462e0549f8aSxypron.glpk@gmx.de 			if (!handler->guid)
463e0549f8aSxypron.glpk@gmx.de 				continue;
464e0549f8aSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
465e0549f8aSxypron.glpk@gmx.de 				r = EFI_INVALID_PARAMETER;
466e0549f8aSxypron.glpk@gmx.de 				goto out;
467e0549f8aSxypron.glpk@gmx.de 			}
468e0549f8aSxypron.glpk@gmx.de 		}
469e0549f8aSxypron.glpk@gmx.de 		/* Install protocol in first empty slot. */
470e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
471e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
472e0549f8aSxypron.glpk@gmx.de 
473e0549f8aSxypron.glpk@gmx.de 			if (handler->guid)
474e0549f8aSxypron.glpk@gmx.de 				continue;
475e0549f8aSxypron.glpk@gmx.de 
476e0549f8aSxypron.glpk@gmx.de 			handler->guid = protocol;
477e0549f8aSxypron.glpk@gmx.de 			handler->protocol_interface = protocol_interface;
478e0549f8aSxypron.glpk@gmx.de 			r = EFI_SUCCESS;
479e0549f8aSxypron.glpk@gmx.de 			goto out;
480e0549f8aSxypron.glpk@gmx.de 		}
481e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
482e0549f8aSxypron.glpk@gmx.de 		goto out;
483e0549f8aSxypron.glpk@gmx.de 	}
484e0549f8aSxypron.glpk@gmx.de 	r = EFI_INVALID_PARAMETER;
485e0549f8aSxypron.glpk@gmx.de out:
4868bee5a3cSxypron.glpk@gmx.de 	return r;
4878bee5a3cSxypron.glpk@gmx.de }
4888bee5a3cSxypron.glpk@gmx.de 
4898bee5a3cSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
4908bee5a3cSxypron.glpk@gmx.de 			efi_guid_t *protocol, int protocol_interface_type,
4918bee5a3cSxypron.glpk@gmx.de 			void *protocol_interface)
4928bee5a3cSxypron.glpk@gmx.de {
4938bee5a3cSxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
4948bee5a3cSxypron.glpk@gmx.de 		  protocol_interface);
4958bee5a3cSxypron.glpk@gmx.de 
4968bee5a3cSxypron.glpk@gmx.de 	return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
4978bee5a3cSxypron.glpk@gmx.de 						       protocol_interface_type,
4988bee5a3cSxypron.glpk@gmx.de 						       protocol_interface));
499e0549f8aSxypron.glpk@gmx.de }
500e0549f8aSxypron.glpk@gmx.de 
501bee91169SAlexander Graf static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
502bee91169SAlexander Graf 			efi_guid_t *protocol, void *old_interface,
503bee91169SAlexander Graf 			void *new_interface)
504bee91169SAlexander Graf {
505bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
506bee91169SAlexander Graf 		  new_interface);
507bee91169SAlexander Graf 	return EFI_EXIT(EFI_ACCESS_DENIED);
508bee91169SAlexander Graf }
509bee91169SAlexander Graf 
510bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
511bee91169SAlexander Graf 			efi_guid_t *protocol, void *protocol_interface)
512bee91169SAlexander Graf {
5134b6ed0d7Sxypron.glpk@gmx.de 	struct list_head *lhandle;
5144b6ed0d7Sxypron.glpk@gmx.de 	int i;
5154b6ed0d7Sxypron.glpk@gmx.de 	efi_status_t r = EFI_NOT_FOUND;
5164b6ed0d7Sxypron.glpk@gmx.de 
5174b6ed0d7Sxypron.glpk@gmx.de 	if (!handle || !protocol) {
5184b6ed0d7Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
5194b6ed0d7Sxypron.glpk@gmx.de 		goto out;
5204b6ed0d7Sxypron.glpk@gmx.de 	}
5214b6ed0d7Sxypron.glpk@gmx.de 
5224b6ed0d7Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
5234b6ed0d7Sxypron.glpk@gmx.de 		struct efi_object *efiobj;
5244b6ed0d7Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
5254b6ed0d7Sxypron.glpk@gmx.de 
5264b6ed0d7Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
5274b6ed0d7Sxypron.glpk@gmx.de 			continue;
5284b6ed0d7Sxypron.glpk@gmx.de 
5294b6ed0d7Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
5304b6ed0d7Sxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
5314b6ed0d7Sxypron.glpk@gmx.de 			const efi_guid_t *hprotocol = handler->guid;
5324b6ed0d7Sxypron.glpk@gmx.de 
5334b6ed0d7Sxypron.glpk@gmx.de 			if (!hprotocol)
5344b6ed0d7Sxypron.glpk@gmx.de 				continue;
5354b6ed0d7Sxypron.glpk@gmx.de 			if (!guidcmp(hprotocol, protocol)) {
5364b6ed0d7Sxypron.glpk@gmx.de 				if (handler->protocol_interface) {
5374b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_ACCESS_DENIED;
5384b6ed0d7Sxypron.glpk@gmx.de 				} else {
5394b6ed0d7Sxypron.glpk@gmx.de 					handler->guid = 0;
5404b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_SUCCESS;
5414b6ed0d7Sxypron.glpk@gmx.de 				}
5424b6ed0d7Sxypron.glpk@gmx.de 				goto out;
5434b6ed0d7Sxypron.glpk@gmx.de 			}
5444b6ed0d7Sxypron.glpk@gmx.de 		}
5454b6ed0d7Sxypron.glpk@gmx.de 	}
5464b6ed0d7Sxypron.glpk@gmx.de 
5474b6ed0d7Sxypron.glpk@gmx.de out:
5483d8e1456Sxypron.glpk@gmx.de 	return r;
5493d8e1456Sxypron.glpk@gmx.de }
5503d8e1456Sxypron.glpk@gmx.de 
5513d8e1456Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
5523d8e1456Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *protocol_interface)
5533d8e1456Sxypron.glpk@gmx.de {
5543d8e1456Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
5553d8e1456Sxypron.glpk@gmx.de 
5563d8e1456Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
5573d8e1456Sxypron.glpk@gmx.de 							 protocol_interface));
558bee91169SAlexander Graf }
559bee91169SAlexander Graf 
560bee91169SAlexander Graf static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
5612fd945feSxypron.glpk@gmx.de 							struct efi_event *event,
562bee91169SAlexander Graf 							void **registration)
563bee91169SAlexander Graf {
564bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
565bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
566bee91169SAlexander Graf }
567bee91169SAlexander Graf 
568bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
569bee91169SAlexander Graf 		      efi_guid_t *protocol, void *search_key,
570bee91169SAlexander Graf 		      struct efi_object *efiobj)
571bee91169SAlexander Graf {
572bee91169SAlexander Graf 	int i;
573bee91169SAlexander Graf 
574bee91169SAlexander Graf 	switch (search_type) {
575bee91169SAlexander Graf 	case all_handles:
576bee91169SAlexander Graf 		return 0;
577bee91169SAlexander Graf 	case by_register_notify:
578bee91169SAlexander Graf 		return -1;
579bee91169SAlexander Graf 	case by_protocol:
580bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
581bee91169SAlexander Graf 			const efi_guid_t *guid = efiobj->protocols[i].guid;
582bee91169SAlexander Graf 			if (guid && !guidcmp(guid, protocol))
583bee91169SAlexander Graf 				return 0;
584bee91169SAlexander Graf 		}
585bee91169SAlexander Graf 		return -1;
586bee91169SAlexander Graf 	}
587bee91169SAlexander Graf 
588bee91169SAlexander Graf 	return -1;
589bee91169SAlexander Graf }
590bee91169SAlexander Graf 
591bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle(
592bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
593bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
594bee91169SAlexander Graf 			unsigned long *buffer_size, efi_handle_t *buffer)
595bee91169SAlexander Graf {
596bee91169SAlexander Graf 	struct list_head *lhandle;
597bee91169SAlexander Graf 	unsigned long size = 0;
598bee91169SAlexander Graf 
599bee91169SAlexander Graf 	/* Count how much space we need */
600bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
601bee91169SAlexander Graf 		struct efi_object *efiobj;
602bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
603bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
604bee91169SAlexander Graf 			size += sizeof(void*);
605bee91169SAlexander Graf 		}
606bee91169SAlexander Graf 	}
607bee91169SAlexander Graf 
608bee91169SAlexander Graf 	if (*buffer_size < size) {
609bee91169SAlexander Graf 		*buffer_size = size;
61026329584Sxypron.glpk@gmx.de 		return EFI_BUFFER_TOO_SMALL;
611bee91169SAlexander Graf 	}
612bee91169SAlexander Graf 
613bee91169SAlexander Graf 	/* Then fill the array */
614bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
615bee91169SAlexander Graf 		struct efi_object *efiobj;
616bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
617bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
618bee91169SAlexander Graf 			*(buffer++) = efiobj->handle;
619bee91169SAlexander Graf 		}
620bee91169SAlexander Graf 	}
621bee91169SAlexander Graf 
622bee91169SAlexander Graf 	*buffer_size = size;
62326329584Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
62426329584Sxypron.glpk@gmx.de }
62526329584Sxypron.glpk@gmx.de 
62626329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
62726329584Sxypron.glpk@gmx.de 			enum efi_locate_search_type search_type,
62826329584Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *search_key,
62926329584Sxypron.glpk@gmx.de 			unsigned long *buffer_size, efi_handle_t *buffer)
63026329584Sxypron.glpk@gmx.de {
63126329584Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
63226329584Sxypron.glpk@gmx.de 		  buffer_size, buffer);
63326329584Sxypron.glpk@gmx.de 
63426329584Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
63526329584Sxypron.glpk@gmx.de 			buffer_size, buffer));
636bee91169SAlexander Graf }
637bee91169SAlexander Graf 
638bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
639bee91169SAlexander Graf 			struct efi_device_path **device_path,
640bee91169SAlexander Graf 			efi_handle_t *device)
641bee91169SAlexander Graf {
642bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
643bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
644bee91169SAlexander Graf }
645bee91169SAlexander Graf 
646d98cdf6aSAlexander Graf /* Collapses configuration table entries, removing index i */
647d98cdf6aSAlexander Graf static void efi_remove_configuration_table(int i)
648d98cdf6aSAlexander Graf {
649d98cdf6aSAlexander Graf 	struct efi_configuration_table *this = &efi_conf_table[i];
650d98cdf6aSAlexander Graf 	struct efi_configuration_table *next = &efi_conf_table[i+1];
651d98cdf6aSAlexander Graf 	struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
652d98cdf6aSAlexander Graf 
653d98cdf6aSAlexander Graf 	memmove(this, next, (ulong)end - (ulong)next);
654d98cdf6aSAlexander Graf 	systab.nr_tables--;
655d98cdf6aSAlexander Graf }
656d98cdf6aSAlexander Graf 
657488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
658bee91169SAlexander Graf {
659bee91169SAlexander Graf 	int i;
660bee91169SAlexander Graf 
661bee91169SAlexander Graf 	/* Check for guid override */
662bee91169SAlexander Graf 	for (i = 0; i < systab.nr_tables; i++) {
663bee91169SAlexander Graf 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
664d98cdf6aSAlexander Graf 			if (table)
665bee91169SAlexander Graf 				efi_conf_table[i].table = table;
666d98cdf6aSAlexander Graf 			else
667d98cdf6aSAlexander Graf 				efi_remove_configuration_table(i);
668488bf12dSAlexander Graf 			return EFI_SUCCESS;
669bee91169SAlexander Graf 		}
670bee91169SAlexander Graf 	}
671bee91169SAlexander Graf 
672d98cdf6aSAlexander Graf 	if (!table)
673d98cdf6aSAlexander Graf 		return EFI_NOT_FOUND;
674d98cdf6aSAlexander Graf 
675bee91169SAlexander Graf 	/* No override, check for overflow */
676bee91169SAlexander Graf 	if (i >= ARRAY_SIZE(efi_conf_table))
677488bf12dSAlexander Graf 		return EFI_OUT_OF_RESOURCES;
678bee91169SAlexander Graf 
679bee91169SAlexander Graf 	/* Add a new entry */
680bee91169SAlexander Graf 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
681bee91169SAlexander Graf 	efi_conf_table[i].table = table;
682aba5e919SAlexander Graf 	systab.nr_tables = i + 1;
683bee91169SAlexander Graf 
684488bf12dSAlexander Graf 	return EFI_SUCCESS;
685488bf12dSAlexander Graf }
686488bf12dSAlexander Graf 
687488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
688488bf12dSAlexander Graf 							       void *table)
689488bf12dSAlexander Graf {
690488bf12dSAlexander Graf 	EFI_ENTRY("%p, %p", guid, table);
691488bf12dSAlexander Graf 	return EFI_EXIT(efi_install_configuration_table(guid, table));
692bee91169SAlexander Graf }
693bee91169SAlexander Graf 
694bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
695bee91169SAlexander Graf 					  efi_handle_t parent_image,
696bee91169SAlexander Graf 					  struct efi_device_path *file_path,
697bee91169SAlexander Graf 					  void *source_buffer,
698bee91169SAlexander Graf 					  unsigned long source_size,
699bee91169SAlexander Graf 					  efi_handle_t *image_handle)
700bee91169SAlexander Graf {
701bee91169SAlexander Graf 	static struct efi_object loaded_image_info_obj = {
702bee91169SAlexander Graf 		.protocols = {
703bee91169SAlexander Graf 			{
704bee91169SAlexander Graf 				.guid = &efi_guid_loaded_image,
705bee91169SAlexander Graf 			},
706bee91169SAlexander Graf 		},
707bee91169SAlexander Graf 	};
708bee91169SAlexander Graf 	struct efi_loaded_image *info;
709bee91169SAlexander Graf 	struct efi_object *obj;
710bee91169SAlexander Graf 
711bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
712bee91169SAlexander Graf 		  file_path, source_buffer, source_size, image_handle);
713bee91169SAlexander Graf 	info = malloc(sizeof(*info));
714b5349f74Sxypron.glpk@gmx.de 	loaded_image_info_obj.protocols[0].protocol_interface = info;
715bee91169SAlexander Graf 	obj = malloc(sizeof(loaded_image_info_obj));
716bee91169SAlexander Graf 	memset(info, 0, sizeof(*info));
717bee91169SAlexander Graf 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
718bee91169SAlexander Graf 	obj->handle = info;
719bee91169SAlexander Graf 	info->file_path = file_path;
720bee91169SAlexander Graf 	info->reserved = efi_load_pe(source_buffer, info);
721bee91169SAlexander Graf 	if (!info->reserved) {
722bee91169SAlexander Graf 		free(info);
723bee91169SAlexander Graf 		free(obj);
724bee91169SAlexander Graf 		return EFI_EXIT(EFI_UNSUPPORTED);
725bee91169SAlexander Graf 	}
726bee91169SAlexander Graf 
727bee91169SAlexander Graf 	*image_handle = info;
728bee91169SAlexander Graf 	list_add_tail(&obj->link, &efi_obj_list);
729bee91169SAlexander Graf 
730bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
731bee91169SAlexander Graf }
732bee91169SAlexander Graf 
733bee91169SAlexander Graf static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
734bee91169SAlexander Graf 					   unsigned long *exit_data_size,
735bee91169SAlexander Graf 					   s16 **exit_data)
736bee91169SAlexander Graf {
737bee91169SAlexander Graf 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
738bee91169SAlexander Graf 	struct efi_loaded_image *info = image_handle;
739bee91169SAlexander Graf 
740bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
741bee91169SAlexander Graf 	entry = info->reserved;
742bee91169SAlexander Graf 
743bee91169SAlexander Graf 	efi_is_direct_boot = false;
744bee91169SAlexander Graf 
745bee91169SAlexander Graf 	/* call the image! */
746a86aeaf2SAlexander Graf 	if (setjmp(&info->exit_jmp)) {
747a86aeaf2SAlexander Graf 		/* We returned from the child image */
748a86aeaf2SAlexander Graf 		return EFI_EXIT(info->exit_status);
749a86aeaf2SAlexander Graf 	}
750a86aeaf2SAlexander Graf 
751*c160d2f5SRob Clark 	__efi_exit_check();
752bee91169SAlexander Graf 	entry(image_handle, &systab);
753*c160d2f5SRob Clark 	__efi_entry_check();
754bee91169SAlexander Graf 
755bee91169SAlexander Graf 	/* Should usually never get here */
756bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
757bee91169SAlexander Graf }
758bee91169SAlexander Graf 
759a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
760a86aeaf2SAlexander Graf 			efi_status_t exit_status, unsigned long exit_data_size,
761a86aeaf2SAlexander Graf 			int16_t *exit_data)
762bee91169SAlexander Graf {
763a86aeaf2SAlexander Graf 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
764a86aeaf2SAlexander Graf 
765bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
766bee91169SAlexander Graf 		  exit_data_size, exit_data);
767a86aeaf2SAlexander Graf 
768a86aeaf2SAlexander Graf 	loaded_image_info->exit_status = exit_status;
769692fcdd8SAlexander Graf 	longjmp(&loaded_image_info->exit_jmp, 1);
770a86aeaf2SAlexander Graf 
771a86aeaf2SAlexander Graf 	panic("EFI application exited");
772bee91169SAlexander Graf }
773bee91169SAlexander Graf 
774bee91169SAlexander Graf static struct efi_object *efi_search_obj(void *handle)
775bee91169SAlexander Graf {
776bee91169SAlexander Graf 	struct list_head *lhandle;
777bee91169SAlexander Graf 
778bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
779bee91169SAlexander Graf 		struct efi_object *efiobj;
780bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
781bee91169SAlexander Graf 		if (efiobj->handle == handle)
782bee91169SAlexander Graf 			return efiobj;
783bee91169SAlexander Graf 	}
784bee91169SAlexander Graf 
785bee91169SAlexander Graf 	return NULL;
786bee91169SAlexander Graf }
787bee91169SAlexander Graf 
788bee91169SAlexander Graf static efi_status_t EFIAPI efi_unload_image(void *image_handle)
789bee91169SAlexander Graf {
790bee91169SAlexander Graf 	struct efi_object *efiobj;
791bee91169SAlexander Graf 
792bee91169SAlexander Graf 	EFI_ENTRY("%p", image_handle);
793bee91169SAlexander Graf 	efiobj = efi_search_obj(image_handle);
794bee91169SAlexander Graf 	if (efiobj)
795bee91169SAlexander Graf 		list_del(&efiobj->link);
796bee91169SAlexander Graf 
797bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
798bee91169SAlexander Graf }
799bee91169SAlexander Graf 
800bee91169SAlexander Graf static void efi_exit_caches(void)
801bee91169SAlexander Graf {
802bee91169SAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
803bee91169SAlexander Graf 	/*
804bee91169SAlexander Graf 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
805bee91169SAlexander Graf 	 * a zImage, but does not know of all cache layers. Give it a hand.
806bee91169SAlexander Graf 	 */
807bee91169SAlexander Graf 	if (efi_is_direct_boot)
808bee91169SAlexander Graf 		cleanup_before_linux();
809bee91169SAlexander Graf #endif
810bee91169SAlexander Graf }
811bee91169SAlexander Graf 
812bee91169SAlexander Graf static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
813bee91169SAlexander Graf 						  unsigned long map_key)
814bee91169SAlexander Graf {
815bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", image_handle, map_key);
816bee91169SAlexander Graf 
817b7b8410aSAlexander Graf 	board_quiesce_devices();
818b7b8410aSAlexander Graf 
819bee91169SAlexander Graf 	/* Fix up caches for EFI payloads if necessary */
820bee91169SAlexander Graf 	efi_exit_caches();
821bee91169SAlexander Graf 
822bee91169SAlexander Graf 	/* This stops all lingering devices */
823bee91169SAlexander Graf 	bootm_disable_interrupts();
824bee91169SAlexander Graf 
825bee91169SAlexander Graf 	/* Give the payload some time to boot */
826bee91169SAlexander Graf 	WATCHDOG_RESET();
827bee91169SAlexander Graf 
828bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
829bee91169SAlexander Graf }
830bee91169SAlexander Graf 
831bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
832bee91169SAlexander Graf {
833bee91169SAlexander Graf 	static uint64_t mono = 0;
834bee91169SAlexander Graf 	EFI_ENTRY("%p", count);
835bee91169SAlexander Graf 	*count = mono++;
836bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
837bee91169SAlexander Graf }
838bee91169SAlexander Graf 
839bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
840bee91169SAlexander Graf {
841bee91169SAlexander Graf 	EFI_ENTRY("%ld", microseconds);
842bee91169SAlexander Graf 	udelay(microseconds);
843bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
844bee91169SAlexander Graf }
845bee91169SAlexander Graf 
846bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
847bee91169SAlexander Graf 						  uint64_t watchdog_code,
848bee91169SAlexander Graf 						  unsigned long data_size,
849bee91169SAlexander Graf 						  uint16_t *watchdog_data)
850bee91169SAlexander Graf {
851bee91169SAlexander Graf 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
852bee91169SAlexander Graf 		  data_size, watchdog_data);
853b5104821SRob Clark 	return efi_unsupported(__func__);
854bee91169SAlexander Graf }
855bee91169SAlexander Graf 
856bee91169SAlexander Graf static efi_status_t EFIAPI efi_connect_controller(
857bee91169SAlexander Graf 			efi_handle_t controller_handle,
858bee91169SAlexander Graf 			efi_handle_t *driver_image_handle,
859bee91169SAlexander Graf 			struct efi_device_path *remain_device_path,
860bee91169SAlexander Graf 			bool recursive)
861bee91169SAlexander Graf {
862bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
863bee91169SAlexander Graf 		  remain_device_path, recursive);
864bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
865bee91169SAlexander Graf }
866bee91169SAlexander Graf 
867bee91169SAlexander Graf static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
868bee91169SAlexander Graf 						     void *driver_image_handle,
869bee91169SAlexander Graf 						     void *child_handle)
870bee91169SAlexander Graf {
871bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
872bee91169SAlexander Graf 		  child_handle);
873bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
874bee91169SAlexander Graf }
875bee91169SAlexander Graf 
876bee91169SAlexander Graf static efi_status_t EFIAPI efi_close_protocol(void *handle,
877bee91169SAlexander Graf 					      efi_guid_t *protocol,
878bee91169SAlexander Graf 					      void *agent_handle,
879bee91169SAlexander Graf 					      void *controller_handle)
880bee91169SAlexander Graf {
881bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
882bee91169SAlexander Graf 		  controller_handle);
883bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
884bee91169SAlexander Graf }
885bee91169SAlexander Graf 
886bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
887bee91169SAlexander Graf 			efi_guid_t *protocol,
888bee91169SAlexander Graf 			struct efi_open_protocol_info_entry **entry_buffer,
889bee91169SAlexander Graf 			unsigned long *entry_count)
890bee91169SAlexander Graf {
891bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
892bee91169SAlexander Graf 		  entry_count);
893bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
894bee91169SAlexander Graf }
895bee91169SAlexander Graf 
896bee91169SAlexander Graf static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
897bee91169SAlexander Graf 			efi_guid_t ***protocol_buffer,
898bee91169SAlexander Graf 			unsigned long *protocol_buffer_count)
899bee91169SAlexander Graf {
900c0ebfc86Sxypron.glpk@gmx.de 	unsigned long buffer_size;
901c0ebfc86Sxypron.glpk@gmx.de 	struct efi_object *efiobj;
902c0ebfc86Sxypron.glpk@gmx.de 	unsigned long i, j;
903c0ebfc86Sxypron.glpk@gmx.de 	struct list_head *lhandle;
904c0ebfc86Sxypron.glpk@gmx.de 	efi_status_t r;
905c0ebfc86Sxypron.glpk@gmx.de 
906bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
907bee91169SAlexander Graf 		  protocol_buffer_count);
908c0ebfc86Sxypron.glpk@gmx.de 
909c0ebfc86Sxypron.glpk@gmx.de 	if (!handle || !protocol_buffer || !protocol_buffer_count)
910c0ebfc86Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
911c0ebfc86Sxypron.glpk@gmx.de 
912c0ebfc86Sxypron.glpk@gmx.de 	*protocol_buffer = NULL;
913661c8327SRob Clark 	*protocol_buffer_count = 0;
914c0ebfc86Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
915c0ebfc86Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
916c0ebfc86Sxypron.glpk@gmx.de 
917c0ebfc86Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
918c0ebfc86Sxypron.glpk@gmx.de 			continue;
919c0ebfc86Sxypron.glpk@gmx.de 
920c0ebfc86Sxypron.glpk@gmx.de 		/* Count protocols */
921c0ebfc86Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
922c0ebfc86Sxypron.glpk@gmx.de 			if (efiobj->protocols[i].guid)
923c0ebfc86Sxypron.glpk@gmx.de 				++*protocol_buffer_count;
924c0ebfc86Sxypron.glpk@gmx.de 		}
925c0ebfc86Sxypron.glpk@gmx.de 		/* Copy guids */
926c0ebfc86Sxypron.glpk@gmx.de 		if (*protocol_buffer_count) {
927c0ebfc86Sxypron.glpk@gmx.de 			buffer_size = sizeof(efi_guid_t *) *
928c0ebfc86Sxypron.glpk@gmx.de 					*protocol_buffer_count;
929c0ebfc86Sxypron.glpk@gmx.de 			r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
930c0ebfc86Sxypron.glpk@gmx.de 					      buffer_size,
931c0ebfc86Sxypron.glpk@gmx.de 					      (void **)protocol_buffer);
932c0ebfc86Sxypron.glpk@gmx.de 			if (r != EFI_SUCCESS)
933c0ebfc86Sxypron.glpk@gmx.de 				return EFI_EXIT(r);
934c0ebfc86Sxypron.glpk@gmx.de 			j = 0;
935c0ebfc86Sxypron.glpk@gmx.de 			for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
936c0ebfc86Sxypron.glpk@gmx.de 				if (efiobj->protocols[i].guid) {
937c0ebfc86Sxypron.glpk@gmx.de 					(*protocol_buffer)[j] = (void *)
938c0ebfc86Sxypron.glpk@gmx.de 						efiobj->protocols[i].guid;
939c0ebfc86Sxypron.glpk@gmx.de 					++j;
940c0ebfc86Sxypron.glpk@gmx.de 				}
941c0ebfc86Sxypron.glpk@gmx.de 			}
942c0ebfc86Sxypron.glpk@gmx.de 		}
943c0ebfc86Sxypron.glpk@gmx.de 		break;
944c0ebfc86Sxypron.glpk@gmx.de 	}
945c0ebfc86Sxypron.glpk@gmx.de 
946c0ebfc86Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_SUCCESS);
947bee91169SAlexander Graf }
948bee91169SAlexander Graf 
949bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
950bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
951bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
952bee91169SAlexander Graf 			unsigned long *no_handles, efi_handle_t **buffer)
953bee91169SAlexander Graf {
954c2e703f9Sxypron.glpk@gmx.de 	efi_status_t r;
955c2e703f9Sxypron.glpk@gmx.de 	unsigned long buffer_size = 0;
956c2e703f9Sxypron.glpk@gmx.de 
957bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
958bee91169SAlexander Graf 		  no_handles, buffer);
959c2e703f9Sxypron.glpk@gmx.de 
960c2e703f9Sxypron.glpk@gmx.de 	if (!no_handles || !buffer) {
961c2e703f9Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
962c2e703f9Sxypron.glpk@gmx.de 		goto out;
963c2e703f9Sxypron.glpk@gmx.de 	}
964c2e703f9Sxypron.glpk@gmx.de 	*no_handles = 0;
965c2e703f9Sxypron.glpk@gmx.de 	*buffer = NULL;
966c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
967c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
968c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_BUFFER_TOO_SMALL)
969c2e703f9Sxypron.glpk@gmx.de 		goto out;
970c2e703f9Sxypron.glpk@gmx.de 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
971c2e703f9Sxypron.glpk@gmx.de 			      (void **)buffer);
972c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
973c2e703f9Sxypron.glpk@gmx.de 		goto out;
974c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
975c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
976c2e703f9Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
977c2e703f9Sxypron.glpk@gmx.de 		*no_handles = buffer_size / sizeof(void *);
978c2e703f9Sxypron.glpk@gmx.de out:
979c2e703f9Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
980bee91169SAlexander Graf }
981bee91169SAlexander Graf 
982bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
983bee91169SAlexander Graf 					       void *registration,
984bee91169SAlexander Graf 					       void **protocol_interface)
985bee91169SAlexander Graf {
98688adae5eSxypron.glpk@gmx.de 	struct list_head *lhandle;
987bee91169SAlexander Graf 	int i;
988bee91169SAlexander Graf 
989bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
99088adae5eSxypron.glpk@gmx.de 
99188adae5eSxypron.glpk@gmx.de 	if (!protocol || !protocol_interface)
99288adae5eSxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
99388adae5eSxypron.glpk@gmx.de 
99488adae5eSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
99588adae5eSxypron.glpk@gmx.de 		struct efi_object *efiobj;
99688adae5eSxypron.glpk@gmx.de 
99788adae5eSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
99888adae5eSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
99988adae5eSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
100088adae5eSxypron.glpk@gmx.de 
100188adae5eSxypron.glpk@gmx.de 			if (!handler->guid)
100288adae5eSxypron.glpk@gmx.de 				continue;
100388adae5eSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
100488adae5eSxypron.glpk@gmx.de 				*protocol_interface =
100588adae5eSxypron.glpk@gmx.de 					handler->protocol_interface;
1006bee91169SAlexander Graf 				return EFI_EXIT(EFI_SUCCESS);
1007bee91169SAlexander Graf 			}
1008bee91169SAlexander Graf 		}
100988adae5eSxypron.glpk@gmx.de 	}
101088adae5eSxypron.glpk@gmx.de 	*protocol_interface = NULL;
1011bee91169SAlexander Graf 
1012bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
1013bee91169SAlexander Graf }
1014bee91169SAlexander Graf 
1015bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1016bee91169SAlexander Graf 			void **handle, ...)
1017bee91169SAlexander Graf {
1018bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
101958b83586Sxypron.glpk@gmx.de 
102058b83586Sxypron.glpk@gmx.de 	va_list argptr;
102158b83586Sxypron.glpk@gmx.de 	efi_guid_t *protocol;
102258b83586Sxypron.glpk@gmx.de 	void *protocol_interface;
102358b83586Sxypron.glpk@gmx.de 	efi_status_t r = EFI_SUCCESS;
102458b83586Sxypron.glpk@gmx.de 	int i = 0;
102558b83586Sxypron.glpk@gmx.de 
102658b83586Sxypron.glpk@gmx.de 	if (!handle)
102758b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
102858b83586Sxypron.glpk@gmx.de 
102958b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
103058b83586Sxypron.glpk@gmx.de 	for (;;) {
103158b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
103258b83586Sxypron.glpk@gmx.de 		if (!protocol)
103358b83586Sxypron.glpk@gmx.de 			break;
103458b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
103558b83586Sxypron.glpk@gmx.de 		r = efi_install_protocol_interface(handle, protocol,
103658b83586Sxypron.glpk@gmx.de 						   EFI_NATIVE_INTERFACE,
103758b83586Sxypron.glpk@gmx.de 						   protocol_interface);
103858b83586Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
103958b83586Sxypron.glpk@gmx.de 			break;
104058b83586Sxypron.glpk@gmx.de 		i++;
104158b83586Sxypron.glpk@gmx.de 	}
104258b83586Sxypron.glpk@gmx.de 	va_end(argptr);
104358b83586Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
104458b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(r);
104558b83586Sxypron.glpk@gmx.de 
104658b83586Sxypron.glpk@gmx.de 	/* If an error occured undo all changes. */
104758b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
104858b83586Sxypron.glpk@gmx.de 	for (; i; --i) {
104958b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
105058b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
105158b83586Sxypron.glpk@gmx.de 		efi_uninstall_protocol_interface(handle, protocol,
105258b83586Sxypron.glpk@gmx.de 						 protocol_interface);
105358b83586Sxypron.glpk@gmx.de 	}
105458b83586Sxypron.glpk@gmx.de 	va_end(argptr);
105558b83586Sxypron.glpk@gmx.de 
105658b83586Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
1057bee91169SAlexander Graf }
1058bee91169SAlexander Graf 
1059bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1060bee91169SAlexander Graf 			void *handle, ...)
1061bee91169SAlexander Graf {
1062bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
1063bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
1064bee91169SAlexander Graf }
1065bee91169SAlexander Graf 
1066bee91169SAlexander Graf static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1067bee91169SAlexander Graf 					       unsigned long data_size,
1068bee91169SAlexander Graf 					       uint32_t *crc32_p)
1069bee91169SAlexander Graf {
1070bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", data, data_size);
1071bee91169SAlexander Graf 	*crc32_p = crc32(0, data, data_size);
1072bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
1073bee91169SAlexander Graf }
1074bee91169SAlexander Graf 
1075bee91169SAlexander Graf static void EFIAPI efi_copy_mem(void *destination, void *source,
1076bee91169SAlexander Graf 				unsigned long length)
1077bee91169SAlexander Graf {
1078bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
1079bee91169SAlexander Graf 	memcpy(destination, source, length);
1080bee91169SAlexander Graf }
1081bee91169SAlexander Graf 
1082bee91169SAlexander Graf static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1083bee91169SAlexander Graf {
1084bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1085bee91169SAlexander Graf 	memset(buffer, value, size);
1086bee91169SAlexander Graf }
1087bee91169SAlexander Graf 
1088bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol(
1089bee91169SAlexander Graf 			void *handle, efi_guid_t *protocol,
1090bee91169SAlexander Graf 			void **protocol_interface, void *agent_handle,
1091bee91169SAlexander Graf 			void *controller_handle, uint32_t attributes)
1092bee91169SAlexander Graf {
1093bee91169SAlexander Graf 	struct list_head *lhandle;
1094bee91169SAlexander Graf 	int i;
109569baec67Sxypron.glpk@gmx.de 	efi_status_t r = EFI_INVALID_PARAMETER;
1096bee91169SAlexander Graf 
1097bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1098bee91169SAlexander Graf 		  protocol_interface, agent_handle, controller_handle,
1099bee91169SAlexander Graf 		  attributes);
1100b5349f74Sxypron.glpk@gmx.de 
110169baec67Sxypron.glpk@gmx.de 	if (!handle || !protocol ||
110269baec67Sxypron.glpk@gmx.de 	    (!protocol_interface && attributes !=
110369baec67Sxypron.glpk@gmx.de 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
110469baec67Sxypron.glpk@gmx.de 		goto out;
110569baec67Sxypron.glpk@gmx.de 	}
110669baec67Sxypron.glpk@gmx.de 
110769baec67Sxypron.glpk@gmx.de 	switch (attributes) {
110869baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
110969baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
111069baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
111169baec67Sxypron.glpk@gmx.de 		break;
111269baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
111369baec67Sxypron.glpk@gmx.de 		if (controller_handle == handle)
111469baec67Sxypron.glpk@gmx.de 			goto out;
111569baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
111669baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
111769baec67Sxypron.glpk@gmx.de 		if (controller_handle == NULL)
111869baec67Sxypron.glpk@gmx.de 			goto out;
111969baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
112069baec67Sxypron.glpk@gmx.de 		if (agent_handle == NULL)
112169baec67Sxypron.glpk@gmx.de 			goto out;
112269baec67Sxypron.glpk@gmx.de 		break;
112369baec67Sxypron.glpk@gmx.de 	default:
1124b5349f74Sxypron.glpk@gmx.de 		goto out;
1125b5349f74Sxypron.glpk@gmx.de 	}
1126b5349f74Sxypron.glpk@gmx.de 
1127bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
1128bee91169SAlexander Graf 		struct efi_object *efiobj;
1129bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
1130bee91169SAlexander Graf 
1131bee91169SAlexander Graf 		if (efiobj->handle != handle)
1132bee91169SAlexander Graf 			continue;
1133bee91169SAlexander Graf 
1134bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1135bee91169SAlexander Graf 			struct efi_handler *handler = &efiobj->protocols[i];
1136bee91169SAlexander Graf 			const efi_guid_t *hprotocol = handler->guid;
1137bee91169SAlexander Graf 			if (!hprotocol)
11384b6ed0d7Sxypron.glpk@gmx.de 				continue;
1139bee91169SAlexander Graf 			if (!guidcmp(hprotocol, protocol)) {
1140b5349f74Sxypron.glpk@gmx.de 				if (attributes !=
1141b5349f74Sxypron.glpk@gmx.de 				    EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1142b5349f74Sxypron.glpk@gmx.de 					*protocol_interface =
1143b5349f74Sxypron.glpk@gmx.de 						handler->protocol_interface;
1144b5349f74Sxypron.glpk@gmx.de 				}
1145b5349f74Sxypron.glpk@gmx.de 				r = EFI_SUCCESS;
1146bee91169SAlexander Graf 				goto out;
1147bee91169SAlexander Graf 			}
1148bee91169SAlexander Graf 		}
114969baec67Sxypron.glpk@gmx.de 		goto unsupported;
1150bee91169SAlexander Graf 	}
1151bee91169SAlexander Graf 
115269baec67Sxypron.glpk@gmx.de unsupported:
115369baec67Sxypron.glpk@gmx.de 	r = EFI_UNSUPPORTED;
1154bee91169SAlexander Graf out:
1155bee91169SAlexander Graf 	return EFI_EXIT(r);
1156bee91169SAlexander Graf }
1157bee91169SAlexander Graf 
1158bee91169SAlexander Graf static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1159bee91169SAlexander Graf 					       efi_guid_t *protocol,
1160bee91169SAlexander Graf 					       void **protocol_interface)
1161bee91169SAlexander Graf {
11628e1d329fSxypron.glpk@gmx.de 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
11638e1d329fSxypron.glpk@gmx.de 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1164bee91169SAlexander Graf }
1165bee91169SAlexander Graf 
1166bee91169SAlexander Graf static const struct efi_boot_services efi_boot_services = {
1167bee91169SAlexander Graf 	.hdr = {
1168bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1169bee91169SAlexander Graf 	},
1170bee91169SAlexander Graf 	.raise_tpl = efi_raise_tpl,
1171bee91169SAlexander Graf 	.restore_tpl = efi_restore_tpl,
1172bee91169SAlexander Graf 	.allocate_pages = efi_allocate_pages_ext,
1173bee91169SAlexander Graf 	.free_pages = efi_free_pages_ext,
1174bee91169SAlexander Graf 	.get_memory_map = efi_get_memory_map_ext,
1175ead1274bSStefan Brüns 	.allocate_pool = efi_allocate_pool_ext,
117642417bc8SStefan Brüns 	.free_pool = efi_free_pool_ext,
117749deb455Sxypron.glpk@gmx.de 	.create_event = efi_create_event_ext,
1178bfc72462Sxypron.glpk@gmx.de 	.set_timer = efi_set_timer_ext,
1179bee91169SAlexander Graf 	.wait_for_event = efi_wait_for_event,
1180c6841592Sxypron.glpk@gmx.de 	.signal_event = efi_signal_event_ext,
1181bee91169SAlexander Graf 	.close_event = efi_close_event,
1182bee91169SAlexander Graf 	.check_event = efi_check_event,
11838bee5a3cSxypron.glpk@gmx.de 	.install_protocol_interface = efi_install_protocol_interface_ext,
1184bee91169SAlexander Graf 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
11853d8e1456Sxypron.glpk@gmx.de 	.uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1186bee91169SAlexander Graf 	.handle_protocol = efi_handle_protocol,
1187bee91169SAlexander Graf 	.reserved = NULL,
1188bee91169SAlexander Graf 	.register_protocol_notify = efi_register_protocol_notify,
118926329584Sxypron.glpk@gmx.de 	.locate_handle = efi_locate_handle_ext,
1190bee91169SAlexander Graf 	.locate_device_path = efi_locate_device_path,
1191488bf12dSAlexander Graf 	.install_configuration_table = efi_install_configuration_table_ext,
1192bee91169SAlexander Graf 	.load_image = efi_load_image,
1193bee91169SAlexander Graf 	.start_image = efi_start_image,
1194a86aeaf2SAlexander Graf 	.exit = efi_exit,
1195bee91169SAlexander Graf 	.unload_image = efi_unload_image,
1196bee91169SAlexander Graf 	.exit_boot_services = efi_exit_boot_services,
1197bee91169SAlexander Graf 	.get_next_monotonic_count = efi_get_next_monotonic_count,
1198bee91169SAlexander Graf 	.stall = efi_stall,
1199bee91169SAlexander Graf 	.set_watchdog_timer = efi_set_watchdog_timer,
1200bee91169SAlexander Graf 	.connect_controller = efi_connect_controller,
1201bee91169SAlexander Graf 	.disconnect_controller = efi_disconnect_controller,
1202bee91169SAlexander Graf 	.open_protocol = efi_open_protocol,
1203bee91169SAlexander Graf 	.close_protocol = efi_close_protocol,
1204bee91169SAlexander Graf 	.open_protocol_information = efi_open_protocol_information,
1205bee91169SAlexander Graf 	.protocols_per_handle = efi_protocols_per_handle,
1206bee91169SAlexander Graf 	.locate_handle_buffer = efi_locate_handle_buffer,
1207bee91169SAlexander Graf 	.locate_protocol = efi_locate_protocol,
1208bee91169SAlexander Graf 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1209bee91169SAlexander Graf 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1210bee91169SAlexander Graf 	.calculate_crc32 = efi_calculate_crc32,
1211bee91169SAlexander Graf 	.copy_mem = efi_copy_mem,
1212bee91169SAlexander Graf 	.set_mem = efi_set_mem,
1213bee91169SAlexander Graf };
1214bee91169SAlexander Graf 
1215bee91169SAlexander Graf 
12163c63db9cSAlexander Graf static uint16_t __efi_runtime_data firmware_vendor[] =
1217bee91169SAlexander Graf 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
1218bee91169SAlexander Graf 
12193c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
1220bee91169SAlexander Graf 	.hdr = {
1221bee91169SAlexander Graf 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
1222bee91169SAlexander Graf 		.revision = 0x20005, /* 2.5 */
1223bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1224bee91169SAlexander Graf 	},
1225bee91169SAlexander Graf 	.fw_vendor = (long)firmware_vendor,
1226bee91169SAlexander Graf 	.con_in = (void*)&efi_con_in,
1227bee91169SAlexander Graf 	.con_out = (void*)&efi_con_out,
1228bee91169SAlexander Graf 	.std_err = (void*)&efi_con_out,
1229bee91169SAlexander Graf 	.runtime = (void*)&efi_runtime_services,
1230bee91169SAlexander Graf 	.boottime = (void*)&efi_boot_services,
1231bee91169SAlexander Graf 	.nr_tables = 0,
1232bee91169SAlexander Graf 	.tables = (void*)efi_conf_table,
1233bee91169SAlexander Graf };
1234