efi-stub-helper.c 21 KB

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