efi_loader: superfluous check in efi_remove_protocol()
[oweals/u-boot.git] / lib / efi_loader / efi_boottime.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application boot time services
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7
8 #include <common.h>
9 #include <div64.h>
10 #include <efi_loader.h>
11 #include <environment.h>
12 #include <malloc.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
15 #include <bootm.h>
16 #include <pe.h>
17 #include <watchdog.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /* Task priority level */
22 static efi_uintn_t efi_tpl = TPL_APPLICATION;
23
24 /* This list contains all the EFI objects our payload has access to */
25 LIST_HEAD(efi_obj_list);
26
27 /* List of all events */
28 LIST_HEAD(efi_events);
29
30 /* List of all events registered by RegisterProtocolNotify() */
31 LIST_HEAD(efi_register_notify_events);
32
33 /* Handle of the currently executing image */
34 static efi_handle_t current_image;
35
36 /*
37  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
38  * we need to do trickery with caches. Since we don't want to break the EFI
39  * aware boot path, only apply hacks when loading exiting directly (breaking
40  * direct Linux EFI booting along the way - oh well).
41  */
42 static bool efi_is_direct_boot = true;
43
44 #ifdef CONFIG_ARM
45 /*
46  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47  * fixed when compiling U-Boot. However, the payload does not know about that
48  * restriction so we need to manually swap its and our view of that register on
49  * EFI callback entry/exit.
50  */
51 static volatile void *efi_gd, *app_gd;
52 #endif
53
54 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
55 static int entry_count = 1;
56 static int nesting_level;
57 /* GUID of the device tree table */
58 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
59 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60 const efi_guid_t efi_guid_driver_binding_protocol =
61                         EFI_DRIVER_BINDING_PROTOCOL_GUID;
62
63 /* event group ExitBootServices() invoked */
64 const efi_guid_t efi_guid_event_group_exit_boot_services =
65                         EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66 /* event group SetVirtualAddressMap() invoked */
67 const efi_guid_t efi_guid_event_group_virtual_address_change =
68                         EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69 /* event group memory map changed */
70 const efi_guid_t efi_guid_event_group_memory_map_change =
71                         EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72 /* event group boot manager about to boot */
73 const efi_guid_t efi_guid_event_group_ready_to_boot =
74                         EFI_EVENT_GROUP_READY_TO_BOOT;
75 /* event group ResetSystem() invoked (before ExitBootServices) */
76 const efi_guid_t efi_guid_event_group_reset_system =
77                         EFI_EVENT_GROUP_RESET_SYSTEM;
78
79 static efi_status_t EFIAPI efi_disconnect_controller(
80                                         efi_handle_t controller_handle,
81                                         efi_handle_t driver_image_handle,
82                                         efi_handle_t child_handle);
83
84 /* Called on every callback entry */
85 int __efi_entry_check(void)
86 {
87         int ret = entry_count++ == 0;
88 #ifdef CONFIG_ARM
89         assert(efi_gd);
90         app_gd = gd;
91         gd = efi_gd;
92 #endif
93         return ret;
94 }
95
96 /* Called on every callback exit */
97 int __efi_exit_check(void)
98 {
99         int ret = --entry_count == 0;
100 #ifdef CONFIG_ARM
101         gd = app_gd;
102 #endif
103         return ret;
104 }
105
106 /* Called from do_bootefi_exec() */
107 void efi_save_gd(void)
108 {
109 #ifdef CONFIG_ARM
110         efi_gd = gd;
111 #endif
112 }
113
114 /*
115  * Special case handler for error/abort that just forces things back to u-boot
116  * world so we can dump out an abort message, without any care about returning
117  * back to UEFI world.
118  */
119 void efi_restore_gd(void)
120 {
121 #ifdef CONFIG_ARM
122         /* Only restore if we're already in EFI context */
123         if (!efi_gd)
124                 return;
125         gd = efi_gd;
126 #endif
127 }
128
129 /**
130  * indent_string() - returns a string for indenting with two spaces per level
131  * @level: indent level
132  *
133  * A maximum of ten indent levels is supported. Higher indent levels will be
134  * truncated.
135  *
136  * Return: A string for indenting with two spaces per level is
137  *         returned.
138  */
139 static const char *indent_string(int level)
140 {
141         const char *indent = "                    ";
142         const int max = strlen(indent);
143
144         level = min(max, level * 2);
145         return &indent[max - level];
146 }
147
148 const char *__efi_nesting(void)
149 {
150         return indent_string(nesting_level);
151 }
152
153 const char *__efi_nesting_inc(void)
154 {
155         return indent_string(nesting_level++);
156 }
157
158 const char *__efi_nesting_dec(void)
159 {
160         return indent_string(--nesting_level);
161 }
162
163 /**
164  * efi_queue_event() - queue an EFI event
165  * @event:     event to signal
166  * @check_tpl: check the TPL level
167  *
168  * This function queues the notification function of the event for future
169  * execution.
170  *
171  * The notification function is called if the task priority level of the event
172  * is higher than the current task priority level.
173  *
174  * For the SignalEvent service see efi_signal_event_ext.
175  *
176  */
177 static void efi_queue_event(struct efi_event *event, bool check_tpl)
178 {
179         if (event->notify_function) {
180                 event->is_queued = true;
181                 /* Check TPL */
182                 if (check_tpl && efi_tpl >= event->notify_tpl)
183                         return;
184                 EFI_CALL_VOID(event->notify_function(event,
185                                                      event->notify_context));
186         }
187         event->is_queued = false;
188 }
189
190 /**
191  * is_valid_tpl() - check if the task priority level is valid
192  *
193  * @tpl:                TPL level to check
194  * Return:              status code
195  */
196 efi_status_t is_valid_tpl(efi_uintn_t tpl)
197 {
198         switch (tpl) {
199         case TPL_APPLICATION:
200         case TPL_CALLBACK:
201         case TPL_NOTIFY:
202         case TPL_HIGH_LEVEL:
203                 return EFI_SUCCESS;
204         default:
205                 return EFI_INVALID_PARAMETER;
206         }
207 }
208
209 /**
210  * efi_signal_event() - signal an EFI event
211  * @event:     event to signal
212  * @check_tpl: check the TPL level
213  *
214  * This function signals an event. If the event belongs to an event group all
215  * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
216  * their notification function is queued.
217  *
218  * For the SignalEvent service see efi_signal_event_ext.
219  */
220 void efi_signal_event(struct efi_event *event, bool check_tpl)
221 {
222         if (event->group) {
223                 struct efi_event *evt;
224
225                 /*
226                  * The signaled state has to set before executing any
227                  * notification function
228                  */
229                 list_for_each_entry(evt, &efi_events, link) {
230                         if (!evt->group || guidcmp(evt->group, event->group))
231                                 continue;
232                         if (evt->is_signaled)
233                                 continue;
234                         evt->is_signaled = true;
235                         if (evt->type & EVT_NOTIFY_SIGNAL &&
236                             evt->notify_function)
237                                 evt->is_queued = true;
238                 }
239                 list_for_each_entry(evt, &efi_events, link) {
240                         if (!evt->group || guidcmp(evt->group, event->group))
241                                 continue;
242                         if (evt->is_queued)
243                                 efi_queue_event(evt, check_tpl);
244                 }
245         } else {
246                 event->is_signaled = true;
247                 if (event->type & EVT_NOTIFY_SIGNAL)
248                         efi_queue_event(event, check_tpl);
249         }
250 }
251
252 /**
253  * efi_raise_tpl() - raise the task priority level
254  * @new_tpl: new value of the task priority level
255  *
256  * This function implements the RaiseTpl service.
257  *
258  * See the Unified Extensible Firmware Interface (UEFI) specification for
259  * details.
260  *
261  * Return: old value of the task priority level
262  */
263 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
264 {
265         efi_uintn_t old_tpl = efi_tpl;
266
267         EFI_ENTRY("0x%zx", new_tpl);
268
269         if (new_tpl < efi_tpl)
270                 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
271         efi_tpl = new_tpl;
272         if (efi_tpl > TPL_HIGH_LEVEL)
273                 efi_tpl = TPL_HIGH_LEVEL;
274
275         EFI_EXIT(EFI_SUCCESS);
276         return old_tpl;
277 }
278
279 /**
280  * efi_restore_tpl() - lower the task priority level
281  * @old_tpl: value of the task priority level to be restored
282  *
283  * This function implements the RestoreTpl service.
284  *
285  * See the Unified Extensible Firmware Interface (UEFI) specification for
286  * details.
287  */
288 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
289 {
290         EFI_ENTRY("0x%zx", old_tpl);
291
292         if (old_tpl > efi_tpl)
293                 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
294         efi_tpl = old_tpl;
295         if (efi_tpl > TPL_HIGH_LEVEL)
296                 efi_tpl = TPL_HIGH_LEVEL;
297
298         /*
299          * Lowering the TPL may have made queued events eligible for execution.
300          */
301         efi_timer_check();
302
303         EFI_EXIT(EFI_SUCCESS);
304 }
305
306 /**
307  * efi_allocate_pages_ext() - allocate memory pages
308  * @type:        type of allocation to be performed
309  * @memory_type: usage type of the allocated memory
310  * @pages:       number of pages to be allocated
311  * @memory:      allocated memory
312  *
313  * This function implements the AllocatePages service.
314  *
315  * See the Unified Extensible Firmware Interface (UEFI) specification for
316  * details.
317  *
318  * Return: status code
319  */
320 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
321                                                   efi_uintn_t pages,
322                                                   uint64_t *memory)
323 {
324         efi_status_t r;
325
326         EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
327         r = efi_allocate_pages(type, memory_type, pages, memory);
328         return EFI_EXIT(r);
329 }
330
331 /**
332  * efi_free_pages_ext() - Free memory pages.
333  * @memory: start of the memory area to be freed
334  * @pages:  number of pages to be freed
335  *
336  * This function implements the FreePages service.
337  *
338  * See the Unified Extensible Firmware Interface (UEFI) specification for
339  * details.
340  *
341  * Return: status code
342  */
343 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
344                                               efi_uintn_t pages)
345 {
346         efi_status_t r;
347
348         EFI_ENTRY("%llx, 0x%zx", memory, pages);
349         r = efi_free_pages(memory, pages);
350         return EFI_EXIT(r);
351 }
352
353 /**
354  * efi_get_memory_map_ext() - get map describing memory usage
355  * @memory_map_size:    on entry the size, in bytes, of the memory map buffer,
356  *                      on exit the size of the copied memory map
357  * @memory_map:         buffer to which the memory map is written
358  * @map_key:            key for the memory map
359  * @descriptor_size:    size of an individual memory descriptor
360  * @descriptor_version: version number of the memory descriptor structure
361  *
362  * This function implements the GetMemoryMap service.
363  *
364  * See the Unified Extensible Firmware Interface (UEFI) specification for
365  * details.
366  *
367  * Return: status code
368  */
369 static efi_status_t EFIAPI efi_get_memory_map_ext(
370                                         efi_uintn_t *memory_map_size,
371                                         struct efi_mem_desc *memory_map,
372                                         efi_uintn_t *map_key,
373                                         efi_uintn_t *descriptor_size,
374                                         uint32_t *descriptor_version)
375 {
376         efi_status_t r;
377
378         EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
379                   map_key, descriptor_size, descriptor_version);
380         r = efi_get_memory_map(memory_map_size, memory_map, map_key,
381                                descriptor_size, descriptor_version);
382         return EFI_EXIT(r);
383 }
384
385 /**
386  * efi_allocate_pool_ext() - allocate memory from pool
387  * @pool_type: type of the pool from which memory is to be allocated
388  * @size:      number of bytes to be allocated
389  * @buffer:    allocated memory
390  *
391  * This function implements the AllocatePool service.
392  *
393  * See the Unified Extensible Firmware Interface (UEFI) specification for
394  * details.
395  *
396  * Return: status code
397  */
398 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
399                                                  efi_uintn_t size,
400                                                  void **buffer)
401 {
402         efi_status_t r;
403
404         EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
405         r = efi_allocate_pool(pool_type, size, buffer);
406         return EFI_EXIT(r);
407 }
408
409 /**
410  * efi_free_pool_ext() - free memory from pool
411  * @buffer: start of memory to be freed
412  *
413  * This function implements the FreePool service.
414  *
415  * See the Unified Extensible Firmware Interface (UEFI) specification for
416  * details.
417  *
418  * Return: status code
419  */
420 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
421 {
422         efi_status_t r;
423
424         EFI_ENTRY("%p", buffer);
425         r = efi_free_pool(buffer);
426         return EFI_EXIT(r);
427 }
428
429 /**
430  * efi_add_handle() - add a new handle to the object list
431  *
432  * @handle:     handle to be added
433  *
434  * The protocols list is initialized. The handle is added to the list of known
435  * UEFI objects.
436  */
437 void efi_add_handle(efi_handle_t handle)
438 {
439         if (!handle)
440                 return;
441         INIT_LIST_HEAD(&handle->protocols);
442         list_add_tail(&handle->link, &efi_obj_list);
443 }
444
445 /**
446  * efi_create_handle() - create handle
447  * @handle: new handle
448  *
449  * Return: status code
450  */
451 efi_status_t efi_create_handle(efi_handle_t *handle)
452 {
453         struct efi_object *obj;
454
455         obj = calloc(1, sizeof(struct efi_object));
456         if (!obj)
457                 return EFI_OUT_OF_RESOURCES;
458
459         efi_add_handle(obj);
460         *handle = obj;
461
462         return EFI_SUCCESS;
463 }
464
465 /**
466  * efi_search_protocol() - find a protocol on a handle.
467  * @handle:        handle
468  * @protocol_guid: GUID of the protocol
469  * @handler:       reference to the protocol
470  *
471  * Return: status code
472  */
473 efi_status_t efi_search_protocol(const efi_handle_t handle,
474                                  const efi_guid_t *protocol_guid,
475                                  struct efi_handler **handler)
476 {
477         struct efi_object *efiobj;
478         struct list_head *lhandle;
479
480         if (!handle || !protocol_guid)
481                 return EFI_INVALID_PARAMETER;
482         efiobj = efi_search_obj(handle);
483         if (!efiobj)
484                 return EFI_INVALID_PARAMETER;
485         list_for_each(lhandle, &efiobj->protocols) {
486                 struct efi_handler *protocol;
487
488                 protocol = list_entry(lhandle, struct efi_handler, link);
489                 if (!guidcmp(protocol->guid, protocol_guid)) {
490                         if (handler)
491                                 *handler = protocol;
492                         return EFI_SUCCESS;
493                 }
494         }
495         return EFI_NOT_FOUND;
496 }
497
498 /**
499  * efi_remove_protocol() - delete protocol from a handle
500  * @handle:             handle from which the protocol shall be deleted
501  * @protocol:           GUID of the protocol to be deleted
502  * @protocol_interface: interface of the protocol implementation
503  *
504  * Return: status code
505  */
506 efi_status_t efi_remove_protocol(const efi_handle_t handle,
507                                  const efi_guid_t *protocol,
508                                  void *protocol_interface)
509 {
510         struct efi_handler *handler;
511         efi_status_t ret;
512
513         ret = efi_search_protocol(handle, protocol, &handler);
514         if (ret != EFI_SUCCESS)
515                 return ret;
516         if (handler->protocol_interface != protocol_interface)
517                 return EFI_INVALID_PARAMETER;
518         list_del(&handler->link);
519         free(handler);
520         return EFI_SUCCESS;
521 }
522
523 /**
524  * efi_remove_all_protocols() - delete all protocols from a handle
525  * @handle: handle from which the protocols shall be deleted
526  *
527  * Return: status code
528  */
529 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
530 {
531         struct efi_object *efiobj;
532         struct efi_handler *protocol;
533         struct efi_handler *pos;
534
535         efiobj = efi_search_obj(handle);
536         if (!efiobj)
537                 return EFI_INVALID_PARAMETER;
538         list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
539                 efi_status_t ret;
540
541                 ret = efi_remove_protocol(handle, protocol->guid,
542                                           protocol->protocol_interface);
543                 if (ret != EFI_SUCCESS)
544                         return ret;
545         }
546         return EFI_SUCCESS;
547 }
548
549 /**
550  * efi_delete_handle() - delete handle
551  *
552  * @obj: handle to delete
553  */
554 void efi_delete_handle(efi_handle_t handle)
555 {
556         if (!handle)
557                 return;
558         efi_remove_all_protocols(handle);
559         list_del(&handle->link);
560         free(handle);
561 }
562
563 /**
564  * efi_is_event() - check if a pointer is a valid event
565  * @event: pointer to check
566  *
567  * Return: status code
568  */
569 static efi_status_t efi_is_event(const struct efi_event *event)
570 {
571         const struct efi_event *evt;
572
573         if (!event)
574                 return EFI_INVALID_PARAMETER;
575         list_for_each_entry(evt, &efi_events, link) {
576                 if (evt == event)
577                         return EFI_SUCCESS;
578         }
579         return EFI_INVALID_PARAMETER;
580 }
581
582 /**
583  * efi_create_event() - create an event
584  * @type:            type of the event to create
585  * @notify_tpl:      task priority level of the event
586  * @notify_function: notification function of the event
587  * @notify_context:  pointer passed to the notification function
588  * @group:           event group
589  * @event:           created event
590  *
591  * This function is used inside U-Boot code to create an event.
592  *
593  * For the API function implementing the CreateEvent service see
594  * efi_create_event_ext.
595  *
596  * Return: status code
597  */
598 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
599                               void (EFIAPI *notify_function) (
600                                         struct efi_event *event,
601                                         void *context),
602                               void *notify_context, efi_guid_t *group,
603                               struct efi_event **event)
604 {
605         struct efi_event *evt;
606
607         if (event == NULL)
608                 return EFI_INVALID_PARAMETER;
609
610         switch (type) {
611         case 0:
612         case EVT_TIMER:
613         case EVT_NOTIFY_SIGNAL:
614         case EVT_TIMER | EVT_NOTIFY_SIGNAL:
615         case EVT_NOTIFY_WAIT:
616         case EVT_TIMER | EVT_NOTIFY_WAIT:
617         case EVT_SIGNAL_EXIT_BOOT_SERVICES:
618         case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
619                 break;
620         default:
621                 return EFI_INVALID_PARAMETER;
622         }
623
624         if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
625             (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
626                 return EFI_INVALID_PARAMETER;
627
628         evt = calloc(1, sizeof(struct efi_event));
629         if (!evt)
630                 return EFI_OUT_OF_RESOURCES;
631         evt->type = type;
632         evt->notify_tpl = notify_tpl;
633         evt->notify_function = notify_function;
634         evt->notify_context = notify_context;
635         evt->group = group;
636         /* Disable timers on boot up */
637         evt->trigger_next = -1ULL;
638         evt->is_queued = false;
639         evt->is_signaled = false;
640         list_add_tail(&evt->link, &efi_events);
641         *event = evt;
642         return EFI_SUCCESS;
643 }
644
645 /*
646  * efi_create_event_ex() - create an event in a group
647  * @type:            type of the event to create
648  * @notify_tpl:      task priority level of the event
649  * @notify_function: notification function of the event
650  * @notify_context:  pointer passed to the notification function
651  * @event:           created event
652  * @event_group:     event group
653  *
654  * This function implements the CreateEventEx service.
655  *
656  * See the Unified Extensible Firmware Interface (UEFI) specification for
657  * details.
658  *
659  * Return: status code
660  */
661 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
662                                         void (EFIAPI *notify_function) (
663                                                         struct efi_event *event,
664                                                         void *context),
665                                         void *notify_context,
666                                         efi_guid_t *event_group,
667                                         struct efi_event **event)
668 {
669         efi_status_t ret;
670
671         EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
672                   notify_context, event_group);
673
674         /*
675          * The allowable input parameters are the same as in CreateEvent()
676          * except for the following two disallowed event types.
677          */
678         switch (type) {
679         case EVT_SIGNAL_EXIT_BOOT_SERVICES:
680         case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
681                 ret = EFI_INVALID_PARAMETER;
682                 goto out;
683         }
684
685         ret = efi_create_event(type, notify_tpl, notify_function,
686                                notify_context, event_group, event);
687 out:
688         return EFI_EXIT(ret);
689 }
690
691 /**
692  * efi_create_event_ext() - create an event
693  * @type:            type of the event to create
694  * @notify_tpl:      task priority level of the event
695  * @notify_function: notification function of the event
696  * @notify_context:  pointer passed to the notification function
697  * @event:           created event
698  *
699  * This function implements the CreateEvent service.
700  *
701  * See the Unified Extensible Firmware Interface (UEFI) specification for
702  * details.
703  *
704  * Return: status code
705  */
706 static efi_status_t EFIAPI efi_create_event_ext(
707                         uint32_t type, efi_uintn_t notify_tpl,
708                         void (EFIAPI *notify_function) (
709                                         struct efi_event *event,
710                                         void *context),
711                         void *notify_context, struct efi_event **event)
712 {
713         EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
714                   notify_context);
715         return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
716                                          notify_context, NULL, event));
717 }
718
719 /**
720  * efi_timer_check() - check if a timer event has occurred
721  *
722  * Check if a timer event has occurred or a queued notification function should
723  * be called.
724  *
725  * Our timers have to work without interrupts, so we check whenever keyboard
726  * input or disk accesses happen if enough time elapsed for them to fire.
727  */
728 void efi_timer_check(void)
729 {
730         struct efi_event *evt;
731         u64 now = timer_get_us();
732
733         list_for_each_entry(evt, &efi_events, link) {
734                 if (evt->is_queued)
735                         efi_queue_event(evt, true);
736                 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
737                         continue;
738                 switch (evt->trigger_type) {
739                 case EFI_TIMER_RELATIVE:
740                         evt->trigger_type = EFI_TIMER_STOP;
741                         break;
742                 case EFI_TIMER_PERIODIC:
743                         evt->trigger_next += evt->trigger_time;
744                         break;
745                 default:
746                         continue;
747                 }
748                 evt->is_signaled = false;
749                 efi_signal_event(evt, true);
750         }
751         WATCHDOG_RESET();
752 }
753
754 /**
755  * efi_set_timer() - set the trigger time for a timer event or stop the event
756  * @event:        event for which the timer is set
757  * @type:         type of the timer
758  * @trigger_time: trigger period in multiples of 100 ns
759  *
760  * This is the function for internal usage in U-Boot. For the API function
761  * implementing the SetTimer service see efi_set_timer_ext.
762  *
763  * Return: status code
764  */
765 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
766                            uint64_t trigger_time)
767 {
768         /* Check that the event is valid */
769         if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
770                 return EFI_INVALID_PARAMETER;
771
772         /*
773          * The parameter defines a multiple of 100 ns.
774          * We use multiples of 1000 ns. So divide by 10.
775          */
776         do_div(trigger_time, 10);
777
778         switch (type) {
779         case EFI_TIMER_STOP:
780                 event->trigger_next = -1ULL;
781                 break;
782         case EFI_TIMER_PERIODIC:
783         case EFI_TIMER_RELATIVE:
784                 event->trigger_next = timer_get_us() + trigger_time;
785                 break;
786         default:
787                 return EFI_INVALID_PARAMETER;
788         }
789         event->trigger_type = type;
790         event->trigger_time = trigger_time;
791         event->is_signaled = false;
792         return EFI_SUCCESS;
793 }
794
795 /**
796  * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
797  *                       event
798  * @event:        event for which the timer is set
799  * @type:         type of the timer
800  * @trigger_time: trigger period in multiples of 100 ns
801  *
802  * This function implements the SetTimer service.
803  *
804  * See the Unified Extensible Firmware Interface (UEFI) specification for
805  * details.
806  *
807  *
808  * Return: status code
809  */
810 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
811                                              enum efi_timer_delay type,
812                                              uint64_t trigger_time)
813 {
814         EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
815         return EFI_EXIT(efi_set_timer(event, type, trigger_time));
816 }
817
818 /**
819  * efi_wait_for_event() - wait for events to be signaled
820  * @num_events: number of events to be waited for
821  * @event:      events to be waited for
822  * @index:      index of the event that was signaled
823  *
824  * This function implements the WaitForEvent service.
825  *
826  * See the Unified Extensible Firmware Interface (UEFI) specification for
827  * details.
828  *
829  * Return: status code
830  */
831 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
832                                               struct efi_event **event,
833                                               efi_uintn_t *index)
834 {
835         int i;
836
837         EFI_ENTRY("%zd, %p, %p", num_events, event, index);
838
839         /* Check parameters */
840         if (!num_events || !event)
841                 return EFI_EXIT(EFI_INVALID_PARAMETER);
842         /* Check TPL */
843         if (efi_tpl != TPL_APPLICATION)
844                 return EFI_EXIT(EFI_UNSUPPORTED);
845         for (i = 0; i < num_events; ++i) {
846                 if (efi_is_event(event[i]) != EFI_SUCCESS)
847                         return EFI_EXIT(EFI_INVALID_PARAMETER);
848                 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
849                         return EFI_EXIT(EFI_INVALID_PARAMETER);
850                 if (!event[i]->is_signaled)
851                         efi_queue_event(event[i], true);
852         }
853
854         /* Wait for signal */
855         for (;;) {
856                 for (i = 0; i < num_events; ++i) {
857                         if (event[i]->is_signaled)
858                                 goto out;
859                 }
860                 /* Allow events to occur. */
861                 efi_timer_check();
862         }
863
864 out:
865         /*
866          * Reset the signal which is passed to the caller to allow periodic
867          * events to occur.
868          */
869         event[i]->is_signaled = false;
870         if (index)
871                 *index = i;
872
873         return EFI_EXIT(EFI_SUCCESS);
874 }
875
876 /**
877  * efi_signal_event_ext() - signal an EFI event
878  * @event: event to signal
879  *
880  * This function implements the SignalEvent service.
881  *
882  * See the Unified Extensible Firmware Interface (UEFI) specification for
883  * details.
884  *
885  * This functions sets the signaled state of the event and queues the
886  * notification function for execution.
887  *
888  * Return: status code
889  */
890 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
891 {
892         EFI_ENTRY("%p", event);
893         if (efi_is_event(event) != EFI_SUCCESS)
894                 return EFI_EXIT(EFI_INVALID_PARAMETER);
895         efi_signal_event(event, true);
896         return EFI_EXIT(EFI_SUCCESS);
897 }
898
899 /**
900  * efi_close_event() - close an EFI event
901  * @event: event to close
902  *
903  * This function implements the CloseEvent service.
904  *
905  * See the Unified Extensible Firmware Interface (UEFI) specification for
906  * details.
907  *
908  * Return: status code
909  */
910 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
911 {
912         struct efi_register_notify_event *item, *next;
913
914         EFI_ENTRY("%p", event);
915         if (efi_is_event(event) != EFI_SUCCESS)
916                 return EFI_EXIT(EFI_INVALID_PARAMETER);
917
918         /* Remove protocol notify registrations for the event */
919         list_for_each_entry_safe(item, next, &efi_register_notify_events,
920                                  link) {
921                 if (event == item->event) {
922                         list_del(&item->link);
923                         free(item);
924                 }
925         }
926
927         list_del(&event->link);
928         free(event);
929         return EFI_EXIT(EFI_SUCCESS);
930 }
931
932 /**
933  * efi_check_event() - check if an event is signaled
934  * @event: event to check
935  *
936  * This function implements the CheckEvent service.
937  *
938  * See the Unified Extensible Firmware Interface (UEFI) specification for
939  * details.
940  *
941  * If an event is not signaled yet, the notification function is queued. The
942  * signaled state is cleared.
943  *
944  * Return: status code
945  */
946 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
947 {
948         EFI_ENTRY("%p", event);
949         efi_timer_check();
950         if (efi_is_event(event) != EFI_SUCCESS ||
951             event->type & EVT_NOTIFY_SIGNAL)
952                 return EFI_EXIT(EFI_INVALID_PARAMETER);
953         if (!event->is_signaled)
954                 efi_queue_event(event, true);
955         if (event->is_signaled) {
956                 event->is_signaled = false;
957                 return EFI_EXIT(EFI_SUCCESS);
958         }
959         return EFI_EXIT(EFI_NOT_READY);
960 }
961
962 /**
963  * efi_search_obj() - find the internal EFI object for a handle
964  * @handle: handle to find
965  *
966  * Return: EFI object
967  */
968 struct efi_object *efi_search_obj(const efi_handle_t handle)
969 {
970         struct efi_object *efiobj;
971
972         if (!handle)
973                 return NULL;
974
975         list_for_each_entry(efiobj, &efi_obj_list, link) {
976                 if (efiobj == handle)
977                         return efiobj;
978         }
979         return NULL;
980 }
981
982 /**
983  * efi_open_protocol_info_entry() - create open protocol info entry and add it
984  *                                  to a protocol
985  * @handler: handler of a protocol
986  *
987  * Return: open protocol info entry
988  */
989 static struct efi_open_protocol_info_entry *efi_create_open_info(
990                         struct efi_handler *handler)
991 {
992         struct efi_open_protocol_info_item *item;
993
994         item = calloc(1, sizeof(struct efi_open_protocol_info_item));
995         if (!item)
996                 return NULL;
997         /* Append the item to the open protocol info list. */
998         list_add_tail(&item->link, &handler->open_infos);
999
1000         return &item->info;
1001 }
1002
1003 /**
1004  * efi_delete_open_info() - remove an open protocol info entry from a protocol
1005  * @item: open protocol info entry to delete
1006  *
1007  * Return: status code
1008  */
1009 static efi_status_t efi_delete_open_info(
1010                         struct efi_open_protocol_info_item *item)
1011 {
1012         list_del(&item->link);
1013         free(item);
1014         return EFI_SUCCESS;
1015 }
1016
1017 /**
1018  * efi_add_protocol() - install new protocol on a handle
1019  * @handle:             handle on which the protocol shall be installed
1020  * @protocol:           GUID of the protocol to be installed
1021  * @protocol_interface: interface of the protocol implementation
1022  *
1023  * Return: status code
1024  */
1025 efi_status_t efi_add_protocol(const efi_handle_t handle,
1026                               const efi_guid_t *protocol,
1027                               void *protocol_interface)
1028 {
1029         struct efi_object *efiobj;
1030         struct efi_handler *handler;
1031         efi_status_t ret;
1032         struct efi_register_notify_event *event;
1033
1034         efiobj = efi_search_obj(handle);
1035         if (!efiobj)
1036                 return EFI_INVALID_PARAMETER;
1037         ret = efi_search_protocol(handle, protocol, NULL);
1038         if (ret != EFI_NOT_FOUND)
1039                 return EFI_INVALID_PARAMETER;
1040         handler = calloc(1, sizeof(struct efi_handler));
1041         if (!handler)
1042                 return EFI_OUT_OF_RESOURCES;
1043         handler->guid = protocol;
1044         handler->protocol_interface = protocol_interface;
1045         INIT_LIST_HEAD(&handler->open_infos);
1046         list_add_tail(&handler->link, &efiobj->protocols);
1047
1048         /* Notify registered events */
1049         list_for_each_entry(event, &efi_register_notify_events, link) {
1050                 if (!guidcmp(protocol, &event->protocol))
1051                         efi_signal_event(event->event, true);
1052         }
1053
1054         if (!guidcmp(&efi_guid_device_path, protocol))
1055                 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1056         return EFI_SUCCESS;
1057 }
1058
1059 /**
1060  * efi_install_protocol_interface() - install protocol interface
1061  * @handle:                  handle on which the protocol shall be installed
1062  * @protocol:                GUID of the protocol to be installed
1063  * @protocol_interface_type: type of the interface to be installed,
1064  *                           always EFI_NATIVE_INTERFACE
1065  * @protocol_interface:      interface of the protocol implementation
1066  *
1067  * This function implements the InstallProtocolInterface service.
1068  *
1069  * See the Unified Extensible Firmware Interface (UEFI) specification for
1070  * details.
1071  *
1072  * Return: status code
1073  */
1074 static efi_status_t EFIAPI efi_install_protocol_interface(
1075                         efi_handle_t *handle, const efi_guid_t *protocol,
1076                         int protocol_interface_type, void *protocol_interface)
1077 {
1078         efi_status_t r;
1079
1080         EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1081                   protocol_interface);
1082
1083         if (!handle || !protocol ||
1084             protocol_interface_type != EFI_NATIVE_INTERFACE) {
1085                 r = EFI_INVALID_PARAMETER;
1086                 goto out;
1087         }
1088
1089         /* Create new handle if requested. */
1090         if (!*handle) {
1091                 r = efi_create_handle(handle);
1092                 if (r != EFI_SUCCESS)
1093                         goto out;
1094                 EFI_PRINT("new handle %p\n", *handle);
1095         } else {
1096                 EFI_PRINT("handle %p\n", *handle);
1097         }
1098         /* Add new protocol */
1099         r = efi_add_protocol(*handle, protocol, protocol_interface);
1100 out:
1101         return EFI_EXIT(r);
1102 }
1103
1104 /**
1105  * efi_get_drivers() - get all drivers associated to a controller
1106  * @handle:               handle of the controller
1107  * @protocol:             protocol GUID (optional)
1108  * @number_of_drivers:    number of child controllers
1109  * @driver_handle_buffer: handles of the the drivers
1110  *
1111  * The allocated buffer has to be freed with free().
1112  *
1113  * Return: status code
1114  */
1115 static efi_status_t efi_get_drivers(efi_handle_t handle,
1116                                     const efi_guid_t *protocol,
1117                                     efi_uintn_t *number_of_drivers,
1118                                     efi_handle_t **driver_handle_buffer)
1119 {
1120         struct efi_handler *handler;
1121         struct efi_open_protocol_info_item *item;
1122         efi_uintn_t count = 0, i;
1123         bool duplicate;
1124
1125         /* Count all driver associations */
1126         list_for_each_entry(handler, &handle->protocols, link) {
1127                 if (protocol && guidcmp(handler->guid, protocol))
1128                         continue;
1129                 list_for_each_entry(item, &handler->open_infos, link) {
1130                         if (item->info.attributes &
1131                             EFI_OPEN_PROTOCOL_BY_DRIVER)
1132                                 ++count;
1133                 }
1134         }
1135         /*
1136          * Create buffer. In case of duplicate driver assignments the buffer
1137          * will be too large. But that does not harm.
1138          */
1139         *number_of_drivers = 0;
1140         *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1141         if (!*driver_handle_buffer)
1142                 return EFI_OUT_OF_RESOURCES;
1143         /* Collect unique driver handles */
1144         list_for_each_entry(handler, &handle->protocols, link) {
1145                 if (protocol && guidcmp(handler->guid, protocol))
1146                         continue;
1147                 list_for_each_entry(item, &handler->open_infos, link) {
1148                         if (item->info.attributes &
1149                             EFI_OPEN_PROTOCOL_BY_DRIVER) {
1150                                 /* Check this is a new driver */
1151                                 duplicate = false;
1152                                 for (i = 0; i < *number_of_drivers; ++i) {
1153                                         if ((*driver_handle_buffer)[i] ==
1154                                             item->info.agent_handle)
1155                                                 duplicate = true;
1156                                 }
1157                                 /* Copy handle to buffer */
1158                                 if (!duplicate) {
1159                                         i = (*number_of_drivers)++;
1160                                         (*driver_handle_buffer)[i] =
1161                                                 item->info.agent_handle;
1162                                 }
1163                         }
1164                 }
1165         }
1166         return EFI_SUCCESS;
1167 }
1168
1169 /**
1170  * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1171  * @handle:       handle of the controller
1172  * @protocol:     protocol GUID (optional)
1173  * @child_handle: handle of the child to destroy
1174  *
1175  * This function implements the DisconnectController service.
1176  *
1177  * See the Unified Extensible Firmware Interface (UEFI) specification for
1178  * details.
1179  *
1180  * Return: status code
1181  */
1182 static efi_status_t efi_disconnect_all_drivers
1183                                 (efi_handle_t handle,
1184                                  const efi_guid_t *protocol,
1185                                  efi_handle_t child_handle)
1186 {
1187         efi_uintn_t number_of_drivers;
1188         efi_handle_t *driver_handle_buffer;
1189         efi_status_t r, ret;
1190
1191         ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1192                               &driver_handle_buffer);
1193         if (ret != EFI_SUCCESS)
1194                 return ret;
1195
1196         ret = EFI_NOT_FOUND;
1197         while (number_of_drivers) {
1198                 r = EFI_CALL(efi_disconnect_controller(
1199                                 handle,
1200                                 driver_handle_buffer[--number_of_drivers],
1201                                 child_handle));
1202                 if (r == EFI_SUCCESS)
1203                         ret = r;
1204         }
1205         free(driver_handle_buffer);
1206         return ret;
1207 }
1208
1209 /**
1210  * efi_uninstall_protocol() - uninstall protocol interface
1211  *
1212  * @handle:             handle from which the protocol shall be removed
1213  * @protocol:           GUID of the protocol to be removed
1214  * @protocol_interface: interface to be removed
1215  *
1216  * This function DOES NOT delete a handle without installed protocol.
1217  *
1218  * Return: status code
1219  */
1220 static efi_status_t efi_uninstall_protocol
1221                         (efi_handle_t handle, const efi_guid_t *protocol,
1222                          void *protocol_interface)
1223 {
1224         struct efi_object *efiobj;
1225         struct efi_handler *handler;
1226         struct efi_open_protocol_info_item *item;
1227         struct efi_open_protocol_info_item *pos;
1228         efi_status_t r;
1229
1230         /* Check handle */
1231         efiobj = efi_search_obj(handle);
1232         if (!efiobj) {
1233                 r = EFI_INVALID_PARAMETER;
1234                 goto out;
1235         }
1236         /* Find the protocol on the handle */
1237         r = efi_search_protocol(handle, protocol, &handler);
1238         if (r != EFI_SUCCESS)
1239                 goto out;
1240         /* Disconnect controllers */
1241         efi_disconnect_all_drivers(efiobj, protocol, NULL);
1242         if (!list_empty(&handler->open_infos)) {
1243                 r =  EFI_ACCESS_DENIED;
1244                 goto out;
1245         }
1246         /* Close protocol */
1247         list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1248                 if (item->info.attributes ==
1249                         EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1250                     item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1251                     item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1252                         list_del(&item->link);
1253         }
1254         if (!list_empty(&handler->open_infos)) {
1255                 r =  EFI_ACCESS_DENIED;
1256                 goto out;
1257         }
1258         r = efi_remove_protocol(handle, protocol, protocol_interface);
1259 out:
1260         return r;
1261 }
1262
1263 /**
1264  * efi_uninstall_protocol_interface() - uninstall protocol interface
1265  * @handle:             handle from which the protocol shall be removed
1266  * @protocol:           GUID of the protocol to be removed
1267  * @protocol_interface: interface to be removed
1268  *
1269  * This function implements the UninstallProtocolInterface service.
1270  *
1271  * See the Unified Extensible Firmware Interface (UEFI) specification for
1272  * details.
1273  *
1274  * Return: status code
1275  */
1276 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1277                         (efi_handle_t handle, const efi_guid_t *protocol,
1278                          void *protocol_interface)
1279 {
1280         efi_status_t ret;
1281
1282         EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1283
1284         ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1285         if (ret != EFI_SUCCESS)
1286                 goto out;
1287
1288         /* If the last protocol has been removed, delete the handle. */
1289         if (list_empty(&handle->protocols)) {
1290                 list_del(&handle->link);
1291                 free(handle);
1292         }
1293 out:
1294         return EFI_EXIT(ret);
1295 }
1296
1297 /**
1298  * efi_register_protocol_notify() - register an event for notification when a
1299  *                                  protocol is installed.
1300  * @protocol:     GUID of the protocol whose installation shall be notified
1301  * @event:        event to be signaled upon installation of the protocol
1302  * @registration: key for retrieving the registration information
1303  *
1304  * This function implements the RegisterProtocolNotify service.
1305  * See the Unified Extensible Firmware Interface (UEFI) specification
1306  * for details.
1307  *
1308  * Return: status code
1309  */
1310 static efi_status_t EFIAPI efi_register_protocol_notify(
1311                                                 const efi_guid_t *protocol,
1312                                                 struct efi_event *event,
1313                                                 void **registration)
1314 {
1315         struct efi_register_notify_event *item;
1316         efi_status_t ret = EFI_SUCCESS;
1317
1318         EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1319
1320         if (!protocol || !event || !registration) {
1321                 ret = EFI_INVALID_PARAMETER;
1322                 goto out;
1323         }
1324
1325         item = calloc(1, sizeof(struct efi_register_notify_event));
1326         if (!item) {
1327                 ret = EFI_OUT_OF_RESOURCES;
1328                 goto out;
1329         }
1330
1331         item->event = event;
1332         memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
1333
1334         list_add_tail(&item->link, &efi_register_notify_events);
1335
1336         *registration = item;
1337 out:
1338         return EFI_EXIT(ret);
1339 }
1340
1341 /**
1342  * efi_search() - determine if an EFI handle implements a protocol
1343  * @search_type: selection criterion
1344  * @protocol:    GUID of the protocol
1345  * @search_key:  registration key
1346  * @handle:      handle
1347  *
1348  * See the documentation of the LocateHandle service in the UEFI specification.
1349  *
1350  * Return: 0 if the handle implements the protocol
1351  */
1352 static int efi_search(enum efi_locate_search_type search_type,
1353                       const efi_guid_t *protocol, efi_handle_t handle)
1354 {
1355         efi_status_t ret;
1356
1357         switch (search_type) {
1358         case ALL_HANDLES:
1359                 return 0;
1360         case BY_REGISTER_NOTIFY:
1361         case BY_PROTOCOL:
1362                 ret = efi_search_protocol(handle, protocol, NULL);
1363                 return (ret != EFI_SUCCESS);
1364         default:
1365                 /* Invalid search type */
1366                 return -1;
1367         }
1368 }
1369
1370 /**
1371  * efi_locate_handle() - locate handles implementing a protocol
1372  *
1373  * @search_type:        selection criterion
1374  * @protocol:           GUID of the protocol
1375  * @search_key:         registration key
1376  * @buffer_size:        size of the buffer to receive the handles in bytes
1377  * @buffer:             buffer to receive the relevant handles
1378  *
1379  * This function is meant for U-Boot internal calls. For the API implementation
1380  * of the LocateHandle service see efi_locate_handle_ext.
1381  *
1382  * Return: status code
1383  */
1384 static efi_status_t efi_locate_handle(
1385                         enum efi_locate_search_type search_type,
1386                         const efi_guid_t *protocol, void *search_key,
1387                         efi_uintn_t *buffer_size, efi_handle_t *buffer)
1388 {
1389         struct efi_object *efiobj;
1390         efi_uintn_t size = 0;
1391         struct efi_register_notify_event *item, *event = NULL;
1392
1393         /* Check parameters */
1394         switch (search_type) {
1395         case ALL_HANDLES:
1396                 break;
1397         case BY_REGISTER_NOTIFY:
1398                 if (!search_key)
1399                         return EFI_INVALID_PARAMETER;
1400                 /* Check that the registration key is valid */
1401                 list_for_each_entry(item, &efi_register_notify_events, link) {
1402                         if (item ==
1403                             (struct efi_register_notify_event *)search_key) {
1404                                 event = item;
1405                                 break;
1406                         }
1407                 }
1408                 if (!event)
1409                         return EFI_INVALID_PARAMETER;
1410
1411                 protocol = &event->protocol;
1412                 break;
1413         case BY_PROTOCOL:
1414                 if (!protocol)
1415                         return EFI_INVALID_PARAMETER;
1416                 break;
1417         default:
1418                 return EFI_INVALID_PARAMETER;
1419         }
1420
1421         /* Count how much space we need */
1422         list_for_each_entry(efiobj, &efi_obj_list, link) {
1423                 if (!efi_search(search_type, protocol, efiobj))
1424                         size += sizeof(void *);
1425         }
1426
1427         if (size == 0)
1428                 return EFI_NOT_FOUND;
1429
1430         if (!buffer_size)
1431                 return EFI_INVALID_PARAMETER;
1432
1433         if (*buffer_size < size) {
1434                 *buffer_size = size;
1435                 return EFI_BUFFER_TOO_SMALL;
1436         }
1437
1438         *buffer_size = size;
1439
1440         /* The buffer size is sufficient but there is no buffer */
1441         if (!buffer)
1442                 return EFI_INVALID_PARAMETER;
1443
1444         /* Then fill the array */
1445         list_for_each_entry(efiobj, &efi_obj_list, link) {
1446                 if (!efi_search(search_type, protocol, efiobj))
1447                         *buffer++ = efiobj;
1448         }
1449
1450         return EFI_SUCCESS;
1451 }
1452
1453 /**
1454  * efi_locate_handle_ext() - locate handles implementing a protocol.
1455  * @search_type: selection criterion
1456  * @protocol:    GUID of the protocol
1457  * @search_key:  registration key
1458  * @buffer_size: size of the buffer to receive the handles in bytes
1459  * @buffer:      buffer to receive the relevant handles
1460  *
1461  * This function implements the LocateHandle service.
1462  *
1463  * See the Unified Extensible Firmware Interface (UEFI) specification for
1464  * details.
1465  *
1466  * Return: 0 if the handle implements the protocol
1467  */
1468 static efi_status_t EFIAPI efi_locate_handle_ext(
1469                         enum efi_locate_search_type search_type,
1470                         const efi_guid_t *protocol, void *search_key,
1471                         efi_uintn_t *buffer_size, efi_handle_t *buffer)
1472 {
1473         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1474                   buffer_size, buffer);
1475
1476         return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1477                         buffer_size, buffer));
1478 }
1479
1480 /**
1481  * efi_remove_configuration_table() - collapses configuration table entries,
1482  *                                    removing index i
1483  *
1484  * @i: index of the table entry to be removed
1485  */
1486 static void efi_remove_configuration_table(int i)
1487 {
1488         struct efi_configuration_table *this = &systab.tables[i];
1489         struct efi_configuration_table *next = &systab.tables[i + 1];
1490         struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1491
1492         memmove(this, next, (ulong)end - (ulong)next);
1493         systab.nr_tables--;
1494 }
1495
1496 /**
1497  * efi_install_configuration_table() - adds, updates, or removes a
1498  *                                     configuration table
1499  * @guid:  GUID of the installed table
1500  * @table: table to be installed
1501  *
1502  * This function is used for internal calls. For the API implementation of the
1503  * InstallConfigurationTable service see efi_install_configuration_table_ext.
1504  *
1505  * Return: status code
1506  */
1507 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1508                                              void *table)
1509 {
1510         struct efi_event *evt;
1511         int i;
1512
1513         if (!guid)
1514                 return EFI_INVALID_PARAMETER;
1515
1516         /* Check for GUID override */
1517         for (i = 0; i < systab.nr_tables; i++) {
1518                 if (!guidcmp(guid, &systab.tables[i].guid)) {
1519                         if (table)
1520                                 systab.tables[i].table = table;
1521                         else
1522                                 efi_remove_configuration_table(i);
1523                         goto out;
1524                 }
1525         }
1526
1527         if (!table)
1528                 return EFI_NOT_FOUND;
1529
1530         /* No override, check for overflow */
1531         if (i >= EFI_MAX_CONFIGURATION_TABLES)
1532                 return EFI_OUT_OF_RESOURCES;
1533
1534         /* Add a new entry */
1535         memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1536         systab.tables[i].table = table;
1537         systab.nr_tables = i + 1;
1538
1539 out:
1540         /* systab.nr_tables may have changed. So we need to update the CRC32 */
1541         efi_update_table_header_crc32(&systab.hdr);
1542
1543         /* Notify that the configuration table was changed */
1544         list_for_each_entry(evt, &efi_events, link) {
1545                 if (evt->group && !guidcmp(evt->group, guid)) {
1546                         efi_signal_event(evt, false);
1547                         break;
1548                 }
1549         }
1550
1551         return EFI_SUCCESS;
1552 }
1553
1554 /**
1555  * efi_install_configuration_table_ex() - Adds, updates, or removes a
1556  *                                        configuration table.
1557  * @guid:  GUID of the installed table
1558  * @table: table to be installed
1559  *
1560  * This function implements the InstallConfigurationTable service.
1561  *
1562  * See the Unified Extensible Firmware Interface (UEFI) specification for
1563  * details.
1564  *
1565  * Return: status code
1566  */
1567 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1568                                                                void *table)
1569 {
1570         EFI_ENTRY("%pUl, %p", guid, table);
1571         return EFI_EXIT(efi_install_configuration_table(guid, table));
1572 }
1573
1574 /**
1575  * efi_setup_loaded_image() - initialize a loaded image
1576  *
1577  * Initialize a loaded_image_info and loaded_image_info object with correct
1578  * protocols, boot-device, etc.
1579  *
1580  * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1581  * code is returned.
1582  *
1583  * @device_path:        device path of the loaded image
1584  * @file_path:          file path of the loaded image
1585  * @handle_ptr:         handle of the loaded image
1586  * @info_ptr:           loaded image protocol
1587  * Return:              status code
1588  */
1589 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1590                                     struct efi_device_path *file_path,
1591                                     struct efi_loaded_image_obj **handle_ptr,
1592                                     struct efi_loaded_image **info_ptr)
1593 {
1594         efi_status_t ret;
1595         struct efi_loaded_image *info = NULL;
1596         struct efi_loaded_image_obj *obj = NULL;
1597         struct efi_device_path *dp;
1598
1599         /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1600         *handle_ptr = NULL;
1601         *info_ptr = NULL;
1602
1603         info = calloc(1, sizeof(*info));
1604         if (!info)
1605                 return EFI_OUT_OF_RESOURCES;
1606         obj = calloc(1, sizeof(*obj));
1607         if (!obj) {
1608                 free(info);
1609                 return EFI_OUT_OF_RESOURCES;
1610         }
1611         obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1612
1613         /* Add internal object to object list */
1614         efi_add_handle(&obj->header);
1615
1616         info->revision =  EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1617         info->file_path = file_path;
1618         info->system_table = &systab;
1619
1620         if (device_path) {
1621                 info->device_handle = efi_dp_find_obj(device_path, NULL);
1622
1623                 dp = efi_dp_append(device_path, file_path);
1624                 if (!dp) {
1625                         ret = EFI_OUT_OF_RESOURCES;
1626                         goto failure;
1627                 }
1628         } else {
1629                 dp = NULL;
1630         }
1631         ret = efi_add_protocol(&obj->header,
1632                                &efi_guid_loaded_image_device_path, dp);
1633         if (ret != EFI_SUCCESS)
1634                 goto failure;
1635
1636         /*
1637          * When asking for the loaded_image interface, just
1638          * return handle which points to loaded_image_info
1639          */
1640         ret = efi_add_protocol(&obj->header,
1641                                &efi_guid_loaded_image, info);
1642         if (ret != EFI_SUCCESS)
1643                 goto failure;
1644
1645         *info_ptr = info;
1646         *handle_ptr = obj;
1647
1648         return ret;
1649 failure:
1650         printf("ERROR: Failure to install protocols for loaded image\n");
1651         efi_delete_handle(&obj->header);
1652         free(info);
1653         return ret;
1654 }
1655
1656 /**
1657  * efi_load_image_from_path() - load an image using a file path
1658  *
1659  * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1660  * callers obligation to update the memory type as needed.
1661  *
1662  * @file_path:  the path of the image to load
1663  * @buffer:     buffer containing the loaded image
1664  * @size:       size of the loaded image
1665  * Return:      status code
1666  */
1667 static
1668 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1669                                       void **buffer, efi_uintn_t *size)
1670 {
1671         struct efi_file_info *info = NULL;
1672         struct efi_file_handle *f;
1673         static efi_status_t ret;
1674         u64 addr;
1675         efi_uintn_t bs;
1676
1677         /* In case of failure nothing is returned */
1678         *buffer = NULL;
1679         *size = 0;
1680
1681         /* Open file */
1682         f = efi_file_from_path(file_path);
1683         if (!f)
1684                 return EFI_DEVICE_ERROR;
1685
1686         /* Get file size */
1687         bs = 0;
1688         EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1689                                   &bs, info));
1690         if (ret != EFI_BUFFER_TOO_SMALL) {
1691                 ret =  EFI_DEVICE_ERROR;
1692                 goto error;
1693         }
1694
1695         info = malloc(bs);
1696         EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1697                                   info));
1698         if (ret != EFI_SUCCESS)
1699                 goto error;
1700
1701         /*
1702          * When reading the file we do not yet know if it contains an
1703          * application, a boottime driver, or a runtime driver. So here we
1704          * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1705          * update the reservation according to the image type.
1706          */
1707         bs = info->file_size;
1708         ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1709                                  EFI_BOOT_SERVICES_DATA,
1710                                  efi_size_in_pages(bs), &addr);
1711         if (ret != EFI_SUCCESS) {
1712                 ret = EFI_OUT_OF_RESOURCES;
1713                 goto error;
1714         }
1715
1716         /* Read file */
1717         EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1718         if (ret != EFI_SUCCESS)
1719                 efi_free_pages(addr, efi_size_in_pages(bs));
1720         *buffer = (void *)(uintptr_t)addr;
1721         *size = bs;
1722 error:
1723         EFI_CALL(f->close(f));
1724         free(info);
1725         return ret;
1726 }
1727
1728 /**
1729  * efi_load_image() - load an EFI image into memory
1730  * @boot_policy:   true for request originating from the boot manager
1731  * @parent_image:  the caller's image handle
1732  * @file_path:     the path of the image to load
1733  * @source_buffer: memory location from which the image is installed
1734  * @source_size:   size of the memory area from which the image is installed
1735  * @image_handle:  handle for the newly installed image
1736  *
1737  * This function implements the LoadImage service.
1738  *
1739  * See the Unified Extensible Firmware Interface (UEFI) specification
1740  * for details.
1741  *
1742  * Return: status code
1743  */
1744 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1745                                    efi_handle_t parent_image,
1746                                    struct efi_device_path *file_path,
1747                                    void *source_buffer,
1748                                    efi_uintn_t source_size,
1749                                    efi_handle_t *image_handle)
1750 {
1751         struct efi_device_path *dp, *fp;
1752         struct efi_loaded_image *info = NULL;
1753         struct efi_loaded_image_obj **image_obj =
1754                 (struct efi_loaded_image_obj **)image_handle;
1755         efi_status_t ret;
1756         void *dest_buffer;
1757
1758         EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1759                   file_path, source_buffer, source_size, image_handle);
1760
1761         if (!image_handle || !efi_search_obj(parent_image)) {
1762                 ret = EFI_INVALID_PARAMETER;
1763                 goto error;
1764         }
1765
1766         if (!source_buffer && !file_path) {
1767                 ret = EFI_NOT_FOUND;
1768                 goto error;
1769         }
1770         /* The parent image handle must refer to a loaded image */
1771         if (!parent_image->type) {
1772                 ret = EFI_INVALID_PARAMETER;
1773                 goto error;
1774         }
1775
1776         if (!source_buffer) {
1777                 ret = efi_load_image_from_path(file_path, &dest_buffer,
1778                                                &source_size);
1779                 if (ret != EFI_SUCCESS)
1780                         goto error;
1781         } else {
1782                 if (!source_size) {
1783                         ret = EFI_LOAD_ERROR;
1784                         goto error;
1785                 }
1786                 dest_buffer = source_buffer;
1787         }
1788         /* split file_path which contains both the device and file parts */
1789         efi_dp_split_file_path(file_path, &dp, &fp);
1790         ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1791         if (ret == EFI_SUCCESS)
1792                 ret = efi_load_pe(*image_obj, dest_buffer, info);
1793         if (!source_buffer)
1794                 /* Release buffer to which file was loaded */
1795                 efi_free_pages((uintptr_t)dest_buffer,
1796                                efi_size_in_pages(source_size));
1797         if (ret == EFI_SUCCESS) {
1798                 info->system_table = &systab;
1799                 info->parent_handle = parent_image;
1800         } else {
1801                 /* The image is invalid. Release all associated resources. */
1802                 efi_delete_handle(*image_handle);
1803                 *image_handle = NULL;
1804                 free(info);
1805         }
1806 error:
1807         return EFI_EXIT(ret);
1808 }
1809
1810 /**
1811  * efi_exit_caches() - fix up caches for EFI payloads if necessary
1812  */
1813 static void efi_exit_caches(void)
1814 {
1815 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1816         /*
1817          * Grub on 32bit ARM needs to have caches disabled before jumping into
1818          * a zImage, but does not know of all cache layers. Give it a hand.
1819          */
1820         if (efi_is_direct_boot)
1821                 cleanup_before_linux();
1822 #endif
1823 }
1824
1825 /**
1826  * efi_exit_boot_services() - stop all boot services
1827  * @image_handle: handle of the loaded image
1828  * @map_key:      key of the memory map
1829  *
1830  * This function implements the ExitBootServices service.
1831  *
1832  * See the Unified Extensible Firmware Interface (UEFI) specification
1833  * for details.
1834  *
1835  * All timer events are disabled. For exit boot services events the
1836  * notification function is called. The boot services are disabled in the
1837  * system table.
1838  *
1839  * Return: status code
1840  */
1841 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1842                                                   efi_uintn_t map_key)
1843 {
1844         struct efi_event *evt;
1845
1846         EFI_ENTRY("%p, %zx", image_handle, map_key);
1847
1848         /* Check that the caller has read the current memory map */
1849         if (map_key != efi_memory_map_key)
1850                 return EFI_INVALID_PARAMETER;
1851
1852         /* Make sure that notification functions are not called anymore */
1853         efi_tpl = TPL_HIGH_LEVEL;
1854
1855         /* Check if ExitBootServices has already been called */
1856         if (!systab.boottime)
1857                 return EFI_EXIT(EFI_SUCCESS);
1858
1859         /* Add related events to the event group */
1860         list_for_each_entry(evt, &efi_events, link) {
1861                 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1862                         evt->group = &efi_guid_event_group_exit_boot_services;
1863         }
1864         /* Notify that ExitBootServices is invoked. */
1865         list_for_each_entry(evt, &efi_events, link) {
1866                 if (evt->group &&
1867                     !guidcmp(evt->group,
1868                              &efi_guid_event_group_exit_boot_services)) {
1869                         efi_signal_event(evt, false);
1870                         break;
1871                 }
1872         }
1873
1874         /* TODO: Should persist EFI variables here */
1875
1876         board_quiesce_devices();
1877
1878         /* Fix up caches for EFI payloads if necessary */
1879         efi_exit_caches();
1880
1881         /* This stops all lingering devices */
1882         bootm_disable_interrupts();
1883
1884         /* Disable boot time services */
1885         systab.con_in_handle = NULL;
1886         systab.con_in = NULL;
1887         systab.con_out_handle = NULL;
1888         systab.con_out = NULL;
1889         systab.stderr_handle = NULL;
1890         systab.std_err = NULL;
1891         systab.boottime = NULL;
1892
1893         /* Recalculate CRC32 */
1894         efi_update_table_header_crc32(&systab.hdr);
1895
1896         /* Give the payload some time to boot */
1897         efi_set_watchdog(0);
1898         WATCHDOG_RESET();
1899
1900         return EFI_EXIT(EFI_SUCCESS);
1901 }
1902
1903 /**
1904  * efi_get_next_monotonic_count() - get next value of the counter
1905  * @count: returned value of the counter
1906  *
1907  * This function implements the NextMonotonicCount service.
1908  *
1909  * See the Unified Extensible Firmware Interface (UEFI) specification for
1910  * details.
1911  *
1912  * Return: status code
1913  */
1914 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1915 {
1916         static uint64_t mono;
1917
1918         EFI_ENTRY("%p", count);
1919         *count = mono++;
1920         return EFI_EXIT(EFI_SUCCESS);
1921 }
1922
1923 /**
1924  * efi_stall() - sleep
1925  * @microseconds: period to sleep in microseconds
1926  *
1927  * This function implements the Stall service.
1928  *
1929  * See the Unified Extensible Firmware Interface (UEFI) specification for
1930  * details.
1931  *
1932  * Return:  status code
1933  */
1934 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1935 {
1936         EFI_ENTRY("%ld", microseconds);
1937         udelay(microseconds);
1938         return EFI_EXIT(EFI_SUCCESS);
1939 }
1940
1941 /**
1942  * efi_set_watchdog_timer() - reset the watchdog timer
1943  * @timeout:       seconds before reset by watchdog
1944  * @watchdog_code: code to be logged when resetting
1945  * @data_size:     size of buffer in bytes
1946  * @watchdog_data: buffer with data describing the reset reason
1947  *
1948  * This function implements the SetWatchdogTimer service.
1949  *
1950  * See the Unified Extensible Firmware Interface (UEFI) specification for
1951  * details.
1952  *
1953  * Return: status code
1954  */
1955 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1956                                                   uint64_t watchdog_code,
1957                                                   unsigned long data_size,
1958                                                   uint16_t *watchdog_data)
1959 {
1960         EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1961                   data_size, watchdog_data);
1962         return EFI_EXIT(efi_set_watchdog(timeout));
1963 }
1964
1965 /**
1966  * efi_close_protocol() - close a protocol
1967  * @handle:            handle on which the protocol shall be closed
1968  * @protocol:          GUID of the protocol to close
1969  * @agent_handle:      handle of the driver
1970  * @controller_handle: handle of the controller
1971  *
1972  * This function implements the CloseProtocol service.
1973  *
1974  * See the Unified Extensible Firmware Interface (UEFI) specification for
1975  * details.
1976  *
1977  * Return: status code
1978  */
1979 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1980                                               const efi_guid_t *protocol,
1981                                               efi_handle_t agent_handle,
1982                                               efi_handle_t controller_handle)
1983 {
1984         struct efi_handler *handler;
1985         struct efi_open_protocol_info_item *item;
1986         struct efi_open_protocol_info_item *pos;
1987         efi_status_t r;
1988
1989         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1990                   controller_handle);
1991
1992         if (!efi_search_obj(agent_handle) ||
1993             (controller_handle && !efi_search_obj(controller_handle))) {
1994                 r = EFI_INVALID_PARAMETER;
1995                 goto out;
1996         }
1997         r = efi_search_protocol(handle, protocol, &handler);
1998         if (r != EFI_SUCCESS)
1999                 goto out;
2000
2001         r = EFI_NOT_FOUND;
2002         list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2003                 if (item->info.agent_handle == agent_handle &&
2004                     item->info.controller_handle == controller_handle) {
2005                         efi_delete_open_info(item);
2006                         r = EFI_SUCCESS;
2007                         break;
2008                 }
2009         }
2010 out:
2011         return EFI_EXIT(r);
2012 }
2013
2014 /**
2015  * efi_open_protocol_information() - provide information about then open status
2016  *                                   of a protocol on a handle
2017  * @handle:       handle for which the information shall be retrieved
2018  * @protocol:     GUID of the protocol
2019  * @entry_buffer: buffer to receive the open protocol information
2020  * @entry_count:  number of entries available in the buffer
2021  *
2022  * This function implements the OpenProtocolInformation service.
2023  *
2024  * See the Unified Extensible Firmware Interface (UEFI) specification for
2025  * details.
2026  *
2027  * Return: status code
2028  */
2029 static efi_status_t EFIAPI efi_open_protocol_information(
2030                         efi_handle_t handle, const efi_guid_t *protocol,
2031                         struct efi_open_protocol_info_entry **entry_buffer,
2032                         efi_uintn_t *entry_count)
2033 {
2034         unsigned long buffer_size;
2035         unsigned long count;
2036         struct efi_handler *handler;
2037         struct efi_open_protocol_info_item *item;
2038         efi_status_t r;
2039
2040         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2041                   entry_count);
2042
2043         /* Check parameters */
2044         if (!entry_buffer) {
2045                 r = EFI_INVALID_PARAMETER;
2046                 goto out;
2047         }
2048         r = efi_search_protocol(handle, protocol, &handler);
2049         if (r != EFI_SUCCESS)
2050                 goto out;
2051
2052         /* Count entries */
2053         count = 0;
2054         list_for_each_entry(item, &handler->open_infos, link) {
2055                 if (item->info.open_count)
2056                         ++count;
2057         }
2058         *entry_count = count;
2059         *entry_buffer = NULL;
2060         if (!count) {
2061                 r = EFI_SUCCESS;
2062                 goto out;
2063         }
2064
2065         /* Copy entries */
2066         buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2067         r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2068                               (void **)entry_buffer);
2069         if (r != EFI_SUCCESS)
2070                 goto out;
2071         list_for_each_entry_reverse(item, &handler->open_infos, link) {
2072                 if (item->info.open_count)
2073                         (*entry_buffer)[--count] = item->info;
2074         }
2075 out:
2076         return EFI_EXIT(r);
2077 }
2078
2079 /**
2080  * efi_protocols_per_handle() - get protocols installed on a handle
2081  * @handle:                handle for which the information is retrieved
2082  * @protocol_buffer:       buffer with protocol GUIDs
2083  * @protocol_buffer_count: number of entries in the buffer
2084  *
2085  * This function implements the ProtocolsPerHandleService.
2086  *
2087  * See the Unified Extensible Firmware Interface (UEFI) specification for
2088  * details.
2089  *
2090  * Return: status code
2091  */
2092 static efi_status_t EFIAPI efi_protocols_per_handle(
2093                         efi_handle_t handle, efi_guid_t ***protocol_buffer,
2094                         efi_uintn_t *protocol_buffer_count)
2095 {
2096         unsigned long buffer_size;
2097         struct efi_object *efiobj;
2098         struct list_head *protocol_handle;
2099         efi_status_t r;
2100
2101         EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2102                   protocol_buffer_count);
2103
2104         if (!handle || !protocol_buffer || !protocol_buffer_count)
2105                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2106
2107         *protocol_buffer = NULL;
2108         *protocol_buffer_count = 0;
2109
2110         efiobj = efi_search_obj(handle);
2111         if (!efiobj)
2112                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2113
2114         /* Count protocols */
2115         list_for_each(protocol_handle, &efiobj->protocols) {
2116                 ++*protocol_buffer_count;
2117         }
2118
2119         /* Copy GUIDs */
2120         if (*protocol_buffer_count) {
2121                 size_t j = 0;
2122
2123                 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2124                 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2125                                       (void **)protocol_buffer);
2126                 if (r != EFI_SUCCESS)
2127                         return EFI_EXIT(r);
2128                 list_for_each(protocol_handle, &efiobj->protocols) {
2129                         struct efi_handler *protocol;
2130
2131                         protocol = list_entry(protocol_handle,
2132                                               struct efi_handler, link);
2133                         (*protocol_buffer)[j] = (void *)protocol->guid;
2134                         ++j;
2135                 }
2136         }
2137
2138         return EFI_EXIT(EFI_SUCCESS);
2139 }
2140
2141 /**
2142  * efi_locate_handle_buffer() - locate handles implementing a protocol
2143  * @search_type: selection criterion
2144  * @protocol:    GUID of the protocol
2145  * @search_key:  registration key
2146  * @no_handles:  number of returned handles
2147  * @buffer:      buffer with the returned handles
2148  *
2149  * This function implements the LocateHandleBuffer service.
2150  *
2151  * See the Unified Extensible Firmware Interface (UEFI) specification for
2152  * details.
2153  *
2154  * Return: status code
2155  */
2156 static efi_status_t EFIAPI efi_locate_handle_buffer(
2157                         enum efi_locate_search_type search_type,
2158                         const efi_guid_t *protocol, void *search_key,
2159                         efi_uintn_t *no_handles, efi_handle_t **buffer)
2160 {
2161         efi_status_t r;
2162         efi_uintn_t buffer_size = 0;
2163
2164         EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2165                   no_handles, buffer);
2166
2167         if (!no_handles || !buffer) {
2168                 r = EFI_INVALID_PARAMETER;
2169                 goto out;
2170         }
2171         *no_handles = 0;
2172         *buffer = NULL;
2173         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2174                               *buffer);
2175         if (r != EFI_BUFFER_TOO_SMALL)
2176                 goto out;
2177         r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2178                               (void **)buffer);
2179         if (r != EFI_SUCCESS)
2180                 goto out;
2181         r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2182                               *buffer);
2183         if (r == EFI_SUCCESS)
2184                 *no_handles = buffer_size / sizeof(efi_handle_t);
2185 out:
2186         return EFI_EXIT(r);
2187 }
2188
2189 /**
2190  * efi_locate_protocol() - find an interface implementing a protocol
2191  * @protocol:           GUID of the protocol
2192  * @registration:       registration key passed to the notification function
2193  * @protocol_interface: interface implementing the protocol
2194  *
2195  * This function implements the LocateProtocol service.
2196  *
2197  * See the Unified Extensible Firmware Interface (UEFI) specification for
2198  * details.
2199  *
2200  * Return: status code
2201  */
2202 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2203                                                void *registration,
2204                                                void **protocol_interface)
2205 {
2206         struct list_head *lhandle;
2207         efi_status_t ret;
2208
2209         EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2210
2211         if (!protocol || !protocol_interface)
2212                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2213
2214         list_for_each(lhandle, &efi_obj_list) {
2215                 struct efi_object *efiobj;
2216                 struct efi_handler *handler;
2217
2218                 efiobj = list_entry(lhandle, struct efi_object, link);
2219
2220                 ret = efi_search_protocol(efiobj, protocol, &handler);
2221                 if (ret == EFI_SUCCESS) {
2222                         *protocol_interface = handler->protocol_interface;
2223                         return EFI_EXIT(EFI_SUCCESS);
2224                 }
2225         }
2226         *protocol_interface = NULL;
2227
2228         return EFI_EXIT(EFI_NOT_FOUND);
2229 }
2230
2231 /**
2232  * efi_locate_device_path() - Get the device path and handle of an device
2233  *                            implementing a protocol
2234  * @protocol:    GUID of the protocol
2235  * @device_path: device path
2236  * @device:      handle of the device
2237  *
2238  * This function implements the LocateDevicePath service.
2239  *
2240  * See the Unified Extensible Firmware Interface (UEFI) specification for
2241  * details.
2242  *
2243  * Return: status code
2244  */
2245 static efi_status_t EFIAPI efi_locate_device_path(
2246                         const efi_guid_t *protocol,
2247                         struct efi_device_path **device_path,
2248                         efi_handle_t *device)
2249 {
2250         struct efi_device_path *dp;
2251         size_t i;
2252         struct efi_handler *handler;
2253         efi_handle_t *handles;
2254         size_t len, len_dp;
2255         size_t len_best = 0;
2256         efi_uintn_t no_handles;
2257         u8 *remainder;
2258         efi_status_t ret;
2259
2260         EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2261
2262         if (!protocol || !device_path || !*device_path) {
2263                 ret = EFI_INVALID_PARAMETER;
2264                 goto out;
2265         }
2266
2267         /* Find end of device path */
2268         len = efi_dp_instance_size(*device_path);
2269
2270         /* Get all handles implementing the protocol */
2271         ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2272                                                 &no_handles, &handles));
2273         if (ret != EFI_SUCCESS)
2274                 goto out;
2275
2276         for (i = 0; i < no_handles; ++i) {
2277                 /* Find the device path protocol */
2278                 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2279                                           &handler);
2280                 if (ret != EFI_SUCCESS)
2281                         continue;
2282                 dp = (struct efi_device_path *)handler->protocol_interface;
2283                 len_dp = efi_dp_instance_size(dp);
2284                 /*
2285                  * This handle can only be a better fit
2286                  * if its device path length is longer than the best fit and
2287                  * if its device path length is shorter of equal the searched
2288                  * device path.
2289                  */
2290                 if (len_dp <= len_best || len_dp > len)
2291                         continue;
2292                 /* Check if dp is a subpath of device_path */
2293                 if (memcmp(*device_path, dp, len_dp))
2294                         continue;
2295                 if (!device) {
2296                         ret = EFI_INVALID_PARAMETER;
2297                         goto out;
2298                 }
2299                 *device = handles[i];
2300                 len_best = len_dp;
2301         }
2302         if (len_best) {
2303                 remainder = (u8 *)*device_path + len_best;
2304                 *device_path = (struct efi_device_path *)remainder;
2305                 ret = EFI_SUCCESS;
2306         } else {
2307                 ret = EFI_NOT_FOUND;
2308         }
2309 out:
2310         return EFI_EXIT(ret);
2311 }
2312
2313 /**
2314  * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2315  *                                              interfaces
2316  * @handle: handle on which the protocol interfaces shall be installed
2317  * @...:    NULL terminated argument list with pairs of protocol GUIDS and
2318  *          interfaces
2319  *
2320  * This function implements the MultipleProtocolInterfaces service.
2321  *
2322  * See the Unified Extensible Firmware Interface (UEFI) specification for
2323  * details.
2324  *
2325  * Return: status code
2326  */
2327 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2328                                 (efi_handle_t *handle, ...)
2329 {
2330         EFI_ENTRY("%p", handle);
2331
2332         efi_va_list argptr;
2333         const efi_guid_t *protocol;
2334         void *protocol_interface;
2335         efi_status_t r = EFI_SUCCESS;
2336         int i = 0;
2337
2338         if (!handle)
2339                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2340
2341         efi_va_start(argptr, handle);
2342         for (;;) {
2343                 protocol = efi_va_arg(argptr, efi_guid_t*);
2344                 if (!protocol)
2345                         break;
2346                 protocol_interface = efi_va_arg(argptr, void*);
2347                 r = EFI_CALL(efi_install_protocol_interface(
2348                                                 handle, protocol,
2349                                                 EFI_NATIVE_INTERFACE,
2350                                                 protocol_interface));
2351                 if (r != EFI_SUCCESS)
2352                         break;
2353                 i++;
2354         }
2355         efi_va_end(argptr);
2356         if (r == EFI_SUCCESS)
2357                 return EFI_EXIT(r);
2358
2359         /* If an error occurred undo all changes. */
2360         efi_va_start(argptr, handle);
2361         for (; i; --i) {
2362                 protocol = efi_va_arg(argptr, efi_guid_t*);
2363                 protocol_interface = efi_va_arg(argptr, void*);
2364                 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2365                                                           protocol_interface));
2366         }
2367         efi_va_end(argptr);
2368
2369         return EFI_EXIT(r);
2370 }
2371
2372 /**
2373  * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2374  *                                                interfaces
2375  * @handle: handle from which the protocol interfaces shall be removed
2376  * @...:    NULL terminated argument list with pairs of protocol GUIDS and
2377  *          interfaces
2378  *
2379  * This function implements the UninstallMultipleProtocolInterfaces service.
2380  *
2381  * See the Unified Extensible Firmware Interface (UEFI) specification for
2382  * details.
2383  *
2384  * Return: status code
2385  */
2386 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2387                         efi_handle_t handle, ...)
2388 {
2389         EFI_ENTRY("%p", handle);
2390
2391         efi_va_list argptr;
2392         const efi_guid_t *protocol;
2393         void *protocol_interface;
2394         efi_status_t r = EFI_SUCCESS;
2395         size_t i = 0;
2396
2397         if (!handle)
2398                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2399
2400         efi_va_start(argptr, handle);
2401         for (;;) {
2402                 protocol = efi_va_arg(argptr, efi_guid_t*);
2403                 if (!protocol)
2404                         break;
2405                 protocol_interface = efi_va_arg(argptr, void*);
2406                 r = efi_uninstall_protocol(handle, protocol,
2407                                            protocol_interface);
2408                 if (r != EFI_SUCCESS)
2409                         break;
2410                 i++;
2411         }
2412         efi_va_end(argptr);
2413         if (r == EFI_SUCCESS) {
2414                 /* If the last protocol has been removed, delete the handle. */
2415                 if (list_empty(&handle->protocols)) {
2416                         list_del(&handle->link);
2417                         free(handle);
2418                 }
2419                 return EFI_EXIT(r);
2420         }
2421
2422         /* If an error occurred undo all changes. */
2423         efi_va_start(argptr, handle);
2424         for (; i; --i) {
2425                 protocol = efi_va_arg(argptr, efi_guid_t*);
2426                 protocol_interface = efi_va_arg(argptr, void*);
2427                 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2428                                                         EFI_NATIVE_INTERFACE,
2429                                                         protocol_interface));
2430         }
2431         efi_va_end(argptr);
2432
2433         /* In case of an error always return EFI_INVALID_PARAMETER */
2434         return EFI_EXIT(EFI_INVALID_PARAMETER);
2435 }
2436
2437 /**
2438  * efi_calculate_crc32() - calculate cyclic redundancy code
2439  * @data:      buffer with data
2440  * @data_size: size of buffer in bytes
2441  * @crc32_p:   cyclic redundancy code
2442  *
2443  * This function implements the CalculateCrc32 service.
2444  *
2445  * See the Unified Extensible Firmware Interface (UEFI) specification for
2446  * details.
2447  *
2448  * Return: status code
2449  */
2450 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2451                                                efi_uintn_t data_size,
2452                                                u32 *crc32_p)
2453 {
2454         EFI_ENTRY("%p, %zu", data, data_size);
2455         *crc32_p = crc32(0, data, data_size);
2456         return EFI_EXIT(EFI_SUCCESS);
2457 }
2458
2459 /**
2460  * efi_copy_mem() - copy memory
2461  * @destination: destination of the copy operation
2462  * @source:      source of the copy operation
2463  * @length:      number of bytes to copy
2464  *
2465  * This function implements the CopyMem service.
2466  *
2467  * See the Unified Extensible Firmware Interface (UEFI) specification for
2468  * details.
2469  */
2470 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2471                                 size_t length)
2472 {
2473         EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2474         memmove(destination, source, length);
2475         EFI_EXIT(EFI_SUCCESS);
2476 }
2477
2478 /**
2479  * efi_set_mem() - Fill memory with a byte value.
2480  * @buffer: buffer to fill
2481  * @size:   size of buffer in bytes
2482  * @value:  byte to copy to the buffer
2483  *
2484  * This function implements the SetMem service.
2485  *
2486  * See the Unified Extensible Firmware Interface (UEFI) specification for
2487  * details.
2488  */
2489 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2490 {
2491         EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2492         memset(buffer, value, size);
2493         EFI_EXIT(EFI_SUCCESS);
2494 }
2495
2496 /**
2497  * efi_protocol_open() - open protocol interface on a handle
2498  * @handler:            handler of a protocol
2499  * @protocol_interface: interface implementing the protocol
2500  * @agent_handle:       handle of the driver
2501  * @controller_handle:  handle of the controller
2502  * @attributes:         attributes indicating how to open the protocol
2503  *
2504  * Return: status code
2505  */
2506 static efi_status_t efi_protocol_open(
2507                         struct efi_handler *handler,
2508                         void **protocol_interface, void *agent_handle,
2509                         void *controller_handle, uint32_t attributes)
2510 {
2511         struct efi_open_protocol_info_item *item;
2512         struct efi_open_protocol_info_entry *match = NULL;
2513         bool opened_by_driver = false;
2514         bool opened_exclusive = false;
2515
2516         /* If there is no agent, only return the interface */
2517         if (!agent_handle)
2518                 goto out;
2519
2520         /* For TEST_PROTOCOL ignore interface attribute */
2521         if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2522                 *protocol_interface = NULL;
2523
2524         /*
2525          * Check if the protocol is already opened by a driver with the same
2526          * attributes or opened exclusively
2527          */
2528         list_for_each_entry(item, &handler->open_infos, link) {
2529                 if (item->info.agent_handle == agent_handle) {
2530                         if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2531                             (item->info.attributes == attributes))
2532                                 return EFI_ALREADY_STARTED;
2533                 }
2534                 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2535                         opened_exclusive = true;
2536         }
2537
2538         /* Only one controller can open the protocol exclusively */
2539         if (opened_exclusive && attributes &
2540             (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2541                 return EFI_ACCESS_DENIED;
2542
2543         /* Prepare exclusive opening */
2544         if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2545                 /* Try to disconnect controllers */
2546                 list_for_each_entry(item, &handler->open_infos, link) {
2547                         if (item->info.attributes ==
2548                                         EFI_OPEN_PROTOCOL_BY_DRIVER)
2549                                 EFI_CALL(efi_disconnect_controller(
2550                                                 item->info.controller_handle,
2551                                                 item->info.agent_handle,
2552                                                 NULL));
2553                 }
2554                 opened_by_driver = false;
2555                 /* Check if all controllers are disconnected */
2556                 list_for_each_entry(item, &handler->open_infos, link) {
2557                         if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2558                                 opened_by_driver = true;
2559                 }
2560                 /* Only one controller can be connected */
2561                 if (opened_by_driver)
2562                         return EFI_ACCESS_DENIED;
2563         }
2564
2565         /* Find existing entry */
2566         list_for_each_entry(item, &handler->open_infos, link) {
2567                 if (item->info.agent_handle == agent_handle &&
2568                     item->info.controller_handle == controller_handle)
2569                         match = &item->info;
2570         }
2571         /* None found, create one */
2572         if (!match) {
2573                 match = efi_create_open_info(handler);
2574                 if (!match)
2575                         return EFI_OUT_OF_RESOURCES;
2576         }
2577
2578         match->agent_handle = agent_handle;
2579         match->controller_handle = controller_handle;
2580         match->attributes = attributes;
2581         match->open_count++;
2582
2583 out:
2584         /* For TEST_PROTOCOL ignore interface attribute. */
2585         if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2586                 *protocol_interface = handler->protocol_interface;
2587
2588         return EFI_SUCCESS;
2589 }
2590
2591 /**
2592  * efi_open_protocol() - open protocol interface on a handle
2593  * @handle:             handle on which the protocol shall be opened
2594  * @protocol:           GUID of the protocol
2595  * @protocol_interface: interface implementing the protocol
2596  * @agent_handle:       handle of the driver
2597  * @controller_handle:  handle of the controller
2598  * @attributes:         attributes indicating how to open the protocol
2599  *
2600  * This function implements the OpenProtocol interface.
2601  *
2602  * See the Unified Extensible Firmware Interface (UEFI) specification for
2603  * details.
2604  *
2605  * Return: status code
2606  */
2607 static efi_status_t EFIAPI efi_open_protocol
2608                         (efi_handle_t handle, const efi_guid_t *protocol,
2609                          void **protocol_interface, efi_handle_t agent_handle,
2610                          efi_handle_t controller_handle, uint32_t attributes)
2611 {
2612         struct efi_handler *handler;
2613         efi_status_t r = EFI_INVALID_PARAMETER;
2614
2615         EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2616                   protocol_interface, agent_handle, controller_handle,
2617                   attributes);
2618
2619         if (!handle || !protocol ||
2620             (!protocol_interface && attributes !=
2621              EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2622                 goto out;
2623         }
2624
2625         switch (attributes) {
2626         case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2627         case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2628         case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2629                 break;
2630         case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2631                 if (controller_handle == handle)
2632                         goto out;
2633                 /* fall-through */
2634         case EFI_OPEN_PROTOCOL_BY_DRIVER:
2635         case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2636                 /* Check that the controller handle is valid */
2637                 if (!efi_search_obj(controller_handle))
2638                         goto out;
2639                 /* fall-through */
2640         case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2641                 /* Check that the agent handle is valid */
2642                 if (!efi_search_obj(agent_handle))
2643                         goto out;
2644                 break;
2645         default:
2646                 goto out;
2647         }
2648
2649         r = efi_search_protocol(handle, protocol, &handler);
2650         switch (r) {
2651         case EFI_SUCCESS:
2652                 break;
2653         case EFI_NOT_FOUND:
2654                 r = EFI_UNSUPPORTED;
2655                 goto out;
2656         default:
2657                 goto out;
2658         }
2659
2660         r = efi_protocol_open(handler, protocol_interface, agent_handle,
2661                               controller_handle, attributes);
2662 out:
2663         return EFI_EXIT(r);
2664 }
2665
2666 /**
2667  * efi_start_image() - call the entry point of an image
2668  * @image_handle:   handle of the image
2669  * @exit_data_size: size of the buffer
2670  * @exit_data:      buffer to receive the exit data of the called image
2671  *
2672  * This function implements the StartImage service.
2673  *
2674  * See the Unified Extensible Firmware Interface (UEFI) specification for
2675  * details.
2676  *
2677  * Return: status code
2678  */
2679 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2680                                     efi_uintn_t *exit_data_size,
2681                                     u16 **exit_data)
2682 {
2683         struct efi_loaded_image_obj *image_obj =
2684                 (struct efi_loaded_image_obj *)image_handle;
2685         efi_status_t ret;
2686         void *info;
2687         efi_handle_t parent_image = current_image;
2688
2689         EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2690
2691         /* Check parameters */
2692         ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2693                                          &info, NULL, NULL,
2694                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2695         if (ret != EFI_SUCCESS)
2696                 return EFI_EXIT(EFI_INVALID_PARAMETER);
2697
2698         efi_is_direct_boot = false;
2699
2700         image_obj->exit_data_size = exit_data_size;
2701         image_obj->exit_data = exit_data;
2702
2703         /* call the image! */
2704         if (setjmp(&image_obj->exit_jmp)) {
2705                 /*
2706                  * We called the entry point of the child image with EFI_CALL
2707                  * in the lines below. The child image called the Exit() boot
2708                  * service efi_exit() which executed the long jump that brought
2709                  * us to the current line. This implies that the second half
2710                  * of the EFI_CALL macro has not been executed.
2711                  */
2712 #ifdef CONFIG_ARM
2713                 /*
2714                  * efi_exit() called efi_restore_gd(). We have to undo this
2715                  * otherwise __efi_entry_check() will put the wrong value into
2716                  * app_gd.
2717                  */
2718                 gd = app_gd;
2719 #endif
2720                 /*
2721                  * To get ready to call EFI_EXIT below we have to execute the
2722                  * missed out steps of EFI_CALL.
2723                  */
2724                 assert(__efi_entry_check());
2725                 EFI_PRINT("%lu returned by started image\n",
2726                           (unsigned long)((uintptr_t)image_obj->exit_status &
2727                           ~EFI_ERROR_MASK));
2728                 current_image = parent_image;
2729                 return EFI_EXIT(image_obj->exit_status);
2730         }
2731
2732         current_image = image_handle;
2733         image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2734         EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2735         ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2736
2737         /*
2738          * Usually UEFI applications call Exit() instead of returning.
2739          * But because the world doesn't consist of ponies and unicorns,
2740          * we're happy to emulate that behavior on behalf of a payload
2741          * that forgot.
2742          */
2743         return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2744 }
2745
2746 /**
2747  * efi_delete_image() - delete loaded image from memory)
2748  *
2749  * @image_obj:                  handle of the loaded image
2750  * @loaded_image_protocol:      loaded image protocol
2751  */
2752 static void efi_delete_image(struct efi_loaded_image_obj *image_obj,
2753                              struct efi_loaded_image *loaded_image_protocol)
2754 {
2755         efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2756                        efi_size_in_pages(loaded_image_protocol->image_size));
2757         efi_delete_handle(&image_obj->header);
2758 }
2759
2760 /**
2761  * efi_unload_image() - unload an EFI image
2762  * @image_handle: handle of the image to be unloaded
2763  *
2764  * This function implements the UnloadImage service.
2765  *
2766  * See the Unified Extensible Firmware Interface (UEFI) specification for
2767  * details.
2768  *
2769  * Return: status code
2770  */
2771 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2772 {
2773         efi_status_t ret = EFI_SUCCESS;
2774         struct efi_object *efiobj;
2775         struct efi_loaded_image *loaded_image_protocol;
2776
2777         EFI_ENTRY("%p", image_handle);
2778
2779         efiobj = efi_search_obj(image_handle);
2780         if (!efiobj) {
2781                 ret = EFI_INVALID_PARAMETER;
2782                 goto out;
2783         }
2784         /* Find the loaded image protocol */
2785         ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2786                                          (void **)&loaded_image_protocol,
2787                                          NULL, NULL,
2788                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2789         if (ret != EFI_SUCCESS) {
2790                 ret = EFI_INVALID_PARAMETER;
2791                 goto out;
2792         }
2793         switch (efiobj->type) {
2794         case EFI_OBJECT_TYPE_STARTED_IMAGE:
2795                 /* Call the unload function */
2796                 if (!loaded_image_protocol->unload) {
2797                         ret = EFI_UNSUPPORTED;
2798                         goto out;
2799                 }
2800                 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2801                 if (ret != EFI_SUCCESS)
2802                         goto out;
2803                 break;
2804         case EFI_OBJECT_TYPE_LOADED_IMAGE:
2805                 break;
2806         default:
2807                 ret = EFI_INVALID_PARAMETER;
2808                 goto out;
2809         }
2810         efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2811                          loaded_image_protocol);
2812 out:
2813         return EFI_EXIT(ret);
2814 }
2815
2816 /**
2817  * efi_update_exit_data() - fill exit data parameters of StartImage()
2818  *
2819  * @image_obj           image handle
2820  * @exit_data_size      size of the exit data buffer
2821  * @exit_data           buffer with data returned by UEFI payload
2822  * Return:              status code
2823  */
2824 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2825                                          efi_uintn_t exit_data_size,
2826                                          u16 *exit_data)
2827 {
2828         efi_status_t ret;
2829
2830         /*
2831          * If exit_data is not provided to StartImage(), exit_data_size must be
2832          * ignored.
2833          */
2834         if (!image_obj->exit_data)
2835                 return EFI_SUCCESS;
2836         if (image_obj->exit_data_size)
2837                 *image_obj->exit_data_size = exit_data_size;
2838         if (exit_data_size && exit_data) {
2839                 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2840                                         exit_data_size,
2841                                         (void **)image_obj->exit_data);
2842                 if (ret != EFI_SUCCESS)
2843                         return ret;
2844                 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2845         } else {
2846                 image_obj->exit_data = NULL;
2847         }
2848         return EFI_SUCCESS;
2849 }
2850
2851 /**
2852  * efi_exit() - leave an EFI application or driver
2853  * @image_handle:   handle of the application or driver that is exiting
2854  * @exit_status:    status code
2855  * @exit_data_size: size of the buffer in bytes
2856  * @exit_data:      buffer with data describing an error
2857  *
2858  * This function implements the Exit service.
2859  *
2860  * See the Unified Extensible Firmware Interface (UEFI) specification for
2861  * details.
2862  *
2863  * Return: status code
2864  */
2865 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2866                                     efi_status_t exit_status,
2867                                     efi_uintn_t exit_data_size,
2868                                     u16 *exit_data)
2869 {
2870         /*
2871          * TODO: We should call the unload procedure of the loaded
2872          *       image protocol.
2873          */
2874         efi_status_t ret;
2875         struct efi_loaded_image *loaded_image_protocol;
2876         struct efi_loaded_image_obj *image_obj =
2877                 (struct efi_loaded_image_obj *)image_handle;
2878
2879         EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2880                   exit_data_size, exit_data);
2881
2882         /* Check parameters */
2883         ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2884                                          (void **)&loaded_image_protocol,
2885                                          NULL, NULL,
2886                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2887         if (ret != EFI_SUCCESS) {
2888                 ret = EFI_INVALID_PARAMETER;
2889                 goto out;
2890         }
2891
2892         /* Unloading of unstarted images */
2893         switch (image_obj->header.type) {
2894         case EFI_OBJECT_TYPE_STARTED_IMAGE:
2895                 break;
2896         case EFI_OBJECT_TYPE_LOADED_IMAGE:
2897                 efi_delete_image(image_obj, loaded_image_protocol);
2898                 ret = EFI_SUCCESS;
2899                 goto out;
2900         default:
2901                 /* Handle does not refer to loaded image */
2902                 ret = EFI_INVALID_PARAMETER;
2903                 goto out;
2904         }
2905         /* A started image can only be unloaded it is the last one started. */
2906         if (image_handle != current_image) {
2907                 ret = EFI_INVALID_PARAMETER;
2908                 goto out;
2909         }
2910
2911         /* Exit data is only foreseen in case of failure. */
2912         if (exit_status != EFI_SUCCESS) {
2913                 ret = efi_update_exit_data(image_obj, exit_data_size,
2914                                            exit_data);
2915                 /* Exiting has priority. Don't return error to caller. */
2916                 if (ret != EFI_SUCCESS)
2917                         EFI_PRINT("%s: out of memory\n", __func__);
2918         }
2919         if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
2920             exit_status != EFI_SUCCESS)
2921                 efi_delete_image(image_obj, loaded_image_protocol);
2922
2923         /* Make sure entry/exit counts for EFI world cross-overs match */
2924         EFI_EXIT(exit_status);
2925
2926         /*
2927          * But longjmp out with the U-Boot gd, not the application's, as
2928          * the other end is a setjmp call inside EFI context.
2929          */
2930         efi_restore_gd();
2931
2932         image_obj->exit_status = exit_status;
2933         longjmp(&image_obj->exit_jmp, 1);
2934
2935         panic("EFI application exited");
2936 out:
2937         return EFI_EXIT(ret);
2938 }
2939
2940 /**
2941  * efi_handle_protocol() - get interface of a protocol on a handle
2942  * @handle:             handle on which the protocol shall be opened
2943  * @protocol:           GUID of the protocol
2944  * @protocol_interface: interface implementing the protocol
2945  *
2946  * This function implements the HandleProtocol service.
2947  *
2948  * See the Unified Extensible Firmware Interface (UEFI) specification for
2949  * details.
2950  *
2951  * Return: status code
2952  */
2953 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2954                                                const efi_guid_t *protocol,
2955                                                void **protocol_interface)
2956 {
2957         return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2958                                  NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2959 }
2960
2961 /**
2962  * efi_bind_controller() - bind a single driver to a controller
2963  * @controller_handle:   controller handle
2964  * @driver_image_handle: driver handle
2965  * @remain_device_path:  remaining path
2966  *
2967  * Return: status code
2968  */
2969 static efi_status_t efi_bind_controller(
2970                         efi_handle_t controller_handle,
2971                         efi_handle_t driver_image_handle,
2972                         struct efi_device_path *remain_device_path)
2973 {
2974         struct efi_driver_binding_protocol *binding_protocol;
2975         efi_status_t r;
2976
2977         r = EFI_CALL(efi_open_protocol(driver_image_handle,
2978                                        &efi_guid_driver_binding_protocol,
2979                                        (void **)&binding_protocol,
2980                                        driver_image_handle, NULL,
2981                                        EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2982         if (r != EFI_SUCCESS)
2983                 return r;
2984         r = EFI_CALL(binding_protocol->supported(binding_protocol,
2985                                                  controller_handle,
2986                                                  remain_device_path));
2987         if (r == EFI_SUCCESS)
2988                 r = EFI_CALL(binding_protocol->start(binding_protocol,
2989                                                      controller_handle,
2990                                                      remain_device_path));
2991         EFI_CALL(efi_close_protocol(driver_image_handle,
2992                                     &efi_guid_driver_binding_protocol,
2993                                     driver_image_handle, NULL));
2994         return r;
2995 }
2996
2997 /**
2998  * efi_connect_single_controller() - connect a single driver to a controller
2999  * @controller_handle:   controller
3000  * @driver_image_handle: driver
3001  * @remain_device_path:  remaining path
3002  *
3003  * Return: status code
3004  */
3005 static efi_status_t efi_connect_single_controller(
3006                         efi_handle_t controller_handle,
3007                         efi_handle_t *driver_image_handle,
3008                         struct efi_device_path *remain_device_path)
3009 {
3010         efi_handle_t *buffer;
3011         size_t count;
3012         size_t i;
3013         efi_status_t r;
3014         size_t connected = 0;
3015
3016         /* Get buffer with all handles with driver binding protocol */
3017         r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3018                                               &efi_guid_driver_binding_protocol,
3019                                               NULL, &count, &buffer));
3020         if (r != EFI_SUCCESS)
3021                 return r;
3022
3023         /*  Context Override */
3024         if (driver_image_handle) {
3025                 for (; *driver_image_handle; ++driver_image_handle) {
3026                         for (i = 0; i < count; ++i) {
3027                                 if (buffer[i] == *driver_image_handle) {
3028                                         buffer[i] = NULL;
3029                                         r = efi_bind_controller(
3030                                                         controller_handle,
3031                                                         *driver_image_handle,
3032                                                         remain_device_path);
3033                                         /*
3034                                          * For drivers that do not support the
3035                                          * controller or are already connected
3036                                          * we receive an error code here.
3037                                          */
3038                                         if (r == EFI_SUCCESS)
3039                                                 ++connected;
3040                                 }
3041                         }
3042                 }
3043         }
3044
3045         /*
3046          * TODO: Some overrides are not yet implemented:
3047          * - Platform Driver Override
3048          * - Driver Family Override Search
3049          * - Bus Specific Driver Override
3050          */
3051
3052         /* Driver Binding Search */
3053         for (i = 0; i < count; ++i) {
3054                 if (buffer[i]) {
3055                         r = efi_bind_controller(controller_handle,
3056                                                 buffer[i],
3057                                                 remain_device_path);
3058                         if (r == EFI_SUCCESS)
3059                                 ++connected;
3060                 }
3061         }
3062
3063         efi_free_pool(buffer);
3064         if (!connected)
3065                 return EFI_NOT_FOUND;
3066         return EFI_SUCCESS;
3067 }
3068
3069 /**
3070  * efi_connect_controller() - connect a controller to a driver
3071  * @controller_handle:   handle of the controller
3072  * @driver_image_handle: handle of the driver
3073  * @remain_device_path:  device path of a child controller
3074  * @recursive:           true to connect all child controllers
3075  *
3076  * This function implements the ConnectController service.
3077  *
3078  * See the Unified Extensible Firmware Interface (UEFI) specification for
3079  * details.
3080  *
3081  * First all driver binding protocol handles are tried for binding drivers.
3082  * Afterwards all handles that have opened a protocol of the controller
3083  * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3084  *
3085  * Return: status code
3086  */
3087 static efi_status_t EFIAPI efi_connect_controller(
3088                         efi_handle_t controller_handle,
3089                         efi_handle_t *driver_image_handle,
3090                         struct efi_device_path *remain_device_path,
3091                         bool recursive)
3092 {
3093         efi_status_t r;
3094         efi_status_t ret = EFI_NOT_FOUND;
3095         struct efi_object *efiobj;
3096
3097         EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3098                   remain_device_path, recursive);
3099
3100         efiobj = efi_search_obj(controller_handle);
3101         if (!efiobj) {
3102                 ret = EFI_INVALID_PARAMETER;
3103                 goto out;
3104         }
3105
3106         r = efi_connect_single_controller(controller_handle,
3107                                           driver_image_handle,
3108                                           remain_device_path);
3109         if (r == EFI_SUCCESS)
3110                 ret = EFI_SUCCESS;
3111         if (recursive) {
3112                 struct efi_handler *handler;
3113                 struct efi_open_protocol_info_item *item;
3114
3115                 list_for_each_entry(handler, &efiobj->protocols, link) {
3116                         list_for_each_entry(item, &handler->open_infos, link) {
3117                                 if (item->info.attributes &
3118                                     EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3119                                         r = EFI_CALL(efi_connect_controller(
3120                                                 item->info.controller_handle,
3121                                                 driver_image_handle,
3122                                                 remain_device_path,
3123                                                 recursive));
3124                                         if (r == EFI_SUCCESS)
3125                                                 ret = EFI_SUCCESS;
3126                                 }
3127                         }
3128                 }
3129         }
3130         /*  Check for child controller specified by end node */
3131         if (ret != EFI_SUCCESS && remain_device_path &&
3132             remain_device_path->type == DEVICE_PATH_TYPE_END)
3133                 ret = EFI_SUCCESS;
3134 out:
3135         return EFI_EXIT(ret);
3136 }
3137
3138 /**
3139  * efi_reinstall_protocol_interface() - reinstall protocol interface
3140  * @handle:        handle on which the protocol shall be reinstalled
3141  * @protocol:      GUID of the protocol to be installed
3142  * @old_interface: interface to be removed
3143  * @new_interface: interface to be installed
3144  *
3145  * This function implements the ReinstallProtocolInterface service.
3146  *
3147  * See the Unified Extensible Firmware Interface (UEFI) specification for
3148  * details.
3149  *
3150  * The old interface is uninstalled. The new interface is installed.
3151  * Drivers are connected.
3152  *
3153  * Return: status code
3154  */
3155 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3156                         efi_handle_t handle, const efi_guid_t *protocol,
3157                         void *old_interface, void *new_interface)
3158 {
3159         efi_status_t ret;
3160
3161         EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3162                   new_interface);
3163
3164         /* Uninstall protocol but do not delete handle */
3165         ret = efi_uninstall_protocol(handle, protocol, old_interface);
3166         if (ret != EFI_SUCCESS)
3167                 goto out;
3168
3169         /* Install the new protocol */
3170         ret = efi_add_protocol(handle, protocol, new_interface);
3171         /*
3172          * The UEFI spec does not specify what should happen to the handle
3173          * if in case of an error no protocol interface remains on the handle.
3174          * So let's do nothing here.
3175          */
3176         if (ret != EFI_SUCCESS)
3177                 goto out;
3178         /*
3179          * The returned status code has to be ignored.
3180          * Do not create an error if no suitable driver for the handle exists.
3181          */
3182         EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3183 out:
3184         return EFI_EXIT(ret);
3185 }
3186
3187 /**
3188  * efi_get_child_controllers() - get all child controllers associated to a driver
3189  * @efiobj:              handle of the controller
3190  * @driver_handle:       handle of the driver
3191  * @number_of_children:  number of child controllers
3192  * @child_handle_buffer: handles of the the child controllers
3193  *
3194  * The allocated buffer has to be freed with free().
3195  *
3196  * Return: status code
3197  */
3198 static efi_status_t efi_get_child_controllers(
3199                                 struct efi_object *efiobj,
3200                                 efi_handle_t driver_handle,
3201                                 efi_uintn_t *number_of_children,
3202                                 efi_handle_t **child_handle_buffer)
3203 {
3204         struct efi_handler *handler;
3205         struct efi_open_protocol_info_item *item;
3206         efi_uintn_t count = 0, i;
3207         bool duplicate;
3208
3209         /* Count all child controller associations */
3210         list_for_each_entry(handler, &efiobj->protocols, link) {
3211                 list_for_each_entry(item, &handler->open_infos, link) {
3212                         if (item->info.agent_handle == driver_handle &&
3213                             item->info.attributes &
3214                             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3215                                 ++count;
3216                 }
3217         }
3218         /*
3219          * Create buffer. In case of duplicate child controller assignments
3220          * the buffer will be too large. But that does not harm.
3221          */
3222         *number_of_children = 0;
3223         *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3224         if (!*child_handle_buffer)
3225                 return EFI_OUT_OF_RESOURCES;
3226         /* Copy unique child handles */
3227         list_for_each_entry(handler, &efiobj->protocols, link) {
3228                 list_for_each_entry(item, &handler->open_infos, link) {
3229                         if (item->info.agent_handle == driver_handle &&
3230                             item->info.attributes &
3231                             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3232                                 /* Check this is a new child controller */
3233                                 duplicate = false;
3234                                 for (i = 0; i < *number_of_children; ++i) {
3235                                         if ((*child_handle_buffer)[i] ==
3236                                             item->info.controller_handle)
3237                                                 duplicate = true;
3238                                 }
3239                                 /* Copy handle to buffer */
3240                                 if (!duplicate) {
3241                                         i = (*number_of_children)++;
3242                                         (*child_handle_buffer)[i] =
3243                                                 item->info.controller_handle;
3244                                 }
3245                         }
3246                 }
3247         }
3248         return EFI_SUCCESS;
3249 }
3250
3251 /**
3252  * efi_disconnect_controller() - disconnect a controller from a driver
3253  * @controller_handle:   handle of the controller
3254  * @driver_image_handle: handle of the driver
3255  * @child_handle:        handle of the child to destroy
3256  *
3257  * This function implements the DisconnectController service.
3258  *
3259  * See the Unified Extensible Firmware Interface (UEFI) specification for
3260  * details.
3261  *
3262  * Return: status code
3263  */
3264 static efi_status_t EFIAPI efi_disconnect_controller(
3265                                 efi_handle_t controller_handle,
3266                                 efi_handle_t driver_image_handle,
3267                                 efi_handle_t child_handle)
3268 {
3269         struct efi_driver_binding_protocol *binding_protocol;
3270         efi_handle_t *child_handle_buffer = NULL;
3271         size_t number_of_children = 0;
3272         efi_status_t r;
3273         size_t stop_count = 0;
3274         struct efi_object *efiobj;
3275
3276         EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3277                   child_handle);
3278
3279         efiobj = efi_search_obj(controller_handle);
3280         if (!efiobj) {
3281                 r = EFI_INVALID_PARAMETER;
3282                 goto out;
3283         }
3284
3285         if (child_handle && !efi_search_obj(child_handle)) {
3286                 r = EFI_INVALID_PARAMETER;
3287                 goto out;
3288         }
3289
3290         /* If no driver handle is supplied, disconnect all drivers */
3291         if (!driver_image_handle) {
3292                 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3293                 goto out;
3294         }
3295
3296         /* Create list of child handles */
3297         if (child_handle) {
3298                 number_of_children = 1;
3299                 child_handle_buffer = &child_handle;
3300         } else {
3301                 efi_get_child_controllers(efiobj,
3302                                           driver_image_handle,
3303                                           &number_of_children,
3304                                           &child_handle_buffer);
3305         }
3306
3307         /* Get the driver binding protocol */
3308         r = EFI_CALL(efi_open_protocol(driver_image_handle,
3309                                        &efi_guid_driver_binding_protocol,
3310                                        (void **)&binding_protocol,
3311                                        driver_image_handle, NULL,
3312                                        EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3313         if (r != EFI_SUCCESS)
3314                 goto out;
3315         /* Remove the children */
3316         if (number_of_children) {
3317                 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3318                                                     controller_handle,
3319                                                     number_of_children,
3320                                                     child_handle_buffer));
3321                 if (r == EFI_SUCCESS)
3322                         ++stop_count;
3323         }
3324         /* Remove the driver */
3325         if (!child_handle)
3326                 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3327                                                     controller_handle,
3328                                                     0, NULL));
3329         if (r == EFI_SUCCESS)
3330                 ++stop_count;
3331         EFI_CALL(efi_close_protocol(driver_image_handle,
3332                                     &efi_guid_driver_binding_protocol,
3333                                     driver_image_handle, NULL));
3334
3335         if (stop_count)
3336                 r = EFI_SUCCESS;
3337         else
3338                 r = EFI_NOT_FOUND;
3339 out:
3340         if (!child_handle)
3341                 free(child_handle_buffer);
3342         return EFI_EXIT(r);
3343 }
3344
3345 static struct efi_boot_services efi_boot_services = {
3346         .hdr = {
3347                 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3348                 .revision = EFI_SPECIFICATION_VERSION,
3349                 .headersize = sizeof(struct efi_boot_services),
3350         },
3351         .raise_tpl = efi_raise_tpl,
3352         .restore_tpl = efi_restore_tpl,
3353         .allocate_pages = efi_allocate_pages_ext,
3354         .free_pages = efi_free_pages_ext,
3355         .get_memory_map = efi_get_memory_map_ext,
3356         .allocate_pool = efi_allocate_pool_ext,
3357         .free_pool = efi_free_pool_ext,
3358         .create_event = efi_create_event_ext,
3359         .set_timer = efi_set_timer_ext,
3360         .wait_for_event = efi_wait_for_event,
3361         .signal_event = efi_signal_event_ext,
3362         .close_event = efi_close_event,
3363         .check_event = efi_check_event,
3364         .install_protocol_interface = efi_install_protocol_interface,
3365         .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3366         .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3367         .handle_protocol = efi_handle_protocol,
3368         .reserved = NULL,
3369         .register_protocol_notify = efi_register_protocol_notify,
3370         .locate_handle = efi_locate_handle_ext,
3371         .locate_device_path = efi_locate_device_path,
3372         .install_configuration_table = efi_install_configuration_table_ext,
3373         .load_image = efi_load_image,
3374         .start_image = efi_start_image,
3375         .exit = efi_exit,
3376         .unload_image = efi_unload_image,
3377         .exit_boot_services = efi_exit_boot_services,
3378         .get_next_monotonic_count = efi_get_next_monotonic_count,
3379         .stall = efi_stall,
3380         .set_watchdog_timer = efi_set_watchdog_timer,
3381         .connect_controller = efi_connect_controller,
3382         .disconnect_controller = efi_disconnect_controller,
3383         .open_protocol = efi_open_protocol,
3384         .close_protocol = efi_close_protocol,
3385         .open_protocol_information = efi_open_protocol_information,
3386         .protocols_per_handle = efi_protocols_per_handle,
3387         .locate_handle_buffer = efi_locate_handle_buffer,
3388         .locate_protocol = efi_locate_protocol,
3389         .install_multiple_protocol_interfaces =
3390                         efi_install_multiple_protocol_interfaces,
3391         .uninstall_multiple_protocol_interfaces =
3392                         efi_uninstall_multiple_protocol_interfaces,
3393         .calculate_crc32 = efi_calculate_crc32,
3394         .copy_mem = efi_copy_mem,
3395         .set_mem = efi_set_mem,
3396         .create_event_ex = efi_create_event_ex,
3397 };
3398
3399 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3400
3401 struct efi_system_table __efi_runtime_data systab = {
3402         .hdr = {
3403                 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3404                 .revision = EFI_SPECIFICATION_VERSION,
3405                 .headersize = sizeof(struct efi_system_table),
3406         },
3407         .fw_vendor = firmware_vendor,
3408         .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3409         .con_in = (void *)&efi_con_in,
3410         .con_out = (void *)&efi_con_out,
3411         .std_err = (void *)&efi_con_out,
3412         .runtime = (void *)&efi_runtime_services,
3413         .boottime = (void *)&efi_boot_services,
3414         .nr_tables = 0,
3415         .tables = NULL,
3416 };
3417
3418 /**
3419  * efi_initialize_system_table() - Initialize system table
3420  *
3421  * Return:      status code
3422  */
3423 efi_status_t efi_initialize_system_table(void)
3424 {
3425         efi_status_t ret;
3426
3427         /* Allocate configuration table array */
3428         ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3429                                 EFI_MAX_CONFIGURATION_TABLES *
3430                                 sizeof(struct efi_configuration_table),
3431                                 (void **)&systab.tables);
3432
3433         /* Set CRC32 field in table headers */
3434         efi_update_table_header_crc32(&systab.hdr);
3435         efi_update_table_header_crc32(&efi_runtime_services.hdr);
3436         efi_update_table_header_crc32(&efi_boot_services.hdr);
3437
3438         return ret;
3439 }