]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/efi_loader/efi_boottime.c
efi_loader: pass handle of loaded image
[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 9#include <common.h>
7d963323 10#include <div64.h>
bee91169 11#include <efi_loader.h>
ad644e7c 12#include <environment.h>
bee91169
AG
13#include <malloc.h>
14#include <asm/global_data.h>
15#include <libfdt_env.h>
16#include <u-boot/crc.h>
17#include <bootm.h>
18#include <inttypes.h>
19#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
32f4b2f8 23/* Task priority level */
152cade3 24static efi_uintn_t efi_tpl = TPL_APPLICATION;
32f4b2f8 25
bee91169
AG
26/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
29/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
37/*
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
41 *
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
44 */
3c63db9c 45static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
bee91169 46
65e4c0b1 47#ifdef CONFIG_ARM
bee91169
AG
48/*
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
54static volatile void *efi_gd, *app_gd;
65e4c0b1 55#endif
bee91169 56
c160d2f5 57static int entry_count;
af65db85 58static int nesting_level;
c160d2f5
RC
59
60/* Called on every callback entry */
61int __efi_entry_check(void)
62{
63 int ret = entry_count++ == 0;
64#ifdef CONFIG_ARM
65 assert(efi_gd);
66 app_gd = gd;
67 gd = efi_gd;
68#endif
69 return ret;
70}
71
72/* Called on every callback exit */
73int __efi_exit_check(void)
74{
75 int ret = --entry_count == 0;
76#ifdef CONFIG_ARM
77 gd = app_gd;
78#endif
79 return ret;
80}
81
bee91169
AG
82/* Called from do_bootefi_exec() */
83void efi_save_gd(void)
84{
65e4c0b1 85#ifdef CONFIG_ARM
bee91169 86 efi_gd = gd;
65e4c0b1 87#endif
bee91169
AG
88}
89
c160d2f5
RC
90/*
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
94 */
bee91169
AG
95void efi_restore_gd(void)
96{
65e4c0b1 97#ifdef CONFIG_ARM
bee91169
AG
98 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
bee91169 101 gd = efi_gd;
65e4c0b1 102#endif
bee91169
AG
103}
104
af65db85
RC
105/*
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
108 */
109static const char *indent_string(int level)
110{
111 const char *indent = " ";
112 const int max = strlen(indent);
113 level = min(max, level * 2);
114 return &indent[max - level];
115}
116
ae0bd3a9
HS
117const char *__efi_nesting(void)
118{
119 return indent_string(nesting_level);
120}
121
af65db85
RC
122const char *__efi_nesting_inc(void)
123{
124 return indent_string(nesting_level++);
125}
126
127const char *__efi_nesting_dec(void)
128{
129 return indent_string(--nesting_level);
130}
131
332468f7
HS
132/*
133 * Queue an EFI event.
134 *
135 * This function queues the notification function of the event for future
136 * execution.
137 *
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
140 *
141 * For the SignalEvent service see efi_signal_event_ext.
142 *
143 * @event event to signal
144 */
91be9a77 145void efi_signal_event(struct efi_event *event)
c6841592 146{
ca62a4f5 147 if (event->notify_function) {
e190e897 148 event->is_queued = true;
32f4b2f8
HS
149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
ea630ce9
HS
152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
c6841592 154 }
e190e897 155 event->is_queued = false;
c6841592 156}
157
332468f7
HS
158/*
159 * Raise the task priority level.
160 *
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
163 * for details.
164 *
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
167 */
152cade3 168static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
bee91169 169{
152cade3 170 efi_uintn_t old_tpl = efi_tpl;
32f4b2f8 171
503f2695 172 EFI_ENTRY("0x%zx", new_tpl);
32f4b2f8
HS
173
174 if (new_tpl < efi_tpl)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176 efi_tpl = new_tpl;
177 if (efi_tpl > TPL_HIGH_LEVEL)
178 efi_tpl = TPL_HIGH_LEVEL;
179
180 EFI_EXIT(EFI_SUCCESS);
181 return old_tpl;
bee91169
AG
182}
183
332468f7
HS
184/*
185 * Lower the task priority level.
186 *
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
189 * for details.
190 *
191 * @old_tpl value of the task priority level to be restored
192 */
152cade3 193static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
bee91169 194{
503f2695 195 EFI_ENTRY("0x%zx", old_tpl);
32f4b2f8
HS
196
197 if (old_tpl > efi_tpl)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199 efi_tpl = old_tpl;
200 if (efi_tpl > TPL_HIGH_LEVEL)
201 efi_tpl = TPL_HIGH_LEVEL;
202
203 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
204}
205
332468f7
HS
206/*
207 * Allocate memory pages.
208 *
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
211 * for details.
212 *
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
218 */
6e0bf8d8 219static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
f5a2a938 220 efi_uintn_t pages,
6e0bf8d8 221 uint64_t *memory)
bee91169
AG
222{
223 efi_status_t r;
224
f5a2a938 225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
bee91169
AG
226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228}
229
332468f7
HS
230/*
231 * Free memory pages.
232 *
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
235 * for details.
236 *
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
240 */
6e0bf8d8 241static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
f5a2a938 242 efi_uintn_t pages)
bee91169
AG
243{
244 efi_status_t r;
245
f5a2a938 246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
bee91169
AG
247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249}
250
332468f7
HS
251/*
252 * Get map describing memory usage.
253 *
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
256 * for details.
257 *
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
265 */
6e0bf8d8 266static efi_status_t EFIAPI efi_get_memory_map_ext(
f5a2a938 267 efi_uintn_t *memory_map_size,
6e0bf8d8 268 struct efi_mem_desc *memory_map,
f5a2a938
HS
269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
6e0bf8d8 271 uint32_t *descriptor_version)
bee91169
AG
272{
273 efi_status_t r;
274
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 map_key, descriptor_size, descriptor_version);
277 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 descriptor_size, descriptor_version);
279 return EFI_EXIT(r);
280}
281
332468f7
HS
282/*
283 * Allocate memory from pool.
284 *
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
287 * for details.
288 *
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
293 */
ead1274b 294static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
f5a2a938 295 efi_uintn_t size,
ead1274b 296 void **buffer)
bee91169 297{
1cd29f0a
AG
298 efi_status_t r;
299
f5a2a938 300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
ead1274b 301 r = efi_allocate_pool(pool_type, size, buffer);
1cd29f0a 302 return EFI_EXIT(r);
bee91169
AG
303}
304
332468f7
HS
305/*
306 * Free memory from pool.
307 *
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
310 * for details.
311 *
312 * @buffer start of memory to be freed
313 * @return status code
314 */
42417bc8 315static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
bee91169 316{
1cd29f0a
AG
317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
42417bc8 320 r = efi_free_pool(buffer);
1cd29f0a 321 return EFI_EXIT(r);
bee91169
AG
322}
323
2edab5e2
HS
324/*
325 * Create handle.
326 *
327 * @handle new handle
328 * @return status code
329 */
330efi_status_t efi_create_handle(void **handle)
3cc6e3fe
HS
331{
332 struct efi_object *obj;
333 efi_status_t r;
334
335 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
336 sizeof(struct efi_object),
337 (void **)&obj);
338 if (r != EFI_SUCCESS)
339 return r;
340 memset(obj, 0, sizeof(struct efi_object));
341 obj->handle = obj;
69fb6b1a 342 INIT_LIST_HEAD(&obj->protocols);
3cc6e3fe
HS
343 list_add_tail(&obj->link, &efi_obj_list);
344 *handle = obj;
345 return r;
346}
347
bee91169 348/*
c6841592 349 * Our event capabilities are very limited. Only a small limited
350 * number of events is allowed to coexist.
bee91169 351 */
c6841592 352static struct efi_event efi_events[16];
bee91169 353
332468f7
HS
354/*
355 * Create an event.
356 *
357 * This function is used inside U-Boot code to create an event.
358 *
359 * For the API function implementing the CreateEvent service see
360 * efi_create_event_ext.
361 *
362 * @type type of the event to create
363 * @notify_tpl task priority level of the event
364 * @notify_function notification function of the event
365 * @notify_context pointer passed to the notification function
366 * @event created event
367 * @return status code
368 */
152cade3 369efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
49deb455 370 void (EFIAPI *notify_function) (
2fd945fe 371 struct efi_event *event,
372 void *context),
49deb455 373 void *notify_context, struct efi_event **event)
bee91169 374{
c6841592 375 int i;
376
a95343b8 377 if (event == NULL)
49deb455 378 return EFI_INVALID_PARAMETER;
a95343b8
JG
379
380 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
49deb455 381 return EFI_INVALID_PARAMETER;
a95343b8
JG
382
383 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
384 notify_function == NULL)
49deb455 385 return EFI_INVALID_PARAMETER;
a95343b8 386
c6841592 387 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
388 if (efi_events[i].type)
389 continue;
390 efi_events[i].type = type;
391 efi_events[i].notify_tpl = notify_tpl;
392 efi_events[i].notify_function = notify_function;
393 efi_events[i].notify_context = notify_context;
394 /* Disable timers on bootup */
395 efi_events[i].trigger_next = -1ULL;
e190e897
HS
396 efi_events[i].is_queued = false;
397 efi_events[i].is_signaled = false;
c6841592 398 *event = &efi_events[i];
49deb455 399 return EFI_SUCCESS;
c6841592 400 }
49deb455 401 return EFI_OUT_OF_RESOURCES;
bee91169
AG
402}
403
332468f7
HS
404/*
405 * Create an event.
406 *
407 * This function implements the CreateEvent service.
408 * See the Unified Extensible Firmware Interface (UEFI) specification
409 * for details.
410 *
411 * @type type of the event to create
412 * @notify_tpl task priority level of the event
413 * @notify_function notification function of the event
414 * @notify_context pointer passed to the notification function
415 * @event created event
416 * @return status code
417 */
49deb455 418static efi_status_t EFIAPI efi_create_event_ext(
152cade3 419 uint32_t type, efi_uintn_t notify_tpl,
49deb455 420 void (EFIAPI *notify_function) (
421 struct efi_event *event,
422 void *context),
423 void *notify_context, struct efi_event **event)
424{
425 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
426 notify_context);
427 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
428 notify_context, event));
429}
430
431
bee91169 432/*
332468f7
HS
433 * Check if a timer event has occurred or a queued notification function should
434 * be called.
435 *
bee91169 436 * Our timers have to work without interrupts, so we check whenever keyboard
332468f7 437 * input or disk accesses happen if enough time elapsed for them to fire.
bee91169
AG
438 */
439void efi_timer_check(void)
440{
c6841592 441 int i;
bee91169
AG
442 u64 now = timer_get_us();
443
c6841592 444 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
ca62a4f5
HS
445 if (!efi_events[i].type)
446 continue;
e190e897 447 if (efi_events[i].is_queued)
ca62a4f5
HS
448 efi_signal_event(&efi_events[i]);
449 if (!(efi_events[i].type & EVT_TIMER) ||
c6841592 450 now < efi_events[i].trigger_next)
451 continue;
ca62a4f5
HS
452 switch (efi_events[i].trigger_type) {
453 case EFI_TIMER_RELATIVE:
454 efi_events[i].trigger_type = EFI_TIMER_STOP;
455 break;
456 case EFI_TIMER_PERIODIC:
c6841592 457 efi_events[i].trigger_next +=
8787b02e 458 efi_events[i].trigger_time;
ca62a4f5
HS
459 break;
460 default:
461 continue;
c6841592 462 }
e190e897 463 efi_events[i].is_signaled = true;
c6841592 464 efi_signal_event(&efi_events[i]);
bee91169 465 }
bee91169
AG
466 WATCHDOG_RESET();
467}
468
332468f7
HS
469/*
470 * Set the trigger time for a timer event or stop the event.
471 *
472 * This is the function for internal usage in U-Boot. For the API function
473 * implementing the SetTimer service see efi_set_timer_ext.
474 *
475 * @event event for which the timer is set
476 * @type type of the timer
477 * @trigger_time trigger period in multiples of 100ns
478 * @return status code
479 */
b521d29e 480efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
bfc72462 481 uint64_t trigger_time)
bee91169 482{
c6841592 483 int i;
bee91169 484
8787b02e 485 /*
486 * The parameter defines a multiple of 100ns.
487 * We use multiples of 1000ns. So divide by 10.
488 */
7d963323 489 do_div(trigger_time, 10);
bee91169 490
c6841592 491 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
492 if (event != &efi_events[i])
493 continue;
bee91169 494
c6841592 495 if (!(event->type & EVT_TIMER))
496 break;
497 switch (type) {
498 case EFI_TIMER_STOP:
499 event->trigger_next = -1ULL;
500 break;
501 case EFI_TIMER_PERIODIC:
502 case EFI_TIMER_RELATIVE:
503 event->trigger_next =
8787b02e 504 timer_get_us() + trigger_time;
c6841592 505 break;
506 default:
bfc72462 507 return EFI_INVALID_PARAMETER;
c6841592 508 }
509 event->trigger_type = type;
510 event->trigger_time = trigger_time;
e190e897 511 event->is_signaled = false;
bfc72462 512 return EFI_SUCCESS;
bee91169 513 }
bfc72462 514 return EFI_INVALID_PARAMETER;
515}
516
332468f7
HS
517/*
518 * Set the trigger time for a timer event or stop the event.
519 *
520 * This function implements the SetTimer service.
521 * See the Unified Extensible Firmware Interface (UEFI) specification
522 * for details.
523 *
524 * @event event for which the timer is set
525 * @type type of the timer
526 * @trigger_time trigger period in multiples of 100ns
527 * @return status code
528 */
b521d29e 529static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
530 enum efi_timer_delay type,
531 uint64_t trigger_time)
bfc72462 532{
533 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
534 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
bee91169
AG
535}
536
332468f7
HS
537/*
538 * Wait for events to be signaled.
539 *
540 * This function implements the WaitForEvent service.
541 * See the Unified Extensible Firmware Interface (UEFI) specification
542 * for details.
543 *
544 * @num_events number of events to be waited for
545 * @events events to be waited for
546 * @index index of the event that was signaled
547 * @return status code
548 */
f5a2a938 549static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
2fd945fe 550 struct efi_event **event,
f5a2a938 551 efi_uintn_t *index)
bee91169 552{
c6841592 553 int i, j;
bee91169 554
f5a2a938 555 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
bee91169 556
c6841592 557 /* Check parameters */
558 if (!num_events || !event)
559 return EFI_EXIT(EFI_INVALID_PARAMETER);
32f4b2f8
HS
560 /* Check TPL */
561 if (efi_tpl != TPL_APPLICATION)
562 return EFI_EXIT(EFI_UNSUPPORTED);
c6841592 563 for (i = 0; i < num_events; ++i) {
564 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
565 if (event[i] == &efi_events[j])
566 goto known_event;
567 }
568 return EFI_EXIT(EFI_INVALID_PARAMETER);
569known_event:
570 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
571 return EFI_EXIT(EFI_INVALID_PARAMETER);
e190e897 572 if (!event[i]->is_signaled)
ca62a4f5 573 efi_signal_event(event[i]);
c6841592 574 }
575
576 /* Wait for signal */
577 for (;;) {
578 for (i = 0; i < num_events; ++i) {
e190e897 579 if (event[i]->is_signaled)
c6841592 580 goto out;
581 }
582 /* Allow events to occur. */
583 efi_timer_check();
584 }
585
586out:
587 /*
588 * Reset the signal which is passed to the caller to allow periodic
589 * events to occur.
590 */
e190e897 591 event[i]->is_signaled = false;
c6841592 592 if (index)
593 *index = i;
bee91169
AG
594
595 return EFI_EXIT(EFI_SUCCESS);
596}
597
332468f7
HS
598/*
599 * Signal an EFI event.
600 *
601 * This function implements the SignalEvent service.
602 * See the Unified Extensible Firmware Interface (UEFI) specification
603 * for details.
604 *
605 * This functions sets the signaled state of the event and queues the
606 * notification function for execution.
607 *
608 * @event event to signal
10a08c4e 609 * @return status code
332468f7 610 */
c6841592 611static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
bee91169 612{
c6841592 613 int i;
614
bee91169 615 EFI_ENTRY("%p", event);
c6841592 616 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
617 if (event != &efi_events[i])
618 continue;
e190e897 619 if (event->is_signaled)
ca62a4f5 620 break;
e190e897 621 event->is_signaled = true;
ca62a4f5
HS
622 if (event->type & EVT_NOTIFY_SIGNAL)
623 efi_signal_event(event);
c6841592 624 break;
625 }
bee91169
AG
626 return EFI_EXIT(EFI_SUCCESS);
627}
628
332468f7
HS
629/*
630 * Close an EFI event.
631 *
632 * This function implements the CloseEvent service.
633 * See the Unified Extensible Firmware Interface (UEFI) specification
634 * for details.
635 *
636 * @event event to close
637 * @return status code
638 */
2fd945fe 639static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
bee91169 640{
c6841592 641 int i;
642
bee91169 643 EFI_ENTRY("%p", event);
c6841592 644 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
645 if (event == &efi_events[i]) {
646 event->type = 0;
647 event->trigger_next = -1ULL;
e190e897
HS
648 event->is_queued = false;
649 event->is_signaled = false;
c6841592 650 return EFI_EXIT(EFI_SUCCESS);
651 }
652 }
653 return EFI_EXIT(EFI_INVALID_PARAMETER);
bee91169
AG
654}
655
332468f7
HS
656/*
657 * Check if an event is signaled.
658 *
659 * This function implements the CheckEvent service.
660 * See the Unified Extensible Firmware Interface (UEFI) specification
661 * for details.
662 *
663 * If an event is not signaled yet the notification function is queued.
664 *
665 * @event event to check
666 * @return status code
667 */
2fd945fe 668static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
bee91169 669{
c6841592 670 int i;
671
bee91169 672 EFI_ENTRY("%p", event);
c6841592 673 efi_timer_check();
674 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
675 if (event != &efi_events[i])
676 continue;
677 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
678 break;
e190e897 679 if (!event->is_signaled)
ca62a4f5 680 efi_signal_event(event);
e190e897 681 if (event->is_signaled)
c6841592 682 return EFI_EXIT(EFI_SUCCESS);
683 return EFI_EXIT(EFI_NOT_READY);
684 }
685 return EFI_EXIT(EFI_INVALID_PARAMETER);
bee91169
AG
686}
687
7b9f8ad7
HS
688/*
689 * Find the internal EFI object for a handle.
690 *
691 * @handle handle to find
692 * @return EFI object
693 */
085d07cd 694struct efi_object *efi_search_obj(const void *handle)
7b9f8ad7 695{
1b68153a 696 struct efi_object *efiobj;
7b9f8ad7 697
1b68153a 698 list_for_each_entry(efiobj, &efi_obj_list, link) {
7b9f8ad7
HS
699 if (efiobj->handle == handle)
700 return efiobj;
701 }
702
703 return NULL;
704}
705
3f79a2b5
HS
706/*
707 * Find a protocol on a handle.
708 *
709 * @handle handle
710 * @protocol_guid GUID of the protocol
711 * @handler reference to the protocol
712 * @return status code
713 */
714efi_status_t efi_search_protocol(const void *handle,
715 const efi_guid_t *protocol_guid,
716 struct efi_handler **handler)
717{
718 struct efi_object *efiobj;
69fb6b1a 719 struct list_head *lhandle;
3f79a2b5
HS
720
721 if (!handle || !protocol_guid)
722 return EFI_INVALID_PARAMETER;
723 efiobj = efi_search_obj(handle);
724 if (!efiobj)
725 return EFI_INVALID_PARAMETER;
69fb6b1a
HS
726 list_for_each(lhandle, &efiobj->protocols) {
727 struct efi_handler *protocol;
728
729 protocol = list_entry(lhandle, struct efi_handler, link);
3f79a2b5
HS
730 if (!guidcmp(protocol->guid, protocol_guid)) {
731 if (handler)
732 *handler = protocol;
733 return EFI_SUCCESS;
734 }
735 }
736 return EFI_NOT_FOUND;
737}
738
739/*
740 * Install new protocol on a handle.
741 *
742 * @handle handle on which the protocol shall be installed
743 * @protocol GUID of the protocol to be installed
744 * @protocol_interface interface of the protocol implementation
745 * @return status code
746 */
747efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
748 void *protocol_interface)
749{
750 struct efi_object *efiobj;
751 struct efi_handler *handler;
752 efi_status_t ret;
3f79a2b5
HS
753
754 efiobj = efi_search_obj(handle);
755 if (!efiobj)
756 return EFI_INVALID_PARAMETER;
757 ret = efi_search_protocol(handle, protocol, NULL);
758 if (ret != EFI_NOT_FOUND)
759 return EFI_INVALID_PARAMETER;
760 handler = calloc(1, sizeof(struct efi_handler));
761 if (!handler)
762 return EFI_OUT_OF_RESOURCES;
69fb6b1a
HS
763 handler->guid = protocol;
764 handler->protocol_interface = protocol_interface;
765 list_add_tail(&handler->link, &efiobj->protocols);
766 return EFI_SUCCESS;
3f79a2b5
HS
767}
768
769/*
770 * Delete protocol from a handle.
771 *
772 * @handle handle from which the protocol shall be deleted
773 * @protocol GUID of the protocol to be deleted
774 * @protocol_interface interface of the protocol implementation
775 * @return status code
776 */
777efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
778 void *protocol_interface)
779{
780 struct efi_handler *handler;
781 efi_status_t ret;
782
783 ret = efi_search_protocol(handle, protocol, &handler);
784 if (ret != EFI_SUCCESS)
785 return ret;
69fb6b1a
HS
786 if (guidcmp(handler->guid, protocol))
787 return EFI_INVALID_PARAMETER;
788 list_del(&handler->link);
789 free(handler);
3f79a2b5
HS
790 return EFI_SUCCESS;
791}
792
793/*
794 * Delete all protocols from a handle.
795 *
796 * @handle handle from which the protocols shall be deleted
797 * @return status code
798 */
799efi_status_t efi_remove_all_protocols(const void *handle)
800{
801 struct efi_object *efiobj;
69fb6b1a
HS
802 struct list_head *lhandle;
803 struct list_head *pos;
3f79a2b5
HS
804
805 efiobj = efi_search_obj(handle);
806 if (!efiobj)
807 return EFI_INVALID_PARAMETER;
69fb6b1a
HS
808 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
809 struct efi_handler *protocol;
810 efi_status_t ret;
3f79a2b5 811
69fb6b1a
HS
812 protocol = list_entry(lhandle, struct efi_handler, link);
813
814 ret = efi_remove_protocol(handle, protocol->guid,
815 protocol->protocol_interface);
816 if (ret != EFI_SUCCESS)
817 return ret;
3f79a2b5
HS
818 }
819 return EFI_SUCCESS;
820}
821
332468f7
HS
822/*
823 * Install protocol interface.
824 *
1760ef57
HS
825 * This function implements the InstallProtocolInterface service.
826 * See the Unified Extensible Firmware Interface (UEFI) specification
827 * for details.
332468f7
HS
828 *
829 * @handle handle on which the protocol shall be installed
830 * @protocol GUID of the protocol to be installed
831 * @protocol_interface_type type of the interface to be installed,
832 * always EFI_NATIVE_INTERFACE
833 * @protocol_interface interface of the protocol implementation
834 * @return status code
835 */
1760ef57
HS
836static efi_status_t EFIAPI efi_install_protocol_interface(
837 void **handle, const efi_guid_t *protocol,
838 int protocol_interface_type, void *protocol_interface)
bee91169 839{
e0549f8a 840 efi_status_t r;
841
1760ef57
HS
842 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
843 protocol_interface);
844
e0549f8a 845 if (!handle || !protocol ||
846 protocol_interface_type != EFI_NATIVE_INTERFACE) {
847 r = EFI_INVALID_PARAMETER;
848 goto out;
849 }
850
851 /* Create new handle if requested. */
852 if (!*handle) {
3cc6e3fe
HS
853 r = efi_create_handle(handle);
854 if (r != EFI_SUCCESS)
855 goto out;
af1408e0
HS
856 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
857 *handle);
858 } else {
859 debug("%sEFI: handle %p\n", indent_string(nesting_level),
860 *handle);
e0549f8a 861 }
1202530d
HS
862 /* Add new protocol */
863 r = efi_add_protocol(*handle, protocol, protocol_interface);
e0549f8a 864out:
1760ef57 865 return EFI_EXIT(r);
bee91169 866}
e0549f8a 867
332468f7
HS
868/*
869 * Reinstall protocol interface.
870 *
871 * This function implements the ReinstallProtocolInterface service.
872 * See the Unified Extensible Firmware Interface (UEFI) specification
873 * for details.
874 *
875 * @handle handle on which the protocol shall be
876 * reinstalled
877 * @protocol GUID of the protocol to be installed
878 * @old_interface interface to be removed
879 * @new_interface interface to be installed
880 * @return status code
881 */
bee91169 882static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
5a9682d0 883 const efi_guid_t *protocol, void *old_interface,
bee91169
AG
884 void *new_interface)
885{
778e6af8 886 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
bee91169
AG
887 new_interface);
888 return EFI_EXIT(EFI_ACCESS_DENIED);
889}
890
332468f7
HS
891/*
892 * Uninstall protocol interface.
893 *
cd534083
HS
894 * This function implements the UninstallProtocolInterface service.
895 * See the Unified Extensible Firmware Interface (UEFI) specification
896 * for details.
332468f7
HS
897 *
898 * @handle handle from which the protocol shall be removed
899 * @protocol GUID of the protocol to be removed
900 * @protocol_interface interface to be removed
901 * @return status code
902 */
cd534083
HS
903static efi_status_t EFIAPI efi_uninstall_protocol_interface(
904 void *handle, const efi_guid_t *protocol,
905 void *protocol_interface)
bee91169 906{
5810511d
HS
907 struct efi_handler *handler;
908 efi_status_t r;
4b6ed0d7 909
cd534083
HS
910 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
911
4b6ed0d7 912 if (!handle || !protocol) {
913 r = EFI_INVALID_PARAMETER;
914 goto out;
915 }
916
5810511d
HS
917 /* Find the protocol on the handle */
918 r = efi_search_protocol(handle, protocol, &handler);
919 if (r != EFI_SUCCESS)
920 goto out;
921 if (handler->protocol_interface) {
922 /* TODO disconnect controllers */
923 r = EFI_ACCESS_DENIED;
924 } else {
925 r = efi_remove_protocol(handle, protocol, protocol_interface);
4b6ed0d7 926 }
4b6ed0d7 927out:
cd534083 928 return EFI_EXIT(r);
bee91169
AG
929}
930
332468f7
HS
931/*
932 * Register an event for notification when a protocol is installed.
933 *
934 * This function implements the RegisterProtocolNotify service.
935 * See the Unified Extensible Firmware Interface (UEFI) specification
936 * for details.
937 *
938 * @protocol GUID of the protocol whose installation shall be
939 * notified
940 * @event event to be signaled upon installation of the protocol
941 * @registration key for retrieving the registration information
942 * @return status code
943 */
5a9682d0
HS
944static efi_status_t EFIAPI efi_register_protocol_notify(
945 const efi_guid_t *protocol,
946 struct efi_event *event,
947 void **registration)
bee91169 948{
778e6af8 949 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
bee91169
AG
950 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
951}
952
332468f7
HS
953/*
954 * Determine if an EFI handle implements a protocol.
955 *
956 * See the documentation of the LocateHandle service in the UEFI specification.
957 *
958 * @search_type selection criterion
959 * @protocol GUID of the protocol
960 * @search_key registration key
961 * @efiobj handle
962 * @return 0 if the handle implements the protocol
963 */
bee91169 964static int efi_search(enum efi_locate_search_type search_type,
5a9682d0 965 const efi_guid_t *protocol, void *search_key,
bee91169
AG
966 struct efi_object *efiobj)
967{
42cf1242 968 efi_status_t ret;
bee91169
AG
969
970 switch (search_type) {
9f0770ff 971 case ALL_HANDLES:
bee91169 972 return 0;
9f0770ff 973 case BY_REGISTER_NOTIFY:
42cf1242 974 /* TODO: RegisterProtocolNotify is not implemented yet */
bee91169 975 return -1;
9f0770ff 976 case BY_PROTOCOL:
42cf1242
HS
977 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
978 return (ret != EFI_SUCCESS);
979 default:
980 /* Invalid search type */
bee91169
AG
981 return -1;
982 }
bee91169
AG
983}
984
332468f7
HS
985/*
986 * Locate handles implementing a protocol.
987 *
988 * This function is meant for U-Boot internal calls. For the API implementation
989 * of the LocateHandle service see efi_locate_handle_ext.
990 *
991 * @search_type selection criterion
992 * @protocol GUID of the protocol
993 * @search_key registration key
994 * @buffer_size size of the buffer to receive the handles in bytes
995 * @buffer buffer to receive the relevant handles
996 * @return status code
997 */
ebf199b9 998static efi_status_t efi_locate_handle(
bee91169 999 enum efi_locate_search_type search_type,
5a9682d0 1000 const efi_guid_t *protocol, void *search_key,
f5a2a938 1001 efi_uintn_t *buffer_size, efi_handle_t *buffer)
bee91169 1002{
caf864e4 1003 struct efi_object *efiobj;
f5a2a938 1004 efi_uintn_t size = 0;
bee91169 1005
caf864e4
HS
1006 /* Check parameters */
1007 switch (search_type) {
1008 case ALL_HANDLES:
1009 break;
1010 case BY_REGISTER_NOTIFY:
1011 if (!search_key)
1012 return EFI_INVALID_PARAMETER;
1013 /* RegisterProtocolNotify is not implemented yet */
1014 return EFI_UNSUPPORTED;
1015 case BY_PROTOCOL:
1016 if (!protocol)
1017 return EFI_INVALID_PARAMETER;
1018 break;
1019 default:
1020 return EFI_INVALID_PARAMETER;
1021 }
1022
1023 /*
1024 * efi_locate_handle_buffer uses this function for
1025 * the calculation of the necessary buffer size.
1026 * So do not require a buffer for buffersize == 0.
1027 */
1028 if (!buffer_size || (*buffer_size && !buffer))
1029 return EFI_INVALID_PARAMETER;
1030
bee91169 1031 /* Count how much space we need */
caf864e4
HS
1032 list_for_each_entry(efiobj, &efi_obj_list, link) {
1033 if (!efi_search(search_type, protocol, search_key, efiobj))
bee91169 1034 size += sizeof(void*);
bee91169
AG
1035 }
1036
1037 if (*buffer_size < size) {
1038 *buffer_size = size;
26329584 1039 return EFI_BUFFER_TOO_SMALL;
bee91169
AG
1040 }
1041
796a78cb
RC
1042 *buffer_size = size;
1043 if (size == 0)
1044 return EFI_NOT_FOUND;
1045
bee91169 1046 /* Then fill the array */
caf864e4
HS
1047 list_for_each_entry(efiobj, &efi_obj_list, link) {
1048 if (!efi_search(search_type, protocol, search_key, efiobj))
1049 *buffer++ = efiobj->handle;
bee91169
AG
1050 }
1051
26329584 1052 return EFI_SUCCESS;
1053}
1054
332468f7
HS
1055/*
1056 * Locate handles implementing a protocol.
1057 *
1058 * This function implements the LocateHandle service.
1059 * See the Unified Extensible Firmware Interface (UEFI) specification
1060 * for details.
1061 *
1062 * @search_type selection criterion
1063 * @protocol GUID of the protocol
1064 * @search_key registration key
1065 * @buffer_size size of the buffer to receive the handles in bytes
1066 * @buffer buffer to receive the relevant handles
1067 * @return 0 if the handle implements the protocol
1068 */
26329584 1069static efi_status_t EFIAPI efi_locate_handle_ext(
1070 enum efi_locate_search_type search_type,
5a9682d0 1071 const efi_guid_t *protocol, void *search_key,
f5a2a938 1072 efi_uintn_t *buffer_size, efi_handle_t *buffer)
26329584 1073{
778e6af8 1074 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
26329584 1075 buffer_size, buffer);
1076
1077 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1078 buffer_size, buffer));
bee91169
AG
1079}
1080
d98cdf6a
AG
1081/* Collapses configuration table entries, removing index i */
1082static void efi_remove_configuration_table(int i)
1083{
1084 struct efi_configuration_table *this = &efi_conf_table[i];
1085 struct efi_configuration_table *next = &efi_conf_table[i+1];
1086 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1087
1088 memmove(this, next, (ulong)end - (ulong)next);
1089 systab.nr_tables--;
1090}
1091
332468f7
HS
1092/*
1093 * Adds, updates, or removes a configuration table.
1094 *
1095 * This function is used for internal calls. For the API implementation of the
1096 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1097 *
1098 * @guid GUID of the installed table
1099 * @table table to be installed
1100 * @return status code
1101 */
488bf12d 1102efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
bee91169
AG
1103{
1104 int i;
1105
bee91169
AG
1106 /* Check for guid override */
1107 for (i = 0; i < systab.nr_tables; i++) {
1108 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
d98cdf6a
AG
1109 if (table)
1110 efi_conf_table[i].table = table;
1111 else
1112 efi_remove_configuration_table(i);
488bf12d 1113 return EFI_SUCCESS;
bee91169
AG
1114 }
1115 }
1116
d98cdf6a
AG
1117 if (!table)
1118 return EFI_NOT_FOUND;
1119
bee91169
AG
1120 /* No override, check for overflow */
1121 if (i >= ARRAY_SIZE(efi_conf_table))
488bf12d 1122 return EFI_OUT_OF_RESOURCES;
bee91169
AG
1123
1124 /* Add a new entry */
1125 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1126 efi_conf_table[i].table = table;
aba5e919 1127 systab.nr_tables = i + 1;
bee91169 1128
488bf12d
AG
1129 return EFI_SUCCESS;
1130}
1131
332468f7
HS
1132/*
1133 * Adds, updates, or removes a configuration table.
1134 *
1135 * This function implements the InstallConfigurationTable service.
1136 * See the Unified Extensible Firmware Interface (UEFI) specification
1137 * for details.
1138 *
1139 * @guid GUID of the installed table
1140 * @table table to be installed
1141 * @return status code
1142 */
488bf12d
AG
1143static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1144 void *table)
1145{
778e6af8 1146 EFI_ENTRY("%pUl, %p", guid, table);
488bf12d 1147 return EFI_EXIT(efi_install_configuration_table(guid, table));
bee91169
AG
1148}
1149
332468f7
HS
1150/*
1151 * Initialize a loaded_image_info + loaded_image_info object with correct
95c5553e 1152 * protocols, boot-device, etc.
332468f7 1153 *
10a08c4e 1154 * @info loaded image info to be passed to the entry point of the
332468f7
HS
1155 * image
1156 * @obj internal object associated with the loaded image
1157 * @device_path device path of the loaded image
1158 * @file_path file path of the loaded image
95c5553e
RC
1159 */
1160void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1161 struct efi_device_path *device_path,
1162 struct efi_device_path *file_path)
1163{
48b66230
HS
1164 efi_status_t ret;
1165
95c5553e
RC
1166 obj->handle = info;
1167
48b66230
HS
1168 info->file_path = file_path;
1169 if (device_path)
1170 info->device_handle = efi_dp_find_obj(device_path, NULL);
1171
69fb6b1a 1172 INIT_LIST_HEAD(&obj->protocols);
48b66230 1173 list_add_tail(&obj->link, &efi_obj_list);
95c5553e
RC
1174 /*
1175 * When asking for the device path interface, return
1176 * bootefi_device_path
1177 */
48b66230
HS
1178 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1179 if (ret != EFI_SUCCESS)
1180 goto failure;
95c5553e
RC
1181
1182 /*
1183 * When asking for the loaded_image interface, just
1184 * return handle which points to loaded_image_info
1185 */
48b66230
HS
1186 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1187 if (ret != EFI_SUCCESS)
1188 goto failure;
95c5553e 1189
48b66230
HS
1190 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1191 (void *)&efi_console_control);
1192 if (ret != EFI_SUCCESS)
1193 goto failure;
95c5553e 1194
48b66230
HS
1195 ret = efi_add_protocol(obj->handle,
1196 &efi_guid_device_path_to_text_protocol,
1197 (void *)&efi_device_path_to_text);
1198 if (ret != EFI_SUCCESS)
1199 goto failure;
95c5553e 1200
48b66230
HS
1201 return;
1202failure:
1203 printf("ERROR: Failure to install protocols for loaded image\n");
95c5553e
RC
1204}
1205
332468f7
HS
1206/*
1207 * Load an image using a file path.
1208 *
1209 * @file_path the path of the image to load
1210 * @buffer buffer containing the loaded image
10a08c4e 1211 * @return status code
332468f7 1212 */
9975fe96
RC
1213efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1214 void **buffer)
838ee4b4
RC
1215{
1216 struct efi_file_info *info = NULL;
1217 struct efi_file_handle *f;
1218 static efi_status_t ret;
1219 uint64_t bs;
1220
1221 f = efi_file_from_path(file_path);
1222 if (!f)
1223 return EFI_DEVICE_ERROR;
1224
1225 bs = 0;
1226 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1227 &bs, info));
1228 if (ret == EFI_BUFFER_TOO_SMALL) {
1229 info = malloc(bs);
1230 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1231 &bs, info));
1232 }
1233 if (ret != EFI_SUCCESS)
1234 goto error;
1235
1236 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1237 if (ret)
1238 goto error;
1239
1240 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1241
1242error:
1243 free(info);
1244 EFI_CALL(f->close(f));
1245
1246 if (ret != EFI_SUCCESS) {
1247 efi_free_pool(*buffer);
1248 *buffer = NULL;
1249 }
1250
1251 return ret;
1252}
1253
332468f7
HS
1254/*
1255 * Load an EFI image into memory.
1256 *
1257 * This function implements the LoadImage service.
1258 * See the Unified Extensible Firmware Interface (UEFI) specification
1259 * for details.
1260 *
1261 * @boot_policy true for request originating from the boot manager
1262 * @parent_image the calles's image handle
1263 * @file_path the path of the image to load
1264 * @source_buffer memory location from which the image is installed
1265 * @source_size size of the memory area from which the image is
1266 * installed
1267 * @image_handle handle for the newly installed image
1268 * @return status code
1269 */
bee91169
AG
1270static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1271 efi_handle_t parent_image,
1272 struct efi_device_path *file_path,
1273 void *source_buffer,
1274 unsigned long source_size,
1275 efi_handle_t *image_handle)
1276{
bee91169
AG
1277 struct efi_loaded_image *info;
1278 struct efi_object *obj;
1279
1280 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1281 file_path, source_buffer, source_size, image_handle);
838ee4b4
RC
1282
1283 info = calloc(1, sizeof(*info));
1284 obj = calloc(1, sizeof(*obj));
1285
1286 if (!source_buffer) {
1287 struct efi_device_path *dp, *fp;
1288 efi_status_t ret;
1289
9975fe96 1290 ret = efi_load_image_from_path(file_path, &source_buffer);
838ee4b4
RC
1291 if (ret != EFI_SUCCESS) {
1292 free(info);
1293 free(obj);
1294 return EFI_EXIT(ret);
1295 }
1296
1297 /*
1298 * split file_path which contains both the device and
1299 * file parts:
1300 */
1301 efi_dp_split_file_path(file_path, &dp, &fp);
1302
1303 efi_setup_loaded_image(info, obj, dp, fp);
1304 } else {
1305 /* In this case, file_path is the "device" path, ie.
1306 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1307 */
1308 efi_setup_loaded_image(info, obj, file_path, NULL);
1309 }
1310
bee91169
AG
1311 info->reserved = efi_load_pe(source_buffer, info);
1312 if (!info->reserved) {
1313 free(info);
1314 free(obj);
1315 return EFI_EXIT(EFI_UNSUPPORTED);
1316 }
1317
32fc2ac3
HS
1318 info->system_table = &systab;
1319 info->parent_handle = parent_image;
ea54ad59 1320 *image_handle = obj->handle;
bee91169
AG
1321
1322 return EFI_EXIT(EFI_SUCCESS);
1323}
1324
332468f7
HS
1325/*
1326 * Call the entry point of an image.
1327 *
1328 * This function implements the StartImage service.
1329 * See the Unified Extensible Firmware Interface (UEFI) specification
1330 * for details.
1331 *
1332 * @image_handle handle of the image
1333 * @exit_data_size size of the buffer
1334 * @exit_data buffer to receive the exit data of the called image
10a08c4e 1335 * @return status code
332468f7 1336 */
bee91169
AG
1337static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1338 unsigned long *exit_data_size,
1339 s16 **exit_data)
1340{
1341 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1342 struct efi_loaded_image *info = image_handle;
1343
1344 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1345 entry = info->reserved;
1346
1347 efi_is_direct_boot = false;
1348
1349 /* call the image! */
a86aeaf2
AG
1350 if (setjmp(&info->exit_jmp)) {
1351 /* We returned from the child image */
1352 return EFI_EXIT(info->exit_status);
1353 }
1354
af65db85 1355 __efi_nesting_dec();
c160d2f5 1356 __efi_exit_check();
bee91169 1357 entry(image_handle, &systab);
c160d2f5 1358 __efi_entry_check();
af65db85 1359 __efi_nesting_inc();
bee91169
AG
1360
1361 /* Should usually never get here */
1362 return EFI_EXIT(EFI_SUCCESS);
1363}
1364
332468f7
HS
1365/*
1366 * Leave an EFI application or driver.
1367 *
1368 * This function implements the Exit service.
1369 * See the Unified Extensible Firmware Interface (UEFI) specification
1370 * for details.
1371 *
1372 * @image_handle handle of the application or driver that is exiting
1373 * @exit_status status code
1374 * @exit_data_size size of the buffer in bytes
1375 * @exit_data buffer with data describing an error
10a08c4e 1376 * @return status code
332468f7 1377 */
a86aeaf2
AG
1378static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1379 efi_status_t exit_status, unsigned long exit_data_size,
1380 int16_t *exit_data)
bee91169 1381{
a86aeaf2
AG
1382 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1383
bee91169
AG
1384 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1385 exit_data_size, exit_data);
a86aeaf2 1386
a148920e 1387 /* Make sure entry/exit counts for EFI world cross-overs match */
da94073b
HS
1388 __efi_exit_check();
1389
a148920e
AG
1390 /*
1391 * But longjmp out with the U-Boot gd, not the application's, as
1392 * the other end is a setjmp call inside EFI context.
1393 */
1394 efi_restore_gd();
1395
a86aeaf2 1396 loaded_image_info->exit_status = exit_status;
692fcdd8 1397 longjmp(&loaded_image_info->exit_jmp, 1);
a86aeaf2
AG
1398
1399 panic("EFI application exited");
bee91169
AG
1400}
1401
332468f7
HS
1402/*
1403 * Unload an EFI image.
1404 *
1405 * This function implements the UnloadImage service.
1406 * See the Unified Extensible Firmware Interface (UEFI) specification
1407 * for details.
1408 *
1409 * @image_handle handle of the image to be unloaded
1410 * @return status code
1411 */
bee91169
AG
1412static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1413{
1414 struct efi_object *efiobj;
1415
1416 EFI_ENTRY("%p", image_handle);
1417 efiobj = efi_search_obj(image_handle);
1418 if (efiobj)
1419 list_del(&efiobj->link);
1420
1421 return EFI_EXIT(EFI_SUCCESS);
1422}
1423
332468f7
HS
1424/*
1425 * Fix up caches for EFI payloads if necessary.
1426 */
bee91169
AG
1427static void efi_exit_caches(void)
1428{
1429#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1430 /*
1431 * Grub on 32bit ARM needs to have caches disabled before jumping into
1432 * a zImage, but does not know of all cache layers. Give it a hand.
1433 */
1434 if (efi_is_direct_boot)
1435 cleanup_before_linux();
1436#endif
1437}
1438
332468f7
HS
1439/*
1440 * Stop boot services.
1441 *
1442 * This function implements the ExitBootServices service.
1443 * See the Unified Extensible Firmware Interface (UEFI) specification
1444 * for details.
1445 *
1446 * @image_handle handle of the loaded image
1447 * @map_key key of the memory map
1448 * @return status code
1449 */
bee91169
AG
1450static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1451 unsigned long map_key)
1452{
152a263c
HS
1453 int i;
1454
bee91169
AG
1455 EFI_ENTRY("%p, %ld", image_handle, map_key);
1456
152a263c
HS
1457 /* Notify that ExitBootServices is invoked. */
1458 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1459 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1460 continue;
1461 efi_signal_event(&efi_events[i]);
1462 }
1463 /* Make sure that notification functions are not called anymore */
1464 efi_tpl = TPL_HIGH_LEVEL;
1465
40583564 1466 /* XXX Should persist EFI variables here */
ad644e7c 1467
b7b8410a
AG
1468 board_quiesce_devices();
1469
bee91169
AG
1470 /* Fix up caches for EFI payloads if necessary */
1471 efi_exit_caches();
1472
1473 /* This stops all lingering devices */
1474 bootm_disable_interrupts();
1475
1476 /* Give the payload some time to boot */
b3d60900 1477 efi_set_watchdog(0);
bee91169
AG
1478 WATCHDOG_RESET();
1479
1480 return EFI_EXIT(EFI_SUCCESS);
1481}
1482
332468f7
HS
1483/*
1484 * Get next value of the counter.
1485 *
1486 * This function implements the NextMonotonicCount service.
1487 * See the Unified Extensible Firmware Interface (UEFI) specification
1488 * for details.
1489 *
1490 * @count returned value of the counter
1491 * @return status code
1492 */
bee91169
AG
1493static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1494{
1495 static uint64_t mono = 0;
1496 EFI_ENTRY("%p", count);
1497 *count = mono++;
1498 return EFI_EXIT(EFI_SUCCESS);
1499}
1500
332468f7
HS
1501/*
1502 * Sleep.
1503 *
1504 * This function implements the Stall sercive.
1505 * See the Unified Extensible Firmware Interface (UEFI) specification
1506 * for details.
1507 *
1508 * @microseconds period to sleep in microseconds
1509 * @return status code
1510 */
bee91169
AG
1511static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1512{
1513 EFI_ENTRY("%ld", microseconds);
1514 udelay(microseconds);
1515 return EFI_EXIT(EFI_SUCCESS);
1516}
1517
332468f7
HS
1518/*
1519 * Reset the watchdog timer.
1520 *
b3d60900 1521 * This function implements the SetWatchdogTimer service.
332468f7
HS
1522 * See the Unified Extensible Firmware Interface (UEFI) specification
1523 * for details.
1524 *
1525 * @timeout seconds before reset by watchdog
1526 * @watchdog_code code to be logged when resetting
1527 * @data_size size of buffer in bytes
1528 * @watchdog_data buffer with data describing the reset reason
10a08c4e 1529 * @return status code
332468f7 1530 */
bee91169
AG
1531static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1532 uint64_t watchdog_code,
1533 unsigned long data_size,
1534 uint16_t *watchdog_data)
1535{
1536 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1537 data_size, watchdog_data);
b3d60900 1538 return EFI_EXIT(efi_set_watchdog(timeout));
bee91169
AG
1539}
1540
332468f7
HS
1541/*
1542 * Connect a controller to a driver.
1543 *
1544 * This function implements the ConnectController service.
1545 * See the Unified Extensible Firmware Interface (UEFI) specification
1546 * for details.
1547 *
1548 * @controller_handle handle of the controller
1549 * @driver_image_handle handle of the driver
1550 * @remain_device_path device path of a child controller
1551 * @recursive true to connect all child controllers
1552 * @return status code
1553 */
bee91169
AG
1554static efi_status_t EFIAPI efi_connect_controller(
1555 efi_handle_t controller_handle,
1556 efi_handle_t *driver_image_handle,
1557 struct efi_device_path *remain_device_path,
1558 bool recursive)
1559{
1560 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1561 remain_device_path, recursive);
1562 return EFI_EXIT(EFI_NOT_FOUND);
1563}
1564
332468f7
HS
1565/*
1566 * Disconnect a controller from a driver.
1567 *
1568 * This function implements the DisconnectController service.
1569 * See the Unified Extensible Firmware Interface (UEFI) specification
1570 * for details.
1571 *
1572 * @controller_handle handle of the controller
1573 * @driver_image_handle handle of the driver
1574 * @child_handle handle of the child to destroy
1575 * @return status code
1576 */
bee91169
AG
1577static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1578 void *driver_image_handle,
1579 void *child_handle)
1580{
1581 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1582 child_handle);
1583 return EFI_EXIT(EFI_INVALID_PARAMETER);
1584}
1585
332468f7
HS
1586/*
1587 * Close a protocol.
1588 *
1589 * This function implements the CloseProtocol service.
1590 * See the Unified Extensible Firmware Interface (UEFI) specification
1591 * for details.
1592 *
1593 * @handle handle on which the protocol shall be closed
1594 * @protocol GUID of the protocol to close
1595 * @agent_handle handle of the driver
1596 * @controller_handle handle of the controller
1597 * @return status code
1598 */
bee91169 1599static efi_status_t EFIAPI efi_close_protocol(void *handle,
5a9682d0 1600 const efi_guid_t *protocol,
bee91169
AG
1601 void *agent_handle,
1602 void *controller_handle)
1603{
778e6af8 1604 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
bee91169
AG
1605 controller_handle);
1606 return EFI_EXIT(EFI_NOT_FOUND);
1607}
1608
332468f7
HS
1609/*
1610 * Provide information about then open status of a protocol on a handle
1611 *
1612 * This function implements the OpenProtocolInformation service.
1613 * See the Unified Extensible Firmware Interface (UEFI) specification
1614 * for details.
1615 *
1616 * @handle handle for which the information shall be retrieved
1617 * @protocol GUID of the protocol
1618 * @entry_buffer buffer to receive the open protocol information
1619 * @entry_count number of entries available in the buffer
1620 * @return status code
1621 */
bee91169 1622static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
5a9682d0 1623 const efi_guid_t *protocol,
bee91169 1624 struct efi_open_protocol_info_entry **entry_buffer,
f5a2a938 1625 efi_uintn_t *entry_count)
bee91169 1626{
778e6af8 1627 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
bee91169
AG
1628 entry_count);
1629 return EFI_EXIT(EFI_NOT_FOUND);
1630}
1631
332468f7
HS
1632/*
1633 * Get protocols installed on a handle.
1634 *
1635 * This function implements the ProtocolsPerHandleService.
1636 * See the Unified Extensible Firmware Interface (UEFI) specification
1637 * for details.
1638 *
1639 * @handle handle for which the information is retrieved
1640 * @protocol_buffer buffer with protocol GUIDs
1641 * @protocol_buffer_count number of entries in the buffer
10a08c4e 1642 * @return status code
332468f7 1643 */
bee91169
AG
1644static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1645 efi_guid_t ***protocol_buffer,
f5a2a938 1646 efi_uintn_t *protocol_buffer_count)
bee91169 1647{
c0ebfc86 1648 unsigned long buffer_size;
1649 struct efi_object *efiobj;
69fb6b1a 1650 struct list_head *protocol_handle;
c0ebfc86 1651 efi_status_t r;
1652
bee91169
AG
1653 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1654 protocol_buffer_count);
c0ebfc86 1655
1656 if (!handle || !protocol_buffer || !protocol_buffer_count)
1657 return EFI_EXIT(EFI_INVALID_PARAMETER);
1658
1659 *protocol_buffer = NULL;
661c8327 1660 *protocol_buffer_count = 0;
c0ebfc86 1661
69fb6b1a
HS
1662 efiobj = efi_search_obj(handle);
1663 if (!efiobj)
1664 return EFI_EXIT(EFI_INVALID_PARAMETER);
c0ebfc86 1665
69fb6b1a
HS
1666 /* Count protocols */
1667 list_for_each(protocol_handle, &efiobj->protocols) {
1668 ++*protocol_buffer_count;
1669 }
1670
1671 /* Copy guids */
1672 if (*protocol_buffer_count) {
1673 size_t j = 0;
1674
1675 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1676 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1677 (void **)protocol_buffer);
1678 if (r != EFI_SUCCESS)
1679 return EFI_EXIT(r);
1680 list_for_each(protocol_handle, &efiobj->protocols) {
1681 struct efi_handler *protocol;
1682
1683 protocol = list_entry(protocol_handle,
1684 struct efi_handler, link);
1685 (*protocol_buffer)[j] = (void *)protocol->guid;
1686 ++j;
c0ebfc86 1687 }
c0ebfc86 1688 }
1689
1690 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
1691}
1692
332468f7
HS
1693/*
1694 * Locate handles implementing a protocol.
1695 *
1696 * This function implements the LocateHandleBuffer service.
1697 * See the Unified Extensible Firmware Interface (UEFI) specification
1698 * for details.
1699 *
1700 * @search_type selection criterion
1701 * @protocol GUID of the protocol
1702 * @search_key registration key
1703 * @no_handles number of returned handles
1704 * @buffer buffer with the returned handles
1705 * @return status code
1706 */
bee91169
AG
1707static efi_status_t EFIAPI efi_locate_handle_buffer(
1708 enum efi_locate_search_type search_type,
5a9682d0 1709 const efi_guid_t *protocol, void *search_key,
f5a2a938 1710 efi_uintn_t *no_handles, efi_handle_t **buffer)
bee91169 1711{
c2e703f9 1712 efi_status_t r;
f5a2a938 1713 efi_uintn_t buffer_size = 0;
c2e703f9 1714
778e6af8 1715 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
bee91169 1716 no_handles, buffer);
c2e703f9 1717
1718 if (!no_handles || !buffer) {
1719 r = EFI_INVALID_PARAMETER;
1720 goto out;
1721 }
1722 *no_handles = 0;
1723 *buffer = NULL;
1724 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1725 *buffer);
1726 if (r != EFI_BUFFER_TOO_SMALL)
1727 goto out;
1728 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1729 (void **)buffer);
1730 if (r != EFI_SUCCESS)
1731 goto out;
1732 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1733 *buffer);
1734 if (r == EFI_SUCCESS)
1735 *no_handles = buffer_size / sizeof(void *);
1736out:
1737 return EFI_EXIT(r);
bee91169
AG
1738}
1739
332468f7
HS
1740/*
1741 * Find an interface implementing a protocol.
1742 *
1743 * This function implements the LocateProtocol service.
1744 * See the Unified Extensible Firmware Interface (UEFI) specification
1745 * for details.
1746 *
1747 * @protocol GUID of the protocol
1748 * @registration registration key passed to the notification function
1749 * @protocol_interface interface implementing the protocol
10a08c4e 1750 * @return status code
332468f7 1751 */
5a9682d0 1752static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
bee91169
AG
1753 void *registration,
1754 void **protocol_interface)
1755{
88adae5e 1756 struct list_head *lhandle;
9172cd91 1757 efi_status_t ret;
bee91169 1758
778e6af8 1759 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
88adae5e 1760
1761 if (!protocol || !protocol_interface)
1762 return EFI_EXIT(EFI_INVALID_PARAMETER);
1763
1764 list_for_each(lhandle, &efi_obj_list) {
1765 struct efi_object *efiobj;
9172cd91 1766 struct efi_handler *handler;
88adae5e 1767
1768 efiobj = list_entry(lhandle, struct efi_object, link);
9172cd91
HS
1769
1770 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1771 if (ret == EFI_SUCCESS) {
1772 *protocol_interface = handler->protocol_interface;
1773 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
1774 }
1775 }
88adae5e 1776 *protocol_interface = NULL;
bee91169
AG
1777
1778 return EFI_EXIT(EFI_NOT_FOUND);
1779}
1780
ae2c85c1
HS
1781/*
1782 * Get the device path and handle of an device implementing a protocol.
1783 *
1784 * This function implements the LocateDevicePath service.
1785 * See the Unified Extensible Firmware Interface (UEFI) specification
1786 * for details.
1787 *
1788 * @protocol GUID of the protocol
1789 * @device_path device path
1790 * @device handle of the device
1791 * @return status code
1792 */
1793static efi_status_t EFIAPI efi_locate_device_path(
1794 const efi_guid_t *protocol,
1795 struct efi_device_path **device_path,
1796 efi_handle_t *device)
1797{
1798 struct efi_device_path *dp;
1799 size_t i;
1800 struct efi_handler *handler;
1801 efi_handle_t *handles;
1802 size_t len, len_dp;
1803 size_t len_best = 0;
1804 efi_uintn_t no_handles;
1805 u8 *remainder;
1806 efi_status_t ret;
1807
1808 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1809
1810 if (!protocol || !device_path || !*device_path || !device) {
1811 ret = EFI_INVALID_PARAMETER;
1812 goto out;
1813 }
1814
1815 /* Find end of device path */
1816 len = efi_dp_size(*device_path);
1817
1818 /* Get all handles implementing the protocol */
1819 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1820 &no_handles, &handles));
1821 if (ret != EFI_SUCCESS)
1822 goto out;
1823
1824 for (i = 0; i < no_handles; ++i) {
1825 /* Find the device path protocol */
1826 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1827 &handler);
1828 if (ret != EFI_SUCCESS)
1829 continue;
1830 dp = (struct efi_device_path *)handler->protocol_interface;
1831 len_dp = efi_dp_size(dp);
1832 /*
1833 * This handle can only be a better fit
1834 * if its device path length is longer than the best fit and
1835 * if its device path length is shorter of equal the searched
1836 * device path.
1837 */
1838 if (len_dp <= len_best || len_dp > len)
1839 continue;
1840 /* Check if dp is a subpath of device_path */
1841 if (memcmp(*device_path, dp, len_dp))
1842 continue;
1843 *device = handles[i];
1844 len_best = len_dp;
1845 }
1846 if (len_best) {
1847 remainder = (u8 *)*device_path + len_best;
1848 *device_path = (struct efi_device_path *)remainder;
1849 ret = EFI_SUCCESS;
1850 } else {
1851 ret = EFI_NOT_FOUND;
1852 }
1853out:
1854 return EFI_EXIT(ret);
1855}
1856
332468f7
HS
1857/*
1858 * Install multiple protocol interfaces.
1859 *
1860 * This function implements the MultipleProtocolInterfaces service.
1861 * See the Unified Extensible Firmware Interface (UEFI) specification
1862 * for details.
1863 *
1864 * @handle handle on which the protocol interfaces shall be installed
1865 * @... NULL terminated argument list with pairs of protocol GUIDS and
1866 * interfaces
1867 * @return status code
1868 */
bee91169
AG
1869static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1870 void **handle, ...)
1871{
1872 EFI_ENTRY("%p", handle);
58b83586 1873
1874 va_list argptr;
5a9682d0 1875 const efi_guid_t *protocol;
58b83586 1876 void *protocol_interface;
1877 efi_status_t r = EFI_SUCCESS;
1878 int i = 0;
1879
1880 if (!handle)
1881 return EFI_EXIT(EFI_INVALID_PARAMETER);
1882
1883 va_start(argptr, handle);
1884 for (;;) {
1885 protocol = va_arg(argptr, efi_guid_t*);
1886 if (!protocol)
1887 break;
1888 protocol_interface = va_arg(argptr, void*);
1760ef57
HS
1889 r = EFI_CALL(efi_install_protocol_interface(
1890 handle, protocol,
1891 EFI_NATIVE_INTERFACE,
1892 protocol_interface));
58b83586 1893 if (r != EFI_SUCCESS)
1894 break;
1895 i++;
1896 }
1897 va_end(argptr);
1898 if (r == EFI_SUCCESS)
1899 return EFI_EXIT(r);
1900
62471e46 1901 /* If an error occurred undo all changes. */
58b83586 1902 va_start(argptr, handle);
1903 for (; i; --i) {
1904 protocol = va_arg(argptr, efi_guid_t*);
1905 protocol_interface = va_arg(argptr, void*);
cd534083
HS
1906 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1907 protocol_interface));
58b83586 1908 }
1909 va_end(argptr);
1910
1911 return EFI_EXIT(r);
bee91169
AG
1912}
1913
332468f7
HS
1914/*
1915 * Uninstall multiple protocol interfaces.
1916 *
1917 * This function implements the UninstallMultipleProtocolInterfaces service.
1918 * See the Unified Extensible Firmware Interface (UEFI) specification
1919 * for details.
1920 *
1921 * @handle handle from which the protocol interfaces shall be removed
1922 * @... NULL terminated argument list with pairs of protocol GUIDS and
1923 * interfaces
1924 * @return status code
1925 */
bee91169
AG
1926static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1927 void *handle, ...)
1928{
1929 EFI_ENTRY("%p", handle);
843ce54c
HS
1930
1931 va_list argptr;
1932 const efi_guid_t *protocol;
1933 void *protocol_interface;
1934 efi_status_t r = EFI_SUCCESS;
1935 size_t i = 0;
1936
1937 if (!handle)
1938 return EFI_EXIT(EFI_INVALID_PARAMETER);
1939
1940 va_start(argptr, handle);
1941 for (;;) {
1942 protocol = va_arg(argptr, efi_guid_t*);
1943 if (!protocol)
1944 break;
1945 protocol_interface = va_arg(argptr, void*);
1946 r = EFI_CALL(efi_uninstall_protocol_interface(
1947 handle, protocol,
1948 protocol_interface));
1949 if (r != EFI_SUCCESS)
1950 break;
1951 i++;
1952 }
1953 va_end(argptr);
1954 if (r == EFI_SUCCESS)
1955 return EFI_EXIT(r);
1956
1957 /* If an error occurred undo all changes. */
1958 va_start(argptr, handle);
1959 for (; i; --i) {
1960 protocol = va_arg(argptr, efi_guid_t*);
1961 protocol_interface = va_arg(argptr, void*);
1962 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1963 EFI_NATIVE_INTERFACE,
1964 protocol_interface));
1965 }
1966 va_end(argptr);
1967
1968 return EFI_EXIT(r);
bee91169
AG
1969}
1970
332468f7
HS
1971/*
1972 * Calculate cyclic redundancy code.
1973 *
1974 * This function implements the CalculateCrc32 service.
1975 * See the Unified Extensible Firmware Interface (UEFI) specification
1976 * for details.
1977 *
1978 * @data buffer with data
1979 * @data_size size of buffer in bytes
1980 * @crc32_p cyclic redundancy code
1981 * @return status code
1982 */
bee91169
AG
1983static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1984 unsigned long data_size,
1985 uint32_t *crc32_p)
1986{
1987 EFI_ENTRY("%p, %ld", data, data_size);
1988 *crc32_p = crc32(0, data, data_size);
1989 return EFI_EXIT(EFI_SUCCESS);
1990}
1991
332468f7
HS
1992/*
1993 * Copy memory.
1994 *
1995 * This function implements the CopyMem service.
1996 * See the Unified Extensible Firmware Interface (UEFI) specification
1997 * for details.
1998 *
1999 * @destination destination of the copy operation
2000 * @source source of the copy operation
2001 * @length number of bytes to copy
2002 */
fc05a959
HS
2003static void EFIAPI efi_copy_mem(void *destination, const void *source,
2004 size_t length)
bee91169 2005{
fc05a959 2006 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
bee91169 2007 memcpy(destination, source, length);
f7c78176 2008 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2009}
2010
332468f7
HS
2011/*
2012 * Fill memory with a byte value.
2013 *
2014 * This function implements the SetMem service.
2015 * See the Unified Extensible Firmware Interface (UEFI) specification
2016 * for details.
2017 *
2018 * @buffer buffer to fill
2019 * @size size of buffer in bytes
2020 * @value byte to copy to the buffer
2021 */
fc05a959 2022static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
bee91169 2023{
fc05a959 2024 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
bee91169 2025 memset(buffer, value, size);
f7c78176 2026 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2027}
2028
332468f7
HS
2029/*
2030 * Open protocol interface on a handle.
2031 *
2032 * This function implements the OpenProtocol interface.
2033 * See the Unified Extensible Firmware Interface (UEFI) specification
2034 * for details.
2035 *
2036 * @handle handle on which the protocol shall be opened
2037 * @protocol GUID of the protocol
2038 * @protocol_interface interface implementing the protocol
2039 * @agent_handle handle of the driver
2040 * @controller_handle handle of the controller
2041 * @attributes attributes indicating how to open the protocol
2042 * @return status code
2043 */
bee91169 2044static efi_status_t EFIAPI efi_open_protocol(
5a9682d0 2045 void *handle, const efi_guid_t *protocol,
bee91169
AG
2046 void **protocol_interface, void *agent_handle,
2047 void *controller_handle, uint32_t attributes)
2048{
80286e8f 2049 struct efi_handler *handler;
69baec67 2050 efi_status_t r = EFI_INVALID_PARAMETER;
bee91169 2051
778e6af8 2052 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
bee91169
AG
2053 protocol_interface, agent_handle, controller_handle,
2054 attributes);
b5349f74 2055
69baec67 2056 if (!handle || !protocol ||
2057 (!protocol_interface && attributes !=
2058 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2059 goto out;
2060 }
2061
2062 switch (attributes) {
2063 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2064 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2065 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2066 break;
2067 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2068 if (controller_handle == handle)
2069 goto out;
2070 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2071 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2072 if (controller_handle == NULL)
2073 goto out;
2074 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2075 if (agent_handle == NULL)
2076 goto out;
2077 break;
2078 default:
b5349f74 2079 goto out;
2080 }
2081
80286e8f
HS
2082 r = efi_search_protocol(handle, protocol, &handler);
2083 if (r != EFI_SUCCESS)
2084 goto out;
bee91169 2085
80286e8f
HS
2086 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2087 *protocol_interface = handler->protocol_interface;
bee91169
AG
2088out:
2089 return EFI_EXIT(r);
2090}
2091
332468f7
HS
2092/*
2093 * Get interface of a protocol on a handle.
2094 *
2095 * This function implements the HandleProtocol service.
2096 * See the Unified Extensible Firmware Interface (UEFI) specification
2097 * for details.
2098 *
2099 * @handle handle on which the protocol shall be opened
2100 * @protocol GUID of the protocol
2101 * @protocol_interface interface implementing the protocol
2102 * @return status code
2103 */
bee91169 2104static efi_status_t EFIAPI efi_handle_protocol(void *handle,
5a9682d0 2105 const efi_guid_t *protocol,
bee91169
AG
2106 void **protocol_interface)
2107{
8e1d329f 2108 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2109 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
bee91169
AG
2110}
2111
2112static const struct efi_boot_services efi_boot_services = {
2113 .hdr = {
2114 .headersize = sizeof(struct efi_table_hdr),
2115 },
2116 .raise_tpl = efi_raise_tpl,
2117 .restore_tpl = efi_restore_tpl,
2118 .allocate_pages = efi_allocate_pages_ext,
2119 .free_pages = efi_free_pages_ext,
2120 .get_memory_map = efi_get_memory_map_ext,
ead1274b 2121 .allocate_pool = efi_allocate_pool_ext,
42417bc8 2122 .free_pool = efi_free_pool_ext,
49deb455 2123 .create_event = efi_create_event_ext,
bfc72462 2124 .set_timer = efi_set_timer_ext,
bee91169 2125 .wait_for_event = efi_wait_for_event,
c6841592 2126 .signal_event = efi_signal_event_ext,
bee91169
AG
2127 .close_event = efi_close_event,
2128 .check_event = efi_check_event,
1760ef57 2129 .install_protocol_interface = efi_install_protocol_interface,
bee91169 2130 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
cd534083 2131 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
bee91169
AG
2132 .handle_protocol = efi_handle_protocol,
2133 .reserved = NULL,
2134 .register_protocol_notify = efi_register_protocol_notify,
26329584 2135 .locate_handle = efi_locate_handle_ext,
bee91169 2136 .locate_device_path = efi_locate_device_path,
488bf12d 2137 .install_configuration_table = efi_install_configuration_table_ext,
bee91169
AG
2138 .load_image = efi_load_image,
2139 .start_image = efi_start_image,
a86aeaf2 2140 .exit = efi_exit,
bee91169
AG
2141 .unload_image = efi_unload_image,
2142 .exit_boot_services = efi_exit_boot_services,
2143 .get_next_monotonic_count = efi_get_next_monotonic_count,
2144 .stall = efi_stall,
2145 .set_watchdog_timer = efi_set_watchdog_timer,
2146 .connect_controller = efi_connect_controller,
2147 .disconnect_controller = efi_disconnect_controller,
2148 .open_protocol = efi_open_protocol,
2149 .close_protocol = efi_close_protocol,
2150 .open_protocol_information = efi_open_protocol_information,
2151 .protocols_per_handle = efi_protocols_per_handle,
2152 .locate_handle_buffer = efi_locate_handle_buffer,
2153 .locate_protocol = efi_locate_protocol,
2154 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2155 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2156 .calculate_crc32 = efi_calculate_crc32,
2157 .copy_mem = efi_copy_mem,
2158 .set_mem = efi_set_mem,
2159};
2160
2161
3c63db9c 2162static uint16_t __efi_runtime_data firmware_vendor[] =
bee91169
AG
2163 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2164
3c63db9c 2165struct efi_system_table __efi_runtime_data systab = {
bee91169
AG
2166 .hdr = {
2167 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2168 .revision = 0x20005, /* 2.5 */
2169 .headersize = sizeof(struct efi_table_hdr),
2170 },
2171 .fw_vendor = (long)firmware_vendor,
2172 .con_in = (void*)&efi_con_in,
2173 .con_out = (void*)&efi_con_out,
2174 .std_err = (void*)&efi_con_out,
2175 .runtime = (void*)&efi_runtime_services,
2176 .boottime = (void*)&efi_boot_services,
2177 .nr_tables = 0,
2178 .tables = (void*)efi_conf_table,
2179};