]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/UefiHiiLib/HiiString.c
MdeModulePkg: Clean up source files
[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 - 2018, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
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
18/**\r
19 This function create a new string in String Package or updates an existing\r
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
24 is not NULL, then the string is added or updated for the set of languages\r
25 specified by SupportedLanguages.\r
26\r
27 If HiiHandle is NULL, then ASSERT().\r
28 If String is NULL, then ASSERT().\r
29\r
30 @param[in] HiiHandle A handle that was previously registered in the\r
31 HII Database.\r
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
39 with HiiHandle.\r
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
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
54\r
55**/\r
56EFI_STRING_ID\r
57EFIAPI\r
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
63 )\r
64{\r
65 EFI_STATUS Status;\r
66 CHAR8 *AllocatedLanguages;\r
67 CHAR8 *Supported;\r
68 CHAR8 *Language;\r
69\r
70 ASSERT (HiiHandle != NULL);\r
71 ASSERT (String != NULL);\r
72\r
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
82 AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);\r
83 }\r
84\r
85 //\r
86 // If there are not enough resources for the supported languages string, then return a StringId of 0\r
87 //\r
88 if (AllocatedLanguages == NULL) {\r
89 return (EFI_STRING_ID)(0);\r
90 }\r
91\r
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
101\r
102 //\r
103 // Search for the next language separator and replace it with a Null-terminator\r
104 //\r
105 for (; *Supported != 0 && *Supported != ';'; Supported++);\r
106 if (*Supported != 0) {\r
107 *(Supported++) = '\0';\r
108 }\r
109\r
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
116\r
117 //\r
118 // If StringId is 0, then call NewString(). Otherwise, call SetString()\r
119 //\r
120 if (StringId == (EFI_STRING_ID)(0)) {\r
121 Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r
122 } else {\r
123 Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r
124 }\r
125\r
126 //\r
127 // If there was an error, then break out of the loop and return a StringId of 0\r
128 //\r
129 if (EFI_ERROR (Status)) {\r
130 break;\r
131 }\r
132 }\r
133\r
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
141 } else {\r
142 return StringId;\r
143 }\r
144}\r
145\r
146\r
147/**\r
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
154 is responsible for freeing the allocated buffer using FreePool().\r
155\r
156 If PackageListGuid is NULL, then ASSERT().\r
157 If StringId is 0, then ASSERT.\r
158\r
159 @param[in] PackageListGuid The GUID of a package list that was previously\r
160 registered in the HII Database.\r
161 @param[in] StringId The identifier of the string to retrieved from the\r
162 string package associated with PackageListGuid.\r
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
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
172\r
173**/\r
174EFI_STRING\r
175EFIAPI\r
176HiiGetPackageString (\r
177 IN CONST EFI_GUID *PackageListGuid,\r
178 IN EFI_STRING_ID StringId,\r
179 IN CONST CHAR8 *Language OPTIONAL\r
180 )\r
181{\r
182 EFI_HANDLE *HiiHandleBuffer;\r
183 EFI_HANDLE HiiHandle;\r
184\r
185 ASSERT (PackageListGuid != NULL);\r
186\r
187 HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);\r
188 if (HiiHandleBuffer == NULL) {\r
189 return NULL;\r
190 }\r
191\r
192 HiiHandle = HiiHandleBuffer[0];\r
193 FreePool (HiiHandleBuffer);\r
194\r
195 return HiiGetString (HiiHandle, StringId, Language);\r
196}\r
197\r
198/**\r
199 Retrieves a string from a string package in a specific language. If the language\r
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
205 for freeing the allocated buffer using FreePool().\r
206\r
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
211 @param[in] StringId The identifier of the string to retrieved from the string\r
212 package associated with HiiHandle.\r
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
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
220\r
221**/\r
222EFI_STRING\r
223EFIAPI\r
224HiiGetString (\r
225 IN EFI_HII_HANDLE HiiHandle,\r
226 IN EFI_STRING_ID StringId,\r
227 IN CONST CHAR8 *Language OPTIONAL\r
228 )\r
229{\r
230 EFI_STATUS Status;\r
231 UINTN StringSize;\r
232 CHAR16 TempString;\r
233 EFI_STRING String;\r
234 CHAR8 *SupportedLanguages;\r
235 CHAR8 *PlatformLanguage;\r
236 CHAR8 *BestLanguage;\r
237\r
238 ASSERT (HiiHandle != NULL);\r
239 ASSERT (StringId != 0);\r
240\r
241 //\r
242 // Initialize all allocated buffers to NULL\r
243 //\r
244 SupportedLanguages = NULL;\r
245 PlatformLanguage = NULL;\r
246 BestLanguage = NULL;\r
247 String = NULL;\r
248\r
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
255 }\r
256\r
257 //\r
258 // Get the current platform language setting\r
259 //\r
260 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
261\r
262 //\r
263 // If Languag is NULL, then set it to an empty string, so it will be\r
264 // skipped by GetBestLanguage()\r
265 //\r
266 if (Language == NULL) {\r
267 Language = "";\r
268 }\r
269\r
270 //\r
271 // Get the best matching language from SupportedLanguages\r
272 //\r
273 BestLanguage = GetBestLanguage (\r
274 SupportedLanguages,\r
275 FALSE, // RFC 4646 mode\r
276 Language, // Highest priority\r
277 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority\r
278 SupportedLanguages, // Lowest priority\r
279 NULL\r
280 );\r
281 if (BestLanguage == NULL) {\r
282 goto Error;\r
283 }\r
284\r
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
299 // If GetString() returns EFI_SUCCESS for a zero size,\r
300 // then there are no supported languages registered for HiiHandle. If GetString()\r
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
307\r
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
315\r
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
334 }\r
335\r
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
348 }\r
349\r
350 //\r
351 // Return the Null-terminated Unicode string\r
352 //\r
353 return String;\r
354}\r
355\r