+/**\r
+ Check if a Unicode character is a hexadecimal character.\r
+\r
+ This function checks if a Unicode character is a \r
+ hexadecimal character. The valid hexadecimal character is \r
+ L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
+\r
+\r
+ @param Char The character to check against.\r
+\r
+ @retval TRUE If the Char is a hexadecmial character.\r
+ @retval FALSE If the Char is not a hexadecmial character.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsHexaDecimalDigitCharacter (\r
+ IN CHAR16 Char\r
+ )\r
+{\r
+ return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
+}\r
+\r
+/**\r
+\r
+ This code checks if variable is hardware error record variable or not.\r
+\r
+ According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
+ and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
+\r
+ @param VariableName Pointer to variable name.\r
+ @param VendorGuid Variable Vendor Guid.\r
+\r
+ @retval TRUE Variable is hardware error record variable.\r
+ @retval FALSE Variable is not hardware error record variable.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsHwErrRecVariable (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid\r
+ )\r
+{\r
+ if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
+ (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
+ (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
+ !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
+ !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
+ !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
+ !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
+ return FALSE;\r
+ }\r
+\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ This code checks if variable guid is global variable guid first.\r
+ If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.\r
+\r
+ @param[in] VariableName Pointer to variable name.\r
+ @param[in] VendorGuid Variable Vendor Guid.\r
+ @param[in] Attributes Attributes of the variable.\r
+\r
+ @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.\r
+ @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CheckEfiGlobalVariable (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN UINT32 Attributes\r
+ )\r
+{\r
+ UINTN Index;\r
+ UINTN NameLength;\r
+\r
+ if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)){\r
+ //\r
+ // Try list 1, exactly match.\r
+ //\r
+ for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {\r
+ if ((StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) &&\r
+ (Attributes == 0 || Attributes == mGlobalVariableList[Index].Attributes)) {\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Try list 2.\r
+ //\r
+ NameLength = StrLen (VariableName) - 4;\r
+ for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {\r
+ if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&\r
+ (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&\r
+ IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&\r
+ IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&\r
+ IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&\r
+ IsHexaDecimalDigitCharacter (VariableName[NameLength + 3]) &&\r
+ (Attributes == 0 || Attributes == mGlobalVariableList2[Index].Attributes)) {\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+\r
+ DEBUG ((EFI_D_INFO, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid, VariableName, Attributes));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Mark a variable that will become read-only after leaving the DXE phase of execution.\r
+\r
+ @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
+ @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
+ @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
+\r
+ @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
+ as pending to be read-only.\r
+ @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
+ Or VariableName is an empty string.\r
+ @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
+ already been signaled.\r
+ @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VariableLockRequestToLock (\r
+ IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid\r
+ )\r
+{\r
+ VARIABLE_ENTRY *Entry;\r
+\r
+ if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (mEndOfDxe) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+\r
+ Entry = AllocateRuntimePool (sizeof (*Entry) + StrSize (VariableName));\r
+ if (Entry == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
+\r
+ AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
+\r
+ Entry->Name = (CHAR16 *) (Entry + 1);\r
+ StrCpy (Entry->Name, VariableName);\r
+ CopyGuid (&Entry->Guid, VendorGuid);\r
+ InsertTailList (&mLockedVariableList, &Entry->Link);\r
+\r
+ ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r