Skip to content
Snippets Groups Projects
efi_boottime.c 79.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     *  EFI application boot time services
     *
     *  Copyright (c) 2016 Alexander Graf
     *
     *  SPDX-License-Identifier:     GPL-2.0+
     */
    
    #include <common.h>
    
    #include <div64.h>
    
    #include <efi_loader.h>
    
    #include <environment.h>
    
    #include <malloc.h>
    #include <asm/global_data.h>
    
    #include <u-boot/crc.h>
    #include <bootm.h>
    #include <inttypes.h>
    #include <watchdog.h>
    
    DECLARE_GLOBAL_DATA_PTR;
    
    
    /* Task priority level */
    
    static efi_uintn_t efi_tpl = TPL_APPLICATION;
    
    /* This list contains all the EFI objects our payload has access to */
    LIST_HEAD(efi_obj_list);
    
    
    /* List of all events */
    
    LIST_HEAD(efi_events);
    
    /*
     * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
     * we need to do trickery with caches. Since we don't want to break the EFI
     * aware boot path, only apply hacks when loading exiting directly (breaking
     * direct Linux EFI booting along the way - oh well).
     */
    static bool efi_is_direct_boot = true;
    
    /*
     * EFI can pass arbitrary additional "tables" containing vendor specific
     * information to the payload. One such table is the FDT table which contains
     * a pointer to a flattened device tree blob.
     *
     * In most cases we want to pass an FDT to the payload, so reserve one slot of
     * config table space for it. The pointer gets populated by do_bootefi_exec().
     */
    
    static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
    
    #ifdef CONFIG_ARM
    
    /*
     * The "gd" pointer lives in a register on ARM and AArch64 that we declare
     * fixed when compiling U-Boot. However, the payload does not know about that
     * restriction so we need to manually swap its and our view of that register on
     * EFI callback entry/exit.
     */
    static volatile void *efi_gd, *app_gd;
    
    static int nesting_level;
    
    /* GUID of the device tree table */
    const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
    
    /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
    const efi_guid_t efi_guid_driver_binding_protocol =
    			EFI_DRIVER_BINDING_PROTOCOL_GUID;
    
    /* event group ExitBootServices() invoked */
    const efi_guid_t efi_guid_event_group_exit_boot_services =
    			EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
    /* event group SetVirtualAddressMap() invoked */
    const efi_guid_t efi_guid_event_group_virtual_address_change =
    			EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
    /* event group memory map changed */
    const efi_guid_t efi_guid_event_group_memory_map_change =
    			EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
    /* event group boot manager about to boot */
    const efi_guid_t efi_guid_event_group_ready_to_boot =
    			EFI_EVENT_GROUP_READY_TO_BOOT;
    /* event group ResetSystem() invoked (before ExitBootServices) */
    const efi_guid_t efi_guid_event_group_reset_system =
    			EFI_EVENT_GROUP_RESET_SYSTEM;
    
    
    static efi_status_t EFIAPI efi_disconnect_controller(
    					efi_handle_t controller_handle,
    					efi_handle_t driver_image_handle,
    					efi_handle_t child_handle);
    
    /* Called on every callback entry */
    int __efi_entry_check(void)
    {
    	int ret = entry_count++ == 0;
    #ifdef CONFIG_ARM
    	assert(efi_gd);
    	app_gd = gd;
    	gd = efi_gd;
    #endif
    	return ret;
    }
    
    /* Called on every callback exit */
    int __efi_exit_check(void)
    {
    	int ret = --entry_count == 0;
    #ifdef CONFIG_ARM
    	gd = app_gd;
    #endif
    	return ret;
    }
    
    
    /* Called from do_bootefi_exec() */
    void efi_save_gd(void)
    {
    
    #ifdef CONFIG_ARM
    
    	efi_gd = gd;
    
    /*
     * Special case handler for error/abort that just forces things back
     * to u-boot world so we can dump out an abort msg, without any care
     * about returning back to UEFI world.
     */
    
    void efi_restore_gd(void)
    {
    
    #ifdef CONFIG_ARM
    
    	/* Only restore if we're already in EFI context */
    	if (!efi_gd)
    		return;
    	gd = efi_gd;
    
     * Return a string for indenting with two spaces per level. A maximum of ten
     * indent levels is supported. Higher indent levels will be truncated.
     *
     * @level	indent level
     * @return	indent string
    
     */
    static const char *indent_string(int level)
    {
    	const char *indent = "                    ";
    	const int max = strlen(indent);
    
    	level = min(max, level * 2);
    	return &indent[max - level];
    }
    
    
    const char *__efi_nesting(void)
    {
    	return indent_string(nesting_level);
    }
    
    
    const char *__efi_nesting_inc(void)
    {
    	return indent_string(nesting_level++);
    }
    
    const char *__efi_nesting_dec(void)
    {
    	return indent_string(--nesting_level);
    }
    
    
    /*
     * Queue an EFI event.
     *
     * This function queues the notification function of the event for future
     * execution.
     *
     * The notification function is called if the task priority level of the
     * event is higher than the current task priority level.
     *
     * For the SignalEvent service see efi_signal_event_ext.
     *
     * @event	event to signal
    
     * @check_tpl	check the TPL level
    
    static void efi_queue_event(struct efi_event *event, bool check_tpl)
    
    		event->is_queued = true;
    
    		if (check_tpl && efi_tpl >= event->notify_tpl)
    
    		EFI_CALL_VOID(event->notify_function(event,
    						     event->notify_context));
    
    	event->is_queued = false;
    
    /*
     * Signal an EFI event.
     *
     * This function signals an event. If the event belongs to an event group
     * all events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
     * their notification function is queued.
     *
     * For the SignalEvent service see efi_signal_event_ext.
     *
     * @event	event to signal
     * @check_tpl	check the TPL level
     */
    void efi_signal_event(struct efi_event *event, bool check_tpl)
    {
    	if (event->group) {
    		struct efi_event *evt;
    
    		/*
    		 * The signaled state has to set before executing any
    		 * notification function
    		 */
    		list_for_each_entry(evt, &efi_events, link) {
    			if (!evt->group || guidcmp(evt->group, event->group))
    				continue;
    			if (evt->is_signaled)
    				continue;
    			evt->is_signaled = true;
    			if (evt->type & EVT_NOTIFY_SIGNAL &&
    			    evt->notify_function)
    				evt->is_queued = true;
    		}
    		list_for_each_entry(evt, &efi_events, link) {
    			if (!evt->group || guidcmp(evt->group, event->group))
    				continue;
    			if (evt->is_queued)
    				efi_queue_event(evt, check_tpl);
    		}
    	} else if (!event->is_signaled) {
    		event->is_signaled = true;
    		if (event->type & EVT_NOTIFY_SIGNAL)
    			efi_queue_event(event, check_tpl);
    	}
    }
    
    
    /*
     * Raise the task priority level.
     *
     * This function implements the RaiseTpl service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @new_tpl	new value of the task priority level
     * @return	old value of the task priority level
     */
    
    static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
    
    	efi_uintn_t old_tpl = efi_tpl;
    
    	EFI_ENTRY("0x%zx", new_tpl);
    
    
    	if (new_tpl < efi_tpl)
    		debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
    	efi_tpl = new_tpl;
    	if (efi_tpl > TPL_HIGH_LEVEL)
    		efi_tpl = TPL_HIGH_LEVEL;
    
    	EFI_EXIT(EFI_SUCCESS);
    	return old_tpl;
    
    /*
     * Lower the task priority level.
     *
     * This function implements the RestoreTpl service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @old_tpl	value of the task priority level to be restored
     */
    
    static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
    
    	EFI_ENTRY("0x%zx", old_tpl);
    
    
    	if (old_tpl > efi_tpl)
    		debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
    	efi_tpl = old_tpl;
    	if (efi_tpl > TPL_HIGH_LEVEL)
    		efi_tpl = TPL_HIGH_LEVEL;
    
    
    	/*
    	 * Lowering the TPL may have made queued events eligible for execution.
    	 */
    	efi_timer_check();
    
    
    /*
     * Allocate memory pages.
     *
     * This function implements the AllocatePages service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @type		type of allocation to be performed
     * @memory_type		usage type of the allocated memory
     * @pages		number of pages to be allocated
     * @memory		allocated memory
     * @return		status code
     */
    
    static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
    
    						  uint64_t *memory)
    
    	EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
    
    	r = efi_allocate_pages(type, memory_type, pages, memory);
    	return EFI_EXIT(r);
    }
    
    
    /*
     * Free memory pages.
     *
     * This function implements the FreePages service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @memory	start of the memory area to be freed
     * @pages	number of pages to be freed
     * @return	status code
     */
    
    static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
    
    	EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
    
    	r = efi_free_pages(memory, pages);
    	return EFI_EXIT(r);
    }
    
    
    /*
     * Get map describing memory usage.
     *
     * This function implements the GetMemoryMap service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @memory_map_size	on entry the size, in bytes, of the memory map buffer,
     *			on exit the size of the copied memory map
     * @memory_map		buffer to which the memory map is written
     * @map_key		key for the memory map
     * @descriptor_size	size of an individual memory descriptor
     * @descriptor_version	version number of the memory descriptor structure
     * @return		status code
     */
    
    static efi_status_t EFIAPI efi_get_memory_map_ext(
    
    					struct efi_mem_desc *memory_map,
    
    					efi_uintn_t *map_key,
    					efi_uintn_t *descriptor_size,
    
    					uint32_t *descriptor_version)
    
    {
    	efi_status_t r;
    
    	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
    		  map_key, descriptor_size, descriptor_version);
    	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
    			       descriptor_size, descriptor_version);
    	return EFI_EXIT(r);
    }
    
    
    /*
     * Allocate memory from pool.
     *
     * This function implements the AllocatePool service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @pool_type	type of the pool from which memory is to be allocated
     * @size	number of bytes to be allocated
     * @buffer	allocated memory
     * @return	status code
     */
    
    static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
    
    	EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
    
    	r = efi_allocate_pool(pool_type, size, buffer);
    
    	return EFI_EXIT(r);
    
    /*
     * Free memory from pool.
     *
     * This function implements the FreePool service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @buffer	start of memory to be freed
     * @return	status code
     */
    
    static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
    
    	efi_status_t r;
    
    	EFI_ENTRY("%p", buffer);
    
    	return EFI_EXIT(r);
    
    /*
     * Add a new object to the object list.
     *
     * The protocols list is initialized.
     * The object handle is set.
     *
     * @obj	object to be added
     */
    void efi_add_handle(struct efi_object *obj)
    {
    	if (!obj)
    		return;
    	INIT_LIST_HEAD(&obj->protocols);
    	obj->handle = obj;
    	list_add_tail(&obj->link, &efi_obj_list);
    }
    
    
    /*
     * Create handle.
     *
     * @handle	new handle
     * @return	status code
     */
    
    efi_status_t efi_create_handle(efi_handle_t *handle)
    
    {
    	struct efi_object *obj;
    	efi_status_t r;
    
    	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
    			      sizeof(struct efi_object),
    			      (void **)&obj);
    	if (r != EFI_SUCCESS)
    		return r;
    
    	efi_add_handle(obj);
    	*handle = obj->handle;
    
    /*
     * Find a protocol on a handle.
     *
     * @handle		handle
     * @protocol_guid	GUID of the protocol
     * @handler		reference to the protocol
     * @return		status code
     */
    
    efi_status_t efi_search_protocol(const efi_handle_t handle,
    
    				 const efi_guid_t *protocol_guid,
    				 struct efi_handler **handler)
    {
    	struct efi_object *efiobj;
    	struct list_head *lhandle;
    
    	if (!handle || !protocol_guid)
    		return EFI_INVALID_PARAMETER;
    	efiobj = efi_search_obj(handle);
    	if (!efiobj)
    		return EFI_INVALID_PARAMETER;
    	list_for_each(lhandle, &efiobj->protocols) {
    		struct efi_handler *protocol;
    
    		protocol = list_entry(lhandle, struct efi_handler, link);
    		if (!guidcmp(protocol->guid, protocol_guid)) {
    			if (handler)
    				*handler = protocol;
    			return EFI_SUCCESS;
    		}
    	}
    	return EFI_NOT_FOUND;
    }
    
    /*
     * Delete protocol from a handle.
     *
     * @handle			handle from which the protocol shall be deleted
     * @protocol			GUID of the protocol to be deleted
     * @protocol_interface		interface of the protocol implementation
     * @return			status code
     */
    
    efi_status_t efi_remove_protocol(const efi_handle_t handle,
    				 const efi_guid_t *protocol,
    
    				 void *protocol_interface)
    {
    	struct efi_handler *handler;
    	efi_status_t ret;
    
    	ret = efi_search_protocol(handle, protocol, &handler);
    	if (ret != EFI_SUCCESS)
    		return ret;
    	if (guidcmp(handler->guid, protocol))
    		return EFI_INVALID_PARAMETER;
    	list_del(&handler->link);
    	free(handler);
    	return EFI_SUCCESS;
    }
    
    /*
     * Delete all protocols from a handle.
     *
     * @handle	handle from which the protocols shall be deleted
     * @return	status code
     */
    
    efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
    
    {
    	struct efi_object *efiobj;
    
    	struct efi_handler *protocol;
    	struct efi_handler *pos;
    
    
    	efiobj = efi_search_obj(handle);
    	if (!efiobj)
    		return EFI_INVALID_PARAMETER;
    
    	list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
    
    		efi_status_t ret;
    
    		ret = efi_remove_protocol(handle, protocol->guid,
    					  protocol->protocol_interface);
    		if (ret != EFI_SUCCESS)
    			return ret;
    	}
    	return EFI_SUCCESS;
    }
    
    /*
     * Delete handle.
     *
     * @handle	handle to delete
     */
    void efi_delete_handle(struct efi_object *obj)
    {
    	if (!obj)
    		return;
    	efi_remove_all_protocols(obj->handle);
    	list_del(&obj->link);
    	free(obj);
    }
    
    
     * Check if a pointer is a valid event.
     *
     * @event		pointer to check
     * @return		status code
    
    static efi_status_t efi_is_event(const struct efi_event *event)
    {
    	const struct efi_event *evt;
    
    	if (!event)
    		return EFI_INVALID_PARAMETER;
    	list_for_each_entry(evt, &efi_events, link) {
    		if (evt == event)
    			return EFI_SUCCESS;
    	}
    	return EFI_INVALID_PARAMETER;
    }
    
    /*
     * Create an event.
     *
     * This function is used inside U-Boot code to create an event.
     *
     * For the API function implementing the CreateEvent service see
     * efi_create_event_ext.
     *
     * @type		type of the event to create
     * @notify_tpl		task priority level of the event
     * @notify_function	notification function of the event
     * @notify_context	pointer passed to the notification function
     * @event		created event
     * @return		status code
     */
    
    efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
    
    			      void (EFIAPI *notify_function) (
    
    					struct efi_event *event,
    					void *context),
    
    			      void *notify_context, efi_guid_t *group,
    			      struct efi_event **event)
    
    	struct efi_event *evt;
    
    	if (event == NULL)
    
    		return EFI_INVALID_PARAMETER;
    
    
    	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
    
    		return EFI_INVALID_PARAMETER;
    
    	if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
    
    	    notify_function == NULL)
    
    		return EFI_INVALID_PARAMETER;
    
    	evt = calloc(1, sizeof(struct efi_event));
    	if (!evt)
    		return EFI_OUT_OF_RESOURCES;
    	evt->type = type;
    	evt->notify_tpl = notify_tpl;
    	evt->notify_function = notify_function;
    	evt->notify_context = notify_context;
    
    	evt->group = group;
    
    	/* Disable timers on bootup */
    	evt->trigger_next = -1ULL;
    	evt->is_queued = false;
    	evt->is_signaled = false;
    	list_add_tail(&evt->link, &efi_events);
    	*event = evt;
    	return EFI_SUCCESS;
    
    /*
     * Create an event in a group.
     *
     * This function implements the CreateEventEx service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     * TODO: Support event groups
     *
     * @type		type of the event to create
     * @notify_tpl		task priority level of the event
     * @notify_function	notification function of the event
     * @notify_context	pointer passed to the notification function
     * @event		created event
     * @event_group		event group
     * @return		status code
     */
    efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
    					void (EFIAPI *notify_function) (
    							struct efi_event *event,
    							void *context),
    					void *notify_context,
    					efi_guid_t *event_group,
    					struct efi_event **event)
    {
    	EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
    		  notify_context, event_group);
    	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
    
    					 notify_context, event_group, event));
    
    /*
     * Create an event.
     *
     * This function implements the CreateEvent service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @type		type of the event to create
     * @notify_tpl		task priority level of the event
     * @notify_function	notification function of the event
     * @notify_context	pointer passed to the notification function
     * @event		created event
     * @return		status code
     */
    
    static efi_status_t EFIAPI efi_create_event_ext(
    
    			uint32_t type, efi_uintn_t notify_tpl,
    
    			void (EFIAPI *notify_function) (
    					struct efi_event *event,
    					void *context),
    			void *notify_context, struct efi_event **event)
    {
    	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
    		  notify_context);
    	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
    
    					 notify_context, NULL, event));
    
     * Check if a timer event has occurred or a queued notification function should
     * be called.
     *
    
     * Our timers have to work without interrupts, so we check whenever keyboard
    
     * input or disk accesses happen if enough time elapsed for them to fire.
    
     */
    void efi_timer_check(void)
    {
    
    	struct efi_event *evt;
    
    	u64 now = timer_get_us();
    
    
    	list_for_each_entry(evt, &efi_events, link) {
    		if (evt->is_queued)
    
    			efi_queue_event(evt, true);
    
    		if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
    
    		switch (evt->trigger_type) {
    
    			evt->trigger_type = EFI_TIMER_STOP;
    
    			evt->trigger_next += evt->trigger_time;
    
    		evt->is_signaled = false;
    
    		efi_signal_event(evt, true);
    
    /*
     * Set the trigger time for a timer event or stop the event.
     *
     * This is the function for internal usage in U-Boot. For the API function
     * implementing the SetTimer service see efi_set_timer_ext.
     *
     * @event		event for which the timer is set
     * @type		type of the timer
     * @trigger_time	trigger period in multiples of 100ns
     * @return		status code
     */
    
    efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
    
    			   uint64_t trigger_time)
    
    	/* Check that the event is valid */
    	if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
    		return EFI_INVALID_PARAMETER;
    
    	/*
    	 * The parameter defines a multiple of 100ns.
    	 * We use multiples of 1000ns. So divide by 10.
    	 */
    
    	do_div(trigger_time, 10);
    
    	switch (type) {
    	case EFI_TIMER_STOP:
    		event->trigger_next = -1ULL;
    		break;
    	case EFI_TIMER_PERIODIC:
    	case EFI_TIMER_RELATIVE:
    		event->trigger_next = timer_get_us() + trigger_time;
    		break;
    	default:
    		return EFI_INVALID_PARAMETER;
    
    	event->trigger_type = type;
    	event->trigger_time = trigger_time;
    	event->is_signaled = false;
    	return EFI_SUCCESS;
    
    /*
     * Set the trigger time for a timer event or stop the event.
     *
     * This function implements the SetTimer service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @event		event for which the timer is set
     * @type		type of the timer
     * @trigger_time	trigger period in multiples of 100ns
     * @return		status code
     */
    
    static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
    					     enum efi_timer_delay type,
    					     uint64_t trigger_time)
    
    	EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
    
    	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
    
    /*
     * Wait for events to be signaled.
     *
     * This function implements the WaitForEvent service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @num_events	number of events to be waited for
     * @events	events to be waited for
     * @index	index of the event that was signaled
     * @return	status code
     */
    
    static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
    
    	EFI_ENTRY("%zd, %p, %p", num_events, event, index);
    
    	/* Check parameters */
    	if (!num_events || !event)
    		return EFI_EXIT(EFI_INVALID_PARAMETER);
    
    	/* Check TPL */
    	if (efi_tpl != TPL_APPLICATION)
    		return EFI_EXIT(EFI_UNSUPPORTED);
    
    	for (i = 0; i < num_events; ++i) {
    
    		if (efi_is_event(event[i]) != EFI_SUCCESS)
    			return EFI_EXIT(EFI_INVALID_PARAMETER);
    
    		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
    			return EFI_EXIT(EFI_INVALID_PARAMETER);
    
    		if (!event[i]->is_signaled)
    
    			efi_queue_event(event[i], true);
    
    	}
    
    	/* Wait for signal */
    	for (;;) {
    		for (i = 0; i < num_events; ++i) {
    
    			if (event[i]->is_signaled)
    
    				goto out;
    		}
    		/* Allow events to occur. */
    		efi_timer_check();
    	}
    
    out:
    	/*
    	 * Reset the signal which is passed to the caller to allow periodic
    	 * events to occur.
    	 */
    
    	event[i]->is_signaled = false;
    
    
    	return EFI_EXIT(EFI_SUCCESS);
    }
    
    
    /*
     * Signal an EFI event.
     *
     * This function implements the SignalEvent service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * This functions sets the signaled state of the event and queues the
     * notification function for execution.
     *
     * @event	event to signal
    
    static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
    
    {
    	EFI_ENTRY("%p", event);
    
    	if (efi_is_event(event) != EFI_SUCCESS)
    		return EFI_EXIT(EFI_INVALID_PARAMETER);
    
    	efi_signal_event(event, true);
    
    	return EFI_EXIT(EFI_SUCCESS);
    }
    
    
    /*
     * Close an EFI event.
     *
     * This function implements the CloseEvent service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
     * @event	event to close
     * @return	status code
     */
    
    static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
    
    {
    	EFI_ENTRY("%p", event);
    
    	if (efi_is_event(event) != EFI_SUCCESS)
    		return EFI_EXIT(EFI_INVALID_PARAMETER);
    	list_del(&event->link);
    	free(event);
    	return EFI_EXIT(EFI_SUCCESS);
    
    /*
     * Check if an event is signaled.
     *
     * This function implements the CheckEvent service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
     *
    
     * If an event is not signaled yet, the notification function is queued.
     * The signaled state is cleared.
    
     *
     * @event	event to check
     * @return	status code
     */
    
    static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
    
    {
    	EFI_ENTRY("%p", event);
    
    	if (efi_is_event(event) != EFI_SUCCESS ||
    	    event->type & EVT_NOTIFY_SIGNAL)
    		return EFI_EXIT(EFI_INVALID_PARAMETER);
    	if (!event->is_signaled)
    
    		efi_queue_event(event, true);
    
    	if (event->is_signaled) {
    		event->is_signaled = false;
    		return EFI_EXIT(EFI_SUCCESS);
    
    	return EFI_EXIT(EFI_NOT_READY);
    
    /*
     * Find the internal EFI object for a handle.
     *
     * @handle	handle to find
     * @return	EFI object
     */
    
    struct efi_object *efi_search_obj(const efi_handle_t handle)
    
    	struct efi_object *efiobj;
    
    	list_for_each_entry(efiobj, &efi_obj_list, link) {
    
    		if (efiobj->handle == handle)
    			return efiobj;
    	}
    
    	return NULL;
    }
    
    
    /*
     * Create open protocol info entry and add it to a protocol.
     *
     * @handler	handler of a protocol
     * @return	open protocol info entry
     */
    static struct efi_open_protocol_info_entry *efi_create_open_info(
    			struct efi_handler *handler)
    {
    	struct efi_open_protocol_info_item *item;
    
    	item = calloc(1, sizeof(struct efi_open_protocol_info_item));
    	if (!item)
    		return NULL;
    	/* Append the item to the open protocol info list. */
    	list_add_tail(&item->link, &handler->open_infos);
    
    	return &item->info;
    }
    
    /*
     * Remove an open protocol info entry from a protocol.
     *
     * @handler	handler of a protocol
     * @return	status code
     */
    static efi_status_t efi_delete_open_info(
    			struct efi_open_protocol_info_item *item)
    {
    	list_del(&item->link);
    	free(item);
    	return EFI_SUCCESS;
    }
    
    
    /*
     * Install new protocol on a handle.
     *
     * @handle			handle on which the protocol shall be installed
     * @protocol			GUID of the protocol to be installed
     * @protocol_interface		interface of the protocol implementation
     * @return			status code
     */
    
    efi_status_t efi_add_protocol(const efi_handle_t handle,
    			      const efi_guid_t *protocol,
    
    			      void *protocol_interface)
    {
    	struct efi_object *efiobj;
    	struct efi_handler *handler;
    	efi_status_t ret;
    
    	efiobj = efi_search_obj(handle);
    	if (!efiobj)
    		return EFI_INVALID_PARAMETER;
    	ret = efi_search_protocol(handle, protocol, NULL);
    	if (ret != EFI_NOT_FOUND)
    		return EFI_INVALID_PARAMETER;
    	handler = calloc(1, sizeof(struct efi_handler));
    	if (!handler)
    		return EFI_OUT_OF_RESOURCES;
    
    	handler->guid = protocol;
    	handler->protocol_interface = protocol_interface;
    
    	INIT_LIST_HEAD(&handler->open_infos);
    
    	list_add_tail(&handler->link, &efiobj->protocols);
    
    	if (!guidcmp(&efi_guid_device_path, protocol))
    		EFI_PRINT("installed device path '%pD'\n", protocol_interface);
    
     * This function implements the InstallProtocolInterface service.
     * See the Unified Extensible Firmware Interface (UEFI) specification
     * for details.
    
     *
     * @handle			handle on which the protocol shall be installed
     * @protocol			GUID of the protocol to be installed
     * @protocol_interface_type	type of the interface to be installed,
     *				always EFI_NATIVE_INTERFACE
     * @protocol_interface		interface of the protocol implementation
     * @return			status code
     */
    
    static efi_status_t EFIAPI efi_install_protocol_interface(
    			void **handle, const efi_guid_t *protocol,
    			int protocol_interface_type, void *protocol_interface)
    
    	EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
    		  protocol_interface);