2004-04-04 13:46:03 +00:00
|
|
|
/* grub-setup.c - make GRUB usable */
|
2003-01-02 20:12:33 +00:00
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2011-10-09 19:13:00 +00:00
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
|
2003-01-02 20:12:33 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2003-01-02 20:12:33 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2003-01-02 20:12:33 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2003-01-02 20:12:33 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2003-01-02 20:12:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2013-10-15 14:16:04 +00:00
|
|
|
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/types.h>
|
2010-05-06 06:04:04 +00:00
|
|
|
#include <grub/emu/misc.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/util/misc.h>
|
|
|
|
#include <grub/device.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/fs.h>
|
2004-12-04 Marco Gerards <metgerards@student.han.nl>
Modulize the partition map support and add support for the amiga
partition map.
* commands/ls.c: Include <grub/partition.h> instead of
<grub/machine/partition.h>.
* kern/disk.c: Likewise.
* kern/rescue.c: Likewise.
* loader/i386/pc/chainloader.c: Likewise.
* normal/cmdline.c: Likewise.
* kern/powerpc/ieee1275/init.c: Likewise.
(grub_machine_init): Call `grub_pc_partition_map_init',
`grub_amiga_partition_map_init' and
`grub_apple_partition_map_init'.
* conf/i386-pc.rmk (kernel_img_SOURCES): Remove
`disk/i386/pc/partition.c'. Add `kern/partition.c'.
(kernel_img_HEADERS): Remove `machine/partition.h'. Add
`partition.h' and `pc_partition.h'.
(grub_setup_SOURCES): Remove
`disk/i386/pc/partition.c'. Add `kern/partition.c',
`partmap/amiga.c', `partmap/apple.c' and `partmap/pc.c'.
(grub_emu_SOURCES): Likewise.
(pkgdata_MODULES): Add `amiga.mod', `apple.mod' and `pc.mod'.
(amiga_mod_SOURCES, amiga_mod_CFLAGS, apple_mod_SOURCES)
(apple_mod_CFLAGS, pc_mod_SOURCES, pc_mod_CFLAGS): New variables.
* conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Remove
`disk/powerpc/ieee1275/partition.c'. Add `kern/partition.c',
`partmap/amiga.c', `partmap/apple.c' and `partmap/pc.c'.
(grubof_SOURCES): Likewise.
* disk/i386/pc/partition.c: File removed.
* disk/powerpc/ieee1275/partition.c: Likewise.
* include/grub/powerpc/ieee1275/partition.h: Likewise.
* include/grub/i386/pc/partition.h: Likewise.
* kern/partition.c: New file.
* partmap/amiga.c: Likewise.
* partmap/apple.c: Likewise.
* partmap/pc.c: Likewise.
* include/grub/partition.h: Likewise..
* include/grub/pc_partition.h: Likewise.
* util/grub-emu.c: Include <grub/partition.h> instead of
<grub/machine/partition.h>.
(main): Call `grub_pc_partition_map_init',
`grub_amiga_partition_map_init' and
`grub_apple_partition_map_init' and deinitialize afterwards.
* util/i386/pc/biosdisk.c: Include `#include
<grub/partition.h>' and `include <grub/pc_partition.h>' instead of
`<grub/machine/partition.h>'.
* util/i386/pc/grub-setup.c: Likewise.
* util/i386/pc/biosdisk.c: Likewise.
(grub_util_biosdisk_get_grub_dev): Only access the PC specific
partition information in case of a PC partition.
* util/i386/pc/grub-setup.c: Include `#include
<grub/partition.h>' and `include <grub/pc_partition.h>' instead of
`<grub/machine/partition.h>'.
(setup): Only access the PC specific partition information in case
of a PC partition.
2004-12-04 18:45:46 +00:00
|
|
|
#include <grub/partition.h>
|
2005-07-20 20:30:46 +00:00
|
|
|
#include <grub/env.h>
|
2010-05-06 06:04:04 +00:00
|
|
|
#include <grub/emu/hostdisk.h>
|
2006-04-23 13:37:36 +00:00
|
|
|
#include <grub/term.h>
|
2009-11-18 23:20:22 +00:00
|
|
|
#include <grub/i18n.h>
|
2013-10-15 14:16:04 +00:00
|
|
|
#include <grub/crypto.h>
|
2010-09-14 19:07:39 +00:00
|
|
|
#include <grub/emu/getroot.h>
|
2013-10-15 14:16:04 +00:00
|
|
|
#include <grub/util/install.h>
|
2012-02-27 20:36:58 +00:00
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
#include <argp.h>
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2010-09-14 19:07:39 +00:00
|
|
|
/* On SPARC this program fills in various fields inside of the 'boot' and 'core'
|
|
|
|
* image files.
|
|
|
|
*
|
|
|
|
* The 'boot' image needs to know the OBP path name of the root
|
|
|
|
* device. It also needs to know the initial block number of
|
|
|
|
* 'core' (which is 'diskboot' concatenated with 'kernel' and
|
|
|
|
* all the modules, this is created by grub-mkimage). This resulting
|
|
|
|
* 'boot' image is 512 bytes in size and is placed in the second block
|
|
|
|
* of a partition.
|
|
|
|
*
|
|
|
|
* The initial 'diskboot' block acts as a loader for the actual GRUB
|
|
|
|
* kernel. It contains the loading code and then a block list.
|
|
|
|
*
|
|
|
|
* The block list of 'core' starts at the end of the 'diskboot' image
|
|
|
|
* and works it's way backwards towards the end of the code of 'diskboot'.
|
|
|
|
*
|
|
|
|
* We patch up the images with the necessary values and write out the
|
|
|
|
* result.
|
|
|
|
*/
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#define DEFAULT_BOOT_FILE "boot.img"
|
|
|
|
#define DEFAULT_CORE_FILE "core.img"
|
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
static struct argp_option options[] = {
|
|
|
|
{"boot-image", 'b', N_("FILE"), 0,
|
2012-02-25 00:15:29 +00:00
|
|
|
N_("use FILE as the boot image [default=%s]"), 0},
|
2010-09-20 10:37:37 +00:00
|
|
|
{"core-image", 'c', N_("FILE"), 0,
|
2012-02-25 00:15:29 +00:00
|
|
|
N_("use FILE as the core image [default=%s]"), 0},
|
2010-09-20 10:37:37 +00:00
|
|
|
{"directory", 'd', N_("DIR"), 0,
|
2012-02-25 00:15:29 +00:00
|
|
|
N_("use GRUB files in the directory DIR [default=%s]"), 0},
|
2010-09-20 10:37:37 +00:00
|
|
|
{"device-map", 'm', N_("FILE"), 0,
|
2012-02-08 18:26:01 +00:00
|
|
|
N_("use FILE as the device map [default=%s]"), 0},
|
2010-09-20 10:37:37 +00:00
|
|
|
{"force", 'f', 0, 0,
|
2012-02-08 18:26:01 +00:00
|
|
|
N_("install even if problems are detected"), 0},
|
2010-09-20 10:37:37 +00:00
|
|
|
{"skip-fs-probe",'s',0, 0,
|
2012-02-25 00:15:29 +00:00
|
|
|
N_("do not probe for filesystems in DEVICE"), 0},
|
2012-02-05 10:23:47 +00:00
|
|
|
{"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
|
2010-10-16 22:35:14 +00:00
|
|
|
{"allow-floppy", 'a', 0, 0,
|
2012-03-05 15:42:26 +00:00
|
|
|
/* TRANSLATORS: The potential breakage isn't limited to floppies but it's
|
|
|
|
likely to make the install unbootable from HDD. */
|
2012-02-25 00:15:29 +00:00
|
|
|
N_("make the drive also bootable as floppy (default for fdX devices). May break on some BIOSes."), 0},
|
2010-10-16 22:35:14 +00:00
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
{ 0, 0, 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *
|
|
|
|
help_filter (int key, const char *text, void *input __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 'b':
|
|
|
|
return xasprintf (text, DEFAULT_BOOT_FILE);
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
return xasprintf (text, DEFAULT_CORE_FILE);
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
return xasprintf (text, DEFAULT_DIRECTORY);
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
return xasprintf (text, DEFAULT_DEVICE_MAP);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
default:
|
|
|
|
return (char *) text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct arguments
|
|
|
|
{
|
|
|
|
char *boot_file;
|
|
|
|
char *core_file;
|
|
|
|
char *dir;
|
|
|
|
char *dev_map;
|
|
|
|
int force;
|
|
|
|
int fs_probe;
|
2010-10-16 22:35:14 +00:00
|
|
|
int allow_floppy;
|
2010-09-20 10:37:37 +00:00
|
|
|
char *device;
|
|
|
|
};
|
|
|
|
|
|
|
|
static error_t
|
|
|
|
argp_parser (int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
/* Get the input argument from argp_parse, which we
|
|
|
|
know is a pointer to our arguments structure. */
|
|
|
|
struct arguments *arguments = state->input;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
2010-10-16 22:35:14 +00:00
|
|
|
case 'a':
|
|
|
|
arguments->allow_floppy = 1;
|
|
|
|
break;
|
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
case 'b':
|
|
|
|
if (arguments->boot_file)
|
|
|
|
free (arguments->boot_file);
|
|
|
|
|
|
|
|
arguments->boot_file = xstrdup (arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
if (arguments->core_file)
|
|
|
|
free (arguments->core_file);
|
|
|
|
|
|
|
|
arguments->core_file = xstrdup (arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
if (arguments->dir)
|
|
|
|
free (arguments->dir);
|
|
|
|
|
|
|
|
arguments->dir = xstrdup (arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
if (arguments->dev_map)
|
|
|
|
free (arguments->dev_map);
|
|
|
|
|
|
|
|
arguments->dev_map = xstrdup (arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
arguments->force = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
arguments->fs_probe = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
verbosity++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARGP_KEY_ARG:
|
|
|
|
if (state->arg_num == 0)
|
|
|
|
arguments->device = xstrdup(arg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Too many arguments. */
|
2012-02-26 16:28:05 +00:00
|
|
|
fprintf (stderr, _("Unknown extra argument `%s'."), arg);
|
|
|
|
fprintf (stderr, "\n");
|
2010-09-20 10:37:37 +00:00
|
|
|
argp_usage (state);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARGP_KEY_NO_ARGS:
|
2010-09-20 16:56:14 +00:00
|
|
|
fprintf (stderr, "%s", _("No device is specified.\n"));
|
2010-09-20 10:37:37 +00:00
|
|
|
argp_usage (state);
|
2012-02-03 20:18:37 +00:00
|
|
|
exit (1);
|
2010-09-20 10:37:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
static struct argp argp = {
|
|
|
|
options, argp_parser, N_("DEVICE"),
|
2010-09-21 09:17:54 +00:00
|
|
|
"\n"N_("\
|
2010-09-20 10:37:37 +00:00
|
|
|
Set up images to boot from DEVICE.\n\
|
|
|
|
\n\
|
2010-09-21 09:17:54 +00:00
|
|
|
You should not normally run this program directly. Use grub-install instead.")
|
|
|
|
"\v"N_("\
|
2010-10-26 10:33:57 +00:00
|
|
|
DEVICE must be an OS device (e.g. /dev/sda)."),
|
2010-09-20 10:37:37 +00:00
|
|
|
NULL, help_filter, NULL
|
|
|
|
};
|
|
|
|
|
2013-12-07 11:00:37 +00:00
|
|
|
static char *
|
|
|
|
get_device_name (char *dev)
|
|
|
|
{
|
|
|
|
size_t len = strlen (dev);
|
|
|
|
|
|
|
|
if (dev[0] != '(' || dev[len - 1] != ')')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dev[len - 1] = '\0';
|
|
|
|
return dev + 1;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2013-12-07 11:00:37 +00:00
|
|
|
char *root_dev = NULL;
|
|
|
|
char *dest_dev = NULL;
|
2010-09-20 10:37:37 +00:00
|
|
|
struct arguments arguments;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-10-13 18:03:42 +00:00
|
|
|
grub_util_host_init (&argc, &argv);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
/* Default option values. */
|
|
|
|
memset (&arguments, 0, sizeof (struct arguments));
|
|
|
|
arguments.fs_probe = 1;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
/* Parse our arguments */
|
|
|
|
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
|
|
|
|
{
|
2010-09-20 16:56:14 +00:00
|
|
|
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
|
2010-09-20 10:37:37 +00:00
|
|
|
exit(1);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-27 13:24:22 +00:00
|
|
|
#ifdef GRUB_SETUP_SPARC64
|
2010-09-30 17:27:28 +00:00
|
|
|
arguments.force = 1;
|
|
|
|
#endif
|
|
|
|
|
2008-02-09 10:46:37 +00:00
|
|
|
if (verbosity > 1)
|
|
|
|
grub_env_set ("debug", "all");
|
|
|
|
|
2005-07-24 18:16:26 +00:00
|
|
|
/* Initialize the emulated biosdisk driver. */
|
2010-09-20 10:37:37 +00:00
|
|
|
grub_util_biosdisk_init (arguments.dev_map ? : DEFAULT_DEVICE_MAP);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2007-07-22 19:17:27 +00:00
|
|
|
/* Initialize all modules. */
|
|
|
|
grub_init_all ();
|
2011-08-16 14:19:06 +00:00
|
|
|
grub_gcry_init_all ();
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-09-05 21:24:57 +00:00
|
|
|
grub_lvm_fini ();
|
2010-09-20 18:09:31 +00:00
|
|
|
grub_mdraid09_fini ();
|
|
|
|
grub_mdraid1x_fini ();
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_diskfilter_fini ();
|
|
|
|
grub_diskfilter_init ();
|
2010-09-20 18:09:31 +00:00
|
|
|
grub_mdraid09_init ();
|
|
|
|
grub_mdraid1x_init ();
|
2010-09-05 21:24:57 +00:00
|
|
|
grub_lvm_init ();
|
|
|
|
|
2013-12-07 11:00:37 +00:00
|
|
|
dest_dev = get_device_name (arguments.device);
|
|
|
|
if (! dest_dev)
|
|
|
|
{
|
|
|
|
/* Possibly, the user specified an OS device file. */
|
|
|
|
dest_dev = grub_util_get_grub_dev (arguments.device);
|
|
|
|
if (! dest_dev)
|
|
|
|
{
|
|
|
|
char *program = xstrdup(program_name);
|
|
|
|
fprintf (stderr, _("Invalid device `%s'.\n"), arguments.device);
|
|
|
|
argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
|
|
|
|
free(program);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
|
|
|
|
arguments.device, dest_dev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For simplicity. */
|
|
|
|
dest_dev = xstrdup (dest_dev);
|
|
|
|
grub_util_info ("Using `%s' as GRUB device", dest_dev);
|
|
|
|
}
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
/* Do the real work. */
|
2013-10-15 14:16:04 +00:00
|
|
|
GRUB_SETUP_FUNC (arguments.dir ? : DEFAULT_DIRECTORY,
|
|
|
|
arguments.boot_file ? : DEFAULT_BOOT_FILE,
|
|
|
|
arguments.core_file ? : DEFAULT_CORE_FILE,
|
2013-12-07 11:00:37 +00:00
|
|
|
dest_dev, arguments.force,
|
2013-10-15 14:16:04 +00:00
|
|
|
arguments.fs_probe, arguments.allow_floppy);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
/* Free resources. */
|
2007-07-22 19:17:27 +00:00
|
|
|
grub_fini_all ();
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_fini ();
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-09-20 10:37:37 +00:00
|
|
|
free (arguments.boot_file);
|
|
|
|
free (arguments.core_file);
|
|
|
|
free (arguments.dir);
|
|
|
|
free (arguments.dev_map);
|
|
|
|
free (arguments.device);
|
2013-12-07 11:00:37 +00:00
|
|
|
free (root_dev);
|
|
|
|
free (dest_dev);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|