]>
git.proxmox.com Git - grub2.git/blob - util/grub-setup.c
90b9de0136e510210461c62f70791070be87ea8d
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>
44 /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
47 * The 'boot' image needs to know the OBP path name of the root
48 * device. It also needs to know the initial block number of
49 * 'core' (which is 'diskboot' concatenated with 'kernel' and
50 * all the modules, this is created by grub-mkimage). This resulting
51 * 'boot' image is 512 bytes in size and is placed in the second block
54 * The initial 'diskboot' block acts as a loader for the actual GRUB
55 * kernel. It contains the loading code and then a block list.
57 * The block list of 'core' starts at the end of the 'diskboot' image
58 * and works it's way backwards towards the end of the code of 'diskboot'.
60 * We patch up the images with the necessary values and write out the
64 #define DEFAULT_BOOT_FILE "boot.img"
65 #define DEFAULT_CORE_FILE "core.img"
67 static struct argp_option options
[] = {
68 {"boot-image", 'b', N_("FILE"), 0,
69 N_("use FILE as the boot image [default=%s]"), 0},
70 {"core-image", 'c', N_("FILE"), 0,
71 N_("use FILE as the core image [default=%s]"), 0},
72 {"directory", 'd', N_("DIR"), 0,
73 N_("use GRUB files in the directory DIR [default=%s]"), 0},
74 {"device-map", 'm', N_("FILE"), 0,
75 N_("use FILE as the device map [default=%s]"), 0},
77 N_("install even if problems are detected"), 0},
78 {"skip-fs-probe",'s',0, 0,
79 N_("do not probe for filesystems in DEVICE"), 0},
80 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
81 {"allow-floppy", 'a', 0, 0,
82 /* TRANSLATORS: The potential breakage isn't limited to floppies but it's
83 likely to make the install unbootable from HDD. */
84 N_("make the drive also bootable as floppy (default for fdX devices). May break on some BIOSes."), 0},
90 help_filter (int key
, const char *text
, void *input
__attribute__ ((unused
)))
95 return xasprintf (text
, DEFAULT_BOOT_FILE
);
98 return xasprintf (text
, DEFAULT_CORE_FILE
);
101 return xasprintf (text
, DEFAULT_DIRECTORY
);
104 return xasprintf (text
, DEFAULT_DEVICE_MAP
);
107 return (char *) text
;
124 argp_parser (int key
, char *arg
, struct argp_state
*state
)
126 /* Get the input argument from argp_parse, which we
127 know is a pointer to our arguments structure. */
128 struct arguments
*arguments
= state
->input
;
133 arguments
->allow_floppy
= 1;
137 if (arguments
->boot_file
)
138 free (arguments
->boot_file
);
140 arguments
->boot_file
= xstrdup (arg
);
144 if (arguments
->core_file
)
145 free (arguments
->core_file
);
147 arguments
->core_file
= xstrdup (arg
);
152 free (arguments
->dir
);
154 arguments
->dir
= xstrdup (arg
);
158 if (arguments
->dev_map
)
159 free (arguments
->dev_map
);
161 arguments
->dev_map
= xstrdup (arg
);
165 arguments
->force
= 1;
169 arguments
->fs_probe
= 0;
177 if (state
->arg_num
== 0)
178 arguments
->device
= xstrdup(arg
);
181 /* Too many arguments. */
182 fprintf (stderr
, _("Unknown extra argument `%s'."), arg
);
183 fprintf (stderr
, "\n");
188 case ARGP_KEY_NO_ARGS
:
189 fprintf (stderr
, "%s", _("No device is specified.\n"));
195 return ARGP_ERR_UNKNOWN
;
201 static struct argp argp
= {
202 options
, argp_parser
, N_("DEVICE"),
204 Set up images to boot from DEVICE.\n\
206 You should not normally run this program directly. Use grub-install instead.")
208 DEVICE must be an OS device (e.g. /dev/sda)."),
209 NULL
, help_filter
, NULL
213 get_device_name (char *dev
)
215 size_t len
= strlen (dev
);
217 if (dev
[0] != '(' || dev
[len
- 1] != ')')
225 main (int argc
, char *argv
[])
227 char *root_dev
= NULL
;
228 char *dest_dev
= NULL
;
229 struct arguments arguments
;
231 grub_util_host_init (&argc
, &argv
);
233 /* Default option values. */
234 memset (&arguments
, 0, sizeof (struct arguments
));
235 arguments
.fs_probe
= 1;
237 /* Parse our arguments */
238 if (argp_parse (&argp
, argc
, argv
, 0, 0, &arguments
) != 0)
240 fprintf (stderr
, "%s", _("Error in parsing command line arguments\n"));
244 #ifdef GRUB_SETUP_SPARC64
249 grub_env_set ("debug", "all");
251 /* Initialize the emulated biosdisk driver. */
252 grub_util_biosdisk_init (arguments
.dev_map
? : DEFAULT_DEVICE_MAP
);
254 /* Initialize all modules. */
256 grub_gcry_init_all ();
259 grub_mdraid09_fini ();
260 grub_mdraid1x_fini ();
261 grub_diskfilter_fini ();
262 grub_diskfilter_init ();
263 grub_mdraid09_init ();
264 grub_mdraid1x_init ();
267 dest_dev
= get_device_name (arguments
.device
);
270 /* Possibly, the user specified an OS device file. */
271 dest_dev
= grub_util_get_grub_dev (arguments
.device
);
274 char *program
= xstrdup(program_name
);
275 fprintf (stderr
, _("Invalid device `%s'.\n"), arguments
.device
);
276 argp_help (&argp
, stderr
, ARGP_HELP_STD_USAGE
, program
);
280 grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
281 arguments
.device
, dest_dev
);
285 /* For simplicity. */
286 dest_dev
= xstrdup (dest_dev
);
287 grub_util_info ("Using `%s' as GRUB device", dest_dev
);
290 /* Do the real work. */
291 GRUB_SETUP_FUNC (arguments
.dir
? : DEFAULT_DIRECTORY
,
292 arguments
.boot_file
? : DEFAULT_BOOT_FILE
,
293 arguments
.core_file
? : DEFAULT_CORE_FILE
,
294 dest_dev
, arguments
.force
,
295 arguments
.fs_probe
, arguments
.allow_floppy
);
297 /* Free resources. */
299 grub_util_biosdisk_fini ();
301 free (arguments
.boot_file
);
302 free (arguments
.core_file
);
303 free (arguments
.dir
);
304 free (arguments
.dev_map
);
305 free (arguments
.device
);