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>
130e00a84cSMasahiro Yamada #include <linux/libfdt_env.h>
14bee91169SAlexander Graf #include <u-boot/crc.h>
15bee91169SAlexander Graf #include <bootm.h>
16bee91169SAlexander Graf #include <inttypes.h>
17bee91169SAlexander Graf #include <watchdog.h>
18bee91169SAlexander Graf
19bee91169SAlexander Graf DECLARE_GLOBAL_DATA_PTR;
20bee91169SAlexander Graf
21bee91169SAlexander Graf /* This list contains all the EFI objects our payload has access to */
22bee91169SAlexander Graf LIST_HEAD(efi_obj_list);
23bee91169SAlexander Graf
24bee91169SAlexander Graf /*
25bee91169SAlexander Graf * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26bee91169SAlexander Graf * we need to do trickery with caches. Since we don't want to break the EFI
27bee91169SAlexander Graf * aware boot path, only apply hacks when loading exiting directly (breaking
28bee91169SAlexander Graf * direct Linux EFI booting along the way - oh well).
29bee91169SAlexander Graf */
30bee91169SAlexander Graf static bool efi_is_direct_boot = true;
31bee91169SAlexander Graf
32bee91169SAlexander Graf /*
33bee91169SAlexander Graf * EFI can pass arbitrary additional "tables" containing vendor specific
34bee91169SAlexander Graf * information to the payload. One such table is the FDT table which contains
35bee91169SAlexander Graf * a pointer to a flattened device tree blob.
36bee91169SAlexander Graf *
37bee91169SAlexander Graf * In most cases we want to pass an FDT to the payload, so reserve one slot of
38bee91169SAlexander Graf * config table space for it. The pointer gets populated by do_bootefi_exec().
39bee91169SAlexander Graf */
403c63db9cSAlexander Graf static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
41bee91169SAlexander Graf
4265e4c0b1SSimon Glass #ifdef CONFIG_ARM
43bee91169SAlexander Graf /*
44bee91169SAlexander Graf * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45bee91169SAlexander Graf * fixed when compiling U-Boot. However, the payload does not know about that
46bee91169SAlexander Graf * restriction so we need to manually swap its and our view of that register on
47bee91169SAlexander Graf * EFI callback entry/exit.
48bee91169SAlexander Graf */
49bee91169SAlexander Graf static volatile void *efi_gd, *app_gd;
5065e4c0b1SSimon Glass #endif
51bee91169SAlexander Graf
52c160d2f5SRob Clark static int entry_count;
53af65db85SRob Clark static int nesting_level;
54c160d2f5SRob Clark
55c160d2f5SRob Clark /* Called on every callback entry */
__efi_entry_check(void)56c160d2f5SRob Clark int __efi_entry_check(void)
57c160d2f5SRob Clark {
58c160d2f5SRob Clark int ret = entry_count++ == 0;
59c160d2f5SRob Clark #ifdef CONFIG_ARM
60c160d2f5SRob Clark assert(efi_gd);
61c160d2f5SRob Clark app_gd = gd;
62c160d2f5SRob Clark gd = efi_gd;
63c160d2f5SRob Clark #endif
64c160d2f5SRob Clark return ret;
65c160d2f5SRob Clark }
66c160d2f5SRob Clark
67c160d2f5SRob Clark /* Called on every callback exit */
__efi_exit_check(void)68c160d2f5SRob Clark int __efi_exit_check(void)
69c160d2f5SRob Clark {
70c160d2f5SRob Clark int ret = --entry_count == 0;
71c160d2f5SRob Clark #ifdef CONFIG_ARM
72c160d2f5SRob Clark gd = app_gd;
73c160d2f5SRob Clark #endif
74c160d2f5SRob Clark return ret;
75c160d2f5SRob Clark }
76c160d2f5SRob Clark
77bee91169SAlexander Graf /* Called from do_bootefi_exec() */
efi_save_gd(void)78bee91169SAlexander Graf void efi_save_gd(void)
79bee91169SAlexander Graf {
8065e4c0b1SSimon Glass #ifdef CONFIG_ARM
81bee91169SAlexander Graf efi_gd = gd;
8265e4c0b1SSimon Glass #endif
83bee91169SAlexander Graf }
84bee91169SAlexander Graf
85c160d2f5SRob Clark /*
86c160d2f5SRob Clark * Special case handler for error/abort that just forces things back
87c160d2f5SRob Clark * to u-boot world so we can dump out an abort msg, without any care
88c160d2f5SRob Clark * about returning back to UEFI world.
89c160d2f5SRob Clark */
efi_restore_gd(void)90bee91169SAlexander Graf void efi_restore_gd(void)
91bee91169SAlexander Graf {
9265e4c0b1SSimon Glass #ifdef CONFIG_ARM
93bee91169SAlexander Graf /* Only restore if we're already in EFI context */
94bee91169SAlexander Graf if (!efi_gd)
95bee91169SAlexander Graf return;
96bee91169SAlexander Graf gd = efi_gd;
9765e4c0b1SSimon Glass #endif
98bee91169SAlexander Graf }
99bee91169SAlexander Graf
100af65db85SRob Clark /*
101af65db85SRob Clark * Two spaces per indent level, maxing out at 10.. which ought to be
102af65db85SRob Clark * enough for anyone ;-)
103af65db85SRob Clark */
indent_string(int level)104af65db85SRob Clark static const char *indent_string(int level)
105af65db85SRob Clark {
106af65db85SRob Clark const char *indent = " ";
107af65db85SRob Clark const int max = strlen(indent);
108af65db85SRob Clark level = min(max, level * 2);
109af65db85SRob Clark return &indent[max - level];
110af65db85SRob Clark }
111af65db85SRob Clark
__efi_nesting_inc(void)112af65db85SRob Clark const char *__efi_nesting_inc(void)
113af65db85SRob Clark {
114af65db85SRob Clark return indent_string(nesting_level++);
115af65db85SRob Clark }
116af65db85SRob Clark
__efi_nesting_dec(void)117af65db85SRob Clark const char *__efi_nesting_dec(void)
118af65db85SRob Clark {
119af65db85SRob Clark return indent_string(--nesting_level);
120af65db85SRob Clark }
121af65db85SRob Clark
1228787b02eSxypron.glpk@gmx.de /* Low 32 bit */
1238787b02eSxypron.glpk@gmx.de #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
1248787b02eSxypron.glpk@gmx.de /* High 32 bit */
1258787b02eSxypron.glpk@gmx.de #define EFI_HIGH32(a) (a >> 32)
1268787b02eSxypron.glpk@gmx.de
1278787b02eSxypron.glpk@gmx.de /*
1288787b02eSxypron.glpk@gmx.de * 64bit division by 10 implemented as multiplication by 1 / 10
1298787b02eSxypron.glpk@gmx.de *
1308787b02eSxypron.glpk@gmx.de * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
1318787b02eSxypron.glpk@gmx.de */
1328787b02eSxypron.glpk@gmx.de #define EFI_TENTH 0x199999999999999A
efi_div10(u64 a)1338787b02eSxypron.glpk@gmx.de static u64 efi_div10(u64 a)
1348787b02eSxypron.glpk@gmx.de {
1358787b02eSxypron.glpk@gmx.de u64 prod;
1368787b02eSxypron.glpk@gmx.de u64 rem;
1378787b02eSxypron.glpk@gmx.de u64 ret;
1388787b02eSxypron.glpk@gmx.de
1398787b02eSxypron.glpk@gmx.de ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
1408787b02eSxypron.glpk@gmx.de prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
1418787b02eSxypron.glpk@gmx.de rem = EFI_LOW32(prod);
1428787b02eSxypron.glpk@gmx.de ret += EFI_HIGH32(prod);
1438787b02eSxypron.glpk@gmx.de prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
1448787b02eSxypron.glpk@gmx.de rem += EFI_LOW32(prod);
1458787b02eSxypron.glpk@gmx.de ret += EFI_HIGH32(prod);
1468787b02eSxypron.glpk@gmx.de prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
1478787b02eSxypron.glpk@gmx.de rem += EFI_HIGH32(prod);
1488787b02eSxypron.glpk@gmx.de ret += EFI_HIGH32(rem);
1498787b02eSxypron.glpk@gmx.de /* Round to nearest integer */
1508787b02eSxypron.glpk@gmx.de if (rem >= (1 << 31))
1518787b02eSxypron.glpk@gmx.de ++ret;
1528787b02eSxypron.glpk@gmx.de return ret;
1538787b02eSxypron.glpk@gmx.de }
1548787b02eSxypron.glpk@gmx.de
efi_signal_event(struct efi_event * event)15591be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event)
156c6841592Sxypron.glpk@gmx.de {
157c6841592Sxypron.glpk@gmx.de if (event->signaled)
158c6841592Sxypron.glpk@gmx.de return;
159c6841592Sxypron.glpk@gmx.de event->signaled = 1;
160c6841592Sxypron.glpk@gmx.de if (event->type & EVT_NOTIFY_SIGNAL) {
161a095aadfSRob Clark EFI_CALL(event->notify_function(event, event->notify_context));
162c6841592Sxypron.glpk@gmx.de }
163c6841592Sxypron.glpk@gmx.de }
164c6841592Sxypron.glpk@gmx.de
efi_unsupported(const char * funcname)165bee91169SAlexander Graf static efi_status_t efi_unsupported(const char *funcname)
166bee91169SAlexander Graf {
167edcef3baSAlexander Graf debug("EFI: App called into unimplemented function %s\n", funcname);
168bee91169SAlexander Graf return EFI_EXIT(EFI_UNSUPPORTED);
169bee91169SAlexander Graf }
170bee91169SAlexander Graf
efi_raise_tpl(UINTN new_tpl)171503f2695Sxypron.glpk@gmx.de static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
172bee91169SAlexander Graf {
173503f2695Sxypron.glpk@gmx.de EFI_ENTRY("0x%zx", new_tpl);
174bee91169SAlexander Graf return EFI_EXIT(0);
175bee91169SAlexander Graf }
176bee91169SAlexander Graf
efi_restore_tpl(UINTN old_tpl)177503f2695Sxypron.glpk@gmx.de static void EFIAPI efi_restore_tpl(UINTN old_tpl)
178bee91169SAlexander Graf {
179503f2695Sxypron.glpk@gmx.de EFI_ENTRY("0x%zx", old_tpl);
180b5104821SRob Clark efi_unsupported(__func__);
181bee91169SAlexander Graf }
182bee91169SAlexander Graf
efi_allocate_pages_ext(int type,int memory_type,unsigned long pages,uint64_t * memory)1836e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
184bee91169SAlexander Graf unsigned long pages,
185bee91169SAlexander Graf uint64_t *memory)
186bee91169SAlexander Graf {
187bee91169SAlexander Graf efi_status_t r;
188bee91169SAlexander Graf
189bee91169SAlexander Graf EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
190bee91169SAlexander Graf r = efi_allocate_pages(type, memory_type, pages, memory);
191bee91169SAlexander Graf return EFI_EXIT(r);
192bee91169SAlexander Graf }
193bee91169SAlexander Graf
efi_free_pages_ext(uint64_t memory,unsigned long pages)1946e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
1956e0bf8d8SMasahiro Yamada unsigned long pages)
196bee91169SAlexander Graf {
197bee91169SAlexander Graf efi_status_t r;
198bee91169SAlexander Graf
199bee91169SAlexander Graf EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
200bee91169SAlexander Graf r = efi_free_pages(memory, pages);
201bee91169SAlexander Graf return EFI_EXIT(r);
202bee91169SAlexander Graf }
203bee91169SAlexander Graf
efi_get_memory_map_ext(unsigned long * memory_map_size,struct efi_mem_desc * memory_map,unsigned long * map_key,unsigned long * descriptor_size,uint32_t * descriptor_version)2046e0bf8d8SMasahiro Yamada static efi_status_t EFIAPI efi_get_memory_map_ext(
2056e0bf8d8SMasahiro Yamada unsigned long *memory_map_size,
206bee91169SAlexander Graf struct efi_mem_desc *memory_map,
207bee91169SAlexander Graf unsigned long *map_key,
208bee91169SAlexander Graf unsigned long *descriptor_size,
209bee91169SAlexander Graf uint32_t *descriptor_version)
210bee91169SAlexander Graf {
211bee91169SAlexander Graf efi_status_t r;
212bee91169SAlexander Graf
213bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
214bee91169SAlexander Graf map_key, descriptor_size, descriptor_version);
215bee91169SAlexander Graf r = efi_get_memory_map(memory_map_size, memory_map, map_key,
216bee91169SAlexander Graf descriptor_size, descriptor_version);
217bee91169SAlexander Graf return EFI_EXIT(r);
218bee91169SAlexander Graf }
219bee91169SAlexander Graf
efi_allocate_pool_ext(int pool_type,unsigned long size,void ** buffer)220ead1274bSStefan Brüns static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
221ead1274bSStefan Brüns unsigned long size,
222bee91169SAlexander Graf void **buffer)
223bee91169SAlexander Graf {
2241cd29f0aSAlexander Graf efi_status_t r;
2251cd29f0aSAlexander Graf
2261cd29f0aSAlexander Graf EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
227ead1274bSStefan Brüns r = efi_allocate_pool(pool_type, size, buffer);
2281cd29f0aSAlexander Graf return EFI_EXIT(r);
229bee91169SAlexander Graf }
230bee91169SAlexander Graf
efi_free_pool_ext(void * buffer)23142417bc8SStefan Brüns static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
232bee91169SAlexander Graf {
2331cd29f0aSAlexander Graf efi_status_t r;
2341cd29f0aSAlexander Graf
2351cd29f0aSAlexander Graf EFI_ENTRY("%p", buffer);
23642417bc8SStefan Brüns r = efi_free_pool(buffer);
2371cd29f0aSAlexander Graf return EFI_EXIT(r);
238bee91169SAlexander Graf }
239bee91169SAlexander Graf
240bee91169SAlexander Graf /*
241c6841592Sxypron.glpk@gmx.de * Our event capabilities are very limited. Only a small limited
242c6841592Sxypron.glpk@gmx.de * number of events is allowed to coexist.
243bee91169SAlexander Graf */
244c6841592Sxypron.glpk@gmx.de static struct efi_event efi_events[16];
245bee91169SAlexander Graf
efi_create_event(uint32_t type,UINTN notify_tpl,void (EFIAPI * notify_function)(struct efi_event * event,void * context),void * notify_context,struct efi_event ** event)246b521d29eSxypron.glpk@gmx.de efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
2472fd945feSxypron.glpk@gmx.de void (EFIAPI *notify_function) (
2482fd945feSxypron.glpk@gmx.de struct efi_event *event,
249e275458cSSimon Glass void *context),
2502fd945feSxypron.glpk@gmx.de void *notify_context, struct efi_event **event)
251bee91169SAlexander Graf {
252c6841592Sxypron.glpk@gmx.de int i;
253c6841592Sxypron.glpk@gmx.de
254a95343b8SJonathan Gray if (event == NULL)
25549deb455Sxypron.glpk@gmx.de return EFI_INVALID_PARAMETER;
256a95343b8SJonathan Gray
257a95343b8SJonathan Gray if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
25849deb455Sxypron.glpk@gmx.de return EFI_INVALID_PARAMETER;
259a95343b8SJonathan Gray
260a95343b8SJonathan Gray if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
261a95343b8SJonathan Gray notify_function == NULL)
26249deb455Sxypron.glpk@gmx.de return EFI_INVALID_PARAMETER;
263a95343b8SJonathan Gray
264c6841592Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
265c6841592Sxypron.glpk@gmx.de if (efi_events[i].type)
266c6841592Sxypron.glpk@gmx.de continue;
267c6841592Sxypron.glpk@gmx.de efi_events[i].type = type;
268c6841592Sxypron.glpk@gmx.de efi_events[i].notify_tpl = notify_tpl;
269c6841592Sxypron.glpk@gmx.de efi_events[i].notify_function = notify_function;
270c6841592Sxypron.glpk@gmx.de efi_events[i].notify_context = notify_context;
271c6841592Sxypron.glpk@gmx.de /* Disable timers on bootup */
272c6841592Sxypron.glpk@gmx.de efi_events[i].trigger_next = -1ULL;
273c6841592Sxypron.glpk@gmx.de efi_events[i].signaled = 0;
274c6841592Sxypron.glpk@gmx.de *event = &efi_events[i];
27549deb455Sxypron.glpk@gmx.de return EFI_SUCCESS;
276bee91169SAlexander Graf }
27749deb455Sxypron.glpk@gmx.de return EFI_OUT_OF_RESOURCES;
278c6841592Sxypron.glpk@gmx.de }
279bee91169SAlexander Graf
efi_create_event_ext(uint32_t type,UINTN notify_tpl,void (EFIAPI * notify_function)(struct efi_event * event,void * context),void * notify_context,struct efi_event ** event)28049deb455Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_create_event_ext(
281b521d29eSxypron.glpk@gmx.de uint32_t type, UINTN notify_tpl,
28249deb455Sxypron.glpk@gmx.de void (EFIAPI *notify_function) (
28349deb455Sxypron.glpk@gmx.de struct efi_event *event,
28449deb455Sxypron.glpk@gmx.de void *context),
28549deb455Sxypron.glpk@gmx.de void *notify_context, struct efi_event **event)
28649deb455Sxypron.glpk@gmx.de {
28749deb455Sxypron.glpk@gmx.de EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
28849deb455Sxypron.glpk@gmx.de notify_context);
28949deb455Sxypron.glpk@gmx.de return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
29049deb455Sxypron.glpk@gmx.de notify_context, event));
29149deb455Sxypron.glpk@gmx.de }
29249deb455Sxypron.glpk@gmx.de
29349deb455Sxypron.glpk@gmx.de
294bee91169SAlexander Graf /*
295bee91169SAlexander Graf * Our timers have to work without interrupts, so we check whenever keyboard
296bee91169SAlexander Graf * input or disk accesses happen if enough time elapsed for it to fire.
297bee91169SAlexander Graf */
efi_timer_check(void)298bee91169SAlexander Graf void efi_timer_check(void)
299bee91169SAlexander Graf {
300c6841592Sxypron.glpk@gmx.de int i;
301bee91169SAlexander Graf u64 now = timer_get_us();
302bee91169SAlexander Graf
303c6841592Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
304c6841592Sxypron.glpk@gmx.de if (!efi_events[i].type ||
305c6841592Sxypron.glpk@gmx.de !(efi_events[i].type & EVT_TIMER) ||
306c6841592Sxypron.glpk@gmx.de efi_events[i].trigger_type == EFI_TIMER_STOP ||
307c6841592Sxypron.glpk@gmx.de now < efi_events[i].trigger_next)
308c6841592Sxypron.glpk@gmx.de continue;
309c6841592Sxypron.glpk@gmx.de if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
310c6841592Sxypron.glpk@gmx.de efi_events[i].trigger_next +=
3118787b02eSxypron.glpk@gmx.de efi_events[i].trigger_time;
312c6841592Sxypron.glpk@gmx.de efi_events[i].signaled = 0;
313bee91169SAlexander Graf }
314c6841592Sxypron.glpk@gmx.de efi_signal_event(&efi_events[i]);
315c6841592Sxypron.glpk@gmx.de }
316bee91169SAlexander Graf WATCHDOG_RESET();
317bee91169SAlexander Graf }
318bee91169SAlexander Graf
efi_set_timer(struct efi_event * event,enum efi_timer_delay type,uint64_t trigger_time)319b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
320bee91169SAlexander Graf uint64_t trigger_time)
321bee91169SAlexander Graf {
322c6841592Sxypron.glpk@gmx.de int i;
323bee91169SAlexander Graf
3248787b02eSxypron.glpk@gmx.de /*
3258787b02eSxypron.glpk@gmx.de * The parameter defines a multiple of 100ns.
3268787b02eSxypron.glpk@gmx.de * We use multiples of 1000ns. So divide by 10.
3278787b02eSxypron.glpk@gmx.de */
3288787b02eSxypron.glpk@gmx.de trigger_time = efi_div10(trigger_time);
329bee91169SAlexander Graf
330c6841592Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
331c6841592Sxypron.glpk@gmx.de if (event != &efi_events[i])
332c6841592Sxypron.glpk@gmx.de continue;
333bee91169SAlexander Graf
334c6841592Sxypron.glpk@gmx.de if (!(event->type & EVT_TIMER))
335c6841592Sxypron.glpk@gmx.de break;
336bee91169SAlexander Graf switch (type) {
337bee91169SAlexander Graf case EFI_TIMER_STOP:
338c6841592Sxypron.glpk@gmx.de event->trigger_next = -1ULL;
339bee91169SAlexander Graf break;
340bee91169SAlexander Graf case EFI_TIMER_PERIODIC:
341bee91169SAlexander Graf case EFI_TIMER_RELATIVE:
342c6841592Sxypron.glpk@gmx.de event->trigger_next =
3438787b02eSxypron.glpk@gmx.de timer_get_us() + trigger_time;
344bee91169SAlexander Graf break;
345bee91169SAlexander Graf default:
346bfc72462Sxypron.glpk@gmx.de return EFI_INVALID_PARAMETER;
347bee91169SAlexander Graf }
348c6841592Sxypron.glpk@gmx.de event->trigger_type = type;
349c6841592Sxypron.glpk@gmx.de event->trigger_time = trigger_time;
350bfc72462Sxypron.glpk@gmx.de return EFI_SUCCESS;
351bee91169SAlexander Graf }
352bfc72462Sxypron.glpk@gmx.de return EFI_INVALID_PARAMETER;
353bfc72462Sxypron.glpk@gmx.de }
354bfc72462Sxypron.glpk@gmx.de
efi_set_timer_ext(struct efi_event * event,enum efi_timer_delay type,uint64_t trigger_time)355b521d29eSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
356b521d29eSxypron.glpk@gmx.de enum efi_timer_delay type,
357bfc72462Sxypron.glpk@gmx.de uint64_t trigger_time)
358bfc72462Sxypron.glpk@gmx.de {
359bfc72462Sxypron.glpk@gmx.de EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
360bfc72462Sxypron.glpk@gmx.de return EFI_EXIT(efi_set_timer(event, type, trigger_time));
361c6841592Sxypron.glpk@gmx.de }
362bee91169SAlexander Graf
efi_wait_for_event(unsigned long num_events,struct efi_event ** event,unsigned long * index)363bee91169SAlexander Graf static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
3642fd945feSxypron.glpk@gmx.de struct efi_event **event,
3652fd945feSxypron.glpk@gmx.de unsigned long *index)
366bee91169SAlexander Graf {
367c6841592Sxypron.glpk@gmx.de int i, j;
368bee91169SAlexander Graf
369bee91169SAlexander Graf EFI_ENTRY("%ld, %p, %p", num_events, event, index);
370bee91169SAlexander Graf
371c6841592Sxypron.glpk@gmx.de /* Check parameters */
372c6841592Sxypron.glpk@gmx.de if (!num_events || !event)
373c6841592Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
374c6841592Sxypron.glpk@gmx.de for (i = 0; i < num_events; ++i) {
375c6841592Sxypron.glpk@gmx.de for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
376c6841592Sxypron.glpk@gmx.de if (event[i] == &efi_events[j])
377c6841592Sxypron.glpk@gmx.de goto known_event;
378c6841592Sxypron.glpk@gmx.de }
379c6841592Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
380c6841592Sxypron.glpk@gmx.de known_event:
381c6841592Sxypron.glpk@gmx.de if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
382c6841592Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
383c6841592Sxypron.glpk@gmx.de }
384c6841592Sxypron.glpk@gmx.de
385c6841592Sxypron.glpk@gmx.de /* Wait for signal */
386c6841592Sxypron.glpk@gmx.de for (;;) {
387c6841592Sxypron.glpk@gmx.de for (i = 0; i < num_events; ++i) {
388c6841592Sxypron.glpk@gmx.de if (event[i]->signaled)
389c6841592Sxypron.glpk@gmx.de goto out;
390c6841592Sxypron.glpk@gmx.de }
391c6841592Sxypron.glpk@gmx.de /* Allow events to occur. */
392bee91169SAlexander Graf efi_timer_check();
393c6841592Sxypron.glpk@gmx.de }
394c6841592Sxypron.glpk@gmx.de
395c6841592Sxypron.glpk@gmx.de out:
396c6841592Sxypron.glpk@gmx.de /*
397c6841592Sxypron.glpk@gmx.de * Reset the signal which is passed to the caller to allow periodic
398c6841592Sxypron.glpk@gmx.de * events to occur.
399c6841592Sxypron.glpk@gmx.de */
400c6841592Sxypron.glpk@gmx.de event[i]->signaled = 0;
401c6841592Sxypron.glpk@gmx.de if (index)
402c6841592Sxypron.glpk@gmx.de *index = i;
403bee91169SAlexander Graf
404bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
405bee91169SAlexander Graf }
406bee91169SAlexander Graf
efi_signal_event_ext(struct efi_event * event)407c6841592Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
408bee91169SAlexander Graf {
409c6841592Sxypron.glpk@gmx.de int i;
410c6841592Sxypron.glpk@gmx.de
411bee91169SAlexander Graf EFI_ENTRY("%p", event);
412c6841592Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
413c6841592Sxypron.glpk@gmx.de if (event != &efi_events[i])
414c6841592Sxypron.glpk@gmx.de continue;
415c6841592Sxypron.glpk@gmx.de efi_signal_event(event);
416c6841592Sxypron.glpk@gmx.de break;
417c6841592Sxypron.glpk@gmx.de }
418bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
419bee91169SAlexander Graf }
420bee91169SAlexander Graf
efi_close_event(struct efi_event * event)4212fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
422bee91169SAlexander Graf {
423c6841592Sxypron.glpk@gmx.de int i;
424c6841592Sxypron.glpk@gmx.de
425bee91169SAlexander Graf EFI_ENTRY("%p", event);
426c6841592Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
427c6841592Sxypron.glpk@gmx.de if (event == &efi_events[i]) {
428c6841592Sxypron.glpk@gmx.de event->type = 0;
429c6841592Sxypron.glpk@gmx.de event->trigger_next = -1ULL;
430c6841592Sxypron.glpk@gmx.de event->signaled = 0;
431bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
432bee91169SAlexander Graf }
433c6841592Sxypron.glpk@gmx.de }
434c6841592Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
435c6841592Sxypron.glpk@gmx.de }
436bee91169SAlexander Graf
efi_check_event(struct efi_event * event)4372fd945feSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
438bee91169SAlexander Graf {
439c6841592Sxypron.glpk@gmx.de int i;
440c6841592Sxypron.glpk@gmx.de
441bee91169SAlexander Graf EFI_ENTRY("%p", event);
442c6841592Sxypron.glpk@gmx.de efi_timer_check();
443c6841592Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
444c6841592Sxypron.glpk@gmx.de if (event != &efi_events[i])
445c6841592Sxypron.glpk@gmx.de continue;
446c6841592Sxypron.glpk@gmx.de if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
447c6841592Sxypron.glpk@gmx.de break;
448c6841592Sxypron.glpk@gmx.de if (event->signaled)
449c6841592Sxypron.glpk@gmx.de return EFI_EXIT(EFI_SUCCESS);
450bee91169SAlexander Graf return EFI_EXIT(EFI_NOT_READY);
451bee91169SAlexander Graf }
452c6841592Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
453c6841592Sxypron.glpk@gmx.de }
454bee91169SAlexander Graf
efi_install_protocol_interface(void ** handle,efi_guid_t * protocol,int protocol_interface_type,void * protocol_interface)455bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
456bee91169SAlexander Graf efi_guid_t *protocol, int protocol_interface_type,
457bee91169SAlexander Graf void *protocol_interface)
458bee91169SAlexander Graf {
459e0549f8aSxypron.glpk@gmx.de struct list_head *lhandle;
460e0549f8aSxypron.glpk@gmx.de int i;
461e0549f8aSxypron.glpk@gmx.de efi_status_t r;
462e0549f8aSxypron.glpk@gmx.de
463e0549f8aSxypron.glpk@gmx.de if (!handle || !protocol ||
464e0549f8aSxypron.glpk@gmx.de protocol_interface_type != EFI_NATIVE_INTERFACE) {
465e0549f8aSxypron.glpk@gmx.de r = EFI_INVALID_PARAMETER;
466e0549f8aSxypron.glpk@gmx.de goto out;
467bee91169SAlexander Graf }
468e0549f8aSxypron.glpk@gmx.de
469e0549f8aSxypron.glpk@gmx.de /* Create new handle if requested. */
470e0549f8aSxypron.glpk@gmx.de if (!*handle) {
471e0549f8aSxypron.glpk@gmx.de r = EFI_OUT_OF_RESOURCES;
472e0549f8aSxypron.glpk@gmx.de goto out;
473e0549f8aSxypron.glpk@gmx.de }
474e0549f8aSxypron.glpk@gmx.de /* Find object. */
475e0549f8aSxypron.glpk@gmx.de list_for_each(lhandle, &efi_obj_list) {
476e0549f8aSxypron.glpk@gmx.de struct efi_object *efiobj;
477e0549f8aSxypron.glpk@gmx.de efiobj = list_entry(lhandle, struct efi_object, link);
478e0549f8aSxypron.glpk@gmx.de
479e0549f8aSxypron.glpk@gmx.de if (efiobj->handle != *handle)
480e0549f8aSxypron.glpk@gmx.de continue;
481e0549f8aSxypron.glpk@gmx.de /* Check if protocol is already installed on the handle. */
482e0549f8aSxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
483e0549f8aSxypron.glpk@gmx.de struct efi_handler *handler = &efiobj->protocols[i];
484e0549f8aSxypron.glpk@gmx.de
485e0549f8aSxypron.glpk@gmx.de if (!handler->guid)
486e0549f8aSxypron.glpk@gmx.de continue;
487e0549f8aSxypron.glpk@gmx.de if (!guidcmp(handler->guid, protocol)) {
488e0549f8aSxypron.glpk@gmx.de r = EFI_INVALID_PARAMETER;
489e0549f8aSxypron.glpk@gmx.de goto out;
490e0549f8aSxypron.glpk@gmx.de }
491e0549f8aSxypron.glpk@gmx.de }
492e0549f8aSxypron.glpk@gmx.de /* Install protocol in first empty slot. */
493e0549f8aSxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
494e0549f8aSxypron.glpk@gmx.de struct efi_handler *handler = &efiobj->protocols[i];
495e0549f8aSxypron.glpk@gmx.de
496e0549f8aSxypron.glpk@gmx.de if (handler->guid)
497e0549f8aSxypron.glpk@gmx.de continue;
498e0549f8aSxypron.glpk@gmx.de
499e0549f8aSxypron.glpk@gmx.de handler->guid = protocol;
500e0549f8aSxypron.glpk@gmx.de handler->protocol_interface = protocol_interface;
501e0549f8aSxypron.glpk@gmx.de r = EFI_SUCCESS;
502e0549f8aSxypron.glpk@gmx.de goto out;
503e0549f8aSxypron.glpk@gmx.de }
504e0549f8aSxypron.glpk@gmx.de r = EFI_OUT_OF_RESOURCES;
505e0549f8aSxypron.glpk@gmx.de goto out;
506e0549f8aSxypron.glpk@gmx.de }
507e0549f8aSxypron.glpk@gmx.de r = EFI_INVALID_PARAMETER;
508e0549f8aSxypron.glpk@gmx.de out:
5098bee5a3cSxypron.glpk@gmx.de return r;
5108bee5a3cSxypron.glpk@gmx.de }
5118bee5a3cSxypron.glpk@gmx.de
efi_install_protocol_interface_ext(void ** handle,efi_guid_t * protocol,int protocol_interface_type,void * protocol_interface)5128bee5a3cSxypron.glpk@gmx.de static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
5138bee5a3cSxypron.glpk@gmx.de efi_guid_t *protocol, int protocol_interface_type,
5148bee5a3cSxypron.glpk@gmx.de void *protocol_interface)
5158bee5a3cSxypron.glpk@gmx.de {
5168bee5a3cSxypron.glpk@gmx.de EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
5178bee5a3cSxypron.glpk@gmx.de protocol_interface);
5188bee5a3cSxypron.glpk@gmx.de
5198bee5a3cSxypron.glpk@gmx.de return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
5208bee5a3cSxypron.glpk@gmx.de protocol_interface_type,
5218bee5a3cSxypron.glpk@gmx.de protocol_interface));
522e0549f8aSxypron.glpk@gmx.de }
523e0549f8aSxypron.glpk@gmx.de
efi_reinstall_protocol_interface(void * handle,efi_guid_t * protocol,void * old_interface,void * new_interface)524bee91169SAlexander Graf static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
525bee91169SAlexander Graf efi_guid_t *protocol, void *old_interface,
526bee91169SAlexander Graf void *new_interface)
527bee91169SAlexander Graf {
528bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
529bee91169SAlexander Graf new_interface);
530bee91169SAlexander Graf return EFI_EXIT(EFI_ACCESS_DENIED);
531bee91169SAlexander Graf }
532bee91169SAlexander Graf
efi_uninstall_protocol_interface(void * handle,efi_guid_t * protocol,void * protocol_interface)533bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
534bee91169SAlexander Graf efi_guid_t *protocol, void *protocol_interface)
535bee91169SAlexander Graf {
5364b6ed0d7Sxypron.glpk@gmx.de struct list_head *lhandle;
5374b6ed0d7Sxypron.glpk@gmx.de int i;
5384b6ed0d7Sxypron.glpk@gmx.de efi_status_t r = EFI_NOT_FOUND;
5394b6ed0d7Sxypron.glpk@gmx.de
5404b6ed0d7Sxypron.glpk@gmx.de if (!handle || !protocol) {
5414b6ed0d7Sxypron.glpk@gmx.de r = EFI_INVALID_PARAMETER;
5424b6ed0d7Sxypron.glpk@gmx.de goto out;
5434b6ed0d7Sxypron.glpk@gmx.de }
5444b6ed0d7Sxypron.glpk@gmx.de
5454b6ed0d7Sxypron.glpk@gmx.de list_for_each(lhandle, &efi_obj_list) {
5464b6ed0d7Sxypron.glpk@gmx.de struct efi_object *efiobj;
5474b6ed0d7Sxypron.glpk@gmx.de efiobj = list_entry(lhandle, struct efi_object, link);
5484b6ed0d7Sxypron.glpk@gmx.de
5494b6ed0d7Sxypron.glpk@gmx.de if (efiobj->handle != handle)
5504b6ed0d7Sxypron.glpk@gmx.de continue;
5514b6ed0d7Sxypron.glpk@gmx.de
5524b6ed0d7Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
5534b6ed0d7Sxypron.glpk@gmx.de struct efi_handler *handler = &efiobj->protocols[i];
5544b6ed0d7Sxypron.glpk@gmx.de const efi_guid_t *hprotocol = handler->guid;
5554b6ed0d7Sxypron.glpk@gmx.de
5564b6ed0d7Sxypron.glpk@gmx.de if (!hprotocol)
5574b6ed0d7Sxypron.glpk@gmx.de continue;
5584b6ed0d7Sxypron.glpk@gmx.de if (!guidcmp(hprotocol, protocol)) {
5594b6ed0d7Sxypron.glpk@gmx.de if (handler->protocol_interface) {
5604b6ed0d7Sxypron.glpk@gmx.de r = EFI_ACCESS_DENIED;
5614b6ed0d7Sxypron.glpk@gmx.de } else {
5624b6ed0d7Sxypron.glpk@gmx.de handler->guid = 0;
5634b6ed0d7Sxypron.glpk@gmx.de r = EFI_SUCCESS;
5644b6ed0d7Sxypron.glpk@gmx.de }
5654b6ed0d7Sxypron.glpk@gmx.de goto out;
5664b6ed0d7Sxypron.glpk@gmx.de }
5674b6ed0d7Sxypron.glpk@gmx.de }
5684b6ed0d7Sxypron.glpk@gmx.de }
5694b6ed0d7Sxypron.glpk@gmx.de
5704b6ed0d7Sxypron.glpk@gmx.de out:
5713d8e1456Sxypron.glpk@gmx.de return r;
5723d8e1456Sxypron.glpk@gmx.de }
5733d8e1456Sxypron.glpk@gmx.de
efi_uninstall_protocol_interface_ext(void * handle,efi_guid_t * protocol,void * protocol_interface)5743d8e1456Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
5753d8e1456Sxypron.glpk@gmx.de efi_guid_t *protocol, void *protocol_interface)
5763d8e1456Sxypron.glpk@gmx.de {
5773d8e1456Sxypron.glpk@gmx.de EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
5783d8e1456Sxypron.glpk@gmx.de
5793d8e1456Sxypron.glpk@gmx.de return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
5803d8e1456Sxypron.glpk@gmx.de protocol_interface));
581bee91169SAlexander Graf }
582bee91169SAlexander Graf
efi_register_protocol_notify(efi_guid_t * protocol,struct efi_event * event,void ** registration)583bee91169SAlexander Graf static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
5842fd945feSxypron.glpk@gmx.de struct efi_event *event,
585bee91169SAlexander Graf void **registration)
586bee91169SAlexander Graf {
587bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p", protocol, event, registration);
588bee91169SAlexander Graf return EFI_EXIT(EFI_OUT_OF_RESOURCES);
589bee91169SAlexander Graf }
590bee91169SAlexander Graf
efi_search(enum efi_locate_search_type search_type,efi_guid_t * protocol,void * search_key,struct efi_object * efiobj)591bee91169SAlexander Graf static int efi_search(enum efi_locate_search_type search_type,
592bee91169SAlexander Graf efi_guid_t *protocol, void *search_key,
593bee91169SAlexander Graf struct efi_object *efiobj)
594bee91169SAlexander Graf {
595bee91169SAlexander Graf int i;
596bee91169SAlexander Graf
597bee91169SAlexander Graf switch (search_type) {
598bee91169SAlexander Graf case all_handles:
599bee91169SAlexander Graf return 0;
600bee91169SAlexander Graf case by_register_notify:
601bee91169SAlexander Graf return -1;
602bee91169SAlexander Graf case by_protocol:
603bee91169SAlexander Graf for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
604bee91169SAlexander Graf const efi_guid_t *guid = efiobj->protocols[i].guid;
605bee91169SAlexander Graf if (guid && !guidcmp(guid, protocol))
606bee91169SAlexander Graf return 0;
607bee91169SAlexander Graf }
608bee91169SAlexander Graf return -1;
609bee91169SAlexander Graf }
610bee91169SAlexander Graf
611bee91169SAlexander Graf return -1;
612bee91169SAlexander Graf }
613bee91169SAlexander Graf
efi_locate_handle(enum efi_locate_search_type search_type,efi_guid_t * protocol,void * search_key,unsigned long * buffer_size,efi_handle_t * buffer)614ebf199b9Sxypron.glpk@gmx.de static efi_status_t efi_locate_handle(
615bee91169SAlexander Graf enum efi_locate_search_type search_type,
616bee91169SAlexander Graf efi_guid_t *protocol, void *search_key,
617bee91169SAlexander Graf unsigned long *buffer_size, efi_handle_t *buffer)
618bee91169SAlexander Graf {
619bee91169SAlexander Graf struct list_head *lhandle;
620bee91169SAlexander Graf unsigned long size = 0;
621bee91169SAlexander Graf
622bee91169SAlexander Graf /* Count how much space we need */
623bee91169SAlexander Graf list_for_each(lhandle, &efi_obj_list) {
624bee91169SAlexander Graf struct efi_object *efiobj;
625bee91169SAlexander Graf efiobj = list_entry(lhandle, struct efi_object, link);
626bee91169SAlexander Graf if (!efi_search(search_type, protocol, search_key, efiobj)) {
627bee91169SAlexander Graf size += sizeof(void*);
628bee91169SAlexander Graf }
629bee91169SAlexander Graf }
630bee91169SAlexander Graf
631bee91169SAlexander Graf if (*buffer_size < size) {
632bee91169SAlexander Graf *buffer_size = size;
63326329584Sxypron.glpk@gmx.de return EFI_BUFFER_TOO_SMALL;
634bee91169SAlexander Graf }
635bee91169SAlexander Graf
636796a78cbSRob Clark *buffer_size = size;
637796a78cbSRob Clark if (size == 0)
638796a78cbSRob Clark return EFI_NOT_FOUND;
639796a78cbSRob Clark
640bee91169SAlexander Graf /* Then fill the array */
641bee91169SAlexander Graf list_for_each(lhandle, &efi_obj_list) {
642bee91169SAlexander Graf struct efi_object *efiobj;
643bee91169SAlexander Graf efiobj = list_entry(lhandle, struct efi_object, link);
644bee91169SAlexander Graf if (!efi_search(search_type, protocol, search_key, efiobj)) {
645bee91169SAlexander Graf *(buffer++) = efiobj->handle;
646bee91169SAlexander Graf }
647bee91169SAlexander Graf }
648bee91169SAlexander Graf
64926329584Sxypron.glpk@gmx.de return EFI_SUCCESS;
65026329584Sxypron.glpk@gmx.de }
65126329584Sxypron.glpk@gmx.de
efi_locate_handle_ext(enum efi_locate_search_type search_type,efi_guid_t * protocol,void * search_key,unsigned long * buffer_size,efi_handle_t * buffer)65226329584Sxypron.glpk@gmx.de static efi_status_t EFIAPI efi_locate_handle_ext(
65326329584Sxypron.glpk@gmx.de enum efi_locate_search_type search_type,
65426329584Sxypron.glpk@gmx.de efi_guid_t *protocol, void *search_key,
65526329584Sxypron.glpk@gmx.de unsigned long *buffer_size, efi_handle_t *buffer)
65626329584Sxypron.glpk@gmx.de {
65726329584Sxypron.glpk@gmx.de EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
65826329584Sxypron.glpk@gmx.de buffer_size, buffer);
65926329584Sxypron.glpk@gmx.de
66026329584Sxypron.glpk@gmx.de return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
66126329584Sxypron.glpk@gmx.de buffer_size, buffer));
662bee91169SAlexander Graf }
663bee91169SAlexander Graf
efi_locate_device_path(efi_guid_t * protocol,struct efi_device_path ** device_path,efi_handle_t * device)664bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
665bee91169SAlexander Graf struct efi_device_path **device_path,
666bee91169SAlexander Graf efi_handle_t *device)
667bee91169SAlexander Graf {
6681fa8dee8SRob Clark struct efi_object *efiobj;
6691fa8dee8SRob Clark
6701fa8dee8SRob Clark EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
6711fa8dee8SRob Clark
6721fa8dee8SRob Clark efiobj = efi_dp_find_obj(*device_path, device_path);
6731fa8dee8SRob Clark if (!efiobj)
674bee91169SAlexander Graf return EFI_EXIT(EFI_NOT_FOUND);
6751fa8dee8SRob Clark
6761fa8dee8SRob Clark *device = efiobj->handle;
6771fa8dee8SRob Clark
6781fa8dee8SRob Clark return EFI_EXIT(EFI_SUCCESS);
679bee91169SAlexander Graf }
680bee91169SAlexander Graf
681d98cdf6aSAlexander Graf /* Collapses configuration table entries, removing index i */
efi_remove_configuration_table(int i)682d98cdf6aSAlexander Graf static void efi_remove_configuration_table(int i)
683d98cdf6aSAlexander Graf {
684d98cdf6aSAlexander Graf struct efi_configuration_table *this = &efi_conf_table[i];
685d98cdf6aSAlexander Graf struct efi_configuration_table *next = &efi_conf_table[i+1];
686d98cdf6aSAlexander Graf struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
687d98cdf6aSAlexander Graf
688d98cdf6aSAlexander Graf memmove(this, next, (ulong)end - (ulong)next);
689d98cdf6aSAlexander Graf systab.nr_tables--;
690d98cdf6aSAlexander Graf }
691d98cdf6aSAlexander Graf
efi_install_configuration_table(const efi_guid_t * guid,void * table)692488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
693bee91169SAlexander Graf {
694bee91169SAlexander Graf int i;
695bee91169SAlexander Graf
696bee91169SAlexander Graf /* Check for guid override */
697bee91169SAlexander Graf for (i = 0; i < systab.nr_tables; i++) {
698bee91169SAlexander Graf if (!guidcmp(guid, &efi_conf_table[i].guid)) {
699d98cdf6aSAlexander Graf if (table)
700bee91169SAlexander Graf efi_conf_table[i].table = table;
701d98cdf6aSAlexander Graf else
702d98cdf6aSAlexander Graf efi_remove_configuration_table(i);
703488bf12dSAlexander Graf return EFI_SUCCESS;
704bee91169SAlexander Graf }
705bee91169SAlexander Graf }
706bee91169SAlexander Graf
707d98cdf6aSAlexander Graf if (!table)
708d98cdf6aSAlexander Graf return EFI_NOT_FOUND;
709d98cdf6aSAlexander Graf
710bee91169SAlexander Graf /* No override, check for overflow */
711bee91169SAlexander Graf if (i >= ARRAY_SIZE(efi_conf_table))
712488bf12dSAlexander Graf return EFI_OUT_OF_RESOURCES;
713bee91169SAlexander Graf
714bee91169SAlexander Graf /* Add a new entry */
715bee91169SAlexander Graf memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
716bee91169SAlexander Graf efi_conf_table[i].table = table;
717aba5e919SAlexander Graf systab.nr_tables = i + 1;
718bee91169SAlexander Graf
719488bf12dSAlexander Graf return EFI_SUCCESS;
720488bf12dSAlexander Graf }
721488bf12dSAlexander Graf
efi_install_configuration_table_ext(efi_guid_t * guid,void * table)722488bf12dSAlexander Graf static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
723488bf12dSAlexander Graf void *table)
724488bf12dSAlexander Graf {
725488bf12dSAlexander Graf EFI_ENTRY("%p, %p", guid, table);
726488bf12dSAlexander Graf return EFI_EXIT(efi_install_configuration_table(guid, table));
727bee91169SAlexander Graf }
728bee91169SAlexander Graf
7292f0750ebSRob Clark /* Initialize a loaded_image_info + loaded_image_info object with correct
7302f0750ebSRob Clark * protocols, boot-device, etc.
7312f0750ebSRob Clark */
efi_setup_loaded_image(struct efi_loaded_image * info,struct efi_object * obj,struct efi_device_path * device_path,struct efi_device_path * file_path)7322f0750ebSRob Clark void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
7332f0750ebSRob Clark struct efi_device_path *device_path,
7342f0750ebSRob Clark struct efi_device_path *file_path)
7352f0750ebSRob Clark {
7362f0750ebSRob Clark obj->handle = info;
7372f0750ebSRob Clark
7382f0750ebSRob Clark /*
7392f0750ebSRob Clark * When asking for the device path interface, return
7402f0750ebSRob Clark * bootefi_device_path
7412f0750ebSRob Clark */
7422f0750ebSRob Clark obj->protocols[0].guid = &efi_guid_device_path;
7432f0750ebSRob Clark obj->protocols[0].protocol_interface = device_path;
7442f0750ebSRob Clark
7452f0750ebSRob Clark /*
7462f0750ebSRob Clark * When asking for the loaded_image interface, just
7472f0750ebSRob Clark * return handle which points to loaded_image_info
7482f0750ebSRob Clark */
7492f0750ebSRob Clark obj->protocols[1].guid = &efi_guid_loaded_image;
7502f0750ebSRob Clark obj->protocols[1].protocol_interface = info;
7512f0750ebSRob Clark
7522f0750ebSRob Clark obj->protocols[2].guid = &efi_guid_console_control;
7532f0750ebSRob Clark obj->protocols[2].protocol_interface = (void *)&efi_console_control;
7542f0750ebSRob Clark
7552f0750ebSRob Clark obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
7562f0750ebSRob Clark obj->protocols[3].protocol_interface =
7572f0750ebSRob Clark (void *)&efi_device_path_to_text;
7582f0750ebSRob Clark
7592f0750ebSRob Clark info->file_path = file_path;
7602f0750ebSRob Clark info->device_handle = efi_dp_find_obj(device_path, NULL);
7612f0750ebSRob Clark
7622f0750ebSRob Clark list_add_tail(&obj->link, &efi_obj_list);
7632f0750ebSRob Clark }
7642f0750ebSRob Clark
efi_load_image(bool boot_policy,efi_handle_t parent_image,struct efi_device_path * file_path,void * source_buffer,unsigned long source_size,efi_handle_t * image_handle)765bee91169SAlexander Graf static efi_status_t EFIAPI efi_load_image(bool boot_policy,
766bee91169SAlexander Graf efi_handle_t parent_image,
767bee91169SAlexander Graf struct efi_device_path *file_path,
768bee91169SAlexander Graf void *source_buffer,
769bee91169SAlexander Graf unsigned long source_size,
770bee91169SAlexander Graf efi_handle_t *image_handle)
771bee91169SAlexander Graf {
772bee91169SAlexander Graf static struct efi_object loaded_image_info_obj = {
773bee91169SAlexander Graf .protocols = {
774bee91169SAlexander Graf {
775bee91169SAlexander Graf .guid = &efi_guid_loaded_image,
776bee91169SAlexander Graf },
777bee91169SAlexander Graf },
778bee91169SAlexander Graf };
779bee91169SAlexander Graf struct efi_loaded_image *info;
780bee91169SAlexander Graf struct efi_object *obj;
781bee91169SAlexander Graf
782bee91169SAlexander Graf EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
783bee91169SAlexander Graf file_path, source_buffer, source_size, image_handle);
784bee91169SAlexander Graf info = malloc(sizeof(*info));
785b5349f74Sxypron.glpk@gmx.de loaded_image_info_obj.protocols[0].protocol_interface = info;
786bee91169SAlexander Graf obj = malloc(sizeof(loaded_image_info_obj));
787bee91169SAlexander Graf memset(info, 0, sizeof(*info));
788bee91169SAlexander Graf memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
789bee91169SAlexander Graf obj->handle = info;
790bee91169SAlexander Graf info->file_path = file_path;
791bee91169SAlexander Graf info->reserved = efi_load_pe(source_buffer, info);
792bee91169SAlexander Graf if (!info->reserved) {
793bee91169SAlexander Graf free(info);
794bee91169SAlexander Graf free(obj);
795bee91169SAlexander Graf return EFI_EXIT(EFI_UNSUPPORTED);
796bee91169SAlexander Graf }
797bee91169SAlexander Graf
798bee91169SAlexander Graf *image_handle = info;
799bee91169SAlexander Graf list_add_tail(&obj->link, &efi_obj_list);
800bee91169SAlexander Graf
801bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
802bee91169SAlexander Graf }
803bee91169SAlexander Graf
efi_start_image(efi_handle_t image_handle,unsigned long * exit_data_size,s16 ** exit_data)804bee91169SAlexander Graf static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
805bee91169SAlexander Graf unsigned long *exit_data_size,
806bee91169SAlexander Graf s16 **exit_data)
807bee91169SAlexander Graf {
808bee91169SAlexander Graf ulong (*entry)(void *image_handle, struct efi_system_table *st);
809bee91169SAlexander Graf struct efi_loaded_image *info = image_handle;
810bee91169SAlexander Graf
811bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
812bee91169SAlexander Graf entry = info->reserved;
813bee91169SAlexander Graf
814bee91169SAlexander Graf efi_is_direct_boot = false;
815bee91169SAlexander Graf
816bee91169SAlexander Graf /* call the image! */
817a86aeaf2SAlexander Graf if (setjmp(&info->exit_jmp)) {
818a86aeaf2SAlexander Graf /* We returned from the child image */
819a86aeaf2SAlexander Graf return EFI_EXIT(info->exit_status);
820a86aeaf2SAlexander Graf }
821a86aeaf2SAlexander Graf
822af65db85SRob Clark __efi_nesting_dec();
823c160d2f5SRob Clark __efi_exit_check();
824bee91169SAlexander Graf entry(image_handle, &systab);
825c160d2f5SRob Clark __efi_entry_check();
826af65db85SRob Clark __efi_nesting_inc();
827bee91169SAlexander Graf
828bee91169SAlexander Graf /* Should usually never get here */
829bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
830bee91169SAlexander Graf }
831bee91169SAlexander Graf
efi_exit(efi_handle_t image_handle,efi_status_t exit_status,unsigned long exit_data_size,int16_t * exit_data)832a86aeaf2SAlexander Graf static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
833a86aeaf2SAlexander Graf efi_status_t exit_status, unsigned long exit_data_size,
834a86aeaf2SAlexander Graf int16_t *exit_data)
835bee91169SAlexander Graf {
836a86aeaf2SAlexander Graf struct efi_loaded_image *loaded_image_info = (void*)image_handle;
837a86aeaf2SAlexander Graf
838bee91169SAlexander Graf EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
839bee91169SAlexander Graf exit_data_size, exit_data);
840a86aeaf2SAlexander Graf
841a86aeaf2SAlexander Graf loaded_image_info->exit_status = exit_status;
842692fcdd8SAlexander Graf longjmp(&loaded_image_info->exit_jmp, 1);
843a86aeaf2SAlexander Graf
844a86aeaf2SAlexander Graf panic("EFI application exited");
845bee91169SAlexander Graf }
846bee91169SAlexander Graf
efi_search_obj(void * handle)847bee91169SAlexander Graf static struct efi_object *efi_search_obj(void *handle)
848bee91169SAlexander Graf {
849bee91169SAlexander Graf struct list_head *lhandle;
850bee91169SAlexander Graf
851bee91169SAlexander Graf list_for_each(lhandle, &efi_obj_list) {
852bee91169SAlexander Graf struct efi_object *efiobj;
853bee91169SAlexander Graf efiobj = list_entry(lhandle, struct efi_object, link);
854bee91169SAlexander Graf if (efiobj->handle == handle)
855bee91169SAlexander Graf return efiobj;
856bee91169SAlexander Graf }
857bee91169SAlexander Graf
858bee91169SAlexander Graf return NULL;
859bee91169SAlexander Graf }
860bee91169SAlexander Graf
efi_unload_image(void * image_handle)861bee91169SAlexander Graf static efi_status_t EFIAPI efi_unload_image(void *image_handle)
862bee91169SAlexander Graf {
863bee91169SAlexander Graf struct efi_object *efiobj;
864bee91169SAlexander Graf
865bee91169SAlexander Graf EFI_ENTRY("%p", image_handle);
866bee91169SAlexander Graf efiobj = efi_search_obj(image_handle);
867bee91169SAlexander Graf if (efiobj)
868bee91169SAlexander Graf list_del(&efiobj->link);
869bee91169SAlexander Graf
870bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
871bee91169SAlexander Graf }
872bee91169SAlexander Graf
efi_exit_caches(void)873bee91169SAlexander Graf static void efi_exit_caches(void)
874bee91169SAlexander Graf {
875bee91169SAlexander Graf #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
876bee91169SAlexander Graf /*
877bee91169SAlexander Graf * Grub on 32bit ARM needs to have caches disabled before jumping into
878bee91169SAlexander Graf * a zImage, but does not know of all cache layers. Give it a hand.
879bee91169SAlexander Graf */
880bee91169SAlexander Graf if (efi_is_direct_boot)
881bee91169SAlexander Graf cleanup_before_linux();
882bee91169SAlexander Graf #endif
883bee91169SAlexander Graf }
884bee91169SAlexander Graf
efi_exit_boot_services(void * image_handle,unsigned long map_key)885bee91169SAlexander Graf static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
886bee91169SAlexander Graf unsigned long map_key)
887bee91169SAlexander Graf {
888bee91169SAlexander Graf EFI_ENTRY("%p, %ld", image_handle, map_key);
889bee91169SAlexander Graf
890*e1aab640SJoseph Chen board_quiesce_devices(NULL);
891b7b8410aSAlexander Graf
892bee91169SAlexander Graf /* Fix up caches for EFI payloads if necessary */
893bee91169SAlexander Graf efi_exit_caches();
894bee91169SAlexander Graf
895bee91169SAlexander Graf /* This stops all lingering devices */
896bee91169SAlexander Graf bootm_disable_interrupts();
897bee91169SAlexander Graf
898bee91169SAlexander Graf /* Give the payload some time to boot */
899bee91169SAlexander Graf WATCHDOG_RESET();
900bee91169SAlexander Graf
901bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
902bee91169SAlexander Graf }
903bee91169SAlexander Graf
efi_get_next_monotonic_count(uint64_t * count)904bee91169SAlexander Graf static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
905bee91169SAlexander Graf {
906bee91169SAlexander Graf static uint64_t mono = 0;
907bee91169SAlexander Graf EFI_ENTRY("%p", count);
908bee91169SAlexander Graf *count = mono++;
909bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
910bee91169SAlexander Graf }
911bee91169SAlexander Graf
efi_stall(unsigned long microseconds)912bee91169SAlexander Graf static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
913bee91169SAlexander Graf {
914bee91169SAlexander Graf EFI_ENTRY("%ld", microseconds);
915bee91169SAlexander Graf udelay(microseconds);
916bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
917bee91169SAlexander Graf }
918bee91169SAlexander Graf
efi_set_watchdog_timer(unsigned long timeout,uint64_t watchdog_code,unsigned long data_size,uint16_t * watchdog_data)919bee91169SAlexander Graf static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
920bee91169SAlexander Graf uint64_t watchdog_code,
921bee91169SAlexander Graf unsigned long data_size,
922bee91169SAlexander Graf uint16_t *watchdog_data)
923bee91169SAlexander Graf {
924bee91169SAlexander Graf EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
925bee91169SAlexander Graf data_size, watchdog_data);
926b5104821SRob Clark return efi_unsupported(__func__);
927bee91169SAlexander Graf }
928bee91169SAlexander Graf
efi_connect_controller(efi_handle_t controller_handle,efi_handle_t * driver_image_handle,struct efi_device_path * remain_device_path,bool recursive)929bee91169SAlexander Graf static efi_status_t EFIAPI efi_connect_controller(
930bee91169SAlexander Graf efi_handle_t controller_handle,
931bee91169SAlexander Graf efi_handle_t *driver_image_handle,
932bee91169SAlexander Graf struct efi_device_path *remain_device_path,
933bee91169SAlexander Graf bool recursive)
934bee91169SAlexander Graf {
935bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
936bee91169SAlexander Graf remain_device_path, recursive);
937bee91169SAlexander Graf return EFI_EXIT(EFI_NOT_FOUND);
938bee91169SAlexander Graf }
939bee91169SAlexander Graf
efi_disconnect_controller(void * controller_handle,void * driver_image_handle,void * child_handle)940bee91169SAlexander Graf static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
941bee91169SAlexander Graf void *driver_image_handle,
942bee91169SAlexander Graf void *child_handle)
943bee91169SAlexander Graf {
944bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
945bee91169SAlexander Graf child_handle);
946bee91169SAlexander Graf return EFI_EXIT(EFI_INVALID_PARAMETER);
947bee91169SAlexander Graf }
948bee91169SAlexander Graf
efi_close_protocol(void * handle,efi_guid_t * protocol,void * agent_handle,void * controller_handle)949bee91169SAlexander Graf static efi_status_t EFIAPI efi_close_protocol(void *handle,
950bee91169SAlexander Graf efi_guid_t *protocol,
951bee91169SAlexander Graf void *agent_handle,
952bee91169SAlexander Graf void *controller_handle)
953bee91169SAlexander Graf {
954bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
955bee91169SAlexander Graf controller_handle);
956bee91169SAlexander Graf return EFI_EXIT(EFI_NOT_FOUND);
957bee91169SAlexander Graf }
958bee91169SAlexander Graf
efi_open_protocol_information(efi_handle_t handle,efi_guid_t * protocol,struct efi_open_protocol_info_entry ** entry_buffer,unsigned long * entry_count)959bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
960bee91169SAlexander Graf efi_guid_t *protocol,
961bee91169SAlexander Graf struct efi_open_protocol_info_entry **entry_buffer,
962bee91169SAlexander Graf unsigned long *entry_count)
963bee91169SAlexander Graf {
964bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
965bee91169SAlexander Graf entry_count);
966bee91169SAlexander Graf return EFI_EXIT(EFI_NOT_FOUND);
967bee91169SAlexander Graf }
968bee91169SAlexander Graf
efi_protocols_per_handle(void * handle,efi_guid_t *** protocol_buffer,unsigned long * protocol_buffer_count)969bee91169SAlexander Graf static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
970bee91169SAlexander Graf efi_guid_t ***protocol_buffer,
971bee91169SAlexander Graf unsigned long *protocol_buffer_count)
972bee91169SAlexander Graf {
973c0ebfc86Sxypron.glpk@gmx.de unsigned long buffer_size;
974c0ebfc86Sxypron.glpk@gmx.de struct efi_object *efiobj;
975c0ebfc86Sxypron.glpk@gmx.de unsigned long i, j;
976c0ebfc86Sxypron.glpk@gmx.de struct list_head *lhandle;
977c0ebfc86Sxypron.glpk@gmx.de efi_status_t r;
978c0ebfc86Sxypron.glpk@gmx.de
979bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
980bee91169SAlexander Graf protocol_buffer_count);
981c0ebfc86Sxypron.glpk@gmx.de
982c0ebfc86Sxypron.glpk@gmx.de if (!handle || !protocol_buffer || !protocol_buffer_count)
983c0ebfc86Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
984c0ebfc86Sxypron.glpk@gmx.de
985c0ebfc86Sxypron.glpk@gmx.de *protocol_buffer = NULL;
986661c8327SRob Clark *protocol_buffer_count = 0;
987c0ebfc86Sxypron.glpk@gmx.de list_for_each(lhandle, &efi_obj_list) {
988c0ebfc86Sxypron.glpk@gmx.de efiobj = list_entry(lhandle, struct efi_object, link);
989c0ebfc86Sxypron.glpk@gmx.de
990c0ebfc86Sxypron.glpk@gmx.de if (efiobj->handle != handle)
991c0ebfc86Sxypron.glpk@gmx.de continue;
992c0ebfc86Sxypron.glpk@gmx.de
993c0ebfc86Sxypron.glpk@gmx.de /* Count protocols */
994c0ebfc86Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
995c0ebfc86Sxypron.glpk@gmx.de if (efiobj->protocols[i].guid)
996c0ebfc86Sxypron.glpk@gmx.de ++*protocol_buffer_count;
997c0ebfc86Sxypron.glpk@gmx.de }
998c0ebfc86Sxypron.glpk@gmx.de /* Copy guids */
999c0ebfc86Sxypron.glpk@gmx.de if (*protocol_buffer_count) {
1000c0ebfc86Sxypron.glpk@gmx.de buffer_size = sizeof(efi_guid_t *) *
1001c0ebfc86Sxypron.glpk@gmx.de *protocol_buffer_count;
1002c0ebfc86Sxypron.glpk@gmx.de r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1003c0ebfc86Sxypron.glpk@gmx.de buffer_size,
1004c0ebfc86Sxypron.glpk@gmx.de (void **)protocol_buffer);
1005c0ebfc86Sxypron.glpk@gmx.de if (r != EFI_SUCCESS)
1006c0ebfc86Sxypron.glpk@gmx.de return EFI_EXIT(r);
1007c0ebfc86Sxypron.glpk@gmx.de j = 0;
1008c0ebfc86Sxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1009c0ebfc86Sxypron.glpk@gmx.de if (efiobj->protocols[i].guid) {
1010c0ebfc86Sxypron.glpk@gmx.de (*protocol_buffer)[j] = (void *)
1011c0ebfc86Sxypron.glpk@gmx.de efiobj->protocols[i].guid;
1012c0ebfc86Sxypron.glpk@gmx.de ++j;
1013c0ebfc86Sxypron.glpk@gmx.de }
1014c0ebfc86Sxypron.glpk@gmx.de }
1015c0ebfc86Sxypron.glpk@gmx.de }
1016c0ebfc86Sxypron.glpk@gmx.de break;
1017c0ebfc86Sxypron.glpk@gmx.de }
1018c0ebfc86Sxypron.glpk@gmx.de
1019c0ebfc86Sxypron.glpk@gmx.de return EFI_EXIT(EFI_SUCCESS);
1020bee91169SAlexander Graf }
1021bee91169SAlexander Graf
efi_locate_handle_buffer(enum efi_locate_search_type search_type,efi_guid_t * protocol,void * search_key,unsigned long * no_handles,efi_handle_t ** buffer)1022bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_handle_buffer(
1023bee91169SAlexander Graf enum efi_locate_search_type search_type,
1024bee91169SAlexander Graf efi_guid_t *protocol, void *search_key,
1025bee91169SAlexander Graf unsigned long *no_handles, efi_handle_t **buffer)
1026bee91169SAlexander Graf {
1027c2e703f9Sxypron.glpk@gmx.de efi_status_t r;
1028c2e703f9Sxypron.glpk@gmx.de unsigned long buffer_size = 0;
1029c2e703f9Sxypron.glpk@gmx.de
1030bee91169SAlexander Graf EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1031bee91169SAlexander Graf no_handles, buffer);
1032c2e703f9Sxypron.glpk@gmx.de
1033c2e703f9Sxypron.glpk@gmx.de if (!no_handles || !buffer) {
1034c2e703f9Sxypron.glpk@gmx.de r = EFI_INVALID_PARAMETER;
1035c2e703f9Sxypron.glpk@gmx.de goto out;
1036c2e703f9Sxypron.glpk@gmx.de }
1037c2e703f9Sxypron.glpk@gmx.de *no_handles = 0;
1038c2e703f9Sxypron.glpk@gmx.de *buffer = NULL;
1039c2e703f9Sxypron.glpk@gmx.de r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1040c2e703f9Sxypron.glpk@gmx.de *buffer);
1041c2e703f9Sxypron.glpk@gmx.de if (r != EFI_BUFFER_TOO_SMALL)
1042c2e703f9Sxypron.glpk@gmx.de goto out;
1043c2e703f9Sxypron.glpk@gmx.de r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1044c2e703f9Sxypron.glpk@gmx.de (void **)buffer);
1045c2e703f9Sxypron.glpk@gmx.de if (r != EFI_SUCCESS)
1046c2e703f9Sxypron.glpk@gmx.de goto out;
1047c2e703f9Sxypron.glpk@gmx.de r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1048c2e703f9Sxypron.glpk@gmx.de *buffer);
1049c2e703f9Sxypron.glpk@gmx.de if (r == EFI_SUCCESS)
1050c2e703f9Sxypron.glpk@gmx.de *no_handles = buffer_size / sizeof(void *);
1051c2e703f9Sxypron.glpk@gmx.de out:
1052c2e703f9Sxypron.glpk@gmx.de return EFI_EXIT(r);
1053bee91169SAlexander Graf }
1054bee91169SAlexander Graf
efi_locate_protocol(efi_guid_t * protocol,void * registration,void ** protocol_interface)1055bee91169SAlexander Graf static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1056bee91169SAlexander Graf void *registration,
1057bee91169SAlexander Graf void **protocol_interface)
1058bee91169SAlexander Graf {
105988adae5eSxypron.glpk@gmx.de struct list_head *lhandle;
1060bee91169SAlexander Graf int i;
1061bee91169SAlexander Graf
1062bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
106388adae5eSxypron.glpk@gmx.de
106488adae5eSxypron.glpk@gmx.de if (!protocol || !protocol_interface)
106588adae5eSxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
106688adae5eSxypron.glpk@gmx.de
106788adae5eSxypron.glpk@gmx.de list_for_each(lhandle, &efi_obj_list) {
106888adae5eSxypron.glpk@gmx.de struct efi_object *efiobj;
106988adae5eSxypron.glpk@gmx.de
107088adae5eSxypron.glpk@gmx.de efiobj = list_entry(lhandle, struct efi_object, link);
107188adae5eSxypron.glpk@gmx.de for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
107288adae5eSxypron.glpk@gmx.de struct efi_handler *handler = &efiobj->protocols[i];
107388adae5eSxypron.glpk@gmx.de
107488adae5eSxypron.glpk@gmx.de if (!handler->guid)
107588adae5eSxypron.glpk@gmx.de continue;
107688adae5eSxypron.glpk@gmx.de if (!guidcmp(handler->guid, protocol)) {
107788adae5eSxypron.glpk@gmx.de *protocol_interface =
107888adae5eSxypron.glpk@gmx.de handler->protocol_interface;
1079bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
1080bee91169SAlexander Graf }
1081bee91169SAlexander Graf }
108288adae5eSxypron.glpk@gmx.de }
108388adae5eSxypron.glpk@gmx.de *protocol_interface = NULL;
1084bee91169SAlexander Graf
1085bee91169SAlexander Graf return EFI_EXIT(EFI_NOT_FOUND);
1086bee91169SAlexander Graf }
1087bee91169SAlexander Graf
efi_install_multiple_protocol_interfaces(void ** handle,...)1088bee91169SAlexander Graf static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1089bee91169SAlexander Graf void **handle, ...)
1090bee91169SAlexander Graf {
1091bee91169SAlexander Graf EFI_ENTRY("%p", handle);
109258b83586Sxypron.glpk@gmx.de
109358b83586Sxypron.glpk@gmx.de va_list argptr;
109458b83586Sxypron.glpk@gmx.de efi_guid_t *protocol;
109558b83586Sxypron.glpk@gmx.de void *protocol_interface;
109658b83586Sxypron.glpk@gmx.de efi_status_t r = EFI_SUCCESS;
109758b83586Sxypron.glpk@gmx.de int i = 0;
109858b83586Sxypron.glpk@gmx.de
109958b83586Sxypron.glpk@gmx.de if (!handle)
110058b83586Sxypron.glpk@gmx.de return EFI_EXIT(EFI_INVALID_PARAMETER);
110158b83586Sxypron.glpk@gmx.de
110258b83586Sxypron.glpk@gmx.de va_start(argptr, handle);
110358b83586Sxypron.glpk@gmx.de for (;;) {
110458b83586Sxypron.glpk@gmx.de protocol = va_arg(argptr, efi_guid_t*);
110558b83586Sxypron.glpk@gmx.de if (!protocol)
110658b83586Sxypron.glpk@gmx.de break;
110758b83586Sxypron.glpk@gmx.de protocol_interface = va_arg(argptr, void*);
110858b83586Sxypron.glpk@gmx.de r = efi_install_protocol_interface(handle, protocol,
110958b83586Sxypron.glpk@gmx.de EFI_NATIVE_INTERFACE,
111058b83586Sxypron.glpk@gmx.de protocol_interface);
111158b83586Sxypron.glpk@gmx.de if (r != EFI_SUCCESS)
111258b83586Sxypron.glpk@gmx.de break;
111358b83586Sxypron.glpk@gmx.de i++;
111458b83586Sxypron.glpk@gmx.de }
111558b83586Sxypron.glpk@gmx.de va_end(argptr);
111658b83586Sxypron.glpk@gmx.de if (r == EFI_SUCCESS)
111758b83586Sxypron.glpk@gmx.de return EFI_EXIT(r);
111858b83586Sxypron.glpk@gmx.de
111958b83586Sxypron.glpk@gmx.de /* If an error occured undo all changes. */
112058b83586Sxypron.glpk@gmx.de va_start(argptr, handle);
112158b83586Sxypron.glpk@gmx.de for (; i; --i) {
112258b83586Sxypron.glpk@gmx.de protocol = va_arg(argptr, efi_guid_t*);
112358b83586Sxypron.glpk@gmx.de protocol_interface = va_arg(argptr, void*);
112458b83586Sxypron.glpk@gmx.de efi_uninstall_protocol_interface(handle, protocol,
112558b83586Sxypron.glpk@gmx.de protocol_interface);
112658b83586Sxypron.glpk@gmx.de }
112758b83586Sxypron.glpk@gmx.de va_end(argptr);
112858b83586Sxypron.glpk@gmx.de
112958b83586Sxypron.glpk@gmx.de return EFI_EXIT(r);
1130bee91169SAlexander Graf }
1131bee91169SAlexander Graf
efi_uninstall_multiple_protocol_interfaces(void * handle,...)1132bee91169SAlexander Graf static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1133bee91169SAlexander Graf void *handle, ...)
1134bee91169SAlexander Graf {
1135bee91169SAlexander Graf EFI_ENTRY("%p", handle);
1136bee91169SAlexander Graf return EFI_EXIT(EFI_INVALID_PARAMETER);
1137bee91169SAlexander Graf }
1138bee91169SAlexander Graf
efi_calculate_crc32(void * data,unsigned long data_size,uint32_t * crc32_p)1139bee91169SAlexander Graf static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1140bee91169SAlexander Graf unsigned long data_size,
1141bee91169SAlexander Graf uint32_t *crc32_p)
1142bee91169SAlexander Graf {
1143bee91169SAlexander Graf EFI_ENTRY("%p, %ld", data, data_size);
1144bee91169SAlexander Graf *crc32_p = crc32(0, data, data_size);
1145bee91169SAlexander Graf return EFI_EXIT(EFI_SUCCESS);
1146bee91169SAlexander Graf }
1147bee91169SAlexander Graf
efi_copy_mem(void * destination,void * source,unsigned long length)1148bee91169SAlexander Graf static void EFIAPI efi_copy_mem(void *destination, void *source,
1149bee91169SAlexander Graf unsigned long length)
1150bee91169SAlexander Graf {
1151bee91169SAlexander Graf EFI_ENTRY("%p, %p, %ld", destination, source, length);
1152bee91169SAlexander Graf memcpy(destination, source, length);
1153bee91169SAlexander Graf }
1154bee91169SAlexander Graf
efi_set_mem(void * buffer,unsigned long size,uint8_t value)1155bee91169SAlexander Graf static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1156bee91169SAlexander Graf {
1157bee91169SAlexander Graf EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1158bee91169SAlexander Graf memset(buffer, value, size);
1159bee91169SAlexander Graf }
1160bee91169SAlexander Graf
efi_open_protocol(void * handle,efi_guid_t * protocol,void ** protocol_interface,void * agent_handle,void * controller_handle,uint32_t attributes)1161bee91169SAlexander Graf static efi_status_t EFIAPI efi_open_protocol(
1162bee91169SAlexander Graf void *handle, efi_guid_t *protocol,
1163bee91169SAlexander Graf void **protocol_interface, void *agent_handle,
1164bee91169SAlexander Graf void *controller_handle, uint32_t attributes)
1165bee91169SAlexander Graf {
1166bee91169SAlexander Graf struct list_head *lhandle;
1167bee91169SAlexander Graf int i;
116869baec67Sxypron.glpk@gmx.de efi_status_t r = EFI_INVALID_PARAMETER;
1169bee91169SAlexander Graf
1170bee91169SAlexander Graf EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1171bee91169SAlexander Graf protocol_interface, agent_handle, controller_handle,
1172bee91169SAlexander Graf attributes);
1173b5349f74Sxypron.glpk@gmx.de
117469baec67Sxypron.glpk@gmx.de if (!handle || !protocol ||
117569baec67Sxypron.glpk@gmx.de (!protocol_interface && attributes !=
117669baec67Sxypron.glpk@gmx.de EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
117769baec67Sxypron.glpk@gmx.de goto out;
117869baec67Sxypron.glpk@gmx.de }
117969baec67Sxypron.glpk@gmx.de
118069baec67Sxypron.glpk@gmx.de switch (attributes) {
118169baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
118269baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
118369baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
118469baec67Sxypron.glpk@gmx.de break;
118569baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
118669baec67Sxypron.glpk@gmx.de if (controller_handle == handle)
118769baec67Sxypron.glpk@gmx.de goto out;
118869baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_BY_DRIVER:
118969baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
119069baec67Sxypron.glpk@gmx.de if (controller_handle == NULL)
119169baec67Sxypron.glpk@gmx.de goto out;
119269baec67Sxypron.glpk@gmx.de case EFI_OPEN_PROTOCOL_EXCLUSIVE:
119369baec67Sxypron.glpk@gmx.de if (agent_handle == NULL)
119469baec67Sxypron.glpk@gmx.de goto out;
119569baec67Sxypron.glpk@gmx.de break;
119669baec67Sxypron.glpk@gmx.de default:
1197b5349f74Sxypron.glpk@gmx.de goto out;
1198b5349f74Sxypron.glpk@gmx.de }
1199b5349f74Sxypron.glpk@gmx.de
1200bee91169SAlexander Graf list_for_each(lhandle, &efi_obj_list) {
1201bee91169SAlexander Graf struct efi_object *efiobj;
1202bee91169SAlexander Graf efiobj = list_entry(lhandle, struct efi_object, link);
1203bee91169SAlexander Graf
1204bee91169SAlexander Graf if (efiobj->handle != handle)
1205bee91169SAlexander Graf continue;
1206bee91169SAlexander Graf
1207bee91169SAlexander Graf for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1208bee91169SAlexander Graf struct efi_handler *handler = &efiobj->protocols[i];
1209bee91169SAlexander Graf const efi_guid_t *hprotocol = handler->guid;
1210bee91169SAlexander Graf if (!hprotocol)
12114b6ed0d7Sxypron.glpk@gmx.de continue;
1212bee91169SAlexander Graf if (!guidcmp(hprotocol, protocol)) {
1213b5349f74Sxypron.glpk@gmx.de if (attributes !=
1214b5349f74Sxypron.glpk@gmx.de EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1215b5349f74Sxypron.glpk@gmx.de *protocol_interface =
1216b5349f74Sxypron.glpk@gmx.de handler->protocol_interface;
1217b5349f74Sxypron.glpk@gmx.de }
1218b5349f74Sxypron.glpk@gmx.de r = EFI_SUCCESS;
1219bee91169SAlexander Graf goto out;
1220bee91169SAlexander Graf }
1221bee91169SAlexander Graf }
122269baec67Sxypron.glpk@gmx.de goto unsupported;
1223bee91169SAlexander Graf }
1224bee91169SAlexander Graf
122569baec67Sxypron.glpk@gmx.de unsupported:
122669baec67Sxypron.glpk@gmx.de r = EFI_UNSUPPORTED;
1227bee91169SAlexander Graf out:
1228bee91169SAlexander Graf return EFI_EXIT(r);
1229bee91169SAlexander Graf }
1230bee91169SAlexander Graf
efi_handle_protocol(void * handle,efi_guid_t * protocol,void ** protocol_interface)1231bee91169SAlexander Graf static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1232bee91169SAlexander Graf efi_guid_t *protocol,
1233bee91169SAlexander Graf void **protocol_interface)
1234bee91169SAlexander Graf {
12358e1d329fSxypron.glpk@gmx.de return efi_open_protocol(handle, protocol, protocol_interface, NULL,
12368e1d329fSxypron.glpk@gmx.de NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1237bee91169SAlexander Graf }
1238bee91169SAlexander Graf
1239bee91169SAlexander Graf static const struct efi_boot_services efi_boot_services = {
1240bee91169SAlexander Graf .hdr = {
1241bee91169SAlexander Graf .headersize = sizeof(struct efi_table_hdr),
1242bee91169SAlexander Graf },
1243bee91169SAlexander Graf .raise_tpl = efi_raise_tpl,
1244bee91169SAlexander Graf .restore_tpl = efi_restore_tpl,
1245bee91169SAlexander Graf .allocate_pages = efi_allocate_pages_ext,
1246bee91169SAlexander Graf .free_pages = efi_free_pages_ext,
1247bee91169SAlexander Graf .get_memory_map = efi_get_memory_map_ext,
1248ead1274bSStefan Brüns .allocate_pool = efi_allocate_pool_ext,
124942417bc8SStefan Brüns .free_pool = efi_free_pool_ext,
125049deb455Sxypron.glpk@gmx.de .create_event = efi_create_event_ext,
1251bfc72462Sxypron.glpk@gmx.de .set_timer = efi_set_timer_ext,
1252bee91169SAlexander Graf .wait_for_event = efi_wait_for_event,
1253c6841592Sxypron.glpk@gmx.de .signal_event = efi_signal_event_ext,
1254bee91169SAlexander Graf .close_event = efi_close_event,
1255bee91169SAlexander Graf .check_event = efi_check_event,
12568bee5a3cSxypron.glpk@gmx.de .install_protocol_interface = efi_install_protocol_interface_ext,
1257bee91169SAlexander Graf .reinstall_protocol_interface = efi_reinstall_protocol_interface,
12583d8e1456Sxypron.glpk@gmx.de .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1259bee91169SAlexander Graf .handle_protocol = efi_handle_protocol,
1260bee91169SAlexander Graf .reserved = NULL,
1261bee91169SAlexander Graf .register_protocol_notify = efi_register_protocol_notify,
126226329584Sxypron.glpk@gmx.de .locate_handle = efi_locate_handle_ext,
1263bee91169SAlexander Graf .locate_device_path = efi_locate_device_path,
1264488bf12dSAlexander Graf .install_configuration_table = efi_install_configuration_table_ext,
1265bee91169SAlexander Graf .load_image = efi_load_image,
1266bee91169SAlexander Graf .start_image = efi_start_image,
1267a86aeaf2SAlexander Graf .exit = efi_exit,
1268bee91169SAlexander Graf .unload_image = efi_unload_image,
1269bee91169SAlexander Graf .exit_boot_services = efi_exit_boot_services,
1270bee91169SAlexander Graf .get_next_monotonic_count = efi_get_next_monotonic_count,
1271bee91169SAlexander Graf .stall = efi_stall,
1272bee91169SAlexander Graf .set_watchdog_timer = efi_set_watchdog_timer,
1273bee91169SAlexander Graf .connect_controller = efi_connect_controller,
1274bee91169SAlexander Graf .disconnect_controller = efi_disconnect_controller,
1275bee91169SAlexander Graf .open_protocol = efi_open_protocol,
1276bee91169SAlexander Graf .close_protocol = efi_close_protocol,
1277bee91169SAlexander Graf .open_protocol_information = efi_open_protocol_information,
1278bee91169SAlexander Graf .protocols_per_handle = efi_protocols_per_handle,
1279bee91169SAlexander Graf .locate_handle_buffer = efi_locate_handle_buffer,
1280bee91169SAlexander Graf .locate_protocol = efi_locate_protocol,
1281bee91169SAlexander Graf .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1282bee91169SAlexander Graf .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1283bee91169SAlexander Graf .calculate_crc32 = efi_calculate_crc32,
1284bee91169SAlexander Graf .copy_mem = efi_copy_mem,
1285bee91169SAlexander Graf .set_mem = efi_set_mem,
1286bee91169SAlexander Graf };
1287bee91169SAlexander Graf
1288bee91169SAlexander Graf
12893c63db9cSAlexander Graf static uint16_t __efi_runtime_data firmware_vendor[] =
1290bee91169SAlexander Graf { 'D','a','s',' ','U','-','b','o','o','t',0 };
1291bee91169SAlexander Graf
12923c63db9cSAlexander Graf struct efi_system_table __efi_runtime_data systab = {
1293bee91169SAlexander Graf .hdr = {
1294bee91169SAlexander Graf .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1295bee91169SAlexander Graf .revision = 0x20005, /* 2.5 */
1296bee91169SAlexander Graf .headersize = sizeof(struct efi_table_hdr),
1297bee91169SAlexander Graf },
1298bee91169SAlexander Graf .fw_vendor = (long)firmware_vendor,
1299bee91169SAlexander Graf .con_in = (void*)&efi_con_in,
1300bee91169SAlexander Graf .con_out = (void*)&efi_con_out,
1301bee91169SAlexander Graf .std_err = (void*)&efi_con_out,
1302bee91169SAlexander Graf .runtime = (void*)&efi_runtime_services,
1303bee91169SAlexander Graf .boottime = (void*)&efi_boot_services,
1304bee91169SAlexander Graf .nr_tables = 0,
1305bee91169SAlexander Graf .tables = (void*)efi_conf_table,
1306bee91169SAlexander Graf };
1307