blob: 6f0279d4a00e7b5bf818676afca11467627f31d2 [file]
/** @file
Dynamic Hii String and String Package generation API functions.
Copyright (c) 2026, Arm Limited. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
@par Reference(s):
- UEFI specification 2.11, section 33.3.6 String Package
**/
#include <Base.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Uefi/UefiInternalFormRepresentation.h>
#include "InternalHiiStringsLib.h"
/** Get the length occupied by all the strings in the string package.
Add up the length of all the strings to be added in the string package,
along with the block header(EFI_HII_SIBT_STRING_UCS2) length.
@param [in] StrList List of strings.
@retval Total length of all the strings in the package, including their
respective block headers(EFI_HII_SIBT_STRING_UCS2).
**/
static
UINTN
DynHiiGetStrLength (
IN CONST LIST_ENTRY *StrList
)
{
UINTN StrLen;
LIST_ENTRY *List;
DYN_HII_STR_INFO *StrInfo;
StrLen = 0;
List = GetFirstNode (StrList);
while (!IsNull (StrList, List)) {
StrInfo = BASE_CR (List, DYN_HII_STR_INFO, Link);
StrLen += StrInfo->Length;
List = GetNextNode (StrList, List);
}
return StrLen;
}
/** Copy all the strings into the string package.
Copy all the added strings to the string package. Each string is prefixed
with EFI_HII_SIBT_STRING_UCS2.
@param [in] StrList List of strings to be added.
@param [in] StrPkgBuf Pointer to the string package where the
strings are to be added.
**/
static
VOID
DynHiiSetStrings (
IN CONST LIST_ENTRY *StrList,
IN DYN_HII_STR_PKG_BUFFER *StrPkgBuf
)
{
UINT8 *Buf;
LIST_ENTRY *List;
DYN_HII_STR_INFO *StrInfo;
Buf = StrPkgBuf->Data + StrPkgBuf->Position;
List = GetFirstNode (StrList);
while (!IsNull (StrList, List)) {
StrInfo = BASE_CR (List, DYN_HII_STR_INFO, Link);
CopyMem (Buf, StrInfo->String, StrInfo->Length);
Buf += StrInfo->Length;
StrPkgBuf->Position += StrInfo->Length;
List = GetNextNode (StrList, List);
}
}
/** Set the EFI_HII_SIBT_END_BLOCK in the string package.
Set the EFI_HII_SIBT_END_BLOCK in the string package once all the
strings have been added to the package.
@param [in] StrPkgBuf Pointer to the string package buffer.
**/
static
VOID
DynHiiSetEndBlock (
IN DYN_HII_STR_PKG_BUFFER *StrPkgBuf
)
{
UINT8 *Buf;
EFI_HII_STRING_BLOCK *Block;
Buf = StrPkgBuf->Data + StrPkgBuf->Position;
Block = (EFI_HII_STRING_BLOCK *)Buf;
Block->BlockType = EFI_HII_SIBT_END;
StrPkgBuf->Position += sizeof (EFI_HII_STRING_BLOCK);
}
/** Get the size of null terminated unicode string in bytes
Returns the size of a possibly unaligned null terminated unicode
string in bytes, including the null-terminator.
@param [in] String Pointer to the unicode string.
@retval Size of the string.
**/
static
UINT32
DynHiiGetStrSize (
IN CONST CHAR16 *String
)
{
CHAR16 Str;
UINT32 StrLen;
for (StrLen = 1; (Str = ReadUnaligned16 (String++)) != L'\0'; StrLen++) {
}
return StrLen * sizeof (CHAR16);
}
/** Dump the contents of the end skip2 block.
Dump the contents of a EFI_HII_SIBT_SKIP2_BLOCK string block.
@param [in] Buf Pointer to the string block.
@param [in] Offset Offset value to be printed.
**/
static
VOID
DynHiiDumpSkip2Block (
IN UINT8 *Buf,
IN UINT32 Offset
)
{
UINT32 BlockSize;
BlockSize = sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, BlockSize, "");
}
/** Dump the contents of the end string block.
Dump the contents of a EFI_HII_SIBT_END_BLOCK string block.
@param [in] Buf Pointer to the string block.
@param [in] Offset Offset value to be printed.
**/
static
VOID
DynHiiDumpEndBlock (
IN UINT8 *Buf,
IN UINT32 Offset
)
{
UINT32 BlockSize;
BlockSize = sizeof (EFI_HII_SIBT_END_BLOCK);
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, BlockSize, "");
}
/** Dump the contents of a UCS2 string block.
Dump the contents of a UCS2 string block.
@param [in] Buf Pointer to the string block.
@param [in] StrLen Length of the string associated
with the block.
@param [in] Offset Offset value to be printed.
**/
static
VOID
DynHiiDumpUcs2Block (
IN UINT8 *Buf,
IN UINT32 StrLen,
IN UINT32 Offset
)
{
UINT32 BlockSize;
BlockSize = StrLen + sizeof (EFI_HII_STRING_BLOCK);
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, BlockSize, "");
}
/** Dump the contents of a String package.
Dump the contents of a string package. Useful for debugging.
@param [in] StrPkg Pointer to the string package.
@param [in] StrPkgLen Length of the string package.
@param [in] Offset Offset to be printed.
@retval EFI_INVALID_PARAMETER If any of the checks fail.
@retval EFI_SUCCESS No issues found.
**/
static
EFI_STATUS
DynHiiDumpStrPkg (
IN UINT8 *StrPkg,
IN UINT32 StrPkgLen,
IN UINT32 Offset
)
{
UINT8 *Buf;
UINT32 HdrLen;
UINT32 StrLen;
EFI_STATUS Status;
EFI_HII_STRING_BLOCK *StrBlock;
Buf = StrPkg;
Status = EFI_SUCCESS;
HdrLen = ReadUnaligned32 (Buf + sizeof (EFI_HII_PACKAGE_HEADER));
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, HdrLen, "");
Offset += HdrLen;
DEBUG ((DEBUG_VERBOSE, "\n\n"));
StrPkgLen -= HdrLen;
while (StrPkgLen != 0) {
StrBlock = (EFI_HII_STRING_BLOCK *)Buf;
switch (StrBlock->BlockType) {
case EFI_HII_SIBT_SKIP2:
DynHiiDumpSkip2Block (Buf, Offset);
Buf += sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
Offset += sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
StrPkgLen -= sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
break;
case EFI_HII_SIBT_STRING_UCS2:
StrLen = DynHiiGetStrSize (
(CHAR16 *)(Buf + sizeof (EFI_HII_STRING_BLOCK))
);
DynHiiDumpUcs2Block (Buf, StrLen, Offset);
Buf += StrLen + sizeof (EFI_HII_STRING_BLOCK);
Offset += StrLen + sizeof (EFI_HII_STRING_BLOCK);
StrPkgLen -= StrLen + sizeof (EFI_HII_STRING_BLOCK);
break;
case EFI_HII_SIBT_END:
DynHiiDumpEndBlock (Buf, Offset);
Buf += sizeof (EFI_HII_SIBT_END_BLOCK);
Offset += sizeof (EFI_HII_SIBT_END_BLOCK);
StrPkgLen -= sizeof (EFI_HII_SIBT_END_BLOCK);
break;
default:
DEBUG ((DEBUG_VERBOSE, "Received Invalid String Block Type.\n"));
Status = EFI_INVALID_PARAMETER;
goto out;
}
}
out:
DEBUG ((
DEBUG_VERBOSE,
"};\n\n"
));
return Status;
}
/** Dump the contents of the String buffer.
Dump the contents of all the string packages. Useful for debugging.
@param [in] StrPkg Pointer to the string package array.
@param [in] StrPkgLen Length of the string package array.
@retval EFI_INVALID_PARAMETER If any of the checks fail.
@retval EFI_SUCCESS No issues found.
**/
static
EFI_STATUS
DynHiiDumpStrPkgBuf (
IN UINT8 *StrPkg,
IN UINT32 StrPkgLen
)
{
UINT8 *Buf;
UINT32 PkgLen;
UINT32 Offset;
EFI_STATUS Status;
if ((StrPkg == NULL) || (StrPkgLen == 0)) {
return EFI_INVALID_PARAMETER;
}
DEBUG ((
DEBUG_VERBOSE,
"INFO: StrPkg => %p, StrPkgLen => 0x%x\n",
StrPkg,
StrPkgLen
));
DEBUG ((
DEBUG_VERBOSE,
"***** Generated String Package *****\n"
));
DEBUG ((
DEBUG_VERBOSE,
"StrByteStream[] = {\n"
));
Buf = StrPkg;
Offset = 0;
// The string package array generated by the
// AutoGen tool consists of a 4 byte array
// length member which has to be printed
// separately.
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, sizeof (UINT32), "");
Buf += sizeof (UINT32);
Offset += sizeof (UINT32);
DEBUG ((DEBUG_VERBOSE, "\n\n"));
StrPkgLen -= sizeof (UINT32);
while (StrPkgLen != 0) {
PkgLen = ReadUnaligned24 (Buf);
if (StrPkgLen < PkgLen) {
return EFI_INVALID_PARAMETER;
}
Status = DynHiiDumpStrPkg (Buf, PkgLen, Offset);
if (EFI_ERROR (Status)) {
return Status;
}
Buf += PkgLen;
Offset += PkgLen;
StrPkgLen -= PkgLen;
}
return EFI_SUCCESS;
}
/** Get number of string packages in the sting package array.
The string package array may consists of multiple string packages, each
associated with a language-code. Count the number of such string packages
in the array, and perform basic checks on the string packages, like they
actually are string packages.
@param [in] StrPkgArr Array of string packages.
@param [out] NumStrPkg Number of string packages found in the
array.
@retval EFI_INVALID_PARAMETER If any of the parameter checks fail.
@retval EFI_BAD_BUFFER_SIZE Wrong string package length found.
@retval EFI_SUCCESS No issues found.
**/
static
EFI_STATUS
DynHiiGetNumStrPkg (
IN CONST UINT8 *StrPkgArr,
OUT UINT32 *NumStrPkg
)
{
CONST UINT8 *StrArr;
UINT8 PkgType;
UINT32 StrArrLen;
UINT32 PkgLen;
UINT32 TotalPkgLen;
UINT32 ReadPkgLen;
UINT32 PkgHdr;
if ((StrPkgArr == NULL) || (NumStrPkg == NULL)) {
return EFI_INVALID_PARAMETER;
}
ReadPkgLen = ReadUnaligned32 (StrPkgArr);
if ((ReadPkgLen == 0) || (ReadPkgLen < sizeof (UINT32))) {
return EFI_BAD_BUFFER_SIZE;
}
// The string package array generated by the
// AutoGen tool consists of a 4 byte array
// length member which has to be discarded.
StrArrLen = ReadPkgLen - sizeof (UINT32);
if (StrArrLen == 0) {
return EFI_BAD_BUFFER_SIZE;
}
StrArr = StrPkgArr + sizeof (UINT32);
*NumStrPkg = 0;
TotalPkgLen = sizeof (UINT32);
while (StrArrLen != 0) {
PkgHdr = ReadUnaligned32 (StrArr);
PkgType = (PkgHdr & 0xFF000000) >> 24;
PkgLen = PkgHdr & 0x00FFFFFF;
if (PkgLen == 0) {
return EFI_BAD_BUFFER_SIZE;
}
if (PkgType != EFI_HII_PACKAGE_STRINGS) {
return EFI_INVALID_PARAMETER;
}
if (StrArrLen < PkgLen) {
return EFI_BAD_BUFFER_SIZE;
}
StrArrLen -= PkgLen;
StrArr += PkgLen;
TotalPkgLen += PkgLen;
(*NumStrPkg)++;
}
if (ReadPkgLen != TotalPkgLen) {
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/** Build a list of strings under a given string package
The string package consists of multiple string blocks. The supported
types of string blocks are EFI_HII_SIBT_STRING_UCS2, EFI_HII_SIBT_SKIP2
and EFI_HII_SIBT_END. This function iterates through the existing string
package, and splits up the strings into a list of string blocks. The
EFI_HII_SIBT_END is not added to the list.
@param [in] StrArr String package array.
@param [in] StrPkgInstance An instance of a string package.
@retval EFI_UNSUPPORTED Unsupported string block type.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
@retval EFI_BAD_BUFFER_SIZE Wrong string package length found.
@retval EFI_SUCCESS The string list was built successfully.
**/
static
EFI_STATUS
DynHiiBuildStrBlocks (
IN CONST UINT8 *StrArr,
IN DYN_HII_STR_PKG_INSTANCE *StrPkgInstance
)
{
UINT32 PkgLen;
UINT32 HdrLen;
UINT32 StrInfoOffset;
UINT64 AllocLen;
CONST UINT8 *StrOffset;
CONST CHAR16 *StrPtr;
EFI_STRING_ID StringId;
DYN_HII_STR_INFO *StrInfo;
CONST EFI_HII_STRING_BLOCK *StrBlock;
PkgLen = ReadUnaligned24 (StrArr);
if (PkgLen == 0) {
return EFI_BAD_BUFFER_SIZE;
}
HdrLen = ReadUnaligned32 (StrArr + sizeof (EFI_HII_PACKAGE_HEADER));
if (HdrLen == 0) {
return EFI_BAD_BUFFER_SIZE;
}
StrInfoOffset = ReadUnaligned32 (
StrArr +
OFFSET_OF (EFI_HII_STRING_PACKAGE_HDR, StringInfoOffset)
);
if (StrInfoOffset != HdrLen) {
return EFI_BAD_BUFFER_SIZE;
}
if (PkgLen <= HdrLen) {
return EFI_BAD_BUFFER_SIZE;
}
StringId = 0;
PkgLen -= HdrLen;
StrOffset = StrArr + HdrLen;
while (PkgLen != 0) {
StrBlock = (CONST EFI_HII_STRING_BLOCK *)StrOffset;
if ((StrBlock->BlockType != EFI_HII_SIBT_STRING_UCS2) &&
(StrBlock->BlockType != EFI_HII_SIBT_SKIP2) &&
(StrBlock->BlockType != EFI_HII_SIBT_END))
{
return EFI_UNSUPPORTED;
}
if (StrBlock->BlockType == EFI_HII_SIBT_SKIP2) {
AllocLen = sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
if (PkgLen <= AllocLen) {
return EFI_BAD_BUFFER_SIZE;
}
StringId += ReadUnaligned16 (
(CONST UINT16 *)(StrOffset + sizeof (EFI_HII_STRING_BLOCK))
);
} else if (StrBlock->BlockType == EFI_HII_SIBT_END) {
PkgLen -= sizeof (EFI_HII_SIBT_END_BLOCK);
if (PkgLen != 0) {
DEBUG ((
DEBUG_ERROR,
"%a: Wrong string package length value found.\n",
__func__
));
return EFI_BAD_BUFFER_SIZE;
}
continue;
} else {
StrPtr = (CONST CHAR16 *)(StrOffset + sizeof (EFI_HII_STRING_BLOCK));
AllocLen = DynHiiGetStrSize (StrPtr) + sizeof (EFI_HII_STRING_BLOCK);
if (AllocLen >= SIZE_16MB) {
return EFI_BAD_BUFFER_SIZE;
}
StringId++;
}
if (PkgLen <= AllocLen) {
return EFI_BAD_BUFFER_SIZE;
}
StrInfo = AllocateZeroPool (sizeof (*StrInfo));
if (StrInfo == NULL) {
return EFI_OUT_OF_RESOURCES;
}
StrInfo->String = AllocateZeroPool (AllocLen);
if (StrInfo->String == NULL) {
FreePool (StrInfo);
return EFI_OUT_OF_RESOURCES;
}
StrInfo->StringId = StringId;
StrInfo->Length = (UINT32)AllocLen;
CopyMem (StrInfo->String, StrOffset, AllocLen);
StrPkgInstance->MaxStringId = StringId;
InitializeListHead (&StrInfo->Link);
InsertTailList (&StrPkgInstance->StringList, &StrInfo->Link);
StrOffset += AllocLen;
PkgLen -= (UINT32)AllocLen;
}
return EFI_SUCCESS;
}
/** Initialize a string package instance.
The string package array may contain multiple string packages, each
associated with a language-code. Information about each such string
package is stored in a string instance structure. Initialize and
populate a string instance for a string package. Add the string
instance to the list of string instance list.
@param [in] PkgIdx Index of the string package.
@param [in] StrPkgArr Array of string packages.
@param [in] StrPkg Top-level string package information
structure.
@retval EFI_UNSUPPORTED Unsupported string block type.
@retval EFI_BAD_BUFFER_SIZE Wrong string package length found.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
@retval EFI_SUCCESS The string list was built successfully.
**/
static
EFI_STATUS
DynHiiInitStrPkgInstance (
IN UINT32 PkgIdx,
IN CONST UINT8 *StrPkgArr,
IN DYN_HII_STR_PKG *StrPkg
)
{
UINT32 Idx;
UINT32 PkgLen;
UINT32 HdrLen;
UINT32 LangLen;
UINT32 FixedHdrLen;
EFI_STATUS Status;
CONST UINT8 *StrArr;
DYN_HII_STR_PKG_INSTANCE *StrPkgInstance;
// The string package array generated by the
// AutoGen tool consists of a 4 byte array
// length member which has to be discarded.
StrArr = StrPkgArr + sizeof (UINT32);
for (Idx = 0; Idx < PkgIdx; Idx++) {
PkgLen = ReadUnaligned24 (StrArr);
StrArr += PkgLen;
}
StrPkgInstance = AllocateZeroPool (sizeof (*StrPkgInstance));
if (StrPkgInstance == NULL) {
return EFI_OUT_OF_RESOURCES;
}
StrPkgInstance->MaxStringId = 0;
InitializeListHead (&StrPkgInstance->StringList);
InitializeListHead (&StrPkgInstance->Link);
InsertTailList (&StrPkg->StrPkgList, &StrPkgInstance->Link);
FixedHdrLen = OFFSET_OF (EFI_HII_STRING_PACKAGE_HDR, Language);
StrPkgInstance->StrPkgHdr = AllocateZeroPool (FixedHdrLen);
if (StrPkgInstance->StrPkgHdr == NULL) {
return EFI_OUT_OF_RESOURCES;
}
CopyMem (
StrPkgInstance->StrPkgHdr,
StrArr,
FixedHdrLen
);
HdrLen = ReadUnaligned32 (StrArr + sizeof (EFI_HII_PACKAGE_HEADER));
if (HdrLen <= FixedHdrLen) {
return EFI_BAD_BUFFER_SIZE;
}
LangLen = HdrLen - FixedHdrLen;
StrPkgInstance->LanguageCode = AllocateZeroPool (LangLen);
if (StrPkgInstance->LanguageCode == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = AsciiStrCpyS (
StrPkgInstance->LanguageCode,
LangLen,
(CONST CHAR8 *)StrArr + FixedHdrLen
);
if (EFI_ERROR (Status)) {
return EFI_BAD_BUFFER_SIZE;
}
return DynHiiBuildStrBlocks (StrArr, StrPkgInstance);
}
/** Check if a language-code is supported in the string package list
Check the list of string packages for a given language support.
@param [in] StrPkg Top-level string package information
structure.
@param [in] Language Language to check for.
@retval TRUE if the language is supported, FALSE otherwise.
**/
static
BOOLEAN
DynHiiLangCodeSupported (
IN CONST DYN_HII_STR_PKG *StrPkg,
IN CONST CHAR8 *Language
)
{
LIST_ENTRY *List;
DYN_HII_STR_PKG_INSTANCE *Tmp;
List = GetFirstNode (&StrPkg->StrPkgList);
while (!IsNull (&StrPkg->StrPkgList, List)) {
Tmp = BASE_CR (List, DYN_HII_STR_PKG_INSTANCE, Link);
if (AsciiStrCmp (Tmp->LanguageCode, Language) == 0) {
return TRUE;
}
List = GetNextNode (&StrPkg->StrPkgList, List);
}
return FALSE;
}
/** Check for duplicate entries of languagecode.
Check if there are any duplicate languagecode entries in the StrTable
array. The number of supported languages is not going to be a large
number, hence this brute-force approach should be fine.
@param [in] StrTable An array of entries of languagecode and
string pair.
@param [in] StrCount Number of entries of StrTable.
@retval TRUE if there is duplication, FALSE otherwise.
**/
static
BOOLEAN
DynHiiDupStrTableEntry (
IN CONST DYN_HII_STR_ENTRY *StrTable,
IN UINT32 StrCount
)
{
UINT32 Idx;
UINT32 Idx1;
CONST DYN_HII_STR_ENTRY *Entry;
for (Idx = 0; Idx < StrCount; Idx++) {
Entry = &StrTable[Idx];
for (Idx1 = Idx + 1; Idx1 < StrCount; Idx1++) {
if (AsciiStrCmp (Entry->LanguageCode, StrTable[Idx1].LanguageCode) == 0) {
return TRUE;
}
}
}
return FALSE;
}
/** Look for a language-code in the string table array.
Look for a language-code match in the string table array. This would
indicate if a string is to be added for the language-code parameter
that has been passed to this function.
@param [in] LanguageCode Language code to look for.
@param [in] StrTable An array of entries of languagecode and
string pair.
@param [in] StrCount Number of entries of StrTable.
@retval Pointer to the StrTable entry, NULL if no match found.
**/
static
CONST DYN_HII_STR_ENTRY *
DynHiiMatchStrTableEntry (
IN CONST CHAR8 *LanguageCode,
IN CONST DYN_HII_STR_ENTRY *StrTable,
IN UINT32 StrCount
)
{
UINT32 Idx;
for (Idx = 0; Idx < StrCount; Idx++) {
if (AsciiStrCmp (StrTable[Idx].LanguageCode, LanguageCode) == 0) {
return &StrTable[Idx];
}
}
return NULL;
}
/** Update the length of the string package header.
Based on the total length of the string blocks computed, added
to the size of EFI_HII_STRING_PACKAGE_HDR, update the string
package size.
@param [in] StrPkgHdr String package header.
@param [in] StrLen Combined length of all string
blocks for the string package.
@retval EFI_BAD_BUFFER_SIZE Input is too large to fit under a
string package.
@retval EFI_SUCCESS The string package header was updated
successfully.
**/
static
EFI_STATUS
DynHiiUpdateStrPkgHdrLen (
IN UINT8 *StrPkgHdr,
IN UINT32 StrLen
)
{
UINT8 *PkgHdr;
UINT32 HdrLen;
UINT32 PkgLen;
PkgHdr = StrPkgHdr;
HdrLen = ReadUnaligned32 (PkgHdr + sizeof (EFI_HII_PACKAGE_HEADER));
PkgLen = HdrLen + StrLen;
if (PkgLen >= SIZE_16MB) {
return EFI_BAD_BUFFER_SIZE;
}
WriteUnaligned24 (StrPkgHdr, PkgLen);
return EFI_SUCCESS;
}
/** Check if all the MaxStringId's match
Iterate through all the string packages that have been added, and
check that the MaxStringId value in all the packages match.
@param [in] StrPkgList List of string packages.
@retval TRUE if the ID's match, FALSE otherwise.
**/
static
BOOLEAN
DynHiiStrIdMatch (
IN LIST_ENTRY *StrPkgList
)
{
LIST_ENTRY *List;
EFI_STRING_ID CachedStrId;
EFI_STRING_ID ReadStrId;
DYN_HII_STR_PKG_INSTANCE *StrPkgInstance;
CachedStrId = 0;
ReadStrId = 0;
List = GetFirstNode (StrPkgList);
while (!IsNull (StrPkgList, List)) {
StrPkgInstance = BASE_CR (List, DYN_HII_STR_PKG_INSTANCE, Link);
ReadStrId = StrPkgInstance->MaxStringId;
ASSERT (ReadStrId != 0);
if (CachedStrId == 0) {
CachedStrId = ReadStrId;
}
if (ReadStrId != CachedStrId) {
return FALSE;
}
List = GetNextNode (StrPkgList, List);
}
return TRUE;
}
/** Append a string to the given string instance
Append a string block, either consisting of a real string or a dummy
EFI_HII_SIBT_STRING_UCS2 block to the list of strings under the string
instance passed to the function.
@param [in] StrPkgInstance An instance of a string package.
@param [in] String A UCS2 string.
@param [out] StringId The string ID for this added string.
@retval EFI_BAD_BUFFER_SIZE Input is too large to fit under a
string package.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
@retval EFI_SUCCESS The string was appended successfully.
**/
static
EFI_STATUS
EFIAPI
DynHiiStringAppend (
IN DYN_HII_STR_PKG_INSTANCE *StrPkgInstance,
IN CONST EFI_STRING String,
OUT EFI_STRING_ID *StringId
)
{
UINT8 *Buf;
UINT64 StrBytes;
BOOLEAN DummyStr;
DYN_HII_STR_INFO *NewStr;
EFI_HII_STRING_BLOCK StrBlock;
EFI_HII_SIBT_STRING_UCS2_BLOCK *Ucs2Block;
ASSERT (StringId != NULL);
if (StrPkgInstance->MaxStringId == MAX_UINT16) {
*StringId = MAX_UINT16;
return EFI_OUT_OF_RESOURCES;
}
NewStr = AllocateZeroPool (sizeof (*NewStr));
if (NewStr == NULL) {
*StringId = StrPkgInstance->MaxStringId + 1;
return EFI_OUT_OF_RESOURCES;
}
DummyStr = String == NULL ? TRUE : FALSE;
StrBytes = DummyStr ? sizeof (*Ucs2Block) :
DynHiiGetStrSize (String) + sizeof (EFI_HII_STRING_BLOCK);
if (StrBytes >= SIZE_16MB) {
*StringId = StrPkgInstance->MaxStringId + 1;
FreePool (NewStr);
return EFI_BAD_BUFFER_SIZE;
}
NewStr->String = AllocateZeroPool (StrBytes);
if (NewStr->String == NULL) {
*StringId = StrPkgInstance->MaxStringId + 1;
FreePool (NewStr);
return EFI_OUT_OF_RESOURCES;
}
InitializeListHead (&NewStr->Link);
InsertTailList (&StrPkgInstance->StringList, &NewStr->Link);
StrPkgInstance->MaxStringId++;
NewStr->StringId = StrPkgInstance->MaxStringId;
*StringId = StrPkgInstance->MaxStringId;
NewStr->Length = (UINT32)StrBytes;
if (DummyStr) {
Ucs2Block = (EFI_HII_SIBT_STRING_UCS2_BLOCK *)NewStr->String;
Ucs2Block->Header.BlockType = EFI_HII_SIBT_STRING_UCS2;
} else {
StrBlock.BlockType = EFI_HII_SIBT_STRING_UCS2;
CopyMem (NewStr->String, &StrBlock, sizeof (EFI_HII_STRING_BLOCK));
Buf = (UINT8 *)NewStr->String + sizeof (EFI_HII_STRING_BLOCK);
CopyMem (Buf, String, StrBytes - sizeof (EFI_HII_STRING_BLOCK));
}
return EFI_SUCCESS;
}
/** Remove a string from the string list
Iterate through the string list, and remove the string
corresponding to the StringId parameter passed to the
function.
@param [in] StrPkgInstance String package instance pointer.
@param [in] StringId String identifier of the string
to be removed.
**/
static
VOID
DynHiiStringPop (
IN DYN_HII_STR_PKG_INSTANCE *StrPkgInstance,
IN EFI_STRING_ID StringId
)
{
LIST_ENTRY *StringList;
LIST_ENTRY *Link;
DYN_HII_STR_INFO *StrInfo;
StringList = &StrPkgInstance->StringList;
Link = GetFirstNode (StringList);
while (!IsNull (StringList, Link)) {
StrInfo = BASE_CR (Link, DYN_HII_STR_INFO, Link);
if (StrInfo->StringId == StringId) {
ASSERT (StrPkgInstance->MaxStringId == StringId);
StrPkgInstance->MaxStringId--;
if (StrInfo->String != NULL) {
FreePool (StrInfo->String);
}
RemoveEntryList (Link);
FreePool (StrInfo);
return;
}
Link = GetNextNode (StringList, Link);
}
}
/** Remove a string from the string packages
Iterate through all the string package lists, and remove the
string corresponding to the StringId parameter passed to the
function.
@param [in] StrPkg String package pointer.
@param [in] StringId String identifier of the string
to be removed.
**/
static
VOID
DynHiiRemoveString (
IN DYN_HII_STR_PKG *StrPkg,
IN EFI_STRING_ID StringId
)
{
LIST_ENTRY *Link;
DYN_HII_STR_PKG_INSTANCE *StrPkgInstance;
Link = GetFirstNode (&StrPkg->StrPkgList);
while (!IsNull (&StrPkg->StrPkgList, Link)) {
StrPkgInstance = BASE_CR (Link, DYN_HII_STR_PKG_INSTANCE, Link);
DynHiiStringPop (StrPkgInstance, StringId);
Link = GetNextNode (&StrPkg->StrPkgList, Link);
}
}
/** Add a string to the string list.
Add the string(s) passed to the function to a list of strings that would
be added to form the string package. Each index of StrTable is a pair of
languagecode and the corresponding string. If there are multiple
languages that are supported, strings for those languages can be added.
Alternatively, it is also possible to not add a string for a supported
language, in which case the string is left blank.
In case of a scenario that DynHiiStringAppend() fails adding a string
for a particular language, revert all the added strings for the other
supported languages.
@param [in] StrPkgHandle Handle to the top-level string package
information structure.
@param [in] StrTable An array of entries of languagecode and
string pair to be added.
@param [in] StrCount Number of entries of StrTable.
@param [out] StringId String Identifier assigned for this set
of strings.
@retval EFI_SUCCESS The string was added to the list successfully.
@retval EFI_INVALID_PARAMETER The StrList or String is NULL.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
EFI_STATUS
EFIAPI
DynHiiAddString (
IN CONST DYN_HII_STR_HANDLE StrPkgHandle,
IN CONST DYN_HII_STR_ENTRY *StrTable,
IN UINT32 StrCount,
OUT EFI_STRING_ID *StringId
)
{
UINT32 Idx;
EFI_STATUS Status;
LIST_ENTRY *List;
EFI_STRING_ID NewStringId;
DYN_HII_STR_PKG *StrPkg;
CONST DYN_HII_STR_ENTRY *Entry;
DYN_HII_STR_PKG_INSTANCE *StrPkgInstance;
if ((StrPkgHandle == NULL) || (StrTable == NULL) || (StringId == NULL) ||
(StrCount == 0))
{
return EFI_INVALID_PARAMETER;
}
StrPkg = StrPkgHandle;
if (StrPkg->Signature != DYN_HII_STR_PKG_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
if (!DynHiiStrIdMatch (&StrPkg->StrPkgList)) {
DEBUG ((
DEBUG_ERROR,
"%a: MaxStrId mismatch found in string packages.\n",
__func__
));
return EFI_INVALID_PARAMETER;
}
for (Idx = 0; Idx < StrCount; Idx++) {
if ((StrTable[Idx].LanguageCode == NULL) ||
(StrTable[Idx].String == NULL))
{
return EFI_INVALID_PARAMETER;
}
if (!DynHiiLangCodeSupported (StrPkg, StrTable[Idx].LanguageCode)) {
return EFI_NOT_FOUND;
}
}
if (DynHiiDupStrTableEntry (StrTable, StrCount)) {
DEBUG ((
DEBUG_INFO,
"%a: Duplicate language-code entries found.\n",
__func__
));
return EFI_INVALID_PARAMETER;
}
NewStringId = 0;
List = GetFirstNode (&StrPkg->StrPkgList);
while (!IsNull (&StrPkg->StrPkgList, List)) {
StrPkgInstance = BASE_CR (List, DYN_HII_STR_PKG_INSTANCE, Link);
Entry = DynHiiMatchStrTableEntry (
StrPkgInstance->LanguageCode,
StrTable,
StrCount
);
Status = DynHiiStringAppend (
StrPkgInstance,
Entry == NULL ? NULL : Entry->String,
&NewStringId
);
if (EFI_ERROR (Status)) {
if (NewStringId != MAX_UINT16) {
DynHiiRemoveString (StrPkg, NewStringId);
}
return Status;
}
List = GetNextNode (&StrPkg->StrPkgList, List);
}
*StringId = NewStringId;
return EFI_SUCCESS;
}
/** Initialize the string package structure.
Initialize the DYN_HII_STR_PKG structure by allocating memory for
certain members of the structure and then initializing them with data
that were passed to the function.
@param [in] StrPkgArr Existing string package array which might
consist of multiple language string arrays
@param [out] StrPkgHandle Pointer to the DYN_HII_STR_HANDLE to hold
the string package structure.
@retval EFI_SUCCESS The structure was initialized successfully.
@retval EFI_UNSUPPORTED Unsupported string block type.
@retval EFI_BAD_BUFFER_SIZE Wrong string package length found.
@retval EFI_INVALID_PARAMETER The StrPkgInfo or LanguageCode is NULL.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
EFI_STATUS
EFIAPI
DynHiiInitStringPkg (
IN CONST UINT8 *StrPkgArr,
OUT DYN_HII_STR_HANDLE *StrPkgHandle
)
{
UINT32 NumStrPkg;
UINT32 Idx;
EFI_STATUS Status;
DYN_HII_STR_PKG *StrPkg;
if ((StrPkgArr == NULL) || (StrPkgHandle == NULL)) {
return EFI_INVALID_PARAMETER;
}
Status = DynHiiGetNumStrPkg (StrPkgArr, &NumStrPkg);
if ((EFI_ERROR (Status)) || (NumStrPkg == 0)) {
DEBUG ((
DEBUG_INFO,
"%a: Incorrect string package array received.\n",
__func__
));
return Status;
}
StrPkg = AllocateZeroPool (sizeof (DYN_HII_STR_PKG));
if (StrPkg == NULL) {
return EFI_OUT_OF_RESOURCES;
}
StrPkg->LanguageCount = NumStrPkg;
StrPkg->Signature = DYN_HII_STR_PKG_SIGNATURE;
InitializeListHead (&StrPkg->StrPkgList);
for (Idx = 0; Idx < NumStrPkg; Idx++) {
Status = DynHiiInitStrPkgInstance (Idx, StrPkgArr, StrPkg);
if (EFI_ERROR (Status)) {
goto err_out;
}
}
if (!DynHiiStrIdMatch (&StrPkg->StrPkgList)) {
DEBUG ((
DEBUG_ERROR,
"%a: MaxStrId mismatch found in string packages.\n",
__func__
));
Status = EFI_INVALID_PARAMETER;
goto err_out;
}
*StrPkgHandle = StrPkg;
return EFI_SUCCESS;
err_out:
DynHiiFreeStringPackage (StrPkg);
return Status;
}
/** Generate string package.
Generate the string package from the input parameter. The string package is
subsequently added by the caller of this function to the HII database
through the HiiAddPackages() call.
The serialized string package buffer is passed back to the caller in
StrPkgData. It is the caller's responsibility to free up the string package
buffer through a call to FreePool().
@param [in] StrPkgHandle Handle to the top-level string package
information structure.
@param [out] StrPkgData The generated array of string package(s).
@retval EFI_SUCCESS The string was added to the list successfully.
@retval EFI_INVALID_PARAMETER The StrPkgInfo or LanguageCode is NULL.
@retval EFI_ALREADY_STARTED If StrPkgBuf is not NULL.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
EFI_STATUS
EFIAPI
DynHiiGenerateStringPackage (
IN DYN_HII_STR_HANDLE StrPkgHandle,
OUT UINT8 **StrPkgData
)
{
UINT8 *Buf;
UINT32 FixedHdrLen;
UINT64 PkgLen;
UINT64 TmpLen;
LIST_ENTRY *List;
EFI_STATUS Status;
DYN_HII_STR_PKG *StrPkg;
DYN_HII_STR_PKG_BUFFER StrPkgBuf;
DYN_HII_STR_PKG_INSTANCE *StrPkgInstance;
if ((StrPkgHandle == NULL) || (StrPkgData == NULL)) {
return EFI_INVALID_PARAMETER;
}
StrPkg = StrPkgHandle;
if (StrPkg->Signature != DYN_HII_STR_PKG_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
if (!DynHiiStrIdMatch (&StrPkg->StrPkgList)) {
DEBUG ((
DEBUG_ERROR,
"%a: MaxStrId mismatch found in string packages.\n",
__func__
));
return EFI_INVALID_PARAMETER;
}
*StrPkgData = NULL;
ZeroMem (&StrPkgBuf, sizeof (DYN_HII_STR_PKG_BUFFER));
TmpLen = 0;
List = GetFirstNode (&StrPkg->StrPkgList);
while (!IsNull (&StrPkg->StrPkgList, List)) {
StrPkgInstance = BASE_CR (List, DYN_HII_STR_PKG_INSTANCE, Link);
PkgLen = DynHiiGetStrLength (&StrPkgInstance->StringList);
PkgLen += sizeof (EFI_HII_SIBT_END_BLOCK);
if (PkgLen > MAX_UINT32) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiUpdateStrPkgHdrLen (
StrPkgInstance->StrPkgHdr,
(UINT32)PkgLen
);
if (EFI_ERROR (Status)) {
return Status;
}
PkgLen += ReadUnaligned32 (
StrPkgInstance->StrPkgHdr + sizeof (EFI_HII_PACKAGE_HEADER)
);
TmpLen += PkgLen;
List = GetNextNode (&StrPkg->StrPkgList, List);
}
TmpLen += sizeof (UINT32);
if (TmpLen > MAX_UINT32) {
return EFI_BAD_BUFFER_SIZE;
}
StrPkgBuf.Size = (UINT32)TmpLen;
StrPkgBuf.Data = AllocateZeroPool (StrPkgBuf.Size);
if (StrPkgBuf.Data == NULL) {
return EFI_OUT_OF_RESOURCES;
}
// The string package array generated by the
// AutoGen tool consists of a 4 byte array
// length member. This needs to be populated
// as well.
*(UINT32 *)StrPkgBuf.Data = StrPkgBuf.Size;
StrPkgBuf.Position = sizeof (UINT32);
List = GetFirstNode (&StrPkg->StrPkgList);
FixedHdrLen = OFFSET_OF (EFI_HII_STRING_PACKAGE_HDR, Language);
while (!IsNull (&StrPkg->StrPkgList, List)) {
StrPkgInstance = BASE_CR (List, DYN_HII_STR_PKG_INSTANCE, Link);
Buf = StrPkgBuf.Data + StrPkgBuf.Position;
CopyMem (
Buf,
StrPkgInstance->StrPkgHdr,
FixedHdrLen
);
StrPkgBuf.Position += FixedHdrLen;
Buf = StrPkgBuf.Data + StrPkgBuf.Position;
CopyMem (
Buf,
StrPkgInstance->LanguageCode,
AsciiStrSize (StrPkgInstance->LanguageCode)
);
StrPkgBuf.Position += (UINT32)AsciiStrSize (StrPkgInstance->LanguageCode);
DynHiiSetStrings (&StrPkgInstance->StringList, &StrPkgBuf);
DynHiiSetEndBlock (&StrPkgBuf);
List = GetNextNode (&StrPkg->StrPkgList, List);
}
if (StrPkgBuf.Size != StrPkgBuf.Position) {
DEBUG ((
DEBUG_INFO,
"%a: Error constructing the string package.\n",
__func__
));
FreePool (StrPkgBuf.Data);
return EFI_BAD_BUFFER_SIZE;
}
*StrPkgData = StrPkgBuf.Data;
DynHiiDumpStrPkgBuf (StrPkgBuf.Data, StrPkgBuf.Size);
return EFI_SUCCESS;
}
/** Free all the strings in a string list.
Iterate through the string list, and free up memory allocated for the
string and the DYN_HII_STR_INFO structure that contains information about
that string.
@param [in] StringList The list of strings to be freed.
**/
static
VOID
EFIAPI
DynHiiFreeStrings (
IN LIST_ENTRY *StringList
)
{
LIST_ENTRY *Link;
LIST_ENTRY *Next;
DYN_HII_STR_INFO *StrInfo;
if (StringList == NULL) {
return;
}
Link = GetFirstNode (StringList);
while (!IsNull (StringList, Link)) {
Next = GetNextNode (StringList, Link);
StrInfo = BASE_CR (Link, DYN_HII_STR_INFO, Link);
RemoveEntryList (Link);
FreePool (StrInfo->String);
FreePool (StrInfo);
Link = Next;
}
InitializeListHead (StringList);
}
/** Free up the string package data structures.
Free up all the structures that were used for building up the string
package.
@param [in] StrPkgHandle Handle to the top-level string package
information structure.
**/
VOID
EFIAPI
DynHiiFreeStringPackage (
IN DYN_HII_STR_HANDLE StrPkgHandle
)
{
LIST_ENTRY *Link;
LIST_ENTRY *Next;
DYN_HII_STR_PKG *StrPkg;
DYN_HII_STR_PKG_INSTANCE *StrPkgInstance;
if (StrPkgHandle == NULL) {
return;
}
StrPkg = StrPkgHandle;
if (StrPkg->Signature != DYN_HII_STR_PKG_SIGNATURE) {
DEBUG ((
DEBUG_ERROR,
"%a: String package signature mismatch found.\n",
__func__
));
return;
}
Link = GetFirstNode (&StrPkg->StrPkgList);
while (!IsNull (&StrPkg->StrPkgList, Link)) {
Next = GetNextNode (&StrPkg->StrPkgList, Link);
StrPkgInstance = BASE_CR (Link, DYN_HII_STR_PKG_INSTANCE, Link);
if (StrPkgInstance->StrPkgHdr != NULL) {
FreePool (StrPkgInstance->StrPkgHdr);
}
if (StrPkgInstance->LanguageCode != NULL) {
FreePool (StrPkgInstance->LanguageCode);
}
DynHiiFreeStrings (&StrPkgInstance->StringList);
RemoveEntryList (Link);
FreePool (StrPkgInstance);
Link = Next;
}
FreePool (StrPkg);
}