make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / util / i386 / pc / grub-setup.c
blob8fbe3e710a79923811603ce0d53c00c4269bd117
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/pc_partition.h>
29 #include <grub/gpt_partition.h>
30 #include <grub/env.h>
31 #include <grub/util/biosdisk.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 grub_term_t
78 grub_term_get_current (void)
80 return 0;
83 void
84 grub_refresh (void)
88 static void
89 setup (const char *prefix, const char *dir,
90 const char *boot_file, const char *core_file,
91 const char *root, const char *dest, int must_embed)
93 char *boot_path, *core_path;
94 char *boot_img, *core_img;
95 size_t boot_size, core_size;
96 grub_uint16_t core_sectors;
97 grub_device_t root_dev, dest_dev;
98 grub_uint8_t *boot_drive, *root_drive;
99 grub_disk_addr_t *kernel_sector;
100 grub_uint16_t *boot_drive_check;
101 struct boot_blocklist *first_block, *block;
102 grub_int32_t *install_dos_part, *install_bsd_part;
103 char *install_prefix;
104 char *tmp_img;
105 int i;
106 grub_disk_addr_t first_sector;
107 grub_uint16_t current_segment
108 = GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
109 grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
110 grub_file_t file;
111 FILE *fp;
112 struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
113 embed_region.start = embed_region.end = ~0UL;
114 int able_to_embed = 1;
116 auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
117 unsigned length);
118 auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
119 unsigned length);
121 auto int find_usable_region (grub_disk_t disk,
122 const grub_partition_t p);
123 int find_usable_region (grub_disk_t disk __attribute__ ((unused)),
124 const grub_partition_t p)
126 if (! strcmp (p->partmap->name, "pc_partition_map"))
128 struct grub_pc_partition *pcdata = p->data;
130 /* There's always an embed region, and it starts right after the MBR. */
131 embed_region.start = 1;
133 /* For its end offset, include as many dummy partitions as we can. */
134 if (! grub_pc_partition_is_empty (pcdata->dos_type)
135 && ! grub_pc_partition_is_bsd (pcdata->dos_type)
136 && embed_region.end > p->start)
137 embed_region.end = p->start;
139 else
141 struct grub_gpt_partentry *gptdata = p->data;
143 /* If there's an embed region, it is in a dedicated partition. */
144 if (! memcmp (&gptdata->type, &grub_gpt_partition_type_bios_boot, 16))
146 embed_region.start = p->start;
147 embed_region.end = p->start + p->len;
149 return 1;
153 return 0;
156 void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
157 unsigned length)
159 grub_util_info ("the first sector is <%llu,%u,%u>",
160 sector, offset, length);
162 if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
163 grub_util_error ("The first sector of the core file is not sector-aligned");
165 first_sector = sector;
168 void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
169 unsigned length)
171 struct boot_blocklist *prev = block + 1;
173 grub_util_info ("saving <%llu,%u,%u> with the segment 0x%x",
174 sector, offset, length, (unsigned) current_segment);
176 if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
177 grub_util_error ("Non-sector-aligned data is found in the core file");
179 if (block != first_block
180 && (grub_le_to_cpu64 (prev->start)
181 + grub_le_to_cpu16 (prev->len)) == sector)
182 prev->len = grub_cpu_to_le16 (grub_le_to_cpu16 (prev->len) + 1);
183 else
185 block->start = grub_cpu_to_le64 (sector);
186 block->len = grub_cpu_to_le16 (1);
187 block->segment = grub_cpu_to_le16 (current_segment);
189 block--;
190 if (block->len)
191 grub_util_error ("The sectors of the core file are too fragmented");
194 last_length = length;
195 current_segment += GRUB_DISK_SECTOR_SIZE >> 4;
198 /* Read the boot image by the OS service. */
199 boot_path = grub_util_get_path (dir, boot_file);
200 boot_size = grub_util_get_image_size (boot_path);
201 if (boot_size != GRUB_DISK_SECTOR_SIZE)
202 grub_util_error ("The size of `%s' is not %d",
203 boot_path, GRUB_DISK_SECTOR_SIZE);
204 boot_img = grub_util_read_image (boot_path);
205 free (boot_path);
207 /* Set the addresses of variables in the boot image. */
208 boot_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_BOOT_DRIVE);
209 root_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_ROOT_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);
225 free (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
230 - sizeof (*block));
232 install_dos_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
233 + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART);
234 install_bsd_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
235 + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART);
236 install_prefix = (core_img + GRUB_DISK_SECTOR_SIZE
237 + GRUB_KERNEL_MACHINE_PREFIX);
239 /* Open the root device and the destination device. */
240 root_dev = grub_device_open (root);
241 if (! root_dev)
242 grub_util_error ("%s", grub_errmsg);
244 dest_dev = grub_device_open (dest);
245 if (! dest_dev)
246 grub_util_error ("%s", grub_errmsg);
248 grub_util_info ("setting the root device to `%s'", root);
249 if (grub_env_set ("root", root) != GRUB_ERR_NONE)
250 grub_util_error ("%s", grub_errmsg);
252 /* Read the original sector from the disk. */
253 tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
254 if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
255 grub_util_error ("%s", grub_errmsg);
257 /* Copy the possible DOS BPB. */
258 memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
259 tmp_img + GRUB_BOOT_MACHINE_BPB_START,
260 GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
262 /* Copy the possible partition table. */
263 if (dest_dev->disk->has_partitions)
264 memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
265 tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
266 GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
268 free (tmp_img);
270 /* If DEST_DRIVE is a hard disk, enable the workaround, which is
271 for buggy BIOSes which don't pass boot drive correctly. Instead,
272 they pass 0x00 or 0x01 even when booted from 0x80. */
273 if (dest_dev->disk->id & 0x80)
274 /* Replace the jmp (2 bytes) with double nop's. */
275 *boot_drive_check = 0x9090;
277 /* If the destination device can have partitions and it is the MBR,
278 try to embed the core image into after the MBR. */
279 if (dest_dev->disk->has_partitions && ! dest_dev->disk->partition)
281 grub_partition_iterate (dest_dev->disk, find_usable_region);
283 /* If there is enough space... */
284 if ((unsigned long) core_sectors <= embed_region.end - embed_region.start)
286 grub_util_info ("will embed the core image at sector 0x%llx", embed_region.start);
288 /* The first blocklist contains the whole sectors. */
289 first_block->start = grub_cpu_to_le64 (embed_region.start + 1);
290 first_block->len = grub_cpu_to_le16 (core_sectors - 1);
291 first_block->segment
292 = grub_cpu_to_le16 (GRUB_BOOT_MACHINE_KERNEL_SEG
293 + (GRUB_DISK_SECTOR_SIZE >> 4));
295 /* Make sure that the second blocklist is a terminator. */
296 block = first_block - 1;
297 block->start = 0;
298 block->len = 0;
299 block->segment = 0;
301 /* Embed information about the installed location. */
302 if (must_embed)
303 *install_dos_part = *install_bsd_part = grub_cpu_to_le32 (-2);
304 else if (root_dev->disk->partition)
306 if (strcmp (root_dev->disk->partition->partmap->name,
307 "pc_partition_map") == 0)
309 struct grub_pc_partition *pcdata =
310 root_dev->disk->partition->data;
311 *install_dos_part
312 = grub_cpu_to_le32 (pcdata->dos_part);
313 *install_bsd_part
314 = grub_cpu_to_le32 (pcdata->bsd_part);
316 else if (strcmp (root_dev->disk->partition->partmap->name,
317 "gpt_partition_map") == 0)
319 *install_dos_part = grub_cpu_to_le32 (root_dev->disk->partition->index);
320 *install_bsd_part = grub_cpu_to_le32 (-1);
322 else
323 grub_util_error ("No PC style partitions found");
325 else
326 *install_dos_part = *install_bsd_part = grub_cpu_to_le32 (-1);
328 grub_util_info ("dos partition is %d, bsd partition is %d, prefix is %s",
329 grub_le_to_cpu32 (*install_dos_part),
330 grub_le_to_cpu32 (*install_bsd_part),
331 prefix);
332 strcpy (install_prefix, prefix);
334 /* Write the core image onto the disk. */
335 if (grub_disk_write (dest_dev->disk, embed_region.start, 0, core_size, core_img))
336 grub_util_error ("%s", grub_errmsg);
338 /* The boot image and the core image are on the same drive,
339 so there is no need to specify the boot drive explicitly. */
340 *boot_drive = 0xff;
341 *kernel_sector = grub_cpu_to_le64 (embed_region.start);
343 /* If the root device is different from the destination device,
344 it is necessary to embed the root drive explicitly. */
345 if (root_dev->disk->id != dest_dev->disk->id)
346 *root_drive = (grub_uint8_t) root_dev->disk->id;
347 else
348 *root_drive = 0xFF;
350 /* Write the boot image onto the disk. */
351 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE,
352 boot_img))
353 grub_util_error ("%s", grub_errmsg);
355 goto finish;
357 else
358 able_to_embed = 0;
360 else
361 able_to_embed = 0;
363 if (must_embed && ! able_to_embed)
364 grub_util_error ("Can't embed the core image, but this is required when\n"
365 "the root device is on a RAID array or LVM volume.");
367 /* The core image must be put on a filesystem unfortunately. */
368 grub_util_info ("will leave the core image on the filesystem");
370 /* Make sure that GRUB reads the identical image as the OS. */
371 tmp_img = xmalloc (core_size);
372 core_path = grub_util_get_path (prefix, core_file);
374 /* It is a Good Thing to sync two times. */
375 sync ();
376 sync ();
378 #define MAX_TRIES 5
380 for (i = 0; i < MAX_TRIES; i++)
382 grub_util_info ("attempting to read the core image `%s' from GRUB%s",
383 core_path, (i == 0) ? "" : " again");
385 grub_disk_cache_invalidate_all ();
387 file = grub_file_open (core_path);
388 if (file)
390 if (grub_file_size (file) != core_size)
391 grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
392 (int) grub_file_size (file), (int) core_size);
393 else if (grub_file_read (file, tmp_img, core_size)
394 != (grub_ssize_t) core_size)
395 grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
396 (int) core_size);
397 else if (memcmp (core_img, tmp_img, core_size) != 0)
399 #if 0
400 FILE *dump;
401 FILE *dump2;
403 dump = fopen ("dump.img", "wb");
404 if (dump)
406 fwrite (tmp_img, 1, core_size, dump);
407 fclose (dump);
410 dump2 = fopen ("dump2.img", "wb");
411 if (dump2)
413 fwrite (core_img, 1, core_size, dump2);
414 fclose (dump2);
417 #endif
418 grub_util_info ("succeeded in opening the core image but the data is different");
420 else
422 grub_file_close (file);
423 break;
426 grub_file_close (file);
428 else
429 grub_util_info ("couldn't open the core image");
431 if (grub_errno)
432 grub_util_info ("error message = %s", grub_errmsg);
434 grub_errno = GRUB_ERR_NONE;
435 sync ();
436 sleep (1);
439 if (i == MAX_TRIES)
440 grub_util_error ("Cannot read `%s' correctly", core_path);
442 /* Clean out the blocklists. */
443 block = first_block;
444 while (block->len)
446 block->start = 0;
447 block->len = 0;
448 block->segment = 0;
450 block--;
452 if ((char *) block <= core_img)
453 grub_util_error ("No terminator in the core image");
456 /* Now read the core image to determine where the sectors are. */
457 file = grub_file_open (core_path);
458 if (! file)
459 grub_util_error ("%s", grub_errmsg);
461 file->read_hook = save_first_sector;
462 if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
463 != GRUB_DISK_SECTOR_SIZE)
464 grub_util_error ("Failed to read the first sector of the core image");
466 block = first_block;
467 file->read_hook = save_blocklists;
468 if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
469 != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
470 grub_util_error ("Failed to read the rest sectors of the core image");
472 grub_file_close (file);
474 free (core_path);
475 free (tmp_img);
477 *kernel_sector = grub_cpu_to_le64 (first_sector);
479 /* If the destination device is different from the root device,
480 it is necessary to embed the boot drive explicitly. */
481 if (root_dev->disk->id != dest_dev->disk->id)
482 *boot_drive = (grub_uint8_t) root_dev->disk->id;
483 else
484 *boot_drive = 0xFF;
486 /* When the core image is not embedded, the root device always follows
487 the boot device. */
488 *root_drive = 0xFF;
490 /* Embed information about the installed location. */
491 if (root_dev->disk->partition)
493 struct grub_pc_partition *pcdata =
494 root_dev->disk->partition->data;
496 if (strcmp (root_dev->disk->partition->partmap->name,
497 "pc_partition_map") == 0)
499 *install_dos_part
500 = grub_cpu_to_le32 (pcdata->dos_part);
501 *install_bsd_part
502 = grub_cpu_to_le32 (pcdata->bsd_part);
504 else if (strcmp (root_dev->disk->partition->partmap->name,
505 "gpt_partition_map") == 0)
507 *install_dos_part = grub_cpu_to_le32 (root_dev->disk->partition->index);
508 *install_bsd_part = grub_cpu_to_le32 (-1);
510 else
511 grub_util_error ("No PC style partitions found");
513 else
514 *install_dos_part = *install_bsd_part = grub_cpu_to_le32 (-1);
516 grub_util_info ("dos partition is %d, bsd partition is %d, prefix is %s",
517 grub_le_to_cpu32 (*install_dos_part),
518 grub_le_to_cpu32 (*install_bsd_part),
519 prefix);
520 strcpy (install_prefix, prefix);
522 /* Write the first two sectors of the core image onto the disk. */
523 core_path = grub_util_get_path (dir, core_file);
524 grub_util_info ("opening the core image `%s'", core_path);
525 fp = fopen (core_path, "r+b");
526 if (! fp)
527 grub_util_error ("Cannot open `%s'", core_path);
529 grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp);
530 fclose (fp);
531 free (core_path);
533 /* Write the boot image onto the disk. */
534 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
535 grub_util_error ("%s", grub_errmsg);
537 finish:
539 /* Sync is a Good Thing. */
540 sync ();
542 free (core_img);
543 free (boot_img);
544 grub_device_close (dest_dev);
545 grub_device_close (root_dev);
548 static struct option options[] =
550 {"boot-image", required_argument, 0, 'b'},
551 {"core-image", required_argument, 0, 'c'},
552 {"directory", required_argument, 0, 'd'},
553 {"device-map", required_argument, 0, 'm'},
554 {"root-device", required_argument, 0, 'r'},
555 {"help", no_argument, 0, 'h'},
556 {"version", no_argument, 0, 'V'},
557 {"verbose", no_argument, 0, 'v'},
558 {0, 0, 0, 0}
561 static void
562 usage (int status)
564 if (status)
565 fprintf (stderr, "Try ``grub-setup --help'' for more information.\n");
566 else
567 printf ("\
568 Usage: grub-setup [OPTION]... DEVICE\n\
570 Set up images to boot from DEVICE.\n\
571 DEVICE must be a GRUB device (e.g. ``(hd0,1)'').\n\
573 -b, --boot-image=FILE use FILE as the boot image [default=%s]\n\
574 -c, --core-image=FILE use FILE as the core image [default=%s]\n\
575 -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n\
576 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
577 -r, --root-device=DEV use DEV as the root device [default=guessed]\n\
578 -h, --help display this message and exit\n\
579 -V, --version print version information and exit\n\
580 -v, --verbose print verbose messages\n\
582 Report bugs to <%s>.\n\
584 DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY,
585 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
587 exit (status);
590 static char *
591 get_device_name (char *dev)
593 size_t len = strlen (dev);
595 if (dev[0] != '(' || dev[len - 1] != ')')
596 return 0;
598 dev[len - 1] = '\0';
599 return dev + 1;
603 main (int argc, char *argv[])
605 char *boot_file = 0;
606 char *core_file = 0;
607 char *dir = 0;
608 char *dev_map = 0;
609 char *root_dev = 0;
610 char *prefix;
611 char *dest_dev;
612 int must_embed = 0;
614 progname = "grub-setup";
616 /* Check for options. */
617 while (1)
619 int c = getopt_long (argc, argv, "b:c:d:m:r:hVv", options, 0);
621 if (c == -1)
622 break;
623 else
624 switch (c)
626 case 'b':
627 if (boot_file)
628 free (boot_file);
630 boot_file = xstrdup (optarg);
631 break;
633 case 'c':
634 if (core_file)
635 free (core_file);
637 core_file = xstrdup (optarg);
638 break;
640 case 'd':
641 if (dir)
642 free (dir);
644 dir = xstrdup (optarg);
645 break;
647 case 'm':
648 if (dev_map)
649 free (dev_map);
651 dev_map = xstrdup (optarg);
652 break;
654 case 'r':
655 if (root_dev)
656 free (root_dev);
658 root_dev = xstrdup (optarg);
659 break;
661 case 'h':
662 usage (0);
663 break;
665 case 'V':
666 printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
667 return 0;
669 case 'v':
670 verbosity++;
671 break;
673 default:
674 usage (1);
675 break;
679 if (verbosity > 1)
680 grub_env_set ("debug", "all");
682 /* Obtain DEST_DEV. */
683 if (optind >= argc)
685 fprintf (stderr, "No device is specified.\n");
686 usage (1);
689 if (optind + 1 != argc)
691 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
692 usage (1);
695 /* Initialize the emulated biosdisk driver. */
696 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
698 /* Initialize all modules. */
699 grub_init_all ();
701 dest_dev = get_device_name (argv[optind]);
702 if (! dest_dev)
704 /* Possibly, the user specified an OS device file. */
705 dest_dev = grub_util_get_grub_dev (argv[optind]);
706 if (! dest_dev)
708 fprintf (stderr, "Invalid device `%s'.\n", argv[optind]);
709 usage (1);
712 else
713 /* For simplicity. */
714 dest_dev = xstrdup (dest_dev);
716 prefix = grub_get_prefix (dir ? : DEFAULT_DIRECTORY);
718 if (root_dev)
720 char *tmp = get_device_name (root_dev);
722 if (! tmp)
723 grub_util_error ("Invalid root device `%s'", root_dev);
725 tmp = xstrdup (tmp);
726 free (root_dev);
727 root_dev = tmp;
729 else
731 root_dev = grub_util_get_grub_dev (grub_guess_root_device (dir ? : DEFAULT_DIRECTORY));
732 if (! root_dev)
734 grub_util_info ("guessing the root device failed, because of `%s'",
735 grub_errmsg);
736 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
740 #ifdef __linux__
741 if (grub_util_lvm_isvolume (root_dev))
743 char *newprefix;
744 must_embed = 1;
746 newprefix = xmalloc (1 + strlen (root_dev) + 1 + strlen (prefix) + 1);
747 sprintf (newprefix, "(%s)%s", root_dev, prefix);
748 free (prefix);
749 prefix = newprefix;
752 if (dest_dev[0] == 'm' && dest_dev[1] == 'd'
753 && dest_dev[2] >= '0' && dest_dev[2] <= '9')
755 char **devicelist;
756 char *raid_prefix;
757 int i;
759 raid_prefix = xmalloc (1 + strlen (dest_dev) + 1 + strlen (prefix) + 1);
761 sprintf (raid_prefix, "(%s)%s", dest_dev, prefix);
763 devicelist = grub_util_raid_getmembers (dest_dev);
765 for (i = 0; devicelist[i]; i++)
767 setup (raid_prefix,
768 dir ? : DEFAULT_DIRECTORY,
769 boot_file ? : DEFAULT_BOOT_FILE,
770 core_file ? : DEFAULT_CORE_FILE,
771 root_dev, grub_util_get_grub_dev (devicelist[i]), 1);
774 free (raid_prefix);
776 else
777 #endif
778 /* Do the real work. */
779 setup (prefix,
780 dir ? : DEFAULT_DIRECTORY,
781 boot_file ? : DEFAULT_BOOT_FILE,
782 core_file ? : DEFAULT_CORE_FILE,
783 root_dev, dest_dev, must_embed);
785 /* Free resources. */
786 grub_fini_all ();
787 grub_util_biosdisk_fini ();
789 free (boot_file);
790 free (core_file);
791 free (dir);
792 free (dev_map);
793 free (root_dev);
794 free (prefix);
795 free (dest_dev);
797 return 0;