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