efi-stub-helper.c 16 KB

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