blob: f01b80598c627947a705f4e04719a978b668cb7b [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
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020021/* Task priority level */
22static UINTN efi_tpl = TPL_APPLICATION;
23
Alexander Grafc15d9212016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
27/*
28 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
29 * we need to do trickery with caches. Since we don't want to break the EFI
30 * aware boot path, only apply hacks when loading exiting directly (breaking
31 * direct Linux EFI booting along the way - oh well).
32 */
33static bool efi_is_direct_boot = true;
34
35/*
36 * EFI can pass arbitrary additional "tables" containing vendor specific
37 * information to the payload. One such table is the FDT table which contains
38 * a pointer to a flattened device tree blob.
39 *
40 * In most cases we want to pass an FDT to the payload, so reserve one slot of
41 * config table space for it. The pointer gets populated by do_bootefi_exec().
42 */
Alexander Graf393dd912016-10-14 13:45:30 +020043static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafc15d9212016-03-04 01:09:59 +010044
Simon Glasscdfe6962016-09-25 15:27:35 -060045#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010046/*
47 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
48 * fixed when compiling U-Boot. However, the payload does not know about that
49 * restriction so we need to manually swap its and our view of that register on
50 * EFI callback entry/exit.
51 */
52static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060053#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010054
Rob Clark86789d52017-07-27 08:04:18 -040055static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040056static int nesting_level;
Rob Clark86789d52017-07-27 08:04:18 -040057
58/* Called on every callback entry */
59int __efi_entry_check(void)
60{
61 int ret = entry_count++ == 0;
62#ifdef CONFIG_ARM
63 assert(efi_gd);
64 app_gd = gd;
65 gd = efi_gd;
66#endif
67 return ret;
68}
69
70/* Called on every callback exit */
71int __efi_exit_check(void)
72{
73 int ret = --entry_count == 0;
74#ifdef CONFIG_ARM
75 gd = app_gd;
76#endif
77 return ret;
78}
79
Alexander Grafc15d9212016-03-04 01:09:59 +010080/* Called from do_bootefi_exec() */
81void efi_save_gd(void)
82{
Simon Glasscdfe6962016-09-25 15:27:35 -060083#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010084 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060085#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010086}
87
Rob Clark86789d52017-07-27 08:04:18 -040088/*
89 * Special case handler for error/abort that just forces things back
90 * to u-boot world so we can dump out an abort msg, without any care
91 * about returning back to UEFI world.
92 */
Alexander Grafc15d9212016-03-04 01:09:59 +010093void efi_restore_gd(void)
94{
Simon Glasscdfe6962016-09-25 15:27:35 -060095#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010096 /* Only restore if we're already in EFI context */
97 if (!efi_gd)
98 return;
Alexander Grafc15d9212016-03-04 01:09:59 +010099 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600100#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100101}
102
Rob Clarke7896c32017-07-27 08:04:19 -0400103/*
104 * Two spaces per indent level, maxing out at 10.. which ought to be
105 * enough for anyone ;-)
106 */
107static const char *indent_string(int level)
108{
109 const char *indent = " ";
110 const int max = strlen(indent);
111 level = min(max, level * 2);
112 return &indent[max - level];
113}
114
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200115const char *__efi_nesting(void)
116{
117 return indent_string(nesting_level);
118}
119
Rob Clarke7896c32017-07-27 08:04:19 -0400120const char *__efi_nesting_inc(void)
121{
122 return indent_string(nesting_level++);
123}
124
125const char *__efi_nesting_dec(void)
126{
127 return indent_string(--nesting_level);
128}
129
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200130/* Low 32 bit */
131#define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
132/* High 32 bit */
133#define EFI_HIGH32(a) (a >> 32)
134
135/*
136 * 64bit division by 10 implemented as multiplication by 1 / 10
137 *
138 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
139 */
140#define EFI_TENTH 0x199999999999999A
141static u64 efi_div10(u64 a)
142{
143 u64 prod;
144 u64 rem;
145 u64 ret;
146
147 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
148 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
149 rem = EFI_LOW32(prod);
150 ret += EFI_HIGH32(prod);
151 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
152 rem += EFI_LOW32(prod);
153 ret += EFI_HIGH32(prod);
154 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
155 rem += EFI_HIGH32(prod);
156 ret += EFI_HIGH32(rem);
157 /* Round to nearest integer */
158 if (rem >= (1 << 31))
159 ++ret;
160 return ret;
161}
162
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200163void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200164{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200165 if (event->notify_function) {
166 event->queued = 1;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200167 /* Check TPL */
168 if (efi_tpl >= event->notify_tpl)
169 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200170 EFI_CALL_VOID(event->notify_function(event,
171 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200172 }
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200173 event->queued = 0;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200174}
175
Alexander Grafc15d9212016-03-04 01:09:59 +0100176static efi_status_t efi_unsupported(const char *funcname)
177{
Alexander Graf62a67482016-06-02 11:38:27 +0200178 debug("EFI: App called into unimplemented function %s\n", funcname);
Alexander Grafc15d9212016-03-04 01:09:59 +0100179 return EFI_EXIT(EFI_UNSUPPORTED);
180}
181
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200182static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100183{
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200184 UINTN old_tpl = efi_tpl;
185
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200186 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200187
188 if (new_tpl < efi_tpl)
189 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
190 efi_tpl = new_tpl;
191 if (efi_tpl > TPL_HIGH_LEVEL)
192 efi_tpl = TPL_HIGH_LEVEL;
193
194 EFI_EXIT(EFI_SUCCESS);
195 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100196}
197
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200198static void EFIAPI efi_restore_tpl(UINTN old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100199{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200200 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200201
202 if (old_tpl > efi_tpl)
203 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
204 efi_tpl = old_tpl;
205 if (efi_tpl > TPL_HIGH_LEVEL)
206 efi_tpl = TPL_HIGH_LEVEL;
207
208 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100209}
210
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900211static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
212 unsigned long pages,
213 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100214{
215 efi_status_t r;
216
217 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
218 r = efi_allocate_pages(type, memory_type, pages, memory);
219 return EFI_EXIT(r);
220}
221
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900222static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
223 unsigned long pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100224{
225 efi_status_t r;
226
227 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
228 r = efi_free_pages(memory, pages);
229 return EFI_EXIT(r);
230}
231
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900232static efi_status_t EFIAPI efi_get_memory_map_ext(
233 unsigned long *memory_map_size,
234 struct efi_mem_desc *memory_map,
235 unsigned long *map_key,
236 unsigned long *descriptor_size,
237 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100238{
239 efi_status_t r;
240
241 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
242 map_key, descriptor_size, descriptor_version);
243 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
244 descriptor_size, descriptor_version);
245 return EFI_EXIT(r);
246}
247
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200248static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
249 unsigned long size,
250 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100251{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100252 efi_status_t r;
253
254 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200255 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100256 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100257}
258
Stefan Brüns67b67d92016-10-09 22:17:26 +0200259static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100260{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100261 efi_status_t r;
262
263 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200264 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100265 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100266}
267
268/*
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200269 * Our event capabilities are very limited. Only a small limited
270 * number of events is allowed to coexist.
Alexander Grafc15d9212016-03-04 01:09:59 +0100271 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200272static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100273
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200274efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200275 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200276 struct efi_event *event,
277 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200278 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100279{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200280 int i;
281
Jonathan Gray7758b212017-03-12 19:26:07 +1100282 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200283 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100284
285 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200286 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100287
288 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
289 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200290 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100291
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200292 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
293 if (efi_events[i].type)
294 continue;
295 efi_events[i].type = type;
296 efi_events[i].notify_tpl = notify_tpl;
297 efi_events[i].notify_function = notify_function;
298 efi_events[i].notify_context = notify_context;
299 /* Disable timers on bootup */
300 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200301 efi_events[i].queued = 0;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200302 efi_events[i].signaled = 0;
303 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200304 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200305 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200306 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100307}
308
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200309static efi_status_t EFIAPI efi_create_event_ext(
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200310 uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200311 void (EFIAPI *notify_function) (
312 struct efi_event *event,
313 void *context),
314 void *notify_context, struct efi_event **event)
315{
316 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
317 notify_context);
318 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
319 notify_context, event));
320}
321
322
Alexander Grafc15d9212016-03-04 01:09:59 +0100323/*
324 * Our timers have to work without interrupts, so we check whenever keyboard
325 * input or disk accesses happen if enough time elapsed for it to fire.
326 */
327void efi_timer_check(void)
328{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200329 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100330 u64 now = timer_get_us();
331
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200332 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200333 if (!efi_events[i].type)
334 continue;
335 if (efi_events[i].queued)
336 efi_signal_event(&efi_events[i]);
337 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200338 now < efi_events[i].trigger_next)
339 continue;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200340 switch (efi_events[i].trigger_type) {
341 case EFI_TIMER_RELATIVE:
342 efi_events[i].trigger_type = EFI_TIMER_STOP;
343 break;
344 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200345 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200346 efi_events[i].trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200347 break;
348 default:
349 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200350 }
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200351 efi_events[i].signaled = 1;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200352 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100353 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100354 WATCHDOG_RESET();
355}
356
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200357efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200358 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100359{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200360 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100361
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200362 /*
363 * The parameter defines a multiple of 100ns.
364 * We use multiples of 1000ns. So divide by 10.
365 */
366 trigger_time = efi_div10(trigger_time);
Alexander Grafc15d9212016-03-04 01:09:59 +0100367
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200368 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
369 if (event != &efi_events[i])
370 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100371
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200372 if (!(event->type & EVT_TIMER))
373 break;
374 switch (type) {
375 case EFI_TIMER_STOP:
376 event->trigger_next = -1ULL;
377 break;
378 case EFI_TIMER_PERIODIC:
379 case EFI_TIMER_RELATIVE:
380 event->trigger_next =
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200381 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200382 break;
383 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200384 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200385 }
386 event->trigger_type = type;
387 event->trigger_time = trigger_time;
Heinrich Schuchardt49c5c042017-09-15 10:06:14 +0200388 event->signaled = 0;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200389 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100390 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200391 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100392}
393
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200394static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
395 enum efi_timer_delay type,
396 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200397{
398 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
399 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
400}
401
Alexander Grafc15d9212016-03-04 01:09:59 +0100402static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200403 struct efi_event **event,
404 unsigned long *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100405{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200406 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100407
408 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
409
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200410 /* Check parameters */
411 if (!num_events || !event)
412 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200413 /* Check TPL */
414 if (efi_tpl != TPL_APPLICATION)
415 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200416 for (i = 0; i < num_events; ++i) {
417 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
418 if (event[i] == &efi_events[j])
419 goto known_event;
420 }
421 return EFI_EXIT(EFI_INVALID_PARAMETER);
422known_event:
423 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
424 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200425 if (!event[i]->signaled)
426 efi_signal_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200427 }
428
429 /* Wait for signal */
430 for (;;) {
431 for (i = 0; i < num_events; ++i) {
432 if (event[i]->signaled)
433 goto out;
434 }
435 /* Allow events to occur. */
436 efi_timer_check();
437 }
438
439out:
440 /*
441 * Reset the signal which is passed to the caller to allow periodic
442 * events to occur.
443 */
444 event[i]->signaled = 0;
445 if (index)
446 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100447
448 return EFI_EXIT(EFI_SUCCESS);
449}
450
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200451static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100452{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200453 int i;
454
Alexander Grafc15d9212016-03-04 01:09:59 +0100455 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200456 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
457 if (event != &efi_events[i])
458 continue;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200459 if (event->signaled)
460 break;
461 event->signaled = 1;
462 if (event->type & EVT_NOTIFY_SIGNAL)
463 efi_signal_event(event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200464 break;
465 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100466 return EFI_EXIT(EFI_SUCCESS);
467}
468
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200469static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100470{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200471 int i;
472
Alexander Grafc15d9212016-03-04 01:09:59 +0100473 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200474 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
475 if (event == &efi_events[i]) {
476 event->type = 0;
477 event->trigger_next = -1ULL;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200478 event->queued = 0;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200479 event->signaled = 0;
480 return EFI_EXIT(EFI_SUCCESS);
481 }
482 }
483 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100484}
485
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200486static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100487{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200488 int i;
489
Alexander Grafc15d9212016-03-04 01:09:59 +0100490 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200491 efi_timer_check();
492 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
493 if (event != &efi_events[i])
494 continue;
495 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
496 break;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200497 if (!event->signaled)
498 efi_signal_event(event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200499 if (event->signaled)
500 return EFI_EXIT(EFI_SUCCESS);
501 return EFI_EXIT(EFI_NOT_READY);
502 }
503 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100504}
505
506static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
507 efi_guid_t *protocol, int protocol_interface_type,
508 void *protocol_interface)
509{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200510 struct list_head *lhandle;
511 int i;
512 efi_status_t r;
513
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200514 if (!handle || !protocol ||
515 protocol_interface_type != EFI_NATIVE_INTERFACE) {
516 r = EFI_INVALID_PARAMETER;
517 goto out;
518 }
519
520 /* Create new handle if requested. */
521 if (!*handle) {
522 r = EFI_OUT_OF_RESOURCES;
523 goto out;
524 }
525 /* Find object. */
526 list_for_each(lhandle, &efi_obj_list) {
527 struct efi_object *efiobj;
528 efiobj = list_entry(lhandle, struct efi_object, link);
529
530 if (efiobj->handle != *handle)
531 continue;
532 /* Check if protocol is already installed on the handle. */
533 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
534 struct efi_handler *handler = &efiobj->protocols[i];
535
536 if (!handler->guid)
537 continue;
538 if (!guidcmp(handler->guid, protocol)) {
539 r = EFI_INVALID_PARAMETER;
540 goto out;
541 }
542 }
543 /* Install protocol in first empty slot. */
544 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
545 struct efi_handler *handler = &efiobj->protocols[i];
546
547 if (handler->guid)
548 continue;
549
550 handler->guid = protocol;
551 handler->protocol_interface = protocol_interface;
552 r = EFI_SUCCESS;
553 goto out;
554 }
555 r = EFI_OUT_OF_RESOURCES;
556 goto out;
557 }
558 r = EFI_INVALID_PARAMETER;
559out:
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +0200560 return r;
561}
562
563static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
564 efi_guid_t *protocol, int protocol_interface_type,
565 void *protocol_interface)
566{
567 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
568 protocol_interface);
569
570 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
571 protocol_interface_type,
572 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100573}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200574
Alexander Grafc15d9212016-03-04 01:09:59 +0100575static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
576 efi_guid_t *protocol, void *old_interface,
577 void *new_interface)
578{
579 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
580 new_interface);
581 return EFI_EXIT(EFI_ACCESS_DENIED);
582}
583
584static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
585 efi_guid_t *protocol, void *protocol_interface)
586{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200587 struct list_head *lhandle;
588 int i;
589 efi_status_t r = EFI_NOT_FOUND;
590
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200591 if (!handle || !protocol) {
592 r = EFI_INVALID_PARAMETER;
593 goto out;
594 }
595
596 list_for_each(lhandle, &efi_obj_list) {
597 struct efi_object *efiobj;
598 efiobj = list_entry(lhandle, struct efi_object, link);
599
600 if (efiobj->handle != handle)
601 continue;
602
603 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
604 struct efi_handler *handler = &efiobj->protocols[i];
605 const efi_guid_t *hprotocol = handler->guid;
606
607 if (!hprotocol)
608 continue;
609 if (!guidcmp(hprotocol, protocol)) {
610 if (handler->protocol_interface) {
611 r = EFI_ACCESS_DENIED;
612 } else {
613 handler->guid = 0;
614 r = EFI_SUCCESS;
615 }
616 goto out;
617 }
618 }
619 }
620
621out:
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +0200622 return r;
623}
624
625static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
626 efi_guid_t *protocol, void *protocol_interface)
627{
628 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
629
630 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
631 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100632}
633
634static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200635 struct efi_event *event,
Alexander Grafc15d9212016-03-04 01:09:59 +0100636 void **registration)
637{
638 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
639 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
640}
641
642static int efi_search(enum efi_locate_search_type search_type,
643 efi_guid_t *protocol, void *search_key,
644 struct efi_object *efiobj)
645{
646 int i;
647
648 switch (search_type) {
649 case all_handles:
650 return 0;
651 case by_register_notify:
652 return -1;
653 case by_protocol:
654 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
655 const efi_guid_t *guid = efiobj->protocols[i].guid;
656 if (guid && !guidcmp(guid, protocol))
657 return 0;
658 }
659 return -1;
660 }
661
662 return -1;
663}
664
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +0200665static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +0100666 enum efi_locate_search_type search_type,
667 efi_guid_t *protocol, void *search_key,
668 unsigned long *buffer_size, efi_handle_t *buffer)
669{
670 struct list_head *lhandle;
671 unsigned long size = 0;
672
Alexander Grafc15d9212016-03-04 01:09:59 +0100673 /* Count how much space we need */
674 list_for_each(lhandle, &efi_obj_list) {
675 struct efi_object *efiobj;
676 efiobj = list_entry(lhandle, struct efi_object, link);
677 if (!efi_search(search_type, protocol, search_key, efiobj)) {
678 size += sizeof(void*);
679 }
680 }
681
682 if (*buffer_size < size) {
683 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200684 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100685 }
686
Rob Clarkcdee3372017-08-06 14:10:07 -0400687 *buffer_size = size;
688 if (size == 0)
689 return EFI_NOT_FOUND;
690
Alexander Grafc15d9212016-03-04 01:09:59 +0100691 /* Then fill the array */
692 list_for_each(lhandle, &efi_obj_list) {
693 struct efi_object *efiobj;
694 efiobj = list_entry(lhandle, struct efi_object, link);
695 if (!efi_search(search_type, protocol, search_key, efiobj)) {
696 *(buffer++) = efiobj->handle;
697 }
698 }
699
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200700 return EFI_SUCCESS;
701}
702
703static efi_status_t EFIAPI efi_locate_handle_ext(
704 enum efi_locate_search_type search_type,
705 efi_guid_t *protocol, void *search_key,
706 unsigned long *buffer_size, efi_handle_t *buffer)
707{
708 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
709 buffer_size, buffer);
710
711 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
712 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +0100713}
714
715static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
716 struct efi_device_path **device_path,
717 efi_handle_t *device)
718{
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400719 struct efi_object *efiobj;
720
721 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
722
723 efiobj = efi_dp_find_obj(*device_path, device_path);
724 if (!efiobj)
725 return EFI_EXIT(EFI_NOT_FOUND);
726
727 *device = efiobj->handle;
728
729 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100730}
731
Alexander Graffe3366f2017-07-26 13:41:04 +0200732/* Collapses configuration table entries, removing index i */
733static void efi_remove_configuration_table(int i)
734{
735 struct efi_configuration_table *this = &efi_conf_table[i];
736 struct efi_configuration_table *next = &efi_conf_table[i+1];
737 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
738
739 memmove(this, next, (ulong)end - (ulong)next);
740 systab.nr_tables--;
741}
742
Alexander Grafc5c11632016-08-19 01:23:24 +0200743efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +0100744{
745 int i;
746
Alexander Grafc15d9212016-03-04 01:09:59 +0100747 /* Check for guid override */
748 for (i = 0; i < systab.nr_tables; i++) {
749 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +0200750 if (table)
751 efi_conf_table[i].table = table;
752 else
753 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +0200754 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100755 }
756 }
757
Alexander Graffe3366f2017-07-26 13:41:04 +0200758 if (!table)
759 return EFI_NOT_FOUND;
760
Alexander Grafc15d9212016-03-04 01:09:59 +0100761 /* No override, check for overflow */
762 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +0200763 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100764
765 /* Add a new entry */
766 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
767 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +0200768 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +0100769
Alexander Grafc5c11632016-08-19 01:23:24 +0200770 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100771}
772
Alexander Grafc5c11632016-08-19 01:23:24 +0200773static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
774 void *table)
775{
776 EFI_ENTRY("%p, %p", guid, table);
777 return EFI_EXIT(efi_install_configuration_table(guid, table));
778}
779
Rob Clarkf8db9222017-09-13 18:05:33 -0400780/* Initialize a loaded_image_info + loaded_image_info object with correct
781 * protocols, boot-device, etc.
782 */
783void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
784 struct efi_device_path *device_path,
785 struct efi_device_path *file_path)
786{
787 obj->handle = info;
788
789 /*
790 * When asking for the device path interface, return
791 * bootefi_device_path
792 */
793 obj->protocols[0].guid = &efi_guid_device_path;
794 obj->protocols[0].protocol_interface = device_path;
795
796 /*
797 * When asking for the loaded_image interface, just
798 * return handle which points to loaded_image_info
799 */
800 obj->protocols[1].guid = &efi_guid_loaded_image;
801 obj->protocols[1].protocol_interface = info;
802
803 obj->protocols[2].guid = &efi_guid_console_control;
804 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
805
806 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
807 obj->protocols[3].protocol_interface =
808 (void *)&efi_device_path_to_text;
809
810 info->file_path = file_path;
811 info->device_handle = efi_dp_find_obj(device_path, NULL);
812
813 list_add_tail(&obj->link, &efi_obj_list);
814}
815
Rob Clark857a1222017-09-13 18:05:35 -0400816static efi_status_t load_image_from_path(struct efi_device_path *file_path,
817 void **buffer)
818{
819 struct efi_file_info *info = NULL;
820 struct efi_file_handle *f;
821 static efi_status_t ret;
822 uint64_t bs;
823
824 f = efi_file_from_path(file_path);
825 if (!f)
826 return EFI_DEVICE_ERROR;
827
828 bs = 0;
829 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
830 &bs, info));
831 if (ret == EFI_BUFFER_TOO_SMALL) {
832 info = malloc(bs);
833 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
834 &bs, info));
835 }
836 if (ret != EFI_SUCCESS)
837 goto error;
838
839 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
840 if (ret)
841 goto error;
842
843 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
844
845error:
846 free(info);
847 EFI_CALL(f->close(f));
848
849 if (ret != EFI_SUCCESS) {
850 efi_free_pool(*buffer);
851 *buffer = NULL;
852 }
853
854 return ret;
855}
856
Alexander Grafc15d9212016-03-04 01:09:59 +0100857static efi_status_t EFIAPI efi_load_image(bool boot_policy,
858 efi_handle_t parent_image,
859 struct efi_device_path *file_path,
860 void *source_buffer,
861 unsigned long source_size,
862 efi_handle_t *image_handle)
863{
Alexander Grafc15d9212016-03-04 01:09:59 +0100864 struct efi_loaded_image *info;
865 struct efi_object *obj;
866
867 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
868 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -0400869
870 info = calloc(1, sizeof(*info));
871 obj = calloc(1, sizeof(*obj));
872
873 if (!source_buffer) {
874 struct efi_device_path *dp, *fp;
875 efi_status_t ret;
876
877 ret = load_image_from_path(file_path, &source_buffer);
878 if (ret != EFI_SUCCESS) {
879 free(info);
880 free(obj);
881 return EFI_EXIT(ret);
882 }
883
884 /*
885 * split file_path which contains both the device and
886 * file parts:
887 */
888 efi_dp_split_file_path(file_path, &dp, &fp);
889
890 efi_setup_loaded_image(info, obj, dp, fp);
891 } else {
892 /* In this case, file_path is the "device" path, ie.
893 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
894 */
895 efi_setup_loaded_image(info, obj, file_path, NULL);
896 }
897
Alexander Grafc15d9212016-03-04 01:09:59 +0100898 info->reserved = efi_load_pe(source_buffer, info);
899 if (!info->reserved) {
900 free(info);
901 free(obj);
902 return EFI_EXIT(EFI_UNSUPPORTED);
903 }
904
905 *image_handle = info;
Alexander Grafc15d9212016-03-04 01:09:59 +0100906
907 return EFI_EXIT(EFI_SUCCESS);
908}
909
910static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
911 unsigned long *exit_data_size,
912 s16 **exit_data)
913{
914 ulong (*entry)(void *image_handle, struct efi_system_table *st);
915 struct efi_loaded_image *info = image_handle;
916
917 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
918 entry = info->reserved;
919
920 efi_is_direct_boot = false;
921
922 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +0200923 if (setjmp(&info->exit_jmp)) {
924 /* We returned from the child image */
925 return EFI_EXIT(info->exit_status);
926 }
927
Rob Clarke7896c32017-07-27 08:04:19 -0400928 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -0400929 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +0100930 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -0400931 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -0400932 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +0100933
934 /* Should usually never get here */
935 return EFI_EXIT(EFI_SUCCESS);
936}
937
Alexander Graf988c0662016-05-20 23:28:23 +0200938static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
939 efi_status_t exit_status, unsigned long exit_data_size,
940 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +0100941{
Alexander Graf988c0662016-05-20 23:28:23 +0200942 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
943
Alexander Grafc15d9212016-03-04 01:09:59 +0100944 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
945 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +0200946
Alexander Graf87e787a2017-09-03 14:14:17 +0200947 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +0200948 __efi_exit_check();
949
Alexander Graf87e787a2017-09-03 14:14:17 +0200950 /*
951 * But longjmp out with the U-Boot gd, not the application's, as
952 * the other end is a setjmp call inside EFI context.
953 */
954 efi_restore_gd();
955
Alexander Graf988c0662016-05-20 23:28:23 +0200956 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +0200957 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +0200958
959 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +0100960}
961
962static struct efi_object *efi_search_obj(void *handle)
963{
964 struct list_head *lhandle;
965
966 list_for_each(lhandle, &efi_obj_list) {
967 struct efi_object *efiobj;
968 efiobj = list_entry(lhandle, struct efi_object, link);
969 if (efiobj->handle == handle)
970 return efiobj;
971 }
972
973 return NULL;
974}
975
976static efi_status_t EFIAPI efi_unload_image(void *image_handle)
977{
978 struct efi_object *efiobj;
979
980 EFI_ENTRY("%p", image_handle);
981 efiobj = efi_search_obj(image_handle);
982 if (efiobj)
983 list_del(&efiobj->link);
984
985 return EFI_EXIT(EFI_SUCCESS);
986}
987
988static void efi_exit_caches(void)
989{
990#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
991 /*
992 * Grub on 32bit ARM needs to have caches disabled before jumping into
993 * a zImage, but does not know of all cache layers. Give it a hand.
994 */
995 if (efi_is_direct_boot)
996 cleanup_before_linux();
997#endif
998}
999
1000static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1001 unsigned long map_key)
1002{
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001003 int i;
1004
Alexander Grafc15d9212016-03-04 01:09:59 +01001005 EFI_ENTRY("%p, %ld", image_handle, map_key);
1006
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001007 /* Notify that ExitBootServices is invoked. */
1008 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1009 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1010 continue;
1011 efi_signal_event(&efi_events[i]);
1012 }
1013 /* Make sure that notification functions are not called anymore */
1014 efi_tpl = TPL_HIGH_LEVEL;
1015
Alexander Graf2ebeb442016-11-17 01:02:57 +01001016 board_quiesce_devices();
1017
Alexander Grafc15d9212016-03-04 01:09:59 +01001018 /* Fix up caches for EFI payloads if necessary */
1019 efi_exit_caches();
1020
1021 /* This stops all lingering devices */
1022 bootm_disable_interrupts();
1023
1024 /* Give the payload some time to boot */
1025 WATCHDOG_RESET();
1026
1027 return EFI_EXIT(EFI_SUCCESS);
1028}
1029
1030static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1031{
1032 static uint64_t mono = 0;
1033 EFI_ENTRY("%p", count);
1034 *count = mono++;
1035 return EFI_EXIT(EFI_SUCCESS);
1036}
1037
1038static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1039{
1040 EFI_ENTRY("%ld", microseconds);
1041 udelay(microseconds);
1042 return EFI_EXIT(EFI_SUCCESS);
1043}
1044
1045static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1046 uint64_t watchdog_code,
1047 unsigned long data_size,
1048 uint16_t *watchdog_data)
1049{
1050 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1051 data_size, watchdog_data);
Rob Clarkd417d2b2017-07-25 20:17:59 -04001052 return efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +01001053}
1054
1055static efi_status_t EFIAPI efi_connect_controller(
1056 efi_handle_t controller_handle,
1057 efi_handle_t *driver_image_handle,
1058 struct efi_device_path *remain_device_path,
1059 bool recursive)
1060{
1061 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1062 remain_device_path, recursive);
1063 return EFI_EXIT(EFI_NOT_FOUND);
1064}
1065
1066static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1067 void *driver_image_handle,
1068 void *child_handle)
1069{
1070 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1071 child_handle);
1072 return EFI_EXIT(EFI_INVALID_PARAMETER);
1073}
1074
1075static efi_status_t EFIAPI efi_close_protocol(void *handle,
1076 efi_guid_t *protocol,
1077 void *agent_handle,
1078 void *controller_handle)
1079{
1080 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
1081 controller_handle);
1082 return EFI_EXIT(EFI_NOT_FOUND);
1083}
1084
1085static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1086 efi_guid_t *protocol,
1087 struct efi_open_protocol_info_entry **entry_buffer,
1088 unsigned long *entry_count)
1089{
1090 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
1091 entry_count);
1092 return EFI_EXIT(EFI_NOT_FOUND);
1093}
1094
1095static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1096 efi_guid_t ***protocol_buffer,
1097 unsigned long *protocol_buffer_count)
1098{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001099 unsigned long buffer_size;
1100 struct efi_object *efiobj;
1101 unsigned long i, j;
1102 struct list_head *lhandle;
1103 efi_status_t r;
1104
Alexander Grafc15d9212016-03-04 01:09:59 +01001105 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1106 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001107
1108 if (!handle || !protocol_buffer || !protocol_buffer_count)
1109 return EFI_EXIT(EFI_INVALID_PARAMETER);
1110
1111 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04001112 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001113 list_for_each(lhandle, &efi_obj_list) {
1114 efiobj = list_entry(lhandle, struct efi_object, link);
1115
1116 if (efiobj->handle != handle)
1117 continue;
1118
1119 /* Count protocols */
1120 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1121 if (efiobj->protocols[i].guid)
1122 ++*protocol_buffer_count;
1123 }
1124 /* Copy guids */
1125 if (*protocol_buffer_count) {
1126 buffer_size = sizeof(efi_guid_t *) *
1127 *protocol_buffer_count;
1128 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1129 buffer_size,
1130 (void **)protocol_buffer);
1131 if (r != EFI_SUCCESS)
1132 return EFI_EXIT(r);
1133 j = 0;
1134 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1135 if (efiobj->protocols[i].guid) {
1136 (*protocol_buffer)[j] = (void *)
1137 efiobj->protocols[i].guid;
1138 ++j;
1139 }
1140 }
1141 }
1142 break;
1143 }
1144
1145 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001146}
1147
1148static efi_status_t EFIAPI efi_locate_handle_buffer(
1149 enum efi_locate_search_type search_type,
1150 efi_guid_t *protocol, void *search_key,
1151 unsigned long *no_handles, efi_handle_t **buffer)
1152{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001153 efi_status_t r;
1154 unsigned long buffer_size = 0;
1155
Alexander Grafc15d9212016-03-04 01:09:59 +01001156 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1157 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001158
1159 if (!no_handles || !buffer) {
1160 r = EFI_INVALID_PARAMETER;
1161 goto out;
1162 }
1163 *no_handles = 0;
1164 *buffer = NULL;
1165 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1166 *buffer);
1167 if (r != EFI_BUFFER_TOO_SMALL)
1168 goto out;
1169 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1170 (void **)buffer);
1171 if (r != EFI_SUCCESS)
1172 goto out;
1173 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1174 *buffer);
1175 if (r == EFI_SUCCESS)
1176 *no_handles = buffer_size / sizeof(void *);
1177out:
1178 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001179}
1180
Alexander Grafc15d9212016-03-04 01:09:59 +01001181static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1182 void *registration,
1183 void **protocol_interface)
1184{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001185 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001186 int i;
1187
1188 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001189
1190 if (!protocol || !protocol_interface)
1191 return EFI_EXIT(EFI_INVALID_PARAMETER);
1192
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001193 EFI_PRINT_GUID("protocol", protocol);
1194
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001195 list_for_each(lhandle, &efi_obj_list) {
1196 struct efi_object *efiobj;
1197
1198 efiobj = list_entry(lhandle, struct efi_object, link);
1199 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1200 struct efi_handler *handler = &efiobj->protocols[i];
1201
1202 if (!handler->guid)
1203 continue;
1204 if (!guidcmp(handler->guid, protocol)) {
1205 *protocol_interface =
1206 handler->protocol_interface;
1207 return EFI_EXIT(EFI_SUCCESS);
1208 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001209 }
1210 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001211 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001212
1213 return EFI_EXIT(EFI_NOT_FOUND);
1214}
1215
1216static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1217 void **handle, ...)
1218{
1219 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001220
1221 va_list argptr;
1222 efi_guid_t *protocol;
1223 void *protocol_interface;
1224 efi_status_t r = EFI_SUCCESS;
1225 int i = 0;
1226
1227 if (!handle)
1228 return EFI_EXIT(EFI_INVALID_PARAMETER);
1229
1230 va_start(argptr, handle);
1231 for (;;) {
1232 protocol = va_arg(argptr, efi_guid_t*);
1233 if (!protocol)
1234 break;
1235 protocol_interface = va_arg(argptr, void*);
1236 r = efi_install_protocol_interface(handle, protocol,
1237 EFI_NATIVE_INTERFACE,
1238 protocol_interface);
1239 if (r != EFI_SUCCESS)
1240 break;
1241 i++;
1242 }
1243 va_end(argptr);
1244 if (r == EFI_SUCCESS)
1245 return EFI_EXIT(r);
1246
1247 /* If an error occured undo all changes. */
1248 va_start(argptr, handle);
1249 for (; i; --i) {
1250 protocol = va_arg(argptr, efi_guid_t*);
1251 protocol_interface = va_arg(argptr, void*);
1252 efi_uninstall_protocol_interface(handle, protocol,
1253 protocol_interface);
1254 }
1255 va_end(argptr);
1256
1257 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001258}
1259
1260static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1261 void *handle, ...)
1262{
1263 EFI_ENTRY("%p", handle);
1264 return EFI_EXIT(EFI_INVALID_PARAMETER);
1265}
1266
1267static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1268 unsigned long data_size,
1269 uint32_t *crc32_p)
1270{
1271 EFI_ENTRY("%p, %ld", data, data_size);
1272 *crc32_p = crc32(0, data, data_size);
1273 return EFI_EXIT(EFI_SUCCESS);
1274}
1275
1276static void EFIAPI efi_copy_mem(void *destination, void *source,
1277 unsigned long length)
1278{
1279 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1280 memcpy(destination, source, length);
1281}
1282
1283static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1284{
1285 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1286 memset(buffer, value, size);
1287}
1288
1289static efi_status_t EFIAPI efi_open_protocol(
1290 void *handle, efi_guid_t *protocol,
1291 void **protocol_interface, void *agent_handle,
1292 void *controller_handle, uint32_t attributes)
1293{
1294 struct list_head *lhandle;
1295 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001296 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001297
1298 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1299 protocol_interface, agent_handle, controller_handle,
1300 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001301
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001302 if (!handle || !protocol ||
1303 (!protocol_interface && attributes !=
1304 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001305 goto out;
1306 }
1307
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001308 EFI_PRINT_GUID("protocol", protocol);
1309
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001310 switch (attributes) {
1311 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1312 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1313 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1314 break;
1315 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1316 if (controller_handle == handle)
1317 goto out;
1318 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1319 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1320 if (controller_handle == NULL)
1321 goto out;
1322 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1323 if (agent_handle == NULL)
1324 goto out;
1325 break;
1326 default:
1327 goto out;
1328 }
1329
Alexander Grafc15d9212016-03-04 01:09:59 +01001330 list_for_each(lhandle, &efi_obj_list) {
1331 struct efi_object *efiobj;
1332 efiobj = list_entry(lhandle, struct efi_object, link);
1333
1334 if (efiobj->handle != handle)
1335 continue;
1336
1337 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1338 struct efi_handler *handler = &efiobj->protocols[i];
1339 const efi_guid_t *hprotocol = handler->guid;
1340 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001341 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001342 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001343 if (attributes !=
1344 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1345 *protocol_interface =
1346 handler->protocol_interface;
1347 }
1348 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001349 goto out;
1350 }
1351 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001352 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001353 }
1354
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001355unsupported:
1356 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01001357out:
1358 return EFI_EXIT(r);
1359}
1360
1361static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1362 efi_guid_t *protocol,
1363 void **protocol_interface)
1364{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02001365 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1366 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01001367}
1368
1369static const struct efi_boot_services efi_boot_services = {
1370 .hdr = {
1371 .headersize = sizeof(struct efi_table_hdr),
1372 },
1373 .raise_tpl = efi_raise_tpl,
1374 .restore_tpl = efi_restore_tpl,
1375 .allocate_pages = efi_allocate_pages_ext,
1376 .free_pages = efi_free_pages_ext,
1377 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02001378 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02001379 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02001380 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02001381 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001382 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001383 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001384 .close_event = efi_close_event,
1385 .check_event = efi_check_event,
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +02001386 .install_protocol_interface = efi_install_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001387 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +02001388 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001389 .handle_protocol = efi_handle_protocol,
1390 .reserved = NULL,
1391 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001392 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001393 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02001394 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001395 .load_image = efi_load_image,
1396 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02001397 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01001398 .unload_image = efi_unload_image,
1399 .exit_boot_services = efi_exit_boot_services,
1400 .get_next_monotonic_count = efi_get_next_monotonic_count,
1401 .stall = efi_stall,
1402 .set_watchdog_timer = efi_set_watchdog_timer,
1403 .connect_controller = efi_connect_controller,
1404 .disconnect_controller = efi_disconnect_controller,
1405 .open_protocol = efi_open_protocol,
1406 .close_protocol = efi_close_protocol,
1407 .open_protocol_information = efi_open_protocol_information,
1408 .protocols_per_handle = efi_protocols_per_handle,
1409 .locate_handle_buffer = efi_locate_handle_buffer,
1410 .locate_protocol = efi_locate_protocol,
1411 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1412 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1413 .calculate_crc32 = efi_calculate_crc32,
1414 .copy_mem = efi_copy_mem,
1415 .set_mem = efi_set_mem,
1416};
1417
1418
Alexander Graf393dd912016-10-14 13:45:30 +02001419static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01001420 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1421
Alexander Graf393dd912016-10-14 13:45:30 +02001422struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01001423 .hdr = {
1424 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1425 .revision = 0x20005, /* 2.5 */
1426 .headersize = sizeof(struct efi_table_hdr),
1427 },
1428 .fw_vendor = (long)firmware_vendor,
1429 .con_in = (void*)&efi_con_in,
1430 .con_out = (void*)&efi_con_out,
1431 .std_err = (void*)&efi_con_out,
1432 .runtime = (void*)&efi_runtime_services,
1433 .boottime = (void*)&efi_boot_services,
1434 .nr_tables = 0,
1435 .tables = (void*)efi_conf_table,
1436};