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/emu/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>
38
#include <grub/util/ofpath.h>
40
#include <grub_setup_init.h>
46
#include <sys/types.h>
49
#include <grub/emu/getroot.h>
56
/* This program fills in various fields inside of the 'boot' and 'core'
59
* The 'boot' image needs to know the OBP path name of the root
60
* device. It also needs to know the initial block number of
61
* 'core' (which is 'diskboot' concatenated with 'kernel' and
62
* all the modules, this is created by grub-mkimage). This resulting
63
* 'boot' image is 512 bytes in size and is placed in the second block
66
* The initial 'diskboot' block acts as a loader for the actual GRUB
67
* kernel. It contains the loading code and then a block list.
69
* The block list of 'core' starts at the end of the 'diskboot' image
70
* and works it's way backwards towards the end of the code of 'diskboot'.
72
* We patch up the images with the necessary values and write out the
76
#define DEFAULT_BOOT_FILE "boot.img"
77
#define DEFAULT_CORE_FILE "core.img"
79
#define grub_target_to_host16(x) grub_be_to_cpu16(x)
80
#define grub_target_to_host32(x) grub_be_to_cpu32(x)
81
#define grub_target_to_host64(x) grub_be_to_cpu64(x)
82
#define grub_host_to_target16(x) grub_cpu_to_be16(x)
83
#define grub_host_to_target32(x) grub_cpu_to_be32(x)
84
#define grub_host_to_target64(x) grub_cpu_to_be64(x)
86
/* This is the blocklist used in the diskboot image. */
91
} __attribute__ ((packed));
94
grub_xputs_real (const char *str)
99
void (*grub_xputs) (const char *str) = grub_xputs_real;
114
setup (const char *prefix, const char *dir,
115
const char *boot_file, const char *core_file,
116
const char *root, const char *dest)
118
char *boot_path, *core_path;
119
char *boot_img, *core_img;
120
size_t boot_size, core_size;
121
grub_uint16_t core_sectors;
122
grub_device_t root_dev, dest_dev;
124
grub_disk_addr_t *kernel_byte;
125
struct boot_blocklist *first_block, *block;
128
grub_disk_addr_t first_sector;
129
grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
132
struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
133
embed_region.start = embed_region.end = ~0UL;
135
auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector,
137
unsigned int length);
138
auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector,
140
unsigned int length);
142
void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector,
146
grub_util_info ("first sector is <%llu,%u,%u>", sector, offset, length);
148
if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
149
grub_util_error ("the first sector of the core file "
150
"is not sector-aligned");
152
first_sector = sector;
155
void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector,
159
struct boot_blocklist *prev = block + 1;
161
grub_util_info ("saving <%llu,%u,%u>", sector, offset, length);
163
if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
164
grub_util_error ("non-sector-aligned data is found in the core file");
166
if (block != first_block
167
&& (grub_be_to_cpu64 (prev->start)
168
+ grub_be_to_cpu16 (prev->len)) == sector)
169
prev->len = grub_cpu_to_be16 (grub_be_to_cpu16 (prev->len) + 1);
172
block->start = grub_cpu_to_be64 (sector);
173
block->len = grub_cpu_to_be16 (1);
177
grub_util_error ("the sectors of the core file are too fragmented");
180
last_length = length;
183
/* Read the boot image by the OS service. */
184
boot_path = grub_util_get_path (dir, boot_file);
185
boot_size = grub_util_get_image_size (boot_path);
186
if (boot_size != GRUB_DISK_SECTOR_SIZE)
187
grub_util_error ("the size of `%s' is not %d",
188
boot_path, GRUB_DISK_SECTOR_SIZE);
189
boot_img = grub_util_read_image (boot_path);
192
/* Set the addresses of variables in the boot image. */
193
boot_devpath = (char *) (boot_img
194
+ GRUB_BOOT_AOUT_HEADER_SIZE
195
+ GRUB_BOOT_MACHINE_BOOT_DEVPATH);
196
kernel_byte = (grub_disk_addr_t *) (boot_img
197
+ GRUB_BOOT_AOUT_HEADER_SIZE
198
+ GRUB_BOOT_MACHINE_KERNEL_BYTE);
200
core_path = grub_util_get_path (dir, core_file);
201
core_size = grub_util_get_image_size (core_path);
202
core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
203
>> GRUB_DISK_SECTOR_BITS);
204
if (core_size < GRUB_DISK_SECTOR_SIZE)
205
grub_util_error ("the size of `%s' is too small", core_path);
207
core_img = grub_util_read_image (core_path);
210
/* Have FIRST_BLOCK to point to the first blocklist. */
211
first_block = (struct boot_blocklist *) (core_img
212
+ GRUB_DISK_SECTOR_SIZE
215
grub_util_info ("root is `%s', dest is `%s'", root, dest);
217
/* Open the root device and the destination device. */
218
grub_util_info ("Opening root");
219
root_dev = grub_device_open (root);
221
grub_util_error ("%s", grub_errmsg);
223
grub_util_info ("Opening dest");
224
dest_dev = grub_device_open (dest);
226
grub_util_error ("%s", grub_errmsg);
228
grub_util_info ("setting the root device to `%s'", root);
229
if (grub_env_set ("root", root) != GRUB_ERR_NONE)
230
grub_util_error ("%s", grub_errmsg);
232
/* The core image must be put on a filesystem unfortunately. */
233
grub_util_info ("will leave the core image on the filesystem");
235
/* Make sure that GRUB reads the identical image as the OS. */
236
tmp_img = xmalloc (core_size);
237
core_path = grub_util_get_path (prefix, core_file);
239
/* It is a Good Thing to sync two times. */
245
for (i = 0; i < MAX_TRIES; i++)
247
grub_util_info ("attempting to read the core image `%s' from GRUB%s",
248
core_path, (i == 0) ? "" : " again");
250
grub_disk_cache_invalidate_all ();
252
file = grub_file_open (core_path);
255
if (grub_file_size (file) != core_size)
256
grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
257
(int) grub_file_size (file), (int) core_size);
258
else if (grub_file_read (file, tmp_img, core_size)
259
!= (grub_ssize_t) core_size)
260
grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
262
else if (memcmp (core_img, tmp_img, core_size) != 0)
268
dump = fopen ("dump.img", "wb");
271
fwrite (tmp_img, 1, core_size, dump);
275
dump2 = fopen ("dump2.img", "wb");
278
fwrite (core_img, 1, core_size, dump2);
283
grub_util_info ("succeeded in opening the core image but the data is different");
287
grub_file_close (file);
291
grub_file_close (file);
294
grub_util_info ("couldn't open the core image");
297
grub_util_info ("error message = %s", grub_errmsg);
299
grub_errno = GRUB_ERR_NONE;
305
grub_util_error ("cannot read `%s' correctly", core_path);
307
/* Clean out the blocklists. */
316
if ((char *) block <= core_img)
317
grub_util_error ("no terminator in the core image");
320
/* Now read the core image to determine where the sectors are. */
321
file = grub_file_open (core_path);
323
grub_util_error ("%s", grub_errmsg);
325
file->read_hook = save_first_sector;
326
if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
327
!= GRUB_DISK_SECTOR_SIZE)
328
grub_util_error ("failed to read the first sector of the core image");
331
file->read_hook = save_blocklists;
332
if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
333
!= (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
334
grub_util_error ("failed to read the rest sectors of the core image");
336
if (file->device->disk->id != dest_dev->disk->id)
338
const char *dest_ofpath;
340
= grub_util_devname_to_ofpath (grub_util_biosdisk_get_osdev (file->device->disk));
341
grub_util_info ("dest_ofpath is `%s'", dest_ofpath);
342
strncpy (boot_devpath, dest_ofpath, GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
343
- GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1);
344
boot_devpath[GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
345
- GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1] = 0;
349
grub_util_info ("non cross-disk install");
350
memset (boot_devpath, 0, GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
351
- GRUB_BOOT_MACHINE_BOOT_DEVPATH);
354
grub_file_close (file);
359
*kernel_byte = grub_cpu_to_be64 (first_sector << GRUB_DISK_SECTOR_BITS);
361
grub_util_info ("boot device path %s, prefix is %s, dest is %s",
362
boot_devpath, prefix, dest);
364
/* Write the first two sectors of the core image onto the disk. */
365
core_path = grub_util_get_path (dir, core_file);
366
grub_util_info ("opening the core image `%s'", core_path);
367
fp = fopen (core_path, "r+b");
369
grub_util_error ("cannot open `%s'", core_path);
371
grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE, fp);
375
/* Write the boot image onto the disk. */
376
if (grub_disk_write (dest_dev->disk, 1, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
377
grub_util_error ("%s", grub_errmsg);
379
/* Sync is a Good Thing. */
384
grub_device_close (dest_dev);
385
grub_device_close (root_dev);
388
static struct option options[] =
390
{"boot-image", required_argument, 0, 'b'},
391
{"core-image", required_argument, 0, 'c'},
392
{"directory", required_argument, 0, 'd'},
393
{"device-map", required_argument, 0, 'm'},
394
{"root-device", required_argument, 0, 'r'},
395
{"help", no_argument, 0, 'h'},
396
{"version", no_argument, 0, 'V'},
397
{"verbose", no_argument, 0, 'v'},
405
fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
408
Usage: %s [OPTION]... DEVICE\n\
410
Set up images to boot from DEVICE.\n\
411
DEVICE must be a GRUB device (e.g. `(hd0,1)').\n\
413
You should not normally run %s directly. Use grub-install instead.\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\
425
", program_name, program_name,
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 ("%s (%s) %s\n", program_name, 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_make_system_path_relative_to_its_root (ginfo.dir ?
639
: DEFAULT_DIRECTORY);
641
check_root_dev (&ginfo);
643
/* Do the real work. */
645
ginfo.dir ? ginfo.dir : DEFAULT_DIRECTORY,
646
ginfo.boot_file ? ginfo.boot_file : DEFAULT_BOOT_FILE,
647
ginfo.core_file ? ginfo.core_file : DEFAULT_CORE_FILE,
648
ginfo.root_dev, ginfo.dest_dev);
650
/* Free resources. */
653
free_memory (&ginfo);