]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_boottime.c
efi_loader: write protocol GUID in OpenProtocol
[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         __efi_exit_check();
802
803         loaded_image_info->exit_status = exit_status;
804         longjmp(&loaded_image_info->exit_jmp, 1);
805
806         panic("EFI application exited");
807 }
808
809 static struct efi_object *efi_search_obj(void *handle)
810 {
811         struct list_head *lhandle;
812
813         list_for_each(lhandle, &efi_obj_list) {
814                 struct efi_object *efiobj;
815                 efiobj = list_entry(lhandle, struct efi_object, link);
816                 if (efiobj->handle == handle)
817                         return efiobj;
818         }
819
820         return NULL;
821 }
822
823 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
824 {
825         struct efi_object *efiobj;
826
827         EFI_ENTRY("%p", image_handle);
828         efiobj = efi_search_obj(image_handle);
829         if (efiobj)
830                 list_del(&efiobj->link);
831
832         return EFI_EXIT(EFI_SUCCESS);
833 }
834
835 static void efi_exit_caches(void)
836 {
837 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
838         /*
839          * Grub on 32bit ARM needs to have caches disabled before jumping into
840          * a zImage, but does not know of all cache layers. Give it a hand.
841          */
842         if (efi_is_direct_boot)
843                 cleanup_before_linux();
844 #endif
845 }
846
847 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
848                                                   unsigned long map_key)
849 {
850         EFI_ENTRY("%p, %ld", image_handle, map_key);
851
852         board_quiesce_devices();
853
854         /* Fix up caches for EFI payloads if necessary */
855         efi_exit_caches();
856
857         /* This stops all lingering devices */
858         bootm_disable_interrupts();
859
860         /* Give the payload some time to boot */
861         WATCHDOG_RESET();
862
863         return EFI_EXIT(EFI_SUCCESS);
864 }
865
866 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
867 {
868         static uint64_t mono = 0;
869         EFI_ENTRY("%p", count);
870         *count = mono++;
871         return EFI_EXIT(EFI_SUCCESS);
872 }
873
874 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
875 {
876         EFI_ENTRY("%ld", microseconds);
877         udelay(microseconds);
878         return EFI_EXIT(EFI_SUCCESS);
879 }
880
881 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
882                                                   uint64_t watchdog_code,
883                                                   unsigned long data_size,
884                                                   uint16_t *watchdog_data)
885 {
886         EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
887                   data_size, watchdog_data);
888         return efi_unsupported(__func__);
889 }
890
891 static efi_status_t EFIAPI efi_connect_controller(
892                         efi_handle_t controller_handle,
893                         efi_handle_t *driver_image_handle,
894                         struct efi_device_path *remain_device_path,
895                         bool recursive)
896 {
897         EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
898                   remain_device_path, recursive);
899         return EFI_EXIT(EFI_NOT_FOUND);
900 }
901
902 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
903                                                      void *driver_image_handle,
904                                                      void *child_handle)
905 {
906         EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
907                   child_handle);
908         return EFI_EXIT(EFI_INVALID_PARAMETER);
909 }
910
911 static efi_status_t EFIAPI efi_close_protocol(void *handle,
912                                               efi_guid_t *protocol,
913                                               void *agent_handle,
914                                               void *controller_handle)
915 {
916         EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
917                   controller_handle);
918         return EFI_EXIT(EFI_NOT_FOUND);
919 }
920
921 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
922                         efi_guid_t *protocol,
923                         struct efi_open_protocol_info_entry **entry_buffer,
924                         unsigned long *entry_count)
925 {
926         EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
927                   entry_count);
928         return EFI_EXIT(EFI_NOT_FOUND);
929 }
930
931 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
932                         efi_guid_t ***protocol_buffer,
933                         unsigned long *protocol_buffer_count)
934 {
935         unsigned long buffer_size;
936         struct efi_object *efiobj;
937         unsigned long i, j;
938         struct list_head *lhandle;
939         efi_status_t r;
940
941         EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
942                   protocol_buffer_count);
943
944         if (!handle || !protocol_buffer || !protocol_buffer_count)
945                 return EFI_EXIT(EFI_INVALID_PARAMETER);
946
947         *protocol_buffer = NULL;
948         *protocol_buffer_count = 0;
949         list_for_each(lhandle, &efi_obj_list) {
950                 efiobj = list_entry(lhandle, struct efi_object, link);
951
952                 if (efiobj->handle != handle)
953                         continue;
954
955                 /* Count protocols */
956                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
957                         if (efiobj->protocols[i].guid)
958                                 ++*protocol_buffer_count;
959                 }
960                 /* Copy guids */
961                 if (*protocol_buffer_count) {
962                         buffer_size = sizeof(efi_guid_t *) *
963                                         *protocol_buffer_count;
964                         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
965                                               buffer_size,
966                                               (void **)protocol_buffer);
967                         if (r != EFI_SUCCESS)
968                                 return EFI_EXIT(r);
969                         j = 0;
970                         for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
971                                 if (efiobj->protocols[i].guid) {
972                                         (*protocol_buffer)[j] = (void *)
973                                                 efiobj->protocols[i].guid;
974                                         ++j;
975                                 }
976                         }
977                 }
978                 break;
979         }
980
981         return EFI_EXIT(EFI_SUCCESS);
982 }
983
984 static efi_status_t EFIAPI efi_locate_handle_buffer(
985                         enum efi_locate_search_type search_type,
986                         efi_guid_t *protocol, void *search_key,
987                         unsigned long *no_handles, efi_handle_t **buffer)
988 {
989         efi_status_t r;
990         unsigned long buffer_size = 0;
991
992         EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
993                   no_handles, buffer);
994
995         if (!no_handles || !buffer) {
996                 r = EFI_INVALID_PARAMETER;
997                 goto out;
998         }
999         *no_handles = 0;
1000         *buffer = NULL;
1001         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1002                               *buffer);
1003         if (r != EFI_BUFFER_TOO_SMALL)
1004                 goto out;
1005         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1006                               (void **)buffer);
1007         if (r != EFI_SUCCESS)
1008                 goto out;
1009         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1010                               *buffer);
1011         if (r == EFI_SUCCESS)
1012                 *no_handles = buffer_size / sizeof(void *);
1013 out:
1014         return EFI_EXIT(r);
1015 }
1016
1017 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1018                                                void *registration,
1019                                                void **protocol_interface)
1020 {
1021         struct list_head *lhandle;
1022         int i;
1023
1024         EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
1025
1026         if (!protocol || !protocol_interface)
1027                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1028
1029         EFI_PRINT_GUID("protocol", protocol);
1030
1031         list_for_each(lhandle, &efi_obj_list) {
1032                 struct efi_object *efiobj;
1033
1034                 efiobj = list_entry(lhandle, struct efi_object, link);
1035                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1036                         struct efi_handler *handler = &efiobj->protocols[i];
1037
1038                         if (!handler->guid)
1039                                 continue;
1040                         if (!guidcmp(handler->guid, protocol)) {
1041                                 *protocol_interface =
1042                                         handler->protocol_interface;
1043                                 return EFI_EXIT(EFI_SUCCESS);
1044                         }
1045                 }
1046         }
1047         *protocol_interface = NULL;
1048
1049         return EFI_EXIT(EFI_NOT_FOUND);
1050 }
1051
1052 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1053                         void **handle, ...)
1054 {
1055         EFI_ENTRY("%p", handle);
1056
1057         va_list argptr;
1058         efi_guid_t *protocol;
1059         void *protocol_interface;
1060         efi_status_t r = EFI_SUCCESS;
1061         int i = 0;
1062
1063         if (!handle)
1064                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1065
1066         va_start(argptr, handle);
1067         for (;;) {
1068                 protocol = va_arg(argptr, efi_guid_t*);
1069                 if (!protocol)
1070                         break;
1071                 protocol_interface = va_arg(argptr, void*);
1072                 r = efi_install_protocol_interface(handle, protocol,
1073                                                    EFI_NATIVE_INTERFACE,
1074                                                    protocol_interface);
1075                 if (r != EFI_SUCCESS)
1076                         break;
1077                 i++;
1078         }
1079         va_end(argptr);
1080         if (r == EFI_SUCCESS)
1081                 return EFI_EXIT(r);
1082
1083         /* If an error occured undo all changes. */
1084         va_start(argptr, handle);
1085         for (; i; --i) {
1086                 protocol = va_arg(argptr, efi_guid_t*);
1087                 protocol_interface = va_arg(argptr, void*);
1088                 efi_uninstall_protocol_interface(handle, protocol,
1089                                                  protocol_interface);
1090         }
1091         va_end(argptr);
1092
1093         return EFI_EXIT(r);
1094 }
1095
1096 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1097                         void *handle, ...)
1098 {
1099         EFI_ENTRY("%p", handle);
1100         return EFI_EXIT(EFI_INVALID_PARAMETER);
1101 }
1102
1103 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1104                                                unsigned long data_size,
1105                                                uint32_t *crc32_p)
1106 {
1107         EFI_ENTRY("%p, %ld", data, data_size);
1108         *crc32_p = crc32(0, data, data_size);
1109         return EFI_EXIT(EFI_SUCCESS);
1110 }
1111
1112 static void EFIAPI efi_copy_mem(void *destination, void *source,
1113                                 unsigned long length)
1114 {
1115         EFI_ENTRY("%p, %p, %ld", destination, source, length);
1116         memcpy(destination, source, length);
1117 }
1118
1119 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1120 {
1121         EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1122         memset(buffer, value, size);
1123 }
1124
1125 static efi_status_t EFIAPI efi_open_protocol(
1126                         void *handle, efi_guid_t *protocol,
1127                         void **protocol_interface, void *agent_handle,
1128                         void *controller_handle, uint32_t attributes)
1129 {
1130         struct list_head *lhandle;
1131         int i;
1132         efi_status_t r = EFI_INVALID_PARAMETER;
1133
1134         EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1135                   protocol_interface, agent_handle, controller_handle,
1136                   attributes);
1137
1138         if (!handle || !protocol ||
1139             (!protocol_interface && attributes !=
1140              EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1141                 goto out;
1142         }
1143
1144         EFI_PRINT_GUID("protocol", protocol);
1145
1146         switch (attributes) {
1147         case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1148         case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1149         case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1150                 break;
1151         case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1152                 if (controller_handle == handle)
1153                         goto out;
1154         case EFI_OPEN_PROTOCOL_BY_DRIVER:
1155         case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1156                 if (controller_handle == NULL)
1157                         goto out;
1158         case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1159                 if (agent_handle == NULL)
1160                         goto out;
1161                 break;
1162         default:
1163                 goto out;
1164         }
1165
1166         list_for_each(lhandle, &efi_obj_list) {
1167                 struct efi_object *efiobj;
1168                 efiobj = list_entry(lhandle, struct efi_object, link);
1169
1170                 if (efiobj->handle != handle)
1171                         continue;
1172
1173                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1174                         struct efi_handler *handler = &efiobj->protocols[i];
1175                         const efi_guid_t *hprotocol = handler->guid;
1176                         if (!hprotocol)
1177                                 continue;
1178                         if (!guidcmp(hprotocol, protocol)) {
1179                                 if (attributes !=
1180                                     EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1181                                         *protocol_interface =
1182                                                 handler->protocol_interface;
1183                                 }
1184                                 r = EFI_SUCCESS;
1185                                 goto out;
1186                         }
1187                 }
1188                 goto unsupported;
1189         }
1190
1191 unsupported:
1192         r = EFI_UNSUPPORTED;
1193 out:
1194         return EFI_EXIT(r);
1195 }
1196
1197 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1198                                                efi_guid_t *protocol,
1199                                                void **protocol_interface)
1200 {
1201         return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1202                                  NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1203 }
1204
1205 static const struct efi_boot_services efi_boot_services = {
1206         .hdr = {
1207                 .headersize = sizeof(struct efi_table_hdr),
1208         },
1209         .raise_tpl = efi_raise_tpl,
1210         .restore_tpl = efi_restore_tpl,
1211         .allocate_pages = efi_allocate_pages_ext,
1212         .free_pages = efi_free_pages_ext,
1213         .get_memory_map = efi_get_memory_map_ext,
1214         .allocate_pool = efi_allocate_pool_ext,
1215         .free_pool = efi_free_pool_ext,
1216         .create_event = efi_create_event_ext,
1217         .set_timer = efi_set_timer_ext,
1218         .wait_for_event = efi_wait_for_event,
1219         .signal_event = efi_signal_event_ext,
1220         .close_event = efi_close_event,
1221         .check_event = efi_check_event,
1222         .install_protocol_interface = efi_install_protocol_interface_ext,
1223         .reinstall_protocol_interface = efi_reinstall_protocol_interface,
1224         .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1225         .handle_protocol = efi_handle_protocol,
1226         .reserved = NULL,
1227         .register_protocol_notify = efi_register_protocol_notify,
1228         .locate_handle = efi_locate_handle_ext,
1229         .locate_device_path = efi_locate_device_path,
1230         .install_configuration_table = efi_install_configuration_table_ext,
1231         .load_image = efi_load_image,
1232         .start_image = efi_start_image,
1233         .exit = efi_exit,
1234         .unload_image = efi_unload_image,
1235         .exit_boot_services = efi_exit_boot_services,
1236         .get_next_monotonic_count = efi_get_next_monotonic_count,
1237         .stall = efi_stall,
1238         .set_watchdog_timer = efi_set_watchdog_timer,
1239         .connect_controller = efi_connect_controller,
1240         .disconnect_controller = efi_disconnect_controller,
1241         .open_protocol = efi_open_protocol,
1242         .close_protocol = efi_close_protocol,
1243         .open_protocol_information = efi_open_protocol_information,
1244         .protocols_per_handle = efi_protocols_per_handle,
1245         .locate_handle_buffer = efi_locate_handle_buffer,
1246         .locate_protocol = efi_locate_protocol,
1247         .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1248         .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1249         .calculate_crc32 = efi_calculate_crc32,
1250         .copy_mem = efi_copy_mem,
1251         .set_mem = efi_set_mem,
1252 };
1253
1254
1255 static uint16_t __efi_runtime_data firmware_vendor[] =
1256         { 'D','a','s',' ','U','-','b','o','o','t',0 };
1257
1258 struct efi_system_table __efi_runtime_data systab = {
1259         .hdr = {
1260                 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
1261                 .revision = 0x20005, /* 2.5 */
1262                 .headersize = sizeof(struct efi_table_hdr),
1263         },
1264         .fw_vendor = (long)firmware_vendor,
1265         .con_in = (void*)&efi_con_in,
1266         .con_out = (void*)&efi_con_out,
1267         .std_err = (void*)&efi_con_out,
1268         .runtime = (void*)&efi_runtime_services,
1269         .boottime = (void*)&efi_boot_services,
1270         .nr_tables = 0,
1271         .tables = (void*)efi_conf_table,
1272 };