efi_unicode_collation.c 9.0 KB

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