xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_boottime.c (revision a095aadffa96f3814d5605792674a6d64951db51)
1bee91169SAlexander Graf /*
2bee91169SAlexander Graf  *  EFI application boot time services
3bee91169SAlexander Graf  *
4bee91169SAlexander Graf  *  Copyright (c) 2016 Alexander Graf
5bee91169SAlexander Graf  *
6bee91169SAlexander Graf  *  SPDX-License-Identifier:     GPL-2.0+
7bee91169SAlexander Graf  */
8bee91169SAlexander Graf 
9bee91169SAlexander Graf #include <common.h>
10bee91169SAlexander Graf #include <efi_loader.h>
11bee91169SAlexander Graf #include <malloc.h>
12bee91169SAlexander Graf #include <asm/global_data.h>
13bee91169SAlexander Graf #include <libfdt_env.h>
14bee91169SAlexander Graf #include <u-boot/crc.h>
15bee91169SAlexander Graf #include <bootm.h>
16bee91169SAlexander Graf #include <inttypes.h>
17bee91169SAlexander Graf #include <watchdog.h>
18bee91169SAlexander Graf 
19bee91169SAlexander Graf DECLARE_GLOBAL_DATA_PTR;
20bee91169SAlexander Graf 
21bee91169SAlexander Graf /* This list contains all the EFI objects our payload has access to */
22bee91169SAlexander Graf LIST_HEAD(efi_obj_list);
23bee91169SAlexander Graf 
24bee91169SAlexander Graf /*
25bee91169SAlexander Graf  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26bee91169SAlexander Graf  * we need to do trickery with caches. Since we don't want to break the EFI
27bee91169SAlexander Graf  * aware boot path, only apply hacks when loading exiting directly (breaking
28bee91169SAlexander Graf  * direct Linux EFI booting along the way - oh well).
29bee91169SAlexander Graf  */
30bee91169SAlexander Graf static bool efi_is_direct_boot = true;
31bee91169SAlexander Graf 
32bee91169SAlexander Graf /*
33bee91169SAlexander Graf  * EFI can pass arbitrary additional "tables" containing vendor specific
34bee91169SAlexander Graf  * information to the payload. One such table is the FDT table which contains
35bee91169SAlexander Graf  * a pointer to a flattened device tree blob.
36bee91169SAlexander Graf  *
37bee91169SAlexander Graf  * In most cases we want to pass an FDT to the payload, so reserve one slot of
38bee91169SAlexander Graf  * config table space for it. The pointer gets populated by do_bootefi_exec().
39bee91169SAlexander Graf  */
403c63db9cSAlexander Graf static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
41bee91169SAlexander Graf 
4265e4c0b1SSimon Glass #ifdef CONFIG_ARM
43bee91169SAlexander Graf /*
44bee91169SAlexander Graf  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45bee91169SAlexander Graf  * fixed when compiling U-Boot. However, the payload does not know about that
46bee91169SAlexander Graf  * restriction so we need to manually swap its and our view of that register on
47bee91169SAlexander Graf  * EFI callback entry/exit.
48bee91169SAlexander Graf  */
49bee91169SAlexander Graf static volatile void *efi_gd, *app_gd;
5065e4c0b1SSimon Glass #endif
51bee91169SAlexander Graf 
52bee91169SAlexander Graf /* Called from do_bootefi_exec() */
53bee91169SAlexander Graf void efi_save_gd(void)
54bee91169SAlexander Graf {
5565e4c0b1SSimon Glass #ifdef CONFIG_ARM
56bee91169SAlexander Graf 	efi_gd = gd;
5765e4c0b1SSimon Glass #endif
58bee91169SAlexander Graf }
59bee91169SAlexander Graf 
60bee91169SAlexander Graf /* Called on every callback entry */
61bee91169SAlexander Graf void efi_restore_gd(void)
62bee91169SAlexander Graf {
6365e4c0b1SSimon Glass #ifdef CONFIG_ARM
64bee91169SAlexander Graf 	/* Only restore if we're already in EFI context */
65bee91169SAlexander Graf 	if (!efi_gd)
66bee91169SAlexander Graf 		return;
67bee91169SAlexander Graf 
68bee91169SAlexander Graf 	if (gd != efi_gd)
69bee91169SAlexander Graf 		app_gd = gd;
70bee91169SAlexander Graf 	gd = efi_gd;
7165e4c0b1SSimon Glass #endif
72bee91169SAlexander Graf }
73bee91169SAlexander Graf 
74bee91169SAlexander Graf /* Called on every callback exit */
75bee91169SAlexander Graf efi_status_t efi_exit_func(efi_status_t ret)
76bee91169SAlexander Graf {
7765e4c0b1SSimon Glass #ifdef CONFIG_ARM
78bee91169SAlexander Graf 	gd = app_gd;
7965e4c0b1SSimon Glass #endif
8065e4c0b1SSimon Glass 
81bee91169SAlexander Graf 	return ret;
82bee91169SAlexander Graf }
83bee91169SAlexander Graf 
848787b02eSxypron.glpk@gmx.de /* Low 32 bit */
858787b02eSxypron.glpk@gmx.de #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
868787b02eSxypron.glpk@gmx.de /* High 32 bit */
878787b02eSxypron.glpk@gmx.de #define EFI_HIGH32(a) (a >> 32)
888787b02eSxypron.glpk@gmx.de 
898787b02eSxypron.glpk@gmx.de /*
908787b02eSxypron.glpk@gmx.de  * 64bit division by 10 implemented as multiplication by 1 / 10
918787b02eSxypron.glpk@gmx.de  *
928787b02eSxypron.glpk@gmx.de  * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
938787b02eSxypron.glpk@gmx.de  */
948787b02eSxypron.glpk@gmx.de #define EFI_TENTH 0x199999999999999A
958787b02eSxypron.glpk@gmx.de static u64 efi_div10(u64 a)
968787b02eSxypron.glpk@gmx.de {
978787b02eSxypron.glpk@gmx.de 	u64 prod;
988787b02eSxypron.glpk@gmx.de 	u64 rem;
998787b02eSxypron.glpk@gmx.de 	u64 ret;
1008787b02eSxypron.glpk@gmx.de 
1018787b02eSxypron.glpk@gmx.de 	ret  = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
1028787b02eSxypron.glpk@gmx.de 	prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
1038787b02eSxypron.glpk@gmx.de 	rem  = EFI_LOW32(prod);
1048787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
1058787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
1068787b02eSxypron.glpk@gmx.de 	rem += EFI_LOW32(prod);
1078787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(prod);
1088787b02eSxypron.glpk@gmx.de 	prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
1098787b02eSxypron.glpk@gmx.de 	rem += EFI_HIGH32(prod);
1108787b02eSxypron.glpk@gmx.de 	ret += EFI_HIGH32(rem);
1118787b02eSxypron.glpk@gmx.de 	/* Round to nearest integer */
1128787b02eSxypron.glpk@gmx.de 	if (rem >= (1 << 31))
1138787b02eSxypron.glpk@gmx.de 		++ret;
1148787b02eSxypron.glpk@gmx.de 	return ret;
1158787b02eSxypron.glpk@gmx.de }
1168787b02eSxypron.glpk@gmx.de 
11791be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event)
118c6841592Sxypron.glpk@gmx.de {
119c6841592Sxypron.glpk@gmx.de 	if (event->signaled)
120c6841592Sxypron.glpk@gmx.de 		return;
121c6841592Sxypron.glpk@gmx.de 	event->signaled = 1;
122c6841592Sxypron.glpk@gmx.de 	if (event->type & EVT_NOTIFY_SIGNAL) {
123*a095aadfSRob Clark 		EFI_CALL(event->notify_function(event, event->notify_context));
124c6841592Sxypron.glpk@gmx.de 	}
125c6841592Sxypron.glpk@gmx.de }
126c6841592Sxypron.glpk@gmx.de 
127bee91169SAlexander Graf static efi_status_t efi_unsupported(const char *funcname)
128bee91169SAlexander Graf {
129edcef3baSAlexander Graf 	debug("EFI: App called into unimplemented function %s\n", funcname);
130bee91169SAlexander Graf 	return EFI_EXIT(EFI_UNSUPPORTED);
131bee91169SAlexander Graf }
132bee91169SAlexander Graf 
133503f2695Sxypron.glpk@gmx.de static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
134bee91169SAlexander Graf {
135503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", new_tpl);
136bee91169SAlexander Graf 	return EFI_EXIT(0);
137bee91169SAlexander Graf }
138bee91169SAlexander Graf 
139503f2695Sxypron.glpk@gmx.de static void EFIAPI efi_restore_tpl(UINTN old_tpl)
140bee91169SAlexander Graf {
141503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", old_tpl);
142b5104821SRob Clark 	efi_unsupported(__func__);
143bee91169SAlexander Graf }
144bee91169SAlexander Graf 
1456e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
146bee91169SAlexander Graf 						  unsigned long pages,
147bee91169SAlexander Graf 						  uint64_t *memory)
148bee91169SAlexander Graf {
149bee91169SAlexander Graf 	efi_status_t r;
150bee91169SAlexander Graf 
151bee91169SAlexander Graf 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
152bee91169SAlexander Graf 	r = efi_allocate_pages(type, memory_type, pages, memory);
153bee91169SAlexander Graf 	return EFI_EXIT(r);
154bee91169SAlexander Graf }
155bee91169SAlexander Graf 
1566e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
1576e0bf8d8SMasahiro Yamada 					      unsigned long pages)
158bee91169SAlexander Graf {
159bee91169SAlexander Graf 	efi_status_t r;
160bee91169SAlexander Graf 
161bee91169SAlexander Graf 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
162bee91169SAlexander Graf 	r = efi_free_pages(memory, pages);
163bee91169SAlexander Graf 	return EFI_EXIT(r);
164bee91169SAlexander Graf }
165bee91169SAlexander Graf 
1666e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
1676e0bf8d8SMasahiro Yamada 					unsigned long *memory_map_size,
168bee91169SAlexander Graf 					struct efi_mem_desc *memory_map,
169bee91169SAlexander Graf 					unsigned long *map_key,
170bee91169SAlexander Graf 					unsigned long *descriptor_size,
171bee91169SAlexander Graf 					uint32_t *descriptor_version)
172bee91169SAlexander Graf {
173bee91169SAlexander Graf 	efi_status_t r;
174bee91169SAlexander Graf 
175bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
176bee91169SAlexander Graf 		  map_key, descriptor_size, descriptor_version);
177bee91169SAlexander Graf 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
178bee91169SAlexander Graf 			       descriptor_size, descriptor_version);
179bee91169SAlexander Graf 	return EFI_EXIT(r);
180bee91169SAlexander Graf }
181bee91169SAlexander Graf 
182ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
183ead1274bSStefan Brüns 						 unsigned long size,
184bee91169SAlexander Graf 						 void **buffer)
185bee91169SAlexander Graf {
1861cd29f0aSAlexander Graf 	efi_status_t r;
1871cd29f0aSAlexander Graf 
1881cd29f0aSAlexander Graf 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
189ead1274bSStefan Brüns 	r = efi_allocate_pool(pool_type, size, buffer);
1901cd29f0aSAlexander Graf 	return EFI_EXIT(r);
191bee91169SAlexander Graf }
192bee91169SAlexander Graf 
19342417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
194bee91169SAlexander Graf {
1951cd29f0aSAlexander Graf 	efi_status_t r;
1961cd29f0aSAlexander Graf 
1971cd29f0aSAlexander Graf 	EFI_ENTRY("%p", buffer);
19842417bc8SStefan Brüns 	r = efi_free_pool(buffer);
1991cd29f0aSAlexander Graf 	return EFI_EXIT(r);
200bee91169SAlexander Graf }
201bee91169SAlexander Graf 
202bee91169SAlexander Graf /*
203c6841592Sxypron.glpk@gmx.de  * Our event capabilities are very limited. Only a small limited
204c6841592Sxypron.glpk@gmx.de  * number of events is allowed to coexist.
205bee91169SAlexander Graf  */
206c6841592Sxypron.glpk@gmx.de static struct efi_event efi_events[16];
207bee91169SAlexander Graf 
208b521d29eSxypron.glpk@gmx.de efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
2092fd945feSxypron.glpk@gmx.de 			      void (EFIAPI *notify_function) (
2102fd945feSxypron.glpk@gmx.de 					struct efi_event *event,
211e275458cSSimon Glass 					void *context),
2122fd945feSxypron.glpk@gmx.de 			      void *notify_context, struct efi_event **event)
213bee91169SAlexander Graf {
214c6841592Sxypron.glpk@gmx.de 	int i;
215c6841592Sxypron.glpk@gmx.de 
216a95343b8SJonathan Gray 	if (event == NULL)
21749deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
218a95343b8SJonathan Gray 
219a95343b8SJonathan Gray 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
22049deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
221a95343b8SJonathan Gray 
222a95343b8SJonathan Gray 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
223a95343b8SJonathan Gray 	    notify_function == NULL)
22449deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
225a95343b8SJonathan Gray 
226c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
227c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].type)
228c6841592Sxypron.glpk@gmx.de 			continue;
229c6841592Sxypron.glpk@gmx.de 		efi_events[i].type = type;
230c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_tpl = notify_tpl;
231c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_function = notify_function;
232c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_context = notify_context;
233c6841592Sxypron.glpk@gmx.de 		/* Disable timers on bootup */
234c6841592Sxypron.glpk@gmx.de 		efi_events[i].trigger_next = -1ULL;
235c6841592Sxypron.glpk@gmx.de 		efi_events[i].signaled = 0;
236c6841592Sxypron.glpk@gmx.de 		*event = &efi_events[i];
23749deb455Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
238bee91169SAlexander Graf 	}
23949deb455Sxypron.glpk@gmx.de 	return EFI_OUT_OF_RESOURCES;
240c6841592Sxypron.glpk@gmx.de }
241bee91169SAlexander Graf 
24249deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
243b521d29eSxypron.glpk@gmx.de 			uint32_t type, UINTN notify_tpl,
24449deb455Sxypron.glpk@gmx.de 			void (EFIAPI *notify_function) (
24549deb455Sxypron.glpk@gmx.de 					struct efi_event *event,
24649deb455Sxypron.glpk@gmx.de 					void *context),
24749deb455Sxypron.glpk@gmx.de 			void *notify_context, struct efi_event **event)
24849deb455Sxypron.glpk@gmx.de {
24949deb455Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
25049deb455Sxypron.glpk@gmx.de 		  notify_context);
25149deb455Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
25249deb455Sxypron.glpk@gmx.de 					 notify_context, event));
25349deb455Sxypron.glpk@gmx.de }
25449deb455Sxypron.glpk@gmx.de 
25549deb455Sxypron.glpk@gmx.de 
256bee91169SAlexander Graf /*
257bee91169SAlexander Graf  * Our timers have to work without interrupts, so we check whenever keyboard
258bee91169SAlexander Graf  * input or disk accesses happen if enough time elapsed for it to fire.
259bee91169SAlexander Graf  */
260bee91169SAlexander Graf void efi_timer_check(void)
261bee91169SAlexander Graf {
262c6841592Sxypron.glpk@gmx.de 	int i;
263bee91169SAlexander Graf 	u64 now = timer_get_us();
264bee91169SAlexander Graf 
265c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
266c6841592Sxypron.glpk@gmx.de 		if (!efi_events[i].type ||
267c6841592Sxypron.glpk@gmx.de 		    !(efi_events[i].type & EVT_TIMER) ||
268c6841592Sxypron.glpk@gmx.de 		    efi_events[i].trigger_type == EFI_TIMER_STOP ||
269c6841592Sxypron.glpk@gmx.de 		    now < efi_events[i].trigger_next)
270c6841592Sxypron.glpk@gmx.de 			continue;
271c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
272c6841592Sxypron.glpk@gmx.de 			efi_events[i].trigger_next +=
2738787b02eSxypron.glpk@gmx.de 				efi_events[i].trigger_time;
274c6841592Sxypron.glpk@gmx.de 			efi_events[i].signaled = 0;
275bee91169SAlexander Graf 		}
276c6841592Sxypron.glpk@gmx.de 		efi_signal_event(&efi_events[i]);
277c6841592Sxypron.glpk@gmx.de 	}
278bee91169SAlexander Graf 	WATCHDOG_RESET();
279bee91169SAlexander Graf }
280bee91169SAlexander Graf 
281b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
282bee91169SAlexander Graf 			   uint64_t trigger_time)
283bee91169SAlexander Graf {
284c6841592Sxypron.glpk@gmx.de 	int i;
285bee91169SAlexander Graf 
2868787b02eSxypron.glpk@gmx.de 	/*
2878787b02eSxypron.glpk@gmx.de 	 * The parameter defines a multiple of 100ns.
2888787b02eSxypron.glpk@gmx.de 	 * We use multiples of 1000ns. So divide by 10.
2898787b02eSxypron.glpk@gmx.de 	 */
2908787b02eSxypron.glpk@gmx.de 	trigger_time = efi_div10(trigger_time);
291bee91169SAlexander Graf 
292c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
293c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
294c6841592Sxypron.glpk@gmx.de 			continue;
295bee91169SAlexander Graf 
296c6841592Sxypron.glpk@gmx.de 		if (!(event->type & EVT_TIMER))
297c6841592Sxypron.glpk@gmx.de 			break;
298bee91169SAlexander Graf 		switch (type) {
299bee91169SAlexander Graf 		case EFI_TIMER_STOP:
300c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
301bee91169SAlexander Graf 			break;
302bee91169SAlexander Graf 		case EFI_TIMER_PERIODIC:
303bee91169SAlexander Graf 		case EFI_TIMER_RELATIVE:
304c6841592Sxypron.glpk@gmx.de 			event->trigger_next =
3058787b02eSxypron.glpk@gmx.de 				timer_get_us() + trigger_time;
306bee91169SAlexander Graf 			break;
307bee91169SAlexander Graf 		default:
308bfc72462Sxypron.glpk@gmx.de 			return EFI_INVALID_PARAMETER;
309bee91169SAlexander Graf 		}
310c6841592Sxypron.glpk@gmx.de 		event->trigger_type = type;
311c6841592Sxypron.glpk@gmx.de 		event->trigger_time = trigger_time;
312bfc72462Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
313bee91169SAlexander Graf 	}
314bfc72462Sxypron.glpk@gmx.de 	return EFI_INVALID_PARAMETER;
315bfc72462Sxypron.glpk@gmx.de }
316bfc72462Sxypron.glpk@gmx.de 
317b521d29eSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
318b521d29eSxypron.glpk@gmx.de 					     enum efi_timer_delay type,
319bfc72462Sxypron.glpk@gmx.de 					     uint64_t trigger_time)
320bfc72462Sxypron.glpk@gmx.de {
321bfc72462Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
322bfc72462Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
323c6841592Sxypron.glpk@gmx.de }
324bee91169SAlexander Graf 
325bee91169SAlexander Graf static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
3262fd945feSxypron.glpk@gmx.de 					      struct efi_event **event,
3272fd945feSxypron.glpk@gmx.de 					      unsigned long *index)
328bee91169SAlexander Graf {
329c6841592Sxypron.glpk@gmx.de 	int i, j;
330bee91169SAlexander Graf 
331bee91169SAlexander Graf 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
332bee91169SAlexander Graf 
333c6841592Sxypron.glpk@gmx.de 	/* Check parameters */
334c6841592Sxypron.glpk@gmx.de 	if (!num_events || !event)
335c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
336c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < num_events; ++i) {
337c6841592Sxypron.glpk@gmx.de 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
338c6841592Sxypron.glpk@gmx.de 			if (event[i] == &efi_events[j])
339c6841592Sxypron.glpk@gmx.de 				goto known_event;
340c6841592Sxypron.glpk@gmx.de 		}
341c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
342c6841592Sxypron.glpk@gmx.de known_event:
343c6841592Sxypron.glpk@gmx.de 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
344c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
345c6841592Sxypron.glpk@gmx.de 	}
346c6841592Sxypron.glpk@gmx.de 
347c6841592Sxypron.glpk@gmx.de 	/* Wait for signal */
348c6841592Sxypron.glpk@gmx.de 	for (;;) {
349c6841592Sxypron.glpk@gmx.de 		for (i = 0; i < num_events; ++i) {
350c6841592Sxypron.glpk@gmx.de 			if (event[i]->signaled)
351c6841592Sxypron.glpk@gmx.de 				goto out;
352c6841592Sxypron.glpk@gmx.de 		}
353c6841592Sxypron.glpk@gmx.de 		/* Allow events to occur. */
354bee91169SAlexander Graf 		efi_timer_check();
355c6841592Sxypron.glpk@gmx.de 	}
356c6841592Sxypron.glpk@gmx.de 
357c6841592Sxypron.glpk@gmx.de out:
358c6841592Sxypron.glpk@gmx.de 	/*
359c6841592Sxypron.glpk@gmx.de 	 * Reset the signal which is passed to the caller to allow periodic
360c6841592Sxypron.glpk@gmx.de 	 * events to occur.
361c6841592Sxypron.glpk@gmx.de 	 */
362c6841592Sxypron.glpk@gmx.de 	event[i]->signaled = 0;
363c6841592Sxypron.glpk@gmx.de 	if (index)
364c6841592Sxypron.glpk@gmx.de 		*index = i;
365bee91169SAlexander Graf 
366bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
367bee91169SAlexander Graf }
368bee91169SAlexander Graf 
369c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
370bee91169SAlexander Graf {
371c6841592Sxypron.glpk@gmx.de 	int i;
372c6841592Sxypron.glpk@gmx.de 
373bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
374c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
375c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
376c6841592Sxypron.glpk@gmx.de 			continue;
377c6841592Sxypron.glpk@gmx.de 		efi_signal_event(event);
378c6841592Sxypron.glpk@gmx.de 		break;
379c6841592Sxypron.glpk@gmx.de 	}
380bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
381bee91169SAlexander Graf }
382bee91169SAlexander Graf 
3832fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
384bee91169SAlexander Graf {
385c6841592Sxypron.glpk@gmx.de 	int i;
386c6841592Sxypron.glpk@gmx.de 
387bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
388c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
389c6841592Sxypron.glpk@gmx.de 		if (event == &efi_events[i]) {
390c6841592Sxypron.glpk@gmx.de 			event->type = 0;
391c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
392c6841592Sxypron.glpk@gmx.de 			event->signaled = 0;
393bee91169SAlexander Graf 			return EFI_EXIT(EFI_SUCCESS);
394bee91169SAlexander Graf 		}
395c6841592Sxypron.glpk@gmx.de 	}
396c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
397c6841592Sxypron.glpk@gmx.de }
398bee91169SAlexander Graf 
3992fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
400bee91169SAlexander Graf {
401c6841592Sxypron.glpk@gmx.de 	int i;
402c6841592Sxypron.glpk@gmx.de 
403bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
404c6841592Sxypron.glpk@gmx.de 	efi_timer_check();
405c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
406c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
407c6841592Sxypron.glpk@gmx.de 			continue;
408c6841592Sxypron.glpk@gmx.de 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
409c6841592Sxypron.glpk@gmx.de 			break;
410c6841592Sxypron.glpk@gmx.de 		if (event->signaled)
411c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_SUCCESS);
412bee91169SAlexander Graf 		return EFI_EXIT(EFI_NOT_READY);
413bee91169SAlexander Graf 	}
414c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
415c6841592Sxypron.glpk@gmx.de }
416bee91169SAlexander Graf 
417bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
418bee91169SAlexander Graf 			efi_guid_t *protocol, int protocol_interface_type,
419bee91169SAlexander Graf 			void *protocol_interface)
420bee91169SAlexander Graf {
421e0549f8aSxypron.glpk@gmx.de 	struct list_head *lhandle;
422e0549f8aSxypron.glpk@gmx.de 	int i;
423e0549f8aSxypron.glpk@gmx.de 	efi_status_t r;
424e0549f8aSxypron.glpk@gmx.de 
425e0549f8aSxypron.glpk@gmx.de 	if (!handle || !protocol ||
426e0549f8aSxypron.glpk@gmx.de 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
427e0549f8aSxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
428e0549f8aSxypron.glpk@gmx.de 		goto out;
429bee91169SAlexander Graf 	}
430e0549f8aSxypron.glpk@gmx.de 
431e0549f8aSxypron.glpk@gmx.de 	/* Create new handle if requested. */
432e0549f8aSxypron.glpk@gmx.de 	if (!*handle) {
433e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
434e0549f8aSxypron.glpk@gmx.de 		goto out;
435e0549f8aSxypron.glpk@gmx.de 	}
436e0549f8aSxypron.glpk@gmx.de 	/* Find object. */
437e0549f8aSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
438e0549f8aSxypron.glpk@gmx.de 		struct efi_object *efiobj;
439e0549f8aSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
440e0549f8aSxypron.glpk@gmx.de 
441e0549f8aSxypron.glpk@gmx.de 		if (efiobj->handle != *handle)
442e0549f8aSxypron.glpk@gmx.de 			continue;
443e0549f8aSxypron.glpk@gmx.de 		/* Check if protocol is already installed on the handle. */
444e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
445e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
446e0549f8aSxypron.glpk@gmx.de 
447e0549f8aSxypron.glpk@gmx.de 			if (!handler->guid)
448e0549f8aSxypron.glpk@gmx.de 				continue;
449e0549f8aSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
450e0549f8aSxypron.glpk@gmx.de 				r = EFI_INVALID_PARAMETER;
451e0549f8aSxypron.glpk@gmx.de 				goto out;
452e0549f8aSxypron.glpk@gmx.de 			}
453e0549f8aSxypron.glpk@gmx.de 		}
454e0549f8aSxypron.glpk@gmx.de 		/* Install protocol in first empty slot. */
455e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
456e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
457e0549f8aSxypron.glpk@gmx.de 
458e0549f8aSxypron.glpk@gmx.de 			if (handler->guid)
459e0549f8aSxypron.glpk@gmx.de 				continue;
460e0549f8aSxypron.glpk@gmx.de 
461e0549f8aSxypron.glpk@gmx.de 			handler->guid = protocol;
462e0549f8aSxypron.glpk@gmx.de 			handler->protocol_interface = protocol_interface;
463e0549f8aSxypron.glpk@gmx.de 			r = EFI_SUCCESS;
464e0549f8aSxypron.glpk@gmx.de 			goto out;
465e0549f8aSxypron.glpk@gmx.de 		}
466e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
467e0549f8aSxypron.glpk@gmx.de 		goto out;
468e0549f8aSxypron.glpk@gmx.de 	}
469e0549f8aSxypron.glpk@gmx.de 	r = EFI_INVALID_PARAMETER;
470e0549f8aSxypron.glpk@gmx.de out:
4718bee5a3cSxypron.glpk@gmx.de 	return r;
4728bee5a3cSxypron.glpk@gmx.de }
4738bee5a3cSxypron.glpk@gmx.de 
4748bee5a3cSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
4758bee5a3cSxypron.glpk@gmx.de 			efi_guid_t *protocol, int protocol_interface_type,
4768bee5a3cSxypron.glpk@gmx.de 			void *protocol_interface)
4778bee5a3cSxypron.glpk@gmx.de {
4788bee5a3cSxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
4798bee5a3cSxypron.glpk@gmx.de 		  protocol_interface);
4808bee5a3cSxypron.glpk@gmx.de 
4818bee5a3cSxypron.glpk@gmx.de 	return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
4828bee5a3cSxypron.glpk@gmx.de 						       protocol_interface_type,
4838bee5a3cSxypron.glpk@gmx.de 						       protocol_interface));
484e0549f8aSxypron.glpk@gmx.de }
485e0549f8aSxypron.glpk@gmx.de 
486bee91169SAlexander Graf static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
487bee91169SAlexander Graf 			efi_guid_t *protocol, void *old_interface,
488bee91169SAlexander Graf 			void *new_interface)
489bee91169SAlexander Graf {
490bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
491bee91169SAlexander Graf 		  new_interface);
492bee91169SAlexander Graf 	return EFI_EXIT(EFI_ACCESS_DENIED);
493bee91169SAlexander Graf }
494bee91169SAlexander Graf 
495bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
496bee91169SAlexander Graf 			efi_guid_t *protocol, void *protocol_interface)
497bee91169SAlexander Graf {
4984b6ed0d7Sxypron.glpk@gmx.de 	struct list_head *lhandle;
4994b6ed0d7Sxypron.glpk@gmx.de 	int i;
5004b6ed0d7Sxypron.glpk@gmx.de 	efi_status_t r = EFI_NOT_FOUND;
5014b6ed0d7Sxypron.glpk@gmx.de 
5024b6ed0d7Sxypron.glpk@gmx.de 	if (!handle || !protocol) {
5034b6ed0d7Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
5044b6ed0d7Sxypron.glpk@gmx.de 		goto out;
5054b6ed0d7Sxypron.glpk@gmx.de 	}
5064b6ed0d7Sxypron.glpk@gmx.de 
5074b6ed0d7Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
5084b6ed0d7Sxypron.glpk@gmx.de 		struct efi_object *efiobj;
5094b6ed0d7Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
5104b6ed0d7Sxypron.glpk@gmx.de 
5114b6ed0d7Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
5124b6ed0d7Sxypron.glpk@gmx.de 			continue;
5134b6ed0d7Sxypron.glpk@gmx.de 
5144b6ed0d7Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
5154b6ed0d7Sxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
5164b6ed0d7Sxypron.glpk@gmx.de 			const efi_guid_t *hprotocol = handler->guid;
5174b6ed0d7Sxypron.glpk@gmx.de 
5184b6ed0d7Sxypron.glpk@gmx.de 			if (!hprotocol)
5194b6ed0d7Sxypron.glpk@gmx.de 				continue;
5204b6ed0d7Sxypron.glpk@gmx.de 			if (!guidcmp(hprotocol, protocol)) {
5214b6ed0d7Sxypron.glpk@gmx.de 				if (handler->protocol_interface) {
5224b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_ACCESS_DENIED;
5234b6ed0d7Sxypron.glpk@gmx.de 				} else {
5244b6ed0d7Sxypron.glpk@gmx.de 					handler->guid = 0;
5254b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_SUCCESS;
5264b6ed0d7Sxypron.glpk@gmx.de 				}
5274b6ed0d7Sxypron.glpk@gmx.de 				goto out;
5284b6ed0d7Sxypron.glpk@gmx.de 			}
5294b6ed0d7Sxypron.glpk@gmx.de 		}
5304b6ed0d7Sxypron.glpk@gmx.de 	}
5314b6ed0d7Sxypron.glpk@gmx.de 
5324b6ed0d7Sxypron.glpk@gmx.de out:
5333d8e1456Sxypron.glpk@gmx.de 	return r;
5343d8e1456Sxypron.glpk@gmx.de }
5353d8e1456Sxypron.glpk@gmx.de 
5363d8e1456Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
5373d8e1456Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *protocol_interface)
5383d8e1456Sxypron.glpk@gmx.de {
5393d8e1456Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
5403d8e1456Sxypron.glpk@gmx.de 
5413d8e1456Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
5423d8e1456Sxypron.glpk@gmx.de 							 protocol_interface));
543bee91169SAlexander Graf }
544bee91169SAlexander Graf 
545bee91169SAlexander Graf static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
5462fd945feSxypron.glpk@gmx.de 							struct efi_event *event,
547bee91169SAlexander Graf 							void **registration)
548bee91169SAlexander Graf {
549bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
550bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
551bee91169SAlexander Graf }
552bee91169SAlexander Graf 
553bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
554bee91169SAlexander Graf 		      efi_guid_t *protocol, void *search_key,
555bee91169SAlexander Graf 		      struct efi_object *efiobj)
556bee91169SAlexander Graf {
557bee91169SAlexander Graf 	int i;
558bee91169SAlexander Graf 
559bee91169SAlexander Graf 	switch (search_type) {
560bee91169SAlexander Graf 	case all_handles:
561bee91169SAlexander Graf 		return 0;
562bee91169SAlexander Graf 	case by_register_notify:
563bee91169SAlexander Graf 		return -1;
564bee91169SAlexander Graf 	case by_protocol:
565bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
566bee91169SAlexander Graf 			const efi_guid_t *guid = efiobj->protocols[i].guid;
567bee91169SAlexander Graf 			if (guid && !guidcmp(guid, protocol))
568bee91169SAlexander Graf 				return 0;
569bee91169SAlexander Graf 		}
570bee91169SAlexander Graf 		return -1;
571bee91169SAlexander Graf 	}
572bee91169SAlexander Graf 
573bee91169SAlexander Graf 	return -1;
574bee91169SAlexander Graf }
575bee91169SAlexander Graf 
576bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle(
577bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
578bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
579bee91169SAlexander Graf 			unsigned long *buffer_size, efi_handle_t *buffer)
580bee91169SAlexander Graf {
581bee91169SAlexander Graf 	struct list_head *lhandle;
582bee91169SAlexander Graf 	unsigned long size = 0;
583bee91169SAlexander Graf 
584bee91169SAlexander Graf 	/* Count how much space we need */
585bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
586bee91169SAlexander Graf 		struct efi_object *efiobj;
587bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
588bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
589bee91169SAlexander Graf 			size += sizeof(void*);
590bee91169SAlexander Graf 		}
591bee91169SAlexander Graf 	}
592bee91169SAlexander Graf 
593bee91169SAlexander Graf 	if (*buffer_size < size) {
594bee91169SAlexander Graf 		*buffer_size = size;
59526329584Sxypron.glpk@gmx.de 		return EFI_BUFFER_TOO_SMALL;
596bee91169SAlexander Graf 	}
597bee91169SAlexander Graf 
598bee91169SAlexander Graf 	/* Then fill the array */
599bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
600bee91169SAlexander Graf 		struct efi_object *efiobj;
601bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
602bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
603bee91169SAlexander Graf 			*(buffer++) = efiobj->handle;
604bee91169SAlexander Graf 		}
605bee91169SAlexander Graf 	}
606bee91169SAlexander Graf 
607bee91169SAlexander Graf 	*buffer_size = size;
60826329584Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
60926329584Sxypron.glpk@gmx.de }
61026329584Sxypron.glpk@gmx.de 
61126329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
61226329584Sxypron.glpk@gmx.de 			enum efi_locate_search_type search_type,
61326329584Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *search_key,
61426329584Sxypron.glpk@gmx.de 			unsigned long *buffer_size, efi_handle_t *buffer)
61526329584Sxypron.glpk@gmx.de {
61626329584Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
61726329584Sxypron.glpk@gmx.de 		  buffer_size, buffer);
61826329584Sxypron.glpk@gmx.de 
61926329584Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
62026329584Sxypron.glpk@gmx.de 			buffer_size, buffer));
621bee91169SAlexander Graf }
622bee91169SAlexander Graf 
623bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
624bee91169SAlexander Graf 			struct efi_device_path **device_path,
625bee91169SAlexander Graf 			efi_handle_t *device)
626bee91169SAlexander Graf {
627bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
628bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
629bee91169SAlexander Graf }
630bee91169SAlexander Graf 
631d98cdf6aSAlexander Graf /* Collapses configuration table entries, removing index i */
632d98cdf6aSAlexander Graf static void efi_remove_configuration_table(int i)
633d98cdf6aSAlexander Graf {
634d98cdf6aSAlexander Graf 	struct efi_configuration_table *this = &efi_conf_table[i];
635d98cdf6aSAlexander Graf 	struct efi_configuration_table *next = &efi_conf_table[i+1];
636d98cdf6aSAlexander Graf 	struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
637d98cdf6aSAlexander Graf 
638d98cdf6aSAlexander Graf 	memmove(this, next, (ulong)end - (ulong)next);
639d98cdf6aSAlexander Graf 	systab.nr_tables--;
640d98cdf6aSAlexander Graf }
641d98cdf6aSAlexander Graf 
642488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
643bee91169SAlexander Graf {
644bee91169SAlexander Graf 	int i;
645bee91169SAlexander Graf 
646bee91169SAlexander Graf 	/* Check for guid override */
647bee91169SAlexander Graf 	for (i = 0; i < systab.nr_tables; i++) {
648bee91169SAlexander Graf 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
649d98cdf6aSAlexander Graf 			if (table)
650bee91169SAlexander Graf 				efi_conf_table[i].table = table;
651d98cdf6aSAlexander Graf 			else
652d98cdf6aSAlexander Graf 				efi_remove_configuration_table(i);
653488bf12dSAlexander Graf 			return EFI_SUCCESS;
654bee91169SAlexander Graf 		}
655bee91169SAlexander Graf 	}
656bee91169SAlexander Graf 
657d98cdf6aSAlexander Graf 	if (!table)
658d98cdf6aSAlexander Graf 		return EFI_NOT_FOUND;
659d98cdf6aSAlexander Graf 
660bee91169SAlexander Graf 	/* No override, check for overflow */
661bee91169SAlexander Graf 	if (i >= ARRAY_SIZE(efi_conf_table))
662488bf12dSAlexander Graf 		return EFI_OUT_OF_RESOURCES;
663bee91169SAlexander Graf 
664bee91169SAlexander Graf 	/* Add a new entry */
665bee91169SAlexander Graf 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
666bee91169SAlexander Graf 	efi_conf_table[i].table = table;
667aba5e919SAlexander Graf 	systab.nr_tables = i + 1;
668bee91169SAlexander Graf 
669488bf12dSAlexander Graf 	return EFI_SUCCESS;
670488bf12dSAlexander Graf }
671488bf12dSAlexander Graf 
672488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
673488bf12dSAlexander Graf 							       void *table)
674488bf12dSAlexander Graf {
675488bf12dSAlexander Graf 	EFI_ENTRY("%p, %p", guid, table);
676488bf12dSAlexander Graf 	return EFI_EXIT(efi_install_configuration_table(guid, table));
677bee91169SAlexander Graf }
678bee91169SAlexander Graf 
679bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
680bee91169SAlexander Graf 					  efi_handle_t parent_image,
681bee91169SAlexander Graf 					  struct efi_device_path *file_path,
682bee91169SAlexander Graf 					  void *source_buffer,
683bee91169SAlexander Graf 					  unsigned long source_size,
684bee91169SAlexander Graf 					  efi_handle_t *image_handle)
685bee91169SAlexander Graf {
686bee91169SAlexander Graf 	static struct efi_object loaded_image_info_obj = {
687bee91169SAlexander Graf 		.protocols = {
688bee91169SAlexander Graf 			{
689bee91169SAlexander Graf 				.guid = &efi_guid_loaded_image,
690bee91169SAlexander Graf 			},
691bee91169SAlexander Graf 		},
692bee91169SAlexander Graf 	};
693bee91169SAlexander Graf 	struct efi_loaded_image *info;
694bee91169SAlexander Graf 	struct efi_object *obj;
695bee91169SAlexander Graf 
696bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
697bee91169SAlexander Graf 		  file_path, source_buffer, source_size, image_handle);
698bee91169SAlexander Graf 	info = malloc(sizeof(*info));
699b5349f74Sxypron.glpk@gmx.de 	loaded_image_info_obj.protocols[0].protocol_interface = info;
700bee91169SAlexander Graf 	obj = malloc(sizeof(loaded_image_info_obj));
701bee91169SAlexander Graf 	memset(info, 0, sizeof(*info));
702bee91169SAlexander Graf 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
703bee91169SAlexander Graf 	obj->handle = info;
704bee91169SAlexander Graf 	info->file_path = file_path;
705bee91169SAlexander Graf 	info->reserved = efi_load_pe(source_buffer, info);
706bee91169SAlexander Graf 	if (!info->reserved) {
707bee91169SAlexander Graf 		free(info);
708bee91169SAlexander Graf 		free(obj);
709bee91169SAlexander Graf 		return EFI_EXIT(EFI_UNSUPPORTED);
710bee91169SAlexander Graf 	}
711bee91169SAlexander Graf 
712bee91169SAlexander Graf 	*image_handle = info;
713bee91169SAlexander Graf 	list_add_tail(&obj->link, &efi_obj_list);
714bee91169SAlexander Graf 
715bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
716bee91169SAlexander Graf }
717bee91169SAlexander Graf 
718bee91169SAlexander Graf static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
719bee91169SAlexander Graf 					   unsigned long *exit_data_size,
720bee91169SAlexander Graf 					   s16 **exit_data)
721bee91169SAlexander Graf {
722bee91169SAlexander Graf 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
723bee91169SAlexander Graf 	struct efi_loaded_image *info = image_handle;
724bee91169SAlexander Graf 
725bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
726bee91169SAlexander Graf 	entry = info->reserved;
727bee91169SAlexander Graf 
728bee91169SAlexander Graf 	efi_is_direct_boot = false;
729bee91169SAlexander Graf 
730bee91169SAlexander Graf 	/* call the image! */
731a86aeaf2SAlexander Graf 	if (setjmp(&info->exit_jmp)) {
732a86aeaf2SAlexander Graf 		/* We returned from the child image */
733a86aeaf2SAlexander Graf 		return EFI_EXIT(info->exit_status);
734a86aeaf2SAlexander Graf 	}
735a86aeaf2SAlexander Graf 
736bee91169SAlexander Graf 	entry(image_handle, &systab);
737bee91169SAlexander Graf 
738bee91169SAlexander Graf 	/* Should usually never get here */
739bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
740bee91169SAlexander Graf }
741bee91169SAlexander Graf 
742a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
743a86aeaf2SAlexander Graf 			efi_status_t exit_status, unsigned long exit_data_size,
744a86aeaf2SAlexander Graf 			int16_t *exit_data)
745bee91169SAlexander Graf {
746a86aeaf2SAlexander Graf 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
747a86aeaf2SAlexander Graf 
748bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
749bee91169SAlexander Graf 		  exit_data_size, exit_data);
750a86aeaf2SAlexander Graf 
751a86aeaf2SAlexander Graf 	loaded_image_info->exit_status = exit_status;
752692fcdd8SAlexander Graf 	longjmp(&loaded_image_info->exit_jmp, 1);
753a86aeaf2SAlexander Graf 
754a86aeaf2SAlexander Graf 	panic("EFI application exited");
755bee91169SAlexander Graf }
756bee91169SAlexander Graf 
757bee91169SAlexander Graf static struct efi_object *efi_search_obj(void *handle)
758bee91169SAlexander Graf {
759bee91169SAlexander Graf 	struct list_head *lhandle;
760bee91169SAlexander Graf 
761bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
762bee91169SAlexander Graf 		struct efi_object *efiobj;
763bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
764bee91169SAlexander Graf 		if (efiobj->handle == handle)
765bee91169SAlexander Graf 			return efiobj;
766bee91169SAlexander Graf 	}
767bee91169SAlexander Graf 
768bee91169SAlexander Graf 	return NULL;
769bee91169SAlexander Graf }
770bee91169SAlexander Graf 
771bee91169SAlexander Graf static efi_status_t EFIAPI efi_unload_image(void *image_handle)
772bee91169SAlexander Graf {
773bee91169SAlexander Graf 	struct efi_object *efiobj;
774bee91169SAlexander Graf 
775bee91169SAlexander Graf 	EFI_ENTRY("%p", image_handle);
776bee91169SAlexander Graf 	efiobj = efi_search_obj(image_handle);
777bee91169SAlexander Graf 	if (efiobj)
778bee91169SAlexander Graf 		list_del(&efiobj->link);
779bee91169SAlexander Graf 
780bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
781bee91169SAlexander Graf }
782bee91169SAlexander Graf 
783bee91169SAlexander Graf static void efi_exit_caches(void)
784bee91169SAlexander Graf {
785bee91169SAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
786bee91169SAlexander Graf 	/*
787bee91169SAlexander Graf 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
788bee91169SAlexander Graf 	 * a zImage, but does not know of all cache layers. Give it a hand.
789bee91169SAlexander Graf 	 */
790bee91169SAlexander Graf 	if (efi_is_direct_boot)
791bee91169SAlexander Graf 		cleanup_before_linux();
792bee91169SAlexander Graf #endif
793bee91169SAlexander Graf }
794bee91169SAlexander Graf 
795bee91169SAlexander Graf static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
796bee91169SAlexander Graf 						  unsigned long map_key)
797bee91169SAlexander Graf {
798bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", image_handle, map_key);
799bee91169SAlexander Graf 
800b7b8410aSAlexander Graf 	board_quiesce_devices();
801b7b8410aSAlexander Graf 
802bee91169SAlexander Graf 	/* Fix up caches for EFI payloads if necessary */
803bee91169SAlexander Graf 	efi_exit_caches();
804bee91169SAlexander Graf 
805bee91169SAlexander Graf 	/* This stops all lingering devices */
806bee91169SAlexander Graf 	bootm_disable_interrupts();
807bee91169SAlexander Graf 
808bee91169SAlexander Graf 	/* Give the payload some time to boot */
809bee91169SAlexander Graf 	WATCHDOG_RESET();
810bee91169SAlexander Graf 
811bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
812bee91169SAlexander Graf }
813bee91169SAlexander Graf 
814bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
815bee91169SAlexander Graf {
816bee91169SAlexander Graf 	static uint64_t mono = 0;
817bee91169SAlexander Graf 	EFI_ENTRY("%p", count);
818bee91169SAlexander Graf 	*count = mono++;
819bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
820bee91169SAlexander Graf }
821bee91169SAlexander Graf 
822bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
823bee91169SAlexander Graf {
824bee91169SAlexander Graf 	EFI_ENTRY("%ld", microseconds);
825bee91169SAlexander Graf 	udelay(microseconds);
826bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
827bee91169SAlexander Graf }
828bee91169SAlexander Graf 
829bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
830bee91169SAlexander Graf 						  uint64_t watchdog_code,
831bee91169SAlexander Graf 						  unsigned long data_size,
832bee91169SAlexander Graf 						  uint16_t *watchdog_data)
833bee91169SAlexander Graf {
834bee91169SAlexander Graf 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
835bee91169SAlexander Graf 		  data_size, watchdog_data);
836b5104821SRob Clark 	return efi_unsupported(__func__);
837bee91169SAlexander Graf }
838bee91169SAlexander Graf 
839bee91169SAlexander Graf static efi_status_t EFIAPI efi_connect_controller(
840bee91169SAlexander Graf 			efi_handle_t controller_handle,
841bee91169SAlexander Graf 			efi_handle_t *driver_image_handle,
842bee91169SAlexander Graf 			struct efi_device_path *remain_device_path,
843bee91169SAlexander Graf 			bool recursive)
844bee91169SAlexander Graf {
845bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
846bee91169SAlexander Graf 		  remain_device_path, recursive);
847bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
848bee91169SAlexander Graf }
849bee91169SAlexander Graf 
850bee91169SAlexander Graf static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
851bee91169SAlexander Graf 						     void *driver_image_handle,
852bee91169SAlexander Graf 						     void *child_handle)
853bee91169SAlexander Graf {
854bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
855bee91169SAlexander Graf 		  child_handle);
856bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
857bee91169SAlexander Graf }
858bee91169SAlexander Graf 
859bee91169SAlexander Graf static efi_status_t EFIAPI efi_close_protocol(void *handle,
860bee91169SAlexander Graf 					      efi_guid_t *protocol,
861bee91169SAlexander Graf 					      void *agent_handle,
862bee91169SAlexander Graf 					      void *controller_handle)
863bee91169SAlexander Graf {
864bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
865bee91169SAlexander Graf 		  controller_handle);
866bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
867bee91169SAlexander Graf }
868bee91169SAlexander Graf 
869bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
870bee91169SAlexander Graf 			efi_guid_t *protocol,
871bee91169SAlexander Graf 			struct efi_open_protocol_info_entry **entry_buffer,
872bee91169SAlexander Graf 			unsigned long *entry_count)
873bee91169SAlexander Graf {
874bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
875bee91169SAlexander Graf 		  entry_count);
876bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
877bee91169SAlexander Graf }
878bee91169SAlexander Graf 
879bee91169SAlexander Graf static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
880bee91169SAlexander Graf 			efi_guid_t ***protocol_buffer,
881bee91169SAlexander Graf 			unsigned long *protocol_buffer_count)
882bee91169SAlexander Graf {
883c0ebfc86Sxypron.glpk@gmx.de 	unsigned long buffer_size;
884c0ebfc86Sxypron.glpk@gmx.de 	struct efi_object *efiobj;
885c0ebfc86Sxypron.glpk@gmx.de 	unsigned long i, j;
886c0ebfc86Sxypron.glpk@gmx.de 	struct list_head *lhandle;
887c0ebfc86Sxypron.glpk@gmx.de 	efi_status_t r;
888c0ebfc86Sxypron.glpk@gmx.de 
889bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
890bee91169SAlexander Graf 		  protocol_buffer_count);
891c0ebfc86Sxypron.glpk@gmx.de 
892c0ebfc86Sxypron.glpk@gmx.de 	if (!handle || !protocol_buffer || !protocol_buffer_count)
893c0ebfc86Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
894c0ebfc86Sxypron.glpk@gmx.de 
895c0ebfc86Sxypron.glpk@gmx.de 	*protocol_buffer = NULL;
896661c8327SRob Clark 	*protocol_buffer_count = 0;
897c0ebfc86Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
898c0ebfc86Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
899c0ebfc86Sxypron.glpk@gmx.de 
900c0ebfc86Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
901c0ebfc86Sxypron.glpk@gmx.de 			continue;
902c0ebfc86Sxypron.glpk@gmx.de 
903c0ebfc86Sxypron.glpk@gmx.de 		/* Count protocols */
904c0ebfc86Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
905c0ebfc86Sxypron.glpk@gmx.de 			if (efiobj->protocols[i].guid)
906c0ebfc86Sxypron.glpk@gmx.de 				++*protocol_buffer_count;
907c0ebfc86Sxypron.glpk@gmx.de 		}
908c0ebfc86Sxypron.glpk@gmx.de 		/* Copy guids */
909c0ebfc86Sxypron.glpk@gmx.de 		if (*protocol_buffer_count) {
910c0ebfc86Sxypron.glpk@gmx.de 			buffer_size = sizeof(efi_guid_t *) *
911c0ebfc86Sxypron.glpk@gmx.de 					*protocol_buffer_count;
912c0ebfc86Sxypron.glpk@gmx.de 			r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
913c0ebfc86Sxypron.glpk@gmx.de 					      buffer_size,
914c0ebfc86Sxypron.glpk@gmx.de 					      (void **)protocol_buffer);
915c0ebfc86Sxypron.glpk@gmx.de 			if (r != EFI_SUCCESS)
916c0ebfc86Sxypron.glpk@gmx.de 				return EFI_EXIT(r);
917c0ebfc86Sxypron.glpk@gmx.de 			j = 0;
918c0ebfc86Sxypron.glpk@gmx.de 			for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
919c0ebfc86Sxypron.glpk@gmx.de 				if (efiobj->protocols[i].guid) {
920c0ebfc86Sxypron.glpk@gmx.de 					(*protocol_buffer)[j] = (void *)
921c0ebfc86Sxypron.glpk@gmx.de 						efiobj->protocols[i].guid;
922c0ebfc86Sxypron.glpk@gmx.de 					++j;
923c0ebfc86Sxypron.glpk@gmx.de 				}
924c0ebfc86Sxypron.glpk@gmx.de 			}
925c0ebfc86Sxypron.glpk@gmx.de 		}
926c0ebfc86Sxypron.glpk@gmx.de 		break;
927c0ebfc86Sxypron.glpk@gmx.de 	}
928c0ebfc86Sxypron.glpk@gmx.de 
929c0ebfc86Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_SUCCESS);
930bee91169SAlexander Graf }
931bee91169SAlexander Graf 
932bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
933bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
934bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
935bee91169SAlexander Graf 			unsigned long *no_handles, efi_handle_t **buffer)
936bee91169SAlexander Graf {
937c2e703f9Sxypron.glpk@gmx.de 	efi_status_t r;
938c2e703f9Sxypron.glpk@gmx.de 	unsigned long buffer_size = 0;
939c2e703f9Sxypron.glpk@gmx.de 
940bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
941bee91169SAlexander Graf 		  no_handles, buffer);
942c2e703f9Sxypron.glpk@gmx.de 
943c2e703f9Sxypron.glpk@gmx.de 	if (!no_handles || !buffer) {
944c2e703f9Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
945c2e703f9Sxypron.glpk@gmx.de 		goto out;
946c2e703f9Sxypron.glpk@gmx.de 	}
947c2e703f9Sxypron.glpk@gmx.de 	*no_handles = 0;
948c2e703f9Sxypron.glpk@gmx.de 	*buffer = NULL;
949c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
950c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
951c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_BUFFER_TOO_SMALL)
952c2e703f9Sxypron.glpk@gmx.de 		goto out;
953c2e703f9Sxypron.glpk@gmx.de 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
954c2e703f9Sxypron.glpk@gmx.de 			      (void **)buffer);
955c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
956c2e703f9Sxypron.glpk@gmx.de 		goto out;
957c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
958c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
959c2e703f9Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
960c2e703f9Sxypron.glpk@gmx.de 		*no_handles = buffer_size / sizeof(void *);
961c2e703f9Sxypron.glpk@gmx.de out:
962c2e703f9Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
963bee91169SAlexander Graf }
964bee91169SAlexander Graf 
965bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
966bee91169SAlexander Graf 					       void *registration,
967bee91169SAlexander Graf 					       void **protocol_interface)
968bee91169SAlexander Graf {
96988adae5eSxypron.glpk@gmx.de 	struct list_head *lhandle;
970bee91169SAlexander Graf 	int i;
971bee91169SAlexander Graf 
972bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
97388adae5eSxypron.glpk@gmx.de 
97488adae5eSxypron.glpk@gmx.de 	if (!protocol || !protocol_interface)
97588adae5eSxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
97688adae5eSxypron.glpk@gmx.de 
97788adae5eSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
97888adae5eSxypron.glpk@gmx.de 		struct efi_object *efiobj;
97988adae5eSxypron.glpk@gmx.de 
98088adae5eSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
98188adae5eSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
98288adae5eSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
98388adae5eSxypron.glpk@gmx.de 
98488adae5eSxypron.glpk@gmx.de 			if (!handler->guid)
98588adae5eSxypron.glpk@gmx.de 				continue;
98688adae5eSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
98788adae5eSxypron.glpk@gmx.de 				*protocol_interface =
98888adae5eSxypron.glpk@gmx.de 					handler->protocol_interface;
989bee91169SAlexander Graf 				return EFI_EXIT(EFI_SUCCESS);
990bee91169SAlexander Graf 			}
991bee91169SAlexander Graf 		}
99288adae5eSxypron.glpk@gmx.de 	}
99388adae5eSxypron.glpk@gmx.de 	*protocol_interface = NULL;
994bee91169SAlexander Graf 
995bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
996bee91169SAlexander Graf }
997bee91169SAlexander Graf 
998bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
999bee91169SAlexander Graf 			void **handle, ...)
1000bee91169SAlexander Graf {
1001bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
100258b83586Sxypron.glpk@gmx.de 
100358b83586Sxypron.glpk@gmx.de 	va_list argptr;
100458b83586Sxypron.glpk@gmx.de 	efi_guid_t *protocol;
100558b83586Sxypron.glpk@gmx.de 	void *protocol_interface;
100658b83586Sxypron.glpk@gmx.de 	efi_status_t r = EFI_SUCCESS;
100758b83586Sxypron.glpk@gmx.de 	int i = 0;
100858b83586Sxypron.glpk@gmx.de 
100958b83586Sxypron.glpk@gmx.de 	if (!handle)
101058b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
101158b83586Sxypron.glpk@gmx.de 
101258b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
101358b83586Sxypron.glpk@gmx.de 	for (;;) {
101458b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
101558b83586Sxypron.glpk@gmx.de 		if (!protocol)
101658b83586Sxypron.glpk@gmx.de 			break;
101758b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
101858b83586Sxypron.glpk@gmx.de 		r = efi_install_protocol_interface(handle, protocol,
101958b83586Sxypron.glpk@gmx.de 						   EFI_NATIVE_INTERFACE,
102058b83586Sxypron.glpk@gmx.de 						   protocol_interface);
102158b83586Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
102258b83586Sxypron.glpk@gmx.de 			break;
102358b83586Sxypron.glpk@gmx.de 		i++;
102458b83586Sxypron.glpk@gmx.de 	}
102558b83586Sxypron.glpk@gmx.de 	va_end(argptr);
102658b83586Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
102758b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(r);
102858b83586Sxypron.glpk@gmx.de 
102958b83586Sxypron.glpk@gmx.de 	/* If an error occured undo all changes. */
103058b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
103158b83586Sxypron.glpk@gmx.de 	for (; i; --i) {
103258b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
103358b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
103458b83586Sxypron.glpk@gmx.de 		efi_uninstall_protocol_interface(handle, protocol,
103558b83586Sxypron.glpk@gmx.de 						 protocol_interface);
103658b83586Sxypron.glpk@gmx.de 	}
103758b83586Sxypron.glpk@gmx.de 	va_end(argptr);
103858b83586Sxypron.glpk@gmx.de 
103958b83586Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
1040bee91169SAlexander Graf }
1041bee91169SAlexander Graf 
1042bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1043bee91169SAlexander Graf 			void *handle, ...)
1044bee91169SAlexander Graf {
1045bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
1046bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
1047bee91169SAlexander Graf }
1048bee91169SAlexander Graf 
1049bee91169SAlexander Graf static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1050bee91169SAlexander Graf 					       unsigned long data_size,
1051bee91169SAlexander Graf 					       uint32_t *crc32_p)
1052bee91169SAlexander Graf {
1053bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", data, data_size);
1054bee91169SAlexander Graf 	*crc32_p = crc32(0, data, data_size);
1055bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
1056bee91169SAlexander Graf }
1057bee91169SAlexander Graf 
1058bee91169SAlexander Graf static void EFIAPI efi_copy_mem(void *destination, void *source,
1059bee91169SAlexander Graf 				unsigned long length)
1060bee91169SAlexander Graf {
1061bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
1062bee91169SAlexander Graf 	memcpy(destination, source, length);
1063bee91169SAlexander Graf }
1064bee91169SAlexander Graf 
1065bee91169SAlexander Graf static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1066bee91169SAlexander Graf {
1067bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1068bee91169SAlexander Graf 	memset(buffer, value, size);
1069bee91169SAlexander Graf }
1070bee91169SAlexander Graf 
1071bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol(
1072bee91169SAlexander Graf 			void *handle, efi_guid_t *protocol,
1073bee91169SAlexander Graf 			void **protocol_interface, void *agent_handle,
1074bee91169SAlexander Graf 			void *controller_handle, uint32_t attributes)
1075bee91169SAlexander Graf {
1076bee91169SAlexander Graf 	struct list_head *lhandle;
1077bee91169SAlexander Graf 	int i;
107869baec67Sxypron.glpk@gmx.de 	efi_status_t r = EFI_INVALID_PARAMETER;
1079bee91169SAlexander Graf 
1080bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1081bee91169SAlexander Graf 		  protocol_interface, agent_handle, controller_handle,
1082bee91169SAlexander Graf 		  attributes);
1083b5349f74Sxypron.glpk@gmx.de 
108469baec67Sxypron.glpk@gmx.de 	if (!handle || !protocol ||
108569baec67Sxypron.glpk@gmx.de 	    (!protocol_interface && attributes !=
108669baec67Sxypron.glpk@gmx.de 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
108769baec67Sxypron.glpk@gmx.de 		goto out;
108869baec67Sxypron.glpk@gmx.de 	}
108969baec67Sxypron.glpk@gmx.de 
109069baec67Sxypron.glpk@gmx.de 	switch (attributes) {
109169baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
109269baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
109369baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
109469baec67Sxypron.glpk@gmx.de 		break;
109569baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
109669baec67Sxypron.glpk@gmx.de 		if (controller_handle == handle)
109769baec67Sxypron.glpk@gmx.de 			goto out;
109869baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
109969baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
110069baec67Sxypron.glpk@gmx.de 		if (controller_handle == NULL)
110169baec67Sxypron.glpk@gmx.de 			goto out;
110269baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
110369baec67Sxypron.glpk@gmx.de 		if (agent_handle == NULL)
110469baec67Sxypron.glpk@gmx.de 			goto out;
110569baec67Sxypron.glpk@gmx.de 		break;
110669baec67Sxypron.glpk@gmx.de 	default:
1107b5349f74Sxypron.glpk@gmx.de 		goto out;
1108b5349f74Sxypron.glpk@gmx.de 	}
1109b5349f74Sxypron.glpk@gmx.de 
1110bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
1111bee91169SAlexander Graf 		struct efi_object *efiobj;
1112bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
1113bee91169SAlexander Graf 
1114bee91169SAlexander Graf 		if (efiobj->handle != handle)
1115bee91169SAlexander Graf 			continue;
1116bee91169SAlexander Graf 
1117bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1118bee91169SAlexander Graf 			struct efi_handler *handler = &efiobj->protocols[i];
1119bee91169SAlexander Graf 			const efi_guid_t *hprotocol = handler->guid;
1120bee91169SAlexander Graf 			if (!hprotocol)
11214b6ed0d7Sxypron.glpk@gmx.de 				continue;
1122bee91169SAlexander Graf 			if (!guidcmp(hprotocol, protocol)) {
1123b5349f74Sxypron.glpk@gmx.de 				if (attributes !=
1124b5349f74Sxypron.glpk@gmx.de 				    EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1125b5349f74Sxypron.glpk@gmx.de 					*protocol_interface =
1126b5349f74Sxypron.glpk@gmx.de 						handler->protocol_interface;
1127b5349f74Sxypron.glpk@gmx.de 				}
1128b5349f74Sxypron.glpk@gmx.de 				r = EFI_SUCCESS;
1129bee91169SAlexander Graf 				goto out;
1130bee91169SAlexander Graf 			}
1131bee91169SAlexander Graf 		}
113269baec67Sxypron.glpk@gmx.de 		goto unsupported;
1133bee91169SAlexander Graf 	}
1134bee91169SAlexander Graf 
113569baec67Sxypron.glpk@gmx.de unsupported:
113669baec67Sxypron.glpk@gmx.de 	r = EFI_UNSUPPORTED;
1137bee91169SAlexander Graf out:
1138bee91169SAlexander Graf 	return EFI_EXIT(r);
1139bee91169SAlexander Graf }
1140bee91169SAlexander Graf 
1141bee91169SAlexander Graf static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1142bee91169SAlexander Graf 					       efi_guid_t *protocol,
1143bee91169SAlexander Graf 					       void **protocol_interface)
1144bee91169SAlexander Graf {
11458e1d329fSxypron.glpk@gmx.de 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
11468e1d329fSxypron.glpk@gmx.de 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1147bee91169SAlexander Graf }
1148bee91169SAlexander Graf 
1149bee91169SAlexander Graf static const struct efi_boot_services efi_boot_services = {
1150bee91169SAlexander Graf 	.hdr = {
1151bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1152bee91169SAlexander Graf 	},
1153bee91169SAlexander Graf 	.raise_tpl = efi_raise_tpl,
1154bee91169SAlexander Graf 	.restore_tpl = efi_restore_tpl,
1155bee91169SAlexander Graf 	.allocate_pages = efi_allocate_pages_ext,
1156bee91169SAlexander Graf 	.free_pages = efi_free_pages_ext,
1157bee91169SAlexander Graf 	.get_memory_map = efi_get_memory_map_ext,
1158ead1274bSStefan Brüns 	.allocate_pool = efi_allocate_pool_ext,
115942417bc8SStefan Brüns 	.free_pool = efi_free_pool_ext,
116049deb455Sxypron.glpk@gmx.de 	.create_event = efi_create_event_ext,
1161bfc72462Sxypron.glpk@gmx.de 	.set_timer = efi_set_timer_ext,
1162bee91169SAlexander Graf 	.wait_for_event = efi_wait_for_event,
1163c6841592Sxypron.glpk@gmx.de 	.signal_event = efi_signal_event_ext,
1164bee91169SAlexander Graf 	.close_event = efi_close_event,
1165bee91169SAlexander Graf 	.check_event = efi_check_event,
11668bee5a3cSxypron.glpk@gmx.de 	.install_protocol_interface = efi_install_protocol_interface_ext,
1167bee91169SAlexander Graf 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
11683d8e1456Sxypron.glpk@gmx.de 	.uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1169bee91169SAlexander Graf 	.handle_protocol = efi_handle_protocol,
1170bee91169SAlexander Graf 	.reserved = NULL,
1171bee91169SAlexander Graf 	.register_protocol_notify = efi_register_protocol_notify,
117226329584Sxypron.glpk@gmx.de 	.locate_handle = efi_locate_handle_ext,
1173bee91169SAlexander Graf 	.locate_device_path = efi_locate_device_path,
1174488bf12dSAlexander Graf 	.install_configuration_table = efi_install_configuration_table_ext,
1175bee91169SAlexander Graf 	.load_image = efi_load_image,
1176bee91169SAlexander Graf 	.start_image = efi_start_image,
1177a86aeaf2SAlexander Graf 	.exit = efi_exit,
1178bee91169SAlexander Graf 	.unload_image = efi_unload_image,
1179bee91169SAlexander Graf 	.exit_boot_services = efi_exit_boot_services,
1180bee91169SAlexander Graf 	.get_next_monotonic_count = efi_get_next_monotonic_count,
1181bee91169SAlexander Graf 	.stall = efi_stall,
1182bee91169SAlexander Graf 	.set_watchdog_timer = efi_set_watchdog_timer,
1183bee91169SAlexander Graf 	.connect_controller = efi_connect_controller,
1184bee91169SAlexander Graf 	.disconnect_controller = efi_disconnect_controller,
1185bee91169SAlexander Graf 	.open_protocol = efi_open_protocol,
1186bee91169SAlexander Graf 	.close_protocol = efi_close_protocol,
1187bee91169SAlexander Graf 	.open_protocol_information = efi_open_protocol_information,
1188bee91169SAlexander Graf 	.protocols_per_handle = efi_protocols_per_handle,
1189bee91169SAlexander Graf 	.locate_handle_buffer = efi_locate_handle_buffer,
1190bee91169SAlexander Graf 	.locate_protocol = efi_locate_protocol,
1191bee91169SAlexander Graf 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1192bee91169SAlexander Graf 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1193bee91169SAlexander Graf 	.calculate_crc32 = efi_calculate_crc32,
1194bee91169SAlexander Graf 	.copy_mem = efi_copy_mem,
1195bee91169SAlexander Graf 	.set_mem = efi_set_mem,
1196bee91169SAlexander Graf };
1197bee91169SAlexander Graf 
1198bee91169SAlexander Graf 
11993c63db9cSAlexander Graf static uint16_t __efi_runtime_data firmware_vendor[] =
1200bee91169SAlexander Graf 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
1201bee91169SAlexander Graf 
12023c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
1203bee91169SAlexander Graf 	.hdr = {
1204bee91169SAlexander Graf 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
1205bee91169SAlexander Graf 		.revision = 0x20005, /* 2.5 */
1206bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1207bee91169SAlexander Graf 	},
1208bee91169SAlexander Graf 	.fw_vendor = (long)firmware_vendor,
1209bee91169SAlexander Graf 	.con_in = (void*)&efi_con_in,
1210bee91169SAlexander Graf 	.con_out = (void*)&efi_con_out,
1211bee91169SAlexander Graf 	.std_err = (void*)&efi_con_out,
1212bee91169SAlexander Graf 	.runtime = (void*)&efi_runtime_services,
1213bee91169SAlexander Graf 	.boottime = (void*)&efi_boot_services,
1214bee91169SAlexander Graf 	.nr_tables = 0,
1215bee91169SAlexander Graf 	.tables = (void*)efi_conf_table,
1216bee91169SAlexander Graf };
1217