1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * EFI Unicode collation protocol
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 */
7
8 #include <common.h>
9 #include <charset.h>
10 #include <cp1250.h>
11 #include <cp437.h>
12 #include <efi_loader.h>
13
14 /* Characters that may not be used in FAT 8.3 file names */
15 static const char illegal[] = "+,<=>:;\"/\\|?*[]\x7f";
16
17 /*
18 * EDK2 assumes codepage 1250 when creating FAT 8.3 file names.
19 * Linux defaults to codepage 437 for FAT 8.3 file names.
20 */
21 #if CONFIG_FAT_DEFAULT_CODEPAGE == 1250
22 /* Unicode code points for code page 1250 characters 0x80 - 0xff */
23 static const u16 codepage[] = CP1250;
24 #else
25 /* Unicode code points for code page 437 characters 0x80 - 0xff */
26 static const u16 codepage[] = CP437;
27 #endif
28
29 /* GUID of the EFI_UNICODE_COLLATION_PROTOCOL2 */
30 const efi_guid_t efi_guid_unicode_collation_protocol2 =
31 EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
32
33 /**
34 * efi_stri_coll() - compare utf-16 strings case-insenitively
35 *
36 * @this: unicode collation protocol instance
37 * @s1: first string
38 * @s2: second string
39 *
40 * This function implements the StriColl() service of the
41 * EFI_UNICODE_COLLATION_PROTOCOL.
42 *
43 * See the Unified Extensible Firmware Interface (UEFI) specification for
44 * details.
45 *
46 * Return: 0: s1 == s2, > 0: s1 > s2, < 0: s1 < s2
47 */
efi_stri_coll(struct efi_unicode_collation_protocol * this,u16 * s1,u16 * s2)48 static efi_intn_t EFIAPI efi_stri_coll(
49 struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2)
50 {
51 s32 c1, c2;
52 efi_intn_t ret = 0;
53
54 EFI_ENTRY("%p, %ls, %ls", this, s1, s2);
55 for (; *s1 | *s2; ++s1, ++s2) {
56 c1 = utf_to_upper(*s1);
57 c2 = utf_to_upper(*s2);
58 if (c1 < c2) {
59 ret = -1;
60 goto out;
61 } else if (c1 > c2) {
62 ret = 1;
63 goto out;
64 }
65 }
66 out:
67 EFI_EXIT(EFI_SUCCESS);
68 return ret;
69 }
70
71 /**
72 * next_lower() - get next codepoint converted to lower case
73 *
74 * @string: pointer to u16 string, on return advanced by one codepoint
75 * Return: first codepoint of string converted to lower case
76 */
next_lower(const u16 ** string)77 static s32 next_lower(const u16 **string)
78 {
79 return utf_to_lower(utf16_get(string));
80 }
81
82 /**
83 * metai_match() - compare utf-16 string with a pattern string case-insenitively
84 *
85 * @string: string to compare
86 * @pattern: pattern string
87 *
88 * The pattern string may use these:
89 * - * matches >= 0 characters
90 * - ? matches 1 character
91 * - [<char1><char2>...<charN>] match any character in the set
92 * - [<char1>-<char2>] matches any character in the range
93 *
94 * This function is called my efi_metai_match().
95 *
96 * For '*' pattern searches this function calls itself recursively.
97 * Performance-wise this is suboptimal, especially for multiple '*' wildcards.
98 * But it results in simple code.
99 *
100 * Return: true if the string is matched.
101 */
metai_match(const u16 * string,const u16 * pattern)102 static bool metai_match(const u16 *string, const u16 *pattern)
103 {
104 s32 first, s, p;
105
106 for (; *string && *pattern;) {
107 const u16 *string_old = string;
108
109 s = next_lower(&string);
110 p = next_lower(&pattern);
111
112 switch (p) {
113 case '*':
114 /* Match 0 or more characters */
115 for (;; s = next_lower(&string)) {
116 if (metai_match(string_old, pattern))
117 return true;
118 if (!s)
119 return false;
120 string_old = string;
121 }
122 case '?':
123 /* Match any one character */
124 break;
125 case '[':
126 /* Match any character in the set */
127 p = next_lower(&pattern);
128 first = p;
129 if (first == ']')
130 /* Empty set */
131 return false;
132 p = next_lower(&pattern);
133 if (p == '-') {
134 /* Range */
135 p = next_lower(&pattern);
136 if (s < first || s > p)
137 return false;
138 p = next_lower(&pattern);
139 if (p != ']')
140 return false;
141 } else {
142 /* Set */
143 bool hit = false;
144
145 if (s == first)
146 hit = true;
147 for (; p && p != ']';
148 p = next_lower(&pattern)) {
149 if (p == s)
150 hit = true;
151 }
152 if (!hit || p != ']')
153 return false;
154 }
155 break;
156 default:
157 /* Match one character */
158 if (p != s)
159 return false;
160 }
161 }
162 if (!*pattern && !*string)
163 return true;
164 return false;
165 }
166
167 /**
168 * efi_metai_match() - compare utf-16 string with a pattern string
169 * case-insenitively
170 *
171 * @this: unicode collation protocol instance
172 * @s: string to compare
173 * @p: pattern string
174 *
175 * The pattern string may use these:
176 * - * matches >= 0 characters
177 * - ? matches 1 character
178 * - [<char1><char2>...<charN>] match any character in the set
179 * - [<char1>-<char2>] matches any character in the range
180 *
181 * This function implements the MetaMatch() service of the
182 * EFI_UNICODE_COLLATION_PROTOCOL.
183 *
184 * Return: true if the string is matched.
185 */
efi_metai_match(struct efi_unicode_collation_protocol * this,const u16 * string,const u16 * pattern)186 static bool EFIAPI efi_metai_match(struct efi_unicode_collation_protocol *this,
187 const u16 *string, const u16 *pattern)
188 {
189 bool ret;
190
191 EFI_ENTRY("%p, %ls, %ls", this, string, pattern);
192 ret = metai_match(string, pattern);
193 EFI_EXIT(EFI_SUCCESS);
194 return ret;
195 }
196
197 /**
198 * efi_str_lwr() - convert to lower case
199 *
200 * @this: unicode collation protocol instance
201 * @string: string to convert
202 * @p: pattern string
203 *
204 * The conversion is done in place. As long as upper and lower letters use the
205 * same number of words this does not pose a problem.
206 *
207 * This function implements the StrLwr() service of the
208 * EFI_UNICODE_COLLATION_PROTOCOL.
209 */
efi_str_lwr(struct efi_unicode_collation_protocol * this,u16 * string)210 static void EFIAPI efi_str_lwr(struct efi_unicode_collation_protocol *this,
211 u16 *string)
212 {
213 EFI_ENTRY("%p, %ls", this, string);
214 for (; *string; ++string)
215 *string = utf_to_lower(*string);
216 EFI_EXIT(EFI_SUCCESS);
217 }
218
219 /**
220 * efi_str_upr() - convert to upper case
221 *
222 * @this: unicode collation protocol instance
223 * @string: string to convert
224 * @p: pattern string
225 *
226 * The conversion is done in place. As long as upper and lower letters use the
227 * same number of words this does not pose a problem.
228 *
229 * This function implements the StrUpr() service of the
230 * EFI_UNICODE_COLLATION_PROTOCOL.
231 */
efi_str_upr(struct efi_unicode_collation_protocol * this,u16 * string)232 static void EFIAPI efi_str_upr(struct efi_unicode_collation_protocol *this,
233 u16 *string)
234 {
235 EFI_ENTRY("%p, %ls", this, string);
236 for (; *string; ++string)
237 *string = utf_to_upper(*string);
238 EFI_EXIT(EFI_SUCCESS);
239 }
240
241 /**
242 * efi_fat_to_str() - convert an 8.3 file name from an OEM codepage to Unicode
243 *
244 * @this: unicode collation protocol instance
245 * @fat_size: size of the string to convert
246 * @fat: string to convert
247 * @string: converted string
248 *
249 * This function implements the FatToStr() service of the
250 * EFI_UNICODE_COLLATION_PROTOCOL.
251 */
efi_fat_to_str(struct efi_unicode_collation_protocol * this,efi_uintn_t fat_size,char * fat,u16 * string)252 static void EFIAPI efi_fat_to_str(struct efi_unicode_collation_protocol *this,
253 efi_uintn_t fat_size, char *fat, u16 *string)
254 {
255 efi_uintn_t i;
256 u16 c;
257
258 EFI_ENTRY("%p, %zu, %s, %p", this, fat_size, fat, string);
259 for (i = 0; i < fat_size; ++i) {
260 c = (unsigned char)fat[i];
261 if (c > 0x80)
262 c = codepage[i - 0x80];
263 string[i] = c;
264 if (!c)
265 break;
266 }
267 string[i] = 0;
268 EFI_EXIT(EFI_SUCCESS);
269 }
270
271 /**
272 * efi_fat_to_str() - convert a utf-16 string to legal characters for a FAT
273 * file name in an OEM code page
274 *
275 * @this: unicode collation protocol instance
276 * @string: Unicode string to convert
277 * @fat_size: size of the target buffer
278 * @fat: converted string
279 *
280 * This function implements the StrToFat() service of the
281 * EFI_UNICODE_COLLATION_PROTOCOL.
282 *
283 * Return: true if an illegal character was substituted by '_'.
284 */
efi_str_to_fat(struct efi_unicode_collation_protocol * this,const u16 * string,efi_uintn_t fat_size,char * fat)285 static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this,
286 const u16 *string, efi_uintn_t fat_size,
287 char *fat)
288 {
289 efi_uintn_t i;
290 s32 c;
291 bool ret = false;
292
293 EFI_ENTRY("%p, %ls, %zu, %p", this, string, fat_size, fat);
294 for (i = 0; i < fat_size;) {
295 c = utf16_get(&string);
296 switch (c) {
297 /* Ignore period and space */
298 case '.':
299 case ' ':
300 continue;
301 case 0:
302 break;
303 }
304 c = utf_to_upper(c);
305 if (c >= 0x80) {
306 int j;
307
308 /* Look for codepage translation */
309 for (j = 0; j < 0x80; ++j) {
310 if (c == codepage[j]) {
311 c = j + 0x80;
312 break;
313 }
314 }
315 if (j >= 0x80) {
316 c = '_';
317 ret = true;
318 }
319 } else if (c && (c < 0x20 || strchr(illegal, c))) {
320 c = '_';
321 ret = true;
322 }
323
324 fat[i] = c;
325 if (!c)
326 break;
327 ++i;
328 }
329 EFI_EXIT(EFI_SUCCESS);
330 return ret;
331 }
332
333 const struct efi_unicode_collation_protocol efi_unicode_collation_protocol2 = {
334 .stri_coll = efi_stri_coll,
335 .metai_match = efi_metai_match,
336 .str_lwr = efi_str_lwr,
337 .str_upr = efi_str_upr,
338 .fat_to_str = efi_fat_to_str,
339 .str_to_fat = efi_str_to_fat,
340 .supported_languages = "en",
341 };
342
343 /*
344 * In EFI 1.10 a version of the Unicode collation protocol using ISO 639-2
345 * language codes existed. This protocol is not part of the UEFI specification
346 * any longer. Unfortunately it is required to run the UEFI Self Certification
347 * Test (SCT) II, version 2.6, 2017. So we implement it here for the sole
348 * purpose of running the SCT. It can be removed when a compliant SCT is
349 * available.
350 */
351 #if CONFIG_IS_ENABLED(EFI_UNICODE_COLLATION_PROTOCOL)
352
353 /* GUID of the EFI_UNICODE_COLLATION_PROTOCOL */
354 const efi_guid_t efi_guid_unicode_collation_protocol =
355 EFI_UNICODE_COLLATION_PROTOCOL_GUID;
356
357 const struct efi_unicode_collation_protocol efi_unicode_collation_protocol = {
358 .stri_coll = efi_stri_coll,
359 .metai_match = efi_metai_match,
360 .str_lwr = efi_str_lwr,
361 .str_upr = efi_str_upr,
362 .fat_to_str = efi_fat_to_str,
363 .str_to_fat = efi_str_to_fat,
364 /* ISO 639-2 language code */
365 .supported_languages = "eng",
366 };
367
368 #endif
369