]>
Commit | Line | Data |
---|---|---|
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 | |
08e4b3cf | 10 | #include "InternalHiiLib.h"\r |
11 | \r | |
08e4b3cf | 12 | /**\r |
d1102dba | 13 | This function create a new string in String Package or updates an existing\r |
cb7d01c0 | 14 | string in a String Package. If StringId is 0, then a new string is added to\r |
15 | a String Package. If StringId is not zero, then a string in String Package is\r | |
16 | updated. If SupportedLanguages is NULL, then the string is added or updated\r | |
17 | for all the languages that the String Package supports. If SupportedLanguages\r | |
d1102dba | 18 | is not NULL, then the string is added or updated for the set of languages\r |
cb7d01c0 | 19 | specified by SupportedLanguages.\r |
d1102dba | 20 | \r |
cb7d01c0 | 21 | If HiiHandle is NULL, then ASSERT().\r |
22 | If String is NULL, then ASSERT().\r | |
23 | \r | |
d1102dba | 24 | @param[in] HiiHandle A handle that was previously registered in the\r |
cb7d01c0 | 25 | HII Database.\r |
d1102dba LG |
26 | @param[in] StringId If zero, then a new string is created in the\r |
27 | String Package associated with HiiHandle. If\r | |
28 | non-zero, then the string specified by StringId\r | |
29 | is updated in the String Package associated\r | |
30 | with HiiHandle.\r | |
31 | @param[in] String A pointer to the Null-terminated Unicode string\r | |
32 | to add or update in the String Package associated\r | |
cb7d01c0 | 33 | with HiiHandle.\r |
d1102dba LG |
34 | @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of\r |
35 | language codes. If this parameter is NULL, then\r | |
36 | String is added or updated in the String Package\r | |
37 | associated with HiiHandle for all the languages\r | |
38 | that the String Package supports. If this\r | |
39 | parameter is not NULL, then then String is added\r | |
40 | or updated in the String Package associated with\r | |
41 | HiiHandle for the set oflanguages specified by\r | |
42 | SupportedLanguages. The format of\r | |
43 | SupportedLanguages must follow the language\r | |
cb7d01c0 | 44 | format assumed the HII Database.\r |
45 | \r | |
46 | @retval 0 The string could not be added or updated in the String Package.\r | |
47 | @retval Other The EFI_STRING_ID of the newly added or updated string.\r | |
08e4b3cf | 48 | \r |
49 | **/\r | |
cb7d01c0 | 50 | EFI_STRING_ID\r |
08e4b3cf | 51 | EFIAPI\r |
cb7d01c0 | 52 | HiiSetString (\r |
53 | IN EFI_HII_HANDLE HiiHandle,\r | |
e3917e22 | 54 | IN EFI_STRING_ID StringId OPTIONAL,\r |
cb7d01c0 | 55 | IN CONST EFI_STRING String,\r |
56 | IN CONST CHAR8 *SupportedLanguages OPTIONAL\r | |
08e4b3cf | 57 | )\r |
58 | {\r | |
1436aea4 MK |
59 | EFI_STATUS Status;\r |
60 | CHAR8 *AllocatedLanguages;\r | |
61 | CHAR8 *Supported;\r | |
62 | CHAR8 *Language;\r | |
08e4b3cf | 63 | \r |
cb7d01c0 | 64 | ASSERT (HiiHandle != NULL);\r |
08e4b3cf | 65 | ASSERT (String != NULL);\r |
08e4b3cf | 66 | \r |
cb7d01c0 | 67 | if (SupportedLanguages == NULL) {\r |
68 | //\r | |
69 | // Retrieve the languages that the package specified by HiiHandle supports\r | |
70 | //\r | |
71 | AllocatedLanguages = HiiGetSupportedLanguages (HiiHandle);\r | |
72 | } else {\r | |
73 | //\r | |
74 | // Allocate a copy of the SupportLanguages string that passed in\r | |
75 | //\r | |
f580c1bd | 76 | AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);\r |
cb7d01c0 | 77 | }\r |
08e4b3cf | 78 | \r |
ea7cd3ec | 79 | //\r |
cb7d01c0 | 80 | // If there are not enough resources for the supported languages string, then return a StringId of 0\r |
ea7cd3ec | 81 | //\r |
cb7d01c0 | 82 | if (AllocatedLanguages == NULL) {\r |
83 | return (EFI_STRING_ID)(0);\r | |
84 | }\r | |
08e4b3cf | 85 | \r |
cb7d01c0 | 86 | Status = EFI_INVALID_PARAMETER;\r |
87 | //\r | |
88 | // Loop through each language that the string supports\r | |
89 | //\r | |
90 | for (Supported = AllocatedLanguages; *Supported != '\0'; ) {\r | |
91 | //\r | |
92 | // Cache a pointer to the beginning of the current language in the list of languages\r | |
93 | //\r | |
94 | Language = Supported;\r | |
08e4b3cf | 95 | \r |
96 | //\r | |
3b28e744 | 97 | // Search for the next language separator and replace it with a Null-terminator\r |
08e4b3cf | 98 | //\r |
1436aea4 MK |
99 | for ( ; *Supported != 0 && *Supported != ';'; Supported++) {\r |
100 | }\r | |
101 | \r | |
cb7d01c0 | 102 | if (*Supported != 0) {\r |
103 | *(Supported++) = '\0';\r | |
08e4b3cf | 104 | }\r |
d1102dba | 105 | \r |
1436aea4 | 106 | if ((SupportedLanguages == NULL) && (AsciiStrnCmp (Language, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) == 0)) {\r |
8fa9ac60 ED |
107 | //\r |
108 | // Skip string package used for keyword protocol.\r | |
109 | //\r | |
110 | continue;\r | |
111 | }\r | |
08e4b3cf | 112 | \r |
cb7d01c0 | 113 | //\r |
114 | // If StringId is 0, then call NewString(). Otherwise, call SetString()\r | |
115 | //\r | |
116 | if (StringId == (EFI_STRING_ID)(0)) {\r | |
d6a82eaf | 117 | Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r |
cb7d01c0 | 118 | } else {\r |
119 | Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r | |
120 | }\r | |
08e4b3cf | 121 | \r |
122 | //\r | |
cb7d01c0 | 123 | // If there was an error, then break out of the loop and return a StringId of 0\r |
08e4b3cf | 124 | //\r |
08e4b3cf | 125 | if (EFI_ERROR (Status)) {\r |
126 | break;\r | |
127 | }\r | |
128 | }\r | |
129 | \r | |
cb7d01c0 | 130 | //\r |
131 | // Free the buffer of supported languages\r | |
132 | //\r | |
133 | FreePool (AllocatedLanguages);\r | |
134 | \r | |
135 | if (EFI_ERROR (Status)) {\r | |
136 | return (EFI_STRING_ID)(0);\r | |
cb7d01c0 | 137 | } else {\r |
138 | return StringId;\r | |
139 | }\r | |
08e4b3cf | 140 | }\r |
141 | \r | |
08e4b3cf | 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 | 169 | EFI_STRING\r |
08e4b3cf | 170 | EFIAPI\r |
cb7d01c0 | 171 | HiiGetPackageString (\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 | 209 | EFI_STRING\r |
08e4b3cf | 210 | EFIAPI\r |
cb7d01c0 | 211 | HiiGetString (\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 | |
253 | EFI_STRING\r | |
254 | EFIAPI\r | |
255 | HiiGetStringEx (\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 | |
1436aea4 | 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 | |
1436aea4 | 321 | BestLanguage = (CHAR8 *)Language;\r |
cb7d01c0 | 322 | }\r |
08e4b3cf | 323 | \r |
cb7d01c0 | 324 | //\r |
325 | // Retrieve the size of the string in the string package for the BestLanguage\r | |
326 | //\r | |
327 | StringSize = 0;\r | |
1436aea4 MK |
328 | Status = gHiiString->GetString (\r |
329 | gHiiString,\r | |
330 | BestLanguage,\r | |
331 | HiiHandle,\r | |
332 | StringId,\r | |
333 | &TempString,\r | |
334 | &StringSize,\r | |
335 | NULL\r | |
336 | );\r | |
cb7d01c0 | 337 | //\r |
d1102dba LG |
338 | // If GetString() returns EFI_SUCCESS for a zero size,\r |
339 | // then there are no supported languages registered for HiiHandle. If GetString()\r | |
cb7d01c0 | 340 | // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r |
341 | // in the HII Database\r | |
342 | //\r | |
343 | if (Status != EFI_BUFFER_TOO_SMALL) {\r | |
344 | goto Error;\r | |
345 | }\r | |
08e4b3cf | 346 | \r |
cb7d01c0 | 347 | //\r |
348 | // Allocate a buffer for the return string\r | |
349 | //\r | |
350 | String = AllocateZeroPool (StringSize);\r | |
351 | if (String == NULL) {\r | |
352 | goto Error;\r | |
353 | }\r | |
08e4b3cf | 354 | \r |
cb7d01c0 | 355 | //\r |
356 | // Retrieve the string from the string package\r | |
357 | //\r | |
358 | Status = gHiiString->GetString (\r | |
359 | gHiiString,\r | |
360 | BestLanguage,\r | |
361 | HiiHandle,\r | |
362 | StringId,\r | |
363 | String,\r | |
364 | &StringSize,\r | |
365 | NULL\r | |
366 | );\r | |
367 | if (EFI_ERROR (Status)) {\r | |
368 | //\r | |
369 | // Free the buffer and return NULL if the supported languages can not be retrieved.\r | |
370 | //\r | |
371 | FreePool (String);\r | |
372 | String = NULL;\r | |
08e4b3cf | 373 | }\r |
374 | \r | |
cb7d01c0 | 375 | Error:\r |
376 | //\r | |
377 | // Free allocated buffers\r | |
378 | //\r | |
379 | if (SupportedLanguages != NULL) {\r | |
380 | FreePool (SupportedLanguages);\r | |
381 | }\r | |
1436aea4 | 382 | \r |
cb7d01c0 | 383 | if (PlatformLanguage != NULL) {\r |
384 | FreePool (PlatformLanguage);\r | |
385 | }\r | |
1436aea4 MK |
386 | \r |
387 | if (TryBestLanguage && (BestLanguage != NULL)) {\r | |
cb7d01c0 | 388 | FreePool (BestLanguage);\r |
08e4b3cf | 389 | }\r |
390 | \r | |
cb7d01c0 | 391 | //\r |
392 | // Return the Null-terminated Unicode string\r | |
393 | //\r | |
394 | return String;\r | |
08e4b3cf | 395 | }\r |