xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_boottime.c (revision bfc724625f73f80321945fee306d0c4dc3015898)
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 
84c6841592Sxypron.glpk@gmx.de static void efi_signal_event(struct efi_event *event)
85c6841592Sxypron.glpk@gmx.de {
86c6841592Sxypron.glpk@gmx.de 	if (event->signaled)
87c6841592Sxypron.glpk@gmx.de 		return;
88c6841592Sxypron.glpk@gmx.de 	event->signaled = 1;
89c6841592Sxypron.glpk@gmx.de 	if (event->type & EVT_NOTIFY_SIGNAL) {
90c6841592Sxypron.glpk@gmx.de 		EFI_EXIT(EFI_SUCCESS);
91c6841592Sxypron.glpk@gmx.de 		event->notify_function(event, event->notify_context);
92c6841592Sxypron.glpk@gmx.de 		EFI_ENTRY("returning from notification function");
93c6841592Sxypron.glpk@gmx.de 	}
94c6841592Sxypron.glpk@gmx.de }
95c6841592Sxypron.glpk@gmx.de 
96bee91169SAlexander Graf static efi_status_t efi_unsupported(const char *funcname)
97bee91169SAlexander Graf {
98edcef3baSAlexander Graf 	debug("EFI: App called into unimplemented function %s\n", funcname);
99bee91169SAlexander Graf 	return EFI_EXIT(EFI_UNSUPPORTED);
100bee91169SAlexander Graf }
101bee91169SAlexander Graf 
102bee91169SAlexander Graf static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
103bee91169SAlexander Graf {
104bee91169SAlexander Graf 	return memcmp(g1, g2, sizeof(efi_guid_t));
105bee91169SAlexander Graf }
106bee91169SAlexander Graf 
107503f2695Sxypron.glpk@gmx.de static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
108bee91169SAlexander Graf {
109503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", new_tpl);
110bee91169SAlexander Graf 	return EFI_EXIT(0);
111bee91169SAlexander Graf }
112bee91169SAlexander Graf 
113503f2695Sxypron.glpk@gmx.de static void EFIAPI efi_restore_tpl(UINTN old_tpl)
114bee91169SAlexander Graf {
115503f2695Sxypron.glpk@gmx.de 	EFI_ENTRY("0x%zx", old_tpl);
116bee91169SAlexander Graf 	EFI_EXIT(efi_unsupported(__func__));
117bee91169SAlexander Graf }
118bee91169SAlexander Graf 
1196e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
120bee91169SAlexander Graf 						  unsigned long pages,
121bee91169SAlexander Graf 						  uint64_t *memory)
122bee91169SAlexander Graf {
123bee91169SAlexander Graf 	efi_status_t r;
124bee91169SAlexander Graf 
125bee91169SAlexander Graf 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
126bee91169SAlexander Graf 	r = efi_allocate_pages(type, memory_type, pages, memory);
127bee91169SAlexander Graf 	return EFI_EXIT(r);
128bee91169SAlexander Graf }
129bee91169SAlexander Graf 
1306e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
1316e0bf8d8SMasahiro Yamada 					      unsigned long pages)
132bee91169SAlexander Graf {
133bee91169SAlexander Graf 	efi_status_t r;
134bee91169SAlexander Graf 
135bee91169SAlexander Graf 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
136bee91169SAlexander Graf 	r = efi_free_pages(memory, pages);
137bee91169SAlexander Graf 	return EFI_EXIT(r);
138bee91169SAlexander Graf }
139bee91169SAlexander Graf 
1406e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
1416e0bf8d8SMasahiro Yamada 					unsigned long *memory_map_size,
142bee91169SAlexander Graf 					struct efi_mem_desc *memory_map,
143bee91169SAlexander Graf 					unsigned long *map_key,
144bee91169SAlexander Graf 					unsigned long *descriptor_size,
145bee91169SAlexander Graf 					uint32_t *descriptor_version)
146bee91169SAlexander Graf {
147bee91169SAlexander Graf 	efi_status_t r;
148bee91169SAlexander Graf 
149bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
150bee91169SAlexander Graf 		  map_key, descriptor_size, descriptor_version);
151bee91169SAlexander Graf 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
152bee91169SAlexander Graf 			       descriptor_size, descriptor_version);
153bee91169SAlexander Graf 	return EFI_EXIT(r);
154bee91169SAlexander Graf }
155bee91169SAlexander Graf 
156ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
157ead1274bSStefan Brüns 						 unsigned long size,
158bee91169SAlexander Graf 						 void **buffer)
159bee91169SAlexander Graf {
1601cd29f0aSAlexander Graf 	efi_status_t r;
1611cd29f0aSAlexander Graf 
1621cd29f0aSAlexander Graf 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
163ead1274bSStefan Brüns 	r = efi_allocate_pool(pool_type, size, buffer);
1641cd29f0aSAlexander Graf 	return EFI_EXIT(r);
165bee91169SAlexander Graf }
166bee91169SAlexander Graf 
16742417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
168bee91169SAlexander Graf {
1691cd29f0aSAlexander Graf 	efi_status_t r;
1701cd29f0aSAlexander Graf 
1711cd29f0aSAlexander Graf 	EFI_ENTRY("%p", buffer);
17242417bc8SStefan Brüns 	r = efi_free_pool(buffer);
1731cd29f0aSAlexander Graf 	return EFI_EXIT(r);
174bee91169SAlexander Graf }
175bee91169SAlexander Graf 
176bee91169SAlexander Graf /*
177c6841592Sxypron.glpk@gmx.de  * Our event capabilities are very limited. Only a small limited
178c6841592Sxypron.glpk@gmx.de  * number of events is allowed to coexist.
179bee91169SAlexander Graf  */
180c6841592Sxypron.glpk@gmx.de static struct efi_event efi_events[16];
181bee91169SAlexander Graf 
18249deb455Sxypron.glpk@gmx.de efi_status_t efi_create_event(enum efi_event_type type, UINTN notify_tpl,
1832fd945feSxypron.glpk@gmx.de 			      void (EFIAPI *notify_function) (
1842fd945feSxypron.glpk@gmx.de 					struct efi_event *event,
185e275458cSSimon Glass 					void *context),
1862fd945feSxypron.glpk@gmx.de 			      void *notify_context, struct efi_event **event)
187bee91169SAlexander Graf {
188c6841592Sxypron.glpk@gmx.de 	int i;
189c6841592Sxypron.glpk@gmx.de 
190a95343b8SJonathan Gray 	if (event == NULL)
19149deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
192a95343b8SJonathan Gray 
193a95343b8SJonathan Gray 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
19449deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
195a95343b8SJonathan Gray 
196a95343b8SJonathan Gray 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
197a95343b8SJonathan Gray 	    notify_function == NULL)
19849deb455Sxypron.glpk@gmx.de 		return EFI_INVALID_PARAMETER;
199a95343b8SJonathan Gray 
200c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
201c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].type)
202c6841592Sxypron.glpk@gmx.de 			continue;
203c6841592Sxypron.glpk@gmx.de 		efi_events[i].type = type;
204c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_tpl = notify_tpl;
205c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_function = notify_function;
206c6841592Sxypron.glpk@gmx.de 		efi_events[i].notify_context = notify_context;
207c6841592Sxypron.glpk@gmx.de 		/* Disable timers on bootup */
208c6841592Sxypron.glpk@gmx.de 		efi_events[i].trigger_next = -1ULL;
209c6841592Sxypron.glpk@gmx.de 		efi_events[i].signaled = 0;
210c6841592Sxypron.glpk@gmx.de 		*event = &efi_events[i];
21149deb455Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
212bee91169SAlexander Graf 	}
21349deb455Sxypron.glpk@gmx.de 	return EFI_OUT_OF_RESOURCES;
214c6841592Sxypron.glpk@gmx.de }
215bee91169SAlexander Graf 
21649deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
21749deb455Sxypron.glpk@gmx.de 			enum efi_event_type type, UINTN notify_tpl,
21849deb455Sxypron.glpk@gmx.de 			void (EFIAPI *notify_function) (
21949deb455Sxypron.glpk@gmx.de 					struct efi_event *event,
22049deb455Sxypron.glpk@gmx.de 					void *context),
22149deb455Sxypron.glpk@gmx.de 			void *notify_context, struct efi_event **event)
22249deb455Sxypron.glpk@gmx.de {
22349deb455Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
22449deb455Sxypron.glpk@gmx.de 		  notify_context);
22549deb455Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
22649deb455Sxypron.glpk@gmx.de 					 notify_context, event));
22749deb455Sxypron.glpk@gmx.de }
22849deb455Sxypron.glpk@gmx.de 
22949deb455Sxypron.glpk@gmx.de 
230bee91169SAlexander Graf /*
231bee91169SAlexander Graf  * Our timers have to work without interrupts, so we check whenever keyboard
232bee91169SAlexander Graf  * input or disk accesses happen if enough time elapsed for it to fire.
233bee91169SAlexander Graf  */
234bee91169SAlexander Graf void efi_timer_check(void)
235bee91169SAlexander Graf {
236c6841592Sxypron.glpk@gmx.de 	int i;
237bee91169SAlexander Graf 	u64 now = timer_get_us();
238bee91169SAlexander Graf 
239c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
240c6841592Sxypron.glpk@gmx.de 		if (!efi_events[i].type ||
241c6841592Sxypron.glpk@gmx.de 		    !(efi_events[i].type & EVT_TIMER) ||
242c6841592Sxypron.glpk@gmx.de 		    efi_events[i].trigger_type == EFI_TIMER_STOP ||
243c6841592Sxypron.glpk@gmx.de 		    now < efi_events[i].trigger_next)
244c6841592Sxypron.glpk@gmx.de 			continue;
245c6841592Sxypron.glpk@gmx.de 		if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
246c6841592Sxypron.glpk@gmx.de 			efi_events[i].trigger_next +=
247c6841592Sxypron.glpk@gmx.de 				efi_events[i].trigger_time / 10;
248c6841592Sxypron.glpk@gmx.de 			efi_events[i].signaled = 0;
249bee91169SAlexander Graf 		}
250c6841592Sxypron.glpk@gmx.de 		efi_signal_event(&efi_events[i]);
251c6841592Sxypron.glpk@gmx.de 	}
252bee91169SAlexander Graf 	WATCHDOG_RESET();
253bee91169SAlexander Graf }
254bee91169SAlexander Graf 
255*bfc72462Sxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, int type,
256bee91169SAlexander Graf 			   uint64_t trigger_time)
257bee91169SAlexander Graf {
258bee91169SAlexander Graf 	/* We don't have 64bit division available everywhere, so limit timer
259bee91169SAlexander Graf 	 * distances to 32bit bits. */
260bee91169SAlexander Graf 	u32 trigger32 = trigger_time;
261c6841592Sxypron.glpk@gmx.de 	int i;
262bee91169SAlexander Graf 
263bee91169SAlexander Graf 	if (trigger32 < trigger_time) {
264bee91169SAlexander Graf 		printf("WARNING: Truncating timer from %"PRIx64" to %x\n",
265bee91169SAlexander Graf 		       trigger_time, trigger32);
266bee91169SAlexander Graf 	}
267bee91169SAlexander Graf 
268c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
269c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
270c6841592Sxypron.glpk@gmx.de 			continue;
271bee91169SAlexander Graf 
272c6841592Sxypron.glpk@gmx.de 		if (!(event->type & EVT_TIMER))
273c6841592Sxypron.glpk@gmx.de 			break;
274bee91169SAlexander Graf 		switch (type) {
275bee91169SAlexander Graf 		case EFI_TIMER_STOP:
276c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
277bee91169SAlexander Graf 			break;
278bee91169SAlexander Graf 		case EFI_TIMER_PERIODIC:
279bee91169SAlexander Graf 		case EFI_TIMER_RELATIVE:
280c6841592Sxypron.glpk@gmx.de 			event->trigger_next =
281c6841592Sxypron.glpk@gmx.de 				timer_get_us() + (trigger32 / 10);
282bee91169SAlexander Graf 			break;
283bee91169SAlexander Graf 		default:
284*bfc72462Sxypron.glpk@gmx.de 			return EFI_INVALID_PARAMETER;
285bee91169SAlexander Graf 		}
286c6841592Sxypron.glpk@gmx.de 		event->trigger_type = type;
287c6841592Sxypron.glpk@gmx.de 		event->trigger_time = trigger_time;
288*bfc72462Sxypron.glpk@gmx.de 		return EFI_SUCCESS;
289bee91169SAlexander Graf 	}
290*bfc72462Sxypron.glpk@gmx.de 	return EFI_INVALID_PARAMETER;
291*bfc72462Sxypron.glpk@gmx.de }
292*bfc72462Sxypron.glpk@gmx.de 
293*bfc72462Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, int type,
294*bfc72462Sxypron.glpk@gmx.de 					 uint64_t trigger_time)
295*bfc72462Sxypron.glpk@gmx.de {
296*bfc72462Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
297*bfc72462Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
298c6841592Sxypron.glpk@gmx.de }
299bee91169SAlexander Graf 
300bee91169SAlexander Graf static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
3012fd945feSxypron.glpk@gmx.de 					      struct efi_event **event,
3022fd945feSxypron.glpk@gmx.de 					      unsigned long *index)
303bee91169SAlexander Graf {
304c6841592Sxypron.glpk@gmx.de 	int i, j;
305bee91169SAlexander Graf 
306bee91169SAlexander Graf 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
307bee91169SAlexander Graf 
308c6841592Sxypron.glpk@gmx.de 	/* Check parameters */
309c6841592Sxypron.glpk@gmx.de 	if (!num_events || !event)
310c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
311c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < num_events; ++i) {
312c6841592Sxypron.glpk@gmx.de 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
313c6841592Sxypron.glpk@gmx.de 			if (event[i] == &efi_events[j])
314c6841592Sxypron.glpk@gmx.de 				goto known_event;
315c6841592Sxypron.glpk@gmx.de 		}
316c6841592Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
317c6841592Sxypron.glpk@gmx.de known_event:
318c6841592Sxypron.glpk@gmx.de 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
319c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_INVALID_PARAMETER);
320c6841592Sxypron.glpk@gmx.de 	}
321c6841592Sxypron.glpk@gmx.de 
322c6841592Sxypron.glpk@gmx.de 	/* Wait for signal */
323c6841592Sxypron.glpk@gmx.de 	for (;;) {
324c6841592Sxypron.glpk@gmx.de 		for (i = 0; i < num_events; ++i) {
325c6841592Sxypron.glpk@gmx.de 			if (event[i]->signaled)
326c6841592Sxypron.glpk@gmx.de 				goto out;
327c6841592Sxypron.glpk@gmx.de 		}
328c6841592Sxypron.glpk@gmx.de 		/* Allow events to occur. */
329bee91169SAlexander Graf 		efi_timer_check();
330c6841592Sxypron.glpk@gmx.de 	}
331c6841592Sxypron.glpk@gmx.de 
332c6841592Sxypron.glpk@gmx.de out:
333c6841592Sxypron.glpk@gmx.de 	/*
334c6841592Sxypron.glpk@gmx.de 	 * Reset the signal which is passed to the caller to allow periodic
335c6841592Sxypron.glpk@gmx.de 	 * events to occur.
336c6841592Sxypron.glpk@gmx.de 	 */
337c6841592Sxypron.glpk@gmx.de 	event[i]->signaled = 0;
338c6841592Sxypron.glpk@gmx.de 	if (index)
339c6841592Sxypron.glpk@gmx.de 		*index = i;
340bee91169SAlexander Graf 
341bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
342bee91169SAlexander Graf }
343bee91169SAlexander Graf 
344c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
345bee91169SAlexander Graf {
346c6841592Sxypron.glpk@gmx.de 	int i;
347c6841592Sxypron.glpk@gmx.de 
348bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
349c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
350c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
351c6841592Sxypron.glpk@gmx.de 			continue;
352c6841592Sxypron.glpk@gmx.de 		efi_signal_event(event);
353c6841592Sxypron.glpk@gmx.de 		break;
354c6841592Sxypron.glpk@gmx.de 	}
355bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
356bee91169SAlexander Graf }
357bee91169SAlexander Graf 
3582fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
359bee91169SAlexander Graf {
360c6841592Sxypron.glpk@gmx.de 	int i;
361c6841592Sxypron.glpk@gmx.de 
362bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
363c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
364c6841592Sxypron.glpk@gmx.de 		if (event == &efi_events[i]) {
365c6841592Sxypron.glpk@gmx.de 			event->type = 0;
366c6841592Sxypron.glpk@gmx.de 			event->trigger_next = -1ULL;
367c6841592Sxypron.glpk@gmx.de 			event->signaled = 0;
368bee91169SAlexander Graf 			return EFI_EXIT(EFI_SUCCESS);
369bee91169SAlexander Graf 		}
370c6841592Sxypron.glpk@gmx.de 	}
371c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
372c6841592Sxypron.glpk@gmx.de }
373bee91169SAlexander Graf 
3742fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
375bee91169SAlexander Graf {
376c6841592Sxypron.glpk@gmx.de 	int i;
377c6841592Sxypron.glpk@gmx.de 
378bee91169SAlexander Graf 	EFI_ENTRY("%p", event);
379c6841592Sxypron.glpk@gmx.de 	efi_timer_check();
380c6841592Sxypron.glpk@gmx.de 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
381c6841592Sxypron.glpk@gmx.de 		if (event != &efi_events[i])
382c6841592Sxypron.glpk@gmx.de 			continue;
383c6841592Sxypron.glpk@gmx.de 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
384c6841592Sxypron.glpk@gmx.de 			break;
385c6841592Sxypron.glpk@gmx.de 		if (event->signaled)
386c6841592Sxypron.glpk@gmx.de 			return EFI_EXIT(EFI_SUCCESS);
387bee91169SAlexander Graf 		return EFI_EXIT(EFI_NOT_READY);
388bee91169SAlexander Graf 	}
389c6841592Sxypron.glpk@gmx.de 	return EFI_EXIT(EFI_INVALID_PARAMETER);
390c6841592Sxypron.glpk@gmx.de }
391bee91169SAlexander Graf 
392bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
393bee91169SAlexander Graf 			efi_guid_t *protocol, int protocol_interface_type,
394bee91169SAlexander Graf 			void *protocol_interface)
395bee91169SAlexander Graf {
396e0549f8aSxypron.glpk@gmx.de 	struct list_head *lhandle;
397e0549f8aSxypron.glpk@gmx.de 	int i;
398e0549f8aSxypron.glpk@gmx.de 	efi_status_t r;
399e0549f8aSxypron.glpk@gmx.de 
400e0549f8aSxypron.glpk@gmx.de 	if (!handle || !protocol ||
401e0549f8aSxypron.glpk@gmx.de 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
402e0549f8aSxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
403e0549f8aSxypron.glpk@gmx.de 		goto out;
404bee91169SAlexander Graf 	}
405e0549f8aSxypron.glpk@gmx.de 
406e0549f8aSxypron.glpk@gmx.de 	/* Create new handle if requested. */
407e0549f8aSxypron.glpk@gmx.de 	if (!*handle) {
408e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
409e0549f8aSxypron.glpk@gmx.de 		goto out;
410e0549f8aSxypron.glpk@gmx.de 	}
411e0549f8aSxypron.glpk@gmx.de 	/* Find object. */
412e0549f8aSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
413e0549f8aSxypron.glpk@gmx.de 		struct efi_object *efiobj;
414e0549f8aSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
415e0549f8aSxypron.glpk@gmx.de 
416e0549f8aSxypron.glpk@gmx.de 		if (efiobj->handle != *handle)
417e0549f8aSxypron.glpk@gmx.de 			continue;
418e0549f8aSxypron.glpk@gmx.de 		/* Check if protocol is already installed on the handle. */
419e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
420e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
421e0549f8aSxypron.glpk@gmx.de 
422e0549f8aSxypron.glpk@gmx.de 			if (!handler->guid)
423e0549f8aSxypron.glpk@gmx.de 				continue;
424e0549f8aSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
425e0549f8aSxypron.glpk@gmx.de 				r = EFI_INVALID_PARAMETER;
426e0549f8aSxypron.glpk@gmx.de 				goto out;
427e0549f8aSxypron.glpk@gmx.de 			}
428e0549f8aSxypron.glpk@gmx.de 		}
429e0549f8aSxypron.glpk@gmx.de 		/* Install protocol in first empty slot. */
430e0549f8aSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
431e0549f8aSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
432e0549f8aSxypron.glpk@gmx.de 
433e0549f8aSxypron.glpk@gmx.de 			if (handler->guid)
434e0549f8aSxypron.glpk@gmx.de 				continue;
435e0549f8aSxypron.glpk@gmx.de 
436e0549f8aSxypron.glpk@gmx.de 			handler->guid = protocol;
437e0549f8aSxypron.glpk@gmx.de 			handler->protocol_interface = protocol_interface;
438e0549f8aSxypron.glpk@gmx.de 			r = EFI_SUCCESS;
439e0549f8aSxypron.glpk@gmx.de 			goto out;
440e0549f8aSxypron.glpk@gmx.de 		}
441e0549f8aSxypron.glpk@gmx.de 		r = EFI_OUT_OF_RESOURCES;
442e0549f8aSxypron.glpk@gmx.de 		goto out;
443e0549f8aSxypron.glpk@gmx.de 	}
444e0549f8aSxypron.glpk@gmx.de 	r = EFI_INVALID_PARAMETER;
445e0549f8aSxypron.glpk@gmx.de out:
4468bee5a3cSxypron.glpk@gmx.de 	return r;
4478bee5a3cSxypron.glpk@gmx.de }
4488bee5a3cSxypron.glpk@gmx.de 
4498bee5a3cSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
4508bee5a3cSxypron.glpk@gmx.de 			efi_guid_t *protocol, int protocol_interface_type,
4518bee5a3cSxypron.glpk@gmx.de 			void *protocol_interface)
4528bee5a3cSxypron.glpk@gmx.de {
4538bee5a3cSxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
4548bee5a3cSxypron.glpk@gmx.de 		  protocol_interface);
4558bee5a3cSxypron.glpk@gmx.de 
4568bee5a3cSxypron.glpk@gmx.de 	return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
4578bee5a3cSxypron.glpk@gmx.de 						       protocol_interface_type,
4588bee5a3cSxypron.glpk@gmx.de 						       protocol_interface));
459e0549f8aSxypron.glpk@gmx.de }
460e0549f8aSxypron.glpk@gmx.de 
461bee91169SAlexander Graf static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
462bee91169SAlexander Graf 			efi_guid_t *protocol, void *old_interface,
463bee91169SAlexander Graf 			void *new_interface)
464bee91169SAlexander Graf {
465bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
466bee91169SAlexander Graf 		  new_interface);
467bee91169SAlexander Graf 	return EFI_EXIT(EFI_ACCESS_DENIED);
468bee91169SAlexander Graf }
469bee91169SAlexander Graf 
470bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
471bee91169SAlexander Graf 			efi_guid_t *protocol, void *protocol_interface)
472bee91169SAlexander Graf {
4734b6ed0d7Sxypron.glpk@gmx.de 	struct list_head *lhandle;
4744b6ed0d7Sxypron.glpk@gmx.de 	int i;
4754b6ed0d7Sxypron.glpk@gmx.de 	efi_status_t r = EFI_NOT_FOUND;
4764b6ed0d7Sxypron.glpk@gmx.de 
4774b6ed0d7Sxypron.glpk@gmx.de 	if (!handle || !protocol) {
4784b6ed0d7Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
4794b6ed0d7Sxypron.glpk@gmx.de 		goto out;
4804b6ed0d7Sxypron.glpk@gmx.de 	}
4814b6ed0d7Sxypron.glpk@gmx.de 
4824b6ed0d7Sxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
4834b6ed0d7Sxypron.glpk@gmx.de 		struct efi_object *efiobj;
4844b6ed0d7Sxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
4854b6ed0d7Sxypron.glpk@gmx.de 
4864b6ed0d7Sxypron.glpk@gmx.de 		if (efiobj->handle != handle)
4874b6ed0d7Sxypron.glpk@gmx.de 			continue;
4884b6ed0d7Sxypron.glpk@gmx.de 
4894b6ed0d7Sxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
4904b6ed0d7Sxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
4914b6ed0d7Sxypron.glpk@gmx.de 			const efi_guid_t *hprotocol = handler->guid;
4924b6ed0d7Sxypron.glpk@gmx.de 
4934b6ed0d7Sxypron.glpk@gmx.de 			if (!hprotocol)
4944b6ed0d7Sxypron.glpk@gmx.de 				continue;
4954b6ed0d7Sxypron.glpk@gmx.de 			if (!guidcmp(hprotocol, protocol)) {
4964b6ed0d7Sxypron.glpk@gmx.de 				if (handler->protocol_interface) {
4974b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_ACCESS_DENIED;
4984b6ed0d7Sxypron.glpk@gmx.de 				} else {
4994b6ed0d7Sxypron.glpk@gmx.de 					handler->guid = 0;
5004b6ed0d7Sxypron.glpk@gmx.de 					r = EFI_SUCCESS;
5014b6ed0d7Sxypron.glpk@gmx.de 				}
5024b6ed0d7Sxypron.glpk@gmx.de 				goto out;
5034b6ed0d7Sxypron.glpk@gmx.de 			}
5044b6ed0d7Sxypron.glpk@gmx.de 		}
5054b6ed0d7Sxypron.glpk@gmx.de 	}
5064b6ed0d7Sxypron.glpk@gmx.de 
5074b6ed0d7Sxypron.glpk@gmx.de out:
5083d8e1456Sxypron.glpk@gmx.de 	return r;
5093d8e1456Sxypron.glpk@gmx.de }
5103d8e1456Sxypron.glpk@gmx.de 
5113d8e1456Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
5123d8e1456Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *protocol_interface)
5133d8e1456Sxypron.glpk@gmx.de {
5143d8e1456Sxypron.glpk@gmx.de 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
5153d8e1456Sxypron.glpk@gmx.de 
5163d8e1456Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
5173d8e1456Sxypron.glpk@gmx.de 							 protocol_interface));
518bee91169SAlexander Graf }
519bee91169SAlexander Graf 
520bee91169SAlexander Graf static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
5212fd945feSxypron.glpk@gmx.de 							struct efi_event *event,
522bee91169SAlexander Graf 							void **registration)
523bee91169SAlexander Graf {
524bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
525bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
526bee91169SAlexander Graf }
527bee91169SAlexander Graf 
528bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
529bee91169SAlexander Graf 		      efi_guid_t *protocol, void *search_key,
530bee91169SAlexander Graf 		      struct efi_object *efiobj)
531bee91169SAlexander Graf {
532bee91169SAlexander Graf 	int i;
533bee91169SAlexander Graf 
534bee91169SAlexander Graf 	switch (search_type) {
535bee91169SAlexander Graf 	case all_handles:
536bee91169SAlexander Graf 		return 0;
537bee91169SAlexander Graf 	case by_register_notify:
538bee91169SAlexander Graf 		return -1;
539bee91169SAlexander Graf 	case by_protocol:
540bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
541bee91169SAlexander Graf 			const efi_guid_t *guid = efiobj->protocols[i].guid;
542bee91169SAlexander Graf 			if (guid && !guidcmp(guid, protocol))
543bee91169SAlexander Graf 				return 0;
544bee91169SAlexander Graf 		}
545bee91169SAlexander Graf 		return -1;
546bee91169SAlexander Graf 	}
547bee91169SAlexander Graf 
548bee91169SAlexander Graf 	return -1;
549bee91169SAlexander Graf }
550bee91169SAlexander Graf 
551bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle(
552bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
553bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
554bee91169SAlexander Graf 			unsigned long *buffer_size, efi_handle_t *buffer)
555bee91169SAlexander Graf {
556bee91169SAlexander Graf 	struct list_head *lhandle;
557bee91169SAlexander Graf 	unsigned long size = 0;
558bee91169SAlexander Graf 
559bee91169SAlexander Graf 	/* Count how much space we need */
560bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
561bee91169SAlexander Graf 		struct efi_object *efiobj;
562bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
563bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
564bee91169SAlexander Graf 			size += sizeof(void*);
565bee91169SAlexander Graf 		}
566bee91169SAlexander Graf 	}
567bee91169SAlexander Graf 
568bee91169SAlexander Graf 	if (*buffer_size < size) {
569bee91169SAlexander Graf 		*buffer_size = size;
57026329584Sxypron.glpk@gmx.de 		return EFI_BUFFER_TOO_SMALL;
571bee91169SAlexander Graf 	}
572bee91169SAlexander Graf 
573bee91169SAlexander Graf 	/* Then fill the array */
574bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
575bee91169SAlexander Graf 		struct efi_object *efiobj;
576bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
577bee91169SAlexander Graf 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
578bee91169SAlexander Graf 			*(buffer++) = efiobj->handle;
579bee91169SAlexander Graf 		}
580bee91169SAlexander Graf 	}
581bee91169SAlexander Graf 
582bee91169SAlexander Graf 	*buffer_size = size;
58326329584Sxypron.glpk@gmx.de 	return EFI_SUCCESS;
58426329584Sxypron.glpk@gmx.de }
58526329584Sxypron.glpk@gmx.de 
58626329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
58726329584Sxypron.glpk@gmx.de 			enum efi_locate_search_type search_type,
58826329584Sxypron.glpk@gmx.de 			efi_guid_t *protocol, void *search_key,
58926329584Sxypron.glpk@gmx.de 			unsigned long *buffer_size, efi_handle_t *buffer)
59026329584Sxypron.glpk@gmx.de {
59126329584Sxypron.glpk@gmx.de 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
59226329584Sxypron.glpk@gmx.de 		  buffer_size, buffer);
59326329584Sxypron.glpk@gmx.de 
59426329584Sxypron.glpk@gmx.de 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
59526329584Sxypron.glpk@gmx.de 			buffer_size, buffer));
596bee91169SAlexander Graf }
597bee91169SAlexander Graf 
598bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
599bee91169SAlexander Graf 			struct efi_device_path **device_path,
600bee91169SAlexander Graf 			efi_handle_t *device)
601bee91169SAlexander Graf {
602bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
603bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
604bee91169SAlexander Graf }
605bee91169SAlexander Graf 
606488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
607bee91169SAlexander Graf {
608bee91169SAlexander Graf 	int i;
609bee91169SAlexander Graf 
610bee91169SAlexander Graf 	/* Check for guid override */
611bee91169SAlexander Graf 	for (i = 0; i < systab.nr_tables; i++) {
612bee91169SAlexander Graf 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
613bee91169SAlexander Graf 			efi_conf_table[i].table = table;
614488bf12dSAlexander Graf 			return EFI_SUCCESS;
615bee91169SAlexander Graf 		}
616bee91169SAlexander Graf 	}
617bee91169SAlexander Graf 
618bee91169SAlexander Graf 	/* No override, check for overflow */
619bee91169SAlexander Graf 	if (i >= ARRAY_SIZE(efi_conf_table))
620488bf12dSAlexander Graf 		return EFI_OUT_OF_RESOURCES;
621bee91169SAlexander Graf 
622bee91169SAlexander Graf 	/* Add a new entry */
623bee91169SAlexander Graf 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
624bee91169SAlexander Graf 	efi_conf_table[i].table = table;
625aba5e919SAlexander Graf 	systab.nr_tables = i + 1;
626bee91169SAlexander Graf 
627488bf12dSAlexander Graf 	return EFI_SUCCESS;
628488bf12dSAlexander Graf }
629488bf12dSAlexander Graf 
630488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
631488bf12dSAlexander Graf 							       void *table)
632488bf12dSAlexander Graf {
633488bf12dSAlexander Graf 	EFI_ENTRY("%p, %p", guid, table);
634488bf12dSAlexander Graf 	return EFI_EXIT(efi_install_configuration_table(guid, table));
635bee91169SAlexander Graf }
636bee91169SAlexander Graf 
637bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
638bee91169SAlexander Graf 					  efi_handle_t parent_image,
639bee91169SAlexander Graf 					  struct efi_device_path *file_path,
640bee91169SAlexander Graf 					  void *source_buffer,
641bee91169SAlexander Graf 					  unsigned long source_size,
642bee91169SAlexander Graf 					  efi_handle_t *image_handle)
643bee91169SAlexander Graf {
644bee91169SAlexander Graf 	static struct efi_object loaded_image_info_obj = {
645bee91169SAlexander Graf 		.protocols = {
646bee91169SAlexander Graf 			{
647bee91169SAlexander Graf 				.guid = &efi_guid_loaded_image,
648bee91169SAlexander Graf 			},
649bee91169SAlexander Graf 		},
650bee91169SAlexander Graf 	};
651bee91169SAlexander Graf 	struct efi_loaded_image *info;
652bee91169SAlexander Graf 	struct efi_object *obj;
653bee91169SAlexander Graf 
654bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
655bee91169SAlexander Graf 		  file_path, source_buffer, source_size, image_handle);
656bee91169SAlexander Graf 	info = malloc(sizeof(*info));
657b5349f74Sxypron.glpk@gmx.de 	loaded_image_info_obj.protocols[0].protocol_interface = info;
658bee91169SAlexander Graf 	obj = malloc(sizeof(loaded_image_info_obj));
659bee91169SAlexander Graf 	memset(info, 0, sizeof(*info));
660bee91169SAlexander Graf 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
661bee91169SAlexander Graf 	obj->handle = info;
662bee91169SAlexander Graf 	info->file_path = file_path;
663bee91169SAlexander Graf 	info->reserved = efi_load_pe(source_buffer, info);
664bee91169SAlexander Graf 	if (!info->reserved) {
665bee91169SAlexander Graf 		free(info);
666bee91169SAlexander Graf 		free(obj);
667bee91169SAlexander Graf 		return EFI_EXIT(EFI_UNSUPPORTED);
668bee91169SAlexander Graf 	}
669bee91169SAlexander Graf 
670bee91169SAlexander Graf 	*image_handle = info;
671bee91169SAlexander Graf 	list_add_tail(&obj->link, &efi_obj_list);
672bee91169SAlexander Graf 
673bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
674bee91169SAlexander Graf }
675bee91169SAlexander Graf 
676bee91169SAlexander Graf static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
677bee91169SAlexander Graf 					   unsigned long *exit_data_size,
678bee91169SAlexander Graf 					   s16 **exit_data)
679bee91169SAlexander Graf {
680bee91169SAlexander Graf 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
681bee91169SAlexander Graf 	struct efi_loaded_image *info = image_handle;
682bee91169SAlexander Graf 
683bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
684bee91169SAlexander Graf 	entry = info->reserved;
685bee91169SAlexander Graf 
686bee91169SAlexander Graf 	efi_is_direct_boot = false;
687bee91169SAlexander Graf 
688bee91169SAlexander Graf 	/* call the image! */
689a86aeaf2SAlexander Graf 	if (setjmp(&info->exit_jmp)) {
690a86aeaf2SAlexander Graf 		/* We returned from the child image */
691a86aeaf2SAlexander Graf 		return EFI_EXIT(info->exit_status);
692a86aeaf2SAlexander Graf 	}
693a86aeaf2SAlexander Graf 
694bee91169SAlexander Graf 	entry(image_handle, &systab);
695bee91169SAlexander Graf 
696bee91169SAlexander Graf 	/* Should usually never get here */
697bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
698bee91169SAlexander Graf }
699bee91169SAlexander Graf 
700a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
701a86aeaf2SAlexander Graf 			efi_status_t exit_status, unsigned long exit_data_size,
702a86aeaf2SAlexander Graf 			int16_t *exit_data)
703bee91169SAlexander Graf {
704a86aeaf2SAlexander Graf 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
705a86aeaf2SAlexander Graf 
706bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
707bee91169SAlexander Graf 		  exit_data_size, exit_data);
708a86aeaf2SAlexander Graf 
709a86aeaf2SAlexander Graf 	loaded_image_info->exit_status = exit_status;
710692fcdd8SAlexander Graf 	longjmp(&loaded_image_info->exit_jmp, 1);
711a86aeaf2SAlexander Graf 
712a86aeaf2SAlexander Graf 	panic("EFI application exited");
713bee91169SAlexander Graf }
714bee91169SAlexander Graf 
715bee91169SAlexander Graf static struct efi_object *efi_search_obj(void *handle)
716bee91169SAlexander Graf {
717bee91169SAlexander Graf 	struct list_head *lhandle;
718bee91169SAlexander Graf 
719bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
720bee91169SAlexander Graf 		struct efi_object *efiobj;
721bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
722bee91169SAlexander Graf 		if (efiobj->handle == handle)
723bee91169SAlexander Graf 			return efiobj;
724bee91169SAlexander Graf 	}
725bee91169SAlexander Graf 
726bee91169SAlexander Graf 	return NULL;
727bee91169SAlexander Graf }
728bee91169SAlexander Graf 
729bee91169SAlexander Graf static efi_status_t EFIAPI efi_unload_image(void *image_handle)
730bee91169SAlexander Graf {
731bee91169SAlexander Graf 	struct efi_object *efiobj;
732bee91169SAlexander Graf 
733bee91169SAlexander Graf 	EFI_ENTRY("%p", image_handle);
734bee91169SAlexander Graf 	efiobj = efi_search_obj(image_handle);
735bee91169SAlexander Graf 	if (efiobj)
736bee91169SAlexander Graf 		list_del(&efiobj->link);
737bee91169SAlexander Graf 
738bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
739bee91169SAlexander Graf }
740bee91169SAlexander Graf 
741bee91169SAlexander Graf static void efi_exit_caches(void)
742bee91169SAlexander Graf {
743bee91169SAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
744bee91169SAlexander Graf 	/*
745bee91169SAlexander Graf 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
746bee91169SAlexander Graf 	 * a zImage, but does not know of all cache layers. Give it a hand.
747bee91169SAlexander Graf 	 */
748bee91169SAlexander Graf 	if (efi_is_direct_boot)
749bee91169SAlexander Graf 		cleanup_before_linux();
750bee91169SAlexander Graf #endif
751bee91169SAlexander Graf }
752bee91169SAlexander Graf 
753bee91169SAlexander Graf static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
754bee91169SAlexander Graf 						  unsigned long map_key)
755bee91169SAlexander Graf {
756bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", image_handle, map_key);
757bee91169SAlexander Graf 
758b7b8410aSAlexander Graf 	board_quiesce_devices();
759b7b8410aSAlexander Graf 
760bee91169SAlexander Graf 	/* Fix up caches for EFI payloads if necessary */
761bee91169SAlexander Graf 	efi_exit_caches();
762bee91169SAlexander Graf 
763bee91169SAlexander Graf 	/* This stops all lingering devices */
764bee91169SAlexander Graf 	bootm_disable_interrupts();
765bee91169SAlexander Graf 
766bee91169SAlexander Graf 	/* Give the payload some time to boot */
767bee91169SAlexander Graf 	WATCHDOG_RESET();
768bee91169SAlexander Graf 
769bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
770bee91169SAlexander Graf }
771bee91169SAlexander Graf 
772bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
773bee91169SAlexander Graf {
774bee91169SAlexander Graf 	static uint64_t mono = 0;
775bee91169SAlexander Graf 	EFI_ENTRY("%p", count);
776bee91169SAlexander Graf 	*count = mono++;
777bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
778bee91169SAlexander Graf }
779bee91169SAlexander Graf 
780bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
781bee91169SAlexander Graf {
782bee91169SAlexander Graf 	EFI_ENTRY("%ld", microseconds);
783bee91169SAlexander Graf 	udelay(microseconds);
784bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
785bee91169SAlexander Graf }
786bee91169SAlexander Graf 
787bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
788bee91169SAlexander Graf 						  uint64_t watchdog_code,
789bee91169SAlexander Graf 						  unsigned long data_size,
790bee91169SAlexander Graf 						  uint16_t *watchdog_data)
791bee91169SAlexander Graf {
792bee91169SAlexander Graf 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
793bee91169SAlexander Graf 		  data_size, watchdog_data);
794bee91169SAlexander Graf 	return EFI_EXIT(efi_unsupported(__func__));
795bee91169SAlexander Graf }
796bee91169SAlexander Graf 
797bee91169SAlexander Graf static efi_status_t EFIAPI efi_connect_controller(
798bee91169SAlexander Graf 			efi_handle_t controller_handle,
799bee91169SAlexander Graf 			efi_handle_t *driver_image_handle,
800bee91169SAlexander Graf 			struct efi_device_path *remain_device_path,
801bee91169SAlexander Graf 			bool recursive)
802bee91169SAlexander Graf {
803bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
804bee91169SAlexander Graf 		  remain_device_path, recursive);
805bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
806bee91169SAlexander Graf }
807bee91169SAlexander Graf 
808bee91169SAlexander Graf static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
809bee91169SAlexander Graf 						     void *driver_image_handle,
810bee91169SAlexander Graf 						     void *child_handle)
811bee91169SAlexander Graf {
812bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
813bee91169SAlexander Graf 		  child_handle);
814bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
815bee91169SAlexander Graf }
816bee91169SAlexander Graf 
817bee91169SAlexander Graf static efi_status_t EFIAPI efi_close_protocol(void *handle,
818bee91169SAlexander Graf 					      efi_guid_t *protocol,
819bee91169SAlexander Graf 					      void *agent_handle,
820bee91169SAlexander Graf 					      void *controller_handle)
821bee91169SAlexander Graf {
822bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
823bee91169SAlexander Graf 		  controller_handle);
824bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
825bee91169SAlexander Graf }
826bee91169SAlexander Graf 
827bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
828bee91169SAlexander Graf 			efi_guid_t *protocol,
829bee91169SAlexander Graf 			struct efi_open_protocol_info_entry **entry_buffer,
830bee91169SAlexander Graf 			unsigned long *entry_count)
831bee91169SAlexander Graf {
832bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
833bee91169SAlexander Graf 		  entry_count);
834bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
835bee91169SAlexander Graf }
836bee91169SAlexander Graf 
837bee91169SAlexander Graf static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
838bee91169SAlexander Graf 			efi_guid_t ***protocol_buffer,
839bee91169SAlexander Graf 			unsigned long *protocol_buffer_count)
840bee91169SAlexander Graf {
841bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
842bee91169SAlexander Graf 		  protocol_buffer_count);
843bee91169SAlexander Graf 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
844bee91169SAlexander Graf }
845bee91169SAlexander Graf 
846bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
847bee91169SAlexander Graf 			enum efi_locate_search_type search_type,
848bee91169SAlexander Graf 			efi_guid_t *protocol, void *search_key,
849bee91169SAlexander Graf 			unsigned long *no_handles, efi_handle_t **buffer)
850bee91169SAlexander Graf {
851c2e703f9Sxypron.glpk@gmx.de 	efi_status_t r;
852c2e703f9Sxypron.glpk@gmx.de 	unsigned long buffer_size = 0;
853c2e703f9Sxypron.glpk@gmx.de 
854bee91169SAlexander Graf 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
855bee91169SAlexander Graf 		  no_handles, buffer);
856c2e703f9Sxypron.glpk@gmx.de 
857c2e703f9Sxypron.glpk@gmx.de 	if (!no_handles || !buffer) {
858c2e703f9Sxypron.glpk@gmx.de 		r = EFI_INVALID_PARAMETER;
859c2e703f9Sxypron.glpk@gmx.de 		goto out;
860c2e703f9Sxypron.glpk@gmx.de 	}
861c2e703f9Sxypron.glpk@gmx.de 	*no_handles = 0;
862c2e703f9Sxypron.glpk@gmx.de 	*buffer = NULL;
863c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
864c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
865c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_BUFFER_TOO_SMALL)
866c2e703f9Sxypron.glpk@gmx.de 		goto out;
867c2e703f9Sxypron.glpk@gmx.de 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
868c2e703f9Sxypron.glpk@gmx.de 			      (void **)buffer);
869c2e703f9Sxypron.glpk@gmx.de 	if (r != EFI_SUCCESS)
870c2e703f9Sxypron.glpk@gmx.de 		goto out;
871c2e703f9Sxypron.glpk@gmx.de 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
872c2e703f9Sxypron.glpk@gmx.de 			      *buffer);
873c2e703f9Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
874c2e703f9Sxypron.glpk@gmx.de 		*no_handles = buffer_size / sizeof(void *);
875c2e703f9Sxypron.glpk@gmx.de out:
876c2e703f9Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
877bee91169SAlexander Graf }
878bee91169SAlexander Graf 
879bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
880bee91169SAlexander Graf 					       void *registration,
881bee91169SAlexander Graf 					       void **protocol_interface)
882bee91169SAlexander Graf {
88388adae5eSxypron.glpk@gmx.de 	struct list_head *lhandle;
884bee91169SAlexander Graf 	int i;
885bee91169SAlexander Graf 
886bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
88788adae5eSxypron.glpk@gmx.de 
88888adae5eSxypron.glpk@gmx.de 	if (!protocol || !protocol_interface)
88988adae5eSxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
89088adae5eSxypron.glpk@gmx.de 
89188adae5eSxypron.glpk@gmx.de 	list_for_each(lhandle, &efi_obj_list) {
89288adae5eSxypron.glpk@gmx.de 		struct efi_object *efiobj;
89388adae5eSxypron.glpk@gmx.de 
89488adae5eSxypron.glpk@gmx.de 		efiobj = list_entry(lhandle, struct efi_object, link);
89588adae5eSxypron.glpk@gmx.de 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
89688adae5eSxypron.glpk@gmx.de 			struct efi_handler *handler = &efiobj->protocols[i];
89788adae5eSxypron.glpk@gmx.de 
89888adae5eSxypron.glpk@gmx.de 			if (!handler->guid)
89988adae5eSxypron.glpk@gmx.de 				continue;
90088adae5eSxypron.glpk@gmx.de 			if (!guidcmp(handler->guid, protocol)) {
90188adae5eSxypron.glpk@gmx.de 				*protocol_interface =
90288adae5eSxypron.glpk@gmx.de 					handler->protocol_interface;
903bee91169SAlexander Graf 				return EFI_EXIT(EFI_SUCCESS);
904bee91169SAlexander Graf 			}
905bee91169SAlexander Graf 		}
90688adae5eSxypron.glpk@gmx.de 	}
90788adae5eSxypron.glpk@gmx.de 	*protocol_interface = NULL;
908bee91169SAlexander Graf 
909bee91169SAlexander Graf 	return EFI_EXIT(EFI_NOT_FOUND);
910bee91169SAlexander Graf }
911bee91169SAlexander Graf 
912bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
913bee91169SAlexander Graf 			void **handle, ...)
914bee91169SAlexander Graf {
915bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
91658b83586Sxypron.glpk@gmx.de 
91758b83586Sxypron.glpk@gmx.de 	va_list argptr;
91858b83586Sxypron.glpk@gmx.de 	efi_guid_t *protocol;
91958b83586Sxypron.glpk@gmx.de 	void *protocol_interface;
92058b83586Sxypron.glpk@gmx.de 	efi_status_t r = EFI_SUCCESS;
92158b83586Sxypron.glpk@gmx.de 	int i = 0;
92258b83586Sxypron.glpk@gmx.de 
92358b83586Sxypron.glpk@gmx.de 	if (!handle)
92458b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(EFI_INVALID_PARAMETER);
92558b83586Sxypron.glpk@gmx.de 
92658b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
92758b83586Sxypron.glpk@gmx.de 	for (;;) {
92858b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
92958b83586Sxypron.glpk@gmx.de 		if (!protocol)
93058b83586Sxypron.glpk@gmx.de 			break;
93158b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
93258b83586Sxypron.glpk@gmx.de 		r = efi_install_protocol_interface(handle, protocol,
93358b83586Sxypron.glpk@gmx.de 						   EFI_NATIVE_INTERFACE,
93458b83586Sxypron.glpk@gmx.de 						   protocol_interface);
93558b83586Sxypron.glpk@gmx.de 		if (r != EFI_SUCCESS)
93658b83586Sxypron.glpk@gmx.de 			break;
93758b83586Sxypron.glpk@gmx.de 		i++;
93858b83586Sxypron.glpk@gmx.de 	}
93958b83586Sxypron.glpk@gmx.de 	va_end(argptr);
94058b83586Sxypron.glpk@gmx.de 	if (r == EFI_SUCCESS)
94158b83586Sxypron.glpk@gmx.de 		return EFI_EXIT(r);
94258b83586Sxypron.glpk@gmx.de 
94358b83586Sxypron.glpk@gmx.de 	/* If an error occured undo all changes. */
94458b83586Sxypron.glpk@gmx.de 	va_start(argptr, handle);
94558b83586Sxypron.glpk@gmx.de 	for (; i; --i) {
94658b83586Sxypron.glpk@gmx.de 		protocol = va_arg(argptr, efi_guid_t*);
94758b83586Sxypron.glpk@gmx.de 		protocol_interface = va_arg(argptr, void*);
94858b83586Sxypron.glpk@gmx.de 		efi_uninstall_protocol_interface(handle, protocol,
94958b83586Sxypron.glpk@gmx.de 						 protocol_interface);
95058b83586Sxypron.glpk@gmx.de 	}
95158b83586Sxypron.glpk@gmx.de 	va_end(argptr);
95258b83586Sxypron.glpk@gmx.de 
95358b83586Sxypron.glpk@gmx.de 	return EFI_EXIT(r);
954bee91169SAlexander Graf }
955bee91169SAlexander Graf 
956bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
957bee91169SAlexander Graf 			void *handle, ...)
958bee91169SAlexander Graf {
959bee91169SAlexander Graf 	EFI_ENTRY("%p", handle);
960bee91169SAlexander Graf 	return EFI_EXIT(EFI_INVALID_PARAMETER);
961bee91169SAlexander Graf }
962bee91169SAlexander Graf 
963bee91169SAlexander Graf static efi_status_t EFIAPI efi_calculate_crc32(void *data,
964bee91169SAlexander Graf 					       unsigned long data_size,
965bee91169SAlexander Graf 					       uint32_t *crc32_p)
966bee91169SAlexander Graf {
967bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld", data, data_size);
968bee91169SAlexander Graf 	*crc32_p = crc32(0, data, data_size);
969bee91169SAlexander Graf 	return EFI_EXIT(EFI_SUCCESS);
970bee91169SAlexander Graf }
971bee91169SAlexander Graf 
972bee91169SAlexander Graf static void EFIAPI efi_copy_mem(void *destination, void *source,
973bee91169SAlexander Graf 				unsigned long length)
974bee91169SAlexander Graf {
975bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
976bee91169SAlexander Graf 	memcpy(destination, source, length);
977bee91169SAlexander Graf }
978bee91169SAlexander Graf 
979bee91169SAlexander Graf static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
980bee91169SAlexander Graf {
981bee91169SAlexander Graf 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
982bee91169SAlexander Graf 	memset(buffer, value, size);
983bee91169SAlexander Graf }
984bee91169SAlexander Graf 
985bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol(
986bee91169SAlexander Graf 			void *handle, efi_guid_t *protocol,
987bee91169SAlexander Graf 			void **protocol_interface, void *agent_handle,
988bee91169SAlexander Graf 			void *controller_handle, uint32_t attributes)
989bee91169SAlexander Graf {
990bee91169SAlexander Graf 	struct list_head *lhandle;
991bee91169SAlexander Graf 	int i;
99269baec67Sxypron.glpk@gmx.de 	efi_status_t r = EFI_INVALID_PARAMETER;
993bee91169SAlexander Graf 
994bee91169SAlexander Graf 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
995bee91169SAlexander Graf 		  protocol_interface, agent_handle, controller_handle,
996bee91169SAlexander Graf 		  attributes);
997b5349f74Sxypron.glpk@gmx.de 
99869baec67Sxypron.glpk@gmx.de 	if (!handle || !protocol ||
99969baec67Sxypron.glpk@gmx.de 	    (!protocol_interface && attributes !=
100069baec67Sxypron.glpk@gmx.de 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
100169baec67Sxypron.glpk@gmx.de 		goto out;
100269baec67Sxypron.glpk@gmx.de 	}
100369baec67Sxypron.glpk@gmx.de 
100469baec67Sxypron.glpk@gmx.de 	switch (attributes) {
100569baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
100669baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
100769baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
100869baec67Sxypron.glpk@gmx.de 		break;
100969baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
101069baec67Sxypron.glpk@gmx.de 		if (controller_handle == handle)
101169baec67Sxypron.glpk@gmx.de 			goto out;
101269baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
101369baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
101469baec67Sxypron.glpk@gmx.de 		if (controller_handle == NULL)
101569baec67Sxypron.glpk@gmx.de 			goto out;
101669baec67Sxypron.glpk@gmx.de 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
101769baec67Sxypron.glpk@gmx.de 		if (agent_handle == NULL)
101869baec67Sxypron.glpk@gmx.de 			goto out;
101969baec67Sxypron.glpk@gmx.de 		break;
102069baec67Sxypron.glpk@gmx.de 	default:
1021b5349f74Sxypron.glpk@gmx.de 		goto out;
1022b5349f74Sxypron.glpk@gmx.de 	}
1023b5349f74Sxypron.glpk@gmx.de 
1024bee91169SAlexander Graf 	list_for_each(lhandle, &efi_obj_list) {
1025bee91169SAlexander Graf 		struct efi_object *efiobj;
1026bee91169SAlexander Graf 		efiobj = list_entry(lhandle, struct efi_object, link);
1027bee91169SAlexander Graf 
1028bee91169SAlexander Graf 		if (efiobj->handle != handle)
1029bee91169SAlexander Graf 			continue;
1030bee91169SAlexander Graf 
1031bee91169SAlexander Graf 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1032bee91169SAlexander Graf 			struct efi_handler *handler = &efiobj->protocols[i];
1033bee91169SAlexander Graf 			const efi_guid_t *hprotocol = handler->guid;
1034bee91169SAlexander Graf 			if (!hprotocol)
10354b6ed0d7Sxypron.glpk@gmx.de 				continue;
1036bee91169SAlexander Graf 			if (!guidcmp(hprotocol, protocol)) {
1037b5349f74Sxypron.glpk@gmx.de 				if (attributes !=
1038b5349f74Sxypron.glpk@gmx.de 				    EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1039b5349f74Sxypron.glpk@gmx.de 					*protocol_interface =
1040b5349f74Sxypron.glpk@gmx.de 						handler->protocol_interface;
1041b5349f74Sxypron.glpk@gmx.de 				}
1042b5349f74Sxypron.glpk@gmx.de 				r = EFI_SUCCESS;
1043bee91169SAlexander Graf 				goto out;
1044bee91169SAlexander Graf 			}
1045bee91169SAlexander Graf 		}
104669baec67Sxypron.glpk@gmx.de 		goto unsupported;
1047bee91169SAlexander Graf 	}
1048bee91169SAlexander Graf 
104969baec67Sxypron.glpk@gmx.de unsupported:
105069baec67Sxypron.glpk@gmx.de 	r = EFI_UNSUPPORTED;
1051bee91169SAlexander Graf out:
1052bee91169SAlexander Graf 	return EFI_EXIT(r);
1053bee91169SAlexander Graf }
1054bee91169SAlexander Graf 
1055bee91169SAlexander Graf static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1056bee91169SAlexander Graf 					       efi_guid_t *protocol,
1057bee91169SAlexander Graf 					       void **protocol_interface)
1058bee91169SAlexander Graf {
10598e1d329fSxypron.glpk@gmx.de 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
10608e1d329fSxypron.glpk@gmx.de 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1061bee91169SAlexander Graf }
1062bee91169SAlexander Graf 
1063bee91169SAlexander Graf static const struct efi_boot_services efi_boot_services = {
1064bee91169SAlexander Graf 	.hdr = {
1065bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1066bee91169SAlexander Graf 	},
1067bee91169SAlexander Graf 	.raise_tpl = efi_raise_tpl,
1068bee91169SAlexander Graf 	.restore_tpl = efi_restore_tpl,
1069bee91169SAlexander Graf 	.allocate_pages = efi_allocate_pages_ext,
1070bee91169SAlexander Graf 	.free_pages = efi_free_pages_ext,
1071bee91169SAlexander Graf 	.get_memory_map = efi_get_memory_map_ext,
1072ead1274bSStefan Brüns 	.allocate_pool = efi_allocate_pool_ext,
107342417bc8SStefan Brüns 	.free_pool = efi_free_pool_ext,
107449deb455Sxypron.glpk@gmx.de 	.create_event = efi_create_event_ext,
1075*bfc72462Sxypron.glpk@gmx.de 	.set_timer = efi_set_timer_ext,
1076bee91169SAlexander Graf 	.wait_for_event = efi_wait_for_event,
1077c6841592Sxypron.glpk@gmx.de 	.signal_event = efi_signal_event_ext,
1078bee91169SAlexander Graf 	.close_event = efi_close_event,
1079bee91169SAlexander Graf 	.check_event = efi_check_event,
10808bee5a3cSxypron.glpk@gmx.de 	.install_protocol_interface = efi_install_protocol_interface_ext,
1081bee91169SAlexander Graf 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
10823d8e1456Sxypron.glpk@gmx.de 	.uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1083bee91169SAlexander Graf 	.handle_protocol = efi_handle_protocol,
1084bee91169SAlexander Graf 	.reserved = NULL,
1085bee91169SAlexander Graf 	.register_protocol_notify = efi_register_protocol_notify,
108626329584Sxypron.glpk@gmx.de 	.locate_handle = efi_locate_handle_ext,
1087bee91169SAlexander Graf 	.locate_device_path = efi_locate_device_path,
1088488bf12dSAlexander Graf 	.install_configuration_table = efi_install_configuration_table_ext,
1089bee91169SAlexander Graf 	.load_image = efi_load_image,
1090bee91169SAlexander Graf 	.start_image = efi_start_image,
1091a86aeaf2SAlexander Graf 	.exit = efi_exit,
1092bee91169SAlexander Graf 	.unload_image = efi_unload_image,
1093bee91169SAlexander Graf 	.exit_boot_services = efi_exit_boot_services,
1094bee91169SAlexander Graf 	.get_next_monotonic_count = efi_get_next_monotonic_count,
1095bee91169SAlexander Graf 	.stall = efi_stall,
1096bee91169SAlexander Graf 	.set_watchdog_timer = efi_set_watchdog_timer,
1097bee91169SAlexander Graf 	.connect_controller = efi_connect_controller,
1098bee91169SAlexander Graf 	.disconnect_controller = efi_disconnect_controller,
1099bee91169SAlexander Graf 	.open_protocol = efi_open_protocol,
1100bee91169SAlexander Graf 	.close_protocol = efi_close_protocol,
1101bee91169SAlexander Graf 	.open_protocol_information = efi_open_protocol_information,
1102bee91169SAlexander Graf 	.protocols_per_handle = efi_protocols_per_handle,
1103bee91169SAlexander Graf 	.locate_handle_buffer = efi_locate_handle_buffer,
1104bee91169SAlexander Graf 	.locate_protocol = efi_locate_protocol,
1105bee91169SAlexander Graf 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1106bee91169SAlexander Graf 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1107bee91169SAlexander Graf 	.calculate_crc32 = efi_calculate_crc32,
1108bee91169SAlexander Graf 	.copy_mem = efi_copy_mem,
1109bee91169SAlexander Graf 	.set_mem = efi_set_mem,
1110bee91169SAlexander Graf };
1111bee91169SAlexander Graf 
1112bee91169SAlexander Graf 
11133c63db9cSAlexander Graf static uint16_t __efi_runtime_data firmware_vendor[] =
1114bee91169SAlexander Graf 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
1115bee91169SAlexander Graf 
11163c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
1117bee91169SAlexander Graf 	.hdr = {
1118bee91169SAlexander Graf 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
1119bee91169SAlexander Graf 		.revision = 0x20005, /* 2.5 */
1120bee91169SAlexander Graf 		.headersize = sizeof(struct efi_table_hdr),
1121bee91169SAlexander Graf 	},
1122bee91169SAlexander Graf 	.fw_vendor = (long)firmware_vendor,
1123bee91169SAlexander Graf 	.con_in = (void*)&efi_con_in,
1124bee91169SAlexander Graf 	.con_out = (void*)&efi_con_out,
1125bee91169SAlexander Graf 	.std_err = (void*)&efi_con_out,
1126bee91169SAlexander Graf 	.runtime = (void*)&efi_runtime_services,
1127bee91169SAlexander Graf 	.boottime = (void*)&efi_boot_services,
1128bee91169SAlexander Graf 	.nr_tables = 0,
1129bee91169SAlexander Graf 	.tables = (void*)efi_conf_table,
1130bee91169SAlexander Graf };
1131