]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_boottime.c
efi_loader: simplify efi_remove_all_protocols
[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
60 /* Called on every callback entry */
61 int __efi_entry_check(void)
62 {
63         int ret = entry_count++ == 0;
64 #ifdef CONFIG_ARM
65         assert(efi_gd);
66         app_gd = gd;
67         gd = efi_gd;
68 #endif
69         return ret;
70 }
71
72 /* Called on every callback exit */
73 int __efi_exit_check(void)
74 {
75         int ret = --entry_count == 0;
76 #ifdef CONFIG_ARM
77         gd = app_gd;
78 #endif
79         return ret;
80 }
81
82 /* Called from do_bootefi_exec() */
83 void efi_save_gd(void)
84 {
85 #ifdef CONFIG_ARM
86         efi_gd = gd;
87 #endif
88 }
89
90 /*
91  * Special case handler for error/abort that just forces things back
92  * to u-boot world so we can dump out an abort msg, without any care
93  * about returning back to UEFI world.
94  */
95 void efi_restore_gd(void)
96 {
97 #ifdef CONFIG_ARM
98         /* Only restore if we're already in EFI context */
99         if (!efi_gd)
100                 return;
101         gd = efi_gd;
102 #endif
103 }
104
105 /*
106  * Two spaces per indent level, maxing out at 10.. which ought to be
107  * enough for anyone ;-)
108  */
109 static const char *indent_string(int level)
110 {
111         const char *indent = "                    ";
112         const int max = strlen(indent);
113         level = min(max, level * 2);
114         return &indent[max - level];
115 }
116
117 const char *__efi_nesting(void)
118 {
119         return indent_string(nesting_level);
120 }
121
122 const char *__efi_nesting_inc(void)
123 {
124         return indent_string(nesting_level++);
125 }
126
127 const char *__efi_nesting_dec(void)
128 {
129         return indent_string(--nesting_level);
130 }
131
132 /*
133  * Queue an EFI event.
134  *
135  * This function queues the notification function of the event for future
136  * execution.
137  *
138  * The notification function is called if the task priority level of the
139  * event is higher than the current task priority level.
140  *
141  * For the SignalEvent service see efi_signal_event_ext.
142  *
143  * @event       event to signal
144  */
145 void efi_signal_event(struct efi_event *event)
146 {
147         if (event->notify_function) {
148                 event->is_queued = true;
149                 /* Check TPL */
150                 if (efi_tpl >= event->notify_tpl)
151                         return;
152                 EFI_CALL_VOID(event->notify_function(event,
153                                                      event->notify_context));
154         }
155         event->is_queued = false;
156 }
157
158 /*
159  * Raise the task priority level.
160  *
161  * This function implements the RaiseTpl service.
162  * See the Unified Extensible Firmware Interface (UEFI) specification
163  * for details.
164  *
165  * @new_tpl     new value of the task priority level
166  * @return      old value of the task priority level
167  */
168 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
169 {
170         efi_uintn_t old_tpl = efi_tpl;
171
172         EFI_ENTRY("0x%zx", new_tpl);
173
174         if (new_tpl < efi_tpl)
175                 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176         efi_tpl = new_tpl;
177         if (efi_tpl > TPL_HIGH_LEVEL)
178                 efi_tpl = TPL_HIGH_LEVEL;
179
180         EFI_EXIT(EFI_SUCCESS);
181         return old_tpl;
182 }
183
184 /*
185  * Lower the task priority level.
186  *
187  * This function implements the RestoreTpl service.
188  * See the Unified Extensible Firmware Interface (UEFI) specification
189  * for details.
190  *
191  * @old_tpl     value of the task priority level to be restored
192  */
193 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
194 {
195         EFI_ENTRY("0x%zx", old_tpl);
196
197         if (old_tpl > efi_tpl)
198                 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199         efi_tpl = old_tpl;
200         if (efi_tpl > TPL_HIGH_LEVEL)
201                 efi_tpl = TPL_HIGH_LEVEL;
202
203         EFI_EXIT(EFI_SUCCESS);
204 }
205
206 /*
207  * Allocate memory pages.
208  *
209  * This function implements the AllocatePages service.
210  * See the Unified Extensible Firmware Interface (UEFI) specification
211  * for details.
212  *
213  * @type                type of allocation to be performed
214  * @memory_type         usage type of the allocated memory
215  * @pages               number of pages to be allocated
216  * @memory              allocated memory
217  * @return              status code
218  */
219 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
220                                                   efi_uintn_t pages,
221                                                   uint64_t *memory)
222 {
223         efi_status_t r;
224
225         EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
226         r = efi_allocate_pages(type, memory_type, pages, memory);
227         return EFI_EXIT(r);
228 }
229
230 /*
231  * Free memory pages.
232  *
233  * This function implements the FreePages service.
234  * See the Unified Extensible Firmware Interface (UEFI) specification
235  * for details.
236  *
237  * @memory      start of the memory area to be freed
238  * @pages       number of pages to be freed
239  * @return      status code
240  */
241 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
242                                               efi_uintn_t pages)
243 {
244         efi_status_t r;
245
246         EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
247         r = efi_free_pages(memory, pages);
248         return EFI_EXIT(r);
249 }
250
251 /*
252  * Get map describing memory usage.
253  *
254  * This function implements the GetMemoryMap service.
255  * See the Unified Extensible Firmware Interface (UEFI) specification
256  * for details.
257  *
258  * @memory_map_size     on entry the size, in bytes, of the memory map buffer,
259  *                      on exit the size of the copied memory map
260  * @memory_map          buffer to which the memory map is written
261  * @map_key             key for the memory map
262  * @descriptor_size     size of an individual memory descriptor
263  * @descriptor_version  version number of the memory descriptor structure
264  * @return              status code
265  */
266 static efi_status_t EFIAPI efi_get_memory_map_ext(
267                                         efi_uintn_t *memory_map_size,
268                                         struct efi_mem_desc *memory_map,
269                                         efi_uintn_t *map_key,
270                                         efi_uintn_t *descriptor_size,
271                                         uint32_t *descriptor_version)
272 {
273         efi_status_t r;
274
275         EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276                   map_key, descriptor_size, descriptor_version);
277         r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278                                descriptor_size, descriptor_version);
279         return EFI_EXIT(r);
280 }
281
282 /*
283  * Allocate memory from pool.
284  *
285  * This function implements the AllocatePool service.
286  * See the Unified Extensible Firmware Interface (UEFI) specification
287  * for details.
288  *
289  * @pool_type   type of the pool from which memory is to be allocated
290  * @size        number of bytes to be allocated
291  * @buffer      allocated memory
292  * @return      status code
293  */
294 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
295                                                  efi_uintn_t size,
296                                                  void **buffer)
297 {
298         efi_status_t r;
299
300         EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
301         r = efi_allocate_pool(pool_type, size, buffer);
302         return EFI_EXIT(r);
303 }
304
305 /*
306  * Free memory from pool.
307  *
308  * This function implements the FreePool service.
309  * See the Unified Extensible Firmware Interface (UEFI) specification
310  * for details.
311  *
312  * @buffer      start of memory to be freed
313  * @return      status code
314  */
315 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
316 {
317         efi_status_t r;
318
319         EFI_ENTRY("%p", buffer);
320         r = efi_free_pool(buffer);
321         return EFI_EXIT(r);
322 }
323
324 /*
325  * Add a new object to the object list.
326  *
327  * The protocols list is initialized.
328  * The object handle is set.
329  *
330  * @obj object to be added
331  */
332 void efi_add_handle(struct efi_object *obj)
333 {
334         if (!obj)
335                 return;
336         INIT_LIST_HEAD(&obj->protocols);
337         obj->handle = obj;
338         list_add_tail(&obj->link, &efi_obj_list);
339 }
340
341 /*
342  * Create handle.
343  *
344  * @handle      new handle
345  * @return      status code
346  */
347 efi_status_t efi_create_handle(void **handle)
348 {
349         struct efi_object *obj;
350         efi_status_t r;
351
352         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
353                               sizeof(struct efi_object),
354                               (void **)&obj);
355         if (r != EFI_SUCCESS)
356                 return r;
357         efi_add_handle(obj);
358         *handle = obj->handle;
359         return r;
360 }
361
362 /*
363  * Find a protocol on a handle.
364  *
365  * @handle              handle
366  * @protocol_guid       GUID of the protocol
367  * @handler             reference to the protocol
368  * @return              status code
369  */
370 efi_status_t efi_search_protocol(const void *handle,
371                                  const efi_guid_t *protocol_guid,
372                                  struct efi_handler **handler)
373 {
374         struct efi_object *efiobj;
375         struct list_head *lhandle;
376
377         if (!handle || !protocol_guid)
378                 return EFI_INVALID_PARAMETER;
379         efiobj = efi_search_obj(handle);
380         if (!efiobj)
381                 return EFI_INVALID_PARAMETER;
382         list_for_each(lhandle, &efiobj->protocols) {
383                 struct efi_handler *protocol;
384
385                 protocol = list_entry(lhandle, struct efi_handler, link);
386                 if (!guidcmp(protocol->guid, protocol_guid)) {
387                         if (handler)
388                                 *handler = protocol;
389                         return EFI_SUCCESS;
390                 }
391         }
392         return EFI_NOT_FOUND;
393 }
394
395 /*
396  * Delete protocol from a handle.
397  *
398  * @handle                      handle from which the protocol shall be deleted
399  * @protocol                    GUID of the protocol to be deleted
400  * @protocol_interface          interface of the protocol implementation
401  * @return                      status code
402  */
403 efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
404                                  void *protocol_interface)
405 {
406         struct efi_handler *handler;
407         efi_status_t ret;
408
409         ret = efi_search_protocol(handle, protocol, &handler);
410         if (ret != EFI_SUCCESS)
411                 return ret;
412         if (guidcmp(handler->guid, protocol))
413                 return EFI_INVALID_PARAMETER;
414         list_del(&handler->link);
415         free(handler);
416         return EFI_SUCCESS;
417 }
418
419 /*
420  * Delete all protocols from a handle.
421  *
422  * @handle      handle from which the protocols shall be deleted
423  * @return      status code
424  */
425 efi_status_t efi_remove_all_protocols(const void *handle)
426 {
427         struct efi_object *efiobj;
428         struct efi_handler *protocol;
429         struct efi_handler *pos;
430
431         efiobj = efi_search_obj(handle);
432         if (!efiobj)
433                 return EFI_INVALID_PARAMETER;
434         list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
435                 efi_status_t ret;
436
437                 ret = efi_remove_protocol(handle, protocol->guid,
438                                           protocol->protocol_interface);
439                 if (ret != EFI_SUCCESS)
440                         return ret;
441         }
442         return EFI_SUCCESS;
443 }
444
445 /*
446  * Delete handle.
447  *
448  * @handle      handle to delete
449  */
450 void efi_delete_handle(struct efi_object *obj)
451 {
452         if (!obj)
453                 return;
454         efi_remove_all_protocols(obj->handle);
455         list_del(&obj->link);
456         free(obj);
457 }
458
459 /*
460  * Our event capabilities are very limited. Only a small limited
461  * number of events is allowed to coexist.
462  */
463 static struct efi_event efi_events[16];
464
465 /*
466  * Create an event.
467  *
468  * This function is used inside U-Boot code to create an event.
469  *
470  * For the API function implementing the CreateEvent service see
471  * efi_create_event_ext.
472  *
473  * @type                type of the event to create
474  * @notify_tpl          task priority level of the event
475  * @notify_function     notification function of the event
476  * @notify_context      pointer passed to the notification function
477  * @event               created event
478  * @return              status code
479  */
480 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
481                               void (EFIAPI *notify_function) (
482                                         struct efi_event *event,
483                                         void *context),
484                               void *notify_context, struct efi_event **event)
485 {
486         int i;
487
488         if (event == NULL)
489                 return EFI_INVALID_PARAMETER;
490
491         if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
492                 return EFI_INVALID_PARAMETER;
493
494         if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
495             notify_function == NULL)
496                 return EFI_INVALID_PARAMETER;
497
498         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
499                 if (efi_events[i].type)
500                         continue;
501                 efi_events[i].type = type;
502                 efi_events[i].notify_tpl = notify_tpl;
503                 efi_events[i].notify_function = notify_function;
504                 efi_events[i].notify_context = notify_context;
505                 /* Disable timers on bootup */
506                 efi_events[i].trigger_next = -1ULL;
507                 efi_events[i].is_queued = false;
508                 efi_events[i].is_signaled = false;
509                 *event = &efi_events[i];
510                 return EFI_SUCCESS;
511         }
512         return EFI_OUT_OF_RESOURCES;
513 }
514
515 /*
516  * Create an event.
517  *
518  * This function implements the CreateEvent service.
519  * See the Unified Extensible Firmware Interface (UEFI) specification
520  * for details.
521  *
522  * @type                type of the event to create
523  * @notify_tpl          task priority level of the event
524  * @notify_function     notification function of the event
525  * @notify_context      pointer passed to the notification function
526  * @event               created event
527  * @return              status code
528  */
529 static efi_status_t EFIAPI efi_create_event_ext(
530                         uint32_t type, efi_uintn_t notify_tpl,
531                         void (EFIAPI *notify_function) (
532                                         struct efi_event *event,
533                                         void *context),
534                         void *notify_context, struct efi_event **event)
535 {
536         EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
537                   notify_context);
538         return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
539                                          notify_context, event));
540 }
541
542
543 /*
544  * Check if a timer event has occurred or a queued notification function should
545  * be called.
546  *
547  * Our timers have to work without interrupts, so we check whenever keyboard
548  * input or disk accesses happen if enough time elapsed for them to fire.
549  */
550 void efi_timer_check(void)
551 {
552         int i;
553         u64 now = timer_get_us();
554
555         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
556                 if (!efi_events[i].type)
557                         continue;
558                 if (efi_events[i].is_queued)
559                         efi_signal_event(&efi_events[i]);
560                 if (!(efi_events[i].type & EVT_TIMER) ||
561                     now < efi_events[i].trigger_next)
562                         continue;
563                 switch (efi_events[i].trigger_type) {
564                 case EFI_TIMER_RELATIVE:
565                         efi_events[i].trigger_type = EFI_TIMER_STOP;
566                         break;
567                 case EFI_TIMER_PERIODIC:
568                         efi_events[i].trigger_next +=
569                                 efi_events[i].trigger_time;
570                         break;
571                 default:
572                         continue;
573                 }
574                 efi_events[i].is_signaled = true;
575                 efi_signal_event(&efi_events[i]);
576         }
577         WATCHDOG_RESET();
578 }
579
580 /*
581  * Set the trigger time for a timer event or stop the event.
582  *
583  * This is the function for internal usage in U-Boot. For the API function
584  * implementing the SetTimer service see efi_set_timer_ext.
585  *
586  * @event               event for which the timer is set
587  * @type                type of the timer
588  * @trigger_time        trigger period in multiples of 100ns
589  * @return              status code
590  */
591 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
592                            uint64_t trigger_time)
593 {
594         int i;
595
596         /*
597          * The parameter defines a multiple of 100ns.
598          * We use multiples of 1000ns. So divide by 10.
599          */
600         do_div(trigger_time, 10);
601
602         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
603                 if (event != &efi_events[i])
604                         continue;
605
606                 if (!(event->type & EVT_TIMER))
607                         break;
608                 switch (type) {
609                 case EFI_TIMER_STOP:
610                         event->trigger_next = -1ULL;
611                         break;
612                 case EFI_TIMER_PERIODIC:
613                 case EFI_TIMER_RELATIVE:
614                         event->trigger_next =
615                                 timer_get_us() + trigger_time;
616                         break;
617                 default:
618                         return EFI_INVALID_PARAMETER;
619                 }
620                 event->trigger_type = type;
621                 event->trigger_time = trigger_time;
622                 event->is_signaled = false;
623                 return EFI_SUCCESS;
624         }
625         return EFI_INVALID_PARAMETER;
626 }
627
628 /*
629  * Set the trigger time for a timer event or stop the event.
630  *
631  * This function implements the SetTimer service.
632  * See the Unified Extensible Firmware Interface (UEFI) specification
633  * for details.
634  *
635  * @event               event for which the timer is set
636  * @type                type of the timer
637  * @trigger_time        trigger period in multiples of 100ns
638  * @return              status code
639  */
640 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
641                                              enum efi_timer_delay type,
642                                              uint64_t trigger_time)
643 {
644         EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
645         return EFI_EXIT(efi_set_timer(event, type, trigger_time));
646 }
647
648 /*
649  * Wait for events to be signaled.
650  *
651  * This function implements the WaitForEvent service.
652  * See the Unified Extensible Firmware Interface (UEFI) specification
653  * for details.
654  *
655  * @num_events  number of events to be waited for
656  * @events      events to be waited for
657  * @index       index of the event that was signaled
658  * @return      status code
659  */
660 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
661                                               struct efi_event **event,
662                                               efi_uintn_t *index)
663 {
664         int i, j;
665
666         EFI_ENTRY("%zd, %p, %p", num_events, event, index);
667
668         /* Check parameters */
669         if (!num_events || !event)
670                 return EFI_EXIT(EFI_INVALID_PARAMETER);
671         /* Check TPL */
672         if (efi_tpl != TPL_APPLICATION)
673                 return EFI_EXIT(EFI_UNSUPPORTED);
674         for (i = 0; i < num_events; ++i) {
675                 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
676                         if (event[i] == &efi_events[j])
677                                 goto known_event;
678                 }
679                 return EFI_EXIT(EFI_INVALID_PARAMETER);
680 known_event:
681                 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
682                         return EFI_EXIT(EFI_INVALID_PARAMETER);
683                 if (!event[i]->is_signaled)
684                         efi_signal_event(event[i]);
685         }
686
687         /* Wait for signal */
688         for (;;) {
689                 for (i = 0; i < num_events; ++i) {
690                         if (event[i]->is_signaled)
691                                 goto out;
692                 }
693                 /* Allow events to occur. */
694                 efi_timer_check();
695         }
696
697 out:
698         /*
699          * Reset the signal which is passed to the caller to allow periodic
700          * events to occur.
701          */
702         event[i]->is_signaled = false;
703         if (index)
704                 *index = i;
705
706         return EFI_EXIT(EFI_SUCCESS);
707 }
708
709 /*
710  * Signal an EFI event.
711  *
712  * This function implements the SignalEvent service.
713  * See the Unified Extensible Firmware Interface (UEFI) specification
714  * for details.
715  *
716  * This functions sets the signaled state of the event and queues the
717  * notification function for execution.
718  *
719  * @event       event to signal
720  * @return      status code
721  */
722 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
723 {
724         int i;
725
726         EFI_ENTRY("%p", event);
727         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
728                 if (event != &efi_events[i])
729                         continue;
730                 if (event->is_signaled)
731                         break;
732                 event->is_signaled = true;
733                 if (event->type & EVT_NOTIFY_SIGNAL)
734                         efi_signal_event(event);
735                 break;
736         }
737         return EFI_EXIT(EFI_SUCCESS);
738 }
739
740 /*
741  * Close an EFI event.
742  *
743  * This function implements the CloseEvent service.
744  * See the Unified Extensible Firmware Interface (UEFI) specification
745  * for details.
746  *
747  * @event       event to close
748  * @return      status code
749  */
750 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
751 {
752         int i;
753
754         EFI_ENTRY("%p", event);
755         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
756                 if (event == &efi_events[i]) {
757                         event->type = 0;
758                         event->trigger_next = -1ULL;
759                         event->is_queued = false;
760                         event->is_signaled = false;
761                         return EFI_EXIT(EFI_SUCCESS);
762                 }
763         }
764         return EFI_EXIT(EFI_INVALID_PARAMETER);
765 }
766
767 /*
768  * Check if an event is signaled.
769  *
770  * This function implements the CheckEvent service.
771  * See the Unified Extensible Firmware Interface (UEFI) specification
772  * for details.
773  *
774  * If an event is not signaled yet the notification function is queued.
775  *
776  * @event       event to check
777  * @return      status code
778  */
779 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
780 {
781         int i;
782
783         EFI_ENTRY("%p", event);
784         efi_timer_check();
785         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
786                 if (event != &efi_events[i])
787                         continue;
788                 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
789                         break;
790                 if (!event->is_signaled)
791                         efi_signal_event(event);
792                 if (event->is_signaled)
793                         return EFI_EXIT(EFI_SUCCESS);
794                 return EFI_EXIT(EFI_NOT_READY);
795         }
796         return EFI_EXIT(EFI_INVALID_PARAMETER);
797 }
798
799 /*
800  * Find the internal EFI object for a handle.
801  *
802  * @handle      handle to find
803  * @return      EFI object
804  */
805 struct efi_object *efi_search_obj(const void *handle)
806 {
807         struct efi_object *efiobj;
808
809         list_for_each_entry(efiobj, &efi_obj_list, link) {
810                 if (efiobj->handle == handle)
811                         return efiobj;
812         }
813
814         return NULL;
815 }
816
817 /*
818  * Install new protocol on a handle.
819  *
820  * @handle                      handle on which the protocol shall be installed
821  * @protocol                    GUID of the protocol to be installed
822  * @protocol_interface          interface of the protocol implementation
823  * @return                      status code
824  */
825 efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
826                               void *protocol_interface)
827 {
828         struct efi_object *efiobj;
829         struct efi_handler *handler;
830         efi_status_t ret;
831
832         efiobj = efi_search_obj(handle);
833         if (!efiobj)
834                 return EFI_INVALID_PARAMETER;
835         ret = efi_search_protocol(handle, protocol, NULL);
836         if (ret != EFI_NOT_FOUND)
837                 return EFI_INVALID_PARAMETER;
838         handler = calloc(1, sizeof(struct efi_handler));
839         if (!handler)
840                 return EFI_OUT_OF_RESOURCES;
841         handler->guid = protocol;
842         handler->protocol_interface = protocol_interface;
843         list_add_tail(&handler->link, &efiobj->protocols);
844         return EFI_SUCCESS;
845 }
846
847 /*
848  * Install protocol interface.
849  *
850  * This function implements the InstallProtocolInterface service.
851  * See the Unified Extensible Firmware Interface (UEFI) specification
852  * for details.
853  *
854  * @handle                      handle on which the protocol shall be installed
855  * @protocol                    GUID of the protocol to be installed
856  * @protocol_interface_type     type of the interface to be installed,
857  *                              always EFI_NATIVE_INTERFACE
858  * @protocol_interface          interface of the protocol implementation
859  * @return                      status code
860  */
861 static efi_status_t EFIAPI efi_install_protocol_interface(
862                         void **handle, const efi_guid_t *protocol,
863                         int protocol_interface_type, void *protocol_interface)
864 {
865         efi_status_t r;
866
867         EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
868                   protocol_interface);
869
870         if (!handle || !protocol ||
871             protocol_interface_type != EFI_NATIVE_INTERFACE) {
872                 r = EFI_INVALID_PARAMETER;
873                 goto out;
874         }
875
876         /* Create new handle if requested. */
877         if (!*handle) {
878                 r = efi_create_handle(handle);
879                 if (r != EFI_SUCCESS)
880                         goto out;
881                 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
882                       *handle);
883         } else {
884                 debug("%sEFI: handle %p\n", indent_string(nesting_level),
885                       *handle);
886         }
887         /* Add new protocol */
888         r = efi_add_protocol(*handle, protocol, protocol_interface);
889 out:
890         return EFI_EXIT(r);
891 }
892
893 /*
894  * Reinstall protocol interface.
895  *
896  * This function implements the ReinstallProtocolInterface service.
897  * See the Unified Extensible Firmware Interface (UEFI) specification
898  * for details.
899  *
900  * @handle                      handle on which the protocol shall be
901  *                              reinstalled
902  * @protocol                    GUID of the protocol to be installed
903  * @old_interface               interface to be removed
904  * @new_interface               interface to be installed
905  * @return                      status code
906  */
907 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
908                         const efi_guid_t *protocol, void *old_interface,
909                         void *new_interface)
910 {
911         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
912                   new_interface);
913         return EFI_EXIT(EFI_ACCESS_DENIED);
914 }
915
916 /*
917  * Uninstall protocol interface.
918  *
919  * This function implements the UninstallProtocolInterface service.
920  * See the Unified Extensible Firmware Interface (UEFI) specification
921  * for details.
922  *
923  * @handle                      handle from which the protocol shall be removed
924  * @protocol                    GUID of the protocol to be removed
925  * @protocol_interface          interface to be removed
926  * @return                      status code
927  */
928 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
929                                 void *handle, const efi_guid_t *protocol,
930                                 void *protocol_interface)
931 {
932         struct efi_handler *handler;
933         efi_status_t r;
934
935         EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
936
937         if (!handle || !protocol) {
938                 r = EFI_INVALID_PARAMETER;
939                 goto out;
940         }
941
942         /* Find the protocol on the handle */
943         r = efi_search_protocol(handle, protocol, &handler);
944         if (r != EFI_SUCCESS)
945                 goto out;
946         if (handler->protocol_interface) {
947                 /* TODO disconnect controllers */
948                 r =  EFI_ACCESS_DENIED;
949         } else {
950                 r = efi_remove_protocol(handle, protocol, protocol_interface);
951         }
952 out:
953         return EFI_EXIT(r);
954 }
955
956 /*
957  * Register an event for notification when a protocol is installed.
958  *
959  * This function implements the RegisterProtocolNotify service.
960  * See the Unified Extensible Firmware Interface (UEFI) specification
961  * for details.
962  *
963  * @protocol            GUID of the protocol whose installation shall be
964  *                      notified
965  * @event               event to be signaled upon installation of the protocol
966  * @registration        key for retrieving the registration information
967  * @return              status code
968  */
969 static efi_status_t EFIAPI efi_register_protocol_notify(
970                                                 const efi_guid_t *protocol,
971                                                 struct efi_event *event,
972                                                 void **registration)
973 {
974         EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
975         return EFI_EXIT(EFI_OUT_OF_RESOURCES);
976 }
977
978 /*
979  * Determine if an EFI handle implements a protocol.
980  *
981  * See the documentation of the LocateHandle service in the UEFI specification.
982  *
983  * @search_type         selection criterion
984  * @protocol            GUID of the protocol
985  * @search_key          registration key
986  * @efiobj              handle
987  * @return              0 if the handle implements the protocol
988  */
989 static int efi_search(enum efi_locate_search_type search_type,
990                       const efi_guid_t *protocol, void *search_key,
991                       struct efi_object *efiobj)
992 {
993         efi_status_t ret;
994
995         switch (search_type) {
996         case ALL_HANDLES:
997                 return 0;
998         case BY_REGISTER_NOTIFY:
999                 /* TODO: RegisterProtocolNotify is not implemented yet */
1000                 return -1;
1001         case BY_PROTOCOL:
1002                 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1003                 return (ret != EFI_SUCCESS);
1004         default:
1005                 /* Invalid search type */
1006                 return -1;
1007         }
1008 }
1009
1010 /*
1011  * Locate handles implementing a protocol.
1012  *
1013  * This function is meant for U-Boot internal calls. For the API implementation
1014  * of the LocateHandle service see efi_locate_handle_ext.
1015  *
1016  * @search_type         selection criterion
1017  * @protocol            GUID of the protocol
1018  * @search_key          registration key
1019  * @buffer_size         size of the buffer to receive the handles in bytes
1020  * @buffer              buffer to receive the relevant handles
1021  * @return              status code
1022  */
1023 static efi_status_t efi_locate_handle(
1024                         enum efi_locate_search_type search_type,
1025                         const efi_guid_t *protocol, void *search_key,
1026                         efi_uintn_t *buffer_size, efi_handle_t *buffer)
1027 {
1028         struct efi_object *efiobj;
1029         efi_uintn_t size = 0;
1030
1031         /* Check parameters */
1032         switch (search_type) {
1033         case ALL_HANDLES:
1034                 break;
1035         case BY_REGISTER_NOTIFY:
1036                 if (!search_key)
1037                         return EFI_INVALID_PARAMETER;
1038                 /* RegisterProtocolNotify is not implemented yet */
1039                 return EFI_UNSUPPORTED;
1040         case BY_PROTOCOL:
1041                 if (!protocol)
1042                         return EFI_INVALID_PARAMETER;
1043                 break;
1044         default:
1045                 return EFI_INVALID_PARAMETER;
1046         }
1047
1048         /*
1049          * efi_locate_handle_buffer uses this function for
1050          * the calculation of the necessary buffer size.
1051          * So do not require a buffer for buffersize == 0.
1052          */
1053         if (!buffer_size || (*buffer_size && !buffer))
1054                 return EFI_INVALID_PARAMETER;
1055
1056         /* Count how much space we need */
1057         list_for_each_entry(efiobj, &efi_obj_list, link) {
1058                 if (!efi_search(search_type, protocol, search_key, efiobj))
1059                         size += sizeof(void*);
1060         }
1061
1062         if (*buffer_size < size) {
1063                 *buffer_size = size;
1064                 return EFI_BUFFER_TOO_SMALL;
1065         }
1066
1067         *buffer_size = size;
1068         if (size == 0)
1069                 return EFI_NOT_FOUND;
1070
1071         /* Then fill the array */
1072         list_for_each_entry(efiobj, &efi_obj_list, link) {
1073                 if (!efi_search(search_type, protocol, search_key, efiobj))
1074                         *buffer++ = efiobj->handle;
1075         }
1076
1077         return EFI_SUCCESS;
1078 }
1079
1080 /*
1081  * Locate handles implementing a protocol.
1082  *
1083  * This function implements the LocateHandle service.
1084  * See the Unified Extensible Firmware Interface (UEFI) specification
1085  * for details.
1086  *
1087  * @search_type         selection criterion
1088  * @protocol            GUID of the protocol
1089  * @search_key          registration key
1090  * @buffer_size         size of the buffer to receive the handles in bytes
1091  * @buffer              buffer to receive the relevant handles
1092  * @return              0 if the handle implements the protocol
1093  */
1094 static efi_status_t EFIAPI efi_locate_handle_ext(
1095                         enum efi_locate_search_type search_type,
1096                         const efi_guid_t *protocol, void *search_key,
1097                         efi_uintn_t *buffer_size, efi_handle_t *buffer)
1098 {
1099         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1100                   buffer_size, buffer);
1101
1102         return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1103                         buffer_size, buffer));
1104 }
1105
1106 /* Collapses configuration table entries, removing index i */
1107 static void efi_remove_configuration_table(int i)
1108 {
1109         struct efi_configuration_table *this = &efi_conf_table[i];
1110         struct efi_configuration_table *next = &efi_conf_table[i+1];
1111         struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1112
1113         memmove(this, next, (ulong)end - (ulong)next);
1114         systab.nr_tables--;
1115 }
1116
1117 /*
1118  * Adds, updates, or removes a configuration table.
1119  *
1120  * This function is used for internal calls. For the API implementation of the
1121  * InstallConfigurationTable service see efi_install_configuration_table_ext.
1122  *
1123  * @guid                GUID of the installed table
1124  * @table               table to be installed
1125  * @return              status code
1126  */
1127 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1128 {
1129         int i;
1130
1131         /* Check for guid override */
1132         for (i = 0; i < systab.nr_tables; i++) {
1133                 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1134                         if (table)
1135                                 efi_conf_table[i].table = table;
1136                         else
1137                                 efi_remove_configuration_table(i);
1138                         return EFI_SUCCESS;
1139                 }
1140         }
1141
1142         if (!table)
1143                 return EFI_NOT_FOUND;
1144
1145         /* No override, check for overflow */
1146         if (i >= ARRAY_SIZE(efi_conf_table))
1147                 return EFI_OUT_OF_RESOURCES;
1148
1149         /* Add a new entry */
1150         memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1151         efi_conf_table[i].table = table;
1152         systab.nr_tables = i + 1;
1153
1154         return EFI_SUCCESS;
1155 }
1156
1157 /*
1158  * Adds, updates, or removes a configuration table.
1159  *
1160  * This function implements the InstallConfigurationTable service.
1161  * See the Unified Extensible Firmware Interface (UEFI) specification
1162  * for details.
1163  *
1164  * @guid                GUID of the installed table
1165  * @table               table to be installed
1166  * @return              status code
1167  */
1168 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1169                                                                void *table)
1170 {
1171         EFI_ENTRY("%pUl, %p", guid, table);
1172         return EFI_EXIT(efi_install_configuration_table(guid, table));
1173 }
1174
1175 /*
1176  * Initialize a loaded_image_info + loaded_image_info object with correct
1177  * protocols, boot-device, etc.
1178  *
1179  * @info                loaded image info to be passed to the entry point of the
1180  *                      image
1181  * @obj                 internal object associated with the loaded image
1182  * @device_path         device path of the loaded image
1183  * @file_path           file path of the loaded image
1184  * @return              status code
1185  */
1186 efi_status_t efi_setup_loaded_image(
1187                         struct efi_loaded_image *info, struct efi_object *obj,
1188                         struct efi_device_path *device_path,
1189                         struct efi_device_path *file_path)
1190 {
1191         efi_status_t ret;
1192
1193         /* Add internal object to object list */
1194         efi_add_handle(obj);
1195         /* efi_exit() assumes that the handle points to the info */
1196         obj->handle = info;
1197
1198         info->file_path = file_path;
1199         if (device_path)
1200                 info->device_handle = efi_dp_find_obj(device_path, NULL);
1201
1202         /*
1203          * When asking for the device path interface, return
1204          * bootefi_device_path
1205          */
1206         ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1207         if (ret != EFI_SUCCESS)
1208                 goto failure;
1209
1210         /*
1211          * When asking for the loaded_image interface, just
1212          * return handle which points to loaded_image_info
1213          */
1214         ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1215         if (ret != EFI_SUCCESS)
1216                 goto failure;
1217
1218         ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1219                                (void *)&efi_console_control);
1220         if (ret != EFI_SUCCESS)
1221                 goto failure;
1222
1223         ret = efi_add_protocol(obj->handle,
1224                                &efi_guid_device_path_to_text_protocol,
1225                                (void *)&efi_device_path_to_text);
1226         if (ret != EFI_SUCCESS)
1227                 goto failure;
1228
1229         return ret;
1230 failure:
1231         printf("ERROR: Failure to install protocols for loaded image\n");
1232         return ret;
1233 }
1234
1235 /*
1236  * Load an image using a file path.
1237  *
1238  * @file_path           the path of the image to load
1239  * @buffer              buffer containing the loaded image
1240  * @return              status code
1241  */
1242 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1243                                       void **buffer)
1244 {
1245         struct efi_file_info *info = NULL;
1246         struct efi_file_handle *f;
1247         static efi_status_t ret;
1248         uint64_t bs;
1249
1250         f = efi_file_from_path(file_path);
1251         if (!f)
1252                 return EFI_DEVICE_ERROR;
1253
1254         bs = 0;
1255         EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1256                                   &bs, info));
1257         if (ret == EFI_BUFFER_TOO_SMALL) {
1258                 info = malloc(bs);
1259                 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1260                                           &bs, info));
1261         }
1262         if (ret != EFI_SUCCESS)
1263                 goto error;
1264
1265         ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1266         if (ret)
1267                 goto error;
1268
1269         EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1270
1271 error:
1272         free(info);
1273         EFI_CALL(f->close(f));
1274
1275         if (ret != EFI_SUCCESS) {
1276                 efi_free_pool(*buffer);
1277                 *buffer = NULL;
1278         }
1279
1280         return ret;
1281 }
1282
1283 /*
1284  * Load an EFI image into memory.
1285  *
1286  * This function implements the LoadImage service.
1287  * See the Unified Extensible Firmware Interface (UEFI) specification
1288  * for details.
1289  *
1290  * @boot_policy         true for request originating from the boot manager
1291  * @parent_image        the calles's image handle
1292  * @file_path           the path of the image to load
1293  * @source_buffer       memory location from which the image is installed
1294  * @source_size         size of the memory area from which the image is
1295  *                      installed
1296  * @image_handle        handle for the newly installed image
1297  * @return              status code
1298  */
1299 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1300                                           efi_handle_t parent_image,
1301                                           struct efi_device_path *file_path,
1302                                           void *source_buffer,
1303                                           unsigned long source_size,
1304                                           efi_handle_t *image_handle)
1305 {
1306         struct efi_loaded_image *info;
1307         struct efi_object *obj;
1308         efi_status_t ret;
1309
1310         EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1311                   file_path, source_buffer, source_size, image_handle);
1312
1313         info = calloc(1, sizeof(*info));
1314         obj = calloc(1, sizeof(*obj));
1315
1316         if (!source_buffer) {
1317                 struct efi_device_path *dp, *fp;
1318
1319                 ret = efi_load_image_from_path(file_path, &source_buffer);
1320                 if (ret != EFI_SUCCESS)
1321                         goto failure;
1322                 /*
1323                  * split file_path which contains both the device and
1324                  * file parts:
1325                  */
1326                 efi_dp_split_file_path(file_path, &dp, &fp);
1327                 ret = efi_setup_loaded_image(info, obj, dp, fp);
1328                 if (ret != EFI_SUCCESS)
1329                         goto failure;
1330         } else {
1331                 /* In this case, file_path is the "device" path, ie.
1332                  * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1333                  */
1334                 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1335                 if (ret != EFI_SUCCESS)
1336                         goto failure;
1337         }
1338         info->reserved = efi_load_pe(source_buffer, info);
1339         if (!info->reserved) {
1340                 ret = EFI_UNSUPPORTED;
1341                 goto failure;
1342         }
1343         info->system_table = &systab;
1344         info->parent_handle = parent_image;
1345         *image_handle = obj->handle;
1346         return EFI_EXIT(EFI_SUCCESS);
1347 failure:
1348         free(info);
1349         efi_delete_handle(obj);
1350         return EFI_EXIT(ret);
1351 }
1352
1353 /*
1354  * Call the entry point of an image.
1355  *
1356  * This function implements the StartImage service.
1357  * See the Unified Extensible Firmware Interface (UEFI) specification
1358  * for details.
1359  *
1360  * @image_handle        handle of the image
1361  * @exit_data_size      size of the buffer
1362  * @exit_data           buffer to receive the exit data of the called image
1363  * @return              status code
1364  */
1365 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1366                                            unsigned long *exit_data_size,
1367                                            s16 **exit_data)
1368 {
1369         ulong (*entry)(void *image_handle, struct efi_system_table *st);
1370         struct efi_loaded_image *info = image_handle;
1371         efi_status_t ret;
1372
1373         EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1374         entry = info->reserved;
1375
1376         efi_is_direct_boot = false;
1377
1378         /* call the image! */
1379         if (setjmp(&info->exit_jmp)) {
1380                 /*
1381                  * We called the entry point of the child image with EFI_CALL
1382                  * in the lines below. The child image called the Exit() boot
1383                  * service efi_exit() which executed the long jump that brought
1384                  * us to the current line. This implies that the second half
1385                  * of the EFI_CALL macro has not been executed.
1386                  */
1387 #ifdef CONFIG_ARM
1388                 /*
1389                  * efi_exit() called efi_restore_gd(). We have to undo this
1390                  * otherwise __efi_entry_check() will put the wrong value into
1391                  * app_gd.
1392                  */
1393                 gd = app_gd;
1394 #endif
1395                 /*
1396                  * To get ready to call EFI_EXIT below we have to execute the
1397                  * missed out steps of EFI_CALL.
1398                  */
1399                 assert(__efi_entry_check());
1400                 debug("%sEFI: %lu returned by started image\n",
1401                       __efi_nesting_dec(),
1402                       (unsigned long)((uintptr_t)info->exit_status &
1403                                       ~EFI_ERROR_MASK));
1404                 return EFI_EXIT(info->exit_status);
1405         }
1406
1407         ret = EFI_CALL(entry(image_handle, &systab));
1408
1409         /* Should usually never get here */
1410         return EFI_EXIT(ret);
1411 }
1412
1413 /*
1414  * Leave an EFI application or driver.
1415  *
1416  * This function implements the Exit service.
1417  * See the Unified Extensible Firmware Interface (UEFI) specification
1418  * for details.
1419  *
1420  * @image_handle        handle of the application or driver that is exiting
1421  * @exit_status         status code
1422  * @exit_data_size      size of the buffer in bytes
1423  * @exit_data           buffer with data describing an error
1424  * @return              status code
1425  */
1426 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1427                         efi_status_t exit_status, unsigned long exit_data_size,
1428                         int16_t *exit_data)
1429 {
1430         /*
1431          * We require that the handle points to the original loaded
1432          * image protocol interface.
1433          *
1434          * For getting the longjmp address this is safer than locating
1435          * the protocol because the protocol may have been reinstalled
1436          * pointing to another memory location.
1437          *
1438          * TODO: We should call the unload procedure of the loaded
1439          *       image protocol.
1440          */
1441         struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1442
1443         EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1444                   exit_data_size, exit_data);
1445
1446         /* Make sure entry/exit counts for EFI world cross-overs match */
1447         EFI_EXIT(exit_status);
1448
1449         /*
1450          * But longjmp out with the U-Boot gd, not the application's, as
1451          * the other end is a setjmp call inside EFI context.
1452          */
1453         efi_restore_gd();
1454
1455         loaded_image_info->exit_status = exit_status;
1456         longjmp(&loaded_image_info->exit_jmp, 1);
1457
1458         panic("EFI application exited");
1459 }
1460
1461 /*
1462  * Unload an EFI image.
1463  *
1464  * This function implements the UnloadImage service.
1465  * See the Unified Extensible Firmware Interface (UEFI) specification
1466  * for details.
1467  *
1468  * @image_handle        handle of the image to be unloaded
1469  * @return              status code
1470  */
1471 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1472 {
1473         struct efi_object *efiobj;
1474
1475         EFI_ENTRY("%p", image_handle);
1476         efiobj = efi_search_obj(image_handle);
1477         if (efiobj)
1478                 list_del(&efiobj->link);
1479
1480         return EFI_EXIT(EFI_SUCCESS);
1481 }
1482
1483 /*
1484  * Fix up caches for EFI payloads if necessary.
1485  */
1486 static void efi_exit_caches(void)
1487 {
1488 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1489         /*
1490          * Grub on 32bit ARM needs to have caches disabled before jumping into
1491          * a zImage, but does not know of all cache layers. Give it a hand.
1492          */
1493         if (efi_is_direct_boot)
1494                 cleanup_before_linux();
1495 #endif
1496 }
1497
1498 /*
1499  * Stop boot services.
1500  *
1501  * This function implements the ExitBootServices service.
1502  * See the Unified Extensible Firmware Interface (UEFI) specification
1503  * for details.
1504  *
1505  * @image_handle        handle of the loaded image
1506  * @map_key             key of the memory map
1507  * @return              status code
1508  */
1509 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1510                                                   unsigned long map_key)
1511 {
1512         int i;
1513
1514         EFI_ENTRY("%p, %ld", image_handle, map_key);
1515
1516         /* Notify that ExitBootServices is invoked. */
1517         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1518                 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1519                         continue;
1520                 efi_signal_event(&efi_events[i]);
1521         }
1522         /* Make sure that notification functions are not called anymore */
1523         efi_tpl = TPL_HIGH_LEVEL;
1524
1525         /* XXX Should persist EFI variables here */
1526
1527         board_quiesce_devices();
1528
1529         /* Fix up caches for EFI payloads if necessary */
1530         efi_exit_caches();
1531
1532         /* This stops all lingering devices */
1533         bootm_disable_interrupts();
1534
1535         /* Give the payload some time to boot */
1536         efi_set_watchdog(0);
1537         WATCHDOG_RESET();
1538
1539         return EFI_EXIT(EFI_SUCCESS);
1540 }
1541
1542 /*
1543  * Get next value of the counter.
1544  *
1545  * This function implements the NextMonotonicCount service.
1546  * See the Unified Extensible Firmware Interface (UEFI) specification
1547  * for details.
1548  *
1549  * @count       returned value of the counter
1550  * @return      status code
1551  */
1552 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1553 {
1554         static uint64_t mono = 0;
1555         EFI_ENTRY("%p", count);
1556         *count = mono++;
1557         return EFI_EXIT(EFI_SUCCESS);
1558 }
1559
1560 /*
1561  * Sleep.
1562  *
1563  * This function implements the Stall sercive.
1564  * See the Unified Extensible Firmware Interface (UEFI) specification
1565  * for details.
1566  *
1567  * @microseconds        period to sleep in microseconds
1568  * @return              status code
1569  */
1570 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1571 {
1572         EFI_ENTRY("%ld", microseconds);
1573         udelay(microseconds);
1574         return EFI_EXIT(EFI_SUCCESS);
1575 }
1576
1577 /*
1578  * Reset the watchdog timer.
1579  *
1580  * This function implements the SetWatchdogTimer service.
1581  * See the Unified Extensible Firmware Interface (UEFI) specification
1582  * for details.
1583  *
1584  * @timeout             seconds before reset by watchdog
1585  * @watchdog_code       code to be logged when resetting
1586  * @data_size           size of buffer in bytes
1587  * @watchdog_data       buffer with data describing the reset reason
1588  * @return              status code
1589  */
1590 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1591                                                   uint64_t watchdog_code,
1592                                                   unsigned long data_size,
1593                                                   uint16_t *watchdog_data)
1594 {
1595         EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1596                   data_size, watchdog_data);
1597         return EFI_EXIT(efi_set_watchdog(timeout));
1598 }
1599
1600 /*
1601  * Connect a controller to a driver.
1602  *
1603  * This function implements the ConnectController service.
1604  * See the Unified Extensible Firmware Interface (UEFI) specification
1605  * for details.
1606  *
1607  * @controller_handle   handle of the controller
1608  * @driver_image_handle handle of the driver
1609  * @remain_device_path  device path of a child controller
1610  * @recursive           true to connect all child controllers
1611  * @return              status code
1612  */
1613 static efi_status_t EFIAPI efi_connect_controller(
1614                         efi_handle_t controller_handle,
1615                         efi_handle_t *driver_image_handle,
1616                         struct efi_device_path *remain_device_path,
1617                         bool recursive)
1618 {
1619         EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1620                   remain_device_path, recursive);
1621         return EFI_EXIT(EFI_NOT_FOUND);
1622 }
1623
1624 /*
1625  * Disconnect a controller from a driver.
1626  *
1627  * This function implements the DisconnectController service.
1628  * See the Unified Extensible Firmware Interface (UEFI) specification
1629  * for details.
1630  *
1631  * @controller_handle   handle of the controller
1632  * @driver_image_handle handle of the driver
1633  * @child_handle        handle of the child to destroy
1634  * @return              status code
1635  */
1636 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1637                                                      void *driver_image_handle,
1638                                                      void *child_handle)
1639 {
1640         EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1641                   child_handle);
1642         return EFI_EXIT(EFI_INVALID_PARAMETER);
1643 }
1644
1645 /*
1646  * Close a protocol.
1647  *
1648  * This function implements the CloseProtocol service.
1649  * See the Unified Extensible Firmware Interface (UEFI) specification
1650  * for details.
1651  *
1652  * @handle              handle on which the protocol shall be closed
1653  * @protocol            GUID of the protocol to close
1654  * @agent_handle        handle of the driver
1655  * @controller_handle   handle of the controller
1656  * @return              status code
1657  */
1658 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1659                                               const efi_guid_t *protocol,
1660                                               void *agent_handle,
1661                                               void *controller_handle)
1662 {
1663         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1664                   controller_handle);
1665         return EFI_EXIT(EFI_NOT_FOUND);
1666 }
1667
1668 /*
1669  * Provide information about then open status of a protocol on a handle
1670  *
1671  * This function implements the OpenProtocolInformation service.
1672  * See the Unified Extensible Firmware Interface (UEFI) specification
1673  * for details.
1674  *
1675  * @handle              handle for which the information shall be retrieved
1676  * @protocol            GUID of the protocol
1677  * @entry_buffer        buffer to receive the open protocol information
1678  * @entry_count         number of entries available in the buffer
1679  * @return              status code
1680  */
1681 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1682                         const efi_guid_t *protocol,
1683                         struct efi_open_protocol_info_entry **entry_buffer,
1684                         efi_uintn_t *entry_count)
1685 {
1686         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1687                   entry_count);
1688         return EFI_EXIT(EFI_NOT_FOUND);
1689 }
1690
1691 /*
1692  * Get protocols installed on a handle.
1693  *
1694  * This function implements the ProtocolsPerHandleService.
1695  * See the Unified Extensible Firmware Interface (UEFI) specification
1696  * for details.
1697  *
1698  * @handle                      handle for which the information is retrieved
1699  * @protocol_buffer             buffer with protocol GUIDs
1700  * @protocol_buffer_count       number of entries in the buffer
1701  * @return                      status code
1702  */
1703 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1704                         efi_guid_t ***protocol_buffer,
1705                         efi_uintn_t *protocol_buffer_count)
1706 {
1707         unsigned long buffer_size;
1708         struct efi_object *efiobj;
1709         struct list_head *protocol_handle;
1710         efi_status_t r;
1711
1712         EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1713                   protocol_buffer_count);
1714
1715         if (!handle || !protocol_buffer || !protocol_buffer_count)
1716                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1717
1718         *protocol_buffer = NULL;
1719         *protocol_buffer_count = 0;
1720
1721         efiobj = efi_search_obj(handle);
1722         if (!efiobj)
1723                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1724
1725         /* Count protocols */
1726         list_for_each(protocol_handle, &efiobj->protocols) {
1727                 ++*protocol_buffer_count;
1728         }
1729
1730         /* Copy guids */
1731         if (*protocol_buffer_count) {
1732                 size_t j = 0;
1733
1734                 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1735                 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1736                                       (void **)protocol_buffer);
1737                 if (r != EFI_SUCCESS)
1738                         return EFI_EXIT(r);
1739                 list_for_each(protocol_handle, &efiobj->protocols) {
1740                         struct efi_handler *protocol;
1741
1742                         protocol = list_entry(protocol_handle,
1743                                               struct efi_handler, link);
1744                         (*protocol_buffer)[j] = (void *)protocol->guid;
1745                         ++j;
1746                 }
1747         }
1748
1749         return EFI_EXIT(EFI_SUCCESS);
1750 }
1751
1752 /*
1753  * Locate handles implementing a protocol.
1754  *
1755  * This function implements the LocateHandleBuffer service.
1756  * See the Unified Extensible Firmware Interface (UEFI) specification
1757  * for details.
1758  *
1759  * @search_type         selection criterion
1760  * @protocol            GUID of the protocol
1761  * @search_key          registration key
1762  * @no_handles          number of returned handles
1763  * @buffer              buffer with the returned handles
1764  * @return              status code
1765  */
1766 static efi_status_t EFIAPI efi_locate_handle_buffer(
1767                         enum efi_locate_search_type search_type,
1768                         const efi_guid_t *protocol, void *search_key,
1769                         efi_uintn_t *no_handles, efi_handle_t **buffer)
1770 {
1771         efi_status_t r;
1772         efi_uintn_t buffer_size = 0;
1773
1774         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1775                   no_handles, buffer);
1776
1777         if (!no_handles || !buffer) {
1778                 r = EFI_INVALID_PARAMETER;
1779                 goto out;
1780         }
1781         *no_handles = 0;
1782         *buffer = NULL;
1783         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1784                               *buffer);
1785         if (r != EFI_BUFFER_TOO_SMALL)
1786                 goto out;
1787         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1788                               (void **)buffer);
1789         if (r != EFI_SUCCESS)
1790                 goto out;
1791         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1792                               *buffer);
1793         if (r == EFI_SUCCESS)
1794                 *no_handles = buffer_size / sizeof(void *);
1795 out:
1796         return EFI_EXIT(r);
1797 }
1798
1799 /*
1800  * Find an interface implementing a protocol.
1801  *
1802  * This function implements the LocateProtocol service.
1803  * See the Unified Extensible Firmware Interface (UEFI) specification
1804  * for details.
1805  *
1806  * @protocol            GUID of the protocol
1807  * @registration        registration key passed to the notification function
1808  * @protocol_interface  interface implementing the protocol
1809  * @return              status code
1810  */
1811 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1812                                                void *registration,
1813                                                void **protocol_interface)
1814 {
1815         struct list_head *lhandle;
1816         efi_status_t ret;
1817
1818         EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1819
1820         if (!protocol || !protocol_interface)
1821                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1822
1823         list_for_each(lhandle, &efi_obj_list) {
1824                 struct efi_object *efiobj;
1825                 struct efi_handler *handler;
1826
1827                 efiobj = list_entry(lhandle, struct efi_object, link);
1828
1829                 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1830                 if (ret == EFI_SUCCESS) {
1831                         *protocol_interface = handler->protocol_interface;
1832                         return EFI_EXIT(EFI_SUCCESS);
1833                 }
1834         }
1835         *protocol_interface = NULL;
1836
1837         return EFI_EXIT(EFI_NOT_FOUND);
1838 }
1839
1840 /*
1841  * Get the device path and handle of an device implementing a protocol.
1842  *
1843  * This function implements the LocateDevicePath service.
1844  * See the Unified Extensible Firmware Interface (UEFI) specification
1845  * for details.
1846  *
1847  * @protocol            GUID of the protocol
1848  * @device_path         device path
1849  * @device              handle of the device
1850  * @return              status code
1851  */
1852 static efi_status_t EFIAPI efi_locate_device_path(
1853                         const efi_guid_t *protocol,
1854                         struct efi_device_path **device_path,
1855                         efi_handle_t *device)
1856 {
1857         struct efi_device_path *dp;
1858         size_t i;
1859         struct efi_handler *handler;
1860         efi_handle_t *handles;
1861         size_t len, len_dp;
1862         size_t len_best = 0;
1863         efi_uintn_t no_handles;
1864         u8 *remainder;
1865         efi_status_t ret;
1866
1867         EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1868
1869         if (!protocol || !device_path || !*device_path || !device) {
1870                 ret = EFI_INVALID_PARAMETER;
1871                 goto out;
1872         }
1873
1874         /* Find end of device path */
1875         len = efi_dp_size(*device_path);
1876
1877         /* Get all handles implementing the protocol */
1878         ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1879                                                 &no_handles, &handles));
1880         if (ret != EFI_SUCCESS)
1881                 goto out;
1882
1883         for (i = 0; i < no_handles; ++i) {
1884                 /* Find the device path protocol */
1885                 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1886                                           &handler);
1887                 if (ret != EFI_SUCCESS)
1888                         continue;
1889                 dp = (struct efi_device_path *)handler->protocol_interface;
1890                 len_dp = efi_dp_size(dp);
1891                 /*
1892                  * This handle can only be a better fit
1893                  * if its device path length is longer than the best fit and
1894                  * if its device path length is shorter of equal the searched
1895                  * device path.
1896                  */
1897                 if (len_dp <= len_best || len_dp > len)
1898                         continue;
1899                 /* Check if dp is a subpath of device_path */
1900                 if (memcmp(*device_path, dp, len_dp))
1901                         continue;
1902                 *device = handles[i];
1903                 len_best = len_dp;
1904         }
1905         if (len_best) {
1906                 remainder = (u8 *)*device_path + len_best;
1907                 *device_path = (struct efi_device_path *)remainder;
1908                 ret = EFI_SUCCESS;
1909         } else {
1910                 ret = EFI_NOT_FOUND;
1911         }
1912 out:
1913         return EFI_EXIT(ret);
1914 }
1915
1916 /*
1917  * Install multiple protocol interfaces.
1918  *
1919  * This function implements the MultipleProtocolInterfaces service.
1920  * See the Unified Extensible Firmware Interface (UEFI) specification
1921  * for details.
1922  *
1923  * @handle      handle on which the protocol interfaces shall be installed
1924  * @...         NULL terminated argument list with pairs of protocol GUIDS and
1925  *              interfaces
1926  * @return      status code
1927  */
1928 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1929                         void **handle, ...)
1930 {
1931         EFI_ENTRY("%p", handle);
1932
1933         va_list argptr;
1934         const efi_guid_t *protocol;
1935         void *protocol_interface;
1936         efi_status_t r = EFI_SUCCESS;
1937         int i = 0;
1938
1939         if (!handle)
1940                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1941
1942         va_start(argptr, handle);
1943         for (;;) {
1944                 protocol = va_arg(argptr, efi_guid_t*);
1945                 if (!protocol)
1946                         break;
1947                 protocol_interface = va_arg(argptr, void*);
1948                 r = EFI_CALL(efi_install_protocol_interface(
1949                                                 handle, protocol,
1950                                                 EFI_NATIVE_INTERFACE,
1951                                                 protocol_interface));
1952                 if (r != EFI_SUCCESS)
1953                         break;
1954                 i++;
1955         }
1956         va_end(argptr);
1957         if (r == EFI_SUCCESS)
1958                 return EFI_EXIT(r);
1959
1960         /* If an error occurred undo all changes. */
1961         va_start(argptr, handle);
1962         for (; i; --i) {
1963                 protocol = va_arg(argptr, efi_guid_t*);
1964                 protocol_interface = va_arg(argptr, void*);
1965                 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1966                                                           protocol_interface));
1967         }
1968         va_end(argptr);
1969
1970         return EFI_EXIT(r);
1971 }
1972
1973 /*
1974  * Uninstall multiple protocol interfaces.
1975  *
1976  * This function implements the UninstallMultipleProtocolInterfaces service.
1977  * See the Unified Extensible Firmware Interface (UEFI) specification
1978  * for details.
1979  *
1980  * @handle      handle from which the protocol interfaces shall be removed
1981  * @...         NULL terminated argument list with pairs of protocol GUIDS and
1982  *              interfaces
1983  * @return      status code
1984  */
1985 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1986                         void *handle, ...)
1987 {
1988         EFI_ENTRY("%p", handle);
1989
1990         va_list argptr;
1991         const efi_guid_t *protocol;
1992         void *protocol_interface;
1993         efi_status_t r = EFI_SUCCESS;
1994         size_t i = 0;
1995
1996         if (!handle)
1997                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1998
1999         va_start(argptr, handle);
2000         for (;;) {
2001                 protocol = va_arg(argptr, efi_guid_t*);
2002                 if (!protocol)
2003                         break;
2004                 protocol_interface = va_arg(argptr, void*);
2005                 r = EFI_CALL(efi_uninstall_protocol_interface(
2006                                                 handle, protocol,
2007                                                 protocol_interface));
2008                 if (r != EFI_SUCCESS)
2009                         break;
2010                 i++;
2011         }
2012         va_end(argptr);
2013         if (r == EFI_SUCCESS)
2014                 return EFI_EXIT(r);
2015
2016         /* If an error occurred undo all changes. */
2017         va_start(argptr, handle);
2018         for (; i; --i) {
2019                 protocol = va_arg(argptr, efi_guid_t*);
2020                 protocol_interface = va_arg(argptr, void*);
2021                 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2022                                                         EFI_NATIVE_INTERFACE,
2023                                                         protocol_interface));
2024         }
2025         va_end(argptr);
2026
2027         return EFI_EXIT(r);
2028 }
2029
2030 /*
2031  * Calculate cyclic redundancy code.
2032  *
2033  * This function implements the CalculateCrc32 service.
2034  * See the Unified Extensible Firmware Interface (UEFI) specification
2035  * for details.
2036  *
2037  * @data        buffer with data
2038  * @data_size   size of buffer in bytes
2039  * @crc32_p     cyclic redundancy code
2040  * @return      status code
2041  */
2042 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2043                                                unsigned long data_size,
2044                                                uint32_t *crc32_p)
2045 {
2046         EFI_ENTRY("%p, %ld", data, data_size);
2047         *crc32_p = crc32(0, data, data_size);
2048         return EFI_EXIT(EFI_SUCCESS);
2049 }
2050
2051 /*
2052  * Copy memory.
2053  *
2054  * This function implements the CopyMem service.
2055  * See the Unified Extensible Firmware Interface (UEFI) specification
2056  * for details.
2057  *
2058  * @destination         destination of the copy operation
2059  * @source              source of the copy operation
2060  * @length              number of bytes to copy
2061  */
2062 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2063                                 size_t length)
2064 {
2065         EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2066         memcpy(destination, source, length);
2067         EFI_EXIT(EFI_SUCCESS);
2068 }
2069
2070 /*
2071  * Fill memory with a byte value.
2072  *
2073  * This function implements the SetMem service.
2074  * See the Unified Extensible Firmware Interface (UEFI) specification
2075  * for details.
2076  *
2077  * @buffer              buffer to fill
2078  * @size                size of buffer in bytes
2079  * @value               byte to copy to the buffer
2080  */
2081 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2082 {
2083         EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2084         memset(buffer, value, size);
2085         EFI_EXIT(EFI_SUCCESS);
2086 }
2087
2088 /*
2089  * Open protocol interface on a handle.
2090  *
2091  * This function implements the OpenProtocol interface.
2092  * See the Unified Extensible Firmware Interface (UEFI) specification
2093  * for details.
2094  *
2095  * @handle              handle on which the protocol shall be opened
2096  * @protocol            GUID of the protocol
2097  * @protocol_interface  interface implementing the protocol
2098  * @agent_handle        handle of the driver
2099  * @controller_handle   handle of the controller
2100  * @attributes          attributes indicating how to open the protocol
2101  * @return              status code
2102  */
2103 static efi_status_t EFIAPI efi_open_protocol(
2104                         void *handle, const efi_guid_t *protocol,
2105                         void **protocol_interface, void *agent_handle,
2106                         void *controller_handle, uint32_t attributes)
2107 {
2108         struct efi_handler *handler;
2109         efi_status_t r = EFI_INVALID_PARAMETER;
2110
2111         EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2112                   protocol_interface, agent_handle, controller_handle,
2113                   attributes);
2114
2115         if (!handle || !protocol ||
2116             (!protocol_interface && attributes !=
2117              EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2118                 goto out;
2119         }
2120
2121         switch (attributes) {
2122         case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2123         case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2124         case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2125                 break;
2126         case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2127                 if (controller_handle == handle)
2128                         goto out;
2129         case EFI_OPEN_PROTOCOL_BY_DRIVER:
2130         case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2131                 if (controller_handle == NULL)
2132                         goto out;
2133         case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2134                 if (agent_handle == NULL)
2135                         goto out;
2136                 break;
2137         default:
2138                 goto out;
2139         }
2140
2141         r = efi_search_protocol(handle, protocol, &handler);
2142         if (r != EFI_SUCCESS)
2143                 goto out;
2144
2145         if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2146                 *protocol_interface = handler->protocol_interface;
2147 out:
2148         return EFI_EXIT(r);
2149 }
2150
2151 /*
2152  * Get interface of a protocol on a handle.
2153  *
2154  * This function implements the HandleProtocol service.
2155  * See the Unified Extensible Firmware Interface (UEFI) specification
2156  * for details.
2157  *
2158  * @handle              handle on which the protocol shall be opened
2159  * @protocol            GUID of the protocol
2160  * @protocol_interface  interface implementing the protocol
2161  * @return              status code
2162  */
2163 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2164                                                const efi_guid_t *protocol,
2165                                                void **protocol_interface)
2166 {
2167         return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2168                                  NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2169 }
2170
2171 static const struct efi_boot_services efi_boot_services = {
2172         .hdr = {
2173                 .headersize = sizeof(struct efi_table_hdr),
2174         },
2175         .raise_tpl = efi_raise_tpl,
2176         .restore_tpl = efi_restore_tpl,
2177         .allocate_pages = efi_allocate_pages_ext,
2178         .free_pages = efi_free_pages_ext,
2179         .get_memory_map = efi_get_memory_map_ext,
2180         .allocate_pool = efi_allocate_pool_ext,
2181         .free_pool = efi_free_pool_ext,
2182         .create_event = efi_create_event_ext,
2183         .set_timer = efi_set_timer_ext,
2184         .wait_for_event = efi_wait_for_event,
2185         .signal_event = efi_signal_event_ext,
2186         .close_event = efi_close_event,
2187         .check_event = efi_check_event,
2188         .install_protocol_interface = efi_install_protocol_interface,
2189         .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2190         .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2191         .handle_protocol = efi_handle_protocol,
2192         .reserved = NULL,
2193         .register_protocol_notify = efi_register_protocol_notify,
2194         .locate_handle = efi_locate_handle_ext,
2195         .locate_device_path = efi_locate_device_path,
2196         .install_configuration_table = efi_install_configuration_table_ext,
2197         .load_image = efi_load_image,
2198         .start_image = efi_start_image,
2199         .exit = efi_exit,
2200         .unload_image = efi_unload_image,
2201         .exit_boot_services = efi_exit_boot_services,
2202         .get_next_monotonic_count = efi_get_next_monotonic_count,
2203         .stall = efi_stall,
2204         .set_watchdog_timer = efi_set_watchdog_timer,
2205         .connect_controller = efi_connect_controller,
2206         .disconnect_controller = efi_disconnect_controller,
2207         .open_protocol = efi_open_protocol,
2208         .close_protocol = efi_close_protocol,
2209         .open_protocol_information = efi_open_protocol_information,
2210         .protocols_per_handle = efi_protocols_per_handle,
2211         .locate_handle_buffer = efi_locate_handle_buffer,
2212         .locate_protocol = efi_locate_protocol,
2213         .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2214         .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2215         .calculate_crc32 = efi_calculate_crc32,
2216         .copy_mem = efi_copy_mem,
2217         .set_mem = efi_set_mem,
2218 };
2219
2220
2221 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2222
2223 struct efi_system_table __efi_runtime_data systab = {
2224         .hdr = {
2225                 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2226                 .revision = 0x20005, /* 2.5 */
2227                 .headersize = sizeof(struct efi_table_hdr),
2228         },
2229         .fw_vendor = (long)firmware_vendor,
2230         .con_in = (void*)&efi_con_in,
2231         .con_out = (void*)&efi_con_out,
2232         .std_err = (void*)&efi_con_out,
2233         .runtime = (void*)&efi_runtime_services,
2234         .boottime = (void*)&efi_boot_services,
2235         .nr_tables = 0,
2236         .tables = (void*)efi_conf_table,
2237 };