Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-setup.c
blob993b0206801af6aab5344af34dccfa4458d9fb62
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.
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/>.
20 #include <config.h>
22 #define _GNU_SOURCE 1
24 #include <string.h>
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>
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"
48 /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
49 * image files.
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.
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
65 * result.
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. */
73 enum {
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},
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 }
101 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
103 static char *
104 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
106 switch (key)
108 case 'b':
109 return xasprintf (text, DEFAULT_BOOT_FILE);
111 case 'c':
112 return xasprintf (text, DEFAULT_CORE_FILE);
114 case 'd':
115 return xasprintf (text, DEFAULT_DIRECTORY);
117 case 'm':
118 return xasprintf (text, DEFAULT_DEVICE_MAP);
120 default:
121 return (char *) text;
125 #pragma GCC diagnostic error "-Wformat-nonliteral"
127 struct arguments
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;
140 static error_t
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;
147 switch (key)
149 case 'a':
150 arguments->allow_floppy = 1;
151 break;
153 case 'b':
154 if (arguments->boot_file)
155 free (arguments->boot_file);
157 arguments->boot_file = xstrdup (arg);
158 break;
160 case 'c':
161 if (arguments->core_file)
162 free (arguments->core_file);
164 arguments->core_file = xstrdup (arg);
165 break;
167 case 'd':
168 if (arguments->dir)
169 free (arguments->dir);
171 arguments->dir = xstrdup (arg);
172 break;
174 case 'm':
175 if (arguments->dev_map)
176 free (arguments->dev_map);
178 arguments->dev_map = xstrdup (arg);
179 break;
181 case 'f':
182 arguments->force = 1;
183 break;
185 case 's':
186 arguments->fs_probe = 0;
187 break;
189 case 'v':
190 verbosity++;
191 break;
193 case NO_RS_CODES_KEY:
194 arguments->add_rs_codes = 0;
195 break;
197 case ARGP_KEY_ARG:
198 if (state->arg_num == 0)
199 arguments->device = xstrdup(arg);
200 else
202 /* Too many arguments. */
203 fprintf (stderr, _("Unknown extra argument `%s'."), arg);
204 fprintf (stderr, "\n");
205 argp_usage (state);
207 break;
209 case ARGP_KEY_NO_ARGS:
210 fprintf (stderr, "%s", _("No device is specified.\n"));
211 argp_usage (state);
212 exit (1);
213 break;
215 default:
216 return ARGP_ERR_UNKNOWN;
219 return 0;
222 static struct argp argp = {
223 options, argp_parser, N_("DEVICE"),
224 "\n"N_("\
225 Set up images to boot from DEVICE.\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
233 static char *
234 get_device_name (char *dev)
236 size_t len = strlen (dev);
238 if (dev[0] != '(' || dev[len - 1] != ')')
239 return 0;
241 dev[len - 1] = '\0';
242 return dev + 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"));
263 exit(1);
266 #ifdef GRUB_SETUP_SPARC64
267 arguments.force = 1;
268 #endif
270 if (verbosity > 1)
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. */
277 grub_init_all ();
278 grub_gcry_init_all ();
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 ();
289 dest_dev = get_device_name (arguments.device);
290 if (! dest_dev)
292 /* Possibly, the user specified an OS device file. */
293 dest_dev = grub_util_get_grub_dev (arguments.device);
294 if (! dest_dev)
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);
302 grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
303 arguments.device, dest_dev);
305 else
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. */
321 grub_fini_all ();
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);
329 free (root_dev);
330 free (dest_dev);
332 return 0;