]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_boottime.c
efi_loader: fix StartImage bootservice
[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         asmlinkage ulong (*entry)(efi_handle_t image_handle,
1537                                   struct efi_system_table *st);
1538         struct efi_loaded_image *info = image_handle;
1539         efi_status_t ret;
1540
1541         EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1542         entry = info->reserved;
1543
1544         efi_is_direct_boot = false;
1545
1546         /* call the image! */
1547         if (setjmp(&info->exit_jmp)) {
1548                 /*
1549                  * We called the entry point of the child image with EFI_CALL
1550                  * in the lines below. The child image called the Exit() boot
1551                  * service efi_exit() which executed the long jump that brought
1552                  * us to the current line. This implies that the second half
1553                  * of the EFI_CALL macro has not been executed.
1554                  */
1555 #ifdef CONFIG_ARM
1556                 /*
1557                  * efi_exit() called efi_restore_gd(). We have to undo this
1558                  * otherwise __efi_entry_check() will put the wrong value into
1559                  * app_gd.
1560                  */
1561                 gd = app_gd;
1562 #endif
1563                 /*
1564                  * To get ready to call EFI_EXIT below we have to execute the
1565                  * missed out steps of EFI_CALL.
1566                  */
1567                 assert(__efi_entry_check());
1568                 debug("%sEFI: %lu returned by started image\n",
1569                       __efi_nesting_dec(),
1570                       (unsigned long)((uintptr_t)info->exit_status &
1571                                       ~EFI_ERROR_MASK));
1572                 return EFI_EXIT(info->exit_status);
1573         }
1574
1575         ret = EFI_CALL(entry(image_handle, &systab));
1576
1577         /* Should usually never get here */
1578         return EFI_EXIT(ret);
1579 }
1580
1581 /*
1582  * Leave an EFI application or driver.
1583  *
1584  * This function implements the Exit service.
1585  * See the Unified Extensible Firmware Interface (UEFI) specification
1586  * for details.
1587  *
1588  * @image_handle        handle of the application or driver that is exiting
1589  * @exit_status         status code
1590  * @exit_data_size      size of the buffer in bytes
1591  * @exit_data           buffer with data describing an error
1592  * @return              status code
1593  */
1594 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1595                         efi_status_t exit_status, unsigned long exit_data_size,
1596                         int16_t *exit_data)
1597 {
1598         /*
1599          * We require that the handle points to the original loaded
1600          * image protocol interface.
1601          *
1602          * For getting the longjmp address this is safer than locating
1603          * the protocol because the protocol may have been reinstalled
1604          * pointing to another memory location.
1605          *
1606          * TODO: We should call the unload procedure of the loaded
1607          *       image protocol.
1608          */
1609         struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1610
1611         EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1612                   exit_data_size, exit_data);
1613
1614         /* Make sure entry/exit counts for EFI world cross-overs match */
1615         EFI_EXIT(exit_status);
1616
1617         /*
1618          * But longjmp out with the U-Boot gd, not the application's, as
1619          * the other end is a setjmp call inside EFI context.
1620          */
1621         efi_restore_gd();
1622
1623         loaded_image_info->exit_status = exit_status;
1624         longjmp(&loaded_image_info->exit_jmp, 1);
1625
1626         panic("EFI application exited");
1627 }
1628
1629 /*
1630  * Unload an EFI image.
1631  *
1632  * This function implements the UnloadImage service.
1633  * See the Unified Extensible Firmware Interface (UEFI) specification
1634  * for details.
1635  *
1636  * @image_handle        handle of the image to be unloaded
1637  * @return              status code
1638  */
1639 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1640 {
1641         struct efi_object *efiobj;
1642
1643         EFI_ENTRY("%p", image_handle);
1644         efiobj = efi_search_obj(image_handle);
1645         if (efiobj)
1646                 list_del(&efiobj->link);
1647
1648         return EFI_EXIT(EFI_SUCCESS);
1649 }
1650
1651 /*
1652  * Fix up caches for EFI payloads if necessary.
1653  */
1654 static void efi_exit_caches(void)
1655 {
1656 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1657         /*
1658          * Grub on 32bit ARM needs to have caches disabled before jumping into
1659          * a zImage, but does not know of all cache layers. Give it a hand.
1660          */
1661         if (efi_is_direct_boot)
1662                 cleanup_before_linux();
1663 #endif
1664 }
1665
1666 /*
1667  * Stop boot services.
1668  *
1669  * This function implements the ExitBootServices service.
1670  * See the Unified Extensible Firmware Interface (UEFI) specification
1671  * for details.
1672  *
1673  * @image_handle        handle of the loaded image
1674  * @map_key             key of the memory map
1675  * @return              status code
1676  */
1677 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1678                                                   unsigned long map_key)
1679 {
1680         int i;
1681
1682         EFI_ENTRY("%p, %ld", image_handle, map_key);
1683
1684         /* Notify that ExitBootServices is invoked. */
1685         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1686                 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1687                         continue;
1688                 efi_signal_event(&efi_events[i]);
1689         }
1690         /* Make sure that notification functions are not called anymore */
1691         efi_tpl = TPL_HIGH_LEVEL;
1692
1693         /* XXX Should persist EFI variables here */
1694
1695         board_quiesce_devices();
1696
1697         /* Fix up caches for EFI payloads if necessary */
1698         efi_exit_caches();
1699
1700         /* This stops all lingering devices */
1701         bootm_disable_interrupts();
1702
1703         /* Give the payload some time to boot */
1704         efi_set_watchdog(0);
1705         WATCHDOG_RESET();
1706
1707         return EFI_EXIT(EFI_SUCCESS);
1708 }
1709
1710 /*
1711  * Get next value of the counter.
1712  *
1713  * This function implements the NextMonotonicCount service.
1714  * See the Unified Extensible Firmware Interface (UEFI) specification
1715  * for details.
1716  *
1717  * @count       returned value of the counter
1718  * @return      status code
1719  */
1720 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1721 {
1722         static uint64_t mono = 0;
1723         EFI_ENTRY("%p", count);
1724         *count = mono++;
1725         return EFI_EXIT(EFI_SUCCESS);
1726 }
1727
1728 /*
1729  * Sleep.
1730  *
1731  * This function implements the Stall sercive.
1732  * See the Unified Extensible Firmware Interface (UEFI) specification
1733  * for details.
1734  *
1735  * @microseconds        period to sleep in microseconds
1736  * @return              status code
1737  */
1738 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1739 {
1740         EFI_ENTRY("%ld", microseconds);
1741         udelay(microseconds);
1742         return EFI_EXIT(EFI_SUCCESS);
1743 }
1744
1745 /*
1746  * Reset the watchdog timer.
1747  *
1748  * This function implements the SetWatchdogTimer service.
1749  * See the Unified Extensible Firmware Interface (UEFI) specification
1750  * for details.
1751  *
1752  * @timeout             seconds before reset by watchdog
1753  * @watchdog_code       code to be logged when resetting
1754  * @data_size           size of buffer in bytes
1755  * @watchdog_data       buffer with data describing the reset reason
1756  * @return              status code
1757  */
1758 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1759                                                   uint64_t watchdog_code,
1760                                                   unsigned long data_size,
1761                                                   uint16_t *watchdog_data)
1762 {
1763         EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1764                   data_size, watchdog_data);
1765         return EFI_EXIT(efi_set_watchdog(timeout));
1766 }
1767
1768 /*
1769  * Close a protocol.
1770  *
1771  * This function implements the CloseProtocol service.
1772  * See the Unified Extensible Firmware Interface (UEFI) specification
1773  * for details.
1774  *
1775  * @handle              handle on which the protocol shall be closed
1776  * @protocol            GUID of the protocol to close
1777  * @agent_handle        handle of the driver
1778  * @controller_handle   handle of the controller
1779  * @return              status code
1780  */
1781 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1782                                               const efi_guid_t *protocol,
1783                                               efi_handle_t agent_handle,
1784                                               efi_handle_t controller_handle)
1785 {
1786         struct efi_handler *handler;
1787         struct efi_open_protocol_info_item *item;
1788         struct efi_open_protocol_info_item *pos;
1789         efi_status_t r;
1790
1791         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1792                   controller_handle);
1793
1794         if (!agent_handle) {
1795                 r = EFI_INVALID_PARAMETER;
1796                 goto out;
1797         }
1798         r = efi_search_protocol(handle, protocol, &handler);
1799         if (r != EFI_SUCCESS)
1800                 goto out;
1801
1802         r = EFI_NOT_FOUND;
1803         list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1804                 if (item->info.agent_handle == agent_handle &&
1805                     item->info.controller_handle == controller_handle) {
1806                         efi_delete_open_info(item);
1807                         r = EFI_SUCCESS;
1808                         break;
1809                 }
1810         }
1811 out:
1812         return EFI_EXIT(r);
1813 }
1814
1815 /*
1816  * Provide information about then open status of a protocol on a handle
1817  *
1818  * This function implements the OpenProtocolInformation service.
1819  * See the Unified Extensible Firmware Interface (UEFI) specification
1820  * for details.
1821  *
1822  * @handle              handle for which the information shall be retrieved
1823  * @protocol            GUID of the protocol
1824  * @entry_buffer        buffer to receive the open protocol information
1825  * @entry_count         number of entries available in the buffer
1826  * @return              status code
1827  */
1828 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1829                         const efi_guid_t *protocol,
1830                         struct efi_open_protocol_info_entry **entry_buffer,
1831                         efi_uintn_t *entry_count)
1832 {
1833         unsigned long buffer_size;
1834         unsigned long count;
1835         struct efi_handler *handler;
1836         struct efi_open_protocol_info_item *item;
1837         efi_status_t r;
1838
1839         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1840                   entry_count);
1841
1842         /* Check parameters */
1843         if (!entry_buffer) {
1844                 r = EFI_INVALID_PARAMETER;
1845                 goto out;
1846         }
1847         r = efi_search_protocol(handle, protocol, &handler);
1848         if (r != EFI_SUCCESS)
1849                 goto out;
1850
1851         /* Count entries */
1852         count = 0;
1853         list_for_each_entry(item, &handler->open_infos, link) {
1854                 if (item->info.open_count)
1855                         ++count;
1856         }
1857         *entry_count = count;
1858         *entry_buffer = NULL;
1859         if (!count) {
1860                 r = EFI_SUCCESS;
1861                 goto out;
1862         }
1863
1864         /* Copy entries */
1865         buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1866         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1867                               (void **)entry_buffer);
1868         if (r != EFI_SUCCESS)
1869                 goto out;
1870         list_for_each_entry_reverse(item, &handler->open_infos, link) {
1871                 if (item->info.open_count)
1872                         (*entry_buffer)[--count] = item->info;
1873         }
1874 out:
1875         return EFI_EXIT(r);
1876 }
1877
1878 /*
1879  * Get protocols installed on a handle.
1880  *
1881  * This function implements the ProtocolsPerHandleService.
1882  * See the Unified Extensible Firmware Interface (UEFI) specification
1883  * for details.
1884  *
1885  * @handle                      handle for which the information is retrieved
1886  * @protocol_buffer             buffer with protocol GUIDs
1887  * @protocol_buffer_count       number of entries in the buffer
1888  * @return                      status code
1889  */
1890 static efi_status_t EFIAPI efi_protocols_per_handle(
1891                         efi_handle_t handle, efi_guid_t ***protocol_buffer,
1892                         efi_uintn_t *protocol_buffer_count)
1893 {
1894         unsigned long buffer_size;
1895         struct efi_object *efiobj;
1896         struct list_head *protocol_handle;
1897         efi_status_t r;
1898
1899         EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1900                   protocol_buffer_count);
1901
1902         if (!handle || !protocol_buffer || !protocol_buffer_count)
1903                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1904
1905         *protocol_buffer = NULL;
1906         *protocol_buffer_count = 0;
1907
1908         efiobj = efi_search_obj(handle);
1909         if (!efiobj)
1910                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1911
1912         /* Count protocols */
1913         list_for_each(protocol_handle, &efiobj->protocols) {
1914                 ++*protocol_buffer_count;
1915         }
1916
1917         /* Copy guids */
1918         if (*protocol_buffer_count) {
1919                 size_t j = 0;
1920
1921                 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1922                 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1923                                       (void **)protocol_buffer);
1924                 if (r != EFI_SUCCESS)
1925                         return EFI_EXIT(r);
1926                 list_for_each(protocol_handle, &efiobj->protocols) {
1927                         struct efi_handler *protocol;
1928
1929                         protocol = list_entry(protocol_handle,
1930                                               struct efi_handler, link);
1931                         (*protocol_buffer)[j] = (void *)protocol->guid;
1932                         ++j;
1933                 }
1934         }
1935
1936         return EFI_EXIT(EFI_SUCCESS);
1937 }
1938
1939 /*
1940  * Locate handles implementing a protocol.
1941  *
1942  * This function implements the LocateHandleBuffer service.
1943  * See the Unified Extensible Firmware Interface (UEFI) specification
1944  * for details.
1945  *
1946  * @search_type         selection criterion
1947  * @protocol            GUID of the protocol
1948  * @search_key          registration key
1949  * @no_handles          number of returned handles
1950  * @buffer              buffer with the returned handles
1951  * @return              status code
1952  */
1953 static efi_status_t EFIAPI efi_locate_handle_buffer(
1954                         enum efi_locate_search_type search_type,
1955                         const efi_guid_t *protocol, void *search_key,
1956                         efi_uintn_t *no_handles, efi_handle_t **buffer)
1957 {
1958         efi_status_t r;
1959         efi_uintn_t buffer_size = 0;
1960
1961         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1962                   no_handles, buffer);
1963
1964         if (!no_handles || !buffer) {
1965                 r = EFI_INVALID_PARAMETER;
1966                 goto out;
1967         }
1968         *no_handles = 0;
1969         *buffer = NULL;
1970         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1971                               *buffer);
1972         if (r != EFI_BUFFER_TOO_SMALL)
1973                 goto out;
1974         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1975                               (void **)buffer);
1976         if (r != EFI_SUCCESS)
1977                 goto out;
1978         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1979                               *buffer);
1980         if (r == EFI_SUCCESS)
1981                 *no_handles = buffer_size / sizeof(efi_handle_t);
1982 out:
1983         return EFI_EXIT(r);
1984 }
1985
1986 /*
1987  * Find an interface implementing a protocol.
1988  *
1989  * This function implements the LocateProtocol service.
1990  * See the Unified Extensible Firmware Interface (UEFI) specification
1991  * for details.
1992  *
1993  * @protocol            GUID of the protocol
1994  * @registration        registration key passed to the notification function
1995  * @protocol_interface  interface implementing the protocol
1996  * @return              status code
1997  */
1998 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1999                                                void *registration,
2000                                                void **protocol_interface)
2001 {
2002         struct list_head *lhandle;
2003         efi_status_t ret;
2004
2005         EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2006
2007         if (!protocol || !protocol_interface)
2008                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2009
2010         list_for_each(lhandle, &efi_obj_list) {
2011                 struct efi_object *efiobj;
2012                 struct efi_handler *handler;
2013
2014                 efiobj = list_entry(lhandle, struct efi_object, link);
2015
2016                 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2017                 if (ret == EFI_SUCCESS) {
2018                         *protocol_interface = handler->protocol_interface;
2019                         return EFI_EXIT(EFI_SUCCESS);
2020                 }
2021         }
2022         *protocol_interface = NULL;
2023
2024         return EFI_EXIT(EFI_NOT_FOUND);
2025 }
2026
2027 /*
2028  * Get the device path and handle of an device implementing a protocol.
2029  *
2030  * This function implements the LocateDevicePath service.
2031  * See the Unified Extensible Firmware Interface (UEFI) specification
2032  * for details.
2033  *
2034  * @protocol            GUID of the protocol
2035  * @device_path         device path
2036  * @device              handle of the device
2037  * @return              status code
2038  */
2039 static efi_status_t EFIAPI efi_locate_device_path(
2040                         const efi_guid_t *protocol,
2041                         struct efi_device_path **device_path,
2042                         efi_handle_t *device)
2043 {
2044         struct efi_device_path *dp;
2045         size_t i;
2046         struct efi_handler *handler;
2047         efi_handle_t *handles;
2048         size_t len, len_dp;
2049         size_t len_best = 0;
2050         efi_uintn_t no_handles;
2051         u8 *remainder;
2052         efi_status_t ret;
2053
2054         EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2055
2056         if (!protocol || !device_path || !*device_path || !device) {
2057                 ret = EFI_INVALID_PARAMETER;
2058                 goto out;
2059         }
2060
2061         /* Find end of device path */
2062         len = efi_dp_size(*device_path);
2063
2064         /* Get all handles implementing the protocol */
2065         ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2066                                                 &no_handles, &handles));
2067         if (ret != EFI_SUCCESS)
2068                 goto out;
2069
2070         for (i = 0; i < no_handles; ++i) {
2071                 /* Find the device path protocol */
2072                 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2073                                           &handler);
2074                 if (ret != EFI_SUCCESS)
2075                         continue;
2076                 dp = (struct efi_device_path *)handler->protocol_interface;
2077                 len_dp = efi_dp_size(dp);
2078                 /*
2079                  * This handle can only be a better fit
2080                  * if its device path length is longer than the best fit and
2081                  * if its device path length is shorter of equal the searched
2082                  * device path.
2083                  */
2084                 if (len_dp <= len_best || len_dp > len)
2085                         continue;
2086                 /* Check if dp is a subpath of device_path */
2087                 if (memcmp(*device_path, dp, len_dp))
2088                         continue;
2089                 *device = handles[i];
2090                 len_best = len_dp;
2091         }
2092         if (len_best) {
2093                 remainder = (u8 *)*device_path + len_best;
2094                 *device_path = (struct efi_device_path *)remainder;
2095                 ret = EFI_SUCCESS;
2096         } else {
2097                 ret = EFI_NOT_FOUND;
2098         }
2099 out:
2100         return EFI_EXIT(ret);
2101 }
2102
2103 /*
2104  * Install multiple protocol interfaces.
2105  *
2106  * This function implements the MultipleProtocolInterfaces service.
2107  * See the Unified Extensible Firmware Interface (UEFI) specification
2108  * for details.
2109  *
2110  * @handle      handle on which the protocol interfaces shall be installed
2111  * @...         NULL terminated argument list with pairs of protocol GUIDS and
2112  *              interfaces
2113  * @return      status code
2114  */
2115 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2116                         void **handle, ...)
2117 {
2118         EFI_ENTRY("%p", handle);
2119
2120         va_list argptr;
2121         const efi_guid_t *protocol;
2122         void *protocol_interface;
2123         efi_status_t r = EFI_SUCCESS;
2124         int i = 0;
2125
2126         if (!handle)
2127                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2128
2129         va_start(argptr, handle);
2130         for (;;) {
2131                 protocol = va_arg(argptr, efi_guid_t*);
2132                 if (!protocol)
2133                         break;
2134                 protocol_interface = va_arg(argptr, void*);
2135                 r = EFI_CALL(efi_install_protocol_interface(
2136                                                 handle, protocol,
2137                                                 EFI_NATIVE_INTERFACE,
2138                                                 protocol_interface));
2139                 if (r != EFI_SUCCESS)
2140                         break;
2141                 i++;
2142         }
2143         va_end(argptr);
2144         if (r == EFI_SUCCESS)
2145                 return EFI_EXIT(r);
2146
2147         /* If an error occurred undo all changes. */
2148         va_start(argptr, handle);
2149         for (; i; --i) {
2150                 protocol = va_arg(argptr, efi_guid_t*);
2151                 protocol_interface = va_arg(argptr, void*);
2152                 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2153                                                           protocol_interface));
2154         }
2155         va_end(argptr);
2156
2157         return EFI_EXIT(r);
2158 }
2159
2160 /*
2161  * Uninstall multiple protocol interfaces.
2162  *
2163  * This function implements the UninstallMultipleProtocolInterfaces service.
2164  * See the Unified Extensible Firmware Interface (UEFI) specification
2165  * for details.
2166  *
2167  * @handle      handle from which the protocol interfaces shall be removed
2168  * @...         NULL terminated argument list with pairs of protocol GUIDS and
2169  *              interfaces
2170  * @return      status code
2171  */
2172 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2173                         void *handle, ...)
2174 {
2175         EFI_ENTRY("%p", handle);
2176
2177         va_list argptr;
2178         const efi_guid_t *protocol;
2179         void *protocol_interface;
2180         efi_status_t r = EFI_SUCCESS;
2181         size_t i = 0;
2182
2183         if (!handle)
2184                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2185
2186         va_start(argptr, handle);
2187         for (;;) {
2188                 protocol = va_arg(argptr, efi_guid_t*);
2189                 if (!protocol)
2190                         break;
2191                 protocol_interface = va_arg(argptr, void*);
2192                 r = EFI_CALL(efi_uninstall_protocol_interface(
2193                                                 handle, protocol,
2194                                                 protocol_interface));
2195                 if (r != EFI_SUCCESS)
2196                         break;
2197                 i++;
2198         }
2199         va_end(argptr);
2200         if (r == EFI_SUCCESS)
2201                 return EFI_EXIT(r);
2202
2203         /* If an error occurred undo all changes. */
2204         va_start(argptr, handle);
2205         for (; i; --i) {
2206                 protocol = va_arg(argptr, efi_guid_t*);
2207                 protocol_interface = va_arg(argptr, void*);
2208                 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2209                                                         EFI_NATIVE_INTERFACE,
2210                                                         protocol_interface));
2211         }
2212         va_end(argptr);
2213
2214         return EFI_EXIT(r);
2215 }
2216
2217 /*
2218  * Calculate cyclic redundancy code.
2219  *
2220  * This function implements the CalculateCrc32 service.
2221  * See the Unified Extensible Firmware Interface (UEFI) specification
2222  * for details.
2223  *
2224  * @data        buffer with data
2225  * @data_size   size of buffer in bytes
2226  * @crc32_p     cyclic redundancy code
2227  * @return      status code
2228  */
2229 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2230                                                unsigned long data_size,
2231                                                uint32_t *crc32_p)
2232 {
2233         EFI_ENTRY("%p, %ld", data, data_size);
2234         *crc32_p = crc32(0, data, data_size);
2235         return EFI_EXIT(EFI_SUCCESS);
2236 }
2237
2238 /*
2239  * Copy memory.
2240  *
2241  * This function implements the CopyMem service.
2242  * See the Unified Extensible Firmware Interface (UEFI) specification
2243  * for details.
2244  *
2245  * @destination         destination of the copy operation
2246  * @source              source of the copy operation
2247  * @length              number of bytes to copy
2248  */
2249 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2250                                 size_t length)
2251 {
2252         EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2253         memcpy(destination, source, length);
2254         EFI_EXIT(EFI_SUCCESS);
2255 }
2256
2257 /*
2258  * Fill memory with a byte value.
2259  *
2260  * This function implements the SetMem service.
2261  * See the Unified Extensible Firmware Interface (UEFI) specification
2262  * for details.
2263  *
2264  * @buffer              buffer to fill
2265  * @size                size of buffer in bytes
2266  * @value               byte to copy to the buffer
2267  */
2268 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2269 {
2270         EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2271         memset(buffer, value, size);
2272         EFI_EXIT(EFI_SUCCESS);
2273 }
2274
2275 /*
2276  * Open protocol interface on a handle.
2277  *
2278  * @handler             handler of a protocol
2279  * @protocol_interface  interface implementing the protocol
2280  * @agent_handle        handle of the driver
2281  * @controller_handle   handle of the controller
2282  * @attributes          attributes indicating how to open the protocol
2283  * @return              status code
2284  */
2285 static efi_status_t efi_protocol_open(
2286                         struct efi_handler *handler,
2287                         void **protocol_interface, void *agent_handle,
2288                         void *controller_handle, uint32_t attributes)
2289 {
2290         struct efi_open_protocol_info_item *item;
2291         struct efi_open_protocol_info_entry *match = NULL;
2292         bool opened_by_driver = false;
2293         bool opened_exclusive = false;
2294
2295         /* If there is no agent, only return the interface */
2296         if (!agent_handle)
2297                 goto out;
2298
2299         /* For TEST_PROTOCOL ignore interface attribute */
2300         if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2301                 *protocol_interface = NULL;
2302
2303         /*
2304          * Check if the protocol is already opened by a driver with the same
2305          * attributes or opened exclusively
2306          */
2307         list_for_each_entry(item, &handler->open_infos, link) {
2308                 if (item->info.agent_handle == agent_handle) {
2309                         if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2310                             (item->info.attributes == attributes))
2311                                 return EFI_ALREADY_STARTED;
2312                 }
2313                 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2314                         opened_exclusive = true;
2315         }
2316
2317         /* Only one controller can open the protocol exclusively */
2318         if (opened_exclusive && attributes &
2319             (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2320                 return EFI_ACCESS_DENIED;
2321
2322         /* Prepare exclusive opening */
2323         if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2324                 /* Try to disconnect controllers */
2325                 list_for_each_entry(item, &handler->open_infos, link) {
2326                         if (item->info.attributes ==
2327                                         EFI_OPEN_PROTOCOL_BY_DRIVER)
2328                                 EFI_CALL(efi_disconnect_controller(
2329                                                 item->info.controller_handle,
2330                                                 item->info.agent_handle,
2331                                                 NULL));
2332                 }
2333                 opened_by_driver = false;
2334                 /* Check if all controllers are disconnected */
2335                 list_for_each_entry(item, &handler->open_infos, link) {
2336                         if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2337                                 opened_by_driver = true;
2338                 }
2339                 /* Only one controller can be conncected */
2340                 if (opened_by_driver)
2341                         return EFI_ACCESS_DENIED;
2342         }
2343
2344         /* Find existing entry */
2345         list_for_each_entry(item, &handler->open_infos, link) {
2346                 if (item->info.agent_handle == agent_handle &&
2347                     item->info.controller_handle == controller_handle)
2348                         match = &item->info;
2349         }
2350         /* None found, create one */
2351         if (!match) {
2352                 match = efi_create_open_info(handler);
2353                 if (!match)
2354                         return EFI_OUT_OF_RESOURCES;
2355         }
2356
2357         match->agent_handle = agent_handle;
2358         match->controller_handle = controller_handle;
2359         match->attributes = attributes;
2360         match->open_count++;
2361
2362 out:
2363         /* For TEST_PROTOCOL ignore interface attribute. */
2364         if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2365                 *protocol_interface = handler->protocol_interface;
2366
2367         return EFI_SUCCESS;
2368 }
2369
2370 /*
2371  * Open protocol interface on a handle.
2372  *
2373  * This function implements the OpenProtocol interface.
2374  * See the Unified Extensible Firmware Interface (UEFI) specification
2375  * for details.
2376  *
2377  * @handle              handle on which the protocol shall be opened
2378  * @protocol            GUID of the protocol
2379  * @protocol_interface  interface implementing the protocol
2380  * @agent_handle        handle of the driver
2381  * @controller_handle   handle of the controller
2382  * @attributes          attributes indicating how to open the protocol
2383  * @return              status code
2384  */
2385 static efi_status_t EFIAPI efi_open_protocol(
2386                         void *handle, const efi_guid_t *protocol,
2387                         void **protocol_interface, void *agent_handle,
2388                         void *controller_handle, uint32_t attributes)
2389 {
2390         struct efi_handler *handler;
2391         efi_status_t r = EFI_INVALID_PARAMETER;
2392
2393         EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2394                   protocol_interface, agent_handle, controller_handle,
2395                   attributes);
2396
2397         if (!handle || !protocol ||
2398             (!protocol_interface && attributes !=
2399              EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2400                 goto out;
2401         }
2402
2403         switch (attributes) {
2404         case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2405         case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2406         case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2407                 break;
2408         case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2409                 if (controller_handle == handle)
2410                         goto out;
2411                 /* fall-through */
2412         case EFI_OPEN_PROTOCOL_BY_DRIVER:
2413         case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2414                 /* Check that the controller handle is valid */
2415                 if (!efi_search_obj(controller_handle))
2416                         goto out;
2417                 /* fall-through */
2418         case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2419                 /* Check that the agent handle is valid */
2420                 if (!efi_search_obj(agent_handle))
2421                         goto out;
2422                 break;
2423         default:
2424                 goto out;
2425         }
2426
2427         r = efi_search_protocol(handle, protocol, &handler);
2428         if (r != EFI_SUCCESS)
2429                 goto out;
2430
2431         r = efi_protocol_open(handler, protocol_interface, agent_handle,
2432                               controller_handle, attributes);
2433 out:
2434         return EFI_EXIT(r);
2435 }
2436
2437 /*
2438  * Get interface of a protocol on a handle.
2439  *
2440  * This function implements the HandleProtocol service.
2441  * See the Unified Extensible Firmware Interface (UEFI) specification
2442  * for details.
2443  *
2444  * @handle              handle on which the protocol shall be opened
2445  * @protocol            GUID of the protocol
2446  * @protocol_interface  interface implementing the protocol
2447  * @return              status code
2448  */
2449 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2450                                                const efi_guid_t *protocol,
2451                                                void **protocol_interface)
2452 {
2453         return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2454                                  NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2455 }
2456
2457 static efi_status_t efi_bind_controller(
2458                         efi_handle_t controller_handle,
2459                         efi_handle_t driver_image_handle,
2460                         struct efi_device_path *remain_device_path)
2461 {
2462         struct efi_driver_binding_protocol *binding_protocol;
2463         efi_status_t r;
2464
2465         r = EFI_CALL(efi_open_protocol(driver_image_handle,
2466                                        &efi_guid_driver_binding_protocol,
2467                                        (void **)&binding_protocol,
2468                                        driver_image_handle, NULL,
2469                                        EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2470         if (r != EFI_SUCCESS)
2471                 return r;
2472         r = EFI_CALL(binding_protocol->supported(binding_protocol,
2473                                                  controller_handle,
2474                                                  remain_device_path));
2475         if (r == EFI_SUCCESS)
2476                 r = EFI_CALL(binding_protocol->start(binding_protocol,
2477                                                      controller_handle,
2478                                                      remain_device_path));
2479         EFI_CALL(efi_close_protocol(driver_image_handle,
2480                                     &efi_guid_driver_binding_protocol,
2481                                     driver_image_handle, NULL));
2482         return r;
2483 }
2484
2485 static efi_status_t efi_connect_single_controller(
2486                         efi_handle_t controller_handle,
2487                         efi_handle_t *driver_image_handle,
2488                         struct efi_device_path *remain_device_path)
2489 {
2490         efi_handle_t *buffer;
2491         size_t count;
2492         size_t i;
2493         efi_status_t r;
2494         size_t connected = 0;
2495
2496         /* Get buffer with all handles with driver binding protocol */
2497         r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2498                                               &efi_guid_driver_binding_protocol,
2499                                               NULL, &count, &buffer));
2500         if (r != EFI_SUCCESS)
2501                 return r;
2502
2503         /*  Context Override */
2504         if (driver_image_handle) {
2505                 for (; *driver_image_handle; ++driver_image_handle) {
2506                         for (i = 0; i < count; ++i) {
2507                                 if (buffer[i] == *driver_image_handle) {
2508                                         buffer[i] = NULL;
2509                                         r = efi_bind_controller(
2510                                                         controller_handle,
2511                                                         *driver_image_handle,
2512                                                         remain_device_path);
2513                                         /*
2514                                          * For drivers that do not support the
2515                                          * controller or are already connected
2516                                          * we receive an error code here.
2517                                          */
2518                                         if (r == EFI_SUCCESS)
2519                                                 ++connected;
2520                                 }
2521                         }
2522                 }
2523         }
2524
2525         /*
2526          * TODO: Some overrides are not yet implemented:
2527          * - Platform Driver Override
2528          * - Driver Family Override Search
2529          * - Bus Specific Driver Override
2530          */
2531
2532         /* Driver Binding Search */
2533         for (i = 0; i < count; ++i) {
2534                 if (buffer[i]) {
2535                         r = efi_bind_controller(controller_handle,
2536                                                 buffer[i],
2537                                                 remain_device_path);
2538                         if (r == EFI_SUCCESS)
2539                                 ++connected;
2540                 }
2541         }
2542
2543         efi_free_pool(buffer);
2544         if (!connected)
2545                 return EFI_NOT_FOUND;
2546         return EFI_SUCCESS;
2547 }
2548
2549 /*
2550  * Connect a controller to a driver.
2551  *
2552  * This function implements the ConnectController service.
2553  * See the Unified Extensible Firmware Interface (UEFI) specification
2554  * for details.
2555  *
2556  * First all driver binding protocol handles are tried for binding drivers.
2557  * Afterwards all handles that have openened a protocol of the controller
2558  * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2559  *
2560  * @controller_handle   handle of the controller
2561  * @driver_image_handle handle of the driver
2562  * @remain_device_path  device path of a child controller
2563  * @recursive           true to connect all child controllers
2564  * @return              status code
2565  */
2566 static efi_status_t EFIAPI efi_connect_controller(
2567                         efi_handle_t controller_handle,
2568                         efi_handle_t *driver_image_handle,
2569                         struct efi_device_path *remain_device_path,
2570                         bool recursive)
2571 {
2572         efi_status_t r;
2573         efi_status_t ret = EFI_NOT_FOUND;
2574         struct efi_object *efiobj;
2575
2576         EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2577                   remain_device_path, recursive);
2578
2579         efiobj = efi_search_obj(controller_handle);
2580         if (!efiobj) {
2581                 ret = EFI_INVALID_PARAMETER;
2582                 goto out;
2583         }
2584
2585         r = efi_connect_single_controller(controller_handle,
2586                                           driver_image_handle,
2587                                           remain_device_path);
2588         if (r == EFI_SUCCESS)
2589                 ret = EFI_SUCCESS;
2590         if (recursive) {
2591                 struct efi_handler *handler;
2592                 struct efi_open_protocol_info_item *item;
2593
2594                 list_for_each_entry(handler, &efiobj->protocols, link) {
2595                         list_for_each_entry(item, &handler->open_infos, link) {
2596                                 if (item->info.attributes &
2597                                     EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2598                                         r = EFI_CALL(efi_connect_controller(
2599                                                 item->info.controller_handle,
2600                                                 driver_image_handle,
2601                                                 remain_device_path,
2602                                                 recursive));
2603                                         if (r == EFI_SUCCESS)
2604                                                 ret = EFI_SUCCESS;
2605                                 }
2606                         }
2607                 }
2608         }
2609         /*  Check for child controller specified by end node */
2610         if (ret != EFI_SUCCESS && remain_device_path &&
2611             remain_device_path->type == DEVICE_PATH_TYPE_END)
2612                 ret = EFI_SUCCESS;
2613 out:
2614         return EFI_EXIT(ret);
2615 }
2616
2617 /*
2618  * Get all child controllers associated to a driver.
2619  * The allocated buffer has to be freed with free().
2620  *
2621  * @efiobj                      handle of the controller
2622  * @driver_handle               handle of the driver
2623  * @number_of_children          number of child controllers
2624  * @child_handle_buffer         handles of the the child controllers
2625  */
2626 static efi_status_t efi_get_child_controllers(
2627                                 struct efi_object *efiobj,
2628                                 efi_handle_t driver_handle,
2629                                 efi_uintn_t *number_of_children,
2630                                 efi_handle_t **child_handle_buffer)
2631 {
2632         struct efi_handler *handler;
2633         struct efi_open_protocol_info_item *item;
2634         efi_uintn_t count = 0, i;
2635         bool duplicate;
2636
2637         /* Count all child controller associations */
2638         list_for_each_entry(handler, &efiobj->protocols, link) {
2639                 list_for_each_entry(item, &handler->open_infos, link) {
2640                         if (item->info.agent_handle == driver_handle &&
2641                             item->info.attributes &
2642                             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2643                                 ++count;
2644                 }
2645         }
2646         /*
2647          * Create buffer. In case of duplicate child controller assignments
2648          * the buffer will be too large. But that does not harm.
2649          */
2650         *number_of_children = 0;
2651         *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2652         if (!*child_handle_buffer)
2653                 return EFI_OUT_OF_RESOURCES;
2654         /* Copy unique child handles */
2655         list_for_each_entry(handler, &efiobj->protocols, link) {
2656                 list_for_each_entry(item, &handler->open_infos, link) {
2657                         if (item->info.agent_handle == driver_handle &&
2658                             item->info.attributes &
2659                             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2660                                 /* Check this is a new child controller */
2661                                 duplicate = false;
2662                                 for (i = 0; i < *number_of_children; ++i) {
2663                                         if ((*child_handle_buffer)[i] ==
2664                                             item->info.controller_handle)
2665                                                 duplicate = true;
2666                                 }
2667                                 /* Copy handle to buffer */
2668                                 if (!duplicate) {
2669                                         i = (*number_of_children)++;
2670                                         (*child_handle_buffer)[i] =
2671                                                 item->info.controller_handle;
2672                                 }
2673                         }
2674                 }
2675         }
2676         return EFI_SUCCESS;
2677 }
2678
2679 /*
2680  * Disconnect a controller from a driver.
2681  *
2682  * This function implements the DisconnectController service.
2683  * See the Unified Extensible Firmware Interface (UEFI) specification
2684  * for details.
2685  *
2686  * @controller_handle   handle of the controller
2687  * @driver_image_handle handle of the driver
2688  * @child_handle        handle of the child to destroy
2689  * @return              status code
2690  */
2691 static efi_status_t EFIAPI efi_disconnect_controller(
2692                                 efi_handle_t controller_handle,
2693                                 efi_handle_t driver_image_handle,
2694                                 efi_handle_t child_handle)
2695 {
2696         struct efi_driver_binding_protocol *binding_protocol;
2697         efi_handle_t *child_handle_buffer = NULL;
2698         size_t number_of_children = 0;
2699         efi_status_t r;
2700         size_t stop_count = 0;
2701         struct efi_object *efiobj;
2702
2703         EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2704                   child_handle);
2705
2706         efiobj = efi_search_obj(controller_handle);
2707         if (!efiobj) {
2708                 r = EFI_INVALID_PARAMETER;
2709                 goto out;
2710         }
2711
2712         if (child_handle && !efi_search_obj(child_handle)) {
2713                 r = EFI_INVALID_PARAMETER;
2714                 goto out;
2715         }
2716
2717         /* If no driver handle is supplied, disconnect all drivers */
2718         if (!driver_image_handle) {
2719                 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2720                 goto out;
2721         }
2722
2723         /* Create list of child handles */
2724         if (child_handle) {
2725                 number_of_children = 1;
2726                 child_handle_buffer = &child_handle;
2727         } else {
2728                 efi_get_child_controllers(efiobj,
2729                                           driver_image_handle,
2730                                           &number_of_children,
2731                                           &child_handle_buffer);
2732         }
2733
2734         /* Get the driver binding protocol */
2735         r = EFI_CALL(efi_open_protocol(driver_image_handle,
2736                                        &efi_guid_driver_binding_protocol,
2737                                        (void **)&binding_protocol,
2738                                        driver_image_handle, NULL,
2739                                        EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2740         if (r != EFI_SUCCESS)
2741                 goto out;
2742         /* Remove the children */
2743         if (number_of_children) {
2744                 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2745                                                     controller_handle,
2746                                                     number_of_children,
2747                                                     child_handle_buffer));
2748                 if (r == EFI_SUCCESS)
2749                         ++stop_count;
2750         }
2751         /* Remove the driver */
2752         if (!child_handle)
2753                 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2754                                                     controller_handle,
2755                                                     0, NULL));
2756         if (r == EFI_SUCCESS)
2757                 ++stop_count;
2758         EFI_CALL(efi_close_protocol(driver_image_handle,
2759                                     &efi_guid_driver_binding_protocol,
2760                                     driver_image_handle, NULL));
2761
2762         if (stop_count)
2763                 r = EFI_SUCCESS;
2764         else
2765                 r = EFI_NOT_FOUND;
2766 out:
2767         if (!child_handle)
2768                 free(child_handle_buffer);
2769         return EFI_EXIT(r);
2770 }
2771
2772 static const struct efi_boot_services efi_boot_services = {
2773         .hdr = {
2774                 .headersize = sizeof(struct efi_table_hdr),
2775         },
2776         .raise_tpl = efi_raise_tpl,
2777         .restore_tpl = efi_restore_tpl,
2778         .allocate_pages = efi_allocate_pages_ext,
2779         .free_pages = efi_free_pages_ext,
2780         .get_memory_map = efi_get_memory_map_ext,
2781         .allocate_pool = efi_allocate_pool_ext,
2782         .free_pool = efi_free_pool_ext,
2783         .create_event = efi_create_event_ext,
2784         .set_timer = efi_set_timer_ext,
2785         .wait_for_event = efi_wait_for_event,
2786         .signal_event = efi_signal_event_ext,
2787         .close_event = efi_close_event,
2788         .check_event = efi_check_event,
2789         .install_protocol_interface = efi_install_protocol_interface,
2790         .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2791         .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2792         .handle_protocol = efi_handle_protocol,
2793         .reserved = NULL,
2794         .register_protocol_notify = efi_register_protocol_notify,
2795         .locate_handle = efi_locate_handle_ext,
2796         .locate_device_path = efi_locate_device_path,
2797         .install_configuration_table = efi_install_configuration_table_ext,
2798         .load_image = efi_load_image,
2799         .start_image = efi_start_image,
2800         .exit = efi_exit,
2801         .unload_image = efi_unload_image,
2802         .exit_boot_services = efi_exit_boot_services,
2803         .get_next_monotonic_count = efi_get_next_monotonic_count,
2804         .stall = efi_stall,
2805         .set_watchdog_timer = efi_set_watchdog_timer,
2806         .connect_controller = efi_connect_controller,
2807         .disconnect_controller = efi_disconnect_controller,
2808         .open_protocol = efi_open_protocol,
2809         .close_protocol = efi_close_protocol,
2810         .open_protocol_information = efi_open_protocol_information,
2811         .protocols_per_handle = efi_protocols_per_handle,
2812         .locate_handle_buffer = efi_locate_handle_buffer,
2813         .locate_protocol = efi_locate_protocol,
2814         .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2815         .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2816         .calculate_crc32 = efi_calculate_crc32,
2817         .copy_mem = efi_copy_mem,
2818         .set_mem = efi_set_mem,
2819 };
2820
2821
2822 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2823
2824 struct efi_system_table __efi_runtime_data systab = {
2825         .hdr = {
2826                 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2827                 .revision = 0x20005, /* 2.5 */
2828                 .headersize = sizeof(struct efi_table_hdr),
2829         },
2830         .fw_vendor = (long)firmware_vendor,
2831         .con_in = (void*)&efi_con_in,
2832         .con_out = (void*)&efi_con_out,
2833         .std_err = (void*)&efi_con_out,
2834         .runtime = (void*)&efi_runtime_services,
2835         .boottime = (void*)&efi_boot_services,
2836         .nr_tables = 0,
2837         .tables = (void*)efi_conf_table,
2838 };