]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/AcpiPlatformDxe/Qemu.c
OvmfPkg: AcpiTimerLib: Split into multiple phase-specific instances
[mirror_edk2.git] / OvmfPkg / AcpiPlatformDxe / Qemu.c
index 5a96d76a1f44df8951ebfb8d9182e85c5aaddbe5..59152487b85b1d35c76e0afb5933246199a08bf5 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   OVMF ACPI QEMU support\r
 \r
-  Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>\r
 \r
   Copyright (C) 2012-2014, Red Hat, Inc.\r
 \r
 **/\r
 \r
 #include "AcpiPlatform.h"\r
+#include "QemuLoader.h"\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/QemuFwCfgLib.h>\r
 #include <Library/DxeServicesTableLib.h>\r
 #include <Library/PcdLib.h>\r
+#include <Library/OrderedCollectionLib.h>\r
 #include <IndustryStandard/Acpi.h>\r
 \r
 BOOLEAN\r
@@ -516,80 +518,333 @@ QemuInstallAcpiTable (
 }\r
 \r
 \r
+//\r
+// The user structure for the ordered collection that will track the fw_cfg\r
+// blobs under processing.\r
+//\r
+typedef struct {\r
+  UINT8   File[QEMU_LOADER_FNAME_SIZE]; // NUL-terminated name of the fw_cfg\r
+                                        // blob. This is the ordering / search\r
+                                        // key.\r
+  UINTN   Size;                         // The number of bytes in this blob.\r
+  UINT8   *Base;                        // Pointer to the blob data.\r
+  BOOLEAN HostsOnlyTableData;           // TRUE iff the blob has been found to\r
+                                        // only contain data that is directly\r
+                                        // part of ACPI tables.\r
+} BLOB;\r
+\r
+\r
 /**\r
-  Check if an array of bytes starts with an RSD PTR structure.\r
+  Compare a standalone key against a user structure containing an embedded key.\r
+\r
+  @param[in] StandaloneKey  Pointer to the bare key.\r
+\r
+  @param[in] UserStruct     Pointer to the user structure with the embedded\r
+                            key.\r
+\r
+  @retval <0  If StandaloneKey compares less than UserStruct's key.\r
+\r
+  @retval  0  If StandaloneKey compares equal to UserStruct's key.\r
+\r
+  @retval >0  If StandaloneKey compares greater than UserStruct's key.\r
+**/\r
+STATIC\r
+INTN\r
+EFIAPI\r
+BlobKeyCompare (\r
+  IN CONST VOID *StandaloneKey,\r
+  IN CONST VOID *UserStruct\r
+  )\r
+{\r
+  CONST BLOB *Blob;\r
+\r
+  Blob = UserStruct;\r
+  return AsciiStrCmp (StandaloneKey, (CONST CHAR8 *)Blob->File);\r
+}\r
+\r
+\r
+/**\r
+  Comparator function for two user structures.\r
+\r
+  @param[in] UserStruct1  Pointer to the first user structure.\r
+\r
+  @param[in] UserStruct2  Pointer to the second user structure.\r
+\r
+  @retval <0  If UserStruct1 compares less than UserStruct2.\r
+\r
+  @retval  0  If UserStruct1 compares equal to UserStruct2.\r
+\r
+  @retval >0  If UserStruct1 compares greater than UserStruct2.\r
+**/\r
+STATIC\r
+INTN\r
+EFIAPI\r
+BlobCompare (\r
+  IN CONST VOID *UserStruct1,\r
+  IN CONST VOID *UserStruct2\r
+  )\r
+{\r
+  CONST BLOB *Blob1;\r
 \r
-  Checksum is ignored.\r
+  Blob1 = UserStruct1;\r
+  return BlobKeyCompare (Blob1->File, UserStruct2);\r
+}\r
+\r
+\r
+/**\r
+  Process a QEMU_LOADER_ALLOCATE command.\r
 \r
-  @param[in] Buffer     The array to check.\r
+  @param[in] Allocate     The QEMU_LOADER_ALLOCATE command to process.\r
 \r
-  @param[in] Size       Number of bytes in Buffer.\r
+  @param[in,out] Tracker  The ORDERED_COLLECTION tracking the BLOB user\r
+                          structures created thus far.\r
 \r
-  @param[out] RsdpSize  If the function returns EFI_SUCCESS, this parameter\r
-                        contains the size of the detected RSD PTR structure.\r
+  @retval EFI_SUCCESS           An area of whole AcpiNVS pages has been\r
+                                allocated for the blob contents, and the\r
+                                contents have been saved. A BLOB object (user\r
+                                structure) has been allocated from pool memory,\r
+                                referencing the blob contents. The BLOB user\r
+                                structure has been linked into Tracker.\r
 \r
-  @retval  EFI_SUCCESS         RSD PTR structure detected at the beginning of\r
-                               Buffer, and its advertised size does not exceed\r
-                               Size.\r
+  @retval EFI_PROTOCOL_ERROR    Malformed fw_cfg file name has been found in\r
+                                Allocate, or the Allocate command references a\r
+                                file that is already known by Tracker.\r
 \r
-  @retval  EFI_PROTOCOL_ERROR  RSD PTR structure detected at the beginning of\r
-                               Buffer, but it has inconsistent size.\r
+  @retval EFI_UNSUPPORTED       Unsupported alignment request has been found in\r
+                                Allocate.\r
 \r
-  @retval  EFI_NOT_FOUND       RSD PTR structure not found.\r
+  @retval EFI_OUT_OF_RESOURCES  Pool allocation failed.\r
 \r
+  @return                       Error codes from QemuFwCfgFindFile() and\r
+                                gBS->AllocatePages().\r
 **/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ProcessCmdAllocate (\r
+  IN CONST QEMU_LOADER_ALLOCATE *Allocate,\r
+  IN OUT ORDERED_COLLECTION     *Tracker\r
+  )\r
+{\r
+  FIRMWARE_CONFIG_ITEM FwCfgItem;\r
+  UINTN                FwCfgSize;\r
+  EFI_STATUS           Status;\r
+  UINTN                NumPages;\r
+  EFI_PHYSICAL_ADDRESS Address;\r
+  BLOB                 *Blob;\r
+\r
+  if (Allocate->File[QEMU_LOADER_FNAME_SIZE - 1] != '\0') {\r
+    DEBUG ((EFI_D_ERROR, "%a: malformed file name\n", __FUNCTION__));\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+\r
+  if (Allocate->Alignment > EFI_PAGE_SIZE) {\r
+    DEBUG ((EFI_D_ERROR, "%a: unsupported alignment 0x%x\n", __FUNCTION__,\r
+      Allocate->Alignment));\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  Status = QemuFwCfgFindFile ((CHAR8 *)Allocate->File, &FwCfgItem, &FwCfgSize);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: QemuFwCfgFindFile(\"%a\"): %r\n", __FUNCTION__,\r
+      Allocate->File, Status));\r
+    return Status;\r
+  }\r
+\r
+  NumPages = EFI_SIZE_TO_PAGES (FwCfgSize);\r
+  Address = 0xFFFFFFFF;\r
+  Status = gBS->AllocatePages (AllocateMaxAddress, EfiACPIMemoryNVS, NumPages,\r
+                  &Address);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Blob = AllocatePool (sizeof *Blob);\r
+  if (Blob == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreePages;\r
+  }\r
+  CopyMem (Blob->File, Allocate->File, QEMU_LOADER_FNAME_SIZE);\r
+  Blob->Size = FwCfgSize;\r
+  Blob->Base = (VOID *)(UINTN)Address;\r
+  Blob->HostsOnlyTableData = TRUE;\r
+\r
+  Status = OrderedCollectionInsert (Tracker, NULL, Blob);\r
+  if (Status == RETURN_ALREADY_STARTED) {\r
+    DEBUG ((EFI_D_ERROR, "%a: duplicated file \"%a\"\n", __FUNCTION__,\r
+      Allocate->File));\r
+    Status = EFI_PROTOCOL_ERROR;\r
+  }\r
+  if (EFI_ERROR (Status)) {\r
+    goto FreeBlob;\r
+  }\r
+\r
+  QemuFwCfgSelectItem (FwCfgItem);\r
+  QemuFwCfgReadBytes (FwCfgSize, Blob->Base);\r
+  ZeroMem (Blob->Base + Blob->Size, EFI_PAGES_TO_SIZE (NumPages) - Blob->Size);\r
+\r
+  DEBUG ((EFI_D_VERBOSE, "%a: File=\"%a\" Alignment=0x%x Zone=%d Size=0x%Lx "\r
+    "Address=0x%Lx\n", __FUNCTION__, Allocate->File, Allocate->Alignment,\r
+    Allocate->Zone, (UINT64)Blob->Size, (UINT64)(UINTN)Blob->Base));\r
+  return EFI_SUCCESS;\r
+\r
+FreeBlob:\r
+  FreePool (Blob);\r
+\r
+FreePages:\r
+  gBS->FreePages (Address, NumPages);\r
+\r
+  return Status;\r
+}\r
+\r
 \r
+/**\r
+  Process a QEMU_LOADER_ADD_POINTER command.\r
+\r
+  @param[in] AddPointer  The QEMU_LOADER_ADD_POINTER command to process.\r
+\r
+  @param[in] Tracker     The ORDERED_COLLECTION tracking the BLOB user\r
+                         structures created thus far.\r
+\r
+  @retval EFI_PROTOCOL_ERROR  Malformed fw_cfg file name(s) have been found in\r
+                              AddPointer, or the AddPointer command references\r
+                              a file unknown to Tracker, or the pointer to\r
+                              relocate has invalid location, size, or value, or\r
+                              the relocated pointer value is not representable\r
+                              in the given pointer size.\r
+\r
+  @retval EFI_SUCCESS         The pointer field inside the pointer blob has\r
+                              been relocated.\r
+**/\r
 STATIC\r
 EFI_STATUS\r
-CheckRsdp (\r
-  IN  CONST VOID *Buffer,\r
-  IN  UINTN      Size,\r
-  OUT UINTN      *RsdpSize\r
+EFIAPI\r
+ProcessCmdAddPointer (\r
+  IN CONST QEMU_LOADER_ADD_POINTER *AddPointer,\r
+  IN CONST ORDERED_COLLECTION      *Tracker\r
   )\r
 {\r
-  CONST UINT64                                       *Signature;\r
-  CONST EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp1;\r
-  CONST EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp2;\r
+  ORDERED_COLLECTION_ENTRY *TrackerEntry, *TrackerEntry2;\r
+  BLOB                     *Blob, *Blob2;\r
+  UINT8                    *PointerField;\r
+  UINT64                   PointerValue;\r
+\r
+  if (AddPointer->PointerFile[QEMU_LOADER_FNAME_SIZE - 1] != '\0' ||\r
+      AddPointer->PointeeFile[QEMU_LOADER_FNAME_SIZE - 1] != '\0') {\r
+    DEBUG ((EFI_D_ERROR, "%a: malformed file name\n", __FUNCTION__));\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+\r
+  TrackerEntry = OrderedCollectionFind (Tracker, AddPointer->PointerFile);\r
+  TrackerEntry2 = OrderedCollectionFind (Tracker, AddPointer->PointeeFile);\r
+  if (TrackerEntry == NULL || TrackerEntry2 == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "%a: invalid blob reference(s) \"%a\" / \"%a\"\n",\r
+      __FUNCTION__, AddPointer->PointerFile, AddPointer->PointeeFile));\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
 \r
-  if (Size < sizeof *Signature) {\r
-    return EFI_NOT_FOUND;\r
+  Blob = OrderedCollectionUserStruct (TrackerEntry);\r
+  Blob2 = OrderedCollectionUserStruct (TrackerEntry2);\r
+  if ((AddPointer->PointerSize != 1 && AddPointer->PointerSize != 2 &&\r
+       AddPointer->PointerSize != 4 && AddPointer->PointerSize != 8) ||\r
+      Blob->Size < AddPointer->PointerSize ||\r
+      Blob->Size - AddPointer->PointerSize < AddPointer->PointerOffset) {\r
+    DEBUG ((EFI_D_ERROR, "%a: invalid pointer location or size in \"%a\"\n",\r
+      __FUNCTION__, AddPointer->PointerFile));\r
+    return EFI_PROTOCOL_ERROR;\r
   }\r
-  Signature = Buffer;\r
 \r
-  if (*Signature != EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) {\r
-    return EFI_NOT_FOUND;\r
+  PointerField = Blob->Base + AddPointer->PointerOffset;\r
+  PointerValue = 0;\r
+  CopyMem (&PointerValue, PointerField, AddPointer->PointerSize);\r
+  if (PointerValue >= Blob2->Size) {\r
+    DEBUG ((EFI_D_ERROR, "%a: invalid pointer value in \"%a\"\n", __FUNCTION__,\r
+      AddPointer->PointerFile));\r
+    return EFI_PROTOCOL_ERROR;\r
   }\r
 \r
   //\r
-  // Signature found -- from this point on we can only report\r
-  // EFI_PROTOCOL_ERROR or EFI_SUCCESS.\r
+  // The memory allocation system ensures that the address of the byte past the\r
+  // last byte of any allocated object is expressible (no wraparound).\r
   //\r
-  if (Size < sizeof *Rsdp1) {\r
+  ASSERT ((UINTN)Blob2->Base <= MAX_ADDRESS - Blob2->Size);\r
+\r
+  PointerValue += (UINT64)(UINTN)Blob2->Base;\r
+  if (RShiftU64 (\r
+        RShiftU64 (PointerValue, AddPointer->PointerSize * 8 - 1), 1) != 0) {\r
+    DEBUG ((EFI_D_ERROR, "%a: relocated pointer value unrepresentable in "\r
+      "\"%a\"\n", __FUNCTION__, AddPointer->PointerFile));\r
     return EFI_PROTOCOL_ERROR;\r
   }\r
-  Rsdp1 = Buffer;\r
 \r
-  if (Rsdp1->Reserved == 0) {\r
-    //\r
-    // ACPI 1.0 doesn't include the Length field\r
-    //\r
-    *RsdpSize = sizeof *Rsdp1;\r
-    return EFI_SUCCESS;\r
+  CopyMem (PointerField, &PointerValue, AddPointer->PointerSize);\r
+\r
+  DEBUG ((EFI_D_VERBOSE, "%a: PointerFile=\"%a\" PointeeFile=\"%a\" "\r
+    "PointerOffset=0x%x PointerSize=%d\n", __FUNCTION__,\r
+    AddPointer->PointerFile, AddPointer->PointeeFile,\r
+    AddPointer->PointerOffset, AddPointer->PointerSize));\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Process a QEMU_LOADER_ADD_CHECKSUM command.\r
+\r
+  @param[in] AddChecksum  The QEMU_LOADER_ADD_CHECKSUM command to process.\r
+\r
+  @param[in] Tracker      The ORDERED_COLLECTION tracking the BLOB user\r
+                          structures created thus far.\r
+\r
+  @retval EFI_PROTOCOL_ERROR  Malformed fw_cfg file name has been found in\r
+                              AddChecksum, or the AddChecksum command\r
+                              references a file unknown to Tracker, or the\r
+                              range to checksum is invalid.\r
+\r
+  @retval EFI_SUCCESS         The requested range has been checksummed.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ProcessCmdAddChecksum (\r
+  IN CONST QEMU_LOADER_ADD_CHECKSUM *AddChecksum,\r
+  IN CONST ORDERED_COLLECTION       *Tracker\r
+  )\r
+{\r
+  ORDERED_COLLECTION_ENTRY *TrackerEntry;\r
+  BLOB                     *Blob;\r
+\r
+  if (AddChecksum->File[QEMU_LOADER_FNAME_SIZE - 1] != '\0') {\r
+    DEBUG ((EFI_D_ERROR, "%a: malformed file name\n", __FUNCTION__));\r
+    return EFI_PROTOCOL_ERROR;\r
   }\r
 \r
-  if (Size < sizeof *Rsdp2) {\r
+  TrackerEntry = OrderedCollectionFind (Tracker, AddChecksum->File);\r
+  if (TrackerEntry == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "%a: invalid blob reference \"%a\"\n", __FUNCTION__,\r
+      AddChecksum->File));\r
     return EFI_PROTOCOL_ERROR;\r
   }\r
-  Rsdp2 = Buffer;\r
 \r
-  if (Size < Rsdp2->Length || Rsdp2->Length < sizeof *Rsdp2) {\r
+  Blob = OrderedCollectionUserStruct (TrackerEntry);\r
+  if (Blob->Size <= AddChecksum->ResultOffset ||\r
+      Blob->Size < AddChecksum->Length ||\r
+      Blob->Size - AddChecksum->Length < AddChecksum->Start) {\r
+    DEBUG ((EFI_D_ERROR, "%a: invalid checksum range in \"%a\"\n",\r
+      __FUNCTION__, AddChecksum->File));\r
     return EFI_PROTOCOL_ERROR;\r
   }\r
 \r
-  *RsdpSize = Rsdp2->Length;\r
+  Blob->Base[AddChecksum->ResultOffset] = CalculateCheckSum8 (\r
+                                           Blob->Base + AddChecksum->Start,\r
+                                           AddChecksum->Length\r
+                                           );\r
+  DEBUG ((EFI_D_VERBOSE, "%a: File=\"%a\" ResultOffset=0x%x Start=0x%x "\r
+    "Length=0x%x\n", __FUNCTION__, AddChecksum->File,\r
+    AddChecksum->ResultOffset, AddChecksum->Start, AddChecksum->Length));\r
   return EFI_SUCCESS;\r
 }\r
 \r
+\r
 //\r
 // We'll be saving the keys of installed tables so that we can roll them back\r
 // in case of failure. 128 tables should be enough for anyone (TM).\r
@@ -597,197 +852,184 @@ CheckRsdp (
 #define INSTALLED_TABLES_MAX 128\r
 \r
 /**\r
-  Download one ACPI table data file from QEMU and interpret it.\r
+  Process a QEMU_LOADER_ADD_POINTER command in order to see if its target byte\r
+  array is an ACPI table, and if so, install it.\r
 \r
-  @param[in] FwCfgFile         The NUL-terminated name of the fw_cfg file to\r
-                               download and interpret.\r
+  This function assumes that the entire QEMU linker/loader command file has\r
+  been processed successfuly in a prior first pass.\r
+\r
+  @param[in] AddPointer        The QEMU_LOADER_ADD_POINTER command to process.\r
+\r
+  @param[in] Tracker           The ORDERED_COLLECTION tracking the BLOB user\r
+                               structures.\r
 \r
   @param[in] AcpiProtocol      The ACPI table protocol used to install tables.\r
 \r
   @param[in,out] InstalledKey  On input, an array of INSTALLED_TABLES_MAX UINTN\r
                                elements, allocated by the caller. On output,\r
                                the function will have stored (appended) the\r
-                               AcpiProtocol-internal keys of the ACPI tables\r
-                               that the function has installed from the fw_cfg\r
-                               file. The array reflects installed tables even\r
-                               if the function returns with an error.\r
+                               AcpiProtocol-internal key of the ACPI table that\r
+                               the function has installed, if the AddPointer\r
+                               command identified an ACPI table that is\r
+                               different from RSDT and XSDT.\r
 \r
   @param[in,out] NumInstalled  On input, the number of entries already used in\r
                                InstalledKey; it must be in [0,\r
                                INSTALLED_TABLES_MAX] inclusive. On output, the\r
-                               parameter is updated to the new cumulative count\r
-                               of the keys stored in InstalledKey; the value\r
-                               reflects installed tables even if the function\r
-                               returns with an error.\r
-\r
-  @retval  EFI_INVALID_PARAMETER  NumInstalled is outside the allowed range on\r
-                                  input.\r
-\r
-  @retval  EFI_UNSUPPORTED        Firmware configuration is unavailable.\r
-\r
-  @retval  EFI_NOT_FOUND          The host doesn't export the requested fw_cfg\r
-                                  file.\r
-\r
-  @retval  EFI_OUT_OF_RESOURCES   Memory allocation failed, or no more room in\r
-                                  InstalledKey.\r
-\r
-  @retval  EFI_PROTOCOL_ERROR     Found truncated or invalid ACPI table header\r
-                                  in the fw_cfg contents.\r
-\r
-  @return                         Status codes returned by\r
-                                  AcpiProtocol->InstallAcpiTable().\r
-\r
+                               parameter is incremented if the AddPointer\r
+                               command identified an ACPI table that is\r
+                               different from RSDT and XSDT.\r
+\r
+  @retval EFI_INVALID_PARAMETER  NumInstalled was outside the allowed range on\r
+                                 input.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   The AddPointer command identified an ACPI\r
+                                 table different from RSDT and XSDT, but there\r
+                                 was no more room in InstalledKey.\r
+\r
+  @retval EFI_SUCCESS            AddPointer has been processed. Either an ACPI\r
+                                 table different from RSDT and XSDT has been\r
+                                 installed (reflected by InstalledKey and\r
+                                 NumInstalled), or RSDT or XSDT has been\r
+                                 identified but not installed, or the fw_cfg\r
+                                 blob pointed-into by AddPointer has been\r
+                                 marked as hosting something else than just\r
+                                 direct ACPI table contents.\r
+\r
+  @return                        Error codes returned by\r
+                                 AcpiProtocol->InstallAcpiTable().\r
 **/\r
-\r
 STATIC\r
 EFI_STATUS\r
-InstallQemuLinkedTables (\r
-  IN     CONST CHAR8             *FwCfgFile,\r
-  IN     EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol,\r
-  IN OUT UINTN                   InstalledKey[INSTALLED_TABLES_MAX],\r
-  IN OUT INT32                   *NumInstalled\r
+EFIAPI\r
+Process2ndPassCmdAddPointer (\r
+  IN     CONST QEMU_LOADER_ADD_POINTER *AddPointer,\r
+  IN     CONST ORDERED_COLLECTION      *Tracker,\r
+  IN     EFI_ACPI_TABLE_PROTOCOL       *AcpiProtocol,\r
+  IN OUT UINTN                         InstalledKey[INSTALLED_TABLES_MAX],\r
+  IN OUT INT32                         *NumInstalled\r
   )\r
 {\r
-  EFI_STATUS           Status;\r
-  FIRMWARE_CONFIG_ITEM TablesFile;\r
-  UINTN                TablesFileSize;\r
-  UINT8                *Tables;\r
-  UINTN                Processed;\r
+  CONST ORDERED_COLLECTION_ENTRY                     *TrackerEntry;\r
+  CONST ORDERED_COLLECTION_ENTRY                     *TrackerEntry2;\r
+  CONST BLOB                                         *Blob;\r
+  BLOB                                               *Blob2;\r
+  CONST UINT8                                        *PointerField;\r
+  UINT64                                             PointerValue;\r
+  UINTN                                              Blob2Remaining;\r
+  UINTN                                              TableSize;\r
+  CONST EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *Facs;\r
+  CONST EFI_ACPI_DESCRIPTION_HEADER                  *Header;\r
+  EFI_STATUS                                         Status;\r
 \r
   if (*NumInstalled < 0 || *NumInstalled > INSTALLED_TABLES_MAX) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Status = QemuFwCfgFindFile (FwCfgFile, &TablesFile, &TablesFileSize);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "%a: \"%a\" unavailable: %r\n", __FUNCTION__,\r
-      FwCfgFile, Status));\r
-    return Status;\r
-  }\r
+  TrackerEntry = OrderedCollectionFind (Tracker, AddPointer->PointerFile);\r
+  TrackerEntry2 = OrderedCollectionFind (Tracker, AddPointer->PointeeFile);\r
+  Blob = OrderedCollectionUserStruct (TrackerEntry);\r
+  Blob2 = OrderedCollectionUserStruct (TrackerEntry2);\r
+  PointerField = Blob->Base + AddPointer->PointerOffset;\r
+  PointerValue = 0;\r
+  CopyMem (&PointerValue, PointerField, AddPointer->PointerSize);\r
 \r
-  Tables = AllocatePool (TablesFileSize);\r
-  if (Tables == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  QemuFwCfgSelectItem (TablesFile);\r
-  QemuFwCfgReadBytes (TablesFileSize, Tables);\r
-\r
-  Processed = 0;\r
-  while (Processed < TablesFileSize) {\r
-    UINTN                       Remaining;\r
-    UINTN                       RsdpSize;\r
-    EFI_ACPI_DESCRIPTION_HEADER *Probe;\r
+  //\r
+  // We assert that PointerValue falls inside Blob2's contents. This is ensured\r
+  // by the Blob2->Size check and later checks in ProcessCmdAddPointer().\r
+  //\r
+  Blob2Remaining = (UINTN)Blob2->Base;\r
+  ASSERT(PointerValue >= Blob2Remaining);\r
+  Blob2Remaining += Blob2->Size;\r
+  ASSERT (PointerValue < Blob2Remaining);\r
 \r
-    Remaining = TablesFileSize - Processed;\r
+  Blob2Remaining -= (UINTN) PointerValue;\r
+  DEBUG ((EFI_D_VERBOSE, "%a: checking for ACPI header in \"%a\" at 0x%Lx "\r
+    "(remaining: 0x%Lx): ", __FUNCTION__, AddPointer->PointeeFile,\r
+    PointerValue, (UINT64)Blob2Remaining));\r
 \r
-    //\r
-    // See if we're looking at an RSD PTR structure.\r
-    //\r
-    RsdpSize = 0;\r
-    Status = CheckRsdp (Tables + Processed, Remaining, &RsdpSize);\r
-    if (Status == EFI_PROTOCOL_ERROR) {\r
-      //\r
-      // RSD PTR found but its size is inconsistent; abort processing. (Note\r
-      // that "RSD PTR found" excludes the NUL-padding case by definition.)\r
-      //\r
-      break;\r
-    }\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // Consistent RSD PTR found, skip it.\r
-      //\r
-      DEBUG ((EFI_D_VERBOSE, "%a: \"%a\" offset 0x%016Lx: RSD PTR "\r
-        "Length=0x%08x\n", __FUNCTION__, FwCfgFile, (UINT64)Processed,\r
-        (UINT32)RsdpSize));\r
-      Processed += RsdpSize;\r
-      continue;\r
-    }\r
-    ASSERT (Status == EFI_NOT_FOUND);\r
+  TableSize = 0;\r
 \r
-    //\r
-    // What we're looking at is not an RSD PTR structure; attempt to parse it\r
-    // as an ACPI table.\r
-    //\r
-    if (Remaining < sizeof *Probe) {\r
-      Status = EFI_PROTOCOL_ERROR;\r
-      break;\r
+  //\r
+  // To make our job simple, the FACS has a custom header. Sigh.\r
+  //\r
+  if (sizeof *Facs <= Blob2Remaining) {\r
+    Facs = (EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)PointerValue;\r
+\r
+    if (Facs->Length >= sizeof *Facs &&\r
+        Facs->Length <= Blob2Remaining &&\r
+        Facs->Signature ==\r
+                EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) {\r
+      DEBUG ((EFI_D_VERBOSE, "found \"%-4.4a\" size 0x%x\n",\r
+        (CONST CHAR8 *)&Facs->Signature, Facs->Length));\r
+      TableSize = Facs->Length;\r
     }\r
+  }\r
 \r
-    Probe = (EFI_ACPI_DESCRIPTION_HEADER *) (Tables + Processed);\r
-    if (Remaining < Probe->Length || Probe->Length < sizeof *Probe) {\r
-      Status = EFI_PROTOCOL_ERROR;\r
-      break;\r
-    }\r
+  //\r
+  // check for the uniform tables\r
+  //\r
+  if (TableSize == 0 && sizeof *Header <= Blob2Remaining) {\r
+    Header = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)PointerValue;\r
 \r
-    DEBUG ((EFI_D_VERBOSE, "%a: \"%a\" offset 0x%016Lx:"\r
-      " Signature=\"%-4.4a\" Length=0x%08x\n",\r
-      __FUNCTION__, FwCfgFile, (UINT64) Processed,\r
-      (CONST CHAR8 *) &Probe->Signature, Probe->Length));\r
+    if (Header->Length >= sizeof *Header &&\r
+        Header->Length <= Blob2Remaining &&\r
+        CalculateSum8 ((CONST UINT8 *)Header, Header->Length) == 0) {\r
+      //\r
+      // This looks very much like an ACPI table from QEMU:\r
+      // - Length field consistent with both ACPI and containing blob size\r
+      // - checksum is correct\r
+      //\r
+      DEBUG ((EFI_D_VERBOSE, "found \"%-4.4a\" size 0x%x\n",\r
+        (CONST CHAR8 *)&Header->Signature, Header->Length));\r
+      TableSize = Header->Length;\r
 \r
-    //\r
-    // skip automatically handled "root" tables: RSDT, XSDT\r
-    //\r
-    if (Probe->Signature !=\r
-                        EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE &&\r
-        Probe->Signature !=\r
+      //\r
+      // Skip RSDT and XSDT because those are handled by\r
+      // EFI_ACPI_TABLE_PROTOCOL automatically.\r
+      if (Header->Signature ==\r
+                    EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE ||\r
+          Header->Signature ==\r
                     EFI_ACPI_2_0_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) {\r
-      if (*NumInstalled == INSTALLED_TABLES_MAX) {\r
-        DEBUG ((EFI_D_ERROR, "%a: can't install more than %d tables\n",\r
-          __FUNCTION__, INSTALLED_TABLES_MAX));\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-        break;\r
+        return EFI_SUCCESS;\r
       }\r
-\r
-      Status = AcpiProtocol->InstallAcpiTable (AcpiProtocol, Probe,\r
-                 Probe->Length, &InstalledKey[*NumInstalled]);\r
-      if (EFI_ERROR (Status)) {\r
-        DEBUG ((EFI_D_ERROR,\r
-          "%a: failed to install table \"%-4.4a\" at \"%a\" offset 0x%Lx: "\r
-          "%r\n", __FUNCTION__, (CONST CHAR8 *)&Probe->Signature, FwCfgFile,\r
-          (UINT64) Processed, Status));\r
-        break;\r
-      }\r
-\r
-      ++*NumInstalled;\r
     }\r
-\r
-    Processed += Probe->Length;\r
   }\r
 \r
-  //\r
-  // NUL-padding at the end is accepted\r
-  //\r
-  if (Status == EFI_PROTOCOL_ERROR) {\r
-    UINTN ErrorLocation;\r
-\r
-    ErrorLocation = Processed;\r
-    while (Processed < TablesFileSize && Tables[Processed] == '\0') {\r
-      ++Processed;\r
-    }\r
-    if (Processed < TablesFileSize) {\r
-      DEBUG ((EFI_D_ERROR, "%a: truncated or invalid ACPI table header at "\r
-        "\"%a\" offset 0x%Lx\n", __FUNCTION__, FwCfgFile,\r
-        (UINT64)ErrorLocation));\r
-    }\r
+  if (TableSize == 0) {\r
+    DEBUG ((EFI_D_VERBOSE, "not found; marking fw_cfg blob as opaque\n"));\r
+    Blob2->HostsOnlyTableData = FALSE;\r
+    return EFI_SUCCESS;\r
   }\r
 \r
-  if (Processed == TablesFileSize) {\r
-    Status = EFI_SUCCESS;\r
-  } else {\r
-    ASSERT (EFI_ERROR (Status));\r
+  if (*NumInstalled == INSTALLED_TABLES_MAX) {\r
+    DEBUG ((EFI_D_ERROR, "%a: can't install more than %d tables\n",\r
+      __FUNCTION__, INSTALLED_TABLES_MAX));\r
+    return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  FreePool (Tables);\r
-  return Status;\r
+  Status = AcpiProtocol->InstallAcpiTable (AcpiProtocol,\r
+                           (VOID *)(UINTN)PointerValue, TableSize,\r
+                           &InstalledKey[*NumInstalled]);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: InstallAcpiTable(): %r\n", __FUNCTION__,\r
+      Status));\r
+    return Status;\r
+  }\r
+  ++*NumInstalled;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
+\r
 /**\r
-  Download all ACPI table data files from QEMU and interpret them.\r
+  Download, process, and install ACPI table data from the QEMU loader\r
+  interface.\r
 \r
   @param[in] AcpiProtocol  The ACPI table protocol used to install tables.\r
 \r
-  @retval  EFI_UNSUPPORTED       Firmware configuration is unavailable.\r
+  @retval  EFI_UNSUPPORTED       Firmware configuration is unavailable, or QEMU\r
+                                 loader command with unsupported parameters\r
+                                 has been found.\r
 \r
   @retval  EFI_NOT_FOUND         The host doesn't export the required fw_cfg\r
                                  files.\r
@@ -795,36 +1037,105 @@ InstallQemuLinkedTables (
   @retval  EFI_OUT_OF_RESOURCES  Memory allocation failed, or more than\r
                                  INSTALLED_TABLES_MAX tables found.\r
 \r
-  @retval  EFI_PROTOCOL_ERROR    Found truncated or invalid ACPI table header\r
-                                 in the fw_cfg contents.\r
+  @retval  EFI_PROTOCOL_ERROR    Found invalid fw_cfg contents.\r
 \r
   @return                        Status codes returned by\r
                                  AcpiProtocol->InstallAcpiTable().\r
 \r
 **/\r
-\r
 EFI_STATUS\r
 EFIAPI\r
 InstallAllQemuLinkedTables (\r
   IN   EFI_ACPI_TABLE_PROTOCOL       *AcpiProtocol\r
   )\r
 {\r
-  UINTN                *InstalledKey;\r
-  INT32                Installed;\r
-  EFI_STATUS           Status;\r
+  EFI_STATUS               Status;\r
+  FIRMWARE_CONFIG_ITEM     FwCfgItem;\r
+  UINTN                    FwCfgSize;\r
+  QEMU_LOADER_ENTRY        *LoaderStart;\r
+  CONST QEMU_LOADER_ENTRY  *LoaderEntry, *LoaderEnd;\r
+  ORDERED_COLLECTION       *Tracker;\r
+  UINTN                    *InstalledKey;\r
+  INT32                    Installed;\r
+  ORDERED_COLLECTION_ENTRY *TrackerEntry, *TrackerEntry2;\r
+\r
+  Status = QemuFwCfgFindFile ("etc/table-loader", &FwCfgItem, &FwCfgSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  if (FwCfgSize % sizeof *LoaderEntry != 0) {\r
+    DEBUG ((EFI_D_ERROR, "%a: \"etc/table-loader\" has invalid size 0x%Lx\n",\r
+      __FUNCTION__, (UINT64)FwCfgSize));\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+\r
+  LoaderStart = AllocatePool (FwCfgSize);\r
+  if (LoaderStart == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  QemuFwCfgSelectItem (FwCfgItem);\r
+  QemuFwCfgReadBytes (FwCfgSize, LoaderStart);\r
+  LoaderEnd = LoaderStart + FwCfgSize / sizeof *LoaderEntry;\r
+\r
+  Tracker = OrderedCollectionInit (BlobCompare, BlobKeyCompare);\r
+  if (Tracker == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreeLoader;\r
+  }\r
+\r
+  //\r
+  // first pass: process the commands\r
+  //\r
+  for (LoaderEntry = LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry) {\r
+    switch (LoaderEntry->Type) {\r
+    case QemuLoaderCmdAllocate:\r
+      Status = ProcessCmdAllocate (&LoaderEntry->Command.Allocate, Tracker);\r
+      break;\r
+\r
+    case QemuLoaderCmdAddPointer:\r
+      Status = ProcessCmdAddPointer (&LoaderEntry->Command.AddPointer,\r
+                 Tracker);\r
+      break;\r
+\r
+    case QemuLoaderCmdAddChecksum:\r
+      Status = ProcessCmdAddChecksum (&LoaderEntry->Command.AddChecksum,\r
+                 Tracker);\r
+      break;\r
+\r
+    default:\r
+      DEBUG ((EFI_D_VERBOSE, "%a: unknown loader command: 0x%x\n",\r
+        __FUNCTION__, LoaderEntry->Type));\r
+      break;\r
+    }\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      goto FreeTracker;\r
+    }\r
+  }\r
 \r
   InstalledKey = AllocatePool (INSTALLED_TABLES_MAX * sizeof *InstalledKey);\r
   if (InstalledKey == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreeTracker;\r
   }\r
+\r
+  //\r
+  // second pass: identify and install ACPI tables\r
+  //\r
   Installed = 0;\r
+  for (LoaderEntry = LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry) {\r
+    if (LoaderEntry->Type == QemuLoaderCmdAddPointer) {\r
+      Status = Process2ndPassCmdAddPointer (&LoaderEntry->Command.AddPointer,\r
+                 Tracker, AcpiProtocol, InstalledKey, &Installed);\r
+      if (EFI_ERROR (Status)) {\r
+        break;\r
+      }\r
+    }\r
+  }\r
 \r
-  Status = InstallQemuLinkedTables ("etc/acpi/tables", AcpiProtocol,\r
-             InstalledKey, &Installed);\r
   if (EFI_ERROR (Status)) {\r
-    ASSERT (Status != EFI_INVALID_PARAMETER);\r
     //\r
-    // Roll back partial installation.\r
+    // roll back partial installation\r
     //\r
     while (Installed > 0) {\r
       --Installed;\r
@@ -835,5 +1146,33 @@ InstallAllQemuLinkedTables (
   }\r
 \r
   FreePool (InstalledKey);\r
+\r
+FreeTracker:\r
+  //\r
+  // Tear down the tracker infrastructure. Each fw_cfg blob will be left in\r
+  // place only if we're exiting with success and the blob hosts data that is\r
+  // not directly part of some ACPI table.\r
+  //\r
+  for (TrackerEntry = OrderedCollectionMin (Tracker); TrackerEntry != NULL;\r
+       TrackerEntry = TrackerEntry2) {\r
+    VOID *UserStruct;\r
+    BLOB *Blob;\r
+\r
+    TrackerEntry2 = OrderedCollectionNext (TrackerEntry);\r
+    OrderedCollectionDelete (Tracker, TrackerEntry, &UserStruct);\r
+    Blob = UserStruct;\r
+\r
+    if (EFI_ERROR (Status) || Blob->HostsOnlyTableData) {\r
+      DEBUG ((EFI_D_VERBOSE, "%a: freeing \"%a\"\n", __FUNCTION__,\r
+        Blob->File));\r
+      gBS->FreePages ((UINTN)Blob->Base, EFI_SIZE_TO_PAGES (Blob->Size));\r
+    }\r
+    FreePool (Blob);\r
+  }\r
+  OrderedCollectionUninit (Tracker);\r
+\r
+FreeLoader:\r
+  FreePool (LoaderStart);\r
+\r
   return Status;\r
 }\r