efi-stub-helper.c 20 KB

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