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