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.
23 #include "UefiIfrLibraryInternal.h"
26 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
27 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
28 // The last 2 CHAR8 values are the ISO 639-1 code.
30 CHAR8 Iso639ToRfc3066ConversionTable
[] =
171 Convert language code from RFC3066 to ISO639-2.
173 @param LanguageRfc3066 RFC3066 language code.
174 @param LanguageIso639 ISO639-2 language code.
176 @retval EFI_SUCCESS Language code converted.
177 @retval EFI_NOT_FOUND Language code not found.
181 ConvertRfc3066LanguageToIso639Language (
182 CHAR8
*LanguageRfc3066
,
183 CHAR8
*LanguageIso639
188 if ((LanguageRfc3066
[2] != '-') && (LanguageRfc3066
[2] != 0)) {
189 CopyMem (LanguageIso639
, LanguageRfc3066
, 3);
193 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
194 if (CompareMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2) == 0) {
195 CopyMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3);
200 return EFI_NOT_FOUND
;
205 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
206 be converted to "engfra".
208 @param SupportedLanguages The RFC3066 language list.
210 @return The ISO639-2 language list.
215 CHAR8
*SupportedLanguages
221 CHAR8 LangRfc3066
[RFC_3066_ENTRY_SIZE
];
222 CHAR8 LangIso639
[ISO_639_2_ENTRY_SIZE
];
225 ReturnValue
= AllocateZeroPool (AsciiStrSize (SupportedLanguages
));
226 if (ReturnValue
== NULL
) {
230 Languages
= ReturnValue
;
231 LangCodes
= SupportedLanguages
;
232 while (*LangCodes
!= 0) {
233 GetNextLanguage (&LangCodes
, LangRfc3066
);
235 Status
= ConvertRfc3066LanguageToIso639Language (LangRfc3066
, LangIso639
);
236 if (!EFI_ERROR (Status
)) {
237 CopyMem (Languages
, LangIso639
, 3);
238 Languages
= Languages
+ 3;
247 Determine what is the current language setting
249 @param Lang Pointer of system language
263 // Get current language setting
265 Size
= RFC_3066_ENTRY_SIZE
;
266 Status
= gRT
->GetVariable (
268 &gEfiGlobalVariableGuid
,
274 if (EFI_ERROR (Status
)) {
275 AsciiStrCpy (Lang
, (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLang
));
283 Get next language from language code list (with separator ';').
285 @param LangCode On input: point to first language in the list. On
286 output: point to next language in the list, or
287 NULL if no more language in the list.
288 @param Lang The first language in the list.
295 IN OUT CHAR8
**LangCode
,
302 if (LangCode
== NULL
|| *LangCode
== NULL
) {
308 StringPtr
= *LangCode
;
309 while (StringPtr
[Index
] != 0 && StringPtr
[Index
] != ';') {
313 CopyMem (Lang
, StringPtr
, Index
);
316 if (StringPtr
[Index
] == ';') {
319 *LangCode
= StringPtr
+ Index
;
324 This function returns the list of supported languages, in the format specified
325 in UEFI specification Appendix M.
327 @param HiiHandle The HII package list handle.
329 @return The supported languages.
333 GetSupportedLanguages (
334 IN EFI_HII_HANDLE HiiHandle
339 CHAR8
*LanguageString
;
341 LocateHiiProtocols ();
344 // Collect current supported Languages for given HII handle
347 LanguageString
= AllocatePool (BufferSize
);
348 Status
= gIfrLibHiiString
->GetLanguages (gIfrLibHiiString
, HiiHandle
, LanguageString
, &BufferSize
);
349 if (Status
== EFI_BUFFER_TOO_SMALL
) {
350 gBS
->FreePool (LanguageString
);
351 LanguageString
= AllocatePool (BufferSize
);
352 Status
= gIfrLibHiiString
->GetLanguages (gIfrLibHiiString
, HiiHandle
, LanguageString
, &BufferSize
);
355 if (EFI_ERROR (Status
)) {
356 LanguageString
= NULL
;
359 return LanguageString
;
364 This function returns the number of supported languages
366 @param HiiHandle The HII package list handle.
368 @return The number of supported languages.
372 GetSupportedLanguageNumber (
373 IN EFI_HII_HANDLE HiiHandle
377 CHAR8
*LanguageString
;
379 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
381 Languages
= GetSupportedLanguages (HiiHandle
);
382 if (Languages
== NULL
) {
387 LanguageString
= Languages
;
388 while (*LanguageString
!= 0) {
389 GetNextLanguage (&LanguageString
, Lang
);
392 gBS
->FreePool (Languages
);
399 Get string specified by StringId form the HiiHandle.
401 @param HiiHandle The HII handle of package list.
402 @param StringId The String ID.
403 @param String The output string.
405 @retval EFI_NOT_FOUND String is not found.
406 @retval EFI_SUCCESS Operation is successful.
407 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
408 @retval EFI_INVALID_PARAMETER The String is NULL.
412 GetStringFromHandle (
413 IN EFI_HII_HANDLE HiiHandle
,
414 IN EFI_STRING_ID StringId
,
415 OUT EFI_STRING
*String
421 if (String
== NULL
) {
422 return EFI_INVALID_PARAMETER
;
425 StringSize
= IFR_LIB_DEFAULT_STRING_SIZE
;
426 *String
= AllocateZeroPool (StringSize
);
427 if (*String
== NULL
) {
428 return EFI_OUT_OF_RESOURCES
;
431 Status
= IfrLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
432 if (Status
== EFI_BUFFER_TOO_SMALL
) {
433 gBS
->FreePool (*String
);
434 *String
= AllocateZeroPool (StringSize
);
435 if (*String
== NULL
) {
436 return EFI_OUT_OF_RESOURCES
;
438 Status
= IfrLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
446 Get the string given the StringId and String package Producer's Guid.
448 @param ProducerGuid The Guid of String package list.
449 @param StringId The String ID.
450 @param String The output string.
452 @retval EFI_NOT_FOUND String is not found.
453 @retval EFI_SUCCESS Operation is successful.
454 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
459 IN EFI_GUID
*ProducerGuid
,
460 IN EFI_STRING_ID StringId
,
461 OUT EFI_STRING
*String
466 UINTN HandleBufferLen
;
467 EFI_HII_HANDLE
*HiiHandleBuffer
;
470 Status
= GetHiiHandles (&HandleBufferLen
, &HiiHandleBuffer
);
471 if (EFI_ERROR(Status
)) {
474 for (Index
= 0; Index
< (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
)); Index
++) {
475 Status
= ExtractGuidFromHiiHandle (HiiHandleBuffer
[Index
], &Guid
);
476 if (EFI_ERROR(Status
)) {
479 if (CompareGuid (&Guid
, ProducerGuid
) == TRUE
) {
484 if (Index
>= (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
))) {
485 Status
= EFI_NOT_FOUND
;
489 Status
= GetStringFromHandle (HiiHandleBuffer
[Index
], StringId
, String
);
492 if (HiiHandleBuffer
!= NULL
) {
493 gBS
->FreePool (HiiHandleBuffer
);
500 This function adds the string into String Package of each language.
502 @param PackageList Handle of the package list where this string will
504 @param StringId On return, contains the new strings id, which is
505 unique within PackageList.
506 @param String Points to the new null-terminated string.
508 @retval EFI_SUCCESS The new string was added successfully.
509 @retval EFI_NOT_FOUND The specified PackageList could not be found in
511 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
512 @retval EFI_INVALID_PARAMETER String is NULL or StringId is NULL is NULL.
517 IN EFI_HII_HANDLE PackageList
,
518 OUT EFI_STRING_ID
*StringId
,
519 IN CONST EFI_STRING String
525 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
527 Status
= EFI_SUCCESS
;
529 LocateHiiProtocols ();
531 Languages
= GetSupportedLanguages (PackageList
);
533 LangStrings
= Languages
;
534 while (*LangStrings
!= 0) {
535 GetNextLanguage (&LangStrings
, Lang
);
537 Status
= gIfrLibHiiString
->NewString (
546 if (EFI_ERROR (Status
)) {
551 gBS
->FreePool (Languages
);
558 This function try to retrieve string from String package of current language.
559 If fail, it try to retrieve string from String package of first language it support.
561 @param PackageList The package list in the HII database to search for
562 the specified string.
563 @param StringId The string's id, which is unique within
565 @param String Points to the new null-terminated string.
566 @param StringSize On entry, points to the size of the buffer pointed
567 to by String, in bytes. On return, points to the
568 length of the string, in bytes.
570 @retval EFI_SUCCESS The string was returned successfully.
571 @retval EFI_NOT_FOUND The string specified by StringId is not available.
572 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringLength is too small
574 @retval EFI_INVALID_PARAMETER The String or StringSize was NULL.
579 IN EFI_HII_HANDLE PackageList
,
580 IN EFI_STRING_ID StringId
,
581 OUT EFI_STRING String
,
582 IN OUT UINTN
*StringSize
588 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
589 CHAR8 CurrentLang
[RFC_3066_ENTRY_SIZE
];
591 LocateHiiProtocols ();
593 GetCurrentLanguage (CurrentLang
);
595 Status
= gIfrLibHiiString
->GetString (
605 if (EFI_ERROR (Status
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
606 Languages
= GetSupportedLanguages (PackageList
);
607 LangStrings
= Languages
;
608 GetNextLanguage (&LangStrings
, Lang
);
609 gBS
->FreePool (Languages
);
611 Status
= gIfrLibHiiString
->GetString (
627 This function updates the string in String package of each language.
629 @param PackageList The package list containing the strings.
630 @param StringId The string's id, which is unique within
632 @param String Points to the new null-terminated string.
634 @retval EFI_SUCCESS The string was updated successfully.
635 @retval EFI_NOT_FOUND The string specified by StringId is not in the
637 @retval EFI_INVALID_PARAMETER The String was NULL.
638 @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the
644 IN EFI_HII_HANDLE PackageList
,
645 IN EFI_STRING_ID StringId
,
646 IN CONST EFI_STRING String
652 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
654 Status
= EFI_SUCCESS
;
656 LocateHiiProtocols ();
658 Languages
= GetSupportedLanguages (PackageList
);
660 LangStrings
= Languages
;
661 while (*LangStrings
!= 0) {
662 GetNextLanguage (&LangStrings
, Lang
);
664 Status
= gIfrLibHiiString
->SetString (
672 if (EFI_ERROR (Status
)) {
677 gBS
->FreePool (Languages
);