4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Milan Jurik. All rights reserved.
24 * Copyright 2016 Toomas Soome <tsoome@me.com>
25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
43 #include <sys/mount.h>
44 #include <sys/mnttab.h>
45 #include <sys/dktp/fdisk.h>
48 #include <sys/types.h>
50 #include <sys/multiboot.h>
51 #include <sys/sysmacros.h>
52 #include <sys/efi_partition.h>
54 #include <libnvpair.h>
58 #include "installgrub.h"
59 #include "./../common/bblk_einfo.h"
60 #include "./../common/boot_utils.h"
61 #include "./../common/mboot_extra.h"
62 #include "getresponse.h"
65 #define TEXT_DOMAIN "SUNW_OST_OSCMD"
69 * Variables to track installgrub desired mode of operation.
70 * 'nowrite' and 'boot_debug' come from boot_common.h.
72 static boolean_t write_mbr
= B_FALSE
;
73 static boolean_t force_mbr
= B_FALSE
;
74 static boolean_t force_update
= B_FALSE
;
75 static boolean_t do_getinfo
= B_FALSE
;
76 static boolean_t do_version
= B_FALSE
;
77 static boolean_t do_mirror_bblk
= B_FALSE
;
78 static boolean_t strip
= B_FALSE
;
79 static boolean_t verbose_dump
= B_FALSE
;
81 /* Installing the bootblock is the default operation. */
82 static boolean_t do_install
= B_TRUE
;
84 /* Versioning string, if present. */
85 static char *update_str
;
88 * Temporary buffer to store the first 32K of data looking for a multiboot
91 char mboot_scan
[MBOOT_SCAN_SIZE
];
93 /* Function prototypes. */
94 static void check_options(char *);
95 static int handle_install(char *, char **);
96 static int handle_mirror(char *, char **);
97 static int handle_getinfo(char *, char **);
98 static int commit_to_disk(ig_data_t
*, char *);
99 static int init_device(ig_device_t
*, char *path
);
100 static void cleanup_device(ig_device_t
*);
101 static void cleanup_stage2(ig_stage2_t
*);
102 static int get_start_sector(ig_device_t
*);
103 static int get_disk_fd(ig_device_t
*device
);
104 static int get_raw_partition_fd(ig_device_t
*);
105 static char *get_raw_partition_path(ig_device_t
*);
106 static int propagate_bootblock(ig_data_t
*, ig_data_t
*, char *);
107 static int find_x86_bootpar(struct mboot
*, int *, uint32_t *);
108 static int write_stage2(ig_data_t
*);
109 static int write_stage1(ig_data_t
*);
110 static void usage(char *);
111 static int read_stage1_from_file(char *, ig_data_t
*);
112 static int read_stage2_from_file(char *, ig_data_t
*);
113 static int read_stage1_from_disk(int, char *);
114 static int read_stage2_from_disk(int, ig_stage2_t
*, int);
115 static int prepare_stage1(ig_data_t
*);
116 static int prepare_stage2(ig_data_t
*, char *);
117 static void prepare_fake_multiboot(ig_stage2_t
*);
118 static void add_stage2_einfo(ig_stage2_t
*, char *updt_str
);
119 static boolean_t
is_update_necessary(ig_data_t
*, char *);
121 extern int read_stage2_blocklist(int, unsigned int *);
124 main(int argc
, char *argv
[])
132 (void) setlocale(LC_ALL
, "");
133 (void) textdomain(TEXT_DOMAIN
);
134 if (init_yes() < 0) {
135 (void) fprintf(stderr
, gettext(ERR_MSG_INIT_YES
),
141 * retro-compatibility: installing the bootblock is the default
142 * and there is no switch for it.
146 while ((opt
= getopt(argc
, argv
, "dVMFfmneiu:")) != EOF
) {
159 do_install
= B_FALSE
;
163 verbose_dump
= B_TRUE
;
169 force_update
= B_TRUE
;
175 do_mirror_bblk
= B_TRUE
;
176 do_install
= B_FALSE
;
182 update_str
= malloc(strlen(optarg
) + 1);
183 if (update_str
== NULL
) {
184 (void) fprintf(stderr
, gettext("Unable to "
185 "allocate memory\n"));
188 (void) strlcpy(update_str
, optarg
, strlen(optarg
) + 1);
191 /* fall through to process non-optional args */
196 /* check arguments */
197 if (argc
!= optind
+ params
) {
203 * clean up options (and bail out if an unrecoverable combination is
207 check_options(progname
);
208 handle_args
= argv
+ optind
;
211 (void) fprintf(stdout
, DRY_RUN
);
214 ret
= handle_getinfo(progname
, handle_args
);
215 } else if (do_mirror_bblk
) {
216 ret
= handle_mirror(progname
, handle_args
);
218 ret
= handle_install(progname
, handle_args
);
223 #define MEANINGLESS_OPT gettext("%s specified but meaningless, ignoring\n")
225 check_options(char *progname
)
227 if (do_getinfo
&& do_mirror_bblk
) {
228 (void) fprintf(stderr
, gettext("Only one of -M and -i can be "
229 "specified at the same time\n"));
234 if (do_mirror_bblk
) {
236 * -u and -F may actually reflect a user intent that is not
237 * correct with this command (mirror can be interpreted
238 * "similar" to install. Emit a message and continue.
239 * -e and -V have no meaning, be quiet here and only report the
240 * incongruence if a debug output is requested.
243 (void) fprintf(stderr
, MEANINGLESS_OPT
, "-u");
244 do_version
= B_FALSE
;
247 (void) fprintf(stderr
, MEANINGLESS_OPT
, "-F");
248 force_update
= B_FALSE
;
250 if (strip
|| verbose_dump
) {
251 BOOT_DEBUG(MEANINGLESS_OPT
, "-e|-V");
253 verbose_dump
= B_FALSE
;
258 if (write_mbr
|| force_mbr
|| do_version
|| force_update
) {
259 BOOT_DEBUG(MEANINGLESS_OPT
, "-m|-f|-u|-F");
260 write_mbr
= force_mbr
= do_version
= B_FALSE
;
261 force_update
= B_FALSE
;
267 * Install a new stage1/stage2 pair on the specified device. handle_install()
268 * expects argv to contain 3 parameters (the path to stage1, the path to stage2,
269 * the target device).
271 * Returns: BC_SUCCESS - if the installation is successful
272 * BC_ERROR - if the installation failed
273 * BC_NOUPDT - if no installation was performed because the GRUB
274 * version currently installed is more recent than the
279 handle_install(char *progname
, char **argv
)
281 ig_data_t install_data
;
282 char *stage1_path
= NULL
;
283 char *stage2_path
= NULL
;
284 char *device_path
= NULL
;
287 stage1_path
= strdup(argv
[0]);
288 stage2_path
= strdup(argv
[1]);
289 device_path
= strdup(argv
[2]);
291 bzero(&install_data
, sizeof (ig_data_t
));
293 if (!stage1_path
|| !stage2_path
|| !device_path
) {
294 (void) fprintf(stderr
, gettext("Missing parameter"));
299 BOOT_DEBUG("stage1 path: %s, stage2 path: %s, device: %s\n",
300 stage1_path
, stage2_path
, device_path
);
302 if (init_device(&install_data
.device
, device_path
) != BC_SUCCESS
) {
303 (void) fprintf(stderr
, gettext("Unable to gather device "
304 "information for %s\n"), device_path
);
308 /* read in stage1 and stage2. */
309 if (read_stage1_from_file(stage1_path
, &install_data
) != BC_SUCCESS
) {
310 (void) fprintf(stderr
, gettext("Error opening %s\n"),
315 if (read_stage2_from_file(stage2_path
, &install_data
) != BC_SUCCESS
) {
316 (void) fprintf(stderr
, gettext("Error opening %s\n"),
321 /* We do not support versioning on PCFS. */
322 if (is_bootpar(install_data
.device
.type
) && do_version
)
323 do_version
= B_FALSE
;
326 * is_update_necessary() will take care of checking if versioning and/or
327 * forcing the update have been specified. It will also emit a warning
328 * if a non-versioned update is attempted over a versioned bootblock.
330 if (!is_update_necessary(&install_data
, update_str
)) {
331 (void) fprintf(stderr
, gettext("GRUB version installed "
332 "on %s is more recent or identical\n"
333 "Use -F to override or install without the -u option\n"),
340 * - the installed GRUB version is older than the one about to be
342 * - no versioning string has been passed through the command line.
343 * - a forced update is requested (-F).
345 BOOT_DEBUG("Ready to commit to disk\n");
346 ret
= commit_to_disk(&install_data
, update_str
);
349 cleanup_device(&install_data
.device
);
358 * Retrieves from a device the extended information (einfo) associated to the
360 * Expects one parameter, the device path, in the form: /dev/rdsk/c?[t?]d?s0.
362 * - BC_SUCCESS (and prints out einfo contents depending on 'flags')
363 * - BC_ERROR (on error)
364 * - BC_NOEINFO (no extended information available)
367 handle_getinfo(char *progname
, char **argv
)
370 ig_stage2_t
*stage2
= &data
.stage2
;
371 ig_device_t
*device
= &data
.device
;
376 int retval
= BC_ERROR
;
379 device_path
= strdup(argv
[0]);
381 (void) fprintf(stderr
, gettext("Missing parameter"));
386 bzero(&data
, sizeof (ig_data_t
));
387 BOOT_DEBUG("device path: %s\n", device_path
);
389 if (init_device(device
, device_path
) != BC_SUCCESS
) {
390 (void) fprintf(stderr
, gettext("Unable to gather device "
391 "information for %s\n"), device_path
);
395 if (is_bootpar(device
->type
)) {
396 (void) fprintf(stderr
, gettext("Versioning not supported on "
401 ret
= read_stage2_from_disk(device
->part_fd
, stage2
, device
->type
);
402 if (ret
== BC_ERROR
) {
403 (void) fprintf(stderr
, gettext("Error reading stage2 from "
404 "%s\n"), device_path
);
408 if (ret
== BC_NOEXTRA
) {
409 (void) fprintf(stdout
, gettext("No multiboot header found on "
410 "%s, unable to locate extra information area\n"),
416 einfo
= find_einfo(stage2
->extra
, stage2
->extra_size
);
419 (void) fprintf(stderr
, gettext("No extended information "
424 /* Print the extended information. */
426 flags
|= EINFO_EASY_PARSE
;
428 flags
|= EINFO_PRINT_HEADER
;
430 size
= stage2
->buf_size
- P2ROUNDUP(stage2
->file_size
, 8);
431 print_einfo(flags
, einfo
, size
);
435 cleanup_device(&data
.device
);
442 * Attempt to mirror (propagate) the current stage2 over the attaching disk.
445 * - BC_SUCCESS (a successful propagation happened)
446 * - BC_ERROR (an error occurred)
447 * - BC_NOEXTRA (it is not possible to dump the current bootblock since
448 * there is no multiboot information)
451 handle_mirror(char *progname
, char **argv
)
454 ig_data_t attach_data
;
455 ig_device_t
*curr_device
= &curr_data
.device
;
456 ig_device_t
*attach_device
= &attach_data
.device
;
457 ig_stage2_t
*stage2_curr
= &curr_data
.stage2
;
458 ig_stage2_t
*stage2_attach
= &attach_data
.stage2
;
459 bblk_einfo_t
*einfo_curr
= NULL
;
460 char *curr_device_path
;
461 char *attach_device_path
;
462 char *updt_str
= NULL
;
463 int retval
= BC_ERROR
;
466 curr_device_path
= strdup(argv
[0]);
467 attach_device_path
= strdup(argv
[1]);
469 if (!curr_device_path
|| !attach_device_path
) {
470 (void) fprintf(stderr
, gettext("Missing parameter"));
474 BOOT_DEBUG("Current device path is: %s, attaching device path is: "
475 " %s\n", curr_device_path
, attach_device_path
);
477 bzero(&curr_data
, sizeof (ig_data_t
));
478 bzero(&attach_data
, sizeof (ig_data_t
));
480 if (init_device(curr_device
, curr_device_path
) != BC_SUCCESS
) {
481 (void) fprintf(stderr
, gettext("Unable to gather device "
482 "information for %s (current device)\n"), curr_device_path
);
486 if (init_device(attach_device
, attach_device_path
) != BC_SUCCESS
) {
487 (void) fprintf(stderr
, gettext("Unable to gather device "
488 "information for %s (attaching device)\n"),
493 if (is_bootpar(curr_device
->type
) || is_bootpar(attach_device
->type
)) {
494 (void) fprintf(stderr
, gettext("boot block mirroring is not "
495 "supported on PCFS\n"));
499 ret
= read_stage2_from_disk(curr_device
->part_fd
, stage2_curr
,
501 if (ret
== BC_ERROR
) {
502 BOOT_DEBUG("Error reading first stage2 blocks from %s\n",
508 if (ret
== BC_NOEXTRA
) {
509 BOOT_DEBUG("No multiboot header found on %s, unable to grab "
510 "stage2\n", curr_device
->path
);
515 einfo_curr
= find_einfo(stage2_curr
->extra
, stage2_curr
->extra_size
);
516 if (einfo_curr
!= NULL
)
517 updt_str
= einfo_get_string(einfo_curr
);
521 retval
= propagate_bootblock(&curr_data
, &attach_data
, updt_str
);
522 cleanup_stage2(stage2_curr
);
523 cleanup_stage2(stage2_attach
);
526 cleanup_device(attach_device
);
528 cleanup_device(curr_device
);
530 free(curr_device_path
);
531 free(attach_device_path
);
536 commit_to_disk(ig_data_t
*install
, char *updt_str
)
538 assert(install
!= NULL
);
540 * vanilla stage1 and stage2 need to be updated at runtime.
541 * Update stage2 before stage1 because stage1 needs to know the first
542 * sector stage2 will be written to.
544 if (prepare_stage2(install
, updt_str
) != BC_SUCCESS
) {
545 (void) fprintf(stderr
, gettext("Error building stage2\n"));
548 if (prepare_stage1(install
) != BC_SUCCESS
) {
549 (void) fprintf(stderr
, gettext("Error building stage1\n"));
553 /* Write stage2 out to disk. */
554 if (write_stage2(install
) != BC_SUCCESS
) {
555 (void) fprintf(stderr
, gettext("Error writing stage2 to "
560 /* Write stage1 to disk and, if requested, to the MBR. */
561 if (write_stage1(install
) != BC_SUCCESS
) {
562 (void) fprintf(stderr
, gettext("Error writing stage1 to "
571 * Propagate the bootblock on the source disk to the destination disk and
572 * version it with 'updt_str' in the process. Since we cannot trust any data
573 * on the attaching disk, we do not perform any specific check on a potential
574 * target extended information structure and we just blindly update.
577 propagate_bootblock(ig_data_t
*source
, ig_data_t
*target
, char *updt_str
)
579 ig_device_t
*src_device
= &source
->device
;
580 ig_device_t
*dest_device
= &target
->device
;
581 ig_stage2_t
*src_stage2
= &source
->stage2
;
582 ig_stage2_t
*dest_stage2
= &target
->stage2
;
586 assert(source
!= NULL
);
587 assert(target
!= NULL
);
589 /* read in stage1 from the source disk. */
590 if (read_stage1_from_disk(src_device
->part_fd
, target
->stage1_buf
)
594 /* Prepare target stage2 for commit_to_disk. */
595 cleanup_stage2(dest_stage2
);
597 if (updt_str
!= NULL
)
600 do_version
= B_FALSE
;
602 buf_size
= src_stage2
->file_size
+ SECTOR_SIZE
;
604 dest_stage2
->buf_size
= P2ROUNDUP(buf_size
, SECTOR_SIZE
);
605 dest_stage2
->buf
= malloc(dest_stage2
->buf_size
);
606 if (dest_stage2
->buf
== NULL
) {
607 perror(gettext("Memory allocation failed"));
610 dest_stage2
->file
= dest_stage2
->buf
;
611 dest_stage2
->file_size
= src_stage2
->file_size
;
612 memcpy(dest_stage2
->file
, src_stage2
->file
, dest_stage2
->file_size
);
613 dest_stage2
->extra
= dest_stage2
->buf
+
614 P2ROUNDUP(dest_stage2
->file_size
, 8);
616 /* If we get down here we do have a mboot structure. */
617 assert(src_stage2
->mboot
);
619 dest_stage2
->mboot_off
= src_stage2
->mboot_off
;
620 dest_stage2
->mboot
= (multiboot_header_t
*)(dest_stage2
->buf
+
621 dest_stage2
->mboot_off
);
623 (void) fprintf(stdout
, gettext("Propagating %s stage1/stage2 to %s\n"),
624 src_device
->path
, dest_device
->path
);
625 retval
= commit_to_disk(target
, updt_str
);
631 * open the device and fill the various members of ig_device_t.
634 init_device(ig_device_t
*device
, char *path
)
640 bzero(device
, sizeof (*device
));
641 device
->part_fd
= -1;
642 device
->disk_fd
= -1;
643 device
->path_p0
= NULL
;
645 device
->path
= strdup(path
);
646 if (device
->path
== NULL
) {
647 perror(gettext("Memory allocation failed"));
651 if (strstr(device
->path
, "diskette")) {
652 (void) fprintf(stderr
, gettext("installing GRUB to a floppy "
653 "disk is no longer supported\n"));
657 /* Detect if the target device is a pcfs partition. */
658 if (strstr(device
->path
, "p0:boot"))
659 device
->type
= IG_DEV_X86BOOTPAR
;
661 if (get_disk_fd(device
) != BC_SUCCESS
)
664 /* read in the device boot sector. */
665 if (read(device
->disk_fd
, device
->boot_sector
, SECTOR_SIZE
)
667 (void) fprintf(stderr
, gettext("Error reading boot sector\n"));
672 if (efi_alloc_and_read(device
->disk_fd
, &vtoc
) >= 0) {
673 device
->type
= IG_DEV_EFI
;
677 if (get_raw_partition_fd(device
) != BC_SUCCESS
)
680 if (is_efi(device
->type
)) {
681 if (fstyp_init(device
->part_fd
, 0, NULL
, &fhdl
) != 0)
684 if (fstyp_ident(fhdl
, "zfs", &fident
) != 0) {
686 (void) fprintf(stderr
, gettext("Booting of EFI labeled "
687 "disks is only supported with ZFS\n"));
693 if (get_start_sector(device
) != BC_SUCCESS
)
700 cleanup_device(ig_device_t
*device
)
705 free(device
->path_p0
);
707 if (device
->part_fd
!= -1)
708 (void) close(device
->part_fd
);
709 if (device
->disk_fd
!= -1)
710 (void) close(device
->disk_fd
);
712 bzero(device
, sizeof (ig_device_t
));
713 device
->part_fd
= -1;
714 device
->disk_fd
= -1;
718 cleanup_stage2(ig_stage2_t
*stage2
)
722 bzero(stage2
, sizeof (ig_stage2_t
));
726 get_start_sector(ig_device_t
*device
)
728 uint32_t secnum
= 0, numsec
= 0;
729 int i
, pno
, rval
, log_part
= 0;
731 struct ipart
*part
= NULL
;
733 struct part_info dkpi
;
734 struct extpart_info edkpi
;
736 if (is_efi(device
->type
)) {
739 if (efi_alloc_and_read(device
->disk_fd
, &vtoc
) < 0)
742 device
->start_sector
= vtoc
->efi_parts
[device
->slice
].p_start
;
743 /* GPT doesn't use traditional slice letters */
744 device
->slice
= 0xff;
745 device
->partition
= 0;
751 mboot
= (struct mboot
*)device
->boot_sector
;
753 if (is_bootpar(device
->type
)) {
754 if (find_x86_bootpar(mboot
, &pno
, &secnum
) != BC_SUCCESS
) {
755 (void) fprintf(stderr
, NOBOOTPAR
);
758 device
->start_sector
= secnum
;
759 device
->partition
= pno
;
765 * Search for Solaris fdisk partition
766 * Get the solaris partition information from the device
767 * and compare the offset of S2 with offset of solaris partition
768 * from fdisk partition table.
770 if (ioctl(device
->part_fd
, DKIOCEXTPARTINFO
, &edkpi
) < 0) {
771 if (ioctl(device
->part_fd
, DKIOCPARTINFO
, &dkpi
) < 0) {
772 (void) fprintf(stderr
, PART_FAIL
);
775 edkpi
.p_start
= dkpi
.p_start
;
779 for (i
= 0; i
< FD_NUMPART
; i
++) {
780 part
= (struct ipart
*)mboot
->parts
+ i
;
782 if (part
->relsect
== 0) {
783 (void) fprintf(stderr
, BAD_PART
, i
);
787 if (edkpi
.p_start
>= part
->relsect
&&
788 edkpi
.p_start
< (part
->relsect
+ part
->numsect
)) {
789 /* Found the partition */
794 if (i
== FD_NUMPART
) {
795 /* No solaris fdisk partitions (primary or logical) */
796 (void) fprintf(stderr
, NOSOLPAR
);
801 * We have found a Solaris fdisk partition (primary or extended)
802 * Handle the simple case first: Solaris in a primary partition
804 if (!fdisk_is_dos_extended(part
->systid
)) {
805 device
->start_sector
= part
->relsect
;
806 device
->partition
= i
;
811 * Solaris in a logical partition. Find that partition in the
814 if ((rval
= libfdisk_init(&epp
, device
->path_p0
, NULL
, FDISK_READ_DISK
))
818 * The first 3 cases are not an error per-se, just that
819 * there is no Solaris logical partition
821 case FDISK_EBADLOGDRIVE
:
822 case FDISK_ENOLOGDRIVE
:
823 case FDISK_EBADMAGIC
:
824 (void) fprintf(stderr
, NOSOLPAR
);
827 (void) fprintf(stderr
, NO_VIRT_GEOM
);
830 (void) fprintf(stderr
, NO_PHYS_GEOM
);
833 (void) fprintf(stderr
, NO_LABEL_GEOM
);
836 (void) fprintf(stderr
, LIBFDISK_INIT_FAIL
);
841 rval
= fdisk_get_solaris_part(epp
, &pno
, &secnum
, &numsec
);
843 if (rval
!= FDISK_SUCCESS
) {
844 /* No solaris logical partition */
845 (void) fprintf(stderr
, NOSOLPAR
);
849 device
->start_sector
= secnum
;
850 device
->partition
= pno
- 1;
854 /* get confirmation for -m */
855 if (write_mbr
&& !force_mbr
) {
856 (void) fprintf(stdout
, MBOOT_PROMPT
);
859 (void) fprintf(stdout
, MBOOT_NOT_UPDATED
);
865 * Currently if Solaris is in an extended partition we need to
866 * write GRUB to the MBR. Check for this.
868 if (log_part
&& !write_mbr
) {
869 (void) fprintf(stdout
, gettext("Installing Solaris on an "
870 "extended partition... forcing MBR update\n"));
875 * warn, if Solaris in primary partition and GRUB not in MBR and
876 * partition is not active
879 if (!log_part
&& part
->bootid
!= 128 && !write_mbr
) {
880 (void) fprintf(stdout
, SOLPAR_INACTIVE
,
881 device
->partition
+ 1);
889 get_disk_fd(ig_device_t
*device
)
892 char save
[2] = { '\0', '\0' };
895 assert(device
!= NULL
);
896 assert(device
->path
!= NULL
);
898 if (is_bootpar(device
->type
)) {
899 end
= strstr(device
->path
, "p0:boot");
900 /* tested at the start of init_device() */
906 i
= strlen(device
->path
);
907 save
[0] = device
->path
[i
- 2];
908 save
[1] = device
->path
[i
- 1];
909 device
->path
[i
- 2] = 'p';
910 device
->path
[i
- 1] = '0';
914 device
->disk_fd
= open(device
->path
, O_RDONLY
);
916 device
->disk_fd
= open(device
->path
, O_RDWR
);
918 device
->path_p0
= strdup(device
->path
);
919 if (device
->path_p0
== NULL
) {
924 if (is_bootpar(device
->type
)) {
927 device
->path
[i
- 2] = save
[0];
928 device
->path
[i
- 1] = save
[1];
931 if (device
->disk_fd
== -1) {
940 prepare_fake_multiboot(ig_stage2_t
*stage2
)
942 multiboot_header_t
*mboot
;
944 assert(stage2
!= NULL
);
945 assert(stage2
->mboot
!= NULL
);
946 assert(stage2
->buf
!= NULL
);
948 mboot
= stage2
->mboot
;
951 * Currently we expect find_multiboot() to have located a multiboot
952 * header with the AOUT kludge flag set.
954 assert(mboot
->flags
& BB_MBOOT_AOUT_FLAG
);
956 /* Insert the information necessary to locate stage2. */
957 mboot
->header_addr
= stage2
->mboot_off
;
958 mboot
->load_addr
= 0;
959 mboot
->load_end_addr
= stage2
->file_size
;
963 add_stage2_einfo(ig_stage2_t
*stage2
, char *updt_str
)
966 uint32_t avail_space
;
968 assert(stage2
!= NULL
);
970 /* Fill bootblock hashing source information. */
971 hs
.src_buf
= (unsigned char *)stage2
->file
;
972 hs
.src_size
= stage2
->file_size
;
973 /* How much space for the extended information structure? */
974 avail_space
= stage2
->buf_size
- P2ROUNDUP(stage2
->file_size
, 8);
975 add_einfo(stage2
->extra
, updt_str
, &hs
, avail_space
);
980 write_stage2(ig_data_t
*install
)
982 ig_device_t
*device
= &install
->device
;
983 ig_stage2_t
*stage2
= &install
->stage2
;
986 assert(install
!= NULL
);
988 if (is_bootpar(device
->type
)) {
990 * stage2 is already on the filesystem, we only need to update
991 * the first two blocks (that we have modified during
994 if (write_out(device
->part_fd
, stage2
->file
, SECTOR_SIZE
,
995 stage2
->pcfs_first_sectors
[0] * SECTOR_SIZE
)
997 write_out(device
->part_fd
, stage2
->file
+ SECTOR_SIZE
,
998 SECTOR_SIZE
, stage2
->pcfs_first_sectors
[1] * SECTOR_SIZE
)
1000 (void) fprintf(stderr
, WRITE_FAIL_STAGE2
);
1003 (void) fprintf(stdout
, WRITE_STAGE2_PCFS
);
1004 return (BC_SUCCESS
);
1008 * For disk, write stage2 starting at STAGE2_BLKOFF sector.
1009 * Note that we use stage2->buf rather than stage2->file, because we
1010 * may have extended information after the latter.
1012 * If we're writing to an EFI-labeled disk where stage2 lives in the
1013 * 3.5MB boot loader gap following the ZFS vdev labels, make sure the
1014 * size of the buffer doesn't exceed the size of the gap.
1016 if (is_efi(device
->type
) && stage2
->buf_size
> STAGE2_MAXSIZE
) {
1017 (void) fprintf(stderr
, WRITE_FAIL_STAGE2
);
1021 offset
= STAGE2_BLKOFF(device
->type
) * SECTOR_SIZE
;
1023 if (write_out(device
->part_fd
, stage2
->buf
, stage2
->buf_size
,
1024 offset
) != BC_SUCCESS
) {
1029 /* Simulate the "old" installgrub output. */
1030 (void) fprintf(stdout
, WRITE_STAGE2_DISK
, device
->partition
,
1031 (stage2
->buf_size
/ SECTOR_SIZE
) + 1, STAGE2_BLKOFF(device
->type
),
1032 stage2
->first_sector
);
1034 return (BC_SUCCESS
);
1038 write_stage1(ig_data_t
*install
)
1040 ig_device_t
*device
= &install
->device
;
1042 assert(install
!= NULL
);
1044 if (write_out(device
->part_fd
, install
->stage1_buf
,
1045 sizeof (install
->stage1_buf
), 0) != BC_SUCCESS
) {
1046 (void) fprintf(stdout
, WRITE_FAIL_PBOOT
);
1051 /* Simulate "old" installgrub output. */
1052 (void) fprintf(stdout
, WRITE_PBOOT
, device
->partition
,
1053 device
->start_sector
);
1056 if (write_out(device
->disk_fd
, install
->stage1_buf
,
1057 sizeof (install
->stage1_buf
), 0) != BC_SUCCESS
) {
1058 (void) fprintf(stdout
, WRITE_FAIL_BOOTSEC
);
1062 /* Simulate "old" installgrub output. */
1063 (void) fprintf(stdout
, WRITE_MBOOT
);
1066 return (BC_SUCCESS
);
1069 #define USAGE_STRING "%s [-m|-f|-n|-F|-u verstr] stage1 stage2 device\n" \
1070 "%s -M [-n] device1 device2\n" \
1071 "%s [-V|-e] -i device\n" \
1073 #define CANON_USAGE_STR gettext(USAGE_STRING)
1076 usage(char *progname
)
1078 (void) fprintf(stdout
, CANON_USAGE_STR
, progname
, progname
, progname
);
1083 read_stage1_from_file(char *path
, ig_data_t
*dest
)
1089 /* read the stage1 file from filesystem */
1090 fd
= open(path
, O_RDONLY
);
1092 read(fd
, dest
->stage1_buf
, SECTOR_SIZE
) != SECTOR_SIZE
) {
1093 (void) fprintf(stderr
, READ_FAIL_STAGE1
, path
);
1097 return (BC_SUCCESS
);
1101 read_stage2_from_file(char *path
, ig_data_t
*dest
)
1105 ig_stage2_t
*stage2
= &dest
->stage2
;
1106 ig_device_t
*device
= &dest
->device
;
1110 assert(stage2
->buf
== NULL
);
1112 fd
= open(path
, O_RDONLY
);
1113 if (fstat(fd
, &sb
) == -1) {
1118 stage2
->file_size
= sb
.st_size
;
1120 if (!is_bootpar(device
->type
)) {
1122 * buffer size needs to account for stage2 plus the extra
1123 * versioning information at the end of it. We reserve one
1124 * extra sector (plus we round up to the next sector boundary).
1126 buf_size
= stage2
->file_size
+ SECTOR_SIZE
;
1128 /* In the PCFS case we only need to read in stage2. */
1129 buf_size
= stage2
->file_size
;
1132 stage2
->buf_size
= P2ROUNDUP(buf_size
, SECTOR_SIZE
);
1134 BOOT_DEBUG("stage2 buffer size = %d (%d sectors)\n", stage2
->buf_size
,
1135 stage2
->buf_size
/ SECTOR_SIZE
);
1137 stage2
->buf
= malloc(stage2
->buf_size
);
1138 if (stage2
->buf
== NULL
) {
1139 perror(gettext("Memory allocation failed"));
1143 stage2
->file
= stage2
->buf
;
1146 * Extra information (e.g. the versioning structure) is placed at the
1147 * end of stage2, aligned on a 8-byte boundary.
1149 if (!(is_bootpar(device
->type
)))
1150 stage2
->extra
= stage2
->file
+ P2ROUNDUP(stage2
->file_size
, 8);
1152 if (lseek(fd
, 0, SEEK_SET
) == -1) {
1157 if (read(fd
, stage2
->file
, stage2
->file_size
) < 0) {
1158 perror(gettext("unable to read stage2"));
1163 return (BC_SUCCESS
);
1175 prepare_stage1(ig_data_t
*install
)
1177 ig_device_t
*device
= &install
->device
;
1179 assert(install
!= NULL
);
1181 /* If PCFS add the BIOS Parameter Block. */
1182 if (is_bootpar(device
->type
)) {
1183 char bpb_sect
[SECTOR_SIZE
];
1185 if (pread(device
->part_fd
, bpb_sect
, SECTOR_SIZE
, 0)
1187 (void) fprintf(stderr
, READ_FAIL_BPB
);
1190 bcopy(bpb_sect
+ STAGE1_BPB_OFFSET
,
1191 install
->stage1_buf
+ STAGE1_BPB_OFFSET
, STAGE1_BPB_SIZE
);
1194 /* copy MBR to stage1 in case of overwriting MBR sector. */
1195 bcopy(device
->boot_sector
+ BOOTSZ
, install
->stage1_buf
+ BOOTSZ
,
1196 SECTOR_SIZE
- BOOTSZ
);
1197 /* modify default stage1 file generated by GRUB. */
1198 *((unsigned char *)(install
->stage1_buf
+ STAGE1_FORCE_LBA
)) = 1;
1199 *((ulong_t
*)(install
->stage1_buf
+ STAGE1_STAGE2_SECTOR
))
1200 = install
->stage2
.first_sector
;
1201 *((ushort_t
*)(install
->stage1_buf
+ STAGE1_STAGE2_ADDRESS
))
1203 *((ushort_t
*)(install
->stage1_buf
+ STAGE1_STAGE2_SEGMENT
))
1204 = STAGE2_MEMADDR
>> 4;
1206 return (BC_SUCCESS
);
1210 * Grab stage1 from the specified device file descriptor.
1213 read_stage1_from_disk(int dev_fd
, char *stage1_buf
)
1215 assert(stage1_buf
!= NULL
);
1217 if (read_in(dev_fd
, stage1_buf
, SECTOR_SIZE
, 0) != BC_SUCCESS
) {
1218 perror(gettext("Unable to read stage1 from disk"));
1221 return (BC_SUCCESS
);
1225 read_stage2_from_disk(int dev_fd
, ig_stage2_t
*stage2
, int type
)
1230 multiboot_header_t
*mboot
;
1232 assert(stage2
!= NULL
);
1233 assert(dev_fd
!= -1);
1235 if (read_in(dev_fd
, mboot_scan
, sizeof (mboot_scan
),
1236 STAGE2_BLKOFF(type
) * SECTOR_SIZE
) != BC_SUCCESS
) {
1237 perror(gettext("Error reading stage2 sectors"));
1241 /* No multiboot means no chance of knowing stage2 size */
1242 if (find_multiboot(mboot_scan
, sizeof (mboot_scan
), &mboot_off
)
1244 BOOT_DEBUG("Unable to find multiboot header\n");
1245 return (BC_NOEXTRA
);
1247 mboot
= (multiboot_header_t
*)(mboot_scan
+ mboot_off
);
1250 * Unfilled mboot values mean an older version of installgrub installed
1251 * the stage2. Again we have no chance of knowing stage2 size.
1253 if (mboot
->load_end_addr
== 0 ||
1254 mboot
->load_end_addr
< mboot
->load_addr
)
1255 return (BC_NOEXTRA
);
1258 * Currently, the amount of space reserved for extra information
1259 * is "fixed". We may have to scan for the terminating extra payload
1262 size
= mboot
->load_end_addr
- mboot
->load_addr
;
1263 buf_size
= P2ROUNDUP(size
+ SECTOR_SIZE
, SECTOR_SIZE
);
1265 stage2
->buf
= malloc(buf_size
);
1266 if (stage2
->buf
== NULL
) {
1267 perror(gettext("Memory allocation failed"));
1270 stage2
->buf_size
= buf_size
;
1272 if (read_in(dev_fd
, stage2
->buf
, buf_size
, STAGE2_BLKOFF(type
) *
1273 SECTOR_SIZE
) != BC_SUCCESS
) {
1279 /* Update pointers. */
1280 stage2
->file
= stage2
->buf
;
1281 stage2
->file_size
= size
;
1282 stage2
->mboot_off
= mboot_off
;
1283 stage2
->mboot
= (multiboot_header_t
*)(stage2
->buf
+ stage2
->mboot_off
);
1284 stage2
->extra
= stage2
->buf
+ P2ROUNDUP(stage2
->file_size
, 8);
1285 stage2
->extra_size
= stage2
->buf_size
- P2ROUNDUP(stage2
->file_size
, 8);
1287 return (BC_SUCCESS
);
1291 is_update_necessary(ig_data_t
*data
, char *updt_str
)
1293 bblk_einfo_t
*einfo
;
1294 bblk_hs_t stage2_hs
;
1295 ig_stage2_t stage2_disk
;
1296 ig_stage2_t
*stage2_file
= &data
->stage2
;
1297 ig_device_t
*device
= &data
->device
;
1298 int dev_fd
= device
->part_fd
;
1300 assert(data
!= NULL
);
1301 assert(device
->part_fd
!= -1);
1303 bzero(&stage2_disk
, sizeof (ig_stage2_t
));
1305 /* Gather stage2 (if present) from the target device. */
1306 if (read_stage2_from_disk(dev_fd
, &stage2_disk
, device
->type
)
1308 BOOT_DEBUG("Unable to read stage2 from %s\n", device
->path
);
1309 BOOT_DEBUG("No multiboot wrapped stage2 on %s\n", device
->path
);
1314 * Look for the extended information structure in the extra payload
1317 einfo
= find_einfo(stage2_disk
.extra
, stage2_disk
.extra_size
);
1318 if (einfo
== NULL
) {
1319 BOOT_DEBUG("No extended information available\n");
1323 if (!do_version
|| updt_str
== NULL
) {
1324 (void) fprintf(stdout
, "WARNING: target device %s has a "
1325 "versioned stage2 that is going to be overwritten by a non "
1326 "versioned one\n", device
->path
);
1331 BOOT_DEBUG("Forcing update of %s bootblock\n", device
->path
);
1335 /* Compare the two extended information structures. */
1336 stage2_hs
.src_buf
= (unsigned char *)stage2_file
->file
;
1337 stage2_hs
.src_size
= stage2_file
->file_size
;
1339 return (einfo_should_update(einfo
, &stage2_hs
, updt_str
));
1343 #define START_BLOCK(pos) (*(ulong_t *)(pos))
1344 #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4))
1345 #define START_SEG(pos) (*(ushort_t *)((pos) + 6))
1348 prepare_stage2(ig_data_t
*install
, char *updt_str
)
1350 ig_device_t
*device
= &install
->device
;
1351 ig_stage2_t
*stage2
= &install
->stage2
;
1352 uint32_t mboot_off
= 0;
1354 assert(install
!= NULL
);
1355 assert(stage2
->file
!= NULL
);
1357 /* New stage2 files come with an embedded stage2. */
1358 if (find_multiboot(stage2
->file
, stage2
->file_size
, &mboot_off
)
1360 BOOT_DEBUG("WARNING: no multiboot structure found in stage2, "
1361 "are you using an old GRUB stage2?\n");
1362 if (do_version
== B_TRUE
) {
1363 (void) fprintf(stderr
, gettext("Versioning requested "
1364 "but stage2 does not support it.. skipping.\n"));
1365 do_version
= B_FALSE
;
1368 /* Keep track of where the multiboot header is. */
1369 stage2
->mboot_off
= mboot_off
;
1370 stage2
->mboot
= (multiboot_header_t
*)(stage2
->file
+
1374 * Adding stage2 information needs to happen before
1375 * we modify the copy of stage2 we have in memory, so
1376 * that the hashing reflects the one of the file.
1377 * An error here is not fatal.
1379 add_stage2_einfo(stage2
, updt_str
);
1382 * Fill multiboot information. We add them even without
1383 * versioning to support as much as possible mirroring.
1385 prepare_fake_multiboot(stage2
);
1388 if (is_bootpar(device
->type
)) {
1389 uint32_t blocklist
[SECTOR_SIZE
/ sizeof (uint32_t)];
1390 uint32_t install_addr
= STAGE2_MEMADDR
+ SECTOR_SIZE
;
1394 bzero(blocklist
, sizeof (blocklist
));
1395 if (read_stage2_blocklist(device
->part_fd
, blocklist
) != 0) {
1396 (void) fprintf(stderr
, gettext("Error reading pcfs "
1397 "stage2 blocklist\n"));
1401 pos
= (uchar_t
*)stage2
->file
+ STAGE2_BLOCKLIST
;
1402 stage2
->first_sector
= device
->start_sector
+ blocklist
[0];
1403 stage2
->pcfs_first_sectors
[0] = blocklist
[0];
1404 BOOT_DEBUG("stage2 first sector: %d\n", stage2
->first_sector
);
1407 if (blocklist
[1] > 1) {
1414 stage2
->pcfs_first_sectors
[1] = blocklist
[i
];
1416 while (blocklist
[i
]) {
1417 if (START_BLOCK(pos
- 8) != 0 &&
1418 START_BLOCK(pos
- 8) != blocklist
[i
+ 2]) {
1419 (void) fprintf(stderr
, PCFS_FRAGMENTED
);
1422 START_BLOCK(pos
) = blocklist
[i
] + device
->start_sector
;
1423 START_SEG(pos
) = (ushort_t
)(install_addr
>> 4);
1424 NUM_BLOCK(pos
) = blocklist
[i
+ 1];
1425 install_addr
+= blocklist
[i
+ 1] * SECTOR_SIZE
;
1430 /* Solaris VTOC & EFI */
1431 if (device
->start_sector
>
1432 UINT32_MAX
- STAGE2_BLKOFF(device
->type
)) {
1433 fprintf(stderr
, gettext("Error: partition start sector "
1434 "must be less than %lld\n"),
1435 (uint64_t)UINT32_MAX
- STAGE2_BLKOFF(device
->type
));
1438 stage2
->first_sector
= device
->start_sector
+
1439 STAGE2_BLKOFF(device
->type
);
1440 BOOT_DEBUG("stage2 first sector: %d\n", stage2
->first_sector
);
1442 * In a solaris partition, stage2 is written to contiguous
1443 * blocks. So we update the starting block only.
1445 *((ulong_t
*)(stage2
->file
+ STAGE2_BLOCKLIST
)) =
1446 stage2
->first_sector
+ 1;
1449 /* force lba and set disk partition */
1450 *((unsigned char *) (stage2
->file
+ STAGE2_FORCE_LBA
)) = 1;
1451 *((long *)(stage2
->file
+ STAGE2_INSTALLPART
))
1452 = (device
->partition
<< 16) | (device
->slice
<< 8) | 0xff;
1454 return (BC_SUCCESS
);
1458 find_x86_bootpar(struct mboot
*mboot
, int *part_num
, uint32_t *start_sect
)
1462 for (i
= 0; i
< FD_NUMPART
; i
++) {
1465 part
= (struct ipart
*)mboot
->parts
+ i
;
1466 if (part
->systid
== 0xbe) {
1468 *start_sect
= part
->relsect
;
1471 /* solaris boot part */
1472 return (BC_SUCCESS
);
1479 get_raw_partition_path(ig_device_t
*device
)
1484 if (is_bootpar(device
->type
)) {
1486 struct mboot
*mboot
;
1488 mboot
= (struct mboot
*)device
->boot_sector
;
1489 if (find_x86_bootpar(mboot
, &part
, NULL
) != BC_SUCCESS
) {
1490 (void) fprintf(stderr
, BOOTPAR_NOTFOUND
,
1495 raw
= strdup(device
->path_p0
);
1497 perror(gettext("Memory allocation failed"));
1501 raw
[strlen(raw
) - 2] = '1' + part
;
1505 /* For disk, remember slice and return whole fdisk partition */
1506 raw
= strdup(device
->path
);
1508 perror(gettext("Memory allocation failed"));
1513 if (!is_efi(device
->type
) &&
1514 (raw
[len
- 2] != 's' || raw
[len
- 1] == '2')) {
1515 (void) fprintf(stderr
, NOT_ROOT_SLICE
);
1519 device
->slice
= atoi(&raw
[len
- 1]);
1521 if (!is_efi(device
->type
)) {
1530 get_raw_partition_fd(ig_device_t
*device
)
1532 struct stat stat
= {0};
1535 raw
= get_raw_partition_path(device
);
1540 device
->part_fd
= open(raw
, O_RDONLY
);
1542 device
->part_fd
= open(raw
, O_RDWR
);
1544 if (device
->part_fd
< 0 || fstat(device
->part_fd
, &stat
) != 0) {
1545 (void) fprintf(stderr
, OPEN_FAIL
, raw
);
1550 if (S_ISCHR(stat
.st_mode
) == 0) {
1551 (void) fprintf(stderr
, NOT_RAW_DEVICE
, raw
);
1552 (void) close(device
->part_fd
);
1553 device
->part_fd
= -1;
1559 return (BC_SUCCESS
);