public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Jordan Justen <jordan.l.justen@intel.com>
To: edk2-devel@lists.01.org
Cc: Jordan Justen <jordan.l.justen@intel.com>,
	Laszlo Ersek <lersek@redhat.com>
Subject: [PATCH 10/12] OvmfPkg PlatformPei: Initialize memory based variable store buffer
Date: Mon, 27 Mar 2017 01:05:42 -0700	[thread overview]
Message-ID: <20170327080544.24748-11-jordan.l.justen@intel.com> (raw)
In-Reply-To: <20170327080544.24748-1-jordan.l.justen@intel.com>

This is similar to the initialization in
OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.c.

Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
---
 OvmfPkg/PlatformPei/PlatformPei.inf |   1 +
 OvmfPkg/PlatformPei/Vars.c          | 161 ++++++++++++++++++++++++++++++++++++
 2 files changed, 162 insertions(+)

diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf
index 0eaf27e553..7a7f5c23b0 100644
--- a/OvmfPkg/PlatformPei/PlatformPei.inf
+++ b/OvmfPkg/PlatformPei/PlatformPei.inf
@@ -50,6 +50,7 @@
 
 [LibraryClasses]
   BaseLib
+  BaseMemoryLib
   DebugLib
   HobLib
   IoLib
diff --git a/OvmfPkg/PlatformPei/Vars.c b/OvmfPkg/PlatformPei/Vars.c
index 563f847a55..452b06e399 100644
--- a/OvmfPkg/PlatformPei/Vars.c
+++ b/OvmfPkg/PlatformPei/Vars.c
@@ -15,18 +15,172 @@
 **/
 
 #include <PiPei.h>
+#include <Guid/VariableFormat.h>
+#include <Guid/SystemNvDataGuid.h>
 #include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/PcdLib.h>
 
 #include "Platform.h"
 
+typedef struct {
+
+  EFI_FIRMWARE_VOLUME_HEADER FvHdr;
+  EFI_FV_BLOCK_MAP_ENTRY     EndBlockMap;
+  VARIABLE_STORE_HEADER      VarHdr;
+
+} FVB_FV_HDR_AND_VARS_TEMPLATE;
+
 #define OVMF_FVB_BLOCK_SIZE (FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))
 #define OVMF_FVB_SIZE (2 * FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))
 #define OVMF_FV_HEADER_LENGTH OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)
 
 
+/**
+  Check the integrity of firmware volume header.
+
+  @param[in] FwVolHeader - A pointer to a firmware volume header
+
+  @retval  EFI_SUCCESS   - The firmware volume is consistent
+  @retval  EFI_NOT_FOUND - The firmware volume has been corrupted.
+
+**/
+STATIC EFI_STATUS
+ValidateFvHeader (
+  IN EFI_FIRMWARE_VOLUME_HEADER   *FwVolHeader
+  )
+{
+  UINT16  Checksum;
+
+  //
+  // Verify the header revision, header signature, length
+  // Length of FvBlock cannot be 2**64-1
+  // HeaderLength cannot be an odd number
+  //
+  if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||
+      (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
+      (FwVolHeader->FvLength != OVMF_FVB_SIZE) ||
+      (FwVolHeader->HeaderLength != OVMF_FV_HEADER_LENGTH)
+      ) {
+    DEBUG ((EFI_D_INFO, "EMU Variable FVB: Basic FV headers were invalid\n"));
+    return EFI_NOT_FOUND;
+  }
+  //
+  // Verify the header checksum
+  //
+  Checksum = CalculateSum16((VOID*) FwVolHeader, FwVolHeader->HeaderLength);
+
+  if (Checksum != 0) {
+    DEBUG ((EFI_D_INFO, "EMU Variable FVB: FV checksum was invalid\n"));
+    return EFI_NOT_FOUND;
+  }
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Initializes the FV Header and Variable Store Header
+  to support variable operations.
+
+  @param[in]  Ptr - Location to initialize the headers
+
+**/
+STATIC VOID
+InitializeFvAndVariableStoreHeaders (
+  IN  VOID   *Ptr
+  )
+{
+  //
+  // Templates for authenticated variable FV header
+  //
+  STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndAuthenticatedVarTemplate = {
+    { // EFI_FIRMWARE_VOLUME_HEADER FvHdr;
+      // UINT8                     ZeroVector[16];
+      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+
+      // EFI_GUID                  FileSystemGuid;
+      EFI_SYSTEM_NV_DATA_FV_GUID,
+
+      // UINT64                    FvLength;
+      OVMF_FVB_SIZE,
+
+      // UINT32                    Signature;
+      EFI_FVH_SIGNATURE,
+
+      // EFI_FVB_ATTRIBUTES_2      Attributes;
+      0x4feff,
+
+      // UINT16                    HeaderLength;
+      OVMF_FV_HEADER_LENGTH,
+
+      // UINT16                    Checksum;
+      0,
+
+      // UINT16                    ExtHeaderOffset;
+      0,
+
+      // UINT8                     Reserved[1];
+      {0},
+
+      // UINT8                     Revision;
+      EFI_FVH_REVISION,
+
+      // EFI_FV_BLOCK_MAP_ENTRY    BlockMap[1];
+      {
+        {
+          2, // UINT32 NumBlocks;
+          OVMF_FVB_BLOCK_SIZE  // UINT32 Length;
+        }
+      }
+    },
+    // EFI_FV_BLOCK_MAP_ENTRY     EndBlockMap;
+    { 0, 0 }, // End of block map
+    { // VARIABLE_STORE_HEADER      VarHdr;
+        // EFI_GUID  Signature;     // need authenticated variables for secure boot
+        EFI_AUTHENTICATED_VARIABLE_GUID,
+
+      // UINT32  Size;
+      (
+        FixedPcdGet32 (PcdVariableStoreSize) -
+        OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)
+      ),
+
+      // UINT8   Format;
+      VARIABLE_STORE_FORMATTED,
+
+      // UINT8   State;
+      VARIABLE_STORE_HEALTHY,
+
+      // UINT16  Reserved;
+      0,
+
+      // UINT32  Reserved1;
+      0
+    }
+  };
+
+  EFI_FIRMWARE_VOLUME_HEADER  *Fv;
+
+  //
+  // Copy the template structure into the location
+  //
+  CopyMem (
+    Ptr,
+    (VOID*) &FvAndAuthenticatedVarTemplate,
+    sizeof (FvAndAuthenticatedVarTemplate)
+    );
+
+  //
+  // Update the checksum for the FV header
+  //
+  Fv = (EFI_FIRMWARE_VOLUME_HEADER*) Ptr;
+  Fv->Checksum = CalculateCheckSum16 (Ptr, Fv->HeaderLength);
+}
+
+
 VOID
 ReserveEmuVariableNvStore (
   )
@@ -57,6 +211,13 @@ ReserveEmuVariableNvStore (
   PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
   ASSERT_RETURN_ERROR (PcdStatus);
 
+  VOID *Ptr = (VOID*)(UINTN) VariableStore;
+
+  if (EFI_ERROR (ValidateFvHeader (Ptr))) {
+    SetMem (Ptr, OVMF_FVB_SIZE, 0xff);
+    InitializeFvAndVariableStoreHeaders (Ptr);
+  }
+
   //
   // Initialize the main FV header and variable store header
   //
-- 
2.11.0



  parent reply	other threads:[~2017-03-27  8:08 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-27  8:05 [PATCH 00/12] OvmfPkg: Enable variable access in PEI Jordan Justen
2017-03-27  8:05 ` [PATCH 01/12] OvmfPkg/build.sh: Add support for --disable-flash switch Jordan Justen
2017-03-27  8:05 ` [PATCH 02/12] OvmfPkg: resolve PcdLib for all PEIMs individually Jordan Justen
2017-03-27  8:05 ` [PATCH 03/12] OvmfPkg: resolve PcdLib for PEIMs to PeiPcdLib by default Jordan Justen
2017-03-27  8:05 ` [PATCH 04/12] OvmfPkg QemuFlash: Make QemuFlash.* Base class safe Jordan Justen
2017-03-27  8:05 ` [PATCH 05/12] OvmfPkg QemuFlash: Make QemuFlashDetected external Jordan Justen
2017-03-27  8:05 ` [PATCH 06/12] OvmfPkg QemuFlash: Add DetectFlashBaseLib.inf 'NULL' library Jordan Justen
2017-03-27  8:05 ` [PATCH 07/12] OvmfPkg PlatformPei: Detect and set PcdOvmfFlashVariablesEnable Jordan Justen
2017-03-27  8:05 ` [PATCH 08/12] OvmfPkg/EmuVariableFvbRuntimeDxe: Use PcdOvmfFlashVariablesEnable Jordan Justen
2017-03-27  8:05 ` [PATCH 09/12] OvmfPkg PlatformPei: Set flash variable PCDs Jordan Justen
2017-03-27  8:05 ` Jordan Justen [this message]
2017-03-27  8:05 ` [PATCH 11/12] OvmfPkg: Enable PEI variable access Jordan Justen
2017-03-27  8:05 ` [PATCH 12/12] OvmfPkg QemuFlashFvbServicesRuntimeDxe: Cleanup init now done in PEI Jordan Justen
2017-03-27 18:03 ` [PATCH 00/12] OvmfPkg: Enable variable access " Laszlo Ersek
2017-03-27 21:47   ` Jordan Justen
2017-03-28  9:22     ` Laszlo Ersek
2017-03-29  5:24       ` Jordan Justen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170327080544.24748-11-jordan.l.justen@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox