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
194 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
196 ASSERT (String
!= NULL
);
197 ASSERT (StringId
!= NULL
);
199 Status
= EFI_SUCCESS
;
201 Languages
= HiiLibGetSupportedLanguages (PackageList
);
202 ASSERT (Languages
!= NULL
);
204 LangStrings
= Languages
;
205 while (*LangStrings
!= 0) {
206 HiiLibGetNextLanguage (&LangStrings
, Lang
);
209 // For each language supported by the package,
210 // a string token is created.
212 Status
= mHiiStringProt
->NewString (
221 if (EFI_ERROR (Status
)) {
226 FreePool (Languages
);
234 This function update the specified string in String Package of each language
235 supported by the package list.
237 If String is NULL, then ASSERT.
238 If PackageList could not be found in the default HII database, then ASSERT.
239 If StringId is not found in PackageList, then ASSERT.
241 @param PackageList Handle of the package list where this string will
243 @param StringId Ths String Id to be updated.
244 @param String Points to the new null-terminated string.
246 @retval EFI_SUCCESS The new string was added successfully.
247 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
253 IN EFI_HII_HANDLE PackageList
,
254 IN EFI_STRING_ID StringId
,
255 IN CONST EFI_STRING String
261 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
263 ASSERT (IsHiiHandleRegistered (PackageList
));
265 Status
= EFI_SUCCESS
;
267 Languages
= HiiLibGetSupportedLanguages (PackageList
);
268 ASSERT (Languages
!= NULL
);
270 LangStrings
= Languages
;
271 while (*LangStrings
!= 0) {
272 HiiLibGetNextLanguage (&LangStrings
, Lang
);
275 // For each language supported by the package,
276 // the string is updated.
278 Status
= mHiiStringProt
->SetString (
286 if (EFI_ERROR (Status
)) {
291 FreePool (Languages
);
298 Get the string given the StringId and String package Producer's Guid. The caller
299 is responsible to free the *String.
301 If PackageList with the matching ProducerGuid is not found, then ASSERT.
302 If PackageList with the matching ProducerGuid is found but no String is
303 specified by StringId is found, then ASSERT.
305 @param ProducerGuid The Guid of String package list.
306 @param StringId The String ID.
307 @param String The output string.
309 @retval EFI_SUCCESS Operation is successful.
310 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
315 HiiLibGetStringFromToken (
316 IN EFI_GUID
*ProducerGuid
,
317 IN EFI_STRING_ID StringId
,
318 OUT EFI_STRING
*String
323 UINTN HandleBufferLen
;
324 EFI_HII_HANDLE
*HiiHandleBuffer
;
327 Status
= HiiLibGetHiiHandles (&HandleBufferLen
, &HiiHandleBuffer
);
328 if (HiiHandleBuffer
== NULL
) {
329 return EFI_NOT_FOUND
;
331 for (Index
= 0; Index
< (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
)); Index
++) {
332 Status
= HiiLibExtractGuidFromHiiHandle (HiiHandleBuffer
[Index
], &Guid
);
333 if (EFI_ERROR(Status
)) {
336 if (CompareGuid (&Guid
, ProducerGuid
)) {
341 if (Index
>= (HandleBufferLen
/ sizeof (EFI_HII_HANDLE
))) {
343 // If PackageList with the matching ProducerGuid is not found, then ASSERT.
346 Status
= EFI_NOT_FOUND
;
350 Status
= HiiLibGetStringFromHandle (HiiHandleBuffer
[Index
], StringId
, String
);
353 FreePool (HiiHandleBuffer
);
359 This function try to retrieve string from String package of current language.
360 If fails, it try to retrieve string from String package of first language it support.
362 If StringSize is NULL, then ASSERT.
363 If String is NULL and *StringSize is not 0, then ASSERT.
364 If PackageList could not be found in the default HII database, then ASSERT.
365 If StringId is not found in PackageList, then ASSERT.
367 @param PackageList The package list in the HII database to search for
368 the specified string.
369 @param StringId The string's id, which is unique within
371 @param String Points to the new null-terminated string.
372 @param StringSize On entry, points to the size of the buffer pointed
373 to by String, in bytes. On return, points to the
374 length of the string, in bytes.
376 @retval EFI_SUCCESS The string was returned successfully.
377 @retval EFI_NOT_FOUND The string specified by StringId is not available.
378 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringLength is too small
385 IN EFI_HII_HANDLE PackageList
,
386 IN EFI_STRING_ID StringId
,
387 OUT EFI_STRING String
,
388 IN OUT UINTN
*StringSize
394 CHAR8 Lang
[RFC_3066_ENTRY_SIZE
];
395 CHAR8 CurrentLang
[RFC_3066_ENTRY_SIZE
];
397 ASSERT (StringSize
!= NULL
);
398 ASSERT (!(*StringSize
!= 0 && String
== NULL
));
399 ASSERT (IsHiiHandleRegistered (PackageList
));
401 GetCurrentLanguage (CurrentLang
);
403 Status
= mHiiStringProt
->GetString (
413 if (EFI_ERROR (Status
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
414 Languages
= HiiLibGetSupportedLanguages (PackageList
);
415 ASSERT (Languages
!= NULL
);
417 LangStrings
= Languages
;
418 HiiLibGetNextLanguage (&LangStrings
, Lang
);
419 FreePool (Languages
);
421 Status
= mHiiStringProt
->GetString (
437 Get string specified by StringId form the HiiHandle. The caller
438 is responsible to free the *String.
440 If String is NULL, then ASSERT.
441 If HiiHandle could not be found in the default HII database, then ASSERT.
442 If StringId is not found in PackageList, then ASSERT.
444 @param HiiHandle The HII handle of package list.
445 @param StringId The String ID.
446 @param String The output string.
448 @retval EFI_NOT_FOUND String is not found.
449 @retval EFI_SUCCESS Operation is successful.
450 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
455 HiiLibGetStringFromHandle (
456 IN EFI_HII_HANDLE HiiHandle
,
457 IN EFI_STRING_ID StringId
,
458 OUT EFI_STRING
*String
464 ASSERT (String
!= NULL
);
466 StringSize
= HII_LIB_DEFAULT_STRING_SIZE
;
467 *String
= AllocateZeroPool (StringSize
);
468 if (*String
== NULL
) {
469 return EFI_OUT_OF_RESOURCES
;
472 Status
= HiiLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
473 if (Status
== EFI_BUFFER_TOO_SMALL
) {
475 *String
= AllocateZeroPool (StringSize
);
476 if (*String
== NULL
) {
477 return EFI_OUT_OF_RESOURCES
;
479 Status
= HiiLibGetString (HiiHandle
, StringId
, *String
, &StringSize
);
488 Convert language code from RFC3066 to ISO639-2.
490 @param LanguageRfc3066 RFC3066 language code.
491 @param LanguageIso639 ISO639-2 language code.
493 @retval EFI_SUCCESS Language code converted.
494 @retval EFI_NOT_FOUND Language code not found.
499 ConvertRfc3066LanguageToIso639Language (
500 IN CHAR8
*LanguageRfc3066
,
501 OUT CHAR8
*LanguageIso639
506 if ((LanguageRfc3066
[2] != '-') && (LanguageRfc3066
[2] != 0)) {
507 CopyMem (LanguageIso639
, LanguageRfc3066
, 3);
511 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
512 if (CompareMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2) == 0) {
513 CopyMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3);
518 return EFI_NOT_FOUND
;
523 Convert language code from ISO639-2 to RFC3066.
525 LanguageIso639 contain a single ISO639-2 code such as
528 The LanguageRfc3066 must be a buffer large enough
529 for RFC_3066_ENTRY_SIZE characters.
531 If LanguageIso639 is NULL, then ASSERT.
532 If LanguageRfc3066 is NULL, then ASSERT.
534 @param LanguageIso639 ISO639-2 language code.
535 @param LanguageRfc3066 RFC3066 language code.
537 @retval EFI_SUCCESS Language code converted.
538 @retval EFI_NOT_FOUND Language code not found.
543 ConvertIso639LanguageToRfc3066Language (
544 IN CONST CHAR8
*LanguageIso639
,
545 OUT CHAR8
*LanguageRfc3066
550 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
551 if (CompareMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3) == 0) {
552 CopyMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2);
557 return EFI_NOT_FOUND
;
561 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
562 be converted to "engfra".
564 @param SupportedLanguages The RFC3066 language list.
566 @return The ISO639-2 language list.
572 CHAR8
*SupportedLanguages
578 CHAR8 LangRfc3066
[RFC_3066_ENTRY_SIZE
];
579 CHAR8 LangIso639
[ISO_639_2_ENTRY_SIZE
];
582 ReturnValue
= AllocateZeroPool (AsciiStrSize (SupportedLanguages
));
583 if (ReturnValue
== NULL
) {
587 Languages
= ReturnValue
;
588 LangCodes
= SupportedLanguages
;
589 while (*LangCodes
!= 0) {
590 HiiLibGetNextLanguage (&LangCodes
, LangRfc3066
);
592 Status
= ConvertRfc3066LanguageToIso639Language (LangRfc3066
, LangIso639
);
593 if (!EFI_ERROR (Status
)) {
594 CopyMem (Languages
, LangIso639
, 3);
595 Languages
= Languages
+ 3;