From 4edb18663fef14ef4c54f985a21d3faacec5d499 Mon Sep 17 00:00:00 2001 From: Star Zeng Date: Tue, 27 Jan 2015 08:42:47 +0000 Subject: [PATCH] MdeModulePkg Variable: Implement variable quota management. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Star Zeng Reviewed-by: Jiewen Yao git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16669 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Include/Guid/VarErrorFlag.h | 41 ++ MdeModulePkg/MdeModulePkg.dec | 32 +- MdeModulePkg/MdeModulePkg.uni | Bin 150012 -> 158370 bytes .../Universal/Variable/RuntimeDxe/VarCheck.c | 263 ++++++++----- .../Universal/Variable/RuntimeDxe/Variable.c | 371 ++++++++++++++++-- .../Universal/Variable/RuntimeDxe/Variable.h | 68 ++++ .../Variable/RuntimeDxe/VariableDxe.c | 8 + .../RuntimeDxe/VariableRuntimeDxe.inf | 3 + .../Variable/RuntimeDxe/VariableSmm.c | 8 + .../Variable/RuntimeDxe/VariableSmm.inf | 3 + 10 files changed, 654 insertions(+), 143 deletions(-) create mode 100644 MdeModulePkg/Include/Guid/VarErrorFlag.h diff --git a/MdeModulePkg/Include/Guid/VarErrorFlag.h b/MdeModulePkg/Include/Guid/VarErrorFlag.h new file mode 100644 index 0000000000..1f05396763 --- /dev/null +++ b/MdeModulePkg/Include/Guid/VarErrorFlag.h @@ -0,0 +1,41 @@ +/** @file + Variable error flag definitions. + + Copyright (c) 2015, Intel Corporation. All rights reserved.
+ This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef _VARIABLE_ERROR_FLAG_H_ +#define _VARIABLE_ERROR_FLAG_H_ + +// +// Before EndOfDxe, the variable indicates the last boot variable error flag, +// then it means the last boot variable error flag must be got before EndOfDxe. +// After EndOfDxe, the variable indicates the current boot variable error flag, +// then it means the current boot variable error flag must be got after EndOfDxe. +// +// If the variable is not present, it has the same meaning with VAR_ERROR_FLAG_NO_ERROR. +// +#define VAR_ERROR_FLAG_NAME L"VarErrorFlag" + +#define VAR_ERROR_FLAG_NO_ERROR 0xFF // 1111-1111 +#define VAR_ERROR_FLAG_SYSTEM_ERROR 0xEF // 1110-1111 +#define VAR_ERROR_FLAG_USER_ERROR 0xFE // 1111-1110 + +typedef UINT8 VAR_ERROR_FLAG; + +#define EDKII_VAR_ERROR_FLAG_GUID { \ + 0x4b37fe8, 0xf6ae, 0x480b, { 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } \ +}; + +extern EFI_GUID gEdkiiVarErrorFlagGuid; + +#endif + diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 9ea8abe3b9..a4cec63a8a 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -279,6 +279,9 @@ ## Include/Guid/MemoryProfile.h gEdkiiMemoryProfileGuid = { 0x821c9a09, 0x541a, 0x40f6, { 0x9f, 0x43, 0xa, 0xd1, 0x93, 0xa1, 0x2c, 0xfe }} + ## Include/Protocol/VarErrorFlag.h + gEdkiiVarErrorFlagGuid = { 0x4b37fe8, 0xf6ae, 0x480b, { 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } } + [Ppis] ## Include/Ppi/AtaController.h gPeiAtaControllerPpiGuid = { 0xa45e60d1, 0xc719, 0x44aa, { 0xb0, 0x7a, 0xaa, 0x77, 0x7f, 0x85, 0x90, 0x6d }} @@ -703,13 +706,38 @@ # @Prompt Maximum HwErr variable size. gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000|UINT32|0x30000004 - ## The size of reserved HwErr variable space. Note that this value must be less than or equal to PcdFlashNvStorageVariableSize. + ## The size of reserved HwErr variable space. Note that this value must be less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER)). # In EdkII implementation, HwErr type variable is stored with common non-volatile variables in the same NV region. - # so the platform integrator should ensure this value is less than or equal to PcdFlashNvStorageVariableSize. + # so the platform integrator should ensure this value is less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER)). # this value is used to guarantee the space of HwErr type variable and not populated by common variable. # @Prompt HwErr variable storage size. gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize|0x0000|UINT32|0x30000006 + ## The size of maximum user NV variable space.

+ # Note that this value must be less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize).
+ # If the value is 0, it means user variable share the same NV storage with system variable, + # this is designed to keep the compatibility for the platform that does not allocate special region for user variable.
+ # If the value is non-0, the below 4 types of variables will be regarded as System Variable, their property could be got by VarCheck protocol, + # otherwise the variable will be regarded as user variable.
+ # 1) UEFI defined variables (gEfiGlobalVariableGuid and gEfiImageSecurityDatabaseGuid(auth variable) variables at least).
+ # 2) Variables managed by Variable driver internally.
+ # 3) Variables need to be locked, they MUST be set by VariableLock protocol.
+ # 4) Important during platform boot, their property SHOULD be set by VarCheck protocol.
+ # The PCD is used to guarantee the space of system variable and not populated by user variable.
+ # @Prompt Maximum user NV variable space size. + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize|0x00|UINT32|0x00000009 + + ## The size of NV variable space reserved at UEFI boottime.

+ # Note that this value must be less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize).
+ # In EdkII implementation, variable driver can reserved some NV storage region for boottime settings. + # So at UEFI runtime, the variable service consumer can not exhaust full NV storage region.
+ # Then the common NV variable space size at boottime will be + # (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize),
+ # and the common NV variable space size at runtime will be + # (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize) - PcdBoottimeReservedNvVariableSpaceSize.
+ # @Prompt Boottime reserved NV variable space size. + gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize|0x00|UINT32|0x30000007 + ## The size of volatile buffer. This buffer is used to store VOLATILE attribute variables. # @Prompt Variable storage size. gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000|UINT32|0x30000005 diff --git a/MdeModulePkg/MdeModulePkg.uni b/MdeModulePkg/MdeModulePkg.uni index 436efaeb56958b8a3de7e092ca1d371fef3863b2..b2ed93baaf766c48e3bb1d2d588ded413b8275e1 100644 GIT binary patch delta 2755 zcmdT`UrbwN6hB|XMiIlh*5(4rdf8@19bJ}!Z|L=&_4JKt@gEoj^$Y5Lv!{ri3A z{Lb&3@BYnIAN^F7Yjdd9Pum@zbaAeA_kmH$Qk*jMDqW!0DT22I+9*w9bcAN`9^-p~ zp~^XX9k1Pbx+-grQ=Ip%pBI#8{|5?IFRr&=4b~b-fWC!ek~D|EiB_8+(A*z<(ptW+ zoc!p4lh^CN=Ce!B@w>r;s)-xFJ-~N^wUa5m z7jd|4N|xqf$}9{?a$}E2H4DlKnM+{Y%(vo%7(4+d;&92tjx^dS^x_DR^d~89U0+c? zzC7mQ?|yUF$xs9nCN`usfQjvCn&shlyoV*Oaf$&ng7}+aAm9_2PoX!CX9mxL6JE}> zJz?z&ICk@&^~>VrQGrT<%45`wh5CFvqkx&l%tgEh^!U6!D9hh3*QqQoUNd|Xnh)_w zJc(#OM~Zm3amVA^#7t;IX3(ZJQU+(uTkkC?Z^HzJ#J?yun|VdQBt*ckDeK822;Vce(8>0#RIq^^{ z$Sr7Gh}SZYr0^?vO}I zXcvlRutKCDM%x;JRA3kK<=wqTDPtWqUySiyH6 zI<2->)IMJAZmBNK-|mg9-$vj}4n&WG{WMG#e3Nc!(;+}&8d-_F${gspWt zSia{m&fKWo{Ku_YL{nc2Fm%hY7_k+W&jR!doNHHk?Sa?o?NC3f{XDjGcn4Q@E7YNW z;e~eR)vjjbxm$9=7t_+)j+HC|p5)+49bf*&WfV$j2{#~FO?gy{Mn1A15|naYPG<&b zE$X&)lNYiSE$(Tp=#zB3fVr3sP7DYs=t{0xnK3agxsk8=c*b!N%Z(s4O)_R2Z%AeC zUNLYb71#fbGGRNO`>TRiA66IwR;crGToOMVD)RFe>!fhC791+^+`6)$JX#if=6c1> zy6T$?SG~>4%uRQ784q>;%EPtu4v+QWf~whxz7OX=dFTvObq=39c4;jr^RcQ6w2m(? mYj40|@R!cB)|(%ry8jw_ith)Gzuy`9pfjXC?+iJXo&NyMsccUG delta 34 qcmZ2 (MAX_ADDRESS - LoadOption->FilePathListLength)) { + if ((UINTN) FilePathList > (MAX_ADDRESS - FilePathListLength)) { return EFI_INVALID_PARAMETER; } - if (((UINTN) FilePathList + LoadOption->FilePathListLength) > ((UINTN) Data + DataSize)) { + if (((UINTN) FilePathList + FilePathListLength) > ((UINTN) Data + DataSize)) { return EFI_INVALID_PARAMETER; } - if (LoadOption->FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) { + if (FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) { return EFI_INVALID_PARAMETER; } - if (!IsDevicePathValid (FilePathList, LoadOption->FilePathListLength)) { + if (!IsDevicePathValid (FilePathList, FilePathListLength)) { return EFI_INVALID_PARAMETER; } @@ -573,7 +565,7 @@ UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = { VAR_CHECK_VARIABLE_PROPERTY_REVISION, 0, VARIABLE_ATTRIBUTE_NV_BS_RT, - sizeof (EFI_LOAD_OPTION), + sizeof (UINT32) + sizeof (UINT16), MAX_UINTN }, InternalVarCheckLoadOption @@ -584,7 +576,7 @@ UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = { VAR_CHECK_VARIABLE_PROPERTY_REVISION, 0, VARIABLE_ATTRIBUTE_NV_BS_RT, - sizeof (EFI_LOAD_OPTION), + sizeof (UINT32) + sizeof (UINT16), MAX_UINTN }, InternalVarCheckLoadOption @@ -602,6 +594,26 @@ UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = { }, }; +typedef struct { + EFI_GUID *Guid; + CHAR16 *Name; + VAR_CHECK_VARIABLE_PROPERTY VariableProperty; +} VARIABLE_DRIVER_VARIABLE_ENTRY; + +VARIABLE_DRIVER_VARIABLE_ENTRY mVariableDriverVariableList[] = { + { + &gEdkiiVarErrorFlagGuid, + VAR_ERROR_FLAG_NAME, + { + VAR_CHECK_VARIABLE_PROPERTY_REVISION, + VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY, + VARIABLE_ATTRIBUTE_NV_BS_RT, + sizeof (VAR_ERROR_FLAG), + sizeof (VAR_ERROR_FLAG), + } + }, +}; + /** Get UEFI defined global variable property. The code will check if variable guid is global variable guid first. @@ -661,14 +673,13 @@ GetUefiDefinedVariableProperty ( *VariableProperty = &mGlobalVariableList2[Index].VariableProperty; return EFI_SUCCESS; } - } else { - if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) { - if (VarCheckFunction != NULL) { - *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction; - } - *VariableProperty = &mGlobalVariableList2[Index].VariableProperty; - return EFI_SUCCESS; + } + if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) { + if (VarCheckFunction != NULL) { + *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction; } + *VariableProperty = &mGlobalVariableList2[Index].VariableProperty; + return EFI_SUCCESS; } } @@ -684,6 +695,32 @@ GetUefiDefinedVariableProperty ( return EFI_SUCCESS; } +/** + Get variable property for variables managed by Varaible driver. + + @param[in] VariableName Pointer to variable name. + @param[in] VendorGuid Variable Vendor Guid. + + @return Pointer to variable property. + +**/ +VAR_CHECK_VARIABLE_PROPERTY * +GetVariableDriverVariableProperty ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + UINTN Index; + + for (Index = 0; Index < sizeof (mVariableDriverVariableList)/sizeof (mVariableDriverVariableList[0]); Index++) { + if ((CompareGuid (mVariableDriverVariableList[Index].Guid, VendorGuid)) && (StrCmp (mVariableDriverVariableList[Index].Name, VariableName) == 0)) { + return &mVariableDriverVariableList[Index].VariableProperty; + } + } + + return NULL; +} + /** Internal SetVariable check. @@ -694,7 +731,7 @@ GetUefiDefinedVariableProperty ( @param[in] Data Data pointer. @retval EFI_SUCCESS The SetVariable check result was success. - @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID were supplied, + @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the DataSize exceeds the minimum or maximum allowed, or the Data value is not following UEFI spec for UEFI defined variables. @retval EFI_WRITE_PROTECTED The variable in question is read-only. @@ -727,23 +764,27 @@ InternalVarCheckSetVariableCheck ( } Property = NULL; - Status = GetUefiDefinedVariableProperty (VariableName, VendorGuid, TRUE, &Property, &VarCheckFunction); - if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_INFO, "[Variable]: Var Check UEFI defined variable fail %r - %g:%s\n", Status, VendorGuid, VariableName)); - return Status; + VarCheckFunction = NULL; + + for ( Link = GetFirstNode (&mVarCheckVariableList) + ; !IsNull (&mVarCheckVariableList, Link) + ; Link = GetNextNode (&mVarCheckVariableList, Link) + ) { + Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link); + Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry)); + if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) { + Property = &Entry->VariableProperty; + break; + } } if (Property == NULL) { - VarCheckFunction = NULL; - for ( Link = GetFirstNode (&mVarCheckVariableList) - ; !IsNull (&mVarCheckVariableList, Link) - ; Link = GetNextNode (&mVarCheckVariableList, Link) - ) { - Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link); - Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry)); - if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) { - Property = &Entry->VariableProperty; - break; - } + Property = GetVariableDriverVariableProperty (VariableName, VendorGuid); + } + if (Property == NULL) { + Status = GetUefiDefinedVariableProperty (VariableName, VendorGuid, TRUE, &Property, &VarCheckFunction); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "[Variable]: Var Check UEFI defined variable fail %r - %g:%s\n", Status, VendorGuid, VariableName)); + return Status; } } if (Property != NULL) { @@ -751,29 +792,28 @@ InternalVarCheckSetVariableCheck ( DEBUG ((EFI_D_INFO, "[Variable]: Var Check ReadOnly variable fail %r - %g:%s\n", EFI_WRITE_PROTECTED, VendorGuid, VariableName)); return EFI_WRITE_PROTECTED; } - if ((DataSize == 0) || (Attributes == 0)) { + if (!((DataSize == 0) || (Attributes == 0))) { // - // Do not check delete variable. + // Not to delete variable. // - return EFI_SUCCESS; - } - if (Attributes != Property->Attributes) { - DEBUG ((EFI_D_INFO, "[Variable]: Var Check Attributes fail %r - %g:%s\n", EFI_INVALID_PARAMETER, VendorGuid, VariableName)); - return EFI_INVALID_PARAMETER; - } - if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) { - DEBUG ((EFI_D_INFO, "[Variable]: Var Check DataSize fail %r - %g:%s\n", EFI_INVALID_PARAMETER, VendorGuid, VariableName)); - return EFI_INVALID_PARAMETER; - } - if (VarCheckFunction != NULL) { - Status = VarCheckFunction ( - Property, - DataSize, - Data - ); - if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_INFO, "[Variable]: Internal Var Check function fail %r - %g:%s\n", Status, VendorGuid, VariableName)); - return Status; + if (Attributes != Property->Attributes) { + DEBUG ((EFI_D_INFO, "[Variable]: Var Check Attributes(0x%08x to 0x%08x) fail %r - %g:%s\n", Property->Attributes, Attributes, EFI_INVALID_PARAMETER, VendorGuid, VariableName)); + return EFI_INVALID_PARAMETER; + } + if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) { + DEBUG ((EFI_D_INFO, "[Variable]: Var Check DataSize fail(0x%x not in 0x%x - 0x%x) %r - %g:%s\n", DataSize, Property->MinSize, Property->MaxSize, EFI_INVALID_PARAMETER, VendorGuid, VariableName)); + return EFI_INVALID_PARAMETER; + } + if (VarCheckFunction != NULL) { + Status = VarCheckFunction ( + Property, + DataSize, + Data + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "[Variable]: Internal Var Check function fail %r - %g:%s\n", Status, VendorGuid, VariableName)); + return Status; + } } } } @@ -888,18 +928,20 @@ VarCheckRegisterSetVariableCheckHandler ( } /** - Internal variable property get. + Variable property get function. - @param[in] Name Pointer to the variable name. - @param[in] Guid Pointer to the vendor GUID. + @param[in] Name Pointer to the variable name. + @param[in] Guid Pointer to the vendor GUID. + @param[in] WildcardMatch Try wildcard match or not. @return Pointer to the property of variable specified by the Name and Guid. **/ VAR_CHECK_VARIABLE_PROPERTY * -InternalVarCheckVariablePropertyGet ( - IN CHAR16 *Name, - IN EFI_GUID *Guid +VariablePropertyGetFunction ( + IN CHAR16 *Name, + IN EFI_GUID *Guid, + IN BOOLEAN WildcardMatch ) { LIST_ENTRY *Link; @@ -907,24 +949,23 @@ InternalVarCheckVariablePropertyGet ( CHAR16 *VariableName; VAR_CHECK_VARIABLE_PROPERTY *Property; - Property = NULL; - GetUefiDefinedVariableProperty (Name, Guid, FALSE, &Property, NULL); - if (Property != NULL) { - return Property; - } else { - for ( Link = GetFirstNode (&mVarCheckVariableList) - ; !IsNull (&mVarCheckVariableList, Link) - ; Link = GetNextNode (&mVarCheckVariableList, Link) - ) { - Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link); - VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry)); - if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) { - return &Entry->VariableProperty; - } + for ( Link = GetFirstNode (&mVarCheckVariableList) + ; !IsNull (&mVarCheckVariableList, Link) + ; Link = GetNextNode (&mVarCheckVariableList, Link) + ) { + Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link); + VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry)); + if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) { + return &Entry->VariableProperty; } } - return NULL; + Property = GetVariableDriverVariableProperty (Name, Guid); + if (Property == NULL) { + GetUefiDefinedVariableProperty (Name, Guid, WildcardMatch, &Property, NULL); + } + + return Property; } /** @@ -975,7 +1016,7 @@ VarCheckVariablePropertySet ( AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); - Property = InternalVarCheckVariablePropertyGet (Name, Guid); + Property = VariablePropertyGetFunction (Name, Guid, FALSE); if (Property != NULL) { CopyMem (Property, VariableProperty, sizeof (*VariableProperty)); } else { @@ -998,20 +1039,19 @@ Done: } /** - Variable property get. + Internal variable property get. @param[in] Name Pointer to the variable name. @param[in] Guid Pointer to the vendor GUID. @param[out] VariableProperty Pointer to the output variable property. @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully. - @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string. @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found. **/ EFI_STATUS EFIAPI -VarCheckVariablePropertyGet ( +InternalVarCheckVariablePropertyGet ( IN CHAR16 *Name, IN EFI_GUID *Guid, OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty @@ -1023,19 +1063,9 @@ VarCheckVariablePropertyGet ( BOOLEAN Found; VAR_CHECK_VARIABLE_PROPERTY *Property; - if (Name == NULL || Name[0] == 0 || Guid == NULL) { - return EFI_INVALID_PARAMETER; - } - - if (VariableProperty == NULL) { - return EFI_INVALID_PARAMETER; - } - Found = FALSE; - AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); - - Property = InternalVarCheckVariablePropertyGet (Name, Guid); + Property = VariablePropertyGetFunction (Name, Guid, TRUE); if (Property != NULL) { CopyMem (VariableProperty, Property, sizeof (*VariableProperty)); Found = TRUE; @@ -1056,8 +1086,45 @@ VarCheckVariablePropertyGet ( } } + return (Found ? EFI_SUCCESS : EFI_NOT_FOUND); +} + +/** + Variable property get. + + @param[in] Name Pointer to the variable name. + @param[in] Guid Pointer to the vendor GUID. + @param[out] VariableProperty Pointer to the output variable property. + + @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully. + @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string. + @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found. + +**/ +EFI_STATUS +EFIAPI +VarCheckVariablePropertyGet ( + IN CHAR16 *Name, + IN EFI_GUID *Guid, + OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty + ) +{ + EFI_STATUS Status; + + if (Name == NULL || Name[0] == 0 || Guid == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (VariableProperty == NULL) { + return EFI_INVALID_PARAMETER; + } + + AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); + + Status = InternalVarCheckVariablePropertyGet (Name, Guid, VariableProperty); + ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); - return (Found ? EFI_SUCCESS : EFI_NOT_FOUND); + return Status; } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c index e3c06b3962..0cf367404f 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -524,6 +524,214 @@ GetEndPointer ( return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size); } +/** + Record variable error flag. + + @param[in] Flag Variable error flag to record. + @param[in] VariableName Name of variable. + @param[in] VendorGuid Guid of variable. + @param[in] Attributes Attributes of the variable. + @param[in] VariableSize Size of the variable. + +**/ +VOID +RecordVarErrorFlag ( + IN VAR_ERROR_FLAG Flag, + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, + IN UINTN VariableSize + ) +{ + EFI_STATUS Status; + VARIABLE_POINTER_TRACK Variable; + VAR_ERROR_FLAG *VarErrFlag; + VAR_ERROR_FLAG TempFlag; + + DEBUG_CODE ( + DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize)); + if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) { + if (AtRuntime ()) { + DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize)); + } else { + DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize)); + } + } else { + DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize)); + } + ); + + // + // Record error flag (it should have be initialized). + // + Status = FindVariable ( + VAR_ERROR_FLAG_NAME, + &gEdkiiVarErrorFlagGuid, + &Variable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + if (!EFI_ERROR (Status)) { + VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr); + TempFlag = *VarErrFlag; + TempFlag &= Flag; + if (TempFlag == *VarErrFlag) { + return; + } + Status = UpdateVariableStore ( + &mVariableModuleGlobal->VariableGlobal, + FALSE, + FALSE, + mVariableModuleGlobal->FvbInstance, + (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, + sizeof (TempFlag), + &TempFlag + ); + if (!EFI_ERROR (Status)) { + // + // Update the data in NV cache. + // + *VarErrFlag = Flag; + } + } +} + +/** + Initialize variable error flag. + + Before EndOfDxe, the variable indicates the last boot variable error flag, + then it means the last boot variable error flag must be got before EndOfDxe. + After EndOfDxe, the variable indicates the current boot variable error flag, + then it means the current boot variable error flag must be got after EndOfDxe. + +**/ +VOID +InitializeVarErrorFlag ( + VOID + ) +{ + EFI_STATUS Status; + VARIABLE_POINTER_TRACK Variable; + VAR_ERROR_FLAG Flag; + VAR_ERROR_FLAG VarErrFlag; + + if (!mEndOfDxe) { + return; + } + + Flag = VAR_ERROR_FLAG_NO_ERROR; + DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag)); + + Status = FindVariable ( + VAR_ERROR_FLAG_NAME, + &gEdkiiVarErrorFlagGuid, + &Variable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + if (!EFI_ERROR (Status)) { + VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr)); + if (VarErrFlag == Flag) { + return; + } + } + + UpdateVariable ( + VAR_ERROR_FLAG_NAME, + &gEdkiiVarErrorFlagGuid, + &Flag, + sizeof (Flag), + VARIABLE_ATTRIBUTE_NV_BS_RT, + &Variable + ); +} + +/** + Is user variable? + + @param[in] Variable Pointer to variable header. + + @retval TRUE User variable. + @retval FALSE System variable. + +**/ +BOOLEAN +IsUserVariable ( + IN VARIABLE_HEADER *Variable + ) +{ + VAR_CHECK_VARIABLE_PROPERTY Property; + + // + // Only after End Of Dxe, the variables belong to system variable are fixed. + // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable, + // then no need to check if the variable is user variable or not specially. + // + if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) { + if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) { + return TRUE; + } + } + return FALSE; +} + +/** + Calculate common user variable total size. + +**/ +VOID +CalculateCommonUserVariableTotalSize ( + VOID + ) +{ + VARIABLE_HEADER *Variable; + VARIABLE_HEADER *NextVariable; + UINTN VariableSize; + VAR_CHECK_VARIABLE_PROPERTY Property; + + // + // Only after End Of Dxe, the variables belong to system variable are fixed. + // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable, + // then no need to calculate the common user variable total size specially. + // + if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) { + Variable = GetStartPointer (mNvVariableCache); + while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) { + NextVariable = GetNextVariablePtr (Variable); + VariableSize = (UINTN) NextVariable - (UINTN) Variable; + if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) { + if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) { + // + // No property, it is user variable. + // + mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize; + } + } + + Variable = NextVariable; + } + } +} + +/** + Initialize variable quota. + +**/ +VOID +InitializeVariableQuota ( + VOID + ) +{ + STATIC BOOLEAN Initialized; + + if (!mEndOfDxe || Initialized) { + return; + } + Initialized = TRUE; + + InitializeVarErrorFlag (); + CalculateCommonUserVariableTotalSize (); +} /** @@ -567,6 +775,7 @@ Reclaim ( BOOLEAN FoundAdded; EFI_STATUS Status; UINTN CommonVariableTotalSize; + UINTN CommonUserVariableTotalSize; UINTN HwErrVariableTotalSize; VARIABLE_HEADER *UpdatingVariable; VARIABLE_HEADER *UpdatingInDeletedTransition; @@ -581,6 +790,7 @@ Reclaim ( VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase); CommonVariableTotalSize = 0; + CommonUserVariableTotalSize = 0; HwErrVariableTotalSize = 0; if (IsVolatile) { @@ -650,6 +860,9 @@ Reclaim ( HwErrVariableTotalSize += VariableSize; } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { CommonVariableTotalSize += VariableSize; + if (IsUserVariable (Variable)) { + CommonUserVariableTotalSize += VariableSize; + } } } Variable = NextVariable; @@ -698,6 +911,9 @@ Reclaim ( HwErrVariableTotalSize += VariableSize; } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { CommonVariableTotalSize += VariableSize; + if (IsUserVariable (Variable)) { + CommonUserVariableTotalSize += VariableSize; + } } } } @@ -721,9 +937,13 @@ Reclaim ( HwErrVariableTotalSize += NewVariableSize; } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) { CommonVariableTotalSize += NewVariableSize; + if (IsUserVariable (NewVariable)) { + CommonUserVariableTotalSize += NewVariableSize; + } } if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) || - (CommonVariableTotalSize > VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize))) { + (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) || + (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) { // // No enough space to store the new variable by NV or NV+HR attribute. // @@ -761,19 +981,24 @@ Reclaim ( *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer); mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize; mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize; + mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize; } else { - NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase); - while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) { - VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER); + Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase); + while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) { + NextVariable = GetNextVariablePtr (Variable); + VariableSize = (UINTN) NextVariable - (UINTN) Variable; if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) { - mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize); + mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize; } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) { - mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize); + mVariableModuleGlobal->CommonVariableTotalSize += VariableSize; + if (IsUserVariable (Variable)) { + mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize; + } } - NextVariable = GetNextVariablePtr (NextVariable); + Variable = NextVariable; } - *LastVariableOffset = (UINTN) NextVariable - (UINTN) VariableBase; + *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase; } } @@ -1627,7 +1852,6 @@ UpdateVariable ( EFI_STATUS Status; VARIABLE_HEADER *NextVariable; UINTN ScratchSize; - UINTN NonVolatileVarableStoreSize; UINTN VarNameOffset; UINTN VarDataOffset; UINTN VarNameSize; @@ -1639,6 +1863,8 @@ UpdateVariable ( VARIABLE_POINTER_TRACK NvVariable; VARIABLE_STORE_HEADER *VariableStoreHeader; UINTN CacheOffset; + BOOLEAN IsCommonVariable; + BOOLEAN IsCommonUserVariable; if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) { // @@ -1856,12 +2082,25 @@ UpdateVariable ( // Create a nonvolatile variable. // Volatile = FALSE; - NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size; - if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) + + IsCommonVariable = FALSE; + IsCommonUserVariable = FALSE; + if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) { + IsCommonVariable = TRUE; + IsCommonUserVariable = IsUserVariable (NextVariable); + } + if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize))) - || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) - && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) { + || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) + || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) + || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) { if (AtRuntime ()) { + if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) { + RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize); + } + if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) { + RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize); + } Status = EFI_OUT_OF_RESOURCES; goto Done; } @@ -1880,6 +2119,13 @@ UpdateVariable ( } UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE); FlushHobVariableToFlash (VariableName, VendorGuid); + } else { + if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) { + RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize); + } + if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) { + RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize); + } } goto Done; } @@ -1965,6 +2211,9 @@ UpdateVariable ( mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize); } else { mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize); + if (IsCommonUserVariable) { + mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize); + } } // // update the memory copy of Flash region. @@ -2579,7 +2828,7 @@ VariableServiceSetVariable ( // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE // Status = EFI_INVALID_PARAMETER; - DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable with different attributes - %g:%s\n", VendorGuid, VariableName)); + DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable.CurrPtr->Attributes, Attributes, VendorGuid, VariableName)); goto Done; } } @@ -2672,8 +2921,11 @@ VariableServiceQueryVariableInfoInternal ( *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER); } else { if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { - ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size); - *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize); + if (AtRuntime ()) { + *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace; + } else { + *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace; + } } // @@ -2749,8 +3001,12 @@ VariableServiceQueryVariableInfoInternal ( if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){ *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize; - }else { - *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize; + } else { + if (*MaximumVariableStorageSize < CommonVariableTotalSize) { + *RemainingVariableStorageSize = 0; + } else { + *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize; + } } if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) { @@ -2851,22 +3107,23 @@ ReclaimForOS( ) { EFI_STATUS Status; - UINTN CommonVariableSpace; - UINTN RemainingCommonVariableSpace; + UINTN RemainingCommonRuntimeVariableSpace; UINTN RemainingHwErrVariableSpace; - Status = EFI_SUCCESS; + Status = EFI_SUCCESS; - CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space - - RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize; + if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) { + RemainingCommonRuntimeVariableSpace = 0; + } else { + RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize; + } RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize; // - // Check if the free area is blow a threshold. + // Check if the free area is below a threshold. // - if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize)) - || ((PcdGet32 (PcdHwErrStorageSize) != 0) && + if ((RemainingCommonRuntimeVariableSpace < PcdGet32 (PcdMaxVariableSize)) + || ((PcdGet32 (PcdHwErrStorageSize) != 0) && (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){ Status = Reclaim ( mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, @@ -2894,6 +3151,7 @@ InitNonVolatileVariableStore ( ) { EFI_FIRMWARE_VOLUME_HEADER *FvHeader; + VARIABLE_HEADER *Variable; VARIABLE_HEADER *NextVariable; EFI_PHYSICAL_ADDRESS VariableStoreBase; UINT64 VariableStoreLength; @@ -2905,17 +3163,12 @@ InitNonVolatileVariableStore ( FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData; UINT32 BackUpOffset; UINT32 BackUpSize; + UINT32 HwErrStorageSize; + UINT32 MaxUserNvVariableSpaceSize; + UINT32 BoottimeReservedNvVariableSpaceSize; mVariableModuleGlobal->FvbInstance = NULL; - // - // Note that in EdkII variable driver implementation, Hardware Error Record type variable - // is stored with common variable in the same NV region. So the platform integrator should - // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of - // PcdFlashNvStorageVariableSize. - // - ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize)); - // // Allocate runtime memory used for a memory copy of the FLASH region. // Keep the memory and the FLASH in sync as updates occur. @@ -2985,6 +3238,37 @@ InitNonVolatileVariableStore ( } ASSERT(mNvVariableCache->Size == VariableStoreLength); + + ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength); + + HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize); + MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize); + BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize); + + // + // Note that in EdkII variable driver implementation, Hardware Error Record type variable + // is stored with common variable in the same NV region. So the platform integrator should + // ensure that the value of PcdHwErrStorageSize is less than the value of + // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)). + // + ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER))); + // + // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of + // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize). + // + ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize)); + // + // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of + // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize). + // + ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize)); + + mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize); + mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace); + mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize; + + DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace)); + // // The max variable or hardware error variable size should be < variable store size. // @@ -2993,18 +3277,19 @@ InitNonVolatileVariableStore ( // // Parse non-volatile variable data and get last variable offset. // - NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase); - while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) { - VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER); - if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize); + Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase); + while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) { + NextVariable = GetNextVariablePtr (Variable); + VariableSize = (UINTN) NextVariable - (UINTN) Variable; + if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize; } else { - mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize); + mVariableModuleGlobal->CommonVariableTotalSize += VariableSize; } - NextVariable = GetNextVariablePtr (NextVariable); + Variable = NextVariable; } - mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase; + mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase; return EFI_SUCCESS; } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h index eb21cd854b..8ea7378231 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h @@ -42,6 +42,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #define VARIABLE_ATTRIBUTE_BS_RT (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS) #define VARIABLE_ATTRIBUTE_NV_BS_RT (VARIABLE_ATTRIBUTE_BS_RT | EFI_VARIABLE_NON_VOLATILE) @@ -85,7 +86,11 @@ typedef struct { VARIABLE_GLOBAL VariableGlobal; UINTN VolatileLastVariableOffset; UINTN NonVolatileLastVariableOffset; + UINTN CommonVariableSpace; + UINTN CommonMaxUserVariableSpace; + UINTN CommonRuntimeVariableSpace; UINTN CommonVariableTotalSize; + UINTN CommonUserVariableTotalSize; UINTN HwErrVariableTotalSize; CHAR8 *PlatformLangCodes; CHAR8 *LangCodes; @@ -360,6 +365,41 @@ GetFvbInfoByAddress ( OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL ); +/** + Finds variable in storage blocks of volatile and non-volatile storage areas. + + This code finds variable in storage blocks of volatile and non-volatile storage areas. + If VariableName is an empty string, then we just return the first + qualified variable without comparing VariableName and VendorGuid. + If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check + at runtime when searching existing variable, only VariableName and VendorGuid are compared. + Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime. + + @param VariableName Name of the variable to be found. + @param VendorGuid Vendor GUID to be found. + @param PtrTrack VARIABLE_POINTER_TRACK structure for output, + including the range searched and the target position. + @param Global Pointer to VARIABLE_GLOBAL structure, including + base of volatile variable storage area, base of + NV variable storage area, and a lock. + @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute + check at runtime when searching variable. + + @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while + VendorGuid is NULL. + @retval EFI_SUCCESS Variable successfully found. + @retval EFI_NOT_FOUND Variable not found + +**/ +EFI_STATUS +FindVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT VARIABLE_POINTER_TRACK *PtrTrack, + IN VARIABLE_GLOBAL *Global, + IN BOOLEAN IgnoreRtCheck + ); + /** This code finds variable in storage blocks (Volatile or Non-Volatile). @@ -615,6 +655,25 @@ VarCheckVariablePropertySet ( IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty ); +/** + Internal variable property get. + + @param[in] Name Pointer to the variable name. + @param[in] Guid Pointer to the vendor GUID. + @param[out] VariableProperty Pointer to the output variable property. + + @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully. + @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found. + +**/ +EFI_STATUS +EFIAPI +InternalVarCheckVariablePropertyGet ( + IN CHAR16 *Name, + IN EFI_GUID *Guid, + OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty + ); + /** Variable property get. @@ -635,6 +694,15 @@ VarCheckVariablePropertyGet ( OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty ); +/** + Initialize variable quota. + +**/ +VOID +InitializeVariableQuota ( + VOID + ); + extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal; #endif diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c index 2aa04f1fef..c61064efde 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c @@ -282,6 +282,10 @@ OnReadyToBoot ( // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled. // mEndOfDxe = TRUE; + // + // The initialization for variable quota. + // + InitializeVariableQuota (); ReclaimForOS (); if (FeaturePcdGet (PcdVariableCollectStatistics)) { gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo); @@ -305,6 +309,10 @@ OnEndOfDxe ( ) { mEndOfDxe = TRUE; + // + // The initialization for variable quota. + // + InitializeVariableQuota (); } /** diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf index 007a6dc591..ed6d1b5cbb 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf @@ -91,6 +91,7 @@ gEfiEndOfDxeEventGroupGuid ## CONSUMES ## Event ## SOMETIMES_CONSUMES ## HOB gEdkiiFaultTolerantWriteGuid + gEdkiiVarErrorFlagGuid ## CONSUMES ## GUID [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize ## CONSUMES @@ -100,6 +101,8 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize ## CONSUMES [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics ## CONSUMES # statistic the information of variable. diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index 6df1660a04..815ee2e819 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -693,6 +693,10 @@ SmmVariableHandler ( case SMM_VARIABLE_FUNCTION_READY_TO_BOOT: mEndOfDxe = TRUE; + // + // The initialization for variable quota. + // + InitializeVariableQuota (); if (AtRuntime()) { Status = EFI_UNSUPPORTED; break; @@ -824,6 +828,10 @@ SmmEndOfDxeCallback ( { DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n")); mEndOfDxe = TRUE; + // + // The initialization for variable quota. + // + InitializeVariableQuota (); return EFI_SUCCESS; } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf index 5e88213449..e57a731d15 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf @@ -93,6 +93,7 @@ gEfiHardwareErrorVariableGuid ## SOMETIMES_CONSUMES ## Variable:L"HwErrRec####" ## SOMETIMES_CONSUMES ## HOB gEdkiiFaultTolerantWriteGuid + gEdkiiVarErrorFlagGuid ## CONSUMES ## GUID [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize ## CONSUMES @@ -102,6 +103,8 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize ## CONSUMES [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics ## CONSUMES # statistic the information of variable. -- 2.39.2