ASSERT(PcdGet32 (PcdOvmfFirmwareFdSize) % mFdBlockSize == 0);\r
mFdBlockCount = PcdGet32 (PcdOvmfFirmwareFdSize) / mFdBlockSize;\r
\r
+ //\r
+ // execute module specific hooks before probing the flash\r
+ //\r
+ QemuFlashBeforeProbe (\r
+ (EFI_PHYSICAL_ADDRESS)(UINTN) mFlashBase,\r
+ mFdBlockSize,\r
+ mFdBlockCount\r
+ );\r
+\r
if (!QemuFlashDetected ()) {\r
ASSERT (!FeaturePcdGet (PcdSmmSmramRequire));\r
return EFI_WRITE_PROTECTED;\r
--- /dev/null
+/** @file\r
+ Define the module hooks used while probing the QEMU flash device.\r
+\r
+ Copyright (C) 2018, Advanced Micro Devices. All rights reserved.\r
+\r
+ This program and the accompanying materials are licensed and made available\r
+ under the terms and conditions of the BSD License which accompanies this\r
+ distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/MemEncryptSevLib.h>\r
+\r
+#include "QemuFlash.h"\r
+\r
+VOID\r
+QemuFlashBeforeProbe (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINTN FdBlockSize,\r
+ IN UINTN FdBlockCount\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ ASSERT (FeaturePcdGet (PcdSmmSmramRequire));\r
+\r
+ if (!MemEncryptSevIsEnabled ()) {\r
+ return;\r
+ }\r
+\r
+ //\r
+ // When SEV is enabled, AmdSevDxe runs early in DXE phase and clears the\r
+ // C-bit from the NonExistent entry -- which is later split and accommodate\r
+ // the flash MMIO but the driver runs in non SMM context hence it cleared the\r
+ // flash ranges from non SMM page table. When SMM is enabled, the flash\r
+ // services are accessed from the SMM mode hence we explicitly clear the\r
+ // C-bit on flash ranges from SMM page table.\r
+ //\r
+\r
+ Status = MemEncryptSevClearPageEncMask (\r
+ 0,\r
+ BaseAddress,\r
+ EFI_SIZE_TO_PAGES (FdBlockSize * FdBlockCount),\r
+ FALSE\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+}\r