]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/MiscSubClassPlatformDxe/MiscNumberOfInstallableLanguagesFunction.c
1. PI SMBIOS Checkin. Major change include:
[mirror_edk2.git] / UnixPkg / MiscSubClassPlatformDxe / MiscNumberOfInstallableLanguagesFunction.c
1 /*++
2
3 Copyright (c) 2009, Intel Corporation. All rights reserved. <BR>
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
10 Intel Corporation.
11
12
13 Module Name:
14
15 MiscNumberOfInstallableLanguagesFunction.c
16
17 Abstract:
18
19 This driver parses the mSmbiosMiscDataTable structure and reports
20 any generated data.
21
22 --*/
23
24 #include "MiscSubClassDriver.h"
25 /*++
26 Check whether the language is supported for given HII handle
27
28 @param HiiHandle The HII package list handle.
29 @param Offset The offest of current lanague in the supported languages.
30 @param CurrentLang The language code.
31
32 @retval TRUE Supported.
33 @retval FALSE Not Supported.
34
35 --*/
36 VOID
37 EFIAPI
38 CurrentLanguageMatch (
39 IN EFI_HII_HANDLE HiiHandle,
40 OUT UINT16 *Offset,
41 OUT CHAR8 *CurrentLang
42 )
43 {
44 CHAR8 *DefaultLang;
45 CHAR8 *BestLanguage;
46 CHAR8 *Languages;
47 CHAR8 *MatchLang;
48 CHAR8 *EndMatchLang;
49 UINTN CompareLength;
50
51 Languages = HiiGetSupportedLanguages (HiiHandle);
52 if (Languages == NULL) {
53 return;
54 }
55
56 CurrentLang = GetEfiGlobalVariable (L"PlatformLang");
57 DefaultLang = (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang);
58 BestLanguage = GetBestLanguage (
59 Languages,
60 FALSE,
61 (CurrentLang != NULL) ? CurrentLang : "",
62 DefaultLang,
63 NULL
64 );
65 if (BestLanguage != NULL) {
66 //
67 // Find the best matching RFC 4646 language, compute the offset.
68 //
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 ;
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 EFI_STATUS Status;
194 EFI_SMBIOS_HANDLE SmbiosHandle;
195 SMBIOS_TABLE_TYPE13 *SmbiosRecord;
196 EFI_MISC_NUMBER_OF_INSTALLABLE_LANGUAGES *ForType13InputData;
197
198 ForType13InputData = (EFI_MISC_NUMBER_OF_INSTALLABLE_LANGUAGES *)RecordData;
199
200 //
201 // First check for invalid parameters.
202 //
203 if (RecordData == NULL) {
204 return EFI_INVALID_PARAMETER;
205 }
206
207 ForType13InputData->NumberOfInstallableLanguages = GetSupportedLanguageNumber (mHiiHandle);
208
209 //
210 // Try to check if current langcode matches with the langcodes in installed languages
211 //
212 ZeroMem(CurrentLang, SMBIOS_STRING_MAX_LENGTH + 1);
213 CurrentLanguageMatch (mHiiHandle, &Offset, CurrentLang);
214 LangStrLen = AsciiStrLen(CurrentLang);
215
216 //
217 // Two zeros following the last string.
218 //
219 SmbiosRecord = AllocatePool(sizeof (SMBIOS_TABLE_TYPE13) + LangStrLen + 1 + 1);
220 ZeroMem(SmbiosRecord, sizeof (SMBIOS_TABLE_TYPE13) + LangStrLen + 1 + 1);
221
222 SmbiosRecord->Hdr.Type = EFI_SMBIOS_TYPE_BIOS_LANGUAGE_INFORMATION;
223 SmbiosRecord->Hdr.Length = sizeof (SMBIOS_TABLE_TYPE13);
224 //
225 // Make handle chosen by smbios protocol.add automatically.
226 //
227 SmbiosRecord->Hdr.Handle = 0;
228
229 SmbiosRecord->InstallableLanguages = (UINT8)ForType13InputData->NumberOfInstallableLanguages;
230 SmbiosRecord->Flags = (UINT8)ForType13InputData->LanguageFlags.AbbreviatedLanguageFormat;
231 SmbiosRecord->CurrentLanguages = 1;
232 OptionalStrStart = (CHAR8 *)(SmbiosRecord + 1);
233 AsciiStrCpy(OptionalStrStart, CurrentLang);
234 //
235 // Now we have got the full smbios record, call smbios protocol to add this record.
236 //
237 SmbiosHandle = 0;
238 Status = Smbios-> Add(
239 Smbios,
240 NULL,
241 &SmbiosHandle,
242 (EFI_SMBIOS_TABLE_HEADER *) SmbiosRecord
243 );
244 FreePool(SmbiosRecord);
245 return Status;
246 }