blob: df2a988ee39eb71c20b9fdd460c476396c20ac71 [file] [log] [blame]
Heinrich Schuchardte59072a2018-09-04 19:34:58 +02001// 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
Heinrich Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020010#include <charset.h>
11#include <cp1250.h>
12#include <cp437.h>
13#include <efi_loader.h>
14
Heinrich Schuchardtb5dd2652019-06-12 21:06:28 +020015/* Characters that may not be used in FAT 8.3 file names */
16static const char illegal[] = "+,<=>:;\"/\\|?*[]\x7f";
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020017
18/*
19 * EDK2 assumes codepage 1250 when creating FAT 8.3 file names.
20 * Linux defaults to codepage 437 for FAT 8.3 file names.
21 */
22#if CONFIG_FAT_DEFAULT_CODEPAGE == 1250
23/* Unicode code points for code page 1250 characters 0x80 - 0xff */
24static const u16 codepage[] = CP1250;
25#else
26/* Unicode code points for code page 437 characters 0x80 - 0xff */
Heinrich Schuchardta9ff07b2021-02-27 14:08:35 +010027static const u16 *codepage = codepage_437;
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020028#endif
29
Heinrich Schuchardtb3258842019-05-16 07:52:58 +020030/* GUID of the EFI_UNICODE_COLLATION_PROTOCOL2 */
31const efi_guid_t efi_guid_unicode_collation_protocol2 =
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020032 EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
33
34/**
35 * efi_stri_coll() - compare utf-16 strings case-insenitively
36 *
37 * @this: unicode collation protocol instance
38 * @s1: first string
39 * @s2: second string
40 *
41 * This function implements the StriColl() service of the
Heinrich Schuchardt3bca2342021-01-16 09:58:06 +010042 * EFI_UNICODE_COLLATION_PROTOCOL2.
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020043 *
44 * See the Unified Extensible Firmware Interface (UEFI) specification for
45 * details.
46 *
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020047 * Return: 0: s1 == s2, > 0: s1 > s2, < 0: s1 < s2
48 */
49static efi_intn_t EFIAPI efi_stri_coll(
50 struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2)
51{
52 s32 c1, c2;
53 efi_intn_t ret = 0;
54
55 EFI_ENTRY("%p, %ls, %ls", this, s1, s2);
56 for (; *s1 | *s2; ++s1, ++s2) {
57 c1 = utf_to_upper(*s1);
58 c2 = utf_to_upper(*s2);
59 if (c1 < c2) {
60 ret = -1;
61 goto out;
62 } else if (c1 > c2) {
63 ret = 1;
64 goto out;
65 }
66 }
67out:
68 EFI_EXIT(EFI_SUCCESS);
69 return ret;
70}
71
72/**
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +020073 * next_lower() - get next codepoint converted to lower case
74 *
75 * @string: pointer to u16 string, on return advanced by one codepoint
76 * Return: first codepoint of string converted to lower case
77 */
78static s32 next_lower(const u16 **string)
79{
80 return utf_to_lower(utf16_get(string));
81}
82
83/**
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020084 * metai_match() - compare utf-16 string with a pattern string case-insenitively
85 *
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +020086 * @string: string to compare
87 * @pattern: pattern string
Heinrich Schuchardte59072a2018-09-04 19:34:58 +020088 *
89 * The pattern string may use these:
90 * - * matches >= 0 characters
91 * - ? matches 1 character
92 * - [<char1><char2>...<charN>] match any character in the set
93 * - [<char1>-<char2>] matches any character in the range
94 *
95 * This function is called my efi_metai_match().
96 *
97 * For '*' pattern searches this function calls itself recursively.
98 * Performance-wise this is suboptimal, especially for multiple '*' wildcards.
99 * But it results in simple code.
100 *
101 * Return: true if the string is matched.
102 */
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200103static bool metai_match(const u16 *string, const u16 *pattern)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200104{
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200105 s32 first, s, p;
106
107 for (; *string && *pattern;) {
108 const u16 *string_old = string;
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200109
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200110 s = next_lower(&string);
111 p = next_lower(&pattern);
112
113 switch (p) {
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200114 case '*':
115 /* Match 0 or more characters */
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200116 for (;; s = next_lower(&string)) {
117 if (metai_match(string_old, pattern))
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200118 return true;
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200119 if (!s)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200120 return false;
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200121 string_old = string;
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200122 }
123 case '?':
124 /* Match any one character */
125 break;
126 case '[':
127 /* Match any character in the set */
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200128 p = next_lower(&pattern);
129 first = p;
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200130 if (first == ']')
131 /* Empty set */
132 return false;
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200133 p = next_lower(&pattern);
134 if (p == '-') {
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200135 /* Range */
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200136 p = next_lower(&pattern);
137 if (s < first || s > p)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200138 return false;
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200139 p = next_lower(&pattern);
140 if (p != ']')
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200141 return false;
142 } else {
143 /* Set */
144 bool hit = false;
145
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200146 if (s == first)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200147 hit = true;
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200148 for (; p && p != ']';
149 p = next_lower(&pattern)) {
150 if (p == s)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200151 hit = true;
152 }
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200153 if (!hit || p != ']')
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200154 return false;
155 }
156 break;
157 default:
158 /* Match one character */
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200159 if (p != s)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200160 return false;
161 }
162 }
Heinrich Schuchardte6e62e02019-06-12 19:18:24 +0200163 if (!*pattern && !*string)
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200164 return true;
165 return false;
166}
167
168/**
169 * efi_metai_match() - compare utf-16 string with a pattern string
170 * case-insenitively
171 *
172 * @this: unicode collation protocol instance
Heinrich Schuchardt41020f32020-04-10 17:39:23 +0200173 * @string: string to compare
174 * @pattern: pattern string
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200175 *
176 * The pattern string may use these:
177 * - * matches >= 0 characters
178 * - ? matches 1 character
179 * - [<char1><char2>...<charN>] match any character in the set
180 * - [<char1>-<char2>] matches any character in the range
181 *
182 * This function implements the MetaMatch() service of the
Heinrich Schuchardt3bca2342021-01-16 09:58:06 +0100183 * EFI_UNICODE_COLLATION_PROTOCOL2.
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200184 *
185 * Return: true if the string is matched.
186 */
187static bool EFIAPI efi_metai_match(struct efi_unicode_collation_protocol *this,
188 const u16 *string, const u16 *pattern)
189{
190 bool ret;
191
192 EFI_ENTRY("%p, %ls, %ls", this, string, pattern);
193 ret = metai_match(string, pattern);
194 EFI_EXIT(EFI_SUCCESS);
195 return ret;
196}
197
198/**
199 * efi_str_lwr() - convert to lower case
200 *
201 * @this: unicode collation protocol instance
202 * @string: string to convert
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200203 *
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
Heinrich Schuchardt3bca2342021-01-16 09:58:06 +0100208 * EFI_UNICODE_COLLATION_PROTOCOL2.
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200209 */
210static 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
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200224 *
225 * The conversion is done in place. As long as upper and lower letters use the
226 * same number of words this does not pose a problem.
227 *
228 * This function implements the StrUpr() service of the
Heinrich Schuchardt3bca2342021-01-16 09:58:06 +0100229 * EFI_UNICODE_COLLATION_PROTOCOL2.
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200230 */
231static void EFIAPI efi_str_upr(struct efi_unicode_collation_protocol *this,
232 u16 *string)
233{
234 EFI_ENTRY("%p, %ls", this, string);
235 for (; *string; ++string)
236 *string = utf_to_upper(*string);
237 EFI_EXIT(EFI_SUCCESS);
238}
239
240/**
241 * efi_fat_to_str() - convert an 8.3 file name from an OEM codepage to Unicode
242 *
243 * @this: unicode collation protocol instance
244 * @fat_size: size of the string to convert
245 * @fat: string to convert
246 * @string: converted string
247 *
248 * This function implements the FatToStr() service of the
Heinrich Schuchardt3bca2342021-01-16 09:58:06 +0100249 * EFI_UNICODE_COLLATION_PROTOCOL2.
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200250 */
251static void EFIAPI efi_fat_to_str(struct efi_unicode_collation_protocol *this,
252 efi_uintn_t fat_size, char *fat, u16 *string)
253{
254 efi_uintn_t i;
255 u16 c;
256
257 EFI_ENTRY("%p, %zu, %s, %p", this, fat_size, fat, string);
258 for (i = 0; i < fat_size; ++i) {
259 c = (unsigned char)fat[i];
260 if (c > 0x80)
Janne Grunau3a043882024-03-16 22:50:20 +0100261 c = codepage[c - 0x60];
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200262 string[i] = c;
263 if (!c)
264 break;
265 }
266 string[i] = 0;
267 EFI_EXIT(EFI_SUCCESS);
268}
269
270/**
Heinrich Schuchardtb0102502024-09-18 23:37:28 +0200271 * efi_str_to_fat() - convert a utf-16 string to legal characters for a FAT
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200272 * file name in an OEM code page
273 *
274 * @this: unicode collation protocol instance
275 * @string: Unicode string to convert
276 * @fat_size: size of the target buffer
277 * @fat: converted string
278 *
279 * This function implements the StrToFat() service of the
Heinrich Schuchardt3bca2342021-01-16 09:58:06 +0100280 * EFI_UNICODE_COLLATION_PROTOCOL2.
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200281 *
282 * Return: true if an illegal character was substituted by '_'.
283 */
284static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this,
285 const u16 *string, efi_uintn_t fat_size,
286 char *fat)
287{
288 efi_uintn_t i;
289 s32 c;
290 bool ret = false;
291
292 EFI_ENTRY("%p, %ls, %zu, %p", this, string, fat_size, fat);
293 for (i = 0; i < fat_size;) {
294 c = utf16_get(&string);
295 switch (c) {
296 /* Ignore period and space */
297 case '.':
298 case ' ':
299 continue;
300 case 0:
301 break;
302 }
303 c = utf_to_upper(c);
Heinrich Schuchardt91fb0892021-02-27 14:08:36 +0100304 if (utf_to_cp(&c, codepage) ||
305 (c && (c < 0x20 || strchr(illegal, c)))) {
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200306 ret = true;
Heinrich Schuchardt91fb0892021-02-27 14:08:36 +0100307 c = '_';
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200308 }
309
310 fat[i] = c;
311 if (!c)
312 break;
313 ++i;
314 }
315 EFI_EXIT(EFI_SUCCESS);
316 return ret;
317}
318
Heinrich Schuchardtb3258842019-05-16 07:52:58 +0200319const struct efi_unicode_collation_protocol efi_unicode_collation_protocol2 = {
Heinrich Schuchardte59072a2018-09-04 19:34:58 +0200320 .stri_coll = efi_stri_coll,
321 .metai_match = efi_metai_match,
322 .str_lwr = efi_str_lwr,
323 .str_upr = efi_str_upr,
324 .fat_to_str = efi_fat_to_str,
325 .str_to_fat = efi_str_to_fat,
326 .supported_languages = "en",
327};