4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #define ISO_639_2_ENTRY_SIZE 3
13 Check if lang is in supported language codes according to language string.
15 This code is used to check if lang is in in supported language codes. It can handle
16 RFC4646 and ISO639 language tags.
17 In ISO639 language tags, take 3-characters as a delimitation to find matched string.
18 In RFC4646 language tags, take semicolon as a delimitation to find matched string.
21 SupportedLang = "engfraengfra"
23 Lang = "eng", the return value is "TRUE", or
24 Lang = "chs", the return value is "FALSE".
26 SupportedLang = "en;fr;en-US;fr-FR"
27 Iso639Language = FALSE
28 Lang = "en", the return value is "TRUE", or
29 Lang = "zh", the return value is "FALSE".
31 @param SupportedLang Platform supported language codes.
32 @param Lang Configured language.
33 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
35 @retval TRUE lang is in supported language codes.
36 @retval FALSE lang is not in supported language codes.
40 IsLangInSupportedLangCodes(
41 IN CHAR8
*SupportedLang
,
43 IN BOOLEAN Iso639Language
51 CompareLength
= ISO_639_2_ENTRY_SIZE
;
52 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
53 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
55 // Successfully find the Lang string in SupportedLang string.
63 // Compare RFC4646 language code
65 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
67 for (; *SupportedLang
!= '\0'; SupportedLang
+= CompareLength
) {
69 // Skip ';' characters in SupportedLang
71 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
73 // Determine the length of the next language code in SupportedLang
75 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
77 if ((CompareLength
== LanguageLength
) &&
78 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
80 // Successfully find the Lang string in SupportedLang string.
90 Initialize Lang or PlatformLang variable, if Lang or PlatformLang variable is not found,
91 or it has been set to an unsupported value(not one of platform supported language codes),
92 set the default language code to it.
94 @param LangName Language name, L"Lang" or L"PlatformLang".
95 @param SupportedLang Platform supported language codes.
96 @param DefaultLang Default language code.
97 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646,
98 TRUE for L"Lang" LangName or FALSE for L"PlatformLang" LangName.
102 InitializeLangVariable (
104 IN CHAR8
*SupportedLang
,
105 IN CHAR8
*DefaultLang
,
106 IN BOOLEAN Iso639Language
112 // Find current Lang or PlatformLang from EFI Variable.
114 GetEfiGlobalVariable2 (LangName
, (VOID
**) &Lang
, NULL
);
117 // If Lang or PlatformLang variable is not found,
118 // or it has been set to an unsupported value(not one of the supported language codes),
119 // set the default language code to it.
121 if ((Lang
== NULL
) || !IsLangInSupportedLangCodes (SupportedLang
, Lang
, Iso639Language
)) {
123 // The default language code should be one of the supported language codes.
125 ASSERT (IsLangInSupportedLangCodes (SupportedLang
, DefaultLang
, Iso639Language
));
126 BdsDxeSetVariableAndReportStatusCodeOnError (
128 &gEfiGlobalVariableGuid
,
129 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
130 AsciiStrSize (DefaultLang
),
141 Determine the current language that will be used
142 based on language related EFI Variables.
144 @param LangCodesSettingRequired - If required to set LangCodes variable
149 BOOLEAN LangCodesSettingRequired
154 CHAR8
*PlatformLangCodes
;
156 LangCodes
= (CHAR8
*)PcdGetPtr (PcdUefiVariableDefaultLangCodes
);
157 PlatformLangCodes
= (CHAR8
*)PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes
);
158 if (LangCodesSettingRequired
) {
159 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
161 // UEFI 2.1 depricated this variable so we support turning it off
163 Status
= gRT
->SetVariable (
165 &gEfiGlobalVariableGuid
,
166 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
167 AsciiStrSize (LangCodes
),
171 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
173 ASSERT_EFI_ERROR(Status
);
176 Status
= gRT
->SetVariable (
177 L
"PlatformLangCodes",
178 &gEfiGlobalVariableGuid
,
179 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
180 AsciiStrSize (PlatformLangCodes
),
184 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
186 ASSERT_EFI_ERROR(Status
);
189 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
191 // UEFI 2.1 depricated this variable so we support turning it off
193 InitializeLangVariable (L
"Lang", LangCodes
, (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultLang
), TRUE
);
195 InitializeLangVariable (L
"PlatformLang", PlatformLangCodes
, (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLang
), FALSE
);