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