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