]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Apply GetBestLanguage() UefiLib to initialize Unicode Collation Protocol.
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
1 /** @file
2 Unicode Collation Support component that hides the trivial difference of Unicode Collation
3 and Unicode collation 2 Protocol.
4
5 Copyright (c) 2007 - 2009, Intel Corporation<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Fat.h"
17
18 EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;
19
20 /**
21 Worker function to initialize Unicode Collation support.
22
23 This function searches Initialized Unicode Collation support based on PCDs:
24 PcdUnicodeCollation2Support and PcdUnicodeCollationSupport.
25 It first tries to locate Unicode Collation 2 protocol and matches it with current
26 platform language code. If for any reason the first attempt fails, it then tries to
27 use Unicode Collation Protocol.
28
29 @param AgentHandle The handle used to open Unicode Collation (2) protocol.
30 @param ProtocolGuid The pointer to Unicode Collation (2) protocol GUID.
31 @param VariableName The name of the RFC 4646 or ISO 639-2 language variable.
32 @param DefaultLanguage The default language in case the RFC 4646 or ISO 639-2 language is absent.
33
34 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
35 @retval Others The Unicode Collation (2) protocol has not been located.
36
37 **/
38 EFI_STATUS
39 InitializeUnicodeCollationSupportWorker (
40 IN EFI_HANDLE AgentHandle,
41 IN EFI_GUID *ProtocolGuid,
42 IN CONST CHAR16 *VariableName,
43 IN CONST CHAR8 *DefaultLanguage
44 )
45 {
46 EFI_STATUS Status;
47 UINTN NumHandles;
48 UINTN Index;
49 EFI_HANDLE *Handles;
50 EFI_UNICODE_COLLATION_PROTOCOL *Uci;
51 BOOLEAN Iso639Language;
52 CHAR8 *Language;
53 CHAR8 *BestLanguage;
54
55 Status = gBS->LocateHandleBuffer (
56 ByProtocol,
57 ProtocolGuid,
58 NULL,
59 &NumHandles,
60 &Handles
61 );
62 if (EFI_ERROR (Status)) {
63 return Status;
64 }
65
66 Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);
67 Language = GetEfiGlobalVariable(VariableName);
68
69 Status = EFI_UNSUPPORTED;
70 for (Index = 0; Index < NumHandles; Index++) {
71 //
72 // Open Unicode Collation Protocol
73 //
74 Status = gBS->OpenProtocol (
75 Handles[Index],
76 ProtocolGuid,
77 (VOID **) &Uci,
78 AgentHandle,
79 NULL,
80 EFI_OPEN_PROTOCOL_GET_PROTOCOL
81 );
82 if (EFI_ERROR (Status)) {
83 continue;
84 }
85
86 //
87 // Find the best matching matching language from the supported languages
88 // of Unicode Collation (2) protocol.
89 //
90 if (Language == NULL) {
91 BestLanguage = GetBestLanguage (
92 Uci->SupportedLanguages,
93 Iso639Language,
94 DefaultLanguage,
95 NULL
96 );
97 } else {
98 BestLanguage = GetBestLanguage (
99 Uci->SupportedLanguages,
100 Iso639Language,
101 Language,
102 Iso639Language,
103 DefaultLanguage,
104 NULL
105 );
106 }
107 if (BestLanguage != NULL) {
108 FreePool (BestLanguage);
109 mUnicodeCollationInterface = Uci;
110 Status = EFI_SUCCESS;
111 break;
112 }
113 }
114
115 if (Language != NULL) {
116 FreePool (Language);
117 }
118
119 FreePool (Handles);
120
121 return Status;
122 }
123
124 /**
125 Initialize Unicode Collation support.
126
127 This function searches Initialized Unicode Collation support based on PCDs:
128 PcdUnicodeCollation2Support and PcdUnicodeCollationSupport.
129 It first tries to locate Unicode Collation 2 protocol and matches it with current
130 platform language code. If for any reason the first attempt fails, it then tries to
131 use Unicode Collation Protocol.
132
133 @param AgentHandle The handle used to open Unicode Collation (2) protocol.
134
135 @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
136 @retval Others The Unicode Collation (2) protocol has not been located.
137
138 **/
139 EFI_STATUS
140 InitializeUnicodeCollationSupport (
141 IN EFI_HANDLE AgentHandle
142 )
143 {
144
145 EFI_STATUS Status;
146
147 Status = EFI_UNSUPPORTED;
148
149 //
150 // First try to use RFC 4646 Unicode Collation 2 Protocol.
151 //
152 if (FeaturePcdGet (PcdUnicodeCollation2Support)) {
153 Status = InitializeUnicodeCollationSupportWorker (
154 AgentHandle,
155 &gEfiUnicodeCollation2ProtocolGuid,
156 L"PlatformLang",
157 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)
158 );
159 }
160
161 //
162 // If the attempt to use Unicode Collation 2 Protocol fails, then we fall back
163 // on the ISO 639-2 Unicode Collation Protocol.
164 //
165 if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) {
166 Status = InitializeUnicodeCollationSupportWorker (
167 AgentHandle,
168 &gEfiUnicodeCollationProtocolGuid,
169 L"Lang",
170 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)
171 );
172 }
173
174 return Status;
175 }
176
177
178 /**
179 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
180
181 @param S1 A pointer to a Null-terminated Unicode string.
182 @param S2 A pointer to a Null-terminated Unicode string.
183
184 @retval 0 S1 is equivalent to S2.
185 @retval >0 S1 is lexically greater than S2.
186 @retval <0 S1 is lexically less than S2.
187 **/
188 INTN
189 FatStriCmp (
190 IN CHAR16 *S1,
191 IN CHAR16 *S2
192 )
193 {
194 ASSERT (StrSize (S1) != 0);
195 ASSERT (StrSize (S2) != 0);
196 ASSERT (mUnicodeCollationInterface != NULL);
197
198 return mUnicodeCollationInterface->StriColl (
199 mUnicodeCollationInterface,
200 S1,
201 S2
202 );
203 }
204
205
206 /**
207 Uppercase a string.
208
209 @param Str The string which will be upper-cased.
210
211 @return None.
212
213 **/
214 VOID
215 FatStrUpr (
216 IN OUT CHAR16 *String
217 )
218 {
219 ASSERT (StrSize (String) != 0);
220 ASSERT (mUnicodeCollationInterface != NULL);
221
222 mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);
223 }
224
225
226 /**
227 Lowercase a string
228
229 @param Str The string which will be lower-cased.
230
231 @return None
232
233 **/
234 VOID
235 FatStrLwr (
236 IN OUT CHAR16 *String
237 )
238 {
239 ASSERT (StrSize (String) != 0);
240 ASSERT (mUnicodeCollationInterface != NULL);
241
242 mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);
243 }
244
245
246 /**
247 Convert FAT string to unicode string.
248
249 @param FatSize The size of FAT string.
250 @param Fat The FAT string.
251 @param String The unicode string.
252
253 @return None.
254
255 **/
256 VOID
257 FatFatToStr (
258 IN UINTN FatSize,
259 IN CHAR8 *Fat,
260 OUT CHAR16 *String
261 )
262 {
263 ASSERT (Fat != NULL);
264 ASSERT (String != NULL);
265 ASSERT (((UINTN) String & 0x01) == 0);
266 ASSERT (mUnicodeCollationInterface != NULL);
267
268 mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);
269 }
270
271
272 /**
273 Convert unicode string to Fat string.
274
275 @param String The unicode string.
276 @param FatSize The size of the FAT string.
277 @param Fat The FAT string.
278
279 @retval TRUE Convert successfully.
280 @retval FALSE Convert error.
281
282 **/
283 BOOLEAN
284 FatStrToFat (
285 IN CHAR16 *String,
286 IN UINTN FatSize,
287 OUT CHAR8 *Fat
288 )
289 {
290 ASSERT (Fat != NULL);
291 ASSERT (StrSize (String) != 0);
292 ASSERT (mUnicodeCollationInterface != NULL);
293
294 return mUnicodeCollationInterface->StrToFat (
295 mUnicodeCollationInterface,
296 String,
297 FatSize,
298 Fat
299 );
300 }