xen_boot.c 13 KB

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