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