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