sparc64: Add blocklist GPT support for SPARC
[grub.git] / util / 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,2011  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/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/device.h>
25 #include <grub/disk.h>
26 #include <grub/file.h>
27 #include <grub/fs.h>
28 #include <grub/partition.h>
29 #include <grub/env.h>
30 #include <grub/emu/hostdisk.h>
31 #include <grub/term.h>
32 #include <grub/i18n.h>
33
34 #ifdef GRUB_SETUP_SPARC64
35 #include <grub/util/ofpath.h>
36 #include <grub/sparc64/ieee1275/boot.h>
37 #include <grub/sparc64/ieee1275/kernel.h>
38 #else
39 #include <grub/i386/pc/boot.h>
40 #include <grub/i386/pc/kernel.h>
41 #endif
42
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <dirent.h>
50 #include <assert.h>
51 #include <grub/emu/getroot.h>
52 #include "progname.h"
53 #include <grub/reed_solomon.h>
54 #include <grub/msdos_partition.h>
55 #include <grub/crypto.h>
56 #include <grub/util/install.h>
57 #include <grub/emu/hostfile.h>
58
59 #include <errno.h>
60
61 /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
62  * image files.
63  *
64  * The 'boot' image needs to know the OBP path name of the root
65  * device.  It also needs to know the initial block number of
66  * 'core' (which is 'diskboot' concatenated with 'kernel' and
67  * all the modules, this is created by grub-mkimage).  This resulting
68  * 'boot' image is 512 bytes in size and is placed in the second block
69  * of a partition.
70  *
71  * The initial 'diskboot' block acts as a loader for the actual GRUB
72  * kernel.  It contains the loading code and then a block list.
73  *
74  * The block list of 'core' starts at the end of the 'diskboot' image
75  * and works it's way backwards towards the end of the code of 'diskboot'.
76  *
77  * We patch up the images with the necessary values and write out the
78  * result.
79  */
80
81 #ifdef GRUB_SETUP_SPARC64
82 #define grub_target_to_host16(x)        grub_be_to_cpu16(x)
83 #define grub_target_to_host32(x)        grub_be_to_cpu32(x)
84 #define grub_target_to_host64(x)        grub_be_to_cpu64(x)
85 #define grub_host_to_target16(x)        grub_cpu_to_be16(x)
86 #define grub_host_to_target32(x)        grub_cpu_to_be32(x)
87 #define grub_host_to_target64(x)        grub_cpu_to_be64(x)
88 #elif defined (GRUB_SETUP_BIOS)
89 #define grub_target_to_host16(x)        grub_le_to_cpu16(x)
90 #define grub_target_to_host32(x)        grub_le_to_cpu32(x)
91 #define grub_target_to_host64(x)        grub_le_to_cpu64(x)
92 #define grub_host_to_target16(x)        grub_cpu_to_le16(x)
93 #define grub_host_to_target32(x)        grub_cpu_to_le32(x)
94 #define grub_host_to_target64(x)        grub_cpu_to_le64(x)
95 #else
96 #error Complete this
97 #endif
98
99 static void
100 write_rootdev (grub_device_t root_dev,
101                char *boot_img, grub_uint64_t first_sector)
102 {
103 #ifdef GRUB_SETUP_BIOS
104   {
105     grub_uint8_t *boot_drive;
106     void *kernel_sector;
107     boot_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_BOOT_DRIVE);
108     kernel_sector = (boot_img + GRUB_BOOT_MACHINE_KERNEL_SECTOR);
109
110     /* FIXME: can this be skipped?  */
111     *boot_drive = 0xFF;
112
113     grub_set_unaligned64 (kernel_sector, grub_cpu_to_le64 (first_sector));
114   }
115 #endif
116 #ifdef GRUB_SETUP_SPARC64
117   {
118     void *kernel_byte;
119     kernel_byte = (boot_img + GRUB_BOOT_AOUT_HEADER_SIZE
120                    + GRUB_BOOT_MACHINE_KERNEL_BYTE);
121     grub_set_unaligned64 (kernel_byte,
122                           grub_cpu_to_be64 (first_sector << GRUB_DISK_SECTOR_BITS));
123   }
124 #endif
125 }
126
127 #ifdef GRUB_SETUP_SPARC64
128 #define BOOT_SECTOR 1
129 #else
130 #define BOOT_SECTOR 0
131 #endif
132
133 /* Helper for setup.  */
134
135 struct blocklists
136 {
137   struct grub_boot_blocklist *first_block, *block;
138 #ifdef GRUB_SETUP_BIOS
139   grub_uint16_t current_segment;
140 #endif
141 #ifdef GRUB_SETUP_SPARC64
142   grub_uint64_t gpt_offset;
143 #endif
144   grub_uint16_t last_length;
145   grub_disk_addr_t first_sector;
146 };
147
148 /* Helper for setup.  */
149 static void
150 save_blocklists (grub_disk_addr_t sector, unsigned offset, unsigned length,
151                  void *data)
152 {
153   struct blocklists *bl = data;
154   struct grub_boot_blocklist *prev = bl->block + 1;
155   grub_uint64_t seclen;
156
157 #ifdef GRUB_SETUP_SPARC64
158   sector -= bl->gpt_offset;
159 #endif
160
161   grub_util_info ("saving <%"  GRUB_HOST_PRIuLONG_LONG ",%u,%u>",
162                   (unsigned long long) sector, offset, length);
163
164   if (bl->first_sector == (grub_disk_addr_t) -1)
165     {
166       if (offset != 0 || length < GRUB_DISK_SECTOR_SIZE)
167         grub_util_error ("%s", _("the first sector of the core file is not sector-aligned"));
168
169       bl->first_sector = sector;
170       sector++;
171       length -= GRUB_DISK_SECTOR_SIZE;
172       if (!length)
173         return;
174     }
175
176   if (offset != 0 || bl->last_length != 0)
177     grub_util_error ("%s", _("non-sector-aligned data is found in the core file"));
178
179   seclen = (length + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS;
180
181   if (bl->block != bl->first_block
182       && (grub_target_to_host64 (prev->start)
183           + grub_target_to_host16 (prev->len)) == sector)
184     {
185       grub_uint16_t t = grub_target_to_host16 (prev->len);
186       t += seclen;
187       prev->len = grub_host_to_target16 (t);
188     }
189   else
190     {
191       bl->block->start = grub_host_to_target64 (sector);
192       bl->block->len = grub_host_to_target16 (seclen);
193 #ifdef GRUB_SETUP_BIOS
194       bl->block->segment = grub_host_to_target16 (bl->current_segment);
195 #endif
196
197       bl->block--;
198       if (bl->block->len)
199         grub_util_error ("%s", _("the sectors of the core file are too fragmented"));
200     }
201
202   bl->last_length = length & (GRUB_DISK_SECTOR_SIZE - 1);
203 #ifdef GRUB_SETUP_BIOS
204   bl->current_segment += seclen << (GRUB_DISK_SECTOR_BITS - 4);
205 #endif
206 }
207
208 #ifdef GRUB_SETUP_BIOS
209 /* Context for setup/identify_partmap.  */
210 struct identify_partmap_ctx
211 {
212   grub_partition_map_t dest_partmap;
213   grub_partition_t container;
214   int multiple_partmaps;
215 };
216
217 /* Helper for setup.
218    Unlike root_dev, with dest_dev we're interested in the partition map even
219    if dest_dev itself is a whole disk.  */
220 static int
221 identify_partmap (grub_disk_t disk __attribute__ ((unused)),
222                   const grub_partition_t p, void *data)
223 {
224   struct identify_partmap_ctx *ctx = data;
225
226   if (p->parent != ctx->container)
227     return 0;
228   /* NetBSD and OpenBSD subpartitions have metadata inside a partition,
229      so they are safe to ignore.
230    */
231   if (grub_strcmp (p->partmap->name, "netbsd") == 0
232       || grub_strcmp (p->partmap->name, "openbsd") == 0)
233     return 0;
234   if (ctx->dest_partmap == NULL)
235     {
236       ctx->dest_partmap = p->partmap;
237       return 0;
238     }
239   if (ctx->dest_partmap == p->partmap)
240     return 0;
241   ctx->multiple_partmaps = 1;
242   return 1;
243 }
244 #endif
245
246 #ifdef GRUB_SETUP_BIOS
247 #define SETUP grub_util_bios_setup
248 #elif GRUB_SETUP_SPARC64
249 #define SETUP grub_util_sparc_setup
250 #else
251 #error "Shouldn't happen"
252 #endif
253
254 void
255 SETUP (const char *dir,
256        const char *boot_file, const char *core_file,
257        const char *dest, int force,
258        int fs_probe, int allow_floppy,
259        int add_rs_codes __attribute__ ((unused))) /* unused on sparc64 */
260 {
261   char *core_path;
262   char *boot_img, *core_img, *boot_path;
263   char *root = 0;
264   size_t boot_size, core_size;
265 #ifdef GRUB_SETUP_BIOS
266   grub_uint16_t core_sectors;
267 #endif
268   grub_device_t root_dev = 0, dest_dev, core_dev;
269   grub_util_fd_t fp;
270   struct blocklists bl;
271
272   bl.first_sector = (grub_disk_addr_t) -1;
273
274 #ifdef GRUB_SETUP_BIOS
275   bl.current_segment =
276     GRUB_BOOT_I386_PC_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
277 #endif
278   bl.last_length = 0;
279
280   /* Read the boot image by the OS service.  */
281   boot_path = grub_util_get_path (dir, boot_file);
282   boot_size = grub_util_get_image_size (boot_path);
283   if (boot_size != GRUB_DISK_SECTOR_SIZE)
284     grub_util_error (_("the size of `%s' is not %u"),
285                      boot_path, GRUB_DISK_SECTOR_SIZE);
286   boot_img = grub_util_read_image (boot_path);
287   free (boot_path);
288
289   core_path = grub_util_get_path (dir, core_file);
290   core_size = grub_util_get_image_size (core_path);
291 #ifdef GRUB_SETUP_BIOS
292   core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
293                   >> GRUB_DISK_SECTOR_BITS);
294 #endif
295   if (core_size < GRUB_DISK_SECTOR_SIZE)
296     grub_util_error (_("the size of `%s' is too small"), core_path);
297 #ifdef GRUB_SETUP_BIOS
298   if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
299     grub_util_error (_("the size of `%s' is too large"), core_path);
300 #endif
301
302   core_img = grub_util_read_image (core_path);
303
304   /* Have FIRST_BLOCK to point to the first blocklist.  */
305   bl.first_block = (struct grub_boot_blocklist *) (core_img
306                                                    + GRUB_DISK_SECTOR_SIZE
307                                                    - sizeof (*bl.block));
308   grub_util_info ("root is `%s', dest is `%s'", root, dest);
309
310   grub_util_info ("Opening dest");
311   dest_dev = grub_device_open (dest);
312   if (! dest_dev)
313     grub_util_error ("%s", grub_errmsg);
314
315   core_dev = dest_dev;
316
317   {
318     char **root_devices = grub_guess_root_devices (dir);
319     char **cur;
320     int found = 0;
321
322     if (!root_devices)
323       grub_util_error (_("cannot find a device for %s (is /dev mounted?)"), dir);
324
325     for (cur = root_devices; *cur; cur++)
326       {
327         char *drive;
328         grub_device_t try_dev;
329
330         drive = grub_util_get_grub_dev (*cur);
331         if (!drive)
332           continue;
333         try_dev = grub_device_open (drive);
334         if (! try_dev)
335           {
336             free (drive);
337             continue;
338           }
339         if (!found && try_dev->disk->id == dest_dev->disk->id
340             && try_dev->disk->dev->id == dest_dev->disk->dev->id)
341           {
342             if (root_dev)
343               grub_device_close (root_dev);
344             free (root);
345             root_dev = try_dev;
346             root = drive;
347             found = 1;
348             continue;
349           }
350         if (!root_dev)
351           {
352             root_dev = try_dev;
353             root = drive;
354             continue;
355           }
356         grub_device_close (try_dev);    
357         free (drive);
358       }
359     if (!root_dev)
360       {
361         grub_util_error ("guessing the root device failed, because of `%s'",
362                          grub_errmsg);
363       }
364     grub_util_info ("guessed root_dev `%s' from "
365                     "dir `%s'", root_dev->disk->name, dir);
366
367     for (cur = root_devices; *cur; cur++)
368       free (*cur);
369     free (root_devices);
370   }
371
372   grub_util_info ("setting the root device to `%s'", root);
373   if (grub_env_set ("root", root) != GRUB_ERR_NONE)
374     grub_util_error ("%s", grub_errmsg);
375
376 #ifdef GRUB_SETUP_BIOS
377   {
378     char *tmp_img;
379     grub_uint8_t *boot_drive_check;
380
381     /* Read the original sector from the disk.  */
382     tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
383     if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
384       grub_util_error ("%s", grub_errmsg);
385     
386     boot_drive_check = (grub_uint8_t *) (boot_img
387                                           + GRUB_BOOT_MACHINE_DRIVE_CHECK);
388     /* Copy the possible DOS BPB.  */
389     memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
390             tmp_img + GRUB_BOOT_MACHINE_BPB_START,
391             GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
392
393     /* If DEST_DRIVE is a hard disk, enable the workaround, which is
394        for buggy BIOSes which don't pass boot drive correctly. Instead,
395        they pass 0x00 or 0x01 even when booted from 0x80.  */
396     if (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk))
397       {
398         /* Replace the jmp (2 bytes) with double nop's.  */
399         boot_drive_check[0] = 0x90;
400         boot_drive_check[1] = 0x90;
401       }
402
403     struct identify_partmap_ctx ctx = {
404       .dest_partmap = NULL,
405       .container = dest_dev->disk->partition,
406       .multiple_partmaps = 0
407     };
408     int is_ldm;
409     grub_err_t err;
410     grub_disk_addr_t *sectors;
411     int i;
412     grub_fs_t fs;
413     unsigned int nsec, maxsec;
414
415     grub_partition_iterate (dest_dev->disk, identify_partmap, &ctx);
416
417     /* Copy the partition table.  */
418     if (ctx.dest_partmap ||
419         (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk)))
420       memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
421               tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
422               GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
423
424     free (tmp_img);
425
426     if (ctx.container
427         && grub_strcmp (ctx.container->partmap->name, "msdos") == 0
428         && ctx.dest_partmap
429         && (ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_NETBSD
430             || ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_OPENBSD))
431       {
432         grub_util_warn ("%s", _("Attempting to install GRUB to a disk with multiple partition labels or both partition label and filesystem.  This is not supported yet."));
433         goto unable_to_embed;
434       }
435
436     fs = grub_fs_probe (dest_dev);
437     if (!fs)
438       grub_errno = GRUB_ERR_NONE;
439
440     is_ldm = grub_util_is_ldm (dest_dev->disk);
441
442     if (fs_probe)
443       {
444         if (!fs && !ctx.dest_partmap)
445           grub_util_error (_("unable to identify a filesystem in %s; safety check can't be performed"),
446                            dest_dev->disk->name);
447         if (fs && !fs->reserved_first_sector)
448           /* TRANSLATORS: Filesystem may reserve the space just GRUB isn't sure about it.  */
449           grub_util_error (_("%s appears to contain a %s filesystem which isn't known to "
450                              "reserve space for DOS-style boot.  Installing GRUB there could "
451                              "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
452                              "by grub-setup (--skip-fs-probe disables this "
453                              "check, use at your own risk)"), dest_dev->disk->name, fs->name);
454
455         if (ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
456             && strcmp (ctx.dest_partmap->name, "gpt") != 0
457             && strcmp (ctx.dest_partmap->name, "bsd") != 0
458             && strcmp (ctx.dest_partmap->name, "netbsd") != 0
459             && strcmp (ctx.dest_partmap->name, "openbsd") != 0
460             && strcmp (ctx.dest_partmap->name, "sunpc") != 0)
461           /* TRANSLATORS: Partition map may reserve the space just GRUB isn't sure about it.  */
462           grub_util_error (_("%s appears to contain a %s partition map which isn't known to "
463                              "reserve space for DOS-style boot.  Installing GRUB there could "
464                              "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
465                              "by grub-setup (--skip-fs-probe disables this "
466                              "check, use at your own risk)"), dest_dev->disk->name, ctx.dest_partmap->name);
467         if (is_ldm && ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
468             && strcmp (ctx.dest_partmap->name, "gpt") != 0)
469           grub_util_error (_("%s appears to contain a %s partition map and "
470                              "LDM which isn't known to be a safe combination."
471                              "  Installing GRUB there could "
472                              "result in FILESYSTEM DESTRUCTION if valuable data"
473                              " is overwritten "
474                              "by grub-setup (--skip-fs-probe disables this "
475                              "check, use at your own risk)"),
476                            dest_dev->disk->name, ctx.dest_partmap->name);
477
478       }
479
480     if (! ctx.dest_partmap && ! fs && !is_ldm)
481       {
482         grub_util_warn ("%s", _("Attempting to install GRUB to a partitionless disk or to a partition.  This is a BAD idea."));
483         goto unable_to_embed;
484       }
485     if (ctx.multiple_partmaps || (ctx.dest_partmap && fs) || (is_ldm && fs))
486       {
487         grub_util_warn ("%s", _("Attempting to install GRUB to a disk with multiple partition labels.  This is not supported yet."));
488         goto unable_to_embed;
489       }
490
491     if (ctx.dest_partmap && !ctx.dest_partmap->embed)
492       {
493         grub_util_warn (_("Partition style `%s' doesn't support embedding"),
494                         ctx.dest_partmap->name);
495         goto unable_to_embed;
496       }
497
498     if (fs && !fs->embed)
499       {
500         grub_util_warn (_("File system `%s' doesn't support embedding"),
501                         fs->name);
502         goto unable_to_embed;
503       }
504
505     nsec = core_sectors;
506
507     if (add_rs_codes)
508       maxsec = 2 * core_sectors;
509     else
510       maxsec = core_sectors;
511
512     if (maxsec > ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
513                 >> GRUB_DISK_SECTOR_BITS))
514       maxsec = ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
515                 >> GRUB_DISK_SECTOR_BITS);
516
517     if (is_ldm)
518       err = grub_util_ldm_embed (dest_dev->disk, &nsec, maxsec,
519                                  GRUB_EMBED_PCBIOS, &sectors);
520     else if (ctx.dest_partmap)
521       err = ctx.dest_partmap->embed (dest_dev->disk, &nsec, maxsec,
522                                      GRUB_EMBED_PCBIOS, &sectors);
523     else
524       err = fs->embed (dest_dev, &nsec, maxsec,
525                        GRUB_EMBED_PCBIOS, &sectors);
526     if (!err && nsec < core_sectors)
527       {
528         err = grub_error (GRUB_ERR_OUT_OF_RANGE,
529                           N_("Your embedding area is unusually small.  "
530                              "core.img won't fit in it."));
531       }
532     
533     if (err)
534       {
535         grub_util_warn ("%s", grub_errmsg);
536         grub_errno = GRUB_ERR_NONE;
537         goto unable_to_embed;
538       }
539
540     assert (nsec <= maxsec);
541
542     /* Clean out the blocklists.  */
543     bl.block = bl.first_block;
544     while (bl.block->len)
545       {
546         grub_memset (bl.block, 0, sizeof (*bl.block));
547       
548         bl.block--;
549
550         if ((char *) bl.block <= core_img)
551           grub_util_error ("%s", _("no terminator in the core image"));
552       }
553
554     bl.block = bl.first_block;
555     for (i = 0; i < nsec; i++)
556       save_blocklists (sectors[i] + grub_partition_get_start (ctx.container),
557                        0, GRUB_DISK_SECTOR_SIZE, &bl);
558
559     /* Make sure that the last blocklist is a terminator.  */
560     if (bl.block == bl.first_block)
561       bl.block--;
562     bl.block->start = 0;
563     bl.block->len = 0;
564     bl.block->segment = 0;
565
566     write_rootdev (root_dev, boot_img, bl.first_sector);
567
568     /* Round up to the nearest sector boundary, and zero the extra memory */
569     core_img = xrealloc (core_img, nsec * GRUB_DISK_SECTOR_SIZE);
570     assert (core_img && (nsec * GRUB_DISK_SECTOR_SIZE >= core_size));
571     memset (core_img + core_size, 0, nsec * GRUB_DISK_SECTOR_SIZE - core_size);
572
573     bl.first_block = (struct grub_boot_blocklist *) (core_img
574                                                      + GRUB_DISK_SECTOR_SIZE
575                                                      - sizeof (*bl.block));
576
577     grub_size_t no_rs_length;
578     no_rs_length = grub_target_to_host16 
579       (grub_get_unaligned16 (core_img
580                              + GRUB_DISK_SECTOR_SIZE
581                              + GRUB_KERNEL_I386_PC_NO_REED_SOLOMON_LENGTH));
582
583     if (no_rs_length == 0xffff)
584       grub_util_error ("%s", _("core.img version mismatch"));
585
586     if (add_rs_codes)
587       {
588         grub_set_unaligned32 ((core_img + GRUB_DISK_SECTOR_SIZE
589                                + GRUB_KERNEL_I386_PC_REED_SOLOMON_REDUNDANCY),
590                               grub_host_to_target32 (nsec * GRUB_DISK_SECTOR_SIZE - core_size));
591
592         void *tmp = xmalloc (core_size);
593         grub_memcpy (tmp, core_img, core_size);
594         grub_reed_solomon_add_redundancy (core_img + no_rs_length + GRUB_DISK_SECTOR_SIZE,
595                                           core_size - no_rs_length - GRUB_DISK_SECTOR_SIZE,
596                                           nsec * GRUB_DISK_SECTOR_SIZE
597                                           - core_size);
598         assert (grub_memcmp (tmp, core_img, core_size) == 0);
599         free (tmp);
600       }
601
602     /* Write the core image onto the disk.  */
603     for (i = 0; i < nsec; i++)
604       grub_disk_write (dest_dev->disk, sectors[i], 0,
605                        GRUB_DISK_SECTOR_SIZE,
606                        core_img + i * GRUB_DISK_SECTOR_SIZE);
607
608     grub_free (sectors);
609
610     goto finish;
611   }
612
613 unable_to_embed:
614 #endif
615
616   if (dest_dev->disk->dev->id != root_dev->disk->dev->id)
617     grub_util_error ("%s", _("embedding is not possible, but this is required for "
618                              "RAID and LVM install"));
619
620   {
621     grub_fs_t fs;
622     fs = grub_fs_probe (root_dev);
623     if (!fs)
624       grub_util_error (_("can't determine filesystem on %s"), root);
625
626     if (!fs->blocklist_install)
627       grub_util_error (_("filesystem `%s' doesn't support blocklists"),
628                        fs->name);
629   }
630
631 #ifdef GRUB_SETUP_BIOS
632   if (dest_dev->disk->id != root_dev->disk->id
633       || dest_dev->disk->dev->id != root_dev->disk->dev->id)
634     /* TRANSLATORS: cross-disk refers to /boot being on one disk
635        but MBR on another.  */
636     grub_util_error ("%s", _("embedding is not possible, but this is required for "
637                              "cross-disk install"));
638 #else
639   core_dev = root_dev;
640 #endif
641
642   grub_util_warn ("%s", _("Embedding is not possible.  GRUB can only be installed in this "
643                           "setup by using blocklists.  However, blocklists are UNRELIABLE and "
644                           "their use is discouraged."));
645   if (! force)
646     /* TRANSLATORS: Here GRUB refuses to continue with blocklist install.  */
647     grub_util_error ("%s", _("will not proceed with blocklists"));
648
649   /* The core image must be put on a filesystem unfortunately.  */
650   grub_util_info ("will leave the core image on the filesystem");
651
652   grub_util_biosdisk_flush (root_dev->disk);
653
654   /* Clean out the blocklists.  */
655   bl.block = bl.first_block;
656   while (bl.block->len)
657     {
658       bl.block->start = 0;
659       bl.block->len = 0;
660 #ifdef GRUB_SETUP_BIOS
661       bl.block->segment = 0;
662 #endif
663
664       bl.block--;
665
666       if ((char *) bl.block <= core_img)
667         grub_util_error ("%s", _("no terminator in the core image"));
668     }
669
670   bl.block = bl.first_block;
671
672 #ifdef GRUB_SETUP_SPARC64
673   {
674     grub_partition_t container = root_dev->disk->partition;
675     bl.gpt_offset = 0;
676
677     if (grub_strstr (container->partmap->name, "gpt"))
678       bl.gpt_offset = grub_partition_get_start (container);
679   }
680 #endif
681
682   grub_install_get_blocklist (root_dev, core_path, core_img, core_size,
683                               save_blocklists, &bl);
684
685   if (bl.first_sector == (grub_disk_addr_t)-1)
686     grub_util_error ("%s", _("can't retrieve blocklists"));
687
688 #ifdef GRUB_SETUP_SPARC64
689   {
690     char *boot_devpath;
691     boot_devpath = (char *) (boot_img
692                              + GRUB_BOOT_AOUT_HEADER_SIZE
693                              + GRUB_BOOT_MACHINE_BOOT_DEVPATH);
694     if (dest_dev->disk->id != root_dev->disk->id
695         || dest_dev->disk->dev->id != root_dev->disk->dev->id)
696       {
697         char *dest_ofpath;
698         dest_ofpath
699           = grub_util_devname_to_ofpath (grub_util_biosdisk_get_osdev (root_dev->disk));
700         /* FIXME handle NULL result */
701         grub_util_info ("dest_ofpath is `%s'", dest_ofpath);
702         strncpy (boot_devpath, dest_ofpath,
703                  GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
704                  - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1);
705         boot_devpath[GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
706                    - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1] = 0;
707         free (dest_ofpath);
708       }
709     else
710       {
711         grub_util_info ("non cross-disk install");
712         memset (boot_devpath, 0, GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
713                 - GRUB_BOOT_MACHINE_BOOT_DEVPATH);
714       }
715     grub_util_info ("boot device path %s", boot_devpath);
716   }
717 #endif
718
719   write_rootdev (root_dev, boot_img, bl.first_sector);
720
721   /* Write the first two sectors of the core image onto the disk.  */
722   grub_util_info ("opening the core image `%s'", core_path);
723   fp = grub_util_fd_open (core_path, GRUB_UTIL_FD_O_WRONLY);
724   if (! GRUB_UTIL_FD_IS_VALID (fp))
725     grub_util_error (_("cannot open `%s': %s"), core_path,
726                      grub_util_fd_strerror ());
727
728   if (grub_util_fd_write (fp, core_img, GRUB_DISK_SECTOR_SIZE * 2)
729       != GRUB_DISK_SECTOR_SIZE * 2)
730     grub_util_error (_("cannot write to `%s': %s"),
731                      core_path, strerror (errno));
732   grub_util_fd_sync (fp);
733   grub_util_fd_close (fp);
734   grub_util_biosdisk_flush (root_dev->disk);
735
736   grub_disk_cache_invalidate_all ();
737
738   {
739     char *buf, *ptr = core_img;
740     size_t len = core_size;
741     grub_uint64_t blk, offset = 0;
742     grub_partition_t container = core_dev->disk->partition;
743     grub_err_t err;
744
745     core_dev->disk->partition = 0;
746 #ifdef GRUB_SETUP_SPARC64
747     offset = bl.gpt_offset;
748 #endif
749
750     buf = xmalloc (core_size);
751     blk = bl.first_sector;
752     err = grub_disk_read (core_dev->disk, blk + offset, 0, GRUB_DISK_SECTOR_SIZE, buf);
753     if (err)
754       grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
755                        grub_errmsg);
756     if (grub_memcmp (buf, ptr, GRUB_DISK_SECTOR_SIZE) != 0)
757       grub_util_error ("%s", _("blocklists are invalid"));
758
759     ptr += GRUB_DISK_SECTOR_SIZE;
760     len -= GRUB_DISK_SECTOR_SIZE;
761
762     bl.block = bl.first_block;
763     while (bl.block->len)
764       {
765         size_t cur = grub_target_to_host16 (bl.block->len) << GRUB_DISK_SECTOR_BITS;
766         blk = grub_target_to_host64 (bl.block->start);
767
768         if (cur > len)
769           cur = len;
770
771         err = grub_disk_read (core_dev->disk, blk + offset, 0, cur, buf);
772         if (err)
773           grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
774                            grub_errmsg);
775
776         if (grub_memcmp (buf, ptr, cur) != 0)
777           grub_util_error ("%s", _("blocklists are invalid"));
778
779         ptr += cur;
780         len -= cur;
781         bl.block--;
782         
783         if ((char *) bl.block <= core_img)
784           grub_util_error ("%s", _("no terminator in the core image"));
785       }
786     if (len)
787       grub_util_error ("%s", _("blocklists are incomplete"));
788     core_dev->disk->partition = container;
789     free (buf);
790   }
791
792 #ifdef GRUB_SETUP_BIOS
793  finish:
794 #endif
795
796   /* Write the boot image onto the disk.  */
797   if (grub_disk_write (dest_dev->disk, BOOT_SECTOR,
798                        0, GRUB_DISK_SECTOR_SIZE, boot_img))
799     grub_util_error ("%s", grub_errmsg);
800
801   grub_util_biosdisk_flush (root_dev->disk);
802   grub_util_biosdisk_flush (dest_dev->disk);
803
804   free (core_path);
805   free (core_img);
806   free (boot_img);
807   grub_device_close (dest_dev);
808   grub_device_close (root_dev);
809 }
810