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 adds the string into String Package of each language
167 supported by the package list.
169 If String is NULL, then ASSERT.
170 If StringId is NULL, the ASSERT.
171 If PackageList could not be found in the default HII database, then ASSERT.
173 @param PackageList Handle of the package list where this string will
175 @param StringId On return, contains the new strings id, which is
176 unique within PackageList.
177 @param String Points to the new null-terminated string.
179 @retval EFI_SUCCESS The new string was added successfully.
180 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
186 IN EFI_HII_HANDLE PackageList
,
187 OUT EFI_STRING_ID
*StringId
,
188 IN CONST EFI_STRING String
196 ASSERT (String
!= NULL
);
197 ASSERT (StringId
!= NULL
);
199 Status
= EFI_SUCCESS
;
201 Languages
= HiiLibGetSupportedLanguages (PackageList
);
202 ASSERT (Languages
!= NULL
);
204 // Allocate working buffer to contain substring of Languages.
206 Lang
= AllocatePool (AsciiStrSize (Languages
));
207 ASSERT (Lang
!= NULL
);
209 LangStrings
= Languages
;
210 while (*LangStrings
!= 0) {
211 HiiLibGetNextLanguage (&LangStrings
, Lang
);
214 // For each language supported by the package,
215 // a string token is created.
217 Status
= mHiiStringProt
->NewString (
226 if (EFI_ERROR (Status
)) {
232 FreePool (Languages
);
240 This function update the specified string in String Package of each language
241 supported by the package list.
243 If String is NULL, then ASSERT.
244 If PackageList could not be found in the default HII database, then ASSERT.
245 If StringId is not found in PackageList, then ASSERT.
247 @param PackageList Handle of the package list where this string will
249 @param StringId Ths String Id to be updated.
250 @param String Points to the new null-terminated string.
252 @retval EFI_SUCCESS The new string was added successfully.
253 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
259 IN EFI_HII_HANDLE PackageList
,
260 IN EFI_STRING_ID StringId
,
261 IN CONST EFI_STRING String
269 ASSERT (IsHiiHandleRegistered (PackageList
));
271 Status
= EFI_SUCCESS
;
273 Languages
= HiiLibGetSupportedLanguages (PackageList
);
274 ASSERT (Languages
!= NULL
);
277 // Allocate working buffer to contain substring of Languages.
279 Lang
= AllocatePool (AsciiStrSize (Languages
));
280 ASSERT (Lang
!= NULL
);
282 LangStrings
= Languages
;
283 while (*LangStrings
!= 0) {
284 HiiLibGetNextLanguage (&LangStrings
, Lang
);
287 // For each language supported by the package,
288 // the string is updated.
290 Status
= mHiiStringProt
->SetString (
298 if (EFI_ERROR (Status
)) {
304 FreePool (Languages
);
311 Get the string given the StringId and String package Producer's Guid. The caller
312 is responsible to free the *String.
314 If PackageList with the matching ProducerGuid is not found, then ASSERT.
315 If PackageList with the matching ProducerGuid is found but no String is
316 specified by StringId is found, then ASSERT.
318 @param ProducerGuid The Guid of String package list.
319 @param StringId The String ID.
320 @param String The output string.
322 @retval EFI_SUCCESS Operation is successful.
323 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
328 HiiLibGetStringFromToken (
329 IN EFI_GUID
*ProducerGuid
,
330 IN EFI_STRING_ID StringId
,
331 OUT EFI_STRING
*String
336 UINTN HandleBufferLen
;
337 EFI_HII_HANDLE
*HiiHandleBuffer
;
340 Status
= HiiLibGetHiiHandles (&HandleBufferLen
, &HiiHandleBuffer
);
341 if (HiiHandleBuffer
== NULL
) {
342 return EFI_NOT_FOUND
;
344 for (Index
= 0; Index
< (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
)); Index
++) {
345 Status
= HiiLibExtractGuidFromHiiHandle (HiiHandleBuffer
[Index
], &Guid
);
346 if (EFI_ERROR(Status
)) {
349 if (CompareGuid (&Guid
, ProducerGuid
)) {
354 if (Index
>= (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
))) {
356 // If PackageList with the matching ProducerGuid is not found, then ASSERT.
359 Status
= EFI_NOT_FOUND
;
363 Status
= HiiLibGetStringFromHandle (HiiHandleBuffer
[Index
], StringId
, String
);
366 FreePool (HiiHandleBuffer
);
372 This function try to retrieve string from String package of current language.
373 If fails, it try to retrieve string from String package of first language it support.
375 If StringSize is NULL, then ASSERT.
376 If String is NULL and *StringSize is not 0, then ASSERT.
377 If PackageList could not be found in the default HII database, then ASSERT.
378 If StringId is not found in PackageList, then ASSERT.
380 @param PackageList The package list in the HII database to search for
381 the specified string.
382 @param StringId The string's id, which is unique within
384 @param String Points to the new null-terminated string.
385 @param StringSize On entry, points to the size of the buffer pointed
386 to by String, in bytes. On return, points to the
387 length of the string, in bytes.
389 @retval EFI_SUCCESS The string was returned successfully.
390 @retval EFI_NOT_FOUND The string specified by StringId is not available.
391 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringLength is too small
398 IN EFI_HII_HANDLE PackageList
,
399 IN EFI_STRING_ID StringId
,
400 OUT EFI_STRING String
,
401 IN OUT UINTN
*StringSize
409 ASSERT (StringSize
!= NULL
);
410 ASSERT (!(*StringSize
!= 0 && String
== NULL
));
411 ASSERT (IsHiiHandleRegistered (PackageList
));
413 Languages
= HiiLibGetSupportedLanguages (PackageList
);
414 ASSERT (Languages
!= NULL
);
416 CurrentLang
= GetEfiGlobalVariable (L
"PlatformLang");
418 Status
= EFI_NOT_FOUND
;
419 BestLanguage
= GetBestLanguage (
422 (CurrentLang
!= NULL
) ? CurrentLang
: "",
423 (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLang
),
427 if (BestLanguage
!= NULL
) {
428 Status
= mHiiStringProt
->GetString (
437 FreePool (BestLanguage
);
439 if (CurrentLang
!= NULL
) {
440 FreePool (CurrentLang
);
442 FreePool (Languages
);
449 Get string specified by StringId form the HiiHandle. The caller
450 is responsible to free the *String.
452 If String is NULL, then ASSERT.
453 If HiiHandle could not be found in the default HII database, then ASSERT.
454 If StringId is not found in PackageList, then ASSERT.
456 @param HiiHandle The HII handle of package list.
457 @param StringId The String ID.
458 @param String The output string.
460 @retval EFI_NOT_FOUND String is not found.
461 @retval EFI_SUCCESS Operation is successful.
462 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
467 HiiLibGetStringFromHandle (
468 IN EFI_HII_HANDLE HiiHandle
,
469 IN EFI_STRING_ID StringId
,
470 OUT EFI_STRING
*String
476 ASSERT (String
!= NULL
);
478 StringSize
= HII_LIB_DEFAULT_STRING_SIZE
;
479 *String
= AllocateZeroPool (StringSize
);
480 if (*String
== NULL
) {
481 return EFI_OUT_OF_RESOURCES
;
484 Status
= HiiLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
485 if (Status
== EFI_BUFFER_TOO_SMALL
) {
487 *String
= AllocateZeroPool (StringSize
);
488 if (*String
== NULL
) {
489 return EFI_OUT_OF_RESOURCES
;
491 Status
= HiiLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
500 Convert language code from RFC3066 to ISO639-2.
502 @param LanguageRfc3066 RFC3066 language code.
503 @param LanguageIso639 ISO639-2 language code.
505 @retval EFI_SUCCESS Language code converted.
506 @retval EFI_NOT_FOUND Language code not found.
511 ConvertRfc3066LanguageToIso639Language (
512 IN CHAR8
*LanguageRfc3066
,
513 OUT CHAR8
*LanguageIso639
518 if ((LanguageRfc3066
[2] != '-') && (LanguageRfc3066
[2] != 0)) {
519 CopyMem (LanguageIso639
, LanguageRfc3066
, 3);
523 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
524 if (CompareMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2) == 0) {
525 CopyMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3);
530 return EFI_NOT_FOUND
;
535 Convert language code from ISO639-2 to RFC3066 and return the converted language.
536 Caller is responsible for freeing the allocated buffer.
538 LanguageIso639 contain a single ISO639-2 code such as
541 If LanguageIso639 is NULL, then ASSERT.
542 If LanguageRfc3066 is NULL, then ASSERT.
544 @param LanguageIso639 ISO639-2 language code.
546 @return the allocated buffer or NULL, if the language is not found.
551 ConvertIso639LanguageToRfc3066Language (
552 IN CONST CHAR8
*LanguageIso639
556 CHAR8
*Rfc3066Language
;
558 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
559 if (CompareMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3) == 0) {
560 Rfc3066Language
= AllocateZeroPool (3);
561 if (Rfc3066Language
!= NULL
) {
562 Rfc3066Language
= CopyMem (Rfc3066Language
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2);
564 return Rfc3066Language
;
572 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
573 be converted to "engfra".
575 @param SupportedLanguages The RFC3066 language list.
577 @return The ISO639-2 language list.
583 CHAR8
*SupportedLanguages
590 CHAR8 LangIso639
[ISO_639_2_ENTRY_SIZE
];
594 LanguageSize
= AsciiStrSize (SupportedLanguages
);
595 ReturnValue
= AllocateZeroPool (LanguageSize
);
596 if (ReturnValue
== NULL
) {
601 // Allocate working buffer to contain substring in SupportedLanguages;
603 LangRfc3066
= AllocatePool (LanguageSize
);
604 if (LangRfc3066
== NULL
) {
605 FreePool (ReturnValue
);
608 Languages
= ReturnValue
;
609 LangCodes
= SupportedLanguages
;
610 while (*LangCodes
!= 0) {
611 HiiLibGetNextLanguage (&LangCodes
, LangRfc3066
);
613 Status
= ConvertRfc3066LanguageToIso639Language (LangRfc3066
, LangIso639
);
614 if (!EFI_ERROR (Status
)) {
615 CopyMem (Languages
, LangIso639
, 3);
616 Languages
= Languages
+ 3;
620 FreePool (LangRfc3066
);