grub-mkrescue.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * Make GRUB rescue image
  3. *
  4. * GRUB -- GRand Unified Bootloader
  5. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  6. *
  7. * GRUB is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GRUB is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <grub/util/install.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/emu/exec.h>
  24. #include <grub/emu/config.h>
  25. #include <grub/emu/hostdisk.h>
  26. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  27. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  28. #include <argp.h>
  29. #pragma GCC diagnostic error "-Wmissing-prototypes"
  30. #pragma GCC diagnostic error "-Wmissing-declarations"
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <string.h>
  34. #include <time.h>
  35. static char *source_dirs[GRUB_INSTALL_PLATFORM_MAX];
  36. static char *rom_directory;
  37. static char *label_font;
  38. static char *label_color;
  39. static char *label_bgcolor;
  40. static char *product_name;
  41. static char *product_version;
  42. static char *output_image;
  43. static char *xorriso;
  44. static char *boot_grub;
  45. static int xorriso_argc;
  46. static int xorriso_arg_alloc;
  47. static char **xorriso_argv;
  48. static char *iso_uuid;
  49. static char *iso9660_dir;
  50. static void
  51. xorriso_push (const char *val)
  52. {
  53. if (xorriso_arg_alloc <= xorriso_argc + 1)
  54. {
  55. xorriso_arg_alloc = 2 * (4 + xorriso_argc);
  56. xorriso_argv = xrealloc (xorriso_argv,
  57. sizeof (xorriso_argv[0])
  58. * xorriso_arg_alloc);
  59. }
  60. xorriso_argv[xorriso_argc++] = xstrdup (val);
  61. }
  62. static void
  63. xorriso_link (const char *from, const char *to)
  64. {
  65. char *tof = grub_util_path_concat (2, iso9660_dir, to);
  66. char *val = xasprintf ("%s=%s", from, tof);
  67. xorriso_push (val);
  68. free (val);
  69. free (tof);
  70. }
  71. enum
  72. {
  73. OPTION_OUTPUT = 'o',
  74. OPTION_ROM_DIRECTORY = 0x301,
  75. OPTION_XORRISO,
  76. OPTION_GLUE_EFI,
  77. OPTION_RENDER_LABEL,
  78. OPTION_LABEL_FONT,
  79. OPTION_LABEL_COLOR,
  80. OPTION_LABEL_BGCOLOR,
  81. OPTION_PRODUCT_NAME,
  82. OPTION_PRODUCT_VERSION,
  83. OPTION_SPARC_BOOT,
  84. OPTION_ARCS_BOOT
  85. };
  86. static struct argp_option options[] = {
  87. GRUB_INSTALL_OPTIONS,
  88. {"output", 'o', N_("FILE"),
  89. 0, N_("save output in FILE [required]"), 2},
  90. {"rom-directory", OPTION_ROM_DIRECTORY, N_("DIR"),
  91. 0, N_("save ROM images in DIR [optional]"), 2},
  92. {"xorriso", OPTION_XORRISO, N_("FILE"),
  93. /* TRANSLATORS: xorriso is a program for creating ISOs and burning CDs. */
  94. 0, N_("use FILE as xorriso [optional]"), 2},
  95. {"grub-glue-efi", OPTION_GLUE_EFI, N_("FILE"), OPTION_HIDDEN, 0, 2},
  96. {"grub-render-label", OPTION_RENDER_LABEL, N_("FILE"), OPTION_HIDDEN, 0, 2},
  97. {"label-font", OPTION_LABEL_FONT, N_("FILE"), 0, N_("use FILE as font for label"), 2},
  98. {"label-color", OPTION_LABEL_COLOR, N_("COLOR"), 0, N_("use COLOR for label"), 2},
  99. {"label-bgcolor", OPTION_LABEL_BGCOLOR, N_("COLOR"), 0, N_("use COLOR for label background"), 2},
  100. {"product-name", OPTION_PRODUCT_NAME, N_("STRING"), 0, N_("use STRING as product name"), 2},
  101. {"product-version", OPTION_PRODUCT_VERSION, N_("STRING"), 0, N_("use STRING as product version"), 2},
  102. {"sparc-boot", OPTION_SPARC_BOOT, 0, 0, N_("enable sparc boot. Disables HFS+, APM, ARCS and boot as disk image for i386-pc"), 2},
  103. {"arcs-boot", OPTION_ARCS_BOOT, 0, 0, N_("enable ARCS (big-endian mips machines, mostly SGI) boot. Disables HFS+, APM, sparc64 and boot as disk image for i386-pc"), 2},
  104. {0, 0, 0, 0, 0, 0}
  105. };
  106. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  107. static char *
  108. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  109. {
  110. switch (key)
  111. {
  112. case ARGP_KEY_HELP_PRE_DOC:
  113. /* TRANSLATORS: it generates one single image which is bootable through any method. */
  114. return strdup (_("Make GRUB CD-ROM, disk, pendrive and floppy bootable image."));
  115. case ARGP_KEY_HELP_POST_DOC:
  116. {
  117. char *p1, *out;
  118. p1 = xasprintf (_("Generates a bootable CD/USB/floppy image. Arguments other than options to this program"
  119. " are passed to xorriso, and indicate source files, source directories, or any of the "
  120. "mkisofs options listed by the output of `%s'."), "xorriso -as mkisofs -help");
  121. out = xasprintf ("%s\n\n%s\n\n%s", p1,
  122. _("Option -- switches to native xorriso command mode."),
  123. _("Mail xorriso support requests to <bug-xorriso@gnu.org>."));
  124. free (p1);
  125. return out;
  126. }
  127. default:
  128. return grub_install_help_filter (key, text, input);
  129. }
  130. }
  131. #pragma GCC diagnostic error "-Wformat-nonliteral"
  132. enum {
  133. SYS_AREA_AUTO,
  134. SYS_AREA_COMMON,
  135. SYS_AREA_SPARC,
  136. SYS_AREA_ARCS
  137. } system_area = SYS_AREA_AUTO;
  138. static error_t
  139. argp_parser (int key, char *arg, struct argp_state *state)
  140. {
  141. if (grub_install_parse (key, arg))
  142. return 0;
  143. switch (key)
  144. {
  145. case OPTION_OUTPUT:
  146. free (output_image);
  147. output_image = xstrdup (arg);
  148. return 0;
  149. case OPTION_ROM_DIRECTORY:
  150. free (rom_directory);
  151. rom_directory = xstrdup (arg);
  152. return 0;
  153. /*
  154. FIXME:
  155. # Intentionally undocumented
  156. --grub-mkimage-extra)
  157. mkimage_extra_arg="$mkimage_extra_arg `argument $option "$@"`"; shift ;;
  158. --grub-mkimage-extra=*)
  159. mkimage_extra_arg="$mkimage_extra_arg `echo "$option" | sed 's/--grub-mkimage-extra=//'`" ;;
  160. */
  161. case OPTION_SPARC_BOOT:
  162. system_area = SYS_AREA_SPARC;
  163. return 0;
  164. case OPTION_ARCS_BOOT:
  165. system_area = SYS_AREA_ARCS;
  166. return 0;
  167. case OPTION_PRODUCT_NAME:
  168. free (product_name);
  169. product_name = xstrdup (arg);
  170. return 0;
  171. case OPTION_PRODUCT_VERSION:
  172. free (product_version);
  173. product_version = xstrdup (arg);
  174. return 0;
  175. /* Accept and ignore for compatibility. */
  176. case OPTION_GLUE_EFI:
  177. case OPTION_RENDER_LABEL:
  178. return 0;
  179. case OPTION_LABEL_FONT:
  180. free (label_font);
  181. label_font = xstrdup (arg);
  182. return 0;
  183. case OPTION_LABEL_COLOR:
  184. free (label_color);
  185. label_color = xstrdup (arg);
  186. return 0;
  187. case OPTION_LABEL_BGCOLOR:
  188. free (label_bgcolor);
  189. label_bgcolor = xstrdup (arg);
  190. return 0;
  191. case OPTION_XORRISO:
  192. free (xorriso);
  193. xorriso = xstrdup (arg);
  194. return 0;
  195. default:
  196. return ARGP_ERR_UNKNOWN;
  197. }
  198. }
  199. struct argp argp = {
  200. options, argp_parser, N_("[OPTION] SOURCE..."),
  201. NULL, NULL, help_filter, NULL
  202. };
  203. static void
  204. write_part (FILE *f, const char *srcdir)
  205. {
  206. FILE *in;
  207. char *inname = grub_util_path_concat (2, srcdir, "partmap.lst");
  208. char buf[260];
  209. in = grub_util_fopen (inname, "rb");
  210. if (!in)
  211. return;
  212. while (fgets (buf, 256, in))
  213. {
  214. char *ptr;
  215. for (ptr = buf + strlen (buf) - 1;
  216. ptr >= buf && (*ptr == '\n' || *ptr == '\r');
  217. ptr--);
  218. ptr[1] = '\0';
  219. fprintf (f, "insmod %s\n", buf);
  220. }
  221. fclose (in);
  222. }
  223. static void
  224. make_image_abs (enum grub_install_plat plat,
  225. const char *mkimage_target,
  226. const char *output)
  227. {
  228. char *load_cfg;
  229. FILE *load_cfg_f;
  230. if (!source_dirs[plat])
  231. return;
  232. grub_util_info (N_("enabling %s support ..."),
  233. mkimage_target);
  234. load_cfg = grub_util_make_temporary_file ();
  235. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  236. fprintf (load_cfg_f, "search --fs-uuid --set=root %s\n", iso_uuid);
  237. fprintf (load_cfg_f, "set prefix=(${root})/boot/grub\n");
  238. write_part (load_cfg_f, source_dirs[plat]);
  239. fclose (load_cfg_f);
  240. grub_install_push_module ("search");
  241. grub_install_push_module ("iso9660");
  242. grub_install_make_image_wrap (source_dirs[plat], "/boot/grub", output,
  243. 0, load_cfg,
  244. mkimage_target, 0);
  245. grub_install_pop_module ();
  246. grub_install_pop_module ();
  247. grub_util_unlink (load_cfg);
  248. }
  249. static void
  250. make_image (enum grub_install_plat plat,
  251. const char *mkimage_target,
  252. const char *output_sub)
  253. {
  254. char *out = grub_util_path_concat (2, boot_grub, output_sub);
  255. make_image_abs (plat, mkimage_target, out);
  256. free (out);
  257. }
  258. static void
  259. make_image_fwdisk_abs (enum grub_install_plat plat,
  260. const char *mkimage_target,
  261. const char *output)
  262. {
  263. char *load_cfg;
  264. FILE *load_cfg_f;
  265. if (!source_dirs[plat])
  266. return;
  267. grub_util_info (N_("enabling %s support ..."),
  268. mkimage_target);
  269. load_cfg = grub_util_make_temporary_file ();
  270. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  271. write_part (load_cfg_f, source_dirs[plat]);
  272. fclose (load_cfg_f);
  273. grub_install_push_module ("iso9660");
  274. grub_install_make_image_wrap (source_dirs[plat], "()/boot/grub", output,
  275. 0, load_cfg, mkimage_target, 0);
  276. grub_install_pop_module ();
  277. grub_util_unlink (load_cfg);
  278. }
  279. static int
  280. check_xorriso (const char *val)
  281. {
  282. const char *argv[5];
  283. int fd;
  284. pid_t pid;
  285. FILE *mdadm;
  286. char *buf = NULL;
  287. size_t len = 0;
  288. int ret = 0;
  289. int wstatus = 0;
  290. argv[0] = xorriso;
  291. argv[1] = "-as";
  292. argv[2] = "mkisofs";
  293. argv[3] = "-help";
  294. argv[4] = NULL;
  295. pid = grub_util_exec_pipe_stderr (argv, &fd);
  296. if (!pid)
  297. return 0;
  298. /* Parent. Read mdadm's output. */
  299. mdadm = fdopen (fd, "r");
  300. if (! mdadm)
  301. return 0;
  302. while (getline (&buf, &len, mdadm) > 0)
  303. {
  304. if (grub_strstr (buf, val))
  305. ret = 1;
  306. }
  307. close (fd);
  308. waitpid (pid, &wstatus, 0);
  309. free (buf);
  310. if (!WIFEXITED (wstatus) || WEXITSTATUS(wstatus) != 0)
  311. return 0;
  312. return ret;
  313. }
  314. static void
  315. make_image_fwdisk (enum grub_install_plat plat,
  316. const char *mkimage_target,
  317. const char *output_sub)
  318. {
  319. char *out = grub_util_path_concat (2, boot_grub, output_sub);
  320. make_image_fwdisk_abs (plat, mkimage_target, out);
  321. free (out);
  322. }
  323. static int
  324. option_is_end (const struct argp_option *opt)
  325. {
  326. return !opt->key && !opt->name && !opt->doc && !opt->group;
  327. }
  328. static int
  329. args_to_eat (const char *arg)
  330. {
  331. int j;
  332. if (arg[0] != '-')
  333. return 0;
  334. if (arg[1] == '-')
  335. {
  336. for (j = 0; !option_is_end(&options[j]); j++)
  337. {
  338. size_t len = strlen (options[j].name);
  339. if (strncmp (arg + 2, options[j].name, len) == 0)
  340. {
  341. if (arg[2 + len] == '=')
  342. return 1;
  343. if (arg[2 + len] == '\0' && options[j].arg)
  344. return 2;
  345. if (arg[2 + len] == '\0')
  346. return 1;
  347. }
  348. }
  349. if (strcmp (arg, "--help") == 0)
  350. return 1;
  351. if (strcmp (arg, "--usage") == 0)
  352. return 1;
  353. if (strcmp (arg, "--version") == 0)
  354. return 1;
  355. return 0;
  356. }
  357. if (arg[2] && arg[3])
  358. return 0;
  359. for (j = 0; !option_is_end(&options[j]); j++)
  360. {
  361. if (options[j].key > 0 && options[j].key < 128 && arg[1] == options[j].key)
  362. {
  363. if (options[j].arg)
  364. return 2;
  365. return 1;
  366. }
  367. if (arg[1] == '?')
  368. return 1;
  369. }
  370. return 0;
  371. }
  372. int
  373. main (int argc, char *argv[])
  374. {
  375. char *romdir;
  376. char *sysarea_img = NULL;
  377. const char *pkgdatadir;
  378. int argp_argc;
  379. char **argp_argv;
  380. int xorriso_tail_argc;
  381. char **xorriso_tail_argv;
  382. int rv;
  383. grub_util_host_init (&argc, &argv);
  384. grub_util_disable_fd_syncs ();
  385. pkgdatadir = grub_util_get_pkgdatadir ();
  386. product_name = xstrdup (PACKAGE_NAME);
  387. product_version = xstrdup (PACKAGE_VERSION);
  388. xorriso = xstrdup ("xorriso");
  389. label_font = grub_util_path_concat (2, pkgdatadir, "unicode.pf2");
  390. argp_argv = xmalloc (sizeof (argp_argv[0]) * argc);
  391. xorriso_tail_argv = xmalloc (sizeof (argp_argv[0]) * argc);
  392. xorriso_tail_argc = 0;
  393. /* Program name */
  394. argp_argv[0] = argv[0];
  395. argp_argc = 1;
  396. /* argp doesn't allow us to catch unknwon arguments,
  397. so catch them before passing to argp
  398. */
  399. {
  400. int i;
  401. for (i = 1; i < argc; i++)
  402. {
  403. if (strcmp (argv[i], "-output") == 0) {
  404. argp_argv[argp_argc++] = (char *) "--output";
  405. i++;
  406. argp_argv[argp_argc++] = argv[i];
  407. continue;
  408. }
  409. switch (args_to_eat (argv[i]))
  410. {
  411. case 2:
  412. argp_argv[argp_argc++] = argv[i++];
  413. /* Fallthrough */
  414. case 1:
  415. argp_argv[argp_argc++] = argv[i];
  416. break;
  417. case 0:
  418. xorriso_tail_argv[xorriso_tail_argc++] = argv[i];
  419. break;
  420. }
  421. }
  422. }
  423. argp_parse (&argp, argp_argc, argp_argv, 0, 0, 0);
  424. if (!output_image)
  425. grub_util_error ("%s", _("output file must be specified"));
  426. if (!check_xorriso ("graft-points")) {
  427. grub_util_error ("%s", _("xorriso not found"));
  428. }
  429. grub_init_all ();
  430. grub_hostfs_init ();
  431. grub_host_init ();
  432. xorriso_push (xorriso);
  433. xorriso_push ("-as");
  434. xorriso_push ("mkisofs");
  435. xorriso_push ("-graft-points");
  436. iso9660_dir = grub_util_make_temporary_dir ();
  437. grub_util_info ("temporary iso9660 dir is `%s'", iso9660_dir);
  438. boot_grub = grub_util_path_concat (3, iso9660_dir, "boot", "grub");
  439. grub_install_mkdir_p (boot_grub);
  440. romdir = grub_util_path_concat (2, boot_grub, "roms");
  441. grub_util_mkdir (romdir);
  442. if (!grub_install_source_directory)
  443. {
  444. const char *pkglibdir = grub_util_get_pkglibdir ();
  445. enum grub_install_plat plat;
  446. for (plat = 0; plat < GRUB_INSTALL_PLATFORM_MAX; plat++)
  447. {
  448. char *platdir = grub_util_path_concat (2, pkglibdir,
  449. grub_install_get_platform_name (plat));
  450. if (!grub_util_is_directory (platdir))
  451. {
  452. free (platdir);
  453. continue;
  454. }
  455. source_dirs[plat] = platdir;
  456. grub_install_copy_files (platdir,
  457. boot_grub, plat);
  458. }
  459. }
  460. else
  461. {
  462. enum grub_install_plat plat;
  463. plat = grub_install_get_target (grub_install_source_directory);
  464. grub_install_copy_files (grub_install_source_directory,
  465. boot_grub, plat);
  466. source_dirs[plat] = xstrdup (grub_install_source_directory);
  467. }
  468. if (system_area == SYS_AREA_AUTO || grub_install_source_directory)
  469. {
  470. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC]
  471. || source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275]
  472. || source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  473. || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
  474. || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
  475. || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
  476. || source_dirs[GRUB_INSTALL_PLATFORM_RISCV32_EFI]
  477. || source_dirs[GRUB_INSTALL_PLATFORM_RISCV64_EFI]
  478. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  479. system_area = SYS_AREA_COMMON;
  480. else if (source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275])
  481. system_area = SYS_AREA_SPARC;
  482. else if (source_dirs[GRUB_INSTALL_PLATFORM_MIPS_ARC])
  483. system_area = SYS_AREA_ARCS;
  484. }
  485. /* obtain date-based UUID. */
  486. {
  487. time_t tim;
  488. struct tm *tmm;
  489. tim = time (NULL);
  490. tmm = gmtime (&tim);
  491. iso_uuid = xmalloc (55);
  492. grub_snprintf (iso_uuid, 50,
  493. "%04d-%02d-%02d-%02d-%02d-%02d-00",
  494. tmm->tm_year + 1900,
  495. tmm->tm_mon + 1,
  496. tmm->tm_mday,
  497. tmm->tm_hour,
  498. tmm->tm_min,
  499. tmm->tm_sec);
  500. }
  501. {
  502. char *uuid_out = xmalloc (strlen (iso_uuid) + 1 + 40);
  503. char *optr;
  504. const char *iptr;
  505. optr = grub_stpcpy (uuid_out, "--modification-date=");
  506. for (iptr = iso_uuid; *iptr; iptr++)
  507. if (*iptr != '-')
  508. *optr++ = *iptr;
  509. *optr = '\0';
  510. xorriso_push (uuid_out);
  511. free (uuid_out);
  512. }
  513. /* build BIOS core.img. */
  514. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC])
  515. {
  516. char *load_cfg;
  517. FILE *load_cfg_f;
  518. char *output = grub_util_path_concat (3, boot_grub, "i386-pc", "eltorito.img");
  519. load_cfg = grub_util_make_temporary_file ();
  520. grub_util_info (N_("enabling %s support ..."), "BIOS");
  521. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  522. write_part (load_cfg_f, source_dirs[GRUB_INSTALL_PLATFORM_I386_PC]);
  523. fclose (load_cfg_f);
  524. grub_install_push_module ("biosdisk");
  525. grub_install_push_module ("iso9660");
  526. grub_install_make_image_wrap (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  527. "/boot/grub", output,
  528. 0, load_cfg,
  529. "i386-pc-eltorito", 0);
  530. xorriso_push ("-b");
  531. xorriso_push ("boot/grub/i386-pc/eltorito.img");
  532. xorriso_push ("-no-emul-boot");
  533. xorriso_push ("-boot-load-size");
  534. xorriso_push ("4");
  535. xorriso_push ("-boot-info-table");
  536. if (system_area == SYS_AREA_COMMON)
  537. {
  538. if (check_xorriso ("grub2-boot-info"))
  539. {
  540. char *boot_hybrid = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  541. "boot_hybrid.img");
  542. xorriso_push ("--grub2-boot-info");
  543. xorriso_push ("--grub2-mbr");
  544. xorriso_push (boot_hybrid);
  545. }
  546. else
  547. {
  548. FILE *sa, *bi;
  549. size_t sz;
  550. char buf[512];
  551. char *bin = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  552. "boot.img");
  553. grub_util_warn ("%s", _("Your xorriso doesn't support `--grub2-boot-info'. Some features are disabled. Please use xorriso 1.2.9 or later."));
  554. sysarea_img = grub_util_make_temporary_file ();
  555. sa = grub_util_fopen (sysarea_img, "wb");
  556. if (!sa)
  557. grub_util_error (_("cannot open `%s': %s"), sysarea_img,
  558. strerror (errno));
  559. bi = grub_util_fopen (bin, "rb");
  560. if (!bi)
  561. grub_util_error (_("cannot open `%s': %s"), bin,
  562. strerror (errno));
  563. if (fread (buf, 1, 512, bi) != 512)
  564. grub_util_error (_("cannot read `%s': %s"), bin,
  565. strerror (errno));
  566. fclose (bi);
  567. fwrite (buf, 1, 512, sa);
  568. grub_install_make_image_wrap_file (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  569. "/boot/grub", sa, sysarea_img,
  570. 0, load_cfg,
  571. "i386-pc", 0);
  572. sz = ftello (sa);
  573. fflush (sa);
  574. grub_util_fd_sync (fileno (sa));
  575. fclose (sa);
  576. if (sz > 32768)
  577. {
  578. grub_util_warn ("%s", _("Your xorriso doesn't support `--grub2-boot-info'. Your core image is too big. Boot as disk is disabled. Please use xorriso 1.2.9 or later."));
  579. }
  580. else
  581. {
  582. xorriso_push ("-G");
  583. xorriso_push (sysarea_img);
  584. }
  585. }
  586. }
  587. grub_install_pop_module ();
  588. grub_install_pop_module ();
  589. grub_util_unlink (load_cfg);
  590. }
  591. /** build multiboot core.img */
  592. grub_install_push_module ("pata");
  593. grub_install_push_module ("ahci");
  594. grub_install_push_module ("at_keyboard");
  595. make_image (GRUB_INSTALL_PLATFORM_I386_MULTIBOOT, "i386-multiboot", "i386-multiboot/core.elf");
  596. grub_install_pop_module ();
  597. grub_install_pop_module ();
  598. grub_install_pop_module ();
  599. make_image_fwdisk (GRUB_INSTALL_PLATFORM_I386_IEEE1275, "i386-ieee1275", "ofwx86.elf");
  600. char *core_services = NULL;
  601. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  602. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI]
  603. || source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275])
  604. {
  605. char *mach_ker, *sv, *label, *label_text;
  606. FILE *f;
  607. core_services = grub_util_path_concat (4, iso9660_dir, "System", "Library", "CoreServices");
  608. grub_install_mkdir_p (core_services);
  609. mach_ker = grub_util_path_concat (2, iso9660_dir, "mach_kernel");
  610. f = grub_util_fopen (mach_ker, "wb");
  611. fclose (f);
  612. free (mach_ker);
  613. sv = grub_util_path_concat (2, core_services, "SystemVersion.plist");
  614. f = grub_util_fopen (sv, "wb");
  615. fprintf (f, "<plist version=\"1.0\">\n"
  616. "<dict>\n"
  617. " <key>ProductBuildVersion</key>\n"
  618. " <string></string>\n"
  619. " <key>ProductName</key>\n"
  620. " <string>%s</string>\n"
  621. " <key>ProductVersion</key>\n"
  622. " <string>%s</string>\n"
  623. "</dict>\n"
  624. "</plist>\n", product_name, product_version);
  625. fclose (f);
  626. free (sv);
  627. label = grub_util_path_concat (2, core_services, ".disk_label");
  628. char *label_string = xasprintf ("%s %s", product_name, product_version);
  629. grub_util_render_label (label_font, label_bgcolor ? : "white",
  630. label_color ? : "black", label_string, label);
  631. free (label);
  632. label_text = grub_util_path_concat (2, core_services, ".disk_label.contentDetails");
  633. f = grub_util_fopen (label_text, "wb");
  634. fprintf (f, "%s\n", label_string);
  635. fclose (f);
  636. free (label_string);
  637. free (label_text);
  638. if (system_area == SYS_AREA_COMMON)
  639. {
  640. xorriso_push ("-hfsplus");
  641. xorriso_push ("-apm-block-size");
  642. xorriso_push ("2048");
  643. xorriso_push ("-hfsplus-file-creator-type");
  644. xorriso_push ("chrp");
  645. xorriso_push ("tbxj");
  646. xorriso_push ("/System/Library/CoreServices/.disk_label");
  647. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  648. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  649. {
  650. xorriso_push ("-hfs-bless-by");
  651. xorriso_push ("i");
  652. xorriso_push ("/System/Library/CoreServices/boot.efi");
  653. }
  654. }
  655. }
  656. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  657. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI]
  658. || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
  659. || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
  660. || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
  661. || source_dirs[GRUB_INSTALL_PLATFORM_RISCV32_EFI]
  662. || source_dirs[GRUB_INSTALL_PLATFORM_RISCV64_EFI])
  663. {
  664. char *efidir = grub_util_make_temporary_dir ();
  665. char *efidir_efi = grub_util_path_concat (2, efidir, "efi");
  666. char *efidir_efi_boot = grub_util_path_concat (3, efidir, "efi", "boot");
  667. char *imgname, *img32, *img64, *img_mac = NULL;
  668. char *efiimgfat;
  669. grub_install_mkdir_p (efidir_efi_boot);
  670. grub_install_push_module ("part_gpt");
  671. grub_install_push_module ("part_msdos");
  672. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootia64.efi");
  673. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_IA64_EFI, "ia64-efi", imgname);
  674. free (imgname);
  675. grub_install_push_module ("part_apple");
  676. img64 = grub_util_path_concat (2, efidir_efi_boot, "bootx64.efi");
  677. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_X86_64_EFI, "x86_64-efi", img64);
  678. grub_install_pop_module ();
  679. grub_install_push_module ("part_apple");
  680. img32 = grub_util_path_concat (2, efidir_efi_boot, "bootia32.efi");
  681. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_I386_EFI, "i386-efi", img32);
  682. grub_install_pop_module ();
  683. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootarm.efi");
  684. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_ARM_EFI, "arm-efi", imgname);
  685. free (imgname);
  686. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootaa64.efi");
  687. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_ARM64_EFI, "arm64-efi",
  688. imgname);
  689. free (imgname);
  690. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootriscv32.efi");
  691. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_RISCV32_EFI, "riscv32-efi",
  692. imgname);
  693. free (imgname);
  694. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootriscv64.efi");
  695. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_RISCV64_EFI, "riscv64-efi",
  696. imgname);
  697. free (imgname);
  698. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI])
  699. {
  700. imgname = grub_util_path_concat (2, efidir_efi_boot, "boot.efi");
  701. /* For old macs. Suggested by Peter Jones. */
  702. grub_install_copy_file (img32, imgname, 1);
  703. }
  704. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  705. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  706. img_mac = grub_util_path_concat (2, core_services, "boot.efi");
  707. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  708. && source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  709. grub_util_glue_efi (img32, img64, img_mac);
  710. else if (source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  711. grub_install_copy_file (img64, img_mac, 1);
  712. else if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI])
  713. grub_install_copy_file (img32, img_mac, 1);
  714. free (img_mac);
  715. free (img32);
  716. free (img64);
  717. free (efidir_efi_boot);
  718. efiimgfat = grub_util_path_concat (2, iso9660_dir, "efi.img");
  719. rv = grub_util_exec ((const char * []) { "mformat", "-C", "-f", "2880", "-L", "16", "-i",
  720. efiimgfat, "::", NULL });
  721. if (rv != 0)
  722. grub_util_error ("`%s` invocation failed\n", "mformat");
  723. rv = grub_util_exec ((const char * []) { "mcopy", "-s", "-i", efiimgfat, efidir_efi, "::/", NULL });
  724. if (rv != 0)
  725. grub_util_error ("`%s` invocation failed\n", "mformat");
  726. xorriso_push ("--efi-boot");
  727. xorriso_push ("efi.img");
  728. xorriso_push ("-efi-boot-part");
  729. xorriso_push ("--efi-boot-image");
  730. grub_util_unlink_recursive (efidir);
  731. free (efiimgfat);
  732. free (efidir_efi);
  733. free (efidir);
  734. grub_install_pop_module ();
  735. grub_install_pop_module ();
  736. }
  737. grub_install_push_module ("part_apple");
  738. make_image_fwdisk (GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275, "powerpc-ieee1275", "powerpc-ieee1275/core.elf");
  739. grub_install_pop_module ();
  740. if (source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275])
  741. {
  742. char *grub_chrp = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275],
  743. "grub.chrp");
  744. char *bisrc = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275],
  745. "bootinfo.txt");
  746. char *bootx = grub_util_path_concat (2, core_services, "BootX");
  747. char *ppc_chrp = grub_util_path_concat (3, iso9660_dir, "ppc", "chrp");
  748. char *bitgt = grub_util_path_concat (3, iso9660_dir, "ppc", "bootinfo.txt");
  749. grub_install_copy_file (grub_chrp, bootx, 1);
  750. grub_install_mkdir_p (ppc_chrp);
  751. grub_install_copy_file (bisrc, bitgt, 1);
  752. xorriso_link ("/System/Library/CoreServices/grub.elf", "/boot/grub/powerpc-ieee1275/core.elf");
  753. xorriso_link ("/boot/grub/powerpc.elf", "/boot/grub/powerpc-ieee1275/core.elf");
  754. /* FIXME: add PreP */
  755. if (system_area == SYS_AREA_COMMON)
  756. {
  757. xorriso_push ("-hfsplus-file-creator-type");
  758. xorriso_push ("chrp");
  759. xorriso_push ("tbxi");
  760. xorriso_push ("/System/Library/CoreServices/BootX");
  761. xorriso_push ("-hfs-bless-by");
  762. xorriso_push ("p");
  763. xorriso_push ("/System/Library/CoreServices");
  764. }
  765. xorriso_push ("-sysid");
  766. xorriso_push ("PPC");
  767. }
  768. make_image_fwdisk (GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275,
  769. "sparc64-ieee1275-cdcore", "sparc64-ieee1275/core.img");
  770. if (source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275]
  771. && system_area == SYS_AREA_SPARC)
  772. {
  773. char *cdboot;
  774. FILE *in, *out;
  775. char buf[512];
  776. sysarea_img = grub_util_make_temporary_file ();
  777. cdboot = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275],
  778. "cdboot.img");
  779. in = grub_util_fopen (cdboot, "rb");
  780. if (!in)
  781. grub_util_error (_("cannot open `%s': %s"), cdboot,
  782. strerror (errno));
  783. out = grub_util_fopen (sysarea_img, "wb");
  784. if (!out)
  785. grub_util_error (_("cannot open `%s': %s"), sysarea_img,
  786. strerror (errno));
  787. memset (buf, 0, 512);
  788. fwrite (buf, 1, 512, out);
  789. if (fread (buf, 1, 512, in) != 512)
  790. grub_util_error (_("cannot read `%s': %s"), cdboot,
  791. strerror (errno));
  792. fwrite (buf, 1, 512, out);
  793. fclose (in);
  794. fclose (out);
  795. xorriso_push ("-G");
  796. xorriso_push (sysarea_img);
  797. xorriso_push ("-B");
  798. xorriso_push (",");
  799. xorriso_push ("--grub2-sparc-core");
  800. xorriso_push ("/boot/grub/sparc64-ieee1275/core.img");
  801. }
  802. make_image_fwdisk (GRUB_INSTALL_PLATFORM_MIPS_ARC, "mips-arc", "mips-arc/core.img");
  803. if (source_dirs[GRUB_INSTALL_PLATFORM_MIPS_ARC])
  804. {
  805. xorriso_link ("/boot/grub/mips-arc/grub", "/boot/grub/mips-arc/core.img");
  806. xorriso_link ("/boot/grub/mips-arc/sashARCS", "/boot/grub/mips-arc/core.img");
  807. xorriso_link ("/boot/grub/mips-arc/sash", "/boot/grub/mips-arc/core.img");
  808. }
  809. if (source_dirs[GRUB_INSTALL_PLATFORM_MIPS_ARC] && system_area == SYS_AREA_ARCS)
  810. {
  811. xorriso_push ("-mips-boot");
  812. xorriso_push ("/boot/grub/mips-arc/sashARCS");
  813. xorriso_push ("-mips-boot");
  814. xorriso_push ("/boot/grub/mips-arc/sash");
  815. xorriso_push ("-mips-boot");
  816. xorriso_push ("/boot/grub/mips-arc/grub");
  817. }
  818. make_image_fwdisk (GRUB_INSTALL_PLATFORM_MIPSEL_ARC, "mipsel-arc", "arc.exe");
  819. grub_install_push_module ("pata");
  820. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS, "mipsel-qemu_mips-elf", "roms/mipsel-qemu_mips.elf");
  821. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "mipsel-loongson-elf", "loongson.elf");
  822. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "mipsel-yeeloong-flash", "mipsel-yeeloong.bin");
  823. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "mipsel-fuloong2f-flash", "mipsel-fuloong2f.bin");
  824. make_image (GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS, "mips-qemu_mips-elf", "roms/mips-qemu_mips.elf");
  825. grub_install_push_module ("at_keyboard");
  826. make_image (GRUB_INSTALL_PLATFORM_I386_QEMU, "i386-qemu", "roms/qemu.img");
  827. grub_install_push_module ("ahci");
  828. make_image (GRUB_INSTALL_PLATFORM_I386_COREBOOT, "i386-coreboot", "roms/coreboot.elf");
  829. grub_install_pop_module ();
  830. grub_install_pop_module ();
  831. grub_install_pop_module ();
  832. if (rom_directory)
  833. {
  834. const struct
  835. {
  836. enum grub_install_plat plat;
  837. const char *from, *to;
  838. } roms[] =
  839. {
  840. {GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS, "roms/mipsel-qemu_mips.elf", "mipsel-qemu_mips.elf"},
  841. {GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "loongson.elf", "mipsel-loongson.elf"},
  842. {GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "roms/mipsel-yeeloong.bin", "mipsel-yeeloong.bin"},
  843. {GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "roms/mipsel-fulong.bin", "mipsel-fulong.bin"},
  844. {GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS, "roms/mips-qemu_mips.elf", "mips-qemu_mips.elf"},
  845. {GRUB_INSTALL_PLATFORM_I386_QEMU, "roms/qemu.img", "qemu.img"},
  846. {GRUB_INSTALL_PLATFORM_I386_COREBOOT, "roms/coreboot.elf", "coreboot.elf"},
  847. };
  848. grub_size_t i;
  849. for (i = 0; i < ARRAY_SIZE (roms); i++)
  850. {
  851. char *from = grub_util_path_concat (2, boot_grub, roms[i].from);
  852. char *to = grub_util_path_concat (2, rom_directory, roms[i].to);
  853. grub_install_copy_file (from, to, 0);
  854. }
  855. }
  856. xorriso_push ("--protective-msdos-label");
  857. xorriso_push ("-o");
  858. xorriso_push (output_image);
  859. xorriso_push ("-r");
  860. xorriso_push (iso9660_dir);
  861. xorriso_push ("--sort-weight");
  862. xorriso_push ("0");
  863. xorriso_push ("/");
  864. xorriso_push ("--sort-weight");
  865. xorriso_push ("1");
  866. xorriso_push ("/boot");
  867. int i;
  868. for (i = 0; i < xorriso_tail_argc; i++)
  869. xorriso_push (xorriso_tail_argv[i]);
  870. xorriso_argv[xorriso_argc] = NULL;
  871. rv = grub_util_exec ((const char *const *)xorriso_argv);
  872. if (rv != 0)
  873. grub_util_error ("`%s` invocation failed\n", "xorriso");
  874. grub_util_unlink_recursive (iso9660_dir);
  875. if (sysarea_img)
  876. grub_util_unlink (sysarea_img);
  877. free (core_services);
  878. free (romdir);
  879. return 0;
  880. }