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