]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Ovmf/HardwareInfoLib: Add Dxe lib to dynamically parse heterogenous data
authorNicolas Ojeda Leon <ncoleon@amazon.com>
Wed, 19 Jan 2022 14:12:30 +0000 (15:12 +0100)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Wed, 22 Jun 2022 15:34:16 +0000 (15:34 +0000)
Following the Hardware Info library, create the DxeHardwareInfoLib
which implements the whole API capable of parsing heterogeneous hardware
information. The list-like API grants callers a flexible and common
pattern to retrieve the data. Moreover, the initial source is a BLOB
which generalizes the host-to-guest transmission mechanism.

The Hardware Info library main objective is to provide a way to
describe non-discoverable hardware so that the host can share the
available resources with the guest in Ovmf platforms. This change
features and embraces the main idea behind the library by providing
an API that parses a BLOB into a linked list to retrieve hardware
data from any source. Additionally, list-like APIs are provided so
that the hardware info list can be traversed conveniently.
Similarly, the capability is provided to filter results by specific
hardware types. However, heterogeneous elements can be added to the
list, increasing the flexibility. This way, a single source, for
example a fw-cfg file, can be used to describe several instances of
multiple types of hardware.

This part of the Hardware Info library makes use of dynamic memory
and is intended for stages in which memory services are available.
A motivation example is the PciHostBridgeLib. This library, part
of the PCI driver populates the list of PCI root bridges during DXE
stage for future steps to discover the resources under them. The
hardware info library can be used to obtain the detailed description
of available host bridges, for instance in the form of a fw-cfg file,
and parse that information into a dynmaic list that allows, first to
verify consistency of the data, and second discover the resources
availabe for each root bridge.

Cc: Alexander Graf <graf@amazon.de>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Nicolas Ojeda Leon <ncoleon@amazon.com>
ArmVirtPkg/ArmVirtQemu.dsc
OvmfPkg/AmdSev/AmdSevX64.dsc
OvmfPkg/Bhyve/BhyveX64.dsc
OvmfPkg/CloudHv/CloudHvX64.dsc
OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf [new file with mode: 0644]
OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c [new file with mode: 0644]
OvmfPkg/Microvm/MicrovmX64.dsc
OvmfPkg/OvmfPkgIa32.dsc
OvmfPkg/OvmfPkgIa32X64.dsc
OvmfPkg/OvmfPkgX64.dsc
OvmfPkg/OvmfXen.dsc

index 9369a88858fd1d68345438c9038390d497497200..56eb53911d620ca9144707c8ae6bd3481667d979 100644 (file)
@@ -82,6 +82,7 @@
   PciHostBridgeLib|OvmfPkg/Fdt/FdtPciHostBridgeLib/FdtPciHostBridgeLib.inf\r
   PciHostBridgeUtilityLib|OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 \r
 !if $(TPM2_ENABLE) == TRUE\r
   Tpm2CommandLib|SecurityPkg/Library/Tpm2CommandLib/Tpm2CommandLib.inf\r
index 7497cace9ce128c5ed7a72fb40acf8c837924826..726521c9438179c35afc6ffcc83e1f866475011e 100644 (file)
   BlobVerifierLib|OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibSevHashes.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 \r
 !if $(SOURCE_DEBUG_ENABLE) == TRUE\r
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf\r
index ada904464e592125c9b838bcb3736cbd1fdfe9a9..ec8ad98db76522b0419848dd4693e0ac311cc42a 100644 (file)
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 \r
   CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf\r
   FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf\r
index 7fbc1021e29b1d0496f0c63a144a346fe3367fd8..ca601aa09d3acb98d706a9ec0301b10dc7e85b7b 100644 (file)
   LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf\r
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 !if $(SMM_REQUIRE) == FALSE\r
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
 !endif\r
diff --git a/OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf b/OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
new file mode 100644 (file)
index 0000000..a2f056e
--- /dev/null
@@ -0,0 +1,39 @@
+## @file\r
+#  Hardware information library to describe non-discoverable hardware resources\r
+#\r
+#  Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = DxeHardwareInfoLib\r
+  FILE_GUID                      = F60B206A-5C56-11EC-AEAC-67CB080BCFF2\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = DxeHardwareInfoLib\r
+\r
+#\r
+# The following information is for reference only and not required by the build\r
+# tools.\r
+#\r
+#  VALID_ARCHITECTURES           = X64\r
+#\r
+\r
+[Sources]\r
+  HardwareInfoDxe.c\r
+  HardwareInfoPciHostBridgeLib.c\r
+  QemuFwCfgHardwareInfoLib.c\r
+\r
+[Packages]\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  MdePkg/MdePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseMemoryLib\r
+  DebugLib\r
+  MemoryAllocationLib\r
diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c
new file mode 100644 (file)
index 0000000..a74de52
--- /dev/null
@@ -0,0 +1,254 @@
+/*/@file\r
+  Hardware info parsing functions.\r
+  Binary data is expected as a consecutive series of header - object pairs.\r
+  Complete library providing list-like interface to dynamically manipulate\r
+  hardware info objects and parsing from a generic blob.\r
+\r
+  Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <Uefi/UefiBaseType.h>\r
+#include <Uefi/UefiSpec.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiLib.h>\r
+\r
+#include <Library/HardwareInfoLib.h>\r
+\r
+EFI_STATUS\r
+CreateHardwareInfoList (\r
+  IN  UINT8               *Blob,\r
+  IN  UINTN               BlobSize,\r
+  IN  HARDWARE_INFO_TYPE  TypeFilter,\r
+  OUT LIST_ENTRY          *ListHead\r
+  )\r
+{\r
+  UINT8          *Index;\r
+  UINT8          *BlobEnd;\r
+  HARDWARE_INFO  *HwComponent;\r
+\r
+  if ((Blob == NULL) || (BlobSize <= 0) ||\r
+      (ListHead == NULL))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Index   = Blob;\r
+  BlobEnd = Blob + BlobSize;\r
+  while (Index < BlobEnd) {\r
+    HwComponent = AllocateZeroPool (sizeof (HARDWARE_INFO));\r
+\r
+    if (HwComponent == NULL) {\r
+      goto FailedAllocate;\r
+    }\r
+\r
+    HwComponent->Header.Type.Uint64 = *((UINT64 *)Index);\r
+    Index                          += sizeof (HwComponent->Header.Type);\r
+    HwComponent->Header.Size        = *((UINT64 *)(Index));\r
+    Index                          += sizeof (HwComponent->Header.Size);\r
+\r
+    if ((HwComponent->Header.Size > MAX_UINTN) || (Index < Blob) || ((Index + HwComponent->Header.Size) > BlobEnd)) {\r
+      goto FreeResources;\r
+    }\r
+\r
+    //\r
+    // Check if optional TypeFilter is set, skip if the current\r
+    // object is of a different type and release the partially\r
+    // allocated object\r
+    //\r
+    if ((TypeFilter != HardwareInfoTypeUndefined) &&\r
+        (HwComponent->Header.Type.Value != TypeFilter))\r
+    {\r
+      FreePool (HwComponent);\r
+      Index += HwComponent->Header.Size;\r
+      continue;\r
+    }\r
+\r
+    HwComponent->Data.Raw = AllocateZeroPool ((UINTN)HwComponent->Header.Size);\r
+    if (HwComponent->Data.Raw == NULL) {\r
+      goto FreeResources;\r
+    }\r
+\r
+    CopyMem (HwComponent->Data.Raw, Index, (UINTN)HwComponent->Header.Size);\r
+    Index += HwComponent->Header.Size;\r
+\r
+    InsertTailList (ListHead, &HwComponent->Link);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+\r
+FreeResources:\r
+  //\r
+  // Clean the resources allocated in the incomplete cycle\r
+  //\r
+  FreePool (HwComponent);\r
+\r
+FailedAllocate:\r
+  DEBUG ((\r
+    EFI_D_ERROR,\r
+    "%a: Failed to allocate memory for hardware info\n",\r
+    __FUNCTION__\r
+    ));\r
+\r
+  return EFI_OUT_OF_RESOURCES;\r
+}\r
+\r
+VOID\r
+FreeHardwareInfoList (\r
+  IN OUT  LIST_ENTRY  *ListHead\r
+  )\r
+{\r
+  LIST_ENTRY     *CurrentLink;\r
+  HARDWARE_INFO  *HwComponent;\r
+\r
+  if (IsListEmpty (ListHead)) {\r
+    return;\r
+  }\r
+\r
+  CurrentLink = ListHead->ForwardLink;\r
+  while (CurrentLink != NULL && CurrentLink != ListHead) {\r
+    HwComponent = HARDWARE_INFO_FROM_LINK (CurrentLink);\r
+\r
+    //\r
+    // Remove item from list before invalidating the pointers\r
+    //\r
+    CurrentLink = RemoveEntryList (CurrentLink);\r
+\r
+    FreePool (HwComponent->Data.Raw);\r
+    FreePool (HwComponent);\r
+  }\r
+}\r
+\r
+/**\r
+  Validates if the specified Node has a valid data size and is of\r
+  specified type.\r
+  The data size can be less or equal to the provided type size to be\r
+  regarded as valid and thus accessible with the typed pointer.\r
+\r
+  For future compatibility the size is allowed to be smaller so that\r
+  different versions interpret fields differently and, particularly,\r
+  have smaller data structures. However, it cannot be larger than the\r
+  type size to avoid accessing memory out of bounds.\r
+\r
+  @param[in]  Node      Hardware Info node to be validated\r
+  @param[in]  TypeSize  Size (in bytes) of the data type intended to be\r
+                        used to dereference the data.\r
+  @retval TRUE  Node is valid and can be accessed\r
+  @retval FALSE Node is not valid\r
+/*/\r
+STATIC\r
+BOOLEAN\r
+IsHardwareInfoNodeValidByType (\r
+  IN  LIST_ENTRY          *ListHead,\r
+  IN  LIST_ENTRY          *Link,\r
+  IN  HARDWARE_INFO_TYPE  Type,\r
+  IN  UINTN               TypeSize\r
+  )\r
+{\r
+  HARDWARE_INFO  *HwComponent;\r
+\r
+  if (IsNull (ListHead, Link)) {\r
+    return FALSE;\r
+  }\r
+\r
+  HwComponent = HARDWARE_INFO_FROM_LINK (Link);\r
+\r
+  //\r
+  // Verify if the node type is the specified one and the size of\r
+  // the data allocated to the node is greater than the size of\r
+  // the type intended to dereference it in order to avoid access\r
+  // to memory out of bondaries.\r
+  //\r
+  if ((HwComponent->Header.Type.Value == Type) &&\r
+      (HwComponent->Header.Size >= TypeSize))\r
+  {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+UINTN\r
+GetHardwareInfoCountByType (\r
+  IN  LIST_ENTRY          *ListHead,\r
+  IN  HARDWARE_INFO_TYPE  Type,\r
+  IN  UINTN               TypeSize\r
+  )\r
+{\r
+  UINTN       Count;\r
+  LIST_ENTRY  *Link;\r
+\r
+  Count = 0;\r
+  for (Link = GetFirstHardwareInfoByType (ListHead, Type, TypeSize);\r
+       !IsNull (ListHead, Link);\r
+       Link = GetNextHardwareInfoByType (ListHead, Link, Type, TypeSize))\r
+  {\r
+    if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) {\r
+      Count++;\r
+    }\r
+  }\r
+\r
+  return Count;\r
+}\r
+\r
+LIST_ENTRY *\r
+GetFirstHardwareInfoByType (\r
+  IN  LIST_ENTRY          *ListHead,\r
+  IN  HARDWARE_INFO_TYPE  Type,\r
+  IN  UINTN               TypeSize\r
+  )\r
+{\r
+  LIST_ENTRY  *Link;\r
+\r
+  if (IsListEmpty (ListHead)) {\r
+    return ListHead;\r
+  }\r
+\r
+  Link = GetFirstNode (ListHead);\r
+\r
+  if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) {\r
+    return Link;\r
+  }\r
+\r
+  return GetNextHardwareInfoByType (ListHead, Link, Type, TypeSize);\r
+}\r
+\r
+LIST_ENTRY *\r
+GetNextHardwareInfoByType (\r
+  IN  LIST_ENTRY          *ListHead,\r
+  IN  LIST_ENTRY          *Node,\r
+  IN  HARDWARE_INFO_TYPE  Type,\r
+  IN  UINTN               TypeSize\r
+  )\r
+{\r
+  LIST_ENTRY  *Link;\r
+\r
+  Link = GetNextNode (ListHead, Node);\r
+\r
+  while (!IsNull (ListHead, Link)) {\r
+    if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) {\r
+      //\r
+      // Found a node of specified type and with valid size. Break and\r
+      // return the found node.\r
+      //\r
+      break;\r
+    }\r
+\r
+    Link = GetNextNode (ListHead, Link);\r
+  }\r
+\r
+  return Link;\r
+}\r
+\r
+BOOLEAN\r
+EndOfHardwareInfoList (\r
+  IN  LIST_ENTRY  *ListHead,\r
+  IN  LIST_ENTRY  *Node\r
+  )\r
+{\r
+  return IsNull (ListHead, Node);\r
+}\r
index 38dd7084779d22f02576c8b9a8882923ac870188..61db9b6e4c83b21b0e4ab4763644986935a59c3b 100644 (file)
   FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 \r
 !if $(SOURCE_DEBUG_ENABLE) == TRUE\r
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf\r
index a3c8b358e55f2537e50ac45d701198dff5d8b1de..934edbbd2a7b4a5634d83d346375c348c96bc7d7 100644 (file)
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLibNull.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 !if $(SMM_REQUIRE) == FALSE\r
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
 !endif\r
index f2ce2cfae25d2a889ffdf204a6cab22847c9a8d4..4f432c294958479da0a0a986113180c0fe8740cb 100644 (file)
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLibNull.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 !if $(SMM_REQUIRE) == FALSE\r
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
 !endif\r
index fa6b93563c0c665808091d2ae5d2859f5cde23bf..b22da97d4f7719b46a49191de10927ca1f5a98a4 100644 (file)
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 \r
 !if $(SMM_REQUIRE) == FALSE\r
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
index 03922b5c16c6ee5cfe94b76286a14a7ce3d6f9db..58a7c97cddf702bfb506a8dee7b7c321e0dbd2e5 100644 (file)
   FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf\r
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf\r
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf\r
+  DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf\r
 \r
 !if $(SOURCE_DEBUG_ENABLE) == TRUE\r
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf\r