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 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/>.
21
#include <grub/types.h>
22
#include <grub/util/misc.h>
23
#include <grub/device.h>
24
#include <grub/i18n.h>
25
#include <grub/disk.h>
26
#include <grub/file.h>
28
#include <grub/partition.h>
29
#include <grub/msdos_partition.h>
30
#include <grub/gpt_partition.h>
32
#include <grub/util/hostdisk.h>
33
#include <grub/machine/boot.h>
34
#include <grub/machine/kernel.h>
35
#include <grub/term.h>
36
#include <grub/util/raid.h>
37
#include <grub/util/lvm.h>
39
#include <grub_setup_init.h>
45
#include <sys/types.h>
48
#include <grub/util/getroot.h>
55
/* This program fills in various fields inside of the 'boot' and 'core'
58
* The 'boot' image needs to know the OBP path name of the root
59
* device. It also needs to know the initial block number of
60
* 'core' (which is 'diskboot' concatenated with 'kernel' and
61
* all the modules, this is created by grub-mkimage). This resulting
62
* 'boot' image is 512 bytes in size and is placed in the second block
65
* The initial 'diskboot' block acts as a loader for the actual GRUB
66
* kernel. It contains the loading code and then a block list.
68
* The block list of 'core' starts at the end of the 'diskboot' image
69
* and works it's way backwards towards the end of the code of 'diskboot'.
71
* We patch up the images with the necessary values and write out the
75
#define DEFAULT_BOOT_FILE "boot.img"
76
#define DEFAULT_CORE_FILE "core.img"
78
/* This is the blocklist used in the diskboot image. */
83
} __attribute__ ((packed));
97
struct grub_handler_class grub_term_input_class;
98
struct grub_handler_class grub_term_output_class;
106
static char *compute_dest_ofpath (const char *dest)
108
int len = strlen (dest);
113
while ((c = *dest++) != '\0')
115
if (c == '\\' && *dest == ',')
129
setup (const char *prefix, const char *dir,
130
const char *boot_file, const char *core_file,
131
const char *root, const char *dest)
133
char *boot_path, *core_path;
134
char *boot_img, *core_img;
135
size_t boot_size, core_size;
136
grub_uint16_t core_sectors;
137
grub_device_t root_dev, dest_dev;
138
char *boot_devpath, *dest_ofpath;
139
grub_disk_addr_t *kernel_sector;
140
struct boot_blocklist *first_block, *block;
143
grub_disk_addr_t first_sector;
144
grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
147
struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
148
embed_region.start = embed_region.end = ~0UL;
150
auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector,
152
unsigned int length);
153
auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector,
155
unsigned int length);
157
void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector,
161
grub_util_info ("first sector is <%llu,%u,%u>", sector, offset, length);
163
if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
164
grub_util_error ("the first sector of the core file "
165
"is not sector-aligned");
167
first_sector = sector;
170
void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector,
174
struct boot_blocklist *prev = block + 1;
176
grub_util_info ("saving <%llu,%u,%u>", sector, offset, length);
178
if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
179
grub_util_error ("non-sector-aligned data is found in the core file");
181
if (block != first_block
182
&& (grub_be_to_cpu64 (prev->start)
183
+ grub_be_to_cpu16 (prev->len)) == sector)
184
prev->len = grub_cpu_to_be16 (grub_be_to_cpu16 (prev->len) + 1);
187
block->start = grub_cpu_to_be64 (sector);
188
block->len = grub_cpu_to_be16 (1);
192
grub_util_error ("the sectors of the core file are too fragmented");
195
last_length = length;
198
dest_ofpath = compute_dest_ofpath (dest);
200
/* Read the boot image by the OS service. */
201
boot_path = grub_util_get_path (dir, boot_file);
202
boot_size = grub_util_get_image_size (boot_path);
203
if (boot_size != GRUB_DISK_SECTOR_SIZE)
204
grub_util_error ("the size of `%s' is not %d",
205
boot_path, GRUB_DISK_SECTOR_SIZE);
206
boot_img = grub_util_read_image (boot_path);
209
/* Set the addresses of variables in the boot image. */
210
boot_devpath = (char *) (boot_img
211
+ GRUB_BOOT_AOUT_HEADER_SIZE
212
+ GRUB_BOOT_MACHINE_BOOT_DEVPATH);
213
kernel_sector = (grub_disk_addr_t *) (boot_img
214
+ GRUB_BOOT_AOUT_HEADER_SIZE
215
+ GRUB_BOOT_MACHINE_KERNEL_SECTOR);
217
core_path = grub_util_get_path (dir, core_file);
218
core_size = grub_util_get_image_size (core_path);
219
core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
220
>> GRUB_DISK_SECTOR_BITS);
221
if (core_size < GRUB_DISK_SECTOR_SIZE)
222
grub_util_error ("the size of `%s' is too small", core_path);
224
core_img = grub_util_read_image (core_path);
227
/* Have FIRST_BLOCK to point to the first blocklist. */
228
first_block = (struct boot_blocklist *) (core_img
229
+ GRUB_DISK_SECTOR_SIZE
232
grub_util_info ("root is `%s', dest is `%s', and dest_ofpath is `%s'",
233
root, dest, dest_ofpath);
235
/* Open the root device and the destination device. */
236
grub_util_info ("Opening root");
237
root_dev = grub_device_open (root);
239
grub_util_error ("%s", grub_errmsg);
241
grub_util_info ("Opening dest");
242
dest_dev = grub_device_open (dest);
244
grub_util_error ("%s", grub_errmsg);
246
grub_util_info ("setting the root device to `%s'", root);
247
if (grub_env_set ("root", root) != GRUB_ERR_NONE)
248
grub_util_error ("%s", grub_errmsg);
250
/* The core image must be put on a filesystem unfortunately. */
251
grub_util_info ("will leave the core image on the filesystem");
253
/* Make sure that GRUB reads the identical image as the OS. */
254
tmp_img = xmalloc (core_size);
255
core_path = grub_util_get_path (prefix, core_file);
257
/* It is a Good Thing to sync two times. */
263
for (i = 0; i < MAX_TRIES; i++)
265
grub_util_info ("attempting to read the core image `%s' from GRUB%s",
266
core_path, (i == 0) ? "" : " again");
268
grub_disk_cache_invalidate_all ();
270
file = grub_file_open (core_path);
273
if (grub_file_size (file) != core_size)
274
grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
275
(int) grub_file_size (file), (int) core_size);
276
else if (grub_file_read (file, tmp_img, core_size)
277
!= (grub_ssize_t) core_size)
278
grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
280
else if (memcmp (core_img, tmp_img, core_size) != 0)
286
dump = fopen ("dump.img", "wb");
289
fwrite (tmp_img, 1, core_size, dump);
293
dump2 = fopen ("dump2.img", "wb");
296
fwrite (core_img, 1, core_size, dump2);
301
grub_util_info ("succeeded in opening the core image but the data is different");
305
grub_file_close (file);
309
grub_file_close (file);
312
grub_util_info ("couldn't open the core image");
315
grub_util_info ("error message = %s", grub_errmsg);
317
grub_errno = GRUB_ERR_NONE;
323
grub_util_error ("cannot read `%s' correctly", core_path);
325
/* Clean out the blocklists. */
334
if ((char *) block <= core_img)
335
grub_util_error ("no terminator in the core image");
338
/* Now read the core image to determine where the sectors are. */
339
file = grub_file_open (core_path);
341
grub_util_error ("%s", grub_errmsg);
343
file->read_hook = save_first_sector;
344
if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
345
!= GRUB_DISK_SECTOR_SIZE)
346
grub_util_error ("failed to read the first sector of the core image");
349
file->read_hook = save_blocklists;
350
if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
351
!= (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
352
grub_util_error ("failed to read the rest sectors of the core image");
354
grub_file_close (file);
359
*kernel_sector = grub_cpu_to_be64 (first_sector);
361
strcpy(boot_devpath, dest_ofpath);
363
grub_util_info ("boot device path %s, prefix is %s, dest is %s",
364
boot_devpath, prefix, dest);
366
/* Write the first two sectors of the core image onto the disk. */
367
core_path = grub_util_get_path (dir, core_file);
368
grub_util_info ("opening the core image `%s'", core_path);
369
fp = fopen (core_path, "r+b");
371
grub_util_error ("cannot open `%s'", core_path);
373
grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE, fp);
377
/* Write the boot image onto the disk. */
378
if (grub_disk_write (dest_dev->disk, 1, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
379
grub_util_error ("%s", grub_errmsg);
381
/* Sync is a Good Thing. */
386
grub_device_close (dest_dev);
387
grub_device_close (root_dev);
390
static struct option options[] =
392
{"boot-image", required_argument, 0, 'b'},
393
{"core-image", required_argument, 0, 'c'},
394
{"directory", required_argument, 0, 'd'},
395
{"device-map", required_argument, 0, 'm'},
396
{"root-device", required_argument, 0, 'r'},
397
{"help", no_argument, 0, 'h'},
398
{"version", no_argument, 0, 'V'},
399
{"verbose", no_argument, 0, 'v'},
407
fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
410
Usage: %s [OPTION]... DEVICE\n\
412
Set up images to boot from DEVICE.\n\
413
DEVICE must be a GRUB device (e.g. `(hd0,1)').\n\
415
-b, --boot-image=FILE use FILE as the boot image [default=%s]\n\
416
-c, --core-image=FILE use FILE as the core image [default=%s]\n\
417
-d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n\
418
-m, --device-map=FILE use FILE as the device map [default=%s]\n\
419
-r, --root-device=DEV use DEV as the root device [default=guessed]\n\
420
-h, --help display this message and exit\n\
421
-V, --version print version information and exit\n\
422
-v, --verbose print verbose messages\n\
424
Report bugs to <%s>.\n\
426
DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY,
427
DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
432
struct grub_setup_info
444
init_info (struct grub_setup_info *gp)
446
gp->boot_file = NULL;
447
gp->core_file = NULL;
456
parse_options (struct grub_setup_info *gp, int argc, char *argv[])
460
int c = getopt_long (argc, argv, "b:c:d:m:r:hVv", options, 0);
469
free (gp->boot_file);
471
gp->boot_file = xstrdup (optarg);
476
free (gp->core_file);
478
gp->core_file = xstrdup (optarg);
485
gp->dir = xstrdup (optarg);
492
gp->dev_map = xstrdup (optarg);
499
gp->root_dev = xstrdup (optarg);
507
printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
521
grub_env_set ("debug", "all");
525
fprintf (stderr, "No device is specified.\n");
529
if (optind + 1 != argc)
531
fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
538
get_device_name (char *dev)
540
size_t len = strlen (dev);
542
if (dev[0] != '(' || dev[len - 1] != ')')
550
find_dest_dev (struct grub_setup_info *gp, char *argv[])
552
gp->dest_dev = get_device_name (argv[optind]);
555
/* Possibly, the user specified an OS device file. */
556
gp->dest_dev = grub_util_get_grub_dev (argv[optind]);
559
fprintf (stderr, "Invalid device `%s'.\n", argv[optind]);
562
grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
563
argv[optind], gp->dest_dev);
567
/* For simplicity. */
568
gp->dest_dev = xstrdup (gp->dest_dev);
569
grub_util_info ("Using `%s' as GRUB device", gp->dest_dev);
574
check_root_dev (struct grub_setup_info *gp)
578
char *tmp = get_device_name (gp->root_dev);
581
grub_util_error ("invalid root device `%s'", gp->root_dev);
589
char *dir = gp->dir ? gp->dir : DEFAULT_DIRECTORY;
590
char *root_device = grub_guess_root_device (dir);
592
gp->root_dev = grub_util_get_grub_dev (root_device);
595
grub_util_info ("guessing the root device failed, because of `%s'",
597
grub_util_error ("cannot guess the root device. "
598
"Specify the option `--root-device'");
600
grub_util_info ("guessed root device `%s' and root_dev `%s' from "
601
"dir `%s'", root_device, gp->root_dev, dir);
606
free_memory (struct grub_setup_info *gp)
608
free (gp->boot_file);
609
free (gp->core_file);
618
main (int argc, char *argv[])
620
struct grub_setup_info ginfo;
622
set_program_name (argv[0]);
624
grub_util_init_nls ();
627
if (!parse_options (&ginfo, argc, argv))
630
/* Initialize the emulated biosdisk driver. */
631
grub_util_biosdisk_init (ginfo.dev_map ? ginfo.dev_map : DEFAULT_DEVICE_MAP);
633
/* Initialize all modules. */
636
find_dest_dev (&ginfo, argv);
638
ginfo.prefix = grub_get_prefix (ginfo.dir ? : DEFAULT_DIRECTORY);
640
check_root_dev (&ginfo);
642
/* Do the real work. */
644
ginfo.dir ? ginfo.dir : DEFAULT_DIRECTORY,
645
ginfo.boot_file ? ginfo.boot_file : DEFAULT_BOOT_FILE,
646
ginfo.core_file ? ginfo.core_file : DEFAULT_CORE_FILE,
647
ginfo.root_dev, ginfo.dest_dev);
649
/* Free resources. */
652
free_memory (&ginfo);