efi-stub-helper.c 14 KB

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