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