]> git.ipfire.org Git - people/ms/u-boot.git/blob - lib/efi_loader/efi_boottime.c
efi_loader: manage protocols in a linked list
[people/ms/u-boot.git] / lib / efi_loader / efi_boottime.c
1 /*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9 #include <common.h>
10 #include <div64.h>
11 #include <efi_loader.h>
12 #include <environment.h>
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
21 DECLARE_GLOBAL_DATA_PTR;
22
23 /* Task priority level */
24 static efi_uintn_t efi_tpl = TPL_APPLICATION;
25
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_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 */
35 static 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 */
45 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
46
47 #ifdef CONFIG_ARM
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 */
54 static volatile void *efi_gd, *app_gd;
55 #endif
56
57 static int entry_count;
58 static int nesting_level;
59
60 /* Called on every callback entry */
61 int __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 */
73 int __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
82 /* Called from do_bootefi_exec() */
83 void efi_save_gd(void)
84 {
85 #ifdef CONFIG_ARM
86 efi_gd = gd;
87 #endif
88 }
89
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 */
95 void efi_restore_gd(void)
96 {
97 #ifdef CONFIG_ARM
98 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
101 gd = efi_gd;
102 #endif
103 }
104
105 /*
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
108 */
109 static 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
117 const char *__efi_nesting(void)
118 {
119 return indent_string(nesting_level);
120 }
121
122 const char *__efi_nesting_inc(void)
123 {
124 return indent_string(nesting_level++);
125 }
126
127 const char *__efi_nesting_dec(void)
128 {
129 return indent_string(--nesting_level);
130 }
131
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 */
145 void efi_signal_event(struct efi_event *event)
146 {
147 if (event->notify_function) {
148 event->is_queued = true;
149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
154 }
155 event->is_queued = false;
156 }
157
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 */
168 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
169 {
170 efi_uintn_t old_tpl = efi_tpl;
171
172 EFI_ENTRY("0x%zx", new_tpl);
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;
182 }
183
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 */
193 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
194 {
195 EFI_ENTRY("0x%zx", old_tpl);
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);
204 }
205
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 */
219 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
220 efi_uintn_t pages,
221 uint64_t *memory)
222 {
223 efi_status_t r;
224
225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228 }
229
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 */
241 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
242 efi_uintn_t pages)
243 {
244 efi_status_t r;
245
246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249 }
250
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 */
266 static efi_status_t EFIAPI efi_get_memory_map_ext(
267 efi_uintn_t *memory_map_size,
268 struct efi_mem_desc *memory_map,
269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
271 uint32_t *descriptor_version)
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
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 */
294 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
295 efi_uintn_t size,
296 void **buffer)
297 {
298 efi_status_t r;
299
300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
301 r = efi_allocate_pool(pool_type, size, buffer);
302 return EFI_EXIT(r);
303 }
304
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 */
315 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
316 {
317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
320 r = efi_free_pool(buffer);
321 return EFI_EXIT(r);
322 }
323
324 /*
325 * Create handle.
326 *
327 * @handle new handle
328 * @return status code
329 */
330 efi_status_t efi_create_handle(void **handle)
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 INIT_LIST_HEAD(&obj->protocols);
343 list_add_tail(&obj->link, &efi_obj_list);
344 *handle = obj;
345 return r;
346 }
347
348 /*
349 * Our event capabilities are very limited. Only a small limited
350 * number of events is allowed to coexist.
351 */
352 static struct efi_event efi_events[16];
353
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 */
369 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
370 void (EFIAPI *notify_function) (
371 struct efi_event *event,
372 void *context),
373 void *notify_context, struct efi_event **event)
374 {
375 int i;
376
377 if (event == NULL)
378 return EFI_INVALID_PARAMETER;
379
380 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
381 return EFI_INVALID_PARAMETER;
382
383 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
384 notify_function == NULL)
385 return EFI_INVALID_PARAMETER;
386
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;
396 efi_events[i].is_queued = false;
397 efi_events[i].is_signaled = false;
398 *event = &efi_events[i];
399 return EFI_SUCCESS;
400 }
401 return EFI_OUT_OF_RESOURCES;
402 }
403
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 */
418 static efi_status_t EFIAPI efi_create_event_ext(
419 uint32_t type, efi_uintn_t notify_tpl,
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
432 /*
433 * Check if a timer event has occurred or a queued notification function should
434 * be called.
435 *
436 * Our timers have to work without interrupts, so we check whenever keyboard
437 * input or disk accesses happen if enough time elapsed for them to fire.
438 */
439 void efi_timer_check(void)
440 {
441 int i;
442 u64 now = timer_get_us();
443
444 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
445 if (!efi_events[i].type)
446 continue;
447 if (efi_events[i].is_queued)
448 efi_signal_event(&efi_events[i]);
449 if (!(efi_events[i].type & EVT_TIMER) ||
450 now < efi_events[i].trigger_next)
451 continue;
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:
457 efi_events[i].trigger_next +=
458 efi_events[i].trigger_time;
459 break;
460 default:
461 continue;
462 }
463 efi_events[i].is_signaled = true;
464 efi_signal_event(&efi_events[i]);
465 }
466 WATCHDOG_RESET();
467 }
468
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 */
480 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
481 uint64_t trigger_time)
482 {
483 int i;
484
485 /*
486 * The parameter defines a multiple of 100ns.
487 * We use multiples of 1000ns. So divide by 10.
488 */
489 do_div(trigger_time, 10);
490
491 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
492 if (event != &efi_events[i])
493 continue;
494
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 =
504 timer_get_us() + trigger_time;
505 break;
506 default:
507 return EFI_INVALID_PARAMETER;
508 }
509 event->trigger_type = type;
510 event->trigger_time = trigger_time;
511 event->is_signaled = false;
512 return EFI_SUCCESS;
513 }
514 return EFI_INVALID_PARAMETER;
515 }
516
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 */
529 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
530 enum efi_timer_delay type,
531 uint64_t trigger_time)
532 {
533 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
534 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
535 }
536
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 */
549 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
550 struct efi_event **event,
551 efi_uintn_t *index)
552 {
553 int i, j;
554
555 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
556
557 /* Check parameters */
558 if (!num_events || !event)
559 return EFI_EXIT(EFI_INVALID_PARAMETER);
560 /* Check TPL */
561 if (efi_tpl != TPL_APPLICATION)
562 return EFI_EXIT(EFI_UNSUPPORTED);
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);
569 known_event:
570 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
571 return EFI_EXIT(EFI_INVALID_PARAMETER);
572 if (!event[i]->is_signaled)
573 efi_signal_event(event[i]);
574 }
575
576 /* Wait for signal */
577 for (;;) {
578 for (i = 0; i < num_events; ++i) {
579 if (event[i]->is_signaled)
580 goto out;
581 }
582 /* Allow events to occur. */
583 efi_timer_check();
584 }
585
586 out:
587 /*
588 * Reset the signal which is passed to the caller to allow periodic
589 * events to occur.
590 */
591 event[i]->is_signaled = false;
592 if (index)
593 *index = i;
594
595 return EFI_EXIT(EFI_SUCCESS);
596 }
597
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
609 * @return status code
610 */
611 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
612 {
613 int i;
614
615 EFI_ENTRY("%p", event);
616 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
617 if (event != &efi_events[i])
618 continue;
619 if (event->is_signaled)
620 break;
621 event->is_signaled = true;
622 if (event->type & EVT_NOTIFY_SIGNAL)
623 efi_signal_event(event);
624 break;
625 }
626 return EFI_EXIT(EFI_SUCCESS);
627 }
628
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 */
639 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
640 {
641 int i;
642
643 EFI_ENTRY("%p", event);
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;
648 event->is_queued = false;
649 event->is_signaled = false;
650 return EFI_EXIT(EFI_SUCCESS);
651 }
652 }
653 return EFI_EXIT(EFI_INVALID_PARAMETER);
654 }
655
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 */
668 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
669 {
670 int i;
671
672 EFI_ENTRY("%p", event);
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;
679 if (!event->is_signaled)
680 efi_signal_event(event);
681 if (event->is_signaled)
682 return EFI_EXIT(EFI_SUCCESS);
683 return EFI_EXIT(EFI_NOT_READY);
684 }
685 return EFI_EXIT(EFI_INVALID_PARAMETER);
686 }
687
688 /*
689 * Find the internal EFI object for a handle.
690 *
691 * @handle handle to find
692 * @return EFI object
693 */
694 struct efi_object *efi_search_obj(const void *handle)
695 {
696 struct efi_object *efiobj;
697
698 list_for_each_entry(efiobj, &efi_obj_list, link) {
699 if (efiobj->handle == handle)
700 return efiobj;
701 }
702
703 return NULL;
704 }
705
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 */
714 efi_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;
719 struct list_head *lhandle;
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 list_for_each(lhandle, &efiobj->protocols) {
727 struct efi_handler *protocol;
728
729 protocol = list_entry(lhandle, struct efi_handler, link);
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 */
747 efi_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
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;
763 handler->guid = protocol;
764 handler->protocol_interface = protocol_interface;
765 list_add_tail(&handler->link, &efiobj->protocols);
766 return EFI_SUCCESS;
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 */
777 efi_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;
786 if (guidcmp(handler->guid, protocol))
787 return EFI_INVALID_PARAMETER;
788 list_del(&handler->link);
789 free(handler);
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 */
799 efi_status_t efi_remove_all_protocols(const void *handle)
800 {
801 struct efi_object *efiobj;
802 struct list_head *lhandle;
803 struct list_head *pos;
804
805 efiobj = efi_search_obj(handle);
806 if (!efiobj)
807 return EFI_INVALID_PARAMETER;
808 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
809 struct efi_handler *protocol;
810 efi_status_t ret;
811
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;
818 }
819 return EFI_SUCCESS;
820 }
821
822 /*
823 * Install protocol interface.
824 *
825 * This function implements the InstallProtocolInterface service.
826 * See the Unified Extensible Firmware Interface (UEFI) specification
827 * for details.
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 */
836 static efi_status_t EFIAPI efi_install_protocol_interface(
837 void **handle, const efi_guid_t *protocol,
838 int protocol_interface_type, void *protocol_interface)
839 {
840 efi_status_t r;
841
842 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
843 protocol_interface);
844
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) {
853 r = efi_create_handle(handle);
854 if (r != EFI_SUCCESS)
855 goto out;
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);
861 }
862 /* Add new protocol */
863 r = efi_add_protocol(*handle, protocol, protocol_interface);
864 out:
865 return EFI_EXIT(r);
866 }
867
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 */
882 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
883 const efi_guid_t *protocol, void *old_interface,
884 void *new_interface)
885 {
886 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
887 new_interface);
888 return EFI_EXIT(EFI_ACCESS_DENIED);
889 }
890
891 /*
892 * Uninstall protocol interface.
893 *
894 * This function implements the UninstallProtocolInterface service.
895 * See the Unified Extensible Firmware Interface (UEFI) specification
896 * for details.
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 */
903 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
904 void *handle, const efi_guid_t *protocol,
905 void *protocol_interface)
906 {
907 struct efi_handler *handler;
908 efi_status_t r;
909
910 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
911
912 if (!handle || !protocol) {
913 r = EFI_INVALID_PARAMETER;
914 goto out;
915 }
916
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);
926 }
927 out:
928 return EFI_EXIT(r);
929 }
930
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 */
944 static efi_status_t EFIAPI efi_register_protocol_notify(
945 const efi_guid_t *protocol,
946 struct efi_event *event,
947 void **registration)
948 {
949 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
950 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
951 }
952
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 */
964 static int efi_search(enum efi_locate_search_type search_type,
965 const efi_guid_t *protocol, void *search_key,
966 struct efi_object *efiobj)
967 {
968 efi_status_t ret;
969
970 switch (search_type) {
971 case ALL_HANDLES:
972 return 0;
973 case BY_REGISTER_NOTIFY:
974 /* TODO: RegisterProtocolNotify is not implemented yet */
975 return -1;
976 case BY_PROTOCOL:
977 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
978 return (ret != EFI_SUCCESS);
979 default:
980 /* Invalid search type */
981 return -1;
982 }
983 }
984
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 */
998 static efi_status_t efi_locate_handle(
999 enum efi_locate_search_type search_type,
1000 const efi_guid_t *protocol, void *search_key,
1001 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1002 {
1003 struct efi_object *efiobj;
1004 efi_uintn_t size = 0;
1005
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
1031 /* Count how much space we need */
1032 list_for_each_entry(efiobj, &efi_obj_list, link) {
1033 if (!efi_search(search_type, protocol, search_key, efiobj))
1034 size += sizeof(void*);
1035 }
1036
1037 if (*buffer_size < size) {
1038 *buffer_size = size;
1039 return EFI_BUFFER_TOO_SMALL;
1040 }
1041
1042 *buffer_size = size;
1043 if (size == 0)
1044 return EFI_NOT_FOUND;
1045
1046 /* Then fill the array */
1047 list_for_each_entry(efiobj, &efi_obj_list, link) {
1048 if (!efi_search(search_type, protocol, search_key, efiobj))
1049 *buffer++ = efiobj->handle;
1050 }
1051
1052 return EFI_SUCCESS;
1053 }
1054
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 */
1069 static efi_status_t EFIAPI efi_locate_handle_ext(
1070 enum efi_locate_search_type search_type,
1071 const efi_guid_t *protocol, void *search_key,
1072 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1073 {
1074 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1075 buffer_size, buffer);
1076
1077 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1078 buffer_size, buffer));
1079 }
1080
1081 /* Collapses configuration table entries, removing index i */
1082 static 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
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 */
1102 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1103 {
1104 int i;
1105
1106 /* Check for guid override */
1107 for (i = 0; i < systab.nr_tables; i++) {
1108 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1109 if (table)
1110 efi_conf_table[i].table = table;
1111 else
1112 efi_remove_configuration_table(i);
1113 return EFI_SUCCESS;
1114 }
1115 }
1116
1117 if (!table)
1118 return EFI_NOT_FOUND;
1119
1120 /* No override, check for overflow */
1121 if (i >= ARRAY_SIZE(efi_conf_table))
1122 return EFI_OUT_OF_RESOURCES;
1123
1124 /* Add a new entry */
1125 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1126 efi_conf_table[i].table = table;
1127 systab.nr_tables = i + 1;
1128
1129 return EFI_SUCCESS;
1130 }
1131
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 */
1143 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1144 void *table)
1145 {
1146 EFI_ENTRY("%pUl, %p", guid, table);
1147 return EFI_EXIT(efi_install_configuration_table(guid, table));
1148 }
1149
1150 /*
1151 * Initialize a loaded_image_info + loaded_image_info object with correct
1152 * protocols, boot-device, etc.
1153 *
1154 * @info loaded image info to be passed to the entry point of the
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
1159 */
1160 void 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 {
1164 efi_status_t ret;
1165
1166 obj->handle = info;
1167
1168 info->file_path = file_path;
1169 if (device_path)
1170 info->device_handle = efi_dp_find_obj(device_path, NULL);
1171
1172 INIT_LIST_HEAD(&obj->protocols);
1173 list_add_tail(&obj->link, &efi_obj_list);
1174 /*
1175 * When asking for the device path interface, return
1176 * bootefi_device_path
1177 */
1178 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1179 if (ret != EFI_SUCCESS)
1180 goto failure;
1181
1182 /*
1183 * When asking for the loaded_image interface, just
1184 * return handle which points to loaded_image_info
1185 */
1186 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1187 if (ret != EFI_SUCCESS)
1188 goto failure;
1189
1190 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1191 (void *)&efi_console_control);
1192 if (ret != EFI_SUCCESS)
1193 goto failure;
1194
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;
1200
1201 return;
1202 failure:
1203 printf("ERROR: Failure to install protocols for loaded image\n");
1204 }
1205
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
1211 * @return status code
1212 */
1213 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1214 void **buffer)
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
1242 error:
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
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 */
1270 static 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 {
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);
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
1290 ret = efi_load_image_from_path(file_path, &source_buffer);
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
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
1318 info->system_table = &systab;
1319 info->parent_handle = parent_image;
1320 *image_handle = info;
1321
1322 return EFI_EXIT(EFI_SUCCESS);
1323 }
1324
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
1335 * @return status code
1336 */
1337 static 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! */
1350 if (setjmp(&info->exit_jmp)) {
1351 /* We returned from the child image */
1352 return EFI_EXIT(info->exit_status);
1353 }
1354
1355 __efi_nesting_dec();
1356 __efi_exit_check();
1357 entry(image_handle, &systab);
1358 __efi_entry_check();
1359 __efi_nesting_inc();
1360
1361 /* Should usually never get here */
1362 return EFI_EXIT(EFI_SUCCESS);
1363 }
1364
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
1376 * @return status code
1377 */
1378 static 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)
1381 {
1382 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1383
1384 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1385 exit_data_size, exit_data);
1386
1387 /* Make sure entry/exit counts for EFI world cross-overs match */
1388 __efi_exit_check();
1389
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
1396 loaded_image_info->exit_status = exit_status;
1397 longjmp(&loaded_image_info->exit_jmp, 1);
1398
1399 panic("EFI application exited");
1400 }
1401
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 */
1412 static 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
1424 /*
1425 * Fix up caches for EFI payloads if necessary.
1426 */
1427 static 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
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 */
1450 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1451 unsigned long map_key)
1452 {
1453 int i;
1454
1455 EFI_ENTRY("%p, %ld", image_handle, map_key);
1456
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
1466 /* XXX Should persist EFI variables here */
1467
1468 board_quiesce_devices();
1469
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 */
1477 efi_set_watchdog(0);
1478 WATCHDOG_RESET();
1479
1480 return EFI_EXIT(EFI_SUCCESS);
1481 }
1482
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 */
1493 static 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
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 */
1511 static 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
1518 /*
1519 * Reset the watchdog timer.
1520 *
1521 * This function implements the SetWatchdogTimer service.
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
1529 * @return status code
1530 */
1531 static 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);
1538 return EFI_EXIT(efi_set_watchdog(timeout));
1539 }
1540
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 */
1554 static 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
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 */
1577 static 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
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 */
1599 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1600 const efi_guid_t *protocol,
1601 void *agent_handle,
1602 void *controller_handle)
1603 {
1604 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1605 controller_handle);
1606 return EFI_EXIT(EFI_NOT_FOUND);
1607 }
1608
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 */
1622 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1623 const efi_guid_t *protocol,
1624 struct efi_open_protocol_info_entry **entry_buffer,
1625 efi_uintn_t *entry_count)
1626 {
1627 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1628 entry_count);
1629 return EFI_EXIT(EFI_NOT_FOUND);
1630 }
1631
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
1642 * @return status code
1643 */
1644 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1645 efi_guid_t ***protocol_buffer,
1646 efi_uintn_t *protocol_buffer_count)
1647 {
1648 unsigned long buffer_size;
1649 struct efi_object *efiobj;
1650 struct list_head *protocol_handle;
1651 efi_status_t r;
1652
1653 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1654 protocol_buffer_count);
1655
1656 if (!handle || !protocol_buffer || !protocol_buffer_count)
1657 return EFI_EXIT(EFI_INVALID_PARAMETER);
1658
1659 *protocol_buffer = NULL;
1660 *protocol_buffer_count = 0;
1661
1662 efiobj = efi_search_obj(handle);
1663 if (!efiobj)
1664 return EFI_EXIT(EFI_INVALID_PARAMETER);
1665
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;
1687 }
1688 }
1689
1690 return EFI_EXIT(EFI_SUCCESS);
1691 }
1692
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 */
1707 static efi_status_t EFIAPI efi_locate_handle_buffer(
1708 enum efi_locate_search_type search_type,
1709 const efi_guid_t *protocol, void *search_key,
1710 efi_uintn_t *no_handles, efi_handle_t **buffer)
1711 {
1712 efi_status_t r;
1713 efi_uintn_t buffer_size = 0;
1714
1715 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1716 no_handles, buffer);
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 *);
1736 out:
1737 return EFI_EXIT(r);
1738 }
1739
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
1750 * @return status code
1751 */
1752 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1753 void *registration,
1754 void **protocol_interface)
1755 {
1756 struct list_head *lhandle;
1757 efi_status_t ret;
1758
1759 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
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;
1766 struct efi_handler *handler;
1767
1768 efiobj = list_entry(lhandle, struct efi_object, link);
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);
1774 }
1775 }
1776 *protocol_interface = NULL;
1777
1778 return EFI_EXIT(EFI_NOT_FOUND);
1779 }
1780
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 */
1793 static 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 }
1853 out:
1854 return EFI_EXIT(ret);
1855 }
1856
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 */
1869 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1870 void **handle, ...)
1871 {
1872 EFI_ENTRY("%p", handle);
1873
1874 va_list argptr;
1875 const efi_guid_t *protocol;
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*);
1889 r = EFI_CALL(efi_install_protocol_interface(
1890 handle, protocol,
1891 EFI_NATIVE_INTERFACE,
1892 protocol_interface));
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
1901 /* If an error occurred undo all changes. */
1902 va_start(argptr, handle);
1903 for (; i; --i) {
1904 protocol = va_arg(argptr, efi_guid_t*);
1905 protocol_interface = va_arg(argptr, void*);
1906 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1907 protocol_interface));
1908 }
1909 va_end(argptr);
1910
1911 return EFI_EXIT(r);
1912 }
1913
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 */
1926 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1927 void *handle, ...)
1928 {
1929 EFI_ENTRY("%p", handle);
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);
1969 }
1970
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 */
1983 static 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
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 */
2003 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2004 size_t length)
2005 {
2006 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2007 memcpy(destination, source, length);
2008 EFI_EXIT(EFI_SUCCESS);
2009 }
2010
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 */
2022 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2023 {
2024 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2025 memset(buffer, value, size);
2026 EFI_EXIT(EFI_SUCCESS);
2027 }
2028
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 */
2044 static efi_status_t EFIAPI efi_open_protocol(
2045 void *handle, const efi_guid_t *protocol,
2046 void **protocol_interface, void *agent_handle,
2047 void *controller_handle, uint32_t attributes)
2048 {
2049 struct efi_handler *handler;
2050 efi_status_t r = EFI_INVALID_PARAMETER;
2051
2052 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2053 protocol_interface, agent_handle, controller_handle,
2054 attributes);
2055
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:
2079 goto out;
2080 }
2081
2082 r = efi_search_protocol(handle, protocol, &handler);
2083 if (r != EFI_SUCCESS)
2084 goto out;
2085
2086 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2087 *protocol_interface = handler->protocol_interface;
2088 out:
2089 return EFI_EXIT(r);
2090 }
2091
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 */
2104 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2105 const efi_guid_t *protocol,
2106 void **protocol_interface)
2107 {
2108 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2109 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2110 }
2111
2112 static 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,
2121 .allocate_pool = efi_allocate_pool_ext,
2122 .free_pool = efi_free_pool_ext,
2123 .create_event = efi_create_event_ext,
2124 .set_timer = efi_set_timer_ext,
2125 .wait_for_event = efi_wait_for_event,
2126 .signal_event = efi_signal_event_ext,
2127 .close_event = efi_close_event,
2128 .check_event = efi_check_event,
2129 .install_protocol_interface = efi_install_protocol_interface,
2130 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2131 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2132 .handle_protocol = efi_handle_protocol,
2133 .reserved = NULL,
2134 .register_protocol_notify = efi_register_protocol_notify,
2135 .locate_handle = efi_locate_handle_ext,
2136 .locate_device_path = efi_locate_device_path,
2137 .install_configuration_table = efi_install_configuration_table_ext,
2138 .load_image = efi_load_image,
2139 .start_image = efi_start_image,
2140 .exit = efi_exit,
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
2162 static uint16_t __efi_runtime_data firmware_vendor[] =
2163 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2164
2165 struct efi_system_table __efi_runtime_data systab = {
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 };