+/**\r
+ Record variable error flag.\r
+\r
+ @param[in] Flag Variable error flag to record.\r
+ @param[in] VariableName Name of variable.\r
+ @param[in] VendorGuid Guid of variable.\r
+ @param[in] Attributes Attributes of the variable.\r
+ @param[in] VariableSize Size of the variable.\r
+\r
+**/\r
+VOID\r
+RecordVarErrorFlag (\r
+ IN VAR_ERROR_FLAG Flag,\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN UINT32 Attributes,\r
+ IN UINTN VariableSize\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ VARIABLE_POINTER_TRACK Variable;\r
+ VAR_ERROR_FLAG *VarErrFlag;\r
+ VAR_ERROR_FLAG TempFlag;\r
+\r
+ DEBUG_CODE (\r
+ DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));\r
+ if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {\r
+ if (AtRuntime ()) {\r
+ DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
+ } else {\r
+ DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
+ }\r
+ } else {\r
+ DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));\r
+ }\r
+ );\r
+\r
+ if (!mEndOfDxe) {\r
+ //\r
+ // Before EndOfDxe, just record the current boot variable error flag to local variable,\r
+ // and leave the variable error flag in NV flash as the last boot variable error flag.\r
+ // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash\r
+ // will be initialized to this local current boot variable error flag.\r
+ //\r
+ mCurrentBootVarErrFlag &= Flag;\r
+ return;\r
+ }\r
+\r
+ //\r
+ // Record error flag (it should have be initialized).\r
+ //\r
+ Status = FindVariable (\r
+ VAR_ERROR_FLAG_NAME,\r
+ &gEdkiiVarErrorFlagGuid,\r
+ &Variable,\r
+ &mVariableModuleGlobal->VariableGlobal,\r
+ FALSE\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);\r
+ TempFlag = *VarErrFlag;\r
+ TempFlag &= Flag;\r
+ if (TempFlag == *VarErrFlag) {\r
+ return;\r
+ }\r
+ Status = UpdateVariableStore (\r
+ &mVariableModuleGlobal->VariableGlobal,\r
+ FALSE,\r
+ FALSE,\r
+ mVariableModuleGlobal->FvbInstance,\r
+ (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
+ sizeof (TempFlag),\r
+ &TempFlag\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // Update the data in NV cache.\r
+ //\r
+ *VarErrFlag = Flag;\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ Initialize variable error flag.\r
+\r
+ Before EndOfDxe, the variable indicates the last boot variable error flag,\r
+ then it means the last boot variable error flag must be got before EndOfDxe.\r
+ After EndOfDxe, the variable indicates the current boot variable error flag,\r
+ then it means the current boot variable error flag must be got after EndOfDxe.\r
+\r
+**/\r
+VOID\r
+InitializeVarErrorFlag (\r
+ VOID\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ VARIABLE_POINTER_TRACK Variable;\r
+ VAR_ERROR_FLAG Flag;\r
+ VAR_ERROR_FLAG VarErrFlag;\r
+\r
+ if (!mEndOfDxe) {\r
+ return;\r
+ }\r
+\r
+ Flag = mCurrentBootVarErrFlag;\r
+ DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));\r
+\r
+ Status = FindVariable (\r
+ VAR_ERROR_FLAG_NAME,\r
+ &gEdkiiVarErrorFlagGuid,\r
+ &Variable,\r
+ &mVariableModuleGlobal->VariableGlobal,\r
+ FALSE\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));\r
+ if (VarErrFlag == Flag) {\r
+ return;\r
+ }\r
+ }\r
+\r
+ UpdateVariable (\r
+ VAR_ERROR_FLAG_NAME,\r
+ &gEdkiiVarErrorFlagGuid,\r
+ &Flag,\r
+ sizeof (Flag),\r
+ VARIABLE_ATTRIBUTE_NV_BS_RT,\r
+ 0,\r
+ 0,\r
+ &Variable,\r
+ NULL\r
+ );\r
+}\r
+\r
+/**\r
+ Is user variable?\r
+\r
+ @param[in] Variable Pointer to variable header.\r
+\r
+ @retval TRUE User variable.\r
+ @retval FALSE System variable.\r
+\r
+**/\r
+BOOLEAN\r
+IsUserVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ VAR_CHECK_VARIABLE_PROPERTY Property;\r
+\r
+ //\r
+ // Only after End Of Dxe, the variables belong to system variable are fixed.\r
+ // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
+ // then no need to check if the variable is user variable or not specially.\r
+ //\r
+ if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
+ if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
+ return TRUE;\r
+ }\r
+ }\r
+ return FALSE;\r
+}\r
+\r
+/**\r
+ Calculate common user variable total size.\r
+\r
+**/\r
+VOID\r
+CalculateCommonUserVariableTotalSize (\r
+ VOID\r
+ )\r
+{\r
+ VARIABLE_HEADER *Variable;\r
+ VARIABLE_HEADER *NextVariable;\r
+ UINTN VariableSize;\r
+ VAR_CHECK_VARIABLE_PROPERTY Property;\r
+\r
+ //\r
+ // Only after End Of Dxe, the variables belong to system variable are fixed.\r
+ // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
+ // then no need to calculate the common user variable total size specially.\r
+ //\r
+ if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
+ Variable = GetStartPointer (mNvVariableCache);\r
+ while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r
+ NextVariable = GetNextVariablePtr (Variable);\r
+ VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
+ if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+ if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
+ //\r
+ // No property, it is user variable.\r
+ //\r
+ mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
+ }\r
+ }\r
+\r
+ Variable = NextVariable;\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ Initialize variable quota.\r
+\r
+**/\r
+VOID\r
+InitializeVariableQuota (\r
+ VOID\r
+ )\r
+{\r
+ STATIC BOOLEAN Initialized;\r
+\r
+ if (!mEndOfDxe || Initialized) {\r
+ return;\r
+ }\r
+ Initialized = TRUE;\r
+\r
+ InitializeVarErrorFlag ();\r
+ CalculateCommonUserVariableTotalSize ();\r
+}\r
+\r