public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter
@ 2022-06-29  8:56 Min Xu
  2022-06-29  8:56 ` [PATCH V3 1/7] OvmfPkg/PeilessStartupLib: Delete TdxValidateCfv Min Xu
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:56 UTC (permalink / raw)
  To: devel
  Cc: Min Xu, Leif Lindholm, Ard Biesheuvel, Abner Chang,
	Daniel Schaefer, Erdem Aktas, James Bottomley, Jiewen Yao,
	Tom Lendacky, Gerd Hoffmann

Secure-Boot related variables include the PK/KEK/DB/DBX and they are
stored in NvVarStore (OVMF_VARS.fd). When lauching with -pflash,
QEMU/OVMF will use emulated flash, and fully support UEFI variables.
But when launching with -bios parameter, UEFI variables will be partially
emulated, and non-volatile variables may lose their contents after a
reboot. See OvmfPkg/README.

Tdx guest is an example that -pflash is not supported. So this patch-set
is designed to initialize the NvVarStore with the content of in
OVMF_VARS.fd.

patch 1:
 Add a new function (AllocateRuntimePages) in PrePiMemoryAllocationLib.
 This function will be used in PeilessStartupLib which will run
 in SEC phase.

patch 2:
 Delete the TdxValidateCfv in PeilessStartupLib. Because it is going to
 be renamed to PlatformValidateNvVarStore and be moved to PlatformInitLib.

patch 3 - 7:
 Then we add functions for EmuVariableNvStore in PlatformInitLib. This
 lib will then be called in OvmfPkg/PlatformPei and PeilessStartupLib.
 We also shortcut ConnectNvVarsToFileSystem in secure-boot.

patch 8:
 At last a build-flag (SECURE_BOOT_FEATURE_ENABLED) is introduced in
 the dsc in OvmfPkg. Because the copy over of OVMR_VARS.fd to
 EmuVariableNvStore is only required when secure-boot is enabled.

Code: https://github.com/mxu9/edk2/tree/secure-boot.v3

v3 changes:
 - Renamed TdxValidateCfv to PlatformValidateNvVarStore and implemented
   in PlatformInitlLib/Platform.c.
 - Shortcut ConnectNvVarsToFileSystem in secure-boot.
 - Other minor changes, such as adding log in
   PlatformInitEmuVariableNvStore.

v2 changes:
 - The v1 title is "Enable Secure-Boot in Tdx guest". Because the
   patch-setwe was first designed to fix the gap when secure-boot feature
   was enabled in Tdx guest. After discussing with the community (see
   the disuccsions under https://edk2.groups.io/g/devel/message/90589)
   this patch-set can fix the secure-boot issue when OVMF is lauched
   with -bios parameter. So the title is updated.
 - Add a new function (AllocateRuntimePages) in PrePiMemoryAllocationLib.
 - Add build-flag SECURE_BOOT_FEATURE_ENABLED to control the copy over
   of OVMF_VARS.fd to EmuVariableNvStore.

Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Abner Chang <abner.chang@hpe.com>
Cc: Daniel Schaefer <daniel.schaefer@hpe.com>
Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com> [jejb]
Cc: Jiewen Yao <jiewen.yao@intel.com> [jyao1]
Cc: Tom Lendacky <thomas.lendacky@amd.com> [tlendacky]
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>

Min M Xu (7):
  OvmfPkg/PeilessStartupLib: Delete TdxValidateCfv
  OvmfPkg/PlatformInitLib: Add functions for EmuVariableNvStore
  OvmfPkg/PlatformPei: Update ReserveEmuVariableNvStore
  OvmfPkg: Reserve and init EmuVariableNvStore in Pei-less Startup
  OvmfPkg/NvVarsFileLib: Shortcut ConnectNvVarsToFileSystem in
    secure-boot
  OvmfPkg/TdxDxe: Set PcdEmuVariableNvStoreReserved
  OvmfPkg: Add build-flag SECURE_BOOT_FEATURE_ENABLED

 OvmfPkg/CloudHv/CloudHvX64.dsc                |   9 +
 OvmfPkg/Include/Library/PlatformInitLib.h     |  51 ++++
 OvmfPkg/IntelTdx/IntelTdxX64.dsc              |   9 +
 OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.c |   7 +
 OvmfPkg/Library/PeilessStartupLib/IntelTdx.c  | 153 -----------
 .../PeilessStartupLib/PeilessStartup.c        |  15 +-
 .../PeilessStartupInternal.h                  |  17 --
 OvmfPkg/Library/PlatformInitLib/Platform.c    | 238 ++++++++++++++++++
 .../PlatformInitLib/PlatformInitLib.inf       |   3 +
 OvmfPkg/OvmfPkgIa32.dsc                       |   9 +
 OvmfPkg/OvmfPkgIa32X64.dsc                    |   9 +
 OvmfPkg/OvmfPkgX64.dsc                        |   9 +
 OvmfPkg/PlatformPei/Platform.c                |  25 +-
 OvmfPkg/TdxDxe/TdxDxe.c                       |   2 +
 OvmfPkg/TdxDxe/TdxDxe.inf                     |   1 +
 15 files changed, 361 insertions(+), 196 deletions(-)

-- 
2.29.2.windows.2


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

* [PATCH V3 1/7] OvmfPkg/PeilessStartupLib: Delete TdxValidateCfv
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
@ 2022-06-29  8:56 ` Min Xu
  2022-06-29  8:56 ` [PATCH V3 2/7] OvmfPkg/PlatformInitLib: Add functions for EmuVariableNvStore Min Xu
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:56 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

TdxValidateCfv is used to validate the integrity of FlashNvVarStore
(PcdOvmfFlashNvStorageVariableBase) and it is not Tdx specific.
So it will be moved to PlatformInitLib and be renamed to
PlatformValidateNvVarStore in the following patch. And it will be called
before EmuVaribleNvStore is initialized with the content in
FlashNvVarStore.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/Library/PeilessStartupLib/IntelTdx.c  | 153 ------------------
 .../PeilessStartupLib/PeilessStartup.c        |   8 -
 .../PeilessStartupInternal.h                  |  17 --
 3 files changed, 178 deletions(-)

diff --git a/OvmfPkg/Library/PeilessStartupLib/IntelTdx.c b/OvmfPkg/Library/PeilessStartupLib/IntelTdx.c
index 484fd21057c8..216c413caad5 100644
--- a/OvmfPkg/Library/PeilessStartupLib/IntelTdx.c
+++ b/OvmfPkg/Library/PeilessStartupLib/IntelTdx.c
@@ -7,8 +7,6 @@
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
-#include <Guid/VariableFormat.h>
-#include <Guid/SystemNvDataGuid.h>
 #include <IndustryStandard/Tpm20.h>
 #include <IndustryStandard/UefiTcgPlatform.h>
 #include <Library/HobLib.h>
@@ -37,157 +35,6 @@ typedef struct {
 
 #pragma pack()
 
-/**
-  Check padding data all bit should be 1.
-
-  @param[in] Buffer     - A pointer to buffer header
-  @param[in] BufferSize - Buffer size
-
-  @retval  TRUE   - The padding data is valid.
-  @retval  TRUE  - The padding data is invalid.
-
-**/
-BOOLEAN
-CheckPaddingData (
-  IN UINT8   *Buffer,
-  IN UINT32  BufferSize
-  )
-{
-  UINT32  index;
-
-  for (index = 0; index < BufferSize; index++) {
-    if (Buffer[index] != 0xFF) {
-      return FALSE;
-    }
-  }
-
-  return TRUE;
-}
-
-/**
-  Check the integrity of CFV data.
-
-  @param[in] TdxCfvBase - A pointer to CFV header
-  @param[in] TdxCfvSize - CFV data size
-
-  @retval  TRUE   - The CFV data is valid.
-  @retval  FALSE  - The CFV data is invalid.
-
-**/
-BOOLEAN
-EFIAPI
-TdxValidateCfv (
-  IN UINT8   *TdxCfvBase,
-  IN UINT32  TdxCfvSize
-  )
-{
-  UINT16                         Checksum;
-  UINTN                          VariableBase;
-  UINT32                         VariableOffset;
-  UINT32                         VariableOffsetBeforeAlign;
-  EFI_FIRMWARE_VOLUME_HEADER     *CfvFvHeader;
-  VARIABLE_STORE_HEADER          *CfvVariableStoreHeader;
-  AUTHENTICATED_VARIABLE_HEADER  *VariableHeader;
-
-  static EFI_GUID  FvHdrGUID       = EFI_SYSTEM_NV_DATA_FV_GUID;
-  static EFI_GUID  VarStoreHdrGUID = EFI_AUTHENTICATED_VARIABLE_GUID;
-
-  VariableOffset = 0;
-
-  if (TdxCfvBase == NULL) {
-    DEBUG ((DEBUG_ERROR, "TDX CFV: CFV pointer is NULL\n"));
-    return FALSE;
-  }
-
-  //
-  // Verify the header zerovetor, filesystemguid,
-  // revision, signature, attributes, fvlength, checksum
-  // HeaderLength cannot be an odd number
-  //
-  CfvFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)TdxCfvBase;
-
-  if ((!IsZeroBuffer (CfvFvHeader->ZeroVector, 16)) ||
-      (!CompareGuid (&FvHdrGUID, &CfvFvHeader->FileSystemGuid)) ||
-      (CfvFvHeader->Signature != EFI_FVH_SIGNATURE) ||
-      (CfvFvHeader->Attributes != 0x4feff) ||
-      (CfvFvHeader->Revision != EFI_FVH_REVISION) ||
-      (CfvFvHeader->FvLength != TdxCfvSize)
-      )
-  {
-    DEBUG ((DEBUG_ERROR, "TDX CFV: Basic FV headers were invalid\n"));
-    return FALSE;
-  }
-
-  //
-  // Verify the header checksum
-  //
-  Checksum = CalculateSum16 ((VOID *)CfvFvHeader, CfvFvHeader->HeaderLength);
-
-  if (Checksum != 0) {
-    DEBUG ((DEBUG_ERROR, "TDX CFV: FV checksum was invalid\n"));
-    return FALSE;
-  }
-
-  //
-  // Verify the header signature, size, format, state
-  //
-  CfvVariableStoreHeader = (VARIABLE_STORE_HEADER *)(TdxCfvBase + CfvFvHeader->HeaderLength);
-  if ((!CompareGuid (&VarStoreHdrGUID, &CfvVariableStoreHeader->Signature)) ||
-      (CfvVariableStoreHeader->Format != VARIABLE_STORE_FORMATTED) ||
-      (CfvVariableStoreHeader->State != VARIABLE_STORE_HEALTHY) ||
-      (CfvVariableStoreHeader->Size > (CfvFvHeader->FvLength - CfvFvHeader->HeaderLength)) ||
-      (CfvVariableStoreHeader->Size < sizeof (VARIABLE_STORE_HEADER))
-      )
-  {
-    DEBUG ((DEBUG_ERROR, "TDX CFV: Variable Store header was invalid\n"));
-    return FALSE;
-  }
-
-  //
-  // Verify the header startId, state
-  // Verify data to the end
-  //
-  VariableBase = (UINTN)TdxCfvBase + CfvFvHeader->HeaderLength + sizeof (VARIABLE_STORE_HEADER);
-  while (VariableOffset  < (CfvVariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER))) {
-    VariableHeader = (AUTHENTICATED_VARIABLE_HEADER *)(VariableBase + VariableOffset);
-    if (VariableHeader->StartId != VARIABLE_DATA) {
-      if (!CheckPaddingData ((UINT8 *)VariableHeader, CfvVariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - VariableOffset)) {
-        DEBUG ((DEBUG_ERROR, "TDX CFV: Variable header was invalid\n"));
-        return FALSE;
-      }
-
-      VariableOffset = CfvVariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
-    } else {
-      if (!((VariableHeader->State == VAR_IN_DELETED_TRANSITION) ||
-            (VariableHeader->State == VAR_DELETED) ||
-            (VariableHeader->State == VAR_HEADER_VALID_ONLY) ||
-            (VariableHeader->State == VAR_ADDED)))
-      {
-        DEBUG ((DEBUG_ERROR, "TDX CFV: Variable header was invalid\n"));
-        return FALSE;
-      }
-
-      VariableOffset += sizeof (AUTHENTICATED_VARIABLE_HEADER) + VariableHeader->NameSize + VariableHeader->DataSize;
-      // Verify VariableOffset should be less than or equal CfvVariableStoreHeader->Size - sizeof(VARIABLE_STORE_HEADER)
-      if (VariableOffset > (CfvVariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER))) {
-        DEBUG ((DEBUG_ERROR, "TDX CFV: Variable header was invalid\n"));
-        return FALSE;
-      }
-
-      VariableOffsetBeforeAlign = VariableOffset;
-      // 4 byte align
-      VariableOffset = (VariableOffset  + 3) & (UINTN)(~3);
-
-      if (!CheckPaddingData ((UINT8 *)(VariableBase + VariableOffsetBeforeAlign), VariableOffset - VariableOffsetBeforeAlign)) {
-        DEBUG ((DEBUG_ERROR, "TDX CFV: Variable header was invalid\n"));
-        return FALSE;
-      }
-    }
-  }
-
-  return TRUE;
-}
-
 /**
   Measure the Hoblist passed from the VMM.
 
diff --git a/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c b/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
index fdfefd00d732..7502ec44669e 100644
--- a/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
+++ b/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
@@ -179,14 +179,6 @@ PeilessStartup (
       CpuDeadLoop ();
     }
 
-    //
-    // Validate Tdx CFV
-    //
-    if (!TdxValidateCfv (CfvBase, FixedPcdGet32 (PcdCfvRawDataSize))) {
-      ASSERT (FALSE);
-      CpuDeadLoop ();
-    }
-
     //
     // Measure Tdx CFV
     //
diff --git a/OvmfPkg/Library/PeilessStartupLib/PeilessStartupInternal.h b/OvmfPkg/Library/PeilessStartupLib/PeilessStartupInternal.h
index 74b5f46552c2..09cac3e26c67 100644
--- a/OvmfPkg/Library/PeilessStartupLib/PeilessStartupInternal.h
+++ b/OvmfPkg/Library/PeilessStartupLib/PeilessStartupInternal.h
@@ -52,23 +52,6 @@ EFIAPI
 ConstructSecHobList (
   );
 
-/**
-  Check the integrity of CFV data.
-
-  @param[in] TdxCfvBase - A pointer to CFV header
-  @param[in] TdxCfvSize - CFV data size
-
-  @retval  TRUE   - The CFV data is valid.
-  @retval  FALSE  - The CFV data is invalid.
-
-**/
-BOOLEAN
-EFIAPI
-TdxValidateCfv (
-  IN UINT8   *TdxCfvBase,
-  IN UINT32  TdxCfvSize
-  );
-
 /**
   Measure the Hoblist passed from the VMM.
 
-- 
2.29.2.windows.2


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

* [PATCH V3 2/7] OvmfPkg/PlatformInitLib: Add functions for EmuVariableNvStore
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
  2022-06-29  8:56 ` [PATCH V3 1/7] OvmfPkg/PeilessStartupLib: Delete TdxValidateCfv Min Xu
@ 2022-06-29  8:56 ` Min Xu
  2022-06-29  8:56 ` [PATCH V3 3/7] OvmfPkg/PlatformPei: Update ReserveEmuVariableNvStore Min Xu
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:56 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

There are 3 functions added for EmuVariableNvStore:
 - PlatformReserveEmuVariableNvStore
 - PlatformInitEmuVariableNvStore
 - PlatformValidateNvVarStore

PlatformReserveEmuVariableNvStore allocate storage for NV variables early
on so it will be at a consistent address.

PlatformInitEmuVariableNvStore copies the content in
PcdOvmfFlashNvStorageVariableBase to the storage allocated by
PlatformReserveEmuVariableNvStore. This is used in the case that OVMF is
launched with -bios parameter. Because in that situation UEFI variables
will be partially emulated, and non-volatile variables may lose their
contents after a reboot. This makes the secure boot feature not working.

PlatformValidateNvVarStore is renamed from TdxValidateCfv and it is used
to validate the integrity of FlashNvVarStore
(PcdOvmfFlashNvStorageVariableBase). It should be called before
PlatformInitEmuVariableNvStore is called to copy over the content.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/Include/Library/PlatformInitLib.h     |  51 ++++
 OvmfPkg/Library/PlatformInitLib/Platform.c    | 238 ++++++++++++++++++
 .../PlatformInitLib/PlatformInitLib.inf       |   3 +
 3 files changed, 292 insertions(+)

diff --git a/OvmfPkg/Include/Library/PlatformInitLib.h b/OvmfPkg/Include/Library/PlatformInitLib.h
index 2987a367cc9c..c5234bf26d45 100644
--- a/OvmfPkg/Include/Library/PlatformInitLib.h
+++ b/OvmfPkg/Include/Library/PlatformInitLib.h
@@ -234,4 +234,55 @@ PlatformTdxPublishRamRegions (
   VOID
   );
 
+/**
+  Check the integrity of NvVarStore.
+
+  @param[in] NvVarStoreBase - A pointer to NvVarStore header
+  @param[in] NvVarStoreSize - NvVarStore size
+
+  @retval  TRUE   - The NvVarStore is valid.
+  @retval  FALSE  - The NvVarStore is invalid.
+
+**/
+BOOLEAN
+EFIAPI
+PlatformValidateNvVarStore (
+  IN UINT8   *NvVarStoreBase,
+  IN UINT32  NvVarStoreSize
+  );
+
+/**
+ Allocate storage for NV variables early on so it will be
+ at a consistent address.  Since VM memory is preserved
+ across reboots, this allows the NV variable storage to survive
+ a VM reboot.
+
+ *
+ * @retval VOID* The pointer to the storage for NV Variables
+ */
+VOID *
+EFIAPI
+PlatformReserveEmuVariableNvStore (
+  VOID
+  );
+
+/**
+ When OVMF is lauched with -bios parameter, UEFI variables will be
+ partially emulated, and non-volatile variables may lose their contents
+ after a reboot. This makes the secure boot feature not working.
+
+ This function is used to initialize the EmuVariableNvStore
+ with the conent in PcdOvmfFlashNvStorageVariableBase.
+
+ @param[in] EmuVariableNvStore      - A pointer to EmuVariableNvStore
+
+ @retval  EFI_SUCCESS   - Successfully init the EmuVariableNvStore
+ @retval  Others        - As the error code indicates
+ */
+EFI_STATUS
+EFIAPI
+PlatformInitEmuVariableNvStore (
+  IN VOID  *EmuVariableNvStore
+  );
+
 #endif // PLATFORM_INIT_LIB_H_
diff --git a/OvmfPkg/Library/PlatformInitLib/Platform.c b/OvmfPkg/Library/PlatformInitLib/Platform.c
index c3d34e43af5a..2582689ffe35 100644
--- a/OvmfPkg/Library/PlatformInitLib/Platform.c
+++ b/OvmfPkg/Library/PlatformInitLib/Platform.c
@@ -25,10 +25,13 @@
 #include <IndustryStandard/Pci22.h>
 #include <IndustryStandard/Q35MchIch9.h>
 #include <IndustryStandard/QemuCpuHotplug.h>
+#include <Library/MemoryAllocationLib.h>
 #include <Library/QemuFwCfgLib.h>
 #include <Library/QemuFwCfgS3Lib.h>
 #include <Library/QemuFwCfgSimpleParserLib.h>
 #include <Library/PciLib.h>
+#include <Guid/SystemNvDataGuid.h>
+#include <Guid/VariableFormat.h>
 #include <OvmfPlatforms.h>
 
 #include <Library/PlatformInitLib.h>
@@ -576,3 +579,238 @@ PlatformMaxCpuCountInitialization (
   PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber  = MaxCpuCount;
   PlatformInfoHob->PcdCpuBootLogicalProcessorNumber = BootCpuCount;
 }
+
+/**
+  Check padding data all bit should be 1.
+
+  @param[in] Buffer     - A pointer to buffer header
+  @param[in] BufferSize - Buffer size
+
+  @retval  TRUE   - The padding data is valid.
+  @retval  TRUE  - The padding data is invalid.
+
+**/
+BOOLEAN
+CheckPaddingData (
+  IN UINT8   *Buffer,
+  IN UINT32  BufferSize
+  )
+{
+  UINT32  index;
+
+  for (index = 0; index < BufferSize; index++) {
+    if (Buffer[index] != 0xFF) {
+      return FALSE;
+    }
+  }
+
+  return TRUE;
+}
+
+/**
+  Check the integrity of NvVarStore.
+
+  @param[in] NvVarStoreBase - A pointer to NvVarStore header
+  @param[in] NvVarStoreSize - NvVarStore size
+
+  @retval  TRUE   - The NvVarStore is valid.
+  @retval  FALSE  - The NvVarStore is invalid.
+
+**/
+BOOLEAN
+EFIAPI
+PlatformValidateNvVarStore (
+  IN UINT8   *NvVarStoreBase,
+  IN UINT32  NvVarStoreSize
+  )
+{
+  UINT16                         Checksum;
+  UINTN                          VariableBase;
+  UINT32                         VariableOffset;
+  UINT32                         VariableOffsetBeforeAlign;
+  EFI_FIRMWARE_VOLUME_HEADER     *NvVarStoreFvHeader;
+  VARIABLE_STORE_HEADER          *NvVarStoreHeader;
+  AUTHENTICATED_VARIABLE_HEADER  *VariableHeader;
+
+  static EFI_GUID  FvHdrGUID       = EFI_SYSTEM_NV_DATA_FV_GUID;
+  static EFI_GUID  VarStoreHdrGUID = EFI_AUTHENTICATED_VARIABLE_GUID;
+
+  VariableOffset = 0;
+
+  if (NvVarStoreBase == NULL) {
+    DEBUG ((DEBUG_ERROR, "NvVarStore pointer is NULL.\n"));
+    return FALSE;
+  }
+
+  //
+  // Verify the header zerovetor, filesystemguid,
+  // revision, signature, attributes, fvlength, checksum
+  // HeaderLength cannot be an odd number
+  //
+  NvVarStoreFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)NvVarStoreBase;
+
+  if ((!IsZeroBuffer (NvVarStoreFvHeader->ZeroVector, 16)) ||
+      (!CompareGuid (&FvHdrGUID, &NvVarStoreFvHeader->FileSystemGuid)) ||
+      (NvVarStoreFvHeader->Signature != EFI_FVH_SIGNATURE) ||
+      (NvVarStoreFvHeader->Attributes != 0x4feff) ||
+      (NvVarStoreFvHeader->Revision != EFI_FVH_REVISION) ||
+      (NvVarStoreFvHeader->FvLength != NvVarStoreSize)
+      )
+  {
+    DEBUG ((DEBUG_ERROR, "NvVarStore FV headers were invalid.\n"));
+    return FALSE;
+  }
+
+  //
+  // Verify the header checksum
+  //
+  Checksum = CalculateSum16 ((VOID *)NvVarStoreFvHeader, NvVarStoreFvHeader->HeaderLength);
+
+  if (Checksum != 0) {
+    DEBUG ((DEBUG_ERROR, "NvVarStore FV checksum was invalid.\n"));
+    return FALSE;
+  }
+
+  //
+  // Verify the header signature, size, format, state
+  //
+  NvVarStoreHeader = (VARIABLE_STORE_HEADER *)(NvVarStoreBase + NvVarStoreFvHeader->HeaderLength);
+  if ((!CompareGuid (&VarStoreHdrGUID, &NvVarStoreHeader->Signature)) ||
+      (NvVarStoreHeader->Format != VARIABLE_STORE_FORMATTED) ||
+      (NvVarStoreHeader->State != VARIABLE_STORE_HEALTHY) ||
+      (NvVarStoreHeader->Size > (NvVarStoreFvHeader->FvLength - NvVarStoreFvHeader->HeaderLength)) ||
+      (NvVarStoreHeader->Size < sizeof (VARIABLE_STORE_HEADER))
+      )
+  {
+    DEBUG ((DEBUG_ERROR, "NvVarStore header signature/size/format/state were invalid.\n"));
+    return FALSE;
+  }
+
+  //
+  // Verify the header startId, state
+  // Verify data to the end
+  //
+  VariableBase = (UINTN)NvVarStoreBase + NvVarStoreFvHeader->HeaderLength + sizeof (VARIABLE_STORE_HEADER);
+  while (VariableOffset  < (NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER))) {
+    VariableHeader = (AUTHENTICATED_VARIABLE_HEADER *)(VariableBase + VariableOffset);
+    if (VariableHeader->StartId != VARIABLE_DATA) {
+      if (!CheckPaddingData ((UINT8 *)VariableHeader, NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - VariableOffset)) {
+        DEBUG ((DEBUG_ERROR, "NvVarStore variable header StartId was invalid.\n"));
+        return FALSE;
+      }
+
+      VariableOffset = NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
+    } else {
+      if (!((VariableHeader->State == VAR_IN_DELETED_TRANSITION) ||
+            (VariableHeader->State == VAR_DELETED) ||
+            (VariableHeader->State == VAR_HEADER_VALID_ONLY) ||
+            (VariableHeader->State == VAR_ADDED)))
+      {
+        DEBUG ((DEBUG_ERROR, "NvVarStore Variable header State was invalid.\n"));
+        return FALSE;
+      }
+
+      VariableOffset += sizeof (AUTHENTICATED_VARIABLE_HEADER) + VariableHeader->NameSize + VariableHeader->DataSize;
+      // Verify VariableOffset should be less than or equal NvVarStoreHeader->Size - sizeof(VARIABLE_STORE_HEADER)
+      if (VariableOffset > (NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER))) {
+        DEBUG ((DEBUG_ERROR, "NvVarStore Variable header VariableOffset was invalid.\n"));
+        return FALSE;
+      }
+
+      VariableOffsetBeforeAlign = VariableOffset;
+      // 4 byte align
+      VariableOffset = (VariableOffset  + 3) & (UINTN)(~3);
+
+      if (!CheckPaddingData ((UINT8 *)(VariableBase + VariableOffsetBeforeAlign), VariableOffset - VariableOffsetBeforeAlign)) {
+        DEBUG ((DEBUG_ERROR, "NvVarStore Variable header PaddingData was invalid.\n"));
+        return FALSE;
+      }
+    }
+  }
+
+  return TRUE;
+}
+
+/**
+ Allocate storage for NV variables early on so it will be
+ at a consistent address.  Since VM memory is preserved
+ across reboots, this allows the NV variable storage to survive
+ a VM reboot.
+
+ *
+ * @retval VOID* The pointer to the storage for NV Variables
+ */
+VOID *
+EFIAPI
+PlatformReserveEmuVariableNvStore (
+  VOID
+  )
+{
+  VOID    *VariableStore;
+  UINT32  VarStoreSize;
+
+  VarStoreSize = 2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  //
+  // Allocate storage for NV variables early on so it will be
+  // at a consistent address.  Since VM memory is preserved
+  // across reboots, this allows the NV variable storage to survive
+  // a VM reboot.
+  //
+  VariableStore =
+    AllocateRuntimePages (
+      EFI_SIZE_TO_PAGES (VarStoreSize)
+      );
+  DEBUG ((
+    DEBUG_INFO,
+    "Reserved variable store memory: 0x%p; size: %dkb\n",
+    VariableStore,
+    VarStoreSize / 1024
+    ));
+
+  return VariableStore;
+}
+
+/**
+ When OVMF is lauched with -bios parameter, UEFI variables will be
+ partially emulated, and non-volatile variables may lose their contents
+ after a reboot. This makes the secure boot feature not working.
+
+ This function is used to initialize the EmuVariableNvStore
+ with the conent in PcdOvmfFlashNvStorageVariableBase.
+
+ @param[in] EmuVariableNvStore      - A pointer to EmuVariableNvStore
+
+ @retval  EFI_SUCCESS   - Successfully init the EmuVariableNvStore
+ @retval  Others        - As the error code indicates
+ */
+EFI_STATUS
+EFIAPI
+PlatformInitEmuVariableNvStore (
+  IN VOID  *EmuVariableNvStore
+  )
+{
+  UINT8   *Base;
+  UINT32  Size;
+  UINT32  EmuVariableNvStoreSize;
+
+  EmuVariableNvStoreSize = 2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  if ((EmuVariableNvStore == NULL) || (EmuVariableNvStoreSize == 0)) {
+    DEBUG ((DEBUG_ERROR, "Invalid EmuVariableNvStore parameter.\n"));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Base = (UINT8 *)(UINTN)PcdGet32 (PcdOvmfFlashNvStorageVariableBase);
+  Size = (UINT32)PcdGet32 (PcdFlashNvStorageVariableSize);
+  ASSERT (Size < EmuVariableNvStoreSize);
+
+  if (!PlatformValidateNvVarStore (Base, PcdGet32 (PcdCfvRawDataSize))) {
+    ASSERT (FALSE);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  DEBUG ((DEBUG_INFO, "Init EmuVariableNvStore with the content in FlashNvStorage\n"));
+
+  CopyMem (EmuVariableNvStore, Base, Size);
+
+  return EFI_SUCCESS;
+}
diff --git a/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf b/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf
index d2fa2d998df8..86a82ad3e084 100644
--- a/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf
+++ b/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf
@@ -47,6 +47,7 @@
   HobLib
   QemuFwCfgLib
   QemuFwCfgSimpleParserLib
+  MemoryAllocationLib
   MtrrLib
   PcdLib
   PciLib
@@ -96,6 +97,8 @@
   gUefiOvmfPkgTokenSpaceGuid.PcdGuidedExtractHandlerTableSize
 
   gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashNvStorageVariableBase
+  gUefiOvmfPkgTokenSpaceGuid.PcdCfvRawDataSize
 
 [FeaturePcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode
-- 
2.29.2.windows.2


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

* [PATCH V3 3/7] OvmfPkg/PlatformPei: Update ReserveEmuVariableNvStore
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
  2022-06-29  8:56 ` [PATCH V3 1/7] OvmfPkg/PeilessStartupLib: Delete TdxValidateCfv Min Xu
  2022-06-29  8:56 ` [PATCH V3 2/7] OvmfPkg/PlatformInitLib: Add functions for EmuVariableNvStore Min Xu
@ 2022-06-29  8:56 ` Min Xu
  2022-06-29  8:57 ` [PATCH V3 4/7] OvmfPkg: Reserve and init EmuVariableNvStore in Pei-less Startup Min Xu
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:56 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

ReserveEmuVariableNvStore is updated with below 2 functions defined in
PlatformInitLib:
 - PlatformReserveEmuVariableNvStore
 - PlatformInitEmuVariableNvStore

PlatformInitEmuVariableNvStore works when secure boot feature is enabled.
This is because secure boot needs the EFI variables (PK/KEK/DB/DBX, etc)
and EmuVariableNvStore is cleared when OVMF is launched with -bios
parameter.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/PlatformPei/Platform.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)

diff --git a/OvmfPkg/PlatformPei/Platform.c b/OvmfPkg/PlatformPei/Platform.c
index 009db67ee60a..b1f8140d6041 100644
--- a/OvmfPkg/PlatformPei/Platform.c
+++ b/OvmfPkg/PlatformPei/Platform.c
@@ -220,24 +220,13 @@ ReserveEmuVariableNvStore (
   EFI_PHYSICAL_ADDRESS  VariableStore;
   RETURN_STATUS         PcdStatus;
 
-  //
-  // Allocate storage for NV variables early on so it will be
-  // at a consistent address.  Since VM memory is preserved
-  // across reboots, this allows the NV variable storage to survive
-  // a VM reboot.
-  //
-  VariableStore =
-    (EFI_PHYSICAL_ADDRESS)(UINTN)
-    AllocateRuntimePages (
-      EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))
-      );
-  DEBUG ((
-    DEBUG_INFO,
-    "Reserved variable store memory: 0x%lX; size: %dkb\n",
-    VariableStore,
-    (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024
-    ));
-  PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
+  VariableStore = (EFI_PHYSICAL_ADDRESS)(UINTN)PlatformReserveEmuVariableNvStore ();
+  PcdStatus     = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
+
+ #ifdef SECURE_BOOT_FEATURE_ENABLED
+  PlatformInitEmuVariableNvStore ((VOID *)(UINTN)VariableStore);
+ #endif
+
   ASSERT_RETURN_ERROR (PcdStatus);
 }
 
-- 
2.29.2.windows.2


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

* [PATCH V3 4/7] OvmfPkg: Reserve and init EmuVariableNvStore in Pei-less Startup
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
                   ` (2 preceding siblings ...)
  2022-06-29  8:56 ` [PATCH V3 3/7] OvmfPkg/PlatformPei: Update ReserveEmuVariableNvStore Min Xu
@ 2022-06-29  8:57 ` Min Xu
  2022-06-29  8:57 ` [PATCH V3 5/7] OvmfPkg/NvVarsFileLib: Shortcut ConnectNvVarsToFileSystem in secure-boot Min Xu
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:57 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

EmuVariableNvStore is reserved and init with below 2 functions defined in
PlatformInitLib:
 - PlatformReserveEmuVariableNvStore
 - PlatformInitEmuVariableNvStore

PlatformInitEmuVariableNvStore works when secure boot feature is enabled.
This is because secure boot needs the EFI variables (PK/KEK/DB/DBX, etc)
and EmuVariableNvStore is cleared when OVMF is launched with -bios
parameter.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c b/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
index 7502ec44669e..380e71597206 100644
--- a/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
+++ b/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
@@ -42,6 +42,7 @@ InitializePlatform (
   )
 {
   UINT32  LowerMemorySize;
+  VOID    *VariableStore;
 
   DEBUG ((DEBUG_INFO, "InitializePlatform in Pei-less boot\n"));
   PlatformDebugDumpCmos ();
@@ -79,6 +80,12 @@ InitializePlatform (
     LowerMemorySize
     ));
 
+  VariableStore                                  = PlatformReserveEmuVariableNvStore ();
+  PlatformInfoHob->PcdEmuVariableNvStoreReserved = (UINT64)(UINTN)VariableStore;
+ #ifdef SECURE_BOOT_FEATURE_ENABLED
+  PlatformInitEmuVariableNvStore (VariableStore);
+ #endif
+
   if (TdIsEnabled ()) {
     PlatformTdxPublishRamRegions ();
   } else {
-- 
2.29.2.windows.2


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

* [PATCH V3 5/7] OvmfPkg/NvVarsFileLib: Shortcut ConnectNvVarsToFileSystem in secure-boot
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
                   ` (3 preceding siblings ...)
  2022-06-29  8:57 ` [PATCH V3 4/7] OvmfPkg: Reserve and init EmuVariableNvStore in Pei-less Startup Min Xu
@ 2022-06-29  8:57 ` Min Xu
  2022-06-29  8:57 ` [PATCH V3 6/7] OvmfPkg/TdxDxe: Set PcdEmuVariableNvStoreReserved Min Xu
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:57 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

OvmfPkg/Library/NvVarsFileLib allows loading variables into emulated
varstore from a on-disk NvVars file.  We can't allow that when secure
boot is active.  So check secure-boot feature and shortcut the
ConnectNvVarsToFileSystem() function when sb is enabled.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Suggested-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.c b/OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.c
index 21b71524ea48..72289da35819 100644
--- a/OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.c
+++ b/OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.c
@@ -28,6 +28,12 @@ ConnectNvVarsToFileSystem (
   IN EFI_HANDLE  FsHandle
   )
 {
+ #ifdef SECURE_BOOT_FEATURE_ENABLED
+
+  return EFI_UNSUPPORTED;
+
+ #else
+
   EFI_STATUS  Status;
 
   //
@@ -46,6 +52,7 @@ ConnectNvVarsToFileSystem (
   }
 
   return Status;
+ #endif
 }
 
 /**
-- 
2.29.2.windows.2


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

* [PATCH V3 6/7] OvmfPkg/TdxDxe: Set PcdEmuVariableNvStoreReserved
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
                   ` (4 preceding siblings ...)
  2022-06-29  8:57 ` [PATCH V3 5/7] OvmfPkg/NvVarsFileLib: Shortcut ConnectNvVarsToFileSystem in secure-boot Min Xu
@ 2022-06-29  8:57 ` Min Xu
  2022-06-29  8:57 ` [PATCH V3 7/7] OvmfPkg: Add build-flag SECURE_BOOT_FEATURE_ENABLED Min Xu
  2022-06-30 13:28 ` [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Gerd Hoffmann
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:57 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

Set PcdEmuVariableNvStoreReserved with the value in PlatformInfoHob. It
is the address of the EmuVariableNvStore reserved in Pei-less startup.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/TdxDxe/TdxDxe.c   | 2 ++
 OvmfPkg/TdxDxe/TdxDxe.inf | 1 +
 2 files changed, 3 insertions(+)

diff --git a/OvmfPkg/TdxDxe/TdxDxe.c b/OvmfPkg/TdxDxe/TdxDxe.c
index 2318db989792..837f1f8e3024 100644
--- a/OvmfPkg/TdxDxe/TdxDxe.c
+++ b/OvmfPkg/TdxDxe/TdxDxe.c
@@ -64,6 +64,8 @@ SetPcdSettings (
     PlatformInfoHob->PcdCpuBootLogicalProcessorNumber
     ));
 
+  PcdSet64S (PcdEmuVariableNvStoreReserved, PlatformInfoHob->PcdEmuVariableNvStoreReserved);
+
   if (TdIsEnabled ()) {
     PcdStatus = PcdSet64S (PcdTdxSharedBitMask, TdSharedPageMask ());
     ASSERT_RETURN_ERROR (PcdStatus);
diff --git a/OvmfPkg/TdxDxe/TdxDxe.inf b/OvmfPkg/TdxDxe/TdxDxe.inf
index a7e0abda1522..3ce8a5c32c98 100644
--- a/OvmfPkg/TdxDxe/TdxDxe.inf
+++ b/OvmfPkg/TdxDxe/TdxDxe.inf
@@ -68,3 +68,4 @@
   gEfiMdePkgTokenSpaceGuid.PcdConfidentialComputingGuestAttr
   gEfiMdeModulePkgTokenSpaceGuid.PcdTdxSharedBitMask
   gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack
+  gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved
-- 
2.29.2.windows.2


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

* [PATCH V3 7/7] OvmfPkg: Add build-flag SECURE_BOOT_FEATURE_ENABLED
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
                   ` (5 preceding siblings ...)
  2022-06-29  8:57 ` [PATCH V3 6/7] OvmfPkg/TdxDxe: Set PcdEmuVariableNvStoreReserved Min Xu
@ 2022-06-29  8:57 ` Min Xu
  2022-06-30 13:28 ` [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Gerd Hoffmann
  7 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-29  8:57 UTC (permalink / raw)
  To: devel
  Cc: Min M Xu, Erdem Aktas, James Bottomley, Jiewen Yao, Tom Lendacky,
	Gerd Hoffmann

From: Min M Xu <min.m.xu@intel.com>

SECURE_BOOT_FEATURE_ENABLED is the build-flag defined when secure boot
is enabled. Currently this flag is used in below lib:
 - OvmfPkg/PlatformPei
 - PeilessStartupLib

So it is defined in below 5 .dsc
 - OvmfPkg/CloudHv/CloudHvX64.dsc
 - OvmfPkg/IntelTdx/IntelTdxX64.dsc
 - OvmfPkg/OvmfPkgIa32.dsc
 - OvmfPkg/OvmfPkgIa32X64.dsc
 - OvmfPkg/OvmfPkgX64.dsc

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/CloudHv/CloudHvX64.dsc   | 9 +++++++++
 OvmfPkg/IntelTdx/IntelTdxX64.dsc | 9 +++++++++
 OvmfPkg/OvmfPkgIa32.dsc          | 9 +++++++++
 OvmfPkg/OvmfPkgIa32X64.dsc       | 9 +++++++++
 OvmfPkg/OvmfPkgX64.dsc           | 9 +++++++++
 5 files changed, 45 insertions(+)

diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc
index ca601aa09d3a..2712731caf55 100644
--- a/OvmfPkg/CloudHv/CloudHvX64.dsc
+++ b/OvmfPkg/CloudHv/CloudHvX64.dsc
@@ -93,6 +93,15 @@
   INTEL:*_*_*_CC_FLAGS = /D DISABLE_NEW_DEPRECATED_INTERFACES
   GCC:*_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES
 
+  #
+  # SECURE_BOOT_FEATURE_ENABLED
+  #
+!if $(SECURE_BOOT_ENABLE) == TRUE
+  MSFT:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  INTEL:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  GCC:*_*_*_CC_FLAGS = -D SECURE_BOOT_FEATURE_ENABLED
+!endif
+
 !include NetworkPkg/NetworkBuildOptions.dsc.inc
 
 [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]
diff --git a/OvmfPkg/IntelTdx/IntelTdxX64.dsc b/OvmfPkg/IntelTdx/IntelTdxX64.dsc
index c662ae8720ff..f4f495a9d199 100644
--- a/OvmfPkg/IntelTdx/IntelTdxX64.dsc
+++ b/OvmfPkg/IntelTdx/IntelTdxX64.dsc
@@ -85,6 +85,15 @@
   INTEL:*_*_*_CC_FLAGS = /D TDX_PEI_LESS_BOOT
   GCC:*_*_*_CC_FLAGS = -D TDX_PEI_LESS_BOOT
 
+  #
+  # SECURE_BOOT_FEATURE_ENABLED
+  #
+!if $(SECURE_BOOT_ENABLE) == TRUE
+  MSFT:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  INTEL:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  GCC:*_*_*_CC_FLAGS = -D SECURE_BOOT_FEATURE_ENABLED
+!endif
+
 [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]
   GCC:*_*_*_DLINK_FLAGS = -z common-page-size=0x1000
   XCODE:*_*_*_DLINK_FLAGS = -seg1addr 0x1000 -segalign 0x1000
diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index 934edbbd2a7b..3126e695b7dd 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -89,6 +89,15 @@
   INTEL:*_*_*_CC_FLAGS = /D DISABLE_NEW_DEPRECATED_INTERFACES
   GCC:*_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES
 
+  #
+  # SECURE_BOOT_FEATURE_ENABLED
+  #
+!if $(SECURE_BOOT_ENABLE) == TRUE
+  MSFT:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  INTEL:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  GCC:*_*_*_CC_FLAGS = -D SECURE_BOOT_FEATURE_ENABLED
+!endif
+
 !include NetworkPkg/NetworkBuildOptions.dsc.inc
 
 [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index 4f432c294958..0c86e0b4882d 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -93,6 +93,15 @@
   INTEL:*_*_*_CC_FLAGS = /D DISABLE_NEW_DEPRECATED_INTERFACES
   GCC:*_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES
 
+  #
+  # SECURE_BOOT_FEATURE_ENABLED
+  #
+!if $(SECURE_BOOT_ENABLE) == TRUE
+  MSFT:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  INTEL:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  GCC:*_*_*_CC_FLAGS = -D SECURE_BOOT_FEATURE_ENABLED
+!endif
+
 !include NetworkPkg/NetworkBuildOptions.dsc.inc
 
 [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index b22da97d4f77..a36bcef4fd3c 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -106,6 +106,15 @@
   INTEL:*_*_*_CC_FLAGS = /D TDX_GUEST_SUPPORTED
   GCC:*_*_*_CC_FLAGS = -D TDX_GUEST_SUPPORTED
 
+  #
+  # SECURE_BOOT_FEATURE_ENABLED
+  #
+!if $(SECURE_BOOT_ENABLE) == TRUE
+  MSFT:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  INTEL:*_*_*_CC_FLAGS = /D SECURE_BOOT_FEATURE_ENABLED
+  GCC:*_*_*_CC_FLAGS = -D SECURE_BOOT_FEATURE_ENABLED
+!endif
+
 !include NetworkPkg/NetworkBuildOptions.dsc.inc
 
 [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]
-- 
2.29.2.windows.2


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

* Re: [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter
  2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
                   ` (6 preceding siblings ...)
  2022-06-29  8:57 ` [PATCH V3 7/7] OvmfPkg: Add build-flag SECURE_BOOT_FEATURE_ENABLED Min Xu
@ 2022-06-30 13:28 ` Gerd Hoffmann
  2022-06-30 22:56   ` Min Xu
  7 siblings, 1 reply; 10+ messages in thread
From: Gerd Hoffmann @ 2022-06-30 13:28 UTC (permalink / raw)
  To: Min Xu
  Cc: devel, Leif Lindholm, Ard Biesheuvel, Abner Chang,
	Daniel Schaefer, Erdem Aktas, James Bottomley, Jiewen Yao,
	Tom Lendacky

On Wed, Jun 29, 2022 at 04:56:56PM +0800, Min Xu wrote:
> Secure-Boot related variables include the PK/KEK/DB/DBX and they are
> stored in NvVarStore (OVMF_VARS.fd). When lauching with -pflash,
> QEMU/OVMF will use emulated flash, and fully support UEFI variables.
> But when launching with -bios parameter, UEFI variables will be partially
> emulated, and non-volatile variables may lose their contents after a
> reboot. See OvmfPkg/README.
> 
> Tdx guest is an example that -pflash is not supported. So this patch-set
> is designed to initialize the NvVarStore with the content of in
> OVMF_VARS.fd.
> 
> patch 1:
>  Add a new function (AllocateRuntimePages) in PrePiMemoryAllocationLib.
>  This function will be used in PeilessStartupLib which will run
>  in SEC phase.
> 
> patch 2:
>  Delete the TdxValidateCfv in PeilessStartupLib. Because it is going to
>  be renamed to PlatformValidateNvVarStore and be moved to PlatformInitLib.
> 
> patch 3 - 7:
>  Then we add functions for EmuVariableNvStore in PlatformInitLib. This
>  lib will then be called in OvmfPkg/PlatformPei and PeilessStartupLib.
>  We also shortcut ConnectNvVarsToFileSystem in secure-boot.
> 
> patch 8:
>  At last a build-flag (SECURE_BOOT_FEATURE_ENABLED) is introduced in
>  the dsc in OvmfPkg. Because the copy over of OVMR_VARS.fd to
>  EmuVariableNvStore is only required when secure-boot is enabled.
> 
> Code: https://github.com/mxu9/edk2/tree/secure-boot.v3

/usr/bin/ld: /home/kraxel/projects/edk2/Build/IntelTdx/DEBUG_GCC5/X64/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib/OUTPUT/SecPeiCpuExceptionHandlerLib.lib(ExceptionHandlerAsm.obj): warning: relocation in read-only section `.text'
/usr/bin/ld: /tmp/ccCEPSuH.ltrans0.ltrans.o: in function `SecCoreStartupWithStack':
/home/kraxel/projects/edk2/OvmfPkg/Library/PlatformInitLib/Platform.c:760: undefined reference to `AllocateRuntimePages'
/usr/bin/ld: /tmp/ccCEPSuH.ltrans0.ltrans.o:/home/kraxel/projects/edk2/OvmfPkg/Library/PlatformInitLib/Platform.c:760: undefined reference to `AllocateRuntimePages'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make: *** [GNUmakefile:431: /home/kraxel/projects/edk2/Build/IntelTdx/DEBUG_GCC5/X64/OvmfPkg/IntelTdx/Sec/SecMain/DEBUG/SecMain.dll] Error 1

adding a build test for intel tdx to ci is probably a good idea ...

take care,
  Gerd


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

* Re: [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter
  2022-06-30 13:28 ` [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Gerd Hoffmann
@ 2022-06-30 22:56   ` Min Xu
  0 siblings, 0 replies; 10+ messages in thread
From: Min Xu @ 2022-06-30 22:56 UTC (permalink / raw)
  To: Gerd Hoffmann
  Cc: devel@edk2.groups.io, Leif Lindholm, Ard Biesheuvel, Chang, Abner,
	Schaefer, Daniel, Aktas, Erdem, James Bottomley, Yao, Jiewen,
	Tom Lendacky

On June 30, 2022 9:28 PM, Gerd Hoffmann wrote:
> On Wed, Jun 29, 2022 at 04:56:56PM +0800, Min Xu wrote:
> > Secure-Boot related variables include the PK/KEK/DB/DBX and they are
> > stored in NvVarStore (OVMF_VARS.fd). When lauching with -pflash,
> > QEMU/OVMF will use emulated flash, and fully support UEFI variables.
> > But when launching with -bios parameter, UEFI variables will be
> > partially emulated, and non-volatile variables may lose their contents
> > after a reboot. See OvmfPkg/README.
> >
> > Tdx guest is an example that -pflash is not supported. So this
> > patch-set is designed to initialize the NvVarStore with the content of
> > in OVMF_VARS.fd.
> >
> > patch 1:
> >  Add a new function (AllocateRuntimePages) in PrePiMemoryAllocationLib.
> >  This function will be used in PeilessStartupLib which will run  in
> > SEC phase.
> >
> > patch 2:
> >  Delete the TdxValidateCfv in PeilessStartupLib. Because it is going
> > to  be renamed to PlatformValidateNvVarStore and be moved to
> PlatformInitLib.
> >
> > patch 3 - 7:
> >  Then we add functions for EmuVariableNvStore in PlatformInitLib. This
> > lib will then be called in OvmfPkg/PlatformPei and PeilessStartupLib.
> >  We also shortcut ConnectNvVarsToFileSystem in secure-boot.
> >
> > patch 8:
> >  At last a build-flag (SECURE_BOOT_FEATURE_ENABLED) is introduced in
> > the dsc in OvmfPkg. Because the copy over of OVMR_VARS.fd to
> > EmuVariableNvStore is only required when secure-boot is enabled.
> >
> > Code: https://github.com/mxu9/edk2/tree/secure-boot.v3
> 
> /usr/bin/ld:
> /home/kraxel/projects/edk2/Build/IntelTdx/DEBUG_GCC5/X64/UefiCpuPkg/
> Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib/OUTPUT/S
> ecPeiCpuExceptionHandlerLib.lib(ExceptionHandlerAsm.obj): warning:
> relocation in read-only section `.text'
> /usr/bin/ld: /tmp/ccCEPSuH.ltrans0.ltrans.o: in function
> `SecCoreStartupWithStack':
> /home/kraxel/projects/edk2/OvmfPkg/Library/PlatformInitLib/Platform.c:76
> 0: undefined reference to `AllocateRuntimePages'
> /usr/bin/ld:
> /tmp/ccCEPSuH.ltrans0.ltrans.o:/home/kraxel/projects/edk2/OvmfPkg/Libra
> ry/PlatformInitLib/Platform.c:760: undefined reference to
> `AllocateRuntimePages'
> /usr/bin/ld: warning: creating DT_TEXTREL in a PIE
> collect2: error: ld returned 1 exit status
> make: *** [GNUmakefile:431:
> /home/kraxel/projects/edk2/Build/IntelTdx/DEBUG_GCC5/X64/OvmfPkg/Int
> elTdx/Sec/SecMain/DEBUG/SecMain.dll] Error 1
> 
Ah  my bad. I forgot sending below patch in the series.
fec80e84db 2022-06-22 EmbeddedPkg: Add AllocateRuntimePages in PrePiMemoryAllocationLib [Min M Xu]
It is in https://github.com/mxu9/edk2/tree/secure-boot.v3
I will send a new version with this patch soon.

> adding a build test for intel tdx to ci is probably a good idea ...
I have submit  such build test and it has been acked-by. But it hasn't been merged. I will ask Jiewen to merge it.
https://edk2.groups.io/g/devel/message/89803

Thanks
Min

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

end of thread, other threads:[~2022-06-30 22:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-29  8:56 [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Min Xu
2022-06-29  8:56 ` [PATCH V3 1/7] OvmfPkg/PeilessStartupLib: Delete TdxValidateCfv Min Xu
2022-06-29  8:56 ` [PATCH V3 2/7] OvmfPkg/PlatformInitLib: Add functions for EmuVariableNvStore Min Xu
2022-06-29  8:56 ` [PATCH V3 3/7] OvmfPkg/PlatformPei: Update ReserveEmuVariableNvStore Min Xu
2022-06-29  8:57 ` [PATCH V3 4/7] OvmfPkg: Reserve and init EmuVariableNvStore in Pei-less Startup Min Xu
2022-06-29  8:57 ` [PATCH V3 5/7] OvmfPkg/NvVarsFileLib: Shortcut ConnectNvVarsToFileSystem in secure-boot Min Xu
2022-06-29  8:57 ` [PATCH V3 6/7] OvmfPkg/TdxDxe: Set PcdEmuVariableNvStoreReserved Min Xu
2022-06-29  8:57 ` [PATCH V3 7/7] OvmfPkg: Add build-flag SECURE_BOOT_FEATURE_ENABLED Min Xu
2022-06-30 13:28 ` [PATCH V3 0/7] Enable secure-boot when lauch OVMF with -bios parameter Gerd Hoffmann
2022-06-30 22:56   ` Min Xu

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