grub-mkrescue.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. argv[0] = xorriso;
  290. argv[1] = "-as";
  291. argv[2] = "mkisofs";
  292. argv[3] = "-help";
  293. argv[4] = NULL;
  294. pid = grub_util_exec_pipe_stderr (argv, &fd);
  295. if (!pid)
  296. return 0;
  297. /* Parent. Read mdadm's output. */
  298. mdadm = fdopen (fd, "r");
  299. if (! mdadm)
  300. return 0;
  301. while (getline (&buf, &len, mdadm) > 0)
  302. {
  303. if (grub_strstr (buf, val))
  304. ret = 1;
  305. }
  306. close (fd);
  307. waitpid (pid, NULL, 0);
  308. free (buf);
  309. return ret;
  310. }
  311. static void
  312. make_image_fwdisk (enum grub_install_plat plat,
  313. const char *mkimage_target,
  314. const char *output_sub)
  315. {
  316. char *out = grub_util_path_concat (2, boot_grub, output_sub);
  317. make_image_fwdisk_abs (plat, mkimage_target, out);
  318. free (out);
  319. }
  320. static int
  321. option_is_end (const struct argp_option *opt)
  322. {
  323. return !opt->key && !opt->name && !opt->doc && !opt->group;
  324. }
  325. static int
  326. args_to_eat (const char *arg)
  327. {
  328. int j;
  329. if (arg[0] != '-')
  330. return 0;
  331. if (arg[1] == '-')
  332. {
  333. for (j = 0; !option_is_end(&options[j]); j++)
  334. {
  335. size_t len = strlen (options[j].name);
  336. if (strncmp (arg + 2, options[j].name, len) == 0)
  337. {
  338. if (arg[2 + len] == '=')
  339. return 1;
  340. if (arg[2 + len] == '\0' && options[j].arg)
  341. return 2;
  342. if (arg[2 + len] == '\0')
  343. return 1;
  344. }
  345. }
  346. if (strcmp (arg, "--help") == 0)
  347. return 1;
  348. if (strcmp (arg, "--usage") == 0)
  349. return 1;
  350. if (strcmp (arg, "--version") == 0)
  351. return 1;
  352. return 0;
  353. }
  354. if (arg[2] && arg[3])
  355. return 0;
  356. for (j = 0; !option_is_end(&options[j]); j++)
  357. {
  358. if (options[j].key > 0 && options[j].key < 128 && arg[1] == options[j].key)
  359. {
  360. if (options[j].arg)
  361. return 2;
  362. return 1;
  363. }
  364. if (arg[1] == '?')
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. int
  370. main (int argc, char *argv[])
  371. {
  372. char *romdir;
  373. char *sysarea_img = NULL;
  374. const char *pkgdatadir;
  375. int argp_argc;
  376. char **argp_argv;
  377. int xorriso_tail_argc;
  378. char **xorriso_tail_argv;
  379. grub_util_host_init (&argc, &argv);
  380. grub_util_disable_fd_syncs ();
  381. pkgdatadir = grub_util_get_pkgdatadir ();
  382. product_name = xstrdup (PACKAGE_NAME);
  383. product_version = xstrdup (PACKAGE_VERSION);
  384. xorriso = xstrdup ("xorriso");
  385. label_font = grub_util_path_concat (2, pkgdatadir, "unicode.pf2");
  386. argp_argv = xmalloc (sizeof (argp_argv[0]) * argc);
  387. xorriso_tail_argv = xmalloc (sizeof (argp_argv[0]) * argc);
  388. xorriso_tail_argc = 0;
  389. /* Program name */
  390. argp_argv[0] = argv[0];
  391. argp_argc = 1;
  392. /* argp doesn't allow us to catch unknwon arguments,
  393. so catch them before passing to argp
  394. */
  395. {
  396. int i;
  397. for (i = 1; i < argc; i++)
  398. {
  399. if (strcmp (argv[i], "-output") == 0) {
  400. argp_argv[argp_argc++] = (char *) "--output";
  401. i++;
  402. argp_argv[argp_argc++] = argv[i];
  403. continue;
  404. }
  405. switch (args_to_eat (argv[i]))
  406. {
  407. case 2:
  408. argp_argv[argp_argc++] = argv[i++];
  409. /* Fallthrough */
  410. case 1:
  411. argp_argv[argp_argc++] = argv[i];
  412. break;
  413. case 0:
  414. xorriso_tail_argv[xorriso_tail_argc++] = argv[i];
  415. break;
  416. }
  417. }
  418. }
  419. argp_parse (&argp, argp_argc, argp_argv, 0, 0, 0);
  420. if (!output_image)
  421. grub_util_error ("%s", _("output file must be specified"));
  422. grub_init_all ();
  423. grub_hostfs_init ();
  424. grub_host_init ();
  425. xorriso_push (xorriso);
  426. xorriso_push ("-as");
  427. xorriso_push ("mkisofs");
  428. xorriso_push ("-graft-points");
  429. iso9660_dir = grub_util_make_temporary_dir ();
  430. grub_util_info ("temporary iso9660 dir is `%s'", iso9660_dir);
  431. boot_grub = grub_util_path_concat (3, iso9660_dir, "boot", "grub");
  432. grub_install_mkdir_p (boot_grub);
  433. romdir = grub_util_path_concat (2, boot_grub, "roms");
  434. grub_util_mkdir (romdir);
  435. if (!grub_install_source_directory)
  436. {
  437. const char *pkglibdir = grub_util_get_pkglibdir ();
  438. enum grub_install_plat plat;
  439. for (plat = 0; plat < GRUB_INSTALL_PLATFORM_MAX; plat++)
  440. {
  441. char *platdir = grub_util_path_concat (2, pkglibdir,
  442. grub_install_get_platform_name (plat));
  443. if (!grub_util_is_directory (platdir))
  444. {
  445. free (platdir);
  446. continue;
  447. }
  448. source_dirs[plat] = platdir;
  449. grub_install_copy_files (platdir,
  450. boot_grub, plat);
  451. }
  452. }
  453. else
  454. {
  455. enum grub_install_plat plat;
  456. plat = grub_install_get_target (grub_install_source_directory);
  457. grub_install_copy_files (grub_install_source_directory,
  458. boot_grub, plat);
  459. source_dirs[plat] = xstrdup (grub_install_source_directory);
  460. }
  461. if (system_area == SYS_AREA_AUTO || grub_install_source_directory)
  462. {
  463. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC]
  464. || source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275]
  465. || source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  466. || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
  467. || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
  468. || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
  469. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  470. system_area = SYS_AREA_COMMON;
  471. else if (source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275])
  472. system_area = SYS_AREA_SPARC;
  473. else if (source_dirs[GRUB_INSTALL_PLATFORM_MIPS_ARC])
  474. system_area = SYS_AREA_ARCS;
  475. }
  476. /* obtain date-based UUID. */
  477. {
  478. time_t tim;
  479. struct tm *tmm;
  480. tim = time (NULL);
  481. tmm = gmtime (&tim);
  482. iso_uuid = xmalloc (55);
  483. grub_snprintf (iso_uuid, 50,
  484. "%04d-%02d-%02d-%02d-%02d-%02d-00",
  485. tmm->tm_year + 1900,
  486. tmm->tm_mon + 1,
  487. tmm->tm_mday,
  488. tmm->tm_hour,
  489. tmm->tm_min,
  490. tmm->tm_sec);
  491. }
  492. {
  493. char *uuid_out = xmalloc (strlen (iso_uuid) + 1 + 40);
  494. char *optr;
  495. const char *iptr;
  496. optr = grub_stpcpy (uuid_out, "--modification-date=");
  497. for (iptr = iso_uuid; *iptr; iptr++)
  498. if (*iptr != '-')
  499. *optr++ = *iptr;
  500. *optr = '\0';
  501. xorriso_push (uuid_out);
  502. free (uuid_out);
  503. }
  504. /* build BIOS core.img. */
  505. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC])
  506. {
  507. char *load_cfg;
  508. FILE *load_cfg_f;
  509. char *output = grub_util_path_concat (3, boot_grub, "i386-pc", "eltorito.img");
  510. load_cfg = grub_util_make_temporary_file ();
  511. grub_util_info (N_("enabling %s support ..."), "BIOS");
  512. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  513. write_part (load_cfg_f, source_dirs[GRUB_INSTALL_PLATFORM_I386_PC]);
  514. fclose (load_cfg_f);
  515. grub_install_push_module ("biosdisk");
  516. grub_install_push_module ("iso9660");
  517. grub_install_make_image_wrap (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  518. "/boot/grub", output,
  519. 0, load_cfg,
  520. "i386-pc-eltorito", 0);
  521. xorriso_push ("-b");
  522. xorriso_push ("boot/grub/i386-pc/eltorito.img");
  523. xorriso_push ("-no-emul-boot");
  524. xorriso_push ("-boot-load-size");
  525. xorriso_push ("4");
  526. xorriso_push ("-boot-info-table");
  527. if (system_area == SYS_AREA_COMMON)
  528. {
  529. if (check_xorriso ("grub2-boot-info"))
  530. {
  531. char *boot_hybrid = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  532. "boot_hybrid.img");
  533. xorriso_push ("--grub2-boot-info");
  534. xorriso_push ("--grub2-mbr");
  535. xorriso_push (boot_hybrid);
  536. }
  537. else
  538. {
  539. FILE *sa, *bi;
  540. size_t sz;
  541. char buf[512];
  542. char *bin = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  543. "boot.img");
  544. grub_util_warn ("%s", _("Your xorriso doesn't support `--grub2-boot-info'. Some features are disabled. Please use xorriso 1.2.9 or later."));
  545. sysarea_img = grub_util_make_temporary_file ();
  546. sa = grub_util_fopen (sysarea_img, "wb");
  547. if (!sa)
  548. grub_util_error (_("cannot open `%s': %s"), sysarea_img,
  549. strerror (errno));
  550. bi = grub_util_fopen (bin, "rb");
  551. if (!bi)
  552. grub_util_error (_("cannot open `%s': %s"), bin,
  553. strerror (errno));
  554. if (fread (buf, 1, 512, bi) != 512)
  555. grub_util_error (_("cannot read `%s': %s"), bin,
  556. strerror (errno));
  557. fclose (bi);
  558. fwrite (buf, 1, 512, sa);
  559. grub_install_make_image_wrap_file (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC],
  560. "/boot/grub", sa, sysarea_img,
  561. 0, load_cfg,
  562. "i386-pc", 0);
  563. sz = ftello (sa);
  564. fflush (sa);
  565. grub_util_fd_sync (fileno (sa));
  566. fclose (sa);
  567. if (sz > 32768)
  568. {
  569. 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."));
  570. }
  571. else
  572. {
  573. xorriso_push ("-G");
  574. xorriso_push (sysarea_img);
  575. }
  576. }
  577. }
  578. grub_install_pop_module ();
  579. grub_install_pop_module ();
  580. grub_util_unlink (load_cfg);
  581. }
  582. /** build multiboot core.img */
  583. grub_install_push_module ("pata");
  584. grub_install_push_module ("ahci");
  585. grub_install_push_module ("at_keyboard");
  586. make_image (GRUB_INSTALL_PLATFORM_I386_MULTIBOOT, "i386-multiboot", "i386-multiboot/core.elf");
  587. grub_install_pop_module ();
  588. grub_install_pop_module ();
  589. grub_install_pop_module ();
  590. make_image_fwdisk (GRUB_INSTALL_PLATFORM_I386_IEEE1275, "i386-ieee1275", "ofwx86.elf");
  591. char *core_services = NULL;
  592. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  593. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI]
  594. || source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275])
  595. {
  596. char *mach_ker, *sv, *label, *label_text;
  597. FILE *f;
  598. core_services = grub_util_path_concat (4, iso9660_dir, "System", "Library", "CoreServices");
  599. grub_install_mkdir_p (core_services);
  600. mach_ker = grub_util_path_concat (2, iso9660_dir, "mach_kernel");
  601. f = grub_util_fopen (mach_ker, "wb");
  602. fclose (f);
  603. free (mach_ker);
  604. sv = grub_util_path_concat (2, core_services, "SystemVersion.plist");
  605. f = grub_util_fopen (sv, "wb");
  606. fprintf (f, "<plist version=\"1.0\">\n"
  607. "<dict>\n"
  608. " <key>ProductBuildVersion</key>\n"
  609. " <string></string>\n"
  610. " <key>ProductName</key>\n"
  611. " <string>%s</string>\n"
  612. " <key>ProductVersion</key>\n"
  613. " <string>%s</string>\n"
  614. "</dict>\n"
  615. "</plist>\n", product_name, product_version);
  616. fclose (f);
  617. free (sv);
  618. label = grub_util_path_concat (2, core_services, ".disk_label");
  619. char *label_string = xasprintf ("%s %s", product_name, product_version);
  620. grub_util_render_label (label_font, label_bgcolor ? : "white",
  621. label_color ? : "black", label_string, label);
  622. free (label);
  623. label_text = grub_util_path_concat (2, core_services, ".disk_label.contentDetails");
  624. f = grub_util_fopen (label_text, "wb");
  625. fprintf (f, "%s\n", label_string);
  626. fclose (f);
  627. free (label_string);
  628. free (label_text);
  629. if (system_area == SYS_AREA_COMMON)
  630. {
  631. xorriso_push ("-hfsplus");
  632. xorriso_push ("-apm-block-size");
  633. xorriso_push ("2048");
  634. xorriso_push ("-hfsplus-file-creator-type");
  635. xorriso_push ("chrp");
  636. xorriso_push ("tbxj");
  637. xorriso_push ("/System/Library/CoreServices/.disk_label");
  638. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  639. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  640. {
  641. xorriso_push ("-hfs-bless-by");
  642. xorriso_push ("i");
  643. xorriso_push ("/System/Library/CoreServices/boot.efi");
  644. }
  645. }
  646. }
  647. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  648. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI]
  649. || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
  650. || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
  651. || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI])
  652. {
  653. char *efidir = grub_util_make_temporary_dir ();
  654. char *efidir_efi = grub_util_path_concat (2, efidir, "efi");
  655. char *efidir_efi_boot = grub_util_path_concat (3, efidir, "efi", "boot");
  656. char *imgname, *img32, *img64, *img_mac = NULL;
  657. char *efiimgfat;
  658. grub_install_mkdir_p (efidir_efi_boot);
  659. grub_install_push_module ("part_gpt");
  660. grub_install_push_module ("part_msdos");
  661. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootia64.efi");
  662. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_IA64_EFI, "ia64-efi", imgname);
  663. free (imgname);
  664. grub_install_push_module ("part_apple");
  665. img64 = grub_util_path_concat (2, efidir_efi_boot, "bootx64.efi");
  666. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_X86_64_EFI, "x86_64-efi", img64);
  667. grub_install_pop_module ();
  668. grub_install_push_module ("part_apple");
  669. img32 = grub_util_path_concat (2, efidir_efi_boot, "bootia32.efi");
  670. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_I386_EFI, "i386-efi", img32);
  671. grub_install_pop_module ();
  672. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootarm.efi");
  673. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_ARM_EFI, "arm-efi", imgname);
  674. free (imgname);
  675. imgname = grub_util_path_concat (2, efidir_efi_boot, "bootaa64.efi");
  676. make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_ARM64_EFI, "arm64-efi",
  677. imgname);
  678. free (imgname);
  679. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI])
  680. {
  681. imgname = grub_util_path_concat (2, efidir_efi_boot, "boot.efi");
  682. /* For old macs. Suggested by Peter Jones. */
  683. grub_install_copy_file (img32, imgname, 1);
  684. }
  685. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  686. || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  687. img_mac = grub_util_path_concat (2, core_services, "boot.efi");
  688. if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI]
  689. && source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  690. grub_util_glue_efi (img32, img64, img_mac);
  691. else if (source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
  692. grub_install_copy_file (img64, img_mac, 1);
  693. else if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI])
  694. grub_install_copy_file (img32, img_mac, 1);
  695. free (img_mac);
  696. free (img32);
  697. free (img64);
  698. free (efidir_efi_boot);
  699. efiimgfat = grub_util_path_concat (2, iso9660_dir, "efi.img");
  700. int rv;
  701. rv = grub_util_exec ((const char * []) { "mformat", "-C", "-f", "2880", "-L", "16", "-i",
  702. efiimgfat, "::", NULL });
  703. if (rv != 0)
  704. grub_util_error ("`%s` invocation failed\n", "mformat");
  705. rv = grub_util_exec ((const char * []) { "mcopy", "-s", "-i", efiimgfat, efidir_efi, "::/", NULL });
  706. if (rv != 0)
  707. grub_util_error ("`%s` invocation failed\n", "mformat");
  708. xorriso_push ("--efi-boot");
  709. xorriso_push ("efi.img");
  710. xorriso_push ("-efi-boot-part");
  711. xorriso_push ("--efi-boot-image");
  712. grub_util_unlink_recursive (efidir);
  713. free (efiimgfat);
  714. free (efidir_efi);
  715. free (efidir);
  716. grub_install_pop_module ();
  717. grub_install_pop_module ();
  718. }
  719. grub_install_push_module ("part_apple");
  720. make_image_fwdisk (GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275, "powerpc-ieee1275", "powerpc-ieee1275/core.elf");
  721. grub_install_pop_module ();
  722. if (source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275])
  723. {
  724. char *grub_chrp = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275],
  725. "grub.chrp");
  726. char *bisrc = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275],
  727. "bootinfo.txt");
  728. char *bootx = grub_util_path_concat (2, core_services, "BootX");
  729. char *ppc_chrp = grub_util_path_concat (3, iso9660_dir, "ppc", "chrp");
  730. char *bitgt = grub_util_path_concat (3, iso9660_dir, "ppc", "bootinfo.txt");
  731. grub_install_copy_file (grub_chrp, bootx, 1);
  732. grub_install_mkdir_p (ppc_chrp);
  733. grub_install_copy_file (bisrc, bitgt, 1);
  734. xorriso_link ("/System/Library/CoreServices/grub.elf", "/boot/grub/powerpc-ieee1275/core.elf");
  735. xorriso_link ("/boot/grub/powerpc.elf", "/boot/grub/powerpc-ieee1275/core.elf");
  736. /* FIXME: add PreP */
  737. if (system_area == SYS_AREA_COMMON)
  738. {
  739. xorriso_push ("-hfsplus-file-creator-type");
  740. xorriso_push ("chrp");
  741. xorriso_push ("tbxi");
  742. xorriso_push ("/System/Library/CoreServices/BootX");
  743. xorriso_push ("-hfs-bless-by");
  744. xorriso_push ("p");
  745. xorriso_push ("/System/Library/CoreServices");
  746. }
  747. xorriso_push ("-sysid");
  748. xorriso_push ("PPC");
  749. }
  750. make_image_fwdisk (GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275,
  751. "sparc64-ieee1275-cdcore", "sparc64-ieee1275/core.img");
  752. if (source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275]
  753. && system_area == SYS_AREA_SPARC)
  754. {
  755. char *cdboot;
  756. FILE *in, *out;
  757. char buf[512];
  758. sysarea_img = grub_util_make_temporary_file ();
  759. cdboot = grub_util_path_concat (2, source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275],
  760. "cdboot.img");
  761. in = grub_util_fopen (cdboot, "rb");
  762. if (!in)
  763. grub_util_error (_("cannot open `%s': %s"), cdboot,
  764. strerror (errno));
  765. out = grub_util_fopen (sysarea_img, "wb");
  766. if (!out)
  767. grub_util_error (_("cannot open `%s': %s"), sysarea_img,
  768. strerror (errno));
  769. memset (buf, 0, 512);
  770. fwrite (buf, 1, 512, out);
  771. if (fread (buf, 1, 512, in) != 512)
  772. grub_util_error (_("cannot read `%s': %s"), cdboot,
  773. strerror (errno));
  774. fwrite (buf, 1, 512, out);
  775. fclose (in);
  776. fclose (out);
  777. xorriso_push ("-G");
  778. xorriso_push (sysarea_img);
  779. xorriso_push ("-B");
  780. xorriso_push (",");
  781. xorriso_push ("--grub2-sparc-core");
  782. xorriso_push ("/boot/grub/sparc64-ieee1275/core.img");
  783. }
  784. make_image_fwdisk (GRUB_INSTALL_PLATFORM_MIPS_ARC, "mips-arc", "mips-arc/core.img");
  785. if (source_dirs[GRUB_INSTALL_PLATFORM_MIPS_ARC])
  786. {
  787. xorriso_link ("/boot/grub/mips-arc/grub", "/boot/grub/mips-arc/core.img");
  788. xorriso_link ("/boot/grub/mips-arc/sashARCS", "/boot/grub/mips-arc/core.img");
  789. xorriso_link ("/boot/grub/mips-arc/sash", "/boot/grub/mips-arc/core.img");
  790. }
  791. if (source_dirs[GRUB_INSTALL_PLATFORM_MIPS_ARC] && system_area == SYS_AREA_ARCS)
  792. {
  793. xorriso_push ("-mips-boot");
  794. xorriso_push ("/boot/grub/mips-arc/sashARCS");
  795. xorriso_push ("-mips-boot");
  796. xorriso_push ("/boot/grub/mips-arc/sash");
  797. xorriso_push ("-mips-boot");
  798. xorriso_push ("/boot/grub/mips-arc/grub");
  799. }
  800. make_image_fwdisk (GRUB_INSTALL_PLATFORM_MIPSEL_ARC, "mipsel-arc", "arc.exe");
  801. grub_install_push_module ("pata");
  802. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS, "mipsel-qemu_mips-elf", "roms/mipsel-qemu_mips.elf");
  803. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "mipsel-loongson-elf", "loongson.elf");
  804. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "mipsel-yeeloong-flash", "mipsel-yeeloong.bin");
  805. make_image (GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "mipsel-fuloong2f-flash", "mipsel-fuloong2f.bin");
  806. make_image (GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS, "mips-qemu_mips-elf", "roms/mips-qemu_mips.elf");
  807. grub_install_push_module ("at_keyboard");
  808. make_image (GRUB_INSTALL_PLATFORM_I386_QEMU, "i386-qemu", "roms/qemu.img");
  809. grub_install_push_module ("ahci");
  810. make_image (GRUB_INSTALL_PLATFORM_I386_COREBOOT, "i386-coreboot", "roms/coreboot.elf");
  811. grub_install_pop_module ();
  812. grub_install_pop_module ();
  813. grub_install_pop_module ();
  814. if (rom_directory)
  815. {
  816. const struct
  817. {
  818. enum grub_install_plat plat;
  819. const char *from, *to;
  820. } roms[] =
  821. {
  822. {GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS, "roms/mipsel-qemu_mips.elf", "mipsel-qemu_mips.elf"},
  823. {GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "loongson.elf", "mipsel-loongson.elf"},
  824. {GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "roms/mipsel-yeeloong.bin", "mipsel-yeeloong.bin"},
  825. {GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON, "roms/mipsel-fulong.bin", "mipsel-fulong.bin"},
  826. {GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS, "roms/mips-qemu_mips.elf", "mips-qemu_mips.elf"},
  827. {GRUB_INSTALL_PLATFORM_I386_QEMU, "roms/qemu.img", "qemu.img"},
  828. {GRUB_INSTALL_PLATFORM_I386_COREBOOT, "roms/coreboot.elf", "coreboot.elf"},
  829. };
  830. grub_size_t i;
  831. for (i = 0; i < ARRAY_SIZE (roms); i++)
  832. {
  833. char *from = grub_util_path_concat (2, boot_grub, roms[i].from);
  834. char *to = grub_util_path_concat (2, rom_directory, roms[i].to);
  835. grub_install_copy_file (from, to, 0);
  836. }
  837. }
  838. xorriso_push ("--protective-msdos-label");
  839. xorriso_push ("-o");
  840. xorriso_push (output_image);
  841. xorriso_push ("-r");
  842. xorriso_push (iso9660_dir);
  843. xorriso_push ("--sort-weight");
  844. xorriso_push ("0");
  845. xorriso_push ("/");
  846. xorriso_push ("--sort-weight");
  847. xorriso_push ("1");
  848. xorriso_push ("/boot");
  849. int i;
  850. for (i = 0; i < xorriso_tail_argc; i++)
  851. xorriso_push (xorriso_tail_argv[i]);
  852. xorriso_argv[xorriso_argc] = NULL;
  853. grub_util_exec ((const char *const *)xorriso_argv);
  854. grub_util_unlink_recursive (iso9660_dir);
  855. if (sysarea_img)
  856. grub_util_unlink (sysarea_img);
  857. free (core_services);
  858. free (romdir);
  859. return 0;
  860. }