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