efi-stub-helper.c 22 KB

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