blob: 2c9379a8ae51c9ce62c723ec3fe343ae04f927b5 [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) {
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200166 EFI_CALL_VOID(event->notify_function(event,
167 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200168 }
169}
170
Alexander Grafc15d9212016-03-04 01:09:59 +0100171static efi_status_t efi_unsupported(const char *funcname)
172{
Alexander Graf62a67482016-06-02 11:38:27 +0200173 debug("EFI: App called into unimplemented function %s\n", funcname);
Alexander Grafc15d9212016-03-04 01:09:59 +0100174 return EFI_EXIT(EFI_UNSUPPORTED);
175}
176
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200177static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100178{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200179 EFI_ENTRY("0x%zx", new_tpl);
Alexander Grafc15d9212016-03-04 01:09:59 +0100180 return EFI_EXIT(0);
181}
182
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200183static void EFIAPI efi_restore_tpl(UINTN old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100184{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200185 EFI_ENTRY("0x%zx", old_tpl);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400186 efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100187}
188
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900189static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
190 unsigned long pages,
191 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100192{
193 efi_status_t r;
194
195 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
196 r = efi_allocate_pages(type, memory_type, pages, memory);
197 return EFI_EXIT(r);
198}
199
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900200static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
201 unsigned long pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100202{
203 efi_status_t r;
204
205 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
206 r = efi_free_pages(memory, pages);
207 return EFI_EXIT(r);
208}
209
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900210static efi_status_t EFIAPI efi_get_memory_map_ext(
211 unsigned long *memory_map_size,
212 struct efi_mem_desc *memory_map,
213 unsigned long *map_key,
214 unsigned long *descriptor_size,
215 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100216{
217 efi_status_t r;
218
219 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
220 map_key, descriptor_size, descriptor_version);
221 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
222 descriptor_size, descriptor_version);
223 return EFI_EXIT(r);
224}
225
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200226static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
227 unsigned long size,
228 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100229{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100230 efi_status_t r;
231
232 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200233 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100234 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100235}
236
Stefan Brüns67b67d92016-10-09 22:17:26 +0200237static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100238{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100239 efi_status_t r;
240
241 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200242 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100243 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100244}
245
246/*
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200247 * Our event capabilities are very limited. Only a small limited
248 * number of events is allowed to coexist.
Alexander Grafc15d9212016-03-04 01:09:59 +0100249 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200250static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100251
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200252efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200253 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200254 struct efi_event *event,
255 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200256 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100257{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200258 int i;
259
Jonathan Gray7758b212017-03-12 19:26:07 +1100260 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200261 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100262
263 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200264 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100265
266 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
267 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200268 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100269
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200270 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
271 if (efi_events[i].type)
272 continue;
273 efi_events[i].type = type;
274 efi_events[i].notify_tpl = notify_tpl;
275 efi_events[i].notify_function = notify_function;
276 efi_events[i].notify_context = notify_context;
277 /* Disable timers on bootup */
278 efi_events[i].trigger_next = -1ULL;
279 efi_events[i].signaled = 0;
280 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200281 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200282 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200283 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100284}
285
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200286static efi_status_t EFIAPI efi_create_event_ext(
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200287 uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200288 void (EFIAPI *notify_function) (
289 struct efi_event *event,
290 void *context),
291 void *notify_context, struct efi_event **event)
292{
293 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
294 notify_context);
295 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
296 notify_context, event));
297}
298
299
Alexander Grafc15d9212016-03-04 01:09:59 +0100300/*
301 * Our timers have to work without interrupts, so we check whenever keyboard
302 * input or disk accesses happen if enough time elapsed for it to fire.
303 */
304void efi_timer_check(void)
305{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200306 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100307 u64 now = timer_get_us();
308
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200309 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
310 if (!efi_events[i].type ||
311 !(efi_events[i].type & EVT_TIMER) ||
312 efi_events[i].trigger_type == EFI_TIMER_STOP ||
313 now < efi_events[i].trigger_next)
314 continue;
315 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
316 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200317 efi_events[i].trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200318 efi_events[i].signaled = 0;
319 }
320 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100321 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100322 WATCHDOG_RESET();
323}
324
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200325efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200326 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100327{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200328 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100329
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200330 /*
331 * The parameter defines a multiple of 100ns.
332 * We use multiples of 1000ns. So divide by 10.
333 */
334 trigger_time = efi_div10(trigger_time);
Alexander Grafc15d9212016-03-04 01:09:59 +0100335
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200336 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
337 if (event != &efi_events[i])
338 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100339
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200340 if (!(event->type & EVT_TIMER))
341 break;
342 switch (type) {
343 case EFI_TIMER_STOP:
344 event->trigger_next = -1ULL;
345 break;
346 case EFI_TIMER_PERIODIC:
347 case EFI_TIMER_RELATIVE:
348 event->trigger_next =
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200349 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200350 break;
351 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200352 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200353 }
354 event->trigger_type = type;
355 event->trigger_time = trigger_time;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200356 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100357 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200358 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100359}
360
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200361static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
362 enum efi_timer_delay type,
363 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200364{
365 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
366 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
367}
368
Alexander Grafc15d9212016-03-04 01:09:59 +0100369static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200370 struct efi_event **event,
371 unsigned long *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100372{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200373 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100374
375 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
376
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200377 /* Check parameters */
378 if (!num_events || !event)
379 return EFI_EXIT(EFI_INVALID_PARAMETER);
380 for (i = 0; i < num_events; ++i) {
381 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
382 if (event[i] == &efi_events[j])
383 goto known_event;
384 }
385 return EFI_EXIT(EFI_INVALID_PARAMETER);
386known_event:
387 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
388 return EFI_EXIT(EFI_INVALID_PARAMETER);
389 }
390
391 /* Wait for signal */
392 for (;;) {
393 for (i = 0; i < num_events; ++i) {
394 if (event[i]->signaled)
395 goto out;
396 }
397 /* Allow events to occur. */
398 efi_timer_check();
399 }
400
401out:
402 /*
403 * Reset the signal which is passed to the caller to allow periodic
404 * events to occur.
405 */
406 event[i]->signaled = 0;
407 if (index)
408 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100409
410 return EFI_EXIT(EFI_SUCCESS);
411}
412
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200413static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100414{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200415 int i;
416
Alexander Grafc15d9212016-03-04 01:09:59 +0100417 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200418 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
419 if (event != &efi_events[i])
420 continue;
421 efi_signal_event(event);
422 break;
423 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100424 return EFI_EXIT(EFI_SUCCESS);
425}
426
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200427static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100428{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200429 int i;
430
Alexander Grafc15d9212016-03-04 01:09:59 +0100431 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200432 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
433 if (event == &efi_events[i]) {
434 event->type = 0;
435 event->trigger_next = -1ULL;
436 event->signaled = 0;
437 return EFI_EXIT(EFI_SUCCESS);
438 }
439 }
440 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100441}
442
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200443static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100444{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200445 int i;
446
Alexander Grafc15d9212016-03-04 01:09:59 +0100447 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200448 efi_timer_check();
449 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
450 if (event != &efi_events[i])
451 continue;
452 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
453 break;
454 if (event->signaled)
455 return EFI_EXIT(EFI_SUCCESS);
456 return EFI_EXIT(EFI_NOT_READY);
457 }
458 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100459}
460
461static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
462 efi_guid_t *protocol, int protocol_interface_type,
463 void *protocol_interface)
464{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200465 struct list_head *lhandle;
466 int i;
467 efi_status_t r;
468
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200469 if (!handle || !protocol ||
470 protocol_interface_type != EFI_NATIVE_INTERFACE) {
471 r = EFI_INVALID_PARAMETER;
472 goto out;
473 }
474
475 /* Create new handle if requested. */
476 if (!*handle) {
477 r = EFI_OUT_OF_RESOURCES;
478 goto out;
479 }
480 /* Find object. */
481 list_for_each(lhandle, &efi_obj_list) {
482 struct efi_object *efiobj;
483 efiobj = list_entry(lhandle, struct efi_object, link);
484
485 if (efiobj->handle != *handle)
486 continue;
487 /* Check if protocol is already installed on the handle. */
488 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
489 struct efi_handler *handler = &efiobj->protocols[i];
490
491 if (!handler->guid)
492 continue;
493 if (!guidcmp(handler->guid, protocol)) {
494 r = EFI_INVALID_PARAMETER;
495 goto out;
496 }
497 }
498 /* Install protocol in first empty slot. */
499 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
500 struct efi_handler *handler = &efiobj->protocols[i];
501
502 if (handler->guid)
503 continue;
504
505 handler->guid = protocol;
506 handler->protocol_interface = protocol_interface;
507 r = EFI_SUCCESS;
508 goto out;
509 }
510 r = EFI_OUT_OF_RESOURCES;
511 goto out;
512 }
513 r = EFI_INVALID_PARAMETER;
514out:
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +0200515 return r;
516}
517
518static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
519 efi_guid_t *protocol, int protocol_interface_type,
520 void *protocol_interface)
521{
522 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
523 protocol_interface);
524
525 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
526 protocol_interface_type,
527 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100528}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200529
Alexander Grafc15d9212016-03-04 01:09:59 +0100530static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
531 efi_guid_t *protocol, void *old_interface,
532 void *new_interface)
533{
534 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
535 new_interface);
536 return EFI_EXIT(EFI_ACCESS_DENIED);
537}
538
539static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
540 efi_guid_t *protocol, void *protocol_interface)
541{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200542 struct list_head *lhandle;
543 int i;
544 efi_status_t r = EFI_NOT_FOUND;
545
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200546 if (!handle || !protocol) {
547 r = EFI_INVALID_PARAMETER;
548 goto out;
549 }
550
551 list_for_each(lhandle, &efi_obj_list) {
552 struct efi_object *efiobj;
553 efiobj = list_entry(lhandle, struct efi_object, link);
554
555 if (efiobj->handle != handle)
556 continue;
557
558 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
559 struct efi_handler *handler = &efiobj->protocols[i];
560 const efi_guid_t *hprotocol = handler->guid;
561
562 if (!hprotocol)
563 continue;
564 if (!guidcmp(hprotocol, protocol)) {
565 if (handler->protocol_interface) {
566 r = EFI_ACCESS_DENIED;
567 } else {
568 handler->guid = 0;
569 r = EFI_SUCCESS;
570 }
571 goto out;
572 }
573 }
574 }
575
576out:
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +0200577 return r;
578}
579
580static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
581 efi_guid_t *protocol, void *protocol_interface)
582{
583 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
584
585 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
586 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100587}
588
589static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200590 struct efi_event *event,
Alexander Grafc15d9212016-03-04 01:09:59 +0100591 void **registration)
592{
593 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
594 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
595}
596
597static int efi_search(enum efi_locate_search_type search_type,
598 efi_guid_t *protocol, void *search_key,
599 struct efi_object *efiobj)
600{
601 int i;
602
603 switch (search_type) {
604 case all_handles:
605 return 0;
606 case by_register_notify:
607 return -1;
608 case by_protocol:
609 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
610 const efi_guid_t *guid = efiobj->protocols[i].guid;
611 if (guid && !guidcmp(guid, protocol))
612 return 0;
613 }
614 return -1;
615 }
616
617 return -1;
618}
619
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +0200620static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +0100621 enum efi_locate_search_type search_type,
622 efi_guid_t *protocol, void *search_key,
623 unsigned long *buffer_size, efi_handle_t *buffer)
624{
625 struct list_head *lhandle;
626 unsigned long size = 0;
627
Alexander Grafc15d9212016-03-04 01:09:59 +0100628 /* Count how much space we need */
629 list_for_each(lhandle, &efi_obj_list) {
630 struct efi_object *efiobj;
631 efiobj = list_entry(lhandle, struct efi_object, link);
632 if (!efi_search(search_type, protocol, search_key, efiobj)) {
633 size += sizeof(void*);
634 }
635 }
636
637 if (*buffer_size < size) {
638 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200639 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100640 }
641
Rob Clarkcdee3372017-08-06 14:10:07 -0400642 *buffer_size = size;
643 if (size == 0)
644 return EFI_NOT_FOUND;
645
Alexander Grafc15d9212016-03-04 01:09:59 +0100646 /* Then fill the array */
647 list_for_each(lhandle, &efi_obj_list) {
648 struct efi_object *efiobj;
649 efiobj = list_entry(lhandle, struct efi_object, link);
650 if (!efi_search(search_type, protocol, search_key, efiobj)) {
651 *(buffer++) = efiobj->handle;
652 }
653 }
654
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200655 return EFI_SUCCESS;
656}
657
658static efi_status_t EFIAPI efi_locate_handle_ext(
659 enum efi_locate_search_type search_type,
660 efi_guid_t *protocol, void *search_key,
661 unsigned long *buffer_size, efi_handle_t *buffer)
662{
663 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
664 buffer_size, buffer);
665
666 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
667 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +0100668}
669
670static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
671 struct efi_device_path **device_path,
672 efi_handle_t *device)
673{
674 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
675 return EFI_EXIT(EFI_NOT_FOUND);
676}
677
Alexander Graffe3366f2017-07-26 13:41:04 +0200678/* Collapses configuration table entries, removing index i */
679static void efi_remove_configuration_table(int i)
680{
681 struct efi_configuration_table *this = &efi_conf_table[i];
682 struct efi_configuration_table *next = &efi_conf_table[i+1];
683 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
684
685 memmove(this, next, (ulong)end - (ulong)next);
686 systab.nr_tables--;
687}
688
Alexander Grafc5c11632016-08-19 01:23:24 +0200689efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +0100690{
691 int i;
692
Alexander Grafc15d9212016-03-04 01:09:59 +0100693 /* Check for guid override */
694 for (i = 0; i < systab.nr_tables; i++) {
695 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +0200696 if (table)
697 efi_conf_table[i].table = table;
698 else
699 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +0200700 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100701 }
702 }
703
Alexander Graffe3366f2017-07-26 13:41:04 +0200704 if (!table)
705 return EFI_NOT_FOUND;
706
Alexander Grafc15d9212016-03-04 01:09:59 +0100707 /* No override, check for overflow */
708 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +0200709 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100710
711 /* Add a new entry */
712 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
713 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +0200714 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +0100715
Alexander Grafc5c11632016-08-19 01:23:24 +0200716 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100717}
718
Alexander Grafc5c11632016-08-19 01:23:24 +0200719static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
720 void *table)
721{
722 EFI_ENTRY("%p, %p", guid, table);
723 return EFI_EXIT(efi_install_configuration_table(guid, table));
724}
725
Alexander Grafc15d9212016-03-04 01:09:59 +0100726static efi_status_t EFIAPI efi_load_image(bool boot_policy,
727 efi_handle_t parent_image,
728 struct efi_device_path *file_path,
729 void *source_buffer,
730 unsigned long source_size,
731 efi_handle_t *image_handle)
732{
733 static struct efi_object loaded_image_info_obj = {
734 .protocols = {
735 {
736 .guid = &efi_guid_loaded_image,
Alexander Grafc15d9212016-03-04 01:09:59 +0100737 },
738 },
739 };
740 struct efi_loaded_image *info;
741 struct efi_object *obj;
742
743 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
744 file_path, source_buffer, source_size, image_handle);
745 info = malloc(sizeof(*info));
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +0200746 loaded_image_info_obj.protocols[0].protocol_interface = info;
Alexander Grafc15d9212016-03-04 01:09:59 +0100747 obj = malloc(sizeof(loaded_image_info_obj));
748 memset(info, 0, sizeof(*info));
749 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
750 obj->handle = info;
751 info->file_path = file_path;
752 info->reserved = efi_load_pe(source_buffer, info);
753 if (!info->reserved) {
754 free(info);
755 free(obj);
756 return EFI_EXIT(EFI_UNSUPPORTED);
757 }
758
759 *image_handle = info;
760 list_add_tail(&obj->link, &efi_obj_list);
761
762 return EFI_EXIT(EFI_SUCCESS);
763}
764
765static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
766 unsigned long *exit_data_size,
767 s16 **exit_data)
768{
769 ulong (*entry)(void *image_handle, struct efi_system_table *st);
770 struct efi_loaded_image *info = image_handle;
771
772 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
773 entry = info->reserved;
774
775 efi_is_direct_boot = false;
776
777 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +0200778 if (setjmp(&info->exit_jmp)) {
779 /* We returned from the child image */
780 return EFI_EXIT(info->exit_status);
781 }
782
Rob Clarke7896c32017-07-27 08:04:19 -0400783 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -0400784 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +0100785 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -0400786 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -0400787 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +0100788
789 /* Should usually never get here */
790 return EFI_EXIT(EFI_SUCCESS);
791}
792
Alexander Graf988c0662016-05-20 23:28:23 +0200793static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
794 efi_status_t exit_status, unsigned long exit_data_size,
795 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +0100796{
Alexander Graf988c0662016-05-20 23:28:23 +0200797 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
798
Alexander Grafc15d9212016-03-04 01:09:59 +0100799 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
800 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +0200801
Alexander Graf87e787a2017-09-03 14:14:17 +0200802 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +0200803 __efi_exit_check();
804
Alexander Graf87e787a2017-09-03 14:14:17 +0200805 /*
806 * But longjmp out with the U-Boot gd, not the application's, as
807 * the other end is a setjmp call inside EFI context.
808 */
809 efi_restore_gd();
810
Alexander Graf988c0662016-05-20 23:28:23 +0200811 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +0200812 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +0200813
814 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +0100815}
816
817static struct efi_object *efi_search_obj(void *handle)
818{
819 struct list_head *lhandle;
820
821 list_for_each(lhandle, &efi_obj_list) {
822 struct efi_object *efiobj;
823 efiobj = list_entry(lhandle, struct efi_object, link);
824 if (efiobj->handle == handle)
825 return efiobj;
826 }
827
828 return NULL;
829}
830
831static efi_status_t EFIAPI efi_unload_image(void *image_handle)
832{
833 struct efi_object *efiobj;
834
835 EFI_ENTRY("%p", image_handle);
836 efiobj = efi_search_obj(image_handle);
837 if (efiobj)
838 list_del(&efiobj->link);
839
840 return EFI_EXIT(EFI_SUCCESS);
841}
842
843static void efi_exit_caches(void)
844{
845#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
846 /*
847 * Grub on 32bit ARM needs to have caches disabled before jumping into
848 * a zImage, but does not know of all cache layers. Give it a hand.
849 */
850 if (efi_is_direct_boot)
851 cleanup_before_linux();
852#endif
853}
854
855static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
856 unsigned long map_key)
857{
858 EFI_ENTRY("%p, %ld", image_handle, map_key);
859
Alexander Graf2ebeb442016-11-17 01:02:57 +0100860 board_quiesce_devices();
861
Alexander Grafc15d9212016-03-04 01:09:59 +0100862 /* Fix up caches for EFI payloads if necessary */
863 efi_exit_caches();
864
865 /* This stops all lingering devices */
866 bootm_disable_interrupts();
867
868 /* Give the payload some time to boot */
869 WATCHDOG_RESET();
870
871 return EFI_EXIT(EFI_SUCCESS);
872}
873
874static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
875{
876 static uint64_t mono = 0;
877 EFI_ENTRY("%p", count);
878 *count = mono++;
879 return EFI_EXIT(EFI_SUCCESS);
880}
881
882static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
883{
884 EFI_ENTRY("%ld", microseconds);
885 udelay(microseconds);
886 return EFI_EXIT(EFI_SUCCESS);
887}
888
889static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
890 uint64_t watchdog_code,
891 unsigned long data_size,
892 uint16_t *watchdog_data)
893{
894 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
895 data_size, watchdog_data);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400896 return efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100897}
898
899static efi_status_t EFIAPI efi_connect_controller(
900 efi_handle_t controller_handle,
901 efi_handle_t *driver_image_handle,
902 struct efi_device_path *remain_device_path,
903 bool recursive)
904{
905 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
906 remain_device_path, recursive);
907 return EFI_EXIT(EFI_NOT_FOUND);
908}
909
910static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
911 void *driver_image_handle,
912 void *child_handle)
913{
914 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
915 child_handle);
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
917}
918
919static efi_status_t EFIAPI efi_close_protocol(void *handle,
920 efi_guid_t *protocol,
921 void *agent_handle,
922 void *controller_handle)
923{
924 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
925 controller_handle);
926 return EFI_EXIT(EFI_NOT_FOUND);
927}
928
929static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
930 efi_guid_t *protocol,
931 struct efi_open_protocol_info_entry **entry_buffer,
932 unsigned long *entry_count)
933{
934 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
935 entry_count);
936 return EFI_EXIT(EFI_NOT_FOUND);
937}
938
939static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
940 efi_guid_t ***protocol_buffer,
941 unsigned long *protocol_buffer_count)
942{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200943 unsigned long buffer_size;
944 struct efi_object *efiobj;
945 unsigned long i, j;
946 struct list_head *lhandle;
947 efi_status_t r;
948
Alexander Grafc15d9212016-03-04 01:09:59 +0100949 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
950 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200951
952 if (!handle || !protocol_buffer || !protocol_buffer_count)
953 return EFI_EXIT(EFI_INVALID_PARAMETER);
954
955 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -0400956 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200957 list_for_each(lhandle, &efi_obj_list) {
958 efiobj = list_entry(lhandle, struct efi_object, link);
959
960 if (efiobj->handle != handle)
961 continue;
962
963 /* Count protocols */
964 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
965 if (efiobj->protocols[i].guid)
966 ++*protocol_buffer_count;
967 }
968 /* Copy guids */
969 if (*protocol_buffer_count) {
970 buffer_size = sizeof(efi_guid_t *) *
971 *protocol_buffer_count;
972 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
973 buffer_size,
974 (void **)protocol_buffer);
975 if (r != EFI_SUCCESS)
976 return EFI_EXIT(r);
977 j = 0;
978 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
979 if (efiobj->protocols[i].guid) {
980 (*protocol_buffer)[j] = (void *)
981 efiobj->protocols[i].guid;
982 ++j;
983 }
984 }
985 }
986 break;
987 }
988
989 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100990}
991
992static efi_status_t EFIAPI efi_locate_handle_buffer(
993 enum efi_locate_search_type search_type,
994 efi_guid_t *protocol, void *search_key,
995 unsigned long *no_handles, efi_handle_t **buffer)
996{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +0200997 efi_status_t r;
998 unsigned long buffer_size = 0;
999
Alexander Grafc15d9212016-03-04 01:09:59 +01001000 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1001 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001002
1003 if (!no_handles || !buffer) {
1004 r = EFI_INVALID_PARAMETER;
1005 goto out;
1006 }
1007 *no_handles = 0;
1008 *buffer = NULL;
1009 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1010 *buffer);
1011 if (r != EFI_BUFFER_TOO_SMALL)
1012 goto out;
1013 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1014 (void **)buffer);
1015 if (r != EFI_SUCCESS)
1016 goto out;
1017 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1018 *buffer);
1019 if (r == EFI_SUCCESS)
1020 *no_handles = buffer_size / sizeof(void *);
1021out:
1022 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001023}
1024
Alexander Grafc15d9212016-03-04 01:09:59 +01001025static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1026 void *registration,
1027 void **protocol_interface)
1028{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001029 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001030 int i;
1031
1032 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001033
1034 if (!protocol || !protocol_interface)
1035 return EFI_EXIT(EFI_INVALID_PARAMETER);
1036
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001037 EFI_PRINT_GUID("protocol", protocol);
1038
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001039 list_for_each(lhandle, &efi_obj_list) {
1040 struct efi_object *efiobj;
1041
1042 efiobj = list_entry(lhandle, struct efi_object, link);
1043 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1044 struct efi_handler *handler = &efiobj->protocols[i];
1045
1046 if (!handler->guid)
1047 continue;
1048 if (!guidcmp(handler->guid, protocol)) {
1049 *protocol_interface =
1050 handler->protocol_interface;
1051 return EFI_EXIT(EFI_SUCCESS);
1052 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001053 }
1054 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001055 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001056
1057 return EFI_EXIT(EFI_NOT_FOUND);
1058}
1059
1060static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1061 void **handle, ...)
1062{
1063 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001064
1065 va_list argptr;
1066 efi_guid_t *protocol;
1067 void *protocol_interface;
1068 efi_status_t r = EFI_SUCCESS;
1069 int i = 0;
1070
1071 if (!handle)
1072 return EFI_EXIT(EFI_INVALID_PARAMETER);
1073
1074 va_start(argptr, handle);
1075 for (;;) {
1076 protocol = va_arg(argptr, efi_guid_t*);
1077 if (!protocol)
1078 break;
1079 protocol_interface = va_arg(argptr, void*);
1080 r = efi_install_protocol_interface(handle, protocol,
1081 EFI_NATIVE_INTERFACE,
1082 protocol_interface);
1083 if (r != EFI_SUCCESS)
1084 break;
1085 i++;
1086 }
1087 va_end(argptr);
1088 if (r == EFI_SUCCESS)
1089 return EFI_EXIT(r);
1090
1091 /* If an error occured undo all changes. */
1092 va_start(argptr, handle);
1093 for (; i; --i) {
1094 protocol = va_arg(argptr, efi_guid_t*);
1095 protocol_interface = va_arg(argptr, void*);
1096 efi_uninstall_protocol_interface(handle, protocol,
1097 protocol_interface);
1098 }
1099 va_end(argptr);
1100
1101 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001102}
1103
1104static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1105 void *handle, ...)
1106{
1107 EFI_ENTRY("%p", handle);
1108 return EFI_EXIT(EFI_INVALID_PARAMETER);
1109}
1110
1111static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1112 unsigned long data_size,
1113 uint32_t *crc32_p)
1114{
1115 EFI_ENTRY("%p, %ld", data, data_size);
1116 *crc32_p = crc32(0, data, data_size);
1117 return EFI_EXIT(EFI_SUCCESS);
1118}
1119
1120static void EFIAPI efi_copy_mem(void *destination, void *source,
1121 unsigned long length)
1122{
1123 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1124 memcpy(destination, source, length);
1125}
1126
1127static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1128{
1129 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1130 memset(buffer, value, size);
1131}
1132
1133static efi_status_t EFIAPI efi_open_protocol(
1134 void *handle, efi_guid_t *protocol,
1135 void **protocol_interface, void *agent_handle,
1136 void *controller_handle, uint32_t attributes)
1137{
1138 struct list_head *lhandle;
1139 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001140 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001141
1142 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1143 protocol_interface, agent_handle, controller_handle,
1144 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001145
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001146 if (!handle || !protocol ||
1147 (!protocol_interface && attributes !=
1148 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001149 goto out;
1150 }
1151
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001152 EFI_PRINT_GUID("protocol", protocol);
1153
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001154 switch (attributes) {
1155 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1156 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1157 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1158 break;
1159 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1160 if (controller_handle == handle)
1161 goto out;
1162 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1163 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1164 if (controller_handle == NULL)
1165 goto out;
1166 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1167 if (agent_handle == NULL)
1168 goto out;
1169 break;
1170 default:
1171 goto out;
1172 }
1173
Alexander Grafc15d9212016-03-04 01:09:59 +01001174 list_for_each(lhandle, &efi_obj_list) {
1175 struct efi_object *efiobj;
1176 efiobj = list_entry(lhandle, struct efi_object, link);
1177
1178 if (efiobj->handle != handle)
1179 continue;
1180
1181 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1182 struct efi_handler *handler = &efiobj->protocols[i];
1183 const efi_guid_t *hprotocol = handler->guid;
1184 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001185 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001186 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001187 if (attributes !=
1188 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1189 *protocol_interface =
1190 handler->protocol_interface;
1191 }
1192 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001193 goto out;
1194 }
1195 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001196 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001197 }
1198
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001199unsupported:
1200 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01001201out:
1202 return EFI_EXIT(r);
1203}
1204
1205static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1206 efi_guid_t *protocol,
1207 void **protocol_interface)
1208{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02001209 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1210 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01001211}
1212
1213static const struct efi_boot_services efi_boot_services = {
1214 .hdr = {
1215 .headersize = sizeof(struct efi_table_hdr),
1216 },
1217 .raise_tpl = efi_raise_tpl,
1218 .restore_tpl = efi_restore_tpl,
1219 .allocate_pages = efi_allocate_pages_ext,
1220 .free_pages = efi_free_pages_ext,
1221 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02001222 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02001223 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02001224 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02001225 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001226 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001227 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001228 .close_event = efi_close_event,
1229 .check_event = efi_check_event,
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +02001230 .install_protocol_interface = efi_install_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001231 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +02001232 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001233 .handle_protocol = efi_handle_protocol,
1234 .reserved = NULL,
1235 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001236 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001237 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02001238 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001239 .load_image = efi_load_image,
1240 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02001241 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01001242 .unload_image = efi_unload_image,
1243 .exit_boot_services = efi_exit_boot_services,
1244 .get_next_monotonic_count = efi_get_next_monotonic_count,
1245 .stall = efi_stall,
1246 .set_watchdog_timer = efi_set_watchdog_timer,
1247 .connect_controller = efi_connect_controller,
1248 .disconnect_controller = efi_disconnect_controller,
1249 .open_protocol = efi_open_protocol,
1250 .close_protocol = efi_close_protocol,
1251 .open_protocol_information = efi_open_protocol_information,
1252 .protocols_per_handle = efi_protocols_per_handle,
1253 .locate_handle_buffer = efi_locate_handle_buffer,
1254 .locate_protocol = efi_locate_protocol,
1255 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1256 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1257 .calculate_crc32 = efi_calculate_crc32,
1258 .copy_mem = efi_copy_mem,
1259 .set_mem = efi_set_mem,
1260};
1261
1262
Alexander Graf393dd912016-10-14 13:45:30 +02001263static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01001264 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1265
Alexander Graf393dd912016-10-14 13:45:30 +02001266struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01001267 .hdr = {
1268 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1269 .revision = 0x20005, /* 2.5 */
1270 .headersize = sizeof(struct efi_table_hdr),
1271 },
1272 .fw_vendor = (long)firmware_vendor,
1273 .con_in = (void*)&efi_con_in,
1274 .con_out = (void*)&efi_con_out,
1275 .std_err = (void*)&efi_con_out,
1276 .runtime = (void*)&efi_runtime_services,
1277 .boottime = (void*)&efi_boot_services,
1278 .nr_tables = 0,
1279 .tables = (void*)efi_conf_table,
1280};