]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/efi_loader/efi_boottime.c
Merge tag 'signed-efi-next' of git://github.com/agraf/u-boot
[people/ms/u-boot.git] / lib / efi_loader / efi_boottime.c
CommitLineData
bee91169
AG
1/*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
bee91169
AG
9#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 */
3c63db9c 40static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
bee91169 41
65e4c0b1 42#ifdef CONFIG_ARM
bee91169
AG
43/*
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;
65e4c0b1 50#endif
bee91169 51
c160d2f5 52static int entry_count;
af65db85 53static int nesting_level;
c160d2f5
RC
54
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
bee91169
AG
77/* Called from do_bootefi_exec() */
78void efi_save_gd(void)
79{
65e4c0b1 80#ifdef CONFIG_ARM
bee91169 81 efi_gd = gd;
65e4c0b1 82#endif
bee91169
AG
83}
84
c160d2f5
RC
85/*
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 */
bee91169
AG
90void efi_restore_gd(void)
91{
65e4c0b1 92#ifdef CONFIG_ARM
bee91169
AG
93 /* Only restore if we're already in EFI context */
94 if (!efi_gd)
95 return;
bee91169 96 gd = efi_gd;
65e4c0b1 97#endif
bee91169
AG
98}
99
af65db85
RC
100/*
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
112const char *__efi_nesting_inc(void)
113{
114 return indent_string(nesting_level++);
115}
116
117const char *__efi_nesting_dec(void)
118{
119 return indent_string(--nesting_level);
120}
121
8787b02e 122/* Low 32 bit */
123#define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
124/* High 32 bit */
125#define EFI_HIGH32(a) (a >> 32)
126
127/*
128 * 64bit division by 10 implemented as multiplication by 1 / 10
129 *
130 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
131 */
132#define EFI_TENTH 0x199999999999999A
133static u64 efi_div10(u64 a)
134{
135 u64 prod;
136 u64 rem;
137 u64 ret;
138
139 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
140 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
141 rem = EFI_LOW32(prod);
142 ret += EFI_HIGH32(prod);
143 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
144 rem += EFI_LOW32(prod);
145 ret += EFI_HIGH32(prod);
146 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
147 rem += EFI_HIGH32(prod);
148 ret += EFI_HIGH32(rem);
149 /* Round to nearest integer */
150 if (rem >= (1 << 31))
151 ++ret;
152 return ret;
153}
154
91be9a77 155void efi_signal_event(struct efi_event *event)
c6841592 156{
157 if (event->signaled)
158 return;
159 event->signaled = 1;
160 if (event->type & EVT_NOTIFY_SIGNAL) {
a095aadf 161 EFI_CALL(event->notify_function(event, event->notify_context));
c6841592 162 }
163}
164
bee91169
AG
165static efi_status_t efi_unsupported(const char *funcname)
166{
edcef3ba 167 debug("EFI: App called into unimplemented function %s\n", funcname);
bee91169
AG
168 return EFI_EXIT(EFI_UNSUPPORTED);
169}
170
503f2695 171static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
bee91169 172{
503f2695 173 EFI_ENTRY("0x%zx", new_tpl);
bee91169
AG
174 return EFI_EXIT(0);
175}
176
503f2695 177static void EFIAPI efi_restore_tpl(UINTN old_tpl)
bee91169 178{
503f2695 179 EFI_ENTRY("0x%zx", old_tpl);
b5104821 180 efi_unsupported(__func__);
bee91169
AG
181}
182
6e0bf8d8
MY
183static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
184 unsigned long pages,
185 uint64_t *memory)
bee91169
AG
186{
187 efi_status_t r;
188
189 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
190 r = efi_allocate_pages(type, memory_type, pages, memory);
191 return EFI_EXIT(r);
192}
193
6e0bf8d8
MY
194static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
195 unsigned long pages)
bee91169
AG
196{
197 efi_status_t r;
198
199 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
200 r = efi_free_pages(memory, pages);
201 return EFI_EXIT(r);
202}
203
6e0bf8d8
MY
204static efi_status_t EFIAPI efi_get_memory_map_ext(
205 unsigned long *memory_map_size,
206 struct efi_mem_desc *memory_map,
207 unsigned long *map_key,
208 unsigned long *descriptor_size,
209 uint32_t *descriptor_version)
bee91169
AG
210{
211 efi_status_t r;
212
213 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
214 map_key, descriptor_size, descriptor_version);
215 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
216 descriptor_size, descriptor_version);
217 return EFI_EXIT(r);
218}
219
ead1274b
SB
220static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
221 unsigned long size,
222 void **buffer)
bee91169 223{
1cd29f0a
AG
224 efi_status_t r;
225
226 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
ead1274b 227 r = efi_allocate_pool(pool_type, size, buffer);
1cd29f0a 228 return EFI_EXIT(r);
bee91169
AG
229}
230
42417bc8 231static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
bee91169 232{
1cd29f0a
AG
233 efi_status_t r;
234
235 EFI_ENTRY("%p", buffer);
42417bc8 236 r = efi_free_pool(buffer);
1cd29f0a 237 return EFI_EXIT(r);
bee91169
AG
238}
239
240/*
c6841592 241 * Our event capabilities are very limited. Only a small limited
242 * number of events is allowed to coexist.
bee91169 243 */
c6841592 244static struct efi_event efi_events[16];
bee91169 245
b521d29e 246efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
49deb455 247 void (EFIAPI *notify_function) (
2fd945fe 248 struct efi_event *event,
249 void *context),
49deb455 250 void *notify_context, struct efi_event **event)
bee91169 251{
c6841592 252 int i;
253
a95343b8 254 if (event == NULL)
49deb455 255 return EFI_INVALID_PARAMETER;
a95343b8
JG
256
257 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
49deb455 258 return EFI_INVALID_PARAMETER;
a95343b8
JG
259
260 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
261 notify_function == NULL)
49deb455 262 return EFI_INVALID_PARAMETER;
a95343b8 263
c6841592 264 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
265 if (efi_events[i].type)
266 continue;
267 efi_events[i].type = type;
268 efi_events[i].notify_tpl = notify_tpl;
269 efi_events[i].notify_function = notify_function;
270 efi_events[i].notify_context = notify_context;
271 /* Disable timers on bootup */
272 efi_events[i].trigger_next = -1ULL;
273 efi_events[i].signaled = 0;
274 *event = &efi_events[i];
49deb455 275 return EFI_SUCCESS;
c6841592 276 }
49deb455 277 return EFI_OUT_OF_RESOURCES;
bee91169
AG
278}
279
49deb455 280static efi_status_t EFIAPI efi_create_event_ext(
b521d29e 281 uint32_t type, UINTN notify_tpl,
49deb455 282 void (EFIAPI *notify_function) (
283 struct efi_event *event,
284 void *context),
285 void *notify_context, struct efi_event **event)
286{
287 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
288 notify_context);
289 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
290 notify_context, event));
291}
292
293
bee91169
AG
294/*
295 * Our timers have to work without interrupts, so we check whenever keyboard
296 * input or disk accesses happen if enough time elapsed for it to fire.
297 */
298void efi_timer_check(void)
299{
c6841592 300 int i;
bee91169
AG
301 u64 now = timer_get_us();
302
c6841592 303 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
304 if (!efi_events[i].type ||
305 !(efi_events[i].type & EVT_TIMER) ||
306 efi_events[i].trigger_type == EFI_TIMER_STOP ||
307 now < efi_events[i].trigger_next)
308 continue;
309 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
310 efi_events[i].trigger_next +=
8787b02e 311 efi_events[i].trigger_time;
c6841592 312 efi_events[i].signaled = 0;
313 }
314 efi_signal_event(&efi_events[i]);
bee91169 315 }
bee91169
AG
316 WATCHDOG_RESET();
317}
318
b521d29e 319efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
bfc72462 320 uint64_t trigger_time)
bee91169 321{
c6841592 322 int i;
bee91169 323
8787b02e 324 /*
325 * The parameter defines a multiple of 100ns.
326 * We use multiples of 1000ns. So divide by 10.
327 */
328 trigger_time = efi_div10(trigger_time);
bee91169 329
c6841592 330 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
331 if (event != &efi_events[i])
332 continue;
bee91169 333
c6841592 334 if (!(event->type & EVT_TIMER))
335 break;
336 switch (type) {
337 case EFI_TIMER_STOP:
338 event->trigger_next = -1ULL;
339 break;
340 case EFI_TIMER_PERIODIC:
341 case EFI_TIMER_RELATIVE:
342 event->trigger_next =
8787b02e 343 timer_get_us() + trigger_time;
c6841592 344 break;
345 default:
bfc72462 346 return EFI_INVALID_PARAMETER;
c6841592 347 }
348 event->trigger_type = type;
349 event->trigger_time = trigger_time;
bfc72462 350 return EFI_SUCCESS;
bee91169 351 }
bfc72462 352 return EFI_INVALID_PARAMETER;
353}
354
b521d29e 355static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
356 enum efi_timer_delay type,
357 uint64_t trigger_time)
bfc72462 358{
359 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
360 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
bee91169
AG
361}
362
363static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
2fd945fe 364 struct efi_event **event,
365 unsigned long *index)
bee91169 366{
c6841592 367 int i, j;
bee91169
AG
368
369 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
370
c6841592 371 /* Check parameters */
372 if (!num_events || !event)
373 return EFI_EXIT(EFI_INVALID_PARAMETER);
374 for (i = 0; i < num_events; ++i) {
375 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
376 if (event[i] == &efi_events[j])
377 goto known_event;
378 }
379 return EFI_EXIT(EFI_INVALID_PARAMETER);
380known_event:
381 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
382 return EFI_EXIT(EFI_INVALID_PARAMETER);
383 }
384
385 /* Wait for signal */
386 for (;;) {
387 for (i = 0; i < num_events; ++i) {
388 if (event[i]->signaled)
389 goto out;
390 }
391 /* Allow events to occur. */
392 efi_timer_check();
393 }
394
395out:
396 /*
397 * Reset the signal which is passed to the caller to allow periodic
398 * events to occur.
399 */
400 event[i]->signaled = 0;
401 if (index)
402 *index = i;
bee91169
AG
403
404 return EFI_EXIT(EFI_SUCCESS);
405}
406
c6841592 407static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
bee91169 408{
c6841592 409 int i;
410
bee91169 411 EFI_ENTRY("%p", event);
c6841592 412 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
413 if (event != &efi_events[i])
414 continue;
415 efi_signal_event(event);
416 break;
417 }
bee91169
AG
418 return EFI_EXIT(EFI_SUCCESS);
419}
420
2fd945fe 421static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
bee91169 422{
c6841592 423 int i;
424
bee91169 425 EFI_ENTRY("%p", event);
c6841592 426 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
427 if (event == &efi_events[i]) {
428 event->type = 0;
429 event->trigger_next = -1ULL;
430 event->signaled = 0;
431 return EFI_EXIT(EFI_SUCCESS);
432 }
433 }
434 return EFI_EXIT(EFI_INVALID_PARAMETER);
bee91169
AG
435}
436
2fd945fe 437static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
bee91169 438{
c6841592 439 int i;
440
bee91169 441 EFI_ENTRY("%p", event);
c6841592 442 efi_timer_check();
443 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
444 if (event != &efi_events[i])
445 continue;
446 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
447 break;
448 if (event->signaled)
449 return EFI_EXIT(EFI_SUCCESS);
450 return EFI_EXIT(EFI_NOT_READY);
451 }
452 return EFI_EXIT(EFI_INVALID_PARAMETER);
bee91169
AG
453}
454
455static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
456 efi_guid_t *protocol, int protocol_interface_type,
457 void *protocol_interface)
458{
e0549f8a 459 struct list_head *lhandle;
460 int i;
461 efi_status_t r;
462
e0549f8a 463 if (!handle || !protocol ||
464 protocol_interface_type != EFI_NATIVE_INTERFACE) {
465 r = EFI_INVALID_PARAMETER;
466 goto out;
467 }
468
469 /* Create new handle if requested. */
470 if (!*handle) {
471 r = EFI_OUT_OF_RESOURCES;
472 goto out;
473 }
474 /* Find object. */
475 list_for_each(lhandle, &efi_obj_list) {
476 struct efi_object *efiobj;
477 efiobj = list_entry(lhandle, struct efi_object, link);
478
479 if (efiobj->handle != *handle)
480 continue;
481 /* Check if protocol is already installed on the handle. */
482 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
483 struct efi_handler *handler = &efiobj->protocols[i];
484
485 if (!handler->guid)
486 continue;
487 if (!guidcmp(handler->guid, protocol)) {
488 r = EFI_INVALID_PARAMETER;
489 goto out;
490 }
491 }
492 /* Install protocol in first empty slot. */
493 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
494 struct efi_handler *handler = &efiobj->protocols[i];
495
496 if (handler->guid)
497 continue;
498
499 handler->guid = protocol;
500 handler->protocol_interface = protocol_interface;
501 r = EFI_SUCCESS;
502 goto out;
503 }
504 r = EFI_OUT_OF_RESOURCES;
505 goto out;
506 }
507 r = EFI_INVALID_PARAMETER;
508out:
8bee5a3c 509 return r;
510}
511
512static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
513 efi_guid_t *protocol, int protocol_interface_type,
514 void *protocol_interface)
515{
516 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
517 protocol_interface);
518
519 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
520 protocol_interface_type,
521 protocol_interface));
bee91169 522}
e0549f8a 523
bee91169
AG
524static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
525 efi_guid_t *protocol, void *old_interface,
526 void *new_interface)
527{
528 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
529 new_interface);
530 return EFI_EXIT(EFI_ACCESS_DENIED);
531}
532
533static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
534 efi_guid_t *protocol, void *protocol_interface)
535{
4b6ed0d7 536 struct list_head *lhandle;
537 int i;
538 efi_status_t r = EFI_NOT_FOUND;
539
4b6ed0d7 540 if (!handle || !protocol) {
541 r = EFI_INVALID_PARAMETER;
542 goto out;
543 }
544
545 list_for_each(lhandle, &efi_obj_list) {
546 struct efi_object *efiobj;
547 efiobj = list_entry(lhandle, struct efi_object, link);
548
549 if (efiobj->handle != handle)
550 continue;
551
552 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
553 struct efi_handler *handler = &efiobj->protocols[i];
554 const efi_guid_t *hprotocol = handler->guid;
555
556 if (!hprotocol)
557 continue;
558 if (!guidcmp(hprotocol, protocol)) {
559 if (handler->protocol_interface) {
560 r = EFI_ACCESS_DENIED;
561 } else {
562 handler->guid = 0;
563 r = EFI_SUCCESS;
564 }
565 goto out;
566 }
567 }
568 }
569
570out:
3d8e1456 571 return r;
572}
573
574static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
575 efi_guid_t *protocol, void *protocol_interface)
576{
577 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
578
579 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
580 protocol_interface));
bee91169
AG
581}
582
583static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
2fd945fe 584 struct efi_event *event,
bee91169
AG
585 void **registration)
586{
587 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
588 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
589}
590
591static int efi_search(enum efi_locate_search_type search_type,
592 efi_guid_t *protocol, void *search_key,
593 struct efi_object *efiobj)
594{
595 int i;
596
597 switch (search_type) {
598 case all_handles:
599 return 0;
600 case by_register_notify:
601 return -1;
602 case by_protocol:
603 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
604 const efi_guid_t *guid = efiobj->protocols[i].guid;
605 if (guid && !guidcmp(guid, protocol))
606 return 0;
607 }
608 return -1;
609 }
610
611 return -1;
612}
613
614static efi_status_t EFIAPI efi_locate_handle(
615 enum efi_locate_search_type search_type,
616 efi_guid_t *protocol, void *search_key,
617 unsigned long *buffer_size, efi_handle_t *buffer)
618{
619 struct list_head *lhandle;
620 unsigned long size = 0;
621
bee91169
AG
622 /* Count how much space we need */
623 list_for_each(lhandle, &efi_obj_list) {
624 struct efi_object *efiobj;
625 efiobj = list_entry(lhandle, struct efi_object, link);
626 if (!efi_search(search_type, protocol, search_key, efiobj)) {
627 size += sizeof(void*);
628 }
629 }
630
631 if (*buffer_size < size) {
632 *buffer_size = size;
26329584 633 return EFI_BUFFER_TOO_SMALL;
bee91169
AG
634 }
635
636 /* Then fill the array */
637 list_for_each(lhandle, &efi_obj_list) {
638 struct efi_object *efiobj;
639 efiobj = list_entry(lhandle, struct efi_object, link);
640 if (!efi_search(search_type, protocol, search_key, efiobj)) {
641 *(buffer++) = efiobj->handle;
642 }
643 }
644
645 *buffer_size = size;
26329584 646 return EFI_SUCCESS;
647}
648
649static efi_status_t EFIAPI efi_locate_handle_ext(
650 enum efi_locate_search_type search_type,
651 efi_guid_t *protocol, void *search_key,
652 unsigned long *buffer_size, efi_handle_t *buffer)
653{
654 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
655 buffer_size, buffer);
656
657 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
658 buffer_size, buffer));
bee91169
AG
659}
660
661static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
662 struct efi_device_path **device_path,
663 efi_handle_t *device)
664{
665 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
666 return EFI_EXIT(EFI_NOT_FOUND);
667}
668
d98cdf6a
AG
669/* Collapses configuration table entries, removing index i */
670static void efi_remove_configuration_table(int i)
671{
672 struct efi_configuration_table *this = &efi_conf_table[i];
673 struct efi_configuration_table *next = &efi_conf_table[i+1];
674 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
675
676 memmove(this, next, (ulong)end - (ulong)next);
677 systab.nr_tables--;
678}
679
488bf12d 680efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
bee91169
AG
681{
682 int i;
683
bee91169
AG
684 /* Check for guid override */
685 for (i = 0; i < systab.nr_tables; i++) {
686 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
d98cdf6a
AG
687 if (table)
688 efi_conf_table[i].table = table;
689 else
690 efi_remove_configuration_table(i);
488bf12d 691 return EFI_SUCCESS;
bee91169
AG
692 }
693 }
694
d98cdf6a
AG
695 if (!table)
696 return EFI_NOT_FOUND;
697
bee91169
AG
698 /* No override, check for overflow */
699 if (i >= ARRAY_SIZE(efi_conf_table))
488bf12d 700 return EFI_OUT_OF_RESOURCES;
bee91169
AG
701
702 /* Add a new entry */
703 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
704 efi_conf_table[i].table = table;
aba5e919 705 systab.nr_tables = i + 1;
bee91169 706
488bf12d
AG
707 return EFI_SUCCESS;
708}
709
710static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
711 void *table)
712{
713 EFI_ENTRY("%p, %p", guid, table);
714 return EFI_EXIT(efi_install_configuration_table(guid, table));
bee91169
AG
715}
716
717static efi_status_t EFIAPI efi_load_image(bool boot_policy,
718 efi_handle_t parent_image,
719 struct efi_device_path *file_path,
720 void *source_buffer,
721 unsigned long source_size,
722 efi_handle_t *image_handle)
723{
724 static struct efi_object loaded_image_info_obj = {
725 .protocols = {
726 {
727 .guid = &efi_guid_loaded_image,
bee91169
AG
728 },
729 },
730 };
731 struct efi_loaded_image *info;
732 struct efi_object *obj;
733
734 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
735 file_path, source_buffer, source_size, image_handle);
736 info = malloc(sizeof(*info));
b5349f74 737 loaded_image_info_obj.protocols[0].protocol_interface = info;
bee91169
AG
738 obj = malloc(sizeof(loaded_image_info_obj));
739 memset(info, 0, sizeof(*info));
740 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
741 obj->handle = info;
742 info->file_path = file_path;
743 info->reserved = efi_load_pe(source_buffer, info);
744 if (!info->reserved) {
745 free(info);
746 free(obj);
747 return EFI_EXIT(EFI_UNSUPPORTED);
748 }
749
750 *image_handle = info;
751 list_add_tail(&obj->link, &efi_obj_list);
752
753 return EFI_EXIT(EFI_SUCCESS);
754}
755
756static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
757 unsigned long *exit_data_size,
758 s16 **exit_data)
759{
760 ulong (*entry)(void *image_handle, struct efi_system_table *st);
761 struct efi_loaded_image *info = image_handle;
762
763 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
764 entry = info->reserved;
765
766 efi_is_direct_boot = false;
767
768 /* call the image! */
a86aeaf2
AG
769 if (setjmp(&info->exit_jmp)) {
770 /* We returned from the child image */
771 return EFI_EXIT(info->exit_status);
772 }
773
af65db85 774 __efi_nesting_dec();
c160d2f5 775 __efi_exit_check();
bee91169 776 entry(image_handle, &systab);
c160d2f5 777 __efi_entry_check();
af65db85 778 __efi_nesting_inc();
bee91169
AG
779
780 /* Should usually never get here */
781 return EFI_EXIT(EFI_SUCCESS);
782}
783
a86aeaf2
AG
784static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
785 efi_status_t exit_status, unsigned long exit_data_size,
786 int16_t *exit_data)
bee91169 787{
a86aeaf2
AG
788 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
789
bee91169
AG
790 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
791 exit_data_size, exit_data);
a86aeaf2
AG
792
793 loaded_image_info->exit_status = exit_status;
692fcdd8 794 longjmp(&loaded_image_info->exit_jmp, 1);
a86aeaf2
AG
795
796 panic("EFI application exited");
bee91169
AG
797}
798
799static struct efi_object *efi_search_obj(void *handle)
800{
801 struct list_head *lhandle;
802
803 list_for_each(lhandle, &efi_obj_list) {
804 struct efi_object *efiobj;
805 efiobj = list_entry(lhandle, struct efi_object, link);
806 if (efiobj->handle == handle)
807 return efiobj;
808 }
809
810 return NULL;
811}
812
813static efi_status_t EFIAPI efi_unload_image(void *image_handle)
814{
815 struct efi_object *efiobj;
816
817 EFI_ENTRY("%p", image_handle);
818 efiobj = efi_search_obj(image_handle);
819 if (efiobj)
820 list_del(&efiobj->link);
821
822 return EFI_EXIT(EFI_SUCCESS);
823}
824
825static void efi_exit_caches(void)
826{
827#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
828 /*
829 * Grub on 32bit ARM needs to have caches disabled before jumping into
830 * a zImage, but does not know of all cache layers. Give it a hand.
831 */
832 if (efi_is_direct_boot)
833 cleanup_before_linux();
834#endif
835}
836
837static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
838 unsigned long map_key)
839{
840 EFI_ENTRY("%p, %ld", image_handle, map_key);
841
b7b8410a
AG
842 board_quiesce_devices();
843
bee91169
AG
844 /* Fix up caches for EFI payloads if necessary */
845 efi_exit_caches();
846
847 /* This stops all lingering devices */
848 bootm_disable_interrupts();
849
850 /* Give the payload some time to boot */
851 WATCHDOG_RESET();
852
853 return EFI_EXIT(EFI_SUCCESS);
854}
855
856static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
857{
858 static uint64_t mono = 0;
859 EFI_ENTRY("%p", count);
860 *count = mono++;
861 return EFI_EXIT(EFI_SUCCESS);
862}
863
864static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
865{
866 EFI_ENTRY("%ld", microseconds);
867 udelay(microseconds);
868 return EFI_EXIT(EFI_SUCCESS);
869}
870
871static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
872 uint64_t watchdog_code,
873 unsigned long data_size,
874 uint16_t *watchdog_data)
875{
876 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
877 data_size, watchdog_data);
b5104821 878 return efi_unsupported(__func__);
bee91169
AG
879}
880
881static efi_status_t EFIAPI efi_connect_controller(
882 efi_handle_t controller_handle,
883 efi_handle_t *driver_image_handle,
884 struct efi_device_path *remain_device_path,
885 bool recursive)
886{
887 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
888 remain_device_path, recursive);
889 return EFI_EXIT(EFI_NOT_FOUND);
890}
891
892static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
893 void *driver_image_handle,
894 void *child_handle)
895{
896 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
897 child_handle);
898 return EFI_EXIT(EFI_INVALID_PARAMETER);
899}
900
901static efi_status_t EFIAPI efi_close_protocol(void *handle,
902 efi_guid_t *protocol,
903 void *agent_handle,
904 void *controller_handle)
905{
906 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
907 controller_handle);
908 return EFI_EXIT(EFI_NOT_FOUND);
909}
910
911static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
912 efi_guid_t *protocol,
913 struct efi_open_protocol_info_entry **entry_buffer,
914 unsigned long *entry_count)
915{
916 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
917 entry_count);
918 return EFI_EXIT(EFI_NOT_FOUND);
919}
920
921static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
922 efi_guid_t ***protocol_buffer,
923 unsigned long *protocol_buffer_count)
924{
c0ebfc86 925 unsigned long buffer_size;
926 struct efi_object *efiobj;
927 unsigned long i, j;
928 struct list_head *lhandle;
929 efi_status_t r;
930
bee91169
AG
931 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
932 protocol_buffer_count);
c0ebfc86 933
934 if (!handle || !protocol_buffer || !protocol_buffer_count)
935 return EFI_EXIT(EFI_INVALID_PARAMETER);
936
937 *protocol_buffer = NULL;
661c8327 938 *protocol_buffer_count = 0;
c0ebfc86 939 list_for_each(lhandle, &efi_obj_list) {
940 efiobj = list_entry(lhandle, struct efi_object, link);
941
942 if (efiobj->handle != handle)
943 continue;
944
945 /* Count protocols */
946 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
947 if (efiobj->protocols[i].guid)
948 ++*protocol_buffer_count;
949 }
950 /* Copy guids */
951 if (*protocol_buffer_count) {
952 buffer_size = sizeof(efi_guid_t *) *
953 *protocol_buffer_count;
954 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
955 buffer_size,
956 (void **)protocol_buffer);
957 if (r != EFI_SUCCESS)
958 return EFI_EXIT(r);
959 j = 0;
960 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
961 if (efiobj->protocols[i].guid) {
962 (*protocol_buffer)[j] = (void *)
963 efiobj->protocols[i].guid;
964 ++j;
965 }
966 }
967 }
968 break;
969 }
970
971 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
972}
973
974static efi_status_t EFIAPI efi_locate_handle_buffer(
975 enum efi_locate_search_type search_type,
976 efi_guid_t *protocol, void *search_key,
977 unsigned long *no_handles, efi_handle_t **buffer)
978{
c2e703f9 979 efi_status_t r;
980 unsigned long buffer_size = 0;
981
bee91169
AG
982 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
983 no_handles, buffer);
c2e703f9 984
985 if (!no_handles || !buffer) {
986 r = EFI_INVALID_PARAMETER;
987 goto out;
988 }
989 *no_handles = 0;
990 *buffer = NULL;
991 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
992 *buffer);
993 if (r != EFI_BUFFER_TOO_SMALL)
994 goto out;
995 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
996 (void **)buffer);
997 if (r != EFI_SUCCESS)
998 goto out;
999 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1000 *buffer);
1001 if (r == EFI_SUCCESS)
1002 *no_handles = buffer_size / sizeof(void *);
1003out:
1004 return EFI_EXIT(r);
bee91169
AG
1005}
1006
bee91169
AG
1007static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1008 void *registration,
1009 void **protocol_interface)
1010{
88adae5e 1011 struct list_head *lhandle;
bee91169
AG
1012 int i;
1013
1014 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
88adae5e 1015
1016 if (!protocol || !protocol_interface)
1017 return EFI_EXIT(EFI_INVALID_PARAMETER);
1018
1019 list_for_each(lhandle, &efi_obj_list) {
1020 struct efi_object *efiobj;
1021
1022 efiobj = list_entry(lhandle, struct efi_object, link);
1023 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1024 struct efi_handler *handler = &efiobj->protocols[i];
1025
1026 if (!handler->guid)
1027 continue;
1028 if (!guidcmp(handler->guid, protocol)) {
1029 *protocol_interface =
1030 handler->protocol_interface;
1031 return EFI_EXIT(EFI_SUCCESS);
1032 }
bee91169
AG
1033 }
1034 }
88adae5e 1035 *protocol_interface = NULL;
bee91169
AG
1036
1037 return EFI_EXIT(EFI_NOT_FOUND);
1038}
1039
1040static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1041 void **handle, ...)
1042{
1043 EFI_ENTRY("%p", handle);
58b83586 1044
1045 va_list argptr;
1046 efi_guid_t *protocol;
1047 void *protocol_interface;
1048 efi_status_t r = EFI_SUCCESS;
1049 int i = 0;
1050
1051 if (!handle)
1052 return EFI_EXIT(EFI_INVALID_PARAMETER);
1053
1054 va_start(argptr, handle);
1055 for (;;) {
1056 protocol = va_arg(argptr, efi_guid_t*);
1057 if (!protocol)
1058 break;
1059 protocol_interface = va_arg(argptr, void*);
1060 r = efi_install_protocol_interface(handle, protocol,
1061 EFI_NATIVE_INTERFACE,
1062 protocol_interface);
1063 if (r != EFI_SUCCESS)
1064 break;
1065 i++;
1066 }
1067 va_end(argptr);
1068 if (r == EFI_SUCCESS)
1069 return EFI_EXIT(r);
1070
1071 /* If an error occured undo all changes. */
1072 va_start(argptr, handle);
1073 for (; i; --i) {
1074 protocol = va_arg(argptr, efi_guid_t*);
1075 protocol_interface = va_arg(argptr, void*);
1076 efi_uninstall_protocol_interface(handle, protocol,
1077 protocol_interface);
1078 }
1079 va_end(argptr);
1080
1081 return EFI_EXIT(r);
bee91169
AG
1082}
1083
1084static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1085 void *handle, ...)
1086{
1087 EFI_ENTRY("%p", handle);
1088 return EFI_EXIT(EFI_INVALID_PARAMETER);
1089}
1090
1091static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1092 unsigned long data_size,
1093 uint32_t *crc32_p)
1094{
1095 EFI_ENTRY("%p, %ld", data, data_size);
1096 *crc32_p = crc32(0, data, data_size);
1097 return EFI_EXIT(EFI_SUCCESS);
1098}
1099
1100static void EFIAPI efi_copy_mem(void *destination, void *source,
1101 unsigned long length)
1102{
1103 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1104 memcpy(destination, source, length);
1105}
1106
1107static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1108{
1109 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1110 memset(buffer, value, size);
1111}
1112
1113static efi_status_t EFIAPI efi_open_protocol(
1114 void *handle, efi_guid_t *protocol,
1115 void **protocol_interface, void *agent_handle,
1116 void *controller_handle, uint32_t attributes)
1117{
1118 struct list_head *lhandle;
1119 int i;
69baec67 1120 efi_status_t r = EFI_INVALID_PARAMETER;
bee91169
AG
1121
1122 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1123 protocol_interface, agent_handle, controller_handle,
1124 attributes);
b5349f74 1125
69baec67 1126 if (!handle || !protocol ||
1127 (!protocol_interface && attributes !=
1128 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1129 goto out;
1130 }
1131
1132 switch (attributes) {
1133 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1134 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1135 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1136 break;
1137 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1138 if (controller_handle == handle)
1139 goto out;
1140 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1141 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1142 if (controller_handle == NULL)
1143 goto out;
1144 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1145 if (agent_handle == NULL)
1146 goto out;
1147 break;
1148 default:
b5349f74 1149 goto out;
1150 }
1151
bee91169
AG
1152 list_for_each(lhandle, &efi_obj_list) {
1153 struct efi_object *efiobj;
1154 efiobj = list_entry(lhandle, struct efi_object, link);
1155
1156 if (efiobj->handle != handle)
1157 continue;
1158
1159 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1160 struct efi_handler *handler = &efiobj->protocols[i];
1161 const efi_guid_t *hprotocol = handler->guid;
1162 if (!hprotocol)
4b6ed0d7 1163 continue;
bee91169 1164 if (!guidcmp(hprotocol, protocol)) {
b5349f74 1165 if (attributes !=
1166 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1167 *protocol_interface =
1168 handler->protocol_interface;
1169 }
1170 r = EFI_SUCCESS;
bee91169
AG
1171 goto out;
1172 }
1173 }
69baec67 1174 goto unsupported;
bee91169
AG
1175 }
1176
69baec67 1177unsupported:
1178 r = EFI_UNSUPPORTED;
bee91169
AG
1179out:
1180 return EFI_EXIT(r);
1181}
1182
1183static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1184 efi_guid_t *protocol,
1185 void **protocol_interface)
1186{
8e1d329f 1187 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1188 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
bee91169
AG
1189}
1190
1191static const struct efi_boot_services efi_boot_services = {
1192 .hdr = {
1193 .headersize = sizeof(struct efi_table_hdr),
1194 },
1195 .raise_tpl = efi_raise_tpl,
1196 .restore_tpl = efi_restore_tpl,
1197 .allocate_pages = efi_allocate_pages_ext,
1198 .free_pages = efi_free_pages_ext,
1199 .get_memory_map = efi_get_memory_map_ext,
ead1274b 1200 .allocate_pool = efi_allocate_pool_ext,
42417bc8 1201 .free_pool = efi_free_pool_ext,
49deb455 1202 .create_event = efi_create_event_ext,
bfc72462 1203 .set_timer = efi_set_timer_ext,
bee91169 1204 .wait_for_event = efi_wait_for_event,
c6841592 1205 .signal_event = efi_signal_event_ext,
bee91169
AG
1206 .close_event = efi_close_event,
1207 .check_event = efi_check_event,
8bee5a3c 1208 .install_protocol_interface = efi_install_protocol_interface_ext,
bee91169 1209 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3d8e1456 1210 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
bee91169
AG
1211 .handle_protocol = efi_handle_protocol,
1212 .reserved = NULL,
1213 .register_protocol_notify = efi_register_protocol_notify,
26329584 1214 .locate_handle = efi_locate_handle_ext,
bee91169 1215 .locate_device_path = efi_locate_device_path,
488bf12d 1216 .install_configuration_table = efi_install_configuration_table_ext,
bee91169
AG
1217 .load_image = efi_load_image,
1218 .start_image = efi_start_image,
a86aeaf2 1219 .exit = efi_exit,
bee91169
AG
1220 .unload_image = efi_unload_image,
1221 .exit_boot_services = efi_exit_boot_services,
1222 .get_next_monotonic_count = efi_get_next_monotonic_count,
1223 .stall = efi_stall,
1224 .set_watchdog_timer = efi_set_watchdog_timer,
1225 .connect_controller = efi_connect_controller,
1226 .disconnect_controller = efi_disconnect_controller,
1227 .open_protocol = efi_open_protocol,
1228 .close_protocol = efi_close_protocol,
1229 .open_protocol_information = efi_open_protocol_information,
1230 .protocols_per_handle = efi_protocols_per_handle,
1231 .locate_handle_buffer = efi_locate_handle_buffer,
1232 .locate_protocol = efi_locate_protocol,
1233 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1234 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1235 .calculate_crc32 = efi_calculate_crc32,
1236 .copy_mem = efi_copy_mem,
1237 .set_mem = efi_set_mem,
1238};
1239
1240
3c63db9c 1241static uint16_t __efi_runtime_data firmware_vendor[] =
bee91169
AG
1242 { 'D','a','s',' ','U','-','b','o','o','t',0 };
1243
3c63db9c 1244struct efi_system_table __efi_runtime_data systab = {
bee91169
AG
1245 .hdr = {
1246 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1247 .revision = 0x20005, /* 2.5 */
1248 .headersize = sizeof(struct efi_table_hdr),
1249 },
1250 .fw_vendor = (long)firmware_vendor,
1251 .con_in = (void*)&efi_con_in,
1252 .con_out = (void*)&efi_con_out,
1253 .std_err = (void*)&efi_con_out,
1254 .runtime = (void*)&efi_runtime_services,
1255 .boottime = (void*)&efi_boot_services,
1256 .nr_tables = 0,
1257 .tables = (void*)efi_conf_table,
1258};