blob: 849d2298213b0490d7dbc4a2fc02a086d2e92464 [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
52/* Called from do_bootefi_exec() */
53void efi_save_gd(void)
54{
Simon Glasscdfe6962016-09-25 15:27:35 -060055#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010056 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060057#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010058}
59
60/* Called on every callback entry */
61void efi_restore_gd(void)
62{
Simon Glasscdfe6962016-09-25 15:27:35 -060063#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010064 /* Only restore if we're already in EFI context */
65 if (!efi_gd)
66 return;
67
68 if (gd != efi_gd)
69 app_gd = gd;
70 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060071#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010072}
73
74/* Called on every callback exit */
75efi_status_t efi_exit_func(efi_status_t ret)
76{
Simon Glasscdfe6962016-09-25 15:27:35 -060077#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010078 gd = app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060079#endif
80
Alexander Grafc15d9212016-03-04 01:09:59 +010081 return ret;
82}
83
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +020084/* Low 32 bit */
85#define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
86/* High 32 bit */
87#define EFI_HIGH32(a) (a >> 32)
88
89/*
90 * 64bit division by 10 implemented as multiplication by 1 / 10
91 *
92 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
93 */
94#define EFI_TENTH 0x199999999999999A
95static u64 efi_div10(u64 a)
96{
97 u64 prod;
98 u64 rem;
99 u64 ret;
100
101 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
102 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
103 rem = EFI_LOW32(prod);
104 ret += EFI_HIGH32(prod);
105 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
106 rem += EFI_LOW32(prod);
107 ret += EFI_HIGH32(prod);
108 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
109 rem += EFI_HIGH32(prod);
110 ret += EFI_HIGH32(rem);
111 /* Round to nearest integer */
112 if (rem >= (1 << 31))
113 ++ret;
114 return ret;
115}
116
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200117void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200118{
119 if (event->signaled)
120 return;
121 event->signaled = 1;
122 if (event->type & EVT_NOTIFY_SIGNAL) {
Rob Clark63ea77a2017-07-27 08:04:17 -0400123 EFI_CALL(event->notify_function(event, event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200124 }
125}
126
Alexander Grafc15d9212016-03-04 01:09:59 +0100127static efi_status_t efi_unsupported(const char *funcname)
128{
Alexander Graf62a67482016-06-02 11:38:27 +0200129 debug("EFI: App called into unimplemented function %s\n", funcname);
Alexander Grafc15d9212016-03-04 01:09:59 +0100130 return EFI_EXIT(EFI_UNSUPPORTED);
131}
132
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200133static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100134{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200135 EFI_ENTRY("0x%zx", new_tpl);
Alexander Grafc15d9212016-03-04 01:09:59 +0100136 return EFI_EXIT(0);
137}
138
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200139static void EFIAPI efi_restore_tpl(UINTN old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100140{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200141 EFI_ENTRY("0x%zx", old_tpl);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400142 efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100143}
144
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900145static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
146 unsigned long pages,
147 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100148{
149 efi_status_t r;
150
151 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
152 r = efi_allocate_pages(type, memory_type, pages, memory);
153 return EFI_EXIT(r);
154}
155
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900156static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
157 unsigned long pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100158{
159 efi_status_t r;
160
161 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
162 r = efi_free_pages(memory, pages);
163 return EFI_EXIT(r);
164}
165
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900166static efi_status_t EFIAPI efi_get_memory_map_ext(
167 unsigned long *memory_map_size,
168 struct efi_mem_desc *memory_map,
169 unsigned long *map_key,
170 unsigned long *descriptor_size,
171 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100172{
173 efi_status_t r;
174
175 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
176 map_key, descriptor_size, descriptor_version);
177 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
178 descriptor_size, descriptor_version);
179 return EFI_EXIT(r);
180}
181
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200182static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
183 unsigned long size,
184 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100185{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100186 efi_status_t r;
187
188 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200189 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100190 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100191}
192
Stefan Brüns67b67d92016-10-09 22:17:26 +0200193static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100194{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100195 efi_status_t r;
196
197 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200198 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100199 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100200}
201
202/*
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200203 * Our event capabilities are very limited. Only a small limited
204 * number of events is allowed to coexist.
Alexander Grafc15d9212016-03-04 01:09:59 +0100205 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200206static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100207
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200208efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200209 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200210 struct efi_event *event,
211 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200212 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100213{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200214 int i;
215
Jonathan Gray7758b212017-03-12 19:26:07 +1100216 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200217 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100218
219 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200220 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100221
222 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
223 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200224 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100225
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200226 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
227 if (efi_events[i].type)
228 continue;
229 efi_events[i].type = type;
230 efi_events[i].notify_tpl = notify_tpl;
231 efi_events[i].notify_function = notify_function;
232 efi_events[i].notify_context = notify_context;
233 /* Disable timers on bootup */
234 efi_events[i].trigger_next = -1ULL;
235 efi_events[i].signaled = 0;
236 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200237 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200238 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200239 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100240}
241
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200242static efi_status_t EFIAPI efi_create_event_ext(
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200243 uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200244 void (EFIAPI *notify_function) (
245 struct efi_event *event,
246 void *context),
247 void *notify_context, struct efi_event **event)
248{
249 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
250 notify_context);
251 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
252 notify_context, event));
253}
254
255
Alexander Grafc15d9212016-03-04 01:09:59 +0100256/*
257 * Our timers have to work without interrupts, so we check whenever keyboard
258 * input or disk accesses happen if enough time elapsed for it to fire.
259 */
260void efi_timer_check(void)
261{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200262 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100263 u64 now = timer_get_us();
264
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200265 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
266 if (!efi_events[i].type ||
267 !(efi_events[i].type & EVT_TIMER) ||
268 efi_events[i].trigger_type == EFI_TIMER_STOP ||
269 now < efi_events[i].trigger_next)
270 continue;
271 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
272 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200273 efi_events[i].trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200274 efi_events[i].signaled = 0;
275 }
276 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100277 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100278 WATCHDOG_RESET();
279}
280
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200281efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200282 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100283{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200284 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100285
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200286 /*
287 * The parameter defines a multiple of 100ns.
288 * We use multiples of 1000ns. So divide by 10.
289 */
290 trigger_time = efi_div10(trigger_time);
Alexander Grafc15d9212016-03-04 01:09:59 +0100291
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200292 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
293 if (event != &efi_events[i])
294 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100295
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200296 if (!(event->type & EVT_TIMER))
297 break;
298 switch (type) {
299 case EFI_TIMER_STOP:
300 event->trigger_next = -1ULL;
301 break;
302 case EFI_TIMER_PERIODIC:
303 case EFI_TIMER_RELATIVE:
304 event->trigger_next =
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200305 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200306 break;
307 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200308 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200309 }
310 event->trigger_type = type;
311 event->trigger_time = trigger_time;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200312 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100313 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200314 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100315}
316
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200317static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
318 enum efi_timer_delay type,
319 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200320{
321 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
322 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
323}
324
Alexander Grafc15d9212016-03-04 01:09:59 +0100325static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200326 struct efi_event **event,
327 unsigned long *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100328{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200329 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100330
331 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
332
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200333 /* Check parameters */
334 if (!num_events || !event)
335 return EFI_EXIT(EFI_INVALID_PARAMETER);
336 for (i = 0; i < num_events; ++i) {
337 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
338 if (event[i] == &efi_events[j])
339 goto known_event;
340 }
341 return EFI_EXIT(EFI_INVALID_PARAMETER);
342known_event:
343 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
344 return EFI_EXIT(EFI_INVALID_PARAMETER);
345 }
346
347 /* Wait for signal */
348 for (;;) {
349 for (i = 0; i < num_events; ++i) {
350 if (event[i]->signaled)
351 goto out;
352 }
353 /* Allow events to occur. */
354 efi_timer_check();
355 }
356
357out:
358 /*
359 * Reset the signal which is passed to the caller to allow periodic
360 * events to occur.
361 */
362 event[i]->signaled = 0;
363 if (index)
364 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100365
366 return EFI_EXIT(EFI_SUCCESS);
367}
368
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200369static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100370{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200371 int i;
372
Alexander Grafc15d9212016-03-04 01:09:59 +0100373 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200374 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
375 if (event != &efi_events[i])
376 continue;
377 efi_signal_event(event);
378 break;
379 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100380 return EFI_EXIT(EFI_SUCCESS);
381}
382
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200383static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100384{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200385 int i;
386
Alexander Grafc15d9212016-03-04 01:09:59 +0100387 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200388 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
389 if (event == &efi_events[i]) {
390 event->type = 0;
391 event->trigger_next = -1ULL;
392 event->signaled = 0;
393 return EFI_EXIT(EFI_SUCCESS);
394 }
395 }
396 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100397}
398
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200399static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100400{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200401 int i;
402
Alexander Grafc15d9212016-03-04 01:09:59 +0100403 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200404 efi_timer_check();
405 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
406 if (event != &efi_events[i])
407 continue;
408 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
409 break;
410 if (event->signaled)
411 return EFI_EXIT(EFI_SUCCESS);
412 return EFI_EXIT(EFI_NOT_READY);
413 }
414 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100415}
416
417static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
418 efi_guid_t *protocol, int protocol_interface_type,
419 void *protocol_interface)
420{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200421 struct list_head *lhandle;
422 int i;
423 efi_status_t r;
424
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200425 if (!handle || !protocol ||
426 protocol_interface_type != EFI_NATIVE_INTERFACE) {
427 r = EFI_INVALID_PARAMETER;
428 goto out;
429 }
430
431 /* Create new handle if requested. */
432 if (!*handle) {
433 r = EFI_OUT_OF_RESOURCES;
434 goto out;
435 }
436 /* Find object. */
437 list_for_each(lhandle, &efi_obj_list) {
438 struct efi_object *efiobj;
439 efiobj = list_entry(lhandle, struct efi_object, link);
440
441 if (efiobj->handle != *handle)
442 continue;
443 /* Check if protocol is already installed on the handle. */
444 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
445 struct efi_handler *handler = &efiobj->protocols[i];
446
447 if (!handler->guid)
448 continue;
449 if (!guidcmp(handler->guid, protocol)) {
450 r = EFI_INVALID_PARAMETER;
451 goto out;
452 }
453 }
454 /* Install protocol in first empty slot. */
455 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
456 struct efi_handler *handler = &efiobj->protocols[i];
457
458 if (handler->guid)
459 continue;
460
461 handler->guid = protocol;
462 handler->protocol_interface = protocol_interface;
463 r = EFI_SUCCESS;
464 goto out;
465 }
466 r = EFI_OUT_OF_RESOURCES;
467 goto out;
468 }
469 r = EFI_INVALID_PARAMETER;
470out:
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +0200471 return r;
472}
473
474static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
475 efi_guid_t *protocol, int protocol_interface_type,
476 void *protocol_interface)
477{
478 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
479 protocol_interface);
480
481 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
482 protocol_interface_type,
483 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100484}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200485
Alexander Grafc15d9212016-03-04 01:09:59 +0100486static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
487 efi_guid_t *protocol, void *old_interface,
488 void *new_interface)
489{
490 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
491 new_interface);
492 return EFI_EXIT(EFI_ACCESS_DENIED);
493}
494
495static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
496 efi_guid_t *protocol, void *protocol_interface)
497{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200498 struct list_head *lhandle;
499 int i;
500 efi_status_t r = EFI_NOT_FOUND;
501
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200502 if (!handle || !protocol) {
503 r = EFI_INVALID_PARAMETER;
504 goto out;
505 }
506
507 list_for_each(lhandle, &efi_obj_list) {
508 struct efi_object *efiobj;
509 efiobj = list_entry(lhandle, struct efi_object, link);
510
511 if (efiobj->handle != handle)
512 continue;
513
514 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
515 struct efi_handler *handler = &efiobj->protocols[i];
516 const efi_guid_t *hprotocol = handler->guid;
517
518 if (!hprotocol)
519 continue;
520 if (!guidcmp(hprotocol, protocol)) {
521 if (handler->protocol_interface) {
522 r = EFI_ACCESS_DENIED;
523 } else {
524 handler->guid = 0;
525 r = EFI_SUCCESS;
526 }
527 goto out;
528 }
529 }
530 }
531
532out:
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +0200533 return r;
534}
535
536static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
537 efi_guid_t *protocol, void *protocol_interface)
538{
539 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
540
541 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
542 protocol_interface));
Alexander Grafc15d9212016-03-04 01:09:59 +0100543}
544
545static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200546 struct efi_event *event,
Alexander Grafc15d9212016-03-04 01:09:59 +0100547 void **registration)
548{
549 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
550 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
551}
552
553static int efi_search(enum efi_locate_search_type search_type,
554 efi_guid_t *protocol, void *search_key,
555 struct efi_object *efiobj)
556{
557 int i;
558
559 switch (search_type) {
560 case all_handles:
561 return 0;
562 case by_register_notify:
563 return -1;
564 case by_protocol:
565 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
566 const efi_guid_t *guid = efiobj->protocols[i].guid;
567 if (guid && !guidcmp(guid, protocol))
568 return 0;
569 }
570 return -1;
571 }
572
573 return -1;
574}
575
576static efi_status_t EFIAPI efi_locate_handle(
577 enum efi_locate_search_type search_type,
578 efi_guid_t *protocol, void *search_key,
579 unsigned long *buffer_size, efi_handle_t *buffer)
580{
581 struct list_head *lhandle;
582 unsigned long size = 0;
583
Alexander Grafc15d9212016-03-04 01:09:59 +0100584 /* Count how much space we need */
585 list_for_each(lhandle, &efi_obj_list) {
586 struct efi_object *efiobj;
587 efiobj = list_entry(lhandle, struct efi_object, link);
588 if (!efi_search(search_type, protocol, search_key, efiobj)) {
589 size += sizeof(void*);
590 }
591 }
592
593 if (*buffer_size < size) {
594 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200595 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100596 }
597
598 /* Then fill the array */
599 list_for_each(lhandle, &efi_obj_list) {
600 struct efi_object *efiobj;
601 efiobj = list_entry(lhandle, struct efi_object, link);
602 if (!efi_search(search_type, protocol, search_key, efiobj)) {
603 *(buffer++) = efiobj->handle;
604 }
605 }
606
607 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200608 return EFI_SUCCESS;
609}
610
611static efi_status_t EFIAPI efi_locate_handle_ext(
612 enum efi_locate_search_type search_type,
613 efi_guid_t *protocol, void *search_key,
614 unsigned long *buffer_size, efi_handle_t *buffer)
615{
616 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
617 buffer_size, buffer);
618
619 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
620 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +0100621}
622
623static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
624 struct efi_device_path **device_path,
625 efi_handle_t *device)
626{
627 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
628 return EFI_EXIT(EFI_NOT_FOUND);
629}
630
Alexander Graffe3366f2017-07-26 13:41:04 +0200631/* Collapses configuration table entries, removing index i */
632static void efi_remove_configuration_table(int i)
633{
634 struct efi_configuration_table *this = &efi_conf_table[i];
635 struct efi_configuration_table *next = &efi_conf_table[i+1];
636 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
637
638 memmove(this, next, (ulong)end - (ulong)next);
639 systab.nr_tables--;
640}
641
Alexander Grafc5c11632016-08-19 01:23:24 +0200642efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +0100643{
644 int i;
645
Alexander Grafc15d9212016-03-04 01:09:59 +0100646 /* Check for guid override */
647 for (i = 0; i < systab.nr_tables; i++) {
648 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +0200649 if (table)
650 efi_conf_table[i].table = table;
651 else
652 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +0200653 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100654 }
655 }
656
Alexander Graffe3366f2017-07-26 13:41:04 +0200657 if (!table)
658 return EFI_NOT_FOUND;
659
Alexander Grafc15d9212016-03-04 01:09:59 +0100660 /* No override, check for overflow */
661 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +0200662 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100663
664 /* Add a new entry */
665 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
666 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +0200667 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +0100668
Alexander Grafc5c11632016-08-19 01:23:24 +0200669 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100670}
671
Alexander Grafc5c11632016-08-19 01:23:24 +0200672static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
673 void *table)
674{
675 EFI_ENTRY("%p, %p", guid, table);
676 return EFI_EXIT(efi_install_configuration_table(guid, table));
677}
678
Alexander Grafc15d9212016-03-04 01:09:59 +0100679static efi_status_t EFIAPI efi_load_image(bool boot_policy,
680 efi_handle_t parent_image,
681 struct efi_device_path *file_path,
682 void *source_buffer,
683 unsigned long source_size,
684 efi_handle_t *image_handle)
685{
686 static struct efi_object loaded_image_info_obj = {
687 .protocols = {
688 {
689 .guid = &efi_guid_loaded_image,
Alexander Grafc15d9212016-03-04 01:09:59 +0100690 },
691 },
692 };
693 struct efi_loaded_image *info;
694 struct efi_object *obj;
695
696 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
697 file_path, source_buffer, source_size, image_handle);
698 info = malloc(sizeof(*info));
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +0200699 loaded_image_info_obj.protocols[0].protocol_interface = info;
Alexander Grafc15d9212016-03-04 01:09:59 +0100700 obj = malloc(sizeof(loaded_image_info_obj));
701 memset(info, 0, sizeof(*info));
702 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
703 obj->handle = info;
704 info->file_path = file_path;
705 info->reserved = efi_load_pe(source_buffer, info);
706 if (!info->reserved) {
707 free(info);
708 free(obj);
709 return EFI_EXIT(EFI_UNSUPPORTED);
710 }
711
712 *image_handle = info;
713 list_add_tail(&obj->link, &efi_obj_list);
714
715 return EFI_EXIT(EFI_SUCCESS);
716}
717
718static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
719 unsigned long *exit_data_size,
720 s16 **exit_data)
721{
722 ulong (*entry)(void *image_handle, struct efi_system_table *st);
723 struct efi_loaded_image *info = image_handle;
724
725 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
726 entry = info->reserved;
727
728 efi_is_direct_boot = false;
729
730 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +0200731 if (setjmp(&info->exit_jmp)) {
732 /* We returned from the child image */
733 return EFI_EXIT(info->exit_status);
734 }
735
Alexander Grafc15d9212016-03-04 01:09:59 +0100736 entry(image_handle, &systab);
737
738 /* Should usually never get here */
739 return EFI_EXIT(EFI_SUCCESS);
740}
741
Alexander Graf988c0662016-05-20 23:28:23 +0200742static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
743 efi_status_t exit_status, unsigned long exit_data_size,
744 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +0100745{
Alexander Graf988c0662016-05-20 23:28:23 +0200746 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
747
Alexander Grafc15d9212016-03-04 01:09:59 +0100748 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
749 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +0200750
751 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +0200752 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +0200753
754 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +0100755}
756
757static struct efi_object *efi_search_obj(void *handle)
758{
759 struct list_head *lhandle;
760
761 list_for_each(lhandle, &efi_obj_list) {
762 struct efi_object *efiobj;
763 efiobj = list_entry(lhandle, struct efi_object, link);
764 if (efiobj->handle == handle)
765 return efiobj;
766 }
767
768 return NULL;
769}
770
771static efi_status_t EFIAPI efi_unload_image(void *image_handle)
772{
773 struct efi_object *efiobj;
774
775 EFI_ENTRY("%p", image_handle);
776 efiobj = efi_search_obj(image_handle);
777 if (efiobj)
778 list_del(&efiobj->link);
779
780 return EFI_EXIT(EFI_SUCCESS);
781}
782
783static void efi_exit_caches(void)
784{
785#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
786 /*
787 * Grub on 32bit ARM needs to have caches disabled before jumping into
788 * a zImage, but does not know of all cache layers. Give it a hand.
789 */
790 if (efi_is_direct_boot)
791 cleanup_before_linux();
792#endif
793}
794
795static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
796 unsigned long map_key)
797{
798 EFI_ENTRY("%p, %ld", image_handle, map_key);
799
Alexander Graf2ebeb442016-11-17 01:02:57 +0100800 board_quiesce_devices();
801
Alexander Grafc15d9212016-03-04 01:09:59 +0100802 /* Fix up caches for EFI payloads if necessary */
803 efi_exit_caches();
804
805 /* This stops all lingering devices */
806 bootm_disable_interrupts();
807
808 /* Give the payload some time to boot */
809 WATCHDOG_RESET();
810
811 return EFI_EXIT(EFI_SUCCESS);
812}
813
814static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
815{
816 static uint64_t mono = 0;
817 EFI_ENTRY("%p", count);
818 *count = mono++;
819 return EFI_EXIT(EFI_SUCCESS);
820}
821
822static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
823{
824 EFI_ENTRY("%ld", microseconds);
825 udelay(microseconds);
826 return EFI_EXIT(EFI_SUCCESS);
827}
828
829static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
830 uint64_t watchdog_code,
831 unsigned long data_size,
832 uint16_t *watchdog_data)
833{
834 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
835 data_size, watchdog_data);
Rob Clarkd417d2b2017-07-25 20:17:59 -0400836 return efi_unsupported(__func__);
Alexander Grafc15d9212016-03-04 01:09:59 +0100837}
838
839static efi_status_t EFIAPI efi_connect_controller(
840 efi_handle_t controller_handle,
841 efi_handle_t *driver_image_handle,
842 struct efi_device_path *remain_device_path,
843 bool recursive)
844{
845 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
846 remain_device_path, recursive);
847 return EFI_EXIT(EFI_NOT_FOUND);
848}
849
850static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
851 void *driver_image_handle,
852 void *child_handle)
853{
854 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
855 child_handle);
856 return EFI_EXIT(EFI_INVALID_PARAMETER);
857}
858
859static efi_status_t EFIAPI efi_close_protocol(void *handle,
860 efi_guid_t *protocol,
861 void *agent_handle,
862 void *controller_handle)
863{
864 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
865 controller_handle);
866 return EFI_EXIT(EFI_NOT_FOUND);
867}
868
869static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
870 efi_guid_t *protocol,
871 struct efi_open_protocol_info_entry **entry_buffer,
872 unsigned long *entry_count)
873{
874 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
875 entry_count);
876 return EFI_EXIT(EFI_NOT_FOUND);
877}
878
879static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
880 efi_guid_t ***protocol_buffer,
881 unsigned long *protocol_buffer_count)
882{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200883 unsigned long buffer_size;
884 struct efi_object *efiobj;
885 unsigned long i, j;
886 struct list_head *lhandle;
887 efi_status_t r;
888
Alexander Grafc15d9212016-03-04 01:09:59 +0100889 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
890 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200891
892 if (!handle || !protocol_buffer || !protocol_buffer_count)
893 return EFI_EXIT(EFI_INVALID_PARAMETER);
894
895 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -0400896 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +0200897 list_for_each(lhandle, &efi_obj_list) {
898 efiobj = list_entry(lhandle, struct efi_object, link);
899
900 if (efiobj->handle != handle)
901 continue;
902
903 /* Count protocols */
904 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
905 if (efiobj->protocols[i].guid)
906 ++*protocol_buffer_count;
907 }
908 /* Copy guids */
909 if (*protocol_buffer_count) {
910 buffer_size = sizeof(efi_guid_t *) *
911 *protocol_buffer_count;
912 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
913 buffer_size,
914 (void **)protocol_buffer);
915 if (r != EFI_SUCCESS)
916 return EFI_EXIT(r);
917 j = 0;
918 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
919 if (efiobj->protocols[i].guid) {
920 (*protocol_buffer)[j] = (void *)
921 efiobj->protocols[i].guid;
922 ++j;
923 }
924 }
925 }
926 break;
927 }
928
929 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100930}
931
932static efi_status_t EFIAPI efi_locate_handle_buffer(
933 enum efi_locate_search_type search_type,
934 efi_guid_t *protocol, void *search_key,
935 unsigned long *no_handles, efi_handle_t **buffer)
936{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +0200937 efi_status_t r;
938 unsigned long buffer_size = 0;
939
Alexander Grafc15d9212016-03-04 01:09:59 +0100940 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
941 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +0200942
943 if (!no_handles || !buffer) {
944 r = EFI_INVALID_PARAMETER;
945 goto out;
946 }
947 *no_handles = 0;
948 *buffer = NULL;
949 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
950 *buffer);
951 if (r != EFI_BUFFER_TOO_SMALL)
952 goto out;
953 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
954 (void **)buffer);
955 if (r != EFI_SUCCESS)
956 goto out;
957 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
958 *buffer);
959 if (r == EFI_SUCCESS)
960 *no_handles = buffer_size / sizeof(void *);
961out:
962 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100963}
964
Alexander Grafc15d9212016-03-04 01:09:59 +0100965static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
966 void *registration,
967 void **protocol_interface)
968{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +0200969 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +0100970 int i;
971
972 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +0200973
974 if (!protocol || !protocol_interface)
975 return EFI_EXIT(EFI_INVALID_PARAMETER);
976
977 list_for_each(lhandle, &efi_obj_list) {
978 struct efi_object *efiobj;
979
980 efiobj = list_entry(lhandle, struct efi_object, link);
981 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
982 struct efi_handler *handler = &efiobj->protocols[i];
983
984 if (!handler->guid)
985 continue;
986 if (!guidcmp(handler->guid, protocol)) {
987 *protocol_interface =
988 handler->protocol_interface;
989 return EFI_EXIT(EFI_SUCCESS);
990 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100991 }
992 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +0200993 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100994
995 return EFI_EXIT(EFI_NOT_FOUND);
996}
997
998static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
999 void **handle, ...)
1000{
1001 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001002
1003 va_list argptr;
1004 efi_guid_t *protocol;
1005 void *protocol_interface;
1006 efi_status_t r = EFI_SUCCESS;
1007 int i = 0;
1008
1009 if (!handle)
1010 return EFI_EXIT(EFI_INVALID_PARAMETER);
1011
1012 va_start(argptr, handle);
1013 for (;;) {
1014 protocol = va_arg(argptr, efi_guid_t*);
1015 if (!protocol)
1016 break;
1017 protocol_interface = va_arg(argptr, void*);
1018 r = efi_install_protocol_interface(handle, protocol,
1019 EFI_NATIVE_INTERFACE,
1020 protocol_interface);
1021 if (r != EFI_SUCCESS)
1022 break;
1023 i++;
1024 }
1025 va_end(argptr);
1026 if (r == EFI_SUCCESS)
1027 return EFI_EXIT(r);
1028
1029 /* If an error occured undo all changes. */
1030 va_start(argptr, handle);
1031 for (; i; --i) {
1032 protocol = va_arg(argptr, efi_guid_t*);
1033 protocol_interface = va_arg(argptr, void*);
1034 efi_uninstall_protocol_interface(handle, protocol,
1035 protocol_interface);
1036 }
1037 va_end(argptr);
1038
1039 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001040}
1041
1042static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1043 void *handle, ...)
1044{
1045 EFI_ENTRY("%p", handle);
1046 return EFI_EXIT(EFI_INVALID_PARAMETER);
1047}
1048
1049static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1050 unsigned long data_size,
1051 uint32_t *crc32_p)
1052{
1053 EFI_ENTRY("%p, %ld", data, data_size);
1054 *crc32_p = crc32(0, data, data_size);
1055 return EFI_EXIT(EFI_SUCCESS);
1056}
1057
1058static void EFIAPI efi_copy_mem(void *destination, void *source,
1059 unsigned long length)
1060{
1061 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1062 memcpy(destination, source, length);
1063}
1064
1065static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1066{
1067 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1068 memset(buffer, value, size);
1069}
1070
1071static efi_status_t EFIAPI efi_open_protocol(
1072 void *handle, efi_guid_t *protocol,
1073 void **protocol_interface, void *agent_handle,
1074 void *controller_handle, uint32_t attributes)
1075{
1076 struct list_head *lhandle;
1077 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001078 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001079
1080 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1081 protocol_interface, agent_handle, controller_handle,
1082 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001083
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001084 if (!handle || !protocol ||
1085 (!protocol_interface && attributes !=
1086 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001087 goto out;
1088 }
1089
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001090 switch (attributes) {
1091 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1092 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1093 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1094 break;
1095 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1096 if (controller_handle == handle)
1097 goto out;
1098 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1099 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1100 if (controller_handle == NULL)
1101 goto out;
1102 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1103 if (agent_handle == NULL)
1104 goto out;
1105 break;
1106 default:
1107 goto out;
1108 }
1109
Alexander Grafc15d9212016-03-04 01:09:59 +01001110 list_for_each(lhandle, &efi_obj_list) {
1111 struct efi_object *efiobj;
1112 efiobj = list_entry(lhandle, struct efi_object, link);
1113
1114 if (efiobj->handle != handle)
1115 continue;
1116
1117 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1118 struct efi_handler *handler = &efiobj->protocols[i];
1119 const efi_guid_t *hprotocol = handler->guid;
1120 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001121 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001122 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001123 if (attributes !=
1124 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1125 *protocol_interface =
1126 handler->protocol_interface;
1127 }
1128 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001129 goto out;
1130 }
1131 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001132 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001133 }
1134
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001135unsupported:
1136 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01001137out:
1138 return EFI_EXIT(r);
1139}
1140
1141static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1142 efi_guid_t *protocol,
1143 void **protocol_interface)
1144{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02001145 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1146 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01001147}
1148
1149static const struct efi_boot_services efi_boot_services = {
1150 .hdr = {
1151 .headersize = sizeof(struct efi_table_hdr),
1152 },
1153 .raise_tpl = efi_raise_tpl,
1154 .restore_tpl = efi_restore_tpl,
1155 .allocate_pages = efi_allocate_pages_ext,
1156 .free_pages = efi_free_pages_ext,
1157 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02001158 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02001159 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02001160 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02001161 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001162 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001163 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001164 .close_event = efi_close_event,
1165 .check_event = efi_check_event,
xypron.glpk@gmx.de8b7b5df2017-07-11 22:06:18 +02001166 .install_protocol_interface = efi_install_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001167 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
xypron.glpk@gmx.dea453e462017-07-11 22:06:19 +02001168 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001169 .handle_protocol = efi_handle_protocol,
1170 .reserved = NULL,
1171 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001172 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001173 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02001174 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001175 .load_image = efi_load_image,
1176 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02001177 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01001178 .unload_image = efi_unload_image,
1179 .exit_boot_services = efi_exit_boot_services,
1180 .get_next_monotonic_count = efi_get_next_monotonic_count,
1181 .stall = efi_stall,
1182 .set_watchdog_timer = efi_set_watchdog_timer,
1183 .connect_controller = efi_connect_controller,
1184 .disconnect_controller = efi_disconnect_controller,
1185 .open_protocol = efi_open_protocol,
1186 .close_protocol = efi_close_protocol,
1187 .open_protocol_information = efi_open_protocol_information,
1188 .protocols_per_handle = efi_protocols_per_handle,
1189 .locate_handle_buffer = efi_locate_handle_buffer,
1190 .locate_protocol = efi_locate_protocol,
1191 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1192 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1193 .calculate_crc32 = efi_calculate_crc32,
1194 .copy_mem = efi_copy_mem,
1195 .set_mem = efi_set_mem,
1196};
1197
1198
Alexander Graf393dd912016-10-14 13:45:30 +02001199static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01001200 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1201
Alexander Graf393dd912016-10-14 13:45:30 +02001202struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01001203 .hdr = {
1204 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1205 .revision = 0x20005, /* 2.5 */
1206 .headersize = sizeof(struct efi_table_hdr),
1207 },
1208 .fw_vendor = (long)firmware_vendor,
1209 .con_in = (void*)&efi_con_in,
1210 .con_out = (void*)&efi_con_out,
1211 .std_err = (void*)&efi_con_out,
1212 .runtime = (void*)&efi_runtime_services,
1213 .boottime = (void*)&efi_boot_services,
1214 .nr_tables = 0,
1215 .tables = (void*)efi_conf_table,
1216};