efi-stub-helper.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. /*
  281. * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
  282. * option, e.g. efi=nochunk.
  283. *
  284. * It should be noted that efi= is parsed in two very different
  285. * environments, first in the early boot environment of the EFI boot
  286. * stub, and subsequently during the kernel boot.
  287. */
  288. efi_status_t efi_parse_options(char *cmdline)
  289. {
  290. char *str;
  291. /*
  292. * If no EFI parameters were specified on the cmdline we've got
  293. * nothing to do.
  294. */
  295. str = strstr(cmdline, "efi=");
  296. if (!str)
  297. return EFI_SUCCESS;
  298. /* Skip ahead to first argument */
  299. str += strlen("efi=");
  300. /*
  301. * Remember, because efi= is also used by the kernel we need to
  302. * skip over arguments we don't understand.
  303. */
  304. while (*str) {
  305. if (!strncmp(str, "nochunk", 7)) {
  306. str += strlen("nochunk");
  307. __chunk_size = -1UL;
  308. }
  309. /* Group words together, delimited by "," */
  310. while (*str && *str != ',')
  311. str++;
  312. if (*str == ',')
  313. str++;
  314. }
  315. return EFI_SUCCESS;
  316. }
  317. /*
  318. * Check the cmdline for a LILO-style file= arguments.
  319. *
  320. * We only support loading a file from the same filesystem as
  321. * the kernel image.
  322. */
  323. efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
  324. efi_loaded_image_t *image,
  325. char *cmd_line, char *option_string,
  326. unsigned long max_addr,
  327. unsigned long *load_addr,
  328. unsigned long *load_size)
  329. {
  330. struct file_info *files;
  331. unsigned long file_addr;
  332. u64 file_size_total;
  333. efi_file_handle_t *fh = NULL;
  334. efi_status_t status;
  335. int nr_files;
  336. char *str;
  337. int i, j, k;
  338. file_addr = 0;
  339. file_size_total = 0;
  340. str = cmd_line;
  341. j = 0; /* See close_handles */
  342. if (!load_addr || !load_size)
  343. return EFI_INVALID_PARAMETER;
  344. *load_addr = 0;
  345. *load_size = 0;
  346. if (!str || !*str)
  347. return EFI_SUCCESS;
  348. for (nr_files = 0; *str; nr_files++) {
  349. str = strstr(str, option_string);
  350. if (!str)
  351. break;
  352. str += strlen(option_string);
  353. /* Skip any leading slashes */
  354. while (*str == '/' || *str == '\\')
  355. str++;
  356. while (*str && *str != ' ' && *str != '\n')
  357. str++;
  358. }
  359. if (!nr_files)
  360. return EFI_SUCCESS;
  361. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  362. nr_files * sizeof(*files), (void **)&files);
  363. if (status != EFI_SUCCESS) {
  364. pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
  365. goto fail;
  366. }
  367. str = cmd_line;
  368. for (i = 0; i < nr_files; i++) {
  369. struct file_info *file;
  370. efi_char16_t filename_16[256];
  371. efi_char16_t *p;
  372. str = strstr(str, option_string);
  373. if (!str)
  374. break;
  375. str += strlen(option_string);
  376. file = &files[i];
  377. p = filename_16;
  378. /* Skip any leading slashes */
  379. while (*str == '/' || *str == '\\')
  380. str++;
  381. while (*str && *str != ' ' && *str != '\n') {
  382. if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
  383. break;
  384. if (*str == '/') {
  385. *p++ = '\\';
  386. str++;
  387. } else {
  388. *p++ = *str++;
  389. }
  390. }
  391. *p = '\0';
  392. /* Only open the volume once. */
  393. if (!i) {
  394. status = efi_open_volume(sys_table_arg, image,
  395. (void **)&fh);
  396. if (status != EFI_SUCCESS)
  397. goto free_files;
  398. }
  399. status = efi_file_size(sys_table_arg, fh, filename_16,
  400. (void **)&file->handle, &file->size);
  401. if (status != EFI_SUCCESS)
  402. goto close_handles;
  403. file_size_total += file->size;
  404. }
  405. if (file_size_total) {
  406. unsigned long addr;
  407. /*
  408. * Multiple files need to be at consecutive addresses in memory,
  409. * so allocate enough memory for all the files. This is used
  410. * for loading multiple files.
  411. */
  412. status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
  413. &file_addr, max_addr);
  414. if (status != EFI_SUCCESS) {
  415. pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
  416. goto close_handles;
  417. }
  418. /* We've run out of free low memory. */
  419. if (file_addr > max_addr) {
  420. pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
  421. status = EFI_INVALID_PARAMETER;
  422. goto free_file_total;
  423. }
  424. addr = file_addr;
  425. for (j = 0; j < nr_files; j++) {
  426. unsigned long size;
  427. size = files[j].size;
  428. while (size) {
  429. unsigned long chunksize;
  430. if (size > __chunk_size)
  431. chunksize = __chunk_size;
  432. else
  433. chunksize = size;
  434. status = efi_file_read(files[j].handle,
  435. &chunksize,
  436. (void *)addr);
  437. if (status != EFI_SUCCESS) {
  438. pr_efi_err(sys_table_arg, "Failed to read file\n");
  439. goto free_file_total;
  440. }
  441. addr += chunksize;
  442. size -= chunksize;
  443. }
  444. efi_file_close(files[j].handle);
  445. }
  446. }
  447. efi_call_early(free_pool, files);
  448. *load_addr = file_addr;
  449. *load_size = file_size_total;
  450. return status;
  451. free_file_total:
  452. efi_free(sys_table_arg, file_size_total, file_addr);
  453. close_handles:
  454. for (k = j; k < i; k++)
  455. efi_file_close(files[k].handle);
  456. free_files:
  457. efi_call_early(free_pool, files);
  458. fail:
  459. *load_addr = 0;
  460. *load_size = 0;
  461. return status;
  462. }
  463. /*
  464. * Relocate a kernel image, either compressed or uncompressed.
  465. * In the ARM64 case, all kernel images are currently
  466. * uncompressed, and as such when we relocate it we need to
  467. * allocate additional space for the BSS segment. Any low
  468. * memory that this function should avoid needs to be
  469. * unavailable in the EFI memory map, as if the preferred
  470. * address is not available the lowest available address will
  471. * be used.
  472. */
  473. efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
  474. unsigned long *image_addr,
  475. unsigned long image_size,
  476. unsigned long alloc_size,
  477. unsigned long preferred_addr,
  478. unsigned long alignment)
  479. {
  480. unsigned long cur_image_addr;
  481. unsigned long new_addr = 0;
  482. efi_status_t status;
  483. unsigned long nr_pages;
  484. efi_physical_addr_t efi_addr = preferred_addr;
  485. if (!image_addr || !image_size || !alloc_size)
  486. return EFI_INVALID_PARAMETER;
  487. if (alloc_size < image_size)
  488. return EFI_INVALID_PARAMETER;
  489. cur_image_addr = *image_addr;
  490. /*
  491. * The EFI firmware loader could have placed the kernel image
  492. * anywhere in memory, but the kernel has restrictions on the
  493. * max physical address it can run at. Some architectures
  494. * also have a prefered address, so first try to relocate
  495. * to the preferred address. If that fails, allocate as low
  496. * as possible while respecting the required alignment.
  497. */
  498. nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  499. status = efi_call_early(allocate_pages,
  500. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  501. nr_pages, &efi_addr);
  502. new_addr = efi_addr;
  503. /*
  504. * If preferred address allocation failed allocate as low as
  505. * possible.
  506. */
  507. if (status != EFI_SUCCESS) {
  508. status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
  509. &new_addr);
  510. }
  511. if (status != EFI_SUCCESS) {
  512. pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
  513. return status;
  514. }
  515. /*
  516. * We know source/dest won't overlap since both memory ranges
  517. * have been allocated by UEFI, so we can safely use memcpy.
  518. */
  519. memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
  520. /* Return the new address of the relocated image. */
  521. *image_addr = new_addr;
  522. return status;
  523. }
  524. /*
  525. * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
  526. * This overestimates for surrogates, but that is okay.
  527. */
  528. static int efi_utf8_bytes(u16 c)
  529. {
  530. return 1 + (c >= 0x80) + (c >= 0x800);
  531. }
  532. /*
  533. * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
  534. */
  535. static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
  536. {
  537. unsigned int c;
  538. while (n--) {
  539. c = *src++;
  540. if (n && c >= 0xd800 && c <= 0xdbff &&
  541. *src >= 0xdc00 && *src <= 0xdfff) {
  542. c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
  543. src++;
  544. n--;
  545. }
  546. if (c >= 0xd800 && c <= 0xdfff)
  547. c = 0xfffd; /* Unmatched surrogate */
  548. if (c < 0x80) {
  549. *dst++ = c;
  550. continue;
  551. }
  552. if (c < 0x800) {
  553. *dst++ = 0xc0 + (c >> 6);
  554. goto t1;
  555. }
  556. if (c < 0x10000) {
  557. *dst++ = 0xe0 + (c >> 12);
  558. goto t2;
  559. }
  560. *dst++ = 0xf0 + (c >> 18);
  561. *dst++ = 0x80 + ((c >> 12) & 0x3f);
  562. t2:
  563. *dst++ = 0x80 + ((c >> 6) & 0x3f);
  564. t1:
  565. *dst++ = 0x80 + (c & 0x3f);
  566. }
  567. return dst;
  568. }
  569. #ifndef MAX_CMDLINE_ADDRESS
  570. #define MAX_CMDLINE_ADDRESS ULONG_MAX
  571. #endif
  572. /*
  573. * Convert the unicode UEFI command line to ASCII to pass to kernel.
  574. * Size of memory allocated return in *cmd_line_len.
  575. * Returns NULL on error.
  576. */
  577. char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
  578. efi_loaded_image_t *image,
  579. int *cmd_line_len)
  580. {
  581. const u16 *s2;
  582. u8 *s1 = NULL;
  583. unsigned long cmdline_addr = 0;
  584. int load_options_chars = image->load_options_size / 2; /* UTF-16 */
  585. const u16 *options = image->load_options;
  586. int options_bytes = 0; /* UTF-8 bytes */
  587. int options_chars = 0; /* UTF-16 chars */
  588. efi_status_t status;
  589. u16 zero = 0;
  590. if (options) {
  591. s2 = options;
  592. while (*s2 && *s2 != '\n'
  593. && options_chars < load_options_chars) {
  594. options_bytes += efi_utf8_bytes(*s2++);
  595. options_chars++;
  596. }
  597. }
  598. if (!options_chars) {
  599. /* No command line options, so return empty string*/
  600. options = &zero;
  601. }
  602. options_bytes++; /* NUL termination */
  603. status = efi_high_alloc(sys_table_arg, options_bytes, 0,
  604. &cmdline_addr, MAX_CMDLINE_ADDRESS);
  605. if (status != EFI_SUCCESS)
  606. return NULL;
  607. s1 = (u8 *)cmdline_addr;
  608. s2 = (const u16 *)options;
  609. s1 = efi_utf16_to_utf8(s1, s2, options_chars);
  610. *s1 = '\0';
  611. *cmd_line_len = options_bytes;
  612. return (char *)cmdline_addr;
  613. }
  614. /*
  615. * Handle calling ExitBootServices according to the requirements set out by the
  616. * spec. Obtains the current memory map, and returns that info after calling
  617. * ExitBootServices. The client must specify a function to perform any
  618. * processing of the memory map data prior to ExitBootServices. A client
  619. * specific structure may be passed to the function via priv. The client
  620. * function may be called multiple times.
  621. */
  622. efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
  623. void *handle,
  624. struct efi_boot_memmap *map,
  625. void *priv,
  626. efi_exit_boot_map_processing priv_func)
  627. {
  628. efi_status_t status;
  629. status = efi_get_memory_map(sys_table_arg, map);
  630. if (status != EFI_SUCCESS)
  631. goto fail;
  632. status = priv_func(sys_table_arg, map, priv);
  633. if (status != EFI_SUCCESS)
  634. goto free_map;
  635. status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
  636. if (status == EFI_INVALID_PARAMETER) {
  637. /*
  638. * The memory map changed between efi_get_memory_map() and
  639. * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
  640. * EFI_BOOT_SERVICES.ExitBootServices we need to get the
  641. * updated map, and try again. The spec implies one retry
  642. * should be sufficent, which is confirmed against the EDK2
  643. * implementation. Per the spec, we can only invoke
  644. * get_memory_map() and exit_boot_services() - we cannot alloc
  645. * so efi_get_memory_map() cannot be used, and we must reuse
  646. * the buffer. For all practical purposes, the headroom in the
  647. * buffer should account for any changes in the map so the call
  648. * to get_memory_map() is expected to succeed here.
  649. */
  650. *map->map_size = *map->buff_size;
  651. status = efi_call_early(get_memory_map,
  652. map->map_size,
  653. *map->map,
  654. map->key_ptr,
  655. map->desc_size,
  656. map->desc_ver);
  657. /* exit_boot_services() was called, thus cannot free */
  658. if (status != EFI_SUCCESS)
  659. goto fail;
  660. status = priv_func(sys_table_arg, map, priv);
  661. /* exit_boot_services() was called, thus cannot free */
  662. if (status != EFI_SUCCESS)
  663. goto fail;
  664. status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
  665. }
  666. /* exit_boot_services() was called, thus cannot free */
  667. if (status != EFI_SUCCESS)
  668. goto fail;
  669. return EFI_SUCCESS;
  670. free_map:
  671. efi_call_early(free_pool, *map->map);
  672. fail:
  673. return status;
  674. }