efi-stub-helper.c 15 KB

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