From 2c4b18e09590e50c95b95fd485144d00b3b7f39f Mon Sep 17 00:00:00 2001 From: Star Zeng Date: Thu, 11 Jul 2013 08:15:47 +0000 Subject: [PATCH] MdeModulePkg: Add the alignment check for FTW spare area address and length, and add the check for PcdFlashNvStorageVariableSize <= PcdFlashNvStorageFtwSpareSize. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Star Zeng Reviewed-by: Jiewen Yao Reviewed-by: Liming Gao git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14463 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/MdeModulePkg.dec | 6 +++--- .../FaultTolerantWriteDxe/FaultTolerantWrite.h | 1 + .../FaultTolerantWriteDxe.inf | 1 + .../FaultTolerantWriteSmm.inf | 1 + .../Universal/FaultTolerantWriteDxe/FtwMisc.c | 18 ++++++++++++++++-- .../Variable/RuntimeDxe/VariableDxe.c | 8 +++++++- .../Variable/RuntimeDxe/VariableSmm.c | 6 ++++++ 7 files changed, 35 insertions(+), 6 deletions(-) diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 7db47ecfb8..ba922a935b 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -548,7 +548,7 @@ ## The size of volatile buffer. This buffer is used to store VOLATILE attribute variable. gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000|UINT32|0x30000005 - ## Size of the FTW spare block range. Note that this value should larger than PcdFlashNvStorageVariableSize + ## Size of the FTW spare block range. Note that this value should larger than PcdFlashNvStorageVariableSize and block size aligned. # The root cause is that variable driver will use FTW protocol to reclaim variable region. # If the length of variable region is larger than FTW spare size, it means the whole variable region can not # be reflushed through the manner of fault tolerant write. @@ -679,7 +679,7 @@ ## Base address of the NV variable range in flash device gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0x0|UINT32|0x30000001 - ## Base address of the FTW spare block range in flash device. + ## Base address of the FTW spare block range in flash device. Note that this value should be block size aligned. gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0x0|UINT32|0x30000013 ## Base address of the FTW working block range in flash device. @@ -688,7 +688,7 @@ ## 64-bit Base address of the NV variable range in flash device gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0x0|UINT64|0x80000001 - ## 64-bit Base address of the FTW spare block range in flash device. + ## 64-bit Base address of the FTW spare block range in flash device. Note that this value should be block size aligned. gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0x0|UINT64|0x80000013 ## 64-bit Base address of the FTW working block range in flash device. diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h index 78c65f2af6..a460694d65 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h @@ -32,6 +32,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include // // Flash erase polarity is 1 diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf index 3d4b9123a1..f4576447db 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf @@ -47,6 +47,7 @@ DebugLib UefiLib PcdLib + ReportStatusCodeLib [Guids] gEdkiiWorkingBlockSignatureGuid ## CONSUMES ## FV Signature of Working Space Header diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf index a5c8a58f22..7740efa359 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf @@ -50,6 +50,7 @@ DebugLib UefiLib PcdLib + ReportStatusCodeLib [Guids] gEdkiiWorkingBlockSignatureGuid ## CONSUMES ## FV Signature of Working Space Header diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c index fdd9dfa0c1..b3352bbe07 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c @@ -1111,6 +1111,20 @@ FindFvbForFtw ( ASSERT (FALSE); return EFI_ABORTED; } + // + // Check the alignment of spare area address and length, they should be block size aligned + // + if (((FtwDevice->SpareAreaAddress & (FtwDevice->BlockSize - 1)) != 0) || + ((FtwDevice->SpareAreaLength & (FtwDevice->BlockSize - 1)) != 0)) { + DEBUG ((EFI_D_ERROR, "Ftw: Spare area address or length is not block size aligned\n")); + FreePool (HandleBuffer); + // + // Report Status Code EFI_SW_EC_ABORTED. + // + REPORT_STATUS_CODE ( (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED), (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ABORTED)); + ASSERT (FALSE); + CpuDeadLoop (); + } break; } } @@ -1118,12 +1132,12 @@ FindFvbForFtw ( } } FreePool (HandleBuffer); - + if ((FtwDevice->FtwBackupFvb == NULL) || (FtwDevice->FtwFvBlock == NULL) || (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) { return EFI_ABORTED; } - + return EFI_SUCCESS; } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c index a697ae1c3a..c02d637699 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c @@ -335,6 +335,7 @@ FtwNotificationEvent ( UINT64 Length; EFI_PHYSICAL_ADDRESS VariableStoreBase; UINT64 VariableStoreLength; + UINTN FtwMaxBlockSize; // // Ensure FTW protocol is installed. @@ -343,7 +344,12 @@ FtwNotificationEvent ( if (EFI_ERROR (Status)) { return ; } - + + Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize); + if (!EFI_ERROR (Status)) { + ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize); + } + // // Find the proper FVB protocol for variable. // diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index 1ffa74e6cc..a80948bce8 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -792,6 +792,7 @@ SmmFtwNotificationEvent ( EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol; EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol; EFI_PHYSICAL_ADDRESS NvStorageVariableBase; + UINTN FtwMaxBlockSize; if (mVariableModuleGlobal->FvbInstance != NULL) { return EFI_SUCCESS; @@ -805,6 +806,11 @@ SmmFtwNotificationEvent ( return Status; } + Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize); + if (!EFI_ERROR (Status)) { + ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize); + } + // // Find the proper FVB protocol for variable. // -- 2.39.2