xen_boot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2014 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/cache.h>
  19. #include <grub/charset.h>
  20. #include <grub/command.h>
  21. #include <grub/err.h>
  22. #include <grub/file.h>
  23. #include <grub/fdt.h>
  24. #include <grub/list.h>
  25. #include <grub/loader.h>
  26. #include <grub/misc.h>
  27. #include <grub/mm.h>
  28. #include <grub/types.h>
  29. #include <grub/cpu/linux.h>
  30. #include <grub/efi/efi.h>
  31. #include <grub/efi/fdtload.h>
  32. #include <grub/efi/memory.h>
  33. #include <grub/efi/pe32.h> /* required by struct xen_hypervisor_header */
  34. #include <grub/i18n.h>
  35. #include <grub/lib/cmdline.h>
  36. GRUB_MOD_LICENSE ("GPLv3+");
  37. #define XEN_HYPERVISOR_NAME "xen_hypervisor"
  38. #define MODULE_CUSTOM_COMPATIBLE "multiboot,module"
  39. /* This maximum size is defined in Power.org ePAPR V1.1
  40. * https://www.power.org/documentation/epapr-version-1-1/
  41. * 2.2.1.1 Node Name Requirements
  42. * node-name@unit-address
  43. * 31 + 1(@) + 16(64bit address in hex format) + 1(\0) = 49
  44. */
  45. #define FDT_NODE_NAME_MAX_SIZE (49)
  46. struct compat_string_struct
  47. {
  48. grub_size_t size;
  49. const char *compat_string;
  50. };
  51. typedef struct compat_string_struct compat_string_struct_t;
  52. #define FDT_COMPATIBLE(x) {.size = sizeof(x), .compat_string = (x)}
  53. enum module_type
  54. {
  55. MODULE_IMAGE,
  56. MODULE_INITRD,
  57. MODULE_XSM,
  58. MODULE_CUSTOM
  59. };
  60. typedef enum module_type module_type_t;
  61. struct xen_hypervisor_header
  62. {
  63. struct linux_arm64_kernel_header efi_head;
  64. /* This is always PE\0\0. */
  65. grub_uint8_t signature[GRUB_PE32_SIGNATURE_SIZE];
  66. /* The COFF file header. */
  67. struct grub_pe32_coff_header coff_header;
  68. /* The Optional header. */
  69. struct grub_pe64_optional_header optional_header;
  70. };
  71. struct xen_boot_binary
  72. {
  73. struct xen_boot_binary *next;
  74. struct xen_boot_binary **prev;
  75. int is_hypervisor;
  76. grub_addr_t start;
  77. grub_size_t size;
  78. grub_size_t align;
  79. char *cmdline;
  80. int cmdline_size;
  81. };
  82. static grub_dl_t my_mod;
  83. static int loaded;
  84. static struct xen_boot_binary *xen_hypervisor;
  85. static struct xen_boot_binary *module_head;
  86. static __inline grub_addr_t
  87. xen_boot_address_align (grub_addr_t start, grub_size_t align)
  88. {
  89. return (align ? (ALIGN_UP (start, align)) : start);
  90. }
  91. static grub_err_t
  92. prepare_xen_hypervisor_params (void *xen_boot_fdt)
  93. {
  94. int chosen_node = 0;
  95. int retval;
  96. chosen_node = grub_fdt_find_subnode (xen_boot_fdt, 0, "chosen");
  97. if (chosen_node < 0)
  98. chosen_node = grub_fdt_add_subnode (xen_boot_fdt, 0, "chosen");
  99. if (chosen_node < 1)
  100. return grub_error (GRUB_ERR_IO, "failed to get chosen node in FDT");
  101. /*
  102. * The address and size are always written using 64-bits value. Set
  103. * #address-cells and #size-cells accordingly.
  104. */
  105. retval = grub_fdt_set_prop32 (xen_boot_fdt, chosen_node, "#address-cells", 2);
  106. if (retval)
  107. return grub_error (GRUB_ERR_IO, "failed to set #address-cells");
  108. retval = grub_fdt_set_prop32 (xen_boot_fdt, chosen_node, "#size-cells", 2);
  109. if (retval)
  110. return grub_error (GRUB_ERR_IO, "failed to set #size-cells");
  111. grub_dprintf ("xen_loader",
  112. "Xen Hypervisor cmdline : %s @ %p size:%d\n",
  113. xen_hypervisor->cmdline, xen_hypervisor->cmdline,
  114. xen_hypervisor->cmdline_size);
  115. retval = grub_fdt_set_prop (xen_boot_fdt, chosen_node, "bootargs",
  116. xen_hypervisor->cmdline,
  117. xen_hypervisor->cmdline_size);
  118. if (retval)
  119. return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
  120. return GRUB_ERR_NONE;
  121. }
  122. static grub_err_t
  123. prepare_xen_module_params (struct xen_boot_binary *module, void *xen_boot_fdt)
  124. {
  125. int retval, chosen_node = 0, module_node = 0;
  126. char module_name[FDT_NODE_NAME_MAX_SIZE];
  127. retval = grub_snprintf (module_name, FDT_NODE_NAME_MAX_SIZE, "module@%lx",
  128. xen_boot_address_align (module->start,
  129. module->align));
  130. grub_dprintf ("xen_loader", "Module node name %s \n", module_name);
  131. if (retval < (int) sizeof ("module@"))
  132. return grub_error (GRUB_ERR_IO, N_("failed to get FDT"));
  133. chosen_node = grub_fdt_find_subnode (xen_boot_fdt, 0, "chosen");
  134. if (chosen_node < 0)
  135. chosen_node = grub_fdt_add_subnode (xen_boot_fdt, 0, "chosen");
  136. if (chosen_node < 1)
  137. return grub_error (GRUB_ERR_IO, "failed to get chosen node in FDT");
  138. module_node =
  139. grub_fdt_find_subnode (xen_boot_fdt, chosen_node, module_name);
  140. if (module_node < 0)
  141. module_node =
  142. grub_fdt_add_subnode (xen_boot_fdt, chosen_node, module_name);
  143. retval = grub_fdt_set_prop (xen_boot_fdt, module_node, "compatible",
  144. MODULE_CUSTOM_COMPATIBLE, sizeof(MODULE_CUSTOM_COMPATIBLE));
  145. if (retval)
  146. return grub_error (GRUB_ERR_IO, "failed to update FDT");
  147. grub_dprintf ("xen_loader", "Module\n");
  148. retval = grub_fdt_set_reg64 (xen_boot_fdt, module_node,
  149. xen_boot_address_align (module->start,
  150. module->align),
  151. module->size);
  152. if (retval)
  153. return grub_error (GRUB_ERR_IO, "failed to update FDT");
  154. if (module->cmdline && module->cmdline_size > 0)
  155. {
  156. grub_dprintf ("xen_loader",
  157. "Module cmdline : %s @ %p size:%d\n",
  158. module->cmdline, module->cmdline, module->cmdline_size);
  159. retval = grub_fdt_set_prop (xen_boot_fdt, module_node, "bootargs",
  160. module->cmdline, module->cmdline_size + 1);
  161. if (retval)
  162. return grub_error (GRUB_ERR_IO, "failed to update FDT");
  163. }
  164. else
  165. {
  166. grub_dprintf ("xen_loader", "Module has no bootargs!\n");
  167. }
  168. return GRUB_ERR_NONE;
  169. }
  170. static grub_err_t
  171. finalize_params_xen_boot (void)
  172. {
  173. struct xen_boot_binary *module;
  174. void *xen_boot_fdt;
  175. grub_size_t additional_size = 0x1000;
  176. /* Hypervisor. */
  177. additional_size += FDT_NODE_NAME_MAX_SIZE + xen_hypervisor->cmdline_size;
  178. FOR_LIST_ELEMENTS (module, module_head)
  179. {
  180. additional_size += 6 * FDT_NODE_NAME_MAX_SIZE + sizeof(MODULE_CUSTOM_COMPATIBLE) - 1
  181. + module->cmdline_size;
  182. }
  183. xen_boot_fdt = grub_fdt_load (additional_size);
  184. if (!xen_boot_fdt)
  185. return grub_error (GRUB_ERR_IO, "failed to get FDT");
  186. if (xen_hypervisor)
  187. {
  188. if (prepare_xen_hypervisor_params (xen_boot_fdt) != GRUB_ERR_NONE)
  189. goto fail;
  190. }
  191. else
  192. {
  193. grub_dprintf ("xen_loader", "Failed to get Xen Hypervisor info!\n");
  194. goto fail;
  195. }
  196. /* Set module params info */
  197. FOR_LIST_ELEMENTS (module, module_head)
  198. {
  199. if (module->start && module->size > 0)
  200. {
  201. grub_dprintf ("xen_loader", "Module @ 0x%lx size:0x%lx\n",
  202. xen_boot_address_align (module->start, module->align),
  203. module->size);
  204. if (prepare_xen_module_params (module, xen_boot_fdt) != GRUB_ERR_NONE)
  205. goto fail;
  206. }
  207. else
  208. {
  209. grub_dprintf ("xen_loader", "Module info error!\n");
  210. goto fail;
  211. }
  212. }
  213. if (grub_fdt_install() == GRUB_ERR_NONE)
  214. return GRUB_ERR_NONE;
  215. fail:
  216. grub_fdt_unload ();
  217. return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
  218. }
  219. static grub_err_t
  220. xen_boot (void)
  221. {
  222. grub_err_t err = finalize_params_xen_boot ();
  223. if (err)
  224. return err;
  225. return grub_arch_efi_linux_boot_image (xen_hypervisor->start,
  226. xen_hypervisor->size,
  227. xen_hypervisor->cmdline);
  228. }
  229. static void
  230. single_binary_unload (struct xen_boot_binary *binary)
  231. {
  232. if (!binary)
  233. return;
  234. if (binary->start && binary->size > 0)
  235. {
  236. grub_efi_free_pages ((grub_efi_physical_address_t) binary->start,
  237. GRUB_EFI_BYTES_TO_PAGES (binary->size + binary->align));
  238. }
  239. if (binary->cmdline && binary->cmdline_size > 0)
  240. {
  241. grub_free (binary->cmdline);
  242. grub_dprintf ("xen_loader",
  243. "Module cmdline memory free @ %p size: %d\n",
  244. binary->cmdline, binary->cmdline_size);
  245. }
  246. if (!binary->is_hypervisor)
  247. grub_list_remove (GRUB_AS_LIST (binary));
  248. grub_dprintf ("xen_loader",
  249. "Module struct memory free @ %p size: 0x%lx\n",
  250. binary, sizeof (binary));
  251. grub_free (binary);
  252. return;
  253. }
  254. static void
  255. all_binaries_unload (void)
  256. {
  257. struct xen_boot_binary *binary;
  258. FOR_LIST_ELEMENTS (binary, module_head)
  259. {
  260. single_binary_unload (binary);
  261. }
  262. if (xen_hypervisor)
  263. single_binary_unload (xen_hypervisor);
  264. return;
  265. }
  266. static grub_err_t
  267. xen_unload (void)
  268. {
  269. loaded = 0;
  270. all_binaries_unload ();
  271. grub_fdt_unload ();
  272. grub_dl_unref (my_mod);
  273. return GRUB_ERR_NONE;
  274. }
  275. static void
  276. xen_boot_binary_load (struct xen_boot_binary *binary, grub_file_t file,
  277. int argc, char *argv[])
  278. {
  279. binary->size = grub_file_size (file);
  280. grub_dprintf ("xen_loader", "Xen_boot file size: 0x%lx\n", binary->size);
  281. binary->start
  282. = (grub_addr_t) grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES
  283. (binary->size +
  284. binary->align));
  285. if (!binary->start)
  286. {
  287. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  288. return;
  289. }
  290. grub_dprintf ("xen_loader", "Xen_boot numpages: 0x%lx\n",
  291. GRUB_EFI_BYTES_TO_PAGES (binary->size + binary->align));
  292. if (grub_file_read (file, (void *) xen_boot_address_align (binary->start,
  293. binary->align),
  294. binary->size) != (grub_ssize_t) binary->size)
  295. {
  296. single_binary_unload (binary);
  297. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
  298. return;
  299. }
  300. if (argc > 1)
  301. {
  302. binary->cmdline_size = grub_loader_cmdline_size (argc - 1, argv + 1);
  303. binary->cmdline = grub_zalloc (binary->cmdline_size);
  304. if (!binary->cmdline)
  305. {
  306. single_binary_unload (binary);
  307. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  308. return;
  309. }
  310. grub_create_loader_cmdline (argc - 1, argv + 1, binary->cmdline,
  311. binary->cmdline_size,
  312. GRUB_VERIFY_KERNEL_CMDLINE);
  313. grub_dprintf ("xen_loader",
  314. "Xen_boot cmdline @ %p %s, size: %d\n",
  315. binary->cmdline, binary->cmdline, binary->cmdline_size);
  316. }
  317. else
  318. {
  319. binary->cmdline_size = 0;
  320. binary->cmdline = NULL;
  321. }
  322. grub_errno = GRUB_ERR_NONE;
  323. return;
  324. }
  325. static grub_err_t
  326. grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
  327. int argc, char *argv[])
  328. {
  329. struct xen_boot_binary *module = NULL;
  330. grub_file_t file = 0;
  331. int nounzip = 0;
  332. if (!argc)
  333. {
  334. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  335. goto fail;
  336. }
  337. if (grub_strcmp (argv[0], "--nounzip") == 0)
  338. {
  339. argv++;
  340. argc--;
  341. nounzip = 1;
  342. }
  343. if (!argc)
  344. {
  345. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  346. goto fail;
  347. }
  348. if (!loaded)
  349. {
  350. grub_error (GRUB_ERR_BAD_ARGUMENT,
  351. N_("you need to load the Xen Hypervisor first"));
  352. goto fail;
  353. }
  354. module =
  355. (struct xen_boot_binary *) grub_zalloc (sizeof (struct xen_boot_binary));
  356. if (!module)
  357. return grub_errno;
  358. module->is_hypervisor = 0;
  359. module->align = 4096;
  360. grub_dprintf ("xen_loader", "Init module and node info\n");
  361. file = grub_file_open (argv[0], GRUB_FILE_TYPE_XEN_MODULE
  362. | (nounzip ? GRUB_FILE_TYPE_NO_DECOMPRESS
  363. : GRUB_FILE_TYPE_NONE));
  364. if (!file)
  365. goto fail;
  366. xen_boot_binary_load (module, file, argc, argv);
  367. if (grub_errno == GRUB_ERR_NONE)
  368. grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
  369. fail:
  370. if (file)
  371. grub_file_close (file);
  372. if (grub_errno != GRUB_ERR_NONE)
  373. single_binary_unload (module);
  374. return grub_errno;
  375. }
  376. static grub_err_t
  377. grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
  378. int argc, char *argv[])
  379. {
  380. struct xen_hypervisor_header sh;
  381. grub_file_t file = NULL;
  382. grub_dl_ref (my_mod);
  383. if (!argc)
  384. {
  385. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  386. goto fail;
  387. }
  388. file = grub_file_open (argv[0], GRUB_FILE_TYPE_XEN_HYPERVISOR);
  389. if (!file)
  390. goto fail;
  391. if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh))
  392. goto fail;
  393. if (grub_arch_efi_linux_check_image
  394. ((struct linux_arch_kernel_header *) &sh) != GRUB_ERR_NONE)
  395. goto fail;
  396. grub_file_seek (file, 0);
  397. /* if another module has called grub_loader_set,
  398. we need to make sure that another module is unloaded properly */
  399. grub_loader_unset ();
  400. xen_hypervisor =
  401. (struct xen_boot_binary *) grub_zalloc (sizeof (struct xen_boot_binary));
  402. if (!xen_hypervisor)
  403. return grub_errno;
  404. xen_hypervisor->is_hypervisor = 1;
  405. xen_hypervisor->align = (grub_size_t) sh.optional_header.section_alignment;
  406. xen_boot_binary_load (xen_hypervisor, file, argc, argv);
  407. if (grub_errno == GRUB_ERR_NONE)
  408. {
  409. grub_loader_set (xen_boot, xen_unload, 0);
  410. loaded = 1;
  411. }
  412. fail:
  413. if (file)
  414. grub_file_close (file);
  415. if (grub_errno != GRUB_ERR_NONE)
  416. {
  417. loaded = 0;
  418. all_binaries_unload ();
  419. grub_dl_unref (my_mod);
  420. }
  421. return grub_errno;
  422. }
  423. static grub_command_t cmd_xen_hypervisor;
  424. static grub_command_t cmd_xen_module;
  425. GRUB_MOD_INIT (xen_boot)
  426. {
  427. cmd_xen_hypervisor =
  428. grub_register_command ("xen_hypervisor", grub_cmd_xen_hypervisor, 0,
  429. N_("Load a xen hypervisor."));
  430. cmd_xen_module =
  431. grub_register_command ("xen_module", grub_cmd_xen_module, 0,
  432. N_("Load a xen module."));
  433. my_mod = mod;
  434. }
  435. GRUB_MOD_FINI (xen_boot)
  436. {
  437. grub_unregister_command (cmd_xen_hypervisor);
  438. grub_unregister_command (cmd_xen_module);
  439. }