efi-stub-helper.c 14 KB

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