What to do: 1. Merge from VariablePei in SecurityPkg to VariablePei in MdeModulePkg.
Why to do: 1. Remove code duplication and reduce maintenance effort. The code logic of VariablePei in SecurityPkg is same with VariablePei in MdeModulePkg. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Star Zeng <star.z...@intel.com> Reviewed-by: Jiewen Yao <jiewen....@intel.com> Reviewed-by: Liming Gao <liming....@intel.com> --- .../Universal/Variable/Pei/PeiVariable.uni | Bin 2130 -> 2182 bytes MdeModulePkg/Universal/Variable/Pei/Variable.c | 194 +++++++++++++++------ MdeModulePkg/Universal/Variable/Pei/Variable.h | 4 +- .../Universal/Variable/Pei/VariablePei.inf | 9 +- 4 files changed, 153 insertions(+), 54 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/Pei/PeiVariable.uni b/MdeModulePkg/Universal/Variable/Pei/PeiVariable.uni index 830143bb2986dea174a32c5b11b2642c9c920293..7c11eff3c1f34dd93025df9d11563621b9389759 100644 GIT binary patch delta 98 zcmca4&?Y#+Y;pqIn#mg&MI=KQG8i%$iWw9bavAa&QW#1Zau`y9yv)h|Y<qz!To_F^ Ve`TD^ieDj*9)l7CF9R1C0{{~$7SI3y delta 40 ycmV+@0N4M95z-KlHj^R&aFap^I+M-;5|Jd*li&d`0W`A_0+R)kY6iZOdI$_2Z4HqC diff --git a/MdeModulePkg/Universal/Variable/Pei/Variable.c b/MdeModulePkg/Universal/Variable/Pei/Variable.c index 97c86f2..8dc828c 100644 --- a/MdeModulePkg/Universal/Variable/Pei/Variable.c +++ b/MdeModulePkg/Universal/Variable/Pei/Variable.c @@ -1,9 +1,8 @@ /** @file - Implement ReadOnly Variable Services required by PEIM and install PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space. -Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR> +Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR> 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 @@ -114,27 +113,65 @@ IsValidVariableHeader ( return TRUE; } +/** + This code gets the size of variable header. + + @param AuthFlag Authenticated variable flag. + + @return Size of variable header in bytes in type UINTN. + +**/ +UINTN +GetVariableHeaderSize ( + IN BOOLEAN AuthFlag + ) +{ + UINTN Value; + + if (AuthFlag) { + Value = sizeof (AUTHENTICATED_VARIABLE_HEADER); + } else { + Value = sizeof (VARIABLE_HEADER); + } + + return Value; +} /** This code gets the size of name of variable. @param Variable Pointer to the Variable Header. + @param AuthFlag Authenticated variable flag. @return Size of variable in bytes in type UINTN. **/ UINTN NameSizeOfVariable ( - IN VARIABLE_HEADER *Variable + IN VARIABLE_HEADER *Variable, + IN BOOLEAN AuthFlag ) { - if (Variable->State == (UINT8) (-1) || - Variable->DataSize == (UINT32) (-1) || - Variable->NameSize == (UINT32) (-1) || - Variable->Attributes == (UINT32) (-1)) { - return 0; + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (AuthFlag) { + if (AuthVariable->State == (UINT8) (-1) || + AuthVariable->DataSize == (UINT32) (-1) || + AuthVariable->NameSize == (UINT32) (-1) || + AuthVariable->Attributes == (UINT32) (-1)) { + return 0; + } + return (UINTN) AuthVariable->NameSize; + } else { + if (Variable->State == (UINT8) (-1) || + Variable->DataSize == (UINT32) (-1) || + Variable->NameSize == (UINT32) (-1) || + Variable->Attributes == (UINT32) (-1)) { + return 0; + } + return (UINTN) Variable->NameSize; } - return (UINTN) Variable->NameSize; } @@ -142,46 +179,88 @@ NameSizeOfVariable ( This code gets the size of data of variable. @param Variable Pointer to the Variable Header. + @param AuthFlag Authenticated variable flag. @return Size of variable in bytes in type UINTN. **/ UINTN DataSizeOfVariable ( - IN VARIABLE_HEADER *Variable + IN VARIABLE_HEADER *Variable, + IN BOOLEAN AuthFlag ) { - if (Variable->State == (UINT8) (-1) || - Variable->DataSize == (UINT32) (-1) || - Variable->NameSize == (UINT32) (-1) || - Variable->Attributes == (UINT32) (-1)) { - return 0; + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (AuthFlag) { + if (AuthVariable->State == (UINT8) (-1) || + AuthVariable->DataSize == (UINT32) (-1) || + AuthVariable->NameSize == (UINT32) (-1) || + AuthVariable->Attributes == (UINT32) (-1)) { + return 0; + } + return (UINTN) AuthVariable->DataSize; + } else { + if (Variable->State == (UINT8) (-1) || + Variable->DataSize == (UINT32) (-1) || + Variable->NameSize == (UINT32) (-1) || + Variable->Attributes == (UINT32) (-1)) { + return 0; + } + return (UINTN) Variable->DataSize; } - return (UINTN) Variable->DataSize; } /** This code gets the pointer to the variable name. @param Variable Pointer to the Variable Header. + @param AuthFlag Authenticated variable flag. @return A CHAR16* pointer to Variable Name. **/ CHAR16 * GetVariableNamePtr ( - IN VARIABLE_HEADER *Variable + IN VARIABLE_HEADER *Variable, + IN BOOLEAN AuthFlag ) { - return (CHAR16 *) (Variable + 1); + return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize (AuthFlag)); } +/** + This code gets the pointer to the variable guid. + + @param Variable Pointer to the Variable Header. + @param AuthFlag Authenticated variable flag. + + @return A EFI_GUID* pointer to Vendor Guid. + +**/ +EFI_GUID * +GetVendorGuidPtr ( + IN VARIABLE_HEADER *Variable, + IN BOOLEAN AuthFlag + ) +{ + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (AuthFlag) { + return &AuthVariable->VendorGuid; + } else { + return &Variable->VendorGuid; + } +} /** This code gets the pointer to the variable data. @param Variable Pointer to the Variable Header. @param VariableHeader Pointer to the Variable Header that has consecutive content. + @param AuthFlag Authenticated variable flag. @return A UINT8* pointer to Variable Data. @@ -189,7 +268,8 @@ GetVariableNamePtr ( UINT8 * GetVariableDataPtr ( IN VARIABLE_HEADER *Variable, - IN VARIABLE_HEADER *VariableHeader + IN VARIABLE_HEADER *VariableHeader, + IN BOOLEAN AuthFlag ) { UINTN Value; @@ -197,9 +277,9 @@ GetVariableDataPtr ( // // Be careful about pad size for alignment // - Value = (UINTN) GetVariableNamePtr (Variable); - Value += NameSizeOfVariable (VariableHeader); - Value += GET_PAD_SIZE (NameSizeOfVariable (VariableHeader)); + Value = (UINTN) GetVariableNamePtr (Variable, AuthFlag); + Value += NameSizeOfVariable (VariableHeader, AuthFlag); + Value += GET_PAD_SIZE (NameSizeOfVariable (VariableHeader, AuthFlag)); return (UINT8 *) Value; } @@ -226,9 +306,9 @@ GetNextVariablePtr ( EFI_PHYSICAL_ADDRESS SpareAddress; UINTN Value; - Value = (UINTN) GetVariableDataPtr (Variable, VariableHeader); - Value += DataSizeOfVariable (VariableHeader); - Value += GET_PAD_SIZE (DataSizeOfVariable (VariableHeader)); + Value = (UINTN) GetVariableDataPtr (Variable, VariableHeader, StoreInfo->AuthFlag); + Value += DataSizeOfVariable (VariableHeader, StoreInfo->AuthFlag); + Value += GET_PAD_SIZE (DataSizeOfVariable (VariableHeader, StoreInfo->AuthFlag)); // // Be careful about pad size for alignment // @@ -263,7 +343,8 @@ GetVariableStoreStatus ( IN VARIABLE_STORE_HEADER *VarStoreHeader ) { - if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) && + if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) || + CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) && VarStoreHeader->Format == VARIABLE_STORE_FORMATTED && VarStoreHeader->State == VARIABLE_STORE_HEALTHY ) { @@ -383,7 +464,10 @@ CompareWithValidVariable ( OUT VARIABLE_POINTER_TRACK *PtrTrack ) { - VOID *Point; + VOID *Point; + EFI_GUID *TempVendorGuid; + + TempVendorGuid = GetVendorGuidPtr (VariableHeader, StoreInfo->AuthFlag); if (VariableName[0] == 0) { PtrTrack->CurrPtr = Variable; @@ -394,14 +478,14 @@ CompareWithValidVariable ( // Instead we compare the GUID a UINT32 at a time and branch // on the first failed comparison. // - if ((((INT32 *) VendorGuid)[0] == ((INT32 *) &VariableHeader->VendorGuid)[0]) && - (((INT32 *) VendorGuid)[1] == ((INT32 *) &VariableHeader->VendorGuid)[1]) && - (((INT32 *) VendorGuid)[2] == ((INT32 *) &VariableHeader->VendorGuid)[2]) && - (((INT32 *) VendorGuid)[3] == ((INT32 *) &VariableHeader->VendorGuid)[3]) + if ((((INT32 *) VendorGuid)[0] == ((INT32 *) TempVendorGuid)[0]) && + (((INT32 *) VendorGuid)[1] == ((INT32 *) TempVendorGuid)[1]) && + (((INT32 *) VendorGuid)[2] == ((INT32 *) TempVendorGuid)[2]) && + (((INT32 *) VendorGuid)[3] == ((INT32 *) TempVendorGuid)[3]) ) { - ASSERT (NameSizeOfVariable (VariableHeader) != 0); - Point = (VOID *) GetVariableNamePtr (Variable); - if (CompareVariableName (StoreInfo, VariableName, Point, NameSizeOfVariable (VariableHeader))) { + ASSERT (NameSizeOfVariable (VariableHeader, StoreInfo->AuthFlag) != 0); + Point = (VOID *) GetVariableNamePtr (Variable, StoreInfo->AuthFlag); + if (CompareVariableName (StoreInfo, VariableName, Point, NameSizeOfVariable (VariableHeader, StoreInfo->AuthFlag))) { PtrTrack->CurrPtr = Variable; return EFI_SUCCESS; } @@ -435,12 +519,20 @@ GetVariableStore ( StoreInfo->IndexTable = NULL; StoreInfo->FtwLastWriteData = NULL; + StoreInfo->AuthFlag = FALSE; VariableStoreHeader = NULL; switch (Type) { case VariableStoreTypeHob: - GuidHob = GetFirstGuidHob (&gEfiVariableGuid); + GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid); if (GuidHob != NULL) { VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob); + StoreInfo->AuthFlag = TRUE; + } else { + GuidHob = GetFirstGuidHob (&gEfiVariableGuid); + if (GuidHob != NULL) { + VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob); + StoreInfo->AuthFlag = FALSE; + } } break; @@ -496,6 +588,8 @@ GetVariableStore ( VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength); + StoreInfo->AuthFlag = (BOOLEAN) (CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid)); + GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid); if (GuidHob != NULL) { StoreInfo->IndexTable = GET_GUID_HOB_DATA (GuidHob); @@ -551,9 +645,9 @@ GetVariableHeader ( return FALSE; } - // - // First assume variable header pointed by Variable is consecutive. - // + // + // First assume variable header pointed by Variable is consecutive. + // *VariableHeader = Variable; if (StoreInfo->FtwLastWriteData != NULL) { @@ -566,7 +660,7 @@ GetVariableHeader ( // return FALSE; } - if (((UINTN) Variable < (UINTN) TargetAddress) && (((UINTN) Variable + sizeof (VARIABLE_HEADER)) > (UINTN) TargetAddress)) { + if (((UINTN) Variable < (UINTN) TargetAddress) && (((UINTN) Variable + GetVariableHeaderSize (StoreInfo->AuthFlag)) > (UINTN) TargetAddress)) { // // Variable header pointed by Variable is inconsecutive, // create a guid hob to combine the two partial variable header content together. @@ -575,7 +669,7 @@ GetVariableHeader ( if (GuidHob != NULL) { *VariableHeader = (VARIABLE_HEADER *) GET_GUID_HOB_DATA (GuidHob); } else { - *VariableHeader = (VARIABLE_HEADER *) BuildGuidHob (&gEfiCallerIdGuid, sizeof (VARIABLE_HEADER)); + *VariableHeader = (VARIABLE_HEADER *) BuildGuidHob (&gEfiCallerIdGuid, GetVariableHeaderSize (StoreInfo->AuthFlag)); PartialHeaderSize = (UINTN) TargetAddress - (UINTN) Variable; // // Partial content is in NV storage. @@ -584,7 +678,7 @@ GetVariableHeader ( // // Another partial content is in spare block. // - CopyMem ((UINT8 *) *VariableHeader + PartialHeaderSize, (UINT8 *) (UINTN) SpareAddress, sizeof (VARIABLE_HEADER) - PartialHeaderSize); + CopyMem ((UINT8 *) *VariableHeader + PartialHeaderSize, (UINT8 *) (UINTN) SpareAddress, GetVariableHeaderSize (StoreInfo->AuthFlag) - PartialHeaderSize); } } } else { @@ -898,13 +992,13 @@ PeiGetVariable ( // // Get data size // - VarDataSize = DataSizeOfVariable (VariableHeader); + VarDataSize = DataSizeOfVariable (VariableHeader, StoreInfo.AuthFlag); if (*DataSize >= VarDataSize) { if (Data == NULL) { return EFI_INVALID_PARAMETER; } - GetVariableNameOrData (&StoreInfo, GetVariableDataPtr (Variable.CurrPtr, VariableHeader), VarDataSize, Data); + GetVariableNameOrData (&StoreInfo, GetVariableDataPtr (Variable.CurrPtr, VariableHeader, StoreInfo.AuthFlag), VarDataSize, Data); if (Attributes != NULL) { *Attributes = VariableHeader->Attributes; @@ -1035,8 +1129,8 @@ PeiGetNextVariableName ( // Status = FindVariableEx ( &StoreInfo, - GetVariableNamePtr (Variable.CurrPtr), - &VariableHeader->VendorGuid, + GetVariableNamePtr (Variable.CurrPtr, StoreInfo.AuthFlag), + GetVendorGuidPtr (VariableHeader, StoreInfo.AuthFlag), &VariablePtrTrack ); if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr != Variable.CurrPtr) { @@ -1053,8 +1147,8 @@ PeiGetNextVariableName ( ) { Status = FindVariableEx ( &StoreInfoForHob, - GetVariableNamePtr (Variable.CurrPtr), - &VariableHeader->VendorGuid, + GetVariableNamePtr (Variable.CurrPtr, StoreInfo.AuthFlag), + GetVendorGuidPtr (VariableHeader, StoreInfo.AuthFlag), &VariableInHob ); if (!EFI_ERROR (Status)) { @@ -1063,13 +1157,13 @@ PeiGetNextVariableName ( } } - VarNameSize = NameSizeOfVariable (VariableHeader); + VarNameSize = NameSizeOfVariable (VariableHeader, StoreInfo.AuthFlag); ASSERT (VarNameSize != 0); if (VarNameSize <= *VariableNameSize) { - GetVariableNameOrData (&StoreInfo, (UINT8 *) GetVariableNamePtr (Variable.CurrPtr), VarNameSize, (UINT8 *) VariableName); + GetVariableNameOrData (&StoreInfo, (UINT8 *) GetVariableNamePtr (Variable.CurrPtr, StoreInfo.AuthFlag), VarNameSize, (UINT8 *) VariableName); - CopyMem (VariableGuid, &VariableHeader->VendorGuid, sizeof (EFI_GUID)); + CopyMem (VariableGuid, GetVendorGuidPtr (VariableHeader, StoreInfo.AuthFlag), sizeof (EFI_GUID)); Status = EFI_SUCCESS; } else { diff --git a/MdeModulePkg/Universal/Variable/Pei/Variable.h b/MdeModulePkg/Universal/Variable/Pei/Variable.h index 55277b0..a37444b 100644 --- a/MdeModulePkg/Universal/Variable/Pei/Variable.h +++ b/MdeModulePkg/Universal/Variable/Pei/Variable.h @@ -1,9 +1,8 @@ /** @file - The internal header file includes the common header files, defines internal structure and functions used by PeiVariable module. -Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR> +Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR> 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 @@ -48,6 +47,7 @@ typedef struct { // in spare block. // FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData; + BOOLEAN AuthFlag; } VARIABLE_STORE_INFO; // diff --git a/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf b/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf index 6404562..72a440d 100644 --- a/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf +++ b/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf @@ -1,7 +1,9 @@ ## @file -# Implement ReadOnly Variable Services required by PEIM and install PEI ReadOnly Varaiable2 PPI. +# Implements ReadOnly Variable Services required by PEIM and installs PEI ReadOnly Varaiable2 PPI. # -# Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR> +# This module implements ReadOnly Variable Services required by PEIM and installs PEI ReadOnly Varaiable2 PPI. +# +# Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR> # 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 @@ -47,6 +49,9 @@ [Guids] ## CONSUMES ## GUID # Variable store header ## SOMETIMES_CONSUMES ## HOB + gEfiAuthenticatedVariableGuid + ## SOMETIMES_CONSUMES ## GUID # Variable store header + ## SOMETIMES_CONSUMES ## HOB gEfiVariableGuid ## SOMETIMES_PRODUCES ## HOB ## SOMETIMES_CONSUMES ## HOB -- 1.9.5.msysgit.0 ------------------------------------------------------------------------------ Monitor 25 network devices or servers for free with OpManager! OpManager is web-based network management software that monitors network devices and physical & virtual servers, alerts via email & sms for fault. Monitor 25 devices for free with no restriction. Download now http://ad.doubleclick.net/ddm/clk/292181274;119417398;o _______________________________________________ edk2-devel mailing list edk2-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/edk2-devel