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