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