efi-stub-helper.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * Helper functions used by the EFI stub on multiple
  3. * architectures. This should be #included by the EFI stub
  4. * implementation files.
  5. *
  6. * Copyright 2011 Intel Corporation; author Matt Fleming
  7. *
  8. * This file is part of the Linux kernel, and is made available
  9. * under the terms of the GNU General Public License version 2.
  10. *
  11. */
  12. #include <linux/efi.h>
  13. #include <asm/efi.h>
  14. #include "efistub.h"
  15. /*
  16. * Some firmware implementations have problems reading files in one go.
  17. * A read chunk size of 1MB seems to work for most platforms.
  18. *
  19. * Unfortunately, reading files in chunks triggers *other* bugs on some
  20. * platforms, so we provide a way to disable this workaround, which can
  21. * be done by passing "efi=nochunk" on the EFI boot stub command line.
  22. *
  23. * If you experience issues with initrd images being corrupt it's worth
  24. * trying efi=nochunk, but chunking is enabled by default because there
  25. * are far more machines that require the workaround than those that
  26. * break with it enabled.
  27. */
  28. #define EFI_READ_CHUNK_SIZE (1024 * 1024)
  29. static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
  30. #define EFI_MMAP_NR_SLACK_SLOTS 8
  31. struct file_info {
  32. efi_file_handle_t *handle;
  33. u64 size;
  34. };
  35. void efi_printk(efi_system_table_t *sys_table_arg, char *str)
  36. {
  37. char *s8;
  38. for (s8 = str; *s8; s8++) {
  39. efi_char16_t ch[2] = { 0 };
  40. ch[0] = *s8;
  41. if (*s8 == '\n') {
  42. efi_char16_t nl[2] = { '\r', 0 };
  43. efi_char16_printk(sys_table_arg, nl);
  44. }
  45. efi_char16_printk(sys_table_arg, ch);
  46. }
  47. }
  48. static inline bool mmap_has_headroom(unsigned long buff_size,
  49. unsigned long map_size,
  50. unsigned long desc_size)
  51. {
  52. unsigned long slack = buff_size - map_size;
  53. return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
  54. }
  55. efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
  56. struct efi_boot_memmap *map)
  57. {
  58. efi_memory_desc_t *m = NULL;
  59. efi_status_t status;
  60. unsigned long key;
  61. u32 desc_version;
  62. *map->desc_size = sizeof(*m);
  63. *map->map_size = *map->desc_size * 32;
  64. *map->buff_size = *map->map_size;
  65. again:
  66. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  67. *map->map_size, (void **)&m);
  68. if (status != EFI_SUCCESS)
  69. goto fail;
  70. *map->desc_size = 0;
  71. key = 0;
  72. status = efi_call_early(get_memory_map, map->map_size, m,
  73. &key, map->desc_size, &desc_version);
  74. if (status == EFI_BUFFER_TOO_SMALL ||
  75. !mmap_has_headroom(*map->buff_size, *map->map_size,
  76. *map->desc_size)) {
  77. efi_call_early(free_pool, m);
  78. /*
  79. * Make sure there is some entries of headroom so that the
  80. * buffer can be reused for a new map after allocations are
  81. * no longer permitted. Its unlikely that the map will grow to
  82. * exceed this headroom once we are ready to trigger
  83. * ExitBootServices()
  84. */
  85. *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
  86. *map->buff_size = *map->map_size;
  87. goto again;
  88. }
  89. if (status != EFI_SUCCESS)
  90. efi_call_early(free_pool, m);
  91. if (map->key_ptr && status == EFI_SUCCESS)
  92. *map->key_ptr = key;
  93. if (map->desc_ver && status == EFI_SUCCESS)
  94. *map->desc_ver = desc_version;
  95. fail:
  96. *map->map = m;
  97. return status;
  98. }
  99. unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
  100. {
  101. efi_status_t status;
  102. unsigned long map_size, buff_size;
  103. unsigned long membase = EFI_ERROR;
  104. struct efi_memory_map map;
  105. efi_memory_desc_t *md;
  106. struct efi_boot_memmap boot_map;
  107. boot_map.map = (efi_memory_desc_t **)&map.map;
  108. boot_map.map_size = &map_size;
  109. boot_map.desc_size = &map.desc_size;
  110. boot_map.desc_ver = NULL;
  111. boot_map.key_ptr = NULL;
  112. boot_map.buff_size = &buff_size;
  113. status = efi_get_memory_map(sys_table_arg, &boot_map);
  114. if (status != EFI_SUCCESS)
  115. return membase;
  116. map.map_end = map.map + map_size;
  117. for_each_efi_memory_desc_in_map(&map, md) {
  118. if (md->attribute & EFI_MEMORY_WB) {
  119. if (membase > md->phys_addr)
  120. membase = md->phys_addr;
  121. }
  122. }
  123. efi_call_early(free_pool, map.map);
  124. return membase;
  125. }
  126. /*
  127. * Allocate at the highest possible address that is not above 'max'.
  128. */
  129. efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
  130. unsigned long size, unsigned long align,
  131. unsigned long *addr, unsigned long max)
  132. {
  133. unsigned long map_size, desc_size, buff_size;
  134. efi_memory_desc_t *map;
  135. efi_status_t status;
  136. unsigned long nr_pages;
  137. u64 max_addr = 0;
  138. int i;
  139. struct efi_boot_memmap boot_map;
  140. boot_map.map = &map;
  141. boot_map.map_size = &map_size;
  142. boot_map.desc_size = &desc_size;
  143. boot_map.desc_ver = NULL;
  144. boot_map.key_ptr = NULL;
  145. boot_map.buff_size = &buff_size;
  146. status = efi_get_memory_map(sys_table_arg, &boot_map);
  147. if (status != EFI_SUCCESS)
  148. goto fail;
  149. /*
  150. * Enforce minimum alignment that EFI or Linux requires when
  151. * requesting a specific address. We are doing page-based (or
  152. * larger) allocations, and both the address and size must meet
  153. * alignment constraints.
  154. */
  155. if (align < EFI_ALLOC_ALIGN)
  156. align = EFI_ALLOC_ALIGN;
  157. size = round_up(size, EFI_ALLOC_ALIGN);
  158. nr_pages = size / EFI_PAGE_SIZE;
  159. again:
  160. for (i = 0; i < map_size / desc_size; i++) {
  161. efi_memory_desc_t *desc;
  162. unsigned long m = (unsigned long)map;
  163. u64 start, end;
  164. desc = (efi_memory_desc_t *)(m + (i * desc_size));
  165. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  166. continue;
  167. if (desc->num_pages < nr_pages)
  168. continue;
  169. start = desc->phys_addr;
  170. end = start + desc->num_pages * EFI_PAGE_SIZE;
  171. if (end > max)
  172. end = max;
  173. if ((start + size) > end)
  174. continue;
  175. if (round_down(end - size, align) < start)
  176. continue;
  177. start = round_down(end - size, align);
  178. /*
  179. * Don't allocate at 0x0. It will confuse code that
  180. * checks pointers against NULL.
  181. */
  182. if (start == 0x0)
  183. continue;
  184. if (start > max_addr)
  185. max_addr = start;
  186. }
  187. if (!max_addr)
  188. status = EFI_NOT_FOUND;
  189. else {
  190. status = efi_call_early(allocate_pages,
  191. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  192. nr_pages, &max_addr);
  193. if (status != EFI_SUCCESS) {
  194. max = max_addr;
  195. max_addr = 0;
  196. goto again;
  197. }
  198. *addr = max_addr;
  199. }
  200. efi_call_early(free_pool, map);
  201. fail:
  202. return status;
  203. }
  204. /*
  205. * Allocate at the lowest possible address.
  206. */
  207. efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
  208. unsigned long size, unsigned long align,
  209. unsigned long *addr)
  210. {
  211. unsigned long map_size, desc_size, buff_size;
  212. efi_memory_desc_t *map;
  213. efi_status_t status;
  214. unsigned long nr_pages;
  215. int i;
  216. struct efi_boot_memmap boot_map;
  217. boot_map.map = &map;
  218. boot_map.map_size = &map_size;
  219. boot_map.desc_size = &desc_size;
  220. boot_map.desc_ver = NULL;
  221. boot_map.key_ptr = NULL;
  222. boot_map.buff_size = &buff_size;
  223. status = efi_get_memory_map(sys_table_arg, &boot_map);
  224. if (status != EFI_SUCCESS)
  225. goto fail;
  226. /*
  227. * Enforce minimum alignment that EFI or Linux requires when
  228. * requesting a specific address. We are doing page-based (or
  229. * larger) allocations, and both the address and size must meet
  230. * alignment constraints.
  231. */
  232. if (align < EFI_ALLOC_ALIGN)
  233. align = EFI_ALLOC_ALIGN;
  234. size = round_up(size, EFI_ALLOC_ALIGN);
  235. nr_pages = size / EFI_PAGE_SIZE;
  236. for (i = 0; i < map_size / desc_size; i++) {
  237. efi_memory_desc_t *desc;
  238. unsigned long m = (unsigned long)map;
  239. u64 start, end;
  240. desc = (efi_memory_desc_t *)(m + (i * desc_size));
  241. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  242. continue;
  243. if (desc->num_pages < nr_pages)
  244. continue;
  245. start = desc->phys_addr;
  246. end = start + desc->num_pages * EFI_PAGE_SIZE;
  247. /*
  248. * Don't allocate at 0x0. It will confuse code that
  249. * checks pointers against NULL. Skip the first 8
  250. * bytes so we start at a nice even number.
  251. */
  252. if (start == 0x0)
  253. start += 8;
  254. start = round_up(start, align);
  255. if ((start + size) > end)
  256. continue;
  257. status = efi_call_early(allocate_pages,
  258. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  259. nr_pages, &start);
  260. if (status == EFI_SUCCESS) {
  261. *addr = start;
  262. break;
  263. }
  264. }
  265. if (i == map_size / desc_size)
  266. status = EFI_NOT_FOUND;
  267. efi_call_early(free_pool, map);
  268. fail:
  269. return status;
  270. }
  271. void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
  272. unsigned long addr)
  273. {
  274. unsigned long nr_pages;
  275. if (!size)
  276. return;
  277. nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  278. efi_call_early(free_pages, addr, nr_pages);
  279. }
  280. static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
  281. efi_char16_t *filename_16, void **handle,
  282. u64 *file_sz)
  283. {
  284. efi_file_handle_t *h, *fh = __fh;
  285. efi_file_info_t *info;
  286. efi_status_t status;
  287. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  288. unsigned long info_sz;
  289. status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
  290. EFI_FILE_MODE_READ, (u64)0);
  291. if (status != EFI_SUCCESS) {
  292. efi_printk(sys_table_arg, "Failed to open file: ");
  293. efi_char16_printk(sys_table_arg, filename_16);
  294. efi_printk(sys_table_arg, "\n");
  295. return status;
  296. }
  297. *handle = h;
  298. info_sz = 0;
  299. status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
  300. &info_sz, NULL);
  301. if (status != EFI_BUFFER_TOO_SMALL) {
  302. efi_printk(sys_table_arg, "Failed to get file info size\n");
  303. return status;
  304. }
  305. grow:
  306. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  307. info_sz, (void **)&info);
  308. if (status != EFI_SUCCESS) {
  309. efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
  310. return status;
  311. }
  312. status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
  313. &info_sz, info);
  314. if (status == EFI_BUFFER_TOO_SMALL) {
  315. efi_call_early(free_pool, info);
  316. goto grow;
  317. }
  318. *file_sz = info->file_size;
  319. efi_call_early(free_pool, info);
  320. if (status != EFI_SUCCESS)
  321. efi_printk(sys_table_arg, "Failed to get initrd info\n");
  322. return status;
  323. }
  324. static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
  325. {
  326. return efi_call_proto(efi_file_handle, read, handle, size, addr);
  327. }
  328. static efi_status_t efi_file_close(void *handle)
  329. {
  330. return efi_call_proto(efi_file_handle, close, handle);
  331. }
  332. /*
  333. * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
  334. * option, e.g. efi=nochunk.
  335. *
  336. * It should be noted that efi= is parsed in two very different
  337. * environments, first in the early boot environment of the EFI boot
  338. * stub, and subsequently during the kernel boot.
  339. */
  340. efi_status_t efi_parse_options(char *cmdline)
  341. {
  342. char *str;
  343. /*
  344. * Currently, the only efi= option we look for is 'nochunk', which
  345. * is intended to work around known issues on certain x86 UEFI
  346. * versions. So ignore for now on other architectures.
  347. */
  348. if (!IS_ENABLED(CONFIG_X86))
  349. return EFI_SUCCESS;
  350. /*
  351. * If no EFI parameters were specified on the cmdline we've got
  352. * nothing to do.
  353. */
  354. str = strstr(cmdline, "efi=");
  355. if (!str)
  356. return EFI_SUCCESS;
  357. /* Skip ahead to first argument */
  358. str += strlen("efi=");
  359. /*
  360. * Remember, because efi= is also used by the kernel we need to
  361. * skip over arguments we don't understand.
  362. */
  363. while (*str) {
  364. if (!strncmp(str, "nochunk", 7)) {
  365. str += strlen("nochunk");
  366. __chunk_size = -1UL;
  367. }
  368. /* Group words together, delimited by "," */
  369. while (*str && *str != ',')
  370. str++;
  371. if (*str == ',')
  372. str++;
  373. }
  374. return EFI_SUCCESS;
  375. }
  376. /*
  377. * Check the cmdline for a LILO-style file= arguments.
  378. *
  379. * We only support loading a file from the same filesystem as
  380. * the kernel image.
  381. */
  382. efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
  383. efi_loaded_image_t *image,
  384. char *cmd_line, char *option_string,
  385. unsigned long max_addr,
  386. unsigned long *load_addr,
  387. unsigned long *load_size)
  388. {
  389. struct file_info *files;
  390. unsigned long file_addr;
  391. u64 file_size_total;
  392. efi_file_handle_t *fh = NULL;
  393. efi_status_t status;
  394. int nr_files;
  395. char *str;
  396. int i, j, k;
  397. file_addr = 0;
  398. file_size_total = 0;
  399. str = cmd_line;
  400. j = 0; /* See close_handles */
  401. if (!load_addr || !load_size)
  402. return EFI_INVALID_PARAMETER;
  403. *load_addr = 0;
  404. *load_size = 0;
  405. if (!str || !*str)
  406. return EFI_SUCCESS;
  407. for (nr_files = 0; *str; nr_files++) {
  408. str = strstr(str, option_string);
  409. if (!str)
  410. break;
  411. str += strlen(option_string);
  412. /* Skip any leading slashes */
  413. while (*str == '/' || *str == '\\')
  414. str++;
  415. while (*str && *str != ' ' && *str != '\n')
  416. str++;
  417. }
  418. if (!nr_files)
  419. return EFI_SUCCESS;
  420. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  421. nr_files * sizeof(*files), (void **)&files);
  422. if (status != EFI_SUCCESS) {
  423. pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
  424. goto fail;
  425. }
  426. str = cmd_line;
  427. for (i = 0; i < nr_files; i++) {
  428. struct file_info *file;
  429. efi_char16_t filename_16[256];
  430. efi_char16_t *p;
  431. str = strstr(str, option_string);
  432. if (!str)
  433. break;
  434. str += strlen(option_string);
  435. file = &files[i];
  436. p = filename_16;
  437. /* Skip any leading slashes */
  438. while (*str == '/' || *str == '\\')
  439. str++;
  440. while (*str && *str != ' ' && *str != '\n') {
  441. if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
  442. break;
  443. if (*str == '/') {
  444. *p++ = '\\';
  445. str++;
  446. } else {
  447. *p++ = *str++;
  448. }
  449. }
  450. *p = '\0';
  451. /* Only open the volume once. */
  452. if (!i) {
  453. status = efi_open_volume(sys_table_arg, image,
  454. (void **)&fh);
  455. if (status != EFI_SUCCESS)
  456. goto free_files;
  457. }
  458. status = efi_file_size(sys_table_arg, fh, filename_16,
  459. (void **)&file->handle, &file->size);
  460. if (status != EFI_SUCCESS)
  461. goto close_handles;
  462. file_size_total += file->size;
  463. }
  464. if (file_size_total) {
  465. unsigned long addr;
  466. /*
  467. * Multiple files need to be at consecutive addresses in memory,
  468. * so allocate enough memory for all the files. This is used
  469. * for loading multiple files.
  470. */
  471. status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
  472. &file_addr, max_addr);
  473. if (status != EFI_SUCCESS) {
  474. pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
  475. goto close_handles;
  476. }
  477. /* We've run out of free low memory. */
  478. if (file_addr > max_addr) {
  479. pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
  480. status = EFI_INVALID_PARAMETER;
  481. goto free_file_total;
  482. }
  483. addr = file_addr;
  484. for (j = 0; j < nr_files; j++) {
  485. unsigned long size;
  486. size = files[j].size;
  487. while (size) {
  488. unsigned long chunksize;
  489. if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
  490. chunksize = __chunk_size;
  491. else
  492. chunksize = size;
  493. status = efi_file_read(files[j].handle,
  494. &chunksize,
  495. (void *)addr);
  496. if (status != EFI_SUCCESS) {
  497. pr_efi_err(sys_table_arg, "Failed to read file\n");
  498. goto free_file_total;
  499. }
  500. addr += chunksize;
  501. size -= chunksize;
  502. }
  503. efi_file_close(files[j].handle);
  504. }
  505. }
  506. efi_call_early(free_pool, files);
  507. *load_addr = file_addr;
  508. *load_size = file_size_total;
  509. return status;
  510. free_file_total:
  511. efi_free(sys_table_arg, file_size_total, file_addr);
  512. close_handles:
  513. for (k = j; k < i; k++)
  514. efi_file_close(files[k].handle);
  515. free_files:
  516. efi_call_early(free_pool, files);
  517. fail:
  518. *load_addr = 0;
  519. *load_size = 0;
  520. return status;
  521. }
  522. /*
  523. * Relocate a kernel image, either compressed or uncompressed.
  524. * In the ARM64 case, all kernel images are currently
  525. * uncompressed, and as such when we relocate it we need to
  526. * allocate additional space for the BSS segment. Any low
  527. * memory that this function should avoid needs to be
  528. * unavailable in the EFI memory map, as if the preferred
  529. * address is not available the lowest available address will
  530. * be used.
  531. */
  532. efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
  533. unsigned long *image_addr,
  534. unsigned long image_size,
  535. unsigned long alloc_size,
  536. unsigned long preferred_addr,
  537. unsigned long alignment)
  538. {
  539. unsigned long cur_image_addr;
  540. unsigned long new_addr = 0;
  541. efi_status_t status;
  542. unsigned long nr_pages;
  543. efi_physical_addr_t efi_addr = preferred_addr;
  544. if (!image_addr || !image_size || !alloc_size)
  545. return EFI_INVALID_PARAMETER;
  546. if (alloc_size < image_size)
  547. return EFI_INVALID_PARAMETER;
  548. cur_image_addr = *image_addr;
  549. /*
  550. * The EFI firmware loader could have placed the kernel image
  551. * anywhere in memory, but the kernel has restrictions on the
  552. * max physical address it can run at. Some architectures
  553. * also have a prefered address, so first try to relocate
  554. * to the preferred address. If that fails, allocate as low
  555. * as possible while respecting the required alignment.
  556. */
  557. nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  558. status = efi_call_early(allocate_pages,
  559. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  560. nr_pages, &efi_addr);
  561. new_addr = efi_addr;
  562. /*
  563. * If preferred address allocation failed allocate as low as
  564. * possible.
  565. */
  566. if (status != EFI_SUCCESS) {
  567. status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
  568. &new_addr);
  569. }
  570. if (status != EFI_SUCCESS) {
  571. pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
  572. return status;
  573. }
  574. /*
  575. * We know source/dest won't overlap since both memory ranges
  576. * have been allocated by UEFI, so we can safely use memcpy.
  577. */
  578. memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
  579. /* Return the new address of the relocated image. */
  580. *image_addr = new_addr;
  581. return status;
  582. }
  583. /*
  584. * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
  585. * This overestimates for surrogates, but that is okay.
  586. */
  587. static int efi_utf8_bytes(u16 c)
  588. {
  589. return 1 + (c >= 0x80) + (c >= 0x800);
  590. }
  591. /*
  592. * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
  593. */
  594. static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
  595. {
  596. unsigned int c;
  597. while (n--) {
  598. c = *src++;
  599. if (n && c >= 0xd800 && c <= 0xdbff &&
  600. *src >= 0xdc00 && *src <= 0xdfff) {
  601. c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
  602. src++;
  603. n--;
  604. }
  605. if (c >= 0xd800 && c <= 0xdfff)
  606. c = 0xfffd; /* Unmatched surrogate */
  607. if (c < 0x80) {
  608. *dst++ = c;
  609. continue;
  610. }
  611. if (c < 0x800) {
  612. *dst++ = 0xc0 + (c >> 6);
  613. goto t1;
  614. }
  615. if (c < 0x10000) {
  616. *dst++ = 0xe0 + (c >> 12);
  617. goto t2;
  618. }
  619. *dst++ = 0xf0 + (c >> 18);
  620. *dst++ = 0x80 + ((c >> 12) & 0x3f);
  621. t2:
  622. *dst++ = 0x80 + ((c >> 6) & 0x3f);
  623. t1:
  624. *dst++ = 0x80 + (c & 0x3f);
  625. }
  626. return dst;
  627. }
  628. #ifndef MAX_CMDLINE_ADDRESS
  629. #define MAX_CMDLINE_ADDRESS ULONG_MAX
  630. #endif
  631. /*
  632. * Convert the unicode UEFI command line to ASCII to pass to kernel.
  633. * Size of memory allocated return in *cmd_line_len.
  634. * Returns NULL on error.
  635. */
  636. char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
  637. efi_loaded_image_t *image,
  638. int *cmd_line_len)
  639. {
  640. const u16 *s2;
  641. u8 *s1 = NULL;
  642. unsigned long cmdline_addr = 0;
  643. int load_options_chars = image->load_options_size / 2; /* UTF-16 */
  644. const u16 *options = image->load_options;
  645. int options_bytes = 0; /* UTF-8 bytes */
  646. int options_chars = 0; /* UTF-16 chars */
  647. efi_status_t status;
  648. u16 zero = 0;
  649. if (options) {
  650. s2 = options;
  651. while (*s2 && *s2 != '\n'
  652. && options_chars < load_options_chars) {
  653. options_bytes += efi_utf8_bytes(*s2++);
  654. options_chars++;
  655. }
  656. }
  657. if (!options_chars) {
  658. /* No command line options, so return empty string*/
  659. options = &zero;
  660. }
  661. options_bytes++; /* NUL termination */
  662. status = efi_high_alloc(sys_table_arg, options_bytes, 0,
  663. &cmdline_addr, MAX_CMDLINE_ADDRESS);
  664. if (status != EFI_SUCCESS)
  665. return NULL;
  666. s1 = (u8 *)cmdline_addr;
  667. s2 = (const u16 *)options;
  668. s1 = efi_utf16_to_utf8(s1, s2, options_chars);
  669. *s1 = '\0';
  670. *cmd_line_len = options_bytes;
  671. return (char *)cmdline_addr;
  672. }
  673. /*
  674. * Handle calling ExitBootServices according to the requirements set out by the
  675. * spec. Obtains the current memory map, and returns that info after calling
  676. * ExitBootServices. The client must specify a function to perform any
  677. * processing of the memory map data prior to ExitBootServices. A client
  678. * specific structure may be passed to the function via priv. The client
  679. * function may be called multiple times.
  680. */
  681. efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
  682. void *handle,
  683. struct efi_boot_memmap *map,
  684. void *priv,
  685. efi_exit_boot_map_processing priv_func)
  686. {
  687. efi_status_t status;
  688. status = efi_get_memory_map(sys_table_arg, map);
  689. if (status != EFI_SUCCESS)
  690. goto fail;
  691. status = priv_func(sys_table_arg, map, priv);
  692. if (status != EFI_SUCCESS)
  693. goto free_map;
  694. status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
  695. if (status == EFI_INVALID_PARAMETER) {
  696. /*
  697. * The memory map changed between efi_get_memory_map() and
  698. * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
  699. * EFI_BOOT_SERVICES.ExitBootServices we need to get the
  700. * updated map, and try again. The spec implies one retry
  701. * should be sufficent, which is confirmed against the EDK2
  702. * implementation. Per the spec, we can only invoke
  703. * get_memory_map() and exit_boot_services() - we cannot alloc
  704. * so efi_get_memory_map() cannot be used, and we must reuse
  705. * the buffer. For all practical purposes, the headroom in the
  706. * buffer should account for any changes in the map so the call
  707. * to get_memory_map() is expected to succeed here.
  708. */
  709. *map->map_size = *map->buff_size;
  710. status = efi_call_early(get_memory_map,
  711. map->map_size,
  712. *map->map,
  713. map->key_ptr,
  714. map->desc_size,
  715. map->desc_ver);
  716. /* exit_boot_services() was called, thus cannot free */
  717. if (status != EFI_SUCCESS)
  718. goto fail;
  719. status = priv_func(sys_table_arg, map, priv);
  720. /* exit_boot_services() was called, thus cannot free */
  721. if (status != EFI_SUCCESS)
  722. goto fail;
  723. status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
  724. }
  725. /* exit_boot_services() was called, thus cannot free */
  726. if (status != EFI_SUCCESS)
  727. goto fail;
  728. return EFI_SUCCESS;
  729. free_map:
  730. efi_call_early(free_pool, *map->map);
  731. fail:
  732. return status;
  733. }