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