/** @file HII Library implementation that uses DXE protocols and services. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "InternalHiiLib.h" #define GUID_CONFIG_STRING_TYPE 0x00 #define NAME_CONFIG_STRING_TYPE 0x01 #define PATH_CONFIG_STRING_TYPE 0x02 #define ACTION_SET_DEFAUTL_VALUE 0x01 #define ACTION_VALIDATE_SETTING 0x02 #define HII_LIB_DEFAULT_VARSTORE_SIZE 0x200 typedef struct { LIST_ENTRY Entry; // Link to Block array UINT16 Offset; UINT16 Width; UINT8 OpCode; UINT8 Scope; } IFR_BLOCK_DATA; typedef struct { EFI_VARSTORE_ID VarStoreId; UINT16 Size; } IFR_VARSTORAGE_DATA; // // Template // GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR16 mConfigHdrTemplate[] = L"GUID=00000000000000000000000000000000&NAME=0000&PATH=00"; EFI_FORM_BROWSER2_PROTOCOL *mUefiFormBrowser2 = NULL; // // Template used to mark the end of a list of packages // GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_HII_PACKAGE_HEADER mEndOfPakageList = { sizeof (EFI_HII_PACKAGE_HEADER), EFI_HII_PACKAGE_END }; /** Extract Hii package list GUID for given HII handle. If HiiHandle could not be found in the HII database, then ASSERT. If Guid is NULL, then ASSERT. @param Handle Hii handle @param Guid Package list GUID @retval EFI_SUCCESS Successfully extract GUID from Hii database. **/ EFI_STATUS EFIAPI InternalHiiExtractGuidFromHiiHandle ( IN EFI_HII_HANDLE Handle, OUT EFI_GUID *Guid ) { EFI_STATUS Status; UINTN BufferSize; EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList; ASSERT (Guid != NULL); ASSERT (Handle != NULL); // // Get HII PackageList // BufferSize = 0; HiiPackageList = NULL; Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &BufferSize, HiiPackageList); ASSERT (Status != EFI_NOT_FOUND); if (Status == EFI_BUFFER_TOO_SMALL) { HiiPackageList = AllocatePool (BufferSize); ASSERT (HiiPackageList != NULL); Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &BufferSize, HiiPackageList); } if (EFI_ERROR (Status)) { FreePool (HiiPackageList); return Status; } // // Extract GUID // CopyGuid (Guid, &HiiPackageList->PackageListGuid); FreePool (HiiPackageList); return EFI_SUCCESS; } /** Registers a list of packages in the HII Database and returns the HII Handle associated with that registration. If an HII Handle has already been registered with the same PackageListGuid and DeviceHandle, then NULL is returned. If there are not enough resources to perform the registration, then NULL is returned. If an empty list of packages is passed in, then NULL is returned. If the size of the list of package is 0, then NULL is returned. The variable arguments are pointers which point to package header that defined by UEFI VFR compiler and StringGather tool. #pragma pack (push, 1) typedef struct { UINT32 BinaryLength; EFI_HII_PACKAGE_HEADER PackageHeader; } EDKII_AUTOGEN_PACKAGES_HEADER; #pragma pack (pop) @param[in] PackageListGuid The GUID of the package list. @param[in] DeviceHandle If not NULL, the Device Handle on which an instance of DEVICE_PATH_PROTOCOL is installed. This Device Handle uniquely defines the device that the added packages are associated with. @param[in] ... The variable argument list that contains pointers to packages terminated by a NULL. @retval NULL A HII Handle has already been registered in the HII Database with the same PackageListGuid and DeviceHandle. @retval NULL The HII Handle could not be created. @retval NULL An empty list of packages was passed in. @retval NULL All packages are empty. @retval Other The HII Handle associated with the newly registered package list. **/ EFI_HII_HANDLE EFIAPI HiiAddPackages ( IN CONST EFI_GUID *PackageListGuid, IN EFI_HANDLE DeviceHandle OPTIONAL, ... ) { EFI_STATUS Status; VA_LIST Args; UINT32 *Package; EFI_HII_PACKAGE_LIST_HEADER *PackageListHeader; EFI_HII_HANDLE HiiHandle; UINT32 Length; UINT8 *Data; ASSERT (PackageListGuid != NULL); // // Calculate the length of all the packages in the variable argument list // for (Length = 0, VA_START (Args, DeviceHandle); (Package = VA_ARG (Args, UINT32 *)) != NULL; ) { Length += (ReadUnaligned32 (Package) - sizeof (UINT32)); } VA_END (Args); // // If there are no packages in the variable argument list or all the packages // are empty, then return a NULL HII Handle // if (Length == 0) { return NULL; } // // Add the length of the Package List Header and the terminating Package Header // Length += sizeof (EFI_HII_PACKAGE_LIST_HEADER) + sizeof (EFI_HII_PACKAGE_HEADER); // // Allocate the storage for the entire Package List // PackageListHeader = AllocateZeroPool (Length); // // If the Package List can not be allocated, then return a NULL HII Handle // if (PackageListHeader == NULL) { return NULL; } // // Fill in the GUID and Length of the Package List Header // CopyGuid (&PackageListHeader->PackageListGuid, PackageListGuid); PackageListHeader->PackageLength = Length; // // Initialize a pointer to the beginning if the Package List data // Data = (UINT8 *)(PackageListHeader + 1); // // Copy the data from each package in the variable argument list // for (VA_START (Args, DeviceHandle); (Package = VA_ARG (Args, UINT32 *)) != NULL; ) { Length = ReadUnaligned32 (Package) - sizeof (UINT32); CopyMem (Data, Package + 1, Length); Data += Length; } VA_END (Args); // // Append a package of type EFI_HII_PACKAGE_END to mark the end of the package list // CopyMem (Data, &mEndOfPakageList, sizeof (mEndOfPakageList)); // // Register the package list with the HII Database // Status = gHiiDatabase->NewPackageList ( gHiiDatabase, PackageListHeader, DeviceHandle, &HiiHandle ); if (EFI_ERROR (Status)) { HiiHandle = NULL; } // // Free the allocated package list // FreePool (PackageListHeader); // // Return the new HII Handle // return HiiHandle; } /** Removes a package list from the HII database. If HiiHandle is NULL, then ASSERT. If HiiHandle is not a valid EFI_HII_HANDLE in the HII database, then ASSERT. @param[in] HiiHandle The handle that was previously registered in the HII database **/ VOID EFIAPI HiiRemovePackages ( IN EFI_HII_HANDLE HiiHandle ) { EFI_STATUS Status; ASSERT (HiiHandle != NULL); Status = gHiiDatabase->RemovePackageList (gHiiDatabase, HiiHandle); ASSERT_EFI_ERROR (Status); } /** Retrieves the array of all the HII Handles or the HII handles of a specific package list GUID in the HII Database. This array is terminated with a NULL HII Handle. This function allocates the returned array using AllocatePool(). The caller is responsible for freeing the array with FreePool(). @param[in] PackageListGuid An optional parameter that is used to request HII Handles associated with a specific Package List GUID. If this parameter is NULL, then all the HII Handles in the HII Database are returned. If this parameter is not NULL, then zero or more HII Handles associated with PackageListGuid are returned. @retval NULL No HII handles were found in the HII database @retval NULL The array of HII Handles could not be retrieved @retval Other A pointer to the NULL terminated array of HII Handles **/ EFI_HII_HANDLE * EFIAPI HiiGetHiiHandles ( IN CONST EFI_GUID *PackageListGuid OPTIONAL ) { EFI_STATUS Status; UINTN HandleBufferLength; EFI_HII_HANDLE TempHiiHandleBuffer; EFI_HII_HANDLE *HiiHandleBuffer; EFI_GUID Guid; UINTN Index1; UINTN Index2; // // Retrieve the size required for the buffer of all HII handles. // HandleBufferLength = 0; Status = gHiiDatabase->ListPackageLists ( gHiiDatabase, EFI_HII_PACKAGE_TYPE_ALL, NULL, &HandleBufferLength, &TempHiiHandleBuffer ); // // If ListPackageLists() returns EFI_SUCCESS for a zero size, // then there are no HII handles in the HII database. If ListPackageLists() // returns an error other than EFI_BUFFER_TOO_SMALL, then there are no HII // handles in the HII database. // if (Status != EFI_BUFFER_TOO_SMALL) { // // Return NULL if the size can not be retrieved, or if there are no HII // handles in the HII Database // return NULL; } // // Allocate the array of HII handles to hold all the HII Handles and a NULL terminator // HiiHandleBuffer = AllocateZeroPool (HandleBufferLength + sizeof (EFI_HII_HANDLE)); if (HiiHandleBuffer == NULL) { // // Return NULL if allocation fails. // return NULL; } // // Retrieve the array of HII Handles in the HII Database // Status = gHiiDatabase->ListPackageLists ( gHiiDatabase, EFI_HII_PACKAGE_TYPE_ALL, NULL, &HandleBufferLength, HiiHandleBuffer ); if (EFI_ERROR (Status)) { // // Free the buffer and return NULL if the HII handles can not be retrieved. // FreePool (HiiHandleBuffer); return NULL; } if (PackageListGuid == NULL) { // // Return the NULL terminated array of HII handles in the HII Database // return HiiHandleBuffer; } else { for (Index1 = 0, Index2 = 0; HiiHandleBuffer[Index1] != NULL; Index1++) { Status = InternalHiiExtractGuidFromHiiHandle (HiiHandleBuffer[Index1], &Guid); ASSERT_EFI_ERROR (Status); if (CompareGuid (&Guid, PackageListGuid)) { HiiHandleBuffer[Index2++] = HiiHandleBuffer[Index1]; } } if (Index2 > 0) { HiiHandleBuffer[Index2] = NULL; return HiiHandleBuffer; } else { FreePool (HiiHandleBuffer); return NULL; } } } /** This function allows a caller to extract the form set opcode form the Hii Handle. The returned buffer is allocated using AllocatePool().The caller is responsible for freeing the allocated buffer using FreePool(). @param Handle The HII handle. @param Buffer On return, points to a pointer which point to the buffer that contain the formset opcode. @param BufferSize On return, points to the length of the buffer. @retval EFI_OUT_OF_RESOURCES No enough memory resource is allocated. @retval EFI_NOT_FOUND Can't find the package data for the input Handle. @retval EFI_INVALID_PARAMETER The input parameters are not correct. @retval EFI_SUCCESS Get the formset opcode from the hii handle successfully. **/ EFI_STATUS EFIAPI HiiGetFormSetFromHiiHandle ( IN EFI_HII_HANDLE Handle, OUT EFI_IFR_FORM_SET **Buffer, OUT UINTN *BufferSize ) { EFI_STATUS Status; UINTN PackageListSize; UINTN TempSize; EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList; UINT8 *Package; UINT8 *OpCodeData; UINT8 *FormSetBuffer; UINT8 *TempBuffer; UINT32 Offset; UINT32 Offset2; UINT32 PackageListLength; EFI_HII_PACKAGE_HEADER PackageHeader; TempSize = 0; FormSetBuffer = NULL; TempBuffer = NULL; // // Get HII PackageList // PackageListSize = 0; HiiPackageList = NULL; Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &PackageListSize, HiiPackageList); if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) { return Status; } HiiPackageList = AllocatePool (PackageListSize); if (HiiPackageList == NULL) { return EFI_OUT_OF_RESOURCES; } Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &PackageListSize, HiiPackageList); ASSERT_EFI_ERROR (Status); // // Get Form package from this HII package List // Status = EFI_NOT_FOUND; Offset = sizeof (EFI_HII_PACKAGE_LIST_HEADER); PackageListLength = ReadUnaligned32 (&HiiPackageList->PackageLength); while (Offset < PackageListLength) { Package = ((UINT8 *)HiiPackageList) + Offset; CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER)); Offset += PackageHeader.Length; if (PackageHeader.Type != EFI_HII_PACKAGE_FORMS) { continue; } // // Search FormSet Opcode in this Form Package // Offset2 = sizeof (EFI_HII_PACKAGE_HEADER); while (Offset2 < PackageHeader.Length) { OpCodeData = Package + Offset2; Offset2 += ((EFI_IFR_OP_HEADER *)OpCodeData)->Length; if (((EFI_IFR_OP_HEADER *)OpCodeData)->OpCode != EFI_IFR_FORM_SET_OP) { continue; } if (FormSetBuffer != NULL) { TempBuffer = ReallocatePool ( TempSize, TempSize + ((EFI_IFR_OP_HEADER *)OpCodeData)->Length, FormSetBuffer ); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; } CopyMem (TempBuffer + TempSize, OpCodeData, ((EFI_IFR_OP_HEADER *)OpCodeData)->Length); FormSetBuffer = NULL; } else { TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *)OpCodeData)->Length); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; } CopyMem (TempBuffer, OpCodeData, ((EFI_IFR_OP_HEADER *)OpCodeData)->Length); } TempSize += ((EFI_IFR_OP_HEADER *)OpCodeData)->Length; FormSetBuffer = TempBuffer; Status = EFI_SUCCESS; // // One form package has one formset, exit current form package to search other form package in the packagelist. // break; } } Done: FreePool (HiiPackageList); *BufferSize = TempSize; *Buffer = (EFI_IFR_FORM_SET *)FormSetBuffer; return Status; } /** Converts all hex dtring characters in range ['A'..'F'] to ['a'..'f'] for hex digits that appear between a '=' and a '&' in a config string. If ConfigString is NULL, then ASSERT(). @param[in] ConfigString Pointer to a Null-terminated Unicode string. @return Pointer to the Null-terminated Unicode result string. **/ EFI_STRING EFIAPI InternalHiiLowerConfigString ( IN EFI_STRING ConfigString ) { EFI_STRING String; BOOLEAN Lower; ASSERT (ConfigString != NULL); // // Convert all hex digits in range [A-F] in the configuration header to [a-f] // for (String = ConfigString, Lower = FALSE; *String != L'\0'; String++) { if (*String == L'=') { Lower = TRUE; } else if (*String == L'&') { Lower = FALSE; } else if (Lower && (*String >= L'A') && (*String <= L'F')) { *String = (CHAR16)(*String - L'A' + L'a'); } } return ConfigString; } /** Uses the BlockToConfig() service of the Config Routing Protocol to convert and a buffer to a If ConfigRequest is NULL, then ASSERT(). If Block is NULL, then ASSERT(). @param[in] ConfigRequest Pointer to a Null-terminated Unicode string. @param[in] Block Pointer to a block of data. @param[in] BlockSize The zie, in bytes, of Block. @retval NULL The string could not be generated. @retval Other Pointer to the Null-terminated Unicode string. **/ EFI_STRING EFIAPI InternalHiiBlockToConfig ( IN CONST EFI_STRING ConfigRequest, IN CONST UINT8 *Block, IN UINTN BlockSize ) { EFI_STATUS Status; EFI_STRING ConfigResp; CHAR16 *Progress; ASSERT (ConfigRequest != NULL); ASSERT (Block != NULL); // // Convert to // Status = gHiiConfigRouting->BlockToConfig ( gHiiConfigRouting, ConfigRequest, Block, BlockSize, &ConfigResp, &Progress ); if (EFI_ERROR (Status)) { return NULL; } return ConfigResp; } /** Uses the BrowserCallback() service of the Form Browser Protocol to retrieve or set uncommitted data. If sata i being retrieved, then the buffer is allocated using AllocatePool(). The caller is then responsible for freeing the buffer using FreePool(). @param[in] VariableGuid Pointer to an EFI_GUID structure. This is an optional parameter that may be NULL. @param[in] VariableName Pointer to a Null-terminated Unicode string. This is an optional parameter that may be NULL. @param[in] SetResultsData If not NULL, then this parameter specified the buffer of uncommited data to set. If this parameter is NULL, then the caller is requesting to get the uncommited data from the Form Browser. @retval NULL The uncommitted data could not be retrieved. @retval Other A pointer to a buffer containing the uncommitted data. **/ EFI_STRING EFIAPI InternalHiiBrowserCallback ( IN CONST EFI_GUID *VariableGuid OPTIONAL, IN CONST CHAR16 *VariableName OPTIONAL, IN CONST EFI_STRING SetResultsData OPTIONAL ) { EFI_STATUS Status; UINTN ResultsDataSize; EFI_STRING ResultsData; CHAR16 TempResultsData; // // Locate protocols // if (mUefiFormBrowser2 == NULL) { Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **)&mUefiFormBrowser2); if (EFI_ERROR (Status) || (mUefiFormBrowser2 == NULL)) { return NULL; } } ResultsDataSize = 0; if (SetResultsData != NULL) { // // Request to to set data in the uncommitted browser state information // ResultsData = SetResultsData; } else { // // Retrieve the length of the buffer required ResultsData from the Browser Callback // Status = mUefiFormBrowser2->BrowserCallback ( mUefiFormBrowser2, &ResultsDataSize, &TempResultsData, TRUE, VariableGuid, VariableName ); if (!EFI_ERROR (Status)) { // // No Resluts Data, only allocate one char for '\0' // ResultsData = AllocateZeroPool (sizeof (CHAR16)); return ResultsData; } if (Status != EFI_BUFFER_TOO_SMALL) { return NULL; } // // Allocate the ResultsData buffer // ResultsData = AllocateZeroPool (ResultsDataSize); if (ResultsData == NULL) { return NULL; } } // // Retrieve or set the ResultsData from the Browser Callback // Status = mUefiFormBrowser2->BrowserCallback ( mUefiFormBrowser2, &ResultsDataSize, ResultsData, (BOOLEAN)(SetResultsData == NULL), VariableGuid, VariableName ); if (EFI_ERROR (Status)) { return NULL; } return ResultsData; } /** Allocates and returns a Null-terminated Unicode string using routing information that includes a GUID, an optional Unicode string name, and a device path. The string returned is allocated with AllocatePool(). The caller is responsible for freeing the allocated string with FreePool(). The format of a is as follows: GUID=32&NAME=NameLength&PATH=DevicePathSize @param[in] Guid Pointer to an EFI_GUID that is the routing information GUID. Each of the 16 bytes in Guid is converted to a 2 Unicode character hexadecimal string. This is an optional parameter that may be NULL. @param[in] Name Pointer to a Null-terminated Unicode string that is the routing information NAME. This is an optional parameter that may be NULL. Each 16-bit Unicode character in Name is converted to a 4 character Unicode hexadecimal string. @param[in] DriverHandle The driver handle which supports a Device Path Protocol that is the routing information PATH. Each byte of the Device Path associated with DriverHandle is converted to a 2 Unicode character hexadecimal string. @retval NULL DriverHandle does not support the Device Path Protocol. @retval Other A pointer to the Null-terminate Unicode string **/ EFI_STRING EFIAPI HiiConstructConfigHdr ( IN CONST EFI_GUID *Guid OPTIONAL, IN CONST CHAR16 *Name OPTIONAL, IN EFI_HANDLE DriverHandle ) { UINTN NameLength; EFI_DEVICE_PATH_PROTOCOL *DevicePath; UINTN DevicePathSize; CHAR16 *String; CHAR16 *ReturnString; UINTN Index; UINT8 *Buffer; UINTN MaxLen; // // Compute the length of Name in Unicode characters. // If Name is NULL, then the length is 0. // NameLength = 0; if (Name != NULL) { NameLength = StrLen (Name); } DevicePath = NULL; DevicePathSize = 0; // // Retrieve DevicePath Protocol associated with DriverHandle // if (DriverHandle != NULL) { DevicePath = DevicePathFromHandle (DriverHandle); if (DevicePath == NULL) { return NULL; } // // Compute the size of the device path in bytes // DevicePathSize = GetDevicePathSize (DevicePath); } // // GUID=32&NAME=NameLength&PATH=DevicePathSize // | 5 | sizeof (EFI_GUID) * 2 | 6 | NameStrLen*4 | 6 | DevicePathSize * 2 | 1 | // MaxLen = 5 + sizeof (EFI_GUID) * 2 + 6 + NameLength * 4 + 6 + DevicePathSize * 2 + 1; String = AllocateZeroPool (MaxLen * sizeof (CHAR16)); if (String == NULL) { return NULL; } // // Start with L"GUID=" // StrCpyS (String, MaxLen, L"GUID="); ReturnString = String; String += StrLen (String); if (Guid != NULL) { // // Append Guid converted to 32 // for (Index = 0, Buffer = (UINT8 *)Guid; Index < sizeof (EFI_GUID); Index++) { UnicodeValueToStringS ( String, MaxLen * sizeof (CHAR16) - ((UINTN)String - (UINTN)ReturnString), PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2 ); String += StrnLenS (String, MaxLen - ((UINTN)String - (UINTN)ReturnString) / sizeof (CHAR16)); } } // // Append L"&NAME=" // StrCatS (ReturnString, MaxLen, L"&NAME="); String += StrLen (String); if (Name != NULL) { // // Append Name converted to NameLength // for ( ; *Name != L'\0'; Name++) { UnicodeValueToStringS ( String, sizeof (CHAR16) * MaxLen - ((UINTN)String - (UINTN)ReturnString), PREFIX_ZERO | RADIX_HEX, *Name, 4 ); String += StrnLenS (String, MaxLen - ((UINTN)String - (UINTN)ReturnString) / sizeof (CHAR16)); } } // // Append L"&PATH=" // StrCatS (ReturnString, MaxLen, L"&PATH="); String += StrLen (String); // // Append the device path associated with DriverHandle converted to DevicePathSize // for (Index = 0, Buffer = (UINT8 *)DevicePath; Index < DevicePathSize; Index++) { UnicodeValueToStringS ( String, sizeof (CHAR16) * MaxLen - ((UINTN)String - (UINTN)ReturnString), PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2 ); String += StrnLenS (String, MaxLen - ((UINTN)String - (UINTN)ReturnString) / sizeof (CHAR16)); } // // Null terminate the Unicode string // *String = L'\0'; // // Convert all hex digits in range [A-F] in the configuration header to [a-f] // return InternalHiiLowerConfigString (ReturnString); } /** Convert the hex UNICODE encoding string of UEFI GUID, NAME or device path to binary buffer from . This is a internal function. @param String UEFI configuration string. @param Flag Flag specifies what type buffer will be retrieved. @param Buffer Binary of Guid, Name or Device path. @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid. @retval EFI_OUT_OF_RESOURCES Lake of resources to store neccesary structures. @retval EFI_SUCCESS The buffer data is retrieved and translated to binary format. **/ EFI_STATUS InternalHiiGetBufferFromString ( IN EFI_STRING String, IN UINT8 Flag, OUT UINT8 **Buffer ) { UINTN Length; EFI_STRING ConfigHdr; CHAR16 *StringPtr; UINT8 *DataBuffer; CHAR16 TemStr[5]; UINTN Index; UINT8 DigitUint8; if ((String == NULL) || (Buffer == NULL)) { return EFI_INVALID_PARAMETER; } DataBuffer = NULL; StringPtr = NULL; ConfigHdr = String; // // The content between 'GUID', 'NAME', 'PATH' of and '&' of next element // or '\0' (end of configuration string) is the UNICODE %02x bytes encoding string. // for (Length = 0; *String != 0 && *String != L'&'; String++, Length++) { } switch (Flag) { case GUID_CONFIG_STRING_TYPE: case PATH_CONFIG_STRING_TYPE: // // The data in is encoded as hex UNICODE %02x bytes in the same order // as the device path and Guid resides in RAM memory. // Translate the data into binary. // DataBuffer = (UINT8 *)AllocateZeroPool ((Length + 1) / 2); if (DataBuffer == NULL) { return EFI_OUT_OF_RESOURCES; } // // Convert binary byte one by one // ZeroMem (TemStr, sizeof (TemStr)); for (Index = 0; Index < Length; Index++) { TemStr[0] = ConfigHdr[Index]; DigitUint8 = (UINT8)StrHexToUint64 (TemStr); if ((Index & 1) == 0) { DataBuffer[Index/2] = DigitUint8; } else { DataBuffer[Index/2] = (UINT8)((DataBuffer[Index/2] << 4) + DigitUint8); } } *Buffer = DataBuffer; break; case NAME_CONFIG_STRING_TYPE: // // Convert Config String to Unicode String, e.g. "0041004200430044" => "ABCD" // // // Add the tailling char L'\0' // DataBuffer = (UINT8 *)AllocateZeroPool ((Length/4 + 1) * sizeof (CHAR16)); if (DataBuffer == NULL) { return EFI_OUT_OF_RESOURCES; } // // Convert character one by one // StringPtr = (CHAR16 *)DataBuffer; ZeroMem (TemStr, sizeof (TemStr)); for (Index = 0; Index < Length; Index += 4) { StrnCpyS (TemStr, sizeof (TemStr) / sizeof (CHAR16), ConfigHdr + Index, 4); StringPtr[Index/4] = (CHAR16)StrHexToUint64 (TemStr); } // // Add tailing L'\0' character // StringPtr[Index/4] = L'\0'; *Buffer = DataBuffer; break; default: return EFI_INVALID_PARAMETER; } return EFI_SUCCESS; } /** This function checks VarOffset and VarWidth is in the block range. @param BlockArray The block array is to be checked. @param VarOffset Offset of var to the structure @param VarWidth Width of var. @retval TRUE This Var is in the block range. @retval FALSE This Var is not in the block range. **/ BOOLEAN BlockArrayCheck ( IN IFR_BLOCK_DATA *BlockArray, IN UINT16 VarOffset, IN UINT16 VarWidth ) { LIST_ENTRY *Link; IFR_BLOCK_DATA *BlockData; // // No Request Block array, all vars are got. // if (BlockArray == NULL) { return TRUE; } // // Check the input var is in the request block range. // for (Link = BlockArray->Entry.ForwardLink; Link != &BlockArray->Entry; Link = Link->ForwardLink) { BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry); if ((VarOffset >= BlockData->Offset) && ((VarOffset + VarWidth) <= (BlockData->Offset + BlockData->Width))) { return TRUE; } } return FALSE; } /** Get the value of in format, i.e. the value of OFFSET or WIDTH or VALUE. ::= 'OFFSET='&'WIDTH='&'VALUE'= @param ValueString String in format and points to the first character of . @param ValueData The output value. Caller takes the responsibility to free memory. @param ValueLength Length of the , in characters. @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary structures. @retval EFI_SUCCESS Value of is outputted in Number successfully. **/ EFI_STATUS EFIAPI InternalHiiGetValueOfNumber ( IN EFI_STRING ValueString, OUT UINT8 **ValueData, OUT UINTN *ValueLength ) { EFI_STRING StringPtr; UINTN Length; UINT8 *Buf; UINT8 DigitUint8; UINTN Index; CHAR16 TemStr[2]; ASSERT (ValueString != NULL && ValueData != NULL && ValueLength != NULL); ASSERT (*ValueString != L'\0'); // // Get the length of value string // StringPtr = ValueString; while (*StringPtr != L'\0' && *StringPtr != L'&') { StringPtr++; } Length = StringPtr - ValueString; // // Allocate buffer to store the value // Buf = (UINT8 *)AllocateZeroPool ((Length + 1) / 2); if (Buf == NULL) { return EFI_OUT_OF_RESOURCES; } // // Convert character one by one to the value buffer // ZeroMem (TemStr, sizeof (TemStr)); for (Index = 0; Index < Length; Index++) { TemStr[0] = ValueString[Length - Index - 1]; DigitUint8 = (UINT8)StrHexToUint64 (TemStr); if ((Index & 1) == 0) { Buf[Index/2] = DigitUint8; } else { Buf[Index/2] = (UINT8)((DigitUint8 << 4) + Buf[Index/2]); } } // // Set the converted value and string length. // *ValueData = Buf; *ValueLength = Length; return EFI_SUCCESS; } /** Get value from config request resp string. @param ConfigElement ConfigResp string contains the current setting. @param VarName The variable name which need to get value. @param VarValue The return value. @retval EFI_SUCCESS Get the value for the VarName @retval EFI_OUT_OF_RESOURCES The memory is not enough. **/ EFI_STATUS GetValueFromRequest ( IN CHAR16 *ConfigElement, IN CHAR16 *VarName, OUT UINT64 *VarValue ) { UINT8 *TmpBuffer; CHAR16 *StringPtr; UINTN Length; EFI_STATUS Status; // // Find VarName related string. // StringPtr = StrStr (ConfigElement, VarName); ASSERT (StringPtr != NULL); // // Skip the "VarName=" string // StringPtr += StrLen (VarName) + 1; // // Get Offset // Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length); if (EFI_ERROR (Status)) { return Status; } *VarValue = 0; CopyMem (VarValue, TmpBuffer, (((Length + 1) / 2) < sizeof (UINT64)) ? ((Length + 1) / 2) : sizeof (UINT64)); FreePool (TmpBuffer); return EFI_SUCCESS; } /** This internal function parses IFR data to validate current setting. Base on the NameValueType, if it is TRUE, RequestElement and HiiHandle is valid; else the VarBuffer and CurrentBlockArray is valid. @param HiiPackageList Point to Hii package list. @param PackageListLength The length of the pacakge. @param VarGuid Guid of the buffer storage. @param VarName Name of the buffer storage. @param VarBuffer The data buffer for the storage. @param CurrentBlockArray The block array from the config Requst string. @param RequestElement The config string for this storage. @param HiiHandle The HiiHandle for this formset. @param NameValueType Whether current storage is name/value varstore or not. @retval EFI_SUCCESS The current setting is valid. @retval EFI_OUT_OF_RESOURCES The memory is not enough. @retval EFI_INVALID_PARAMETER The config string or the Hii package is invalid. **/ EFI_STATUS ValidateQuestionFromVfr ( IN EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList, IN UINTN PackageListLength, IN EFI_GUID *VarGuid, IN CHAR16 *VarName, IN UINT8 *VarBuffer, IN IFR_BLOCK_DATA *CurrentBlockArray, IN CHAR16 *RequestElement, IN EFI_HII_HANDLE HiiHandle, IN BOOLEAN NameValueType ) { IFR_BLOCK_DATA VarBlockData; UINT16 Offset; UINT16 Width; UINT64 VarValue; EFI_IFR_TYPE_VALUE TmpValue; EFI_STATUS Status; EFI_HII_PACKAGE_HEADER PackageHeader; UINT32 PackageOffset; UINT8 *PackageData; UINTN IfrOffset; EFI_IFR_OP_HEADER *IfrOpHdr; EFI_IFR_VARSTORE *IfrVarStore; EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueStore; EFI_IFR_VARSTORE_EFI *IfrEfiVarStore; IFR_VARSTORAGE_DATA VarStoreData; EFI_IFR_ONE_OF *IfrOneOf; EFI_IFR_NUMERIC *IfrNumeric; EFI_IFR_ONE_OF_OPTION *IfrOneOfOption; EFI_IFR_CHECKBOX *IfrCheckBox; EFI_IFR_STRING *IfrString; CHAR8 *VarStoreName; UINTN Index; CHAR16 *QuestionName; CHAR16 *StringPtr; UINT16 BitOffset; UINT16 BitWidth; UINT16 TotalBits; UINTN StartBit; UINTN EndBit; BOOLEAN QuestionReferBitField; UINT32 BufferValue; // // Initialize the local variables. // Index = 0; VarStoreName = NULL; Status = EFI_SUCCESS; VarValue = 0; IfrVarStore = NULL; IfrNameValueStore = NULL; IfrEfiVarStore = NULL; ZeroMem (&VarStoreData, sizeof (IFR_VARSTORAGE_DATA)); ZeroMem (&VarBlockData, sizeof (VarBlockData)); BitOffset = 0; BitWidth = 0; QuestionReferBitField = FALSE; // // Check IFR value is in block data, then Validate Value // PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER); while (PackageOffset < PackageListLength) { CopyMem (&PackageHeader, (UINT8 *)HiiPackageList + PackageOffset, sizeof (PackageHeader)); // // Parse IFR opcode from the form package. // if (PackageHeader.Type == EFI_HII_PACKAGE_FORMS) { IfrOffset = sizeof (PackageHeader); PackageData = (UINT8 *)HiiPackageList + PackageOffset; while (IfrOffset < PackageHeader.Length) { IfrOpHdr = (EFI_IFR_OP_HEADER *)(PackageData + IfrOffset); // // Validate current setting to the value built in IFR opcode // switch (IfrOpHdr->OpCode) { case EFI_IFR_VARSTORE_OP: // // VarStoreId has been found. No further found. // if (VarStoreData.VarStoreId != 0) { break; } // // Find the matched VarStoreId to the input VarGuid and VarName // IfrVarStore = (EFI_IFR_VARSTORE *)IfrOpHdr; if (CompareGuid ((EFI_GUID *)(VOID *)&IfrVarStore->Guid, VarGuid)) { VarStoreName = (CHAR8 *)IfrVarStore->Name; for (Index = 0; VarStoreName[Index] != 0; Index++) { if ((CHAR16)VarStoreName[Index] != VarName[Index]) { break; } } // // The matched VarStore is found. // if ((VarStoreName[Index] != 0) || (VarName[Index] != 0)) { IfrVarStore = NULL; } } else { IfrVarStore = NULL; } if (IfrVarStore != NULL) { VarStoreData.VarStoreId = IfrVarStore->VarStoreId; VarStoreData.Size = IfrVarStore->Size; } break; case EFI_IFR_VARSTORE_NAME_VALUE_OP: // // VarStoreId has been found. No further found. // if (VarStoreData.VarStoreId != 0) { break; } // // Find the matched VarStoreId to the input VarGuid // IfrNameValueStore = (EFI_IFR_VARSTORE_NAME_VALUE *)IfrOpHdr; if (!CompareGuid ((EFI_GUID *)(VOID *)&IfrNameValueStore->Guid, VarGuid)) { IfrNameValueStore = NULL; } if (IfrNameValueStore != NULL) { VarStoreData.VarStoreId = IfrNameValueStore->VarStoreId; } break; case EFI_IFR_VARSTORE_EFI_OP: // // VarStore is found. Don't need to search any more. // if (VarStoreData.VarStoreId != 0) { break; } IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *)IfrOpHdr; // // If the length is small than the structure, this is from old efi // varstore definition. Old efi varstore get config directly from // GetVariable function. // if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) { break; } if (CompareGuid ((EFI_GUID *)(VOID *)&IfrEfiVarStore->Guid, VarGuid)) { VarStoreName = (CHAR8 *)IfrEfiVarStore->Name; for (Index = 0; VarStoreName[Index] != 0; Index++) { if ((CHAR16)VarStoreName[Index] != VarName[Index]) { break; } } // // The matched VarStore is found. // if ((VarStoreName[Index] != 0) || (VarName[Index] != 0)) { IfrEfiVarStore = NULL; } } else { IfrEfiVarStore = NULL; } if (IfrEfiVarStore != NULL) { // // Find the matched VarStore // VarStoreData.VarStoreId = IfrEfiVarStore->VarStoreId; VarStoreData.Size = IfrEfiVarStore->Size; } break; case EFI_IFR_FORM_OP: case EFI_IFR_FORM_MAP_OP: // // Check the matched VarStoreId is found. // if (VarStoreData.VarStoreId == 0) { return EFI_SUCCESS; } break; case EFI_IFR_ONE_OF_OP: // // Check whether current value is the one of option. // // // OneOf question is not in IFR Form. This IFR form is not valid. // if (VarStoreData.VarStoreId == 0) { return EFI_INVALID_PARAMETER; } // // Check whether this question is for the requested varstore. // IfrOneOf = (EFI_IFR_ONE_OF *)IfrOpHdr; if (IfrOneOf->Question.VarStoreId != VarStoreData.VarStoreId) { break; } if (NameValueType) { QuestionName = HiiGetString (HiiHandle, IfrOneOf->Question.VarStoreInfo.VarName, NULL); ASSERT (QuestionName != NULL); if (StrStr (RequestElement, QuestionName) == NULL) { // // This question is not in the current configuration string. Skip it. // break; } Status = GetValueFromRequest (RequestElement, QuestionName, &VarValue); if (EFI_ERROR (Status)) { return Status; } } else { // // Get Offset by Question header and Width by DataType Flags // if (QuestionReferBitField) { // // Get the byte offset/width for bit field. // BitOffset = IfrOneOf->Question.VarStoreInfo.VarOffset; BitWidth = IfrOneOf->Flags & EDKII_IFR_NUMERIC_SIZE_BIT; Offset = BitOffset / 8; TotalBits = BitOffset % 8 + BitWidth; Width = (TotalBits % 8 == 0 ? TotalBits / 8 : TotalBits / 8 + 1); } else { Offset = IfrOneOf->Question.VarStoreInfo.VarOffset; Width = (UINT16)(1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE)); } // // Check whether this question is in current block array. // if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) { // // This question is not in the current configuration string. Skip it. // break; } // // Check this var question is in the var storage // if ((Offset + Width) > VarStoreData.Size) { // // This question exceeds the var store size. // return EFI_INVALID_PARAMETER; } // // Get the current value for oneof opcode // VarValue = 0; if (QuestionReferBitField) { // // Get the value in bit fields. // StartBit = BitOffset % 8; EndBit = StartBit + BitWidth - 1; CopyMem ((UINT8 *)&BufferValue, VarBuffer + Offset, Width); VarValue = BitFieldRead32 (BufferValue, StartBit, EndBit); } else { CopyMem (&VarValue, VarBuffer + Offset, Width); } } // // Set Block Data, to be checked in the following Oneof option opcode. // VarBlockData.OpCode = IfrOpHdr->OpCode; VarBlockData.Scope = IfrOpHdr->Scope; break; case EFI_IFR_NUMERIC_OP: // // Check the current value is in the numeric range. // // // Numeric question is not in IFR Form. This IFR form is not valid. // if (VarStoreData.VarStoreId == 0) { return EFI_INVALID_PARAMETER; } // // Check whether this question is for the requested varstore. // IfrNumeric = (EFI_IFR_NUMERIC *)IfrOpHdr; if (IfrNumeric->Question.VarStoreId != VarStoreData.VarStoreId) { break; } if (NameValueType) { QuestionName = HiiGetString (HiiHandle, IfrNumeric->Question.VarStoreInfo.VarName, NULL); ASSERT (QuestionName != NULL); if (StrStr (RequestElement, QuestionName) == NULL) { // // This question is not in the current configuration string. Skip it. // break; } Status = GetValueFromRequest (RequestElement, QuestionName, &VarValue); if (EFI_ERROR (Status)) { return Status; } } else { // // Get Offset by Question header and Width by DataType Flags // if (QuestionReferBitField) { // // Get the byte offset/width for bit field. // BitOffset = IfrNumeric->Question.VarStoreInfo.VarOffset; BitWidth = IfrNumeric->Flags & EDKII_IFR_NUMERIC_SIZE_BIT; Offset = BitOffset / 8; TotalBits = BitOffset % 8 + BitWidth; Width = (TotalBits % 8 == 0 ? TotalBits / 8 : TotalBits / 8 + 1); } else { Offset = IfrNumeric->Question.VarStoreInfo.VarOffset; Width = (UINT16)(1 << (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE)); } // // Check whether this question is in current block array. // if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) { // // This question is not in the current configuration string. Skip it. // break; } // // Check this var question is in the var storage // if ((Offset + Width) > VarStoreData.Size) { // // This question exceeds the var store size. // return EFI_INVALID_PARAMETER; } // // Check the current value is in the numeric range. // VarValue = 0; if (QuestionReferBitField) { // // Get the value in the bit fields. // StartBit = BitOffset % 8; EndBit = StartBit + BitWidth - 1; CopyMem ((UINT8 *)&BufferValue, VarBuffer + Offset, Width); VarValue = BitFieldRead32 (BufferValue, StartBit, EndBit); } else { CopyMem (&VarValue, VarBuffer + Offset, Width); } } if ( QuestionReferBitField) { // // Value in bit fields was stored as UINt32 type. // if ((IfrNumeric->Flags & EDKII_IFR_DISPLAY_BIT) == 0) { if (((INT32)VarValue < (INT32)IfrNumeric->data.u32.MinValue) || ((INT32)VarValue > (INT32)IfrNumeric->data.u32.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } } else { if ((VarValue < IfrNumeric->data.u32.MinValue) || (VarValue > IfrNumeric->data.u32.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } } } else { if ((IfrNumeric->Flags & EFI_IFR_DISPLAY) == 0) { switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) { case EFI_IFR_NUMERIC_SIZE_1: if (((INT8)VarValue < (INT8)IfrNumeric->data.u8.MinValue) || ((INT8)VarValue > (INT8)IfrNumeric->data.u8.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; case EFI_IFR_NUMERIC_SIZE_2: if (((INT16)VarValue < (INT16)IfrNumeric->data.u16.MinValue) || ((INT16)VarValue > (INT16)IfrNumeric->data.u16.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; case EFI_IFR_NUMERIC_SIZE_4: if (((INT32)VarValue < (INT32)IfrNumeric->data.u32.MinValue) || ((INT32)VarValue > (INT32)IfrNumeric->data.u32.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; case EFI_IFR_NUMERIC_SIZE_8: if (((INT64)VarValue < (INT64)IfrNumeric->data.u64.MinValue) || ((INT64)VarValue > (INT64)IfrNumeric->data.u64.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; } } else { switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) { case EFI_IFR_NUMERIC_SIZE_1: if (((UINT8)VarValue < IfrNumeric->data.u8.MinValue) || ((UINT8)VarValue > IfrNumeric->data.u8.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; case EFI_IFR_NUMERIC_SIZE_2: if (((UINT16)VarValue < IfrNumeric->data.u16.MinValue) || ((UINT16)VarValue > IfrNumeric->data.u16.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; case EFI_IFR_NUMERIC_SIZE_4: if (((UINT32)VarValue < IfrNumeric->data.u32.MinValue) || ((UINT32)VarValue > IfrNumeric->data.u32.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; case EFI_IFR_NUMERIC_SIZE_8: if (((UINT64)VarValue < IfrNumeric->data.u64.MinValue) || ((UINT64)VarValue > IfrNumeric->data.u64.MaxValue)) { // // Not in the valid range. // return EFI_INVALID_PARAMETER; } break; } } } break; case EFI_IFR_CHECKBOX_OP: // // Check value is BOOLEAN type, only 0 and 1 is valid. // // // CheckBox question is not in IFR Form. This IFR form is not valid. // if (VarStoreData.VarStoreId == 0) { return EFI_INVALID_PARAMETER; } // // Check whether this question is for the requested varstore. // IfrCheckBox = (EFI_IFR_CHECKBOX *)IfrOpHdr; if (IfrCheckBox->Question.VarStoreId != VarStoreData.VarStoreId) { break; } if (NameValueType) { QuestionName = HiiGetString (HiiHandle, IfrCheckBox->Question.VarStoreInfo.VarName, NULL); ASSERT (QuestionName != NULL); if (StrStr (RequestElement, QuestionName) == NULL) { // // This question is not in the current configuration string. Skip it. // break; } Status = GetValueFromRequest (RequestElement, QuestionName, &VarValue); if (EFI_ERROR (Status)) { return Status; } } else { // // Get Offset by Question header // if (QuestionReferBitField) { // // Get the byte offset/width for bit field. // BitOffset = IfrCheckBox->Question.VarStoreInfo.VarOffset; BitWidth = 1; Offset = BitOffset / 8; TotalBits = BitOffset % 8 + BitWidth; Width = (TotalBits % 8 == 0 ? TotalBits / 8 : TotalBits / 8 + 1); } else { Offset = IfrCheckBox->Question.VarStoreInfo.VarOffset; Width = (UINT16)sizeof (BOOLEAN); } // // Check whether this question is in current block array. // if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) { // // This question is not in the current configuration string. Skip it. // break; } // // Check this var question is in the var storage // if ((Offset + Width) > VarStoreData.Size) { // // This question exceeds the var store size. // return EFI_INVALID_PARAMETER; } // // Check the current value is in the numeric range. // VarValue = 0; if (QuestionReferBitField) { // // Get the value in bit fields. // StartBit = BitOffset % 8; EndBit = StartBit + BitWidth - 1; CopyMem ((UINT8 *)&BufferValue, VarBuffer + Offset, Width); VarValue = BitFieldRead32 (BufferValue, StartBit, EndBit); } else { CopyMem (&VarValue, VarBuffer + Offset, Width); } } // // Boolean type, only 1 and 0 is valid. // if (VarValue > 1) { return EFI_INVALID_PARAMETER; } break; case EFI_IFR_STRING_OP: // // Check current string length is less than maxsize // // // CheckBox question is not in IFR Form. This IFR form is not valid. // if (VarStoreData.VarStoreId == 0) { return EFI_INVALID_PARAMETER; } // // Check whether this question is for the requested varstore. // IfrString = (EFI_IFR_STRING *)IfrOpHdr; if (IfrString->Question.VarStoreId != VarStoreData.VarStoreId) { break; } // // Get the Max size of the string. // Width = (UINT16)(IfrString->MaxSize * sizeof (UINT16)); if (NameValueType) { QuestionName = HiiGetString (HiiHandle, IfrString->Question.VarStoreInfo.VarName, NULL); ASSERT (QuestionName != NULL); StringPtr = StrStr (RequestElement, QuestionName); if (StringPtr == NULL) { // // This question is not in the current configuration string. Skip it. // break; } // // Skip the VarName. // StringPtr += StrLen (QuestionName); // // Skip the "=". // StringPtr += 1; // // Check current string length is less than maxsize // e.g Config String: "0041004200430044", Unicode String: "ABCD". Unicode String length = Config String length / 4. // Config string format in UEFI spec. // ::=