X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FHiiDatabaseDxe%2FString.c;h=a76e5b64c93199f3144ac0615f6a54da5220df0d;hb=7c9fbd79d1fc8b817acb72c61cc19605d2f24907;hp=0d830106cd7532a55bc6fd964fffeb25d287e2da;hpb=ac644614683362804223844048c5be1a2adfa6b7;p=mirror_edk2.git diff --git a/MdeModulePkg/Universal/HiiDatabaseDxe/String.c b/MdeModulePkg/Universal/HiiDatabaseDxe/String.c index 0d830106cd..a76e5b64c9 100644 --- a/MdeModulePkg/Universal/HiiDatabaseDxe/String.c +++ b/MdeModulePkg/Universal/HiiDatabaseDxe/String.c @@ -1,7 +1,9 @@ /** @file +Implementation for EFI_HII_STRING_PROTOCOL. -Copyright (c) 2007, Intel Corporation -All rights reserved. This program and the accompanying materials + +Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.
+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 http://opensource.org/licenses/bsd-license.php @@ -9,17 +11,6 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - String.c - -Abstract: - - Implementation for EFI_HII_STRING_PROTOCOL. - -Revision History - - **/ @@ -38,8 +29,12 @@ CHAR16 mLanguageWindow[16] = { font info list or not. (i.e. HII_FONT_INFO is generated.) If not, create a HII_FONT_INFO to refer it locally. + This is a internal function. + + @param Private Hii database private structure. @param StringPackage HII string package instance. + @param FontId Font identifer, which must be unique within the string package. @param DuplicateEnable If true, duplicate HII_FONT_INFO which refers to the same EFI_FONT_INFO is permitted. Otherwise it is not allowed. @@ -52,11 +47,11 @@ CHAR16 mLanguageWindow[16] = { @retval FALSE Not referred before calling this function. **/ -STATIC BOOLEAN ReferFontInfoLocally ( IN HII_DATABASE_PRIVATE_DATA *Private, IN HII_STRING_PACKAGE_INSTANCE *StringPackage, + IN UINT8 FontId, IN BOOLEAN DuplicateEnable, IN HII_GLOBAL_FONT_INFO *GlobalFontInfo, OUT HII_FONT_INFO **LocalFontInfo @@ -82,11 +77,6 @@ ReferFontInfoLocally ( } } } - // - // Since string package tool set FontId initially to 0 and increases it - // progressively by one, StringPackage->FondId always represents an unique - // and available FontId. - // // FontId identifies EFI_FONT_INFO in local string package uniquely. // GlobalEntry points to a HII_GLOBAL_FONT_INFO which identifies // EFI_FONT_INFO uniquely in whole hii database. @@ -95,12 +85,10 @@ ReferFontInfoLocally ( ASSERT (LocalFont != NULL); LocalFont->Signature = HII_FONT_INFO_SIGNATURE; - LocalFont->FontId = StringPackage->FontId; + LocalFont->FontId = FontId; LocalFont->GlobalEntry = &GlobalFontInfo->Entry; InsertTailList (&StringPackage->FontInfoList, &LocalFont->Entry); - StringPackage->FontId++; - *LocalFontInfo = LocalFont; return FALSE; } @@ -109,8 +97,12 @@ ReferFontInfoLocally ( /** Convert Ascii string text to unicode string test. - @param StringSrc Points to current null-terminated Ascii string. - @param StringDest Buffer to store the converted string text. + This is a internal function. + + + @param StringDest Buffer to store the string text. If it is NULL, + only the size will be returned. + @param StringSrc Points to current null-terminated string. @param BufferSize Length of the buffer. @retval EFI_SUCCESS The string text was outputed successfully. @@ -119,7 +111,6 @@ ReferFontInfoLocally ( size. **/ -STATIC EFI_STATUS ConvertToUnicodeText ( OUT EFI_STRING StringDest, @@ -133,7 +124,7 @@ ConvertToUnicodeText ( ASSERT (StringSrc != NULL && BufferSize != NULL); StringSize = AsciiStrSize (StringSrc) * 2; - if (*BufferSize < StringSize) { + if (*BufferSize < StringSize || StringDest == NULL) { *BufferSize = StringSize; return EFI_BUFFER_TOO_SMALL; } @@ -151,8 +142,11 @@ ConvertToUnicodeText ( Calculate the size of StringSrc and output it. If StringDest is not NULL, copy string text from src to dest. + This is a internal function. + + @param StringDest Buffer to store the string text. If it is NULL, + only the size will be returned. @param StringSrc Points to current null-terminated string. - @param StringDest Buffer to store the string text. @param BufferSize Length of the buffer. @retval EFI_SUCCESS The string text was outputed successfully. @@ -161,7 +155,6 @@ ConvertToUnicodeText ( size. **/ -STATIC EFI_STATUS GetUnicodeStringTextOrSize ( OUT EFI_STRING StringDest, OPTIONAL @@ -170,26 +163,23 @@ GetUnicodeStringTextOrSize ( ) { UINTN StringSize; - CHAR16 Zero; UINT8 *StringPtr; ASSERT (StringSrc != NULL && BufferSize != NULL); - ZeroMem (&Zero, sizeof (CHAR16)); StringSize = sizeof (CHAR16); StringPtr = StringSrc; - while (CompareMem (StringPtr, &Zero, sizeof (CHAR16)) != 0) { + while (ReadUnaligned16 ((UINT16 *) StringPtr) != 0) { StringSize += sizeof (CHAR16); StringPtr += sizeof (CHAR16); } + if (*BufferSize < StringSize) { + *BufferSize = StringSize; + return EFI_BUFFER_TOO_SMALL; + } if (StringDest != NULL) { - if (*BufferSize < StringSize) { - *BufferSize = StringSize; - return EFI_BUFFER_TOO_SMALL; - } CopyMem (StringDest, StringSrc, StringSize); - return EFI_SUCCESS; } *BufferSize = StringSize; @@ -200,6 +190,8 @@ GetUnicodeStringTextOrSize ( /** Copy string font info to a buffer. + This is a internal function. + @param StringPackage Hii string package instance. @param FontId Font identifier which is unique in a string package. @@ -210,7 +202,6 @@ GetUnicodeStringTextOrSize ( @retval EFI_NOT_FOUND The specified font id does not exist. **/ -STATIC EFI_STATUS GetStringFontInfo ( IN HII_STRING_PACKAGE_INSTANCE *StringPackage, @@ -244,25 +235,27 @@ GetStringFontInfo ( /** Parse all string blocks to find a String block specified by StringId. If StringId = (EFI_STRING_ID) (-1), find out all EFI_HII_SIBT_FONT blocks - within this string package and backup its information. - If StringId = 0, output the string id of last string block (EFI_HII_SIBT_END). - - @param Private Hii database private structure. - @param StringPackage Hii string package instance. - @param StringId The string's id, which is unique within - PackageList. - @param BlockType Output the block type of found string block. - @param StringBlockAddr Output the block address of found string block. - @param StringTextOffset Offset, relative to the found block address, of - the string text information. - @param LastStringId Output the last string id when StringId = 0. - - @retval EFI_SUCCESS The string text and font is retrieved - successfully. - @retval EFI_NOT_FOUND The specified text or font info can not be found - out. - @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the - task. + within this string package and backup its information. If LastStringId is + specified, the string id of last string block will also be output. + If StringId = 0, output the string id of last string block (EFI_HII_SIBT_STRING). + + @param Private Hii database private structure. + @param StringPackage Hii string package instance. + @param StringId The string's id, which is unique within + PackageList. + @param BlockType Output the block type of found string block. + @param StringBlockAddr Output the block address of found string block. + @param StringTextOffset Offset, relative to the found block address, of + the string text information. + @param LastStringId Output the last string id when StringId = 0 or StringId = -1. + @param StartStringId The first id in the skip block which StringId in the block. + + @retval EFI_SUCCESS The string text and font is retrieved + successfully. + @retval EFI_NOT_FOUND The specified text or font info can not be found + out. + @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the + task. **/ EFI_STATUS @@ -273,7 +266,8 @@ FindStringBlock ( OUT UINT8 *BlockType, OPTIONAL OUT UINT8 **StringBlockAddr, OPTIONAL OUT UINTN *StringTextOffset, OPTIONAL - OUT EFI_STRING_ID *LastStringId OPTIONAL + OUT EFI_STRING_ID *LastStringId, OPTIONAL + OUT EFI_STRING_ID *StartStringId OPTIONAL ) { UINT8 *BlockHdr; @@ -292,6 +286,7 @@ FindStringBlock ( UINT16 FontSize; UINT8 Length8; EFI_HII_SIBT_EXT2_BLOCK Ext2; + UINT8 FontId; UINT32 Length32; UINTN StringSize; CHAR16 Zero; @@ -303,8 +298,15 @@ FindStringBlock ( if (StringId != (EFI_STRING_ID) (-1) && StringId != 0) { ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL); + if (StringId > StringPackage->MaxStringId) { + return EFI_NOT_FOUND; + } } else { ASSERT (Private != NULL && Private->Signature == HII_DATABASE_PRIVATE_DATA_SIGNATURE); + if (StringId == 0 && LastStringId != NULL) { + *LastStringId = StringPackage->MaxStringId; + return EFI_SUCCESS; + } } ZeroMem (&Zero, sizeof (CHAR16)); @@ -339,6 +341,7 @@ FindStringBlock ( for (Index = 0; Index < StringCount; Index++) { BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr); if (CurrentStringId == StringId) { + ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL); *BlockType = *BlockHdr; *StringBlockAddr = BlockHdr; *StringTextOffset = StringTextPtr - BlockHdr; @@ -361,6 +364,7 @@ FindStringBlock ( for (Index = 0; Index < StringCount; Index++) { BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr); if (CurrentStringId == StringId) { + ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL); *BlockType = *BlockHdr; *StringBlockAddr = BlockHdr; *StringTextOffset = StringTextPtr - BlockHdr; @@ -404,6 +408,7 @@ FindStringBlock ( GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize); BlockSize += StringSize; if (CurrentStringId == StringId) { + ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL); *BlockType = *BlockHdr; *StringBlockAddr = BlockHdr; *StringTextOffset = StringTextPtr - BlockHdr; @@ -427,6 +432,7 @@ FindStringBlock ( GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize); BlockSize += StringSize; if (CurrentStringId == StringId) { + ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL); *BlockType = *BlockHdr; *StringBlockAddr = BlockHdr; *StringTextOffset = StringTextPtr - BlockHdr; @@ -486,7 +492,9 @@ FindStringBlock ( // Find the relationship between global font info and the font info of // this EFI_HII_SIBT_FONT block then backup its information in local package. // - BlockHdr += sizeof (EFI_HII_SIBT_EXT2_BLOCK) + sizeof (UINT8); + BlockHdr += sizeof (EFI_HII_SIBT_EXT2_BLOCK); + CopyMem (&FontId, BlockHdr, sizeof (UINT8)); + BlockHdr += sizeof (UINT8); CopyMem (&FontSize, BlockHdr, sizeof (UINT16)); BlockHdr += sizeof (UINT16); CopyMem (&FontStyle, BlockHdr, sizeof (EFI_HII_FONT_STYLE)); @@ -502,17 +510,22 @@ FindStringBlock ( FontInfo->FontSize = FontSize; CopyMem (FontInfo->FontName, BlockHdr, StringSize); + // + // If find the corresponding global font info, save the relationship. + // Otherwise ignore this EFI_HII_SIBT_FONT block. + // if (IsFontInfoExisted (Private, FontInfo, NULL, NULL, &GlobalFont)) { - // - // If find the corresponding global font info, save the relationship. - // - ReferFontInfoLocally (Private, StringPackage, TRUE, GlobalFont, &LocalFont); + ReferFontInfoLocally (Private, StringPackage, FontId, TRUE, GlobalFont, &LocalFont); } // - // If can not find, ignore this EFI_HII_SIBT_FONT block. - // - SafeFreePool (FontInfo); + // Since string package tool set FontId initially to 0 and increases it + // progressively by one, StringPackage->FondId always represents an unique + // and available FontId. + // + StringPackage->FontId++; + + FreePool (FontInfo); } BlockSize += Ext2.Length; @@ -533,12 +546,21 @@ FindStringBlock ( break; } - if (StringId > 0) { + if (StringId > 0 && StringId != (EFI_STRING_ID)(-1)) { + ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL); + *BlockType = *BlockHdr; + *StringBlockAddr = BlockHdr; + *StringTextOffset = Offset; + if (StringId == CurrentStringId - 1) { - *BlockType = *BlockHdr; - *StringBlockAddr = BlockHdr; - *StringTextOffset = Offset; - return EFI_SUCCESS; + // + // if only one skip item, return EFI_NOT_FOUND. + // + if(*BlockType == EFI_HII_SIBT_SKIP2 || *BlockType == EFI_HII_SIBT_SKIP1) { + return EFI_NOT_FOUND; + } else { + return EFI_SUCCESS; + } } if (StringId < CurrentStringId - 1) { @@ -546,15 +568,16 @@ FindStringBlock ( } } BlockHdr = StringPackage->StringBlock + BlockSize; - - } - - if (StringId == (EFI_STRING_ID) (-1)) { - return EFI_SUCCESS; + if (StartStringId != NULL) { + *StartStringId = CurrentStringId; + } } - - if (StringId == 0 && LastStringId != NULL) { - *LastStringId = CurrentStringId; + + // + // Get last string ID + // + if (StringId == (EFI_STRING_ID) (-1) && LastStringId != NULL) { + *LastStringId = (EFI_STRING_ID) (CurrentStringId - 1); return EFI_SUCCESS; } @@ -565,6 +588,8 @@ FindStringBlock ( /** Parse all string blocks to get a string specified by StringId. + This is a internal function. + @param Private Hii database private structure. @param StringPackage Hii string package instance. @param StringId The string's id, which is unique within @@ -585,14 +610,13 @@ FindStringBlock ( hold the string. **/ -STATIC EFI_STATUS GetStringWorker ( IN HII_DATABASE_PRIVATE_DATA *Private, IN HII_STRING_PACKAGE_INSTANCE *StringPackage, IN EFI_STRING_ID StringId, OUT EFI_STRING String, - IN OUT UINTN *StringSize, + IN OUT UINTN *StringSize, OPTIONAL OUT EFI_FONT_INFO **StringFontInfo OPTIONAL ) { @@ -603,7 +627,7 @@ GetStringWorker ( EFI_STATUS Status; UINT8 FontId; - ASSERT (StringPackage != NULL && StringSize != NULL); + ASSERT (StringPackage != NULL); ASSERT (Private != NULL && Private->Signature == HII_DATABASE_PRIVATE_DATA_SIGNATURE); // @@ -616,12 +640,20 @@ GetStringWorker ( &BlockType, &StringBlockAddr, &StringTextOffset, + NULL, NULL ); if (EFI_ERROR (Status)) { return Status; } + if (StringSize == NULL) { + // + // String text buffer is not requested + // + return EFI_SUCCESS; + } + // // Get the string text. // @@ -647,7 +679,8 @@ GetStringWorker ( } // - // Get the string font. + // Get the string font. The FontId 0 is the default font for those string blocks which + // do not specify a font identifier. If default font is not specified, return NULL. // if (StringFontInfo != NULL) { switch (BlockType) { @@ -656,20 +689,172 @@ GetStringWorker ( case EFI_HII_SIBT_STRING_UCS2_FONT: case EFI_HII_SIBT_STRINGS_UCS2_FONT: FontId = *(StringBlockAddr + sizeof (EFI_HII_STRING_BLOCK)); - return GetStringFontInfo (StringPackage, FontId, StringFontInfo); break; default: - break; + FontId = 0; + } + Status = GetStringFontInfo (StringPackage, FontId, StringFontInfo); + if (Status == EFI_NOT_FOUND) { + *StringFontInfo = NULL; } } return EFI_SUCCESS; } +/** + If GetStringBlock find the StringId's string is not saved in the exist string block, + this function will create the UCS2 string block to save the string; also split the + skip block into two or one skip block. + + This is a internal function. + + @param StringPackage Hii string package instance. + @param StartStringId The first id in the skip block which StringId in the block. + @param StringId The string's id, which is unique within + PackageList. + @param BlockType Output the block type of found string block. + @param StringBlockAddr Output the block address of found string block. + @param FontBlock whether this string block has font info. + + @retval EFI_SUCCESS The string font is outputed successfully. + @retval EFI_OUT_OF_RESOURCES NO resource for the memory to save the new string block. + +**/ +EFI_STATUS +InsertLackStringBlock ( + IN OUT HII_STRING_PACKAGE_INSTANCE *StringPackage, + IN EFI_STRING_ID StartStringId, + IN EFI_STRING_ID StringId, + IN OUT UINT8 *BlockType, + IN OUT UINT8 **StringBlockAddr, + IN BOOLEAN FontBlock + ) +{ + UINT8 *BlockPtr; + UINT8 *StringBlock; + UINT32 SkipLen; + UINT32 OldBlockSize; + UINT32 NewBlockSize; + UINT32 FrontSkipNum; + UINT32 NewUCSBlockLen; + UINT8 *OldStringAddr; + UINT32 IdCount; + + FrontSkipNum = 0; + SkipLen = 0; + OldStringAddr = *StringBlockAddr; + + ASSERT (*BlockType == EFI_HII_SIBT_SKIP1 || *BlockType == EFI_HII_SIBT_SKIP2); + // + // Old skip block size. + // + if (*BlockType == EFI_HII_SIBT_SKIP1) { + SkipLen = sizeof (EFI_HII_SIBT_SKIP1_BLOCK); + IdCount = *(UINT8*)(OldStringAddr + sizeof (EFI_HII_STRING_BLOCK)); + } else { + SkipLen = sizeof (EFI_HII_SIBT_SKIP2_BLOCK); + IdCount = *(UINT16*)(OldStringAddr + sizeof (EFI_HII_STRING_BLOCK)); + } + + // + // New create UCS or UCS2 block size. + // + if (FontBlock) { + NewUCSBlockLen = sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK); + } else { + NewUCSBlockLen = sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK); + } + + OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize; + + if (StartStringId == StringId) { + // + // New block + [Skip block] + // + if (IdCount > 1) { + NewBlockSize = OldBlockSize + NewUCSBlockLen; + } else { + NewBlockSize = OldBlockSize + NewUCSBlockLen - SkipLen; + } + } else if (StartStringId + IdCount - 1 == StringId){ + // + // Skip block + New block + // + NewBlockSize = OldBlockSize + NewUCSBlockLen; + FrontSkipNum = StringId - StartStringId; + } else { + // + // Skip block + New block + [Skip block] + // + NewBlockSize = OldBlockSize + NewUCSBlockLen + SkipLen; + FrontSkipNum = StringId - StartStringId; + } + + StringBlock = (UINT8 *) AllocateZeroPool (NewBlockSize); + if (StringBlock == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Copy old block in front of skip block. + // + CopyMem (StringBlock, StringPackage->StringBlock, OldStringAddr - StringPackage->StringBlock); + BlockPtr = StringBlock + (OldStringAddr - StringPackage->StringBlock); + + if (FrontSkipNum > 0) { + *BlockPtr = *BlockType; + if (*BlockType == EFI_HII_SIBT_SKIP1) { + *(BlockPtr + sizeof (EFI_HII_STRING_BLOCK)) = (UINT8) FrontSkipNum; + } else { + *(UINT16 *)(BlockPtr + sizeof (EFI_HII_STRING_BLOCK)) = (UINT16) FrontSkipNum; + } + BlockPtr += SkipLen; + } + + // + // Create a EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK + // + *StringBlockAddr = BlockPtr; + if (FontBlock) { + *BlockPtr = EFI_HII_SIBT_STRING_UCS2_FONT; + } else { + *BlockPtr = EFI_HII_SIBT_STRING_UCS2; + } + BlockPtr += NewUCSBlockLen; + + if (IdCount > FrontSkipNum + 1) { + *BlockPtr = *BlockType; + if (*BlockType == EFI_HII_SIBT_SKIP1) { + *(BlockPtr + sizeof (EFI_HII_STRING_BLOCK)) = (UINT8) (IdCount - FrontSkipNum - 1); + } else { + *(UINT16 *)(BlockPtr + sizeof (EFI_HII_STRING_BLOCK)) = (UINT16) (IdCount - FrontSkipNum - 1); + } + BlockPtr += SkipLen; + } + + // + // Append a EFI_HII_SIBT_END block to the end. + // + CopyMem (BlockPtr, OldStringAddr + SkipLen, OldBlockSize - (OldStringAddr - StringPackage->StringBlock) - SkipLen); + + if (FontBlock) { + *BlockType = EFI_HII_SIBT_STRING_UCS2_FONT; + } else { + *BlockType = EFI_HII_SIBT_STRING_UCS2; + } + FreePool (StringPackage->StringBlock); + StringPackage->StringBlock = StringBlock; + StringPackage->StringPkgHdr->Header.Length += NewBlockSize - OldBlockSize; + + return EFI_SUCCESS; +} /** Parse all string blocks to set a String specified by StringId. + This is a internal function. + @param Private HII database driver private structure. @param StringPackage HII string package instance. @param StringId The string's id, which is unique within @@ -687,7 +872,6 @@ GetStringWorker ( task. **/ -STATIC EFI_STATUS SetStringWorker ( IN HII_DATABASE_PRIVATE_DATA *Private, @@ -712,8 +896,9 @@ SetStringWorker ( EFI_HII_SIBT_EXT2_BLOCK Ext2; UINTN StringSize; UINTN TmpSize; + EFI_STRING_ID StartStringId; - + StartStringId = 0; ASSERT (Private != NULL && StringPackage != NULL && String != NULL); ASSERT (Private->Signature == HII_DATABASE_PRIVATE_DATA_SIGNATURE); // @@ -726,10 +911,25 @@ SetStringWorker ( &BlockType, &StringBlockAddr, &StringTextOffset, - NULL + NULL, + &StartStringId ); - if (EFI_ERROR (Status)) { - return Status; + if (EFI_ERROR (Status) && (BlockType == EFI_HII_SIBT_SKIP1 || BlockType == EFI_HII_SIBT_SKIP2)) { + Status = InsertLackStringBlock(StringPackage, + StartStringId, + StringId, + &BlockType, + &StringBlockAddr, + (BOOLEAN)(StringFontInfo != NULL) + ); + if (EFI_ERROR (Status)) { + return Status; + } + if (StringFontInfo != NULL) { + StringTextOffset = sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK) - sizeof (CHAR16); + } else { + StringTextOffset = sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK) - sizeof (CHAR16); + } } LocalFont = NULL; @@ -737,38 +937,48 @@ SetStringWorker ( Referred = FALSE; // - // Set the string font according to input font information. + // The input StringFontInfo should exist in current database if specified. // if (StringFontInfo != NULL) { - // - // The input StringFontInfo should exist in current database - // if (!IsFontInfoExisted (Private, StringFontInfo, NULL, NULL, &GlobalFont)) { return EFI_INVALID_PARAMETER; } else { - Referred = ReferFontInfoLocally (Private, StringPackage, FALSE, GlobalFont, &LocalFont); + Referred = ReferFontInfoLocally ( + Private, + StringPackage, + StringPackage->FontId, + FALSE, + GlobalFont, + &LocalFont + ); + if (!Referred) { + StringPackage->FontId++; + } } - // - // Update the FontId of the specified string block + // Update the FontId of the specified string block to input font info. // switch (BlockType) { - case EFI_HII_SIBT_STRING_SCSU_FONT: + case EFI_HII_SIBT_STRING_SCSU_FONT: case EFI_HII_SIBT_STRINGS_SCSU_FONT: case EFI_HII_SIBT_STRING_UCS2_FONT: case EFI_HII_SIBT_STRINGS_UCS2_FONT: *(StringBlockAddr + sizeof (EFI_HII_STRING_BLOCK)) = LocalFont->FontId; break; default: - return EFI_NOT_FOUND; + // + // When modify the font info of these blocks, the block type should be updated + // to contain font info thus the whole structure should be revised. + // It is recommended to use tool to modify the block type not in the code. + // + return EFI_UNSUPPORTED; } - } OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize; // - // Set the string text. + // Set the string text and font. // StringTextPtr = StringBlockAddr + StringTextOffset; switch (BlockType) { @@ -777,7 +987,7 @@ SetStringWorker ( case EFI_HII_SIBT_STRINGS_SCSU: case EFI_HII_SIBT_STRINGS_SCSU_FONT: BlockSize = OldBlockSize + StrLen (String); - BlockSize -= AsciiStrLen ((CHAR8 *) StringTextPtr); + BlockSize -= AsciiStrSize ((CHAR8 *) StringTextPtr); Block = AllocateZeroPool (BlockSize); if (Block == NULL) { return EFI_OUT_OF_RESOURCES; @@ -799,7 +1009,7 @@ SetStringWorker ( TmpSize ); - SafeFreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringBlock); StringPackage->StringBlock = Block; StringPackage->StringPkgHdr->Header.Length += (UINT32) (BlockSize - OldBlockSize); break; @@ -832,7 +1042,7 @@ SetStringWorker ( OldBlockSize - (StringTextPtr - StringPackage->StringBlock) - StringSize ); - SafeFreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringBlock); StringPackage->StringBlock = Block; StringPackage->StringPkgHdr->Header.Length += (UINT32) (BlockSize - OldBlockSize); break; @@ -883,7 +1093,7 @@ SetStringWorker ( CopyMem (BlockPtr, StringPackage->StringBlock, OldBlockSize); - SafeFreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringBlock); StringPackage->StringBlock = Block; StringPackage->StringPkgHdr->Header.Length += Ext2.Length; @@ -894,7 +1104,10 @@ SetStringWorker ( /** This function adds the string String to the group of strings owned by PackageList, with the - specified font information StringFontInfo and returns a new string id. + specified font information StringFontInfo and returns a new string id. + The new string identifier is guaranteed to be unique within the package list. + That new string identifier is reserved for all languages in the package list. + @param This A pointer to the EFI_HII_STRING_PROTOCOL instance. @param PackageList Handle of the package list where this string will @@ -937,7 +1150,6 @@ HiiNewString ( { EFI_STATUS Status; LIST_ENTRY *Link; - BOOLEAN Matched; HII_DATABASE_PRIVATE_DATA *Private; HII_DATABASE_RECORD *DatabaseRecord; HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode; @@ -953,6 +1165,12 @@ HiiNewString ( EFI_HII_SIBT_EXT2_BLOCK Ext2; HII_FONT_INFO *LocalFont; HII_GLOBAL_FONT_INFO *GlobalFont; + EFI_STRING_ID NewStringId; + EFI_STRING_ID NextStringId; + EFI_STRING_ID Index; + HII_STRING_PACKAGE_INSTANCE *MatchStringPackage; + BOOLEAN NewStringPackageCreated; + if (This == NULL || String == NULL || StringId == NULL || Language == NULL || PackageList == NULL) { return EFI_INVALID_PARAMETER; @@ -989,37 +1207,114 @@ HiiNewString ( return EFI_NOT_FOUND; } - // - // Try to get the matching string package. Create a new string package when failed. - // + Status = EFI_SUCCESS; + NewStringPackageCreated = FALSE; + NewStringId = 0; + NextStringId = 0; StringPackage = NULL; - Matched = FALSE; + MatchStringPackage = NULL; for (Link = PackageListNode->StringPkgHdr.ForwardLink; Link != &PackageListNode->StringPkgHdr; Link = Link->ForwardLink ) { StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); - if (R8_EfiLibCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language)) { - Matched = TRUE; - break; + // + // Create a string block and corresponding font block if exists, then append them + // to the end of the string package. + // + Status = FindStringBlock ( + Private, + StringPackage, + 0, + NULL, + NULL, + NULL, + &NextStringId, + NULL + ); + if (EFI_ERROR (Status)) { + goto Done; + } + // + // Make sure that new StringId is same in all String Packages for the different language. + // + if (NewStringId != 0 && NewStringId != NextStringId) { + ASSERT (FALSE); + Status = EFI_INVALID_PARAMETER; + goto Done; } + NewStringId = NextStringId; + // + // Get the matched string package with language. + // + if (HiiCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language)) { + MatchStringPackage = StringPackage; + } else { + OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize; + // + // Create a blank EFI_HII_SIBT_STRING_UCS2_BLOCK to reserve new string ID. + // + Ucs2BlockSize = (UINT32) sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK); + + StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + Ucs2BlockSize); + if (StringBlock == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Done; + } + // + // Copy original string blocks, except the EFI_HII_SIBT_END. + // + CopyMem (StringBlock, StringPackage->StringBlock, OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK)); + // + // Create a blank EFI_HII_SIBT_STRING_UCS2 block + // + BlockPtr = StringBlock + OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK); + *BlockPtr = EFI_HII_SIBT_STRING_UCS2; + BlockPtr += sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK); + + // + // Append a EFI_HII_SIBT_END block to the end. + // + *BlockPtr = EFI_HII_SIBT_END; + FreePool (StringPackage->StringBlock); + StringPackage->StringBlock = StringBlock; + StringPackage->StringPkgHdr->Header.Length += Ucs2BlockSize; + PackageListNode->PackageListHdr.PackageLength += Ucs2BlockSize; + } + } + if (NewStringId == 0) { + // + // No string package is found. + // Create new string package. StringId 1 is reserved for Language Name string. + // + *StringId = 2; + } else { + // + // Set new StringId + // + *StringId = (EFI_STRING_ID) (NewStringId + 1); } - if (!Matched) { + if (MatchStringPackage != NULL) { + StringPackage = MatchStringPackage; + } else { // // LanguageName is required to create a new string package. // if (LanguageName == NULL) { - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } StringPackage = AllocateZeroPool (sizeof (HII_STRING_PACKAGE_INSTANCE)); if (StringPackage == NULL) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } - StringPackage->Signature = HII_STRING_PACKAGE_SIGNATURE; - StringPackage->FontId = 0; + StringPackage->Signature = HII_STRING_PACKAGE_SIGNATURE; + StringPackage->MaxStringId = *StringId; + StringPackage->FontId = 0; InitializeListHead (&StringPackage->FontInfoList); // @@ -1028,13 +1323,14 @@ HiiNewString ( HeaderSize = (UINT32) (AsciiStrSize ((CHAR8 *) Language) - 1 + sizeof (EFI_HII_STRING_PACKAGE_HDR)); StringPackage->StringPkgHdr = AllocateZeroPool (HeaderSize); if (StringPackage->StringPkgHdr == NULL) { - SafeFreePool (StringPackage); - return EFI_OUT_OF_RESOURCES; + FreePool (StringPackage); + Status = EFI_OUT_OF_RESOURCES; + goto Done; } StringPackage->StringPkgHdr->Header.Type = EFI_HII_PACKAGE_STRINGS; StringPackage->StringPkgHdr->HdrSize = HeaderSize; StringPackage->StringPkgHdr->StringInfoOffset = HeaderSize; - CopyMem (StringPackage->StringPkgHdr->LanguageWindow, mLanguageWindow, 16 * sizeof (CHAR16));; + CopyMem (StringPackage->StringPkgHdr->LanguageWindow, mLanguageWindow, 16 * sizeof (CHAR16)); StringPackage->StringPkgHdr->LanguageName = 1; AsciiStrCpy (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language); @@ -1042,15 +1338,16 @@ HiiNewString ( // Calculate the length of the string blocks, including string block to record // printable language full name and EFI_HII_SIBT_END_BLOCK. // - Ucs2BlockSize = (UINT32) (StrSize ((CHAR16 *) LanguageName) + - sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK) - sizeof (CHAR16)); + Ucs2BlockSize = (UINT32) (StrSize ((CHAR16 *) LanguageName) + + (*StringId - 1) * sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK) - sizeof (CHAR16)); BlockSize = Ucs2BlockSize + sizeof (EFI_HII_SIBT_END_BLOCK); StringPackage->StringBlock = (UINT8 *) AllocateZeroPool (BlockSize); if (StringPackage->StringBlock == NULL) { - SafeFreePool (StringPackage->StringPkgHdr); - SafeFreePool (StringPackage); - return EFI_OUT_OF_RESOURCES; + FreePool (StringPackage->StringPkgHdr); + FreePool (StringPackage); + Status = EFI_OUT_OF_RESOURCES; + goto Done; } // @@ -1061,7 +1358,10 @@ HiiNewString ( BlockPtr += sizeof (EFI_HII_STRING_BLOCK); CopyMem (BlockPtr, (EFI_STRING) LanguageName, StrSize ((EFI_STRING) LanguageName)); BlockPtr += StrSize ((EFI_STRING) LanguageName); - + for (Index = 2; Index <= *StringId - 1; Index ++) { + *BlockPtr = EFI_HII_SIBT_STRING_UCS2; + BlockPtr += sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK); + } // // Insert the end block // @@ -1073,24 +1373,7 @@ HiiNewString ( StringPackage->StringPkgHdr->Header.Length = HeaderSize + BlockSize; PackageListNode->PackageListHdr.PackageLength += StringPackage->StringPkgHdr->Header.Length; InsertTailList (&PackageListNode->StringPkgHdr, &StringPackage->StringEntry); - - } - - // - // Create a string block and corresponding font block if exists, then append them - // to the end of the string package. - // - Status = FindStringBlock ( - Private, - StringPackage, - 0, - NULL, - NULL, - NULL, - StringId - ); - if (EFI_ERROR (Status)) { - return Status; + NewStringPackageCreated = TRUE; } OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize; @@ -1099,13 +1382,13 @@ HiiNewString ( // // Create a EFI_HII_SIBT_STRING_UCS2_BLOCK since font info is not specified. // - Ucs2BlockSize = (UINT32) (StrSize (String) + sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK) - sizeof (CHAR16)); StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + Ucs2BlockSize); if (StringBlock == NULL) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } // // Copy original string blocks, except the EFI_HII_SIBT_END. @@ -1124,7 +1407,7 @@ HiiNewString ( // Append a EFI_HII_SIBT_END block to the end. // *BlockPtr = EFI_HII_SIBT_END; - SafeFreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringBlock); StringPackage->StringBlock = StringBlock; StringPackage->StringPkgHdr->Header.Length += Ucs2BlockSize; PackageListNode->PackageListHdr.PackageLength += Ucs2BlockSize; @@ -1138,13 +1421,14 @@ HiiNewString ( // Ucs2FontBlockSize = (UINT32) (StrSize (String) + sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK) - sizeof (CHAR16)); - if (ReferFontInfoLocally (Private, StringPackage, FALSE, GlobalFont, &LocalFont)) { + if (ReferFontInfoLocally (Private, StringPackage, StringPackage->FontId, FALSE, GlobalFont, &LocalFont)) { // // Create a EFI_HII_SIBT_STRING_UCS2_FONT block only. // StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + Ucs2FontBlockSize); if (StringBlock == NULL) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } // // Copy original string blocks, except the EFI_HII_SIBT_END. @@ -1165,7 +1449,7 @@ HiiNewString ( // Append a EFI_HII_SIBT_END block to the end. // *BlockPtr = EFI_HII_SIBT_END; - SafeFreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringBlock); StringPackage->StringBlock = StringBlock; StringPackage->StringPkgHdr->Header.Length += Ucs2FontBlockSize; PackageListNode->PackageListHdr.PackageLength += Ucs2FontBlockSize; @@ -1180,7 +1464,8 @@ HiiNewString ( sizeof (EFI_HII_SIBT_FONT_BLOCK) - sizeof (CHAR16)); StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + FontBlockSize + Ucs2FontBlockSize); if (StringBlock == NULL) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } // // Copy original string blocks, except the EFI_HII_SIBT_END. @@ -1210,7 +1495,7 @@ HiiNewString ( &((EFI_FONT_INFO *) StringFontInfo)->FontName, StrSize (((EFI_FONT_INFO *) StringFontInfo)->FontName) ); - + BlockPtr += StrSize (((EFI_FONT_INFO *) StringFontInfo)->FontName); // // Create a EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK // @@ -1225,14 +1510,55 @@ HiiNewString ( // Append a EFI_HII_SIBT_END block to the end. // *BlockPtr = EFI_HII_SIBT_END; - SafeFreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringBlock); StringPackage->StringBlock = StringBlock; StringPackage->StringPkgHdr->Header.Length += FontBlockSize + Ucs2FontBlockSize; PackageListNode->PackageListHdr.PackageLength += FontBlockSize + Ucs2FontBlockSize; + + // + // Increase the FontId to make it unique since we already add + // a EFI_HII_SIBT_FONT block to this string package. + // + StringPackage->FontId++; } } - return EFI_SUCCESS; +Done: + if (!EFI_ERROR (Status) && NewStringPackageCreated) { + // + // Trigger any registered notification function for new string package + // + Status = InvokeRegisteredFunction ( + Private, + EFI_HII_DATABASE_NOTIFY_NEW_PACK, + (VOID *) StringPackage, + EFI_HII_PACKAGE_STRINGS, + PackageList + ); + } + + if (!EFI_ERROR (Status)) { + // + // Update MaxString Id to new StringId + // + for (Link = PackageListNode->StringPkgHdr.ForwardLink; + Link != &PackageListNode->StringPkgHdr; + Link = Link->ForwardLink + ) { + StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); + StringPackage->MaxStringId = *StringId; + } + } else if (NewStringPackageCreated) { + // + // Free the allocated new string Package when new string can't be added. + // + RemoveEntryList (&StringPackage->StringEntry); + FreePool (StringPackage->StringBlock); + FreePool (StringPackage->StringPkgHdr); + FreePool (StringPackage); + } + + return Status; } @@ -1258,7 +1584,9 @@ HiiNewString ( @retval EFI_SUCCESS The string was returned successfully. @retval EFI_NOT_FOUND The string specified by StringId is not available. @retval EFI_NOT_FOUND The string specified by StringId is available but - not in the specified language. + not in the specified language. + The specified PackageList is not in the database. + @retval EFI_INVALID_LANGUAGE - The string specified by StringId is available but @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringSize is too small to hold the string. @retval EFI_INVALID_PARAMETER The String or Language or StringSize was NULL. @@ -1309,18 +1637,34 @@ HiiGetString ( } if (PackageListNode != NULL) { + // + // First search: to match the StringId in the specified language. + // for (Link = PackageListNode->StringPkgHdr.ForwardLink; Link != &PackageListNode->StringPkgHdr; Link = Link->ForwardLink ) { - StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); - if (R8_EfiLibCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language)) { - Status = GetStringWorker (Private, StringPackage, StringId, String, StringSize, StringFontInfo); - if (Status != EFI_NOT_FOUND) { - return Status; + StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); + if (HiiCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language)) { + Status = GetStringWorker (Private, StringPackage, StringId, String, StringSize, StringFontInfo); + if (Status != EFI_NOT_FOUND) { + return Status; + } } } - } + // + // Second search: to match the StringId in other available languages if exist. + // + for (Link = PackageListNode->StringPkgHdr.ForwardLink; + Link != &PackageListNode->StringPkgHdr; + Link = Link->ForwardLink + ) { + StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); + Status = GetStringWorker (Private, StringPackage, StringId, NULL, NULL, NULL); + if (!EFI_ERROR (Status)) { + return EFI_INVALID_LANGUAGE; + } + } } return EFI_NOT_FOUND; @@ -1394,7 +1738,7 @@ HiiSetString ( Link = Link->ForwardLink ) { StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); - if (R8_EfiLibCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language)) { + if (HiiCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language)) { OldPackageLen = StringPackage->StringPkgHdr->Header.Length; Status = SetStringWorker ( Private, @@ -1423,7 +1767,8 @@ HiiSetString ( @param This A pointer to the EFI_HII_STRING_PROTOCOL instance. @param PackageList The package list to examine. - @param Languages Points to the buffer to hold the returned string. + @param Languages Points to the buffer to hold the returned + null-terminated ASCII string. @param LanguagesSize On entry, points to the size of the buffer pointed to by Languages, in bytes. On return, points to the length of Languages, in bytes. @@ -1484,7 +1829,7 @@ HiiGetLanguages ( ) { StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); ResultSize += AsciiStrSize (StringPackage->StringPkgHdr->Language); - if (ResultSize < *LanguagesSize) { + if (ResultSize <= *LanguagesSize) { AsciiStrCpy (Languages, StringPackage->StringPkgHdr->Language); Languages += AsciiStrSize (StringPackage->StringPkgHdr->Language); *(Languages - 1) = L';'; @@ -1511,25 +1856,29 @@ HiiGetLanguages ( @param This A pointer to the EFI_HII_STRING_PROTOCOL instance. @param PackageList The package list to examine. - @param FirstLanguage Points to the primary language. - @param SecondaryLanguages Points to the buffer to hold the returned list of + @param PrimaryLanguage Points to the null-terminated ASCII string that specifies + the primary language. Languages are specified in the + format specified in Appendix M of the UEFI 2.0 specification. + @param SecondaryLanguages Points to the buffer to hold the returned null-terminated + ASCII string that describes the list of secondary languages for the specified - FirstLanguage. If there are no secondary - languages, the function returns successfully, but + PrimaryLanguage. If there are no secondary + languages, the function returns successfully, but this is set to NULL. - @param SecondaryLanguageSize On entry, points to the size of the buffer pointed - to by SecondLanguages, in bytes. On return, - points to the length of SecondLanguages in bytes. + @param SecondaryLanguagesSize On entry, points to the size of the buffer pointed + to by SecondaryLanguages, in bytes. On return, + points to the length of SecondaryLanguages in bytes. @retval EFI_SUCCESS Secondary languages were correctly returned. - @retval EFI_INVALID_PARAMETER FirstLanguage or SecondLanguages or - SecondLanguagesSize was NULL. - @retval EFI_BUFFER_TOO_SMALL The buffer specified by SecondLanguagesSize is + @retval EFI_INVALID_PARAMETER PrimaryLanguage or SecondaryLanguages or + SecondaryLanguagesSize was NULL. + @retval EFI_BUFFER_TOO_SMALL The buffer specified by SecondaryLanguagesSize is too small to hold the returned information. - SecondLanguageSize is updated to hold the size of + SecondaryLanguageSize is updated to hold the size of the buffer required. - @retval EFI_NOT_FOUND The language specified by FirstLanguage is not + @retval EFI_INVALID_LANGUAGE The language specified by PrimaryLanguage is not present in the specified package list. + @retval EFI_NOT_FOUND The specified PackageList is not in the Database. **/ EFI_STATUS @@ -1537,9 +1886,9 @@ EFIAPI HiiGetSecondaryLanguages ( IN CONST EFI_HII_STRING_PROTOCOL *This, IN EFI_HII_HANDLE PackageList, - IN CONST CHAR8 *FirstLanguage, - IN OUT CHAR8 *SecondLanguages, - IN OUT UINTN *SecondLanguagesSize + IN CONST CHAR8 *PrimaryLanguage, + IN OUT CHAR8 *SecondaryLanguages, + IN OUT UINTN *SecondaryLanguagesSize ) { LIST_ENTRY *Link; @@ -1551,10 +1900,10 @@ HiiGetSecondaryLanguages ( CHAR8 *Languages; UINTN ResultSize; - if (This == NULL || PackageList == NULL || FirstLanguage == NULL) { + if (This == NULL || PackageList == NULL || PrimaryLanguage == NULL) { return EFI_INVALID_PARAMETER; } - if (SecondLanguages == NULL || SecondLanguagesSize == NULL) { + if (SecondaryLanguages == NULL || SecondaryLanguagesSize == NULL) { return EFI_INVALID_PARAMETER; } if (!IsHiiHandleValid (PackageList)) { @@ -1562,45 +1911,137 @@ HiiGetSecondaryLanguages ( } Private = HII_STRING_DATABASE_PRIVATE_DATA_FROM_THIS (This); - Languages = NULL; - ResultSize = 0; + PackageListNode = NULL; for (Link = Private->DatabaseList.ForwardLink; Link != &Private->DatabaseList; Link = Link->ForwardLink) { DatabaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE); if (DatabaseRecord->Handle == PackageList) { PackageListNode = (HII_DATABASE_PACKAGE_LIST_INSTANCE *) (DatabaseRecord->PackageList); - for (Link1 = PackageListNode->StringPkgHdr.ForwardLink; - Link1 != &PackageListNode->StringPkgHdr; - Link1 = Link1->ForwardLink - ) { - StringPackage = CR (Link1, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); - if (R8_EfiLibCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) FirstLanguage)) { - Languages = StringPackage->StringPkgHdr->Language; - // - // Language is a series of ';' terminated strings, first one is primary - // language and following with other secondary languages or NULL if no - // secondary languages any more. - // - Languages = AsciiStrStr (Languages, ";"); - if (Languages == NULL) { - break; - } - Languages++; - - ResultSize = AsciiStrSize (Languages); - if (ResultSize <= *SecondLanguagesSize) { - AsciiStrCpy (SecondLanguages, Languages); - } else { - *SecondLanguagesSize = ResultSize; - return EFI_BUFFER_TOO_SMALL; - } + break; + } + } + if (PackageListNode == NULL) { + return EFI_NOT_FOUND; + } + + Languages = NULL; + ResultSize = 0; + for (Link1 = PackageListNode->StringPkgHdr.ForwardLink; + Link1 != &PackageListNode->StringPkgHdr; + Link1 = Link1->ForwardLink + ) { + StringPackage = CR (Link1, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE); + if (HiiCompareLanguage (StringPackage->StringPkgHdr->Language, (CHAR8 *) PrimaryLanguage)) { + Languages = StringPackage->StringPkgHdr->Language; + // + // Language is a series of ';' terminated strings, first one is primary + // language and following with other secondary languages or NULL if no + // secondary languages any more. + // + Languages = AsciiStrStr (Languages, ";"); + if (Languages == NULL) { + break; + } + Languages++; - return EFI_SUCCESS; - } + ResultSize = AsciiStrSize (Languages); + if (ResultSize <= *SecondaryLanguagesSize) { + AsciiStrCpy (SecondaryLanguages, Languages); + } else { + *SecondaryLanguagesSize = ResultSize; + return EFI_BUFFER_TOO_SMALL; } + + return EFI_SUCCESS; } } - return EFI_NOT_FOUND; + return EFI_INVALID_LANGUAGE; } +/** + Converts the ascii character of the string from uppercase to lowercase. + This is a internal function. + + @param ConfigString String to be converted + +**/ +VOID +EFIAPI +AsciiHiiToLower ( + IN CHAR8 *ConfigString + ) +{ + ASSERT (ConfigString != NULL); + + // + // Convert all hex digits in range [A-F] in the configuration header to [a-f] + // + for (; *ConfigString != '\0'; ConfigString++) { + if ( *ConfigString >= 'A' && *ConfigString <= 'Z') { + *ConfigString = (CHAR8) (*ConfigString - 'A' + 'a'); + } + } +} + +/** + Compare whether two names of languages are identical. + + @param Language1 Name of language 1 from StringPackage + @param Language2 Name of language 2 to be compared with language 1. + + @retval TRUE same + @retval FALSE not same + +**/ +BOOLEAN +HiiCompareLanguage ( + IN CHAR8 *Language1, + IN CHAR8 *Language2 + ) +{ + UINTN Index; + UINTN StrLen; + CHAR8 *Lan1; + CHAR8 *Lan2; + + // + // Convert to lower to compare. + // + StrLen = AsciiStrSize (Language1); + Lan1 = AllocateZeroPool (StrLen); + ASSERT (Lan1 != NULL); + AsciiStrCpy(Lan1, Language1); + AsciiHiiToLower (Lan1); + + StrLen = AsciiStrSize (Language2); + Lan2 = AllocateZeroPool (StrLen); + ASSERT (Lan2 != NULL); + AsciiStrCpy(Lan2, Language2); + AsciiHiiToLower (Lan2); + + // + // Compare the Primary Language in Language1 to Language2 + // + for (Index = 0; Lan1[Index] != 0 && Lan1[Index] != ';'; Index++) { + if (Lan1[Index] != Lan2[Index]) { + // + // Return FALSE if any characters are different. + // + FreePool (Lan1); + FreePool (Lan2); + return FALSE; + } + } + + FreePool (Lan1); + FreePool (Lan2); + + // + // Only return TRUE if Language2[Index] is a Null-terminator which means + // the Primary Language in Language1 is the same length as Language2. If + // Language2[Index] is not a Null-terminator, then Language2 is longer than + // the Primary Language in Language1, and FALSE must be returned. + // + return (BOOLEAN) (Language2[Index] == 0); +}