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