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