public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v5] MinPlatformPkg: Update HWSignature filed in FACS
@ 2023-05-03  2:39 VincentX Ke
  2023-05-08  3:23 ` [edk2-devel] " Ni, Ray
  0 siblings, 1 reply; 3+ messages in thread
From: VincentX Ke @ 2023-05-03  2:39 UTC (permalink / raw)
  To: devel
  Cc: VincentX Ke, Chasel Chiu, Nate DeSimone, Isaac Oram, Liming Gao,
	Eric Dong

From: VincentX Ke <vincentx.ke@intel.com>

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4428

Calculating CRC based on each ACPI table.
Update HWSignature filed in FACS based on CRC while ACPI table changed.

Change-Id: Ic0ca66ff10cda0fbcd0683020fab1bc9aea9b78c
Signed-off-by: VincentX Ke <vincentx.ke@intel.com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>
Cc: Isaac Oram <isaac.w.oram@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Eric Dong <eric.dong@intel.com>
Signed-off-by: VincentX Ke <vincentx.ke@intel.com>
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c   | 182 +++++++++++++-------
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.inf |   1 +
 2 files changed, 118 insertions(+), 65 deletions(-)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index e967031a3b..a940424ced 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -1191,98 +1191,150 @@ PlatformUpdateTables (
 }
 
 /**
-  This function calculates RCR based on PCI Device ID and Vendor ID from the devices
-  available on the platform.
-  It also includes other instances of BIOS change to calculate CRC and provides as
-  HWSignature filed in FADT table.
+  This function calculates CRC based on each offset in the ACPI table.
+
+  @param[in] Table          The pointer to ACPI table that
+                            required to calculate CRC.
+
+  @retval CRC               A pointer to allocate UINT32 that
+                            contains the CRC32 data.
+**/
+UINT32
+AcpiTableCrcCalculator (
+  IN  VOID  *Table
+  )
+{
+  UINT32  CRC;
+
+  CRC = 0;
+
+  //
+  // Calculate CRC value.
+  //
+  if (((EFI_ACPI_6_5_COMMON_HEADER *)(UINTN)Table)->Signature == EFI_ACPI_6_5_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) {
+    //
+    // Zero HardwareSignature field before Calculating FACS CRC
+    //
+    do {
+      ((EFI_ACPI_6_5_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)Table)->HardwareSignature = 0;
+    } while (((EFI_ACPI_6_5_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)Table)->HardwareSignature);
+
+    gBS->CalculateCrc32 ((UINT8 *)Table, (UINTN)((EFI_ACPI_6_5_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)Table)->Length, &CRC);
+  } else {
+    gBS->CalculateCrc32 ((UINT8 *)Table, (UINTN)((EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Table)->Length, &CRC);
+  }
+
+  return CRC;
+}
+
+/**
+  This function calculates CRC based on each ACPI table.
+  It also calculates CRC and provides as HWSignature filed in FACS.
 **/
 VOID
-IsHardwareChange (
+IsAcpiTableChange (
   VOID
   )
 {
-  EFI_STATUS                    Status;
-  UINTN                         Index;
-  UINTN                         HandleCount;
-  EFI_HANDLE                    *HandleBuffer;
-  EFI_PCI_IO_PROTOCOL           *PciIo;
-  UINT32                        CRC;
-  UINT32                        *HWChange;
-  UINTN                         HWChangeSize;
-  UINT32                        PciId;
-  UINTN                         Handle;
-  EFI_ACPI_6_3_FIRMWARE_ACPI_CONTROL_STRUCTURE *FacsPtr;
-  EFI_ACPI_6_3_FIXED_ACPI_DESCRIPTION_TABLE    *pFADT;
-
-  HandleCount  = 0;
-  HandleBuffer = NULL;
-
-  Status = gBS->LocateHandleBuffer (
-                  ByProtocol,
-                  &gEfiPciIoProtocolGuid,
-                  NULL,
-                  &HandleCount,
-                  &HandleBuffer
-                  );
-  if (EFI_ERROR (Status)) {
-    return; // PciIO protocol not installed yet!
+  EFI_STATUS                                    Status;
+  UINTN                                         Index;
+  UINTN                                         AcpiTableCount;
+  UINT32                                        Table;
+  UINT32                                        HWSignature;
+  UINT32                                        *AcpiTableCrc;
+  EFI_ACPI_6_5_ROOT_SYSTEM_DESCRIPTION_POINTER  *Rsdp;
+  EFI_ACPI_DESCRIPTION_HEADER                   *Rsdt;
+  EFI_ACPI_DESCRIPTION_HEADER                   *Xsdt;
+  EFI_ACPI_6_5_FIRMWARE_ACPI_CONTROL_STRUCTURE  *FacsPtr;
+  EFI_ACPI_6_5_FIXED_ACPI_DESCRIPTION_TABLE     *pFADT;
+
+  AcpiTableCount = 0;
+  AcpiTableCrc   = NULL;
+  Rsdp           = NULL;
+  Rsdt           = NULL;
+  Xsdt           = NULL;
+  FacsPtr        = NULL;
+  pFADT          = NULL;
+
+  DEBUG ((DEBUG_INFO, "%a() - Start\n", __FUNCTION__));
+
+  Status = EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid, (VOID **)&Rsdp);
+  if (EFI_ERROR (Status) || (Rsdp == NULL)) {
+    return;
   }
 
   //
-  // Allocate memory for HWChange and add additional entrie for
-  // pFADT->XDsdt
+  // ACPI table count starts with 2 as RSDT and XSDT are already located.
+  // Then add ACPI tables found by XSDT and FADT.
   //
-  HWChangeSize = HandleCount + 1;
-  HWChange = AllocateZeroPool (sizeof(UINT32) * HWChangeSize);
-  ASSERT(HWChange != NULL);
+  Rsdt            = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Rsdp->RsdtAddress;
+  Xsdt            = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Rsdp->XsdtAddress;
+  AcpiTableCount += 2;
+  AcpiTableCount  = AcpiTableCount + (Xsdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER))/sizeof (UINT64);
 
-  if (HWChange == NULL) return;
+  for (Index = sizeof (EFI_ACPI_DESCRIPTION_HEADER); Index < (Xsdt->Length); Index = Index + sizeof (UINT64)) {
+    Table = *((UINT32 *)((UINT8 *)Xsdt + Index));
+    if (((EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Table)->Signature == EFI_ACPI_6_5_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
+      pFADT = (EFI_ACPI_6_5_FIXED_ACPI_DESCRIPTION_TABLE *)(UINTN)Table;
+      if (pFADT->FirmwareCtrl) {
+        AcpiTableCount++;
+      }
 
-  //
-  // add HWChange inputs: PCI devices
-  //
-  for (Index = 0; HandleCount > 0; HandleCount--) {
-    PciId = 0;
-    Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiPciIoProtocolGuid, (VOID **) &PciIo);
-    if (!EFI_ERROR (Status)) {
-      Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0, 1, &PciId);
-      if (EFI_ERROR (Status)) {
-        continue;
+      if ((pFADT->XDsdt) || (pFADT->Dsdt)) {
+        AcpiTableCount++;
       }
-      HWChange[Index++] = PciId;
     }
   }
 
   //
-  // Locate FACP Table
+  // Allocate memory for founded ACPI tables.
   //
-  Handle = 0;
-  Status = LocateAcpiTableBySignature (
-              EFI_ACPI_6_3_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,
-              (EFI_ACPI_DESCRIPTION_HEADER **) &pFADT,
-              &Handle
-              );
-  if (EFI_ERROR (Status) || (pFADT == NULL)) {
-    return;  //Table not found or out of memory resource for pFADT table
+  AcpiTableCrc = AllocateZeroPool (sizeof (UINT32) * AcpiTableCount);
+  if (AcpiTableCrc == NULL) {
+    return;
+  }
+
+  AcpiTableCount                 = 0;
+  AcpiTableCrc[AcpiTableCount++] = AcpiTableCrcCalculator ((VOID *)(UINTN)Rsdt);
+  AcpiTableCrc[AcpiTableCount++] = AcpiTableCrcCalculator ((VOID *)(UINTN)Xsdt);
+
+  for (Index = sizeof (EFI_ACPI_DESCRIPTION_HEADER); Index < (Xsdt->Length); Index = Index + sizeof (UINT64)) {
+    Table                          = *((UINT32 *)((UINT8 *)Xsdt + Index));
+    AcpiTableCrc[AcpiTableCount++] = AcpiTableCrcCalculator ((VOID *)(UINTN)Table);
+    if (((EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Table)->Signature == EFI_ACPI_6_5_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
+      pFADT = (EFI_ACPI_6_5_FIXED_ACPI_DESCRIPTION_TABLE *)(UINTN)Table;
+      if (pFADT->FirmwareCtrl) {
+        AcpiTableCrc[AcpiTableCount++] = AcpiTableCrcCalculator ((VOID *)(UINTN)pFADT->FirmwareCtrl);
+      }
+
+      if (pFADT->XDsdt) {
+        AcpiTableCrc[AcpiTableCount++] = AcpiTableCrcCalculator ((VOID *)(UINTN)pFADT->XDsdt);
+      } else {
+        AcpiTableCrc[AcpiTableCount++] = AcpiTableCrcCalculator ((VOID *)(UINTN)pFADT->Dsdt);
+      }
+    }
   }
 
   //
-  // add HWChange inputs: others
+  // pFADT table not found.
   //
-  HWChange[Index++] = (UINT32)pFADT->XDsdt;
+  if (pFADT == NULL) {
+    return;
+  }
 
   //
-  // Calculate CRC value with HWChange data.
+  // Calculate HWSignature data.
   //
-  Status = gBS->CalculateCrc32(HWChange, HWChangeSize, &CRC);
-  DEBUG ((DEBUG_INFO, "CRC = %x and Status = %r\n", CRC, Status));
+  Status = gBS->CalculateCrc32 (AcpiTableCrc, AcpiTableCount, &HWSignature);
+  DEBUG ((DEBUG_INFO, "HardwareSignature = %x and Status = %r\n", HWSignature, Status));
 
   //
   // Set HardwareSignature value based on CRC value.
   //
-  FacsPtr = (EFI_ACPI_6_3_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)pFADT->FirmwareCtrl;
-  FacsPtr->HardwareSignature = CRC;
-  FreePool (HWChange);
+  FacsPtr                    = (EFI_ACPI_6_5_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)pFADT->FirmwareCtrl;
+  FacsPtr->HardwareSignature = HWSignature;
+  FreePool (AcpiTableCrc);
+  DEBUG ((DEBUG_INFO, "%a() - End\n", __FUNCTION__));
 }
 
 VOID
@@ -1329,7 +1381,7 @@ AcpiEndOfDxeEvent (
   //
   // Calculate Hardware Signature value based on current platform configurations
   //
-  IsHardwareChange ();
+  IsAcpiTableChange ();
 }
 
 /**
diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.inf b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.inf
index 694492112b..f47cc3908d 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.inf
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.inf
@@ -128,6 +128,7 @@
   gEfiGlobalVariableGuid                        ## CONSUMES
   gEfiHobListGuid                               ## CONSUMES
   gEfiEndOfDxeEventGroupGuid                    ## CONSUMES
+  gEfiAcpiTableGuid                             ## CONSUMES
 
 [Depex]
   gEfiAcpiTableProtocolGuid           AND
-- 
2.39.2.windows.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-05-10 10:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-03  2:39 [PATCH v5] MinPlatformPkg: Update HWSignature filed in FACS VincentX Ke
2023-05-08  3:23 ` [edk2-devel] " Ni, Ray
2023-05-10 10:11   ` VincentX Ke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox