blob: 73793cd986724dd5e5a0c0c84224b6d46025d538 [file] [log] [blame]
Alexander Grafc15d9212016-03-04 01:09:59 +01001/*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Grafc15d9212016-03-04 01:09:59 +01009#include <common.h>
10#include <efi_loader.h>
11#include <malloc.h>
12#include <asm/global_data.h>
13#include <libfdt_env.h>
14#include <u-boot/crc.h>
15#include <bootm.h>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21/* This list contains all the EFI objects our payload has access to */
22LIST_HEAD(efi_obj_list);
23
24/*
25 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26 * we need to do trickery with caches. Since we don't want to break the EFI
27 * aware boot path, only apply hacks when loading exiting directly (breaking
28 * direct Linux EFI booting along the way - oh well).
29 */
30static bool efi_is_direct_boot = true;
31
32/*
33 * EFI can pass arbitrary additional "tables" containing vendor specific
34 * information to the payload. One such table is the FDT table which contains
35 * a pointer to a flattened device tree blob.
36 *
37 * In most cases we want to pass an FDT to the payload, so reserve one slot of
38 * config table space for it. The pointer gets populated by do_bootefi_exec().
39 */
Alexander Graf393dd912016-10-14 13:45:30 +020040static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafc15d9212016-03-04 01:09:59 +010041
Simon Glasscdfe6962016-09-25 15:27:35 -060042#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010043/*
44 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45 * fixed when compiling U-Boot. However, the payload does not know about that
46 * restriction so we need to manually swap its and our view of that register on
47 * EFI callback entry/exit.
48 */
49static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060050#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010051
Rob Clark86789d52017-07-27 08:04:18 -040052static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040053static int nesting_level;
Rob Clark86789d52017-07-27 08:04:18 -040054
55/* Called on every callback entry */
56int __efi_entry_check(void)
57{
58 int ret = entry_count++ == 0;
59#ifdef CONFIG_ARM
60 assert(efi_gd);
61 app_gd = gd;
62 gd = efi_gd;
63#endif
64 return ret;
65}
66
67/* Called on every callback exit */
68int __efi_exit_check(void)
69{
70 int ret = --entry_count == 0;
71#ifdef CONFIG_ARM
72 gd = app_gd;
73#endif
74 return ret;
75}
76
Alexander Grafc15d9212016-03-04 01:09:59 +010077/* Called from do_bootefi_exec() */
78void efi_save_gd(void)
79{
Simon Glasscdfe6962016-09-25 15:27:35 -060080#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010081 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060082#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010083}
84
Rob Clark86789d52017-07-27 08:04:18 -040085/*
86 * Special case handler for error/abort that just forces things back
87 * to u-boot world so we can dump out an abort msg, without any care
88 * about returning back to UEFI world.
89 */
Alexander Grafc15d9212016-03-04 01:09:59 +010090void efi_restore_gd(void)
91{
Simon Glasscdfe6962016-09-25 15:27:35 -060092#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010093 /* Only restore if we're already in EFI context */
94 if (!efi_gd)
95 return;
Alexander Grafc15d9212016-03-04 01:09:59 +010096 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060097#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010098}
99
Rob Clarke7896c32017-07-27 08:04:19 -0400100/*
101 * Two spaces per indent level, maxing out at 10.. which ought to be
102 * enough for anyone ;-)
103 */
104static const char *indent_string(int level)
105{
106 const char *indent = " ";
107 const int max = strlen(indent);
108 level = min(max, level * 2);
109 return &indent[max - level];
110}
111
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200112const char *__efi_nesting(void)
113{
114 return indent_string(nesting_level);
115}
116
Rob Clarke7896c32017-07-27 08:04:19 -0400117const char *__efi_nesting_inc(void)
118{
119 return indent_string(nesting_level++);
120}
121
122const char *__efi_nesting_dec(void)
123{
124 return indent_string(--nesting_level);
125}
126
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200127/* Low 32 bit */
128#define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
129/* High 32 bit */
130#define EFI_HIGH32(a) (a >> 32)
131
132/*
133 * 64bit division by 10 implemented as multiplication by 1 / 10
134 *
135 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
136 */
137#define EFI_TENTH 0x199999999999999A
138static u64 efi_div10(u64 a)
139{
140 u64 prod;
141 u64 rem;
142 u64 ret;
143
144 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
145 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
146 rem = EFI_LOW32(prod);
147 ret += EFI_HIGH32(prod);
148 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
149 rem += EFI_LOW32(prod);
150 ret += EFI_HIGH32(prod);
151 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
152 rem += EFI_HIGH32(prod);
153 ret += EFI_HIGH32(rem);
154 /* Round to nearest integer */
155 if (rem >= (1 << 31))
156 ++ret;
157 return ret;
158}
159
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200160void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200161{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200162 if (event->notify_function) {
163 event->queued = 1;
164 /* Put missing TPL check here */
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200165 EFI_CALL_VOID(event->notify_function(event,
166 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200167 }
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200168 event->queued = 0;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200169}
170
Alexander Grafc15d9212016-03-04 01:09:59 +0100171static efi_status_t efi_unsupported(const char *funcname)
172{
Alexander Graf62a67482016-06-02 11:38:27 +0200173 debug("EFI: App called into unimplemented function %s\n", funcname);
Alexander Grafc15d9212016-03-04 01:09:59 +0100174 return EFI_EXIT(EFI_UNSUPPORTED);
175}
176
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200177static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100178{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200179 EFI_ENTRY("0x%zx", new_tpl);
Alexander Grafc15d9212016-03-04 01:09:59 +0100180 return EFI_EXIT(0);
181}
182
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200183static void EFIAPI efi_restore_tpl(UINTN old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100184{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200185 EFI_ENTRY("0x%zx", old_tpl);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400186 efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100187}
188
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900189static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
190 unsigned long pages,
191 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100192{
193 efi_status_t r;
194
195 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
196 r = efi_allocate_pages(type, memory_type, pages, memory);
197 return EFI_EXIT(r);
198}
199
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900200static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
201 unsigned long pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100202{
203 efi_status_t r;
204
205 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
206 r = efi_free_pages(memory, pages);
207 return EFI_EXIT(r);
208}
209
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900210static efi_status_t EFIAPI efi_get_memory_map_ext(
211 unsigned long *memory_map_size,
212 struct efi_mem_desc *memory_map,
213 unsigned long *map_key,
214 unsigned long *descriptor_size,
215 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100216{
217 efi_status_t r;
218
219 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
220 map_key, descriptor_size, descriptor_version);
221 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
222 descriptor_size, descriptor_version);
223 return EFI_EXIT(r);
224}
225
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200226static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
227 unsigned long size,
228 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100229{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100230 efi_status_t r;
231
232 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200233 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100234 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100235}
236
Stefan Brüns67b67d92016-10-09 22:17:26 +0200237static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100238{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100239 efi_status_t r;
240
241 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200242 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100243 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100244}
245
246/*
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200247 * Our event capabilities are very limited. Only a small limited
248 * number of events is allowed to coexist.
Alexander Grafc15d9212016-03-04 01:09:59 +0100249 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200250static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100251
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200252efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200253 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200254 struct efi_event *event,
255 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200256 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100257{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200258 int i;
259
Jonathan Gray7758b212017-03-12 19:26:07 +1100260 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200261 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100262
263 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200264 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100265
266 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
267 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200268 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100269
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200270 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
271 if (efi_events[i].type)
272 continue;
273 efi_events[i].type = type;
274 efi_events[i].notify_tpl = notify_tpl;
275 efi_events[i].notify_function = notify_function;
276 efi_events[i].notify_context = notify_context;
277 /* Disable timers on bootup */
278 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200279 efi_events[i].queued = 0;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200280 efi_events[i].signaled = 0;
281 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200282 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200283 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200284 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100285}
286
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200287static efi_status_t EFIAPI efi_create_event_ext(
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200288 uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200289 void (EFIAPI *notify_function) (
290 struct efi_event *event,
291 void *context),
292 void *notify_context, struct efi_event **event)
293{
294 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
295 notify_context);
296 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
297 notify_context, event));
298}
299
300
Alexander Grafc15d9212016-03-04 01:09:59 +0100301/*
302 * Our timers have to work without interrupts, so we check whenever keyboard
303 * input or disk accesses happen if enough time elapsed for it to fire.
304 */
305void efi_timer_check(void)
306{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200307 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100308 u64 now = timer_get_us();
309
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200310 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200311 if (!efi_events[i].type)
312 continue;
313 if (efi_events[i].queued)
314 efi_signal_event(&efi_events[i]);
315 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200316 now < efi_events[i].trigger_next)
317 continue;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200318 switch (efi_events[i].trigger_type) {
319 case EFI_TIMER_RELATIVE:
320 efi_events[i].trigger_type = EFI_TIMER_STOP;
321 break;
322 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200323 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200324 efi_events[i].trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200325 break;
326 default:
327 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200328 }
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200329 efi_events[i].signaled = 1;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200330 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100331 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100332 WATCHDOG_RESET();
333}
334
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200335efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200336 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100337{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200338 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100339
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200340 /*
341 * The parameter defines a multiple of 100ns.
342 * We use multiples of 1000ns. So divide by 10.
343 */
344 trigger_time = efi_div10(trigger_time);
Alexander Grafc15d9212016-03-04 01:09:59 +0100345
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200346 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
347 if (event != &efi_events[i])
348 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100349
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200350 if (!(event->type & EVT_TIMER))
351 break;
352 switch (type) {
353 case EFI_TIMER_STOP:
354 event->trigger_next = -1ULL;
355 break;
356 case EFI_TIMER_PERIODIC:
357 case EFI_TIMER_RELATIVE:
358 event->trigger_next =
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200359 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200360 break;
361 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200362 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200363 }
364 event->trigger_type = type;
365 event->trigger_time = trigger_time;
Heinrich Schuchardt49c5c042017-09-15 10:06:14 +0200366 event->signaled = 0;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200367 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100368 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200369 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100370}
371
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200372static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
373 enum efi_timer_delay type,
374 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200375{
376 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
377 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
378}
379
Alexander Grafc15d9212016-03-04 01:09:59 +0100380static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200381 struct efi_event **event,
382 unsigned long *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100383{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200384 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100385
386 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
387
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200388 /* Check parameters */
389 if (!num_events || !event)
390 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200391 /* Put missing TPL check here */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200392 for (i = 0; i < num_events; ++i) {
393 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
394 if (event[i] == &efi_events[j])
395 goto known_event;
396 }
397 return EFI_EXIT(EFI_INVALID_PARAMETER);
398known_event:
399 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
400 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200401 if (!event[i]->signaled)
402 efi_signal_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200403 }
404
405 /* Wait for signal */
406 for (;;) {
407 for (i = 0; i < num_events; ++i) {
408 if (event[i]->signaled)
409 goto out;
410 }
411 /* Allow events to occur. */
412 efi_timer_check();
413 }
414
415out:
416 /*
417 * Reset the signal which is passed to the caller to allow periodic
418 * events to occur.
419 */
420 event[i]->signaled = 0;
421 if (index)
422 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100423
424 return EFI_EXIT(EFI_SUCCESS);
425}
426
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200427static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100428{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200429 int i;
430
Alexander Grafc15d9212016-03-04 01:09:59 +0100431 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200432 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
433 if (event != &efi_events[i])
434 continue;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200435 if (event->signaled)
436 break;
437 event->signaled = 1;
438 if (event->type & EVT_NOTIFY_SIGNAL)
439 efi_signal_event(event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200440 break;
441 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100442 return EFI_EXIT(EFI_SUCCESS);
443}
444
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200445static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100446{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200447 int i;
448
Alexander Grafc15d9212016-03-04 01:09:59 +0100449 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200450 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
451 if (event == &efi_events[i]) {
452 event->type = 0;
453 event->trigger_next = -1ULL;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200454 event->queued = 0;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200455 event->signaled = 0;
456 return EFI_EXIT(EFI_SUCCESS);
457 }
458 }
459 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100460}
461
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200462static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100463{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200464 int i;
465
Alexander Grafc15d9212016-03-04 01:09:59 +0100466 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200467 efi_timer_check();
468 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
469 if (event != &efi_events[i])
470 continue;
471 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
472 break;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200473 if (!event->signaled)
474 efi_signal_event(event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200475 if (event->signaled)
476 return EFI_EXIT(EFI_SUCCESS);
477 return EFI_EXIT(EFI_NOT_READY);
478 }
479 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100480}
481
482static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
483 efi_guid_t *protocol, int protocol_interface_type,
484 void *protocol_interface)
485{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200486 struct list_head *lhandle;
487 int i;
488 efi_status_t r;
489
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200490 if (!handle || !protocol ||
491 protocol_interface_type != EFI_NATIVE_INTERFACE) {
492 r = EFI_INVALID_PARAMETER;
493 goto out;
494 }
495
496 /* Create new handle if requested. */
497 if (!*handle) {
498 r = EFI_OUT_OF_RESOURCES;
499 goto out;
500 }
501 /* Find object. */
502 list_for_each(lhandle, &efi_obj_list) {
503 struct efi_object *efiobj;
504 efiobj = list_entry(lhandle, struct efi_object, link);
505
506 if (efiobj->handle != *handle)
507 continue;
508 /* Check if protocol is already installed on the handle. */
509 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
510 struct efi_handler *handler = &efiobj->protocols[i];
511
512 if (!handler->guid)
513 continue;
514 if (!guidcmp(handler->guid, protocol)) {
515 r = EFI_INVALID_PARAMETER;
516 goto out;
517 }
518 }
519 /* Install protocol in first empty slot. */
520 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
521 struct efi_handler *handler = &efiobj->protocols[i];
522
523 if (handler->guid)
524 continue;
525
526 handler->guid = protocol;
527 handler->protocol_interface = protocol_interface;
528 r = EFI_SUCCESS;
529 goto out;
530 }
531 r = EFI_OUT_OF_RESOURCES;
532 goto out;
533 }
534 r = EFI_INVALID_PARAMETER;
535out:
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +0200536 return r;
537}
538
539static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
540 efi_guid_t *protocol, int protocol_interface_type,
541 void *protocol_interface)
542{
543 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
544 protocol_interface);
545
546 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
547 protocol_interface_type,
548 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100549}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200550
Alexander Grafc15d9212016-03-04 01:09:59 +0100551static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
552 efi_guid_t *protocol, void *old_interface,
553 void *new_interface)
554{
555 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
556 new_interface);
557 return EFI_EXIT(EFI_ACCESS_DENIED);
558}
559
560static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
561 efi_guid_t *protocol, void *protocol_interface)
562{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200563 struct list_head *lhandle;
564 int i;
565 efi_status_t r = EFI_NOT_FOUND;
566
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200567 if (!handle || !protocol) {
568 r = EFI_INVALID_PARAMETER;
569 goto out;
570 }
571
572 list_for_each(lhandle, &efi_obj_list) {
573 struct efi_object *efiobj;
574 efiobj = list_entry(lhandle, struct efi_object, link);
575
576 if (efiobj->handle != handle)
577 continue;
578
579 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
580 struct efi_handler *handler = &efiobj->protocols[i];
581 const efi_guid_t *hprotocol = handler->guid;
582
583 if (!hprotocol)
584 continue;
585 if (!guidcmp(hprotocol, protocol)) {
586 if (handler->protocol_interface) {
587 r = EFI_ACCESS_DENIED;
588 } else {
589 handler->guid = 0;
590 r = EFI_SUCCESS;
591 }
592 goto out;
593 }
594 }
595 }
596
597out:
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +0200598 return r;
599}
600
601static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
602 efi_guid_t *protocol, void *protocol_interface)
603{
604 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
605
606 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
607 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100608}
609
610static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200611 struct efi_event *event,
Alexander Grafc15d9212016-03-04 01:09:59 +0100612 void **registration)
613{
614 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
615 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
616}
617
618static int efi_search(enum efi_locate_search_type search_type,
619 efi_guid_t *protocol, void *search_key,
620 struct efi_object *efiobj)
621{
622 int i;
623
624 switch (search_type) {
625 case all_handles:
626 return 0;
627 case by_register_notify:
628 return -1;
629 case by_protocol:
630 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
631 const efi_guid_t *guid = efiobj->protocols[i].guid;
632 if (guid && !guidcmp(guid, protocol))
633 return 0;
634 }
635 return -1;
636 }
637
638 return -1;
639}
640
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +0200641static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +0100642 enum efi_locate_search_type search_type,
643 efi_guid_t *protocol, void *search_key,
644 unsigned long *buffer_size, efi_handle_t *buffer)
645{
646 struct list_head *lhandle;
647 unsigned long size = 0;
648
Alexander Grafc15d9212016-03-04 01:09:59 +0100649 /* Count how much space we need */
650 list_for_each(lhandle, &efi_obj_list) {
651 struct efi_object *efiobj;
652 efiobj = list_entry(lhandle, struct efi_object, link);
653 if (!efi_search(search_type, protocol, search_key, efiobj)) {
654 size += sizeof(void*);
655 }
656 }
657
658 if (*buffer_size < size) {
659 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200660 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100661 }
662
Rob Clarkcdee3372017-08-06 14:10:07 -0400663 *buffer_size = size;
664 if (size == 0)
665 return EFI_NOT_FOUND;
666
Alexander Grafc15d9212016-03-04 01:09:59 +0100667 /* Then fill the array */
668 list_for_each(lhandle, &efi_obj_list) {
669 struct efi_object *efiobj;
670 efiobj = list_entry(lhandle, struct efi_object, link);
671 if (!efi_search(search_type, protocol, search_key, efiobj)) {
672 *(buffer++) = efiobj->handle;
673 }
674 }
675
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200676 return EFI_SUCCESS;
677}
678
679static efi_status_t EFIAPI efi_locate_handle_ext(
680 enum efi_locate_search_type search_type,
681 efi_guid_t *protocol, void *search_key,
682 unsigned long *buffer_size, efi_handle_t *buffer)
683{
684 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
685 buffer_size, buffer);
686
687 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
688 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +0100689}
690
691static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
692 struct efi_device_path **device_path,
693 efi_handle_t *device)
694{
695 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
696 return EFI_EXIT(EFI_NOT_FOUND);
697}
698
Alexander Graffe3366f2017-07-26 13:41:04 +0200699/* Collapses configuration table entries, removing index i */
700static void efi_remove_configuration_table(int i)
701{
702 struct efi_configuration_table *this = &efi_conf_table[i];
703 struct efi_configuration_table *next = &efi_conf_table[i+1];
704 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
705
706 memmove(this, next, (ulong)end - (ulong)next);
707 systab.nr_tables--;
708}
709
Alexander Grafc5c11632016-08-19 01:23:24 +0200710efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +0100711{
712 int i;
713
Alexander Grafc15d9212016-03-04 01:09:59 +0100714 /* Check for guid override */
715 for (i = 0; i < systab.nr_tables; i++) {
716 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +0200717 if (table)
718 efi_conf_table[i].table = table;
719 else
720 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +0200721 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100722 }
723 }
724
Alexander Graffe3366f2017-07-26 13:41:04 +0200725 if (!table)
726 return EFI_NOT_FOUND;
727
Alexander Grafc15d9212016-03-04 01:09:59 +0100728 /* No override, check for overflow */
729 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +0200730 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100731
732 /* Add a new entry */
733 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
734 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +0200735 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +0100736
Alexander Grafc5c11632016-08-19 01:23:24 +0200737 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100738}
739
Alexander Grafc5c11632016-08-19 01:23:24 +0200740static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
741 void *table)
742{
743 EFI_ENTRY("%p, %p", guid, table);
744 return EFI_EXIT(efi_install_configuration_table(guid, table));
745}
746
Alexander Grafc15d9212016-03-04 01:09:59 +0100747static efi_status_t EFIAPI efi_load_image(bool boot_policy,
748 efi_handle_t parent_image,
749 struct efi_device_path *file_path,
750 void *source_buffer,
751 unsigned long source_size,
752 efi_handle_t *image_handle)
753{
754 static struct efi_object loaded_image_info_obj = {
755 .protocols = {
756 {
757 .guid = &efi_guid_loaded_image,
Alexander Grafc15d9212016-03-04 01:09:59 +0100758 },
759 },
760 };
761 struct efi_loaded_image *info;
762 struct efi_object *obj;
763
764 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
765 file_path, source_buffer, source_size, image_handle);
766 info = malloc(sizeof(*info));
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +0200767 loaded_image_info_obj.protocols[0].protocol_interface = info;
Alexander Grafc15d9212016-03-04 01:09:59 +0100768 obj = malloc(sizeof(loaded_image_info_obj));
769 memset(info, 0, sizeof(*info));
770 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
771 obj->handle = info;
772 info->file_path = file_path;
773 info->reserved = efi_load_pe(source_buffer, info);
774 if (!info->reserved) {
775 free(info);
776 free(obj);
777 return EFI_EXIT(EFI_UNSUPPORTED);
778 }
779
780 *image_handle = info;
781 list_add_tail(&obj->link, &efi_obj_list);
782
783 return EFI_EXIT(EFI_SUCCESS);
784}
785
786static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
787 unsigned long *exit_data_size,
788 s16 **exit_data)
789{
790 ulong (*entry)(void *image_handle, struct efi_system_table *st);
791 struct efi_loaded_image *info = image_handle;
792
793 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
794 entry = info->reserved;
795
796 efi_is_direct_boot = false;
797
798 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +0200799 if (setjmp(&info->exit_jmp)) {
800 /* We returned from the child image */
801 return EFI_EXIT(info->exit_status);
802 }
803
Rob Clarke7896c32017-07-27 08:04:19 -0400804 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -0400805 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +0100806 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -0400807 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -0400808 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +0100809
810 /* Should usually never get here */
811 return EFI_EXIT(EFI_SUCCESS);
812}
813
Alexander Graf988c0662016-05-20 23:28:23 +0200814static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
815 efi_status_t exit_status, unsigned long exit_data_size,
816 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +0100817{
Alexander Graf988c0662016-05-20 23:28:23 +0200818 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
819
Alexander Grafc15d9212016-03-04 01:09:59 +0100820 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
821 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +0200822
Alexander Graf87e787a2017-09-03 14:14:17 +0200823 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +0200824 __efi_exit_check();
825
Alexander Graf87e787a2017-09-03 14:14:17 +0200826 /*
827 * But longjmp out with the U-Boot gd, not the application's, as
828 * the other end is a setjmp call inside EFI context.
829 */
830 efi_restore_gd();
831
Alexander Graf988c0662016-05-20 23:28:23 +0200832 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +0200833 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +0200834
835 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +0100836}
837
838static struct efi_object *efi_search_obj(void *handle)
839{
840 struct list_head *lhandle;
841
842 list_for_each(lhandle, &efi_obj_list) {
843 struct efi_object *efiobj;
844 efiobj = list_entry(lhandle, struct efi_object, link);
845 if (efiobj->handle == handle)
846 return efiobj;
847 }
848
849 return NULL;
850}
851
852static efi_status_t EFIAPI efi_unload_image(void *image_handle)
853{
854 struct efi_object *efiobj;
855
856 EFI_ENTRY("%p", image_handle);
857 efiobj = efi_search_obj(image_handle);
858 if (efiobj)
859 list_del(&efiobj->link);
860
861 return EFI_EXIT(EFI_SUCCESS);
862}
863
864static void efi_exit_caches(void)
865{
866#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
867 /*
868 * Grub on 32bit ARM needs to have caches disabled before jumping into
869 * a zImage, but does not know of all cache layers. Give it a hand.
870 */
871 if (efi_is_direct_boot)
872 cleanup_before_linux();
873#endif
874}
875
876static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
877 unsigned long map_key)
878{
879 EFI_ENTRY("%p, %ld", image_handle, map_key);
880
Alexander Graf2ebeb442016-11-17 01:02:57 +0100881 board_quiesce_devices();
882
Alexander Grafc15d9212016-03-04 01:09:59 +0100883 /* Fix up caches for EFI payloads if necessary */
884 efi_exit_caches();
885
886 /* This stops all lingering devices */
887 bootm_disable_interrupts();
888
889 /* Give the payload some time to boot */
890 WATCHDOG_RESET();
891
892 return EFI_EXIT(EFI_SUCCESS);
893}
894
895static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
896{
897 static uint64_t mono = 0;
898 EFI_ENTRY("%p", count);
899 *count = mono++;
900 return EFI_EXIT(EFI_SUCCESS);
901}
902
903static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
904{
905 EFI_ENTRY("%ld", microseconds);
906 udelay(microseconds);
907 return EFI_EXIT(EFI_SUCCESS);
908}
909
910static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
911 uint64_t watchdog_code,
912 unsigned long data_size,
913 uint16_t *watchdog_data)
914{
915 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
916 data_size, watchdog_data);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400917 return efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100918}
919
920static efi_status_t EFIAPI efi_connect_controller(
921 efi_handle_t controller_handle,
922 efi_handle_t *driver_image_handle,
923 struct efi_device_path *remain_device_path,
924 bool recursive)
925{
926 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
927 remain_device_path, recursive);
928 return EFI_EXIT(EFI_NOT_FOUND);
929}
930
931static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
932 void *driver_image_handle,
933 void *child_handle)
934{
935 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
936 child_handle);
937 return EFI_EXIT(EFI_INVALID_PARAMETER);
938}
939
940static efi_status_t EFIAPI efi_close_protocol(void *handle,
941 efi_guid_t *protocol,
942 void *agent_handle,
943 void *controller_handle)
944{
945 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
946 controller_handle);
947 return EFI_EXIT(EFI_NOT_FOUND);
948}
949
950static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
951 efi_guid_t *protocol,
952 struct efi_open_protocol_info_entry **entry_buffer,
953 unsigned long *entry_count)
954{
955 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
956 entry_count);
957 return EFI_EXIT(EFI_NOT_FOUND);
958}
959
960static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
961 efi_guid_t ***protocol_buffer,
962 unsigned long *protocol_buffer_count)
963{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200964 unsigned long buffer_size;
965 struct efi_object *efiobj;
966 unsigned long i, j;
967 struct list_head *lhandle;
968 efi_status_t r;
969
Alexander Grafc15d9212016-03-04 01:09:59 +0100970 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
971 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200972
973 if (!handle || !protocol_buffer || !protocol_buffer_count)
974 return EFI_EXIT(EFI_INVALID_PARAMETER);
975
976 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -0400977 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200978 list_for_each(lhandle, &efi_obj_list) {
979 efiobj = list_entry(lhandle, struct efi_object, link);
980
981 if (efiobj->handle != handle)
982 continue;
983
984 /* Count protocols */
985 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
986 if (efiobj->protocols[i].guid)
987 ++*protocol_buffer_count;
988 }
989 /* Copy guids */
990 if (*protocol_buffer_count) {
991 buffer_size = sizeof(efi_guid_t *) *
992 *protocol_buffer_count;
993 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
994 buffer_size,
995 (void **)protocol_buffer);
996 if (r != EFI_SUCCESS)
997 return EFI_EXIT(r);
998 j = 0;
999 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1000 if (efiobj->protocols[i].guid) {
1001 (*protocol_buffer)[j] = (void *)
1002 efiobj->protocols[i].guid;
1003 ++j;
1004 }
1005 }
1006 }
1007 break;
1008 }
1009
1010 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001011}
1012
1013static efi_status_t EFIAPI efi_locate_handle_buffer(
1014 enum efi_locate_search_type search_type,
1015 efi_guid_t *protocol, void *search_key,
1016 unsigned long *no_handles, efi_handle_t **buffer)
1017{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001018 efi_status_t r;
1019 unsigned long buffer_size = 0;
1020
Alexander Grafc15d9212016-03-04 01:09:59 +01001021 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1022 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001023
1024 if (!no_handles || !buffer) {
1025 r = EFI_INVALID_PARAMETER;
1026 goto out;
1027 }
1028 *no_handles = 0;
1029 *buffer = NULL;
1030 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1031 *buffer);
1032 if (r != EFI_BUFFER_TOO_SMALL)
1033 goto out;
1034 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1035 (void **)buffer);
1036 if (r != EFI_SUCCESS)
1037 goto out;
1038 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1039 *buffer);
1040 if (r == EFI_SUCCESS)
1041 *no_handles = buffer_size / sizeof(void *);
1042out:
1043 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001044}
1045
Alexander Grafc15d9212016-03-04 01:09:59 +01001046static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1047 void *registration,
1048 void **protocol_interface)
1049{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001050 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001051 int i;
1052
1053 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001054
1055 if (!protocol || !protocol_interface)
1056 return EFI_EXIT(EFI_INVALID_PARAMETER);
1057
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001058 EFI_PRINT_GUID("protocol", protocol);
1059
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001060 list_for_each(lhandle, &efi_obj_list) {
1061 struct efi_object *efiobj;
1062
1063 efiobj = list_entry(lhandle, struct efi_object, link);
1064 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1065 struct efi_handler *handler = &efiobj->protocols[i];
1066
1067 if (!handler->guid)
1068 continue;
1069 if (!guidcmp(handler->guid, protocol)) {
1070 *protocol_interface =
1071 handler->protocol_interface;
1072 return EFI_EXIT(EFI_SUCCESS);
1073 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001074 }
1075 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001076 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001077
1078 return EFI_EXIT(EFI_NOT_FOUND);
1079}
1080
1081static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1082 void **handle, ...)
1083{
1084 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001085
1086 va_list argptr;
1087 efi_guid_t *protocol;
1088 void *protocol_interface;
1089 efi_status_t r = EFI_SUCCESS;
1090 int i = 0;
1091
1092 if (!handle)
1093 return EFI_EXIT(EFI_INVALID_PARAMETER);
1094
1095 va_start(argptr, handle);
1096 for (;;) {
1097 protocol = va_arg(argptr, efi_guid_t*);
1098 if (!protocol)
1099 break;
1100 protocol_interface = va_arg(argptr, void*);
1101 r = efi_install_protocol_interface(handle, protocol,
1102 EFI_NATIVE_INTERFACE,
1103 protocol_interface);
1104 if (r != EFI_SUCCESS)
1105 break;
1106 i++;
1107 }
1108 va_end(argptr);
1109 if (r == EFI_SUCCESS)
1110 return EFI_EXIT(r);
1111
1112 /* If an error occured undo all changes. */
1113 va_start(argptr, handle);
1114 for (; i; --i) {
1115 protocol = va_arg(argptr, efi_guid_t*);
1116 protocol_interface = va_arg(argptr, void*);
1117 efi_uninstall_protocol_interface(handle, protocol,
1118 protocol_interface);
1119 }
1120 va_end(argptr);
1121
1122 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001123}
1124
1125static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1126 void *handle, ...)
1127{
1128 EFI_ENTRY("%p", handle);
1129 return EFI_EXIT(EFI_INVALID_PARAMETER);
1130}
1131
1132static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1133 unsigned long data_size,
1134 uint32_t *crc32_p)
1135{
1136 EFI_ENTRY("%p, %ld", data, data_size);
1137 *crc32_p = crc32(0, data, data_size);
1138 return EFI_EXIT(EFI_SUCCESS);
1139}
1140
1141static void EFIAPI efi_copy_mem(void *destination, void *source,
1142 unsigned long length)
1143{
1144 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1145 memcpy(destination, source, length);
1146}
1147
1148static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1149{
1150 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1151 memset(buffer, value, size);
1152}
1153
1154static efi_status_t EFIAPI efi_open_protocol(
1155 void *handle, efi_guid_t *protocol,
1156 void **protocol_interface, void *agent_handle,
1157 void *controller_handle, uint32_t attributes)
1158{
1159 struct list_head *lhandle;
1160 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001161 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001162
1163 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1164 protocol_interface, agent_handle, controller_handle,
1165 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001166
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001167 if (!handle || !protocol ||
1168 (!protocol_interface && attributes !=
1169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001170 goto out;
1171 }
1172
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001173 EFI_PRINT_GUID("protocol", protocol);
1174
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001175 switch (attributes) {
1176 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1177 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1178 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1179 break;
1180 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1181 if (controller_handle == handle)
1182 goto out;
1183 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1184 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1185 if (controller_handle == NULL)
1186 goto out;
1187 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1188 if (agent_handle == NULL)
1189 goto out;
1190 break;
1191 default:
1192 goto out;
1193 }
1194
Alexander Grafc15d9212016-03-04 01:09:59 +01001195 list_for_each(lhandle, &efi_obj_list) {
1196 struct efi_object *efiobj;
1197 efiobj = list_entry(lhandle, struct efi_object, link);
1198
1199 if (efiobj->handle != handle)
1200 continue;
1201
1202 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1203 struct efi_handler *handler = &efiobj->protocols[i];
1204 const efi_guid_t *hprotocol = handler->guid;
1205 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001206 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001207 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001208 if (attributes !=
1209 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1210 *protocol_interface =
1211 handler->protocol_interface;
1212 }
1213 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001214 goto out;
1215 }
1216 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001217 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001218 }
1219
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001220unsupported:
1221 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01001222out:
1223 return EFI_EXIT(r);
1224}
1225
1226static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1227 efi_guid_t *protocol,
1228 void **protocol_interface)
1229{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02001230 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1231 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01001232}
1233
1234static const struct efi_boot_services efi_boot_services = {
1235 .hdr = {
1236 .headersize = sizeof(struct efi_table_hdr),
1237 },
1238 .raise_tpl = efi_raise_tpl,
1239 .restore_tpl = efi_restore_tpl,
1240 .allocate_pages = efi_allocate_pages_ext,
1241 .free_pages = efi_free_pages_ext,
1242 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02001243 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02001244 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02001245 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02001246 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001247 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001248 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001249 .close_event = efi_close_event,
1250 .check_event = efi_check_event,
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +02001251 .install_protocol_interface = efi_install_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001252 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +02001253 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001254 .handle_protocol = efi_handle_protocol,
1255 .reserved = NULL,
1256 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001257 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001258 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02001259 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001260 .load_image = efi_load_image,
1261 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02001262 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01001263 .unload_image = efi_unload_image,
1264 .exit_boot_services = efi_exit_boot_services,
1265 .get_next_monotonic_count = efi_get_next_monotonic_count,
1266 .stall = efi_stall,
1267 .set_watchdog_timer = efi_set_watchdog_timer,
1268 .connect_controller = efi_connect_controller,
1269 .disconnect_controller = efi_disconnect_controller,
1270 .open_protocol = efi_open_protocol,
1271 .close_protocol = efi_close_protocol,
1272 .open_protocol_information = efi_open_protocol_information,
1273 .protocols_per_handle = efi_protocols_per_handle,
1274 .locate_handle_buffer = efi_locate_handle_buffer,
1275 .locate_protocol = efi_locate_protocol,
1276 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1277 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1278 .calculate_crc32 = efi_calculate_crc32,
1279 .copy_mem = efi_copy_mem,
1280 .set_mem = efi_set_mem,
1281};
1282
1283
Alexander Graf393dd912016-10-14 13:45:30 +02001284static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01001285 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1286
Alexander Graf393dd912016-10-14 13:45:30 +02001287struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01001288 .hdr = {
1289 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1290 .revision = 0x20005, /* 2.5 */
1291 .headersize = sizeof(struct efi_table_hdr),
1292 },
1293 .fw_vendor = (long)firmware_vendor,
1294 .con_in = (void*)&efi_con_in,
1295 .con_out = (void*)&efi_con_out,
1296 .std_err = (void*)&efi_con_out,
1297 .runtime = (void*)&efi_runtime_services,
1298 .boottime = (void*)&efi_boot_services,
1299 .nr_tables = 0,
1300 .tables = (void*)efi_conf_table,
1301};