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