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