3 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
4 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 HiiHandleBuffer
= NULL
;
493 Status
= GetHiiHandles (&HandleBufferLen
, &HiiHandleBuffer
);
494 if (EFI_ERROR(Status
)) {
497 for (Index
= 0; Index
< (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
)); Index
++) {
498 Status
= ExtractGuidFromHiiHandle (HiiHandleBuffer
[Index
], &Guid
);
499 if (EFI_ERROR(Status
)) {
502 if (EfiCompareGuid (&Guid
, ProducerGuid
) == TRUE
) {
507 if (Index
>= (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
))) {
508 Status
= EFI_NOT_FOUND
;
512 Status
= GetStringFromHandle (HiiHandleBuffer
[Index
], StringId
, String
);
515 if (HiiHandleBuffer
!= NULL
) {
516 gBS
->FreePool (HiiHandleBuffer
);
523 IN EFI_HII_HANDLE PackageList
,
524 OUT EFI_STRING_ID
*StringId
,
525 IN CONST EFI_STRING String
530 This function adds the string into String Package of each language.
533 PackageList - Handle of the package list where this string will be added.
534 StringId - On return, contains the new strings id, which is unique within PackageList.
535 String - Points to the new null-terminated string.
538 EFI_SUCCESS - The new string was added successfully.
539 EFI_NOT_FOUND - The specified PackageList could not be found in database.
540 EFI_OUT_OF_RESOURCES - Could not add the string due to lack of resources.
541 EFI_INVALID_PARAMETER - String is NULL or StringId is NULL is NULL.
548 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
550 Status
= EFI_SUCCESS
;
552 LocateHiiProtocols ();
554 Languages
= GetSupportedLanguages (PackageList
);
555 if (Languages
== NULL
) {
556 return EFI_NOT_FOUND
;
559 if (StringId
== NULL
) {
560 return EFI_INVALID_PARAMETER
;
564 LangStrings
= Languages
;
565 while (*LangStrings
!= 0) {
566 GetNextLanguage (&LangStrings
, Lang
);
568 if (*StringId
== 0) {
569 Status
= gIfrLibHiiString
->NewString (
579 Status
= gIfrLibHiiString
->SetString (
589 if (EFI_ERROR (Status
)) {
594 gBS
->FreePool (Languages
);
601 IN EFI_HII_HANDLE PackageList
,
602 IN EFI_STRING_ID StringId
,
603 OUT EFI_STRING String
,
604 IN OUT UINTN
*StringSize
609 This function try to retrieve string from String package of current language.
610 If fail, it try to retrieve string from String package of first language it support.
613 PackageList - The package list in the HII database to search for the specified string.
614 StringId - The string's id, which is unique within PackageList.
615 String - Points to the new null-terminated string.
616 StringSize - On entry, points to the size of the buffer pointed to by String, in bytes. On return,
617 points to the length of the string, in bytes.
620 EFI_SUCCESS - The string was returned successfully.
621 EFI_NOT_FOUND - The string specified by StringId is not available.
622 EFI_BUFFER_TOO_SMALL - The buffer specified by StringLength is too small to hold the string.
623 EFI_INVALID_PARAMETER - The String or StringSize was NULL.
630 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
631 CHAR8 CurrentLang
[RFC_3066_ENTRY_SIZE
];
633 LocateHiiProtocols ();
635 GetCurrentLanguage (CurrentLang
);
637 Status
= gIfrLibHiiString
->GetString (
647 if (EFI_ERROR (Status
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
648 Languages
= GetSupportedLanguages (PackageList
);
649 LangStrings
= Languages
;
650 GetNextLanguage (&LangStrings
, Lang
);
651 gBS
->FreePool (Languages
);
653 Status
= gIfrLibHiiString
->GetString (
669 IN EFI_HII_HANDLE PackageList
,
670 IN EFI_STRING_ID StringId
,
671 IN CONST EFI_STRING String
676 This function updates the string in String package of each language.
679 PackageList - The package list containing the strings.
680 StringId - The string's id, which is unique within PackageList.
681 String - Points to the new null-terminated string.
684 EFI_SUCCESS - The string was updated successfully.
685 EFI_NOT_FOUND - The string specified by StringId is not in the database.
686 EFI_INVALID_PARAMETER - The String was NULL.
687 EFI_OUT_OF_RESOURCES - The system is out of resources to accomplish the task.
694 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
696 Status
= EFI_SUCCESS
;
698 LocateHiiProtocols ();
700 Languages
= GetSupportedLanguages (PackageList
);
701 if (Languages
== NULL
) {
702 return EFI_NOT_FOUND
;
705 LangStrings
= Languages
;
706 while (*LangStrings
!= 0) {
707 GetNextLanguage (&LangStrings
, Lang
);
709 Status
= gIfrLibHiiString
->SetString (
717 if (EFI_ERROR (Status
)) {
722 gBS
->FreePool (Languages
);