]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_boottime.c
efi_loader: Fix efi_exit gd clobbering
[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 <efi_loader.h>
11 #include <malloc.h>
12 #include <asm/global_data.h>
13 #include <libfdt_env.h>
14 #include <u-boot/crc.h>
15 #include <bootm.h>
16 #include <inttypes.h>
17 #include <watchdog.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /* This list contains all the EFI objects our payload has access to */
22 LIST_HEAD(efi_obj_list);
23
24 /*
25  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26  * we need to do trickery with caches. Since we don't want to break the EFI
27  * aware boot path, only apply hacks when loading exiting directly (breaking
28  * direct Linux EFI booting along the way - oh well).
29  */
30 static bool efi_is_direct_boot = true;
31
32 /*
33  * EFI can pass arbitrary additional "tables" containing vendor specific
34  * information to the payload. One such table is the FDT table which contains
35  * a pointer to a flattened device tree blob.
36  *
37  * In most cases we want to pass an FDT to the payload, so reserve one slot of
38  * config table space for it. The pointer gets populated by do_bootefi_exec().
39  */
40 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
41
42 #ifdef CONFIG_ARM
43 /*
44  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45  * fixed when compiling U-Boot. However, the payload does not know about that
46  * restriction so we need to manually swap its and our view of that register on
47  * EFI callback entry/exit.
48  */
49 static volatile void *efi_gd, *app_gd;
50 #endif
51
52 static int entry_count;
53 static int nesting_level;
54
55 /* Called on every callback entry */
56 int __efi_entry_check(void)
57 {
58         int ret = entry_count++ == 0;
59 #ifdef CONFIG_ARM
60         assert(efi_gd);
61         app_gd = gd;
62         gd = efi_gd;
63 #endif
64         return ret;
65 }
66
67 /* Called on every callback exit */
68 int __efi_exit_check(void)
69 {
70         int ret = --entry_count == 0;
71 #ifdef CONFIG_ARM
72         gd = app_gd;
73 #endif
74         return ret;
75 }
76
77 /* Called from do_bootefi_exec() */
78 void efi_save_gd(void)
79 {
80 #ifdef CONFIG_ARM
81         efi_gd = gd;
82 #endif
83 }
84
85 /*
86  * Special case handler for error/abort that just forces things back
87  * to u-boot world so we can dump out an abort msg, without any care
88  * about returning back to UEFI world.
89  */
90 void efi_restore_gd(void)
91 {
92 #ifdef CONFIG_ARM
93         /* Only restore if we're already in EFI context */
94         if (!efi_gd)
95                 return;
96         gd = efi_gd;
97 #endif
98 }
99
100 /*
101  * Two spaces per indent level, maxing out at 10.. which ought to be
102  * enough for anyone ;-)
103  */
104 static const char *indent_string(int level)
105 {
106         const char *indent = "                    ";
107         const int max = strlen(indent);
108         level = min(max, level * 2);
109         return &indent[max - level];
110 }
111
112 const char *__efi_nesting(void)
113 {
114         return indent_string(nesting_level);
115 }
116
117 const char *__efi_nesting_inc(void)
118 {
119         return indent_string(nesting_level++);
120 }
121
122 const char *__efi_nesting_dec(void)
123 {
124         return indent_string(--nesting_level);
125 }
126
127 /* Low 32 bit */
128 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
129 /* High 32 bit */
130 #define EFI_HIGH32(a) (a >> 32)
131
132 /*
133  * 64bit division by 10 implemented as multiplication by 1 / 10
134  *
135  * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
136  */
137 #define EFI_TENTH 0x199999999999999A
138 static u64 efi_div10(u64 a)
139 {
140         u64 prod;
141         u64 rem;
142         u64 ret;
143
144         ret  = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
145         prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
146         rem  = EFI_LOW32(prod);
147         ret += EFI_HIGH32(prod);
148         prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
149         rem += EFI_LOW32(prod);
150         ret += EFI_HIGH32(prod);
151         prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
152         rem += EFI_HIGH32(prod);
153         ret += EFI_HIGH32(rem);
154         /* Round to nearest integer */
155         if (rem >= (1 << 31))
156                 ++ret;
157         return ret;
158 }
159
160 void efi_signal_event(struct efi_event *event)
161 {
162         if (event->signaled)
163                 return;
164         event->signaled = 1;
165         if (event->type & EVT_NOTIFY_SIGNAL) {
166                 EFI_CALL(event->notify_function(event, event->notify_context));
167         }
168 }
169
170 static efi_status_t efi_unsupported(const char *funcname)
171 {
172         debug("EFI: App called into unimplemented function %s\n", funcname);
173         return EFI_EXIT(EFI_UNSUPPORTED);
174 }
175
176 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
177 {
178         EFI_ENTRY("0x%zx", new_tpl);
179         return EFI_EXIT(0);
180 }
181
182 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
183 {
184         EFI_ENTRY("0x%zx", old_tpl);
185         efi_unsupported(__func__);
186 }
187
188 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
189                                                   unsigned long pages,
190                                                   uint64_t *memory)
191 {
192         efi_status_t r;
193
194         EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
195         r = efi_allocate_pages(type, memory_type, pages, memory);
196         return EFI_EXIT(r);
197 }
198
199 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
200                                               unsigned long pages)
201 {
202         efi_status_t r;
203
204         EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
205         r = efi_free_pages(memory, pages);
206         return EFI_EXIT(r);
207 }
208
209 static efi_status_t EFIAPI efi_get_memory_map_ext(
210                                         unsigned long *memory_map_size,
211                                         struct efi_mem_desc *memory_map,
212                                         unsigned long *map_key,
213                                         unsigned long *descriptor_size,
214                                         uint32_t *descriptor_version)
215 {
216         efi_status_t r;
217
218         EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
219                   map_key, descriptor_size, descriptor_version);
220         r = efi_get_memory_map(memory_map_size, memory_map, map_key,
221                                descriptor_size, descriptor_version);
222         return EFI_EXIT(r);
223 }
224
225 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
226                                                  unsigned long size,
227                                                  void **buffer)
228 {
229         efi_status_t r;
230
231         EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
232         r = efi_allocate_pool(pool_type, size, buffer);
233         return EFI_EXIT(r);
234 }
235
236 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
237 {
238         efi_status_t r;
239
240         EFI_ENTRY("%p", buffer);
241         r = efi_free_pool(buffer);
242         return EFI_EXIT(r);
243 }
244
245 /*
246  * Our event capabilities are very limited. Only a small limited
247  * number of events is allowed to coexist.
248  */
249 static struct efi_event efi_events[16];
250
251 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
252                               void (EFIAPI *notify_function) (
253                                         struct efi_event *event,
254                                         void *context),
255                               void *notify_context, struct efi_event **event)
256 {
257         int i;
258
259         if (event == NULL)
260                 return EFI_INVALID_PARAMETER;
261
262         if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
263                 return EFI_INVALID_PARAMETER;
264
265         if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
266             notify_function == NULL)
267                 return EFI_INVALID_PARAMETER;
268
269         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
270                 if (efi_events[i].type)
271                         continue;
272                 efi_events[i].type = type;
273                 efi_events[i].notify_tpl = notify_tpl;
274                 efi_events[i].notify_function = notify_function;
275                 efi_events[i].notify_context = notify_context;
276                 /* Disable timers on bootup */
277                 efi_events[i].trigger_next = -1ULL;
278                 efi_events[i].signaled = 0;
279                 *event = &efi_events[i];
280                 return EFI_SUCCESS;
281         }
282         return EFI_OUT_OF_RESOURCES;
283 }
284
285 static efi_status_t EFIAPI efi_create_event_ext(
286                         uint32_t type, UINTN notify_tpl,
287                         void (EFIAPI *notify_function) (
288                                         struct efi_event *event,
289                                         void *context),
290                         void *notify_context, struct efi_event **event)
291 {
292         EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
293                   notify_context);
294         return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
295                                          notify_context, event));
296 }
297
298
299 /*
300  * Our timers have to work without interrupts, so we check whenever keyboard
301  * input or disk accesses happen if enough time elapsed for it to fire.
302  */
303 void efi_timer_check(void)
304 {
305         int i;
306         u64 now = timer_get_us();
307
308         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
309                 if (!efi_events[i].type ||
310                     !(efi_events[i].type & EVT_TIMER) ||
311                     efi_events[i].trigger_type == EFI_TIMER_STOP ||
312                     now < efi_events[i].trigger_next)
313                         continue;
314                 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
315                         efi_events[i].trigger_next +=
316                                 efi_events[i].trigger_time;
317                         efi_events[i].signaled = 0;
318                 }
319                 efi_signal_event(&efi_events[i]);
320         }
321         WATCHDOG_RESET();
322 }
323
324 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
325                            uint64_t trigger_time)
326 {
327         int i;
328
329         /*
330          * The parameter defines a multiple of 100ns.
331          * We use multiples of 1000ns. So divide by 10.
332          */
333         trigger_time = efi_div10(trigger_time);
334
335         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
336                 if (event != &efi_events[i])
337                         continue;
338
339                 if (!(event->type & EVT_TIMER))
340                         break;
341                 switch (type) {
342                 case EFI_TIMER_STOP:
343                         event->trigger_next = -1ULL;
344                         break;
345                 case EFI_TIMER_PERIODIC:
346                 case EFI_TIMER_RELATIVE:
347                         event->trigger_next =
348                                 timer_get_us() + trigger_time;
349                         break;
350                 default:
351                         return EFI_INVALID_PARAMETER;
352                 }
353                 event->trigger_type = type;
354                 event->trigger_time = trigger_time;
355                 return EFI_SUCCESS;
356         }
357         return EFI_INVALID_PARAMETER;
358 }
359
360 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
361                                              enum efi_timer_delay type,
362                                              uint64_t trigger_time)
363 {
364         EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
365         return EFI_EXIT(efi_set_timer(event, type, trigger_time));
366 }
367
368 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
369                                               struct efi_event **event,
370                                               unsigned long *index)
371 {
372         int i, j;
373
374         EFI_ENTRY("%ld, %p, %p", num_events, event, index);
375
376         /* Check parameters */
377         if (!num_events || !event)
378                 return EFI_EXIT(EFI_INVALID_PARAMETER);
379         for (i = 0; i < num_events; ++i) {
380                 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
381                         if (event[i] == &efi_events[j])
382                                 goto known_event;
383                 }
384                 return EFI_EXIT(EFI_INVALID_PARAMETER);
385 known_event:
386                 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
387                         return EFI_EXIT(EFI_INVALID_PARAMETER);
388         }
389
390         /* Wait for signal */
391         for (;;) {
392                 for (i = 0; i < num_events; ++i) {
393                         if (event[i]->signaled)
394                                 goto out;
395                 }
396                 /* Allow events to occur. */
397                 efi_timer_check();
398         }
399
400 out:
401         /*
402          * Reset the signal which is passed to the caller to allow periodic
403          * events to occur.
404          */
405         event[i]->signaled = 0;
406         if (index)
407                 *index = i;
408
409         return EFI_EXIT(EFI_SUCCESS);
410 }
411
412 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
413 {
414         int i;
415
416         EFI_ENTRY("%p", event);
417         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
418                 if (event != &efi_events[i])
419                         continue;
420                 efi_signal_event(event);
421                 break;
422         }
423         return EFI_EXIT(EFI_SUCCESS);
424 }
425
426 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
427 {
428         int i;
429
430         EFI_ENTRY("%p", event);
431         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
432                 if (event == &efi_events[i]) {
433                         event->type = 0;
434                         event->trigger_next = -1ULL;
435                         event->signaled = 0;
436                         return EFI_EXIT(EFI_SUCCESS);
437                 }
438         }
439         return EFI_EXIT(EFI_INVALID_PARAMETER);
440 }
441
442 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
443 {
444         int i;
445
446         EFI_ENTRY("%p", event);
447         efi_timer_check();
448         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
449                 if (event != &efi_events[i])
450                         continue;
451                 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
452                         break;
453                 if (event->signaled)
454                         return EFI_EXIT(EFI_SUCCESS);
455                 return EFI_EXIT(EFI_NOT_READY);
456         }
457         return EFI_EXIT(EFI_INVALID_PARAMETER);
458 }
459
460 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
461                         efi_guid_t *protocol, int protocol_interface_type,
462                         void *protocol_interface)
463 {
464         struct list_head *lhandle;
465         int i;
466         efi_status_t r;
467
468         if (!handle || !protocol ||
469             protocol_interface_type != EFI_NATIVE_INTERFACE) {
470                 r = EFI_INVALID_PARAMETER;
471                 goto out;
472         }
473
474         /* Create new handle if requested. */
475         if (!*handle) {
476                 r = EFI_OUT_OF_RESOURCES;
477                 goto out;
478         }
479         /* Find object. */
480         list_for_each(lhandle, &efi_obj_list) {
481                 struct efi_object *efiobj;
482                 efiobj = list_entry(lhandle, struct efi_object, link);
483
484                 if (efiobj->handle != *handle)
485                         continue;
486                 /* Check if protocol is already installed on the handle. */
487                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
488                         struct efi_handler *handler = &efiobj->protocols[i];
489
490                         if (!handler->guid)
491                                 continue;
492                         if (!guidcmp(handler->guid, protocol)) {
493                                 r = EFI_INVALID_PARAMETER;
494                                 goto out;
495                         }
496                 }
497                 /* Install protocol in first empty slot. */
498                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
499                         struct efi_handler *handler = &efiobj->protocols[i];
500
501                         if (handler->guid)
502                                 continue;
503
504                         handler->guid = protocol;
505                         handler->protocol_interface = protocol_interface;
506                         r = EFI_SUCCESS;
507                         goto out;
508                 }
509                 r = EFI_OUT_OF_RESOURCES;
510                 goto out;
511         }
512         r = EFI_INVALID_PARAMETER;
513 out:
514         return r;
515 }
516
517 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
518                         efi_guid_t *protocol, int protocol_interface_type,
519                         void *protocol_interface)
520 {
521         EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
522                   protocol_interface);
523
524         return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
525                                                        protocol_interface_type,
526                                                        protocol_interface));
527 }
528
529 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
530                         efi_guid_t *protocol, void *old_interface,
531                         void *new_interface)
532 {
533         EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
534                   new_interface);
535         return EFI_EXIT(EFI_ACCESS_DENIED);
536 }
537
538 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
539                         efi_guid_t *protocol, void *protocol_interface)
540 {
541         struct list_head *lhandle;
542         int i;
543         efi_status_t r = EFI_NOT_FOUND;
544
545         if (!handle || !protocol) {
546                 r = EFI_INVALID_PARAMETER;
547                 goto out;
548         }
549
550         list_for_each(lhandle, &efi_obj_list) {
551                 struct efi_object *efiobj;
552                 efiobj = list_entry(lhandle, struct efi_object, link);
553
554                 if (efiobj->handle != handle)
555                         continue;
556
557                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
558                         struct efi_handler *handler = &efiobj->protocols[i];
559                         const efi_guid_t *hprotocol = handler->guid;
560
561                         if (!hprotocol)
562                                 continue;
563                         if (!guidcmp(hprotocol, protocol)) {
564                                 if (handler->protocol_interface) {
565                                         r = EFI_ACCESS_DENIED;
566                                 } else {
567                                         handler->guid = 0;
568                                         r = EFI_SUCCESS;
569                                 }
570                                 goto out;
571                         }
572                 }
573         }
574
575 out:
576         return r;
577 }
578
579 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
580                         efi_guid_t *protocol, void *protocol_interface)
581 {
582         EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
583
584         return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
585                                                          protocol_interface));
586 }
587
588 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
589                                                         struct efi_event *event,
590                                                         void **registration)
591 {
592         EFI_ENTRY("%p, %p, %p", protocol, event, registration);
593         return EFI_EXIT(EFI_OUT_OF_RESOURCES);
594 }
595
596 static int efi_search(enum efi_locate_search_type search_type,
597                       efi_guid_t *protocol, void *search_key,
598                       struct efi_object *efiobj)
599 {
600         int i;
601
602         switch (search_type) {
603         case all_handles:
604                 return 0;
605         case by_register_notify:
606                 return -1;
607         case by_protocol:
608                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
609                         const efi_guid_t *guid = efiobj->protocols[i].guid;
610                         if (guid && !guidcmp(guid, protocol))
611                                 return 0;
612                 }
613                 return -1;
614         }
615
616         return -1;
617 }
618
619 static efi_status_t efi_locate_handle(
620                         enum efi_locate_search_type search_type,
621                         efi_guid_t *protocol, void *search_key,
622                         unsigned long *buffer_size, efi_handle_t *buffer)
623 {
624         struct list_head *lhandle;
625         unsigned long size = 0;
626
627         /* Count how much space we need */
628         list_for_each(lhandle, &efi_obj_list) {
629                 struct efi_object *efiobj;
630                 efiobj = list_entry(lhandle, struct efi_object, link);
631                 if (!efi_search(search_type, protocol, search_key, efiobj)) {
632                         size += sizeof(void*);
633                 }
634         }
635
636         if (*buffer_size < size) {
637                 *buffer_size = size;
638                 return EFI_BUFFER_TOO_SMALL;
639         }
640
641         *buffer_size = size;
642         if (size == 0)
643                 return EFI_NOT_FOUND;
644
645         /* Then fill the array */
646         list_for_each(lhandle, &efi_obj_list) {
647                 struct efi_object *efiobj;
648                 efiobj = list_entry(lhandle, struct efi_object, link);
649                 if (!efi_search(search_type, protocol, search_key, efiobj)) {
650                         *(buffer++) = efiobj->handle;
651                 }
652         }
653
654         return EFI_SUCCESS;
655 }
656
657 static efi_status_t EFIAPI efi_locate_handle_ext(
658                         enum efi_locate_search_type search_type,
659                         efi_guid_t *protocol, void *search_key,
660                         unsigned long *buffer_size, efi_handle_t *buffer)
661 {
662         EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
663                   buffer_size, buffer);
664
665         return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
666                         buffer_size, buffer));
667 }
668
669 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
670                         struct efi_device_path **device_path,
671                         efi_handle_t *device)
672 {
673         EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
674         return EFI_EXIT(EFI_NOT_FOUND);
675 }
676
677 /* Collapses configuration table entries, removing index i */
678 static void efi_remove_configuration_table(int i)
679 {
680         struct efi_configuration_table *this = &efi_conf_table[i];
681         struct efi_configuration_table *next = &efi_conf_table[i+1];
682         struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
683
684         memmove(this, next, (ulong)end - (ulong)next);
685         systab.nr_tables--;
686 }
687
688 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
689 {
690         int i;
691
692         /* Check for guid override */
693         for (i = 0; i < systab.nr_tables; i++) {
694                 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
695                         if (table)
696                                 efi_conf_table[i].table = table;
697                         else
698                                 efi_remove_configuration_table(i);
699                         return EFI_SUCCESS;
700                 }
701         }
702
703         if (!table)
704                 return EFI_NOT_FOUND;
705
706         /* No override, check for overflow */
707         if (i >= ARRAY_SIZE(efi_conf_table))
708                 return EFI_OUT_OF_RESOURCES;
709
710         /* Add a new entry */
711         memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
712         efi_conf_table[i].table = table;
713         systab.nr_tables = i + 1;
714
715         return EFI_SUCCESS;
716 }
717
718 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
719                                                                void *table)
720 {
721         EFI_ENTRY("%p, %p", guid, table);
722         return EFI_EXIT(efi_install_configuration_table(guid, table));
723 }
724
725 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
726                                           efi_handle_t parent_image,
727                                           struct efi_device_path *file_path,
728                                           void *source_buffer,
729                                           unsigned long source_size,
730                                           efi_handle_t *image_handle)
731 {
732         static struct efi_object loaded_image_info_obj = {
733                 .protocols = {
734                         {
735                                 .guid = &efi_guid_loaded_image,
736                         },
737                 },
738         };
739         struct efi_loaded_image *info;
740         struct efi_object *obj;
741
742         EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
743                   file_path, source_buffer, source_size, image_handle);
744         info = malloc(sizeof(*info));
745         loaded_image_info_obj.protocols[0].protocol_interface = info;
746         obj = malloc(sizeof(loaded_image_info_obj));
747         memset(info, 0, sizeof(*info));
748         memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
749         obj->handle = info;
750         info->file_path = file_path;
751         info->reserved = efi_load_pe(source_buffer, info);
752         if (!info->reserved) {
753                 free(info);
754                 free(obj);
755                 return EFI_EXIT(EFI_UNSUPPORTED);
756         }
757
758         *image_handle = info;
759         list_add_tail(&obj->link, &efi_obj_list);
760
761         return EFI_EXIT(EFI_SUCCESS);
762 }
763
764 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
765                                            unsigned long *exit_data_size,
766                                            s16 **exit_data)
767 {
768         ulong (*entry)(void *image_handle, struct efi_system_table *st);
769         struct efi_loaded_image *info = image_handle;
770
771         EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
772         entry = info->reserved;
773
774         efi_is_direct_boot = false;
775
776         /* call the image! */
777         if (setjmp(&info->exit_jmp)) {
778                 /* We returned from the child image */
779                 return EFI_EXIT(info->exit_status);
780         }
781
782         __efi_nesting_dec();
783         __efi_exit_check();
784         entry(image_handle, &systab);
785         __efi_entry_check();
786         __efi_nesting_inc();
787
788         /* Should usually never get here */
789         return EFI_EXIT(EFI_SUCCESS);
790 }
791
792 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
793                         efi_status_t exit_status, unsigned long exit_data_size,
794                         int16_t *exit_data)
795 {
796         struct efi_loaded_image *loaded_image_info = (void*)image_handle;
797
798         EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
799                   exit_data_size, exit_data);
800
801         /* Make sure entry/exit counts for EFI world cross-overs match */
802         __efi_exit_check();
803
804         /*
805          * But longjmp out with the U-Boot gd, not the application's, as
806          * the other end is a setjmp call inside EFI context.
807          */
808         efi_restore_gd();
809
810         loaded_image_info->exit_status = exit_status;
811         longjmp(&loaded_image_info->exit_jmp, 1);
812
813         panic("EFI application exited");
814 }
815
816 static struct efi_object *efi_search_obj(void *handle)
817 {
818         struct list_head *lhandle;
819
820         list_for_each(lhandle, &efi_obj_list) {
821                 struct efi_object *efiobj;
822                 efiobj = list_entry(lhandle, struct efi_object, link);
823                 if (efiobj->handle == handle)
824                         return efiobj;
825         }
826
827         return NULL;
828 }
829
830 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
831 {
832         struct efi_object *efiobj;
833
834         EFI_ENTRY("%p", image_handle);
835         efiobj = efi_search_obj(image_handle);
836         if (efiobj)
837                 list_del(&efiobj->link);
838
839         return EFI_EXIT(EFI_SUCCESS);
840 }
841
842 static void efi_exit_caches(void)
843 {
844 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
845         /*
846          * Grub on 32bit ARM needs to have caches disabled before jumping into
847          * a zImage, but does not know of all cache layers. Give it a hand.
848          */
849         if (efi_is_direct_boot)
850                 cleanup_before_linux();
851 #endif
852 }
853
854 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
855                                                   unsigned long map_key)
856 {
857         EFI_ENTRY("%p, %ld", image_handle, map_key);
858
859         board_quiesce_devices();
860
861         /* Fix up caches for EFI payloads if necessary */
862         efi_exit_caches();
863
864         /* This stops all lingering devices */
865         bootm_disable_interrupts();
866
867         /* Give the payload some time to boot */
868         WATCHDOG_RESET();
869
870         return EFI_EXIT(EFI_SUCCESS);
871 }
872
873 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
874 {
875         static uint64_t mono = 0;
876         EFI_ENTRY("%p", count);
877         *count = mono++;
878         return EFI_EXIT(EFI_SUCCESS);
879 }
880
881 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
882 {
883         EFI_ENTRY("%ld", microseconds);
884         udelay(microseconds);
885         return EFI_EXIT(EFI_SUCCESS);
886 }
887
888 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
889                                                   uint64_t watchdog_code,
890                                                   unsigned long data_size,
891                                                   uint16_t *watchdog_data)
892 {
893         EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
894                   data_size, watchdog_data);
895         return efi_unsupported(__func__);
896 }
897
898 static efi_status_t EFIAPI efi_connect_controller(
899                         efi_handle_t controller_handle,
900                         efi_handle_t *driver_image_handle,
901                         struct efi_device_path *remain_device_path,
902                         bool recursive)
903 {
904         EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
905                   remain_device_path, recursive);
906         return EFI_EXIT(EFI_NOT_FOUND);
907 }
908
909 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
910                                                      void *driver_image_handle,
911                                                      void *child_handle)
912 {
913         EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
914                   child_handle);
915         return EFI_EXIT(EFI_INVALID_PARAMETER);
916 }
917
918 static efi_status_t EFIAPI efi_close_protocol(void *handle,
919                                               efi_guid_t *protocol,
920                                               void *agent_handle,
921                                               void *controller_handle)
922 {
923         EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
924                   controller_handle);
925         return EFI_EXIT(EFI_NOT_FOUND);
926 }
927
928 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
929                         efi_guid_t *protocol,
930                         struct efi_open_protocol_info_entry **entry_buffer,
931                         unsigned long *entry_count)
932 {
933         EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
934                   entry_count);
935         return EFI_EXIT(EFI_NOT_FOUND);
936 }
937
938 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
939                         efi_guid_t ***protocol_buffer,
940                         unsigned long *protocol_buffer_count)
941 {
942         unsigned long buffer_size;
943         struct efi_object *efiobj;
944         unsigned long i, j;
945         struct list_head *lhandle;
946         efi_status_t r;
947
948         EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
949                   protocol_buffer_count);
950
951         if (!handle || !protocol_buffer || !protocol_buffer_count)
952                 return EFI_EXIT(EFI_INVALID_PARAMETER);
953
954         *protocol_buffer = NULL;
955         *protocol_buffer_count = 0;
956         list_for_each(lhandle, &efi_obj_list) {
957                 efiobj = list_entry(lhandle, struct efi_object, link);
958
959                 if (efiobj->handle != handle)
960                         continue;
961
962                 /* Count protocols */
963                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
964                         if (efiobj->protocols[i].guid)
965                                 ++*protocol_buffer_count;
966                 }
967                 /* Copy guids */
968                 if (*protocol_buffer_count) {
969                         buffer_size = sizeof(efi_guid_t *) *
970                                         *protocol_buffer_count;
971                         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
972                                               buffer_size,
973                                               (void **)protocol_buffer);
974                         if (r != EFI_SUCCESS)
975                                 return EFI_EXIT(r);
976                         j = 0;
977                         for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
978                                 if (efiobj->protocols[i].guid) {
979                                         (*protocol_buffer)[j] = (void *)
980                                                 efiobj->protocols[i].guid;
981                                         ++j;
982                                 }
983                         }
984                 }
985                 break;
986         }
987
988         return EFI_EXIT(EFI_SUCCESS);
989 }
990
991 static efi_status_t EFIAPI efi_locate_handle_buffer(
992                         enum efi_locate_search_type search_type,
993                         efi_guid_t *protocol, void *search_key,
994                         unsigned long *no_handles, efi_handle_t **buffer)
995 {
996         efi_status_t r;
997         unsigned long buffer_size = 0;
998
999         EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
1000                   no_handles, buffer);
1001
1002         if (!no_handles || !buffer) {
1003                 r = EFI_INVALID_PARAMETER;
1004                 goto out;
1005         }
1006         *no_handles = 0;
1007         *buffer = NULL;
1008         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1009                               *buffer);
1010         if (r != EFI_BUFFER_TOO_SMALL)
1011                 goto out;
1012         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1013                               (void **)buffer);
1014         if (r != EFI_SUCCESS)
1015                 goto out;
1016         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1017                               *buffer);
1018         if (r == EFI_SUCCESS)
1019                 *no_handles = buffer_size / sizeof(void *);
1020 out:
1021         return EFI_EXIT(r);
1022 }
1023
1024 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1025                                                void *registration,
1026                                                void **protocol_interface)
1027 {
1028         struct list_head *lhandle;
1029         int i;
1030
1031         EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
1032
1033         if (!protocol || !protocol_interface)
1034                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1035
1036         EFI_PRINT_GUID("protocol", protocol);
1037
1038         list_for_each(lhandle, &efi_obj_list) {
1039                 struct efi_object *efiobj;
1040
1041                 efiobj = list_entry(lhandle, struct efi_object, link);
1042                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1043                         struct efi_handler *handler = &efiobj->protocols[i];
1044
1045                         if (!handler->guid)
1046                                 continue;
1047                         if (!guidcmp(handler->guid, protocol)) {
1048                                 *protocol_interface =
1049                                         handler->protocol_interface;
1050                                 return EFI_EXIT(EFI_SUCCESS);
1051                         }
1052                 }
1053         }
1054         *protocol_interface = NULL;
1055
1056         return EFI_EXIT(EFI_NOT_FOUND);
1057 }
1058
1059 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1060                         void **handle, ...)
1061 {
1062         EFI_ENTRY("%p", handle);
1063
1064         va_list argptr;
1065         efi_guid_t *protocol;
1066         void *protocol_interface;
1067         efi_status_t r = EFI_SUCCESS;
1068         int i = 0;
1069
1070         if (!handle)
1071                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1072
1073         va_start(argptr, handle);
1074         for (;;) {
1075                 protocol = va_arg(argptr, efi_guid_t*);
1076                 if (!protocol)
1077                         break;
1078                 protocol_interface = va_arg(argptr, void*);
1079                 r = efi_install_protocol_interface(handle, protocol,
1080                                                    EFI_NATIVE_INTERFACE,
1081                                                    protocol_interface);
1082                 if (r != EFI_SUCCESS)
1083                         break;
1084                 i++;
1085         }
1086         va_end(argptr);
1087         if (r == EFI_SUCCESS)
1088                 return EFI_EXIT(r);
1089
1090         /* If an error occured undo all changes. */
1091         va_start(argptr, handle);
1092         for (; i; --i) {
1093                 protocol = va_arg(argptr, efi_guid_t*);
1094                 protocol_interface = va_arg(argptr, void*);
1095                 efi_uninstall_protocol_interface(handle, protocol,
1096                                                  protocol_interface);
1097         }
1098         va_end(argptr);
1099
1100         return EFI_EXIT(r);
1101 }
1102
1103 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1104                         void *handle, ...)
1105 {
1106         EFI_ENTRY("%p", handle);
1107         return EFI_EXIT(EFI_INVALID_PARAMETER);
1108 }
1109
1110 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1111                                                unsigned long data_size,
1112                                                uint32_t *crc32_p)
1113 {
1114         EFI_ENTRY("%p, %ld", data, data_size);
1115         *crc32_p = crc32(0, data, data_size);
1116         return EFI_EXIT(EFI_SUCCESS);
1117 }
1118
1119 static void EFIAPI efi_copy_mem(void *destination, void *source,
1120                                 unsigned long length)
1121 {
1122         EFI_ENTRY("%p, %p, %ld", destination, source, length);
1123         memcpy(destination, source, length);
1124 }
1125
1126 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1127 {
1128         EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1129         memset(buffer, value, size);
1130 }
1131
1132 static efi_status_t EFIAPI efi_open_protocol(
1133                         void *handle, efi_guid_t *protocol,
1134                         void **protocol_interface, void *agent_handle,
1135                         void *controller_handle, uint32_t attributes)
1136 {
1137         struct list_head *lhandle;
1138         int i;
1139         efi_status_t r = EFI_INVALID_PARAMETER;
1140
1141         EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1142                   protocol_interface, agent_handle, controller_handle,
1143                   attributes);
1144
1145         if (!handle || !protocol ||
1146             (!protocol_interface && attributes !=
1147              EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1148                 goto out;
1149         }
1150
1151         EFI_PRINT_GUID("protocol", protocol);
1152
1153         switch (attributes) {
1154         case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1155         case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1156         case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1157                 break;
1158         case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1159                 if (controller_handle == handle)
1160                         goto out;
1161         case EFI_OPEN_PROTOCOL_BY_DRIVER:
1162         case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1163                 if (controller_handle == NULL)
1164                         goto out;
1165         case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1166                 if (agent_handle == NULL)
1167                         goto out;
1168                 break;
1169         default:
1170                 goto out;
1171         }
1172
1173         list_for_each(lhandle, &efi_obj_list) {
1174                 struct efi_object *efiobj;
1175                 efiobj = list_entry(lhandle, struct efi_object, link);
1176
1177                 if (efiobj->handle != handle)
1178                         continue;
1179
1180                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1181                         struct efi_handler *handler = &efiobj->protocols[i];
1182                         const efi_guid_t *hprotocol = handler->guid;
1183                         if (!hprotocol)
1184                                 continue;
1185                         if (!guidcmp(hprotocol, protocol)) {
1186                                 if (attributes !=
1187                                     EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1188                                         *protocol_interface =
1189                                                 handler->protocol_interface;
1190                                 }
1191                                 r = EFI_SUCCESS;
1192                                 goto out;
1193                         }
1194                 }
1195                 goto unsupported;
1196         }
1197
1198 unsupported:
1199         r = EFI_UNSUPPORTED;
1200 out:
1201         return EFI_EXIT(r);
1202 }
1203
1204 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1205                                                efi_guid_t *protocol,
1206                                                void **protocol_interface)
1207 {
1208         return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1209                                  NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1210 }
1211
1212 static const struct efi_boot_services efi_boot_services = {
1213         .hdr = {
1214                 .headersize = sizeof(struct efi_table_hdr),
1215         },
1216         .raise_tpl = efi_raise_tpl,
1217         .restore_tpl = efi_restore_tpl,
1218         .allocate_pages = efi_allocate_pages_ext,
1219         .free_pages = efi_free_pages_ext,
1220         .get_memory_map = efi_get_memory_map_ext,
1221         .allocate_pool = efi_allocate_pool_ext,
1222         .free_pool = efi_free_pool_ext,
1223         .create_event = efi_create_event_ext,
1224         .set_timer = efi_set_timer_ext,
1225         .wait_for_event = efi_wait_for_event,
1226         .signal_event = efi_signal_event_ext,
1227         .close_event = efi_close_event,
1228         .check_event = efi_check_event,
1229         .install_protocol_interface = efi_install_protocol_interface_ext,
1230         .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1231         .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1232         .handle_protocol = efi_handle_protocol,
1233         .reserved = NULL,
1234         .register_protocol_notify = efi_register_protocol_notify,
1235         .locate_handle = efi_locate_handle_ext,
1236         .locate_device_path = efi_locate_device_path,
1237         .install_configuration_table = efi_install_configuration_table_ext,
1238         .load_image = efi_load_image,
1239         .start_image = efi_start_image,
1240         .exit = efi_exit,
1241         .unload_image = efi_unload_image,
1242         .exit_boot_services = efi_exit_boot_services,
1243         .get_next_monotonic_count = efi_get_next_monotonic_count,
1244         .stall = efi_stall,
1245         .set_watchdog_timer = efi_set_watchdog_timer,
1246         .connect_controller = efi_connect_controller,
1247         .disconnect_controller = efi_disconnect_controller,
1248         .open_protocol = efi_open_protocol,
1249         .close_protocol = efi_close_protocol,
1250         .open_protocol_information = efi_open_protocol_information,
1251         .protocols_per_handle = efi_protocols_per_handle,
1252         .locate_handle_buffer = efi_locate_handle_buffer,
1253         .locate_protocol = efi_locate_protocol,
1254         .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1255         .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1256         .calculate_crc32 = efi_calculate_crc32,
1257         .copy_mem = efi_copy_mem,
1258         .set_mem = efi_set_mem,
1259 };
1260
1261
1262 static uint16_t __efi_runtime_data firmware_vendor[] =
1263         { 'D','a','s',' ','U','-','b','o','o','t',0 };
1264
1265 struct efi_system_table __efi_runtime_data systab = {
1266         .hdr = {
1267                 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1268                 .revision = 0x20005, /* 2.5 */
1269                 .headersize = sizeof(struct efi_table_hdr),
1270         },
1271         .fw_vendor = (long)firmware_vendor,
1272         .con_in = (void*)&efi_con_in,
1273         .con_out = (void*)&efi_con_out,
1274         .std_err = (void*)&efi_con_out,
1275         .runtime = (void*)&efi_runtime_services,
1276         .boottime = (void*)&efi_boot_services,
1277         .nr_tables = 0,
1278         .tables = (void*)efi_conf_table,
1279 };