From 33473bdbb152b63a4ea9cf747d6f5256b57520f9 Mon Sep 17 00:00:00 2001 From: v-boscochen Date: Wed, 18 Dec 2024 13:24:43 +0800 Subject: [PATCH] Createa Mock for PciRootBridgeIoProtocol, MockPciPlatformProtocol & add mock function under MockUefiBootServiceTableLib (#1213) --- .../Library/MockUefiBootServicesTableLib.h | 13 + .../Protocol/MockPciPlatformProtocol.h | 92 ++++++ .../Protocol/MockPciRootBridgeIoProtocol.h | 262 ++++++++++++++++++ .../MockUefiBootServicesTableLib.cpp | 8 +- 4 files changed, 373 insertions(+), 2 deletions(-) create mode 100644 MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciPlatformProtocol.h create mode 100644 MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciRootBridgeIoProtocol.h diff --git a/MdePkg/Test/Mock/Include/GoogleTest/Library/MockUefiBootServicesTableLib.h b/MdePkg/Test/Mock/Include/GoogleTest/Library/MockUefiBootServicesTableLib.h index a9fba4897f..d58bf08259 100644 --- a/MdePkg/Test/Mock/Include/GoogleTest/Library/MockUefiBootServicesTableLib.h +++ b/MdePkg/Test/Mock/Include/GoogleTest/Library/MockUefiBootServicesTableLib.h @@ -204,6 +204,19 @@ struct MockUefiBootServicesTableLib { IN EFI_HANDLE DriverImageHandle OPTIONAL, IN EFI_HANDLE ChildHandle OPTIONAL) ); + + MOCK_FUNCTION_DECLARATION ( + EFI_TPL, + gBS_RaiseTpl, + + (IN EFI_TPL NewTpl) + ); + + MOCK_FUNCTION_DECLARATION ( + VOID, + gBS_RestoreTpl, + (IN EFI_TPL OldTpl) + ); }; #endif // MOCK_UEFI_BOOT_SERVICES_TABLE_LIB_H_ diff --git a/MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciPlatformProtocol.h b/MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciPlatformProtocol.h new file mode 100644 index 0000000000..aa450cf36b --- /dev/null +++ b/MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciPlatformProtocol.h @@ -0,0 +1,92 @@ +/** @file + This file declares a mock of PCI Platform Protocol. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef MOCK_PCIPLATFORMPROTOCOL_H +#define MOCK_PCIPLATFORMPROTOCOL_H + +#include +#include + +extern "C" { + #include + #include +} + +struct MockPciPlatformPhaseNotify { + MOCK_INTERFACE_DECLARATION (MockPciPlatformPhaseNotify); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPlatformNotify, + (IN EFI_PCI_PLATFORM_PROTOCOL *This, + IN EFI_HANDLE HostBridge, + IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase, + IN EFI_PCI_EXECUTION_PHASE ExecPhase + ) + ); +}; + +struct MockPciPlatformPreprocessController { + MOCK_INTERFACE_DECLARATION (MockPciPlatformPreprocessController); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPlatformPrepController, + ( + IN EFI_PCI_PLATFORM_PROTOCOL *This, + IN EFI_HANDLE HostBridge, + IN EFI_HANDLE RootBridge, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress, + IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase, + IN EFI_PCI_EXECUTION_PHASE ExecPhase + ) + ); +}; + +struct MockPciPlatformGetPlatformPolicy { + MOCK_INTERFACE_DECLARATION (MockPciPlatformGetPlatformPolicy); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockGetPlatformPolicy, + ( + IN CONST EFI_PCI_PLATFORM_PROTOCOL *This, + OUT EFI_PCI_PLATFORM_POLICY *PciPolicy + ) + ); +}; + +struct MockPciPlatformGetPciRom { + MOCK_INTERFACE_DECLARATION (MockPciPlatformGetPciRom); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockGetPciRom, + ( + IN CONST EFI_PCI_PLATFORM_PROTOCOL *This, + IN EFI_HANDLE PciHandle, + OUT VOID **RomImage, + OUT UINTN *RomSize + ) + ); +}; + +MOCK_INTERFACE_DEFINITION (MockPciPlatformPhaseNotify); +MOCK_FUNCTION_DEFINITION (MockPciPlatformPhaseNotify, MockPlatformNotify, 4, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciPlatformPreprocessController); +MOCK_FUNCTION_DEFINITION (MockPciPlatformPreprocessController, MockPlatformPrepController, 6, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciPlatformGetPlatformPolicy); +MOCK_FUNCTION_DEFINITION (MockPciPlatformGetPlatformPolicy, MockGetPlatformPolicy, 2, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciPlatformGetPciRom); +MOCK_FUNCTION_DEFINITION (MockPciPlatformGetPciRom, MockGetPciRom, 4, EFIAPI); + +#define MOCK_EFI_PCI_PLATFORM_PROTOCOL_INSTANCE(NAME) \ +EFI_PCI_PLATFORM_PROTOCOL NAME##_INSTANCE = { \ + MockPlatformNotify, \ + MockPlatformPrepController, \ + MockGetPlatformPolicy, \ + MockGetPciRom, \ +}; \ +EFI_PCI_PLATFORM_PROTOCOL *NAME = &NAME##_INSTANCE; + +#endif // MOCK_PCIPLATFORMPROTOCOL_H diff --git a/MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciRootBridgeIoProtocol.h b/MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciRootBridgeIoProtocol.h new file mode 100644 index 0000000000..c4d1a188ec --- /dev/null +++ b/MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciRootBridgeIoProtocol.h @@ -0,0 +1,262 @@ +/** @file + This file declares a mock of PCI Root Bridge IO Protocol. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef MOCK_PCIROOTBRIDGEIOPROTOCOL_H +#define MOCK_PCIROOTBRIDGEIOPROTOCOL_H + +#include +#include + +extern "C" +{ + #include + #include +} + +// +// Declarations to handle usage of the Pci Root Bridge Io Protocol by creating mock +// +struct MockPciRootBridgeIoPollIoMem { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoPollIoMem); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgePollMem, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINT64 Mask, + IN UINT64 Value, + IN UINT64 Delay, + OUT UINT64 *Result) + ); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgePollIo, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINT64 Mask, + IN UINT64 Value, + IN UINT64 Delay, + OUT UINT64 *Result) + ); +}; + +struct MockPciRootBridgeIoConfigAccess { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoConfigAccess); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeMemRead, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINTN Count, + IN OUT VOID *Buffer) + ); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeMemWrite, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINTN Count, + IN OUT VOID *Buffer) + ); + + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeIoRead, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINTN Count, + IN OUT VOID *Buffer) + ); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeIoWrite, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINTN Count, + IN OUT VOID *Buffer) + ); + + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgePciRead, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINTN Count, + IN OUT VOID *Buffer) + ); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgePciWrite, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 Address, + IN UINTN Count, + IN OUT VOID *Buffer) + ); +}; + +struct MockPciRootBridgeIoCopyMem { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoCopyMem); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeCopyMem, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width, + IN UINT64 DestAddress, + IN UINT64 SrcAddress, + IN UINTN Count) + ); +}; + +struct MockPciRootBridgeIoMap { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoMap); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeMap, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL * This, + IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION Operation, + IN VOID *HostAddress, + IN OUT UINTN *NumberOfBytes, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, + OUT VOID **Mapping) + ); +}; + +struct MockPciRootBridgeIoUnMap { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoUnMap); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeUnMap, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This, + IN VOID *Mapping) + ); +}; + +struct MockPciRootBridgeIoAllocateBuffer { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoAllocateBuffer); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeAllocateBuffer, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This, + IN EFI_ALLOCATE_TYPE Type, + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN OUT VOID **HostAddress, + IN UINT64 Attributes) + ); +}; + +struct MockPciRootBridgeIoFreeBuffer { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoFreeBuffer); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeFreeBuffer, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This, + IN UINTN Pages, + IN VOID *HostAddress) + ); +}; + +struct MockPciRootBridgeIoFlush { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoFlush); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeFlush, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This) + ); +}; + +struct MockPciRootBridgeIoGetAttributes { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoGetAttributes); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeGetAttributes, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This, + OUT UINT64 *Supports, + OUT UINT64 *Attributes) + ); +}; + +struct MockPciRootBridgeIoSetAttributes { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoSetAttributes); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeSetAttributes, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This, + IN UINT64 Attributes, + IN OUT UINT64 *ResourceBase, + IN OUT UINT64 *ResourceLength) + ); +}; + +struct MockPciRootBridgeIoConfiguration { + MOCK_INTERFACE_DECLARATION (MockPciRootBridgeIoConfiguration); + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + MockPciRootBridgeConfiguration, + (IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This, + OUT VOID **Resources) + ); +}; + +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoPollIoMem); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoPollIoMem, MockPciRootBridgePollMem, 7, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoPollIoMem, MockPciRootBridgePollIo, 7, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoConfigAccess); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfigAccess, MockPciRootBridgeMemRead, 5, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfigAccess, MockPciRootBridgeMemWrite, 5, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfigAccess, MockPciRootBridgeIoRead, 5, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfigAccess, MockPciRootBridgeIoWrite, 5, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfigAccess, MockPciRootBridgePciRead, 5, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfigAccess, MockPciRootBridgePciWrite, 5, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoCopyMem); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoCopyMem, MockPciRootBridgeCopyMem, 5, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoMap); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoMap, MockPciRootBridgeMap, 6, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoUnMap); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoUnMap, MockPciRootBridgeUnMap, 2, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoAllocateBuffer); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoAllocateBuffer, MockPciRootBridgeAllocateBuffer, 6, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoFreeBuffer); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoFreeBuffer, MockPciRootBridgeFreeBuffer, 3, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoFlush); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoFlush, MockPciRootBridgeFlush, 1, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoGetAttributes); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoGetAttributes, MockPciRootBridgeGetAttributes, 3, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoSetAttributes); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoSetAttributes, MockPciRootBridgeSetAttributes, 4, EFIAPI); +MOCK_INTERFACE_DEFINITION (MockPciRootBridgeIoConfiguration); +MOCK_FUNCTION_DEFINITION (MockPciRootBridgeIoConfiguration, MockPciRootBridgeConfiguration, 2, EFIAPI); + +#define MOCK_PCI_ROOT_BRIDGE_IO_PROTOCOL_INSTANCE(NAME) \ +EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL NAME##_INSTANCE = { \ + NULL, \ + MockPciRootBridgePollMem, \ + MockPciRootBridgePollIo, \ + { MockPciRootBridgeMemRead, MockPciRootBridgeMemWrite },\ + { MockPciRootBridgeIoRead, MockPciRootBridgeIoWrite },\ + { MockPciRootBridgePciRead, MockPciRootBridgePciWrite },\ + MockPciRootBridgeCopyMem, \ + MockPciRootBridgeMap, \ + MockPciRootBridgeUnMap, \ + MockPciRootBridgeAllocateBuffer, \ + MockPciRootBridgeFreeBuffer, \ + MockPciRootBridgeFlush, \ + MockPciRootBridgeGetAttributes, \ + MockPciRootBridgeSetAttributes, \ + MockPciRootBridgeConfiguration, \ +}; \ +EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *NAME = &NAME##_INSTANCE; + +#endif // MOCK_PCIIOPROTOCOL_H diff --git a/MdePkg/Test/Mock/Library/GoogleTest/MockUefiBootServicesTableLib/MockUefiBootServicesTableLib.cpp b/MdePkg/Test/Mock/Library/GoogleTest/MockUefiBootServicesTableLib/MockUefiBootServicesTableLib.cpp index 0743e9caec..8bbdc21b7b 100644 --- a/MdePkg/Test/Mock/Library/GoogleTest/MockUefiBootServicesTableLib/MockUefiBootServicesTableLib.cpp +++ b/MdePkg/Test/Mock/Library/GoogleTest/MockUefiBootServicesTableLib/MockUefiBootServicesTableLib.cpp @@ -28,6 +28,9 @@ MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_AllocatePool, 3, EFI MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_LocateHandle, 5, EFIAPI); MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_ConnectController, 4, EFIAPI); MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_DisconnectController, 3, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_RaiseTpl, 1, EFIAPI); + +MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_RestoreTpl, 1, EFIAPI); extern "C" { EFI_STATUS @@ -103,8 +106,9 @@ extern "C" { static EFI_BOOT_SERVICES LocalBs = { { 0, 0, 0, 0, 0 }, // EFI_TABLE_HEADER - NULL, // EFI_RAISE_TPL - NULL, // EFI_RESTORE_TPL + gBS_RaiseTpl, // EFI_RAISE_TPL + + gBS_RestoreTpl, // EFI_RESTORE_TPL NULL, // EFI_ALLOCATE_PAGES NULL, // EFI_FREE_PAGES gBS_GetMemoryMap, // EFI_GET_MEMORY_MAP