2 HII Library implementation that uses DXE protocols and services.
4 Copyright (c) 2006 - 2008, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "InternalHiiLib.h"
20 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
21 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
22 // The last 2 CHAR8 values are the ISO 639-1 code.
24 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 Iso639ToRfc3066ConversionTable
[] =
166 This function create a new string in String Package or updates an existing
167 string in a String Package. If StringId is 0, then a new string is added to
168 a String Package. If StringId is not zero, then a string in String Package is
169 updated. If SupportedLanguages is NULL, then the string is added or updated
170 for all the languages that the String Package supports. If SupportedLanguages
171 is not NULL, then the string is added or updated for the set of languages
172 specified by SupportedLanguages.
174 If HiiHandle is NULL, then ASSERT().
175 If String is NULL, then ASSERT().
177 @param[in] HiiHandle A handle that was previously registered in the
179 @param[in] StringId If zero, then a new string is created in the
180 String Package associated with HiiHandle. If
181 non-zero, then the string specified by StringId
182 is updated in the String Package associated
184 @param[in] String A pointer to the Null-terminated Unicode string
185 to add or update in the String Package associated
187 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of
188 language codes. If this parameter is NULL, then
189 String is added or updated in the String Package
190 associated with HiiHandle for all the languages
191 that the String Package supports. If this
192 parameter is not NULL, then then String is added
193 or updated in the String Package associated with
194 HiiHandle for the set oflanguages specified by
195 SupportedLanguages. The format of
196 SupportedLanguages must follow the language
197 format assumed the HII Database.
199 @retval 0 The string could not be added or updated in the String Package.
200 @retval Other The EFI_STRING_ID of the newly added or updated string.
206 IN EFI_HII_HANDLE HiiHandle
,
207 IN EFI_STRING_ID StringId
, OPTIONAL
208 IN CONST EFI_STRING String
,
209 IN CONST CHAR8
*SupportedLanguages OPTIONAL
213 CHAR8
*AllocatedLanguages
;
216 EFI_STRING_ID NewStringId
;
218 ASSERT (HiiHandle
!= NULL
);
219 ASSERT (String
!= NULL
);
221 if (SupportedLanguages
== NULL
) {
223 // Retrieve the languages that the package specified by HiiHandle supports
225 AllocatedLanguages
= HiiGetSupportedLanguages (HiiHandle
);
228 // Allocate a copy of the SupportLanguages string that passed in
230 AllocatedLanguages
= AllocateCopyPool (AsciiStrLen (SupportedLanguages
), SupportedLanguages
);
234 // If there are not enough resources for the supported languages string, then return a StringId of 0
236 if (AllocatedLanguages
== NULL
) {
237 return (EFI_STRING_ID
)(0);
241 Status
= EFI_INVALID_PARAMETER
;
243 // Loop through each language that the string supports
245 for (Supported
= AllocatedLanguages
; *Supported
!= '\0'; ) {
247 // Cache a pointer to the beginning of the current language in the list of languages
249 Language
= Supported
;
252 // Search for the next language seperator and replace it with a Null-terminator
254 for (; *Supported
!= 0 && *Supported
!= ';'; Supported
++);
255 if (*Supported
!= 0) {
256 *(Supported
++) = '\0';
260 // If StringId is 0, then call NewString(). Otherwise, call SetString()
262 if (StringId
== (EFI_STRING_ID
)(0)) {
263 Status
= gHiiString
->NewString (gHiiString
, HiiHandle
, &NewStringId
, Language
, NULL
, String
, NULL
);
265 Status
= gHiiString
->SetString (gHiiString
, HiiHandle
, StringId
, Language
, String
, NULL
);
269 // If there was an error, then break out of the loop and return a StringId of 0
271 if (EFI_ERROR (Status
)) {
277 // Free the buffer of supported languages
279 FreePool (AllocatedLanguages
);
281 if (EFI_ERROR (Status
)) {
282 return (EFI_STRING_ID
)(0);
283 } else if (StringId
== (EFI_STRING_ID
)(0)) {
292 Retrieves a string from a string package names by GUID in a specific language.
293 If the language is not specified, then a string from a string package in the
294 current platform language is retrieved. If the string can not be retrieved
295 using the specified language or the current platform language, then the string
296 is retrieved from the string package in the first language the string package
297 supports. The returned string is allocated using AllocatePool(). The caller
298 is responsible for freeing the allocated buffer using FreePool().
300 If PackageListGuid is NULL, then ASSERT().
301 If StringId is 0, then ASSERT.
303 @param[in] PackageListGuid The GUID of a package list that was previously
304 registered in the HII Database.
305 @param[in] StringId The identifier of the string to retrieved from the
306 string package associated with PackageListGuid.
307 @param[in] Language The language of the string to retrieve. If this
308 parameter is NULL, then the current platform
309 language is used. The format of Language must
310 follow the language format assumed the HII Database.
312 @retval NULL The package list specified by PackageListGuid is not present in the
314 @retval NULL The string specified by StringId is not present in the string package.
315 @retval Other The string was returned.
320 HiiGetPackageString (
321 IN CONST EFI_GUID
*PackageListGuid
,
322 IN EFI_STRING_ID StringId
,
323 IN CONST CHAR8
*Language OPTIONAL
326 EFI_HANDLE
*HiiHandleBuffer
;
327 EFI_HANDLE HiiHandle
;
329 ASSERT (PackageListGuid
!= NULL
);
331 HiiHandleBuffer
= HiiGetHiiHandles (PackageListGuid
);
332 if (HiiHandleBuffer
== NULL
) {
336 HiiHandle
= HiiHandleBuffer
[0];
337 FreePool (HiiHandleBuffer
);
339 return HiiGetString (HiiHandle
, StringId
, Language
);
343 Retrieves a string from a string package in a specific language. If the language
344 is not specified, then a string from a string package in the current platform
345 language is retrieved. If the string can not be retrieved using the specified
346 language or the current platform language, then the string is retrieved from
347 the string package in the first language the string package supports. The
348 returned string is allocated using AllocatePool(). The caller is responsible
349 for freeing the allocated buffer using FreePool().
351 If HiiHandle is NULL, then ASSERT().
352 If StringId is 0, then ASSET.
354 @param[in] HiiHandle A handle that was previously registered in the HII Database.
355 @param[in] StringId The identifier of the string to retrieved from the string
356 package associated with HiiHandle.
357 @param[in] Language The language of the string to retrieve. If this parameter
358 is NULL, then the current platform language is used. The
359 format of Language must follow the language format assumed
362 @retval NULL The string specified by StringId is not present in the string package.
363 @retval Other The string was returned.
369 IN EFI_HII_HANDLE HiiHandle
,
370 IN EFI_STRING_ID StringId
,
371 IN CONST CHAR8
*Language OPTIONAL
378 CHAR8
*SupportedLanguages
;
379 CHAR8
*PlatformLanguage
;
382 ASSERT (HiiHandle
!= NULL
);
383 ASSERT (StringId
!= 0);
386 // Initialize all allocated buffers to NULL
388 SupportedLanguages
= NULL
;
389 PlatformLanguage
= NULL
;
394 // Get the languages that the package specified by HiiHandle supports
396 SupportedLanguages
= HiiGetSupportedLanguages (HiiHandle
);
397 if (SupportedLanguages
== NULL
) {
402 // Get the current platform language setting
404 PlatformLanguage
= GetEfiGlobalVariable (L
"PlatformLang");
407 // If Languag is NULL, then set it to an empty string, so it will be
408 // skipped by GetBestLanguage()
410 if (Language
== NULL
) {
415 // Get the best matching language from SupportedLanguages
417 BestLanguage
= GetBestLanguage (
419 FALSE
, // RFC 4646 mode
420 Language
, // Highest priority
421 PlatformLanguage
!= NULL
? PlatformLanguage
: "", // Next highest priority
422 SupportedLanguages
, // Lowest priority
425 if (BestLanguage
== NULL
) {
430 // Retrieve the size of the string in the string package for the BestLanguage
433 Status
= gHiiString
->GetString (
443 // If GetString() returns EFI_SUCCESS for a zero size,
444 // then there are no supported languages registered for HiiHandle. If GetString()
445 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
446 // in the HII Database
448 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
453 // Allocate a buffer for the return string
455 String
= AllocateZeroPool (StringSize
);
456 if (String
== NULL
) {
461 // Retrieve the string from the string package
463 Status
= gHiiString
->GetString (
472 if (EFI_ERROR (Status
)) {
474 // Free the buffer and return NULL if the supported languages can not be retrieved.
482 // Free allocated buffers
484 if (SupportedLanguages
!= NULL
) {
485 FreePool (SupportedLanguages
);
487 if (PlatformLanguage
!= NULL
) {
488 FreePool (PlatformLanguage
);
490 if (BestLanguage
!= NULL
) {
491 FreePool (BestLanguage
);
495 // Return the Null-terminated Unicode string
501 Convert language code from RFC3066 to ISO639-2.
503 @param LanguageRfc3066 RFC3066 language code.
504 @param LanguageIso639 ISO639-2 language code.
506 @retval EFI_SUCCESS Language code converted.
507 @retval EFI_NOT_FOUND Language code not found.
512 ConvertRfc3066LanguageToIso639Language (
513 IN CHAR8
*LanguageRfc3066
,
514 OUT CHAR8
*LanguageIso639
519 if ((LanguageRfc3066
[2] != '-') && (LanguageRfc3066
[2] != 0)) {
520 CopyMem (LanguageIso639
, LanguageRfc3066
, 3);
524 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
525 if (CompareMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2) == 0) {
526 CopyMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3);
531 return EFI_NOT_FOUND
;
536 Convert language code from ISO639-2 to RFC3066 and return the converted language.
537 Caller is responsible for freeing the allocated buffer.
539 LanguageIso639 contain a single ISO639-2 code such as
542 If LanguageIso639 is NULL, then ASSERT.
543 If LanguageRfc3066 is NULL, then ASSERT.
545 @param LanguageIso639 ISO639-2 language code.
547 @return the allocated buffer or NULL, if the language is not found.
552 ConvertIso639LanguageToRfc3066Language (
553 IN CONST CHAR8
*LanguageIso639
557 CHAR8
*Rfc3066Language
;
559 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
560 if (CompareMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3) == 0) {
561 Rfc3066Language
= AllocateZeroPool (3);
562 if (Rfc3066Language
!= NULL
) {
563 Rfc3066Language
= CopyMem (Rfc3066Language
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2);
565 return Rfc3066Language
;
573 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
574 be converted to "engfra".
576 @param SupportedLanguages The RFC3066 language list.
578 @return The ISO639-2 language list.
584 CHAR8
*SupportedLanguages
591 CHAR8 LangIso639
[ISO_639_2_ENTRY_SIZE
];
595 LanguageSize
= AsciiStrSize (SupportedLanguages
);
596 ReturnValue
= AllocateZeroPool (LanguageSize
);
597 if (ReturnValue
== NULL
) {
602 // Allocate working buffer to contain substring in SupportedLanguages;
604 LangRfc3066
= AllocatePool (LanguageSize
);
605 if (LangRfc3066
== NULL
) {
606 FreePool (ReturnValue
);
609 Languages
= ReturnValue
;
610 LangCodes
= SupportedLanguages
;
611 while (*LangCodes
!= 0) {
612 HiiLibGetNextLanguage (&LangCodes
, LangRfc3066
);
614 Status
= ConvertRfc3066LanguageToIso639Language (LangRfc3066
, LangIso639
);
615 if (!EFI_ERROR (Status
)) {
616 CopyMem (Languages
, LangIso639
, 3);
617 Languages
= Languages
+ 3;
621 FreePool (LangRfc3066
);