VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Library/UefiHiiLib/HiiString.c

Last change on this file was 99404, checked in by vboxsync, 22 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 13.4 KB
Line 
1/** @file
2 HII Library implementation that uses DXE protocols and services.
3
4 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include "InternalHiiLib.h"
11
12/**
13 This function create a new string in String Package or updates an existing
14 string in a String Package. If StringId is 0, then a new string is added to
15 a String Package. If StringId is not zero, then a string in String Package is
16 updated. If SupportedLanguages is NULL, then the string is added or updated
17 for all the languages that the String Package supports. If SupportedLanguages
18 is not NULL, then the string is added or updated for the set of languages
19 specified by SupportedLanguages.
20
21 If HiiHandle is NULL, then ASSERT().
22 If String is NULL, then ASSERT().
23
24 @param[in] HiiHandle A handle that was previously registered in the
25 HII Database.
26 @param[in] StringId If zero, then a new string is created in the
27 String Package associated with HiiHandle. If
28 non-zero, then the string specified by StringId
29 is updated in the String Package associated
30 with HiiHandle.
31 @param[in] String A pointer to the Null-terminated Unicode string
32 to add or update in the String Package associated
33 with HiiHandle.
34 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of
35 language codes. If this parameter is NULL, then
36 String is added or updated in the String Package
37 associated with HiiHandle for all the languages
38 that the String Package supports. If this
39 parameter is not NULL, then then String is added
40 or updated in the String Package associated with
41 HiiHandle for the set oflanguages specified by
42 SupportedLanguages. The format of
43 SupportedLanguages must follow the language
44 format assumed the HII Database.
45
46 @retval 0 The string could not be added or updated in the String Package.
47 @retval Other The EFI_STRING_ID of the newly added or updated string.
48
49**/
50EFI_STRING_ID
51EFIAPI
52HiiSetString (
53 IN EFI_HII_HANDLE HiiHandle,
54 IN EFI_STRING_ID StringId OPTIONAL,
55 IN CONST EFI_STRING String,
56 IN CONST CHAR8 *SupportedLanguages OPTIONAL
57 )
58{
59 EFI_STATUS Status;
60 CHAR8 *AllocatedLanguages;
61 CHAR8 *Supported;
62 CHAR8 *Language;
63
64 ASSERT (HiiHandle != NULL);
65 ASSERT (String != NULL);
66
67 if (SupportedLanguages == NULL) {
68 //
69 // Retrieve the languages that the package specified by HiiHandle supports
70 //
71 AllocatedLanguages = HiiGetSupportedLanguages (HiiHandle);
72 } else {
73 //
74 // Allocate a copy of the SupportLanguages string that passed in
75 //
76 AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);
77 }
78
79 //
80 // If there are not enough resources for the supported languages string, then return a StringId of 0
81 //
82 if (AllocatedLanguages == NULL) {
83 return (EFI_STRING_ID)(0);
84 }
85
86 Status = EFI_INVALID_PARAMETER;
87 //
88 // Loop through each language that the string supports
89 //
90 for (Supported = AllocatedLanguages; *Supported != '\0'; ) {
91 //
92 // Cache a pointer to the beginning of the current language in the list of languages
93 //
94 Language = Supported;
95
96 //
97 // Search for the next language separator and replace it with a Null-terminator
98 //
99 for ( ; *Supported != 0 && *Supported != ';'; Supported++) {
100 }
101
102 if (*Supported != 0) {
103 *(Supported++) = '\0';
104 }
105
106 if ((SupportedLanguages == NULL) && (AsciiStrnCmp (Language, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) == 0)) {
107 //
108 // Skip string package used for keyword protocol.
109 //
110 continue;
111 }
112
113 //
114 // If StringId is 0, then call NewString(). Otherwise, call SetString()
115 //
116 if (StringId == (EFI_STRING_ID)(0)) {
117 Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);
118 } else {
119 Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);
120 }
121
122 //
123 // If there was an error, then break out of the loop and return a StringId of 0
124 //
125 if (EFI_ERROR (Status)) {
126 break;
127 }
128 }
129
130 //
131 // Free the buffer of supported languages
132 //
133 FreePool (AllocatedLanguages);
134
135 if (EFI_ERROR (Status)) {
136 return (EFI_STRING_ID)(0);
137 } else {
138 return StringId;
139 }
140}
141
142/**
143 Retrieves a string from a string package names by GUID in a specific language.
144 If the language is not specified, then a string from a string package in the
145 current platform language is retrieved. If the string can not be retrieved
146 using the specified language or the current platform language, then the string
147 is retrieved from the string package in the first language the string package
148 supports. The returned string is allocated using AllocatePool(). The caller
149 is responsible for freeing the allocated buffer using FreePool().
150
151 If PackageListGuid is NULL, then ASSERT().
152 If StringId is 0, then ASSERT.
153
154 @param[in] PackageListGuid The GUID of a package list that was previously
155 registered in the HII Database.
156 @param[in] StringId The identifier of the string to retrieved from the
157 string package associated with PackageListGuid.
158 @param[in] Language The language of the string to retrieve. If this
159 parameter is NULL, then the current platform
160 language is used. The format of Language must
161 follow the language format assumed the HII Database.
162
163 @retval NULL The package list specified by PackageListGuid is not present in the
164 HII Database.
165 @retval NULL The string specified by StringId is not present in the string package.
166 @retval Other The string was returned.
167
168**/
169EFI_STRING
170EFIAPI
171HiiGetPackageString (
172 IN CONST EFI_GUID *PackageListGuid,
173 IN EFI_STRING_ID StringId,
174 IN CONST CHAR8 *Language OPTIONAL
175 )
176{
177 EFI_HII_HANDLE *HiiHandleBuffer;
178 EFI_HII_HANDLE HiiHandle;
179
180 ASSERT (PackageListGuid != NULL);
181
182 HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);
183 if (HiiHandleBuffer == NULL) {
184 return NULL;
185 }
186
187 HiiHandle = HiiHandleBuffer[0];
188 FreePool (HiiHandleBuffer);
189
190 return HiiGetString (HiiHandle, StringId, Language);
191}
192
193/**
194 Retrieves a string from a string package in a specific language specified in Language
195 or in the best lanaguage. See HiiGetStringEx () for the details.
196
197 @param[in] HiiHandle A handle that was previously registered in the HII Database.
198 @param[in] StringId The identifier of the string to retrieved from the string
199 package associated with HiiHandle.
200 @param[in] Language The language of the string to retrieve. If this parameter
201 is NULL, then the current platform language is used. The
202 format of Language must follow the language format assumed
203 the HII Database.
204
205 @retval NULL The string specified by StringId is not present in the string package.
206 @retval Other The string was returned.
207
208**/
209EFI_STRING
210EFIAPI
211HiiGetString (
212 IN EFI_HII_HANDLE HiiHandle,
213 IN EFI_STRING_ID StringId,
214 IN CONST CHAR8 *Language OPTIONAL
215 )
216{
217 return HiiGetStringEx (HiiHandle, StringId, Language, TRUE);
218}
219
220/**
221 Retrieves a string from a string package in a specific language or in the best
222 language at discretion of this function according to the priority of languages.
223 TryBestLanguage is used to get the string in the best language or in the language
224 specified in Language parameter. The behavior is,
225 If TryBestLanguage is TRUE, this function looks for the best language for the string.
226 - If the string can not be retrieved using the specified language or the current
227 platform language, then the string is retrieved from the string package in the
228 first language the string package supports.
229 If TryBestLanguage is FALSE, Language must be specified for retrieving the string.
230
231 The returned string is allocated using AllocatePool(). The caller is responsible
232 for freeing the allocated buffer using FreePool().
233
234 If HiiHandle is NULL, then ASSERT().
235 If StringId is 0, then ASSET.
236 If TryBestLanguage is FALE and Language is NULL, then ASSERT().
237
238 @param[in] HiiHandle A handle that was previously registered in the HII Database.
239 @param[in] StringId The identifier of the string to retrieved from the string
240 package associated with HiiHandle.
241 @param[in] Language The language of the string to retrieve. If this parameter
242 is NULL, then the current platform language is used. The
243 format of Language must follow the language format assumed
244 the HII Database.
245 @param[in] TryBestLanguage If TRUE, try to get the best matching language from all
246 supported languages.If FALSE, the Language must be assigned
247 for the StringID.
248
249 @retval NULL The string specified by StringId is not present in the string package.
250 @retval Other The string was returned.
251
252**/
253EFI_STRING
254EFIAPI
255HiiGetStringEx (
256 IN EFI_HII_HANDLE HiiHandle,
257 IN EFI_STRING_ID StringId,
258 IN CONST CHAR8 *Language OPTIONAL,
259 IN BOOLEAN TryBestLanguage
260 )
261{
262 EFI_STATUS Status;
263 UINTN StringSize;
264 CHAR16 TempString;
265 EFI_STRING String;
266 CHAR8 *SupportedLanguages;
267 CHAR8 *PlatformLanguage;
268 CHAR8 *BestLanguage;
269
270 ASSERT (HiiHandle != NULL);
271 ASSERT (StringId != 0);
272 //
273 // Language must be specified if TryBestLanguage = FALSE.
274 //
275 ASSERT (!(!TryBestLanguage && Language == NULL));
276 //
277 // Initialize all allocated buffers to NULL
278 //
279 SupportedLanguages = NULL;
280 PlatformLanguage = NULL;
281 BestLanguage = NULL;
282 String = NULL;
283
284 //
285 // Get the languages that the package specified by HiiHandle supports
286 //
287 SupportedLanguages = HiiGetSupportedLanguages (HiiHandle);
288 if (SupportedLanguages == NULL) {
289 goto Error;
290 }
291
292 //
293 // Get the current platform language setting
294 //
295 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **)&PlatformLanguage, NULL);
296
297 //
298 // If Languag is NULL, then set it to an empty string, so it will be
299 // skipped by GetBestLanguage()
300 //
301 if (Language == NULL) {
302 Language = "";
303 }
304
305 if (TryBestLanguage) {
306 //
307 // Get the best matching language from SupportedLanguages
308 //
309 BestLanguage = GetBestLanguage (
310 SupportedLanguages,
311 FALSE, // RFC 4646 mode
312 Language, // Highest priority
313 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority
314 SupportedLanguages, // Lowest priority
315 NULL
316 );
317 if (BestLanguage == NULL) {
318 goto Error;
319 }
320 } else {
321 BestLanguage = (CHAR8 *)Language;
322 }
323
324 //
325 // Retrieve the size of the string in the string package for the BestLanguage
326 //
327 StringSize = 0;
328 Status = gHiiString->GetString (
329 gHiiString,
330 BestLanguage,
331 HiiHandle,
332 StringId,
333 &TempString,
334 &StringSize,
335 NULL
336 );
337 //
338 // If GetString() returns EFI_SUCCESS for a zero size,
339 // then there are no supported languages registered for HiiHandle. If GetString()
340 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
341 // in the HII Database
342 //
343 if (Status != EFI_BUFFER_TOO_SMALL) {
344 goto Error;
345 }
346
347 //
348 // Allocate a buffer for the return string
349 //
350 String = AllocateZeroPool (StringSize);
351 if (String == NULL) {
352 goto Error;
353 }
354
355 //
356 // Retrieve the string from the string package
357 //
358 Status = gHiiString->GetString (
359 gHiiString,
360 BestLanguage,
361 HiiHandle,
362 StringId,
363 String,
364 &StringSize,
365 NULL
366 );
367 if (EFI_ERROR (Status)) {
368 //
369 // Free the buffer and return NULL if the supported languages can not be retrieved.
370 //
371 FreePool (String);
372 String = NULL;
373 }
374
375Error:
376 //
377 // Free allocated buffers
378 //
379 if (SupportedLanguages != NULL) {
380 FreePool (SupportedLanguages);
381 }
382
383 if (PlatformLanguage != NULL) {
384 FreePool (PlatformLanguage);
385 }
386
387 if (TryBestLanguage && (BestLanguage != NULL)) {
388 FreePool (BestLanguage);
389 }
390
391 //
392 // Return the Null-terminated Unicode string
393 //
394 return String;
395}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette