efi-stub-helper.c 16 KB

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