]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiHiiLib/HiiString.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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
d1102dba 4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
08e4b3cf 6\r
7**/\r
8\r
9\r
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 50EFI_STRING_ID\r
08e4b3cf 51EFIAPI\r
cb7d01c0 52HiiSetString (\r
53 IN EFI_HII_HANDLE HiiHandle,\r
54 IN EFI_STRING_ID StringId, OPTIONAL\r
55 IN CONST EFI_STRING String,\r
56 IN CONST CHAR8 *SupportedLanguages OPTIONAL\r
08e4b3cf 57 )\r
58{\r
cb7d01c0 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
cb7d01c0 99 for (; *Supported != 0 && *Supported != ';'; Supported++);\r
100 if (*Supported != 0) {\r
101 *(Supported++) = '\0';\r
08e4b3cf 102 }\r
d1102dba 103\r
8fa9ac60
ED
104 if ((SupportedLanguages == NULL) && AsciiStrnCmp (Language, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) == 0) {\r
105 //\r
106 // Skip string package used for keyword protocol.\r
107 //\r
108 continue;\r
109 }\r
08e4b3cf 110\r
cb7d01c0 111 //\r
112 // If StringId is 0, then call NewString(). Otherwise, call SetString()\r
113 //\r
114 if (StringId == (EFI_STRING_ID)(0)) {\r
d6a82eaf 115 Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r
cb7d01c0 116 } else {\r
117 Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r
118 }\r
08e4b3cf 119\r
120 //\r
cb7d01c0 121 // If there was an error, then break out of the loop and return a StringId of 0\r
08e4b3cf 122 //\r
08e4b3cf 123 if (EFI_ERROR (Status)) {\r
124 break;\r
125 }\r
126 }\r
127\r
cb7d01c0 128 //\r
129 // Free the buffer of supported languages\r
130 //\r
131 FreePool (AllocatedLanguages);\r
132\r
133 if (EFI_ERROR (Status)) {\r
134 return (EFI_STRING_ID)(0);\r
cb7d01c0 135 } else {\r
136 return StringId;\r
137 }\r
08e4b3cf 138}\r
139\r
140\r
141/**\r
d1102dba
LG
142 Retrieves a string from a string package names by GUID in a specific language.\r
143 If the language is not specified, then a string from a string package in the\r
144 current platform language is retrieved. If the string can not be retrieved\r
145 using the specified language or the current platform language, then the string\r
146 is retrieved from the string package in the first language the string package\r
147 supports. The returned string is allocated using AllocatePool(). The caller\r
cb7d01c0 148 is responsible for freeing the allocated buffer using FreePool().\r
d1102dba 149\r
cb7d01c0 150 If PackageListGuid is NULL, then ASSERT().\r
151 If StringId is 0, then ASSERT.\r
152\r
d1102dba 153 @param[in] PackageListGuid The GUID of a package list that was previously\r
cb7d01c0 154 registered in the HII Database.\r
d1102dba 155 @param[in] StringId The identifier of the string to retrieved from the\r
cb7d01c0 156 string package associated with PackageListGuid.\r
d1102dba
LG
157 @param[in] Language The language of the string to retrieve. If this\r
158 parameter is NULL, then the current platform\r
159 language is used. The format of Language must\r
cb7d01c0 160 follow the language format assumed the HII Database.\r
161\r
162 @retval NULL The package list specified by PackageListGuid is not present in the\r
163 HII Database.\r
164 @retval NULL The string specified by StringId is not present in the string package.\r
165 @retval Other The string was returned.\r
08e4b3cf 166\r
167**/\r
cb7d01c0 168EFI_STRING\r
08e4b3cf 169EFIAPI\r
cb7d01c0 170HiiGetPackageString (\r
171 IN CONST EFI_GUID *PackageListGuid,\r
172 IN EFI_STRING_ID StringId,\r
173 IN CONST CHAR8 *Language OPTIONAL\r
08e4b3cf 174 )\r
175{\r
cb7d01c0 176 EFI_HANDLE *HiiHandleBuffer;\r
177 EFI_HANDLE HiiHandle;\r
08e4b3cf 178\r
cb7d01c0 179 ASSERT (PackageListGuid != NULL);\r
08e4b3cf 180\r
cb7d01c0 181 HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);\r
182 if (HiiHandleBuffer == NULL) {\r
183 return NULL;\r
08e4b3cf 184 }\r
185\r
cb7d01c0 186 HiiHandle = HiiHandleBuffer[0];\r
4a1102c9 187 FreePool (HiiHandleBuffer);\r
188\r
cb7d01c0 189 return HiiGetString (HiiHandle, StringId, Language);\r
08e4b3cf 190}\r
191\r
192/**\r
cb7d01c0 193 Retrieves a string from a string package in a specific language. If the language\r
d1102dba
LG
194 is not specified, then a string from a string package in the current platform\r
195 language is retrieved. If the string can not be retrieved using the specified\r
196 language or the current platform language, then the string is retrieved from\r
197 the string package in the first language the string package supports. The\r
198 returned string is allocated using AllocatePool(). The caller is responsible\r
cb7d01c0 199 for freeing the allocated buffer using FreePool().\r
d1102dba 200\r
cb7d01c0 201 If HiiHandle is NULL, then ASSERT().\r
202 If StringId is 0, then ASSET.\r
203\r
204 @param[in] HiiHandle A handle that was previously registered in the HII Database.\r
d1102dba 205 @param[in] StringId The identifier of the string to retrieved from the string\r
cb7d01c0 206 package associated with HiiHandle.\r
d1102dba
LG
207 @param[in] Language The language of the string to retrieve. If this parameter\r
208 is NULL, then the current platform language is used. The\r
209 format of Language must follow the language format assumed\r
cb7d01c0 210 the HII Database.\r
211\r
212 @retval NULL The string specified by StringId is not present in the string package.\r
213 @retval Other The string was returned.\r
08e4b3cf 214\r
215**/\r
cb7d01c0 216EFI_STRING\r
08e4b3cf 217EFIAPI\r
cb7d01c0 218HiiGetString (\r
219 IN EFI_HII_HANDLE HiiHandle,\r
220 IN EFI_STRING_ID StringId,\r
221 IN CONST CHAR8 *Language OPTIONAL\r
08e4b3cf 222 )\r
223{\r
224 EFI_STATUS Status;\r
cb7d01c0 225 UINTN StringSize;\r
226 CHAR16 TempString;\r
227 EFI_STRING String;\r
228 CHAR8 *SupportedLanguages;\r
229 CHAR8 *PlatformLanguage;\r
ea7cd3ec 230 CHAR8 *BestLanguage;\r
08e4b3cf 231\r
cb7d01c0 232 ASSERT (HiiHandle != NULL);\r
233 ASSERT (StringId != 0);\r
08e4b3cf 234\r
cb7d01c0 235 //\r
236 // Initialize all allocated buffers to NULL\r
d1102dba 237 //\r
cb7d01c0 238 SupportedLanguages = NULL;\r
239 PlatformLanguage = NULL;\r
240 BestLanguage = NULL;\r
241 String = NULL;\r
08e4b3cf 242\r
cb7d01c0 243 //\r
244 // Get the languages that the package specified by HiiHandle supports\r
245 //\r
246 SupportedLanguages = HiiGetSupportedLanguages (HiiHandle);\r
247 if (SupportedLanguages == NULL) {\r
248 goto Error;\r
08e4b3cf 249 }\r
250\r
cb7d01c0 251 //\r
252 // Get the current platform language setting\r
253 //\r
f01b91ae 254 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
08e4b3cf 255\r
cb7d01c0 256 //\r
d1102dba 257 // If Languag is NULL, then set it to an empty string, so it will be\r
cb7d01c0 258 // skipped by GetBestLanguage()\r
259 //\r
260 if (Language == NULL) {\r
261 Language = "";\r
262 }\r
08e4b3cf 263\r
cb7d01c0 264 //\r
265 // Get the best matching language from SupportedLanguages\r
266 //\r
267 BestLanguage = GetBestLanguage (\r
d1102dba 268 SupportedLanguages,\r
cb7d01c0 269 FALSE, // RFC 4646 mode\r
d1102dba 270 Language, // Highest priority\r
cb7d01c0 271 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority\r
d1102dba 272 SupportedLanguages, // Lowest priority\r
cb7d01c0 273 NULL\r
274 );\r
275 if (BestLanguage == NULL) {\r
276 goto Error;\r
277 }\r
08e4b3cf 278\r
cb7d01c0 279 //\r
280 // Retrieve the size of the string in the string package for the BestLanguage\r
281 //\r
282 StringSize = 0;\r
283 Status = gHiiString->GetString (\r
284 gHiiString,\r
285 BestLanguage,\r
286 HiiHandle,\r
287 StringId,\r
288 &TempString,\r
289 &StringSize,\r
290 NULL\r
291 );\r
292 //\r
d1102dba
LG
293 // If GetString() returns EFI_SUCCESS for a zero size,\r
294 // then there are no supported languages registered for HiiHandle. If GetString()\r
cb7d01c0 295 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r
296 // in the HII Database\r
297 //\r
298 if (Status != EFI_BUFFER_TOO_SMALL) {\r
299 goto Error;\r
300 }\r
08e4b3cf 301\r
cb7d01c0 302 //\r
303 // Allocate a buffer for the return string\r
304 //\r
305 String = AllocateZeroPool (StringSize);\r
306 if (String == NULL) {\r
307 goto Error;\r
308 }\r
08e4b3cf 309\r
cb7d01c0 310 //\r
311 // Retrieve the string from the string package\r
312 //\r
313 Status = gHiiString->GetString (\r
314 gHiiString,\r
315 BestLanguage,\r
316 HiiHandle,\r
317 StringId,\r
318 String,\r
319 &StringSize,\r
320 NULL\r
321 );\r
322 if (EFI_ERROR (Status)) {\r
323 //\r
324 // Free the buffer and return NULL if the supported languages can not be retrieved.\r
325 //\r
326 FreePool (String);\r
327 String = NULL;\r
08e4b3cf 328 }\r
329\r
cb7d01c0 330Error:\r
331 //\r
332 // Free allocated buffers\r
333 //\r
334 if (SupportedLanguages != NULL) {\r
335 FreePool (SupportedLanguages);\r
336 }\r
337 if (PlatformLanguage != NULL) {\r
338 FreePool (PlatformLanguage);\r
339 }\r
340 if (BestLanguage != NULL) {\r
341 FreePool (BestLanguage);\r
08e4b3cf 342 }\r
343\r
cb7d01c0 344 //\r
345 // Return the Null-terminated Unicode string\r
346 //\r
347 return String;\r
08e4b3cf 348}\r
349\r