From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.5966.1667521520470320144 for ; Thu, 03 Nov 2022 17:25:20 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@linux.microsoft.com header.s=default header.b=FKK8xZ50; spf=pass (domain: linux.microsoft.com, ip: 13.77.154.182, mailfrom: mikuback@linux.microsoft.com) Received: from localhost.localdomain (unknown [47.201.8.94]) by linux.microsoft.com (Postfix) with ESMTPSA id 830C820B929F; Thu, 3 Nov 2022 17:25:19 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 830C820B929F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1667521520; bh=E4Jy1PcADw4aw7cvJ14OmihaaWzA37C4Go2sAoUt8mQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FKK8xZ50d+1ToYJa9AQS5pLjRxni947xv0ijYZhF18yYJrIsgkKzCSsaFUQul8QWz 7klloYOAo+VRV4ajMSqALtlBgn5323M2EOyZLSPy/0rvKpWBDtXub5UKu3e438cIIt QsTW+eZyHYCcMQArC4uxPCdCvzTIriYQXP5OzCN8= From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Michael D Kinney , Sean Brogan Subject: [PATCH v1 1/2] UnitTestFrameworkPkg: Add UnitTestUefiBootServicesTableLib Date: Thu, 3 Nov 2022 20:24:39 -0400 Message-Id: <20221104002440.2316-2-mikuback@linux.microsoft.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20221104002440.2316-1-mikuback@linux.microsoft.com> References: <20221104002440.2316-1-mikuback@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Kubacki This library supports a Boot Services table library implementation that allows code dependent upon UefiBootServicesTableLib to operate in an isolated execution environment such as within the context of a host-based unit test framework. The unit test should initialize the Boot Services database with any required elements (e.g. protocols, events, handles, etc.) prior to the services being invoked by code under test. It is strongly recommended to clean any global databases (e.g. protocol, event, handles, etc.) after every unit test so the tests execute in a predictable manner from a clean state. This library is being moved here from PrmPkg so it can be made more generally available to other packages and improved upon for others use. Cc: Michael D Kinney Cc: Sean Brogan Signed-off-by: Michael Kubacki --- UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLib.c | 119 ++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibEventTimer.c | 180 +++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibImage.c | 163 ++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibMemory.c | 145 ++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibMisc.c | 198 +++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibProtocol.c | 1650 ++++++++++++++++++++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibTpl.c | 43 + UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLib.h | 1042 ++++++++++++ UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLib.inf | 46 + UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLib.uni | 12 + UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUe= fiBootServicesTableLibProtocol.h | 120 ++ UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc = | 1 + UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc = | 1 + 13 files changed, 3720 insertions(+) diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLib.c b/UnitTestFrameworkPkg/Library/UnitT= estUefiBootServicesTableLib/UnitTestUefiBootServicesTableLib.c new file mode 100644 index 000000000000..a6fc13e7d21e --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLib.c @@ -0,0 +1,119 @@ +/** @file + This library supports a Boot Services table library implementation tha= t allows code dependent + upon UefiBootServicesTableLib to operate in an isolated execution envi= ronment such as within + the context of a host-based unit test framework. + + The unit test should initialize the Boot Services database with any re= quired elements + (e.g. protocols, events, handles, etc.) prior to the services being in= voked by code under test. + + It is strongly recommended to clean any global databases (e.g. protoco= l, event, handles, etc.) after + every unit test so the tests execute in a predictable manner from a cl= ean state. + + Copyright (c) Microsoft Corporation + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLib.h" + +EFI_HANDLE gImageHandle =3D NULL; +EFI_SYSTEM_TABLE *gST =3D NULL; + +STATIC EFI_BOOT_SERVICES mBootServices =3D { + { + EFI_BOOT_SERVICES_SIGNATURE, = // Signature + EFI_BOOT_SERVICES_REVISION, = // Revision + sizeof (EFI_BOOT_SERVICES), = // HeaderSize + 0, = // CRC32 + 0 = // Reserved + }, + (EFI_RAISE_TPL)UnitTestRaiseTpl, = // RaiseTPL + (EFI_RESTORE_TPL)UnitTestRestoreTpl, = // RestoreTPL + (EFI_ALLOCATE_PAGES)UnitTestAllocatePages, = // AllocatePages + (EFI_FREE_PAGES)UnitTestFreePages, = // FreePages + (EFI_GET_MEMORY_MAP)UnitTestGetMemoryMap, = // GetMemoryMap + (EFI_ALLOCATE_POOL)UnitTestAllocatePool, = // AllocatePool + (EFI_FREE_POOL)UnitTestFreePool, = // FreePool + (EFI_CREATE_EVENT)UnitTestCreateEvent, = // CreateEvent + (EFI_SET_TIMER)UnitTestSetTimer, = // SetTimer + (EFI_WAIT_FOR_EVENT)UnitTestWaitForEvent, = // WaitForEvent + (EFI_SIGNAL_EVENT)UnitTestSignalEvent, = // SignalEvent + (EFI_CLOSE_EVENT)UnitTestCloseEvent, = // CloseEvent + (EFI_CHECK_EVENT)UnitTestCheckEvent, = // CheckEvent + (EFI_INSTALL_PROTOCOL_INTERFACE)UnitTestInstallProtocolInterface, = // InstallProtocolInterface + (EFI_REINSTALL_PROTOCOL_INTERFACE)UnitTestReinstallProtocolInterface, = // ReinstallProtocolInterface + (EFI_UNINSTALL_PROTOCOL_INTERFACE)UnitTestUninstallProtocolInterface, = // UninstallProtocolInterface + (EFI_HANDLE_PROTOCOL)UnitTestHandleProtocol, = // HandleProtocol + (VOID *)NULL, = // Reserved + (EFI_REGISTER_PROTOCOL_NOTIFY)UnitTestRegisterProtocolNotify, = // RegisterProtocolNotify + (EFI_LOCATE_HANDLE)UnitTestLocateHandle, = // LocateHandle + (EFI_LOCATE_DEVICE_PATH)UnitTestLocateDevicePath, = // LocateDevicePath + (EFI_INSTALL_CONFIGURATION_TABLE)UnitTestInstallConfigurationTable, = // InstallConfigurationTable + (EFI_IMAGE_LOAD)UnitTestLoadImage, = // LoadImage + (EFI_IMAGE_START)UnitTestStartImage, = // StartImage + (EFI_EXIT)UnitTestExit, = // Exit + (EFI_IMAGE_UNLOAD)UnitTestUnloadImage, = // UnloadImage + (EFI_EXIT_BOOT_SERVICES)UnitTestExitBootServices, = // ExitBootServices + (EFI_GET_NEXT_MONOTONIC_COUNT)UnitTestGetNextMonotonicCount, = // GetNextMonotonicCount + (EFI_STALL)UnitTestStall, = // Stall + (EFI_SET_WATCHDOG_TIMER)UnitTestSetWatchdogTimer, = // SetWatchdogTimer + (EFI_CONNECT_CONTROLLER)UnitTestConnectController, = // ConnectController + (EFI_DISCONNECT_CONTROLLER)UnitTestDisconnectController, = // DisconnectController + (EFI_OPEN_PROTOCOL)UnitTestOpenProtocol, = // OpenProtocol + (EFI_CLOSE_PROTOCOL)UnitTestCloseProtocol, = // CloseProtocol + (EFI_OPEN_PROTOCOL_INFORMATION)UnitTestOpenProtocolInformation, = // OpenProtocolInformation + (EFI_PROTOCOLS_PER_HANDLE)UnitTestProtocolsPerHandle, = // ProtocolsPerHandle + (EFI_LOCATE_HANDLE_BUFFER)UnitTestLocateHandleBuffer, = // LocateHandleBuffer + (EFI_LOCATE_PROTOCOL)UnitTestLocateProtocol, = // LocateProtocol + (EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)UnitTestInstallMultipleProto= colInterfaces, // InstallMultipleProtocolInterfaces + (EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)UnitTestUninstallMultipleP= rotocolInterfaces, // UninstallMultipleProtocolInterfaces + (EFI_CALCULATE_CRC32)UnitTestCalculateCrc32, = // CalculateCrc32 + (EFI_COPY_MEM)CopyMem, = // CopyMem + (EFI_SET_MEM)SetMem, = // SetMem + (EFI_CREATE_EVENT_EX)UnitTestCreateEventEx = // CreateEventEx +}; + +EFI_BOOT_SERVICES *gBS =3D &mBootServices; + +/** + The constructor function caches the pointer of Boot Services Table. + + The constructor function caches the pointer of Boot Services Table thr= ough System Table. + It will ASSERT() if the pointer of System Table is NULL. + It will ASSERT() if the pointer of Boot Services Table is NULL. + It will always return EFI_SUCCESS. + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +UnitTestUefiBootServicesTableLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + // + // Cache the Image Handle + // + gImageHandle =3D ImageHandle; + ASSERT (gImageHandle !=3D NULL); + + // + // Cache pointer to the EFI System Table + // + + // Note: The system table is not implemented + gST =3D NULL; + + // + // Cache pointer to the EFI Boot Services Table + // + gBS =3D SystemTable->BootServices; + ASSERT (gBS !=3D NULL); + + return EFI_SUCCESS; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibEventTimer.c b/UnitTestFrameworkPkg/Lib= rary/UnitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibEve= ntTimer.c new file mode 100644 index 000000000000..dc02c4ef9a8e --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibEventTimer.c @@ -0,0 +1,180 @@ +/** @file + Implementation of event and timer related services in the UEFI Boot Se= rvices table for use in unit tests. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLib.h" + +/** + Creates an event. + + @param Type The type of event to create and its mod= e and + attributes + @param NotifyTpl The task priority level of event notifi= cations + @param NotifyFunction Pointer to the events notification func= tion + @param NotifyContext Pointer to the notification functions c= ontext + corresponds to parameter "Context" in t= he + notification function + @param Event Pointer to the newly created event if t= he call + succeeds undefined otherwise + + @retval EFI_SUCCESS The event structure was created + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue + @retval EFI_OUT_OF_RESOURCES The event could not be allocated + +**/ +EFI_STATUS +EFIAPI +UnitTestCreateEvent ( + IN UINT32 Type, + IN EFI_TPL NotifyTpl, + IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL + IN VOID *NotifyContext, OPTIONAL + OUT EFI_EVENT *Event + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Sets the type of timer and the trigger time for a timer event. + + @param UserEvent The timer event that is to be signaled = at the + specified time + @param Type The type of time that is specified in + TriggerTime + @param TriggerTime The number of 100ns units until the tim= er + expires + + @retval EFI_SUCCESS The event has been set to be signaled a= t the + requested time + @retval EFI_INVALID_PARAMETER Event or Type is not valid + +**/ +EFI_STATUS +EFIAPI +UnitTestSetTimer ( + IN EFI_EVENT UserEvent, + IN EFI_TIMER_DELAY Type, + IN UINT64 TriggerTime + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Stops execution until an event is signaled. + + @param NumberOfEvents The number of events in the UserEvents = array + @param UserEvents An array of EFI_EVENT + @param UserIndex Pointer to the index of the event which + satisfied the wait condition + + @retval EFI_SUCCESS The event indicated by Index was signal= ed. + @retval EFI_INVALID_PARAMETER The event indicated by Index has a noti= fication + function or Event was not a valid type + @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION + +**/ +EFI_STATUS +EFIAPI +UnitTestWaitForEvent ( + IN UINTN NumberOfEvents, + IN EFI_EVENT *UserEvents, + OUT UINTN *UserIndex + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Signals the event. Queues the event to be notified if needed. + + @param UserEvent The event to signal . + + @retval EFI_INVALID_PARAMETER Parameters are not valid. + @retval EFI_SUCCESS The event was signaled. + +**/ +EFI_STATUS +EFIAPI +UnitTestSignalEvent ( + IN EFI_EVENT UserEvent + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Closes an event and frees the event structure. + + @param UserEvent Event to close + + @retval EFI_INVALID_PARAMETER Parameters are not valid. + @retval EFI_SUCCESS The event has been closed + +**/ +EFI_STATUS +EFIAPI +UnitTestCloseEvent ( + IN EFI_EVENT UserEvent + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Check the status of an event. + + @param UserEvent The event to check + + @retval EFI_SUCCESS The event is in the signaled state + @retval EFI_NOT_READY The event is not in the signaled state + @retval EFI_INVALID_PARAMETER Event is of type EVT_NOTIFY_SIGNAL + +**/ +EFI_STATUS +EFIAPI +UnitTestCheckEvent ( + IN EFI_EVENT UserEvent + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Creates an event in a group. + + @param Type The type of event to create and its mod= e and + attributes + @param NotifyTpl The task priority level of event notifi= cations + @param NotifyFunction Pointer to the events notification func= tion + @param NotifyContext Pointer to the notification functions c= ontext + corresponds to parameter "Context" in t= he + notification function + @param EventGroup GUID for EventGroup if NULL act the sam= e as + gBS->CreateEvent(). + @param Event Pointer to the newly created event if t= he call + succeeds undefined otherwise + + @retval EFI_SUCCESS The event structure was created + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue + @retval EFI_OUT_OF_RESOURCES The event could not be allocated + +**/ +EFI_STATUS +EFIAPI +UnitTestCreateEventEx ( + IN UINT32 Type, + IN EFI_TPL NotifyTpl, + IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL + IN CONST VOID *NotifyContext, OPTIONAL + IN CONST EFI_GUID *EventGroup, OPTIONAL + OUT EFI_EVENT *Event + ) +{ + return EFI_NOT_AVAILABLE_YET; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibImage.c b/UnitTestFrameworkPkg/Library/= UnitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibImage.c new file mode 100644 index 000000000000..144cc33e25c0 --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibImage.c @@ -0,0 +1,163 @@ +/** @file + Implementation of image related services in the UEFI Boot Services tab= le for use in unit tests. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLib.h" + +/** + Loads an EFI image into memory and returns a handle to the image. + + @param BootPolicy If TRUE, indicates that the request or= iginates + from the boot manager, and that the bo= ot + manager is attempting to load FilePath= as a + boot selection. + @param ParentImageHandle The caller's image handle. + @param FilePath The specific file path from which the = image is + loaded. + @param SourceBuffer If not NULL, a pointer to the memory l= ocation + containing a copy of the image to be l= oaded. + @param SourceSize The size in bytes of SourceBuffer. + @param ImageHandle Pointer to the returned image handle t= hat is + created when the image is successfully= loaded. + + @retval EFI_SUCCESS The image was loaded into memory. + @retval EFI_NOT_FOUND The FilePath was not found. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid v= alue. + @retval EFI_UNSUPPORTED The image type is not supported, or th= e device + path cannot be parsed to locate the pr= oper + protocol for loading the file. + @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficie= nt + resources. + @retval EFI_LOAD_ERROR Image was not loaded because the image= format was corrupt or not + understood. + @retval EFI_DEVICE_ERROR Image was not loaded because the devic= e returned a read error. + @retval EFI_ACCESS_DENIED Image was not loaded because the platf= orm policy prohibits the + image from being loaded. NULL is retur= ned in *ImageHandle. + @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle wa= s created with a + valid EFI_LOADED_IMAGE_PROTOCOL. Howev= er, the current + platform policy specifies that the ima= ge should not be started. + +**/ +EFI_STATUS +EFIAPI +UnitTestLoadImage ( + IN BOOLEAN BootPolicy, + IN EFI_HANDLE ParentImageHandle, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN VOID *SourceBuffer OPTIONAL, + IN UINTN SourceSize, + OUT EFI_HANDLE *ImageHandle + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Transfer control to a loaded image's entry point. + + @param ImageHandle Handle of image to be started. + @param ExitDataSize Pointer of the size to ExitData + @param ExitData Pointer to a pointer to a data buffer = that + includes a Null-terminated string, + optionally followed by additional bina= ry data. + The string is a description that the c= aller may + use to further indicate the reason for= the + image's exit. + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate + @retval EFI_SECURITY_VIOLATION The current platform policy specifies = that the image should not be started. + @retval EFI_SUCCESS Successfully transfer control to the i= mage's + entry point. + +**/ +EFI_STATUS +EFIAPI +UnitTestStartImage ( + IN EFI_HANDLE ImageHandle, + OUT UINTN *ExitDataSize, + OUT CHAR16 **ExitData OPTIONAL + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Terminates the currently loaded EFI image and returns control to boot = services. + + @param ImageHandle Handle that identifies the image. This + parameter is passed to the image on en= try. + @param Status The image's exit code. + @param ExitDataSize The size, in bytes, of ExitData. Ignor= ed if + ExitStatus is EFI_SUCCESS. + @param ExitData Pointer to a data buffer that includes= a + Null-terminated Unicode string, option= ally + followed by additional binary data. Th= e string + is a description that the caller may u= se to + further indicate the reason for the im= age's + exit. + + @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not curr= ent + image. + @retval EFI_SUCCESS Successfully terminates the currently = loaded + EFI image. + @retval EFI_ACCESS_DENIED Should never reach there. + @retval EFI_OUT_OF_RESOURCES Could not allocate pool + +**/ +EFI_STATUS +EFIAPI +UnitTestExit ( + IN EFI_HANDLE ImageHandle, + IN EFI_STATUS Status, + IN UINTN ExitDataSize, + IN CHAR16 *ExitData OPTIONAL + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be + unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_UNSUPPORTED The image has been started, and does n= ot support + unload. + @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handl= e. + +**/ +EFI_STATUS +EFIAPI +UnitTestUnloadImage ( + IN EFI_HANDLE ImageHandle + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Terminates all boot services. + + @param ImageHandle Handle that identifies the exiting imag= e. + @param MapKey Key to the latest memory map. + + @retval EFI_SUCCESS Boot Services terminated + @retval EFI_INVALID_PARAMETER MapKey is incorrect. + +**/ +EFI_STATUS +EFIAPI +UnitTestExitBootServices ( + IN EFI_HANDLE ImageHandle, + IN UINTN MapKey + ) +{ + return EFI_NOT_AVAILABLE_YET; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibMemory.c b/UnitTestFrameworkPkg/Library= /UnitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibMemory.= c new file mode 100644 index 000000000000..abbb12974a55 --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibMemory.c @@ -0,0 +1,145 @@ +/** @file + Implementation of memory related services in the UEFI Boot Services ta= ble for use in unit tests. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLib.h" + +/** + Allocates pages from the memory map. + + @param Type The type of allocation to perform + @param MemoryType The type of memory to turn the allocate= d pages + into + @param NumberOfPages The number of pages to allocate + @param Memory A pointer to receive the base allocated= memory + address + + @return Status. On success, Memory is filled in with the base address = allocated + @retval EFI_INVALID_PARAMETER Parameters violate checking rules defin= ed in + spec. + @retval EFI_NOT_FOUND Could not allocate pages match the requ= irement. + @retval EFI_OUT_OF_RESOURCES No enough pages to allocate. + @retval EFI_SUCCESS Pages successfully allocated. + +**/ +EFI_STATUS +EFIAPI +UnitTestAllocatePages ( + IN EFI_ALLOCATE_TYPE Type, + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN NumberOfPages, + IN OUT EFI_PHYSICAL_ADDRESS *Memory + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Frees previous allocated pages. + + @param Memory Base address of memory being freed + @param NumberOfPages The number of pages to free + + @retval EFI_NOT_FOUND Could not find the entry that covers th= e range + @retval EFI_INVALID_PARAMETER Address not aligned + @return EFI_SUCCESS -Pages successfully freed. + +**/ +EFI_STATUS +EFIAPI +UnitTestFreePages ( + IN EFI_PHYSICAL_ADDRESS Memory, + IN UINTN NumberOfPages + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + This function returns a copy of the current memory map. The map is an = array of + memory descriptors, each of which describes a contiguous block of memo= ry. + + @param MemoryMapSize A pointer to the size, in bytes, of the + MemoryMap buffer. On input, this is the= size of + the buffer allocated by the caller. On= output, + it is the size of the buffer returned b= y the + firmware if the buffer was large enoug= h, or the + size of the buffer needed to contain t= he map if + the buffer was too small. + @param MemoryMap A pointer to the buffer in which firmwa= re places + the current memory map. + @param MapKey A pointer to the location in which firm= ware + returns the key for the current memory = map. + @param DescriptorSize A pointer to the location in which firm= ware + returns the size, in bytes, of an indiv= idual + EFI_MEMORY_DESCRIPTOR. + @param DescriptorVersion A pointer to the location in which firm= ware + returns the version number associated w= ith the + EFI_MEMORY_DESCRIPTOR. + + @retval EFI_SUCCESS The memory map was returned in the Memo= ryMap + buffer. + @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The= current + buffer size needed to hold the memory m= ap is + returned in MemoryMapSize. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue. + +**/ +EFI_STATUS +EFIAPI +UnitTestGetMemoryMap ( + IN OUT UINTN *MemoryMapSize, + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + OUT UINTN *MapKey, + OUT UINTN *DescriptorSize, + OUT UINT32 *DescriptorVersion + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Allocate pool of a particular type. + + @param PoolType Type of pool to allocate + @param Size The amount of pool to allocate + @param Buffer The address to return a pointer to the = allocated + pool + + @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL + @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocatio= n failed. + @retval EFI_SUCCESS Pool successfully allocated. + +**/ +EFI_STATUS +EFIAPI +UnitTestAllocatePool ( + IN EFI_MEMORY_TYPE PoolType, + IN UINTN Size, + OUT VOID **Buffer + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Frees pool. + + @param Buffer The allocated pool entry to free + + @retval EFI_INVALID_PARAMETER Buffer is not a valid value. + @retval EFI_SUCCESS Pool successfully freed. + +**/ +EFI_STATUS +EFIAPI +UnitTestFreePool ( + IN VOID *Buffer + ) +{ + return EFI_NOT_AVAILABLE_YET; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibMisc.c b/UnitTestFrameworkPkg/Library/U= nitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibMisc.c new file mode 100644 index 000000000000..121496b3b994 --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibMisc.c @@ -0,0 +1,198 @@ +/** @file + Implementation of miscellaneous services in the UEFI Boot Services tab= le for use in unit tests. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLib.h" + +/** + Returns a monotonically increasing count for the platform. + + @param[out] Count The pointer to returned value. + + @retval EFI_SUCCESS The next monotonic count was returned. + @retval EFI_INVALID_PARAMETER Count is NULL. + @retval EFI_DEVICE_ERROR The device is not functioning properly. + +**/ +EFI_STATUS +EFIAPI +UnitTestGetNextMonotonicCount ( + OUT UINT64 *Count + ) +{ + STATIC UINT64 StaticCount =3D 0; + + *Count =3D StaticCount++; + + return EFI_SUCCESS; +} + +/** + Introduces a fine-grained stall. + + @param Microseconds The number of microseconds to stall exe= cution. + + @retval EFI_SUCCESS Execution was stalled for at least the = requested + amount of microseconds. + @retval EFI_NOT_AVAILABLE_YET gMetronome is not available yet + +**/ +EFI_STATUS +EFIAPI +UnitTestStall ( + IN UINTN Microseconds + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Sets the system's watchdog timer. + + @param Timeout The number of seconds to set the watchdog time= r to. + A value of zero disables the timer. + @param WatchdogCode The numeric code to log on a watchdog timer ti= meout + event. The firmware reserves codes 0x0000 to 0= xFFFF. + Loaders and operating systems may use other ti= meout + codes. + @param DataSize The size, in bytes, of WatchdogData. + @param WatchdogData A data buffer that includes a Null-terminated = Unicode + string, optionally followed by additional bina= ry data. + The string is a description that the call may = use to + further indicate the reason to be logged with = a + watchdog event. + + @return EFI_SUCCESS Timeout has been set + @return EFI_NOT_AVAILABLE_YET WatchdogTimer is not available yet + @return EFI_UNSUPPORTED System does not have a timer (curren= tly not used) + @return EFI_DEVICE_ERROR Could not complete due to hardware e= rror + +**/ +EFI_STATUS +EFIAPI +UnitTestSetWatchdogTimer ( + IN UINTN Timeout, + IN UINT64 WatchdogCode, + IN UINTN DataSize, + IN CHAR16 *WatchdogData OPTIONAL + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Connects one or more drivers to a controller. + + @param ControllerHandle The handle of the controller to which dr= iver(s) are to be connected. + @param DriverImageHandle A pointer to an ordered list handles tha= t support the + EFI_DRIVER_BINDING_PROTOCOL. + @param RemainingDevicePath A pointer to the device path that specif= ies a child of the + controller specified by ControllerHandle= . + @param Recursive If TRUE, then ConnectController() is cal= led recursively + until the entire tree of controllers bel= ow the controller specified + by ControllerHandle have been created. I= f FALSE, then + the tree of controllers is only expanded= one level. + + @retval EFI_SUCCESS 1) One or more drivers were connected to= ControllerHandle. + 2) No drivers were connected to Controll= erHandle, but + RemainingDevicePath is not NULL, and it = is an End Device + Path Node. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_NOT_FOUND 1) There are no EFI_DRIVER_BINDING_PROTO= COL instances + present in the system. + 2) No drivers were connected to Controll= erHandle. + @retval EFI_SECURITY_VIOLATION + The user has no permission to start UEFI= device drivers on the device path + associated with the ControllerHandle or = specified by the RemainingDevicePath. + +**/ +EFI_STATUS +EFIAPI +UnitTestConnectController ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE *DriverImageHandle OPTIONAL, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN BOOLEAN Recursive + ) +{ + return EFI_SUCCESS; // Return success for now +} + +/** + Disconnects a controller from a driver + + @param ControllerHandle ControllerHandle The han= dle of + the controller from whic= h + driver(s) are to be + disconnected. + @param DriverImageHandle DriverImageHandle The dr= iver to + disconnect from Controll= erHandle. + @param ChildHandle ChildHandle The handle o= f the + child to destroy. + + @retval EFI_SUCCESS One or more drivers were + disconnected from the co= ntroller. + @retval EFI_SUCCESS On entry, no drivers are= managing + ControllerHandle. + @retval EFI_SUCCESS DriverImageHandle is not= NULL, + and on entry DriverImage= Handle is + not managing ControllerH= andle. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL= . + @retval EFI_INVALID_PARAMETER DriverImageHandle is not= NULL, + and it is not a valid EF= I_HANDLE. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL,= and it + is not a valid EFI_HANDL= E. + @retval EFI_OUT_OF_RESOURCES There are not enough res= ources + available to disconnect = any + drivers from ControllerH= andle. + @retval EFI_DEVICE_ERROR The controller could not= be + disconnected because of = a device + error. + +**/ +EFI_STATUS +EFIAPI +UnitTestDisconnectController ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE DriverImageHandle OPTIONAL, + IN EFI_HANDLE ChildHandle OPTIONAL + ) +{ + return EFI_SUCCESS; // Return success for now +} + +/** + Computes and returns a 32-bit CRC for a data buffer. + + @param[in] Data A pointer to the buffer on which the 32-= bit CRC is to be computed. + @param[in] DataSize The number of bytes in the buffer Data. + @param[out] Crc32 The 32-bit CRC that was computed for the= data buffer specified by Data + and DataSize. + + @retval EFI_SUCCESS The 32-bit CRC was computed for the data= buffer and returned in + Crc32. + @retval EFI_INVALID_PARAMETER Data is NULL. + @retval EFI_INVALID_PARAMETER Crc32 is NULL. + @retval EFI_INVALID_PARAMETER DataSize is 0. + +**/ +EFI_STATUS +EFIAPI +UnitTestCalculateCrc32 ( + IN VOID *Data, + IN UINTN DataSize, + OUT UINT32 *Crc32 + ) +{ + if ((Data =3D=3D NULL) || (Crc32 =3D=3D NULL) || (DataSize =3D=3D 0)) = { + return EFI_INVALID_PARAMETER; + } + + *Crc32 =3D CalculateCrc32 (Data, DataSize); + + return EFI_SUCCESS; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibProtocol.c b/UnitTestFrameworkPkg/Libra= ry/UnitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibProto= col.c new file mode 100644 index 000000000000..437082d516ea --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibProtocol.c @@ -0,0 +1,1650 @@ +/** @file + Implementation of protocol related services in the UEFI Boot Services = table for use in unit tests. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLibProtocol.h" + +STATIC LIST_ENTRY mProtocolDatabase =3D INITIALIZE_LIST_HEAD_VARI= ABLE (mProtocolDatabase); +STATIC LIST_ENTRY gHandleList =3D INITIALIZE_LIST_HEAD_VARI= ABLE (gHandleList); +STATIC UINT64 gHandleDatabaseKey =3D 0; +STATIC UINTN mEfiLocateHandleRequest =3D 0; + +// +// Helper Functions +// + +/** + Check whether a handle is a valid EFI_HANDLE + + @param UserHandle The handle to check + + @retval EFI_INVALID_PARAMETER The handle is NULL or not a valid EFI_H= ANDLE. + @retval EFI_SUCCESS The handle is valid EFI_HANDLE. + +**/ +EFI_STATUS +UnitTestValidateHandle ( + IN EFI_HANDLE UserHandle + ) +{ + IHANDLE *Handle; + LIST_ENTRY *Link; + + if (UserHandle =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + for (Link =3D gHandleList.BackLink; Link !=3D &gHandleList; Link =3D L= ink->BackLink) { + Handle =3D CR (Link, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE); + if (Handle =3D=3D (IHANDLE *)UserHandle) { + return EFI_SUCCESS; + } + } + + return EFI_INVALID_PARAMETER; +} + +/** + Finds the protocol entry for the requested protocol. + + @param Protocol The ID of the protocol + @param Create Create a new entry if not found + + @return Protocol entry + +**/ +PROTOCOL_ENTRY * +UnitTestFindProtocolEntry ( + IN EFI_GUID *Protocol, + IN BOOLEAN Create + ) +{ + LIST_ENTRY *Link; + PROTOCOL_ENTRY *Item; + PROTOCOL_ENTRY *ProtEntry; + + // + // Search the database for the matching GUID + // + + ProtEntry =3D NULL; + for (Link =3D mProtocolDatabase.ForwardLink; + Link !=3D &mProtocolDatabase; + Link =3D Link->ForwardLink) + { + Item =3D CR (Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNAT= URE); + if (CompareGuid (&Item->ProtocolID, Protocol)) { + // + // This is the protocol entry + // + + ProtEntry =3D Item; + break; + } + } + + // + // If the protocol entry was not found and Create is TRUE, then + // allocate a new entry + // + if ((ProtEntry =3D=3D NULL) && Create) { + ProtEntry =3D AllocatePool (sizeof (PROTOCOL_ENTRY)); + + if (ProtEntry !=3D NULL) { + // + // Initialize new protocol entry structure + // + ProtEntry->Signature =3D PROTOCOL_ENTRY_SIGNATURE; + CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol); + InitializeListHead (&ProtEntry->Protocols); + InitializeListHead (&ProtEntry->Notify); + + // + // Add it to protocol database + // + InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries); + } + } + + return ProtEntry; +} + +/** + Finds the protocol instance for the requested handle and protocol. + Note: This function doesn't do parameters checking, it's caller's resp= onsibility + to pass in valid parameters. + + @param Handle The handle to search the protocol on + @param Protocol GUID of the protocol + @param Interface The interface for the protocol being se= arched + + @return Protocol instance (NULL: Not found) + +**/ +PROTOCOL_INTERFACE * +UnitTestFindProtocolInterface ( + IN IHANDLE *Handle, + IN EFI_GUID *Protocol, + IN VOID *Interface + ) +{ + PROTOCOL_INTERFACE *Prot; + PROTOCOL_ENTRY *ProtEntry; + LIST_ENTRY *Link; + + Prot =3D NULL; + + // + // Lookup the protocol entry for this protocol ID + // + + ProtEntry =3D UnitTestFindProtocolEntry (Protocol, FALSE); + if (ProtEntry !=3D NULL) { + // + // Look at each protocol interface for any matches + // + for (Link =3D Handle->Protocols.ForwardLink; Link !=3D &Handle->Prot= ocols; Link =3D Link->ForwardLink) { + // + // If this protocol interface matches, remove it + // + Prot =3D CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SI= GNATURE); + if ((Prot->Interface =3D=3D Interface) && (Prot->Protocol =3D=3D P= rotEntry)) { + break; + } + + Prot =3D NULL; + } + } + + return Prot; +} + +/** + Signal event for every protocol in protocol entry. + + @param ProtEntry Protocol entry + +**/ +VOID +UnitTestNotifyProtocolEntry ( + IN PROTOCOL_ENTRY *ProtEntry + ) +{ + PROTOCOL_NOTIFY *ProtNotify; + LIST_ENTRY *Link; + + for (Link =3D ProtEntry->Notify.ForwardLink; Link !=3D &ProtEntry->Not= ify; Link =3D Link->ForwardLink) { + ProtNotify =3D CR (Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGN= ATURE); + UnitTestSignalEvent (ProtNotify->Event); + } +} + +/** + Routine to get the next Handle, when you are searching for all handles= . + + @param Position Information about which Handle to seach= for. + @param Interface Return the interface structure for the = matching + protocol. + + @return An pointer to IHANDLE if the next Position is not the end of t= he list. + Otherwise,NULL is returned. + +**/ +IHANDLE * +UnitTestGetNextLocateAllHandles ( + IN OUT LOCATE_POSITION *Position, + OUT VOID **Interface + ) +{ + IHANDLE *Handle; + + // + // Next handle + // + Position->Position =3D Position->Position->ForwardLink; + + // + // If not at the end of the list, get the handle + // + Handle =3D NULL; + *Interface =3D NULL; + if (Position->Position !=3D &gHandleList) { + Handle =3D CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_S= IGNATURE); + } + + return Handle; +} + +/** + Routine to get the next Handle, when you are searching for register pr= otocol + notifies. + + @param Position Information about which Handle to seach= for. + @param Interface Return the interface structure for the = matching + protocol. + + @return An pointer to IHANDLE if the next Position is not the end of t= he list. + Otherwise,NULL is returned. + +**/ +IHANDLE * +UnitTestGetNextLocateByRegisterNotify ( + IN OUT LOCATE_POSITION *Position, + OUT VOID **Interface + ) +{ + IHANDLE *Handle; + PROTOCOL_NOTIFY *ProtNotify; + PROTOCOL_INTERFACE *Prot; + LIST_ENTRY *Link; + + Handle =3D NULL; + *Interface =3D NULL; + ProtNotify =3D Position->SearchKey; + + // + // If this is the first request, get the next handle + // + if (ProtNotify !=3D NULL) { + ASSERT (ProtNotify->Signature =3D=3D PROTOCOL_NOTIFY_SIGNATURE); + Position->SearchKey =3D NULL; + + // + // If not at the end of the list, get the next handle + // + Link =3D ProtNotify->Position->ForwardLink; + if (Link !=3D &ProtNotify->Protocol->Protocols) { + Prot =3D CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_= INTERFACE_SIGNATURE); + Handle =3D Prot->Handle; + *Interface =3D Prot->Interface; + } + } + + return Handle; +} + +/** + Routine to get the next Handle, when you are searching for a given pro= tocol. + + @param Position Information about which Handle to seach= for. + @param Interface Return the interface structure for the = matching + protocol. + + @return An pointer to IHANDLE if the next Position is not the end of t= he list. + Otherwise,NULL is returned. + +**/ +IHANDLE * +UnitTestGetNextLocateByProtocol ( + IN OUT LOCATE_POSITION *Position, + OUT VOID **Interface + ) +{ + IHANDLE *Handle; + LIST_ENTRY *Link; + PROTOCOL_INTERFACE *Prot; + + Handle =3D NULL; + *Interface =3D NULL; + for ( ; ;) { + // + // Next entry + // + Link =3D Position->Position->ForwardLink; + Position->Position =3D Link; + + // + // If not at the end, return the handle + // + if (Link =3D=3D &Position->ProtEntry->Protocols) { + Handle =3D NULL; + break; + } + + // + // Get the handle + // + Prot =3D CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_IN= TERFACE_SIGNATURE); + Handle =3D Prot->Handle; + *Interface =3D Prot->Interface; + + // + // If this handle has not been returned this request, then + // return it now + // + if (Handle->LocateRequest !=3D mEfiLocateHandleRequest) { + Handle->LocateRequest =3D mEfiLocateHandleRequest; + break; + } + } + + return Handle; +} + +/** + Attempts to disconnect all drivers that are using the protocol interfa= ce being queried. + If failed, reconnect all drivers disconnected. + Note: This function doesn't do parameters checking, it's caller's resp= onsibility + to pass in valid parameters. + + @param UserHandle The handle on which the protocol is ins= talled + @param Prot The protocol to disconnect drivers from + + @retval EFI_SUCCESS Drivers using the protocol interface ar= e all + disconnected + @retval EFI_ACCESS_DENIED Failed to disconnect one or all of the = drivers + +**/ +EFI_STATUS +UnitTestDisconnectControllersUsingProtocolInterface ( + IN EFI_HANDLE UserHandle, + IN PROTOCOL_INTERFACE *Prot + ) +{ + EFI_STATUS Status; + BOOLEAN ItemFound; + LIST_ENTRY *Link; + OPEN_PROTOCOL_DATA *OpenData; + + Status =3D EFI_SUCCESS; + + // + // Attempt to disconnect all drivers from this protocol interface + // + do { + ItemFound =3D FALSE; + for (Link =3D Prot->OpenList.ForwardLink; Link !=3D &Prot->OpenList;= Link =3D Link->ForwardLink) { + OpenData =3D CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DAT= A_SIGNATURE); + if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) !=3D 0) { + Status =3D UnitTestDisconnectController (UserHandle, OpenData->A= gentHandle, NULL); + if (!EFI_ERROR (Status)) { + ItemFound =3D TRUE; + } + + break; + } + } + } while (ItemFound); + + if (!EFI_ERROR (Status)) { + // + // Attempt to remove BY_HANDLE_PROTOCOL and GET_PROTOCOL and TEST_PR= OTOCOL Open List items + // + for (Link =3D Prot->OpenList.ForwardLink; Link !=3D &Prot->OpenList;= ) { + OpenData =3D CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DAT= A_SIGNATURE); + if ((OpenData->Attributes & + (EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL | EFI_OPEN_PROTOCOL_GET= _PROTOCOL | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) !=3D 0) + { + Link =3D RemoveEntryList (&OpenData->Link); + Prot->OpenListCount--; + FreePool (OpenData); + } else { + Link =3D Link->ForwardLink; + } + } + } + + // + // If there are errors or still has open items in the list, then recon= nect all the drivers and return an error + // + if (EFI_ERROR (Status) || (Prot->OpenListCount > 0)) { + UnitTestConnectController (UserHandle, NULL, NULL, TRUE); + Status =3D EFI_ACCESS_DENIED; + } + + return Status; +} + +/** + Removes Protocol from the protocol list (but not the handle list). + + @param Handle The handle to remove protocol on. + @param Protocol GUID of the protocol to be moved + @param Interface The interface of the protocol + + @return Protocol Entry + +**/ +PROTOCOL_INTERFACE * +UnitTestRemoveInterfaceFromProtocol ( + IN IHANDLE *Handle, + IN EFI_GUID *Protocol, + IN VOID *Interface + ) +{ + PROTOCOL_INTERFACE *Prot; + PROTOCOL_NOTIFY *ProtNotify; + PROTOCOL_ENTRY *ProtEntry; + LIST_ENTRY *Link; + + Prot =3D UnitTestFindProtocolInterface (Handle, Protocol, Interface); + if (Prot !=3D NULL) { + ProtEntry =3D Prot->Protocol; + + // + // If there's a protocol notify location pointing to this entry, bac= k it up one + // + for (Link =3D ProtEntry->Notify.ForwardLink; Link !=3D &ProtEntry->N= otify; Link =3D Link->ForwardLink) { + ProtNotify =3D CR (Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SI= GNATURE); + + if (ProtNotify->Position =3D=3D &Prot->ByProtocol) { + ProtNotify->Position =3D Prot->ByProtocol.BackLink; + } + } + + // + // Remove the protocol interface entry + // + RemoveEntryList (&Prot->ByProtocol); + } + + return Prot; +} + +// +// Boot Services Function Implementation +// + +/** + Locate a certain GUID protocol interface in a Handle's protocols. + + @param UserHandle The handle to obtain the protocol inter= face on + @param Protocol The GUID of the protocol + + @return The requested protocol interface for the handle + +**/ +PROTOCOL_INTERFACE * +UnitTestGetProtocolInterface ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol + ) +{ + EFI_STATUS Status; + PROTOCOL_ENTRY *ProtEntry; + PROTOCOL_INTERFACE *Prot; + IHANDLE *Handle; + LIST_ENTRY *Link; + + Status =3D UnitTestValidateHandle (UserHandle); + if (EFI_ERROR (Status)) { + return NULL; + } + + Handle =3D (IHANDLE *)UserHandle; + + // + // Look at each protocol interface for a match + // + for (Link =3D Handle->Protocols.ForwardLink; Link !=3D &Handle->Protoc= ols; Link =3D Link->ForwardLink) { + Prot =3D CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE= _SIGNATURE); + ProtEntry =3D Prot->Protocol; + if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) { + return Prot; + } + } + + return NULL; +} + +/** + Installs a protocol interface into the boot services environment. + + @param UserHandle The handle to install the protocol hand= ler on, + or NULL if a new handle is to be alloca= ted + @param Protocol The protocol to add to the handle + @param InterfaceType Indicates whether Interface is supplied= in + native form. + @param Interface The interface for the protocol being ad= ded + @param Notify indicates whether notify the notificati= on list + for this protocol + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate + @retval EFI_SUCCESS Protocol interface successfully install= ed + +**/ +EFI_STATUS +UnitTestInstallProtocolInterfaceNotify ( + IN OUT EFI_HANDLE *UserHandle, + IN EFI_GUID *Protocol, + IN EFI_INTERFACE_TYPE InterfaceType, + IN VOID *Interface, + IN BOOLEAN Notify + ) +{ + PROTOCOL_INTERFACE *Prot; + PROTOCOL_ENTRY *ProtEntry; + IHANDLE *Handle; + EFI_STATUS Status; + VOID *ExistingInterface; + + // + // returns EFI_INVALID_PARAMETER if InterfaceType is invalid. + // Also added check for invalid UserHandle and Protocol pointers. + // + if ((UserHandle =3D=3D NULL) || (Protocol =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (InterfaceType !=3D EFI_NATIVE_INTERFACE) { + return EFI_INVALID_PARAMETER; + } + + // + // Print debug message + // + UT_LOG_INFO ("InstallProtocolInterface: %g %p\n", Protocol, Interface)= ; + + Status =3D EFI_OUT_OF_RESOURCES; + Prot =3D NULL; + Handle =3D NULL; + + if (*UserHandle !=3D NULL) { + Status =3D UnitTestHandleProtocol (*UserHandle, Protocol, (VOID **)&= ExistingInterface); + if (!EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + } + + // + // Lookup the Protocol Entry for the requested protocol + // + ProtEntry =3D UnitTestFindProtocolEntry (Protocol, TRUE); + if (ProtEntry =3D=3D NULL) { + goto Done; + } + + // + // Allocate a new protocol interface structure + // + Prot =3D AllocateZeroPool (sizeof (PROTOCOL_INTERFACE)); + if (Prot =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + + // + // If caller didn't supply a handle, allocate a new one + // + Handle =3D (IHANDLE *)*UserHandle; + if (Handle =3D=3D NULL) { + Handle =3D AllocateZeroPool (sizeof (IHANDLE)); + if (Handle =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + + // + // Initialize new handler structure + // + Handle->Signature =3D EFI_HANDLE_SIGNATURE; + InitializeListHead (&Handle->Protocols); + + // + // Initialize the Key to show that the handle has been created/modif= ied + // + gHandleDatabaseKey++; + Handle->Key =3D gHandleDatabaseKey; + + // + // Add this handle to the list global list of all handles + // in the system + // + InsertTailList (&gHandleList, &Handle->AllHandles); + } else { + Status =3D UnitTestValidateHandle (Handle); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "InstallProtocolInterface: input handle at 0x= %x is invalid\n", Handle)); + goto Done; + } + } + + // + // Each interface that is added must be unique + // + ASSERT (UnitTestFindProtocolInterface (Handle, Protocol, Interface) =3D= =3D NULL); + + // + // Initialize the protocol interface structure + // + Prot->Signature =3D PROTOCOL_INTERFACE_SIGNATURE; + Prot->Handle =3D Handle; + Prot->Protocol =3D ProtEntry; + Prot->Interface =3D Interface; + + // + // Initialize OpenProtocol Data base + // + InitializeListHead (&Prot->OpenList); + Prot->OpenListCount =3D 0; + + // + // Add this protocol interface to the head of the supported + // protocol list for this handle + // + InsertHeadList (&Handle->Protocols, &Prot->Link); + + // + // Add this protocol interface to the tail of the + // protocol entry + // + InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol); + + // + // Notify the notification list for this protocol + // + if (Notify) { + UnitTestNotifyProtocolEntry (ProtEntry); + } + + Status =3D EFI_SUCCESS; + +Done: + if (!EFI_ERROR (Status)) { + // + // Return the new handle back to the caller + // + *UserHandle =3D Handle; + } else { + // + // There was an error, clean up + // + if (Prot !=3D NULL) { + UnitTestFreePool (Prot); + } + + DEBUG ((DEBUG_ERROR, "InstallProtocolInterface: %g %p failed with %r= \n", Protocol, Interface, Status)); + } + + return Status; +} + +/** + Wrapper function to UnitTestInstallProtocolInterfaceNotify. This is t= he public API which + Calls the private one which contains a BOOLEAN parameter for notificat= ions + + @param UserHandle The handle to install the protocol hand= ler on, + or NULL if a new handle is to be alloca= ted + @param Protocol The protocol to add to the handle + @param InterfaceType Indicates whether Interface is supplied= in + native form. + @param Interface The interface for the protocol being ad= ded + + @return Status code + +**/ +EFI_STATUS +EFIAPI +UnitTestInstallProtocolInterface ( + IN OUT EFI_HANDLE *UserHandle, + IN EFI_GUID *Protocol, + IN EFI_INTERFACE_TYPE InterfaceType, + IN VOID *Interface + ) +{ + return UnitTestInstallProtocolInterfaceNotify ( + UserHandle, + Protocol, + InterfaceType, + Interface, + TRUE + ); +} + +/** + Reinstall a protocol interface on a device handle. The OldInterface f= or Protocol is replaced by the NewInterface. + + @param UserHandle Handle on which the interface is to be + reinstalled + @param Protocol The numeric ID of the interface + @param OldInterface A pointer to the old interface + @param NewInterface A pointer to the new interface + + @retval EFI_SUCCESS The protocol interface was installed + @retval EFI_NOT_FOUND The OldInterface on the handle was not = found + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue + +**/ +EFI_STATUS +EFIAPI +UnitTestReinstallProtocolInterface ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + IN VOID *OldInterface, + IN VOID *NewInterface + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Uninstalls all instances of a protocol:interfacer from a handle. + If the last protocol interface is remove from the handle, the + handle is freed. + + @param UserHandle The handle to remove the protocol handl= er from + @param Protocol The protocol, of protocol:interface, to= remove + @param Interface The interface, of protocol:interface, t= o remove + + @retval EFI_INVALID_PARAMETER Protocol is NULL. + @retval EFI_SUCCESS Protocol interface successfully uninsta= lled. + +**/ +EFI_STATUS +EFIAPI +UnitTestUninstallProtocolInterface ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + IN VOID *Interface + ) +{ + EFI_STATUS Status; + IHANDLE *Handle; + PROTOCOL_INTERFACE *Prot; + + // + // Check that Protocol is valid + // + if (Protocol =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check that UserHandle is a valid handle + // + Status =3D UnitTestValidateHandle (UserHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Check that Protocol exists on UserHandle, and Interface matches the= interface in the database + // + Prot =3D UnitTestFindProtocolInterface (UserHandle, Protocol, Interfac= e); + if (Prot =3D=3D NULL) { + Status =3D EFI_NOT_FOUND; + goto Done; + } + + // + // Attempt to disconnect all drivers that are using the protocol inter= face that is about to be removed + // + Status =3D UnitTestDisconnectControllersUsingProtocolInterface ( + UserHandle, + Prot + ); + if (EFI_ERROR (Status)) { + // + // One or more drivers refused to release, so return the error + // + goto Done; + } + + // + // Remove the protocol interface from the protocol + // + Status =3D EFI_NOT_FOUND; + Handle =3D (IHANDLE *)UserHandle; + Prot =3D UnitTestRemoveInterfaceFromProtocol (Handle, Protocol, Inte= rface); + + if (Prot !=3D NULL) { + // + // Update the Key to show that the handle has been created/modified + // + gHandleDatabaseKey++; + Handle->Key =3D gHandleDatabaseKey; + + // + // Remove the protocol interface from the handle + // + RemoveEntryList (&Prot->Link); + + // + // Free the memory + // + Prot->Signature =3D 0; + FreePool (Prot); + Status =3D EFI_SUCCESS; + } + + // + // If there are no more handlers for the handle, free the handle + // + if (IsListEmpty (&Handle->Protocols)) { + Handle->Signature =3D 0; + RemoveEntryList (&Handle->AllHandles); + FreePool (Handle); + } + +Done: + return Status; +} + +/** + Queries a handle to determine if it supports a specified protocol. + + @param UserHandle The handle being queried. + @param Protocol The published unique identifier of the = protocol. + @param Interface Supplies the address where a pointer to= the + corresponding Protocol Interface is ret= urned. + + @return The requested protocol interface for the handle + +**/ +EFI_STATUS +EFIAPI +UnitTestHandleProtocol ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + OUT VOID **Interface + ) +{ + return UnitTestOpenProtocol ( + UserHandle, + Protocol, + Interface, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL + ); +} + +/** + Add a new protocol notification record for the request protocol. + + @param Protocol The requested protocol to add the notif= y + registration + @param Event The event to signal + @param Registration Returns the registration record + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_SUCCESS Successfully returned the registration = record + that has been added + +**/ +EFI_STATUS +EFIAPI +UnitTestRegisterProtocolNotify ( + IN EFI_GUID *Protocol, + IN EFI_EVENT Event, + OUT VOID **Registration + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Locates the requested handle(s) and returns them in Buffer. + + @param SearchType The type of search to perform to locate= the + handles + @param Protocol The protocol to search for + @param SearchKey Dependant on SearchType + @param BufferSize On input the size of Buffer. On output= the + size of data returned. + @param Buffer The buffer to return the results in + + @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size = is + returned in BufferSize. + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_SUCCESS Successfully found the requested handle= (s) and + returns them in Buffer. + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateHandle ( + IN EFI_LOCATE_SEARCH_TYPE SearchType, + IN EFI_GUID *Protocol OPTIONAL, + IN VOID *SearchKey OPTIONAL, + IN OUT UINTN *BufferSize, + OUT EFI_HANDLE *Buffer + ) +{ + EFI_STATUS Status; + LOCATE_POSITION Position; + PROTOCOL_NOTIFY *ProtNotify; + UNIT_TEST_GET_NEXT GetNext; + UINTN ResultSize; + IHANDLE *Handle; + IHANDLE **ResultBuffer; + VOID *Interface; + + if (BufferSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if ((*BufferSize > 0) && (Buffer =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + GetNext =3D NULL; + + // + // Set initial position + // + Position.Protocol =3D Protocol; + Position.SearchKey =3D SearchKey; + Position.Position =3D &gHandleList; + + ResultSize =3D 0; + ResultBuffer =3D (IHANDLE **)Buffer; + Status =3D EFI_SUCCESS; + + // + // Get the search function based on type + // + switch (SearchType) { + case AllHandles: + GetNext =3D UnitTestGetNextLocateAllHandles; + break; + + case ByRegisterNotify: + // + // Must have SearchKey for locate ByRegisterNotify + // + if (SearchKey =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + break; + } + + GetNext =3D UnitTestGetNextLocateByRegisterNotify; + break; + + case ByProtocol: + GetNext =3D UnitTestGetNextLocateByProtocol; + if (Protocol =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + break; + } + + // + // Look up the protocol entry and set the head pointer + // + Position.ProtEntry =3D UnitTestFindProtocolEntry (Protocol, FALSE)= ; + if (Position.ProtEntry =3D=3D NULL) { + Status =3D EFI_NOT_FOUND; + break; + } + + Position.Position =3D &Position.ProtEntry->Protocols; + break; + + default: + Status =3D EFI_INVALID_PARAMETER; + break; + } + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (GetNext !=3D NULL); + // + // Enumerate out the matching handles + // + mEfiLocateHandleRequest +=3D 1; + for ( ; ;) { + // + // Get the next handle. If no more handles, stop + // + Handle =3D GetNext (&Position, &Interface); + if (NULL =3D=3D Handle) { + break; + } + + // + // Increase the resulting buffer size, and if this handle + // fits return it + // + ResultSize +=3D sizeof (Handle); + if (ResultSize <=3D *BufferSize) { + *ResultBuffer =3D Handle; + ResultBuffer +=3D 1; + } + } + + // + // If the result is a zero length buffer, then there were no + // matching handles + // + if (ResultSize =3D=3D 0) { + Status =3D EFI_NOT_FOUND; + } else { + // + // Return the resulting buffer size. If it's larger than what + // was passed, then set the error code + // + if (ResultSize > *BufferSize) { + Status =3D EFI_BUFFER_TOO_SMALL; + } + + *BufferSize =3D ResultSize; + + if ((SearchType =3D=3D ByRegisterNotify) && !EFI_ERROR (Status)) { + // + // If this is a search by register notify and a handle was + // returned, update the register notification position + // + ASSERT (SearchKey !=3D NULL); + ProtNotify =3D SearchKey; + ProtNotify->Position =3D ProtNotify->Position->ForwardLink; + } + } + + return Status; +} + +/** + Locates the handle to a device on the device path that best matches th= e specified protocol. + + @param Protocol The protocol to search for. + @param DevicePath On input, a pointer to a pointer to the= device + path. On output, the device path pointe= r is + modified to point to the remaining part= of the + devicepath. + @param Device A pointer to the returned device handle= . + + @retval EFI_SUCCESS The resulting handle was returned. + @retval EFI_NOT_FOUND No handles matched the search. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue. + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateDevicePath ( + IN EFI_GUID *Protocol, + IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, + OUT EFI_HANDLE *Device + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Boot Service called to add, modify, or remove a system configuration t= able from + the EFI System Table. + + @param Guid Pointer to the GUID for the entry to add, updat= e, or + remove + @param Table Pointer to the configuration table for the entr= y to add, + update, or remove, may be NULL. + + @return EFI_SUCCESS Guid, Table pair added, updated, or = removed. + @return EFI_INVALID_PARAMETER Input GUID not valid. + @return EFI_NOT_FOUND Attempted to delete non-existant ent= ry + @return EFI_OUT_OF_RESOURCES Not enough memory available + +**/ +EFI_STATUS +EFIAPI +UnitTestInstallConfigurationTable ( + IN EFI_GUID *Guid, + IN VOID *Table + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Locates the installed protocol handler for the handle, and + invokes it to obtain the protocol interface. Usage information + is registered in the protocol data base. + + @param UserHandle The handle to obtain the protocol inter= face on + @param Protocol The ID of the protocol + @param Interface The location to return the protocol int= erface + @param ImageHandle The handle of the Image that is opening= the + protocol interface specified by Protoco= l and + Interface. + @param ControllerHandle The controller handle that is requiring= this + interface. + @param Attributes The open mode of the protocol interface + specified by Handle and Protocol. + + @retval EFI_INVALID_PARAMETER Protocol is NULL. + @retval EFI_SUCCESS Get the protocol interface. + +**/ +EFI_STATUS +EFIAPI +UnitTestOpenProtocol ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + OUT VOID **Interface OPTIONAL, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN UINT32 Attributes + ) +{ + EFI_STATUS Status; + PROTOCOL_INTERFACE *Prot; + LIST_ENTRY *Link; + OPEN_PROTOCOL_DATA *OpenData; + BOOLEAN ByDriver; + BOOLEAN Exclusive; + BOOLEAN Disconnect; + BOOLEAN ExactMatch; + + // + // Check for invalid Protocol + // + if (Protocol =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check for invalid Interface + // + if ((Attributes !=3D EFI_OPEN_PROTOCOL_TEST_PROTOCOL) && (Interface =3D= =3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Check for invalid UserHandle + // + Status =3D UnitTestValidateHandle (UserHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Check for invalid Attributes + // + switch (Attributes) { + case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: + Status =3D UnitTestValidateHandle (ImageHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D UnitTestValidateHandle (ControllerHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + if (UserHandle =3D=3D ControllerHandle) { + return EFI_INVALID_PARAMETER; + } + + break; + case EFI_OPEN_PROTOCOL_BY_DRIVER: + case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: + Status =3D UnitTestValidateHandle (ImageHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D UnitTestValidateHandle (ControllerHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + break; + case EFI_OPEN_PROTOCOL_EXCLUSIVE: + Status =3D UnitTestValidateHandle (ImageHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + break; + case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: + case EFI_OPEN_PROTOCOL_GET_PROTOCOL: + case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: + break; + default: + return EFI_INVALID_PARAMETER; + } + + // + // Look at each protocol interface for a match + // + Prot =3D UnitTestGetProtocolInterface (UserHandle, Protocol); + if (Prot =3D=3D NULL) { + Status =3D EFI_UNSUPPORTED; + goto Done; + } + + Status =3D EFI_SUCCESS; + + ByDriver =3D FALSE; + Exclusive =3D FALSE; + for ( Link =3D Prot->OpenList.ForwardLink; Link !=3D &Prot->OpenList; = Link =3D Link->ForwardLink) { + OpenData =3D CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DAT= A_SIGNATURE); + ExactMatch =3D (BOOLEAN)((OpenData->AgentHandle =3D=3D ImageHandle)= && + (OpenData->Attributes =3D=3D Attributes) && + (OpenData->ControllerHandle =3D=3D Controlle= rHandle)); + if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) !=3D 0) { + ByDriver =3D TRUE; + if (ExactMatch) { + Status =3D EFI_ALREADY_STARTED; + goto Done; + } + } + + if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) !=3D 0) { + Exclusive =3D TRUE; + } else if (ExactMatch) { + OpenData->OpenCount++; + Status =3D EFI_SUCCESS; + goto Done; + } + } + + // + // ByDriver TRUE -> A driver is managing (UserHandle, Protocol) + // ByDriver FALSE -> There are no drivers managing (UserHandle, Proto= col) + // Exclusive TRUE -> Something has exclusive access to (UserHandle, P= rotocol) + // Exclusive FALSE -> Nothing has exclusive access to (UserHandle, Pro= tocol) + // + + switch (Attributes) { + case EFI_OPEN_PROTOCOL_BY_DRIVER: + if (Exclusive || ByDriver) { + Status =3D EFI_ACCESS_DENIED; + goto Done; + } + + break; + case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: + case EFI_OPEN_PROTOCOL_EXCLUSIVE: + if (Exclusive) { + Status =3D EFI_ACCESS_DENIED; + goto Done; + } + + if (ByDriver) { + do { + Disconnect =3D FALSE; + for (Link =3D Prot->OpenList.ForwardLink; Link !=3D &Prot->Ope= nList; Link =3D Link->ForwardLink) { + OpenData =3D CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOC= OL_DATA_SIGNATURE); + if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) !=3D= 0) { + Disconnect =3D TRUE; + Status =3D UnitTestDisconnectController (UserHandle, O= penData->AgentHandle, NULL); + if (EFI_ERROR (Status)) { + Status =3D EFI_ACCESS_DENIED; + goto Done; + } else { + break; + } + } + } + } while (Disconnect); + } + + break; + case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: + case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: + case EFI_OPEN_PROTOCOL_GET_PROTOCOL: + case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: + break; + } + + if (ImageHandle =3D=3D NULL) { + Status =3D EFI_SUCCESS; + goto Done; + } + + // + // Create new entry + // + OpenData =3D AllocatePool (sizeof (OPEN_PROTOCOL_DATA)); + if (OpenData =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + } else { + OpenData->Signature =3D OPEN_PROTOCOL_DATA_SIGNATURE; + OpenData->AgentHandle =3D ImageHandle; + OpenData->ControllerHandle =3D ControllerHandle; + OpenData->Attributes =3D Attributes; + OpenData->OpenCount =3D 1; + InsertTailList (&Prot->OpenList, &OpenData->Link); + Prot->OpenListCount++; + Status =3D EFI_SUCCESS; + } + +Done: + + if (Attributes !=3D EFI_OPEN_PROTOCOL_TEST_PROTOCOL) { + // + // Keep Interface unmodified in case of any Error + // except EFI_ALREADY_STARTED and EFI_UNSUPPORTED. + // + if (!EFI_ERROR (Status) || (Status =3D=3D EFI_ALREADY_STARTED)) { + // + // According to above logic, if 'Prot' is NULL, then the 'Status' = must be + // EFI_UNSUPPORTED. Here the 'Status' is not EFI_UNSUPPORTED, so '= Prot' + // must be not NULL. + // + // The ASSERT here is for addressing a false positive NULL pointer + // dereference issue raised from static analysis. + // + ASSERT (Prot !=3D NULL); + // + // EFI_ALREADY_STARTED is not an error for bus driver. + // Return the corresponding protocol interface. + // + *Interface =3D Prot->Interface; + } else if (Status =3D=3D EFI_UNSUPPORTED) { + // + // Return NULL Interface if Unsupported Protocol. + // + *Interface =3D NULL; + } + } + + return Status; +} + +/** + Closes a protocol on a handle that was opened using OpenProtocol(). + + @param UserHandle The handle for the protocol interface t= hat was + previously opened with OpenProtocol(), = and is + now being closed. + @param Protocol The published unique identifier of the = protocol. + It is the caller's responsibility to pa= ss in a + valid GUID. + @param AgentHandle The handle of the agent that is closing= the + protocol interface. + @param ControllerHandle If the agent that opened a protocol is = a driver + that follows the EFI Driver Model, then= this + parameter is the controller handle that= required + the protocol interface. If the agent do= es not + follow the EFI Driver Model, then this = parameter + is optional and may be NULL. + + @retval EFI_SUCCESS The protocol instance was closed. + @retval EFI_INVALID_PARAMETER Handle, AgentHandle or ControllerHandle= is not a + valid EFI_HANDLE. + @retval EFI_NOT_FOUND Can not find the specified protocol or + AgentHandle. + +**/ +EFI_STATUS +EFIAPI +UnitTestCloseProtocol ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + IN EFI_HANDLE AgentHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Return information about Opened protocols in the system + + @param UserHandle The handle to close the protocol interf= ace on + @param Protocol The ID of the protocol + @param EntryBuffer A pointer to a buffer of open protocol + information in the form of + EFI_OPEN_PROTOCOL_INFORMATION_ENTRY str= uctures. + @param EntryCount Number of EntryBuffer entries + +**/ +EFI_STATUS +EFIAPI +UnitTestOpenProtocolInformation ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + OUT EFI_OPEN_PROTOCOL_INFORMATION_ENTRY **EntryBuffer, + OUT UINTN *EntryCount + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Retrieves the list of protocol interface GUIDs that are installed on a= handle in a buffer allocated + from pool. + + @param UserHandle The handle from which to retrieve the l= ist of + protocol interface GUIDs. + @param ProtocolBuffer A pointer to the list of protocol inter= face GUID + pointers that are installed on Handle. + @param ProtocolBufferCount A pointer to the number of GUID pointer= s present + in ProtocolBuffer. + + @retval EFI_SUCCESS The list of protocol interface GUIDs in= stalled + on Handle was returned in ProtocolBuffe= r. The + number of protocol interface GUIDs was = returned + in ProtocolBufferCount. + @retval EFI_INVALID_PARAMETER Handle is NULL. + @retval EFI_INVALID_PARAMETER Handle is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER ProtocolBuffer is NULL. + @retval EFI_INVALID_PARAMETER ProtocolBufferCount is NULL. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to stor= e the + results. + +**/ +EFI_STATUS +EFIAPI +UnitTestProtocolsPerHandle ( + IN EFI_HANDLE UserHandle, + OUT EFI_GUID ***ProtocolBuffer, + OUT UINTN *ProtocolBufferCount + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Function returns an array of handles that support the requested protoc= ol + in a buffer allocated from pool. This is a version of UnitTestLocateHa= ndle() + that allocates a buffer for the caller. + + @param SearchType Specifies which handle(s) are to be ret= urned. + @param Protocol Provides the protocol to search by. = This + parameter is only valid for SearchType + ByProtocol. + @param SearchKey Supplies the search key depending on th= e + SearchType. + @param NumberHandles The number of handles returned in Buffe= r. + @param Buffer A pointer to the buffer to return the r= equested + array of handles that support Protocol= . + + @retval EFI_SUCCESS The result array of handles was returne= d. + @retval EFI_NOT_FOUND No handles match the search. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to stor= e the + matching results. + @retval EFI_INVALID_PARAMETER One or more parameters are not valid. + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateHandleBuffer ( + IN EFI_LOCATE_SEARCH_TYPE SearchType, + IN EFI_GUID *Protocol OPTIONAL, + IN VOID *SearchKey OPTIONAL, + IN OUT UINTN *NumberHandles, + OUT EFI_HANDLE **Buffer + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + + if (NumberHandles =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + BufferSize =3D 0; + *NumberHandles =3D 0; + *Buffer =3D NULL; + Status =3D UnitTestLocateHandle ( + SearchType, + Protocol, + SearchKey, + &BufferSize, + *Buffer + ); + // + // LocateHandleBuffer() returns incorrect status code if SearchType is + // invalid. + // + // Add code to correctly handle expected errors from UnitTestLocateHan= dle(). + // + if (EFI_ERROR (Status) && (Status !=3D EFI_BUFFER_TOO_SMALL)) { + if (Status !=3D EFI_INVALID_PARAMETER) { + Status =3D EFI_NOT_FOUND; + } + + return Status; + } + + *Buffer =3D AllocatePool (BufferSize); + if (*Buffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status =3D UnitTestLocateHandle ( + SearchType, + Protocol, + SearchKey, + &BufferSize, + *Buffer + ); + + *NumberHandles =3D BufferSize / sizeof (EFI_HANDLE); + if (EFI_ERROR (Status)) { + *NumberHandles =3D 0; + } + + return Status; +} + +/** + Return the first Protocol Interface that matches the Protocol GUID. If + Registration is passed in, return a Protocol Instance that was just ad= d + to the system. If Registration is NULL return the first Protocol Inter= face + you find. + + @param Protocol The protocol to search for + @param Registration Optional Registration Key returned from + RegisterProtocolNotify() + @param Interface Return the Protocol interface (instance= ). + + @retval EFI_SUCCESS If a valid Interface is returned + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_NOT_FOUND Protocol interface not found + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateProtocol ( + IN EFI_GUID *Protocol, + IN VOID *Registration OPTIONAL, + OUT VOID **Interface + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Installs a list of protocol interface into the boot services environme= nt. + This function calls InstallProtocolInterface() in a loop. If any error + occurs all the protocols added by this function are removed. This is + basically a lib function to save space. + + @param Handle The handle to install the protocol hand= lers on, + or NULL if a new handle is to be alloca= ted + @param ... EFI_GUID followed by protocol instance.= A NULL + terminates the list. The pairs are the + arguments to InstallProtocolInterface()= . All the + protocols are added to Handle. + + @retval EFI_SUCCESS All the protocol interface was installe= d. + @retval EFI_OUT_OF_RESOURCES There was not enough memory in pool to = install all the protocols. + @retval EFI_ALREADY_STARTED A Device Path Protocol instance was pas= sed in that is already present in + the handle database. + @retval EFI_INVALID_PARAMETER Handle is NULL. + @retval EFI_INVALID_PARAMETER Protocol is already installed on the ha= ndle specified by Handle. + +**/ +EFI_STATUS +EFIAPI +UnitTestInstallMultipleProtocolInterfaces ( + IN OUT EFI_HANDLE *Handle, + ... + ) +{ + return EFI_NOT_AVAILABLE_YET; +} + +/** + Uninstalls a list of protocol interface in the boot services environme= nt. + This function calls UninstallProtocolInterface() in a loop. This is + basically a lib function to save space. + + @param Handle The handle to uninstall the protocol + @param ... EFI_GUID followed by protocol instance.= A NULL + terminates the list. The pairs are the + arguments to UninstallProtocolInterface= (). All + the protocols are added to Handle. + + @return Status code + +**/ +EFI_STATUS +EFIAPI +UnitTestUninstallMultipleProtocolInterfaces ( + IN EFI_HANDLE Handle, + ... + ) +{ + EFI_STATUS Status; + VA_LIST Args; + EFI_GUID *Protocol; + VOID *Interface; + UINTN Index; + + VA_START (Args, Handle); + for (Index =3D 0, Status =3D EFI_SUCCESS; !EFI_ERROR (Status); Index++= ) { + // + // If protocol is NULL, then it's the end of the list + // + Protocol =3D VA_ARG (Args, EFI_GUID *); + if (Protocol =3D=3D NULL) { + break; + } + + Interface =3D VA_ARG (Args, VOID *); + + // + // Uninstall it + // + Status =3D UnitTestUninstallProtocolInterface (Handle, Protocol, Int= erface); + } + + VA_END (Args); + + // + // If there was an error, add all the interfaces that were + // uninstalled without any errors + // + if (EFI_ERROR (Status)) { + // + // Reset the va_arg back to the first argument. + // + VA_START (Args, Handle); + for ( ; Index > 1; Index--) { + Protocol =3D VA_ARG (Args, EFI_GUID *); + Interface =3D VA_ARG (Args, VOID *); + UnitTestInstallProtocolInterface (&Handle, Protocol, EFI_NATIVE_IN= TERFACE, Interface); + } + + VA_END (Args); + Status =3D EFI_INVALID_PARAMETER; + } + + return Status; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibTpl.c b/UnitTestFrameworkPkg/Library/Un= itTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibTpl.c new file mode 100644 index 000000000000..2837a463aa14 --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibTpl.c @@ -0,0 +1,43 @@ +/** @file + Implementation of Task Priority Level (TPL) related services in the UE= FI Boot Services table for use in unit tests. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UnitTestUefiBootServicesTableLib.h" + +/** + Raise the task priority level to the new level. + High level is implemented by disabling processor interrupts. + + @param NewTpl New task priority level + + @return The previous task priority level + +**/ +EFI_TPL +EFIAPI +UnitTestRaiseTpl ( + IN EFI_TPL NewTpl + ) +{ + return TPL_APPLICATION; +} + +/** + Lowers the task priority to the previous value. If the new + priority unmasks events at a higher priority, they are dispatched. + + @param NewTpl New, lower, task priority + +**/ +VOID +EFIAPI +UnitTestRestoreTpl ( + IN EFI_TPL NewTpl + ) +{ + return; +} diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLib.h b/UnitTestFrameworkPkg/Library/UnitT= estUefiBootServicesTableLib/UnitTestUefiBootServicesTableLib.h new file mode 100644 index 000000000000..82853bb61a1c --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLib.h @@ -0,0 +1,1042 @@ +/** @file + An internal header file for the Unit Test instance of the UEFI Boot Se= rvices Table Library. + + This file includes common header files, defines internal structure and= functions used by + the library implementation. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_H_ +#define UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_H_ + +#include + +#include +#include +#include +#include +#include +#include +#include + +/** + Raise the task priority level to the new level. + High level is implemented by disabling processor interrupts. + + @param NewTpl New task priority level + + @return The previous task priority level + +**/ +EFI_TPL +EFIAPI +UnitTestRaiseTpl ( + IN EFI_TPL NewTpl + ); + +/** + Lowers the task priority to the previous value. If the new + priority unmasks events at a higher priority, they are dispatched. + + @param NewTpl New, lower, task priority + +**/ +VOID +EFIAPI +UnitTestRestoreTpl ( + IN EFI_TPL NewTpl + ); + +/** + Allocates pages from the memory map. + + @param Type The type of allocation to perform + @param MemoryType The type of memory to turn the allocate= d pages + into + @param NumberOfPages The number of pages to allocate + @param Memory A pointer to receive the base allocated= memory + address + + @return Status. On success, Memory is filled in with the base address = allocated + @retval EFI_INVALID_PARAMETER Parameters violate checking rules defin= ed in + spec. + @retval EFI_NOT_FOUND Could not allocate pages match the requ= irement. + @retval EFI_OUT_OF_RESOURCES No enough pages to allocate. + @retval EFI_SUCCESS Pages successfully allocated. + +**/ +EFI_STATUS +EFIAPI +UnitTestAllocatePages ( + IN EFI_ALLOCATE_TYPE Type, + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN NumberOfPages, + IN OUT EFI_PHYSICAL_ADDRESS *Memory + ); + +/** + Frees previous allocated pages. + + @param Memory Base address of memory being freed + @param NumberOfPages The number of pages to free + + @retval EFI_NOT_FOUND Could not find the entry that covers th= e range + @retval EFI_INVALID_PARAMETER Address not aligned + @return EFI_SUCCESS -Pages successfully freed. + +**/ +EFI_STATUS +EFIAPI +UnitTestFreePages ( + IN EFI_PHYSICAL_ADDRESS Memory, + IN UINTN NumberOfPages + ); + +/** + This function returns a copy of the current memory map. The map is an = array of + memory descriptors, each of which describes a contiguous block of memo= ry. + + @param MemoryMapSize A pointer to the size, in bytes, of the + MemoryMap buffer. On input, this is the= size of + the buffer allocated by the caller. On= output, + it is the size of the buffer returned b= y the + firmware if the buffer was large enoug= h, or the + size of the buffer needed to contain t= he map if + the buffer was too small. + @param MemoryMap A pointer to the buffer in which firmwa= re places + the current memory map. + @param MapKey A pointer to the location in which firm= ware + returns the key for the current memory = map. + @param DescriptorSize A pointer to the location in which firm= ware + returns the size, in bytes, of an indiv= idual + EFI_MEMORY_DESCRIPTOR. + @param DescriptorVersion A pointer to the location in which firm= ware + returns the version number associated w= ith the + EFI_MEMORY_DESCRIPTOR. + + @retval EFI_SUCCESS The memory map was returned in the Memo= ryMap + buffer. + @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The= current + buffer size needed to hold the memory m= ap is + returned in MemoryMapSize. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue. + +**/ +EFI_STATUS +EFIAPI +UnitTestGetMemoryMap ( + IN OUT UINTN *MemoryMapSize, + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + OUT UINTN *MapKey, + OUT UINTN *DescriptorSize, + OUT UINT32 *DescriptorVersion + ); + +/** + Allocate pool of a particular type. + + @param PoolType Type of pool to allocate + @param Size The amount of pool to allocate + @param Buffer The address to return a pointer to the = allocated + pool + + @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL + @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocatio= n failed. + @retval EFI_SUCCESS Pool successfully allocated. + +**/ +EFI_STATUS +EFIAPI +UnitTestAllocatePool ( + IN EFI_MEMORY_TYPE PoolType, + IN UINTN Size, + OUT VOID **Buffer + ); + +/** + Frees pool. + + @param Buffer The allocated pool entry to free + + @retval EFI_INVALID_PARAMETER Buffer is not a valid value. + @retval EFI_SUCCESS Pool successfully freed. + +**/ +EFI_STATUS +EFIAPI +UnitTestFreePool ( + IN VOID *Buffer + ); + +/** + Frees pool. + + @param Buffer The allocated pool entry to free + @param PoolType Pointer to pool type + + @retval EFI_INVALID_PARAMETER Buffer is not a valid value. + @retval EFI_SUCCESS Pool successfully freed. + +**/ +EFI_STATUS +EFIAPI +UnitTestInternalFreePool ( + IN VOID *Buffer, + OUT EFI_MEMORY_TYPE *PoolType OPTIONAL + ); + +/** + Creates an event. + + @param Type The type of event to create and its mod= e and + attributes + @param NotifyTpl The task priority level of event notifi= cations + @param NotifyFunction Pointer to the events notification func= tion + @param NotifyContext Pointer to the notification functions c= ontext; + corresponds to parameter "Context" in t= he + notification function + @param Event Pointer to the newly created event if t= he call + succeeds; undefined otherwise + + @retval EFI_SUCCESS The event structure was created + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue + @retval EFI_OUT_OF_RESOURCES The event could not be allocated + +**/ +EFI_STATUS +EFIAPI +UnitTestCreateEvent ( + IN UINT32 Type, + IN EFI_TPL NotifyTpl, + IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL + IN VOID *NotifyContext, OPTIONAL + OUT EFI_EVENT *Event + ); + +/** + Sets the type of timer and the trigger time for a timer event. + + @param UserEvent The timer event that is to be signaled = at the + specified time + @param Type The type of time that is specified in + TriggerTime + @param TriggerTime The number of 100ns units until the tim= er + expires + + @retval EFI_SUCCESS The event has been set to be signaled a= t the + requested time + @retval EFI_INVALID_PARAMETER Event or Type is not valid + +**/ +EFI_STATUS +EFIAPI +UnitTestSetTimer ( + IN EFI_EVENT UserEvent, + IN EFI_TIMER_DELAY Type, + IN UINT64 TriggerTime + ); + +/** + Stops execution until an event is signaled. + + @param NumberOfEvents The number of events in the UserEvents = array + @param UserEvents An array of EFI_EVENT + @param UserIndex Pointer to the index of the event which + satisfied the wait condition + + @retval EFI_SUCCESS The event indicated by Index was signal= ed. + @retval EFI_INVALID_PARAMETER The event indicated by Index has a noti= fication + function or Event was not a valid type + @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION + +**/ +EFI_STATUS +EFIAPI +UnitTestWaitForEvent ( + IN UINTN NumberOfEvents, + IN EFI_EVENT *UserEvents, + OUT UINTN *UserIndex + ); + +/** + Signals the event. Queues the event to be notified if needed. + + @param UserEvent The event to signal . + + @retval EFI_INVALID_PARAMETER Parameters are not valid. + @retval EFI_SUCCESS The event was signaled. + +**/ +EFI_STATUS +EFIAPI +UnitTestSignalEvent ( + IN EFI_EVENT UserEvent + ); + +/** + Closes an event and frees the event structure. + + @param UserEvent Event to close + + @retval EFI_INVALID_PARAMETER Parameters are not valid. + @retval EFI_SUCCESS The event has been closed + +**/ +EFI_STATUS +EFIAPI +UnitTestCloseEvent ( + IN EFI_EVENT UserEvent + ); + +/** + Check the status of an event. + + @param UserEvent The event to check + + @retval EFI_SUCCESS The event is in the signaled state + @retval EFI_NOT_READY The event is not in the signaled state + @retval EFI_INVALID_PARAMETER Event is of type EVT_NOTIFY_SIGNAL + +**/ +EFI_STATUS +EFIAPI +UnitTestCheckEvent ( + IN EFI_EVENT UserEvent + ); + +/** + Wrapper function to UnitTestInstallProtocolInterfaceNotify. This is t= he public API which + Calls the private one which contains a BOOLEAN parameter for notificat= ions + + @param UserHandle The handle to install the protocol hand= ler on, + or NULL if a new handle is to be alloca= ted + @param Protocol The protocol to add to the handle + @param InterfaceType Indicates whether Interface is supplied= in + native form. + @param Interface The interface for the protocol being ad= ded + + @return Status code + +**/ +EFI_STATUS +EFIAPI +UnitTestInstallProtocolInterface ( + IN OUT EFI_HANDLE *UserHandle, + IN EFI_GUID *Protocol, + IN EFI_INTERFACE_TYPE InterfaceType, + IN VOID *Interface + ); + +/** + Reinstall a protocol interface on a device handle. The OldInterface f= or Protocol is replaced by the NewInterface. + + @param UserHandle Handle on which the interface is to be + reinstalled + @param Protocol The numeric ID of the interface + @param OldInterface A pointer to the old interface + @param NewInterface A pointer to the new interface + + @retval EFI_SUCCESS The protocol interface was installed + @retval EFI_NOT_FOUND The OldInterface on the handle was not = found + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue + +**/ +EFI_STATUS +EFIAPI +UnitTestReinstallProtocolInterface ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + IN VOID *OldInterface, + IN VOID *NewInterface + ); + +/** + Uninstalls all instances of a protocol:interfacer from a handle. + If the last protocol interface is remove from the handle, the + handle is freed. + + @param UserHandle The handle to remove the protocol handl= er from + @param Protocol The protocol, of protocol:interface, to= remove + @param Interface The interface, of protocol:interface, t= o remove + + @retval EFI_INVALID_PARAMETER Protocol is NULL. + @retval EFI_SUCCESS Protocol interface successfully uninsta= lled. + +**/ +EFI_STATUS +EFIAPI +UnitTestUninstallProtocolInterface ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + IN VOID *Interface + ); + +/** + Queries a handle to determine if it supports a specified protocol. + + @param UserHandle The handle being queried. + @param Protocol The published unique identifier of the = protocol. + @param Interface Supplies the address where a pointer to= the + corresponding Protocol Interface is ret= urned. + + @return The requested protocol interface for the handle + +**/ +EFI_STATUS +EFIAPI +UnitTestHandleProtocol ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + OUT VOID **Interface + ); + +/** + Add a new protocol notification record for the request protocol. + + @param Protocol The requested protocol to add the notif= y + registration + @param Event The event to signal + @param Registration Returns the registration record + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_SUCCESS Successfully returned the registration = record + that has been added + +**/ +EFI_STATUS +EFIAPI +UnitTestRegisterProtocolNotify ( + IN EFI_GUID *Protocol, + IN EFI_EVENT Event, + OUT VOID **Registration + ); + +/** + Locates the requested handle(s) and returns them in Buffer. + + @param SearchType The type of search to perform to locate= the + handles + @param Protocol The protocol to search for + @param SearchKey Dependant on SearchType + @param BufferSize On input the size of Buffer. On output= the + size of data returned. + @param Buffer The buffer to return the results in + + @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size = is + returned in BufferSize. + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_SUCCESS Successfully found the requested handle= (s) and + returns them in Buffer. + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateHandle ( + IN EFI_LOCATE_SEARCH_TYPE SearchType, + IN EFI_GUID *Protocol OPTIONAL, + IN VOID *SearchKey OPTIONAL, + IN OUT UINTN *BufferSize, + OUT EFI_HANDLE *Buffer + ); + +/** + Locates the handle to a device on the device path that best matches th= e specified protocol. + + @param Protocol The protocol to search for. + @param DevicePath On input, a pointer to a pointer to the= device + path. On output, the device path pointe= r is + modified to point to the remaining part= of the + devicepath. + @param Device A pointer to the returned device handle= . + + @retval EFI_SUCCESS The resulting handle was returned. + @retval EFI_NOT_FOUND No handles matched the search. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue. + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateDevicePath ( + IN EFI_GUID *Protocol, + IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, + OUT EFI_HANDLE *Device + ); + +/** + Boot Service called to add, modify, or remove a system configuration t= able from + the EFI System Table. + + @param Guid Pointer to the GUID for the entry to add, updat= e, or + remove + @param Table Pointer to the configuration table for the entr= y to add, + update, or remove, may be NULL. + + @return EFI_SUCCESS Guid, Table pair added, updated, or = removed. + @return EFI_INVALID_PARAMETER Input GUID not valid. + @return EFI_NOT_FOUND Attempted to delete non-existant ent= ry + @return EFI_OUT_OF_RESOURCES Not enough memory available + +**/ +EFI_STATUS +EFIAPI +UnitTestInstallConfigurationTable ( + IN EFI_GUID *Guid, + IN VOID *Table + ); + +/** + Loads an EFI image into memory and returns a handle to the image. + + @param BootPolicy If TRUE, indicates that the request or= iginates + from the boot manager, and that the bo= ot + manager is attempting to load FilePath= as a + boot selection. + @param ParentImageHandle The caller's image handle. + @param FilePath The specific file path from which the = image is + loaded. + @param SourceBuffer If not NULL, a pointer to the memory l= ocation + containing a copy of the image to be l= oaded. + @param SourceSize The size in bytes of SourceBuffer. + @param ImageHandle Pointer to the returned image handle t= hat is + created when the image is successfully= loaded. + + @retval EFI_SUCCESS The image was loaded into memory. + @retval EFI_NOT_FOUND The FilePath was not found. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid v= alue. + @retval EFI_UNSUPPORTED The image type is not supported, or th= e device + path cannot be parsed to locate the pr= oper + protocol for loading the file. + @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficie= nt + resources. + @retval EFI_LOAD_ERROR Image was not loaded because the image= format was corrupt or not + understood. + @retval EFI_DEVICE_ERROR Image was not loaded because the devic= e returned a read error. + @retval EFI_ACCESS_DENIED Image was not loaded because the platf= orm policy prohibits the + image from being loaded. NULL is retur= ned in *ImageHandle. + @retval EFI_SECURITY_VIOLATION Image was loaded and an ImageHandle wa= s created with a + valid EFI_LOADED_IMAGE_PROTOCOL. Howev= er, the current + platform policy specifies that the ima= ge should not be started. + +**/ +EFI_STATUS +EFIAPI +UnitTestLoadImage ( + IN BOOLEAN BootPolicy, + IN EFI_HANDLE ParentImageHandle, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN VOID *SourceBuffer OPTIONAL, + IN UINTN SourceSize, + OUT EFI_HANDLE *ImageHandle + ); + +/** + Transfer control to a loaded image's entry point. + + @param ImageHandle Handle of image to be started. + @param ExitDataSize Pointer of the size to ExitData + @param ExitData Pointer to a pointer to a data buffer = that + includes a Null-terminated string, + optionally followed by additional bina= ry data. + The string is a description that the c= aller may + use to further indicate the reason for= the + image's exit. + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate + @retval EFI_SECURITY_VIOLATION The current platform policy specifies = that the image should not be started. + @retval EFI_SUCCESS Successfully transfer control to the i= mage's + entry point. + +**/ +EFI_STATUS +EFIAPI +UnitTestStartImage ( + IN EFI_HANDLE ImageHandle, + OUT UINTN *ExitDataSize, + OUT CHAR16 **ExitData OPTIONAL + ); + +/** + Terminates the currently loaded EFI image and returns control to boot = services. + + @param ImageHandle Handle that identifies the image. This + parameter is passed to the image on en= try. + @param Status The image's exit code. + @param ExitDataSize The size, in bytes, of ExitData. Ignor= ed if + ExitStatus is EFI_SUCCESS. + @param ExitData Pointer to a data buffer that includes= a + Null-terminated Unicode string, option= ally + followed by additional binary data. Th= e string + is a description that the caller may u= se to + further indicate the reason for the im= age's + exit. + + @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not curr= ent + image. + @retval EFI_SUCCESS Successfully terminates the currently = loaded + EFI image. + @retval EFI_ACCESS_DENIED Should never reach there. + @retval EFI_OUT_OF_RESOURCES Could not allocate pool + +**/ +EFI_STATUS +EFIAPI +UnitTestExit ( + IN EFI_HANDLE ImageHandle, + IN EFI_STATUS Status, + IN UINTN ExitDataSize, + IN CHAR16 *ExitData OPTIONAL + ); + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be + unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_UNSUPPORTED The image has been started, and does n= ot support + unload. + @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handl= e. + +**/ +EFI_STATUS +EFIAPI +UnitTestUnloadImage ( + IN EFI_HANDLE ImageHandle + ); + +/** + Terminates all boot services. + + @param ImageHandle Handle that identifies the exiting imag= e. + @param MapKey Key to the latest memory map. + + @retval EFI_SUCCESS Boot Services terminated + @retval EFI_INVALID_PARAMETER MapKey is incorrect. + +**/ +EFI_STATUS +EFIAPI +UnitTestExitBootServices ( + IN EFI_HANDLE ImageHandle, + IN UINTN MapKey + ); + +/** + Returns a monotonically increasing count for the platform. + + @param[out] Count The pointer to returned value. + + @retval EFI_SUCCESS The next monotonic count was returned. + @retval EFI_INVALID_PARAMETER Count is NULL. + @retval EFI_DEVICE_ERROR The device is not functioning properly. + +**/ +EFI_STATUS +EFIAPI +UnitTestGetNextMonotonicCount ( + OUT UINT64 *Count + ); + +/** + Introduces a fine-grained stall. + + @param Microseconds The number of microseconds to stall exe= cution. + + @retval EFI_SUCCESS Execution was stalled for at least the = requested + amount of microseconds. + @retval EFI_NOT_AVAILABLE_YET gMetronome is not available yet + +**/ +EFI_STATUS +EFIAPI +UnitTestStall ( + IN UINTN Microseconds + ); + +/** + Sets the system's watchdog timer. + + @param Timeout The number of seconds to set the watchdog time= r to. + A value of zero disables the timer. + @param WatchdogCode The numeric code to log on a watchdog timer ti= meout + event. The firmware reserves codes 0x0000 to 0= xFFFF. + Loaders and operating systems may use other ti= meout + codes. + @param DataSize The size, in bytes, of WatchdogData. + @param WatchdogData A data buffer that includes a Null-terminated = Unicode + string, optionally followed by additional bina= ry data. + The string is a description that the call may = use to + further indicate the reason to be logged with = a + watchdog event. + + @return EFI_SUCCESS Timeout has been set + @return EFI_NOT_AVAILABLE_YET WatchdogTimer is not available yet + @return EFI_UNSUPPORTED System does not have a timer (curren= tly not used) + @return EFI_DEVICE_ERROR Could not complete due to hardware e= rror + +**/ +EFI_STATUS +EFIAPI +UnitTestSetWatchdogTimer ( + IN UINTN Timeout, + IN UINT64 WatchdogCode, + IN UINTN DataSize, + IN CHAR16 *WatchdogData OPTIONAL + ); + +/** + Connects one or more drivers to a controller. + + @param ControllerHandle The handle of the controller to which dr= iver(s) are to be connected. + @param DriverImageHandle A pointer to an ordered list handles tha= t support the + EFI_DRIVER_BINDING_PROTOCOL. + @param RemainingDevicePath A pointer to the device path that specif= ies a child of the + controller specified by ControllerHandle= . + @param Recursive If TRUE, then ConnectController() is cal= led recursively + until the entire tree of controllers bel= ow the controller specified + by ControllerHandle have been created. I= f FALSE, then + the tree of controllers is only expanded= one level. + + @retval EFI_SUCCESS 1) One or more drivers were connected to= ControllerHandle. + 2) No drivers were connected to Controll= erHandle, but + RemainingDevicePath is not NULL, and it = is an End Device + Path Node. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_NOT_FOUND 1) There are no EFI_DRIVER_BINDING_PROTO= COL instances + present in the system. + 2) No drivers were connected to Controll= erHandle. + @retval EFI_SECURITY_VIOLATION + The user has no permission to start UEFI= device drivers on the device path + associated with the ControllerHandle or = specified by the RemainingDevicePath. + +**/ +EFI_STATUS +EFIAPI +UnitTestConnectController ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE *DriverImageHandle OPTIONAL, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN BOOLEAN Recursive + ); + +/** + Disconnects a controller from a driver + + @param ControllerHandle ControllerHandle The han= dle of + the controller from whic= h + driver(s) are to be + disconnected. + @param DriverImageHandle DriverImageHandle The dr= iver to + disconnect from Controll= erHandle. + @param ChildHandle ChildHandle The handle o= f the + child to destroy. + + @retval EFI_SUCCESS One or more drivers were + disconnected from the co= ntroller. + @retval EFI_SUCCESS On entry, no drivers are= managing + ControllerHandle. + @retval EFI_SUCCESS DriverImageHandle is not= NULL, + and on entry DriverImage= Handle is + not managing ControllerH= andle. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL= . + @retval EFI_INVALID_PARAMETER DriverImageHandle is not= NULL, + and it is not a valid EF= I_HANDLE. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL,= and it + is not a valid EFI_HANDL= E. + @retval EFI_OUT_OF_RESOURCES There are not enough res= ources + available to disconnect = any + drivers from ControllerH= andle. + @retval EFI_DEVICE_ERROR The controller could not= be + disconnected because of = a device + error. + +**/ +EFI_STATUS +EFIAPI +UnitTestDisconnectController ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE DriverImageHandle OPTIONAL, + IN EFI_HANDLE ChildHandle OPTIONAL + ); + +/** + Locates the installed protocol handler for the handle, and + invokes it to obtain the protocol interface. Usage information + is registered in the protocol data base. + + @param UserHandle The handle to obtain the protocol inter= face on + @param Protocol The ID of the protocol + @param Interface The location to return the protocol int= erface + @param ImageHandle The handle of the Image that is opening= the + protocol interface specified by Protoco= l and + Interface. + @param ControllerHandle The controller handle that is requiring= this + interface. + @param Attributes The open mode of the protocol interface + specified by Handle and Protocol. + + @retval EFI_INVALID_PARAMETER Protocol is NULL. + @retval EFI_SUCCESS Get the protocol interface. + +**/ +EFI_STATUS +EFIAPI +UnitTestOpenProtocol ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + OUT VOID **Interface OPTIONAL, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN UINT32 Attributes + ); + +/** + Closes a protocol on a handle that was opened using OpenProtocol(). + + @param UserHandle The handle for the protocol interface t= hat was + previously opened with OpenProtocol(), = and is + now being closed. + @param Protocol The published unique identifier of the = protocol. + It is the caller's responsibility to pa= ss in a + valid GUID. + @param AgentHandle The handle of the agent that is closing= the + protocol interface. + @param ControllerHandle If the agent that opened a protocol is = a driver + that follows the EFI Driver Model, then= this + parameter is the controller handle that= required + the protocol interface. If the agent do= es not + follow the EFI Driver Model, then this = parameter + is optional and may be NULL. + + @retval EFI_SUCCESS The protocol instance was closed. + @retval EFI_INVALID_PARAMETER Handle, AgentHandle or ControllerHandle= is not a + valid EFI_HANDLE. + @retval EFI_NOT_FOUND Can not find the specified protocol or + AgentHandle. + +**/ +EFI_STATUS +EFIAPI +UnitTestCloseProtocol ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + IN EFI_HANDLE AgentHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + Return information about Opened protocols in the system + + @param UserHandle The handle to close the protocol interf= ace on + @param Protocol The ID of the protocol + @param EntryBuffer A pointer to a buffer of open protocol + information in the form of + EFI_OPEN_PROTOCOL_INFORMATION_ENTRY str= uctures. + @param EntryCount Number of EntryBuffer entries + +**/ +EFI_STATUS +EFIAPI +UnitTestOpenProtocolInformation ( + IN EFI_HANDLE UserHandle, + IN EFI_GUID *Protocol, + OUT EFI_OPEN_PROTOCOL_INFORMATION_ENTRY **EntryBuffer, + OUT UINTN *EntryCount + ); + +/** + Retrieves the list of protocol interface GUIDs that are installed on a= handle in a buffer allocated + from pool. + + @param UserHandle The handle from which to retrieve the l= ist of + protocol interface GUIDs. + @param ProtocolBuffer A pointer to the list of protocol inter= face GUID + pointers that are installed on Handle. + @param ProtocolBufferCount A pointer to the number of GUID pointer= s present + in ProtocolBuffer. + + @retval EFI_SUCCESS The list of protocol interface GUIDs in= stalled + on Handle was returned in ProtocolBuffe= r. The + number of protocol interface GUIDs was = returned + in ProtocolBufferCount. + @retval EFI_INVALID_PARAMETER Handle is NULL. + @retval EFI_INVALID_PARAMETER Handle is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER ProtocolBuffer is NULL. + @retval EFI_INVALID_PARAMETER ProtocolBufferCount is NULL. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to stor= e the + results. + +**/ +EFI_STATUS +EFIAPI +UnitTestProtocolsPerHandle ( + IN EFI_HANDLE UserHandle, + OUT EFI_GUID ***ProtocolBuffer, + OUT UINTN *ProtocolBufferCount + ); + +/** + Function returns an array of handles that support the requested protoc= ol + in a buffer allocated from pool. This is a version of UnitTestLocateHa= ndle() + that allocates a buffer for the caller. + + @param SearchType Specifies which handle(s) are to be ret= urned. + @param Protocol Provides the protocol to search by. = This + parameter is only valid for SearchType + ByProtocol. + @param SearchKey Supplies the search key depending on th= e + SearchType. + @param NumberHandles The number of handles returned in Buffe= r. + @param Buffer A pointer to the buffer to return the r= equested + array of handles that support Protocol= . + + @retval EFI_SUCCESS The result array of handles was returne= d. + @retval EFI_NOT_FOUND No handles match the search. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to stor= e the + matching results. + @retval EFI_INVALID_PARAMETER One or more parameters are not valid. + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateHandleBuffer ( + IN EFI_LOCATE_SEARCH_TYPE SearchType, + IN EFI_GUID *Protocol OPTIONAL, + IN VOID *SearchKey OPTIONAL, + IN OUT UINTN *NumberHandles, + OUT EFI_HANDLE **Buffer + ); + +/** + Return the first Protocol Interface that matches the Protocol GUID. If + Registration is passed in, return a Protocol Instance that was just ad= d + to the system. If Registration is NULL return the first Protocol Inter= face + you find. + + @param Protocol The protocol to search for + @param Registration Optional Registration Key returned from + RegisterProtocolNotify() + @param Interface Return the Protocol interface (instance= ). + + @retval EFI_SUCCESS If a valid Interface is returned + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_NOT_FOUND Protocol interface not found + +**/ +EFI_STATUS +EFIAPI +UnitTestLocateProtocol ( + IN EFI_GUID *Protocol, + IN VOID *Registration OPTIONAL, + OUT VOID **Interface + ); + +/** + Installs a list of protocol interface into the boot services environme= nt. + This function calls InstallProtocolInterface() in a loop. If any error + occurs all the protocols added by this function are removed. This is + basically a lib function to save space. + + @param Handle The handle to install the protocol hand= lers on, + or NULL if a new handle is to be alloca= ted + @param ... EFI_GUID followed by protocol instance.= A NULL + terminates the list. The pairs are the + arguments to InstallProtocolInterface()= . All the + protocols are added to Handle. + + @retval EFI_SUCCESS All the protocol interface was installe= d. + @retval EFI_OUT_OF_RESOURCES There was not enough memory in pool to = install all the protocols. + @retval EFI_ALREADY_STARTED A Device Path Protocol instance was pas= sed in that is already present in + the handle database. + @retval EFI_INVALID_PARAMETER Handle is NULL. + @retval EFI_INVALID_PARAMETER Protocol is already installed on the ha= ndle specified by Handle. + +**/ +EFI_STATUS +EFIAPI +UnitTestInstallMultipleProtocolInterfaces ( + IN OUT EFI_HANDLE *Handle, + ... + ); + +/** + Uninstalls a list of protocol interface in the boot services environme= nt. + This function calls UninstallProtocolInterface() in a loop. This is + basically a lib function to save space. + + @param Handle The handle to uninstall the protocol + @param ... EFI_GUID followed by protocol instance.= A NULL + terminates the list. The pairs are the + arguments to UninstallProtocolInterface= (). All + the protocols are added to Handle. + + @return Status code + +**/ +EFI_STATUS +EFIAPI +UnitTestUninstallMultipleProtocolInterfaces ( + IN EFI_HANDLE Handle, + ... + ); + +/** + Computes and returns a 32-bit CRC for a data buffer. + + @param[in] Data A pointer to the buffer on which the 32-= bit CRC is to be computed. + @param[in] DataSize The number of bytes in the buffer Data. + @param[out] Crc32 The 32-bit CRC that was computed for the= data buffer specified by Data + and DataSize. + + @retval EFI_SUCCESS The 32-bit CRC was computed for the data= buffer and returned in + Crc32. + @retval EFI_INVALID_PARAMETER Data is NULL. + @retval EFI_INVALID_PARAMETER Crc32 is NULL. + @retval EFI_INVALID_PARAMETER DataSize is 0. + +**/ +EFI_STATUS +EFIAPI +UnitTestCalculateCrc32 ( + IN VOID *Data, + IN UINTN DataSize, + OUT UINT32 *Crc32 + ); + +/** + Creates an event in a group. + + @param Type The type of event to create and its mod= e and + attributes + @param NotifyTpl The task priority level of event notifi= cations + @param NotifyFunction Pointer to the events notification func= tion + @param NotifyContext Pointer to the notification functions c= ontext; + corresponds to parameter "Context" in t= he + notification function + @param EventGroup GUID for EventGroup if NULL act the sam= e as + gBS->CreateEvent(). + @param Event Pointer to the newly created event if t= he call + succeeds; undefined otherwise + + @retval EFI_SUCCESS The event structure was created + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid va= lue + @retval EFI_OUT_OF_RESOURCES The event could not be allocated + +**/ +EFI_STATUS +EFIAPI +UnitTestCreateEventEx ( + IN UINT32 Type, + IN EFI_TPL NotifyTpl, + IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL + IN CONST VOID *NotifyContext, OPTIONAL + IN CONST EFI_GUID *EventGroup, OPTIONAL + OUT EFI_EVENT *Event + ); + +#endif diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLib.inf b/UnitTestFrameworkPkg/Library/Uni= tTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLib.inf new file mode 100644 index 000000000000..b694dd0232ee --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLib.inf @@ -0,0 +1,46 @@ +## @file +# UEFI Boot Services Table Library for unit tests implementation. +# +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D UnitTestUefiBootServicesTableLib + MODULE_UNI_FILE =3D UefiBootServicesTableLibTest.uni + FILE_GUID =3D AA3A0651-89EB-4951-9D68-50F27360EBC= 2 + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D UefiBootServicesTableLib|DXE_CORE D= XE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER = SMM_CORE + + CONSTRUCTOR =3D UnitTestUefiBootServicesTableLibCon= structor + +# +# VALID_ARCHITECTURES =3D IA32 X64 EBC +# + +[Sources] + UnitTestUefiBootServicesTableLib.h + UnitTestUefiBootServicesTableLib.c + UnitTestUefiBootServicesTableLibEventTimer.c + UnitTestUefiBootServicesTableLibImage.c + UnitTestUefiBootServicesTableLibMemory.c + UnitTestUefiBootServicesTableLibProtocol.h + UnitTestUefiBootServicesTableLibProtocol.c + UnitTestUefiBootServicesTableLibMisc.c + UnitTestUefiBootServicesTableLibTpl.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + UnitTestLib + +[UserExtensions.TianoCore."ExtraFiles"] + TimerExtra.uni diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLib.uni b/UnitTestFrameworkPkg/Library/Uni= tTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLib.uni new file mode 100644 index 000000000000..1f742bb6d8e9 --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLib.uni @@ -0,0 +1,12 @@ +// /** @file +// UEFI Boot Services Table Library for unit tests implementation. +// +// Copyright (c) Microsoft Corporation +// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_MODULE_ABSTRACT #language en-US "UEFI Boot Servi= ces Table Library for unit tests" + +#string STR_MODULE_DESCRIPTION #language en-US "UEFI Boot Servi= ces Table Library for unit tests." diff --git a/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLi= b/UnitTestUefiBootServicesTableLibProtocol.h b/UnitTestFrameworkPkg/Libra= ry/UnitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLibProto= col.h new file mode 100644 index 000000000000..fe91e0e39639 --- /dev/null +++ b/UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitT= estUefiBootServicesTableLibProtocol.h @@ -0,0 +1,120 @@ +/** @file + An internal header file for the Unit Test instance of the UEFI Boot Se= rvices Table Library. + + This file includes common header files, defines internal structure and= functions used by + the library implementation. + +Copyright (c) Microsoft Corporation +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_PROTOCOL_H_ +#define UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_PROTOCOL_H_ + +#include "UnitTestUefiBootServicesTableLib.h" + +#define EFI_HANDLE_SIGNATURE SIGNATURE_32('h','n','d','l') + +/// +/// IHANDLE - contains a list of protocol handles +/// +typedef struct { + UINTN Signature; + /// All handles list of IHANDLE + LIST_ENTRY AllHandles; + /// List of PROTOCOL_INTERFACE's for this handle + LIST_ENTRY Protocols; + UINTN LocateRequest; + /// The Handle Database Key value when this handle was last created or= modified + UINT64 Key; +} IHANDLE; + +#define ASSERT_IS_HANDLE(a) ASSERT((a)->Signature =3D=3D EFI_HANDLE_SIG= NATURE) + +#define PROTOCOL_ENTRY_SIGNATURE SIGNATURE_32('p','r','t','e') + +/// +/// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol +/// database. Each handler that supports this protocol is listed, along +/// with a list of registered notifies. +/// +typedef struct { + UINTN Signature; + /// Link Entry inserted to mProtocolDatabase + LIST_ENTRY AllEntries; + /// ID of the protocol + EFI_GUID ProtocolID; + /// All protocol interfaces + LIST_ENTRY Protocols; + /// Registered notification handlers + LIST_ENTRY Notify; +} PROTOCOL_ENTRY; + +#define PROTOCOL_INTERFACE_SIGNATURE SIGNATURE_32('p','i','f','c') + +/// +/// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked +/// with a protocol interface structure +/// +typedef struct { + UINTN Signature; + /// Link on IHANDLE.Protocols + LIST_ENTRY Link; + /// Back pointer + IHANDLE *Handle; + /// Link on PROTOCOL_ENTRY.Protocols + LIST_ENTRY ByProtocol; + /// The protocol ID + PROTOCOL_ENTRY *Protocol; + /// The interface value + VOID *Interface; + /// OPEN_PROTOCOL_DATA list + LIST_ENTRY OpenList; + UINTN OpenListCount; +} PROTOCOL_INTERFACE; + +#define OPEN_PROTOCOL_DATA_SIGNATURE SIGNATURE_32('p','o','d','l') + +typedef struct { + UINTN Signature; + /// Link on PROTOCOL_INTERFACE.OpenList + LIST_ENTRY Link; + + EFI_HANDLE AgentHandle; + EFI_HANDLE ControllerHandle; + UINT32 Attributes; + UINT32 OpenCount; +} OPEN_PROTOCOL_DATA; + +#define PROTOCOL_NOTIFY_SIGNATURE SIGNATURE_32('p','r','t','n') + +/// +/// PROTOCOL_NOTIFY - used for each register notification for a protocol +/// +typedef struct { + UINTN Signature; + PROTOCOL_ENTRY *Protocol; + /// All notifications for this protocol + LIST_ENTRY Link; + /// Event to notify + EFI_EVENT Event; + /// Last position notified + LIST_ENTRY *Position; +} PROTOCOL_NOTIFY; + +typedef struct { + EFI_GUID *Protocol; + VOID *SearchKey; + LIST_ENTRY *Position; + PROTOCOL_ENTRY *ProtEntry; +} LOCATE_POSITION; + +typedef +IHANDLE * +(*UNIT_TEST_GET_NEXT) ( + IN OUT LOCATE_POSITION *Position, + OUT VOID **Interface + ); + +#endif diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc b/UnitTestFram= eworkPkg/UnitTestFrameworkPkg.dsc index e4f9fb6eb604..a76e31d97d5d 100644 --- a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc +++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc @@ -34,6 +34,7 @@ [Components] UnitTestFrameworkPkg/Library/UnitTestBootLibUsbClass/UnitTestBootLibUs= bClass.inf UnitTestFrameworkPkg/Library/UnitTestPersistenceLibSimpleFileSystem/Un= itTestPersistenceLibSimpleFileSystem.inf UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAsser= tLib.inf + UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTest= UefiBootServicesTableLib.inf =20 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTes= tDxe.inf UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTes= tPei.inf diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc b/Unit= TestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc index f249813713a8..9beeaef1ba5e 100644 --- a/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc +++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc @@ -17,6 +17,7 @@ [LibraryClasses.common.HOST_APPLICATION] UnitTestLib|UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLibCmocka= .inf DebugLib|UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosi= x.inf MemoryAllocationLib|UnitTestFrameworkPkg/Library/Posix/MemoryAllocatio= nLibPosix/MemoryAllocationLibPosix.inf + UefiBootServicesTableLib|UnitTestFrameworkPkg/Library/UnitTestUefiBoot= ServicesTableLib/UnitTestUefiBootServicesTableLib.inf =20 [BuildOptions] GCC:*_*_*_CC_FLAGS =3D -fno-pie --=20 2.28.0.windows.1