grub-mkimage.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  78. { 0, 0, 0, 0, 0, 0 }
  79. };
  80. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  81. static char *
  82. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  83. {
  84. switch (key)
  85. {
  86. case 'd':
  87. return xasprintf (text, grub_util_get_pkglibdir ());
  88. case 'O':
  89. {
  90. char *formats = grub_install_get_image_targets_string (), *ret;
  91. ret = xasprintf ("%s\n%s %s", _("generate an image in FORMAT"),
  92. _("available formats:"), formats);
  93. free (formats);
  94. return ret;
  95. }
  96. default:
  97. return (char *) text;
  98. }
  99. }
  100. #pragma GCC diagnostic error "-Wformat-nonliteral"
  101. struct arguments
  102. {
  103. size_t nmodules;
  104. size_t modules_max;
  105. char **modules;
  106. char *output;
  107. char *dir;
  108. char *prefix;
  109. char *memdisk;
  110. char *dtb;
  111. char **pubkeys;
  112. size_t npubkeys;
  113. char *font;
  114. char *config;
  115. char *sbat;
  116. int note;
  117. int disable_shim_lock;
  118. const struct grub_install_image_target_desc *image_target;
  119. grub_compression_t comp;
  120. };
  121. static error_t
  122. argp_parser (int key, char *arg, struct argp_state *state)
  123. {
  124. /* Get the input argument from argp_parse, which we
  125. know is a pointer to our arguments structure. */
  126. struct arguments *arguments = state->input;
  127. switch (key)
  128. {
  129. case 'o':
  130. if (arguments->output)
  131. free (arguments->output);
  132. arguments->output = xstrdup (arg);
  133. break;
  134. case 'O':
  135. {
  136. arguments->image_target = grub_install_get_image_target (arg);
  137. if (!arguments->image_target)
  138. {
  139. printf (_("unknown target format %s\n"), arg);
  140. argp_usage (state);
  141. exit (1);
  142. }
  143. break;
  144. }
  145. case 'd':
  146. if (arguments->dir)
  147. free (arguments->dir);
  148. arguments->dir = xstrdup (arg);
  149. break;
  150. case 'n':
  151. arguments->note = 1;
  152. break;
  153. case 'm':
  154. if (arguments->memdisk)
  155. free (arguments->memdisk);
  156. arguments->memdisk = xstrdup (arg);
  157. if (arguments->prefix)
  158. free (arguments->prefix);
  159. arguments->prefix = xstrdup ("(memdisk)/boot/grub");
  160. break;
  161. case 'D':
  162. if (arguments->dtb)
  163. free (arguments->dtb);
  164. arguments->dtb = xstrdup (arg);
  165. break;
  166. case 'k':
  167. arguments->pubkeys = xrealloc (arguments->pubkeys,
  168. sizeof (arguments->pubkeys[0])
  169. * (arguments->npubkeys + 1));
  170. arguments->pubkeys[arguments->npubkeys++] = xstrdup (arg);
  171. break;
  172. case 'c':
  173. if (arguments->config)
  174. free (arguments->config);
  175. arguments->config = xstrdup (arg);
  176. break;
  177. case 'C':
  178. if (grub_strcmp (arg, "xz") == 0)
  179. {
  180. #ifdef HAVE_LIBLZMA
  181. arguments->comp = GRUB_COMPRESSION_XZ;
  182. #else
  183. grub_util_error ("%s",
  184. _("grub-mkimage is compiled without XZ support"));
  185. #endif
  186. }
  187. else if (grub_strcmp (arg, "none") == 0)
  188. arguments->comp = GRUB_COMPRESSION_NONE;
  189. else if (grub_strcmp (arg, "auto") == 0)
  190. arguments->comp = GRUB_COMPRESSION_AUTO;
  191. else
  192. grub_util_error (_("Unknown compression format %s"), arg);
  193. break;
  194. case 'p':
  195. if (arguments->prefix)
  196. free (arguments->prefix);
  197. arguments->prefix = xstrdup (arg);
  198. break;
  199. case 's':
  200. if (arguments->sbat)
  201. free (arguments->sbat);
  202. arguments->sbat = xstrdup (arg);
  203. break;
  204. case GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK:
  205. arguments->disable_shim_lock = 1;
  206. break;
  207. case 'v':
  208. verbosity++;
  209. break;
  210. case ARGP_KEY_ARG:
  211. assert (arguments->nmodules < arguments->modules_max);
  212. arguments->modules[arguments->nmodules++] = xstrdup(arg);
  213. break;
  214. default:
  215. return ARGP_ERR_UNKNOWN;
  216. }
  217. return 0;
  218. }
  219. static struct argp argp = {
  220. options, argp_parser, N_("[OPTION]... [MODULES]"),
  221. N_("Make a bootable image of GRUB."),
  222. NULL, help_filter, NULL
  223. };
  224. int
  225. main (int argc, char *argv[])
  226. {
  227. FILE *fp = stdout;
  228. struct arguments arguments;
  229. unsigned i;
  230. grub_util_host_init (&argc, &argv);
  231. memset (&arguments, 0, sizeof (struct arguments));
  232. arguments.comp = GRUB_COMPRESSION_AUTO;
  233. arguments.modules_max = argc + 1;
  234. arguments.modules = xmalloc ((arguments.modules_max + 1)
  235. * sizeof (arguments.modules[0]));
  236. memset (arguments.modules, 0, (arguments.modules_max + 1)
  237. * sizeof (arguments.modules[0]));
  238. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  239. {
  240. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  241. exit(1);
  242. }
  243. if (!arguments.image_target)
  244. {
  245. char *program = xstrdup(program_name);
  246. printf ("%s\n", _("Target format not specified (use the -O option)."));
  247. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  248. free (program);
  249. exit(1);
  250. }
  251. if (!arguments.prefix)
  252. {
  253. char *program = xstrdup(program_name);
  254. printf ("%s\n", _("Prefix not specified (use the -p option)."));
  255. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  256. free (program);
  257. exit(1);
  258. }
  259. if (arguments.output)
  260. {
  261. fp = grub_util_fopen (arguments.output, "wb");
  262. if (! fp)
  263. grub_util_error (_("cannot open `%s': %s"), arguments.output,
  264. strerror (errno));
  265. }
  266. if (!arguments.dir)
  267. {
  268. const char *dn = grub_util_get_target_dirname (arguments.image_target);
  269. const char *pkglibdir = grub_util_get_pkglibdir ();
  270. char *ptr;
  271. arguments.dir = xmalloc (grub_strlen (pkglibdir) + grub_strlen (dn) + 2);
  272. ptr = grub_stpcpy (arguments.dir, pkglibdir);
  273. *ptr++ = '/';
  274. strcpy (ptr, dn);
  275. }
  276. grub_install_generate_image (arguments.dir, arguments.prefix, fp,
  277. arguments.output, arguments.modules,
  278. arguments.memdisk, arguments.pubkeys,
  279. arguments.npubkeys, arguments.config,
  280. arguments.image_target, arguments.note,
  281. arguments.comp, arguments.dtb,
  282. arguments.sbat, arguments.disable_shim_lock);
  283. if (grub_util_file_sync (fp) < 0)
  284. grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",
  285. strerror (errno));
  286. if (fclose (fp) == EOF)
  287. grub_util_error (_("cannot close `%s': %s"), arguments.output ? : "stdout",
  288. strerror (errno));
  289. for (i = 0; i < arguments.nmodules; i++)
  290. free (arguments.modules[i]);
  291. free (arguments.dir);
  292. free (arguments.prefix);
  293. free (arguments.modules);
  294. if (arguments.output)
  295. free (arguments.output);
  296. if (arguments.sbat)
  297. free (arguments.sbat);
  298. return 0;
  299. }