efi-stub-helper.c 16 KB

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