grub-mkimage.c 9.0 KB

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