efi-stub-helper.c 16 KB

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