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