]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/efi_loader/efi_boottime.c
efi_loader: do not install NULL as device path
[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;
f0959dbe
HS
59/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
c160d2f5 62
2074f700
HS
63static efi_status_t EFIAPI efi_disconnect_controller(
64 efi_handle_t controller_handle,
65 efi_handle_t driver_image_handle,
66 efi_handle_t child_handle);
3f9b0042 67
c160d2f5
RC
68/* Called on every callback entry */
69int __efi_entry_check(void)
70{
71 int ret = entry_count++ == 0;
72#ifdef CONFIG_ARM
73 assert(efi_gd);
74 app_gd = gd;
75 gd = efi_gd;
76#endif
77 return ret;
78}
79
80/* Called on every callback exit */
81int __efi_exit_check(void)
82{
83 int ret = --entry_count == 0;
84#ifdef CONFIG_ARM
85 gd = app_gd;
86#endif
87 return ret;
88}
89
bee91169
AG
90/* Called from do_bootefi_exec() */
91void efi_save_gd(void)
92{
65e4c0b1 93#ifdef CONFIG_ARM
bee91169 94 efi_gd = gd;
65e4c0b1 95#endif
bee91169
AG
96}
97
c160d2f5
RC
98/*
99 * Special case handler for error/abort that just forces things back
100 * to u-boot world so we can dump out an abort msg, without any care
101 * about returning back to UEFI world.
102 */
bee91169
AG
103void efi_restore_gd(void)
104{
65e4c0b1 105#ifdef CONFIG_ARM
bee91169
AG
106 /* Only restore if we're already in EFI context */
107 if (!efi_gd)
108 return;
bee91169 109 gd = efi_gd;
65e4c0b1 110#endif
bee91169
AG
111}
112
af65db85 113/*
c8df80c5
HS
114 * Return a string for indenting with two spaces per level. A maximum of ten
115 * indent levels is supported. Higher indent levels will be truncated.
116 *
117 * @level indent level
118 * @return indent string
af65db85
RC
119 */
120static const char *indent_string(int level)
121{
122 const char *indent = " ";
123 const int max = strlen(indent);
124 level = min(max, level * 2);
125 return &indent[max - level];
126}
127
ae0bd3a9
HS
128const char *__efi_nesting(void)
129{
130 return indent_string(nesting_level);
131}
132
af65db85
RC
133const char *__efi_nesting_inc(void)
134{
135 return indent_string(nesting_level++);
136}
137
138const char *__efi_nesting_dec(void)
139{
140 return indent_string(--nesting_level);
141}
142
332468f7
HS
143/*
144 * Queue an EFI event.
145 *
146 * This function queues the notification function of the event for future
147 * execution.
148 *
149 * The notification function is called if the task priority level of the
150 * event is higher than the current task priority level.
151 *
152 * For the SignalEvent service see efi_signal_event_ext.
153 *
154 * @event event to signal
9bc9664d 155 * @check_tpl check the TPL level
332468f7 156 */
9bc9664d 157void efi_signal_event(struct efi_event *event, bool check_tpl)
c6841592 158{
ca62a4f5 159 if (event->notify_function) {
e190e897 160 event->is_queued = true;
32f4b2f8 161 /* Check TPL */
9bc9664d 162 if (check_tpl && efi_tpl >= event->notify_tpl)
32f4b2f8 163 return;
ea630ce9
HS
164 EFI_CALL_VOID(event->notify_function(event,
165 event->notify_context));
c6841592 166 }
e190e897 167 event->is_queued = false;
c6841592 168}
169
332468f7
HS
170/*
171 * Raise the task priority level.
172 *
173 * This function implements the RaiseTpl service.
174 * See the Unified Extensible Firmware Interface (UEFI) specification
175 * for details.
176 *
177 * @new_tpl new value of the task priority level
178 * @return old value of the task priority level
179 */
152cade3 180static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
bee91169 181{
152cade3 182 efi_uintn_t old_tpl = efi_tpl;
32f4b2f8 183
503f2695 184 EFI_ENTRY("0x%zx", new_tpl);
32f4b2f8
HS
185
186 if (new_tpl < efi_tpl)
187 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
188 efi_tpl = new_tpl;
189 if (efi_tpl > TPL_HIGH_LEVEL)
190 efi_tpl = TPL_HIGH_LEVEL;
191
192 EFI_EXIT(EFI_SUCCESS);
193 return old_tpl;
bee91169
AG
194}
195
332468f7
HS
196/*
197 * Lower the task priority level.
198 *
199 * This function implements the RestoreTpl service.
200 * See the Unified Extensible Firmware Interface (UEFI) specification
201 * for details.
202 *
203 * @old_tpl value of the task priority level to be restored
204 */
152cade3 205static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
bee91169 206{
503f2695 207 EFI_ENTRY("0x%zx", old_tpl);
32f4b2f8
HS
208
209 if (old_tpl > efi_tpl)
210 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
211 efi_tpl = old_tpl;
212 if (efi_tpl > TPL_HIGH_LEVEL)
213 efi_tpl = TPL_HIGH_LEVEL;
214
215 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
216}
217
332468f7
HS
218/*
219 * Allocate memory pages.
220 *
221 * This function implements the AllocatePages service.
222 * See the Unified Extensible Firmware Interface (UEFI) specification
223 * for details.
224 *
225 * @type type of allocation to be performed
226 * @memory_type usage type of the allocated memory
227 * @pages number of pages to be allocated
228 * @memory allocated memory
229 * @return status code
230 */
6e0bf8d8 231static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
f5a2a938 232 efi_uintn_t pages,
6e0bf8d8 233 uint64_t *memory)
bee91169
AG
234{
235 efi_status_t r;
236
f5a2a938 237 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
bee91169
AG
238 r = efi_allocate_pages(type, memory_type, pages, memory);
239 return EFI_EXIT(r);
240}
241
332468f7
HS
242/*
243 * Free memory pages.
244 *
245 * This function implements the FreePages service.
246 * See the Unified Extensible Firmware Interface (UEFI) specification
247 * for details.
248 *
249 * @memory start of the memory area to be freed
250 * @pages number of pages to be freed
251 * @return status code
252 */
6e0bf8d8 253static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
f5a2a938 254 efi_uintn_t pages)
bee91169
AG
255{
256 efi_status_t r;
257
f5a2a938 258 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
bee91169
AG
259 r = efi_free_pages(memory, pages);
260 return EFI_EXIT(r);
261}
262
332468f7
HS
263/*
264 * Get map describing memory usage.
265 *
266 * This function implements the GetMemoryMap service.
267 * See the Unified Extensible Firmware Interface (UEFI) specification
268 * for details.
269 *
270 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
271 * on exit the size of the copied memory map
272 * @memory_map buffer to which the memory map is written
273 * @map_key key for the memory map
274 * @descriptor_size size of an individual memory descriptor
275 * @descriptor_version version number of the memory descriptor structure
276 * @return status code
277 */
6e0bf8d8 278static efi_status_t EFIAPI efi_get_memory_map_ext(
f5a2a938 279 efi_uintn_t *memory_map_size,
6e0bf8d8 280 struct efi_mem_desc *memory_map,
f5a2a938
HS
281 efi_uintn_t *map_key,
282 efi_uintn_t *descriptor_size,
6e0bf8d8 283 uint32_t *descriptor_version)
bee91169
AG
284{
285 efi_status_t r;
286
287 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
288 map_key, descriptor_size, descriptor_version);
289 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
290 descriptor_size, descriptor_version);
291 return EFI_EXIT(r);
292}
293
332468f7
HS
294/*
295 * Allocate memory from pool.
296 *
297 * This function implements the AllocatePool service.
298 * See the Unified Extensible Firmware Interface (UEFI) specification
299 * for details.
300 *
301 * @pool_type type of the pool from which memory is to be allocated
302 * @size number of bytes to be allocated
303 * @buffer allocated memory
304 * @return status code
305 */
ead1274b 306static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
f5a2a938 307 efi_uintn_t size,
ead1274b 308 void **buffer)
bee91169 309{
1cd29f0a
AG
310 efi_status_t r;
311
f5a2a938 312 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
ead1274b 313 r = efi_allocate_pool(pool_type, size, buffer);
1cd29f0a 314 return EFI_EXIT(r);
bee91169
AG
315}
316
332468f7
HS
317/*
318 * Free memory from pool.
319 *
320 * This function implements the FreePool service.
321 * See the Unified Extensible Firmware Interface (UEFI) specification
322 * for details.
323 *
324 * @buffer start of memory to be freed
325 * @return status code
326 */
42417bc8 327static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
bee91169 328{
1cd29f0a
AG
329 efi_status_t r;
330
331 EFI_ENTRY("%p", buffer);
42417bc8 332 r = efi_free_pool(buffer);
1cd29f0a 333 return EFI_EXIT(r);
bee91169
AG
334}
335
44549d62
HS
336/*
337 * Add a new object to the object list.
338 *
339 * The protocols list is initialized.
340 * The object handle is set.
341 *
342 * @obj object to be added
343 */
344void efi_add_handle(struct efi_object *obj)
345{
346 if (!obj)
347 return;
348 INIT_LIST_HEAD(&obj->protocols);
349 obj->handle = obj;
350 list_add_tail(&obj->link, &efi_obj_list);
351}
352
2edab5e2
HS
353/*
354 * Create handle.
355 *
356 * @handle new handle
357 * @return status code
358 */
2074f700 359efi_status_t efi_create_handle(efi_handle_t *handle)
3cc6e3fe
HS
360{
361 struct efi_object *obj;
362 efi_status_t r;
363
364 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
365 sizeof(struct efi_object),
366 (void **)&obj);
367 if (r != EFI_SUCCESS)
368 return r;
44549d62
HS
369 efi_add_handle(obj);
370 *handle = obj->handle;
3cc6e3fe
HS
371 return r;
372}
373
678e03a0
HS
374/*
375 * Find a protocol on a handle.
376 *
377 * @handle handle
378 * @protocol_guid GUID of the protocol
379 * @handler reference to the protocol
380 * @return status code
381 */
2074f700 382efi_status_t efi_search_protocol(const efi_handle_t handle,
678e03a0
HS
383 const efi_guid_t *protocol_guid,
384 struct efi_handler **handler)
385{
386 struct efi_object *efiobj;
387 struct list_head *lhandle;
388
389 if (!handle || !protocol_guid)
390 return EFI_INVALID_PARAMETER;
391 efiobj = efi_search_obj(handle);
392 if (!efiobj)
393 return EFI_INVALID_PARAMETER;
394 list_for_each(lhandle, &efiobj->protocols) {
395 struct efi_handler *protocol;
396
397 protocol = list_entry(lhandle, struct efi_handler, link);
398 if (!guidcmp(protocol->guid, protocol_guid)) {
399 if (handler)
400 *handler = protocol;
401 return EFI_SUCCESS;
402 }
403 }
404 return EFI_NOT_FOUND;
405}
406
407/*
408 * Delete protocol from a handle.
409 *
410 * @handle handle from which the protocol shall be deleted
411 * @protocol GUID of the protocol to be deleted
412 * @protocol_interface interface of the protocol implementation
413 * @return status code
414 */
2074f700
HS
415efi_status_t efi_remove_protocol(const efi_handle_t handle,
416 const efi_guid_t *protocol,
678e03a0
HS
417 void *protocol_interface)
418{
419 struct efi_handler *handler;
420 efi_status_t ret;
421
422 ret = efi_search_protocol(handle, protocol, &handler);
423 if (ret != EFI_SUCCESS)
424 return ret;
425 if (guidcmp(handler->guid, protocol))
426 return EFI_INVALID_PARAMETER;
427 list_del(&handler->link);
428 free(handler);
429 return EFI_SUCCESS;
430}
431
432/*
433 * Delete all protocols from a handle.
434 *
435 * @handle handle from which the protocols shall be deleted
436 * @return status code
437 */
2074f700 438efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
678e03a0
HS
439{
440 struct efi_object *efiobj;
32e6fed6
HS
441 struct efi_handler *protocol;
442 struct efi_handler *pos;
678e03a0
HS
443
444 efiobj = efi_search_obj(handle);
445 if (!efiobj)
446 return EFI_INVALID_PARAMETER;
32e6fed6 447 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
678e03a0
HS
448 efi_status_t ret;
449
678e03a0
HS
450 ret = efi_remove_protocol(handle, protocol->guid,
451 protocol->protocol_interface);
452 if (ret != EFI_SUCCESS)
453 return ret;
454 }
455 return EFI_SUCCESS;
456}
457
458/*
459 * Delete handle.
460 *
461 * @handle handle to delete
462 */
463void efi_delete_handle(struct efi_object *obj)
464{
465 if (!obj)
466 return;
467 efi_remove_all_protocols(obj->handle);
468 list_del(&obj->link);
469 free(obj);
470}
471
bee91169 472/*
c6841592 473 * Our event capabilities are very limited. Only a small limited
474 * number of events is allowed to coexist.
bee91169 475 */
c6841592 476static struct efi_event efi_events[16];
bee91169 477
332468f7
HS
478/*
479 * Create an event.
480 *
481 * This function is used inside U-Boot code to create an event.
482 *
483 * For the API function implementing the CreateEvent service see
484 * efi_create_event_ext.
485 *
486 * @type type of the event to create
487 * @notify_tpl task priority level of the event
488 * @notify_function notification function of the event
489 * @notify_context pointer passed to the notification function
490 * @event created event
491 * @return status code
492 */
152cade3 493efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
49deb455 494 void (EFIAPI *notify_function) (
2fd945fe 495 struct efi_event *event,
496 void *context),
49deb455 497 void *notify_context, struct efi_event **event)
bee91169 498{
c6841592 499 int i;
500
a95343b8 501 if (event == NULL)
49deb455 502 return EFI_INVALID_PARAMETER;
a95343b8
JG
503
504 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
49deb455 505 return EFI_INVALID_PARAMETER;
a95343b8
JG
506
507 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
508 notify_function == NULL)
49deb455 509 return EFI_INVALID_PARAMETER;
a95343b8 510
c6841592 511 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
512 if (efi_events[i].type)
513 continue;
514 efi_events[i].type = type;
515 efi_events[i].notify_tpl = notify_tpl;
516 efi_events[i].notify_function = notify_function;
517 efi_events[i].notify_context = notify_context;
518 /* Disable timers on bootup */
519 efi_events[i].trigger_next = -1ULL;
e190e897
HS
520 efi_events[i].is_queued = false;
521 efi_events[i].is_signaled = false;
c6841592 522 *event = &efi_events[i];
49deb455 523 return EFI_SUCCESS;
c6841592 524 }
49deb455 525 return EFI_OUT_OF_RESOURCES;
bee91169
AG
526}
527
332468f7
HS
528/*
529 * Create an event.
530 *
531 * This function implements the CreateEvent service.
532 * See the Unified Extensible Firmware Interface (UEFI) specification
533 * for details.
534 *
535 * @type type of the event to create
536 * @notify_tpl task priority level of the event
537 * @notify_function notification function of the event
538 * @notify_context pointer passed to the notification function
539 * @event created event
540 * @return status code
541 */
49deb455 542static efi_status_t EFIAPI efi_create_event_ext(
152cade3 543 uint32_t type, efi_uintn_t notify_tpl,
49deb455 544 void (EFIAPI *notify_function) (
545 struct efi_event *event,
546 void *context),
547 void *notify_context, struct efi_event **event)
548{
549 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
550 notify_context);
551 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
552 notify_context, event));
553}
554
555
bee91169 556/*
332468f7
HS
557 * Check if a timer event has occurred or a queued notification function should
558 * be called.
559 *
bee91169 560 * Our timers have to work without interrupts, so we check whenever keyboard
332468f7 561 * input or disk accesses happen if enough time elapsed for them to fire.
bee91169
AG
562 */
563void efi_timer_check(void)
564{
c6841592 565 int i;
bee91169
AG
566 u64 now = timer_get_us();
567
c6841592 568 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
ca62a4f5
HS
569 if (!efi_events[i].type)
570 continue;
e190e897 571 if (efi_events[i].is_queued)
9bc9664d 572 efi_signal_event(&efi_events[i], true);
ca62a4f5 573 if (!(efi_events[i].type & EVT_TIMER) ||
c6841592 574 now < efi_events[i].trigger_next)
575 continue;
ca62a4f5
HS
576 switch (efi_events[i].trigger_type) {
577 case EFI_TIMER_RELATIVE:
578 efi_events[i].trigger_type = EFI_TIMER_STOP;
579 break;
580 case EFI_TIMER_PERIODIC:
c6841592 581 efi_events[i].trigger_next +=
8787b02e 582 efi_events[i].trigger_time;
ca62a4f5
HS
583 break;
584 default:
585 continue;
c6841592 586 }
e190e897 587 efi_events[i].is_signaled = true;
9bc9664d 588 efi_signal_event(&efi_events[i], true);
bee91169 589 }
bee91169
AG
590 WATCHDOG_RESET();
591}
592
332468f7
HS
593/*
594 * Set the trigger time for a timer event or stop the event.
595 *
596 * This is the function for internal usage in U-Boot. For the API function
597 * implementing the SetTimer service see efi_set_timer_ext.
598 *
599 * @event event for which the timer is set
600 * @type type of the timer
601 * @trigger_time trigger period in multiples of 100ns
602 * @return status code
603 */
b521d29e 604efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
bfc72462 605 uint64_t trigger_time)
bee91169 606{
c6841592 607 int i;
bee91169 608
8787b02e 609 /*
610 * The parameter defines a multiple of 100ns.
611 * We use multiples of 1000ns. So divide by 10.
612 */
7d963323 613 do_div(trigger_time, 10);
bee91169 614
c6841592 615 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
616 if (event != &efi_events[i])
617 continue;
bee91169 618
c6841592 619 if (!(event->type & EVT_TIMER))
620 break;
621 switch (type) {
622 case EFI_TIMER_STOP:
623 event->trigger_next = -1ULL;
624 break;
625 case EFI_TIMER_PERIODIC:
626 case EFI_TIMER_RELATIVE:
627 event->trigger_next =
8787b02e 628 timer_get_us() + trigger_time;
c6841592 629 break;
630 default:
bfc72462 631 return EFI_INVALID_PARAMETER;
c6841592 632 }
633 event->trigger_type = type;
634 event->trigger_time = trigger_time;
e190e897 635 event->is_signaled = false;
bfc72462 636 return EFI_SUCCESS;
bee91169 637 }
bfc72462 638 return EFI_INVALID_PARAMETER;
639}
640
332468f7
HS
641/*
642 * Set the trigger time for a timer event or stop the event.
643 *
644 * This function implements the SetTimer service.
645 * See the Unified Extensible Firmware Interface (UEFI) specification
646 * for details.
647 *
648 * @event event for which the timer is set
649 * @type type of the timer
650 * @trigger_time trigger period in multiples of 100ns
651 * @return status code
652 */
b521d29e 653static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
654 enum efi_timer_delay type,
655 uint64_t trigger_time)
bfc72462 656{
657 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
658 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
bee91169
AG
659}
660
332468f7
HS
661/*
662 * Wait for events to be signaled.
663 *
664 * This function implements the WaitForEvent service.
665 * See the Unified Extensible Firmware Interface (UEFI) specification
666 * for details.
667 *
668 * @num_events number of events to be waited for
669 * @events events to be waited for
670 * @index index of the event that was signaled
671 * @return status code
672 */
f5a2a938 673static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
2fd945fe 674 struct efi_event **event,
f5a2a938 675 efi_uintn_t *index)
bee91169 676{
c6841592 677 int i, j;
bee91169 678
f5a2a938 679 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
bee91169 680
c6841592 681 /* Check parameters */
682 if (!num_events || !event)
683 return EFI_EXIT(EFI_INVALID_PARAMETER);
32f4b2f8
HS
684 /* Check TPL */
685 if (efi_tpl != TPL_APPLICATION)
686 return EFI_EXIT(EFI_UNSUPPORTED);
c6841592 687 for (i = 0; i < num_events; ++i) {
688 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
689 if (event[i] == &efi_events[j])
690 goto known_event;
691 }
692 return EFI_EXIT(EFI_INVALID_PARAMETER);
693known_event:
694 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
695 return EFI_EXIT(EFI_INVALID_PARAMETER);
e190e897 696 if (!event[i]->is_signaled)
9bc9664d 697 efi_signal_event(event[i], true);
c6841592 698 }
699
700 /* Wait for signal */
701 for (;;) {
702 for (i = 0; i < num_events; ++i) {
e190e897 703 if (event[i]->is_signaled)
c6841592 704 goto out;
705 }
706 /* Allow events to occur. */
707 efi_timer_check();
708 }
709
710out:
711 /*
712 * Reset the signal which is passed to the caller to allow periodic
713 * events to occur.
714 */
e190e897 715 event[i]->is_signaled = false;
c6841592 716 if (index)
717 *index = i;
bee91169
AG
718
719 return EFI_EXIT(EFI_SUCCESS);
720}
721
332468f7
HS
722/*
723 * Signal an EFI event.
724 *
725 * This function implements the SignalEvent service.
726 * See the Unified Extensible Firmware Interface (UEFI) specification
727 * for details.
728 *
729 * This functions sets the signaled state of the event and queues the
730 * notification function for execution.
731 *
732 * @event event to signal
10a08c4e 733 * @return status code
332468f7 734 */
c6841592 735static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
bee91169 736{
c6841592 737 int i;
738
bee91169 739 EFI_ENTRY("%p", event);
c6841592 740 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
741 if (event != &efi_events[i])
742 continue;
e190e897 743 if (event->is_signaled)
ca62a4f5 744 break;
e190e897 745 event->is_signaled = true;
ca62a4f5 746 if (event->type & EVT_NOTIFY_SIGNAL)
9bc9664d 747 efi_signal_event(event, true);
c6841592 748 break;
749 }
bee91169
AG
750 return EFI_EXIT(EFI_SUCCESS);
751}
752
332468f7
HS
753/*
754 * Close an EFI event.
755 *
756 * This function implements the CloseEvent service.
757 * See the Unified Extensible Firmware Interface (UEFI) specification
758 * for details.
759 *
760 * @event event to close
761 * @return status code
762 */
2fd945fe 763static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
bee91169 764{
c6841592 765 int i;
766
bee91169 767 EFI_ENTRY("%p", event);
c6841592 768 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
769 if (event == &efi_events[i]) {
770 event->type = 0;
771 event->trigger_next = -1ULL;
e190e897
HS
772 event->is_queued = false;
773 event->is_signaled = false;
c6841592 774 return EFI_EXIT(EFI_SUCCESS);
775 }
776 }
777 return EFI_EXIT(EFI_INVALID_PARAMETER);
bee91169
AG
778}
779
332468f7
HS
780/*
781 * Check if an event is signaled.
782 *
783 * This function implements the CheckEvent service.
784 * See the Unified Extensible Firmware Interface (UEFI) specification
785 * for details.
786 *
787 * If an event is not signaled yet the notification function is queued.
788 *
789 * @event event to check
790 * @return status code
791 */
2fd945fe 792static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
bee91169 793{
c6841592 794 int i;
795
bee91169 796 EFI_ENTRY("%p", event);
c6841592 797 efi_timer_check();
798 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
799 if (event != &efi_events[i])
800 continue;
801 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
802 break;
e190e897 803 if (!event->is_signaled)
9bc9664d 804 efi_signal_event(event, true);
e190e897 805 if (event->is_signaled)
c6841592 806 return EFI_EXIT(EFI_SUCCESS);
807 return EFI_EXIT(EFI_NOT_READY);
808 }
809 return EFI_EXIT(EFI_INVALID_PARAMETER);
bee91169
AG
810}
811
7b9f8ad7
HS
812/*
813 * Find the internal EFI object for a handle.
814 *
815 * @handle handle to find
816 * @return EFI object
817 */
2074f700 818struct efi_object *efi_search_obj(const efi_handle_t handle)
7b9f8ad7 819{
1b68153a 820 struct efi_object *efiobj;
7b9f8ad7 821
1b68153a 822 list_for_each_entry(efiobj, &efi_obj_list, link) {
7b9f8ad7
HS
823 if (efiobj->handle == handle)
824 return efiobj;
825 }
826
827 return NULL;
828}
829
fe1599da
HS
830/*
831 * Create open protocol info entry and add it to a protocol.
832 *
833 * @handler handler of a protocol
834 * @return open protocol info entry
835 */
836static struct efi_open_protocol_info_entry *efi_create_open_info(
837 struct efi_handler *handler)
838{
839 struct efi_open_protocol_info_item *item;
840
841 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
842 if (!item)
843 return NULL;
844 /* Append the item to the open protocol info list. */
845 list_add_tail(&item->link, &handler->open_infos);
846
847 return &item->info;
848}
849
850/*
851 * Remove an open protocol info entry from a protocol.
852 *
853 * @handler handler of a protocol
854 * @return status code
855 */
856static efi_status_t efi_delete_open_info(
857 struct efi_open_protocol_info_item *item)
858{
859 list_del(&item->link);
860 free(item);
861 return EFI_SUCCESS;
862}
863
3f79a2b5
HS
864/*
865 * Install new protocol on a handle.
866 *
867 * @handle handle on which the protocol shall be installed
868 * @protocol GUID of the protocol to be installed
869 * @protocol_interface interface of the protocol implementation
870 * @return status code
871 */
2074f700
HS
872efi_status_t efi_add_protocol(const efi_handle_t handle,
873 const efi_guid_t *protocol,
3f79a2b5
HS
874 void *protocol_interface)
875{
876 struct efi_object *efiobj;
877 struct efi_handler *handler;
878 efi_status_t ret;
3f79a2b5
HS
879
880 efiobj = efi_search_obj(handle);
881 if (!efiobj)
882 return EFI_INVALID_PARAMETER;
883 ret = efi_search_protocol(handle, protocol, NULL);
884 if (ret != EFI_NOT_FOUND)
885 return EFI_INVALID_PARAMETER;
886 handler = calloc(1, sizeof(struct efi_handler));
887 if (!handler)
888 return EFI_OUT_OF_RESOURCES;
69fb6b1a
HS
889 handler->guid = protocol;
890 handler->protocol_interface = protocol_interface;
fe1599da 891 INIT_LIST_HEAD(&handler->open_infos);
69fb6b1a 892 list_add_tail(&handler->link, &efiobj->protocols);
d5504144
HS
893 if (!guidcmp(&efi_guid_device_path, protocol))
894 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
69fb6b1a 895 return EFI_SUCCESS;
3f79a2b5
HS
896}
897
332468f7
HS
898/*
899 * Install protocol interface.
900 *
1760ef57
HS
901 * This function implements the InstallProtocolInterface service.
902 * See the Unified Extensible Firmware Interface (UEFI) specification
903 * for details.
332468f7
HS
904 *
905 * @handle handle on which the protocol shall be installed
906 * @protocol GUID of the protocol to be installed
907 * @protocol_interface_type type of the interface to be installed,
908 * always EFI_NATIVE_INTERFACE
909 * @protocol_interface interface of the protocol implementation
910 * @return status code
911 */
1760ef57
HS
912static efi_status_t EFIAPI efi_install_protocol_interface(
913 void **handle, const efi_guid_t *protocol,
914 int protocol_interface_type, void *protocol_interface)
bee91169 915{
e0549f8a 916 efi_status_t r;
917
1760ef57
HS
918 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
919 protocol_interface);
920
e0549f8a 921 if (!handle || !protocol ||
922 protocol_interface_type != EFI_NATIVE_INTERFACE) {
923 r = EFI_INVALID_PARAMETER;
924 goto out;
925 }
926
927 /* Create new handle if requested. */
928 if (!*handle) {
3cc6e3fe
HS
929 r = efi_create_handle(handle);
930 if (r != EFI_SUCCESS)
931 goto out;
af1408e0
HS
932 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
933 *handle);
934 } else {
935 debug("%sEFI: handle %p\n", indent_string(nesting_level),
936 *handle);
e0549f8a 937 }
1202530d
HS
938 /* Add new protocol */
939 r = efi_add_protocol(*handle, protocol, protocol_interface);
e0549f8a 940out:
1760ef57 941 return EFI_EXIT(r);
bee91169 942}
e0549f8a 943
332468f7
HS
944/*
945 * Reinstall protocol interface.
946 *
947 * This function implements the ReinstallProtocolInterface service.
948 * See the Unified Extensible Firmware Interface (UEFI) specification
949 * for details.
950 *
951 * @handle handle on which the protocol shall be
952 * reinstalled
953 * @protocol GUID of the protocol to be installed
954 * @old_interface interface to be removed
955 * @new_interface interface to be installed
956 * @return status code
957 */
2074f700
HS
958static efi_status_t EFIAPI efi_reinstall_protocol_interface(
959 efi_handle_t handle, const efi_guid_t *protocol,
960 void *old_interface, void *new_interface)
bee91169 961{
778e6af8 962 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
bee91169
AG
963 new_interface);
964 return EFI_EXIT(EFI_ACCESS_DENIED);
965}
966
3f9b0042
HS
967/*
968 * Get all drivers associated to a controller.
969 * The allocated buffer has to be freed with free().
970 *
971 * @efiobj handle of the controller
972 * @protocol protocol guid (optional)
973 * @number_of_drivers number of child controllers
974 * @driver_handle_buffer handles of the the drivers
975 * @return status code
976 */
977static efi_status_t efi_get_drivers(struct efi_object *efiobj,
978 const efi_guid_t *protocol,
979 efi_uintn_t *number_of_drivers,
980 efi_handle_t **driver_handle_buffer)
981{
982 struct efi_handler *handler;
983 struct efi_open_protocol_info_item *item;
984 efi_uintn_t count = 0, i;
985 bool duplicate;
986
987 /* Count all driver associations */
988 list_for_each_entry(handler, &efiobj->protocols, link) {
989 if (protocol && guidcmp(handler->guid, protocol))
990 continue;
991 list_for_each_entry(item, &handler->open_infos, link) {
992 if (item->info.attributes &
993 EFI_OPEN_PROTOCOL_BY_DRIVER)
994 ++count;
995 }
996 }
997 /*
998 * Create buffer. In case of duplicate driver assignments the buffer
999 * will be too large. But that does not harm.
1000 */
1001 *number_of_drivers = 0;
1002 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1003 if (!*driver_handle_buffer)
1004 return EFI_OUT_OF_RESOURCES;
1005 /* Collect unique driver handles */
1006 list_for_each_entry(handler, &efiobj->protocols, link) {
1007 if (protocol && guidcmp(handler->guid, protocol))
1008 continue;
1009 list_for_each_entry(item, &handler->open_infos, link) {
1010 if (item->info.attributes &
1011 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1012 /* Check this is a new driver */
1013 duplicate = false;
1014 for (i = 0; i < *number_of_drivers; ++i) {
1015 if ((*driver_handle_buffer)[i] ==
1016 item->info.agent_handle)
1017 duplicate = true;
1018 }
1019 /* Copy handle to buffer */
1020 if (!duplicate) {
1021 i = (*number_of_drivers)++;
1022 (*driver_handle_buffer)[i] =
1023 item->info.agent_handle;
1024 }
1025 }
1026 }
1027 }
1028 return EFI_SUCCESS;
1029}
1030
1031/*
1032 * Disconnect all drivers from a controller.
1033 *
1034 * This function implements the DisconnectController service.
1035 * See the Unified Extensible Firmware Interface (UEFI) specification
1036 * for details.
1037 *
1038 * @efiobj handle of the controller
1039 * @protocol protocol guid (optional)
1040 * @child_handle handle of the child to destroy
1041 * @return status code
1042 */
1043static efi_status_t efi_disconnect_all_drivers(
1044 struct efi_object *efiobj,
1045 const efi_guid_t *protocol,
1046 efi_handle_t child_handle)
1047{
1048 efi_uintn_t number_of_drivers;
1049 efi_handle_t *driver_handle_buffer;
1050 efi_status_t r, ret;
1051
1052 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1053 &driver_handle_buffer);
1054 if (ret != EFI_SUCCESS)
1055 return ret;
1056
1057 ret = EFI_NOT_FOUND;
1058 while (number_of_drivers) {
1059 r = EFI_CALL(efi_disconnect_controller(
1060 efiobj->handle,
1061 driver_handle_buffer[--number_of_drivers],
1062 child_handle));
1063 if (r == EFI_SUCCESS)
1064 ret = r;
1065 }
1066 free(driver_handle_buffer);
1067 return ret;
1068}
1069
332468f7
HS
1070/*
1071 * Uninstall protocol interface.
1072 *
cd534083
HS
1073 * This function implements the UninstallProtocolInterface service.
1074 * See the Unified Extensible Firmware Interface (UEFI) specification
1075 * for details.
332468f7
HS
1076 *
1077 * @handle handle from which the protocol shall be removed
1078 * @protocol GUID of the protocol to be removed
1079 * @protocol_interface interface to be removed
1080 * @return status code
1081 */
cd534083 1082static efi_status_t EFIAPI efi_uninstall_protocol_interface(
2074f700 1083 efi_handle_t handle, const efi_guid_t *protocol,
cd534083 1084 void *protocol_interface)
bee91169 1085{
ad97373b 1086 struct efi_object *efiobj;
5810511d 1087 struct efi_handler *handler;
ad97373b
HS
1088 struct efi_open_protocol_info_item *item;
1089 struct efi_open_protocol_info_item *pos;
5810511d 1090 efi_status_t r;
4b6ed0d7 1091
cd534083
HS
1092 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1093
ad97373b
HS
1094 /* Check handle */
1095 efiobj = efi_search_obj(handle);
1096 if (!efiobj) {
4b6ed0d7 1097 r = EFI_INVALID_PARAMETER;
1098 goto out;
1099 }
5810511d
HS
1100 /* Find the protocol on the handle */
1101 r = efi_search_protocol(handle, protocol, &handler);
1102 if (r != EFI_SUCCESS)
1103 goto out;
ad97373b
HS
1104 /* Disconnect controllers */
1105 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1106 if (!list_empty(&handler->open_infos)) {
5810511d 1107 r = EFI_ACCESS_DENIED;
ad97373b
HS
1108 goto out;
1109 }
1110 /* Close protocol */
1111 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1112 if (item->info.attributes ==
1113 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1114 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1115 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1116 list_del(&item->link);
1117 }
1118 if (!list_empty(&handler->open_infos)) {
1119 r = EFI_ACCESS_DENIED;
1120 goto out;
4b6ed0d7 1121 }
ad97373b 1122 r = efi_remove_protocol(handle, protocol, protocol_interface);
4b6ed0d7 1123out:
cd534083 1124 return EFI_EXIT(r);
bee91169
AG
1125}
1126
332468f7
HS
1127/*
1128 * Register an event for notification when a protocol is installed.
1129 *
1130 * This function implements the RegisterProtocolNotify service.
1131 * See the Unified Extensible Firmware Interface (UEFI) specification
1132 * for details.
1133 *
1134 * @protocol GUID of the protocol whose installation shall be
1135 * notified
1136 * @event event to be signaled upon installation of the protocol
1137 * @registration key for retrieving the registration information
1138 * @return status code
1139 */
5a9682d0
HS
1140static efi_status_t EFIAPI efi_register_protocol_notify(
1141 const efi_guid_t *protocol,
1142 struct efi_event *event,
1143 void **registration)
bee91169 1144{
778e6af8 1145 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
bee91169
AG
1146 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1147}
1148
332468f7
HS
1149/*
1150 * Determine if an EFI handle implements a protocol.
1151 *
1152 * See the documentation of the LocateHandle service in the UEFI specification.
1153 *
1154 * @search_type selection criterion
1155 * @protocol GUID of the protocol
1156 * @search_key registration key
1157 * @efiobj handle
1158 * @return 0 if the handle implements the protocol
1159 */
bee91169 1160static int efi_search(enum efi_locate_search_type search_type,
5a9682d0 1161 const efi_guid_t *protocol, void *search_key,
bee91169
AG
1162 struct efi_object *efiobj)
1163{
42cf1242 1164 efi_status_t ret;
bee91169
AG
1165
1166 switch (search_type) {
9f0770ff 1167 case ALL_HANDLES:
bee91169 1168 return 0;
9f0770ff 1169 case BY_REGISTER_NOTIFY:
42cf1242 1170 /* TODO: RegisterProtocolNotify is not implemented yet */
bee91169 1171 return -1;
9f0770ff 1172 case BY_PROTOCOL:
42cf1242
HS
1173 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1174 return (ret != EFI_SUCCESS);
1175 default:
1176 /* Invalid search type */
bee91169
AG
1177 return -1;
1178 }
bee91169
AG
1179}
1180
332468f7
HS
1181/*
1182 * Locate handles implementing a protocol.
1183 *
1184 * This function is meant for U-Boot internal calls. For the API implementation
1185 * of the LocateHandle service see efi_locate_handle_ext.
1186 *
1187 * @search_type selection criterion
1188 * @protocol GUID of the protocol
1189 * @search_key registration key
1190 * @buffer_size size of the buffer to receive the handles in bytes
1191 * @buffer buffer to receive the relevant handles
1192 * @return status code
1193 */
ebf199b9 1194static efi_status_t efi_locate_handle(
bee91169 1195 enum efi_locate_search_type search_type,
5a9682d0 1196 const efi_guid_t *protocol, void *search_key,
f5a2a938 1197 efi_uintn_t *buffer_size, efi_handle_t *buffer)
bee91169 1198{
caf864e4 1199 struct efi_object *efiobj;
f5a2a938 1200 efi_uintn_t size = 0;
bee91169 1201
caf864e4
HS
1202 /* Check parameters */
1203 switch (search_type) {
1204 case ALL_HANDLES:
1205 break;
1206 case BY_REGISTER_NOTIFY:
1207 if (!search_key)
1208 return EFI_INVALID_PARAMETER;
1209 /* RegisterProtocolNotify is not implemented yet */
1210 return EFI_UNSUPPORTED;
1211 case BY_PROTOCOL:
1212 if (!protocol)
1213 return EFI_INVALID_PARAMETER;
1214 break;
1215 default:
1216 return EFI_INVALID_PARAMETER;
1217 }
1218
1219 /*
1220 * efi_locate_handle_buffer uses this function for
1221 * the calculation of the necessary buffer size.
1222 * So do not require a buffer for buffersize == 0.
1223 */
1224 if (!buffer_size || (*buffer_size && !buffer))
1225 return EFI_INVALID_PARAMETER;
1226
bee91169 1227 /* Count how much space we need */
caf864e4
HS
1228 list_for_each_entry(efiobj, &efi_obj_list, link) {
1229 if (!efi_search(search_type, protocol, search_key, efiobj))
bee91169 1230 size += sizeof(void*);
bee91169
AG
1231 }
1232
1233 if (*buffer_size < size) {
1234 *buffer_size = size;
26329584 1235 return EFI_BUFFER_TOO_SMALL;
bee91169
AG
1236 }
1237
796a78cb
RC
1238 *buffer_size = size;
1239 if (size == 0)
1240 return EFI_NOT_FOUND;
1241
bee91169 1242 /* Then fill the array */
caf864e4
HS
1243 list_for_each_entry(efiobj, &efi_obj_list, link) {
1244 if (!efi_search(search_type, protocol, search_key, efiobj))
1245 *buffer++ = efiobj->handle;
bee91169
AG
1246 }
1247
26329584 1248 return EFI_SUCCESS;
1249}
1250
332468f7
HS
1251/*
1252 * Locate handles implementing a protocol.
1253 *
1254 * This function implements the LocateHandle service.
1255 * See the Unified Extensible Firmware Interface (UEFI) specification
1256 * for details.
1257 *
1258 * @search_type selection criterion
1259 * @protocol GUID of the protocol
1260 * @search_key registration key
1261 * @buffer_size size of the buffer to receive the handles in bytes
1262 * @buffer buffer to receive the relevant handles
1263 * @return 0 if the handle implements the protocol
1264 */
26329584 1265static efi_status_t EFIAPI efi_locate_handle_ext(
1266 enum efi_locate_search_type search_type,
5a9682d0 1267 const efi_guid_t *protocol, void *search_key,
f5a2a938 1268 efi_uintn_t *buffer_size, efi_handle_t *buffer)
26329584 1269{
778e6af8 1270 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
26329584 1271 buffer_size, buffer);
1272
1273 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1274 buffer_size, buffer));
bee91169
AG
1275}
1276
d98cdf6a
AG
1277/* Collapses configuration table entries, removing index i */
1278static void efi_remove_configuration_table(int i)
1279{
1280 struct efi_configuration_table *this = &efi_conf_table[i];
1281 struct efi_configuration_table *next = &efi_conf_table[i+1];
1282 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1283
1284 memmove(this, next, (ulong)end - (ulong)next);
1285 systab.nr_tables--;
1286}
1287
332468f7
HS
1288/*
1289 * Adds, updates, or removes a configuration table.
1290 *
1291 * This function is used for internal calls. For the API implementation of the
1292 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1293 *
1294 * @guid GUID of the installed table
1295 * @table table to be installed
1296 * @return status code
1297 */
488bf12d 1298efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
bee91169
AG
1299{
1300 int i;
1301
bee91169
AG
1302 /* Check for guid override */
1303 for (i = 0; i < systab.nr_tables; i++) {
1304 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
d98cdf6a
AG
1305 if (table)
1306 efi_conf_table[i].table = table;
1307 else
1308 efi_remove_configuration_table(i);
488bf12d 1309 return EFI_SUCCESS;
bee91169
AG
1310 }
1311 }
1312
d98cdf6a
AG
1313 if (!table)
1314 return EFI_NOT_FOUND;
1315
bee91169
AG
1316 /* No override, check for overflow */
1317 if (i >= ARRAY_SIZE(efi_conf_table))
488bf12d 1318 return EFI_OUT_OF_RESOURCES;
bee91169
AG
1319
1320 /* Add a new entry */
1321 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1322 efi_conf_table[i].table = table;
aba5e919 1323 systab.nr_tables = i + 1;
bee91169 1324
488bf12d
AG
1325 return EFI_SUCCESS;
1326}
1327
332468f7
HS
1328/*
1329 * Adds, updates, or removes a configuration table.
1330 *
1331 * This function implements the InstallConfigurationTable service.
1332 * See the Unified Extensible Firmware Interface (UEFI) specification
1333 * for details.
1334 *
1335 * @guid GUID of the installed table
1336 * @table table to be installed
1337 * @return status code
1338 */
488bf12d
AG
1339static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1340 void *table)
1341{
778e6af8 1342 EFI_ENTRY("%pUl, %p", guid, table);
488bf12d 1343 return EFI_EXIT(efi_install_configuration_table(guid, table));
bee91169
AG
1344}
1345
332468f7
HS
1346/*
1347 * Initialize a loaded_image_info + loaded_image_info object with correct
95c5553e 1348 * protocols, boot-device, etc.
332468f7 1349 *
10a08c4e 1350 * @info loaded image info to be passed to the entry point of the
332468f7
HS
1351 * image
1352 * @obj internal object associated with the loaded image
1353 * @device_path device path of the loaded image
1354 * @file_path file path of the loaded image
56d92888 1355 * @return status code
95c5553e 1356 */
56d92888
HS
1357efi_status_t efi_setup_loaded_image(
1358 struct efi_loaded_image *info, struct efi_object *obj,
1359 struct efi_device_path *device_path,
1360 struct efi_device_path *file_path)
95c5553e 1361{
48b66230
HS
1362 efi_status_t ret;
1363
44549d62
HS
1364 /* Add internal object to object list */
1365 efi_add_handle(obj);
1366 /* efi_exit() assumes that the handle points to the info */
95c5553e
RC
1367 obj->handle = info;
1368
48b66230 1369 info->file_path = file_path;
48b66230 1370
7df5af6f
HS
1371 if (device_path) {
1372 info->device_handle = efi_dp_find_obj(device_path, NULL);
1373 /*
1374 * When asking for the device path interface, return
1375 * bootefi_device_path
1376 */
1377 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1378 device_path);
1379 if (ret != EFI_SUCCESS)
1380 goto failure;
1381 }
95c5553e
RC
1382
1383 /*
1384 * When asking for the loaded_image interface, just
1385 * return handle which points to loaded_image_info
1386 */
48b66230
HS
1387 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1388 if (ret != EFI_SUCCESS)
1389 goto failure;
95c5553e 1390
48b66230
HS
1391 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1392 (void *)&efi_console_control);
1393 if (ret != EFI_SUCCESS)
1394 goto failure;
95c5553e 1395
48b66230
HS
1396 ret = efi_add_protocol(obj->handle,
1397 &efi_guid_device_path_to_text_protocol,
1398 (void *)&efi_device_path_to_text);
1399 if (ret != EFI_SUCCESS)
1400 goto failure;
95c5553e 1401
56d92888 1402 return ret;
48b66230
HS
1403failure:
1404 printf("ERROR: Failure to install protocols for loaded image\n");
56d92888 1405 return ret;
95c5553e
RC
1406}
1407
332468f7
HS
1408/*
1409 * Load an image using a file path.
1410 *
1411 * @file_path the path of the image to load
1412 * @buffer buffer containing the loaded image
10a08c4e 1413 * @return status code
332468f7 1414 */
9975fe96
RC
1415efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1416 void **buffer)
838ee4b4
RC
1417{
1418 struct efi_file_info *info = NULL;
1419 struct efi_file_handle *f;
1420 static efi_status_t ret;
1421 uint64_t bs;
1422
1423 f = efi_file_from_path(file_path);
1424 if (!f)
1425 return EFI_DEVICE_ERROR;
1426
1427 bs = 0;
1428 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1429 &bs, info));
1430 if (ret == EFI_BUFFER_TOO_SMALL) {
1431 info = malloc(bs);
1432 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1433 &bs, info));
1434 }
1435 if (ret != EFI_SUCCESS)
1436 goto error;
1437
1438 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1439 if (ret)
1440 goto error;
1441
1442 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1443
1444error:
1445 free(info);
1446 EFI_CALL(f->close(f));
1447
1448 if (ret != EFI_SUCCESS) {
1449 efi_free_pool(*buffer);
1450 *buffer = NULL;
1451 }
1452
1453 return ret;
1454}
1455
332468f7
HS
1456/*
1457 * Load an EFI image into memory.
1458 *
1459 * This function implements the LoadImage service.
1460 * See the Unified Extensible Firmware Interface (UEFI) specification
1461 * for details.
1462 *
1463 * @boot_policy true for request originating from the boot manager
c8df80c5 1464 * @parent_image the caller's image handle
332468f7
HS
1465 * @file_path the path of the image to load
1466 * @source_buffer memory location from which the image is installed
1467 * @source_size size of the memory area from which the image is
1468 * installed
1469 * @image_handle handle for the newly installed image
1470 * @return status code
1471 */
bee91169
AG
1472static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1473 efi_handle_t parent_image,
1474 struct efi_device_path *file_path,
1475 void *source_buffer,
1476 unsigned long source_size,
1477 efi_handle_t *image_handle)
1478{
bee91169
AG
1479 struct efi_loaded_image *info;
1480 struct efi_object *obj;
b9b17598 1481 efi_status_t ret;
bee91169 1482
476ed96e 1483 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
bee91169 1484 file_path, source_buffer, source_size, image_handle);
838ee4b4
RC
1485
1486 info = calloc(1, sizeof(*info));
1487 obj = calloc(1, sizeof(*obj));
1488
1489 if (!source_buffer) {
1490 struct efi_device_path *dp, *fp;
838ee4b4 1491
9975fe96 1492 ret = efi_load_image_from_path(file_path, &source_buffer);
b9b17598
HS
1493 if (ret != EFI_SUCCESS)
1494 goto failure;
838ee4b4
RC
1495 /*
1496 * split file_path which contains both the device and
1497 * file parts:
1498 */
1499 efi_dp_split_file_path(file_path, &dp, &fp);
b9b17598
HS
1500 ret = efi_setup_loaded_image(info, obj, dp, fp);
1501 if (ret != EFI_SUCCESS)
1502 goto failure;
838ee4b4
RC
1503 } else {
1504 /* In this case, file_path is the "device" path, ie.
1505 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1506 */
b9b17598
HS
1507 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1508 if (ret != EFI_SUCCESS)
1509 goto failure;
838ee4b4 1510 }
bee91169
AG
1511 info->reserved = efi_load_pe(source_buffer, info);
1512 if (!info->reserved) {
b9b17598
HS
1513 ret = EFI_UNSUPPORTED;
1514 goto failure;
bee91169 1515 }
32fc2ac3
HS
1516 info->system_table = &systab;
1517 info->parent_handle = parent_image;
ea54ad59 1518 *image_handle = obj->handle;
bee91169 1519 return EFI_EXIT(EFI_SUCCESS);
b9b17598
HS
1520failure:
1521 free(info);
1522 efi_delete_handle(obj);
1523 return EFI_EXIT(ret);
bee91169
AG
1524}
1525
332468f7
HS
1526/*
1527 * Call the entry point of an image.
1528 *
1529 * This function implements the StartImage service.
1530 * See the Unified Extensible Firmware Interface (UEFI) specification
1531 * for details.
1532 *
1533 * @image_handle handle of the image
1534 * @exit_data_size size of the buffer
1535 * @exit_data buffer to receive the exit data of the called image
10a08c4e 1536 * @return status code
332468f7 1537 */
bee91169
AG
1538static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1539 unsigned long *exit_data_size,
1540 s16 **exit_data)
1541{
c6fa5df6
AG
1542 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1543 struct efi_system_table *st);
bee91169 1544 struct efi_loaded_image *info = image_handle;
727a1afb 1545 efi_status_t ret;
bee91169
AG
1546
1547 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1548 entry = info->reserved;
1549
1550 efi_is_direct_boot = false;
1551
1552 /* call the image! */
a86aeaf2 1553 if (setjmp(&info->exit_jmp)) {
727a1afb
HS
1554 /*
1555 * We called the entry point of the child image with EFI_CALL
1556 * in the lines below. The child image called the Exit() boot
1557 * service efi_exit() which executed the long jump that brought
1558 * us to the current line. This implies that the second half
1559 * of the EFI_CALL macro has not been executed.
1560 */
1561#ifdef CONFIG_ARM
1562 /*
1563 * efi_exit() called efi_restore_gd(). We have to undo this
1564 * otherwise __efi_entry_check() will put the wrong value into
1565 * app_gd.
1566 */
1567 gd = app_gd;
1568#endif
1569 /*
1570 * To get ready to call EFI_EXIT below we have to execute the
1571 * missed out steps of EFI_CALL.
1572 */
1573 assert(__efi_entry_check());
1574 debug("%sEFI: %lu returned by started image\n",
1575 __efi_nesting_dec(),
1576 (unsigned long)((uintptr_t)info->exit_status &
1577 ~EFI_ERROR_MASK));
a86aeaf2
AG
1578 return EFI_EXIT(info->exit_status);
1579 }
1580
727a1afb 1581 ret = EFI_CALL(entry(image_handle, &systab));
bee91169 1582
56672bf5
AG
1583 /*
1584 * Usually UEFI applications call Exit() instead of returning.
1585 * But because the world doesn not consist of ponies and unicorns,
1586 * we're happy to emulate that behavior on behalf of a payload
1587 * that forgot.
1588 */
1589 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
bee91169
AG
1590}
1591
332468f7
HS
1592/*
1593 * Leave an EFI application or driver.
1594 *
1595 * This function implements the Exit service.
1596 * See the Unified Extensible Firmware Interface (UEFI) specification
1597 * for details.
1598 *
1599 * @image_handle handle of the application or driver that is exiting
1600 * @exit_status status code
1601 * @exit_data_size size of the buffer in bytes
1602 * @exit_data buffer with data describing an error
10a08c4e 1603 * @return status code
332468f7 1604 */
a86aeaf2
AG
1605static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1606 efi_status_t exit_status, unsigned long exit_data_size,
1607 int16_t *exit_data)
bee91169 1608{
44549d62
HS
1609 /*
1610 * We require that the handle points to the original loaded
1611 * image protocol interface.
1612 *
1613 * For getting the longjmp address this is safer than locating
1614 * the protocol because the protocol may have been reinstalled
1615 * pointing to another memory location.
1616 *
1617 * TODO: We should call the unload procedure of the loaded
1618 * image protocol.
1619 */
a86aeaf2
AG
1620 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1621
bee91169
AG
1622 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1623 exit_data_size, exit_data);
a86aeaf2 1624
a148920e 1625 /* Make sure entry/exit counts for EFI world cross-overs match */
727a1afb 1626 EFI_EXIT(exit_status);
da94073b 1627
a148920e
AG
1628 /*
1629 * But longjmp out with the U-Boot gd, not the application's, as
1630 * the other end is a setjmp call inside EFI context.
1631 */
1632 efi_restore_gd();
1633
a86aeaf2 1634 loaded_image_info->exit_status = exit_status;
692fcdd8 1635 longjmp(&loaded_image_info->exit_jmp, 1);
a86aeaf2
AG
1636
1637 panic("EFI application exited");
bee91169
AG
1638}
1639
332468f7
HS
1640/*
1641 * Unload an EFI image.
1642 *
1643 * This function implements the UnloadImage service.
1644 * See the Unified Extensible Firmware Interface (UEFI) specification
1645 * for details.
1646 *
1647 * @image_handle handle of the image to be unloaded
1648 * @return status code
1649 */
2074f700 1650static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
bee91169
AG
1651{
1652 struct efi_object *efiobj;
1653
1654 EFI_ENTRY("%p", image_handle);
1655 efiobj = efi_search_obj(image_handle);
1656 if (efiobj)
1657 list_del(&efiobj->link);
1658
1659 return EFI_EXIT(EFI_SUCCESS);
1660}
1661
332468f7
HS
1662/*
1663 * Fix up caches for EFI payloads if necessary.
1664 */
bee91169
AG
1665static void efi_exit_caches(void)
1666{
1667#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1668 /*
1669 * Grub on 32bit ARM needs to have caches disabled before jumping into
1670 * a zImage, but does not know of all cache layers. Give it a hand.
1671 */
1672 if (efi_is_direct_boot)
1673 cleanup_before_linux();
1674#endif
1675}
1676
332468f7 1677/*
cc20ed03 1678 * Stop all boot services.
332468f7
HS
1679 *
1680 * This function implements the ExitBootServices service.
1681 * See the Unified Extensible Firmware Interface (UEFI) specification
1682 * for details.
1683 *
cc20ed03
HS
1684 * All timer events are disabled.
1685 * For exit boot services events the notification function is called.
1686 * The boot services are disabled in the system table.
1687 *
332468f7
HS
1688 * @image_handle handle of the loaded image
1689 * @map_key key of the memory map
1690 * @return status code
1691 */
2074f700 1692static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
bee91169
AG
1693 unsigned long map_key)
1694{
152a263c
HS
1695 int i;
1696
bee91169
AG
1697 EFI_ENTRY("%p, %ld", image_handle, map_key);
1698
cc20ed03
HS
1699 /* Make sure that notification functions are not called anymore */
1700 efi_tpl = TPL_HIGH_LEVEL;
1701
1702 /* Check if ExitBootServices has already been called */
1703 if (!systab.boottime)
1704 return EFI_EXIT(EFI_SUCCESS);
1705
152a263c
HS
1706 /* Notify that ExitBootServices is invoked. */
1707 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1708 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1709 continue;
cc20ed03
HS
1710 efi_events[i].is_signaled = true;
1711 efi_signal_event(&efi_events[i], false);
152a263c 1712 }
152a263c 1713
cc20ed03 1714 /* TODO Should persist EFI variables here */
ad644e7c 1715
b7b8410a
AG
1716 board_quiesce_devices();
1717
bee91169
AG
1718 /* Fix up caches for EFI payloads if necessary */
1719 efi_exit_caches();
1720
1721 /* This stops all lingering devices */
1722 bootm_disable_interrupts();
1723
cc20ed03
HS
1724 /* Disable boottime services */
1725 systab.con_in_handle = NULL;
1726 systab.con_in = NULL;
1727 systab.con_out_handle = NULL;
1728 systab.con_out = NULL;
1729 systab.stderr_handle = NULL;
1730 systab.std_err = NULL;
1731 systab.boottime = NULL;
1732
1733 /* Recalculate CRC32 */
1734 systab.hdr.crc32 = 0;
1735 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1736 sizeof(struct efi_system_table));
1737
bee91169 1738 /* Give the payload some time to boot */
b3d60900 1739 efi_set_watchdog(0);
bee91169
AG
1740 WATCHDOG_RESET();
1741
1742 return EFI_EXIT(EFI_SUCCESS);
1743}
1744
332468f7
HS
1745/*
1746 * Get next value of the counter.
1747 *
1748 * This function implements the NextMonotonicCount service.
1749 * See the Unified Extensible Firmware Interface (UEFI) specification
1750 * for details.
1751 *
1752 * @count returned value of the counter
1753 * @return status code
1754 */
bee91169
AG
1755static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1756{
1757 static uint64_t mono = 0;
1758 EFI_ENTRY("%p", count);
1759 *count = mono++;
1760 return EFI_EXIT(EFI_SUCCESS);
1761}
1762
332468f7
HS
1763/*
1764 * Sleep.
1765 *
1766 * This function implements the Stall sercive.
1767 * See the Unified Extensible Firmware Interface (UEFI) specification
1768 * for details.
1769 *
1770 * @microseconds period to sleep in microseconds
1771 * @return status code
1772 */
bee91169
AG
1773static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1774{
1775 EFI_ENTRY("%ld", microseconds);
1776 udelay(microseconds);
1777 return EFI_EXIT(EFI_SUCCESS);
1778}
1779
332468f7
HS
1780/*
1781 * Reset the watchdog timer.
1782 *
b3d60900 1783 * This function implements the SetWatchdogTimer service.
332468f7
HS
1784 * See the Unified Extensible Firmware Interface (UEFI) specification
1785 * for details.
1786 *
1787 * @timeout seconds before reset by watchdog
1788 * @watchdog_code code to be logged when resetting
1789 * @data_size size of buffer in bytes
1790 * @watchdog_data buffer with data describing the reset reason
10a08c4e 1791 * @return status code
332468f7 1792 */
bee91169
AG
1793static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1794 uint64_t watchdog_code,
1795 unsigned long data_size,
1796 uint16_t *watchdog_data)
1797{
1798 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1799 data_size, watchdog_data);
b3d60900 1800 return EFI_EXIT(efi_set_watchdog(timeout));
bee91169
AG
1801}
1802
332468f7
HS
1803/*
1804 * Close a protocol.
1805 *
1806 * This function implements the CloseProtocol service.
1807 * See the Unified Extensible Firmware Interface (UEFI) specification
1808 * for details.
1809 *
1810 * @handle handle on which the protocol shall be closed
1811 * @protocol GUID of the protocol to close
1812 * @agent_handle handle of the driver
1813 * @controller_handle handle of the controller
1814 * @return status code
1815 */
2074f700 1816static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
5a9682d0 1817 const efi_guid_t *protocol,
2074f700
HS
1818 efi_handle_t agent_handle,
1819 efi_handle_t controller_handle)
bee91169 1820{
3b8a489c
HS
1821 struct efi_handler *handler;
1822 struct efi_open_protocol_info_item *item;
1823 struct efi_open_protocol_info_item *pos;
1824 efi_status_t r;
1825
778e6af8 1826 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
bee91169 1827 controller_handle);
3b8a489c
HS
1828
1829 if (!agent_handle) {
1830 r = EFI_INVALID_PARAMETER;
1831 goto out;
1832 }
1833 r = efi_search_protocol(handle, protocol, &handler);
1834 if (r != EFI_SUCCESS)
1835 goto out;
1836
1837 r = EFI_NOT_FOUND;
1838 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1839 if (item->info.agent_handle == agent_handle &&
1840 item->info.controller_handle == controller_handle) {
1841 efi_delete_open_info(item);
1842 r = EFI_SUCCESS;
1843 break;
1844 }
1845 }
1846out:
1847 return EFI_EXIT(r);
bee91169
AG
1848}
1849
332468f7
HS
1850/*
1851 * Provide information about then open status of a protocol on a handle
1852 *
1853 * This function implements the OpenProtocolInformation service.
1854 * See the Unified Extensible Firmware Interface (UEFI) specification
1855 * for details.
1856 *
1857 * @handle handle for which the information shall be retrieved
1858 * @protocol GUID of the protocol
1859 * @entry_buffer buffer to receive the open protocol information
1860 * @entry_count number of entries available in the buffer
1861 * @return status code
1862 */
bee91169 1863static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
5a9682d0 1864 const efi_guid_t *protocol,
bee91169 1865 struct efi_open_protocol_info_entry **entry_buffer,
f5a2a938 1866 efi_uintn_t *entry_count)
bee91169 1867{
e3fbbc36
HS
1868 unsigned long buffer_size;
1869 unsigned long count;
1870 struct efi_handler *handler;
1871 struct efi_open_protocol_info_item *item;
1872 efi_status_t r;
1873
778e6af8 1874 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
bee91169 1875 entry_count);
e3fbbc36
HS
1876
1877 /* Check parameters */
1878 if (!entry_buffer) {
1879 r = EFI_INVALID_PARAMETER;
1880 goto out;
1881 }
1882 r = efi_search_protocol(handle, protocol, &handler);
1883 if (r != EFI_SUCCESS)
1884 goto out;
1885
1886 /* Count entries */
1887 count = 0;
1888 list_for_each_entry(item, &handler->open_infos, link) {
1889 if (item->info.open_count)
1890 ++count;
1891 }
1892 *entry_count = count;
1893 *entry_buffer = NULL;
1894 if (!count) {
1895 r = EFI_SUCCESS;
1896 goto out;
1897 }
1898
1899 /* Copy entries */
1900 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1901 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1902 (void **)entry_buffer);
1903 if (r != EFI_SUCCESS)
1904 goto out;
1905 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1906 if (item->info.open_count)
1907 (*entry_buffer)[--count] = item->info;
1908 }
1909out:
1910 return EFI_EXIT(r);
bee91169
AG
1911}
1912
332468f7
HS
1913/*
1914 * Get protocols installed on a handle.
1915 *
1916 * This function implements the ProtocolsPerHandleService.
1917 * See the Unified Extensible Firmware Interface (UEFI) specification
1918 * for details.
1919 *
1920 * @handle handle for which the information is retrieved
1921 * @protocol_buffer buffer with protocol GUIDs
1922 * @protocol_buffer_count number of entries in the buffer
10a08c4e 1923 * @return status code
332468f7 1924 */
2074f700
HS
1925static efi_status_t EFIAPI efi_protocols_per_handle(
1926 efi_handle_t handle, efi_guid_t ***protocol_buffer,
f5a2a938 1927 efi_uintn_t *protocol_buffer_count)
bee91169 1928{
c0ebfc86 1929 unsigned long buffer_size;
1930 struct efi_object *efiobj;
69fb6b1a 1931 struct list_head *protocol_handle;
c0ebfc86 1932 efi_status_t r;
1933
bee91169
AG
1934 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1935 protocol_buffer_count);
c0ebfc86 1936
1937 if (!handle || !protocol_buffer || !protocol_buffer_count)
1938 return EFI_EXIT(EFI_INVALID_PARAMETER);
1939
1940 *protocol_buffer = NULL;
661c8327 1941 *protocol_buffer_count = 0;
c0ebfc86 1942
69fb6b1a
HS
1943 efiobj = efi_search_obj(handle);
1944 if (!efiobj)
1945 return EFI_EXIT(EFI_INVALID_PARAMETER);
c0ebfc86 1946
69fb6b1a
HS
1947 /* Count protocols */
1948 list_for_each(protocol_handle, &efiobj->protocols) {
1949 ++*protocol_buffer_count;
1950 }
1951
1952 /* Copy guids */
1953 if (*protocol_buffer_count) {
1954 size_t j = 0;
1955
1956 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1957 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1958 (void **)protocol_buffer);
1959 if (r != EFI_SUCCESS)
1960 return EFI_EXIT(r);
1961 list_for_each(protocol_handle, &efiobj->protocols) {
1962 struct efi_handler *protocol;
1963
1964 protocol = list_entry(protocol_handle,
1965 struct efi_handler, link);
1966 (*protocol_buffer)[j] = (void *)protocol->guid;
1967 ++j;
c0ebfc86 1968 }
c0ebfc86 1969 }
1970
1971 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
1972}
1973
332468f7
HS
1974/*
1975 * Locate handles implementing a protocol.
1976 *
1977 * This function implements the LocateHandleBuffer service.
1978 * See the Unified Extensible Firmware Interface (UEFI) specification
1979 * for details.
1980 *
1981 * @search_type selection criterion
1982 * @protocol GUID of the protocol
1983 * @search_key registration key
1984 * @no_handles number of returned handles
1985 * @buffer buffer with the returned handles
1986 * @return status code
1987 */
bee91169
AG
1988static efi_status_t EFIAPI efi_locate_handle_buffer(
1989 enum efi_locate_search_type search_type,
5a9682d0 1990 const efi_guid_t *protocol, void *search_key,
f5a2a938 1991 efi_uintn_t *no_handles, efi_handle_t **buffer)
bee91169 1992{
c2e703f9 1993 efi_status_t r;
f5a2a938 1994 efi_uintn_t buffer_size = 0;
c2e703f9 1995
778e6af8 1996 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
bee91169 1997 no_handles, buffer);
c2e703f9 1998
1999 if (!no_handles || !buffer) {
2000 r = EFI_INVALID_PARAMETER;
2001 goto out;
2002 }
2003 *no_handles = 0;
2004 *buffer = NULL;
2005 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2006 *buffer);
2007 if (r != EFI_BUFFER_TOO_SMALL)
2008 goto out;
2009 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2010 (void **)buffer);
2011 if (r != EFI_SUCCESS)
2012 goto out;
2013 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2014 *buffer);
2015 if (r == EFI_SUCCESS)
2074f700 2016 *no_handles = buffer_size / sizeof(efi_handle_t);
c2e703f9 2017out:
2018 return EFI_EXIT(r);
bee91169
AG
2019}
2020
332468f7
HS
2021/*
2022 * Find an interface implementing a protocol.
2023 *
2024 * This function implements the LocateProtocol service.
2025 * See the Unified Extensible Firmware Interface (UEFI) specification
2026 * for details.
2027 *
2028 * @protocol GUID of the protocol
2029 * @registration registration key passed to the notification function
2030 * @protocol_interface interface implementing the protocol
10a08c4e 2031 * @return status code
332468f7 2032 */
5a9682d0 2033static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
bee91169
AG
2034 void *registration,
2035 void **protocol_interface)
2036{
88adae5e 2037 struct list_head *lhandle;
9172cd91 2038 efi_status_t ret;
bee91169 2039
778e6af8 2040 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
88adae5e 2041
2042 if (!protocol || !protocol_interface)
2043 return EFI_EXIT(EFI_INVALID_PARAMETER);
2044
2045 list_for_each(lhandle, &efi_obj_list) {
2046 struct efi_object *efiobj;
9172cd91 2047 struct efi_handler *handler;
88adae5e 2048
2049 efiobj = list_entry(lhandle, struct efi_object, link);
9172cd91
HS
2050
2051 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2052 if (ret == EFI_SUCCESS) {
2053 *protocol_interface = handler->protocol_interface;
2054 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2055 }
2056 }
88adae5e 2057 *protocol_interface = NULL;
bee91169
AG
2058
2059 return EFI_EXIT(EFI_NOT_FOUND);
2060}
2061
ae2c85c1
HS
2062/*
2063 * Get the device path and handle of an device implementing a protocol.
2064 *
2065 * This function implements the LocateDevicePath service.
2066 * See the Unified Extensible Firmware Interface (UEFI) specification
2067 * for details.
2068 *
2069 * @protocol GUID of the protocol
2070 * @device_path device path
2071 * @device handle of the device
2072 * @return status code
2073 */
2074static efi_status_t EFIAPI efi_locate_device_path(
2075 const efi_guid_t *protocol,
2076 struct efi_device_path **device_path,
2077 efi_handle_t *device)
2078{
2079 struct efi_device_path *dp;
2080 size_t i;
2081 struct efi_handler *handler;
2082 efi_handle_t *handles;
2083 size_t len, len_dp;
2084 size_t len_best = 0;
2085 efi_uintn_t no_handles;
2086 u8 *remainder;
2087 efi_status_t ret;
2088
2089 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2090
2091 if (!protocol || !device_path || !*device_path || !device) {
2092 ret = EFI_INVALID_PARAMETER;
2093 goto out;
2094 }
2095
2096 /* Find end of device path */
2097 len = efi_dp_size(*device_path);
2098
2099 /* Get all handles implementing the protocol */
2100 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2101 &no_handles, &handles));
2102 if (ret != EFI_SUCCESS)
2103 goto out;
2104
2105 for (i = 0; i < no_handles; ++i) {
2106 /* Find the device path protocol */
2107 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2108 &handler);
2109 if (ret != EFI_SUCCESS)
2110 continue;
2111 dp = (struct efi_device_path *)handler->protocol_interface;
2112 len_dp = efi_dp_size(dp);
2113 /*
2114 * This handle can only be a better fit
2115 * if its device path length is longer than the best fit and
2116 * if its device path length is shorter of equal the searched
2117 * device path.
2118 */
2119 if (len_dp <= len_best || len_dp > len)
2120 continue;
2121 /* Check if dp is a subpath of device_path */
2122 if (memcmp(*device_path, dp, len_dp))
2123 continue;
2124 *device = handles[i];
2125 len_best = len_dp;
2126 }
2127 if (len_best) {
2128 remainder = (u8 *)*device_path + len_best;
2129 *device_path = (struct efi_device_path *)remainder;
2130 ret = EFI_SUCCESS;
2131 } else {
2132 ret = EFI_NOT_FOUND;
2133 }
2134out:
2135 return EFI_EXIT(ret);
2136}
2137
332468f7
HS
2138/*
2139 * Install multiple protocol interfaces.
2140 *
2141 * This function implements the MultipleProtocolInterfaces service.
2142 * See the Unified Extensible Firmware Interface (UEFI) specification
2143 * for details.
2144 *
2145 * @handle handle on which the protocol interfaces shall be installed
2146 * @... NULL terminated argument list with pairs of protocol GUIDS and
2147 * interfaces
2148 * @return status code
2149 */
bee91169
AG
2150static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2151 void **handle, ...)
2152{
2153 EFI_ENTRY("%p", handle);
58b83586 2154
2155 va_list argptr;
5a9682d0 2156 const efi_guid_t *protocol;
58b83586 2157 void *protocol_interface;
2158 efi_status_t r = EFI_SUCCESS;
2159 int i = 0;
2160
2161 if (!handle)
2162 return EFI_EXIT(EFI_INVALID_PARAMETER);
2163
2164 va_start(argptr, handle);
2165 for (;;) {
2166 protocol = va_arg(argptr, efi_guid_t*);
2167 if (!protocol)
2168 break;
2169 protocol_interface = va_arg(argptr, void*);
1760ef57
HS
2170 r = EFI_CALL(efi_install_protocol_interface(
2171 handle, protocol,
2172 EFI_NATIVE_INTERFACE,
2173 protocol_interface));
58b83586 2174 if (r != EFI_SUCCESS)
2175 break;
2176 i++;
2177 }
2178 va_end(argptr);
2179 if (r == EFI_SUCCESS)
2180 return EFI_EXIT(r);
2181
62471e46 2182 /* If an error occurred undo all changes. */
58b83586 2183 va_start(argptr, handle);
2184 for (; i; --i) {
2185 protocol = va_arg(argptr, efi_guid_t*);
2186 protocol_interface = va_arg(argptr, void*);
cd534083
HS
2187 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2188 protocol_interface));
58b83586 2189 }
2190 va_end(argptr);
2191
2192 return EFI_EXIT(r);
bee91169
AG
2193}
2194
332468f7
HS
2195/*
2196 * Uninstall multiple protocol interfaces.
2197 *
2198 * This function implements the UninstallMultipleProtocolInterfaces service.
2199 * See the Unified Extensible Firmware Interface (UEFI) specification
2200 * for details.
2201 *
2202 * @handle handle from which the protocol interfaces shall be removed
2203 * @... NULL terminated argument list with pairs of protocol GUIDS and
2204 * interfaces
2205 * @return status code
2206 */
bee91169
AG
2207static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2208 void *handle, ...)
2209{
2210 EFI_ENTRY("%p", handle);
843ce54c
HS
2211
2212 va_list argptr;
2213 const efi_guid_t *protocol;
2214 void *protocol_interface;
2215 efi_status_t r = EFI_SUCCESS;
2216 size_t i = 0;
2217
2218 if (!handle)
2219 return EFI_EXIT(EFI_INVALID_PARAMETER);
2220
2221 va_start(argptr, handle);
2222 for (;;) {
2223 protocol = va_arg(argptr, efi_guid_t*);
2224 if (!protocol)
2225 break;
2226 protocol_interface = va_arg(argptr, void*);
2227 r = EFI_CALL(efi_uninstall_protocol_interface(
2228 handle, protocol,
2229 protocol_interface));
2230 if (r != EFI_SUCCESS)
2231 break;
2232 i++;
2233 }
2234 va_end(argptr);
2235 if (r == EFI_SUCCESS)
2236 return EFI_EXIT(r);
2237
2238 /* If an error occurred undo all changes. */
2239 va_start(argptr, handle);
2240 for (; i; --i) {
2241 protocol = va_arg(argptr, efi_guid_t*);
2242 protocol_interface = va_arg(argptr, void*);
2243 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2244 EFI_NATIVE_INTERFACE,
2245 protocol_interface));
2246 }
2247 va_end(argptr);
2248
2249 return EFI_EXIT(r);
bee91169
AG
2250}
2251
332468f7
HS
2252/*
2253 * Calculate cyclic redundancy code.
2254 *
2255 * This function implements the CalculateCrc32 service.
2256 * See the Unified Extensible Firmware Interface (UEFI) specification
2257 * for details.
2258 *
2259 * @data buffer with data
2260 * @data_size size of buffer in bytes
2261 * @crc32_p cyclic redundancy code
2262 * @return status code
2263 */
bee91169
AG
2264static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2265 unsigned long data_size,
2266 uint32_t *crc32_p)
2267{
2268 EFI_ENTRY("%p, %ld", data, data_size);
2269 *crc32_p = crc32(0, data, data_size);
2270 return EFI_EXIT(EFI_SUCCESS);
2271}
2272
332468f7
HS
2273/*
2274 * Copy memory.
2275 *
2276 * This function implements the CopyMem service.
2277 * See the Unified Extensible Firmware Interface (UEFI) specification
2278 * for details.
2279 *
2280 * @destination destination of the copy operation
2281 * @source source of the copy operation
2282 * @length number of bytes to copy
2283 */
fc05a959
HS
2284static void EFIAPI efi_copy_mem(void *destination, const void *source,
2285 size_t length)
bee91169 2286{
fc05a959 2287 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
bee91169 2288 memcpy(destination, source, length);
f7c78176 2289 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2290}
2291
332468f7
HS
2292/*
2293 * Fill memory with a byte value.
2294 *
2295 * This function implements the SetMem service.
2296 * See the Unified Extensible Firmware Interface (UEFI) specification
2297 * for details.
2298 *
2299 * @buffer buffer to fill
2300 * @size size of buffer in bytes
2301 * @value byte to copy to the buffer
2302 */
fc05a959 2303static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
bee91169 2304{
fc05a959 2305 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
bee91169 2306 memset(buffer, value, size);
f7c78176 2307 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2308}
2309
191a41cc
HS
2310/*
2311 * Open protocol interface on a handle.
2312 *
2313 * @handler handler of a protocol
2314 * @protocol_interface interface implementing the protocol
2315 * @agent_handle handle of the driver
2316 * @controller_handle handle of the controller
2317 * @attributes attributes indicating how to open the protocol
2318 * @return status code
2319 */
2320static efi_status_t efi_protocol_open(
2321 struct efi_handler *handler,
2322 void **protocol_interface, void *agent_handle,
2323 void *controller_handle, uint32_t attributes)
2324{
2325 struct efi_open_protocol_info_item *item;
2326 struct efi_open_protocol_info_entry *match = NULL;
2327 bool opened_by_driver = false;
2328 bool opened_exclusive = false;
2329
2330 /* If there is no agent, only return the interface */
2331 if (!agent_handle)
2332 goto out;
2333
2334 /* For TEST_PROTOCOL ignore interface attribute */
2335 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2336 *protocol_interface = NULL;
2337
2338 /*
2339 * Check if the protocol is already opened by a driver with the same
2340 * attributes or opened exclusively
2341 */
2342 list_for_each_entry(item, &handler->open_infos, link) {
2343 if (item->info.agent_handle == agent_handle) {
2344 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2345 (item->info.attributes == attributes))
2346 return EFI_ALREADY_STARTED;
2347 }
2348 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2349 opened_exclusive = true;
2350 }
2351
2352 /* Only one controller can open the protocol exclusively */
2353 if (opened_exclusive && attributes &
2354 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2355 return EFI_ACCESS_DENIED;
2356
2357 /* Prepare exclusive opening */
2358 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2359 /* Try to disconnect controllers */
2360 list_for_each_entry(item, &handler->open_infos, link) {
2361 if (item->info.attributes ==
2362 EFI_OPEN_PROTOCOL_BY_DRIVER)
2363 EFI_CALL(efi_disconnect_controller(
2364 item->info.controller_handle,
2365 item->info.agent_handle,
2366 NULL));
2367 }
2368 opened_by_driver = false;
2369 /* Check if all controllers are disconnected */
2370 list_for_each_entry(item, &handler->open_infos, link) {
2371 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2372 opened_by_driver = true;
2373 }
2374 /* Only one controller can be conncected */
2375 if (opened_by_driver)
2376 return EFI_ACCESS_DENIED;
2377 }
2378
2379 /* Find existing entry */
2380 list_for_each_entry(item, &handler->open_infos, link) {
2381 if (item->info.agent_handle == agent_handle &&
2382 item->info.controller_handle == controller_handle)
2383 match = &item->info;
2384 }
2385 /* None found, create one */
2386 if (!match) {
2387 match = efi_create_open_info(handler);
2388 if (!match)
2389 return EFI_OUT_OF_RESOURCES;
2390 }
2391
2392 match->agent_handle = agent_handle;
2393 match->controller_handle = controller_handle;
2394 match->attributes = attributes;
2395 match->open_count++;
2396
2397out:
2398 /* For TEST_PROTOCOL ignore interface attribute. */
2399 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2400 *protocol_interface = handler->protocol_interface;
2401
2402 return EFI_SUCCESS;
2403}
2404
332468f7
HS
2405/*
2406 * Open protocol interface on a handle.
2407 *
2408 * This function implements the OpenProtocol interface.
2409 * See the Unified Extensible Firmware Interface (UEFI) specification
2410 * for details.
2411 *
2412 * @handle handle on which the protocol shall be opened
2413 * @protocol GUID of the protocol
2414 * @protocol_interface interface implementing the protocol
2415 * @agent_handle handle of the driver
2416 * @controller_handle handle of the controller
2417 * @attributes attributes indicating how to open the protocol
2418 * @return status code
2419 */
bee91169 2420static efi_status_t EFIAPI efi_open_protocol(
5a9682d0 2421 void *handle, const efi_guid_t *protocol,
bee91169
AG
2422 void **protocol_interface, void *agent_handle,
2423 void *controller_handle, uint32_t attributes)
2424{
80286e8f 2425 struct efi_handler *handler;
69baec67 2426 efi_status_t r = EFI_INVALID_PARAMETER;
bee91169 2427
778e6af8 2428 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
bee91169
AG
2429 protocol_interface, agent_handle, controller_handle,
2430 attributes);
b5349f74 2431
69baec67 2432 if (!handle || !protocol ||
2433 (!protocol_interface && attributes !=
2434 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2435 goto out;
2436 }
2437
2438 switch (attributes) {
2439 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2440 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2441 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2442 break;
2443 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2444 if (controller_handle == handle)
2445 goto out;
191a41cc 2446 /* fall-through */
69baec67 2447 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2448 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
191a41cc
HS
2449 /* Check that the controller handle is valid */
2450 if (!efi_search_obj(controller_handle))
69baec67 2451 goto out;
191a41cc 2452 /* fall-through */
69baec67 2453 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
191a41cc
HS
2454 /* Check that the agent handle is valid */
2455 if (!efi_search_obj(agent_handle))
69baec67 2456 goto out;
2457 break;
2458 default:
b5349f74 2459 goto out;
2460 }
2461
80286e8f
HS
2462 r = efi_search_protocol(handle, protocol, &handler);
2463 if (r != EFI_SUCCESS)
2464 goto out;
bee91169 2465
191a41cc
HS
2466 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2467 controller_handle, attributes);
bee91169
AG
2468out:
2469 return EFI_EXIT(r);
2470}
2471
332468f7
HS
2472/*
2473 * Get interface of a protocol on a handle.
2474 *
2475 * This function implements the HandleProtocol service.
2476 * See the Unified Extensible Firmware Interface (UEFI) specification
2477 * for details.
2478 *
2479 * @handle handle on which the protocol shall be opened
2480 * @protocol GUID of the protocol
2481 * @protocol_interface interface implementing the protocol
2482 * @return status code
2483 */
2074f700 2484static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
5a9682d0 2485 const efi_guid_t *protocol,
bee91169
AG
2486 void **protocol_interface)
2487{
8e1d329f 2488 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2489 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
bee91169
AG
2490}
2491
f0959dbe
HS
2492static efi_status_t efi_bind_controller(
2493 efi_handle_t controller_handle,
2494 efi_handle_t driver_image_handle,
2495 struct efi_device_path *remain_device_path)
2496{
2497 struct efi_driver_binding_protocol *binding_protocol;
2498 efi_status_t r;
2499
2500 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2501 &efi_guid_driver_binding_protocol,
2502 (void **)&binding_protocol,
2503 driver_image_handle, NULL,
2504 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2505 if (r != EFI_SUCCESS)
2506 return r;
2507 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2508 controller_handle,
2509 remain_device_path));
2510 if (r == EFI_SUCCESS)
2511 r = EFI_CALL(binding_protocol->start(binding_protocol,
2512 controller_handle,
2513 remain_device_path));
2514 EFI_CALL(efi_close_protocol(driver_image_handle,
2515 &efi_guid_driver_binding_protocol,
2516 driver_image_handle, NULL));
2517 return r;
2518}
2519
2520static efi_status_t efi_connect_single_controller(
2521 efi_handle_t controller_handle,
2522 efi_handle_t *driver_image_handle,
2523 struct efi_device_path *remain_device_path)
2524{
2525 efi_handle_t *buffer;
2526 size_t count;
2527 size_t i;
2528 efi_status_t r;
2529 size_t connected = 0;
2530
2531 /* Get buffer with all handles with driver binding protocol */
2532 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2533 &efi_guid_driver_binding_protocol,
2534 NULL, &count, &buffer));
2535 if (r != EFI_SUCCESS)
2536 return r;
2537
2538 /* Context Override */
2539 if (driver_image_handle) {
2540 for (; *driver_image_handle; ++driver_image_handle) {
2541 for (i = 0; i < count; ++i) {
2542 if (buffer[i] == *driver_image_handle) {
2543 buffer[i] = NULL;
2544 r = efi_bind_controller(
2545 controller_handle,
2546 *driver_image_handle,
2547 remain_device_path);
2548 /*
2549 * For drivers that do not support the
2550 * controller or are already connected
2551 * we receive an error code here.
2552 */
2553 if (r == EFI_SUCCESS)
2554 ++connected;
2555 }
2556 }
2557 }
2558 }
2559
2560 /*
2561 * TODO: Some overrides are not yet implemented:
2562 * - Platform Driver Override
2563 * - Driver Family Override Search
2564 * - Bus Specific Driver Override
2565 */
2566
2567 /* Driver Binding Search */
2568 for (i = 0; i < count; ++i) {
2569 if (buffer[i]) {
2570 r = efi_bind_controller(controller_handle,
2571 buffer[i],
2572 remain_device_path);
2573 if (r == EFI_SUCCESS)
2574 ++connected;
2575 }
2576 }
2577
2578 efi_free_pool(buffer);
2579 if (!connected)
2580 return EFI_NOT_FOUND;
2581 return EFI_SUCCESS;
2582}
2583
2584/*
2585 * Connect a controller to a driver.
2586 *
2587 * This function implements the ConnectController service.
2588 * See the Unified Extensible Firmware Interface (UEFI) specification
2589 * for details.
2590 *
2591 * First all driver binding protocol handles are tried for binding drivers.
2592 * Afterwards all handles that have openened a protocol of the controller
2593 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2594 *
2595 * @controller_handle handle of the controller
2596 * @driver_image_handle handle of the driver
2597 * @remain_device_path device path of a child controller
2598 * @recursive true to connect all child controllers
2599 * @return status code
2600 */
2601static efi_status_t EFIAPI efi_connect_controller(
2602 efi_handle_t controller_handle,
2603 efi_handle_t *driver_image_handle,
2604 struct efi_device_path *remain_device_path,
2605 bool recursive)
2606{
2607 efi_status_t r;
2608 efi_status_t ret = EFI_NOT_FOUND;
2609 struct efi_object *efiobj;
2610
2611 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2612 remain_device_path, recursive);
2613
2614 efiobj = efi_search_obj(controller_handle);
2615 if (!efiobj) {
2616 ret = EFI_INVALID_PARAMETER;
2617 goto out;
2618 }
2619
2620 r = efi_connect_single_controller(controller_handle,
2621 driver_image_handle,
2622 remain_device_path);
2623 if (r == EFI_SUCCESS)
2624 ret = EFI_SUCCESS;
2625 if (recursive) {
2626 struct efi_handler *handler;
2627 struct efi_open_protocol_info_item *item;
2628
2629 list_for_each_entry(handler, &efiobj->protocols, link) {
2630 list_for_each_entry(item, &handler->open_infos, link) {
2631 if (item->info.attributes &
2632 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2633 r = EFI_CALL(efi_connect_controller(
2634 item->info.controller_handle,
2635 driver_image_handle,
2636 remain_device_path,
2637 recursive));
2638 if (r == EFI_SUCCESS)
2639 ret = EFI_SUCCESS;
2640 }
2641 }
2642 }
2643 }
2644 /* Check for child controller specified by end node */
2645 if (ret != EFI_SUCCESS && remain_device_path &&
2646 remain_device_path->type == DEVICE_PATH_TYPE_END)
2647 ret = EFI_SUCCESS;
2648out:
2649 return EFI_EXIT(ret);
2650}
2651
3f9b0042
HS
2652/*
2653 * Get all child controllers associated to a driver.
2654 * The allocated buffer has to be freed with free().
2655 *
2656 * @efiobj handle of the controller
2657 * @driver_handle handle of the driver
2658 * @number_of_children number of child controllers
2659 * @child_handle_buffer handles of the the child controllers
2660 */
2661static efi_status_t efi_get_child_controllers(
2662 struct efi_object *efiobj,
2663 efi_handle_t driver_handle,
2664 efi_uintn_t *number_of_children,
2665 efi_handle_t **child_handle_buffer)
2666{
2667 struct efi_handler *handler;
2668 struct efi_open_protocol_info_item *item;
2669 efi_uintn_t count = 0, i;
2670 bool duplicate;
2671
2672 /* Count all child controller associations */
2673 list_for_each_entry(handler, &efiobj->protocols, link) {
2674 list_for_each_entry(item, &handler->open_infos, link) {
2675 if (item->info.agent_handle == driver_handle &&
2676 item->info.attributes &
2677 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2678 ++count;
2679 }
2680 }
2681 /*
2682 * Create buffer. In case of duplicate child controller assignments
2683 * the buffer will be too large. But that does not harm.
2684 */
2685 *number_of_children = 0;
2686 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2687 if (!*child_handle_buffer)
2688 return EFI_OUT_OF_RESOURCES;
2689 /* Copy unique child handles */
2690 list_for_each_entry(handler, &efiobj->protocols, link) {
2691 list_for_each_entry(item, &handler->open_infos, link) {
2692 if (item->info.agent_handle == driver_handle &&
2693 item->info.attributes &
2694 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2695 /* Check this is a new child controller */
2696 duplicate = false;
2697 for (i = 0; i < *number_of_children; ++i) {
2698 if ((*child_handle_buffer)[i] ==
2699 item->info.controller_handle)
2700 duplicate = true;
2701 }
2702 /* Copy handle to buffer */
2703 if (!duplicate) {
2704 i = (*number_of_children)++;
2705 (*child_handle_buffer)[i] =
2706 item->info.controller_handle;
2707 }
2708 }
2709 }
2710 }
2711 return EFI_SUCCESS;
2712}
2713
2714/*
2715 * Disconnect a controller from a driver.
2716 *
2717 * This function implements the DisconnectController service.
2718 * See the Unified Extensible Firmware Interface (UEFI) specification
2719 * for details.
2720 *
2721 * @controller_handle handle of the controller
2722 * @driver_image_handle handle of the driver
2723 * @child_handle handle of the child to destroy
2724 * @return status code
2725 */
2726static efi_status_t EFIAPI efi_disconnect_controller(
2727 efi_handle_t controller_handle,
2728 efi_handle_t driver_image_handle,
2729 efi_handle_t child_handle)
2730{
2731 struct efi_driver_binding_protocol *binding_protocol;
2732 efi_handle_t *child_handle_buffer = NULL;
2733 size_t number_of_children = 0;
2734 efi_status_t r;
2735 size_t stop_count = 0;
2736 struct efi_object *efiobj;
2737
2738 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2739 child_handle);
2740
2741 efiobj = efi_search_obj(controller_handle);
2742 if (!efiobj) {
2743 r = EFI_INVALID_PARAMETER;
2744 goto out;
2745 }
2746
2747 if (child_handle && !efi_search_obj(child_handle)) {
2748 r = EFI_INVALID_PARAMETER;
2749 goto out;
2750 }
2751
2752 /* If no driver handle is supplied, disconnect all drivers */
2753 if (!driver_image_handle) {
2754 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2755 goto out;
2756 }
2757
2758 /* Create list of child handles */
2759 if (child_handle) {
2760 number_of_children = 1;
2761 child_handle_buffer = &child_handle;
2762 } else {
2763 efi_get_child_controllers(efiobj,
2764 driver_image_handle,
2765 &number_of_children,
2766 &child_handle_buffer);
2767 }
2768
2769 /* Get the driver binding protocol */
2770 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2771 &efi_guid_driver_binding_protocol,
2772 (void **)&binding_protocol,
2773 driver_image_handle, NULL,
2774 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2775 if (r != EFI_SUCCESS)
2776 goto out;
2777 /* Remove the children */
2778 if (number_of_children) {
2779 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2780 controller_handle,
2781 number_of_children,
2782 child_handle_buffer));
2783 if (r == EFI_SUCCESS)
2784 ++stop_count;
2785 }
2786 /* Remove the driver */
2787 if (!child_handle)
2788 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2789 controller_handle,
2790 0, NULL));
2791 if (r == EFI_SUCCESS)
2792 ++stop_count;
2793 EFI_CALL(efi_close_protocol(driver_image_handle,
2794 &efi_guid_driver_binding_protocol,
2795 driver_image_handle, NULL));
2796
2797 if (stop_count)
2798 r = EFI_SUCCESS;
2799 else
2800 r = EFI_NOT_FOUND;
2801out:
2802 if (!child_handle)
2803 free(child_handle_buffer);
2804 return EFI_EXIT(r);
2805}
2806
bee91169
AG
2807static const struct efi_boot_services efi_boot_services = {
2808 .hdr = {
2809 .headersize = sizeof(struct efi_table_hdr),
2810 },
2811 .raise_tpl = efi_raise_tpl,
2812 .restore_tpl = efi_restore_tpl,
2813 .allocate_pages = efi_allocate_pages_ext,
2814 .free_pages = efi_free_pages_ext,
2815 .get_memory_map = efi_get_memory_map_ext,
ead1274b 2816 .allocate_pool = efi_allocate_pool_ext,
42417bc8 2817 .free_pool = efi_free_pool_ext,
49deb455 2818 .create_event = efi_create_event_ext,
bfc72462 2819 .set_timer = efi_set_timer_ext,
bee91169 2820 .wait_for_event = efi_wait_for_event,
c6841592 2821 .signal_event = efi_signal_event_ext,
bee91169
AG
2822 .close_event = efi_close_event,
2823 .check_event = efi_check_event,
1760ef57 2824 .install_protocol_interface = efi_install_protocol_interface,
bee91169 2825 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
cd534083 2826 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
bee91169
AG
2827 .handle_protocol = efi_handle_protocol,
2828 .reserved = NULL,
2829 .register_protocol_notify = efi_register_protocol_notify,
26329584 2830 .locate_handle = efi_locate_handle_ext,
bee91169 2831 .locate_device_path = efi_locate_device_path,
488bf12d 2832 .install_configuration_table = efi_install_configuration_table_ext,
bee91169
AG
2833 .load_image = efi_load_image,
2834 .start_image = efi_start_image,
a86aeaf2 2835 .exit = efi_exit,
bee91169
AG
2836 .unload_image = efi_unload_image,
2837 .exit_boot_services = efi_exit_boot_services,
2838 .get_next_monotonic_count = efi_get_next_monotonic_count,
2839 .stall = efi_stall,
2840 .set_watchdog_timer = efi_set_watchdog_timer,
2841 .connect_controller = efi_connect_controller,
2842 .disconnect_controller = efi_disconnect_controller,
2843 .open_protocol = efi_open_protocol,
2844 .close_protocol = efi_close_protocol,
2845 .open_protocol_information = efi_open_protocol_information,
2846 .protocols_per_handle = efi_protocols_per_handle,
2847 .locate_handle_buffer = efi_locate_handle_buffer,
2848 .locate_protocol = efi_locate_protocol,
2849 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2850 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2851 .calculate_crc32 = efi_calculate_crc32,
2852 .copy_mem = efi_copy_mem,
2853 .set_mem = efi_set_mem,
2854};
2855
2856
05b6f56e 2857static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
bee91169 2858
3c63db9c 2859struct efi_system_table __efi_runtime_data systab = {
bee91169
AG
2860 .hdr = {
2861 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2862 .revision = 0x20005, /* 2.5 */
2863 .headersize = sizeof(struct efi_table_hdr),
2864 },
2865 .fw_vendor = (long)firmware_vendor,
2866 .con_in = (void*)&efi_con_in,
2867 .con_out = (void*)&efi_con_out,
2868 .std_err = (void*)&efi_con_out,
2869 .runtime = (void*)&efi_runtime_services,
2870 .boottime = (void*)&efi_boot_services,
2871 .nr_tables = 0,
2872 .tables = (void*)efi_conf_table,
2873};