]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/UefiHiiLib/HiiString.c
MdeModulePkg/DxeCapsuleLibFmp: Use new Variable Lock interface
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiString.c
... / ...
CommitLineData
1/** @file\r
2 HII Library implementation that uses DXE protocols and services.\r
3\r
4 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
5 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10\r
11#include "InternalHiiLib.h"\r
12\r
13/**\r
14 This function create a new string in String Package or updates an existing\r
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
19 is not NULL, then the string is added or updated for the set of languages\r
20 specified by SupportedLanguages.\r
21\r
22 If HiiHandle is NULL, then ASSERT().\r
23 If String is NULL, then ASSERT().\r
24\r
25 @param[in] HiiHandle A handle that was previously registered in the\r
26 HII Database.\r
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
34 with HiiHandle.\r
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
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
49\r
50**/\r
51EFI_STRING_ID\r
52EFIAPI\r
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
58 )\r
59{\r
60 EFI_STATUS Status;\r
61 CHAR8 *AllocatedLanguages;\r
62 CHAR8 *Supported;\r
63 CHAR8 *Language;\r
64\r
65 ASSERT (HiiHandle != NULL);\r
66 ASSERT (String != NULL);\r
67\r
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
77 AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);\r
78 }\r
79\r
80 //\r
81 // If there are not enough resources for the supported languages string, then return a StringId of 0\r
82 //\r
83 if (AllocatedLanguages == NULL) {\r
84 return (EFI_STRING_ID)(0);\r
85 }\r
86\r
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
96\r
97 //\r
98 // Search for the next language separator and replace it with a Null-terminator\r
99 //\r
100 for (; *Supported != 0 && *Supported != ';'; Supported++);\r
101 if (*Supported != 0) {\r
102 *(Supported++) = '\0';\r
103 }\r
104\r
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
111\r
112 //\r
113 // If StringId is 0, then call NewString(). Otherwise, call SetString()\r
114 //\r
115 if (StringId == (EFI_STRING_ID)(0)) {\r
116 Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r
117 } else {\r
118 Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r
119 }\r
120\r
121 //\r
122 // If there was an error, then break out of the loop and return a StringId of 0\r
123 //\r
124 if (EFI_ERROR (Status)) {\r
125 break;\r
126 }\r
127 }\r
128\r
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
136 } else {\r
137 return StringId;\r
138 }\r
139}\r
140\r
141\r
142/**\r
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
149 is responsible for freeing the allocated buffer using FreePool().\r
150\r
151 If PackageListGuid is NULL, then ASSERT().\r
152 If StringId is 0, then ASSERT.\r
153\r
154 @param[in] PackageListGuid The GUID of a package list that was previously\r
155 registered in the HII Database.\r
156 @param[in] StringId The identifier of the string to retrieved from the\r
157 string package associated with PackageListGuid.\r
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
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
167\r
168**/\r
169EFI_STRING\r
170EFIAPI\r
171HiiGetPackageString (\r
172 IN CONST EFI_GUID *PackageListGuid,\r
173 IN EFI_STRING_ID StringId,\r
174 IN CONST CHAR8 *Language OPTIONAL\r
175 )\r
176{\r
177 EFI_HII_HANDLE *HiiHandleBuffer;\r
178 EFI_HII_HANDLE HiiHandle;\r
179\r
180 ASSERT (PackageListGuid != NULL);\r
181\r
182 HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);\r
183 if (HiiHandleBuffer == NULL) {\r
184 return NULL;\r
185 }\r
186\r
187 HiiHandle = HiiHandleBuffer[0];\r
188 FreePool (HiiHandleBuffer);\r
189\r
190 return HiiGetString (HiiHandle, StringId, Language);\r
191}\r
192\r
193/**\r
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
196\r
197 @param[in] HiiHandle A handle that was previously registered in the HII Database.\r
198 @param[in] StringId The identifier of the string to retrieved from the string\r
199 package associated with HiiHandle.\r
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
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
207\r
208**/\r
209EFI_STRING\r
210EFIAPI\r
211HiiGetString (\r
212 IN EFI_HII_HANDLE HiiHandle,\r
213 IN EFI_STRING_ID StringId,\r
214 IN CONST CHAR8 *Language OPTIONAL\r
215 )\r
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
261{\r
262 EFI_STATUS Status;\r
263 UINTN StringSize;\r
264 CHAR16 TempString;\r
265 EFI_STRING String;\r
266 CHAR8 *SupportedLanguages;\r
267 CHAR8 *PlatformLanguage;\r
268 CHAR8 *BestLanguage;\r
269\r
270 ASSERT (HiiHandle != NULL);\r
271 ASSERT (StringId != 0);\r
272 //\r
273 // Language must be specified if TryBestLanguage = FALSE.\r
274 //\r
275 ASSERT (!(!TryBestLanguage && Language == NULL));\r
276 //\r
277 // Initialize all allocated buffers to NULL\r
278 //\r
279 SupportedLanguages = NULL;\r
280 PlatformLanguage = NULL;\r
281 BestLanguage = NULL;\r
282 String = NULL;\r
283\r
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
290 }\r
291\r
292 //\r
293 // Get the current platform language setting\r
294 //\r
295 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
296\r
297 //\r
298 // If Languag is NULL, then set it to an empty string, so it will be\r
299 // skipped by GetBestLanguage()\r
300 //\r
301 if (Language == NULL) {\r
302 Language = "";\r
303 }\r
304\r
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
322 }\r
323\r
324\r
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
339 // If GetString() returns EFI_SUCCESS for a zero size,\r
340 // then there are no supported languages registered for HiiHandle. If GetString()\r
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
347\r
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
355\r
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
374 }\r
375\r
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
386 if (TryBestLanguage && BestLanguage != NULL) {\r
387 FreePool (BestLanguage);\r
388 }\r
389\r
390 //\r
391 // Return the Null-terminated Unicode string\r
392 //\r
393 return String;\r
394}\r
395\r