X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FVariable%2FRuntimeDxe%2FVariable.c;h=fca8d5380924449533b19572fef040d2d8ec9b60;hp=1cb0409e97f450b35b646b4f340ec4bbcd64fd38;hb=09808bd39b0c76559354253639766458ec24da79;hpb=b2c59ce83cf857cfe9868525526001a7944f3d33 diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c index 1cb0409e97..fca8d53809 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -16,8 +16,8 @@ VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow, integer overflow. It should also check attribute to avoid authentication bypass. -Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
-(C) Copyright 2015 Hewlett Packard Enterprise Development LP
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP
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 @@ -97,20 +97,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = { AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut; -/** - - SecureBoot Hook for auth variable update. - - @param[in] VariableName Name of Variable to be found. - @param[in] VendorGuid Variable vendor GUID. -**/ -VOID -EFIAPI -SecureBootHook ( - IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid - ); - /** Routine used to track statistical information about variable usage. The data is stored in the EFI system table so it can be accessed later. @@ -252,6 +238,8 @@ IsValidVariableHeader ( @param Buffer Pointer to the buffer from which data is written. @retval EFI_INVALID_PARAMETER Parameters not valid. + @retval EFI_UNSUPPORTED Fvb is a NULL for Non-Volatile variable update. + @retval EFI_OUT_OF_RESOURCES The remaining size is not enough. @retval EFI_SUCCESS Variable store successfully updated. **/ @@ -288,7 +276,7 @@ UpdateVariableStore ( // if (!Volatile) { if (Fvb == NULL) { - return EFI_INVALID_PARAMETER; + return EFI_UNSUPPORTED; } Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr); ASSERT_EFI_ERROR (Status); @@ -302,8 +290,8 @@ UpdateVariableStore ( DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase; } - if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) { - return EFI_INVALID_PARAMETER; + if ((DataPtr + DataSize) > ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) { + return EFI_OUT_OF_RESOURCES; } } else { // @@ -315,8 +303,8 @@ UpdateVariableStore ( DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase; } - if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) { - return EFI_INVALID_PARAMETER; + if ((DataPtr + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) { + return EFI_OUT_OF_RESOURCES; } // @@ -799,7 +787,7 @@ RecordVarErrorFlag ( // // Update the data in NV cache. // - *VarErrFlag = Flag; + *VarErrFlag = TempFlag; } } } @@ -1133,7 +1121,7 @@ Reclaim ( // Install the new variable if it is not NULL. // if (NewVariable != NULL) { - if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) { + if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) { // // No enough space to store the new variable. // @@ -1174,8 +1162,8 @@ Reclaim ( // If volatile variable store, just copy valid buffer. // SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff); - CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer)); - *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer); + CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer); + *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer; Status = EFI_SUCCESS; } else { // @@ -1186,7 +1174,7 @@ Reclaim ( (VARIABLE_STORE_HEADER *) ValidBuffer ); if (!EFI_ERROR (Status)) { - *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer); + *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer; mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize; mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize; mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize; @@ -1545,8 +1533,8 @@ GetLangFromSupportedLangCodes ( @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that contains a set of language codes in the format specified by Iso639Language. - @param[in] Iso639Language If TRUE, then all language codes are assumed to be - in ISO 639-2 format. If FALSE, then all language + @param[in] Iso639Language If not zero, then all language codes are assumed to be + in ISO 639-2 format. If zero, then all language codes are assumed to be in RFC 4646 language format @param[in] ... A variable argument list that contains pointers to Null-terminated ASCII strings that contain one or more @@ -1574,7 +1562,7 @@ CHAR8 * EFIAPI VariableGetBestLanguage ( IN CONST CHAR8 *SupportedLanguages, - IN BOOLEAN Iso639Language, + IN UINTN Iso639Language, ... ) { @@ -1600,7 +1588,7 @@ VariableGetBestLanguage ( // // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language // - if (!Iso639Language) { + if (Iso639Language == 0) { for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++); } @@ -1615,7 +1603,7 @@ VariableGetBestLanguage ( // // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages // - if (!Iso639Language) { + if (Iso639Language == 0) { // // Skip ';' characters in Supported // @@ -1637,13 +1625,13 @@ VariableGetBestLanguage ( if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) { VA_END (Args); - Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang; + Buffer = (Iso639Language != 0) ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang; Buffer[CompareLength] = '\0'; return CopyMem (Buffer, Supported, CompareLength); } } - if (Iso639Language) { + if (Iso639Language != 0) { // // If ISO 639 mode, then each language can only be tested once // @@ -1734,6 +1722,7 @@ CheckRemainingSpaceForConsistencyInternal ( TotalNeededSize += VariableEntry->VariableSize; VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *); } + VA_END (Args); if (RemainingVariableStorageSize >= TotalNeededSize) { // @@ -1778,6 +1767,7 @@ CheckRemainingSpaceForConsistencyInternal ( // // No enough space for Variable[Index]. // + VA_END (Args); return FALSE; } // @@ -1786,6 +1776,7 @@ CheckRemainingSpaceForConsistencyInternal ( RemainingVariableStorageSize -= VariableEntry->VariableSize; VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *); } + VA_END (Args); return TRUE; } @@ -2201,6 +2192,7 @@ UpdateVariable ( // go to delete this variable in variable HOB and // try to flush other variables from HOB to flash. // + UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE); FlushHobVariableToFlash (VariableName, VendorGuid); return EFI_SUCCESS; } @@ -2355,12 +2347,14 @@ UpdateVariable ( CopyMem (BufferForMerge, (UINT8 *) ((UINTN) CacheVariable->CurrPtr + DataOffset), DataSizeOfVariable (CacheVariable->CurrPtr)); // - // Set Max Common/Auth Variable Data Size as default MaxDataSize. + // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize. // if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) { MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset; - } else { + } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset; + } else { + MaxDataSize = mVariableModuleGlobal->MaxVolatileVariableSize - DataOffset; } // @@ -2784,7 +2778,8 @@ Done: @param Attributes Attribute value of the variable found. @param DataSize Size of Data found. If size is less than the data, this value contains the required size. - @param Data Data pointer. + @param Data The buffer to return the contents of the variable. May be NULL + with a zero DataSize in order to determine the size buffer needed. @return EFI_INVALID_PARAMETER Invalid parameter. @return EFI_SUCCESS Find the specified variable. @@ -2799,7 +2794,7 @@ VariableServiceGetVariable ( IN EFI_GUID *VendorGuid, OUT UINT32 *Attributes OPTIONAL, IN OUT UINTN *DataSize, - OUT VOID *Data + OUT VOID *Data OPTIONAL ) { EFI_STATUS Status; @@ -2810,6 +2805,10 @@ VariableServiceGetVariable ( return EFI_INVALID_PARAMETER; } + if (VariableName[0] == 0) { + return EFI_NOT_FOUND; + } + AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); @@ -2860,8 +2859,11 @@ Done: @param[in] VendorGuid Variable Vendor Guid. @param[out] VariablePtr Pointer to variable header address. - @return EFI_SUCCESS Find the specified variable. - @return EFI_NOT_FOUND Not found. + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and + GUID of an existing variable. **/ EFI_STATUS @@ -2881,6 +2883,19 @@ VariableServiceGetNextVariableInternal ( Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { + // + // For VariableName is an empty string, FindVariable() will try to find and return + // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND) + // as no any variable is found, still go to return the error (EFI_NOT_FOUND). + // + if (VariableName[0] != 0) { + // + // For VariableName is not an empty string, and FindVariable() returns error as + // VariableName and VendorGuid are not a name and GUID of an existing variable, + // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER. + // + Status = EFI_INVALID_PARAMETER; + } goto Done; } @@ -3001,14 +3016,22 @@ Done: Caution: This function may receive untrusted input. This function may be invoked in SMM mode. This function will do basic validation, before parse the data. - @param VariableNameSize Size of the variable name. + @param VariableNameSize The size of the VariableName buffer. The size must be large + enough to fit input string supplied in VariableName buffer. @param VariableName Pointer to variable name. @param VendorGuid Variable Vendor Guid. - @return EFI_INVALID_PARAMETER Invalid parameter. - @return EFI_SUCCESS Find the specified variable. - @return EFI_NOT_FOUND Not found. - @return EFI_BUFFER_TO_SMALL DataSize is too small for the result. + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result. + VariableNameSize has been updated with the size needed to complete the request. + @retval EFI_INVALID_PARAMETER VariableNameSize is NULL. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and + GUID of an existing variable. + @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of + the input VariableName buffer. **/ EFI_STATUS @@ -3020,6 +3043,7 @@ VariableServiceGetNextVariableName ( ) { EFI_STATUS Status; + UINTN MaxLen; UINTN VarNameSize; VARIABLE_HEADER *VariablePtr; @@ -3027,6 +3051,18 @@ VariableServiceGetNextVariableName ( return EFI_INVALID_PARAMETER; } + // + // Calculate the possible maximum length of name string, including the Null terminator. + // + MaxLen = *VariableNameSize / sizeof (CHAR16); + if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) { + // + // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer, + // follow spec to return EFI_INVALID_PARAMETER. + // + return EFI_INVALID_PARAMETER; + } + AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr); @@ -3102,8 +3138,11 @@ VariableServiceSetVariable ( // // Check for reserverd bit in variable attribute. + // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow + // the delete operation of common authenticated variable at user physical presence. + // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib // - if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) { + if ((Attributes & (~(EFI_VARIABLE_ATTRIBUTES_MASK | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS))) != 0) { return EFI_INVALID_PARAMETER; } @@ -3111,7 +3150,11 @@ VariableServiceSetVariable ( // Make sure if runtime bit is set, boot service bit is set also. // if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) { - return EFI_INVALID_PARAMETER; + if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) { + return EFI_UNSUPPORTED; + } else { + return EFI_INVALID_PARAMETER; + } } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) { if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) { // @@ -3134,15 +3177,16 @@ VariableServiceSetVariable ( // if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { - return EFI_INVALID_PARAMETER; + return EFI_UNSUPPORTED; } if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) { - if (DataSize < AUTHINFO_SIZE) { - // - // Try to write Authenticated Variable without AuthInfo. - // - return EFI_SECURITY_VIOLATION; + // + // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0. + // Maybe it's the delete operation of common authenticated variable at user physical presence. + // + if (DataSize != AUTHINFO_SIZE) { + return EFI_UNSUPPORTED; } PayloadSize = DataSize - AUTHINFO_SIZE; } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { @@ -3178,19 +3222,38 @@ VariableServiceSetVariable ( } else { // // The size of the VariableName, including the Unicode Null in bytes plus - // the DataSize is limited to maximum size of Max(Auth)VariableSize bytes. + // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes. // if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) { if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) { return EFI_INVALID_PARAMETER; } - } else { + } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) { return EFI_INVALID_PARAMETER; } + } else { + if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ()) { + return EFI_INVALID_PARAMETER; + } } } + // + // Special Handling for MOR Lock variable. + // + Status = SetVariableCheckHandlerMor (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize)); + if (Status == EFI_ALREADY_STARTED) { + // + // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor(). + // Variable driver can just return SUCCESS. + // + return EFI_SUCCESS; + } + if (EFI_ERROR (Status)) { + return Status; + } + Status = VarCheckLibSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize), mRequestSource); if (EFI_ERROR (Status)) { return Status; @@ -3344,12 +3407,14 @@ VariableServiceQueryVariableInfoInternal ( } // - // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size. + // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size. // if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) { *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize (); - } else { + } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize (); + } else { + *MaximumVariableSize = mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize (); } } @@ -3473,6 +3538,13 @@ VariableServiceQueryVariableInfo ( return EFI_INVALID_PARAMETER; } + if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) { + // + // Deprecated attribute, make this check as highest priority. + // + return EFI_UNSUPPORTED; + } + if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) { // // Make sure the Attributes combination is supported by the platform. @@ -3595,6 +3667,30 @@ GetNonVolatileMaxVariableSize ( } } +/** + Get maximum variable size, covering both non-volatile and volatile variables. + + @return Maximum variable size. + +**/ +UINTN +GetMaxVariableSize ( + VOID + ) +{ + UINTN MaxVariableSize; + + MaxVariableSize = GetNonVolatileMaxVariableSize(); + // + // The condition below fails implicitly if PcdMaxVolatileVariableSize equals + // the default zero value. + // + if (MaxVariableSize < PcdGet32 (PcdMaxVolatileVariableSize)) { + MaxVariableSize = PcdGet32 (PcdMaxVolatileVariableSize); + } + return MaxVariableSize; +} + /** Init non-volatile variable store. @@ -3626,6 +3722,8 @@ InitNonVolatileVariableStore ( UINT32 HwErrStorageSize; UINT32 MaxUserNvVariableSpaceSize; UINT32 BoottimeReservedNvVariableSpaceSize; + EFI_STATUS Status; + VOID *FtwProtocol; mVariableModuleGlobal->FvbInstance = NULL; @@ -3648,30 +3746,36 @@ InitNonVolatileVariableStore ( // CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize); + Status = GetFtwProtocol ((VOID **)&FtwProtocol); // - // Check the FTW last write data hob. + // If FTW protocol has been installed, no need to check FTW last write data hob. // - GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid); - if (GuidHob != NULL) { - FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob); - if (FtwLastWriteData->TargetAddress == NvStorageBase) { - DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress)); - // - // Copy the backed up NV storage data to the memory buffer from spare block. - // - CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize); - } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) && - (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) { - // - // Flash NV storage from the Offset is backed up in spare block. - // - BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase); - BackUpSize = NvStorageSize - BackUpOffset; - DEBUG ((EFI_D_INFO, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress)); - // - // Copy the partial backed up NV storage data to the memory buffer from spare block. - // - CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize); + if (EFI_ERROR (Status)) { + // + // Check the FTW last write data hob. + // + GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid); + if (GuidHob != NULL) { + FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob); + if (FtwLastWriteData->TargetAddress == NvStorageBase) { + DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress)); + // + // Copy the backed up NV storage data to the memory buffer from spare block. + // + CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize); + } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) && + (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) { + // + // Flash NV storage from the Offset is backed up in spare block. + // + BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase); + BackUpSize = NvStorageSize - BackUpOffset; + DEBUG ((EFI_D_INFO, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress)); + // + // Copy the partial backed up NV storage data to the memory buffer from spare block. + // + CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize); + } } } @@ -3686,8 +3790,8 @@ InitNonVolatileVariableStore ( return EFI_VOLUME_CORRUPTED; } - VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength); - VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength); + VariableStoreBase = (UINTN) FvHeader + FvHeader->HeaderLength; + VariableStoreLength = NvStorageSize - FvHeader->HeaderLength; mNvFvHeaderCache = FvHeader; mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase; @@ -3949,7 +4053,7 @@ VariableWriteServiceInitialize ( } if (!EFI_ERROR (Status)) { - for (Index = 0; Index < sizeof (mVariableEntryProperty) / sizeof (mVariableEntryProperty[0]); Index++) { + for (Index = 0; Index < ARRAY_SIZE (mVariableEntryProperty); Index++) { VariableEntry = &mVariableEntryProperty[Index]; Status = VarCheckLibVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty); ASSERT_EFI_ERROR (Status); @@ -3957,9 +4061,171 @@ VariableWriteServiceInitialize ( } ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); + + // + // Initialize MOR Lock variable. + // + MorLockInit (); + return Status; } +/** + Convert normal variable storage to the allocated auth variable storage. + + @param[in] NormalVarStorage Pointer to the normal variable storage header + + @retval the allocated auth variable storage +**/ +VOID * +ConvertNormalVarStorageToAuthVarStorage ( + VARIABLE_STORE_HEADER *NormalVarStorage + ) +{ + VARIABLE_HEADER *StartPtr; + UINT8 *NextPtr; + VARIABLE_HEADER *EndPtr; + UINTN AuthVarStroageSize; + AUTHENTICATED_VARIABLE_HEADER *AuthStartPtr; + VARIABLE_STORE_HEADER *AuthVarStorage; + + AuthVarStroageSize = sizeof (VARIABLE_STORE_HEADER); + // + // Set AuthFormat as FALSE for normal variable storage + // + mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE; + + // + // Calculate Auth Variable Storage Size + // + StartPtr = GetStartPointer (NormalVarStorage); + EndPtr = GetEndPointer (NormalVarStorage); + while (StartPtr < EndPtr) { + if (StartPtr->State == VAR_ADDED) { + AuthVarStroageSize = HEADER_ALIGN (AuthVarStroageSize); + AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER); + AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE (StartPtr->NameSize); + AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE (StartPtr->DataSize); + } + StartPtr = GetNextVariablePtr (StartPtr); + } + + // + // Allocate Runtime memory for Auth Variable Storage + // + AuthVarStorage = AllocateRuntimeZeroPool (AuthVarStroageSize); + ASSERT (AuthVarStorage != NULL); + if (AuthVarStorage == NULL) { + return NULL; + } + + // + // Copy Variable from Normal storage to Auth storage + // + StartPtr = GetStartPointer (NormalVarStorage); + EndPtr = GetEndPointer (NormalVarStorage); + AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) GetStartPointer (AuthVarStorage); + while (StartPtr < EndPtr) { + if (StartPtr->State == VAR_ADDED) { + AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (AuthStartPtr); + // + // Copy Variable Header + // + AuthStartPtr->StartId = StartPtr->StartId; + AuthStartPtr->State = StartPtr->State; + AuthStartPtr->Attributes = StartPtr->Attributes; + AuthStartPtr->NameSize = StartPtr->NameSize; + AuthStartPtr->DataSize = StartPtr->DataSize; + CopyGuid (&AuthStartPtr->VendorGuid, &StartPtr->VendorGuid); + // + // Copy Variable Name + // + NextPtr = (UINT8 *) (AuthStartPtr + 1); + CopyMem (NextPtr, GetVariableNamePtr (StartPtr), AuthStartPtr->NameSize); + // + // Copy Variable Data + // + NextPtr = NextPtr + AuthStartPtr->NameSize + GET_PAD_SIZE (AuthStartPtr->NameSize); + CopyMem (NextPtr, GetVariableDataPtr (StartPtr), AuthStartPtr->DataSize); + // + // Go to next variable + // + AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) (NextPtr + AuthStartPtr->DataSize + GET_PAD_SIZE (AuthStartPtr->DataSize)); + } + StartPtr = GetNextVariablePtr (StartPtr); + } + // + // Update Auth Storage Header + // + AuthVarStorage->Format = NormalVarStorage->Format; + AuthVarStorage->State = NormalVarStorage->State; + AuthVarStorage->Size = (UINT32)((UINTN)AuthStartPtr - (UINTN)AuthVarStorage); + CopyGuid (&AuthVarStorage->Signature, &gEfiAuthenticatedVariableGuid); + ASSERT (AuthVarStorage->Size <= AuthVarStroageSize); + + // + // Restore AuthFormat + // + mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE; + return AuthVarStorage; +} + +/** + Get HOB variable store. + + @param[out] VariableGuid NV variable store signature. + + @retval EFI_SUCCESS Function successfully executed. + @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource. + +**/ +EFI_STATUS +GetHobVariableStore ( + IN EFI_GUID *VariableGuid + ) +{ + VARIABLE_STORE_HEADER *VariableStoreHeader; + UINT64 VariableStoreLength; + EFI_HOB_GUID_TYPE *GuidHob; + BOOLEAN NeedConvertNormalToAuth; + + // + // Combinations supported: + // 1. Normal NV variable store + + // Normal HOB variable store + // 2. Auth NV variable store + + // Auth HOB variable store + // 3. Auth NV variable store + + // Normal HOB variable store (code will convert it to Auth Format) + // + NeedConvertNormalToAuth = FALSE; + GuidHob = GetFirstGuidHob (VariableGuid); + if (GuidHob == NULL && VariableGuid == &gEfiAuthenticatedVariableGuid) { + // + // Try getting it from normal variable HOB + // + GuidHob = GetFirstGuidHob (&gEfiVariableGuid); + NeedConvertNormalToAuth = TRUE; + } + if (GuidHob != NULL) { + VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob); + VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE); + if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) { + if (!NeedConvertNormalToAuth) { + mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader); + } else { + mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) ConvertNormalVarStorageToAuthVarStorage ((VOID *) VariableStoreHeader); + } + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) { + return EFI_OUT_OF_RESOURCES; + } + } else { + DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n")); + } + } + + return EFI_SUCCESS; +} /** Initializes variable store area for non-volatile and volatile variable. @@ -3975,10 +4241,7 @@ VariableCommonInitialize ( { EFI_STATUS Status; VARIABLE_STORE_HEADER *VolatileVariableStore; - VARIABLE_STORE_HEADER *VariableStoreHeader; - UINT64 VariableStoreLength; UINTN ScratchSize; - EFI_HOB_GUID_TYPE *GuidHob; EFI_GUID *VariableGuid; EFI_FIRMWARE_VOLUME_HEADER *NvFvHeader; @@ -4022,26 +4285,21 @@ VariableCommonInitialize ( // // Get HOB variable store. // - GuidHob = GetFirstGuidHob (VariableGuid); - if (GuidHob != NULL) { - VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob); - VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE)); - if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) { - mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader); - if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) { - FreePool (NvFvHeader); - FreePool (mVariableModuleGlobal); - return EFI_OUT_OF_RESOURCES; - } - } else { - DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n")); - } + Status = GetHobVariableStore (VariableGuid); + if (EFI_ERROR (Status)) { + FreePool (NvFvHeader); + FreePool (mVariableModuleGlobal); + return Status; } + mVariableModuleGlobal->MaxVolatileVariableSize = ((PcdGet32 (PcdMaxVolatileVariableSize) != 0) ? + PcdGet32 (PcdMaxVolatileVariableSize) : + mVariableModuleGlobal->MaxVariableSize + ); // // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data. // - ScratchSize = GetNonVolatileMaxVariableSize (); + ScratchSize = GetMaxVariableSize (); mVariableModuleGlobal->ScratchBufferSize = ScratchSize; VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize); if (VolatileVariableStore == NULL) {