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