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