grub-setup.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* grub-setup.c - make GRUB usable */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 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. #define _GNU_SOURCE 1
  21. #include <string.h>
  22. #include <grub/types.h>
  23. #include <grub/emu/misc.h>
  24. #include <grub/util/misc.h>
  25. #include <grub/device.h>
  26. #include <grub/disk.h>
  27. #include <grub/file.h>
  28. #include <grub/fs.h>
  29. #include <grub/partition.h>
  30. #include <grub/env.h>
  31. #include <grub/emu/hostdisk.h>
  32. #include <grub/term.h>
  33. #include <grub/i18n.h>
  34. #include <grub/crypto.h>
  35. #include <grub/emu/getroot.h>
  36. #include <grub/util/install.h>
  37. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  38. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  39. #include <argp.h>
  40. #pragma GCC diagnostic error "-Wmissing-prototypes"
  41. #pragma GCC diagnostic error "-Wmissing-declarations"
  42. /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
  43. * image files.
  44. *
  45. * The 'boot' image needs to know the OBP path name of the root
  46. * device. It also needs to know the initial block number of
  47. * 'core' (which is 'diskboot' concatenated with 'kernel' and
  48. * all the modules, this is created by grub-mkimage). This resulting
  49. * 'boot' image is 512 bytes in size and is placed in the second block
  50. * of a partition.
  51. *
  52. * The initial 'diskboot' block acts as a loader for the actual GRUB
  53. * kernel. It contains the loading code and then a block list.
  54. *
  55. * The block list of 'core' starts at the end of the 'diskboot' image
  56. * and works it's way backwards towards the end of the code of 'diskboot'.
  57. *
  58. * We patch up the images with the necessary values and write out the
  59. * result.
  60. */
  61. #define DEFAULT_BOOT_FILE "boot.img"
  62. #define DEFAULT_CORE_FILE "core.img"
  63. /* Non-printable "keys" for arguments with no short form.
  64. * See grub-core/gnulib/argp.h for details. */
  65. enum {
  66. NO_RS_CODES_KEY = 0x100,
  67. };
  68. static struct argp_option options[] = {
  69. {"boot-image", 'b', N_("FILE"), 0,
  70. N_("use FILE as the boot image [default=%s]"), 0},
  71. {"core-image", 'c', N_("FILE"), 0,
  72. N_("use FILE as the core image [default=%s]"), 0},
  73. {"directory", 'd', N_("DIR"), 0,
  74. N_("use GRUB files in the directory DIR [default=%s]"), 0},
  75. {"device-map", 'm', N_("FILE"), 0,
  76. N_("use FILE as the device map [default=%s]"), 0},
  77. {"force", 'f', 0, 0,
  78. N_("install even if problems are detected"), 0},
  79. {"skip-fs-probe",'s',0, 0,
  80. N_("do not probe for filesystems in DEVICE"), 0},
  81. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  82. {"allow-floppy", 'a', 0, 0,
  83. /* TRANSLATORS: The potential breakage isn't limited to floppies but it's
  84. likely to make the install unbootable from HDD. */
  85. N_("make the drive also bootable as floppy (default for fdX devices). May break on some BIOSes."), 0},
  86. {"no-rs-codes", NO_RS_CODES_KEY, 0, 0,
  87. N_("Do not apply any reed-solomon codes when embedding core.img. "
  88. "This option is only available on x86 BIOS targets."), 0},
  89. { 0, 0, 0, 0, 0, 0 }
  90. };
  91. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  92. static char *
  93. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  94. {
  95. switch (key)
  96. {
  97. case 'b':
  98. return xasprintf (text, DEFAULT_BOOT_FILE);
  99. case 'c':
  100. return xasprintf (text, DEFAULT_CORE_FILE);
  101. case 'd':
  102. return xasprintf (text, DEFAULT_DIRECTORY);
  103. case 'm':
  104. return xasprintf (text, DEFAULT_DEVICE_MAP);
  105. default:
  106. return (char *) text;
  107. }
  108. }
  109. #pragma GCC diagnostic error "-Wformat-nonliteral"
  110. struct arguments
  111. {
  112. char *boot_file;
  113. char *core_file;
  114. char *dir;
  115. char *dev_map;
  116. int force;
  117. int fs_probe;
  118. int allow_floppy;
  119. char *device;
  120. int add_rs_codes;
  121. };
  122. static error_t
  123. argp_parser (int key, char *arg, struct argp_state *state)
  124. {
  125. /* Get the input argument from argp_parse, which we
  126. know is a pointer to our arguments structure. */
  127. struct arguments *arguments = state->input;
  128. switch (key)
  129. {
  130. case 'a':
  131. arguments->allow_floppy = 1;
  132. break;
  133. case 'b':
  134. if (arguments->boot_file)
  135. free (arguments->boot_file);
  136. arguments->boot_file = xstrdup (arg);
  137. break;
  138. case 'c':
  139. if (arguments->core_file)
  140. free (arguments->core_file);
  141. arguments->core_file = xstrdup (arg);
  142. break;
  143. case 'd':
  144. if (arguments->dir)
  145. free (arguments->dir);
  146. arguments->dir = xstrdup (arg);
  147. break;
  148. case 'm':
  149. if (arguments->dev_map)
  150. free (arguments->dev_map);
  151. arguments->dev_map = xstrdup (arg);
  152. break;
  153. case 'f':
  154. arguments->force = 1;
  155. break;
  156. case 's':
  157. arguments->fs_probe = 0;
  158. break;
  159. case 'v':
  160. verbosity++;
  161. break;
  162. case NO_RS_CODES_KEY:
  163. arguments->add_rs_codes = 0;
  164. break;
  165. case ARGP_KEY_ARG:
  166. if (state->arg_num == 0)
  167. arguments->device = xstrdup(arg);
  168. else
  169. {
  170. /* Too many arguments. */
  171. fprintf (stderr, _("Unknown extra argument `%s'."), arg);
  172. fprintf (stderr, "\n");
  173. argp_usage (state);
  174. }
  175. break;
  176. case ARGP_KEY_NO_ARGS:
  177. fprintf (stderr, "%s", _("No device is specified.\n"));
  178. argp_usage (state);
  179. exit (1);
  180. break;
  181. default:
  182. return ARGP_ERR_UNKNOWN;
  183. }
  184. return 0;
  185. }
  186. static struct argp argp = {
  187. options, argp_parser, N_("DEVICE"),
  188. "\n"N_("\
  189. Set up images to boot from DEVICE.\n\
  190. \n\
  191. You should not normally run this program directly. Use grub-install instead.")
  192. "\v"N_("\
  193. DEVICE must be an OS device (e.g. /dev/sda)."),
  194. NULL, help_filter, NULL
  195. };
  196. static char *
  197. get_device_name (char *dev)
  198. {
  199. size_t len = strlen (dev);
  200. if (dev[0] != '(' || dev[len - 1] != ')')
  201. return 0;
  202. dev[len - 1] = '\0';
  203. return dev + 1;
  204. }
  205. int
  206. main (int argc, char *argv[])
  207. {
  208. char *root_dev = NULL;
  209. char *dest_dev = NULL;
  210. struct arguments arguments;
  211. grub_util_host_init (&argc, &argv);
  212. /* Default option values. */
  213. memset (&arguments, 0, sizeof (struct arguments));
  214. arguments.fs_probe = 1;
  215. arguments.add_rs_codes = 1;
  216. /* Parse our arguments */
  217. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  218. {
  219. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  220. exit(1);
  221. }
  222. #ifdef GRUB_SETUP_SPARC64
  223. arguments.force = 1;
  224. #endif
  225. if (verbosity > 1)
  226. grub_env_set ("debug", "all");
  227. /* Initialize the emulated biosdisk driver. */
  228. grub_util_biosdisk_init (arguments.dev_map ? : DEFAULT_DEVICE_MAP);
  229. /* Initialize all modules. */
  230. grub_init_all ();
  231. grub_gcry_init_all ();
  232. grub_lvm_fini ();
  233. grub_mdraid09_fini ();
  234. grub_mdraid1x_fini ();
  235. grub_diskfilter_fini ();
  236. grub_diskfilter_init ();
  237. grub_mdraid09_init ();
  238. grub_mdraid1x_init ();
  239. grub_lvm_init ();
  240. dest_dev = get_device_name (arguments.device);
  241. if (! dest_dev)
  242. {
  243. /* Possibly, the user specified an OS device file. */
  244. dest_dev = grub_util_get_grub_dev (arguments.device);
  245. if (! dest_dev)
  246. {
  247. char *program = xstrdup(program_name);
  248. fprintf (stderr, _("Invalid device `%s'.\n"), arguments.device);
  249. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  250. free(program);
  251. exit(1);
  252. }
  253. grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
  254. arguments.device, dest_dev);
  255. }
  256. else
  257. {
  258. /* For simplicity. */
  259. dest_dev = xstrdup (dest_dev);
  260. grub_util_info ("Using `%s' as GRUB device", dest_dev);
  261. }
  262. /* Do the real work. */
  263. GRUB_SETUP_FUNC (arguments.dir ? : DEFAULT_DIRECTORY,
  264. arguments.boot_file ? : DEFAULT_BOOT_FILE,
  265. arguments.core_file ? : DEFAULT_CORE_FILE,
  266. dest_dev, arguments.force,
  267. arguments.fs_probe, arguments.allow_floppy,
  268. arguments.add_rs_codes);
  269. /* Free resources. */
  270. grub_fini_all ();
  271. grub_util_biosdisk_fini ();
  272. free (arguments.boot_file);
  273. free (arguments.core_file);
  274. free (arguments.dir);
  275. free (arguments.dev_map);
  276. free (arguments.device);
  277. free (root_dev);
  278. free (dest_dev);
  279. return 0;
  280. }