]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_boottime.c
efi_loader: set parent handle in efi_load_image
[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 <div64.h>
11 #include <efi_loader.h>
12 #include <environment.h>
13 #include <malloc.h>
14 #include <asm/global_data.h>
15 #include <libfdt_env.h>
16 #include <u-boot/crc.h>
17 #include <bootm.h>
18 #include <inttypes.h>
19 #include <watchdog.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 /* Task priority level */
24 static UINTN efi_tpl = TPL_APPLICATION;
25
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list);
28
29 /*
30  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31  * we need to do trickery with caches. Since we don't want to break the EFI
32  * aware boot path, only apply hacks when loading exiting directly (breaking
33  * direct Linux EFI booting along the way - oh well).
34  */
35 static bool efi_is_direct_boot = true;
36
37 /*
38  * EFI can pass arbitrary additional "tables" containing vendor specific
39  * information to the payload. One such table is the FDT table which contains
40  * a pointer to a flattened device tree blob.
41  *
42  * In most cases we want to pass an FDT to the payload, so reserve one slot of
43  * config table space for it. The pointer gets populated by do_bootefi_exec().
44  */
45 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
46
47 #ifdef CONFIG_ARM
48 /*
49  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50  * fixed when compiling U-Boot. However, the payload does not know about that
51  * restriction so we need to manually swap its and our view of that register on
52  * EFI callback entry/exit.
53  */
54 static volatile void *efi_gd, *app_gd;
55 #endif
56
57 static int entry_count;
58 static int nesting_level;
59
60 /* Called on every callback entry */
61 int __efi_entry_check(void)
62 {
63         int ret = entry_count++ == 0;
64 #ifdef CONFIG_ARM
65         assert(efi_gd);
66         app_gd = gd;
67         gd = efi_gd;
68 #endif
69         return ret;
70 }
71
72 /* Called on every callback exit */
73 int __efi_exit_check(void)
74 {
75         int ret = --entry_count == 0;
76 #ifdef CONFIG_ARM
77         gd = app_gd;
78 #endif
79         return ret;
80 }
81
82 /* Called from do_bootefi_exec() */
83 void efi_save_gd(void)
84 {
85 #ifdef CONFIG_ARM
86         efi_gd = gd;
87 #endif
88 }
89
90 /*
91  * Special case handler for error/abort that just forces things back
92  * to u-boot world so we can dump out an abort msg, without any care
93  * about returning back to UEFI world.
94  */
95 void efi_restore_gd(void)
96 {
97 #ifdef CONFIG_ARM
98         /* Only restore if we're already in EFI context */
99         if (!efi_gd)
100                 return;
101         gd = efi_gd;
102 #endif
103 }
104
105 /*
106  * Two spaces per indent level, maxing out at 10.. which ought to be
107  * enough for anyone ;-)
108  */
109 static const char *indent_string(int level)
110 {
111         const char *indent = "                    ";
112         const int max = strlen(indent);
113         level = min(max, level * 2);
114         return &indent[max - level];
115 }
116
117 const char *__efi_nesting(void)
118 {
119         return indent_string(nesting_level);
120 }
121
122 const char *__efi_nesting_inc(void)
123 {
124         return indent_string(nesting_level++);
125 }
126
127 const char *__efi_nesting_dec(void)
128 {
129         return indent_string(--nesting_level);
130 }
131
132 /*
133  * Queue an EFI event.
134  *
135  * This function queues the notification function of the event for future
136  * execution.
137  *
138  * The notification function is called if the task priority level of the
139  * event is higher than the current task priority level.
140  *
141  * For the SignalEvent service see efi_signal_event_ext.
142  *
143  * @event       event to signal
144  */
145 void efi_signal_event(struct efi_event *event)
146 {
147         if (event->notify_function) {
148                 event->is_queued = true;
149                 /* Check TPL */
150                 if (efi_tpl >= event->notify_tpl)
151                         return;
152                 EFI_CALL_VOID(event->notify_function(event,
153                                                      event->notify_context));
154         }
155         event->is_queued = false;
156 }
157
158 /*
159  * Raise the task priority level.
160  *
161  * This function implements the RaiseTpl service.
162  * See the Unified Extensible Firmware Interface (UEFI) specification
163  * for details.
164  *
165  * @new_tpl     new value of the task priority level
166  * @return      old value of the task priority level
167  */
168 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
169 {
170         UINTN old_tpl = efi_tpl;
171
172         EFI_ENTRY("0x%zx", new_tpl);
173
174         if (new_tpl < efi_tpl)
175                 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176         efi_tpl = new_tpl;
177         if (efi_tpl > TPL_HIGH_LEVEL)
178                 efi_tpl = TPL_HIGH_LEVEL;
179
180         EFI_EXIT(EFI_SUCCESS);
181         return old_tpl;
182 }
183
184 /*
185  * Lower the task priority level.
186  *
187  * This function implements the RestoreTpl service.
188  * See the Unified Extensible Firmware Interface (UEFI) specification
189  * for details.
190  *
191  * @old_tpl     value of the task priority level to be restored
192  */
193 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
194 {
195         EFI_ENTRY("0x%zx", old_tpl);
196
197         if (old_tpl > efi_tpl)
198                 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199         efi_tpl = old_tpl;
200         if (efi_tpl > TPL_HIGH_LEVEL)
201                 efi_tpl = TPL_HIGH_LEVEL;
202
203         EFI_EXIT(EFI_SUCCESS);
204 }
205
206 /*
207  * Allocate memory pages.
208  *
209  * This function implements the AllocatePages service.
210  * See the Unified Extensible Firmware Interface (UEFI) specification
211  * for details.
212  *
213  * @type                type of allocation to be performed
214  * @memory_type         usage type of the allocated memory
215  * @pages               number of pages to be allocated
216  * @memory              allocated memory
217  * @return              status code
218  */
219 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
220                                                   unsigned long pages,
221                                                   uint64_t *memory)
222 {
223         efi_status_t r;
224
225         EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
226         r = efi_allocate_pages(type, memory_type, pages, memory);
227         return EFI_EXIT(r);
228 }
229
230 /*
231  * Free memory pages.
232  *
233  * This function implements the FreePages service.
234  * See the Unified Extensible Firmware Interface (UEFI) specification
235  * for details.
236  *
237  * @memory      start of the memory area to be freed
238  * @pages       number of pages to be freed
239  * @return      status code
240  */
241 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
242                                               unsigned long pages)
243 {
244         efi_status_t r;
245
246         EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
247         r = efi_free_pages(memory, pages);
248         return EFI_EXIT(r);
249 }
250
251 /*
252  * Get map describing memory usage.
253  *
254  * This function implements the GetMemoryMap service.
255  * See the Unified Extensible Firmware Interface (UEFI) specification
256  * for details.
257  *
258  * @memory_map_size     on entry the size, in bytes, of the memory map buffer,
259  *                      on exit the size of the copied memory map
260  * @memory_map          buffer to which the memory map is written
261  * @map_key             key for the memory map
262  * @descriptor_size     size of an individual memory descriptor
263  * @descriptor_version  version number of the memory descriptor structure
264  * @return              status code
265  */
266 static efi_status_t EFIAPI efi_get_memory_map_ext(
267                                         unsigned long *memory_map_size,
268                                         struct efi_mem_desc *memory_map,
269                                         unsigned long *map_key,
270                                         unsigned long *descriptor_size,
271                                         uint32_t *descriptor_version)
272 {
273         efi_status_t r;
274
275         EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276                   map_key, descriptor_size, descriptor_version);
277         r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278                                descriptor_size, descriptor_version);
279         return EFI_EXIT(r);
280 }
281
282 /*
283  * Allocate memory from pool.
284  *
285  * This function implements the AllocatePool service.
286  * See the Unified Extensible Firmware Interface (UEFI) specification
287  * for details.
288  *
289  * @pool_type   type of the pool from which memory is to be allocated
290  * @size        number of bytes to be allocated
291  * @buffer      allocated memory
292  * @return      status code
293  */
294 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
295                                                  unsigned long size,
296                                                  void **buffer)
297 {
298         efi_status_t r;
299
300         EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
301         r = efi_allocate_pool(pool_type, size, buffer);
302         return EFI_EXIT(r);
303 }
304
305 /*
306  * Free memory from pool.
307  *
308  * This function implements the FreePool service.
309  * See the Unified Extensible Firmware Interface (UEFI) specification
310  * for details.
311  *
312  * @buffer      start of memory to be freed
313  * @return      status code
314  */
315 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
316 {
317         efi_status_t r;
318
319         EFI_ENTRY("%p", buffer);
320         r = efi_free_pool(buffer);
321         return EFI_EXIT(r);
322 }
323
324 static efi_status_t efi_create_handle(void **handle)
325 {
326         struct efi_object *obj;
327         efi_status_t r;
328
329         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
330                               sizeof(struct efi_object),
331                               (void **)&obj);
332         if (r != EFI_SUCCESS)
333                 return r;
334         memset(obj, 0, sizeof(struct efi_object));
335         obj->handle = obj;
336         list_add_tail(&obj->link, &efi_obj_list);
337         *handle = obj;
338         return r;
339 }
340
341 /*
342  * Our event capabilities are very limited. Only a small limited
343  * number of events is allowed to coexist.
344  */
345 static struct efi_event efi_events[16];
346
347 /*
348  * Create an event.
349  *
350  * This function is used inside U-Boot code to create an event.
351  *
352  * For the API function implementing the CreateEvent service see
353  * efi_create_event_ext.
354  *
355  * @type                type of the event to create
356  * @notify_tpl          task priority level of the event
357  * @notify_function     notification function of the event
358  * @notify_context      pointer passed to the notification function
359  * @event               created event
360  * @return              status code
361  */
362 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
363                               void (EFIAPI *notify_function) (
364                                         struct efi_event *event,
365                                         void *context),
366                               void *notify_context, struct efi_event **event)
367 {
368         int i;
369
370         if (event == NULL)
371                 return EFI_INVALID_PARAMETER;
372
373         if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
374                 return EFI_INVALID_PARAMETER;
375
376         if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
377             notify_function == NULL)
378                 return EFI_INVALID_PARAMETER;
379
380         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
381                 if (efi_events[i].type)
382                         continue;
383                 efi_events[i].type = type;
384                 efi_events[i].notify_tpl = notify_tpl;
385                 efi_events[i].notify_function = notify_function;
386                 efi_events[i].notify_context = notify_context;
387                 /* Disable timers on bootup */
388                 efi_events[i].trigger_next = -1ULL;
389                 efi_events[i].is_queued = false;
390                 efi_events[i].is_signaled = false;
391                 *event = &efi_events[i];
392                 return EFI_SUCCESS;
393         }
394         return EFI_OUT_OF_RESOURCES;
395 }
396
397 /*
398  * Create an event.
399  *
400  * This function implements the CreateEvent service.
401  * See the Unified Extensible Firmware Interface (UEFI) specification
402  * for details.
403  *
404  * @type                type of the event to create
405  * @notify_tpl          task priority level of the event
406  * @notify_function     notification function of the event
407  * @notify_context      pointer passed to the notification function
408  * @event               created event
409  * @return              status code
410  */
411 static efi_status_t EFIAPI efi_create_event_ext(
412                         uint32_t type, UINTN notify_tpl,
413                         void (EFIAPI *notify_function) (
414                                         struct efi_event *event,
415                                         void *context),
416                         void *notify_context, struct efi_event **event)
417 {
418         EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
419                   notify_context);
420         return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
421                                          notify_context, event));
422 }
423
424
425 /*
426  * Check if a timer event has occurred or a queued notification function should
427  * be called.
428  *
429  * Our timers have to work without interrupts, so we check whenever keyboard
430  * input or disk accesses happen if enough time elapsed for them to fire.
431  */
432 void efi_timer_check(void)
433 {
434         int i;
435         u64 now = timer_get_us();
436
437         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
438                 if (!efi_events[i].type)
439                         continue;
440                 if (efi_events[i].is_queued)
441                         efi_signal_event(&efi_events[i]);
442                 if (!(efi_events[i].type & EVT_TIMER) ||
443                     now < efi_events[i].trigger_next)
444                         continue;
445                 switch (efi_events[i].trigger_type) {
446                 case EFI_TIMER_RELATIVE:
447                         efi_events[i].trigger_type = EFI_TIMER_STOP;
448                         break;
449                 case EFI_TIMER_PERIODIC:
450                         efi_events[i].trigger_next +=
451                                 efi_events[i].trigger_time;
452                         break;
453                 default:
454                         continue;
455                 }
456                 efi_events[i].is_signaled = true;
457                 efi_signal_event(&efi_events[i]);
458         }
459         WATCHDOG_RESET();
460 }
461
462 /*
463  * Set the trigger time for a timer event or stop the event.
464  *
465  * This is the function for internal usage in U-Boot. For the API function
466  * implementing the SetTimer service see efi_set_timer_ext.
467  *
468  * @event               event for which the timer is set
469  * @type                type of the timer
470  * @trigger_time        trigger period in multiples of 100ns
471  * @return              status code
472  */
473 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
474                            uint64_t trigger_time)
475 {
476         int i;
477
478         /*
479          * The parameter defines a multiple of 100ns.
480          * We use multiples of 1000ns. So divide by 10.
481          */
482         do_div(trigger_time, 10);
483
484         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
485                 if (event != &efi_events[i])
486                         continue;
487
488                 if (!(event->type & EVT_TIMER))
489                         break;
490                 switch (type) {
491                 case EFI_TIMER_STOP:
492                         event->trigger_next = -1ULL;
493                         break;
494                 case EFI_TIMER_PERIODIC:
495                 case EFI_TIMER_RELATIVE:
496                         event->trigger_next =
497                                 timer_get_us() + trigger_time;
498                         break;
499                 default:
500                         return EFI_INVALID_PARAMETER;
501                 }
502                 event->trigger_type = type;
503                 event->trigger_time = trigger_time;
504                 event->is_signaled = false;
505                 return EFI_SUCCESS;
506         }
507         return EFI_INVALID_PARAMETER;
508 }
509
510 /*
511  * Set the trigger time for a timer event or stop the event.
512  *
513  * This function implements the SetTimer service.
514  * See the Unified Extensible Firmware Interface (UEFI) specification
515  * for details.
516  *
517  * @event               event for which the timer is set
518  * @type                type of the timer
519  * @trigger_time        trigger period in multiples of 100ns
520  * @return              status code
521  */
522 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
523                                              enum efi_timer_delay type,
524                                              uint64_t trigger_time)
525 {
526         EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
527         return EFI_EXIT(efi_set_timer(event, type, trigger_time));
528 }
529
530 /*
531  * Wait for events to be signaled.
532  *
533  * This function implements the WaitForEvent service.
534  * See the Unified Extensible Firmware Interface (UEFI) specification
535  * for details.
536  *
537  * @num_events  number of events to be waited for
538  * @events      events to be waited for
539  * @index       index of the event that was signaled
540  * @return      status code
541  */
542 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
543                                               struct efi_event **event,
544                                               size_t *index)
545 {
546         int i, j;
547
548         EFI_ENTRY("%ld, %p, %p", num_events, event, index);
549
550         /* Check parameters */
551         if (!num_events || !event)
552                 return EFI_EXIT(EFI_INVALID_PARAMETER);
553         /* Check TPL */
554         if (efi_tpl != TPL_APPLICATION)
555                 return EFI_EXIT(EFI_UNSUPPORTED);
556         for (i = 0; i < num_events; ++i) {
557                 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
558                         if (event[i] == &efi_events[j])
559                                 goto known_event;
560                 }
561                 return EFI_EXIT(EFI_INVALID_PARAMETER);
562 known_event:
563                 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
564                         return EFI_EXIT(EFI_INVALID_PARAMETER);
565                 if (!event[i]->is_signaled)
566                         efi_signal_event(event[i]);
567         }
568
569         /* Wait for signal */
570         for (;;) {
571                 for (i = 0; i < num_events; ++i) {
572                         if (event[i]->is_signaled)
573                                 goto out;
574                 }
575                 /* Allow events to occur. */
576                 efi_timer_check();
577         }
578
579 out:
580         /*
581          * Reset the signal which is passed to the caller to allow periodic
582          * events to occur.
583          */
584         event[i]->is_signaled = false;
585         if (index)
586                 *index = i;
587
588         return EFI_EXIT(EFI_SUCCESS);
589 }
590
591 /*
592  * Signal an EFI event.
593  *
594  * This function implements the SignalEvent service.
595  * See the Unified Extensible Firmware Interface (UEFI) specification
596  * for details.
597  *
598  * This functions sets the signaled state of the event and queues the
599  * notification function for execution.
600  *
601  * @event       event to signal
602  * @return      status code
603  */
604 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
605 {
606         int i;
607
608         EFI_ENTRY("%p", event);
609         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
610                 if (event != &efi_events[i])
611                         continue;
612                 if (event->is_signaled)
613                         break;
614                 event->is_signaled = true;
615                 if (event->type & EVT_NOTIFY_SIGNAL)
616                         efi_signal_event(event);
617                 break;
618         }
619         return EFI_EXIT(EFI_SUCCESS);
620 }
621
622 /*
623  * Close an EFI event.
624  *
625  * This function implements the CloseEvent service.
626  * See the Unified Extensible Firmware Interface (UEFI) specification
627  * for details.
628  *
629  * @event       event to close
630  * @return      status code
631  */
632 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
633 {
634         int i;
635
636         EFI_ENTRY("%p", event);
637         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
638                 if (event == &efi_events[i]) {
639                         event->type = 0;
640                         event->trigger_next = -1ULL;
641                         event->is_queued = false;
642                         event->is_signaled = false;
643                         return EFI_EXIT(EFI_SUCCESS);
644                 }
645         }
646         return EFI_EXIT(EFI_INVALID_PARAMETER);
647 }
648
649 /*
650  * Check if an event is signaled.
651  *
652  * This function implements the CheckEvent service.
653  * See the Unified Extensible Firmware Interface (UEFI) specification
654  * for details.
655  *
656  * If an event is not signaled yet the notification function is queued.
657  *
658  * @event       event to check
659  * @return      status code
660  */
661 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
662 {
663         int i;
664
665         EFI_ENTRY("%p", event);
666         efi_timer_check();
667         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
668                 if (event != &efi_events[i])
669                         continue;
670                 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
671                         break;
672                 if (!event->is_signaled)
673                         efi_signal_event(event);
674                 if (event->is_signaled)
675                         return EFI_EXIT(EFI_SUCCESS);
676                 return EFI_EXIT(EFI_NOT_READY);
677         }
678         return EFI_EXIT(EFI_INVALID_PARAMETER);
679 }
680
681 /*
682  * Find the internal EFI object for a handle.
683  *
684  * @handle      handle to find
685  * @return      EFI object
686  */
687 static struct efi_object *efi_search_obj(void *handle)
688 {
689         struct list_head *lhandle;
690
691         list_for_each(lhandle, &efi_obj_list) {
692                 struct efi_object *efiobj;
693
694                 efiobj = list_entry(lhandle, struct efi_object, link);
695                 if (efiobj->handle == handle)
696                         return efiobj;
697         }
698
699         return NULL;
700 }
701
702 /*
703  * Install protocol interface.
704  *
705  * This is the function for internal calls. For the API implementation of the
706  * InstallProtocolInterface service see function
707  * efi_install_protocol_interface_ext.
708  *
709  * @handle                      handle on which the protocol shall be installed
710  * @protocol                    GUID of the protocol to be installed
711  * @protocol_interface_type     type of the interface to be installed,
712  *                              always EFI_NATIVE_INTERFACE
713  * @protocol_interface          interface of the protocol implementation
714  * @return                      status code
715  */
716 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
717                         const efi_guid_t *protocol, int protocol_interface_type,
718                         void *protocol_interface)
719 {
720         struct list_head *lhandle;
721         int i;
722         efi_status_t r;
723
724         if (!handle || !protocol ||
725             protocol_interface_type != EFI_NATIVE_INTERFACE) {
726                 r = EFI_INVALID_PARAMETER;
727                 goto out;
728         }
729
730         /* Create new handle if requested. */
731         if (!*handle) {
732                 r = efi_create_handle(handle);
733                 if (r != EFI_SUCCESS)
734                         goto out;
735         }
736         /* Find object. */
737         list_for_each(lhandle, &efi_obj_list) {
738                 struct efi_object *efiobj;
739                 efiobj = list_entry(lhandle, struct efi_object, link);
740
741                 if (efiobj->handle != *handle)
742                         continue;
743                 /* Check if protocol is already installed on the handle. */
744                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
745                         struct efi_handler *handler = &efiobj->protocols[i];
746
747                         if (!handler->guid)
748                                 continue;
749                         if (!guidcmp(handler->guid, protocol)) {
750                                 r = EFI_INVALID_PARAMETER;
751                                 goto out;
752                         }
753                 }
754                 /* Install protocol in first empty slot. */
755                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
756                         struct efi_handler *handler = &efiobj->protocols[i];
757
758                         if (handler->guid)
759                                 continue;
760
761                         handler->guid = protocol;
762                         handler->protocol_interface = protocol_interface;
763                         r = EFI_SUCCESS;
764                         goto out;
765                 }
766                 r = EFI_OUT_OF_RESOURCES;
767                 goto out;
768         }
769         r = EFI_INVALID_PARAMETER;
770 out:
771         return r;
772 }
773
774 /*
775  * Install protocol interface.
776  *
777  * This function implements the InstallProtocolInterface service.
778  * See the Unified Extensible Firmware Interface (UEFI) specification
779  * for details.
780  *
781  * @handle                      handle on which the protocol shall be installed
782  * @protocol                    GUID of the protocol to be installed
783  * @protocol_interface_type     type of the interface to be installed,
784  *                              always EFI_NATIVE_INTERFACE
785  * @protocol_interface          interface of the protocol implementation
786  * @return                      status code
787  */
788 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
789                         const efi_guid_t *protocol, int protocol_interface_type,
790                         void *protocol_interface)
791 {
792         EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
793                   protocol_interface);
794
795         return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
796                                                        protocol_interface_type,
797                                                        protocol_interface));
798 }
799
800 /*
801  * Reinstall protocol interface.
802  *
803  * This function implements the ReinstallProtocolInterface service.
804  * See the Unified Extensible Firmware Interface (UEFI) specification
805  * for details.
806  *
807  * @handle                      handle on which the protocol shall be
808  *                              reinstalled
809  * @protocol                    GUID of the protocol to be installed
810  * @old_interface               interface to be removed
811  * @new_interface               interface to be installed
812  * @return                      status code
813  */
814 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
815                         const efi_guid_t *protocol, void *old_interface,
816                         void *new_interface)
817 {
818         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
819                   new_interface);
820         return EFI_EXIT(EFI_ACCESS_DENIED);
821 }
822
823 /*
824  * Uninstall protocol interface.
825  *
826  * This is the function for internal calls. For the API implementation of the
827  * UninstallProtocolInterface service see function
828  * efi_uninstall_protocol_interface_ext.
829  *
830  * @handle                      handle from which the protocol shall be removed
831  * @protocol                    GUID of the protocol to be removed
832  * @protocol_interface          interface to be removed
833  * @return                      status code
834  */
835 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
836                         const efi_guid_t *protocol, void *protocol_interface)
837 {
838         struct list_head *lhandle;
839         int i;
840         efi_status_t r = EFI_NOT_FOUND;
841
842         if (!handle || !protocol) {
843                 r = EFI_INVALID_PARAMETER;
844                 goto out;
845         }
846
847         list_for_each(lhandle, &efi_obj_list) {
848                 struct efi_object *efiobj;
849                 efiobj = list_entry(lhandle, struct efi_object, link);
850
851                 if (efiobj->handle != handle)
852                         continue;
853
854                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
855                         struct efi_handler *handler = &efiobj->protocols[i];
856                         const efi_guid_t *hprotocol = handler->guid;
857
858                         if (!hprotocol)
859                                 continue;
860                         if (!guidcmp(hprotocol, protocol)) {
861                                 if (handler->protocol_interface) {
862                                         r = EFI_ACCESS_DENIED;
863                                 } else {
864                                         handler->guid = 0;
865                                         r = EFI_SUCCESS;
866                                 }
867                                 goto out;
868                         }
869                 }
870         }
871
872 out:
873         return r;
874 }
875
876 /*
877  * Uninstall protocol interface.
878  *
879  * This function implements the UninstallProtocolInterface service.
880  * See the Unified Extensible Firmware Interface (UEFI) specification
881  * for details.
882  *
883  * @handle                      handle from which the protocol shall be removed
884  * @protocol                    GUID of the protocol to be removed
885  * @protocol_interface          interface to be removed
886  * @return                      status code
887  */
888 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
889                         const efi_guid_t *protocol, void *protocol_interface)
890 {
891         EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
892
893         return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
894                                                          protocol_interface));
895 }
896
897 /*
898  * Register an event for notification when a protocol is installed.
899  *
900  * This function implements the RegisterProtocolNotify service.
901  * See the Unified Extensible Firmware Interface (UEFI) specification
902  * for details.
903  *
904  * @protocol            GUID of the protocol whose installation shall be
905  *                      notified
906  * @event               event to be signaled upon installation of the protocol
907  * @registration        key for retrieving the registration information
908  * @return              status code
909  */
910 static efi_status_t EFIAPI efi_register_protocol_notify(
911                                                 const efi_guid_t *protocol,
912                                                 struct efi_event *event,
913                                                 void **registration)
914 {
915         EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
916         return EFI_EXIT(EFI_OUT_OF_RESOURCES);
917 }
918
919 /*
920  * Determine if an EFI handle implements a protocol.
921  *
922  * See the documentation of the LocateHandle service in the UEFI specification.
923  *
924  * @search_type         selection criterion
925  * @protocol            GUID of the protocol
926  * @search_key          registration key
927  * @efiobj              handle
928  * @return              0 if the handle implements the protocol
929  */
930 static int efi_search(enum efi_locate_search_type search_type,
931                       const efi_guid_t *protocol, void *search_key,
932                       struct efi_object *efiobj)
933 {
934         int i;
935
936         switch (search_type) {
937         case all_handles:
938                 return 0;
939         case by_register_notify:
940                 return -1;
941         case by_protocol:
942                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
943                         const efi_guid_t *guid = efiobj->protocols[i].guid;
944                         if (guid && !guidcmp(guid, protocol))
945                                 return 0;
946                 }
947                 return -1;
948         }
949
950         return -1;
951 }
952
953 /*
954  * Locate handles implementing a protocol.
955  *
956  * This function is meant for U-Boot internal calls. For the API implementation
957  * of the LocateHandle service see efi_locate_handle_ext.
958  *
959  * @search_type         selection criterion
960  * @protocol            GUID of the protocol
961  * @search_key          registration key
962  * @buffer_size         size of the buffer to receive the handles in bytes
963  * @buffer              buffer to receive the relevant handles
964  * @return              status code
965  */
966 static efi_status_t efi_locate_handle(
967                         enum efi_locate_search_type search_type,
968                         const efi_guid_t *protocol, void *search_key,
969                         unsigned long *buffer_size, efi_handle_t *buffer)
970 {
971         struct list_head *lhandle;
972         unsigned long size = 0;
973
974         /* Count how much space we need */
975         list_for_each(lhandle, &efi_obj_list) {
976                 struct efi_object *efiobj;
977                 efiobj = list_entry(lhandle, struct efi_object, link);
978                 if (!efi_search(search_type, protocol, search_key, efiobj)) {
979                         size += sizeof(void*);
980                 }
981         }
982
983         if (*buffer_size < size) {
984                 *buffer_size = size;
985                 return EFI_BUFFER_TOO_SMALL;
986         }
987
988         *buffer_size = size;
989         if (size == 0)
990                 return EFI_NOT_FOUND;
991
992         /* Then fill the array */
993         list_for_each(lhandle, &efi_obj_list) {
994                 struct efi_object *efiobj;
995                 efiobj = list_entry(lhandle, struct efi_object, link);
996                 if (!efi_search(search_type, protocol, search_key, efiobj)) {
997                         *(buffer++) = efiobj->handle;
998                 }
999         }
1000
1001         return EFI_SUCCESS;
1002 }
1003
1004 /*
1005  * Locate handles implementing a protocol.
1006  *
1007  * This function implements the LocateHandle service.
1008  * See the Unified Extensible Firmware Interface (UEFI) specification
1009  * for details.
1010  *
1011  * @search_type         selection criterion
1012  * @protocol            GUID of the protocol
1013  * @search_key          registration key
1014  * @buffer_size         size of the buffer to receive the handles in bytes
1015  * @buffer              buffer to receive the relevant handles
1016  * @return              0 if the handle implements the protocol
1017  */
1018 static efi_status_t EFIAPI efi_locate_handle_ext(
1019                         enum efi_locate_search_type search_type,
1020                         const efi_guid_t *protocol, void *search_key,
1021                         unsigned long *buffer_size, efi_handle_t *buffer)
1022 {
1023         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1024                   buffer_size, buffer);
1025
1026         return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1027                         buffer_size, buffer));
1028 }
1029
1030 /*
1031  * Get the device path and handle of an device implementing a protocol.
1032  *
1033  * This function implements the LocateDevicePath service.
1034  * See the Unified Extensible Firmware Interface (UEFI) specification
1035  * for details.
1036  *
1037  * @protocol            GUID of the protocol
1038  * @device_path         device path
1039  * @device              handle of the device
1040  * @return              status code
1041  */
1042 static efi_status_t EFIAPI efi_locate_device_path(
1043                         const efi_guid_t *protocol,
1044                         struct efi_device_path **device_path,
1045                         efi_handle_t *device)
1046 {
1047         struct efi_object *efiobj;
1048
1049         EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1050
1051         efiobj = efi_dp_find_obj(*device_path, device_path);
1052         if (!efiobj)
1053                 return EFI_EXIT(EFI_NOT_FOUND);
1054
1055         *device = efiobj->handle;
1056
1057         return EFI_EXIT(EFI_SUCCESS);
1058 }
1059
1060 /* Collapses configuration table entries, removing index i */
1061 static void efi_remove_configuration_table(int i)
1062 {
1063         struct efi_configuration_table *this = &efi_conf_table[i];
1064         struct efi_configuration_table *next = &efi_conf_table[i+1];
1065         struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1066
1067         memmove(this, next, (ulong)end - (ulong)next);
1068         systab.nr_tables--;
1069 }
1070
1071 /*
1072  * Adds, updates, or removes a configuration table.
1073  *
1074  * This function is used for internal calls. For the API implementation of the
1075  * InstallConfigurationTable service see efi_install_configuration_table_ext.
1076  *
1077  * @guid                GUID of the installed table
1078  * @table               table to be installed
1079  * @return              status code
1080  */
1081 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1082 {
1083         int i;
1084
1085         /* Check for guid override */
1086         for (i = 0; i < systab.nr_tables; i++) {
1087                 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1088                         if (table)
1089                                 efi_conf_table[i].table = table;
1090                         else
1091                                 efi_remove_configuration_table(i);
1092                         return EFI_SUCCESS;
1093                 }
1094         }
1095
1096         if (!table)
1097                 return EFI_NOT_FOUND;
1098
1099         /* No override, check for overflow */
1100         if (i >= ARRAY_SIZE(efi_conf_table))
1101                 return EFI_OUT_OF_RESOURCES;
1102
1103         /* Add a new entry */
1104         memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1105         efi_conf_table[i].table = table;
1106         systab.nr_tables = i + 1;
1107
1108         return EFI_SUCCESS;
1109 }
1110
1111 /*
1112  * Adds, updates, or removes a configuration table.
1113  *
1114  * This function implements the InstallConfigurationTable service.
1115  * See the Unified Extensible Firmware Interface (UEFI) specification
1116  * for details.
1117  *
1118  * @guid                GUID of the installed table
1119  * @table               table to be installed
1120  * @return              status code
1121  */
1122 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1123                                                                void *table)
1124 {
1125         EFI_ENTRY("%pUl, %p", guid, table);
1126         return EFI_EXIT(efi_install_configuration_table(guid, table));
1127 }
1128
1129 /*
1130  * Initialize a loaded_image_info + loaded_image_info object with correct
1131  * protocols, boot-device, etc.
1132  *
1133  * @info                loaded image info to be passed to the entry point of the
1134  *                      image
1135  * @obj                 internal object associated with the loaded image
1136  * @device_path         device path of the loaded image
1137  * @file_path           file path of the loaded image
1138  */
1139 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1140                             struct efi_device_path *device_path,
1141                             struct efi_device_path *file_path)
1142 {
1143         obj->handle = info;
1144
1145         /*
1146          * When asking for the device path interface, return
1147          * bootefi_device_path
1148          */
1149         obj->protocols[0].guid = &efi_guid_device_path;
1150         obj->protocols[0].protocol_interface = device_path;
1151
1152         /*
1153          * When asking for the loaded_image interface, just
1154          * return handle which points to loaded_image_info
1155          */
1156         obj->protocols[1].guid = &efi_guid_loaded_image;
1157         obj->protocols[1].protocol_interface = info;
1158
1159         obj->protocols[2].guid = &efi_guid_console_control;
1160         obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1161
1162         obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1163         obj->protocols[3].protocol_interface =
1164                 (void *)&efi_device_path_to_text;
1165
1166         info->file_path = file_path;
1167         if (device_path)
1168                 info->device_handle = efi_dp_find_obj(device_path, NULL);
1169
1170         list_add_tail(&obj->link, &efi_obj_list);
1171 }
1172
1173 /*
1174  * Load an image using a file path.
1175  *
1176  * @file_path           the path of the image to load
1177  * @buffer              buffer containing the loaded image
1178  * @return              status code
1179  */
1180 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1181                                       void **buffer)
1182 {
1183         struct efi_file_info *info = NULL;
1184         struct efi_file_handle *f;
1185         static efi_status_t ret;
1186         uint64_t bs;
1187
1188         f = efi_file_from_path(file_path);
1189         if (!f)
1190                 return EFI_DEVICE_ERROR;
1191
1192         bs = 0;
1193         EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1194                                   &bs, info));
1195         if (ret == EFI_BUFFER_TOO_SMALL) {
1196                 info = malloc(bs);
1197                 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1198                                           &bs, info));
1199         }
1200         if (ret != EFI_SUCCESS)
1201                 goto error;
1202
1203         ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1204         if (ret)
1205                 goto error;
1206
1207         EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1208
1209 error:
1210         free(info);
1211         EFI_CALL(f->close(f));
1212
1213         if (ret != EFI_SUCCESS) {
1214                 efi_free_pool(*buffer);
1215                 *buffer = NULL;
1216         }
1217
1218         return ret;
1219 }
1220
1221 /*
1222  * Load an EFI image into memory.
1223  *
1224  * This function implements the LoadImage service.
1225  * See the Unified Extensible Firmware Interface (UEFI) specification
1226  * for details.
1227  *
1228  * @boot_policy         true for request originating from the boot manager
1229  * @parent_image        the calles's image handle
1230  * @file_path           the path of the image to load
1231  * @source_buffer       memory location from which the image is installed
1232  * @source_size         size of the memory area from which the image is
1233  *                      installed
1234  * @image_handle        handle for the newly installed image
1235  * @return              status code
1236  */
1237 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1238                                           efi_handle_t parent_image,
1239                                           struct efi_device_path *file_path,
1240                                           void *source_buffer,
1241                                           unsigned long source_size,
1242                                           efi_handle_t *image_handle)
1243 {
1244         struct efi_loaded_image *info;
1245         struct efi_object *obj;
1246
1247         EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1248                   file_path, source_buffer, source_size, image_handle);
1249
1250         info = calloc(1, sizeof(*info));
1251         obj = calloc(1, sizeof(*obj));
1252
1253         if (!source_buffer) {
1254                 struct efi_device_path *dp, *fp;
1255                 efi_status_t ret;
1256
1257                 ret = efi_load_image_from_path(file_path, &source_buffer);
1258                 if (ret != EFI_SUCCESS) {
1259                         free(info);
1260                         free(obj);
1261                         return EFI_EXIT(ret);
1262                 }
1263
1264                 /*
1265                  * split file_path which contains both the device and
1266                  * file parts:
1267                  */
1268                 efi_dp_split_file_path(file_path, &dp, &fp);
1269
1270                 efi_setup_loaded_image(info, obj, dp, fp);
1271         } else {
1272                 /* In this case, file_path is the "device" path, ie.
1273                  * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1274                  */
1275                 efi_setup_loaded_image(info, obj, file_path, NULL);
1276         }
1277
1278         info->reserved = efi_load_pe(source_buffer, info);
1279         if (!info->reserved) {
1280                 free(info);
1281                 free(obj);
1282                 return EFI_EXIT(EFI_UNSUPPORTED);
1283         }
1284
1285         info->system_table = &systab;
1286         info->parent_handle = parent_image;
1287         *image_handle = info;
1288
1289         return EFI_EXIT(EFI_SUCCESS);
1290 }
1291
1292 /*
1293  * Call the entry point of an image.
1294  *
1295  * This function implements the StartImage service.
1296  * See the Unified Extensible Firmware Interface (UEFI) specification
1297  * for details.
1298  *
1299  * @image_handle        handle of the image
1300  * @exit_data_size      size of the buffer
1301  * @exit_data           buffer to receive the exit data of the called image
1302  * @return              status code
1303  */
1304 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1305                                            unsigned long *exit_data_size,
1306                                            s16 **exit_data)
1307 {
1308         ulong (*entry)(void *image_handle, struct efi_system_table *st);
1309         struct efi_loaded_image *info = image_handle;
1310
1311         EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1312         entry = info->reserved;
1313
1314         efi_is_direct_boot = false;
1315
1316         /* call the image! */
1317         if (setjmp(&info->exit_jmp)) {
1318                 /* We returned from the child image */
1319                 return EFI_EXIT(info->exit_status);
1320         }
1321
1322         __efi_nesting_dec();
1323         __efi_exit_check();
1324         entry(image_handle, &systab);
1325         __efi_entry_check();
1326         __efi_nesting_inc();
1327
1328         /* Should usually never get here */
1329         return EFI_EXIT(EFI_SUCCESS);
1330 }
1331
1332 /*
1333  * Leave an EFI application or driver.
1334  *
1335  * This function implements the Exit service.
1336  * See the Unified Extensible Firmware Interface (UEFI) specification
1337  * for details.
1338  *
1339  * @image_handle        handle of the application or driver that is exiting
1340  * @exit_status         status code
1341  * @exit_data_size      size of the buffer in bytes
1342  * @exit_data           buffer with data describing an error
1343  * @return              status code
1344  */
1345 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1346                         efi_status_t exit_status, unsigned long exit_data_size,
1347                         int16_t *exit_data)
1348 {
1349         struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1350
1351         EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1352                   exit_data_size, exit_data);
1353
1354         /* Make sure entry/exit counts for EFI world cross-overs match */
1355         __efi_exit_check();
1356
1357         /*
1358          * But longjmp out with the U-Boot gd, not the application's, as
1359          * the other end is a setjmp call inside EFI context.
1360          */
1361         efi_restore_gd();
1362
1363         loaded_image_info->exit_status = exit_status;
1364         longjmp(&loaded_image_info->exit_jmp, 1);
1365
1366         panic("EFI application exited");
1367 }
1368
1369 /*
1370  * Unload an EFI image.
1371  *
1372  * This function implements the UnloadImage service.
1373  * See the Unified Extensible Firmware Interface (UEFI) specification
1374  * for details.
1375  *
1376  * @image_handle        handle of the image to be unloaded
1377  * @return              status code
1378  */
1379 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1380 {
1381         struct efi_object *efiobj;
1382
1383         EFI_ENTRY("%p", image_handle);
1384         efiobj = efi_search_obj(image_handle);
1385         if (efiobj)
1386                 list_del(&efiobj->link);
1387
1388         return EFI_EXIT(EFI_SUCCESS);
1389 }
1390
1391 /*
1392  * Fix up caches for EFI payloads if necessary.
1393  */
1394 static void efi_exit_caches(void)
1395 {
1396 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1397         /*
1398          * Grub on 32bit ARM needs to have caches disabled before jumping into
1399          * a zImage, but does not know of all cache layers. Give it a hand.
1400          */
1401         if (efi_is_direct_boot)
1402                 cleanup_before_linux();
1403 #endif
1404 }
1405
1406 /*
1407  * Stop boot services.
1408  *
1409  * This function implements the ExitBootServices service.
1410  * See the Unified Extensible Firmware Interface (UEFI) specification
1411  * for details.
1412  *
1413  * @image_handle        handle of the loaded image
1414  * @map_key             key of the memory map
1415  * @return              status code
1416  */
1417 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1418                                                   unsigned long map_key)
1419 {
1420         int i;
1421
1422         EFI_ENTRY("%p, %ld", image_handle, map_key);
1423
1424         /* Notify that ExitBootServices is invoked. */
1425         for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1426                 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1427                         continue;
1428                 efi_signal_event(&efi_events[i]);
1429         }
1430         /* Make sure that notification functions are not called anymore */
1431         efi_tpl = TPL_HIGH_LEVEL;
1432
1433         /* XXX Should persist EFI variables here */
1434
1435         board_quiesce_devices();
1436
1437         /* Fix up caches for EFI payloads if necessary */
1438         efi_exit_caches();
1439
1440         /* This stops all lingering devices */
1441         bootm_disable_interrupts();
1442
1443         /* Give the payload some time to boot */
1444         efi_set_watchdog(0);
1445         WATCHDOG_RESET();
1446
1447         return EFI_EXIT(EFI_SUCCESS);
1448 }
1449
1450 /*
1451  * Get next value of the counter.
1452  *
1453  * This function implements the NextMonotonicCount service.
1454  * See the Unified Extensible Firmware Interface (UEFI) specification
1455  * for details.
1456  *
1457  * @count       returned value of the counter
1458  * @return      status code
1459  */
1460 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1461 {
1462         static uint64_t mono = 0;
1463         EFI_ENTRY("%p", count);
1464         *count = mono++;
1465         return EFI_EXIT(EFI_SUCCESS);
1466 }
1467
1468 /*
1469  * Sleep.
1470  *
1471  * This function implements the Stall sercive.
1472  * See the Unified Extensible Firmware Interface (UEFI) specification
1473  * for details.
1474  *
1475  * @microseconds        period to sleep in microseconds
1476  * @return              status code
1477  */
1478 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1479 {
1480         EFI_ENTRY("%ld", microseconds);
1481         udelay(microseconds);
1482         return EFI_EXIT(EFI_SUCCESS);
1483 }
1484
1485 /*
1486  * Reset the watchdog timer.
1487  *
1488  * This function implements the SetWatchdogTimer service.
1489  * See the Unified Extensible Firmware Interface (UEFI) specification
1490  * for details.
1491  *
1492  * @timeout             seconds before reset by watchdog
1493  * @watchdog_code       code to be logged when resetting
1494  * @data_size           size of buffer in bytes
1495  * @watchdog_data       buffer with data describing the reset reason
1496  * @return              status code
1497  */
1498 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1499                                                   uint64_t watchdog_code,
1500                                                   unsigned long data_size,
1501                                                   uint16_t *watchdog_data)
1502 {
1503         EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1504                   data_size, watchdog_data);
1505         return EFI_EXIT(efi_set_watchdog(timeout));
1506 }
1507
1508 /*
1509  * Connect a controller to a driver.
1510  *
1511  * This function implements the ConnectController service.
1512  * See the Unified Extensible Firmware Interface (UEFI) specification
1513  * for details.
1514  *
1515  * @controller_handle   handle of the controller
1516  * @driver_image_handle handle of the driver
1517  * @remain_device_path  device path of a child controller
1518  * @recursive           true to connect all child controllers
1519  * @return              status code
1520  */
1521 static efi_status_t EFIAPI efi_connect_controller(
1522                         efi_handle_t controller_handle,
1523                         efi_handle_t *driver_image_handle,
1524                         struct efi_device_path *remain_device_path,
1525                         bool recursive)
1526 {
1527         EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1528                   remain_device_path, recursive);
1529         return EFI_EXIT(EFI_NOT_FOUND);
1530 }
1531
1532 /*
1533  * Disconnect a controller from a driver.
1534  *
1535  * This function implements the DisconnectController service.
1536  * See the Unified Extensible Firmware Interface (UEFI) specification
1537  * for details.
1538  *
1539  * @controller_handle   handle of the controller
1540  * @driver_image_handle handle of the driver
1541  * @child_handle        handle of the child to destroy
1542  * @return              status code
1543  */
1544 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1545                                                      void *driver_image_handle,
1546                                                      void *child_handle)
1547 {
1548         EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1549                   child_handle);
1550         return EFI_EXIT(EFI_INVALID_PARAMETER);
1551 }
1552
1553 /*
1554  * Close a protocol.
1555  *
1556  * This function implements the CloseProtocol service.
1557  * See the Unified Extensible Firmware Interface (UEFI) specification
1558  * for details.
1559  *
1560  * @handle              handle on which the protocol shall be closed
1561  * @protocol            GUID of the protocol to close
1562  * @agent_handle        handle of the driver
1563  * @controller_handle   handle of the controller
1564  * @return              status code
1565  */
1566 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1567                                               const efi_guid_t *protocol,
1568                                               void *agent_handle,
1569                                               void *controller_handle)
1570 {
1571         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1572                   controller_handle);
1573         return EFI_EXIT(EFI_NOT_FOUND);
1574 }
1575
1576 /*
1577  * Provide information about then open status of a protocol on a handle
1578  *
1579  * This function implements the OpenProtocolInformation service.
1580  * See the Unified Extensible Firmware Interface (UEFI) specification
1581  * for details.
1582  *
1583  * @handle              handle for which the information shall be retrieved
1584  * @protocol            GUID of the protocol
1585  * @entry_buffer        buffer to receive the open protocol information
1586  * @entry_count         number of entries available in the buffer
1587  * @return              status code
1588  */
1589 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1590                         const efi_guid_t *protocol,
1591                         struct efi_open_protocol_info_entry **entry_buffer,
1592                         unsigned long *entry_count)
1593 {
1594         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1595                   entry_count);
1596         return EFI_EXIT(EFI_NOT_FOUND);
1597 }
1598
1599 /*
1600  * Get protocols installed on a handle.
1601  *
1602  * This function implements the ProtocolsPerHandleService.
1603  * See the Unified Extensible Firmware Interface (UEFI) specification
1604  * for details.
1605  *
1606  * @handle                      handle for which the information is retrieved
1607  * @protocol_buffer             buffer with protocol GUIDs
1608  * @protocol_buffer_count       number of entries in the buffer
1609  * @return                      status code
1610  */
1611 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1612                         efi_guid_t ***protocol_buffer,
1613                         unsigned long *protocol_buffer_count)
1614 {
1615         unsigned long buffer_size;
1616         struct efi_object *efiobj;
1617         unsigned long i, j;
1618         struct list_head *lhandle;
1619         efi_status_t r;
1620
1621         EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1622                   protocol_buffer_count);
1623
1624         if (!handle || !protocol_buffer || !protocol_buffer_count)
1625                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1626
1627         *protocol_buffer = NULL;
1628         *protocol_buffer_count = 0;
1629         list_for_each(lhandle, &efi_obj_list) {
1630                 efiobj = list_entry(lhandle, struct efi_object, link);
1631
1632                 if (efiobj->handle != handle)
1633                         continue;
1634
1635                 /* Count protocols */
1636                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1637                         if (efiobj->protocols[i].guid)
1638                                 ++*protocol_buffer_count;
1639                 }
1640                 /* Copy guids */
1641                 if (*protocol_buffer_count) {
1642                         buffer_size = sizeof(efi_guid_t *) *
1643                                         *protocol_buffer_count;
1644                         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1645                                               buffer_size,
1646                                               (void **)protocol_buffer);
1647                         if (r != EFI_SUCCESS)
1648                                 return EFI_EXIT(r);
1649                         j = 0;
1650                         for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1651                                 if (efiobj->protocols[i].guid) {
1652                                         (*protocol_buffer)[j] = (void *)
1653                                                 efiobj->protocols[i].guid;
1654                                         ++j;
1655                                 }
1656                         }
1657                 }
1658                 break;
1659         }
1660
1661         return EFI_EXIT(EFI_SUCCESS);
1662 }
1663
1664 /*
1665  * Locate handles implementing a protocol.
1666  *
1667  * This function implements the LocateHandleBuffer service.
1668  * See the Unified Extensible Firmware Interface (UEFI) specification
1669  * for details.
1670  *
1671  * @search_type         selection criterion
1672  * @protocol            GUID of the protocol
1673  * @search_key          registration key
1674  * @no_handles          number of returned handles
1675  * @buffer              buffer with the returned handles
1676  * @return              status code
1677  */
1678 static efi_status_t EFIAPI efi_locate_handle_buffer(
1679                         enum efi_locate_search_type search_type,
1680                         const efi_guid_t *protocol, void *search_key,
1681                         unsigned long *no_handles, efi_handle_t **buffer)
1682 {
1683         efi_status_t r;
1684         unsigned long buffer_size = 0;
1685
1686         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1687                   no_handles, buffer);
1688
1689         if (!no_handles || !buffer) {
1690                 r = EFI_INVALID_PARAMETER;
1691                 goto out;
1692         }
1693         *no_handles = 0;
1694         *buffer = NULL;
1695         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1696                               *buffer);
1697         if (r != EFI_BUFFER_TOO_SMALL)
1698                 goto out;
1699         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1700                               (void **)buffer);
1701         if (r != EFI_SUCCESS)
1702                 goto out;
1703         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1704                               *buffer);
1705         if (r == EFI_SUCCESS)
1706                 *no_handles = buffer_size / sizeof(void *);
1707 out:
1708         return EFI_EXIT(r);
1709 }
1710
1711 /*
1712  * Find an interface implementing a protocol.
1713  *
1714  * This function implements the LocateProtocol service.
1715  * See the Unified Extensible Firmware Interface (UEFI) specification
1716  * for details.
1717  *
1718  * @protocol            GUID of the protocol
1719  * @registration        registration key passed to the notification function
1720  * @protocol_interface  interface implementing the protocol
1721  * @return              status code
1722  */
1723 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1724                                                void *registration,
1725                                                void **protocol_interface)
1726 {
1727         struct list_head *lhandle;
1728         int i;
1729
1730         EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1731
1732         if (!protocol || !protocol_interface)
1733                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1734
1735         EFI_PRINT_GUID("protocol", protocol);
1736
1737         list_for_each(lhandle, &efi_obj_list) {
1738                 struct efi_object *efiobj;
1739
1740                 efiobj = list_entry(lhandle, struct efi_object, link);
1741                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1742                         struct efi_handler *handler = &efiobj->protocols[i];
1743
1744                         if (!handler->guid)
1745                                 continue;
1746                         if (!guidcmp(handler->guid, protocol)) {
1747                                 *protocol_interface =
1748                                         handler->protocol_interface;
1749                                 return EFI_EXIT(EFI_SUCCESS);
1750                         }
1751                 }
1752         }
1753         *protocol_interface = NULL;
1754
1755         return EFI_EXIT(EFI_NOT_FOUND);
1756 }
1757
1758 /*
1759  * Install multiple protocol interfaces.
1760  *
1761  * This function implements the MultipleProtocolInterfaces service.
1762  * See the Unified Extensible Firmware Interface (UEFI) specification
1763  * for details.
1764  *
1765  * @handle      handle on which the protocol interfaces shall be installed
1766  * @...         NULL terminated argument list with pairs of protocol GUIDS and
1767  *              interfaces
1768  * @return      status code
1769  */
1770 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1771                         void **handle, ...)
1772 {
1773         EFI_ENTRY("%p", handle);
1774
1775         va_list argptr;
1776         const efi_guid_t *protocol;
1777         void *protocol_interface;
1778         efi_status_t r = EFI_SUCCESS;
1779         int i = 0;
1780
1781         if (!handle)
1782                 return EFI_EXIT(EFI_INVALID_PARAMETER);
1783
1784         va_start(argptr, handle);
1785         for (;;) {
1786                 protocol = va_arg(argptr, efi_guid_t*);
1787                 if (!protocol)
1788                         break;
1789                 protocol_interface = va_arg(argptr, void*);
1790                 r = efi_install_protocol_interface(handle, protocol,
1791                                                    EFI_NATIVE_INTERFACE,
1792                                                    protocol_interface);
1793                 if (r != EFI_SUCCESS)
1794                         break;
1795                 i++;
1796         }
1797         va_end(argptr);
1798         if (r == EFI_SUCCESS)
1799                 return EFI_EXIT(r);
1800
1801         /* If an error occured undo all changes. */
1802         va_start(argptr, handle);
1803         for (; i; --i) {
1804                 protocol = va_arg(argptr, efi_guid_t*);
1805                 protocol_interface = va_arg(argptr, void*);
1806                 efi_uninstall_protocol_interface(handle, protocol,
1807                                                  protocol_interface);
1808         }
1809         va_end(argptr);
1810
1811         return EFI_EXIT(r);
1812 }
1813
1814 /*
1815  * Uninstall multiple protocol interfaces.
1816  *
1817  * This function implements the UninstallMultipleProtocolInterfaces service.
1818  * See the Unified Extensible Firmware Interface (UEFI) specification
1819  * for details.
1820  *
1821  * @handle      handle from which the protocol interfaces shall be removed
1822  * @...         NULL terminated argument list with pairs of protocol GUIDS and
1823  *              interfaces
1824  * @return      status code
1825  */
1826 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1827                         void *handle, ...)
1828 {
1829         EFI_ENTRY("%p", handle);
1830         return EFI_EXIT(EFI_INVALID_PARAMETER);
1831 }
1832
1833 /*
1834  * Calculate cyclic redundancy code.
1835  *
1836  * This function implements the CalculateCrc32 service.
1837  * See the Unified Extensible Firmware Interface (UEFI) specification
1838  * for details.
1839  *
1840  * @data        buffer with data
1841  * @data_size   size of buffer in bytes
1842  * @crc32_p     cyclic redundancy code
1843  * @return      status code
1844  */
1845 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1846                                                unsigned long data_size,
1847                                                uint32_t *crc32_p)
1848 {
1849         EFI_ENTRY("%p, %ld", data, data_size);
1850         *crc32_p = crc32(0, data, data_size);
1851         return EFI_EXIT(EFI_SUCCESS);
1852 }
1853
1854 /*
1855  * Copy memory.
1856  *
1857  * This function implements the CopyMem service.
1858  * See the Unified Extensible Firmware Interface (UEFI) specification
1859  * for details.
1860  *
1861  * @destination         destination of the copy operation
1862  * @source              source of the copy operation
1863  * @length              number of bytes to copy
1864  */
1865 static void EFIAPI efi_copy_mem(void *destination, const void *source,
1866                                 size_t length)
1867 {
1868         EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
1869         memcpy(destination, source, length);
1870         EFI_EXIT(EFI_SUCCESS);
1871 }
1872
1873 /*
1874  * Fill memory with a byte value.
1875  *
1876  * This function implements the SetMem service.
1877  * See the Unified Extensible Firmware Interface (UEFI) specification
1878  * for details.
1879  *
1880  * @buffer              buffer to fill
1881  * @size                size of buffer in bytes
1882  * @value               byte to copy to the buffer
1883  */
1884 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
1885 {
1886         EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
1887         memset(buffer, value, size);
1888         EFI_EXIT(EFI_SUCCESS);
1889 }
1890
1891 /*
1892  * Open protocol interface on a handle.
1893  *
1894  * This function implements the OpenProtocol interface.
1895  * See the Unified Extensible Firmware Interface (UEFI) specification
1896  * for details.
1897  *
1898  * @handle              handle on which the protocol shall be opened
1899  * @protocol            GUID of the protocol
1900  * @protocol_interface  interface implementing the protocol
1901  * @agent_handle        handle of the driver
1902  * @controller_handle   handle of the controller
1903  * @attributes          attributes indicating how to open the protocol
1904  * @return              status code
1905  */
1906 static efi_status_t EFIAPI efi_open_protocol(
1907                         void *handle, const efi_guid_t *protocol,
1908                         void **protocol_interface, void *agent_handle,
1909                         void *controller_handle, uint32_t attributes)
1910 {
1911         struct list_head *lhandle;
1912         int i;
1913         efi_status_t r = EFI_INVALID_PARAMETER;
1914
1915         EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
1916                   protocol_interface, agent_handle, controller_handle,
1917                   attributes);
1918
1919         if (!handle || !protocol ||
1920             (!protocol_interface && attributes !=
1921              EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1922                 goto out;
1923         }
1924
1925         EFI_PRINT_GUID("protocol", protocol);
1926
1927         switch (attributes) {
1928         case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1929         case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1930         case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1931                 break;
1932         case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1933                 if (controller_handle == handle)
1934                         goto out;
1935         case EFI_OPEN_PROTOCOL_BY_DRIVER:
1936         case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1937                 if (controller_handle == NULL)
1938                         goto out;
1939         case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1940                 if (agent_handle == NULL)
1941                         goto out;
1942                 break;
1943         default:
1944                 goto out;
1945         }
1946
1947         list_for_each(lhandle, &efi_obj_list) {
1948                 struct efi_object *efiobj;
1949                 efiobj = list_entry(lhandle, struct efi_object, link);
1950
1951                 if (efiobj->handle != handle)
1952                         continue;
1953
1954                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1955                         struct efi_handler *handler = &efiobj->protocols[i];
1956                         const efi_guid_t *hprotocol = handler->guid;
1957                         if (!hprotocol)
1958                                 continue;
1959                         if (!guidcmp(hprotocol, protocol)) {
1960                                 if (attributes !=
1961                                     EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1962                                         *protocol_interface =
1963                                                 handler->protocol_interface;
1964                                 }
1965                                 r = EFI_SUCCESS;
1966                                 goto out;
1967                         }
1968                 }
1969                 goto unsupported;
1970         }
1971
1972 unsupported:
1973         r = EFI_UNSUPPORTED;
1974 out:
1975         return EFI_EXIT(r);
1976 }
1977
1978 /*
1979  * Get interface of a protocol on a handle.
1980  *
1981  * This function implements the HandleProtocol service.
1982  * See the Unified Extensible Firmware Interface (UEFI) specification
1983  * for details.
1984  *
1985  * @handle              handle on which the protocol shall be opened
1986  * @protocol            GUID of the protocol
1987  * @protocol_interface  interface implementing the protocol
1988  * @return              status code
1989  */
1990 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1991                                                const efi_guid_t *protocol,
1992                                                void **protocol_interface)
1993 {
1994         return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1995                                  NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1996 }
1997
1998 static const struct efi_boot_services efi_boot_services = {
1999         .hdr = {
2000                 .headersize = sizeof(struct efi_table_hdr),
2001         },
2002         .raise_tpl = efi_raise_tpl,
2003         .restore_tpl = efi_restore_tpl,
2004         .allocate_pages = efi_allocate_pages_ext,
2005         .free_pages = efi_free_pages_ext,
2006         .get_memory_map = efi_get_memory_map_ext,
2007         .allocate_pool = efi_allocate_pool_ext,
2008         .free_pool = efi_free_pool_ext,
2009         .create_event = efi_create_event_ext,
2010         .set_timer = efi_set_timer_ext,
2011         .wait_for_event = efi_wait_for_event,
2012         .signal_event = efi_signal_event_ext,
2013         .close_event = efi_close_event,
2014         .check_event = efi_check_event,
2015         .install_protocol_interface = efi_install_protocol_interface_ext,
2016         .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2017         .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
2018         .handle_protocol = efi_handle_protocol,
2019         .reserved = NULL,
2020         .register_protocol_notify = efi_register_protocol_notify,
2021         .locate_handle = efi_locate_handle_ext,
2022         .locate_device_path = efi_locate_device_path,
2023         .install_configuration_table = efi_install_configuration_table_ext,
2024         .load_image = efi_load_image,
2025         .start_image = efi_start_image,
2026         .exit = efi_exit,
2027         .unload_image = efi_unload_image,
2028         .exit_boot_services = efi_exit_boot_services,
2029         .get_next_monotonic_count = efi_get_next_monotonic_count,
2030         .stall = efi_stall,
2031         .set_watchdog_timer = efi_set_watchdog_timer,
2032         .connect_controller = efi_connect_controller,
2033         .disconnect_controller = efi_disconnect_controller,
2034         .open_protocol = efi_open_protocol,
2035         .close_protocol = efi_close_protocol,
2036         .open_protocol_information = efi_open_protocol_information,
2037         .protocols_per_handle = efi_protocols_per_handle,
2038         .locate_handle_buffer = efi_locate_handle_buffer,
2039         .locate_protocol = efi_locate_protocol,
2040         .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2041         .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2042         .calculate_crc32 = efi_calculate_crc32,
2043         .copy_mem = efi_copy_mem,
2044         .set_mem = efi_set_mem,
2045 };
2046
2047
2048 static uint16_t __efi_runtime_data firmware_vendor[] =
2049         { 'D','a','s',' ','U','-','b','o','o','t',0 };
2050
2051 struct efi_system_table __efi_runtime_data systab = {
2052         .hdr = {
2053                 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2054                 .revision = 0x20005, /* 2.5 */
2055                 .headersize = sizeof(struct efi_table_hdr),
2056         },
2057         .fw_vendor = (long)firmware_vendor,
2058         .con_in = (void*)&efi_con_in,
2059         .con_out = (void*)&efi_con_out,
2060         .std_err = (void*)&efi_con_out,
2061         .runtime = (void*)&efi_runtime_services,
2062         .boottime = (void*)&efi_boot_services,
2063         .nr_tables = 0,
2064         .tables = (void*)efi_conf_table,
2065 };