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