]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/SmBiosMiscDxe/MiscNumberOfInstallableLanguagesFunction.c
Upload BSD-licensed Vlv2TbltDevicePkg and Vlv2DeviceRefCodePkg to
[mirror_edk2.git] / Vlv2TbltDevicePkg / SmBiosMiscDxe / MiscNumberOfInstallableLanguagesFunction.c
1 /*++
2
3 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
4
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
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.
12
13
14
15 Module Name:
16
17 MiscNumberOfInstallableLanguagesFunction.c
18
19 Abstract:
20
21 This driver parses the mSmbiosMiscDataTable structure and reports
22 any generated data.
23
24 --*/
25
26
27 #include "CommonHeader.h"
28
29 #include "MiscSubclassDriver.h"
30
31 /**
32 Check whether the language is supported for given HII handle
33
34 @param HiiHandle The HII package list handle.
35 @param Offset The offest of current lanague in the supported languages.
36 @param CurrentLang The language code.
37
38 @retval TRUE Supported.
39 @retval FALSE Not Supported.
40
41 **/
42 VOID
43 EFIAPI
44 CurrentLanguageMatch (
45 IN EFI_HII_HANDLE HiiHandle,
46 OUT UINT16 *Offset,
47 OUT CHAR8 *CurrentLang
48 )
49 {
50 CHAR8 *DefaultLang;
51 CHAR8 *BestLanguage;
52 CHAR8 *Languages;
53 CHAR8 *MatchLang;
54 CHAR8 *EndMatchLang;
55 UINTN CompareLength;
56
57 Languages = HiiGetSupportedLanguages (HiiHandle);
58 if (Languages == NULL) {
59 return;
60 }
61
62 CurrentLang = GetEfiGlobalVariable (L"PlatformLang");
63 DefaultLang = (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang);
64 BestLanguage = GetBestLanguage (
65 Languages,
66 FALSE,
67 (CurrentLang != NULL) ? CurrentLang : "",
68 DefaultLang,
69 NULL
70 );
71 if (BestLanguage != NULL) {
72 //
73 // Find the best matching RFC 4646 language, compute the offset.
74 //
75 CompareLength = AsciiStrLen (BestLanguage);
76 for (MatchLang = Languages, (*Offset) = 0; MatchLang != '\0'; (*Offset)++) {
77 //
78 // Seek to the end of current match language.
79 //
80 for (EndMatchLang = MatchLang; *EndMatchLang != '\0' && *EndMatchLang != ';'; EndMatchLang++);
81
82 if ((EndMatchLang == MatchLang + CompareLength) && AsciiStrnCmp(MatchLang, BestLanguage, CompareLength) == 0) {
83 //
84 // Find the current best Language in the supported languages
85 //
86 break;
87 }
88 //
89 // best language match be in the supported language.
90 //
91 ASSERT (*EndMatchLang == ';');
92 MatchLang = EndMatchLang + 1;
93 }
94 FreePool (BestLanguage);
95 }
96
97 FreePool (Languages);
98 if (CurrentLang != NULL) {
99 FreePool (CurrentLang);
100 }
101 return ;
102 }
103
104
105 /**
106 Get next language from language code list (with separator ';').
107
108 @param LangCode Input: point to first language in the list. On
109 Otput: point to next language in the list, or
110 NULL if no more language in the list.
111 @param Lang The first language in the list.
112
113 **/
114 VOID
115 EFIAPI
116 GetNextLanguage (
117 IN OUT CHAR8 **LangCode,
118 OUT CHAR8 *Lang
119 )
120 {
121 UINTN Index;
122 CHAR8 *StringPtr;
123
124 ASSERT (LangCode != NULL);
125 ASSERT (*LangCode != NULL);
126 ASSERT (Lang != NULL);
127
128 Index = 0;
129 StringPtr = *LangCode;
130 while (StringPtr[Index] != 0 && StringPtr[Index] != ';') {
131 Index++;
132 }
133
134 CopyMem (Lang, StringPtr, Index);
135 Lang[Index] = 0;
136
137 if (StringPtr[Index] == ';') {
138 Index++;
139 }
140 *LangCode = StringPtr + Index;
141 }
142
143 /**
144 This function returns the number of supported languages on HiiHandle.
145
146 @param HiiHandle The HII package list handle.
147
148 @retval The number of supported languages.
149
150 **/
151 UINT16
152 EFIAPI
153 GetSupportedLanguageNumber (
154 IN EFI_HII_HANDLE HiiHandle
155 )
156 {
157 CHAR8 *Lang;
158 CHAR8 *Languages;
159 CHAR8 *LanguageString;
160 UINT16 LangNumber;
161
162 Languages = HiiGetSupportedLanguages (HiiHandle);
163 if (Languages == NULL) {
164 return 0;
165 }
166
167 LangNumber = 0;
168 Lang = AllocatePool (AsciiStrSize (Languages));
169 if (Lang != NULL) {
170 LanguageString = Languages;
171 while (*LanguageString != 0) {
172 GetNextLanguage (&LanguageString, Lang);
173 LangNumber++;
174 }
175 FreePool (Lang);
176 }
177 FreePool (Languages);
178 return LangNumber;
179 }
180
181
182 /**
183 This function makes boot time changes to the contents of the
184 MiscNumberOfInstallableLanguages (Type 13).
185
186 @param RecordData Pointer to copy of RecordData from the Data Table.
187
188 @retval EFI_SUCCESS All parameters were valid.
189 @retval EFI_UNSUPPORTED Unexpected RecordType value.
190 @retval EFI_INVALID_PARAMETER Invalid parameter was found.
191
192 **/
193 MISC_SMBIOS_TABLE_FUNCTION(NumberOfInstallableLanguages)
194 {
195 UINTN LangStrLen;
196 CHAR8 CurrentLang[SMBIOS_STRING_MAX_LENGTH + 1];
197 CHAR8 *OptionalStrStart;
198 UINT16 Offset;
199 EFI_STATUS Status;
200 EFI_SMBIOS_HANDLE SmbiosHandle;
201 SMBIOS_TABLE_TYPE13 *SmbiosRecord;
202 EFI_MISC_NUMBER_OF_INSTALLABLE_LANGUAGES *ForType13InputData;
203
204 ForType13InputData = (EFI_MISC_NUMBER_OF_INSTALLABLE_LANGUAGES *)RecordData;
205
206 //
207 // First check for invalid parameters.
208 //
209 if (RecordData == NULL) {
210 return EFI_INVALID_PARAMETER;
211 }
212
213 ForType13InputData->NumberOfInstallableLanguages = GetSupportedLanguageNumber (mHiiHandle);
214
215 //
216 // Try to check if current langcode matches with the langcodes in installed languages
217 //
218 ZeroMem(CurrentLang, SMBIOS_STRING_MAX_LENGTH + 1);
219 CurrentLanguageMatch (mHiiHandle, &Offset, CurrentLang);
220 LangStrLen = AsciiStrLen(CurrentLang);
221
222 //
223 // Two zeros following the last string.
224 //
225 SmbiosRecord = AllocatePool(sizeof (SMBIOS_TABLE_TYPE13) + LangStrLen + 1 + 1);
226 ZeroMem(SmbiosRecord, sizeof (SMBIOS_TABLE_TYPE13) + LangStrLen + 1 + 1);
227
228 SmbiosRecord->Hdr.Type = EFI_SMBIOS_TYPE_BIOS_LANGUAGE_INFORMATION;
229 SmbiosRecord->Hdr.Length = sizeof (SMBIOS_TABLE_TYPE13);
230
231 //
232 // Make handle chosen by smbios protocol.add automatically.
233 //
234 SmbiosRecord->Hdr.Handle = 0;
235
236 SmbiosRecord->InstallableLanguages = (UINT8)ForType13InputData->NumberOfInstallableLanguages;
237 SmbiosRecord->Flags = (UINT8)ForType13InputData->LanguageFlags.AbbreviatedLanguageFormat;
238 SmbiosRecord->CurrentLanguages = 1;
239 OptionalStrStart = (CHAR8 *)(SmbiosRecord + 1);
240 AsciiStrCpy(OptionalStrStart, CurrentLang);
241
242 //
243 // Now we have got the full smbios record, call smbios protocol to add this record.
244 //
245 SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
246 Status = Smbios-> Add(
247 Smbios,
248 NULL,
249 &SmbiosHandle,
250 (EFI_SMBIOS_TABLE_HEADER *) SmbiosRecord
251 );
252 FreePool(SmbiosRecord);
253 return Status;
254 }