grub-mkimage.c 8.4 KB

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