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