1 /* grub-setup.c - make GRUB usable */
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.
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.
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.
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/>.
26 #include <grub/types.h>
27 #include <grub/emu/misc.h>
28 #include <grub/util/misc.h>
29 #include <grub/device.h>
30 #include <grub/disk.h>
31 #include <grub/file.h>
33 #include <grub/partition.h>
35 #include <grub/emu/hostdisk.h>
36 #include <grub/term.h>
37 #include <grub/i18n.h>
38 #include <grub/crypto.h>
39 #include <grub/emu/getroot.h>
40 #include <grub/util/install.h>
42 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
43 #pragma GCC diagnostic ignored "-Wmissing-declarations"
45 #pragma GCC diagnostic error "-Wmissing-prototypes"
46 #pragma GCC diagnostic error "-Wmissing-declarations"
48 /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
51 * The 'boot' image needs to know the OBP path name of the root
52 * device. It also needs to know the initial block number of
53 * 'core' (which is 'diskboot' concatenated with 'kernel' and
54 * all the modules, this is created by grub-mkimage). This resulting
55 * 'boot' image is 512 bytes in size and is placed in the second block
58 * The initial 'diskboot' block acts as a loader for the actual GRUB
59 * kernel. It contains the loading code and then a block list.
61 * The block list of 'core' starts at the end of the 'diskboot' image
62 * and works it's way backwards towards the end of the code of 'diskboot'.
64 * We patch up the images with the necessary values and write out the
68 #define DEFAULT_BOOT_FILE "boot.img"
69 #define DEFAULT_CORE_FILE "core.img"
71 /* Non-printable "keys" for arguments with no short form.
72 * See grub-core/gnulib/argp.h for details. */
74 NO_RS_CODES_KEY
= 0x100,
77 static struct argp_option options
[] = {
78 {"boot-image", 'b', N_("FILE"), 0,
79 N_("use FILE as the boot image [default=%s]"), 0},
80 {"core-image", 'c', N_("FILE"), 0,
81 N_("use FILE as the core image [default=%s]"), 0},
82 {"directory", 'd', N_("DIR"), 0,
83 N_("use GRUB files in the directory DIR [default=%s]"), 0},
84 {"device-map", 'm', N_("FILE"), 0,
85 N_("use FILE as the device map [default=%s]"), 0},
87 N_("install even if problems are detected"), 0},
88 {"skip-fs-probe",'s',0, 0,
89 N_("do not probe for filesystems in DEVICE"), 0},
90 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
91 {"allow-floppy", 'a', 0, 0,
92 /* TRANSLATORS: The potential breakage isn't limited to floppies but it's
93 likely to make the install unbootable from HDD. */
94 N_("make the drive also bootable as floppy (default for fdX devices). May break on some BIOSes."), 0},
95 {"no-rs-codes", NO_RS_CODES_KEY
, 0, 0,
96 N_("Do not apply any reed-solomon codes when embedding core.img. "
97 "This option is only available on x86 BIOS targets."), 0},
101 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
104 help_filter (int key
, const char *text
, void *input
__attribute__ ((unused
)))
109 return xasprintf (text
, DEFAULT_BOOT_FILE
);
112 return xasprintf (text
, DEFAULT_CORE_FILE
);
115 return xasprintf (text
, DEFAULT_DIRECTORY
);
118 return xasprintf (text
, DEFAULT_DEVICE_MAP
);
121 return (char *) text
;
125 #pragma GCC diagnostic error "-Wformat-nonliteral"
141 argp_parser (int key
, char *arg
, struct argp_state
*state
)
143 /* Get the input argument from argp_parse, which we
144 know is a pointer to our arguments structure. */
145 struct arguments
*arguments
= state
->input
;
150 arguments
->allow_floppy
= 1;
154 if (arguments
->boot_file
)
155 free (arguments
->boot_file
);
157 arguments
->boot_file
= xstrdup (arg
);
161 if (arguments
->core_file
)
162 free (arguments
->core_file
);
164 arguments
->core_file
= xstrdup (arg
);
169 free (arguments
->dir
);
171 arguments
->dir
= xstrdup (arg
);
175 if (arguments
->dev_map
)
176 free (arguments
->dev_map
);
178 arguments
->dev_map
= xstrdup (arg
);
182 arguments
->force
= 1;
186 arguments
->fs_probe
= 0;
193 case NO_RS_CODES_KEY
:
194 arguments
->add_rs_codes
= 0;
198 if (state
->arg_num
== 0)
199 arguments
->device
= xstrdup(arg
);
202 /* Too many arguments. */
203 fprintf (stderr
, _("Unknown extra argument `%s'."), arg
);
204 fprintf (stderr
, "\n");
209 case ARGP_KEY_NO_ARGS
:
210 fprintf (stderr
, "%s", _("No device is specified.\n"));
216 return ARGP_ERR_UNKNOWN
;
222 static struct argp argp
= {
223 options
, argp_parser
, N_("DEVICE"),
225 Set up images to boot from DEVICE.\n\
227 You should not normally run this program directly. Use grub-install instead.")
229 DEVICE must be an OS device (e.g. /dev/sda)."),
230 NULL
, help_filter
, NULL
234 get_device_name (char *dev
)
236 size_t len
= strlen (dev
);
238 if (dev
[0] != '(' || dev
[len
- 1] != ')')
246 main (int argc
, char *argv
[])
248 char *root_dev
= NULL
;
249 char *dest_dev
= NULL
;
250 struct arguments arguments
;
252 grub_util_host_init (&argc
, &argv
);
254 /* Default option values. */
255 memset (&arguments
, 0, sizeof (struct arguments
));
256 arguments
.fs_probe
= 1;
257 arguments
.add_rs_codes
= 1;
259 /* Parse our arguments */
260 if (argp_parse (&argp
, argc
, argv
, 0, 0, &arguments
) != 0)
262 fprintf (stderr
, "%s", _("Error in parsing command line arguments\n"));
266 #ifdef GRUB_SETUP_SPARC64
271 grub_env_set ("debug", "all");
273 /* Initialize the emulated biosdisk driver. */
274 grub_util_biosdisk_init (arguments
.dev_map
? : DEFAULT_DEVICE_MAP
);
276 /* Initialize all modules. */
278 grub_gcry_init_all ();
281 grub_mdraid09_fini ();
282 grub_mdraid1x_fini ();
283 grub_diskfilter_fini ();
284 grub_diskfilter_init ();
285 grub_mdraid09_init ();
286 grub_mdraid1x_init ();
289 dest_dev
= get_device_name (arguments
.device
);
292 /* Possibly, the user specified an OS device file. */
293 dest_dev
= grub_util_get_grub_dev (arguments
.device
);
296 char *program
= xstrdup(program_name
);
297 fprintf (stderr
, _("Invalid device `%s'.\n"), arguments
.device
);
298 argp_help (&argp
, stderr
, ARGP_HELP_STD_USAGE
, program
);
302 grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
303 arguments
.device
, dest_dev
);
307 /* For simplicity. */
308 dest_dev
= xstrdup (dest_dev
);
309 grub_util_info ("Using `%s' as GRUB device", dest_dev
);
312 /* Do the real work. */
313 GRUB_SETUP_FUNC (arguments
.dir
? : DEFAULT_DIRECTORY
,
314 arguments
.boot_file
? : DEFAULT_BOOT_FILE
,
315 arguments
.core_file
? : DEFAULT_CORE_FILE
,
316 dest_dev
, arguments
.force
,
317 arguments
.fs_probe
, arguments
.allow_floppy
,
318 arguments
.add_rs_codes
);
320 /* Free resources. */
322 grub_util_biosdisk_fini ();
324 free (arguments
.boot_file
);
325 free (arguments
.core_file
);
326 free (arguments
.dir
);
327 free (arguments
.dev_map
);
328 free (arguments
.device
);