1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI Unicode collation protocol
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
12 #include <efi_loader.h>
14 /* Characters that may not be used in FAT 8.3 file names */
15 static const char illegal[] = "+,<=>:;\"/\\|?*[]\x7f";
18 * EDK2 assumes codepage 1250 when creating FAT 8.3 file names.
19 * Linux defaults to codepage 437 for FAT 8.3 file names.
21 #if CONFIG_FAT_DEFAULT_CODEPAGE == 1250
22 /* Unicode code points for code page 1250 characters 0x80 - 0xff */
23 static const u16 codepage[] = CP1250;
25 /* Unicode code points for code page 437 characters 0x80 - 0xff */
26 static const u16 *codepage = codepage_437;
29 /* GUID of the EFI_UNICODE_COLLATION_PROTOCOL2 */
30 const efi_guid_t efi_guid_unicode_collation_protocol2 =
31 EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
34 * efi_stri_coll() - compare utf-16 strings case-insenitively
36 * @this: unicode collation protocol instance
40 * This function implements the StriColl() service of the
41 * EFI_UNICODE_COLLATION_PROTOCOL2.
43 * See the Unified Extensible Firmware Interface (UEFI) specification for
46 * Return: 0: s1 == s2, > 0: s1 > s2, < 0: s1 < s2
48 static efi_intn_t EFIAPI efi_stri_coll(
49 struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2)
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);
67 EFI_EXIT(EFI_SUCCESS);
72 * next_lower() - get next codepoint converted to lower case
74 * @string: pointer to u16 string, on return advanced by one codepoint
75 * Return: first codepoint of string converted to lower case
77 static s32 next_lower(const u16 **string)
79 return utf_to_lower(utf16_get(string));
83 * metai_match() - compare utf-16 string with a pattern string case-insenitively
85 * @string: string to compare
86 * @pattern: pattern string
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
94 * This function is called my efi_metai_match().
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.
100 * Return: true if the string is matched.
102 static bool metai_match(const u16 *string, const u16 *pattern)
106 for (; *string && *pattern;) {
107 const u16 *string_old = string;
109 s = next_lower(&string);
110 p = next_lower(&pattern);
114 /* Match 0 or more characters */
115 for (;; s = next_lower(&string)) {
116 if (metai_match(string_old, pattern))
123 /* Match any one character */
126 /* Match any character in the set */
127 p = next_lower(&pattern);
132 p = next_lower(&pattern);
135 p = next_lower(&pattern);
136 if (s < first || s > p)
138 p = next_lower(&pattern);
147 for (; p && p != ']';
148 p = next_lower(&pattern)) {
152 if (!hit || p != ']')
157 /* Match one character */
162 if (!*pattern && !*string)
168 * efi_metai_match() - compare utf-16 string with a pattern string
171 * @this: unicode collation protocol instance
172 * @string: string to compare
173 * @pattern: pattern string
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
181 * This function implements the MetaMatch() service of the
182 * EFI_UNICODE_COLLATION_PROTOCOL2.
184 * Return: true if the string is matched.
186 static bool EFIAPI efi_metai_match(struct efi_unicode_collation_protocol *this,
187 const u16 *string, const u16 *pattern)
191 EFI_ENTRY("%p, %ls, %ls", this, string, pattern);
192 ret = metai_match(string, pattern);
193 EFI_EXIT(EFI_SUCCESS);
198 * efi_str_lwr() - convert to lower case
200 * @this: unicode collation protocol instance
201 * @string: string to convert
203 * The conversion is done in place. As long as upper and lower letters use the
204 * same number of words this does not pose a problem.
206 * This function implements the StrLwr() service of the
207 * EFI_UNICODE_COLLATION_PROTOCOL2.
209 static void EFIAPI efi_str_lwr(struct efi_unicode_collation_protocol *this,
212 EFI_ENTRY("%p, %ls", this, string);
213 for (; *string; ++string)
214 *string = utf_to_lower(*string);
215 EFI_EXIT(EFI_SUCCESS);
219 * efi_str_upr() - convert to upper case
221 * @this: unicode collation protocol instance
222 * @string: string to convert
224 * The conversion is done in place. As long as upper and lower letters use the
225 * same number of words this does not pose a problem.
227 * This function implements the StrUpr() service of the
228 * EFI_UNICODE_COLLATION_PROTOCOL2.
230 static void EFIAPI efi_str_upr(struct efi_unicode_collation_protocol *this,
233 EFI_ENTRY("%p, %ls", this, string);
234 for (; *string; ++string)
235 *string = utf_to_upper(*string);
236 EFI_EXIT(EFI_SUCCESS);
240 * efi_fat_to_str() - convert an 8.3 file name from an OEM codepage to Unicode
242 * @this: unicode collation protocol instance
243 * @fat_size: size of the string to convert
244 * @fat: string to convert
245 * @string: converted string
247 * This function implements the FatToStr() service of the
248 * EFI_UNICODE_COLLATION_PROTOCOL2.
250 static void EFIAPI efi_fat_to_str(struct efi_unicode_collation_protocol *this,
251 efi_uintn_t fat_size, char *fat, u16 *string)
256 EFI_ENTRY("%p, %zu, %s, %p", this, fat_size, fat, string);
257 for (i = 0; i < fat_size; ++i) {
258 c = (unsigned char)fat[i];
260 c = codepage[c - 0x80];
266 EFI_EXIT(EFI_SUCCESS);
270 * efi_fat_to_str() - convert a utf-16 string to legal characters for a FAT
271 * file name in an OEM code page
273 * @this: unicode collation protocol instance
274 * @string: Unicode string to convert
275 * @fat_size: size of the target buffer
276 * @fat: converted string
278 * This function implements the StrToFat() service of the
279 * EFI_UNICODE_COLLATION_PROTOCOL2.
281 * Return: true if an illegal character was substituted by '_'.
283 static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this,
284 const u16 *string, efi_uintn_t fat_size,
291 EFI_ENTRY("%p, %ls, %zu, %p", this, string, fat_size, fat);
292 for (i = 0; i < fat_size;) {
293 c = utf16_get(&string);
295 /* Ignore period and space */
303 if (utf_to_cp(&c, codepage) ||
304 (c && (c < 0x20 || strchr(illegal, c)))) {
314 EFI_EXIT(EFI_SUCCESS);
318 const struct efi_unicode_collation_protocol efi_unicode_collation_protocol2 = {
319 .stri_coll = efi_stri_coll,
320 .metai_match = efi_metai_match,
321 .str_lwr = efi_str_lwr,
322 .str_upr = efi_str_upr,
323 .fat_to_str = efi_fat_to_str,
324 .str_to_fat = efi_str_to_fat,
325 .supported_languages = "en",