efi-stub-helper.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. static int __section(.data) __nokaslr;
  31. static int __section(.data) __quiet;
  32. static int __section(.data) __novamap;
  33. int __pure nokaslr(void)
  34. {
  35. return __nokaslr;
  36. }
  37. int __pure is_quiet(void)
  38. {
  39. return __quiet;
  40. }
  41. int __pure novamap(void)
  42. {
  43. return __novamap;
  44. }
  45. #define EFI_MMAP_NR_SLACK_SLOTS 8
  46. struct file_info {
  47. efi_file_handle_t *handle;
  48. u64 size;
  49. };
  50. void efi_printk(efi_system_table_t *sys_table_arg, char *str)
  51. {
  52. char *s8;
  53. for (s8 = str; *s8; s8++) {
  54. efi_char16_t ch[2] = { 0 };
  55. ch[0] = *s8;
  56. if (*s8 == '\n') {
  57. efi_char16_t nl[2] = { '\r', 0 };
  58. efi_char16_printk(sys_table_arg, nl);
  59. }
  60. efi_char16_printk(sys_table_arg, ch);
  61. }
  62. }
  63. static inline bool mmap_has_headroom(unsigned long buff_size,
  64. unsigned long map_size,
  65. unsigned long desc_size)
  66. {
  67. unsigned long slack = buff_size - map_size;
  68. return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
  69. }
  70. efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
  71. struct efi_boot_memmap *map)
  72. {
  73. efi_memory_desc_t *m = NULL;
  74. efi_status_t status;
  75. unsigned long key;
  76. u32 desc_version;
  77. *map->desc_size = sizeof(*m);
  78. *map->map_size = *map->desc_size * 32;
  79. *map->buff_size = *map->map_size;
  80. again:
  81. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  82. *map->map_size, (void **)&m);
  83. if (status != EFI_SUCCESS)
  84. goto fail;
  85. *map->desc_size = 0;
  86. key = 0;
  87. status = efi_call_early(get_memory_map, map->map_size, m,
  88. &key, map->desc_size, &desc_version);
  89. if (status == EFI_BUFFER_TOO_SMALL ||
  90. !mmap_has_headroom(*map->buff_size, *map->map_size,
  91. *map->desc_size)) {
  92. efi_call_early(free_pool, m);
  93. /*
  94. * Make sure there is some entries of headroom so that the
  95. * buffer can be reused for a new map after allocations are
  96. * no longer permitted. Its unlikely that the map will grow to
  97. * exceed this headroom once we are ready to trigger
  98. * ExitBootServices()
  99. */
  100. *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
  101. *map->buff_size = *map->map_size;
  102. goto again;
  103. }
  104. if (status != EFI_SUCCESS)
  105. efi_call_early(free_pool, m);
  106. if (map->key_ptr && status == EFI_SUCCESS)
  107. *map->key_ptr = key;
  108. if (map->desc_ver && status == EFI_SUCCESS)
  109. *map->desc_ver = desc_version;
  110. fail:
  111. *map->map = m;
  112. return status;
  113. }
  114. unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
  115. {
  116. efi_status_t status;
  117. unsigned long map_size, buff_size;
  118. unsigned long membase = EFI_ERROR;
  119. struct efi_memory_map map;
  120. efi_memory_desc_t *md;
  121. struct efi_boot_memmap boot_map;
  122. boot_map.map = (efi_memory_desc_t **)&map.map;
  123. boot_map.map_size = &map_size;
  124. boot_map.desc_size = &map.desc_size;
  125. boot_map.desc_ver = NULL;
  126. boot_map.key_ptr = NULL;
  127. boot_map.buff_size = &buff_size;
  128. status = efi_get_memory_map(sys_table_arg, &boot_map);
  129. if (status != EFI_SUCCESS)
  130. return membase;
  131. map.map_end = map.map + map_size;
  132. for_each_efi_memory_desc_in_map(&map, md) {
  133. if (md->attribute & EFI_MEMORY_WB) {
  134. if (membase > md->phys_addr)
  135. membase = md->phys_addr;
  136. }
  137. }
  138. efi_call_early(free_pool, map.map);
  139. return membase;
  140. }
  141. /*
  142. * Allocate at the highest possible address that is not above 'max'.
  143. */
  144. efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
  145. unsigned long size, unsigned long align,
  146. unsigned long *addr, unsigned long max)
  147. {
  148. unsigned long map_size, desc_size, buff_size;
  149. efi_memory_desc_t *map;
  150. efi_status_t status;
  151. unsigned long nr_pages;
  152. u64 max_addr = 0;
  153. int i;
  154. struct efi_boot_memmap boot_map;
  155. boot_map.map = &map;
  156. boot_map.map_size = &map_size;
  157. boot_map.desc_size = &desc_size;
  158. boot_map.desc_ver = NULL;
  159. boot_map.key_ptr = NULL;
  160. boot_map.buff_size = &buff_size;
  161. status = efi_get_memory_map(sys_table_arg, &boot_map);
  162. if (status != EFI_SUCCESS)
  163. goto fail;
  164. /*
  165. * Enforce minimum alignment that EFI or Linux requires when
  166. * requesting a specific address. We are doing page-based (or
  167. * larger) allocations, and both the address and size must meet
  168. * alignment constraints.
  169. */
  170. if (align < EFI_ALLOC_ALIGN)
  171. align = EFI_ALLOC_ALIGN;
  172. size = round_up(size, EFI_ALLOC_ALIGN);
  173. nr_pages = size / EFI_PAGE_SIZE;
  174. again:
  175. for (i = 0; i < map_size / desc_size; i++) {
  176. efi_memory_desc_t *desc;
  177. unsigned long m = (unsigned long)map;
  178. u64 start, end;
  179. desc = efi_early_memdesc_ptr(m, desc_size, i);
  180. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  181. continue;
  182. if (desc->num_pages < nr_pages)
  183. continue;
  184. start = desc->phys_addr;
  185. end = start + desc->num_pages * EFI_PAGE_SIZE;
  186. if (end > max)
  187. end = max;
  188. if ((start + size) > end)
  189. continue;
  190. if (round_down(end - size, align) < start)
  191. continue;
  192. start = round_down(end - size, align);
  193. /*
  194. * Don't allocate at 0x0. It will confuse code that
  195. * checks pointers against NULL.
  196. */
  197. if (start == 0x0)
  198. continue;
  199. if (start > max_addr)
  200. max_addr = start;
  201. }
  202. if (!max_addr)
  203. status = EFI_NOT_FOUND;
  204. else {
  205. status = efi_call_early(allocate_pages,
  206. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  207. nr_pages, &max_addr);
  208. if (status != EFI_SUCCESS) {
  209. max = max_addr;
  210. max_addr = 0;
  211. goto again;
  212. }
  213. *addr = max_addr;
  214. }
  215. efi_call_early(free_pool, map);
  216. fail:
  217. return status;
  218. }
  219. /*
  220. * Allocate at the lowest possible address.
  221. */
  222. efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
  223. unsigned long size, unsigned long align,
  224. unsigned long *addr)
  225. {
  226. unsigned long map_size, desc_size, buff_size;
  227. efi_memory_desc_t *map;
  228. efi_status_t status;
  229. unsigned long nr_pages;
  230. int i;
  231. struct efi_boot_memmap boot_map;
  232. boot_map.map = &map;
  233. boot_map.map_size = &map_size;
  234. boot_map.desc_size = &desc_size;
  235. boot_map.desc_ver = NULL;
  236. boot_map.key_ptr = NULL;
  237. boot_map.buff_size = &buff_size;
  238. status = efi_get_memory_map(sys_table_arg, &boot_map);
  239. if (status != EFI_SUCCESS)
  240. goto fail;
  241. /*
  242. * Enforce minimum alignment that EFI or Linux requires when
  243. * requesting a specific address. We are doing page-based (or
  244. * larger) allocations, and both the address and size must meet
  245. * alignment constraints.
  246. */
  247. if (align < EFI_ALLOC_ALIGN)
  248. align = EFI_ALLOC_ALIGN;
  249. size = round_up(size, EFI_ALLOC_ALIGN);
  250. nr_pages = size / EFI_PAGE_SIZE;
  251. for (i = 0; i < map_size / desc_size; i++) {
  252. efi_memory_desc_t *desc;
  253. unsigned long m = (unsigned long)map;
  254. u64 start, end;
  255. desc = efi_early_memdesc_ptr(m, desc_size, i);
  256. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  257. continue;
  258. if (desc->num_pages < nr_pages)
  259. continue;
  260. start = desc->phys_addr;
  261. end = start + desc->num_pages * EFI_PAGE_SIZE;
  262. /*
  263. * Don't allocate at 0x0. It will confuse code that
  264. * checks pointers against NULL. Skip the first 8
  265. * bytes so we start at a nice even number.
  266. */
  267. if (start == 0x0)
  268. start += 8;
  269. start = round_up(start, align);
  270. if ((start + size) > end)
  271. continue;
  272. status = efi_call_early(allocate_pages,
  273. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  274. nr_pages, &start);
  275. if (status == EFI_SUCCESS) {
  276. *addr = start;
  277. break;
  278. }
  279. }
  280. if (i == map_size / desc_size)
  281. status = EFI_NOT_FOUND;
  282. efi_call_early(free_pool, map);
  283. fail:
  284. return status;
  285. }
  286. void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
  287. unsigned long addr)
  288. {
  289. unsigned long nr_pages;
  290. if (!size)
  291. return;
  292. nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  293. efi_call_early(free_pages, addr, nr_pages);
  294. }
  295. static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
  296. efi_char16_t *filename_16, void **handle,
  297. u64 *file_sz)
  298. {
  299. efi_file_handle_t *h, *fh = __fh;
  300. efi_file_info_t *info;
  301. efi_status_t status;
  302. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  303. unsigned long info_sz;
  304. status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
  305. EFI_FILE_MODE_READ, (u64)0);
  306. if (status != EFI_SUCCESS) {
  307. efi_printk(sys_table_arg, "Failed to open file: ");
  308. efi_char16_printk(sys_table_arg, filename_16);
  309. efi_printk(sys_table_arg, "\n");
  310. return status;
  311. }
  312. *handle = h;
  313. info_sz = 0;
  314. status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
  315. &info_sz, NULL);
  316. if (status != EFI_BUFFER_TOO_SMALL) {
  317. efi_printk(sys_table_arg, "Failed to get file info size\n");
  318. return status;
  319. }
  320. grow:
  321. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  322. info_sz, (void **)&info);
  323. if (status != EFI_SUCCESS) {
  324. efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
  325. return status;
  326. }
  327. status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
  328. &info_sz, info);
  329. if (status == EFI_BUFFER_TOO_SMALL) {
  330. efi_call_early(free_pool, info);
  331. goto grow;
  332. }
  333. *file_sz = info->file_size;
  334. efi_call_early(free_pool, info);
  335. if (status != EFI_SUCCESS)
  336. efi_printk(sys_table_arg, "Failed to get initrd info\n");
  337. return status;
  338. }
  339. static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
  340. {
  341. return efi_call_proto(efi_file_handle, read, handle, size, addr);
  342. }
  343. static efi_status_t efi_file_close(void *handle)
  344. {
  345. return efi_call_proto(efi_file_handle, close, handle);
  346. }
  347. static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
  348. efi_loaded_image_t *image,
  349. efi_file_handle_t **__fh)
  350. {
  351. efi_file_io_interface_t *io;
  352. efi_file_handle_t *fh;
  353. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  354. efi_status_t status;
  355. void *handle = (void *)(unsigned long)efi_table_attr(efi_loaded_image,
  356. device_handle,
  357. image);
  358. status = efi_call_early(handle_protocol, handle,
  359. &fs_proto, (void **)&io);
  360. if (status != EFI_SUCCESS) {
  361. efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
  362. return status;
  363. }
  364. status = efi_call_proto(efi_file_io_interface, open_volume, io, &fh);
  365. if (status != EFI_SUCCESS)
  366. efi_printk(sys_table_arg, "Failed to open volume\n");
  367. else
  368. *__fh = fh;
  369. return status;
  370. }
  371. /*
  372. * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
  373. * option, e.g. efi=nochunk.
  374. *
  375. * It should be noted that efi= is parsed in two very different
  376. * environments, first in the early boot environment of the EFI boot
  377. * stub, and subsequently during the kernel boot.
  378. */
  379. efi_status_t efi_parse_options(char const *cmdline)
  380. {
  381. char *str;
  382. str = strstr(cmdline, "nokaslr");
  383. if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
  384. __nokaslr = 1;
  385. str = strstr(cmdline, "quiet");
  386. if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
  387. __quiet = 1;
  388. /*
  389. * If no EFI parameters were specified on the cmdline we've got
  390. * nothing to do.
  391. */
  392. str = strstr(cmdline, "efi=");
  393. if (!str)
  394. return EFI_SUCCESS;
  395. /* Skip ahead to first argument */
  396. str += strlen("efi=");
  397. /*
  398. * Remember, because efi= is also used by the kernel we need to
  399. * skip over arguments we don't understand.
  400. */
  401. while (*str && *str != ' ') {
  402. if (!strncmp(str, "nochunk", 7)) {
  403. str += strlen("nochunk");
  404. __chunk_size = -1UL;
  405. }
  406. if (!strncmp(str, "novamap", 7)) {
  407. str += strlen("novamap");
  408. __novamap = 1;
  409. }
  410. /* Group words together, delimited by "," */
  411. while (*str && *str != ' ' && *str != ',')
  412. str++;
  413. if (*str == ',')
  414. str++;
  415. }
  416. return EFI_SUCCESS;
  417. }
  418. /*
  419. * Check the cmdline for a LILO-style file= arguments.
  420. *
  421. * We only support loading a file from the same filesystem as
  422. * the kernel image.
  423. */
  424. efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
  425. efi_loaded_image_t *image,
  426. char *cmd_line, char *option_string,
  427. unsigned long max_addr,
  428. unsigned long *load_addr,
  429. unsigned long *load_size)
  430. {
  431. struct file_info *files;
  432. unsigned long file_addr;
  433. u64 file_size_total;
  434. efi_file_handle_t *fh = NULL;
  435. efi_status_t status;
  436. int nr_files;
  437. char *str;
  438. int i, j, k;
  439. file_addr = 0;
  440. file_size_total = 0;
  441. str = cmd_line;
  442. j = 0; /* See close_handles */
  443. if (!load_addr || !load_size)
  444. return EFI_INVALID_PARAMETER;
  445. *load_addr = 0;
  446. *load_size = 0;
  447. if (!str || !*str)
  448. return EFI_SUCCESS;
  449. for (nr_files = 0; *str; nr_files++) {
  450. str = strstr(str, option_string);
  451. if (!str)
  452. break;
  453. str += strlen(option_string);
  454. /* Skip any leading slashes */
  455. while (*str == '/' || *str == '\\')
  456. str++;
  457. while (*str && *str != ' ' && *str != '\n')
  458. str++;
  459. }
  460. if (!nr_files)
  461. return EFI_SUCCESS;
  462. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  463. nr_files * sizeof(*files), (void **)&files);
  464. if (status != EFI_SUCCESS) {
  465. pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
  466. goto fail;
  467. }
  468. str = cmd_line;
  469. for (i = 0; i < nr_files; i++) {
  470. struct file_info *file;
  471. efi_char16_t filename_16[256];
  472. efi_char16_t *p;
  473. str = strstr(str, option_string);
  474. if (!str)
  475. break;
  476. str += strlen(option_string);
  477. file = &files[i];
  478. p = filename_16;
  479. /* Skip any leading slashes */
  480. while (*str == '/' || *str == '\\')
  481. str++;
  482. while (*str && *str != ' ' && *str != '\n') {
  483. if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
  484. break;
  485. if (*str == '/') {
  486. *p++ = '\\';
  487. str++;
  488. } else {
  489. *p++ = *str++;
  490. }
  491. }
  492. *p = '\0';
  493. /* Only open the volume once. */
  494. if (!i) {
  495. status = efi_open_volume(sys_table_arg, image, &fh);
  496. if (status != EFI_SUCCESS)
  497. goto free_files;
  498. }
  499. status = efi_file_size(sys_table_arg, fh, filename_16,
  500. (void **)&file->handle, &file->size);
  501. if (status != EFI_SUCCESS)
  502. goto close_handles;
  503. file_size_total += file->size;
  504. }
  505. if (file_size_total) {
  506. unsigned long addr;
  507. /*
  508. * Multiple files need to be at consecutive addresses in memory,
  509. * so allocate enough memory for all the files. This is used
  510. * for loading multiple files.
  511. */
  512. status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
  513. &file_addr, max_addr);
  514. if (status != EFI_SUCCESS) {
  515. pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
  516. goto close_handles;
  517. }
  518. /* We've run out of free low memory. */
  519. if (file_addr > max_addr) {
  520. pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
  521. status = EFI_INVALID_PARAMETER;
  522. goto free_file_total;
  523. }
  524. addr = file_addr;
  525. for (j = 0; j < nr_files; j++) {
  526. unsigned long size;
  527. size = files[j].size;
  528. while (size) {
  529. unsigned long chunksize;
  530. if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
  531. chunksize = __chunk_size;
  532. else
  533. chunksize = size;
  534. status = efi_file_read(files[j].handle,
  535. &chunksize,
  536. (void *)addr);
  537. if (status != EFI_SUCCESS) {
  538. pr_efi_err(sys_table_arg, "Failed to read file\n");
  539. goto free_file_total;
  540. }
  541. addr += chunksize;
  542. size -= chunksize;
  543. }
  544. efi_file_close(files[j].handle);
  545. }
  546. }
  547. efi_call_early(free_pool, files);
  548. *load_addr = file_addr;
  549. *load_size = file_size_total;
  550. return status;
  551. free_file_total:
  552. efi_free(sys_table_arg, file_size_total, file_addr);
  553. close_handles:
  554. for (k = j; k < i; k++)
  555. efi_file_close(files[k].handle);
  556. free_files:
  557. efi_call_early(free_pool, files);
  558. fail:
  559. *load_addr = 0;
  560. *load_size = 0;
  561. return status;
  562. }
  563. /*
  564. * Relocate a kernel image, either compressed or uncompressed.
  565. * In the ARM64 case, all kernel images are currently
  566. * uncompressed, and as such when we relocate it we need to
  567. * allocate additional space for the BSS segment. Any low
  568. * memory that this function should avoid needs to be
  569. * unavailable in the EFI memory map, as if the preferred
  570. * address is not available the lowest available address will
  571. * be used.
  572. */
  573. efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
  574. unsigned long *image_addr,
  575. unsigned long image_size,
  576. unsigned long alloc_size,
  577. unsigned long preferred_addr,
  578. unsigned long alignment)
  579. {
  580. unsigned long cur_image_addr;
  581. unsigned long new_addr = 0;
  582. efi_status_t status;
  583. unsigned long nr_pages;
  584. efi_physical_addr_t efi_addr = preferred_addr;
  585. if (!image_addr || !image_size || !alloc_size)
  586. return EFI_INVALID_PARAMETER;
  587. if (alloc_size < image_size)
  588. return EFI_INVALID_PARAMETER;
  589. cur_image_addr = *image_addr;
  590. /*
  591. * The EFI firmware loader could have placed the kernel image
  592. * anywhere in memory, but the kernel has restrictions on the
  593. * max physical address it can run at. Some architectures
  594. * also have a prefered address, so first try to relocate
  595. * to the preferred address. If that fails, allocate as low
  596. * as possible while respecting the required alignment.
  597. */
  598. nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  599. status = efi_call_early(allocate_pages,
  600. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  601. nr_pages, &efi_addr);
  602. new_addr = efi_addr;
  603. /*
  604. * If preferred address allocation failed allocate as low as
  605. * possible.
  606. */
  607. if (status != EFI_SUCCESS) {
  608. status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
  609. &new_addr);
  610. }
  611. if (status != EFI_SUCCESS) {
  612. pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
  613. return status;
  614. }
  615. /*
  616. * We know source/dest won't overlap since both memory ranges
  617. * have been allocated by UEFI, so we can safely use memcpy.
  618. */
  619. memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
  620. /* Return the new address of the relocated image. */
  621. *image_addr = new_addr;
  622. return status;
  623. }
  624. /*
  625. * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
  626. * This overestimates for surrogates, but that is okay.
  627. */
  628. static int efi_utf8_bytes(u16 c)
  629. {
  630. return 1 + (c >= 0x80) + (c >= 0x800);
  631. }
  632. /*
  633. * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
  634. */
  635. static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
  636. {
  637. unsigned int c;
  638. while (n--) {
  639. c = *src++;
  640. if (n && c >= 0xd800 && c <= 0xdbff &&
  641. *src >= 0xdc00 && *src <= 0xdfff) {
  642. c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
  643. src++;
  644. n--;
  645. }
  646. if (c >= 0xd800 && c <= 0xdfff)
  647. c = 0xfffd; /* Unmatched surrogate */
  648. if (c < 0x80) {
  649. *dst++ = c;
  650. continue;
  651. }
  652. if (c < 0x800) {
  653. *dst++ = 0xc0 + (c >> 6);
  654. goto t1;
  655. }
  656. if (c < 0x10000) {
  657. *dst++ = 0xe0 + (c >> 12);
  658. goto t2;
  659. }
  660. *dst++ = 0xf0 + (c >> 18);
  661. *dst++ = 0x80 + ((c >> 12) & 0x3f);
  662. t2:
  663. *dst++ = 0x80 + ((c >> 6) & 0x3f);
  664. t1:
  665. *dst++ = 0x80 + (c & 0x3f);
  666. }
  667. return dst;
  668. }
  669. #ifndef MAX_CMDLINE_ADDRESS
  670. #define MAX_CMDLINE_ADDRESS ULONG_MAX
  671. #endif
  672. /*
  673. * Convert the unicode UEFI command line to ASCII to pass to kernel.
  674. * Size of memory allocated return in *cmd_line_len.
  675. * Returns NULL on error.
  676. */
  677. char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
  678. efi_loaded_image_t *image,
  679. int *cmd_line_len)
  680. {
  681. const u16 *s2;
  682. u8 *s1 = NULL;
  683. unsigned long cmdline_addr = 0;
  684. int load_options_chars = image->load_options_size / 2; /* UTF-16 */
  685. const u16 *options = image->load_options;
  686. int options_bytes = 0; /* UTF-8 bytes */
  687. int options_chars = 0; /* UTF-16 chars */
  688. efi_status_t status;
  689. u16 zero = 0;
  690. if (options) {
  691. s2 = options;
  692. while (*s2 && *s2 != '\n'
  693. && options_chars < load_options_chars) {
  694. options_bytes += efi_utf8_bytes(*s2++);
  695. options_chars++;
  696. }
  697. }
  698. if (!options_chars) {
  699. /* No command line options, so return empty string*/
  700. options = &zero;
  701. }
  702. options_bytes++; /* NUL termination */
  703. status = efi_high_alloc(sys_table_arg, options_bytes, 0,
  704. &cmdline_addr, MAX_CMDLINE_ADDRESS);
  705. if (status != EFI_SUCCESS)
  706. return NULL;
  707. s1 = (u8 *)cmdline_addr;
  708. s2 = (const u16 *)options;
  709. s1 = efi_utf16_to_utf8(s1, s2, options_chars);
  710. *s1 = '\0';
  711. *cmd_line_len = options_bytes;
  712. return (char *)cmdline_addr;
  713. }
  714. /*
  715. * Handle calling ExitBootServices according to the requirements set out by the
  716. * spec. Obtains the current memory map, and returns that info after calling
  717. * ExitBootServices. The client must specify a function to perform any
  718. * processing of the memory map data prior to ExitBootServices. A client
  719. * specific structure may be passed to the function via priv. The client
  720. * function may be called multiple times.
  721. */
  722. efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
  723. void *handle,
  724. struct efi_boot_memmap *map,
  725. void *priv,
  726. efi_exit_boot_map_processing priv_func)
  727. {
  728. efi_status_t status;
  729. status = efi_get_memory_map(sys_table_arg, map);
  730. if (status != EFI_SUCCESS)
  731. goto fail;
  732. status = priv_func(sys_table_arg, map, priv);
  733. if (status != EFI_SUCCESS)
  734. goto free_map;
  735. status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
  736. if (status == EFI_INVALID_PARAMETER) {
  737. /*
  738. * The memory map changed between efi_get_memory_map() and
  739. * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
  740. * EFI_BOOT_SERVICES.ExitBootServices we need to get the
  741. * updated map, and try again. The spec implies one retry
  742. * should be sufficent, which is confirmed against the EDK2
  743. * implementation. Per the spec, we can only invoke
  744. * get_memory_map() and exit_boot_services() - we cannot alloc
  745. * so efi_get_memory_map() cannot be used, and we must reuse
  746. * the buffer. For all practical purposes, the headroom in the
  747. * buffer should account for any changes in the map so the call
  748. * to get_memory_map() is expected to succeed here.
  749. */
  750. *map->map_size = *map->buff_size;
  751. status = efi_call_early(get_memory_map,
  752. map->map_size,
  753. *map->map,
  754. map->key_ptr,
  755. map->desc_size,
  756. map->desc_ver);
  757. /* exit_boot_services() was called, thus cannot free */
  758. if (status != EFI_SUCCESS)
  759. goto fail;
  760. status = priv_func(sys_table_arg, map, priv);
  761. /* exit_boot_services() was called, thus cannot free */
  762. if (status != EFI_SUCCESS)
  763. goto fail;
  764. status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
  765. }
  766. /* exit_boot_services() was called, thus cannot free */
  767. if (status != EFI_SUCCESS)
  768. goto fail;
  769. return EFI_SUCCESS;
  770. free_map:
  771. efi_call_early(free_pool, *map->map);
  772. fail:
  773. return status;
  774. }