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