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