2 This file implements the protocol functions related to string package.
4 Copyright (c) 2006 - 2008, Intel Corporation
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 "HiiDatabase.h"
23 ISO639TORFC3066MAP Iso639ToRfc3066Map
[] = {
29 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
30 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
31 // The last 2 CHAR8 values are the ISO 639-1 code.
33 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 Iso639ToRfc3066ConversionTable
[] =
173 ConvertIso639ToRfc3066 (
178 CHAR8 AsciiLanguage
[ISO_639_2_ENTRY_SIZE
+ 1];
180 AsciiStrnCpy (AsciiLanguage
, Iso638Lang
, sizeof (AsciiLanguage
));
181 for (Index
= 0; Index
< ISO_639_2_ENTRY_SIZE
+ 1; Index
++) {
182 if (AsciiLanguage
[Index
] == 0) {
184 } else if (AsciiLanguage
[Index
] >= 'A' && AsciiLanguage
[Index
] <= 'Z') {
185 AsciiLanguage
[Index
] = (CHAR8
) (AsciiLanguage
[Index
] - 'A' + 'a');
189 for (Index
= 0; Index
< sizeof (Iso639ToRfc3066Map
) / sizeof (Iso639ToRfc3066Map
[0]); Index
++) {
190 if (AsciiStrnCmp (AsciiLanguage
, Iso639ToRfc3066Map
[Index
].Iso639
, AsciiStrSize (AsciiLanguage
)) == 0) {
191 return Iso639ToRfc3066Map
[Index
].Rfc3066
;
195 return (CHAR8
*) NULL
;
199 Convert language code from RFC3066 to ISO639-2.
201 @param LanguageRfc3066 RFC3066 language code.
202 @param LanguageIso639 ISO639-2 language code.
204 @retval EFI_SUCCESS Language code converted.
205 @retval EFI_NOT_FOUND Language code not found.
210 ConvertRfc3066LanguageToIso639Language (
211 IN CHAR8
*LanguageRfc3066
,
212 OUT CHAR8
*LanguageIso639
217 if ((LanguageRfc3066
[2] != '-') && (LanguageRfc3066
[2] != 0)) {
218 CopyMem (LanguageIso639
, LanguageRfc3066
, 3);
222 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
223 if (CompareMem (LanguageRfc3066
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2) == 0) {
224 CopyMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3);
229 return EFI_NOT_FOUND
;
234 Convert language code from ISO639-2 to RFC3066 and return the converted language.
235 Caller is responsible for freeing the allocated buffer.
237 LanguageIso639 contain a single ISO639-2 code such as
240 If LanguageIso639 is NULL, then ASSERT.
241 If LanguageRfc3066 is NULL, then ASSERT.
243 @param LanguageIso639 ISO639-2 language code.
245 @return the allocated buffer or NULL, if the language is not found.
250 ConvertIso639LanguageToRfc3066Language (
251 IN CONST CHAR8
*LanguageIso639
255 CHAR8
*Rfc3066Language
;
257 for (Index
= 0; Iso639ToRfc3066ConversionTable
[Index
] != 0; Index
+= 5) {
258 if (CompareMem (LanguageIso639
, &Iso639ToRfc3066ConversionTable
[Index
], 3) == 0) {
259 Rfc3066Language
= AllocateZeroPool (3);
260 if (Rfc3066Language
!= NULL
) {
261 Rfc3066Language
= CopyMem (Rfc3066Language
, &Iso639ToRfc3066ConversionTable
[Index
+ 3], 2);
263 return Rfc3066Language
;
271 Get next language from language code list (with separator ';').
273 If LangCode is NULL, then ASSERT.
274 If Lang is NULL, then ASSERT.
276 @param LangCode On input: point to first language in the list. On
277 output: point to next language in the list, or
278 NULL if no more language in the list.
279 @param Lang The first language in the list.
285 IN OUT CHAR8
**LangCode
,
292 ASSERT (LangCode
!= NULL
);
293 ASSERT (*LangCode
!= NULL
);
294 ASSERT (Lang
!= NULL
);
297 StringPtr
= *LangCode
;
298 while (StringPtr
[Index
] != 0 && StringPtr
[Index
] != ';') {
302 CopyMem (Lang
, StringPtr
, Index
);
305 if (StringPtr
[Index
] == ';') {
308 *LangCode
= StringPtr
+ Index
;
312 Test if all of the characters in a string have corresponding font characters.
314 This is a deprecated API. No Framework HII module is calling it. This function will ASSERT and
315 return EFI_UNSUPPORTED.
317 @param This A pointer to the EFI_HII_PROTOCOL instance.
318 @param StringToTest A pointer to a Unicode string.
319 @param FirstMissing A pointer to an index into the string. On input, the index of
320 the first character in the StringToTest to examine. On exit, the index
321 of the first character encountered for which a glyph is unavailable.
322 If all glyphs in the string are available, the index is the index of the terminator
324 @param GlyphBufferSize A pointer to a value. On output, if the function returns EFI_SUCCESS,
325 it contains the amount of memory that is required to store the string? glyph equivalent.
327 @retval EFI_UNSUPPORTED The function performs nothing and return EFI_UNSUPPORTED.
332 IN EFI_HII_PROTOCOL
*This
,
333 IN CHAR16
*StringToTest
,
334 IN OUT UINT32
*FirstMissing
,
335 OUT UINT32
*GlyphBufferSize
340 return EFI_UNSUPPORTED
;
345 Find the corressponding TAG GUID from a Framework HII Handle given.
347 @param Private The HII Thunk Module Private context.
348 @param FwHiiHandle The Framemwork HII Handle.
349 @param TagGuid The output of TAG GUID found.
351 @return NULL If Framework HII Handle is invalid.
352 @return The corresponding HII Thunk Context.
355 GetTagGuidByFwHiiHandle (
356 IN CONST HII_THUNK_PRIVATE_DATA
*Private
,
357 IN FRAMEWORK_EFI_HII_HANDLE FwHiiHandle
,
358 OUT EFI_GUID
*TagGuid
362 HII_THUNK_CONTEXT
*ThunkContext
;
364 ASSERT (TagGuid
!= NULL
);
366 Link
= GetFirstNode (&Private
->ThunkContextListHead
);
367 while (!IsNull (&Private
->ThunkContextListHead
, Link
)) {
369 ThunkContext
= HII_THUNK_CONTEXT_FROM_LINK (Link
);
371 if (FwHiiHandle
== ThunkContext
->FwHiiHandle
) {
372 CopyGuid (TagGuid
, &ThunkContext
->TagGuid
);
376 Link
= GetNextNode (&Private
->ThunkContextListHead
, Link
);
379 return EFI_NOT_FOUND
;
383 Create or update the String given a new string and String ID.
385 @param ThunkContext The Thunk Context.
386 @param Rfc3066AsciiLanguage The RFC 3066 Language code in ASCII string format.
387 @param NewString The new string.
388 @param StringId The String ID. If StringId is 0, a new String Token
389 is created. Otherwise, the String Token StringId is
393 @retval EFI_SUCCESS The new string is created or updated successfully.
394 The new String Token ID is returned in StringId if
395 *StringId is 0 on input.
396 @return Others The update of string failed.
401 IN CONST HII_THUNK_CONTEXT
*ThunkContext
,
402 IN CONST CHAR8
*Rfc3066AsciiLanguage
,
403 IN CHAR16
*NewString
,
404 IN OUT STRING_REF
*StringId
407 EFI_STRING_ID NewStringId
;
409 NewStringId
= HiiSetString (ThunkContext
->UefiHiiHandle
, *StringId
, NewString
, Rfc3066AsciiLanguage
);
410 *StringId
= NewStringId
;
411 if (NewStringId
== 0) {
413 // Only EFI_INVALID_PARAMETER is defined in HII 0.92 specification.
415 return EFI_INVALID_PARAMETER
;
422 Create or update a String Token in a String Package.
424 If *Reference == 0, a new String Token is created.
426 @param This A pointer to the EFI_HII_PROTOCOL instance.
427 @param Language Pointer to a NULL-terminated string containing a single ISO 639-2 language
428 identifier, indicating the language to print. A string consisting of
429 all spaces indicates that the string is applicable to all languages.
430 @param Handle The handle of the language pack to which the string is to be added.
431 @param Token The string token assigned to the string.
432 @param NewString The string to be added.
435 @retval EFI_SUCCESS The string was effectively registered.
436 @retval EFI_INVALID_PARAMETER The Handle was unknown. The string is not created or updated in the
443 IN EFI_HII_PROTOCOL
*This
,
445 IN FRAMEWORK_EFI_HII_HANDLE Handle
,
446 IN OUT STRING_REF
*Reference
,
451 HII_THUNK_PRIVATE_DATA
*Private
;
454 HII_THUNK_CONTEXT
*ThunkContext
;
455 HII_THUNK_CONTEXT
*StringPackThunkContext
;
456 EFI_STRING_ID StringId
;
457 EFI_STRING_ID LastStringId
;
458 CHAR8 AsciiLanguage
[ISO_639_2_ENTRY_SIZE
+ 1];
459 CHAR16 LanguageCopy
[ISO_639_2_ENTRY_SIZE
+ 1];
460 CHAR8
*Rfc3066AsciiLanguage
;
462 LastStringId
= (EFI_STRING_ID
) 0;
463 StringId
= (EFI_STRING_ID
) 0;
464 Rfc3066AsciiLanguage
= NULL
;
466 if (Language
!= NULL
) {
467 ZeroMem (AsciiLanguage
, sizeof (AsciiLanguage
));;
468 ZeroMem (LanguageCopy
, sizeof (LanguageCopy
));
469 CopyMem (LanguageCopy
, Language
, ISO_639_2_ENTRY_SIZE
* sizeof (CHAR16
));
470 UnicodeStrToAsciiStr (LanguageCopy
, AsciiLanguage
);
471 Rfc3066AsciiLanguage
= ConvertIso639ToRfc3066 (AsciiLanguage
);
472 ASSERT (Rfc3066AsciiLanguage
!= NULL
);
475 Private
= HII_THUNK_PRIVATE_DATA_FROM_THIS(This
);
477 StringPackThunkContext
= FwHiiHandleToThunkContext (Private
, Handle
);
478 if (StringPackThunkContext
== NULL
) {
479 return EFI_INVALID_PARAMETER
;
482 if (StringPackThunkContext
->SharingStringPack
) {
483 Status
= GetTagGuidByFwHiiHandle (Private
, Handle
, &TagGuid
);
484 ASSERT_EFI_ERROR (Status
);
486 Link
= GetFirstNode (&Private
->ThunkContextListHead
);
487 while (!IsNull (&Private
->ThunkContextListHead
, Link
)) {
488 ThunkContext
= HII_THUNK_CONTEXT_FROM_LINK (Link
);
490 if (CompareGuid (&TagGuid
, &ThunkContext
->TagGuid
)) {
491 if (ThunkContext
->SharingStringPack
) {
492 StringId
= *Reference
;
493 Status
= UpdateString (ThunkContext
, Rfc3066AsciiLanguage
, NewString
, &StringId
);
494 if (EFI_ERROR (Status
)) {
499 if (*Reference
== 0) {
501 // When creating new string token, make sure all created token is the same
502 // for all string packages registered using FW HII interface.
504 if (LastStringId
== (EFI_STRING_ID
) 0) {
505 LastStringId
= StringId
;
507 if (LastStringId
!= StringId
) {
517 Link
= GetNextNode (&Private
->ThunkContextListHead
, Link
);
520 StringId
= *Reference
;
521 Status
= UpdateString (StringPackThunkContext
, Rfc3066AsciiLanguage
, NewString
, &StringId
);
524 if (!EFI_ERROR (Status
)) {
525 if (*Reference
== 0) {
526 *Reference
= StringId
;
530 // Only EFI_INVALID_PARAMETER is defined in HII 0.92 specification.
532 Status
= EFI_INVALID_PARAMETER
;
539 This function removes any new strings that were added after the initial string export for this handle.
540 UEFI HII String Protocol does not have Reset String function. This function perform nothing.
542 @param This A pointer to the EFI_HII_PROTOCOL instance.
543 @param Handle The HII handle on which the string resides.
545 @retval EFI_SUCCESS This function is a NOP and always return EFI_SUCCESS.
551 IN EFI_HII_PROTOCOL
*This
,
552 IN FRAMEWORK_EFI_HII_HANDLE Handle
559 This function extracts a string from a package already registered with the EFI HII database.
561 @param This A pointer to the EFI_HII_PROTOCOL instance.
562 @param Handle The HII handle on which the string resides.
563 @param Token The string token assigned to the string.
564 @param Raw If TRUE, the string is returned unedited in the internal storage format described
565 above. If false, the string returned is edited by replacing <cr> with <space>
566 and by removing special characters such as the <wide> prefix.
567 @param LanguageString Pointer to a NULL-terminated string containing a single ISO 639-2 language
568 identifier, indicating the language to print. If the LanguageString is empty (starts
569 with a NULL), the default system language will be used to determine the language.
570 @param BufferLength Length of the StringBuffer. If the status reports that the buffer width is too
571 small, this parameter is filled with the length of the buffer needed.
572 @param StringBuffer The buffer designed to receive the characters in the string. Type EFI_STRING is
575 @retval EFI_INVALID_PARAMETER If input parameter is invalid.
576 @retval EFI_BUFFER_TOO_SMALL If the *BufferLength is too small.
577 @retval EFI_SUCCESS Operation is successful.
583 IN EFI_HII_PROTOCOL
*This
,
584 IN FRAMEWORK_EFI_HII_HANDLE Handle
,
587 IN CHAR16
*LanguageString
,
588 IN OUT UINTN
*BufferLengthTemp
,
589 OUT EFI_STRING StringBuffer
592 HII_THUNK_PRIVATE_DATA
*Private
;
593 CHAR8
*Iso639AsciiLanguage
;
594 CHAR8
*Rfc3066AsciiLanguage
;
595 CHAR8
*SupportedLanguages
;
596 CHAR8
*PlatformLanguage
;
598 EFI_HII_HANDLE UefiHiiHandle
;
601 Private
= HII_THUNK_PRIVATE_DATA_FROM_THIS(This
);
603 Iso639AsciiLanguage
= NULL
;
604 Rfc3066AsciiLanguage
= NULL
;
606 if (LanguageString
!= NULL
) {
607 Iso639AsciiLanguage
= AllocateZeroPool (StrLen (LanguageString
) + 1);
608 if (Iso639AsciiLanguage
== NULL
) {
609 return EFI_OUT_OF_RESOURCES
;
611 UnicodeStrToAsciiStr (LanguageString
, Iso639AsciiLanguage
);
614 // Caller of Framework HII Interface uses the Language Identification String defined
615 // in Iso639. So map it to the Language Identifier defined in RFC3066.
617 Rfc3066AsciiLanguage
= ConvertIso639ToRfc3066 (Iso639AsciiLanguage
);
620 // If Rfc3066AsciiLanguage is NULL, more language mapping must be added to
621 // Iso639ToRfc3066Map.
623 ASSERT (Rfc3066AsciiLanguage
!= NULL
);
627 UefiHiiHandle
= FwHiiHandleToUefiHiiHandle (Private
, Handle
);
628 if (UefiHiiHandle
== NULL
) {
629 Status
= EFI_NOT_FOUND
;
633 if (Rfc3066AsciiLanguage
== NULL
) {
635 // Get the languages that the package specified by HiiHandle supports
637 SupportedLanguages
= HiiGetSupportedLanguages (UefiHiiHandle
);
638 if (SupportedLanguages
== NULL
) {
643 // Get the current platform language setting
645 PlatformLanguage
= GetEfiGlobalVariable (L
"PlatformLang");
646 if (PlatformLanguage
== NULL
) {
651 // Get the best matching language from SupportedLanguages
653 BestLanguage
= GetBestLanguage (
655 FALSE
, // RFC 4646 mode
656 PlatformLanguage
, // Next highest priority
657 SupportedLanguages
, // Lowest priority
660 if (BestLanguage
== NULL
) {
661 FreePool (PlatformLanguage
);
663 FreePool (SupportedLanguages
);
665 Status
= EFI_INVALID_PARAMETER
;
669 Status
= mHiiStringProtocol
->GetString (
678 FreePool (BestLanguage
);
680 Status
= mHiiStringProtocol
->GetString (
682 Rfc3066AsciiLanguage
,
692 if (Iso639AsciiLanguage
!= NULL
) {
693 FreePool (Iso639AsciiLanguage
);
701 This function allows a program to extract a part of a string of not more than a given width.
702 With repeated calls, this allows a calling program to extract "lines" of text that fit inside
703 columns. The effort of measuring the fit of strings inside columns is localized to this call.
705 This is a deprecated API. No Framework HII module is calling it. This function will ASSERT and
706 return EFI_UNSUPPORTED.
708 @param This A pointer to the EFI_HII_PROTOCOL instance.
709 @param Handle The HII handle on which the string resides.
710 @param Token The string token assigned to the string.
711 @param Raw If TRUE, the string is returned unedited in the internal storage format described
712 above. If false, the string returned is edited by replacing <cr> with <space>
713 and by removing special characters such as the <wide> prefix.
714 @param LanguageString Pointer to a NULL-terminated string containing a single ISO 639-2 language
715 identifier, indicating the language to print. If the LanguageString is empty (starts
716 with a NULL), the default system language will be used to determine the language.
717 @param BufferLength Length of the StringBuffer. If the status reports that the buffer width is too
718 small, this parameter is filled with the length of the buffer needed.
719 @param StringBuffer The buffer designed to receive the characters in the string. Type EFI_STRING is
722 @retval EFI_UNSUPPORTED.
727 IN EFI_HII_PROTOCOL
*This
,
728 IN FRAMEWORK_EFI_HII_HANDLE Handle
,
730 IN OUT UINT16
*Index
,
732 IN CHAR16
*LanguageString
,
733 IN OUT UINT16
*BufferLength
,
734 OUT EFI_STRING StringBuffer
738 return EFI_UNSUPPORTED
;