GRUB-1.98 changes
[grub2/jjazz.git] / util / i386 / pc / grub-setup.c
blob4e2517ef25be7302012e63b869f1d5434f759c2b
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 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>
21 #include <grub/types.h>
22 #include <grub/util/misc.h>
23 #include <grub/device.h>
24 #include <grub/disk.h>
25 #include <grub/file.h>
26 #include <grub/fs.h>
27 #include <grub/partition.h>
28 #include <grub/msdos_partition.h>
29 #include <grub/gpt_partition.h>
30 #include <grub/env.h>
31 #include <grub/util/hostdisk.h>
32 #include <grub/machine/boot.h>
33 #include <grub/machine/kernel.h>
34 #include <grub/term.h>
35 #include <grub/i18n.h>
36 #include <grub/util/raid.h>
37 #include <grub/util/lvm.h>
38 #include <grub/util/getroot.h>
40 static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_PARTITION_TYPE_BIOS_BOOT;
42 #include <grub_setup_init.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <dirent.h>
51 #include <assert.h>
52 #include "progname.h"
54 #define _GNU_SOURCE 1
55 #include <getopt.h>
57 #define DEFAULT_BOOT_FILE "boot.img"
58 #define DEFAULT_CORE_FILE "core.img"
60 void
61 grub_putchar (int c)
63 putchar (c);
66 int
67 grub_getkey (void)
69 return -1;
72 struct grub_handler_class grub_term_input_class;
73 struct grub_handler_class grub_term_output_class;
75 void
76 grub_refresh (void)
78 fflush (stdout);
81 static void
82 setup (const char *dir,
83 const char *boot_file, const char *core_file,
84 const char *root, const char *dest, int must_embed, int force, int fs_probe)
86 char *boot_path, *core_path, *core_path_dev, *core_path_dev_full;
87 char *boot_img, *core_img;
88 size_t boot_size, core_size;
89 grub_uint16_t core_sectors;
90 grub_device_t root_dev, dest_dev;
91 const char *dest_partmap;
92 grub_uint8_t *boot_drive;
93 grub_disk_addr_t *kernel_sector;
94 grub_uint16_t *boot_drive_check;
95 struct grub_boot_blocklist *first_block, *block;
96 grub_int32_t *install_dos_part, *install_bsd_part;
97 grub_int32_t dos_part, bsd_part;
98 char *tmp_img;
99 int i;
100 grub_disk_addr_t first_sector;
101 grub_uint16_t current_segment
102 = GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
103 grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
104 grub_file_t file;
105 FILE *fp;
106 struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
107 embed_region.start = embed_region.end = ~0UL;
109 auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
110 unsigned length);
111 auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
112 unsigned length);
114 auto int NESTED_FUNC_ATTR find_usable_region_msdos (grub_disk_t disk,
115 const grub_partition_t p);
116 int NESTED_FUNC_ATTR find_usable_region_msdos (grub_disk_t disk __attribute__ ((unused)),
117 const grub_partition_t p)
119 struct grub_msdos_partition *pcdata = p->data;
121 /* There's always an embed region, and it starts right after the MBR. */
122 embed_region.start = 1;
124 /* For its end offset, include as many dummy partitions as we can. */
125 if (! grub_msdos_partition_is_empty (pcdata->dos_type)
126 && ! grub_msdos_partition_is_bsd (pcdata->dos_type)
127 && embed_region.end > p->start)
128 embed_region.end = p->start;
130 return 0;
133 auto int NESTED_FUNC_ATTR find_usable_region_gpt (grub_disk_t disk,
134 const grub_partition_t p);
135 int NESTED_FUNC_ATTR find_usable_region_gpt (grub_disk_t disk __attribute__ ((unused)),
136 const grub_partition_t p)
138 struct grub_gpt_partentry *gptdata = p->data;
140 /* If there's an embed region, it is in a dedicated partition. */
141 if (! memcmp (&gptdata->type, &grub_gpt_partition_type_bios_boot, 16))
143 embed_region.start = p->start;
144 embed_region.end = p->start + p->len;
146 return 1;
149 return 0;
152 void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
153 unsigned length)
155 grub_util_info ("the first sector is <%llu,%u,%u>",
156 sector, offset, length);
158 if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
159 grub_util_error (_("the first sector of the core file is not sector-aligned"));
161 first_sector = sector;
164 void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
165 unsigned length)
167 struct grub_boot_blocklist *prev = block + 1;
169 grub_util_info ("saving <%llu,%u,%u> with the segment 0x%x",
170 sector, offset, length, (unsigned) current_segment);
172 if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
173 grub_util_error (_("non-sector-aligned data is found in the core file"));
175 if (block != first_block
176 && (grub_le_to_cpu64 (prev->start)
177 + grub_le_to_cpu16 (prev->len)) == sector)
178 prev->len = grub_cpu_to_le16 (grub_le_to_cpu16 (prev->len) + 1);
179 else
181 block->start = grub_cpu_to_le64 (sector);
182 block->len = grub_cpu_to_le16 (1);
183 block->segment = grub_cpu_to_le16 (current_segment);
185 block--;
186 if (block->len)
187 grub_util_error (_("the sectors of the core file are too fragmented"));
190 last_length = length;
191 current_segment += GRUB_DISK_SECTOR_SIZE >> 4;
194 /* Read the boot image by the OS service. */
195 boot_path = grub_util_get_path (dir, boot_file);
196 boot_size = grub_util_get_image_size (boot_path);
197 if (boot_size != GRUB_DISK_SECTOR_SIZE)
198 grub_util_error (_("the size of `%s' is not %u"),
199 boot_path, GRUB_DISK_SECTOR_SIZE);
200 boot_img = grub_util_read_image (boot_path);
201 free (boot_path);
203 /* Set the addresses of variables in the boot image. */
204 boot_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_BOOT_DRIVE);
205 kernel_sector = (grub_disk_addr_t *) (boot_img
206 + GRUB_BOOT_MACHINE_KERNEL_SECTOR);
207 boot_drive_check = (grub_uint16_t *) (boot_img
208 + GRUB_BOOT_MACHINE_DRIVE_CHECK);
210 core_path = grub_util_get_path (dir, core_file);
211 core_size = grub_util_get_image_size (core_path);
212 core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
213 >> GRUB_DISK_SECTOR_BITS);
214 if (core_size < GRUB_DISK_SECTOR_SIZE)
215 grub_util_error (_("the size of `%s' is too small"), core_path);
216 else if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
217 grub_util_error (_("the size of `%s' is too large"), core_path);
219 core_img = grub_util_read_image (core_path);
221 /* Have FIRST_BLOCK to point to the first blocklist. */
222 first_block = (struct grub_boot_blocklist *) (core_img
223 + GRUB_DISK_SECTOR_SIZE
224 - sizeof (*block));
226 install_dos_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
227 + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART);
228 install_bsd_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
229 + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART);
231 /* Open the root device and the destination device. */
232 root_dev = grub_device_open (root);
233 if (! root_dev)
234 grub_util_error ("%s", grub_errmsg);
236 dest_dev = grub_device_open (dest);
237 if (! dest_dev)
238 grub_util_error ("%s", grub_errmsg);
240 grub_util_info ("setting the root device to `%s'", root);
241 if (grub_env_set ("root", root) != GRUB_ERR_NONE)
242 grub_util_error ("%s", grub_errmsg);
244 /* Read the original sector from the disk. */
245 tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
246 if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
247 grub_util_error ("%s", grub_errmsg);
249 if (dest_dev->disk->partition && fs_probe)
251 grub_fs_t fs;
252 fs = grub_fs_probe (dest_dev);
253 if (! fs)
254 grub_util_error (_("unable to identify a filesystem in %s; safety check can't be performed"),
255 dest_dev->disk->name);
257 if (! fs->reserved_first_sector)
258 grub_util_error (_("%s appears to contain a %s filesystem which isn't known to "
259 "reserve space for DOS-style boot. Installing GRUB there could "
260 "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
261 "by grub-setup (--skip-fs-probe disables this "
262 "check, use at your own risk)"), dest_dev->disk->name, fs->name);
265 /* Copy the possible DOS BPB. */
266 memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
267 tmp_img + GRUB_BOOT_MACHINE_BPB_START,
268 GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
270 /* Copy the possible partition table. */
271 if (dest_dev->disk->has_partitions)
272 memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
273 tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
274 GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
276 free (tmp_img);
278 /* If DEST_DRIVE is a hard disk, enable the workaround, which is
279 for buggy BIOSes which don't pass boot drive correctly. Instead,
280 they pass 0x00 or 0x01 even when booted from 0x80. */
281 if (dest_dev->disk->id & 0x80)
282 /* Replace the jmp (2 bytes) with double nop's. */
283 *boot_drive_check = 0x9090;
285 /* If we hardcoded drive as part of prefix, we don't want to
286 override the current setting. */
287 if (*install_dos_part != -2)
289 /* Embed information about the installed location. */
290 if (root_dev->disk->partition)
292 if (strcmp (root_dev->disk->partition->partmap->name,
293 "part_msdos") == 0)
295 struct grub_msdos_partition *pcdata =
296 root_dev->disk->partition->data;
297 dos_part = pcdata->dos_part;
298 bsd_part = pcdata->bsd_part;
300 else if (strcmp (root_dev->disk->partition->partmap->name,
301 "part_gpt") == 0)
303 dos_part = root_dev->disk->partition->index;
304 bsd_part = -1;
306 else
307 grub_util_error (_("no DOS-style partitions found"));
309 else
310 dos_part = bsd_part = -1;
312 else
314 dos_part = grub_le_to_cpu32 (*install_dos_part);
315 bsd_part = grub_le_to_cpu32 (*install_bsd_part);
318 grub_util_info ("dos partition is %d, bsd partition is %d",
319 dos_part, bsd_part);
321 if (! dest_dev->disk->has_partitions)
323 grub_util_warn (_("Attempting to install GRUB to a partitionless disk. This is a BAD idea."));
324 goto unable_to_embed;
327 if (dest_dev->disk->partition)
329 grub_util_warn (_("Attempting to install GRUB to a partition instead of the MBR. This is a BAD idea."));
330 goto unable_to_embed;
333 /* Unlike root_dev, with dest_dev we're interested in the partition map even
334 if dest_dev itself is a whole disk. */
335 auto int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk,
336 const grub_partition_t p);
337 int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk __attribute__ ((unused)),
338 const grub_partition_t p)
340 dest_partmap = p->partmap->name;
341 return 1;
343 dest_partmap = 0;
344 grub_partition_iterate (dest_dev->disk, identify_partmap);
346 if (! dest_partmap)
348 grub_util_warn (_("Attempting to install GRUB to a partitionless disk. This is a BAD idea."));
349 goto unable_to_embed;
352 if (strcmp (dest_partmap, "part_msdos") == 0)
353 grub_partition_iterate (dest_dev->disk, find_usable_region_msdos);
354 else if (strcmp (dest_partmap, "part_gpt") == 0)
355 grub_partition_iterate (dest_dev->disk, find_usable_region_gpt);
356 else
357 grub_util_error (_("No DOS-style partitions found"));
359 if (embed_region.end == embed_region.start)
361 if (! strcmp (dest_partmap, "part_msdos"))
362 grub_util_warn (_("This msdos-style partition label has no post-MBR gap; embedding won't be possible!"));
363 else
364 grub_util_warn (_("This GPT partition label has no BIOS Boot Partition; embedding won't be possible!"));
365 goto unable_to_embed;
368 if ((unsigned long) core_sectors > embed_region.end - embed_region.start)
370 if (core_sectors > 62)
371 grub_util_warn (_("Your core.img is unusually large. It won't fit in the embedding area."));
372 else /* embed_region.end - embed_region.start < 62 */
373 grub_util_warn (_("Your embedding area is unusually small. core.img won't fit in it."));
374 goto unable_to_embed;
378 grub_util_info ("the core image will be embedded at sector 0x%llx", embed_region.start);
380 *install_dos_part = grub_cpu_to_le32 (dos_part);
381 *install_bsd_part = grub_cpu_to_le32 (bsd_part);
383 /* The first blocklist contains the whole sectors. */
384 first_block->start = grub_cpu_to_le64 (embed_region.start + 1);
386 /* These are filled elsewhere. Verify them just in case. */
387 assert (first_block->len == grub_host_to_target16 (core_sectors - 1));
388 assert (first_block->segment == grub_host_to_target16 (GRUB_BOOT_MACHINE_KERNEL_SEG
389 + (GRUB_DISK_SECTOR_SIZE >> 4)));
391 /* Make sure that the second blocklist is a terminator. */
392 block = first_block - 1;
393 block->start = 0;
394 block->len = 0;
395 block->segment = 0;
397 /* Write the core image onto the disk. */
398 if (grub_disk_write (dest_dev->disk, embed_region.start, 0, core_size, core_img))
399 grub_util_error ("%s", grub_errmsg);
401 /* FIXME: can this be skipped? */
402 *boot_drive = 0xFF;
404 *kernel_sector = grub_cpu_to_le64 (embed_region.start);
406 /* Write the boot image onto the disk. */
407 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE,
408 boot_img))
409 grub_util_error ("%s", grub_errmsg);
411 goto finish;
413 unable_to_embed:
415 if (must_embed)
416 grub_util_error (_("embedding is not possible, but this is required when "
417 "the root device is on a RAID array or LVM volume"));
419 grub_util_warn (_("Embedding is not possible. GRUB can only be installed in this "
420 "setup by using blocklists. However, blocklists are UNRELIABLE and "
421 "its use is discouraged."));
422 if (! force)
423 grub_util_error (_("if you really want blocklists, use --force"));
425 /* Make sure that GRUB reads the identical image as the OS. */
426 tmp_img = xmalloc (core_size);
427 core_path_dev_full = grub_util_get_path (dir, core_file);
428 core_path_dev = make_system_path_relative_to_its_root (core_path_dev_full);
429 free (core_path_dev_full);
431 /* It is a Good Thing to sync two times. */
432 sync ();
433 sync ();
435 #define MAX_TRIES 5
437 for (i = 0; i < MAX_TRIES; i++)
439 grub_util_info ((i == 0) ? _("attempting to read the core image `%s' from GRUB")
440 : _("attempting to read the core image `%s' from GRUB again"),
441 core_path_dev);
443 grub_disk_cache_invalidate_all ();
445 file = grub_file_open (core_path_dev);
446 if (file)
448 if (grub_file_size (file) != core_size)
449 grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
450 (int) grub_file_size (file), (int) core_size);
451 else if (grub_file_read (file, tmp_img, core_size)
452 != (grub_ssize_t) core_size)
453 grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
454 (int) core_size);
455 else if (memcmp (core_img, tmp_img, core_size) != 0)
457 #if 0
458 FILE *dump;
459 FILE *dump2;
461 dump = fopen ("dump.img", "wb");
462 if (dump)
464 fwrite (tmp_img, 1, core_size, dump);
465 fclose (dump);
468 dump2 = fopen ("dump2.img", "wb");
469 if (dump2)
471 fwrite (core_img, 1, core_size, dump2);
472 fclose (dump2);
475 #endif
476 grub_util_info ("succeeded in opening the core image but the data is different");
478 else
480 grub_file_close (file);
481 break;
484 grub_file_close (file);
486 else
487 grub_util_info ("couldn't open the core image");
489 if (grub_errno)
490 grub_util_info ("error message = %s", grub_errmsg);
492 grub_errno = GRUB_ERR_NONE;
493 sync ();
494 sleep (1);
497 if (i == MAX_TRIES)
498 grub_util_error (_("cannot read `%s' correctly"), core_path_dev);
500 /* Clean out the blocklists. */
501 block = first_block;
502 while (block->len)
504 block->start = 0;
505 block->len = 0;
506 block->segment = 0;
508 block--;
510 if ((char *) block <= core_img)
511 grub_util_error (_("no terminator in the core image"));
514 /* Now read the core image to determine where the sectors are. */
515 file = grub_file_open (core_path_dev);
516 if (! file)
517 grub_util_error ("%s", grub_errmsg);
519 file->read_hook = save_first_sector;
520 if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
521 != GRUB_DISK_SECTOR_SIZE)
522 grub_util_error (_("failed to read the first sector of the core image"));
524 block = first_block;
525 file->read_hook = save_blocklists;
526 if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
527 != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
528 grub_util_error (_("failed to read the rest sectors of the core image"));
530 grub_file_close (file);
532 free (core_path_dev);
533 free (tmp_img);
535 *kernel_sector = grub_cpu_to_le64 (first_sector);
537 /* FIXME: can this be skipped? */
538 *boot_drive = 0xFF;
540 *install_dos_part = grub_cpu_to_le32 (dos_part);
541 *install_bsd_part = grub_cpu_to_le32 (bsd_part);
543 /* Write the first two sectors of the core image onto the disk. */
544 grub_util_info ("opening the core image `%s'", core_path);
545 fp = fopen (core_path, "r+b");
546 if (! fp)
547 grub_util_error (_("cannot open `%s'"), core_path);
549 grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp);
550 fclose (fp);
552 /* Write the boot image onto the disk. */
553 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
554 grub_util_error ("%s", grub_errmsg);
556 finish:
558 /* Sync is a Good Thing. */
559 sync ();
561 free (core_path);
562 free (core_img);
563 free (boot_img);
564 grub_device_close (dest_dev);
565 grub_device_close (root_dev);
568 static struct option options[] =
570 {"boot-image", required_argument, 0, 'b'},
571 {"core-image", required_argument, 0, 'c'},
572 {"directory", required_argument, 0, 'd'},
573 {"device-map", required_argument, 0, 'm'},
574 {"root-device", required_argument, 0, 'r'},
575 {"force", no_argument, 0, 'f'},
576 {"skip-fs-probe", no_argument, 0, 's'},
577 {"help", no_argument, 0, 'h'},
578 {"version", no_argument, 0, 'V'},
579 {"verbose", no_argument, 0, 'v'},
580 {0, 0, 0, 0}
583 static void
584 usage (int status)
586 if (status)
587 fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name);
588 else
589 printf (_("\
590 Usage: %s [OPTION]... DEVICE\n\
592 Set up images to boot from DEVICE.\n\
593 DEVICE must be a GRUB device (e.g. `(hd0,1)').\n\
595 -b, --boot-image=FILE use FILE as the boot image [default=%s]\n\
596 -c, --core-image=FILE use FILE as the core image [default=%s]\n\
597 -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n\
598 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
599 -r, --root-device=DEV use DEV as the root device [default=guessed]\n\
600 -f, --force install even if problems are detected\n\
601 -s, --skip-fs-probe do not probe for filesystems in DEVICE\n\
602 -h, --help display this message and exit\n\
603 -V, --version print version information and exit\n\
604 -v, --verbose print verbose messages\n\
606 Report bugs to <%s>.\n\
608 program_name,
609 DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY,
610 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
612 exit (status);
615 static char *
616 get_device_name (char *dev)
618 size_t len = strlen (dev);
620 if (dev[0] != '(' || dev[len - 1] != ')')
621 return 0;
623 dev[len - 1] = '\0';
624 return dev + 1;
628 main (int argc, char *argv[])
630 char *boot_file = 0;
631 char *core_file = 0;
632 char *dir = 0;
633 char *dev_map = 0;
634 char *root_dev = 0;
635 char *dest_dev;
636 int must_embed = 0, force = 0, fs_probe = 1;
638 set_program_name (argv[0]);
640 grub_util_init_nls ();
642 /* Check for options. */
643 while (1)
645 int c = getopt_long (argc, argv, "b:c:d:m:r:hVvf", options, 0);
647 if (c == -1)
648 break;
649 else
650 switch (c)
652 case 'b':
653 if (boot_file)
654 free (boot_file);
656 boot_file = xstrdup (optarg);
657 break;
659 case 'c':
660 if (core_file)
661 free (core_file);
663 core_file = xstrdup (optarg);
664 break;
666 case 'd':
667 if (dir)
668 free (dir);
670 dir = xstrdup (optarg);
671 break;
673 case 'm':
674 if (dev_map)
675 free (dev_map);
677 dev_map = xstrdup (optarg);
678 break;
680 case 'r':
681 if (root_dev)
682 free (root_dev);
684 root_dev = xstrdup (optarg);
685 break;
687 case 'f':
688 force = 1;
689 break;
691 case 's':
692 fs_probe = 0;
693 break;
695 case 'h':
696 usage (0);
697 break;
699 case 'V':
700 printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
701 return 0;
703 case 'v':
704 verbosity++;
705 break;
707 default:
708 usage (1);
709 break;
713 if (verbosity > 1)
714 grub_env_set ("debug", "all");
716 /* Obtain DEST_DEV. */
717 if (optind >= argc)
719 fprintf (stderr, _("No device is specified.\n"));
720 usage (1);
723 if (optind + 1 != argc)
725 fprintf (stderr, _("Unknown extra argument `%s'.\n"), argv[optind + 1]);
726 usage (1);
729 /* Initialize the emulated biosdisk driver. */
730 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
732 /* Initialize all modules. */
733 grub_init_all ();
735 dest_dev = get_device_name (argv[optind]);
736 if (! dest_dev)
738 /* Possibly, the user specified an OS device file. */
739 dest_dev = grub_util_get_grub_dev (argv[optind]);
740 if (! dest_dev)
742 fprintf (stderr, _("Invalid device `%s'.\n"), argv[optind]);
743 usage (1);
746 else
747 /* For simplicity. */
748 dest_dev = xstrdup (dest_dev);
750 if (root_dev)
752 char *tmp = get_device_name (root_dev);
754 if (! tmp)
755 grub_util_error (_("invalid root device `%s'"), root_dev);
757 tmp = xstrdup (tmp);
758 free (root_dev);
759 root_dev = tmp;
761 else
763 root_dev = grub_util_get_grub_dev (grub_guess_root_device (dir ? : DEFAULT_DIRECTORY));
764 if (! root_dev)
766 grub_util_info ("guessing the root device failed, because of `%s'",
767 grub_errmsg);
768 grub_util_error (_("cannot guess the root device. Specify the option `--root-device'"));
772 #ifdef __linux__
773 if (grub_util_lvm_isvolume (root_dev))
774 must_embed = 1;
776 if (root_dev[0] == 'm' && root_dev[1] == 'd'
777 && root_dev[2] >= '0' && root_dev[2] <= '9')
779 /* FIXME: we can avoid this on RAID1. */
780 must_embed = 1;
783 if (dest_dev[0] == 'm' && dest_dev[1] == 'd'
784 && dest_dev[2] >= '0' && dest_dev[2] <= '9')
786 char **devicelist;
787 int i;
789 devicelist = grub_util_raid_getmembers (dest_dev);
791 for (i = 0; devicelist[i]; i++)
793 setup (dir ? : DEFAULT_DIRECTORY,
794 boot_file ? : DEFAULT_BOOT_FILE,
795 core_file ? : DEFAULT_CORE_FILE,
796 root_dev, grub_util_get_grub_dev (devicelist[i]), 1, force, fs_probe);
799 else
800 #endif
801 /* Do the real work. */
802 setup (dir ? : DEFAULT_DIRECTORY,
803 boot_file ? : DEFAULT_BOOT_FILE,
804 core_file ? : DEFAULT_CORE_FILE,
805 root_dev, dest_dev, must_embed, force, fs_probe);
807 /* Free resources. */
808 grub_fini_all ();
809 grub_util_biosdisk_fini ();
811 free (boot_file);
812 free (core_file);
813 free (dir);
814 free (dev_map);
815 free (root_dev);
816 free (dest_dev);
818 return 0;