efi-stub-helper.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. free_pool:
  142. efi_call_phys1(sys_table_arg->boottime->free_pool, map);
  143. fail:
  144. return status;
  145. }
  146. /*
  147. * Allocate at the lowest possible address.
  148. */
  149. static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
  150. unsigned long size, unsigned long align,
  151. unsigned long *addr)
  152. {
  153. unsigned long map_size, desc_size;
  154. efi_memory_desc_t *map;
  155. efi_status_t status;
  156. unsigned long nr_pages;
  157. int i;
  158. status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
  159. NULL, NULL);
  160. if (status != EFI_SUCCESS)
  161. goto fail;
  162. /*
  163. * Enforce minimum alignment that EFI requires when requesting
  164. * a specific address. We are doing page-based allocations,
  165. * so we must be aligned to a page.
  166. */
  167. if (align < EFI_PAGE_SIZE)
  168. align = EFI_PAGE_SIZE;
  169. nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  170. for (i = 0; i < map_size / desc_size; i++) {
  171. efi_memory_desc_t *desc;
  172. unsigned long m = (unsigned long)map;
  173. u64 start, end;
  174. desc = (efi_memory_desc_t *)(m + (i * desc_size));
  175. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  176. continue;
  177. if (desc->num_pages < nr_pages)
  178. continue;
  179. start = desc->phys_addr;
  180. end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
  181. /*
  182. * Don't allocate at 0x0. It will confuse code that
  183. * checks pointers against NULL. Skip the first 8
  184. * bytes so we start at a nice even number.
  185. */
  186. if (start == 0x0)
  187. start += 8;
  188. start = round_up(start, align);
  189. if ((start + size) > end)
  190. continue;
  191. status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
  192. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  193. nr_pages, &start);
  194. if (status == EFI_SUCCESS) {
  195. *addr = start;
  196. break;
  197. }
  198. }
  199. if (i == map_size / desc_size)
  200. status = EFI_NOT_FOUND;
  201. free_pool:
  202. efi_call_phys1(sys_table_arg->boottime->free_pool, map);
  203. fail:
  204. return status;
  205. }
  206. static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
  207. unsigned long addr)
  208. {
  209. unsigned long nr_pages;
  210. if (!size)
  211. return;
  212. nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  213. efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
  214. }
  215. /*
  216. * Check the cmdline for a LILO-style file= arguments.
  217. *
  218. * We only support loading a file from the same filesystem as
  219. * the kernel image.
  220. */
  221. static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
  222. efi_loaded_image_t *image,
  223. char *cmd_line, char *option_string,
  224. unsigned long max_addr,
  225. unsigned long *load_addr,
  226. unsigned long *load_size)
  227. {
  228. struct file_info *files;
  229. unsigned long file_addr;
  230. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  231. u64 file_size_total;
  232. efi_file_io_interface_t *io;
  233. efi_file_handle_t *fh;
  234. efi_status_t status;
  235. int nr_files;
  236. char *str;
  237. int i, j, k;
  238. file_addr = 0;
  239. file_size_total = 0;
  240. str = cmd_line;
  241. j = 0; /* See close_handles */
  242. if (!load_addr || !load_size)
  243. return EFI_INVALID_PARAMETER;
  244. *load_addr = 0;
  245. *load_size = 0;
  246. if (!str || !*str)
  247. return EFI_SUCCESS;
  248. for (nr_files = 0; *str; nr_files++) {
  249. str = strstr(str, option_string);
  250. if (!str)
  251. break;
  252. str += strlen(option_string);
  253. /* Skip any leading slashes */
  254. while (*str == '/' || *str == '\\')
  255. str++;
  256. while (*str && *str != ' ' && *str != '\n')
  257. str++;
  258. }
  259. if (!nr_files)
  260. return EFI_SUCCESS;
  261. status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
  262. EFI_LOADER_DATA,
  263. nr_files * sizeof(*files),
  264. &files);
  265. if (status != EFI_SUCCESS) {
  266. efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n");
  267. goto fail;
  268. }
  269. str = cmd_line;
  270. for (i = 0; i < nr_files; i++) {
  271. struct file_info *file;
  272. efi_file_handle_t *h;
  273. efi_file_info_t *info;
  274. efi_char16_t filename_16[256];
  275. unsigned long info_sz;
  276. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  277. efi_char16_t *p;
  278. u64 file_sz;
  279. str = strstr(str, option_string);
  280. if (!str)
  281. break;
  282. str += strlen(option_string);
  283. file = &files[i];
  284. p = filename_16;
  285. /* Skip any leading slashes */
  286. while (*str == '/' || *str == '\\')
  287. str++;
  288. while (*str && *str != ' ' && *str != '\n') {
  289. if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
  290. break;
  291. if (*str == '/') {
  292. *p++ = '\\';
  293. *str++;
  294. } else {
  295. *p++ = *str++;
  296. }
  297. }
  298. *p = '\0';
  299. /* Only open the volume once. */
  300. if (!i) {
  301. efi_boot_services_t *boottime;
  302. boottime = sys_table_arg->boottime;
  303. status = efi_call_phys3(boottime->handle_protocol,
  304. image->device_handle, &fs_proto, &io);
  305. if (status != EFI_SUCCESS) {
  306. efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
  307. goto free_files;
  308. }
  309. status = efi_call_phys2(io->open_volume, io, &fh);
  310. if (status != EFI_SUCCESS) {
  311. efi_printk(sys_table_arg, "Failed to open volume\n");
  312. goto free_files;
  313. }
  314. }
  315. status = efi_call_phys5(fh->open, fh, &h, filename_16,
  316. EFI_FILE_MODE_READ, (u64)0);
  317. if (status != EFI_SUCCESS) {
  318. efi_printk(sys_table_arg, "Failed to open file: ");
  319. efi_char16_printk(sys_table_arg, filename_16);
  320. efi_printk(sys_table_arg, "\n");
  321. goto close_handles;
  322. }
  323. file->handle = h;
  324. info_sz = 0;
  325. status = efi_call_phys4(h->get_info, h, &info_guid,
  326. &info_sz, NULL);
  327. if (status != EFI_BUFFER_TOO_SMALL) {
  328. efi_printk(sys_table_arg, "Failed to get file info size\n");
  329. goto close_handles;
  330. }
  331. grow:
  332. status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
  333. EFI_LOADER_DATA, info_sz, &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. u64 size;
  376. size = files[j].size;
  377. while (size) {
  378. u64 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, addr);
  386. if (status != EFI_SUCCESS) {
  387. efi_printk(sys_table_arg, "Failed to read file\n");
  388. goto free_file_total;
  389. }
  390. addr += chunksize;
  391. size -= chunksize;
  392. }
  393. efi_call_phys1(fh->close, files[j].handle);
  394. }
  395. }
  396. efi_call_phys1(sys_table_arg->boottime->free_pool, files);
  397. *load_addr = file_addr;
  398. *load_size = file_size_total;
  399. return status;
  400. free_file_total:
  401. efi_free(sys_table_arg, file_size_total, file_addr);
  402. close_handles:
  403. for (k = j; k < i; k++)
  404. efi_call_phys1(fh->close, files[k].handle);
  405. free_files:
  406. efi_call_phys1(sys_table_arg->boottime->free_pool, files);
  407. fail:
  408. *load_addr = 0;
  409. *load_size = 0;
  410. return status;
  411. }
  412. /*
  413. * Relocate a kernel image, either compressed or uncompressed.
  414. * In the ARM64 case, all kernel images are currently
  415. * uncompressed, and as such when we relocate it we need to
  416. * allocate additional space for the BSS segment. Any low
  417. * memory that this function should avoid needs to be
  418. * unavailable in the EFI memory map, as if the preferred
  419. * address is not available the lowest available address will
  420. * be used.
  421. */
  422. static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
  423. unsigned long *image_addr,
  424. unsigned long image_size,
  425. unsigned long alloc_size,
  426. unsigned long preferred_addr,
  427. unsigned long alignment)
  428. {
  429. unsigned long cur_image_addr;
  430. unsigned long new_addr = 0;
  431. efi_status_t status;
  432. unsigned long nr_pages;
  433. efi_physical_addr_t efi_addr = preferred_addr;
  434. if (!image_addr || !image_size || !alloc_size)
  435. return EFI_INVALID_PARAMETER;
  436. if (alloc_size < image_size)
  437. return EFI_INVALID_PARAMETER;
  438. cur_image_addr = *image_addr;
  439. /*
  440. * The EFI firmware loader could have placed the kernel image
  441. * anywhere in memory, but the kernel has restrictions on the
  442. * max physical address it can run at. Some architectures
  443. * also have a prefered address, so first try to relocate
  444. * to the preferred address. If that fails, allocate as low
  445. * as possible while respecting the required alignment.
  446. */
  447. nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  448. status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
  449. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  450. nr_pages, &efi_addr);
  451. new_addr = efi_addr;
  452. /*
  453. * If preferred address allocation failed allocate as low as
  454. * possible.
  455. */
  456. if (status != EFI_SUCCESS) {
  457. status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
  458. &new_addr);
  459. }
  460. if (status != EFI_SUCCESS) {
  461. efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
  462. return status;
  463. }
  464. /*
  465. * We know source/dest won't overlap since both memory ranges
  466. * have been allocated by UEFI, so we can safely use memcpy.
  467. */
  468. memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
  469. /* Zero any extra space we may have allocated for BSS. */
  470. memset((void *)(new_addr + image_size), alloc_size - image_size, 0);
  471. /* Return the new address of the relocated image. */
  472. *image_addr = new_addr;
  473. return status;
  474. }
  475. /*
  476. * Convert the unicode UEFI command line to ASCII to pass to kernel.
  477. * Size of memory allocated return in *cmd_line_len.
  478. * Returns NULL on error.
  479. */
  480. static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
  481. efi_loaded_image_t *image,
  482. int *cmd_line_len)
  483. {
  484. u16 *s2;
  485. u8 *s1 = NULL;
  486. unsigned long cmdline_addr = 0;
  487. int load_options_size = image->load_options_size / 2; /* ASCII */
  488. void *options = image->load_options;
  489. int options_size = 0;
  490. efi_status_t status;
  491. int i;
  492. u16 zero = 0;
  493. if (options) {
  494. s2 = options;
  495. while (*s2 && *s2 != '\n' && options_size < load_options_size) {
  496. s2++;
  497. options_size++;
  498. }
  499. }
  500. if (options_size == 0) {
  501. /* No command line options, so return empty string*/
  502. options_size = 1;
  503. options = &zero;
  504. }
  505. options_size++; /* NUL termination */
  506. #ifdef CONFIG_ARM
  507. /*
  508. * For ARM, allocate at a high address to avoid reserved
  509. * regions at low addresses that we don't know the specfics of
  510. * at the time we are processing the command line.
  511. */
  512. status = efi_high_alloc(sys_table_arg, options_size, 0,
  513. &cmdline_addr, 0xfffff000);
  514. #else
  515. status = efi_low_alloc(sys_table_arg, options_size, 0,
  516. &cmdline_addr);
  517. #endif
  518. if (status != EFI_SUCCESS)
  519. return NULL;
  520. s1 = (u8 *)cmdline_addr;
  521. s2 = (u16 *)options;
  522. for (i = 0; i < options_size - 1; i++)
  523. *s1++ = *s2++;
  524. *s1 = '\0';
  525. *cmd_line_len = options_size;
  526. return (char *)cmdline_addr;
  527. }