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