grub-mkimage.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* grub-mkimage.c - make a bootable image */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/elf.h>
  22. #include <grub/aout.h>
  23. #include <grub/i18n.h>
  24. #include <grub/kernel.h>
  25. #include <grub/disk.h>
  26. #include <grub/emu/misc.h>
  27. #include <grub/util/misc.h>
  28. #include <grub/util/resolve.h>
  29. #include <grub/misc.h>
  30. #include <grub/offsets.h>
  31. #include <grub/crypto.h>
  32. #include <grub/dl.h>
  33. #include <time.h>
  34. #include <multiboot.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <assert.h>
  40. #include <grub/efi/pe32.h>
  41. #include <grub/uboot/image.h>
  42. #include <grub/arm/reloc.h>
  43. #include <grub/ia64/reloc.h>
  44. #include <grub/osdep/hostfile.h>
  45. #include <grub/util/install.h>
  46. #include <grub/emu/config.h>
  47. #define _GNU_SOURCE 1
  48. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  49. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  50. #include <argp.h>
  51. #pragma GCC diagnostic error "-Wmissing-prototypes"
  52. #pragma GCC diagnostic error "-Wmissing-declarations"
  53. #include "progname.h"
  54. static struct argp_option options[] = {
  55. {"directory", 'd', N_("DIR"), 0,
  56. /* TRANSLATORS: platform here isn't identifier. It can be translated. */
  57. N_("use images and modules under DIR [default=%s/<platform>]"), 0},
  58. {"prefix", 'p', N_("DIR"), 0, N_("set prefix directory"), 0},
  59. {"memdisk", 'm', N_("FILE"), 0,
  60. /* TRANSLATORS: "memdisk" here isn't an identifier, it can be translated.
  61. "embed" is a verb (command description). "*/
  62. N_("embed FILE as a memdisk image\n"
  63. "Implies `-p (memdisk)/boot/grub' and overrides any prefix supplied previously,"
  64. " but the prefix itself can be overridden by later options"), 0},
  65. {"dtb", 'D', N_("FILE"), 0, N_("embed FILE as a device tree (DTB)\n"), 0},
  66. /* TRANSLATORS: "embed" is a verb (command description). "*/
  67. {"config", 'c', N_("FILE"), 0, N_("embed FILE as an early config"), 0},
  68. /* TRANSLATORS: "embed" is a verb (command description). "*/
  69. {"pubkey", 'k', N_("FILE"), 0, N_("embed FILE as public key for signature checking"), 0},
  70. /* TRANSLATORS: NOTE is a name of segment. */
  71. {"note", 'n', 0, 0, N_("add NOTE segment for CHRP IEEE1275"), 0},
  72. {"output", 'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
  73. {"format", 'O', N_("FORMAT"), 0, 0, 0},
  74. {"compression", 'C', "(xz|none|auto)", 0, N_("choose the compression to use for core image"), 0},
  75. {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0},
  76. {"disable-shim-lock", GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK, 0, 0, N_("disable shim_lock verifier"), 0},
  77. {"disable-cli", GRUB_INSTALL_OPTIONS_DISABLE_CLI, 0, 0, N_("disable command line interface access"), 0},
  78. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  79. { 0, 0, 0, 0, 0, 0 }
  80. };
  81. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  82. static char *
  83. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  84. {
  85. switch (key)
  86. {
  87. case 'd':
  88. return xasprintf (text, grub_util_get_pkglibdir ());
  89. case 'O':
  90. {
  91. char *formats = grub_install_get_image_targets_string (), *ret;
  92. ret = xasprintf ("%s\n%s %s", _("generate an image in FORMAT"),
  93. _("available formats:"), formats);
  94. free (formats);
  95. return ret;
  96. }
  97. default:
  98. return (char *) text;
  99. }
  100. }
  101. #pragma GCC diagnostic error "-Wformat-nonliteral"
  102. struct arguments
  103. {
  104. size_t nmodules;
  105. size_t modules_max;
  106. char **modules;
  107. char *output;
  108. char *dir;
  109. char *prefix;
  110. char *memdisk;
  111. char *dtb;
  112. char **pubkeys;
  113. size_t npubkeys;
  114. char *font;
  115. char *config;
  116. char *sbat;
  117. int note;
  118. int disable_shim_lock;
  119. int disable_cli;
  120. const struct grub_install_image_target_desc *image_target;
  121. grub_compression_t comp;
  122. };
  123. static error_t
  124. argp_parser (int key, char *arg, struct argp_state *state)
  125. {
  126. /* Get the input argument from argp_parse, which we
  127. know is a pointer to our arguments structure. */
  128. struct arguments *arguments = state->input;
  129. switch (key)
  130. {
  131. case 'o':
  132. if (arguments->output)
  133. free (arguments->output);
  134. arguments->output = xstrdup (arg);
  135. break;
  136. case 'O':
  137. {
  138. arguments->image_target = grub_install_get_image_target (arg);
  139. if (!arguments->image_target)
  140. {
  141. printf (_("unknown target format %s\n"), arg);
  142. argp_usage (state);
  143. exit (1);
  144. }
  145. break;
  146. }
  147. case 'd':
  148. if (arguments->dir)
  149. free (arguments->dir);
  150. arguments->dir = xstrdup (arg);
  151. break;
  152. case 'n':
  153. arguments->note = 1;
  154. break;
  155. case 'm':
  156. if (arguments->memdisk)
  157. free (arguments->memdisk);
  158. arguments->memdisk = xstrdup (arg);
  159. if (arguments->prefix)
  160. free (arguments->prefix);
  161. arguments->prefix = xstrdup ("(memdisk)/boot/grub");
  162. break;
  163. case 'D':
  164. if (arguments->dtb)
  165. free (arguments->dtb);
  166. arguments->dtb = xstrdup (arg);
  167. break;
  168. case 'k':
  169. arguments->pubkeys = xrealloc (arguments->pubkeys,
  170. sizeof (arguments->pubkeys[0])
  171. * (arguments->npubkeys + 1));
  172. arguments->pubkeys[arguments->npubkeys++] = xstrdup (arg);
  173. break;
  174. case 'c':
  175. if (arguments->config)
  176. free (arguments->config);
  177. arguments->config = xstrdup (arg);
  178. break;
  179. case 'C':
  180. if (grub_strcmp (arg, "xz") == 0)
  181. {
  182. #ifdef HAVE_LIBLZMA
  183. arguments->comp = GRUB_COMPRESSION_XZ;
  184. #else
  185. grub_util_error ("%s",
  186. _("grub-mkimage is compiled without XZ support"));
  187. #endif
  188. }
  189. else if (grub_strcmp (arg, "none") == 0)
  190. arguments->comp = GRUB_COMPRESSION_NONE;
  191. else if (grub_strcmp (arg, "auto") == 0)
  192. arguments->comp = GRUB_COMPRESSION_AUTO;
  193. else
  194. grub_util_error (_("Unknown compression format %s"), arg);
  195. break;
  196. case 'p':
  197. if (arguments->prefix)
  198. free (arguments->prefix);
  199. arguments->prefix = xstrdup (arg);
  200. break;
  201. case 's':
  202. if (arguments->sbat)
  203. free (arguments->sbat);
  204. arguments->sbat = xstrdup (arg);
  205. break;
  206. case GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK:
  207. arguments->disable_shim_lock = 1;
  208. break;
  209. case GRUB_INSTALL_OPTIONS_DISABLE_CLI:
  210. arguments->disable_cli = 1;
  211. break;
  212. case 'v':
  213. verbosity++;
  214. break;
  215. case ARGP_KEY_ARG:
  216. assert (arguments->nmodules < arguments->modules_max);
  217. arguments->modules[arguments->nmodules++] = xstrdup(arg);
  218. break;
  219. default:
  220. return ARGP_ERR_UNKNOWN;
  221. }
  222. return 0;
  223. }
  224. static struct argp argp = {
  225. options, argp_parser, N_("[OPTION]... [MODULES]"),
  226. N_("Make a bootable image of GRUB."),
  227. NULL, help_filter, NULL
  228. };
  229. int
  230. main (int argc, char *argv[])
  231. {
  232. FILE *fp = stdout;
  233. struct arguments arguments;
  234. unsigned i;
  235. grub_util_host_init (&argc, &argv);
  236. memset (&arguments, 0, sizeof (struct arguments));
  237. arguments.comp = GRUB_COMPRESSION_AUTO;
  238. arguments.modules_max = argc + 1;
  239. arguments.modules = xmalloc ((arguments.modules_max + 1)
  240. * sizeof (arguments.modules[0]));
  241. memset (arguments.modules, 0, (arguments.modules_max + 1)
  242. * sizeof (arguments.modules[0]));
  243. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  244. {
  245. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  246. exit(1);
  247. }
  248. if (!arguments.image_target)
  249. {
  250. char *program = xstrdup(program_name);
  251. printf ("%s\n", _("Target format not specified (use the -O option)."));
  252. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  253. free (program);
  254. exit(1);
  255. }
  256. if (!arguments.prefix)
  257. {
  258. char *program = xstrdup(program_name);
  259. printf ("%s\n", _("Prefix not specified (use the -p option)."));
  260. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  261. free (program);
  262. exit(1);
  263. }
  264. if (arguments.output)
  265. {
  266. fp = grub_util_fopen (arguments.output, "wb");
  267. if (! fp)
  268. grub_util_error (_("cannot open `%s': %s"), arguments.output,
  269. strerror (errno));
  270. }
  271. if (!arguments.dir)
  272. {
  273. const char *dn = grub_util_get_target_dirname (arguments.image_target);
  274. const char *pkglibdir = grub_util_get_pkglibdir ();
  275. char *ptr;
  276. arguments.dir = xmalloc (grub_strlen (pkglibdir) + grub_strlen (dn) + 2);
  277. ptr = grub_stpcpy (arguments.dir, pkglibdir);
  278. *ptr++ = '/';
  279. strcpy (ptr, dn);
  280. }
  281. grub_install_generate_image (arguments.dir, arguments.prefix, fp,
  282. arguments.output, arguments.modules,
  283. arguments.memdisk, arguments.pubkeys,
  284. arguments.npubkeys, arguments.config,
  285. arguments.image_target, arguments.note,
  286. arguments.comp, arguments.dtb,
  287. arguments.sbat, arguments.disable_shim_lock,
  288. arguments.disable_cli);
  289. if (grub_util_file_sync (fp) < 0)
  290. grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",
  291. strerror (errno));
  292. if (fclose (fp) == EOF)
  293. grub_util_error (_("cannot close `%s': %s"), arguments.output ? : "stdout",
  294. strerror (errno));
  295. for (i = 0; i < arguments.nmodules; i++)
  296. free (arguments.modules[i]);
  297. free (arguments.dir);
  298. free (arguments.prefix);
  299. free (arguments.modules);
  300. if (arguments.output)
  301. free (arguments.output);
  302. if (arguments.sbat)
  303. free (arguments.sbat);
  304. return 0;
  305. }