10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55589Ssy25831  * Common Development and Distribution License (the "License").
65589Ssy25831  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*10021SSheshadri.Vasudevan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <libgen.h>
290Sstevel@tonic-gate #include <malloc.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <sys/mount.h>
370Sstevel@tonic-gate #include <sys/mnttab.h>
380Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
395589Ssy25831 #include <sys/dkio.h>
405589Ssy25831 #include <sys/vtoc.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <libintl.h>
430Sstevel@tonic-gate #include <locale.h>
440Sstevel@tonic-gate #include "message.h"
45322Sjongkis #include <errno.h>
46*10021SSheshadri.Vasudevan@Sun.COM #include <libfdisk.h>
478434SEnrico.Perla@Sun.COM #include <md5.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
500Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	SECTOR_SIZE	0x200
548434SEnrico.Perla@Sun.COM #define	HASH_SIZE	0x10
558434SEnrico.Perla@Sun.COM #define	VERSION_SIZE	0x50
560Sstevel@tonic-gate #define	STAGE2_MEMADDR	0x8000	/* loading addr of stage2 */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	STAGE1_BPB_OFFSET	0x3
590Sstevel@tonic-gate #define	STAGE1_BPB_SIZE		0x3B
600Sstevel@tonic-gate #define	STAGE1_BOOT_DRIVE	0x40
610Sstevel@tonic-gate #define	STAGE1_FORCE_LBA	0x41
620Sstevel@tonic-gate #define	STAGE1_STAGE2_ADDRESS	0x42
630Sstevel@tonic-gate #define	STAGE1_STAGE2_SECTOR	0x44
640Sstevel@tonic-gate #define	STAGE1_STAGE2_SEGMENT	0x48
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	STAGE2_BLOCKLIST	(SECTOR_SIZE - 0x8)
670Sstevel@tonic-gate #define	STAGE2_INSTALLPART	(SECTOR_SIZE + 0x8)
680Sstevel@tonic-gate #define	STAGE2_FORCE_LBA	(SECTOR_SIZE + 0x11)
690Sstevel@tonic-gate #define	STAGE2_VER_STRING	(SECTOR_SIZE + 0x12)
708434SEnrico.Perla@Sun.COM #define	STAGE2_SIGN_OFFSET	(SECTOR_SIZE + 0x60)
718434SEnrico.Perla@Sun.COM #define	STAGE2_PKG_VERSION	(SECTOR_SIZE + 0x70)
720Sstevel@tonic-gate #define	STAGE2_BLKOFF		50	/* offset from start of fdisk part */
730Sstevel@tonic-gate 
748434SEnrico.Perla@Sun.COM static char extended_sig[] = "\xCC\xCC\xCC\xCC\xAA\xAA\xAA\xAA\xBB\xBB\xBB\xBB"
758434SEnrico.Perla@Sun.COM "\xBB\xBB\xBB\xBB";
768434SEnrico.Perla@Sun.COM 
770Sstevel@tonic-gate static int nowrite = 0;
780Sstevel@tonic-gate static int write_mboot = 0;
790Sstevel@tonic-gate static int force_mboot = 0;
808434SEnrico.Perla@Sun.COM static int getinfo = 0;
818434SEnrico.Perla@Sun.COM static int do_version = 0;
820Sstevel@tonic-gate static int is_floppy = 0;
830Sstevel@tonic-gate static int is_bootpar = 0;
848434SEnrico.Perla@Sun.COM static int strip = 0;
850Sstevel@tonic-gate static int stage2_fd;
860Sstevel@tonic-gate static int partition, slice = 0xff;
87*10021SSheshadri.Vasudevan@Sun.COM static char *device_p0;
88*10021SSheshadri.Vasudevan@Sun.COM static uint32_t stage2_first_sector, stage2_second_sector;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE];
920Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE];
930Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE];
940Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE];
958434SEnrico.Perla@Sun.COM static char signature[HASH_SIZE];
968434SEnrico.Perla@Sun.COM static char verstring[VERSION_SIZE];
977563SPrasad.Singamsetty@Sun.COM static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)];
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static int open_device(char *);
1000Sstevel@tonic-gate static void read_bpb_sect(int);
1010Sstevel@tonic-gate static void read_boot_sect(char *);
1020Sstevel@tonic-gate static void write_boot_sect(char *);
1030Sstevel@tonic-gate static void read_stage1_stage2(char *, char *);
1040Sstevel@tonic-gate static void modify_and_write_stage1(int);
1050Sstevel@tonic-gate static void modify_and_write_stage2(int);
1067563SPrasad.Singamsetty@Sun.COM static unsigned int get_start_sector(int);
1070Sstevel@tonic-gate static void copy_stage2(int, char *);
1080Sstevel@tonic-gate static char *get_raw_partition(char *);
1090Sstevel@tonic-gate static void usage(char *);
1108434SEnrico.Perla@Sun.COM static void print_info();
1118434SEnrico.Perla@Sun.COM static int read_stage2_info(int);
1128434SEnrico.Perla@Sun.COM static void check_extended_support();
1130Sstevel@tonic-gate 
1147563SPrasad.Singamsetty@Sun.COM extern int read_stage2_blocklist(int, unsigned int *);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate int
1170Sstevel@tonic-gate main(int argc, char *argv[])
1180Sstevel@tonic-gate {
1198434SEnrico.Perla@Sun.COM 	int dev_fd, opt, params = 3;
1200Sstevel@tonic-gate 	char *stage1, *stage2, *device;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1230Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1240Sstevel@tonic-gate 
1258434SEnrico.Perla@Sun.COM 	while ((opt = getopt(argc, argv, "fmneis:")) != EOF) {
1260Sstevel@tonic-gate 		switch (opt) {
1270Sstevel@tonic-gate 		case 'm':
1280Sstevel@tonic-gate 			write_mboot = 1;
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 		case 'n':
1310Sstevel@tonic-gate 			nowrite = 1;
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 		case 'f':
1340Sstevel@tonic-gate 			force_mboot = 1;
1350Sstevel@tonic-gate 			break;
1368434SEnrico.Perla@Sun.COM 		case 'i':
1378434SEnrico.Perla@Sun.COM 			getinfo = 1;
1388434SEnrico.Perla@Sun.COM 			params = 1;
1398434SEnrico.Perla@Sun.COM 			break;
1408434SEnrico.Perla@Sun.COM 		case 'e':
1418434SEnrico.Perla@Sun.COM 			strip = 1;
1428434SEnrico.Perla@Sun.COM 			break;
1438434SEnrico.Perla@Sun.COM 		case 's':
1448434SEnrico.Perla@Sun.COM 			do_version = 1;
1458434SEnrico.Perla@Sun.COM 			(void) snprintf(verstring, sizeof (verstring), "%s",
1468434SEnrico.Perla@Sun.COM 			    optarg);
1478434SEnrico.Perla@Sun.COM 			break;
1480Sstevel@tonic-gate 		default:
1490Sstevel@tonic-gate 			/* fall through to process non-optional args */
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* check arguments */
1558434SEnrico.Perla@Sun.COM 	if (argc != optind + params) {
1560Sstevel@tonic-gate 		usage(argv[0]);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (nowrite) {
1600Sstevel@tonic-gate 		(void) fprintf(stdout, DRY_RUN);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1638434SEnrico.Perla@Sun.COM 	if (params == 1) {
1648434SEnrico.Perla@Sun.COM 		device = strdup(argv[optind]);
1658434SEnrico.Perla@Sun.COM 		if (!device) {
1668434SEnrico.Perla@Sun.COM 			usage(argv[0]);
1678434SEnrico.Perla@Sun.COM 		}
1688434SEnrico.Perla@Sun.COM 	} else if (params == 3) {
1698434SEnrico.Perla@Sun.COM 		stage1 = strdup(argv[optind]);
1708434SEnrico.Perla@Sun.COM 		stage2 = strdup(argv[optind + 1]);
1718434SEnrico.Perla@Sun.COM 		device = strdup(argv[optind + 2]);
1720Sstevel@tonic-gate 
1738434SEnrico.Perla@Sun.COM 		if (!stage1 || !stage2 || !device) {
1748434SEnrico.Perla@Sun.COM 			usage(argv[0]);
1758434SEnrico.Perla@Sun.COM 		}
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/* open and check device type */
1790Sstevel@tonic-gate 	dev_fd = open_device(device);
1800Sstevel@tonic-gate 
1818434SEnrico.Perla@Sun.COM 	if (getinfo) {
1828434SEnrico.Perla@Sun.COM 		if (read_stage2_info(dev_fd) != 0) {
1838434SEnrico.Perla@Sun.COM 			fprintf(stderr, "Unable to read extended information"
1848434SEnrico.Perla@Sun.COM 			    " from %s\n", device);
1858434SEnrico.Perla@Sun.COM 			exit(1);
1868434SEnrico.Perla@Sun.COM 		}
1878434SEnrico.Perla@Sun.COM 		print_info();
1888434SEnrico.Perla@Sun.COM 		(void) free(device);
1898434SEnrico.Perla@Sun.COM 		(void) close(dev_fd);
1908434SEnrico.Perla@Sun.COM 		return (0);
1918434SEnrico.Perla@Sun.COM 	}
1928434SEnrico.Perla@Sun.COM 
1930Sstevel@tonic-gate 	/* read in stage1 and stage2 into buffer */
1940Sstevel@tonic-gate 	read_stage1_stage2(stage1, stage2);
1950Sstevel@tonic-gate 
1968434SEnrico.Perla@Sun.COM 	/* check if stage2 supports extended versioning */
1978434SEnrico.Perla@Sun.COM 	if (do_version)
1988434SEnrico.Perla@Sun.COM 		check_extended_support(stage2);
1998434SEnrico.Perla@Sun.COM 
2000Sstevel@tonic-gate 	/* In the pcfs case, write a fresh stage2 */
2010Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
2020Sstevel@tonic-gate 		copy_stage2(dev_fd, device);
2030Sstevel@tonic-gate 		read_bpb_sect(dev_fd);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/* read in boot sector */
2070Sstevel@tonic-gate 	if (!is_floppy)
2080Sstevel@tonic-gate 		read_boot_sect(device);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* modify stage1 based on grub needs */
2110Sstevel@tonic-gate 	modify_and_write_stage1(dev_fd);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/* modify stage2 and write to media */
2140Sstevel@tonic-gate 	modify_and_write_stage2(dev_fd);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (!is_floppy && write_mboot)
2170Sstevel@tonic-gate 		write_boot_sect(device);
2188434SEnrico.Perla@Sun.COM 
2190Sstevel@tonic-gate 	(void) close(dev_fd);
2208434SEnrico.Perla@Sun.COM 	free(device);
2218434SEnrico.Perla@Sun.COM 	free(stage1);
2228434SEnrico.Perla@Sun.COM 	free(stage2);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	return (0);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2277563SPrasad.Singamsetty@Sun.COM static unsigned int
2285589Ssy25831 get_start_sector(int fd)
2290Sstevel@tonic-gate {
2307563SPrasad.Singamsetty@Sun.COM 	static unsigned int start_sect = 0;
231*10021SSheshadri.Vasudevan@Sun.COM 	uint32_t secnum, numsec;
232*10021SSheshadri.Vasudevan@Sun.COM 	int i, pno, rval, ext_sol_part_found = 0;
2330Sstevel@tonic-gate 	struct mboot *mboot;
2340Sstevel@tonic-gate 	struct ipart *part;
235*10021SSheshadri.Vasudevan@Sun.COM 	ext_part_t *epp;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (start_sect)
2380Sstevel@tonic-gate 		return (start_sect);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	mboot = (struct mboot *)boot_sect;
2410Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
2420Sstevel@tonic-gate 		part = (struct ipart *)mboot->parts + i;
2430Sstevel@tonic-gate 		if (is_bootpar) {
2440Sstevel@tonic-gate 			if (part->systid == 0xbe)
2450Sstevel@tonic-gate 				break;
2465589Ssy25831 		}
2475589Ssy25831 	}
2485589Ssy25831 
249*10021SSheshadri.Vasudevan@Sun.COM 	/* Read extended partition to find a solaris partition */
250*10021SSheshadri.Vasudevan@Sun.COM 	if ((rval = libfdisk_init(&epp, device_p0, NULL, FDISK_READ_DISK))
251*10021SSheshadri.Vasudevan@Sun.COM 	    != FDISK_SUCCESS) {
252*10021SSheshadri.Vasudevan@Sun.COM 		switch (rval) {
253*10021SSheshadri.Vasudevan@Sun.COM 			/*
254*10021SSheshadri.Vasudevan@Sun.COM 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
255*10021SSheshadri.Vasudevan@Sun.COM 			 * be considered as soft errors and hence
256*10021SSheshadri.Vasudevan@Sun.COM 			 * we do not exit
257*10021SSheshadri.Vasudevan@Sun.COM 			 */
258*10021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EBADLOGDRIVE:
259*10021SSheshadri.Vasudevan@Sun.COM 				break;
260*10021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLOGDRIVE:
261*10021SSheshadri.Vasudevan@Sun.COM 				break;
262*10021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOVGEOM:
263*10021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr, NO_VIRT_GEOM);
264*10021SSheshadri.Vasudevan@Sun.COM 				exit(1);
265*10021SSheshadri.Vasudevan@Sun.COM 				break;
266*10021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOPGEOM:
267*10021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr, NO_PHYS_GEOM);
268*10021SSheshadri.Vasudevan@Sun.COM 				exit(1);
269*10021SSheshadri.Vasudevan@Sun.COM 				break;
270*10021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLGEOM:
271*10021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr, NO_LABEL_GEOM);
272*10021SSheshadri.Vasudevan@Sun.COM 				exit(1);
273*10021SSheshadri.Vasudevan@Sun.COM 				break;
274*10021SSheshadri.Vasudevan@Sun.COM 			default:
275*10021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr, LIBFDISK_INIT_FAIL);
276*10021SSheshadri.Vasudevan@Sun.COM 				exit(1);
277*10021SSheshadri.Vasudevan@Sun.COM 				break;
278*10021SSheshadri.Vasudevan@Sun.COM 		}
279*10021SSheshadri.Vasudevan@Sun.COM 	}
280*10021SSheshadri.Vasudevan@Sun.COM 
281*10021SSheshadri.Vasudevan@Sun.COM 	rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
282*10021SSheshadri.Vasudevan@Sun.COM 	if (rval == FDISK_SUCCESS) {
283*10021SSheshadri.Vasudevan@Sun.COM 		ext_sol_part_found = 1;
284*10021SSheshadri.Vasudevan@Sun.COM 	}
285*10021SSheshadri.Vasudevan@Sun.COM 	libfdisk_fini(&epp);
286*10021SSheshadri.Vasudevan@Sun.COM 
2875589Ssy25831 	/*
2885589Ssy25831 	 * If there is no boot partition, find the solaris partition
2895589Ssy25831 	 */
2905589Ssy25831 
2915589Ssy25831 	if (i == FD_NUMPART) {
2925589Ssy25831 		struct part_info dkpi;
2937563SPrasad.Singamsetty@Sun.COM 		struct extpart_info edkpi;
2945589Ssy25831 
2955589Ssy25831 		/*
2965589Ssy25831 		 * Get the solaris partition information from the device
2975589Ssy25831 		 * and compare the offset of S2 with offset of solaris partition
2985589Ssy25831 		 * from fdisk partition table.
2995589Ssy25831 		 */
3007563SPrasad.Singamsetty@Sun.COM 		if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
3017563SPrasad.Singamsetty@Sun.COM 			if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) {
3027563SPrasad.Singamsetty@Sun.COM 				(void) fprintf(stderr, PART_FAIL);
3037563SPrasad.Singamsetty@Sun.COM 				exit(-1);
3047563SPrasad.Singamsetty@Sun.COM 			} else {
3057563SPrasad.Singamsetty@Sun.COM 				edkpi.p_start = dkpi.p_start;
3067563SPrasad.Singamsetty@Sun.COM 			}
3075589Ssy25831 		}
3085589Ssy25831 
3095589Ssy25831 		for (i = 0; i < FD_NUMPART; i++) {
3105589Ssy25831 			part = (struct ipart *)mboot->parts + i;
3115589Ssy25831 
3125589Ssy25831 			if (part->relsect == 0) {
3135589Ssy25831 				(void) fprintf(stderr, BAD_PART, i);
3145589Ssy25831 				exit(-1);
3155589Ssy25831 			}
316*10021SSheshadri.Vasudevan@Sun.COM 
3177563SPrasad.Singamsetty@Sun.COM 			if (edkpi.p_start >= part->relsect &&
3187563SPrasad.Singamsetty@Sun.COM 			    edkpi.p_start < (part->relsect + part->numsect)) {
3195589Ssy25831 				/* Found the partition */
3200Sstevel@tonic-gate 				break;
3215589Ssy25831 			}
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
325*10021SSheshadri.Vasudevan@Sun.COM 	if ((i == FD_NUMPART) && (!ext_sol_part_found)) {
3260Sstevel@tonic-gate 		(void) fprintf(stderr, BOOTPAR);
3270Sstevel@tonic-gate 		exit(-1);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* get confirmation for -m */
3310Sstevel@tonic-gate 	if (write_mboot && !force_mboot) {
3320Sstevel@tonic-gate 		(void) fprintf(stdout, MBOOT_PROMPT);
3330Sstevel@tonic-gate 		if (getchar() != 'y') {
3340Sstevel@tonic-gate 			write_mboot = 0;
3350Sstevel@tonic-gate 			(void) fprintf(stdout, MBOOT_NOT_UPDATED);
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
339*10021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_is_dos_extended(part->systid)) {
340*10021SSheshadri.Vasudevan@Sun.COM 		start_sect = secnum;
341*10021SSheshadri.Vasudevan@Sun.COM 		partition = pno;
342*10021SSheshadri.Vasudevan@Sun.COM 	} else {
343*10021SSheshadri.Vasudevan@Sun.COM 		start_sect = part->relsect;
344*10021SSheshadri.Vasudevan@Sun.COM 		partition = i;
345*10021SSheshadri.Vasudevan@Sun.COM 	}
346*10021SSheshadri.Vasudevan@Sun.COM 
3470Sstevel@tonic-gate 	if (part->bootid != 128 && write_mboot == 0) {
3480Sstevel@tonic-gate 		(void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	return (start_sect);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static void
3550Sstevel@tonic-gate usage(char *progname)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	(void) fprintf(stderr, USAGE, basename(progname));
3580Sstevel@tonic-gate 	exit(-1);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate static int
3620Sstevel@tonic-gate open_device(char *device)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	int dev_fd;
3650Sstevel@tonic-gate 	struct stat stat;
3660Sstevel@tonic-gate 	char *raw_part;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) &&
3690Sstevel@tonic-gate 	    strncmp(device, "/dev/dsk", strlen("/dev/dsk"));
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/* handle boot partition specification */
3720Sstevel@tonic-gate 	if (!is_floppy && strstr(device, "p0:boot")) {
3730Sstevel@tonic-gate 		is_bootpar = 1;
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	raw_part = get_raw_partition(device);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (nowrite)
3790Sstevel@tonic-gate 		dev_fd = open(raw_part, O_RDONLY);
3800Sstevel@tonic-gate 	else
3810Sstevel@tonic-gate 		dev_fd = open(raw_part, O_RDWR);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) {
3840Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL, raw_part);
3850Sstevel@tonic-gate 		exit(-1);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	if (S_ISCHR(stat.st_mode) == 0) {
3880Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_RAW_DEVICE, raw_part);
3890Sstevel@tonic-gate 		exit(-1);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (dev_fd);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate static void
3960Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	int fd;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	/* read the stage1 file from filesystem */
4010Sstevel@tonic-gate 	fd = open(stage1, O_RDONLY);
4020Sstevel@tonic-gate 	if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) {
4030Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_STAGE1, stage1);
4040Sstevel@tonic-gate 		exit(-1);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	(void) close(fd);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/* read first two blocks of stage 2 from filesystem */
4090Sstevel@tonic-gate 	stage2_fd = open(stage2, O_RDONLY);
4100Sstevel@tonic-gate 	if (stage2_fd == -1 ||
4110Sstevel@tonic-gate 	    read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE)
4120Sstevel@tonic-gate 	    != 2 * SECTOR_SIZE) {
4130Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_STAGE2, stage2);
4140Sstevel@tonic-gate 		exit(-1);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	/* leave the stage2 file open for later */
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate static void
4200Sstevel@tonic-gate read_bpb_sect(int dev_fd)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) {
4230Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_BPB);
4240Sstevel@tonic-gate 		exit(-1);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate static void
4290Sstevel@tonic-gate read_boot_sect(char *device)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	static int read_mbr = 0;
4320Sstevel@tonic-gate 	int i, fd;
4330Sstevel@tonic-gate 	char save[2];
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (read_mbr)
4360Sstevel@tonic-gate 		return;
4370Sstevel@tonic-gate 	read_mbr = 1;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/* get the whole disk (p0) */
4400Sstevel@tonic-gate 	i = strlen(device);
4410Sstevel@tonic-gate 	save[0] = device[i - 2];
4420Sstevel@tonic-gate 	save[1] = device[i - 1];
4430Sstevel@tonic-gate 	device[i - 2] = 'p';
4440Sstevel@tonic-gate 	device[i - 1] = '0';
4450Sstevel@tonic-gate 
446*10021SSheshadri.Vasudevan@Sun.COM 	device_p0 = strdup(device);
4470Sstevel@tonic-gate 	fd = open(device, O_RDONLY);
4480Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
4490Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_MBR, device);
4500Sstevel@tonic-gate 		if (fd == -1)
4510Sstevel@tonic-gate 			perror("open");
4520Sstevel@tonic-gate 		else
4530Sstevel@tonic-gate 			perror("read");
4540Sstevel@tonic-gate 		exit(-1);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	(void) close(fd);
4570Sstevel@tonic-gate 	device[i - 2] = save[0];
4580Sstevel@tonic-gate 	device[i - 1] = save[1];
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate static void
4620Sstevel@tonic-gate write_boot_sect(char *device)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	int fd, len;
4650Sstevel@tonic-gate 	char *raw, *end;
4660Sstevel@tonic-gate 	struct stat stat;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	/* make a copy and chop off ":boot" */
4690Sstevel@tonic-gate 	raw = strdup(device);
4700Sstevel@tonic-gate 	end = strstr(raw, "p0:boot");
4710Sstevel@tonic-gate 	if (end)
4720Sstevel@tonic-gate 		end[2] = 0;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/* open p0 (whole disk) */
4750Sstevel@tonic-gate 	len = strlen(raw);
4760Sstevel@tonic-gate 	raw[len - 2] = 'p';
4770Sstevel@tonic-gate 	raw[len - 1] = '0';
4780Sstevel@tonic-gate 	fd = open(raw, O_WRONLY);
4790Sstevel@tonic-gate 	if (fd == -1 || fstat(fd, &stat) != 0) {
4800Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL, raw);
4810Sstevel@tonic-gate 		exit(-1);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	if (!nowrite &&
4840Sstevel@tonic-gate 	    pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
4850Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_BOOTSEC);
4860Sstevel@tonic-gate 		exit(-1);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	(void) fprintf(stdout, WRITE_MBOOT);
4890Sstevel@tonic-gate 	(void) close(fd);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate static void
4930Sstevel@tonic-gate modify_and_write_stage1(int dev_fd)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	if (is_floppy) {
4960Sstevel@tonic-gate 		stage2_first_sector = blocklist[0];
4970Sstevel@tonic-gate 		/* copy bios parameter block (for fat fs) */
4980Sstevel@tonic-gate 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
4990Sstevel@tonic-gate 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
5000Sstevel@tonic-gate 	} else if (is_bootpar) {
5015589Ssy25831 		stage2_first_sector = get_start_sector(dev_fd) + blocklist[0];
5020Sstevel@tonic-gate 		/* copy bios parameter block (for fat fs) and MBR */
5030Sstevel@tonic-gate 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
5040Sstevel@tonic-gate 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
5050Sstevel@tonic-gate 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
5060Sstevel@tonic-gate 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
5070Sstevel@tonic-gate 	} else {
5085589Ssy25831 		stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF;
5090Sstevel@tonic-gate 		/* copy MBR to stage1 in case of overwriting MBR sector */
5100Sstevel@tonic-gate 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
5110Sstevel@tonic-gate 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/* modify default stage1 file generated by GRUB */
5150Sstevel@tonic-gate 	*((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR))
5165589Ssy25831 	    = stage2_first_sector;
5170Sstevel@tonic-gate 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS))
5185589Ssy25831 	    = STAGE2_MEMADDR;
5190Sstevel@tonic-gate 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT))
5205589Ssy25831 	    = STAGE2_MEMADDR >> 4;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * XXX the default grub distribution also:
5240Sstevel@tonic-gate 	 * - Copy the possible MBR/extended part table
5250Sstevel@tonic-gate 	 * - Set the boot drive of stage1
5260Sstevel@tonic-gate 	 */
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/* write stage1/pboot to 1st sector */
5290Sstevel@tonic-gate 	if (!nowrite &&
5300Sstevel@tonic-gate 	    pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
5310Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_PBOOT);
5320Sstevel@tonic-gate 		exit(-1);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (is_floppy) {
5360Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY);
5370Sstevel@tonic-gate 	} else {
5380Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_PBOOT,
5395589Ssy25831 		    partition, get_start_sector(dev_fd));
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5438434SEnrico.Perla@Sun.COM static void check_extended_support(char *stage2)
5448434SEnrico.Perla@Sun.COM {
5458434SEnrico.Perla@Sun.COM 	char	*cmp = stage2_buffer + STAGE2_SIGN_OFFSET - 1;
5468434SEnrico.Perla@Sun.COM 
5478434SEnrico.Perla@Sun.COM 	if ((*cmp++ != '\xEE') && memcmp(cmp, extended_sig, HASH_SIZE) != 0) {
5488434SEnrico.Perla@Sun.COM 		fprintf(stderr, "%s does not support extended versioning\n",
5498434SEnrico.Perla@Sun.COM 		    stage2);
5508434SEnrico.Perla@Sun.COM 		do_version = 0;
5518434SEnrico.Perla@Sun.COM 	}
5528434SEnrico.Perla@Sun.COM }
5538434SEnrico.Perla@Sun.COM 
5548434SEnrico.Perla@Sun.COM 
5558434SEnrico.Perla@Sun.COM static void print_info()
5568434SEnrico.Perla@Sun.COM {
5578434SEnrico.Perla@Sun.COM 	int	i;
5588434SEnrico.Perla@Sun.COM 
5598434SEnrico.Perla@Sun.COM 	if (strip) {
5608434SEnrico.Perla@Sun.COM 		fprintf(stdout, "%s\n", verstring);
5618434SEnrico.Perla@Sun.COM 	} else {
5628434SEnrico.Perla@Sun.COM 		fprintf(stdout, "Grub extended version information : %s\n",
5638434SEnrico.Perla@Sun.COM 		    verstring);
5648434SEnrico.Perla@Sun.COM 		fprintf(stdout, "Grub stage2 (MD5) signature : ");
5658434SEnrico.Perla@Sun.COM 	}
5668434SEnrico.Perla@Sun.COM 
5678434SEnrico.Perla@Sun.COM 	for (i = 0; i < HASH_SIZE; i++)
5688434SEnrico.Perla@Sun.COM 		fprintf(stdout, "%02x", (unsigned char)signature[i]);
5698434SEnrico.Perla@Sun.COM 
5708434SEnrico.Perla@Sun.COM 	fprintf(stdout, "\n");
5718434SEnrico.Perla@Sun.COM }
5728434SEnrico.Perla@Sun.COM 
5738434SEnrico.Perla@Sun.COM static int
5748434SEnrico.Perla@Sun.COM read_stage2_info(int dev_fd)
5758434SEnrico.Perla@Sun.COM {
5768434SEnrico.Perla@Sun.COM 	int 	ret;
5778434SEnrico.Perla@Sun.COM 	int	first_offset, second_offset;
5788434SEnrico.Perla@Sun.COM 	char	*sign;
5798434SEnrico.Perla@Sun.COM 
5808434SEnrico.Perla@Sun.COM 	if (is_floppy || is_bootpar) {
5818434SEnrico.Perla@Sun.COM 
5828434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage1_buffer, SECTOR_SIZE, 0);
5838434SEnrico.Perla@Sun.COM 		if (ret != SECTOR_SIZE) {
5848434SEnrico.Perla@Sun.COM 			perror("Error reading stage1 sector");
5858434SEnrico.Perla@Sun.COM 			return (1);
5868434SEnrico.Perla@Sun.COM 		}
5878434SEnrico.Perla@Sun.COM 
5888434SEnrico.Perla@Sun.COM 		first_offset = *((ulong_t *)(stage1_buffer +
5898434SEnrico.Perla@Sun.COM 		    STAGE1_STAGE2_SECTOR));
5908434SEnrico.Perla@Sun.COM 
5918434SEnrico.Perla@Sun.COM 		/* Start reading in the first sector of stage 2 */
5928434SEnrico.Perla@Sun.COM 
5938434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage2_buffer, SECTOR_SIZE, first_offset *
5948434SEnrico.Perla@Sun.COM 		    SECTOR_SIZE);
5958434SEnrico.Perla@Sun.COM 		if (ret != SECTOR_SIZE) {
5968434SEnrico.Perla@Sun.COM 			perror("Error reading stage2 first sector");
5978434SEnrico.Perla@Sun.COM 			return (1);
5988434SEnrico.Perla@Sun.COM 		}
5998434SEnrico.Perla@Sun.COM 
6008434SEnrico.Perla@Sun.COM 		/* From the block list section grab stage2 second sector */
6018434SEnrico.Perla@Sun.COM 
6028434SEnrico.Perla@Sun.COM 		second_offset = *((ulong_t *)(stage2_buffer +
6038434SEnrico.Perla@Sun.COM 		    STAGE2_BLOCKLIST));
6048434SEnrico.Perla@Sun.COM 
6058434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
6068434SEnrico.Perla@Sun.COM 		    second_offset * SECTOR_SIZE);
6078434SEnrico.Perla@Sun.COM 		if (ret != SECTOR_SIZE) {
6088434SEnrico.Perla@Sun.COM 			perror("Error reading stage2 second sector");
6098434SEnrico.Perla@Sun.COM 			return (1);
6108434SEnrico.Perla@Sun.COM 		}
6118434SEnrico.Perla@Sun.COM 	} else {
6128434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
6138434SEnrico.Perla@Sun.COM 		    STAGE2_BLKOFF * SECTOR_SIZE);
6148434SEnrico.Perla@Sun.COM 		if (ret != 2 * SECTOR_SIZE) {
6158434SEnrico.Perla@Sun.COM 			perror("Error reading stage2 sectors");
6168434SEnrico.Perla@Sun.COM 			return (1);
6178434SEnrico.Perla@Sun.COM 		}
6188434SEnrico.Perla@Sun.COM 	}
6198434SEnrico.Perla@Sun.COM 
6208434SEnrico.Perla@Sun.COM 	sign = stage2_buffer + STAGE2_SIGN_OFFSET - 1;
6218434SEnrico.Perla@Sun.COM 	if (*sign++ != '\xEE')
6228434SEnrico.Perla@Sun.COM 		return (1);
6238434SEnrico.Perla@Sun.COM 	(void) memcpy(signature, sign, HASH_SIZE);
6248434SEnrico.Perla@Sun.COM 	sign = stage2_buffer + STAGE2_PKG_VERSION;
6258434SEnrico.Perla@Sun.COM 	(void) strncpy(verstring, sign, VERSION_SIZE);
6268434SEnrico.Perla@Sun.COM 	return (0);
6278434SEnrico.Perla@Sun.COM }
6288434SEnrico.Perla@Sun.COM 
6298434SEnrico.Perla@Sun.COM 
6308434SEnrico.Perla@Sun.COM static int
6318434SEnrico.Perla@Sun.COM compute_and_write_md5hash(char *dest)
6328434SEnrico.Perla@Sun.COM {
6338434SEnrico.Perla@Sun.COM 	struct stat	sb;
6348434SEnrico.Perla@Sun.COM 	char		*buffer;
6358434SEnrico.Perla@Sun.COM 
6368434SEnrico.Perla@Sun.COM 	if (fstat(stage2_fd, &sb) == -1)
6378434SEnrico.Perla@Sun.COM 		return (-1);
6388434SEnrico.Perla@Sun.COM 
6398434SEnrico.Perla@Sun.COM 	buffer = malloc(sb.st_size);
6408434SEnrico.Perla@Sun.COM 	if (buffer == NULL)
6418434SEnrico.Perla@Sun.COM 		return (-1);
6428434SEnrico.Perla@Sun.COM 
6438434SEnrico.Perla@Sun.COM 	if (lseek(stage2_fd, 0, SEEK_SET) == -1)
6448434SEnrico.Perla@Sun.COM 		return (-1);
6458434SEnrico.Perla@Sun.COM 	if (read(stage2_fd, buffer, sb.st_size) < 0)
6468434SEnrico.Perla@Sun.COM 		return (-1);
6478434SEnrico.Perla@Sun.COM 
6488434SEnrico.Perla@Sun.COM 	md5_calc(dest, buffer, sb.st_size);
6498434SEnrico.Perla@Sun.COM 	free(buffer);
6508434SEnrico.Perla@Sun.COM 	return (0);
6518434SEnrico.Perla@Sun.COM }
6528434SEnrico.Perla@Sun.COM 
6538434SEnrico.Perla@Sun.COM 
6540Sstevel@tonic-gate #define	START_BLOCK(pos)	(*(ulong_t *)(pos))
6550Sstevel@tonic-gate #define	NUM_BLOCK(pos)		(*(ushort_t *)((pos) + 4))
6560Sstevel@tonic-gate #define	START_SEG(pos)		(*(ushort_t *)((pos) + 6))
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate static void
6590Sstevel@tonic-gate modify_and_write_stage2(int dev_fd)
6600Sstevel@tonic-gate {
6618434SEnrico.Perla@Sun.COM 	int 	nrecord;
6628434SEnrico.Perla@Sun.COM 	off_t 	offset;
6638434SEnrico.Perla@Sun.COM 	char	*dest;
6648434SEnrico.Perla@Sun.COM 
6658434SEnrico.Perla@Sun.COM 	if (do_version) {
6668434SEnrico.Perla@Sun.COM 		dest = stage2_buffer + STAGE2_SIGN_OFFSET;
6678434SEnrico.Perla@Sun.COM 		if (compute_and_write_md5hash(dest) < 0)
6688434SEnrico.Perla@Sun.COM 			perror("MD5 operation");
6698434SEnrico.Perla@Sun.COM 		dest = stage2_buffer + STAGE2_PKG_VERSION;
6708434SEnrico.Perla@Sun.COM 		(void) strncpy(dest, verstring, VERSION_SIZE);
6718434SEnrico.Perla@Sun.COM 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
6740Sstevel@tonic-gate 		int i = 0;
675*10021SSheshadri.Vasudevan@Sun.COM 		uint32_t partition_offset;
676*10021SSheshadri.Vasudevan@Sun.COM 		uint32_t install_addr = 0x8200;
6770Sstevel@tonic-gate 		uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		stage2_first_sector = blocklist[0];
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		/* figure out the second sector */
6820Sstevel@tonic-gate 		if (blocklist[1] > 1) {
6830Sstevel@tonic-gate 			blocklist[0]++;
6840Sstevel@tonic-gate 			blocklist[1]--;
6850Sstevel@tonic-gate 		} else {
6860Sstevel@tonic-gate 			i += 2;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		stage2_second_sector = blocklist[i];
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		if (is_floppy)
6910Sstevel@tonic-gate 			partition_offset = 0;
6920Sstevel@tonic-gate 		else	/* solaris boot partition */
6935589Ssy25831 			partition_offset = get_start_sector(dev_fd);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 		/* install the blocklist at the end of stage2_buffer */
6960Sstevel@tonic-gate 		while (blocklist[i]) {
6970Sstevel@tonic-gate 			if (START_BLOCK(pos - 8) != 0 &&
6980Sstevel@tonic-gate 			    START_BLOCK(pos - 8) != blocklist[i + 2]) {
6990Sstevel@tonic-gate 				(void) fprintf(stderr, PCFS_FRAGMENTED);
7000Sstevel@tonic-gate 				exit(-1);
7010Sstevel@tonic-gate 			}
7020Sstevel@tonic-gate 			START_BLOCK(pos) = blocklist[i] + partition_offset;
7030Sstevel@tonic-gate 			START_SEG(pos) = (ushort_t)(install_addr >> 4);
7040Sstevel@tonic-gate 			NUM_BLOCK(pos) = blocklist[i + 1];
7050Sstevel@tonic-gate 			install_addr += blocklist[i + 1] * SECTOR_SIZE;
7060Sstevel@tonic-gate 			pos -= 8;
7070Sstevel@tonic-gate 			i += 2;
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	} else {
7110Sstevel@tonic-gate 		/*
7120Sstevel@tonic-gate 		 * In a solaris partition, stage2 is written to contiguous
7130Sstevel@tonic-gate 		 * blocks. So we update the starting block only.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		*((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) =
7160Sstevel@tonic-gate 		    stage2_first_sector + 1;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (is_floppy) {
7200Sstevel@tonic-gate 		/* modify the config file to add (fd0) */
7210Sstevel@tonic-gate 		char *config_file = stage2_buffer + STAGE2_VER_STRING;
7220Sstevel@tonic-gate 		while (*config_file++)
7230Sstevel@tonic-gate 			;
7240Sstevel@tonic-gate 		strcpy(config_file, "(fd0)/boot/grub/menu.lst");
7250Sstevel@tonic-gate 	} else {
7260Sstevel@tonic-gate 		/* force lba and set disk partition */
7270Sstevel@tonic-gate 		*((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1;
7280Sstevel@tonic-gate 		*((long *)(stage2_buffer + STAGE2_INSTALLPART))
7290Sstevel@tonic-gate 		    = (partition << 16) | (slice << 8) | 0xff;
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/* modification done, now do the writing */
7330Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
7340Sstevel@tonic-gate 		/* we rewrite block 0 and 1 and that's it */
7350Sstevel@tonic-gate 		if (!nowrite &&
7360Sstevel@tonic-gate 		    (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
7370Sstevel@tonic-gate 		    stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE ||
7380Sstevel@tonic-gate 		    pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
7390Sstevel@tonic-gate 		    stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) {
7400Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2);
7410Sstevel@tonic-gate 			exit(-1);
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_STAGE2_PCFS);
7440Sstevel@tonic-gate 		return;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/* for disk, write stage2 starting at STAGE2_BLKOFF sector */
7480Sstevel@tonic-gate 	offset = STAGE2_BLKOFF;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/* write the modified first two sectors */
7510Sstevel@tonic-gate 	if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
7520Sstevel@tonic-gate 	    offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) {
7530Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_STAGE2);
7540Sstevel@tonic-gate 		exit(-1);
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	/* write the remaining sectors */
7580Sstevel@tonic-gate 	nrecord = 2;
7590Sstevel@tonic-gate 	offset += 2;
7600Sstevel@tonic-gate 	for (;;) {
7610Sstevel@tonic-gate 		int nread, nwrite;
7620Sstevel@tonic-gate 		nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE,
7630Sstevel@tonic-gate 		    nrecord * SECTOR_SIZE);
7640Sstevel@tonic-gate 		if (nread > 0 && !nowrite)
7650Sstevel@tonic-gate 			nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
7660Sstevel@tonic-gate 			    offset * SECTOR_SIZE);
7670Sstevel@tonic-gate 		else
7680Sstevel@tonic-gate 			nwrite = SECTOR_SIZE;
7690Sstevel@tonic-gate 		if (nread < 0 || nwrite != SECTOR_SIZE) {
7700Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
7710Sstevel@tonic-gate 			    nread, nwrite);
7720Sstevel@tonic-gate 			break;
7730Sstevel@tonic-gate 		}
774322Sjongkis 		if (nread > 0) {
775322Sjongkis 			nrecord ++;
776322Sjongkis 			offset ++;
777322Sjongkis 		}
7780Sstevel@tonic-gate 		if (nread < SECTOR_SIZE)
7790Sstevel@tonic-gate 			break;	/* end of file */
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	(void) fprintf(stdout, WRITE_STAGE2_DISK,
7820Sstevel@tonic-gate 	    partition, nrecord, STAGE2_BLKOFF, stage2_first_sector);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate static char *
7860Sstevel@tonic-gate get_raw_partition(char *device)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	int len;
7890Sstevel@tonic-gate 	struct mboot *mboot;
7900Sstevel@tonic-gate 	static char *raw = NULL;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (raw)
7930Sstevel@tonic-gate 		return (raw);
7940Sstevel@tonic-gate 	raw = strdup(device);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if (is_floppy)
7970Sstevel@tonic-gate 		return (raw);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	if (is_bootpar) {
8000Sstevel@tonic-gate 		int i;
8010Sstevel@tonic-gate 		char *end = strstr(raw, "p0:boot");
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		end[2] = 0;		/* chop off :boot */
8040Sstevel@tonic-gate 		read_boot_sect(raw);
8050Sstevel@tonic-gate 		mboot = (struct mboot *)boot_sect;
8060Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
8070Sstevel@tonic-gate 			struct ipart *part = (struct ipart *)mboot->parts + i;
8080Sstevel@tonic-gate 			if (part->systid == 0xbe)	/* solaris boot part */
8090Sstevel@tonic-gate 				break;
8100Sstevel@tonic-gate 		}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 		if (i == FD_NUMPART) {
8130Sstevel@tonic-gate 			(void) fprintf(stderr, BOOTPAR_NOTFOUND, device);
8140Sstevel@tonic-gate 			exit(-1);
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		end[1] = '1' + i;	/* set partition name */
8170Sstevel@tonic-gate 		return (raw);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/* For disk, remember slice and return whole fdisk partition  */
8210Sstevel@tonic-gate 	len = strlen(raw);
8220Sstevel@tonic-gate 	if (raw[len - 2] != 's' || raw[len - 1] == '2') {
8230Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_ROOT_SLICE);
8240Sstevel@tonic-gate 		exit(-1);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	slice = atoi(&raw[len - 1]);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	raw[len - 2] = 's';
8290Sstevel@tonic-gate 	raw[len - 1] = '2';
8300Sstevel@tonic-gate 	return (raw);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate #define	TMP_MNTPT	"/tmp/installgrub_pcfs"
8340Sstevel@tonic-gate static void
8350Sstevel@tonic-gate copy_stage2(int dev_fd, char *device)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	FILE *mntfp;
8380Sstevel@tonic-gate 	int i, pcfs_fp;
8390Sstevel@tonic-gate 	char buf[SECTOR_SIZE];
8400Sstevel@tonic-gate 	char *cp;
8410Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/* convert raw to block device name by removing the first 'r' */
8440Sstevel@tonic-gate 	(void) strncpy(buf, device, sizeof (buf));
8450Sstevel@tonic-gate 	buf[sizeof (buf) - 1] = 0;
8460Sstevel@tonic-gate 	cp = strchr(buf, 'r');
8470Sstevel@tonic-gate 	if (cp == NULL) {
8480Sstevel@tonic-gate 		(void) fprintf(stderr, CONVERT_FAIL, device);
8490Sstevel@tonic-gate 		exit(-1);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 	do {
8520Sstevel@tonic-gate 		*cp = *(cp + 1);
8530Sstevel@tonic-gate 	} while (*(++cp));
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/* get the mount point, if any */
8560Sstevel@tonic-gate 	mntfp = fopen("/etc/mnttab", "r");
8570Sstevel@tonic-gate 	if (mntfp == NULL) {
8580Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab");
8590Sstevel@tonic-gate 		exit(-1);
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	mpref.mnt_special = buf;
8630Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
8640Sstevel@tonic-gate 		char cmd[128];
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		/* not mounted, try remount */
8670Sstevel@tonic-gate 		(void) mkdir(TMP_MNTPT, S_IRWXU);
8680Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s",
8690Sstevel@tonic-gate 		    buf, TMP_MNTPT);
8700Sstevel@tonic-gate 		(void) system(cmd);
8710Sstevel@tonic-gate 		rewind(mntfp);
8720Sstevel@tonic-gate 		bzero(&mp, sizeof (mp));
8730Sstevel@tonic-gate 		if (getmntany(mntfp, &mp, &mpref) != 0) {
8740Sstevel@tonic-gate 			(void) fprintf(stderr, MOUNT_FAIL, buf);
8750Sstevel@tonic-gate 			exit(-1);
8760Sstevel@tonic-gate 		}
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
8800Sstevel@tonic-gate 	    "%s/boot", mp.mnt_mountp);
8810Sstevel@tonic-gate 	(void) mkdir(buf, S_IRWXU);
8820Sstevel@tonic-gate 	(void) strcat(buf, "/grub");
8830Sstevel@tonic-gate 	(void) mkdir(buf, S_IRWXU);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	(void) strcat(buf, "/stage2");
8860Sstevel@tonic-gate 	pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU);
8870Sstevel@tonic-gate 	if (pcfs_fp == -1) {
8880Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL_FILE, buf);
8890Sstevel@tonic-gate 		perror("open:");
8900Sstevel@tonic-gate 		(void) umount(TMP_MNTPT);
8910Sstevel@tonic-gate 		exit(-1);
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/* write stage2 to pcfs */
8950Sstevel@tonic-gate 	for (i = 0; ; i++) {
8960Sstevel@tonic-gate 		int nread, nwrite;
8970Sstevel@tonic-gate 		nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE);
8980Sstevel@tonic-gate 		if (nowrite)
8990Sstevel@tonic-gate 			nwrite = nread;
9000Sstevel@tonic-gate 		else
9010Sstevel@tonic-gate 			nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE);
9020Sstevel@tonic-gate 		if (nread < 0 || nwrite != nread) {
9030Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
9040Sstevel@tonic-gate 			    nread, nwrite);
9050Sstevel@tonic-gate 			break;
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 		if (nread < SECTOR_SIZE)
9080Sstevel@tonic-gate 			break;	/* end of file */
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 	(void) close(pcfs_fp);
9110Sstevel@tonic-gate 	(void) umount(TMP_MNTPT);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	/*
9140Sstevel@tonic-gate 	 * Now, get the blocklist from the device.
9150Sstevel@tonic-gate 	 */
9160Sstevel@tonic-gate 	bzero(blocklist, sizeof (blocklist));
9170Sstevel@tonic-gate 	if (read_stage2_blocklist(dev_fd, blocklist) != 0)
9180Sstevel@tonic-gate 		exit(-1);
9190Sstevel@tonic-gate }
920