TianoCore EDK2 master
Loading...
Searching...
No Matches
HiiString.c
Go to the documentation of this file.
1
10#include "InternalHiiLib.h"
11
50EFI_STRING_ID
51EFIAPI
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
169EFI_STRING
170EFIAPI
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
209EFI_STRING
210EFIAPI
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
253EFI_STRING
254EFIAPI
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 (
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 (
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}
UINT64 UINTN
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
INTN EFIAPI AsciiStrnCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString, IN UINTN Length)
Definition: String.c:872
UINTN EFIAPI AsciiStrSize(IN CONST CHAR8 *String)
Definition: String.c:681
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
EFI_HII_HANDLE *EFIAPI HiiGetHiiHandles(IN CONST EFI_GUID *PackageListGuid OPTIONAL)
Definition: HiiLib.c:286
CHAR8 *EFIAPI HiiGetSupportedLanguages(IN EFI_HII_HANDLE HiiHandle)
Definition: HiiLanguage.c:31
EFI_STRING EFIAPI HiiGetStringEx(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId, IN CONST CHAR8 *Language OPTIONAL, IN BOOLEAN TryBestLanguage)
Definition: HiiString.c:255
EFI_STRING EFIAPI HiiGetString(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId, IN CONST CHAR8 *Language OPTIONAL)
Definition: HiiString.c:211
EFI_STRING EFIAPI HiiGetPackageString(IN CONST EFI_GUID *PackageListGuid, IN EFI_STRING_ID StringId, IN CONST CHAR8 *Language OPTIONAL)
Definition: HiiString.c:171
EFI_STRING_ID EFIAPI HiiSetString(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId OPTIONAL, IN CONST EFI_STRING String, IN CONST CHAR8 *SupportedLanguages OPTIONAL)
Definition: HiiString.c:52
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
EFI_HII_STRING_PROTOCOL * gHiiString
VOID * EFI_HII_HANDLE
EFI_STATUS EFIAPI GetEfiGlobalVariable2(IN CONST CHAR16 *Name, OUT VOID **Value, OUT UINTN *Size OPTIONAL)
Definition: UefiLib.c:1470
CHAR8 *EFIAPI GetBestLanguage(IN CONST CHAR8 *SupportedLanguages, IN UINTN Iso639Language,...)
Definition: UefiLib.c:1522
Definition: Base.h:213