]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/UefiHiiLib/HiiString.c
MdeModulePkg/DxeCapsuleLibFmp: Use new Variable Lock interface
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiString.c
index 2c317e5de355c1501ba2ac1b4eab0f282396fe45..48f6d6881ddf3770051218b41c2d5edff5f1e016 100644 (file)
 /** @file\r
   HII Library implementation that uses DXE protocols and services.\r
 \r
-  Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 \r
 #include "InternalHiiLib.h"\r
 \r
-\r
-//\r
-// Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes\r
-// Each entry is 5 CHAR8 values long.  The first 3 CHAR8 values are the ISO 639-2 code.\r
-// The last 2 CHAR8 values are the ISO 639-1 code.\r
-//\r
-GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 Iso639ToRfc3066ConversionTable[] =\r
-"\\r
-aaraa\\r
-abkab\\r
-afraf\\r
-amham\\r
-araar\\r
-asmas\\r
-aymay\\r
-azeaz\\r
-bakba\\r
-belbe\\r
-benbn\\r
-bihbh\\r
-bisbi\\r
-bodbo\\r
-brebr\\r
-bulbg\\r
-catca\\r
-cescs\\r
-corkw\\r
-cosco\\r
-cymcy\\r
-danda\\r
-deude\\r
-dzodz\\r
-ellel\\r
-engen\\r
-epoeo\\r
-estet\\r
-euseu\\r
-faofo\\r
-fasfa\\r
-fijfj\\r
-finfi\\r
-frafr\\r
-fryfy\\r
-gaiga\\r
-gdhgd\\r
-glggl\\r
-grngn\\r
-gujgu\\r
-hauha\\r
-hebhe\\r
-hinhi\\r
-hrvhr\\r
-hunhu\\r
-hyehy\\r
-ikuiu\\r
-ileie\\r
-inaia\\r
-indid\\r
-ipkik\\r
-islis\\r
-itait\\r
-jawjw\\r
-jpnja\\r
-kalkl\\r
-kankn\\r
-kasks\\r
-katka\\r
-kazkk\\r
-khmkm\\r
-kinrw\\r
-kirky\\r
-korko\\r
-kurku\\r
-laolo\\r
-latla\\r
-lavlv\\r
-linln\\r
-litlt\\r
-ltzlb\\r
-malml\\r
-marmr\\r
-mkdmk\\r
-mlgmg\\r
-mltmt\\r
-molmo\\r
-monmn\\r
-mrimi\\r
-msams\\r
-myamy\\r
-nauna\\r
-nepne\\r
-nldnl\\r
-norno\\r
-ocioc\\r
-ormom\\r
-panpa\\r
-polpl\\r
-porpt\\r
-pusps\\r
-quequ\\r
-rohrm\\r
-ronro\\r
-runrn\\r
-rusru\\r
-sagsg\\r
-sansa\\r
-sinsi\\r
-slksk\\r
-slvsl\\r
-smise\\r
-smosm\\r
-snasn\\r
-sndsd\\r
-somso\\r
-sotst\\r
-spaes\\r
-sqisq\\r
-srpsr\\r
-sswss\\r
-sunsu\\r
-swasw\\r
-swesv\\r
-tamta\\r
-tattt\\r
-telte\\r
-tgktg\\r
-tgltl\\r
-thath\\r
-tsnts\\r
-tuktk\\r
-twitw\\r
-uigug\\r
-ukruk\\r
-urdur\\r
-uzbuz\\r
-vievi\\r
-volvo\\r
-wolwo\\r
-xhoxh\\r
-yidyi\\r
-zhaza\\r
-zhozh\\r
-zulzu\\r
-";\r
-\r
-\r
-\r
 /**\r
-  This function create a new string in String Package or updates an existing \r
+  This function create a new string in String Package or updates an existing\r
   string in a String Package.  If StringId is 0, then a new string is added to\r
   a String Package.  If StringId is not zero, then a string in String Package is\r
   updated.  If SupportedLanguages is NULL, then the string is added or updated\r
   for all the languages that the String Package supports.  If SupportedLanguages\r
-  is not NULL, then the string is added or updated for the set of languages \r
+  is not NULL, then the string is added or updated for the set of languages\r
   specified by SupportedLanguages.\r
-    \r
+\r
   If HiiHandle is NULL, then ASSERT().\r
   If String is NULL, then ASSERT().\r
 \r
-  @param[in]  HiiHandle           A handle that was previously registered in the \r
+  @param[in]  HiiHandle           A handle that was previously registered in the\r
                                   HII Database.\r
-  @param[in]  StringId            If zero, then a new string is created in the \r
-                                  String Package associated with HiiHandle.  If \r
-                                  non-zero, then the string specified by StringId \r
-                                  is updated in the String Package  associated \r
-                                  with HiiHandle. \r
-  @param[in]  String              A pointer to the Null-terminated Unicode string \r
-                                  to add or update in the String Package associated \r
+  @param[in]  StringId            If zero, then a new string is created in the\r
+                                  String Package associated with HiiHandle.  If\r
+                                  non-zero, then the string specified by StringId\r
+                                  is updated in the String Package  associated\r
+                                  with HiiHandle.\r
+  @param[in]  String              A pointer to the Null-terminated Unicode string\r
+                                  to add or update in the String Package associated\r
                                   with HiiHandle.\r
-  @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string of \r
-                                  language codes.  If this parameter is NULL, then \r
-                                  String is added or updated in the String Package \r
-                                  associated with HiiHandle for all the languages \r
-                                  that the String Package supports.  If this \r
-                                  parameter is not NULL, then then String is added \r
-                                  or updated in the String Package associated with \r
-                                  HiiHandle for the set oflanguages specified by \r
-                                  SupportedLanguages.  The format of \r
-                                  SupportedLanguages must follow the language \r
+  @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string of\r
+                                  language codes.  If this parameter is NULL, then\r
+                                  String is added or updated in the String Package\r
+                                  associated with HiiHandle for all the languages\r
+                                  that the String Package supports.  If this\r
+                                  parameter is not NULL, then then String is added\r
+                                  or updated in the String Package associated with\r
+                                  HiiHandle for the set oflanguages specified by\r
+                                  SupportedLanguages.  The format of\r
+                                  SupportedLanguages must follow the language\r
                                   format assumed the HII Database.\r
 \r
   @retval 0      The string could not be added or updated in the String Package.\r
@@ -213,7 +61,6 @@ HiiSetString (
   CHAR8          *AllocatedLanguages;\r
   CHAR8          *Supported;\r
   CHAR8          *Language;\r
-  EFI_STRING_ID  NewStringId;\r
 \r
   ASSERT (HiiHandle != NULL);\r
   ASSERT (String != NULL);\r
@@ -227,7 +74,7 @@ HiiSetString (
     //\r
     // Allocate a copy of the SupportLanguages string that passed in\r
     //\r
-    AllocatedLanguages = AllocateCopyPool (AsciiStrLen (SupportedLanguages), SupportedLanguages);\r
+    AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);\r
   }\r
 \r
   //\r
@@ -237,7 +84,6 @@ HiiSetString (
     return (EFI_STRING_ID)(0);\r
   }\r
 \r
-  NewStringId = 0;\r
   Status = EFI_INVALID_PARAMETER;\r
   //\r
   // Loop through each language that the string supports\r
@@ -249,18 +95,25 @@ HiiSetString (
     Language = Supported;\r
 \r
     //\r
-    // Search for the next language seperator and replace it with a Null-terminator\r
+    // Search for the next language separator and replace it with a Null-terminator\r
     //\r
     for (; *Supported != 0 && *Supported != ';'; Supported++);\r
     if (*Supported != 0) {\r
       *(Supported++) = '\0';\r
     }\r
 \r
+    if ((SupportedLanguages == NULL) && AsciiStrnCmp (Language, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) == 0) {\r
+      //\r
+      // Skip string package used for keyword protocol.\r
+      //\r
+      continue;\r
+    }\r
+\r
     //\r
     // If StringId is 0, then call NewString().  Otherwise, call SetString()\r
     //\r
     if (StringId == (EFI_STRING_ID)(0)) {\r
-      Status = gHiiString->NewString (gHiiString, HiiHandle, &NewStringId, Language, NULL, String, NULL);\r
+      Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r
     } else {\r
       Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r
     }\r
@@ -280,8 +133,6 @@ HiiSetString (
 \r
   if (EFI_ERROR (Status)) {\r
     return (EFI_STRING_ID)(0);\r
-  } else if (StringId == (EFI_STRING_ID)(0)) {\r
-    return NewStringId;\r
   } else {\r
     return StringId;\r
   }\r
@@ -289,24 +140,24 @@ HiiSetString (
 \r
 \r
 /**\r
-  Retrieves a string from a string package names by GUID in a specific language.  \r
-  If the language is not specified, then a string from a string package in the \r
-  current platform  language is retrieved.  If the string can not be retrieved \r
-  using the specified language or the current platform language, then the string \r
-  is retrieved from the string package in the first language the string package \r
-  supports.  The returned string is allocated using AllocatePool().  The caller \r
+  Retrieves a string from a string package names by GUID in a specific language.\r
+  If the language is not specified, then a string from a string package in the\r
+  current platform  language is retrieved.  If the string can not be retrieved\r
+  using the specified language or the current platform language, then the string\r
+  is retrieved from the string package in the first language the string package\r
+  supports.  The returned string is allocated using AllocatePool().  The caller\r
   is responsible for freeing the allocated buffer using FreePool().\r
-  \r
+\r
   If PackageListGuid is NULL, then ASSERT().\r
   If StringId is 0, then ASSERT.\r
 \r
-  @param[in]  PackageListGuid  The GUID of a package list that was previously \r
+  @param[in]  PackageListGuid  The GUID of a package list that was previously\r
                                registered in the HII Database.\r
-  @param[in]  StringId         The identifier of the string to retrieved from the \r
+  @param[in]  StringId         The identifier of the string to retrieved from the\r
                                string package associated with PackageListGuid.\r
-  @param[in]  Language         The language of the string to retrieve.  If this \r
-                               parameter is NULL, then the current platform \r
-                               language is used.  The format of Language must \r
+  @param[in]  Language         The language of the string to retrieve.  If this\r
+                               parameter is NULL, then the current platform\r
+                               language is used.  The format of Language must\r
                                follow the language format assumed the HII Database.\r
 \r
   @retval NULL   The package list specified by PackageListGuid is not present in the\r
@@ -323,8 +174,8 @@ HiiGetPackageString (
   IN CONST CHAR8     *Language  OPTIONAL\r
   )\r
 {\r
-  EFI_HANDLE  *HiiHandleBuffer;\r
-  EFI_HANDLE  HiiHandle;\r
+  EFI_HII_HANDLE  *HiiHandleBuffer;\r
+  EFI_HII_HANDLE  HiiHandle;\r
 \r
   ASSERT (PackageListGuid != NULL);\r
 \r
@@ -340,23 +191,15 @@ HiiGetPackageString (
 }\r
 \r
 /**\r
-  Retrieves a string from a string package in a specific language.  If the language\r
-  is not specified, then a string from a string package in the current platform \r
-  language is retrieved.  If the string can not be retrieved using the specified \r
-  language or the current platform language, then the string is retrieved from \r
-  the string package in the first language the string package supports.  The \r
-  returned string is allocated using AllocatePool().  The caller is responsible \r
-  for freeing the allocated buffer using FreePool().\r
-  \r
-  If HiiHandle is NULL, then ASSERT().\r
-  If StringId is 0, then ASSET.\r
+  Retrieves a string from a string package in a specific language specified in Language\r
+  or in the best lanaguage. See HiiGetStringEx () for the details.\r
 \r
   @param[in]  HiiHandle  A handle that was previously registered in the HII Database.\r
-  @param[in]  StringId   The identifier of the string to retrieved from the string \r
+  @param[in]  StringId   The identifier of the string to retrieved from the string\r
                          package associated with HiiHandle.\r
-  @param[in]  Language   The language of the string to retrieve.  If this parameter \r
-                         is NULL, then the current platform language is used.  The \r
-                         format of Language must follow the language format assumed \r
+  @param[in]  Language   The language of the string to retrieve.  If this parameter\r
+                         is NULL, then the current platform language is used.  The\r
+                         format of Language must follow the language format assumed\r
                          the HII Database.\r
 \r
   @retval NULL   The string specified by StringId is not present in the string package.\r
@@ -370,6 +213,51 @@ HiiGetString (
   IN EFI_STRING_ID   StringId,\r
   IN CONST CHAR8     *Language  OPTIONAL\r
   )\r
+{\r
+  return HiiGetStringEx (HiiHandle, StringId, Language, TRUE);\r
+}\r
+\r
+/**\r
+  Retrieves a string from a string package in a specific language or in the best\r
+  language at discretion of this function according to the priority of languages.\r
+  TryBestLanguage is used to get the string in the best language or in the language\r
+  specified in Language parameter. The behavior is,\r
+  If TryBestLanguage is TRUE, this function looks for the best language for the string.\r
+   - If the string can not be retrieved using the specified language or the current\r
+     platform language, then the string is retrieved from the string package in the\r
+     first language the string package supports.\r
+  If TryBestLanguage is FALSE, Language must be specified for retrieving the string.\r
+\r
+  The returned string is allocated using AllocatePool().  The caller is responsible\r
+  for freeing the allocated buffer using FreePool().\r
+\r
+  If HiiHandle is NULL, then ASSERT().\r
+  If StringId is 0, then ASSET.\r
+  If TryBestLanguage is FALE and Language is NULL, then ASSERT().\r
+\r
+  @param[in]  HiiHandle         A handle that was previously registered in the HII Database.\r
+  @param[in]  StringId          The identifier of the string to retrieved from the string\r
+                                package associated with HiiHandle.\r
+  @param[in]  Language          The language of the string to retrieve.  If this parameter\r
+                                is NULL, then the current platform language is used.  The\r
+                                format of Language must follow the language format assumed\r
+                                the HII Database.\r
+  @param[in]  TryBestLanguage   If TRUE, try to get the best matching language from all\r
+                                supported languages.If FALSE, the Language must be assigned\r
+                                for the StringID.\r
+\r
+  @retval NULL   The string specified by StringId is not present in the string package.\r
+  @retval Other  The string was returned.\r
+\r
+**/\r
+EFI_STRING\r
+EFIAPI\r
+HiiGetStringEx (\r
+  IN EFI_HII_HANDLE  HiiHandle,\r
+  IN EFI_STRING_ID   StringId,\r
+  IN CONST CHAR8     *Language  OPTIONAL,\r
+  IN BOOLEAN         TryBestLanguage\r
+  )\r
 {\r
   EFI_STATUS  Status;\r
   UINTN       StringSize;\r
@@ -381,10 +269,13 @@ HiiGetString (
 \r
   ASSERT (HiiHandle != NULL);\r
   ASSERT (StringId != 0);\r
-\r
+  //\r
+  // Language must be specified if TryBestLanguage = FALSE.\r
+  //\r
+  ASSERT (!(!TryBestLanguage && Language == NULL));\r
   //\r
   // Initialize all allocated buffers to NULL\r
-  // \r
+  //\r
   SupportedLanguages = NULL;\r
   PlatformLanguage   = NULL;\r
   BestLanguage       = NULL;\r
@@ -401,31 +292,36 @@ HiiGetString (
   //\r
   // Get the current platform language setting\r
   //\r
-  PlatformLanguage = GetEfiGlobalVariable (L"PlatformLang");\r
+  GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
 \r
   //\r
-  // If Languag is NULL, then set it to an empty string, so it will be \r
+  // If Languag is NULL, then set it to an empty string, so it will be\r
   // skipped by GetBestLanguage()\r
   //\r
   if (Language == NULL) {\r
     Language = "";\r
   }\r
 \r
-  //\r
-  // Get the best matching language from SupportedLanguages\r
-  //\r
-  BestLanguage = GetBestLanguage (\r
-                   SupportedLanguages, \r
-                   FALSE,                                             // RFC 4646 mode\r
-                   Language,                                          // Highest priority \r
-                   PlatformLanguage != NULL ? PlatformLanguage : "",  // Next highest priority\r
-                   SupportedLanguages,                                // Lowest priority \r
-                   NULL\r
-                   );\r
-  if (BestLanguage == NULL) {\r
-    goto Error;\r
+  if (TryBestLanguage) {\r
+    //\r
+    // Get the best matching language from SupportedLanguages\r
+    //\r
+    BestLanguage = GetBestLanguage (\r
+                     SupportedLanguages,\r
+                     FALSE,                                             // RFC 4646 mode\r
+                     Language,                                          // Highest priority\r
+                     PlatformLanguage != NULL ? PlatformLanguage : "",  // Next highest priority\r
+                     SupportedLanguages,                                // Lowest priority\r
+                     NULL\r
+                     );\r
+    if (BestLanguage == NULL) {\r
+      goto Error;\r
+    }\r
+  } else {\r
+    BestLanguage = (CHAR8 *) Language;\r
   }\r
 \r
+\r
   //\r
   // Retrieve the size of the string in the string package for the BestLanguage\r
   //\r
@@ -440,8 +336,8 @@ HiiGetString (
                          NULL\r
                          );\r
   //\r
-  // If GetString() returns EFI_SUCCESS for a zero size, \r
-  // then there are no supported languages registered for HiiHandle.  If GetString() \r
+  // If GetString() returns EFI_SUCCESS for a zero size,\r
+  // then there are no supported languages registered for HiiHandle.  If GetString()\r
   // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r
   // in the HII Database\r
   //\r
@@ -487,7 +383,7 @@ Error:
   if (PlatformLanguage != NULL) {\r
     FreePool (PlatformLanguage);\r
   }\r
-  if (BestLanguage != NULL) {\r
+  if (TryBestLanguage && BestLanguage != NULL) {\r
     FreePool (BestLanguage);\r
   }\r
 \r
@@ -497,129 +393,3 @@ Error:
   return String;\r
 }\r
 \r
-/**\r
-  Convert language code from RFC3066 to ISO639-2.\r
-\r
-  @param  LanguageRfc3066        RFC3066 language code.\r
-  @param  LanguageIso639         ISO639-2 language code.\r
-\r
-  @retval EFI_SUCCESS            Language code converted.\r
-  @retval EFI_NOT_FOUND          Language code not found.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-ConvertRfc3066LanguageToIso639Language (\r
-  IN  CHAR8   *LanguageRfc3066,\r
-  OUT CHAR8   *LanguageIso639\r
-  )\r
-{\r
-  UINTN  Index;\r
-\r
-  if ((LanguageRfc3066[2] != '-') && (LanguageRfc3066[2] != 0)) {\r
-    CopyMem (LanguageIso639, LanguageRfc3066, 3);\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {\r
-    if (CompareMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2) == 0) {\r
-      CopyMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3);\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-\r
-/**\r
-  Convert language code from ISO639-2 to RFC3066 and return the converted language.\r
-  Caller is responsible for freeing the allocated buffer.\r
-\r
-  LanguageIso639 contain a single ISO639-2 code such as\r
-  "eng" or "fra".\r
-\r
-  If LanguageIso639 is NULL, then ASSERT.\r
-  If LanguageRfc3066 is NULL, then ASSERT.\r
-\r
-  @param  LanguageIso639         ISO639-2 language code.\r
-\r
-  @return the allocated buffer or NULL, if the language is not found.\r
-\r
-**/\r
-CHAR8*\r
-EFIAPI\r
-ConvertIso639LanguageToRfc3066Language (\r
-  IN  CONST CHAR8   *LanguageIso639\r
-  )\r
-{\r
-  UINTN Index;\r
-  CHAR8 *Rfc3066Language;\r
-  \r
-  for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {\r
-    if (CompareMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3) == 0) {\r
-      Rfc3066Language = AllocateZeroPool (3);\r
-      if (Rfc3066Language != NULL) {\r
-        Rfc3066Language = CopyMem (Rfc3066Language, &Iso639ToRfc3066ConversionTable[Index + 3], 2);\r
-      }\r
-      return Rfc3066Language;\r
-    }\r
-  }\r
-\r
-  return NULL;\r
-}\r
-\r
-/**\r
-  Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will\r
-  be converted to "engfra".\r
-\r
-  @param  SupportedLanguages     The RFC3066 language list.\r
-\r
-  @return The ISO639-2 language list.\r
-\r
-**/\r
-CHAR8 *\r
-EFIAPI\r
-Rfc3066ToIso639 (\r
-  CHAR8  *SupportedLanguages\r
-  )\r
-{\r
-  CHAR8       *Languages;\r
-  CHAR8       *ReturnValue;\r
-  CHAR8       *LangCodes;\r
-  CHAR8       *LangRfc3066;\r
-  CHAR8       LangIso639[ISO_639_2_ENTRY_SIZE];\r
-  UINTN       LanguageSize;\r
-  EFI_STATUS  Status;\r
-\r
-  LanguageSize = AsciiStrSize (SupportedLanguages);\r
-  ReturnValue = AllocateZeroPool (LanguageSize);\r
-  if (ReturnValue == NULL) {\r
-    return ReturnValue;\r
-  }\r
-\r
-  //\r
-  // Allocate working buffer to contain substring in SupportedLanguages;\r
-  //\r
-  LangRfc3066 = AllocatePool (LanguageSize);\r
-  if (LangRfc3066 == NULL) {\r
-    FreePool (ReturnValue);\r
-    return NULL;\r
-  }\r
-  Languages = ReturnValue;\r
-  LangCodes = SupportedLanguages;\r
-  while (*LangCodes != 0) {\r
-    HiiLibGetNextLanguage (&LangCodes, LangRfc3066);\r
-\r
-    Status = ConvertRfc3066LanguageToIso639Language (LangRfc3066, LangIso639);\r
-    if (!EFI_ERROR (Status)) {\r
-      CopyMem (Languages, LangIso639, 3);\r
-      Languages = Languages + 3;\r
-    }\r
-  }\r
-\r
-  FreePool (LangRfc3066);\r
-  return ReturnValue;\r
-}\r
-\r
-\r