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