]> git.proxmox.com Git - grub2.git/blob - util/i386/pc/grub-setup.c
merge mainline into rescue-efi
[grub2.git] / util / i386 / pc / grub-setup.c
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.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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/emu/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/emu/getroot.h>
39
40 static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_PARTITION_TYPE_BIOS_BOOT;
41
42 #include <grub_setup_init.h>
43
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"
53
54 #define _GNU_SOURCE 1
55 #include <getopt.h>
56
57 #define DEFAULT_BOOT_FILE "boot.img"
58 #define DEFAULT_CORE_FILE "core.img"
59
60 #define grub_target_to_host16(x) grub_le_to_cpu16(x)
61 #define grub_target_to_host32(x) grub_le_to_cpu32(x)
62 #define grub_target_to_host64(x) grub_le_to_cpu64(x)
63 #define grub_host_to_target16(x) grub_cpu_to_le16(x)
64 #define grub_host_to_target32(x) grub_cpu_to_le32(x)
65 #define grub_host_to_target64(x) grub_cpu_to_le64(x)
66
67 void
68 grub_putchar (int c)
69 {
70 putchar (c);
71 }
72
73 int
74 grub_getkey (void)
75 {
76 return -1;
77 }
78
79 struct grub_handler_class grub_term_input_class;
80 struct grub_handler_class grub_term_output_class;
81
82 void
83 grub_refresh (void)
84 {
85 fflush (stdout);
86 }
87
88 static void
89 setup (const char *dir,
90 const char *boot_file, const char *core_file,
91 const char *root, const char *dest, int must_embed, int force, int fs_probe)
92 {
93 char *boot_path, *core_path, *core_path_dev, *core_path_dev_full;
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 const char *dest_partmap;
99 grub_uint8_t *boot_drive;
100 grub_disk_addr_t *kernel_sector;
101 grub_uint16_t *boot_drive_check;
102 struct grub_boot_blocklist *first_block, *block;
103 grub_int32_t *install_dos_part, *install_bsd_part;
104 grub_int32_t dos_part, bsd_part;
105 char *tmp_img;
106 int i;
107 grub_disk_addr_t first_sector;
108 grub_uint16_t current_segment
109 = GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
110 grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
111 grub_file_t file;
112 FILE *fp;
113 struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
114 embed_region.start = embed_region.end = ~0UL;
115
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);
120
121 auto int NESTED_FUNC_ATTR find_usable_region_msdos (grub_disk_t disk,
122 const grub_partition_t p);
123 int NESTED_FUNC_ATTR find_usable_region_msdos (grub_disk_t disk __attribute__ ((unused)),
124 const grub_partition_t p)
125 {
126 /* There's always an embed region, and it starts right after the MBR. */
127 embed_region.start = 1;
128
129 if (embed_region.end > p->start)
130 embed_region.end = p->start;
131
132 return 0;
133 }
134
135 auto int NESTED_FUNC_ATTR find_usable_region_gpt (grub_disk_t disk,
136 const grub_partition_t p);
137 int NESTED_FUNC_ATTR find_usable_region_gpt (grub_disk_t disk __attribute__ ((unused)),
138 const grub_partition_t p)
139 {
140 struct grub_gpt_partentry gptdata;
141
142 disk->partition = p->parent;
143 if (grub_disk_read (disk, p->offset, p->index,
144 sizeof (gptdata), &gptdata))
145 return 0;
146
147 /* If there's an embed region, it is in a dedicated partition. */
148 if (! memcmp (&gptdata.type, &grub_gpt_partition_type_bios_boot, 16))
149 {
150 embed_region.start = p->start;
151 embed_region.end = p->start + p->len;
152
153 return 1;
154 }
155 return 0;
156 }
157
158 void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
159 unsigned length)
160 {
161 grub_util_info ("the first sector is <%llu,%u,%u>",
162 sector, offset, length);
163
164 if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
165 grub_util_error (_("the first sector of the core file is not sector-aligned"));
166
167 first_sector = sector;
168 }
169
170 void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
171 unsigned length)
172 {
173 struct grub_boot_blocklist *prev = block + 1;
174
175 grub_util_info ("saving <%llu,%u,%u> with the segment 0x%x",
176 sector, offset, length, (unsigned) current_segment);
177
178 if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
179 grub_util_error (_("non-sector-aligned data is found in the core file"));
180
181 if (block != first_block
182 && (grub_le_to_cpu64 (prev->start)
183 + grub_le_to_cpu16 (prev->len)) == sector)
184 prev->len = grub_cpu_to_le16 (grub_le_to_cpu16 (prev->len) + 1);
185 else
186 {
187 block->start = grub_cpu_to_le64 (sector);
188 block->len = grub_cpu_to_le16 (1);
189 block->segment = grub_cpu_to_le16 (current_segment);
190
191 block--;
192 if (block->len)
193 grub_util_error (_("the sectors of the core file are too fragmented"));
194 }
195
196 last_length = length;
197 current_segment += GRUB_DISK_SECTOR_SIZE >> 4;
198 }
199
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 %u"),
205 boot_path, GRUB_DISK_SECTOR_SIZE);
206 boot_img = grub_util_read_image (boot_path);
207 free (boot_path);
208
209 /* Set the addresses of variables in the boot image. */
210 boot_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_BOOT_DRIVE);
211 kernel_sector = (grub_disk_addr_t *) (boot_img
212 + GRUB_BOOT_MACHINE_KERNEL_SECTOR);
213 boot_drive_check = (grub_uint16_t *) (boot_img
214 + GRUB_BOOT_MACHINE_DRIVE_CHECK);
215
216 core_path = grub_util_get_path (dir, core_file);
217 core_size = grub_util_get_image_size (core_path);
218 core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
219 >> GRUB_DISK_SECTOR_BITS);
220 if (core_size < GRUB_DISK_SECTOR_SIZE)
221 grub_util_error (_("the size of `%s' is too small"), core_path);
222 else if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
223 grub_util_error (_("the size of `%s' is too large"), core_path);
224
225 core_img = grub_util_read_image (core_path);
226
227 /* Have FIRST_BLOCK to point to the first blocklist. */
228 first_block = (struct grub_boot_blocklist *) (core_img
229 + GRUB_DISK_SECTOR_SIZE
230 - sizeof (*block));
231
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
237 /* Open the root device and the destination device. */
238 root_dev = grub_device_open (root);
239 if (! root_dev)
240 grub_util_error ("%s", grub_errmsg);
241
242 dest_dev = grub_device_open (dest);
243 if (! dest_dev)
244 grub_util_error ("%s", grub_errmsg);
245
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);
249
250 /* Read the original sector from the disk. */
251 tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
252 if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
253 grub_util_error ("%s", grub_errmsg);
254
255 if (dest_dev->disk->partition && fs_probe)
256 {
257 grub_fs_t fs;
258 fs = grub_fs_probe (dest_dev);
259 if (! fs)
260 grub_util_error (_("unable to identify a filesystem in %s; safety check can't be performed"),
261 dest_dev->disk->name);
262
263 if (! fs->reserved_first_sector)
264 grub_util_error (_("%s appears to contain a %s filesystem which isn't known to "
265 "reserve space for DOS-style boot. Installing GRUB there could "
266 "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
267 "by grub-setup (--skip-fs-probe disables this "
268 "check, use at your own risk)"), dest_dev->disk->name, fs->name);
269 }
270
271 /* Copy the possible DOS BPB. */
272 memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
273 tmp_img + GRUB_BOOT_MACHINE_BPB_START,
274 GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
275
276 /* Copy the possible partition table. */
277 if (dest_dev->disk->has_partitions)
278 memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
279 tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
280 GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
281
282 free (tmp_img);
283
284 /* If DEST_DRIVE is a hard disk, enable the workaround, which is
285 for buggy BIOSes which don't pass boot drive correctly. Instead,
286 they pass 0x00 or 0x01 even when booted from 0x80. */
287 if (dest_dev->disk->id & 0x80)
288 /* Replace the jmp (2 bytes) with double nop's. */
289 *boot_drive_check = 0x9090;
290
291 /* If we hardcoded drive as part of prefix, we don't want to
292 override the current setting. */
293 if (*install_dos_part != -2)
294 {
295 /* Embed information about the installed location. */
296 if (root_dev->disk->partition)
297 {
298 if (root_dev->disk->partition->parent)
299 {
300 if (root_dev->disk->partition->parent->parent)
301 grub_util_error ("Installing on doubly nested partitions is "
302 "not supported");
303 dos_part = root_dev->disk->partition->parent->number;
304 bsd_part = root_dev->disk->partition->number;
305 }
306 else
307 {
308 dos_part = root_dev->disk->partition->number;
309 bsd_part = -1;
310 }
311 }
312 else
313 dos_part = bsd_part = -1;
314 }
315 else
316 {
317 dos_part = grub_le_to_cpu32 (*install_dos_part);
318 bsd_part = grub_le_to_cpu32 (*install_bsd_part);
319 }
320
321 grub_util_info ("dos partition is %d, bsd partition is %d",
322 dos_part, bsd_part);
323
324 if (! dest_dev->disk->has_partitions)
325 {
326 grub_util_warn (_("Attempting to install GRUB to a partitionless disk. This is a BAD idea."));
327 goto unable_to_embed;
328 }
329
330 if (dest_dev->disk->partition)
331 {
332 grub_util_warn (_("Attempting to install GRUB to a partition instead of the MBR. This is a BAD idea."));
333 goto unable_to_embed;
334 }
335
336 /* Unlike root_dev, with dest_dev we're interested in the partition map even
337 if dest_dev itself is a whole disk. */
338 auto int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk,
339 const grub_partition_t p);
340 int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk __attribute__ ((unused)),
341 const grub_partition_t p)
342 {
343 if (p->parent)
344 return 0;
345 dest_partmap = p->partmap->name;
346 return 1;
347 }
348 dest_partmap = 0;
349 grub_partition_iterate (dest_dev->disk, identify_partmap);
350
351 if (! dest_partmap)
352 {
353 grub_util_warn (_("Attempting to install GRUB to a partitionless disk. This is a BAD idea."));
354 goto unable_to_embed;
355 }
356
357 if (strcmp (dest_partmap, "msdos") == 0)
358 grub_partition_iterate (dest_dev->disk, find_usable_region_msdos);
359 else if (strcmp (dest_partmap, "gpt") == 0)
360 grub_partition_iterate (dest_dev->disk, find_usable_region_gpt);
361 else
362 grub_util_error (_("No DOS-style partitions found"));
363
364 if (embed_region.end == embed_region.start)
365 {
366 if (! strcmp (dest_partmap, "msdos"))
367 grub_util_warn (_("This msdos-style partition label has no post-MBR gap; embedding won't be possible!"));
368 else
369 grub_util_warn (_("This GPT partition label has no BIOS Boot Partition; embedding won't be possible!"));
370 goto unable_to_embed;
371 }
372
373 if ((unsigned long) core_sectors > embed_region.end - embed_region.start)
374 {
375 if (core_sectors > 62)
376 grub_util_warn (_("Your core.img is unusually large. It won't fit in the embedding area."));
377 else /* embed_region.end - embed_region.start < 62 */
378 grub_util_warn (_("Your embedding area is unusually small. core.img won't fit in it."));
379 goto unable_to_embed;
380 }
381
382
383 grub_util_info ("the core image will be embedded at sector 0x%llx", embed_region.start);
384
385 *install_dos_part = grub_cpu_to_le32 (dos_part);
386 *install_bsd_part = grub_cpu_to_le32 (bsd_part);
387
388 /* The first blocklist contains the whole sectors. */
389 first_block->start = grub_cpu_to_le64 (embed_region.start + 1);
390
391 /* These are filled elsewhere. Verify them just in case. */
392 assert (first_block->len == grub_host_to_target16 (core_sectors - 1));
393 assert (first_block->segment == grub_host_to_target16 (GRUB_BOOT_MACHINE_KERNEL_SEG
394 + (GRUB_DISK_SECTOR_SIZE >> 4)));
395
396 /* Make sure that the second blocklist is a terminator. */
397 block = first_block - 1;
398 block->start = 0;
399 block->len = 0;
400 block->segment = 0;
401
402 /* Write the core image onto the disk. */
403 if (grub_disk_write (dest_dev->disk, embed_region.start, 0, core_size, core_img))
404 grub_util_error ("%s", grub_errmsg);
405
406 /* FIXME: can this be skipped? */
407 *boot_drive = 0xFF;
408
409 *kernel_sector = grub_cpu_to_le64 (embed_region.start);
410
411 /* Write the boot image onto the disk. */
412 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE,
413 boot_img))
414 grub_util_error ("%s", grub_errmsg);
415
416 goto finish;
417
418 unable_to_embed:
419
420 if (must_embed)
421 grub_util_error (_("embedding is not possible, but this is required when "
422 "the root device is on a RAID array or LVM volume"));
423
424 grub_util_warn (_("Embedding is not possible. GRUB can only be installed in this "
425 "setup by using blocklists. However, blocklists are UNRELIABLE and "
426 "their use is discouraged."));
427 if (! force)
428 grub_util_error (_("if you really want blocklists, use --force"));
429
430 /* Make sure that GRUB reads the identical image as the OS. */
431 tmp_img = xmalloc (core_size);
432 core_path_dev_full = grub_util_get_path (dir, core_file);
433 core_path_dev = grub_make_system_path_relative_to_its_root (core_path_dev_full);
434 free (core_path_dev_full);
435
436 /* It is a Good Thing to sync two times. */
437 sync ();
438 sync ();
439
440 #define MAX_TRIES 5
441
442 for (i = 0; i < MAX_TRIES; i++)
443 {
444 grub_util_info ((i == 0) ? _("attempting to read the core image `%s' from GRUB")
445 : _("attempting to read the core image `%s' from GRUB again"),
446 core_path_dev);
447
448 grub_disk_cache_invalidate_all ();
449
450 file = grub_file_open (core_path_dev);
451 if (file)
452 {
453 if (grub_file_size (file) != core_size)
454 grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
455 (int) grub_file_size (file), (int) core_size);
456 else if (grub_file_read (file, tmp_img, core_size)
457 != (grub_ssize_t) core_size)
458 grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
459 (int) core_size);
460 else if (memcmp (core_img, tmp_img, core_size) != 0)
461 {
462 #if 0
463 FILE *dump;
464 FILE *dump2;
465
466 dump = fopen ("dump.img", "wb");
467 if (dump)
468 {
469 fwrite (tmp_img, 1, core_size, dump);
470 fclose (dump);
471 }
472
473 dump2 = fopen ("dump2.img", "wb");
474 if (dump2)
475 {
476 fwrite (core_img, 1, core_size, dump2);
477 fclose (dump2);
478 }
479
480 #endif
481 grub_util_info ("succeeded in opening the core image but the data is different");
482 }
483 else
484 {
485 grub_file_close (file);
486 break;
487 }
488
489 grub_file_close (file);
490 }
491 else
492 grub_util_info ("couldn't open the core image");
493
494 if (grub_errno)
495 grub_util_info ("error message = %s", grub_errmsg);
496
497 grub_errno = GRUB_ERR_NONE;
498 sync ();
499 sleep (1);
500 }
501
502 if (i == MAX_TRIES)
503 grub_util_error (_("cannot read `%s' correctly"), core_path_dev);
504
505 /* Clean out the blocklists. */
506 block = first_block;
507 while (block->len)
508 {
509 block->start = 0;
510 block->len = 0;
511 block->segment = 0;
512
513 block--;
514
515 if ((char *) block <= core_img)
516 grub_util_error (_("no terminator in the core image"));
517 }
518
519 /* Now read the core image to determine where the sectors are. */
520 file = grub_file_open (core_path_dev);
521 if (! file)
522 grub_util_error ("%s", grub_errmsg);
523
524 file->read_hook = save_first_sector;
525 if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
526 != GRUB_DISK_SECTOR_SIZE)
527 grub_util_error (_("failed to read the first sector of the core image"));
528
529 block = first_block;
530 file->read_hook = save_blocklists;
531 if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
532 != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
533 grub_util_error (_("failed to read the rest sectors of the core image"));
534
535 grub_file_close (file);
536
537 free (core_path_dev);
538 free (tmp_img);
539
540 *kernel_sector = grub_cpu_to_le64 (first_sector);
541
542 /* FIXME: can this be skipped? */
543 *boot_drive = 0xFF;
544
545 *install_dos_part = grub_cpu_to_le32 (dos_part);
546 *install_bsd_part = grub_cpu_to_le32 (bsd_part);
547
548 /* Write the first two sectors of the core image onto the disk. */
549 grub_util_info ("opening the core image `%s'", core_path);
550 fp = fopen (core_path, "r+b");
551 if (! fp)
552 grub_util_error (_("cannot open `%s'"), core_path);
553
554 grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp);
555 fclose (fp);
556
557 /* Write the boot image onto the disk. */
558 if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
559 grub_util_error ("%s", grub_errmsg);
560
561 finish:
562
563 /* Sync is a Good Thing. */
564 sync ();
565
566 free (core_path);
567 free (core_img);
568 free (boot_img);
569 grub_device_close (dest_dev);
570 grub_device_close (root_dev);
571 }
572
573 static struct option options[] =
574 {
575 {"boot-image", required_argument, 0, 'b'},
576 {"core-image", required_argument, 0, 'c'},
577 {"directory", required_argument, 0, 'd'},
578 {"device-map", required_argument, 0, 'm'},
579 {"root-device", required_argument, 0, 'r'},
580 {"force", no_argument, 0, 'f'},
581 {"skip-fs-probe", no_argument, 0, 's'},
582 {"help", no_argument, 0, 'h'},
583 {"version", no_argument, 0, 'V'},
584 {"verbose", no_argument, 0, 'v'},
585 {0, 0, 0, 0}
586 };
587
588 static void
589 usage (int status)
590 {
591 if (status)
592 fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name);
593 else
594 printf (_("\
595 Usage: %s [OPTION]... DEVICE\n\
596 \n\
597 Set up images to boot from DEVICE.\n\
598 DEVICE must be a GRUB device (e.g. `(hd0,1)').\n\
599 \n\
600 -b, --boot-image=FILE use FILE as the boot image [default=%s]\n\
601 -c, --core-image=FILE use FILE as the core image [default=%s]\n\
602 -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n\
603 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
604 -r, --root-device=DEV use DEV as the root device [default=guessed]\n\
605 -f, --force install even if problems are detected\n\
606 -s, --skip-fs-probe do not probe for filesystems in DEVICE\n\
607 -h, --help display this message and exit\n\
608 -V, --version print version information and exit\n\
609 -v, --verbose print verbose messages\n\
610 \n\
611 Report bugs to <%s>.\n\
612 "),
613 program_name,
614 DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY,
615 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
616
617 exit (status);
618 }
619
620 static char *
621 get_device_name (char *dev)
622 {
623 size_t len = strlen (dev);
624
625 if (dev[0] != '(' || dev[len - 1] != ')')
626 return 0;
627
628 dev[len - 1] = '\0';
629 return dev + 1;
630 }
631
632 int
633 main (int argc, char *argv[])
634 {
635 char *boot_file = 0;
636 char *core_file = 0;
637 char *dir = 0;
638 char *dev_map = 0;
639 char *root_dev = 0;
640 char *dest_dev;
641 int must_embed = 0, force = 0, fs_probe = 1;
642
643 set_program_name (argv[0]);
644
645 grub_util_init_nls ();
646
647 /* Check for options. */
648 while (1)
649 {
650 int c = getopt_long (argc, argv, "b:c:d:m:r:hVvf", options, 0);
651
652 if (c == -1)
653 break;
654 else
655 switch (c)
656 {
657 case 'b':
658 if (boot_file)
659 free (boot_file);
660
661 boot_file = xstrdup (optarg);
662 break;
663
664 case 'c':
665 if (core_file)
666 free (core_file);
667
668 core_file = xstrdup (optarg);
669 break;
670
671 case 'd':
672 if (dir)
673 free (dir);
674
675 dir = xstrdup (optarg);
676 break;
677
678 case 'm':
679 if (dev_map)
680 free (dev_map);
681
682 dev_map = xstrdup (optarg);
683 break;
684
685 case 'r':
686 if (root_dev)
687 free (root_dev);
688
689 root_dev = xstrdup (optarg);
690 break;
691
692 case 'f':
693 force = 1;
694 break;
695
696 case 's':
697 fs_probe = 0;
698 break;
699
700 case 'h':
701 usage (0);
702 break;
703
704 case 'V':
705 printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
706 return 0;
707
708 case 'v':
709 verbosity++;
710 break;
711
712 default:
713 usage (1);
714 break;
715 }
716 }
717
718 if (verbosity > 1)
719 grub_env_set ("debug", "all");
720
721 /* Obtain DEST_DEV. */
722 if (optind >= argc)
723 {
724 fprintf (stderr, _("No device is specified.\n"));
725 usage (1);
726 }
727
728 if (optind + 1 != argc)
729 {
730 fprintf (stderr, _("Unknown extra argument `%s'.\n"), argv[optind + 1]);
731 usage (1);
732 }
733
734 /* Initialize the emulated biosdisk driver. */
735 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
736
737 /* Initialize all modules. */
738 grub_init_all ();
739
740 dest_dev = get_device_name (argv[optind]);
741 if (! dest_dev)
742 {
743 /* Possibly, the user specified an OS device file. */
744 dest_dev = grub_util_get_grub_dev (argv[optind]);
745 if (! dest_dev)
746 {
747 fprintf (stderr, _("Invalid device `%s'.\n"), argv[optind]);
748 usage (1);
749 }
750 }
751 else
752 /* For simplicity. */
753 dest_dev = xstrdup (dest_dev);
754
755 if (root_dev)
756 {
757 char *tmp = get_device_name (root_dev);
758
759 if (! tmp)
760 grub_util_error (_("invalid root device `%s'"), root_dev);
761
762 tmp = xstrdup (tmp);
763 free (root_dev);
764 root_dev = tmp;
765 }
766 else
767 {
768 root_dev = grub_util_get_grub_dev (grub_guess_root_device (dir ? : DEFAULT_DIRECTORY));
769 if (! root_dev)
770 {
771 grub_util_info ("guessing the root device failed, because of `%s'",
772 grub_errmsg);
773 grub_util_error (_("cannot guess the root device. Specify the option `--root-device'"));
774 }
775 }
776
777 #ifdef __linux__
778 if (grub_util_lvm_isvolume (root_dev))
779 must_embed = 1;
780
781 if (root_dev[0] == 'm' && root_dev[1] == 'd'
782 && root_dev[2] >= '0' && root_dev[2] <= '9')
783 {
784 /* FIXME: we can avoid this on RAID1. */
785 must_embed = 1;
786 }
787
788 if (dest_dev[0] == 'm' && dest_dev[1] == 'd'
789 && dest_dev[2] >= '0' && dest_dev[2] <= '9')
790 {
791 char **devicelist;
792 int i;
793
794 devicelist = grub_util_raid_getmembers (dest_dev);
795
796 for (i = 0; devicelist[i]; i++)
797 {
798 setup (dir ? : DEFAULT_DIRECTORY,
799 boot_file ? : DEFAULT_BOOT_FILE,
800 core_file ? : DEFAULT_CORE_FILE,
801 root_dev, grub_util_get_grub_dev (devicelist[i]), 1, force, fs_probe);
802 }
803 }
804 else
805 #endif
806 /* Do the real work. */
807 setup (dir ? : DEFAULT_DIRECTORY,
808 boot_file ? : DEFAULT_BOOT_FILE,
809 core_file ? : DEFAULT_CORE_FILE,
810 root_dev, dest_dev, must_embed, force, fs_probe);
811
812 /* Free resources. */
813 grub_fini_all ();
814 grub_util_biosdisk_fini ();
815
816 free (boot_file);
817 free (core_file);
818 free (dir);
819 free (dev_map);
820 free (root_dev);
821 free (dest_dev);
822
823 return 0;
824 }