blob: 99c5a6ee0cabcec7277f9253148d9aa39b29484f [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{
162 if (event->signaled)
163 return;
164 event->signaled = 1;
165 if (event->type & EVT_NOTIFY_SIGNAL) {
Rob Clark63ea77a2017-07-27 08:04:17 -0400166 EFI_CALL(event->notify_function(event, event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200167 }
168}
169
Alexander Grafc15d9212016-03-04 01:09:59 +0100170static efi_status_t efi_unsupported(const char *funcname)
171{
Alexander Graf62a67482016-06-02 11:38:27 +0200172 debug("EFI: App called into unimplemented function %s\n", funcname);
Alexander Grafc15d9212016-03-04 01:09:59 +0100173 return EFI_EXIT(EFI_UNSUPPORTED);
174}
175
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200176static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100177{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200178 EFI_ENTRY("0x%zx", new_tpl);
Alexander Grafc15d9212016-03-04 01:09:59 +0100179 return EFI_EXIT(0);
180}
181
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200182static void EFIAPI efi_restore_tpl(UINTN old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100183{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200184 EFI_ENTRY("0x%zx", old_tpl);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400185 efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100186}
187
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900188static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
189 unsigned long pages,
190 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100191{
192 efi_status_t r;
193
194 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
195 r = efi_allocate_pages(type, memory_type, pages, memory);
196 return EFI_EXIT(r);
197}
198
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900199static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
200 unsigned long pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100201{
202 efi_status_t r;
203
204 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
205 r = efi_free_pages(memory, pages);
206 return EFI_EXIT(r);
207}
208
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900209static efi_status_t EFIAPI efi_get_memory_map_ext(
210 unsigned long *memory_map_size,
211 struct efi_mem_desc *memory_map,
212 unsigned long *map_key,
213 unsigned long *descriptor_size,
214 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100215{
216 efi_status_t r;
217
218 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
219 map_key, descriptor_size, descriptor_version);
220 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
221 descriptor_size, descriptor_version);
222 return EFI_EXIT(r);
223}
224
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200225static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
226 unsigned long size,
227 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100228{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100229 efi_status_t r;
230
231 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200232 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100233 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100234}
235
Stefan Brüns67b67d92016-10-09 22:17:26 +0200236static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100237{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100238 efi_status_t r;
239
240 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200241 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100242 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100243}
244
245/*
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200246 * Our event capabilities are very limited. Only a small limited
247 * number of events is allowed to coexist.
Alexander Grafc15d9212016-03-04 01:09:59 +0100248 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200249static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100250
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200251efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200252 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200253 struct efi_event *event,
254 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200255 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100256{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200257 int i;
258
Jonathan Gray7758b212017-03-12 19:26:07 +1100259 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200260 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100261
262 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200263 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100264
265 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
266 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200267 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100268
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200269 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
270 if (efi_events[i].type)
271 continue;
272 efi_events[i].type = type;
273 efi_events[i].notify_tpl = notify_tpl;
274 efi_events[i].notify_function = notify_function;
275 efi_events[i].notify_context = notify_context;
276 /* Disable timers on bootup */
277 efi_events[i].trigger_next = -1ULL;
278 efi_events[i].signaled = 0;
279 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200280 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200281 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200282 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100283}
284
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200285static efi_status_t EFIAPI efi_create_event_ext(
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200286 uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200287 void (EFIAPI *notify_function) (
288 struct efi_event *event,
289 void *context),
290 void *notify_context, struct efi_event **event)
291{
292 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
293 notify_context);
294 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
295 notify_context, event));
296}
297
298
Alexander Grafc15d9212016-03-04 01:09:59 +0100299/*
300 * Our timers have to work without interrupts, so we check whenever keyboard
301 * input or disk accesses happen if enough time elapsed for it to fire.
302 */
303void efi_timer_check(void)
304{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200305 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100306 u64 now = timer_get_us();
307
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200308 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
309 if (!efi_events[i].type ||
310 !(efi_events[i].type & EVT_TIMER) ||
311 efi_events[i].trigger_type == EFI_TIMER_STOP ||
312 now < efi_events[i].trigger_next)
313 continue;
314 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
315 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200316 efi_events[i].trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200317 efi_events[i].signaled = 0;
318 }
319 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100320 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100321 WATCHDOG_RESET();
322}
323
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200324efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200325 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100326{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200327 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100328
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200329 /*
330 * The parameter defines a multiple of 100ns.
331 * We use multiples of 1000ns. So divide by 10.
332 */
333 trigger_time = efi_div10(trigger_time);
Alexander Grafc15d9212016-03-04 01:09:59 +0100334
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200335 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
336 if (event != &efi_events[i])
337 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100338
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200339 if (!(event->type & EVT_TIMER))
340 break;
341 switch (type) {
342 case EFI_TIMER_STOP:
343 event->trigger_next = -1ULL;
344 break;
345 case EFI_TIMER_PERIODIC:
346 case EFI_TIMER_RELATIVE:
347 event->trigger_next =
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200348 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200349 break;
350 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200351 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200352 }
353 event->trigger_type = type;
354 event->trigger_time = trigger_time;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200355 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100356 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200357 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100358}
359
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200360static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
361 enum efi_timer_delay type,
362 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200363{
364 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
365 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
366}
367
Alexander Grafc15d9212016-03-04 01:09:59 +0100368static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200369 struct efi_event **event,
370 unsigned long *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100371{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200372 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100373
374 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
375
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200376 /* Check parameters */
377 if (!num_events || !event)
378 return EFI_EXIT(EFI_INVALID_PARAMETER);
379 for (i = 0; i < num_events; ++i) {
380 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
381 if (event[i] == &efi_events[j])
382 goto known_event;
383 }
384 return EFI_EXIT(EFI_INVALID_PARAMETER);
385known_event:
386 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
387 return EFI_EXIT(EFI_INVALID_PARAMETER);
388 }
389
390 /* Wait for signal */
391 for (;;) {
392 for (i = 0; i < num_events; ++i) {
393 if (event[i]->signaled)
394 goto out;
395 }
396 /* Allow events to occur. */
397 efi_timer_check();
398 }
399
400out:
401 /*
402 * Reset the signal which is passed to the caller to allow periodic
403 * events to occur.
404 */
405 event[i]->signaled = 0;
406 if (index)
407 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100408
409 return EFI_EXIT(EFI_SUCCESS);
410}
411
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200412static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100413{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200414 int i;
415
Alexander Grafc15d9212016-03-04 01:09:59 +0100416 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200417 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
418 if (event != &efi_events[i])
419 continue;
420 efi_signal_event(event);
421 break;
422 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100423 return EFI_EXIT(EFI_SUCCESS);
424}
425
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200426static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100427{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200428 int i;
429
Alexander Grafc15d9212016-03-04 01:09:59 +0100430 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200431 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
432 if (event == &efi_events[i]) {
433 event->type = 0;
434 event->trigger_next = -1ULL;
435 event->signaled = 0;
436 return EFI_EXIT(EFI_SUCCESS);
437 }
438 }
439 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100440}
441
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200442static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100443{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200444 int i;
445
Alexander Grafc15d9212016-03-04 01:09:59 +0100446 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200447 efi_timer_check();
448 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
449 if (event != &efi_events[i])
450 continue;
451 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
452 break;
453 if (event->signaled)
454 return EFI_EXIT(EFI_SUCCESS);
455 return EFI_EXIT(EFI_NOT_READY);
456 }
457 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100458}
459
460static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
461 efi_guid_t *protocol, int protocol_interface_type,
462 void *protocol_interface)
463{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200464 struct list_head *lhandle;
465 int i;
466 efi_status_t r;
467
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200468 if (!handle || !protocol ||
469 protocol_interface_type != EFI_NATIVE_INTERFACE) {
470 r = EFI_INVALID_PARAMETER;
471 goto out;
472 }
473
474 /* Create new handle if requested. */
475 if (!*handle) {
476 r = EFI_OUT_OF_RESOURCES;
477 goto out;
478 }
479 /* Find object. */
480 list_for_each(lhandle, &efi_obj_list) {
481 struct efi_object *efiobj;
482 efiobj = list_entry(lhandle, struct efi_object, link);
483
484 if (efiobj->handle != *handle)
485 continue;
486 /* Check if protocol is already installed on the handle. */
487 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
488 struct efi_handler *handler = &efiobj->protocols[i];
489
490 if (!handler->guid)
491 continue;
492 if (!guidcmp(handler->guid, protocol)) {
493 r = EFI_INVALID_PARAMETER;
494 goto out;
495 }
496 }
497 /* Install protocol in first empty slot. */
498 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
499 struct efi_handler *handler = &efiobj->protocols[i];
500
501 if (handler->guid)
502 continue;
503
504 handler->guid = protocol;
505 handler->protocol_interface = protocol_interface;
506 r = EFI_SUCCESS;
507 goto out;
508 }
509 r = EFI_OUT_OF_RESOURCES;
510 goto out;
511 }
512 r = EFI_INVALID_PARAMETER;
513out:
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +0200514 return r;
515}
516
517static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
518 efi_guid_t *protocol, int protocol_interface_type,
519 void *protocol_interface)
520{
521 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
522 protocol_interface);
523
524 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
525 protocol_interface_type,
526 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100527}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200528
Alexander Grafc15d9212016-03-04 01:09:59 +0100529static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
530 efi_guid_t *protocol, void *old_interface,
531 void *new_interface)
532{
533 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
534 new_interface);
535 return EFI_EXIT(EFI_ACCESS_DENIED);
536}
537
538static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
539 efi_guid_t *protocol, void *protocol_interface)
540{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200541 struct list_head *lhandle;
542 int i;
543 efi_status_t r = EFI_NOT_FOUND;
544
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200545 if (!handle || !protocol) {
546 r = EFI_INVALID_PARAMETER;
547 goto out;
548 }
549
550 list_for_each(lhandle, &efi_obj_list) {
551 struct efi_object *efiobj;
552 efiobj = list_entry(lhandle, struct efi_object, link);
553
554 if (efiobj->handle != handle)
555 continue;
556
557 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
558 struct efi_handler *handler = &efiobj->protocols[i];
559 const efi_guid_t *hprotocol = handler->guid;
560
561 if (!hprotocol)
562 continue;
563 if (!guidcmp(hprotocol, protocol)) {
564 if (handler->protocol_interface) {
565 r = EFI_ACCESS_DENIED;
566 } else {
567 handler->guid = 0;
568 r = EFI_SUCCESS;
569 }
570 goto out;
571 }
572 }
573 }
574
575out:
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +0200576 return r;
577}
578
579static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
580 efi_guid_t *protocol, void *protocol_interface)
581{
582 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
583
584 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
585 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100586}
587
588static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200589 struct efi_event *event,
Alexander Grafc15d9212016-03-04 01:09:59 +0100590 void **registration)
591{
592 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
593 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
594}
595
596static int efi_search(enum efi_locate_search_type search_type,
597 efi_guid_t *protocol, void *search_key,
598 struct efi_object *efiobj)
599{
600 int i;
601
602 switch (search_type) {
603 case all_handles:
604 return 0;
605 case by_register_notify:
606 return -1;
607 case by_protocol:
608 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
609 const efi_guid_t *guid = efiobj->protocols[i].guid;
610 if (guid && !guidcmp(guid, protocol))
611 return 0;
612 }
613 return -1;
614 }
615
616 return -1;
617}
618
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +0200619static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +0100620 enum efi_locate_search_type search_type,
621 efi_guid_t *protocol, void *search_key,
622 unsigned long *buffer_size, efi_handle_t *buffer)
623{
624 struct list_head *lhandle;
625 unsigned long size = 0;
626
Alexander Grafc15d9212016-03-04 01:09:59 +0100627 /* Count how much space we need */
628 list_for_each(lhandle, &efi_obj_list) {
629 struct efi_object *efiobj;
630 efiobj = list_entry(lhandle, struct efi_object, link);
631 if (!efi_search(search_type, protocol, search_key, efiobj)) {
632 size += sizeof(void*);
633 }
634 }
635
636 if (*buffer_size < size) {
637 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200638 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100639 }
640
Rob Clarkcdee3372017-08-06 14:10:07 -0400641 *buffer_size = size;
642 if (size == 0)
643 return EFI_NOT_FOUND;
644
Alexander Grafc15d9212016-03-04 01:09:59 +0100645 /* Then fill the array */
646 list_for_each(lhandle, &efi_obj_list) {
647 struct efi_object *efiobj;
648 efiobj = list_entry(lhandle, struct efi_object, link);
649 if (!efi_search(search_type, protocol, search_key, efiobj)) {
650 *(buffer++) = efiobj->handle;
651 }
652 }
653
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200654 return EFI_SUCCESS;
655}
656
657static efi_status_t EFIAPI efi_locate_handle_ext(
658 enum efi_locate_search_type search_type,
659 efi_guid_t *protocol, void *search_key,
660 unsigned long *buffer_size, efi_handle_t *buffer)
661{
662 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
663 buffer_size, buffer);
664
665 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
666 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +0100667}
668
669static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
670 struct efi_device_path **device_path,
671 efi_handle_t *device)
672{
673 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
674 return EFI_EXIT(EFI_NOT_FOUND);
675}
676
Alexander Graffe3366f2017-07-26 13:41:04 +0200677/* Collapses configuration table entries, removing index i */
678static void efi_remove_configuration_table(int i)
679{
680 struct efi_configuration_table *this = &efi_conf_table[i];
681 struct efi_configuration_table *next = &efi_conf_table[i+1];
682 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
683
684 memmove(this, next, (ulong)end - (ulong)next);
685 systab.nr_tables--;
686}
687
Alexander Grafc5c11632016-08-19 01:23:24 +0200688efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +0100689{
690 int i;
691
Alexander Grafc15d9212016-03-04 01:09:59 +0100692 /* Check for guid override */
693 for (i = 0; i < systab.nr_tables; i++) {
694 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +0200695 if (table)
696 efi_conf_table[i].table = table;
697 else
698 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +0200699 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100700 }
701 }
702
Alexander Graffe3366f2017-07-26 13:41:04 +0200703 if (!table)
704 return EFI_NOT_FOUND;
705
Alexander Grafc15d9212016-03-04 01:09:59 +0100706 /* No override, check for overflow */
707 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +0200708 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100709
710 /* Add a new entry */
711 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
712 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +0200713 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +0100714
Alexander Grafc5c11632016-08-19 01:23:24 +0200715 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100716}
717
Alexander Grafc5c11632016-08-19 01:23:24 +0200718static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
719 void *table)
720{
721 EFI_ENTRY("%p, %p", guid, table);
722 return EFI_EXIT(efi_install_configuration_table(guid, table));
723}
724
Alexander Grafc15d9212016-03-04 01:09:59 +0100725static efi_status_t EFIAPI efi_load_image(bool boot_policy,
726 efi_handle_t parent_image,
727 struct efi_device_path *file_path,
728 void *source_buffer,
729 unsigned long source_size,
730 efi_handle_t *image_handle)
731{
732 static struct efi_object loaded_image_info_obj = {
733 .protocols = {
734 {
735 .guid = &efi_guid_loaded_image,
Alexander Grafc15d9212016-03-04 01:09:59 +0100736 },
737 },
738 };
739 struct efi_loaded_image *info;
740 struct efi_object *obj;
741
742 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
743 file_path, source_buffer, source_size, image_handle);
744 info = malloc(sizeof(*info));
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +0200745 loaded_image_info_obj.protocols[0].protocol_interface = info;
Alexander Grafc15d9212016-03-04 01:09:59 +0100746 obj = malloc(sizeof(loaded_image_info_obj));
747 memset(info, 0, sizeof(*info));
748 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
749 obj->handle = info;
750 info->file_path = file_path;
751 info->reserved = efi_load_pe(source_buffer, info);
752 if (!info->reserved) {
753 free(info);
754 free(obj);
755 return EFI_EXIT(EFI_UNSUPPORTED);
756 }
757
758 *image_handle = info;
759 list_add_tail(&obj->link, &efi_obj_list);
760
761 return EFI_EXIT(EFI_SUCCESS);
762}
763
764static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
765 unsigned long *exit_data_size,
766 s16 **exit_data)
767{
768 ulong (*entry)(void *image_handle, struct efi_system_table *st);
769 struct efi_loaded_image *info = image_handle;
770
771 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
772 entry = info->reserved;
773
774 efi_is_direct_boot = false;
775
776 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +0200777 if (setjmp(&info->exit_jmp)) {
778 /* We returned from the child image */
779 return EFI_EXIT(info->exit_status);
780 }
781
Rob Clarke7896c32017-07-27 08:04:19 -0400782 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -0400783 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +0100784 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -0400785 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -0400786 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +0100787
788 /* Should usually never get here */
789 return EFI_EXIT(EFI_SUCCESS);
790}
791
Alexander Graf988c0662016-05-20 23:28:23 +0200792static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
793 efi_status_t exit_status, unsigned long exit_data_size,
794 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +0100795{
Alexander Graf988c0662016-05-20 23:28:23 +0200796 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
797
Alexander Grafc15d9212016-03-04 01:09:59 +0100798 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
799 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +0200800
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +0200801 __efi_exit_check();
802
Alexander Graf988c0662016-05-20 23:28:23 +0200803 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +0200804 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +0200805
806 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +0100807}
808
809static struct efi_object *efi_search_obj(void *handle)
810{
811 struct list_head *lhandle;
812
813 list_for_each(lhandle, &efi_obj_list) {
814 struct efi_object *efiobj;
815 efiobj = list_entry(lhandle, struct efi_object, link);
816 if (efiobj->handle == handle)
817 return efiobj;
818 }
819
820 return NULL;
821}
822
823static efi_status_t EFIAPI efi_unload_image(void *image_handle)
824{
825 struct efi_object *efiobj;
826
827 EFI_ENTRY("%p", image_handle);
828 efiobj = efi_search_obj(image_handle);
829 if (efiobj)
830 list_del(&efiobj->link);
831
832 return EFI_EXIT(EFI_SUCCESS);
833}
834
835static void efi_exit_caches(void)
836{
837#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
838 /*
839 * Grub on 32bit ARM needs to have caches disabled before jumping into
840 * a zImage, but does not know of all cache layers. Give it a hand.
841 */
842 if (efi_is_direct_boot)
843 cleanup_before_linux();
844#endif
845}
846
847static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
848 unsigned long map_key)
849{
850 EFI_ENTRY("%p, %ld", image_handle, map_key);
851
Alexander Graf2ebeb442016-11-17 01:02:57 +0100852 board_quiesce_devices();
853
Alexander Grafc15d9212016-03-04 01:09:59 +0100854 /* Fix up caches for EFI payloads if necessary */
855 efi_exit_caches();
856
857 /* This stops all lingering devices */
858 bootm_disable_interrupts();
859
860 /* Give the payload some time to boot */
861 WATCHDOG_RESET();
862
863 return EFI_EXIT(EFI_SUCCESS);
864}
865
866static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
867{
868 static uint64_t mono = 0;
869 EFI_ENTRY("%p", count);
870 *count = mono++;
871 return EFI_EXIT(EFI_SUCCESS);
872}
873
874static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
875{
876 EFI_ENTRY("%ld", microseconds);
877 udelay(microseconds);
878 return EFI_EXIT(EFI_SUCCESS);
879}
880
881static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
882 uint64_t watchdog_code,
883 unsigned long data_size,
884 uint16_t *watchdog_data)
885{
886 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
887 data_size, watchdog_data);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400888 return efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100889}
890
891static efi_status_t EFIAPI efi_connect_controller(
892 efi_handle_t controller_handle,
893 efi_handle_t *driver_image_handle,
894 struct efi_device_path *remain_device_path,
895 bool recursive)
896{
897 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
898 remain_device_path, recursive);
899 return EFI_EXIT(EFI_NOT_FOUND);
900}
901
902static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
903 void *driver_image_handle,
904 void *child_handle)
905{
906 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
907 child_handle);
908 return EFI_EXIT(EFI_INVALID_PARAMETER);
909}
910
911static efi_status_t EFIAPI efi_close_protocol(void *handle,
912 efi_guid_t *protocol,
913 void *agent_handle,
914 void *controller_handle)
915{
916 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
917 controller_handle);
918 return EFI_EXIT(EFI_NOT_FOUND);
919}
920
921static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
922 efi_guid_t *protocol,
923 struct efi_open_protocol_info_entry **entry_buffer,
924 unsigned long *entry_count)
925{
926 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
927 entry_count);
928 return EFI_EXIT(EFI_NOT_FOUND);
929}
930
931static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
932 efi_guid_t ***protocol_buffer,
933 unsigned long *protocol_buffer_count)
934{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200935 unsigned long buffer_size;
936 struct efi_object *efiobj;
937 unsigned long i, j;
938 struct list_head *lhandle;
939 efi_status_t r;
940
Alexander Grafc15d9212016-03-04 01:09:59 +0100941 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
942 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200943
944 if (!handle || !protocol_buffer || !protocol_buffer_count)
945 return EFI_EXIT(EFI_INVALID_PARAMETER);
946
947 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -0400948 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200949 list_for_each(lhandle, &efi_obj_list) {
950 efiobj = list_entry(lhandle, struct efi_object, link);
951
952 if (efiobj->handle != handle)
953 continue;
954
955 /* Count protocols */
956 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
957 if (efiobj->protocols[i].guid)
958 ++*protocol_buffer_count;
959 }
960 /* Copy guids */
961 if (*protocol_buffer_count) {
962 buffer_size = sizeof(efi_guid_t *) *
963 *protocol_buffer_count;
964 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
965 buffer_size,
966 (void **)protocol_buffer);
967 if (r != EFI_SUCCESS)
968 return EFI_EXIT(r);
969 j = 0;
970 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
971 if (efiobj->protocols[i].guid) {
972 (*protocol_buffer)[j] = (void *)
973 efiobj->protocols[i].guid;
974 ++j;
975 }
976 }
977 }
978 break;
979 }
980
981 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100982}
983
984static efi_status_t EFIAPI efi_locate_handle_buffer(
985 enum efi_locate_search_type search_type,
986 efi_guid_t *protocol, void *search_key,
987 unsigned long *no_handles, efi_handle_t **buffer)
988{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +0200989 efi_status_t r;
990 unsigned long buffer_size = 0;
991
Alexander Grafc15d9212016-03-04 01:09:59 +0100992 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
993 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +0200994
995 if (!no_handles || !buffer) {
996 r = EFI_INVALID_PARAMETER;
997 goto out;
998 }
999 *no_handles = 0;
1000 *buffer = NULL;
1001 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1002 *buffer);
1003 if (r != EFI_BUFFER_TOO_SMALL)
1004 goto out;
1005 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1006 (void **)buffer);
1007 if (r != EFI_SUCCESS)
1008 goto out;
1009 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1010 *buffer);
1011 if (r == EFI_SUCCESS)
1012 *no_handles = buffer_size / sizeof(void *);
1013out:
1014 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001015}
1016
Alexander Grafc15d9212016-03-04 01:09:59 +01001017static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1018 void *registration,
1019 void **protocol_interface)
1020{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001021 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001022 int i;
1023
1024 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001025
1026 if (!protocol || !protocol_interface)
1027 return EFI_EXIT(EFI_INVALID_PARAMETER);
1028
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001029 EFI_PRINT_GUID("protocol", protocol);
1030
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001031 list_for_each(lhandle, &efi_obj_list) {
1032 struct efi_object *efiobj;
1033
1034 efiobj = list_entry(lhandle, struct efi_object, link);
1035 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1036 struct efi_handler *handler = &efiobj->protocols[i];
1037
1038 if (!handler->guid)
1039 continue;
1040 if (!guidcmp(handler->guid, protocol)) {
1041 *protocol_interface =
1042 handler->protocol_interface;
1043 return EFI_EXIT(EFI_SUCCESS);
1044 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001045 }
1046 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001047 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001048
1049 return EFI_EXIT(EFI_NOT_FOUND);
1050}
1051
1052static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1053 void **handle, ...)
1054{
1055 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001056
1057 va_list argptr;
1058 efi_guid_t *protocol;
1059 void *protocol_interface;
1060 efi_status_t r = EFI_SUCCESS;
1061 int i = 0;
1062
1063 if (!handle)
1064 return EFI_EXIT(EFI_INVALID_PARAMETER);
1065
1066 va_start(argptr, handle);
1067 for (;;) {
1068 protocol = va_arg(argptr, efi_guid_t*);
1069 if (!protocol)
1070 break;
1071 protocol_interface = va_arg(argptr, void*);
1072 r = efi_install_protocol_interface(handle, protocol,
1073 EFI_NATIVE_INTERFACE,
1074 protocol_interface);
1075 if (r != EFI_SUCCESS)
1076 break;
1077 i++;
1078 }
1079 va_end(argptr);
1080 if (r == EFI_SUCCESS)
1081 return EFI_EXIT(r);
1082
1083 /* If an error occured undo all changes. */
1084 va_start(argptr, handle);
1085 for (; i; --i) {
1086 protocol = va_arg(argptr, efi_guid_t*);
1087 protocol_interface = va_arg(argptr, void*);
1088 efi_uninstall_protocol_interface(handle, protocol,
1089 protocol_interface);
1090 }
1091 va_end(argptr);
1092
1093 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001094}
1095
1096static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1097 void *handle, ...)
1098{
1099 EFI_ENTRY("%p", handle);
1100 return EFI_EXIT(EFI_INVALID_PARAMETER);
1101}
1102
1103static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1104 unsigned long data_size,
1105 uint32_t *crc32_p)
1106{
1107 EFI_ENTRY("%p, %ld", data, data_size);
1108 *crc32_p = crc32(0, data, data_size);
1109 return EFI_EXIT(EFI_SUCCESS);
1110}
1111
1112static void EFIAPI efi_copy_mem(void *destination, void *source,
1113 unsigned long length)
1114{
1115 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1116 memcpy(destination, source, length);
1117}
1118
1119static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1120{
1121 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1122 memset(buffer, value, size);
1123}
1124
1125static efi_status_t EFIAPI efi_open_protocol(
1126 void *handle, efi_guid_t *protocol,
1127 void **protocol_interface, void *agent_handle,
1128 void *controller_handle, uint32_t attributes)
1129{
1130 struct list_head *lhandle;
1131 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001132 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001133
1134 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1135 protocol_interface, agent_handle, controller_handle,
1136 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001137
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001138 if (!handle || !protocol ||
1139 (!protocol_interface && attributes !=
1140 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001141 goto out;
1142 }
1143
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001144 EFI_PRINT_GUID("protocol", protocol);
1145
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001146 switch (attributes) {
1147 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1148 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1149 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1150 break;
1151 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1152 if (controller_handle == handle)
1153 goto out;
1154 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1155 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1156 if (controller_handle == NULL)
1157 goto out;
1158 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1159 if (agent_handle == NULL)
1160 goto out;
1161 break;
1162 default:
1163 goto out;
1164 }
1165
Alexander Grafc15d9212016-03-04 01:09:59 +01001166 list_for_each(lhandle, &efi_obj_list) {
1167 struct efi_object *efiobj;
1168 efiobj = list_entry(lhandle, struct efi_object, link);
1169
1170 if (efiobj->handle != handle)
1171 continue;
1172
1173 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1174 struct efi_handler *handler = &efiobj->protocols[i];
1175 const efi_guid_t *hprotocol = handler->guid;
1176 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001177 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001178 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001179 if (attributes !=
1180 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1181 *protocol_interface =
1182 handler->protocol_interface;
1183 }
1184 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001185 goto out;
1186 }
1187 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001188 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001189 }
1190
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001191unsupported:
1192 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01001193out:
1194 return EFI_EXIT(r);
1195}
1196
1197static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1198 efi_guid_t *protocol,
1199 void **protocol_interface)
1200{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02001201 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1202 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01001203}
1204
1205static const struct efi_boot_services efi_boot_services = {
1206 .hdr = {
1207 .headersize = sizeof(struct efi_table_hdr),
1208 },
1209 .raise_tpl = efi_raise_tpl,
1210 .restore_tpl = efi_restore_tpl,
1211 .allocate_pages = efi_allocate_pages_ext,
1212 .free_pages = efi_free_pages_ext,
1213 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02001214 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02001215 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02001216 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02001217 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001218 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001219 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001220 .close_event = efi_close_event,
1221 .check_event = efi_check_event,
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +02001222 .install_protocol_interface = efi_install_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001223 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +02001224 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001225 .handle_protocol = efi_handle_protocol,
1226 .reserved = NULL,
1227 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001228 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001229 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02001230 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001231 .load_image = efi_load_image,
1232 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02001233 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01001234 .unload_image = efi_unload_image,
1235 .exit_boot_services = efi_exit_boot_services,
1236 .get_next_monotonic_count = efi_get_next_monotonic_count,
1237 .stall = efi_stall,
1238 .set_watchdog_timer = efi_set_watchdog_timer,
1239 .connect_controller = efi_connect_controller,
1240 .disconnect_controller = efi_disconnect_controller,
1241 .open_protocol = efi_open_protocol,
1242 .close_protocol = efi_close_protocol,
1243 .open_protocol_information = efi_open_protocol_information,
1244 .protocols_per_handle = efi_protocols_per_handle,
1245 .locate_handle_buffer = efi_locate_handle_buffer,
1246 .locate_protocol = efi_locate_protocol,
1247 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1248 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1249 .calculate_crc32 = efi_calculate_crc32,
1250 .copy_mem = efi_copy_mem,
1251 .set_mem = efi_set_mem,
1252};
1253
1254
Alexander Graf393dd912016-10-14 13:45:30 +02001255static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01001256 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1257
Alexander Graf393dd912016-10-14 13:45:30 +02001258struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01001259 .hdr = {
1260 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1261 .revision = 0x20005, /* 2.5 */
1262 .headersize = sizeof(struct efi_table_hdr),
1263 },
1264 .fw_vendor = (long)firmware_vendor,
1265 .con_in = (void*)&efi_con_in,
1266 .con_out = (void*)&efi_con_out,
1267 .std_err = (void*)&efi_con_out,
1268 .runtime = (void*)&efi_runtime_services,
1269 .boottime = (void*)&efi_boot_services,
1270 .nr_tables = 0,
1271 .tables = (void*)efi_conf_table,
1272};