2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2/phcoder/solaris.git] / util / i386 / pc / grub-setup.c
blobc536be0c404a3bae8379f3b249b4448ac9698fe9
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 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/util/raid.h>
36 #include <grub/util/lvm.h>
38 static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_PARTITION_TYPE_BIOS_BOOT;
40 #include <grub_setup_init.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <dirent.h>
49 #include <grub/util/getroot.h>
51 #define _GNU_SOURCE 1
52 #include <getopt.h>
54 #define DEFAULT_BOOT_FILE "boot.img"
55 #define DEFAULT_CORE_FILE "core.img"
57 /* This is the blocklist used in the diskboot image. */
58 struct boot_blocklist
60 grub_uint64_t start;
61 grub_uint16_t len;
62 grub_uint16_t segment;
63 } __attribute__ ((packed));
65 void
66 grub_putchar (int c)
68 putchar (c);
71 int
72 grub_getkey (void)
74 return -1;
77 struct grub_handler_class grub_term_input_class;
78 struct grub_handler_class grub_term_output_class;
80 void
81 grub_refresh (void)
83 fflush (stdout);
86 static void
87 setup (const char *dir,
88 const char *boot_file, const char *core_file,
89 const char *root, const char *dest, int must_embed, int force, int fs_probe)
91 char *boot_path, *core_path, *core_path_dev;
92 char *boot_img, *core_img;
93 size_t boot_size, core_size;
94 grub_uint16_t core_sectors;
95 grub_device_t root_dev, dest_dev;
96 const char *dest_partmap;
97 grub_uint8_t *boot_drive;
98 grub_disk_addr_t *kernel_sector;
99 grub_uint16_t *boot_drive_check;
100 struct boot_blocklist *first_block, *block;
101 grub_int32_t *install_dos_part, *install_bsd_part;
102 grub_int32_t dos_part, bsd_part;
103 char *tmp_img;
104 int i;
105 grub_disk_addr_t first_sector;
106 grub_uint16_t current_segment
107 = GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
108 grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
109 grub_file_t file;
110 FILE *fp;
111 struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
112 embed_region.start = embed_region.end = ~0UL;
114 auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
115 unsigned length);
116 auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
117 unsigned length);
119 auto int NESTED_FUNC_ATTR find_usable_region_msdos (grub_disk_t disk,
120 const grub_partition_t p);
121 int NESTED_FUNC_ATTR find_usable_region_msdos (grub_disk_t disk __attribute__ ((unused)),
122 const grub_partition_t p)
124 struct grub_msdos_partition *pcdata = p->data;
126 /* There's always an embed region, and it starts right after the MBR. */
127 embed_region.start = 1;
129 /* For its end offset, include as many dummy partitions as we can. */
130 if (! grub_msdos_partition_is_empty (pcdata->dos_type)
131 && ! grub_msdos_partition_is_bsd (pcdata->dos_type)
132 && embed_region.end > p->start)
133 embed_region.end = p->start;
135 return 0;
138 auto int NESTED_FUNC_ATTR find_usable_region_gpt (grub_disk_t disk,
139 const grub_partition_t p);
140 int NESTED_FUNC_ATTR find_usable_region_gpt (grub_disk_t disk __attribute__ ((unused)),
141 const grub_partition_t p)
143 struct grub_gpt_partentry *gptdata = p->data;
145 /* If there's an embed region, it is in a dedicated partition. */
146 if (! memcmp (&gptdata->type, &grub_gpt_partition_type_bios_boot, 16))
148 embed_region.start = p->start;
149 embed_region.end = p->start + p->len;
151 return 1;
154 return 0;
157 void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
158 unsigned length)
160 grub_util_info ("the first sector is <%llu,%u,%u>",
161 sector, offset, length);
163 if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
164 grub_util_error ("The first sector of the core file is not sector-aligned");
166 first_sector = sector;
169 void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
170 unsigned length)
172 struct boot_blocklist *prev = block + 1;
174 grub_util_info ("saving <%llu,%u,%u> with the segment 0x%x",
175 sector, offset, length, (unsigned) current_segment);
177 if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
178 grub_util_error ("Non-sector-aligned data is found in the core file");
180 if (block != first_block
181 && (grub_le_to_cpu64 (prev->start)
182 + grub_le_to_cpu16 (prev->len)) == sector)
183 prev->len = grub_cpu_to_le16 (grub_le_to_cpu16 (prev->len) + 1);
184 else
186 block->start = grub_cpu_to_le64 (sector);
187 block->len = grub_cpu_to_le16 (1);
188 block->segment = grub_cpu_to_le16 (current_segment);
190 block--;
191 if (block->len)
192 grub_util_error ("The sectors of the core file are too fragmented");
195 last_length = length;
196 current_segment += GRUB_DISK_SECTOR_SIZE >> 4;
199 /* Read the boot image by the OS service. */
200 boot_path = grub_util_get_path (dir, boot_file);
201 boot_size = grub_util_get_image_size (boot_path);
202 if (boot_size != GRUB_DISK_SECTOR_SIZE)
203 grub_util_error ("The size of `%s' is not %d",
204 boot_path, GRUB_DISK_SECTOR_SIZE);
205 boot_img = grub_util_read_image (boot_path);
206 free (boot_path);
208 /* Set the addresses of variables in the boot image. */
209 boot_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_BOOT_DRIVE);
210 kernel_sector = (grub_disk_addr_t *) (boot_img
211 + GRUB_BOOT_MACHINE_KERNEL_SECTOR);
212 boot_drive_check = (grub_uint16_t *) (boot_img
213 + GRUB_BOOT_MACHINE_DRIVE_CHECK);
215 core_path = grub_util_get_path (dir, core_file);
216 core_size = grub_util_get_image_size (core_path);
217 core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
218 >> GRUB_DISK_SECTOR_BITS);
219 if (core_size < GRUB_DISK_SECTOR_SIZE)
220 grub_util_error ("The size of `%s' is too small", core_path);
221 else if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
222 grub_util_error ("The size of `%s' is too large", core_path);
224 core_img = grub_util_read_image (core_path);
226 /* Have FIRST_BLOCK to point to the first blocklist. */
227 first_block = (struct boot_blocklist *) (core_img
228 + GRUB_DISK_SECTOR_SIZE
229 - sizeof (*block));
231 install_dos_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
232 + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART);
233 install_bsd_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
234 + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART);
236 /* Open the root device and the destination device. */
237 root_dev = grub_device_open (root);
238 if (! root_dev)
239 grub_util_error ("%s", grub_errmsg);
241 dest_dev = grub_device_open (dest);
242 if (! dest_dev)
243 grub_util_error ("%s", grub_errmsg);
245 grub_util_info ("setting the root device to `%s'", root);
246 if (grub_env_set ("root", root) != GRUB_ERR_NONE)
247 grub_util_error ("%s", grub_errmsg);
249 /* Read the original sector from the disk. */
250 tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
251 if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
252 grub_util_error ("%s", grub_errmsg);
254 if (dest_dev->disk->partition && fs_probe)
256 grub_fs_t fs;
257 fs = grub_fs_probe (dest_dev);
258 if (! fs)
259 grub_util_error ("Unable to identify a filesystem in %s; safety check can't be performed.",
260 dest_dev->disk->name);
262 if (! fs->reserved_first_sector)
263 grub_util_error ("%s appears to contain a %s filesystem which isn't known to "
264 "reserve space for DOS-style boot. Installing GRUB there could "
265 "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
266 "by grub-setup (--skip-fs-probe disables this "
267 "check, use at your own risk).", dest_dev->disk->name, fs->name);
270 /* Copy the possible DOS BPB. */
271 memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
272 tmp_img + GRUB_BOOT_MACHINE_BPB_START,
273 GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
275 /* Copy the possible partition table. */
276 if (dest_dev->disk->has_partitions)
277 memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
278 tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
279 GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
281 free (tmp_img);
283 /* If DEST_DRIVE is a hard disk, enable the workaround, which is
284 for buggy BIOSes which don't pass boot drive correctly. Instead,
285 they pass 0x00 or 0x01 even when booted from 0x80. */
286 if (dest_dev->disk->id & 0x80)
287 /* Replace the jmp (2 bytes) with double nop's. */
288 *boot_drive_check = 0x9090;
290 /* If we hardcoded drive as part of prefix, we don't want to
291 override the current setting. */
292 if (*install_dos_part != -2)
294 /* Embed information about the installed location. */
295 if (root_dev->disk->partition)
297 if (strcmp (root_dev->disk->partition->partmap->name,
298 "part_msdos") == 0)
300 struct grub_msdos_partition *pcdata =
301 root_dev->disk->partition->data;
302 dos_part = pcdata->dos_part;
303 bsd_part = pcdata->bsd_part;
305 else if (strcmp (root_dev->disk->partition->partmap->name,
306 "part_gpt") == 0)
308 dos_part = root_dev->disk->partition->index;
309 bsd_part = -1;
311 else
312 grub_util_error ("No PC style partitions found");
314 else
315 dos_part = bsd_part = -1;
317 else
319 dos_part = grub_le_to_cpu32 (*install_dos_part);
320 bsd_part = grub_le_to_cpu32 (*install_bsd_part);
323 grub_util_info ("dos partition is %d, bsd partition is %d",
324 dos_part, bsd_part);
326 if (! dest_dev->disk->has_partitions)
328 grub_util_warn ("Attempting to install GRUB to a partitionless disk. This is a BAD idea.");
329 goto unable_to_embed;
332 if (dest_dev->disk->partition)
334 grub_util_warn ("Attempting to install GRUB to a partition instead of the MBR. This is a BAD idea.");
335 goto unable_to_embed;
338 /* Unlike root_dev, with dest_dev we're interested in the partition map even
339 if dest_dev itself is a whole disk. */
340 auto int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk,
341 const grub_partition_t p);
342 int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk __attribute__ ((unused)),
343 const grub_partition_t p)
345 dest_partmap = p->partmap->name;
346 return 1;
348 dest_partmap = 0;
349 grub_partition_iterate (dest_dev->disk, identify_partmap);
351 if (! dest_partmap)
353 grub_util_warn ("Attempting to install GRUB to a partitionless disk. This is a BAD idea.");
354 goto unable_to_embed;
357 grub_partition_iterate (dest_dev->disk, (strcmp (dest_partmap, "part_msdos") ?
358 find_usable_region_gpt : find_usable_region_msdos));
360 if (embed_region.end == embed_region.start)
362 if (! strcmp (dest_partmap, "part_msdos"))
363 grub_util_warn ("This msdos-style partition label has no post-MBR gap; embedding won't be possible!");
364 else
365 grub_util_warn ("This GPT partition label has no BIOS Boot Partition; embedding won't be possible!");
366 goto unable_to_embed;
369 if ((unsigned long) core_sectors > embed_region.end - embed_region.start)
371 if (core_sectors > 62)
372 grub_util_warn ("Your core.img is unusually large. It won't fit in the embedding area.");
373 else if (embed_region.end - embed_region.start < 62)
374 grub_util_warn ("Your embedding area is unusually small. core.img won't fit in it.");
375 else
376 grub_util_warn ("Embedding area is too small for core.img.");
377 goto unable_to_embed;
381 grub_util_info ("will embed the core image at sector 0x%llx", embed_region.start);
383 *install_dos_part = grub_cpu_to_le32 (dos_part);
384 *install_bsd_part = grub_cpu_to_le32 (bsd_part);
386 /* The first blocklist contains the whole sectors. */
387 first_block->start = grub_cpu_to_le64 (embed_region.start + 1);
388 first_block->len = grub_cpu_to_le16 (core_sectors - 1);
389 first_block->segment
390 = grub_cpu_to_le16 (GRUB_BOOT_MACHINE_KERNEL_SEG
391 + (GRUB_DISK_SECTOR_SIZE >> 4));
393 /* Make sure that the second blocklist is a terminator. */
394 block = first_block - 1;
395 block->start = 0;
396 block->len = 0;
397 block->segment = 0;
399 /* Write the core image onto the disk. */
400 if (grub_disk_write (dest_dev->disk, embed_region.start, 0, core_size, core_img))
401 grub_util_error ("%s", grub_errmsg);
403 /* FIXME: can this be skipped? */
404 *boot_drive = 0xFF;
406 *kernel_sector = grub_cpu_to_le64 (embed_region.start);
408 /* Write the boot image onto the disk. */
409 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE,
410 boot_img))
411 grub_util_error ("%s", grub_errmsg);
413 goto finish;
415 unable_to_embed:
417 if (must_embed)
418 grub_util_error ("Embedding is not possible, but this is required when "
419 "the root device is on a RAID array or LVM volume.");
421 grub_util_warn ("Embedding is not possible. GRUB can only be installed in this "
422 "setup by using blocklists. However, blocklists are UNRELIABLE and "
423 "its use is discouraged.");
424 if (! force)
425 grub_util_error ("If you really want blocklists, use --force.");
427 /* Make sure that GRUB reads the identical image as the OS. */
428 tmp_img = xmalloc (core_size);
429 core_path_dev = grub_util_get_path (dir, core_file);
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 ("attempting to read the core image `%s' from GRUB%s",
440 core_path_dev, (i == 0) ? "" : " again");
442 grub_disk_cache_invalidate_all ();
444 file = grub_file_open (core_path_dev);
445 if (file)
447 if (grub_file_size (file) != core_size)
448 grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
449 (int) grub_file_size (file), (int) core_size);
450 else if (grub_file_read (file, tmp_img, core_size)
451 != (grub_ssize_t) core_size)
452 grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
453 (int) core_size);
454 else if (memcmp (core_img, tmp_img, core_size) != 0)
456 #if 0
457 FILE *dump;
458 FILE *dump2;
460 dump = fopen ("dump.img", "wb");
461 if (dump)
463 fwrite (tmp_img, 1, core_size, dump);
464 fclose (dump);
467 dump2 = fopen ("dump2.img", "wb");
468 if (dump2)
470 fwrite (core_img, 1, core_size, dump2);
471 fclose (dump2);
474 #endif
475 grub_util_info ("succeeded in opening the core image but the data is different");
477 else
479 grub_file_close (file);
480 break;
483 grub_file_close (file);
485 else
486 grub_util_info ("couldn't open the core image");
488 if (grub_errno)
489 grub_util_info ("error message = %s", grub_errmsg);
491 grub_errno = GRUB_ERR_NONE;
492 sync ();
493 sleep (1);
496 if (i == MAX_TRIES)
497 grub_util_error ("Cannot read `%s' correctly", core_path_dev);
499 /* Clean out the blocklists. */
500 block = first_block;
501 while (block->len)
503 block->start = 0;
504 block->len = 0;
505 block->segment = 0;
507 block--;
509 if ((char *) block <= core_img)
510 grub_util_error ("No terminator in the core image");
513 /* Now read the core image to determine where the sectors are. */
514 file = grub_file_open (core_path_dev);
515 if (! file)
516 grub_util_error ("%s", grub_errmsg);
518 file->read_hook = save_first_sector;
519 if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
520 != GRUB_DISK_SECTOR_SIZE)
521 grub_util_error ("Failed to read the first sector of the core image");
523 block = first_block;
524 file->read_hook = save_blocklists;
525 if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
526 != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
527 grub_util_error ("Failed to read the rest sectors of the core image");
529 grub_file_close (file);
531 free (core_path_dev);
532 free (tmp_img);
534 *kernel_sector = grub_cpu_to_le64 (first_sector);
536 /* FIXME: can this be skipped? */
537 *boot_drive = 0xFF;
539 *install_dos_part = grub_cpu_to_le32 (dos_part);
540 *install_bsd_part = grub_cpu_to_le32 (bsd_part);
542 /* Write the first two sectors of the core image onto the disk. */
543 grub_util_info ("opening the core image `%s'", core_path);
544 fp = fopen (core_path, "r+b");
545 if (! fp)
546 grub_util_error ("Cannot open `%s'", core_path);
548 grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp);
549 fclose (fp);
551 /* Write the boot image onto the disk. */
552 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
553 grub_util_error ("%s", grub_errmsg);
555 finish:
557 /* Sync is a Good Thing. */
558 sync ();
560 free (core_path);
561 free (core_img);
562 free (boot_img);
563 grub_device_close (dest_dev);
564 grub_device_close (root_dev);
567 static struct option options[] =
569 {"boot-image", required_argument, 0, 'b'},
570 {"core-image", required_argument, 0, 'c'},
571 {"directory", required_argument, 0, 'd'},
572 {"device-map", required_argument, 0, 'm'},
573 {"root-device", required_argument, 0, 'r'},
574 {"force", no_argument, 0, 'f'},
575 {"skip-fs-probe", no_argument, 0, 's'},
576 {"help", no_argument, 0, 'h'},
577 {"version", no_argument, 0, 'V'},
578 {"verbose", no_argument, 0, 'v'},
579 {0, 0, 0, 0}
582 static void
583 usage (int status)
585 if (status)
586 fprintf (stderr, "Try ``grub-setup --help'' for more information.\n");
587 else
588 printf ("\
589 Usage: grub-setup [OPTION]... DEVICE\n\
591 Set up images to boot from DEVICE.\n\
592 DEVICE must be a GRUB device (e.g. ``(hd0,1)'').\n\
594 -b, --boot-image=FILE use FILE as the boot image [default=%s]\n\
595 -c, --core-image=FILE use FILE as the core image [default=%s]\n\
596 -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n\
597 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
598 -r, --root-device=DEV use DEV as the root device [default=guessed]\n\
599 -f, --force install even if problems are detected\n\
600 -s, --skip-fs-probe do not probe for filesystems in DEVICE\n\
601 -h, --help display this message and exit\n\
602 -V, --version print version information and exit\n\
603 -v, --verbose print verbose messages\n\
605 Report bugs to <%s>.\n\
607 DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY,
608 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
610 exit (status);
613 static char *
614 get_device_name (char *dev)
616 size_t len = strlen (dev);
618 if (dev[0] != '(' || dev[len - 1] != ')')
619 return 0;
621 dev[len - 1] = '\0';
622 return dev + 1;
626 main (int argc, char *argv[])
628 char *boot_file = 0;
629 char *core_file = 0;
630 char *dir = 0;
631 char *dev_map = 0;
632 char *root_dev = 0;
633 char *dest_dev;
634 int must_embed = 0, force = 0, fs_probe = 1;
636 progname = "grub-setup";
638 /* Check for options. */
639 while (1)
641 int c = getopt_long (argc, argv, "b:c:d:m:r:hVvf", options, 0);
643 if (c == -1)
644 break;
645 else
646 switch (c)
648 case 'b':
649 if (boot_file)
650 free (boot_file);
652 boot_file = xstrdup (optarg);
653 break;
655 case 'c':
656 if (core_file)
657 free (core_file);
659 core_file = xstrdup (optarg);
660 break;
662 case 'd':
663 if (dir)
664 free (dir);
666 dir = xstrdup (optarg);
667 break;
669 case 'm':
670 if (dev_map)
671 free (dev_map);
673 dev_map = xstrdup (optarg);
674 break;
676 case 'r':
677 if (root_dev)
678 free (root_dev);
680 root_dev = xstrdup (optarg);
681 break;
683 case 'f':
684 force = 1;
685 break;
687 case 's':
688 fs_probe = 0;
689 break;
691 case 'h':
692 usage (0);
693 break;
695 case 'V':
696 printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
697 return 0;
699 case 'v':
700 verbosity++;
701 break;
703 default:
704 usage (1);
705 break;
709 if (verbosity > 1)
710 grub_env_set ("debug", "all");
712 /* Obtain DEST_DEV. */
713 if (optind >= argc)
715 fprintf (stderr, "No device is specified.\n");
716 usage (1);
719 if (optind + 1 != argc)
721 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
722 usage (1);
725 /* Initialize the emulated biosdisk driver. */
726 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
728 /* Initialize all modules. */
729 grub_init_all ();
731 dest_dev = get_device_name (argv[optind]);
732 if (! dest_dev)
734 /* Possibly, the user specified an OS device file. */
735 dest_dev = grub_util_get_grub_dev (argv[optind]);
736 if (! dest_dev)
738 fprintf (stderr, "Invalid device `%s'.\n", argv[optind]);
739 usage (1);
742 else
743 /* For simplicity. */
744 dest_dev = xstrdup (dest_dev);
746 if (root_dev)
748 char *tmp = get_device_name (root_dev);
750 if (! tmp)
751 grub_util_error ("Invalid root device `%s'", root_dev);
753 tmp = xstrdup (tmp);
754 free (root_dev);
755 root_dev = tmp;
757 else
759 root_dev = grub_util_get_grub_dev (grub_guess_root_device (dir ? : DEFAULT_DIRECTORY));
760 if (! root_dev)
762 grub_util_info ("guessing the root device failed, because of `%s'",
763 grub_errmsg);
764 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
768 #ifdef __linux__
769 if (grub_util_lvm_isvolume (root_dev))
770 must_embed = 1;
772 if (root_dev[0] == 'm' && root_dev[1] == 'd'
773 && root_dev[2] >= '0' && root_dev[2] <= '9')
775 /* FIXME: we can avoid this on RAID1. */
776 must_embed = 1;
779 if (dest_dev[0] == 'm' && dest_dev[1] == 'd'
780 && dest_dev[2] >= '0' && dest_dev[2] <= '9')
782 char **devicelist;
783 int i;
785 devicelist = grub_util_raid_getmembers (dest_dev);
787 for (i = 0; devicelist[i]; i++)
789 setup (dir ? : DEFAULT_DIRECTORY,
790 boot_file ? : DEFAULT_BOOT_FILE,
791 core_file ? : DEFAULT_CORE_FILE,
792 root_dev, grub_util_get_grub_dev (devicelist[i]), 1, force, fs_probe);
795 else
796 #endif
797 /* Do the real work. */
798 setup (dir ? : DEFAULT_DIRECTORY,
799 boot_file ? : DEFAULT_BOOT_FILE,
800 core_file ? : DEFAULT_CORE_FILE,
801 root_dev, dest_dev, must_embed, force, fs_probe);
803 /* Free resources. */
804 grub_fini_all ();
805 grub_util_biosdisk_fini ();
807 free (boot_file);
808 free (core_file);
809 free (dir);
810 free (dev_map);
811 free (root_dev);
812 free (dest_dev);
814 return 0;