3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Common Library Routines to assist to handle String and Language.
22 #include "UefiIfrLibrary.h"
25 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
26 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
27 // The last 2 CHAR8 values are the ISO 639-1 code.
29 CHAR8 Iso639ToRfc3066ConversionTable
[] =
169 ConvertRfc3066LanguageToIso639Language (
170 CHAR8
*LanguageRfc3066
,
171 CHAR8
*LanguageIso639
176 Convert language code from RFC3066 to ISO639-2.
179 LanguageRfc3066 - RFC3066 language code.
180 LanguageIso639 - ISO639-2 language code.
183 EFI_SUCCESS - Language code converted.
184 EFI_NOT_FOUND - Language code not found.
190 if ((LanguageRfc3066
[2] != '-') && (LanguageRfc3066
[2] != 0)) {
191 EfiCopyMem (LanguageIso639
, LanguageRfc3066
, 3);
195 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
196 if (EfiCompareMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2) == 0) {
197 EfiCopyMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3);
202 return EFI_NOT_FOUND
;
207 CHAR8
*SupportedLanguages
212 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
213 be converted to "engfra".
216 SupportedLanguages - The RFC3066 language list.
219 The ISO639-2 language list.
226 CHAR8 LangRfc3066
[RFC_3066_ENTRY_SIZE
];
227 CHAR8 LangIso639
[ISO_639_2_ENTRY_SIZE
];
230 ReturnValue
= EfiLibAllocateZeroPool (EfiAsciiStrSize (SupportedLanguages
));
231 if (ReturnValue
== NULL
) {
235 Languages
= ReturnValue
;
236 LangCodes
= SupportedLanguages
;
237 while (*LangCodes
!= 0) {
238 GetNextLanguage (&LangCodes
, LangRfc3066
);
240 Status
= ConvertRfc3066LanguageToIso639Language (LangRfc3066
, LangIso639
);
241 if (!EFI_ERROR (Status
)) {
242 EfiCopyMem (Languages
, LangIso639
, 3);
243 Languages
= Languages
+ 3;
257 Determine what is the current language setting
260 Lang - Pointer of system language
271 // Get current language setting
273 Size
= RFC_3066_ENTRY_SIZE
;
274 Status
= gRT
->GetVariable (
276 &gEfiGlobalVariableGuid
,
282 if (EFI_ERROR (Status
)) {
283 EfiAsciiStrCpy (Lang
, (CHAR8
*) "en-US");
291 IN OUT CHAR8
**LangCode
,
297 Get next language from language code list (with separator ';').
300 LangCode - On input: point to first language in the list. On output: point to
301 next language in the list, or NULL if no more language in the list.
302 Lang - The first language in the list.
312 if (LangCode
== NULL
|| *LangCode
== NULL
) {
318 StringPtr
= *LangCode
;
319 while (StringPtr
[Index
] != 0 && StringPtr
[Index
] != ';') {
323 EfiCopyMem (Lang
, StringPtr
, Index
);
326 if (StringPtr
[Index
] == ';') {
329 *LangCode
= StringPtr
+ Index
;
333 GetSupportedLanguages (
334 IN EFI_HII_HANDLE HiiHandle
339 This function returns the list of supported languages, in the format specified
340 in UEFI specification Appendix M.
343 HiiHandle - The HII package list handle.
346 The supported languages.
352 CHAR8
*LanguageString
;
354 LocateHiiProtocols ();
357 // Collect current supported Languages for given HII handle
360 LanguageString
= EfiLibAllocatePool (BufferSize
);
361 Status
= gIfrLibHiiString
->GetLanguages (gIfrLibHiiString
, HiiHandle
, LanguageString
, &BufferSize
);
362 if (Status
== EFI_BUFFER_TOO_SMALL
) {
363 gBS
->FreePool (LanguageString
);
364 LanguageString
= EfiLibAllocatePool (BufferSize
);
365 Status
= gIfrLibHiiString
->GetLanguages (gIfrLibHiiString
, HiiHandle
, LanguageString
, &BufferSize
);
368 if (EFI_ERROR (Status
)) {
369 LanguageString
= NULL
;
372 return LanguageString
;
376 GetSupportedLanguageNumber (
377 IN EFI_HII_HANDLE HiiHandle
382 This function returns the number of supported languages
385 HiiHandle - The HII package list handle.
388 The number of supported languages.
393 CHAR8
*LanguageString
;
395 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
397 Languages
= GetSupportedLanguages (HiiHandle
);
398 if (Languages
== NULL
) {
403 LanguageString
= Languages
;
404 while (*LanguageString
!= 0) {
405 GetNextLanguage (&LanguageString
, Lang
);
408 gBS
->FreePool (Languages
);
414 GetStringFromHandle (
415 IN EFI_HII_HANDLE HiiHandle
,
416 IN EFI_STRING_ID StringId
,
417 OUT EFI_STRING
*String
422 Get string specified by StringId form the HiiHandle.
425 HiiHandle - The HII handle of package list.
426 StringId - The String ID.
427 String - The output string.
430 EFI_NOT_FOUND - String is not found.
431 EFI_SUCCESS - Operation is successful.
432 EFI_OUT_OF_RESOURCES - There is not enought memory in the system.
433 EFI_INVALID_PARAMETER - The String is NULL.
440 if (String
== NULL
) {
441 return EFI_INVALID_PARAMETER
;
444 StringSize
= IFR_LIB_DEFAULT_STRING_SIZE
;
445 *String
= EfiLibAllocateZeroPool (StringSize
);
446 if (*String
== NULL
) {
447 return EFI_OUT_OF_RESOURCES
;
450 Status
= IfrLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
451 if (Status
== EFI_BUFFER_TOO_SMALL
) {
452 gBS
->FreePool (*String
);
453 *String
= EfiLibAllocateZeroPool (StringSize
);
454 if (*String
== NULL
) {
455 return EFI_OUT_OF_RESOURCES
;
457 Status
= IfrLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
465 IN EFI_GUID
*ProducerGuid
,
466 IN EFI_STRING_ID StringId
,
467 OUT EFI_STRING
*String
472 Get the string given the StringId and String package Producer's Guid.
475 ProducerGuid - The Guid of String package list.
476 StringId - The String ID.
477 String - The output string.
480 EFI_NOT_FOUND - String is not found.
481 EFI_SUCCESS - Operation is successful.
482 EFI_OUT_OF_RESOURCES - There is not enought memory in the system.
488 UINTN HandleBufferLen
;
489 EFI_HII_HANDLE
*HiiHandleBuffer
;
492 Status
= GetHiiHandles (&HandleBufferLen
, &HiiHandleBuffer
);
493 if (EFI_ERROR(Status
)) {
496 for (Index
= 0; Index
< (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
)); Index
++) {
497 Status
= ExtractGuidFromHiiHandle (HiiHandleBuffer
[Index
], &Guid
);
498 if (EFI_ERROR(Status
)) {
501 if (EfiCompareGuid (&Guid
, ProducerGuid
) == TRUE
) {
506 if (Index
>= (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
))) {
507 Status
= EFI_NOT_FOUND
;
511 Status
= GetStringFromHandle (HiiHandleBuffer
[Index
], StringId
, String
);
514 if (HiiHandleBuffer
!= NULL
) {
515 gBS
->FreePool (HiiHandleBuffer
);
522 IN EFI_HII_HANDLE PackageList
,
523 OUT EFI_STRING_ID
*StringId
,
524 IN CONST EFI_STRING String
529 This function adds the string into String Package of each language.
532 PackageList - Handle of the package list where this string will be added.
533 StringId - On return, contains the new strings id, which is unique within PackageList.
534 String - Points to the new null-terminated string.
537 EFI_SUCCESS - The new string was added successfully.
538 EFI_NOT_FOUND - The specified PackageList could not be found in database.
539 EFI_OUT_OF_RESOURCES - Could not add the string due to lack of resources.
540 EFI_INVALID_PARAMETER - String is NULL or StringId is NULL is NULL.
547 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
549 Status
= EFI_SUCCESS
;
551 LocateHiiProtocols ();
553 Languages
= GetSupportedLanguages (PackageList
);
555 LangStrings
= Languages
;
556 while (*LangStrings
!= 0) {
557 GetNextLanguage (&LangStrings
, Lang
);
559 Status
= gIfrLibHiiString
->NewString (
568 if (EFI_ERROR (Status
)) {
573 gBS
->FreePool (Languages
);
580 IN EFI_HII_HANDLE PackageList
,
581 IN EFI_STRING_ID StringId
,
582 OUT EFI_STRING String
,
583 IN OUT UINTN
*StringSize
588 This function try to retrieve string from String package of current language.
589 If fail, it try to retrieve string from String package of first language it support.
592 PackageList - The package list in the HII database to search for the specified string.
593 StringId - The string's id, which is unique within PackageList.
594 String - Points to the new null-terminated string.
595 StringSize - On entry, points to the size of the buffer pointed to by String, in bytes. On return,
596 points to the length of the string, in bytes.
599 EFI_SUCCESS - The string was returned successfully.
600 EFI_NOT_FOUND - The string specified by StringId is not available.
601 EFI_BUFFER_TOO_SMALL - The buffer specified by StringLength is too small to hold the string.
602 EFI_INVALID_PARAMETER - The String or StringSize was NULL.
609 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
610 CHAR8 CurrentLang
[RFC_3066_ENTRY_SIZE
];
612 LocateHiiProtocols ();
614 GetCurrentLanguage (CurrentLang
);
616 Status
= gIfrLibHiiString
->GetString (
626 if (EFI_ERROR (Status
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
627 Languages
= GetSupportedLanguages (PackageList
);
628 LangStrings
= Languages
;
629 GetNextLanguage (&LangStrings
, Lang
);
630 gBS
->FreePool (Languages
);
632 Status
= gIfrLibHiiString
->GetString (
648 IN EFI_HII_HANDLE PackageList
,
649 IN EFI_STRING_ID StringId
,
650 IN CONST EFI_STRING String
655 This function updates the string in String package of each language.
658 PackageList - The package list containing the strings.
659 StringId - The string's id, which is unique within PackageList.
660 String - Points to the new null-terminated string.
663 EFI_SUCCESS - The string was updated successfully.
664 EFI_NOT_FOUND - The string specified by StringId is not in the database.
665 EFI_INVALID_PARAMETER - The String was NULL.
666 EFI_OUT_OF_RESOURCES - The system is out of resources to accomplish the task.
673 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
675 Status
= EFI_SUCCESS
;
677 LocateHiiProtocols ();
679 Languages
= GetSupportedLanguages (PackageList
);
681 LangStrings
= Languages
;
682 while (*LangStrings
!= 0) {
683 GetNextLanguage (&LangStrings
, Lang
);
685 Status
= gIfrLibHiiString
->SetString (
693 if (EFI_ERROR (Status
)) {
698 gBS
->FreePool (Languages
);