]> git.proxmox.com Git - grub2.git/blob - util/grub-setup.c
Fix an infinite loop in grub-mkconfig
[grub2.git] / util / grub-setup.c
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
20 #include <config.h>
21
22 #define _GNU_SOURCE 1
23
24 #include <string.h>
25
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>
32 #include <grub/fs.h>
33 #include <grub/partition.h>
34 #include <grub/env.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>
41
42 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
43 #pragma GCC diagnostic ignored "-Wmissing-declarations"
44 #include <argp.h>
45 #pragma GCC diagnostic error "-Wmissing-prototypes"
46 #pragma GCC diagnostic error "-Wmissing-declarations"
47
48 /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
49 * image files.
50 *
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
56 * of a partition.
57 *
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.
60 *
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'.
63 *
64 * We patch up the images with the necessary values and write out the
65 * result.
66 */
67
68 #define DEFAULT_BOOT_FILE "boot.img"
69 #define DEFAULT_CORE_FILE "core.img"
70
71 /* Non-printable "keys" for arguments with no short form.
72 * See grub-core/gnulib/argp.h for details. */
73 enum {
74 NO_RS_CODES_KEY = 0x100,
75 };
76
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},
86 {"force", 'f', 0, 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},
98 { 0, 0, 0, 0, 0, 0 }
99 };
100
101 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
102
103 static char *
104 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
105 {
106 switch (key)
107 {
108 case 'b':
109 return xasprintf (text, DEFAULT_BOOT_FILE);
110
111 case 'c':
112 return xasprintf (text, DEFAULT_CORE_FILE);
113
114 case 'd':
115 return xasprintf (text, DEFAULT_DIRECTORY);
116
117 case 'm':
118 return xasprintf (text, DEFAULT_DEVICE_MAP);
119
120 default:
121 return (char *) text;
122 }
123 }
124
125 #pragma GCC diagnostic error "-Wformat-nonliteral"
126
127 struct arguments
128 {
129 char *boot_file;
130 char *core_file;
131 char *dir;
132 char *dev_map;
133 int force;
134 int fs_probe;
135 int allow_floppy;
136 char *device;
137 int add_rs_codes;
138 };
139
140 static error_t
141 argp_parser (int key, char *arg, struct argp_state *state)
142 {
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;
146
147 switch (key)
148 {
149 case 'a':
150 arguments->allow_floppy = 1;
151 break;
152
153 case 'b':
154 if (arguments->boot_file)
155 free (arguments->boot_file);
156
157 arguments->boot_file = xstrdup (arg);
158 break;
159
160 case 'c':
161 if (arguments->core_file)
162 free (arguments->core_file);
163
164 arguments->core_file = xstrdup (arg);
165 break;
166
167 case 'd':
168 if (arguments->dir)
169 free (arguments->dir);
170
171 arguments->dir = xstrdup (arg);
172 break;
173
174 case 'm':
175 if (arguments->dev_map)
176 free (arguments->dev_map);
177
178 arguments->dev_map = xstrdup (arg);
179 break;
180
181 case 'f':
182 arguments->force = 1;
183 break;
184
185 case 's':
186 arguments->fs_probe = 0;
187 break;
188
189 case 'v':
190 verbosity++;
191 break;
192
193 case NO_RS_CODES_KEY:
194 arguments->add_rs_codes = 0;
195 break;
196
197 case ARGP_KEY_ARG:
198 if (state->arg_num == 0)
199 arguments->device = xstrdup(arg);
200 else
201 {
202 /* Too many arguments. */
203 fprintf (stderr, _("Unknown extra argument `%s'."), arg);
204 fprintf (stderr, "\n");
205 argp_usage (state);
206 }
207 break;
208
209 case ARGP_KEY_NO_ARGS:
210 fprintf (stderr, "%s", _("No device is specified.\n"));
211 argp_usage (state);
212 exit (1);
213 break;
214
215 default:
216 return ARGP_ERR_UNKNOWN;
217 }
218
219 return 0;
220 }
221
222 static struct argp argp = {
223 options, argp_parser, N_("DEVICE"),
224 "\n"N_("\
225 Set up images to boot from DEVICE.\n\
226 \n\
227 You should not normally run this program directly. Use grub-install instead.")
228 "\v"N_("\
229 DEVICE must be an OS device (e.g. /dev/sda)."),
230 NULL, help_filter, NULL
231 };
232
233 static char *
234 get_device_name (char *dev)
235 {
236 size_t len = strlen (dev);
237
238 if (dev[0] != '(' || dev[len - 1] != ')')
239 return 0;
240
241 dev[len - 1] = '\0';
242 return dev + 1;
243 }
244
245 int
246 main (int argc, char *argv[])
247 {
248 char *root_dev = NULL;
249 char *dest_dev = NULL;
250 struct arguments arguments;
251
252 grub_util_host_init (&argc, &argv);
253
254 /* Default option values. */
255 memset (&arguments, 0, sizeof (struct arguments));
256 arguments.fs_probe = 1;
257 arguments.add_rs_codes = 1;
258
259 /* Parse our arguments */
260 if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
261 {
262 fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
263 exit(1);
264 }
265
266 #ifdef GRUB_SETUP_SPARC64
267 arguments.force = 1;
268 #endif
269
270 if (verbosity > 1)
271 grub_env_set ("debug", "all");
272
273 /* Initialize the emulated biosdisk driver. */
274 grub_util_biosdisk_init (arguments.dev_map ? : DEFAULT_DEVICE_MAP);
275
276 /* Initialize all modules. */
277 grub_init_all ();
278 grub_gcry_init_all ();
279
280 grub_lvm_fini ();
281 grub_mdraid09_fini ();
282 grub_mdraid1x_fini ();
283 grub_diskfilter_fini ();
284 grub_diskfilter_init ();
285 grub_mdraid09_init ();
286 grub_mdraid1x_init ();
287 grub_lvm_init ();
288
289 dest_dev = get_device_name (arguments.device);
290 if (! dest_dev)
291 {
292 /* Possibly, the user specified an OS device file. */
293 dest_dev = grub_util_get_grub_dev (arguments.device);
294 if (! dest_dev)
295 {
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);
299 free(program);
300 exit(1);
301 }
302 grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
303 arguments.device, dest_dev);
304 }
305 else
306 {
307 /* For simplicity. */
308 dest_dev = xstrdup (dest_dev);
309 grub_util_info ("Using `%s' as GRUB device", dest_dev);
310 }
311
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);
319
320 /* Free resources. */
321 grub_fini_all ();
322 grub_util_biosdisk_fini ();
323
324 free (arguments.boot_file);
325 free (arguments.core_file);
326 free (arguments.dir);
327 free (arguments.dev_map);
328 free (arguments.device);
329 free (root_dev);
330 free (dest_dev);
331
332 return 0;
333 }