]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/UnicodeCollation.c
Fix a migration bug in Fat driver as the value of lock has been changed from EDK...
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / UnicodeCollation.c
... / ...
CommitLineData
1/** @file\r
2 Unicode Collation Support component that hides the trivial difference of Unicode Collation\r
3 and Unicode collation 2 Protocol.\r
4\r
5 Copyright (c) 2007 - 2010, Intel Corporation<BR>\r
6 All rights reserved. This program and the accompanying materials are licensed and made available\r
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
13\r
14**/\r
15\r
16#include "Fat.h"\r
17\r
18EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;\r
19\r
20/**\r
21 Worker function to initialize Unicode Collation support.\r
22\r
23 It tries to locate Unicode Collation (2) protocol and matches it with current\r
24 platform language code.\r
25\r
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
30\r
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
33\r
34**/\r
35EFI_STATUS\r
36InitializeUnicodeCollationSupportWorker (\r
37 IN EFI_HANDLE AgentHandle,\r
38 IN EFI_GUID *ProtocolGuid,\r
39 IN CONST CHAR16 *VariableName,\r
40 IN CONST CHAR8 *DefaultLanguage\r
41 )\r
42{\r
43 EFI_STATUS Status;\r
44 UINTN NumHandles;\r
45 UINTN Index;\r
46 EFI_HANDLE *Handles;\r
47 EFI_UNICODE_COLLATION_PROTOCOL *Uci;\r
48 BOOLEAN Iso639Language;\r
49 CHAR8 *Language;\r
50 CHAR8 *BestLanguage;\r
51\r
52 Status = gBS->LocateHandleBuffer (\r
53 ByProtocol,\r
54 ProtocolGuid,\r
55 NULL,\r
56 &NumHandles,\r
57 &Handles\r
58 );\r
59 if (EFI_ERROR (Status)) {\r
60 return Status;\r
61 }\r
62\r
63 Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);\r
64 Language = GetEfiGlobalVariable(VariableName);\r
65\r
66 Status = EFI_UNSUPPORTED;\r
67 for (Index = 0; Index < NumHandles; Index++) {\r
68 //\r
69 // Open Unicode Collation Protocol\r
70 //\r
71 Status = gBS->OpenProtocol (\r
72 Handles[Index],\r
73 ProtocolGuid,\r
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
83 //\r
84 // Find the best matching matching language from the supported languages\r
85 // of Unicode Collation (2) protocol. \r
86 //\r
87 BestLanguage = GetBestLanguage (\r
88 Uci->SupportedLanguages,\r
89 Iso639Language,\r
90 (Language == NULL) ? "" : Language,\r
91 DefaultLanguage,\r
92 NULL\r
93 );\r
94 if (BestLanguage != NULL) {\r
95 FreePool (BestLanguage);\r
96 mUnicodeCollationInterface = Uci;\r
97 Status = EFI_SUCCESS;\r
98 break;\r
99 }\r
100 }\r
101\r
102 if (Language != NULL) {\r
103 FreePool (Language);\r
104 }\r
105\r
106 FreePool (Handles);\r
107\r
108 return Status;\r
109}\r
110\r
111/**\r
112 Initialize Unicode Collation support.\r
113\r
114 It tries to locate Unicode Collation 2 protocol and matches it with current\r
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
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
133\r
134 //\r
135 // First try to use RFC 4646 Unicode Collation 2 Protocol.\r
136 //\r
137 Status = InitializeUnicodeCollationSupportWorker (\r
138 AgentHandle,\r
139 &gEfiUnicodeCollation2ProtocolGuid,\r
140 L"PlatformLang",\r
141 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)\r
142 );\r
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
147 if (EFI_ERROR (Status)) {\r
148 Status = InitializeUnicodeCollationSupportWorker (\r
149 AgentHandle,\r
150 &gEfiUnicodeCollationProtocolGuid,\r
151 L"Lang",\r
152 (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)\r
153 );\r
154 }\r
155\r
156 return Status;\r
157}\r
158\r
159\r
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
247 ASSERT (((UINTN) String & 0x01) == 0);\r
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