]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiHiiLib/HiiString.c
MdeModulePkg/DxeCapsuleLibFmp: Use new Variable Lock interface
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiString.c
CommitLineData
08e4b3cf 1/** @file\r
2 HII Library implementation that uses DXE protocols and services.\r
3\r
0d96664d
AC
4 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
5 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
9d510e61 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
08e4b3cf 7\r
8**/\r
9\r
10\r
11#include "InternalHiiLib.h"\r
12\r
08e4b3cf 13/**\r
d1102dba 14 This function create a new string in String Package or updates an existing\r
cb7d01c0 15 string in a String Package. If StringId is 0, then a new string is added to\r
16 a String Package. If StringId is not zero, then a string in String Package is\r
17 updated. If SupportedLanguages is NULL, then the string is added or updated\r
18 for all the languages that the String Package supports. If SupportedLanguages\r
d1102dba 19 is not NULL, then the string is added or updated for the set of languages\r
cb7d01c0 20 specified by SupportedLanguages.\r
d1102dba 21\r
cb7d01c0 22 If HiiHandle is NULL, then ASSERT().\r
23 If String is NULL, then ASSERT().\r
24\r
d1102dba 25 @param[in] HiiHandle A handle that was previously registered in the\r
cb7d01c0 26 HII Database.\r
d1102dba
LG
27 @param[in] StringId If zero, then a new string is created in the\r
28 String Package associated with HiiHandle. If\r
29 non-zero, then the string specified by StringId\r
30 is updated in the String Package associated\r
31 with HiiHandle.\r
32 @param[in] String A pointer to the Null-terminated Unicode string\r
33 to add or update in the String Package associated\r
cb7d01c0 34 with HiiHandle.\r
d1102dba
LG
35 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of\r
36 language codes. If this parameter is NULL, then\r
37 String is added or updated in the String Package\r
38 associated with HiiHandle for all the languages\r
39 that the String Package supports. If this\r
40 parameter is not NULL, then then String is added\r
41 or updated in the String Package associated with\r
42 HiiHandle for the set oflanguages specified by\r
43 SupportedLanguages. The format of\r
44 SupportedLanguages must follow the language\r
cb7d01c0 45 format assumed the HII Database.\r
46\r
47 @retval 0 The string could not be added or updated in the String Package.\r
48 @retval Other The EFI_STRING_ID of the newly added or updated string.\r
08e4b3cf 49\r
50**/\r
cb7d01c0 51EFI_STRING_ID\r
08e4b3cf 52EFIAPI\r
cb7d01c0 53HiiSetString (\r
54 IN EFI_HII_HANDLE HiiHandle,\r
55 IN EFI_STRING_ID StringId, OPTIONAL\r
56 IN CONST EFI_STRING String,\r
57 IN CONST CHAR8 *SupportedLanguages OPTIONAL\r
08e4b3cf 58 )\r
59{\r
cb7d01c0 60 EFI_STATUS Status;\r
61 CHAR8 *AllocatedLanguages;\r
62 CHAR8 *Supported;\r
63 CHAR8 *Language;\r
08e4b3cf 64\r
cb7d01c0 65 ASSERT (HiiHandle != NULL);\r
08e4b3cf 66 ASSERT (String != NULL);\r
08e4b3cf 67\r
cb7d01c0 68 if (SupportedLanguages == NULL) {\r
69 //\r
70 // Retrieve the languages that the package specified by HiiHandle supports\r
71 //\r
72 AllocatedLanguages = HiiGetSupportedLanguages (HiiHandle);\r
73 } else {\r
74 //\r
75 // Allocate a copy of the SupportLanguages string that passed in\r
76 //\r
f580c1bd 77 AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);\r
cb7d01c0 78 }\r
08e4b3cf 79\r
ea7cd3ec 80 //\r
cb7d01c0 81 // If there are not enough resources for the supported languages string, then return a StringId of 0\r
ea7cd3ec 82 //\r
cb7d01c0 83 if (AllocatedLanguages == NULL) {\r
84 return (EFI_STRING_ID)(0);\r
85 }\r
08e4b3cf 86\r
cb7d01c0 87 Status = EFI_INVALID_PARAMETER;\r
88 //\r
89 // Loop through each language that the string supports\r
90 //\r
91 for (Supported = AllocatedLanguages; *Supported != '\0'; ) {\r
92 //\r
93 // Cache a pointer to the beginning of the current language in the list of languages\r
94 //\r
95 Language = Supported;\r
08e4b3cf 96\r
97 //\r
3b28e744 98 // Search for the next language separator and replace it with a Null-terminator\r
08e4b3cf 99 //\r
cb7d01c0 100 for (; *Supported != 0 && *Supported != ';'; Supported++);\r
101 if (*Supported != 0) {\r
102 *(Supported++) = '\0';\r
08e4b3cf 103 }\r
d1102dba 104\r
8fa9ac60
ED
105 if ((SupportedLanguages == NULL) && AsciiStrnCmp (Language, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) == 0) {\r
106 //\r
107 // Skip string package used for keyword protocol.\r
108 //\r
109 continue;\r
110 }\r
08e4b3cf 111\r
cb7d01c0 112 //\r
113 // If StringId is 0, then call NewString(). Otherwise, call SetString()\r
114 //\r
115 if (StringId == (EFI_STRING_ID)(0)) {\r
d6a82eaf 116 Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r
cb7d01c0 117 } else {\r
118 Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r
119 }\r
08e4b3cf 120\r
121 //\r
cb7d01c0 122 // If there was an error, then break out of the loop and return a StringId of 0\r
08e4b3cf 123 //\r
08e4b3cf 124 if (EFI_ERROR (Status)) {\r
125 break;\r
126 }\r
127 }\r
128\r
cb7d01c0 129 //\r
130 // Free the buffer of supported languages\r
131 //\r
132 FreePool (AllocatedLanguages);\r
133\r
134 if (EFI_ERROR (Status)) {\r
135 return (EFI_STRING_ID)(0);\r
cb7d01c0 136 } else {\r
137 return StringId;\r
138 }\r
08e4b3cf 139}\r
140\r
141\r
142/**\r
d1102dba
LG
143 Retrieves a string from a string package names by GUID in a specific language.\r
144 If the language is not specified, then a string from a string package in the\r
145 current platform language is retrieved. If the string can not be retrieved\r
146 using the specified language or the current platform language, then the string\r
147 is retrieved from the string package in the first language the string package\r
148 supports. The returned string is allocated using AllocatePool(). The caller\r
cb7d01c0 149 is responsible for freeing the allocated buffer using FreePool().\r
d1102dba 150\r
cb7d01c0 151 If PackageListGuid is NULL, then ASSERT().\r
152 If StringId is 0, then ASSERT.\r
153\r
d1102dba 154 @param[in] PackageListGuid The GUID of a package list that was previously\r
cb7d01c0 155 registered in the HII Database.\r
d1102dba 156 @param[in] StringId The identifier of the string to retrieved from the\r
cb7d01c0 157 string package associated with PackageListGuid.\r
d1102dba
LG
158 @param[in] Language The language of the string to retrieve. If this\r
159 parameter is NULL, then the current platform\r
160 language is used. The format of Language must\r
cb7d01c0 161 follow the language format assumed the HII Database.\r
162\r
163 @retval NULL The package list specified by PackageListGuid is not present in the\r
164 HII Database.\r
165 @retval NULL The string specified by StringId is not present in the string package.\r
166 @retval Other The string was returned.\r
08e4b3cf 167\r
168**/\r
cb7d01c0 169EFI_STRING\r
08e4b3cf 170EFIAPI\r
cb7d01c0 171HiiGetPackageString (\r
172 IN CONST EFI_GUID *PackageListGuid,\r
173 IN EFI_STRING_ID StringId,\r
174 IN CONST CHAR8 *Language OPTIONAL\r
08e4b3cf 175 )\r
176{\r
abf8f69e
LE
177 EFI_HII_HANDLE *HiiHandleBuffer;\r
178 EFI_HII_HANDLE HiiHandle;\r
08e4b3cf 179\r
cb7d01c0 180 ASSERT (PackageListGuid != NULL);\r
08e4b3cf 181\r
cb7d01c0 182 HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);\r
183 if (HiiHandleBuffer == NULL) {\r
184 return NULL;\r
08e4b3cf 185 }\r
186\r
cb7d01c0 187 HiiHandle = HiiHandleBuffer[0];\r
4a1102c9 188 FreePool (HiiHandleBuffer);\r
189\r
cb7d01c0 190 return HiiGetString (HiiHandle, StringId, Language);\r
08e4b3cf 191}\r
192\r
193/**\r
0d96664d
AC
194 Retrieves a string from a string package in a specific language specified in Language\r
195 or in the best lanaguage. See HiiGetStringEx () for the details.\r
cb7d01c0 196\r
197 @param[in] HiiHandle A handle that was previously registered in the HII Database.\r
d1102dba 198 @param[in] StringId The identifier of the string to retrieved from the string\r
cb7d01c0 199 package associated with HiiHandle.\r
d1102dba
LG
200 @param[in] Language The language of the string to retrieve. If this parameter\r
201 is NULL, then the current platform language is used. The\r
202 format of Language must follow the language format assumed\r
cb7d01c0 203 the HII Database.\r
204\r
205 @retval NULL The string specified by StringId is not present in the string package.\r
206 @retval Other The string was returned.\r
08e4b3cf 207\r
208**/\r
cb7d01c0 209EFI_STRING\r
08e4b3cf 210EFIAPI\r
cb7d01c0 211HiiGetString (\r
212 IN EFI_HII_HANDLE HiiHandle,\r
213 IN EFI_STRING_ID StringId,\r
214 IN CONST CHAR8 *Language OPTIONAL\r
08e4b3cf 215 )\r
0d96664d
AC
216{\r
217 return HiiGetStringEx (HiiHandle, StringId, Language, TRUE);\r
218}\r
219\r
220/**\r
221 Retrieves a string from a string package in a specific language or in the best\r
222 language at discretion of this function according to the priority of languages.\r
223 TryBestLanguage is used to get the string in the best language or in the language\r
224 specified in Language parameter. The behavior is,\r
225 If TryBestLanguage is TRUE, this function looks for the best language for the string.\r
226 - If the string can not be retrieved using the specified language or the current\r
227 platform language, then the string is retrieved from the string package in the\r
228 first language the string package supports.\r
229 If TryBestLanguage is FALSE, Language must be specified for retrieving the string.\r
230\r
231 The returned string is allocated using AllocatePool(). The caller is responsible\r
232 for freeing the allocated buffer using FreePool().\r
233\r
234 If HiiHandle is NULL, then ASSERT().\r
235 If StringId is 0, then ASSET.\r
236 If TryBestLanguage is FALE and Language is NULL, then ASSERT().\r
237\r
238 @param[in] HiiHandle A handle that was previously registered in the HII Database.\r
239 @param[in] StringId The identifier of the string to retrieved from the string\r
240 package associated with HiiHandle.\r
241 @param[in] Language The language of the string to retrieve. If this parameter\r
242 is NULL, then the current platform language is used. The\r
243 format of Language must follow the language format assumed\r
244 the HII Database.\r
245 @param[in] TryBestLanguage If TRUE, try to get the best matching language from all\r
246 supported languages.If FALSE, the Language must be assigned\r
247 for the StringID.\r
248\r
249 @retval NULL The string specified by StringId is not present in the string package.\r
250 @retval Other The string was returned.\r
251\r
252**/\r
253EFI_STRING\r
254EFIAPI\r
255HiiGetStringEx (\r
256 IN EFI_HII_HANDLE HiiHandle,\r
257 IN EFI_STRING_ID StringId,\r
258 IN CONST CHAR8 *Language OPTIONAL,\r
259 IN BOOLEAN TryBestLanguage\r
260 )\r
08e4b3cf 261{\r
262 EFI_STATUS Status;\r
cb7d01c0 263 UINTN StringSize;\r
264 CHAR16 TempString;\r
265 EFI_STRING String;\r
266 CHAR8 *SupportedLanguages;\r
267 CHAR8 *PlatformLanguage;\r
ea7cd3ec 268 CHAR8 *BestLanguage;\r
08e4b3cf 269\r
cb7d01c0 270 ASSERT (HiiHandle != NULL);\r
271 ASSERT (StringId != 0);\r
0d96664d
AC
272 //\r
273 // Language must be specified if TryBestLanguage = FALSE.\r
274 //\r
275 ASSERT (!(!TryBestLanguage && Language == NULL));\r
cb7d01c0 276 //\r
277 // Initialize all allocated buffers to NULL\r
d1102dba 278 //\r
cb7d01c0 279 SupportedLanguages = NULL;\r
280 PlatformLanguage = NULL;\r
281 BestLanguage = NULL;\r
282 String = NULL;\r
08e4b3cf 283\r
cb7d01c0 284 //\r
285 // Get the languages that the package specified by HiiHandle supports\r
286 //\r
287 SupportedLanguages = HiiGetSupportedLanguages (HiiHandle);\r
288 if (SupportedLanguages == NULL) {\r
289 goto Error;\r
08e4b3cf 290 }\r
291\r
cb7d01c0 292 //\r
293 // Get the current platform language setting\r
294 //\r
f01b91ae 295 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
08e4b3cf 296\r
cb7d01c0 297 //\r
d1102dba 298 // If Languag is NULL, then set it to an empty string, so it will be\r
cb7d01c0 299 // skipped by GetBestLanguage()\r
300 //\r
301 if (Language == NULL) {\r
302 Language = "";\r
303 }\r
08e4b3cf 304\r
0d96664d
AC
305 if (TryBestLanguage) {\r
306 //\r
307 // Get the best matching language from SupportedLanguages\r
308 //\r
309 BestLanguage = GetBestLanguage (\r
310 SupportedLanguages,\r
311 FALSE, // RFC 4646 mode\r
312 Language, // Highest priority\r
313 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority\r
314 SupportedLanguages, // Lowest priority\r
315 NULL\r
316 );\r
317 if (BestLanguage == NULL) {\r
318 goto Error;\r
319 }\r
320 } else {\r
321 BestLanguage = (CHAR8 *) Language;\r
cb7d01c0 322 }\r
08e4b3cf 323\r
0d96664d 324\r
cb7d01c0 325 //\r
326 // Retrieve the size of the string in the string package for the BestLanguage\r
327 //\r
328 StringSize = 0;\r
329 Status = gHiiString->GetString (\r
330 gHiiString,\r
331 BestLanguage,\r
332 HiiHandle,\r
333 StringId,\r
334 &TempString,\r
335 &StringSize,\r
336 NULL\r
337 );\r
338 //\r
d1102dba
LG
339 // If GetString() returns EFI_SUCCESS for a zero size,\r
340 // then there are no supported languages registered for HiiHandle. If GetString()\r
cb7d01c0 341 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r
342 // in the HII Database\r
343 //\r
344 if (Status != EFI_BUFFER_TOO_SMALL) {\r
345 goto Error;\r
346 }\r
08e4b3cf 347\r
cb7d01c0 348 //\r
349 // Allocate a buffer for the return string\r
350 //\r
351 String = AllocateZeroPool (StringSize);\r
352 if (String == NULL) {\r
353 goto Error;\r
354 }\r
08e4b3cf 355\r
cb7d01c0 356 //\r
357 // Retrieve the string from the string package\r
358 //\r
359 Status = gHiiString->GetString (\r
360 gHiiString,\r
361 BestLanguage,\r
362 HiiHandle,\r
363 StringId,\r
364 String,\r
365 &StringSize,\r
366 NULL\r
367 );\r
368 if (EFI_ERROR (Status)) {\r
369 //\r
370 // Free the buffer and return NULL if the supported languages can not be retrieved.\r
371 //\r
372 FreePool (String);\r
373 String = NULL;\r
08e4b3cf 374 }\r
375\r
cb7d01c0 376Error:\r
377 //\r
378 // Free allocated buffers\r
379 //\r
380 if (SupportedLanguages != NULL) {\r
381 FreePool (SupportedLanguages);\r
382 }\r
383 if (PlatformLanguage != NULL) {\r
384 FreePool (PlatformLanguage);\r
385 }\r
0d96664d 386 if (TryBestLanguage && BestLanguage != NULL) {\r
cb7d01c0 387 FreePool (BestLanguage);\r
08e4b3cf 388 }\r
389\r
cb7d01c0 390 //\r
391 // Return the Null-terminated Unicode string\r
392 //\r
393 return String;\r
08e4b3cf 394}\r
395\r