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 /*
225784Ssetje  * Copyright 2008 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*8434SEnrico.Perla@Sun.COM #include <md5.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
490Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	SECTOR_SIZE	0x200
53*8434SEnrico.Perla@Sun.COM #define	HASH_SIZE	0x10
54*8434SEnrico.Perla@Sun.COM #define	VERSION_SIZE	0x50
550Sstevel@tonic-gate #define	STAGE2_MEMADDR	0x8000	/* loading addr of stage2 */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #define	STAGE1_BPB_OFFSET	0x3
580Sstevel@tonic-gate #define	STAGE1_BPB_SIZE		0x3B
590Sstevel@tonic-gate #define	STAGE1_BOOT_DRIVE	0x40
600Sstevel@tonic-gate #define	STAGE1_FORCE_LBA	0x41
610Sstevel@tonic-gate #define	STAGE1_STAGE2_ADDRESS	0x42
620Sstevel@tonic-gate #define	STAGE1_STAGE2_SECTOR	0x44
630Sstevel@tonic-gate #define	STAGE1_STAGE2_SEGMENT	0x48
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	STAGE2_BLOCKLIST	(SECTOR_SIZE - 0x8)
660Sstevel@tonic-gate #define	STAGE2_INSTALLPART	(SECTOR_SIZE + 0x8)
670Sstevel@tonic-gate #define	STAGE2_FORCE_LBA	(SECTOR_SIZE + 0x11)
680Sstevel@tonic-gate #define	STAGE2_VER_STRING	(SECTOR_SIZE + 0x12)
69*8434SEnrico.Perla@Sun.COM #define	STAGE2_SIGN_OFFSET	(SECTOR_SIZE + 0x60)
70*8434SEnrico.Perla@Sun.COM #define	STAGE2_PKG_VERSION	(SECTOR_SIZE + 0x70)
710Sstevel@tonic-gate #define	STAGE2_BLKOFF		50	/* offset from start of fdisk part */
720Sstevel@tonic-gate 
73*8434SEnrico.Perla@Sun.COM static char extended_sig[] = "\xCC\xCC\xCC\xCC\xAA\xAA\xAA\xAA\xBB\xBB\xBB\xBB"
74*8434SEnrico.Perla@Sun.COM "\xBB\xBB\xBB\xBB";
75*8434SEnrico.Perla@Sun.COM 
760Sstevel@tonic-gate static int nowrite = 0;
770Sstevel@tonic-gate static int write_mboot = 0;
780Sstevel@tonic-gate static int force_mboot = 0;
79*8434SEnrico.Perla@Sun.COM static int getinfo = 0;
80*8434SEnrico.Perla@Sun.COM static int do_version = 0;
810Sstevel@tonic-gate static int is_floppy = 0;
820Sstevel@tonic-gate static int is_bootpar = 0;
83*8434SEnrico.Perla@Sun.COM static int strip = 0;
840Sstevel@tonic-gate static int stage2_fd;
850Sstevel@tonic-gate static int partition, slice = 0xff;
868333SSuhasini.Peddada@Sun.COM static unsigned int stage2_first_sector, stage2_second_sector;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE];
900Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE];
910Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE];
920Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE];
93*8434SEnrico.Perla@Sun.COM static char signature[HASH_SIZE];
94*8434SEnrico.Perla@Sun.COM static char verstring[VERSION_SIZE];
957563SPrasad.Singamsetty@Sun.COM static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)];
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static int open_device(char *);
980Sstevel@tonic-gate static void read_bpb_sect(int);
990Sstevel@tonic-gate static void read_boot_sect(char *);
1000Sstevel@tonic-gate static void write_boot_sect(char *);
1010Sstevel@tonic-gate static void read_stage1_stage2(char *, char *);
1020Sstevel@tonic-gate static void modify_and_write_stage1(int);
1030Sstevel@tonic-gate static void modify_and_write_stage2(int);
1047563SPrasad.Singamsetty@Sun.COM static unsigned int get_start_sector(int);
1050Sstevel@tonic-gate static void copy_stage2(int, char *);
1060Sstevel@tonic-gate static char *get_raw_partition(char *);
1070Sstevel@tonic-gate static void usage(char *);
108*8434SEnrico.Perla@Sun.COM static void print_info();
109*8434SEnrico.Perla@Sun.COM static int read_stage2_info(int);
110*8434SEnrico.Perla@Sun.COM static void check_extended_support();
1110Sstevel@tonic-gate 
1127563SPrasad.Singamsetty@Sun.COM extern int read_stage2_blocklist(int, unsigned int *);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate int
1150Sstevel@tonic-gate main(int argc, char *argv[])
1160Sstevel@tonic-gate {
117*8434SEnrico.Perla@Sun.COM 	int dev_fd, opt, params = 3;
1180Sstevel@tonic-gate 	char *stage1, *stage2, *device;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1210Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1220Sstevel@tonic-gate 
123*8434SEnrico.Perla@Sun.COM 	while ((opt = getopt(argc, argv, "fmneis:")) != EOF) {
1240Sstevel@tonic-gate 		switch (opt) {
1250Sstevel@tonic-gate 		case 'm':
1260Sstevel@tonic-gate 			write_mboot = 1;
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		case 'n':
1290Sstevel@tonic-gate 			nowrite = 1;
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 		case 'f':
1320Sstevel@tonic-gate 			force_mboot = 1;
1330Sstevel@tonic-gate 			break;
134*8434SEnrico.Perla@Sun.COM 		case 'i':
135*8434SEnrico.Perla@Sun.COM 			getinfo = 1;
136*8434SEnrico.Perla@Sun.COM 			params = 1;
137*8434SEnrico.Perla@Sun.COM 			break;
138*8434SEnrico.Perla@Sun.COM 		case 'e':
139*8434SEnrico.Perla@Sun.COM 			strip = 1;
140*8434SEnrico.Perla@Sun.COM 			break;
141*8434SEnrico.Perla@Sun.COM 		case 's':
142*8434SEnrico.Perla@Sun.COM 			do_version = 1;
143*8434SEnrico.Perla@Sun.COM 			(void) snprintf(verstring, sizeof (verstring), "%s",
144*8434SEnrico.Perla@Sun.COM 			    optarg);
145*8434SEnrico.Perla@Sun.COM 			break;
1460Sstevel@tonic-gate 		default:
1470Sstevel@tonic-gate 			/* fall through to process non-optional args */
1480Sstevel@tonic-gate 			break;
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* check arguments */
153*8434SEnrico.Perla@Sun.COM 	if (argc != optind + params) {
1540Sstevel@tonic-gate 		usage(argv[0]);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (nowrite) {
1580Sstevel@tonic-gate 		(void) fprintf(stdout, DRY_RUN);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
161*8434SEnrico.Perla@Sun.COM 	if (params == 1) {
162*8434SEnrico.Perla@Sun.COM 		device = strdup(argv[optind]);
163*8434SEnrico.Perla@Sun.COM 		if (!device) {
164*8434SEnrico.Perla@Sun.COM 			usage(argv[0]);
165*8434SEnrico.Perla@Sun.COM 		}
166*8434SEnrico.Perla@Sun.COM 	} else if (params == 3) {
167*8434SEnrico.Perla@Sun.COM 		stage1 = strdup(argv[optind]);
168*8434SEnrico.Perla@Sun.COM 		stage2 = strdup(argv[optind + 1]);
169*8434SEnrico.Perla@Sun.COM 		device = strdup(argv[optind + 2]);
1700Sstevel@tonic-gate 
171*8434SEnrico.Perla@Sun.COM 		if (!stage1 || !stage2 || !device) {
172*8434SEnrico.Perla@Sun.COM 			usage(argv[0]);
173*8434SEnrico.Perla@Sun.COM 		}
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* open and check device type */
1770Sstevel@tonic-gate 	dev_fd = open_device(device);
1780Sstevel@tonic-gate 
179*8434SEnrico.Perla@Sun.COM 	if (getinfo) {
180*8434SEnrico.Perla@Sun.COM 		if (read_stage2_info(dev_fd) != 0) {
181*8434SEnrico.Perla@Sun.COM 			fprintf(stderr, "Unable to read extended information"
182*8434SEnrico.Perla@Sun.COM 			    " from %s\n", device);
183*8434SEnrico.Perla@Sun.COM 			exit(1);
184*8434SEnrico.Perla@Sun.COM 		}
185*8434SEnrico.Perla@Sun.COM 		print_info();
186*8434SEnrico.Perla@Sun.COM 		(void) free(device);
187*8434SEnrico.Perla@Sun.COM 		(void) close(dev_fd);
188*8434SEnrico.Perla@Sun.COM 		return (0);
189*8434SEnrico.Perla@Sun.COM 	}
190*8434SEnrico.Perla@Sun.COM 
1910Sstevel@tonic-gate 	/* read in stage1 and stage2 into buffer */
1920Sstevel@tonic-gate 	read_stage1_stage2(stage1, stage2);
1930Sstevel@tonic-gate 
194*8434SEnrico.Perla@Sun.COM 	/* check if stage2 supports extended versioning */
195*8434SEnrico.Perla@Sun.COM 	if (do_version)
196*8434SEnrico.Perla@Sun.COM 		check_extended_support(stage2);
197*8434SEnrico.Perla@Sun.COM 
1980Sstevel@tonic-gate 	/* In the pcfs case, write a fresh stage2 */
1990Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
2000Sstevel@tonic-gate 		copy_stage2(dev_fd, device);
2010Sstevel@tonic-gate 		read_bpb_sect(dev_fd);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/* read in boot sector */
2050Sstevel@tonic-gate 	if (!is_floppy)
2060Sstevel@tonic-gate 		read_boot_sect(device);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* modify stage1 based on grub needs */
2090Sstevel@tonic-gate 	modify_and_write_stage1(dev_fd);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* modify stage2 and write to media */
2120Sstevel@tonic-gate 	modify_and_write_stage2(dev_fd);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (!is_floppy && write_mboot)
2150Sstevel@tonic-gate 		write_boot_sect(device);
216*8434SEnrico.Perla@Sun.COM 
2170Sstevel@tonic-gate 	(void) close(dev_fd);
218*8434SEnrico.Perla@Sun.COM 	free(device);
219*8434SEnrico.Perla@Sun.COM 	free(stage1);
220*8434SEnrico.Perla@Sun.COM 	free(stage2);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	return (0);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2257563SPrasad.Singamsetty@Sun.COM static unsigned int
2265589Ssy25831 get_start_sector(int fd)
2270Sstevel@tonic-gate {
2287563SPrasad.Singamsetty@Sun.COM 	static unsigned int start_sect = 0;
2298333SSuhasini.Peddada@Sun.COM 
2308333SSuhasini.Peddada@Sun.COM 	int i;
2310Sstevel@tonic-gate 	struct mboot *mboot;
2320Sstevel@tonic-gate 	struct ipart *part;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (start_sect)
2350Sstevel@tonic-gate 		return (start_sect);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	mboot = (struct mboot *)boot_sect;
2380Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
2390Sstevel@tonic-gate 		part = (struct ipart *)mboot->parts + i;
2400Sstevel@tonic-gate 		if (is_bootpar) {
2410Sstevel@tonic-gate 			if (part->systid == 0xbe)
2420Sstevel@tonic-gate 				break;
2435589Ssy25831 		}
2445589Ssy25831 	}
2455589Ssy25831 
2465589Ssy25831 	/*
2475589Ssy25831 	 * If there is no boot partition, find the solaris partition
2485589Ssy25831 	 */
2495589Ssy25831 
2505589Ssy25831 	if (i == FD_NUMPART) {
2515589Ssy25831 		struct part_info dkpi;
2527563SPrasad.Singamsetty@Sun.COM 		struct extpart_info edkpi;
2535589Ssy25831 
2545589Ssy25831 		/*
2555589Ssy25831 		 * Get the solaris partition information from the device
2565589Ssy25831 		 * and compare the offset of S2 with offset of solaris partition
2575589Ssy25831 		 * from fdisk partition table.
2585589Ssy25831 		 */
2597563SPrasad.Singamsetty@Sun.COM 		if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
2607563SPrasad.Singamsetty@Sun.COM 			if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) {
2617563SPrasad.Singamsetty@Sun.COM 				(void) fprintf(stderr, PART_FAIL);
2627563SPrasad.Singamsetty@Sun.COM 				exit(-1);
2637563SPrasad.Singamsetty@Sun.COM 			} else {
2647563SPrasad.Singamsetty@Sun.COM 				edkpi.p_start = dkpi.p_start;
2657563SPrasad.Singamsetty@Sun.COM 			}
2665589Ssy25831 		}
2675589Ssy25831 
2685589Ssy25831 		for (i = 0; i < FD_NUMPART; i++) {
2695589Ssy25831 			part = (struct ipart *)mboot->parts + i;
2705589Ssy25831 
2715589Ssy25831 			if (part->relsect == 0) {
2725589Ssy25831 				(void) fprintf(stderr, BAD_PART, i);
2735589Ssy25831 				exit(-1);
2745589Ssy25831 			}
2757563SPrasad.Singamsetty@Sun.COM 			if (edkpi.p_start >= part->relsect &&
2767563SPrasad.Singamsetty@Sun.COM 			    edkpi.p_start < (part->relsect + part->numsect)) {
2775589Ssy25831 				/* Found the partition */
2780Sstevel@tonic-gate 				break;
2795589Ssy25831 			}
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2838333SSuhasini.Peddada@Sun.COM 	if (i == FD_NUMPART) {
2840Sstevel@tonic-gate 		(void) fprintf(stderr, BOOTPAR);
2850Sstevel@tonic-gate 		exit(-1);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/* get confirmation for -m */
2890Sstevel@tonic-gate 	if (write_mboot && !force_mboot) {
2900Sstevel@tonic-gate 		(void) fprintf(stdout, MBOOT_PROMPT);
2910Sstevel@tonic-gate 		if (getchar() != 'y') {
2920Sstevel@tonic-gate 			write_mboot = 0;
2930Sstevel@tonic-gate 			(void) fprintf(stdout, MBOOT_NOT_UPDATED);
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2978333SSuhasini.Peddada@Sun.COM 	start_sect = part->relsect;
2980Sstevel@tonic-gate 	if (part->bootid != 128 && write_mboot == 0) {
2990Sstevel@tonic-gate 		(void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3028333SSuhasini.Peddada@Sun.COM 	partition = i;
3030Sstevel@tonic-gate 	return (start_sect);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static void
3070Sstevel@tonic-gate usage(char *progname)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	(void) fprintf(stderr, USAGE, basename(progname));
3100Sstevel@tonic-gate 	exit(-1);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate static int
3140Sstevel@tonic-gate open_device(char *device)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	int dev_fd;
3170Sstevel@tonic-gate 	struct stat stat;
3180Sstevel@tonic-gate 	char *raw_part;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) &&
3210Sstevel@tonic-gate 	    strncmp(device, "/dev/dsk", strlen("/dev/dsk"));
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/* handle boot partition specification */
3240Sstevel@tonic-gate 	if (!is_floppy && strstr(device, "p0:boot")) {
3250Sstevel@tonic-gate 		is_bootpar = 1;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	raw_part = get_raw_partition(device);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	if (nowrite)
3310Sstevel@tonic-gate 		dev_fd = open(raw_part, O_RDONLY);
3320Sstevel@tonic-gate 	else
3330Sstevel@tonic-gate 		dev_fd = open(raw_part, O_RDWR);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) {
3360Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL, raw_part);
3370Sstevel@tonic-gate 		exit(-1);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	if (S_ISCHR(stat.st_mode) == 0) {
3400Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_RAW_DEVICE, raw_part);
3410Sstevel@tonic-gate 		exit(-1);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (dev_fd);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate static void
3480Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	int fd;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/* read the stage1 file from filesystem */
3530Sstevel@tonic-gate 	fd = open(stage1, O_RDONLY);
3540Sstevel@tonic-gate 	if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) {
3550Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_STAGE1, stage1);
3560Sstevel@tonic-gate 		exit(-1);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 	(void) close(fd);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/* read first two blocks of stage 2 from filesystem */
3610Sstevel@tonic-gate 	stage2_fd = open(stage2, O_RDONLY);
3620Sstevel@tonic-gate 	if (stage2_fd == -1 ||
3630Sstevel@tonic-gate 	    read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE)
3640Sstevel@tonic-gate 	    != 2 * SECTOR_SIZE) {
3650Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_STAGE2, stage2);
3660Sstevel@tonic-gate 		exit(-1);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 	/* leave the stage2 file open for later */
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate static void
3720Sstevel@tonic-gate read_bpb_sect(int dev_fd)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) {
3750Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_BPB);
3760Sstevel@tonic-gate 		exit(-1);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate static void
3810Sstevel@tonic-gate read_boot_sect(char *device)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	static int read_mbr = 0;
3840Sstevel@tonic-gate 	int i, fd;
3850Sstevel@tonic-gate 	char save[2];
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (read_mbr)
3880Sstevel@tonic-gate 		return;
3890Sstevel@tonic-gate 	read_mbr = 1;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/* get the whole disk (p0) */
3920Sstevel@tonic-gate 	i = strlen(device);
3930Sstevel@tonic-gate 	save[0] = device[i - 2];
3940Sstevel@tonic-gate 	save[1] = device[i - 1];
3950Sstevel@tonic-gate 	device[i - 2] = 'p';
3960Sstevel@tonic-gate 	device[i - 1] = '0';
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	fd = open(device, O_RDONLY);
3990Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
4000Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_MBR, device);
4010Sstevel@tonic-gate 		if (fd == -1)
4020Sstevel@tonic-gate 			perror("open");
4030Sstevel@tonic-gate 		else
4040Sstevel@tonic-gate 			perror("read");
4050Sstevel@tonic-gate 		exit(-1);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	(void) close(fd);
4080Sstevel@tonic-gate 	device[i - 2] = save[0];
4090Sstevel@tonic-gate 	device[i - 1] = save[1];
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate static void
4130Sstevel@tonic-gate write_boot_sect(char *device)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	int fd, len;
4160Sstevel@tonic-gate 	char *raw, *end;
4170Sstevel@tonic-gate 	struct stat stat;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/* make a copy and chop off ":boot" */
4200Sstevel@tonic-gate 	raw = strdup(device);
4210Sstevel@tonic-gate 	end = strstr(raw, "p0:boot");
4220Sstevel@tonic-gate 	if (end)
4230Sstevel@tonic-gate 		end[2] = 0;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/* open p0 (whole disk) */
4260Sstevel@tonic-gate 	len = strlen(raw);
4270Sstevel@tonic-gate 	raw[len - 2] = 'p';
4280Sstevel@tonic-gate 	raw[len - 1] = '0';
4290Sstevel@tonic-gate 	fd = open(raw, O_WRONLY);
4300Sstevel@tonic-gate 	if (fd == -1 || fstat(fd, &stat) != 0) {
4310Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL, raw);
4320Sstevel@tonic-gate 		exit(-1);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	if (!nowrite &&
4350Sstevel@tonic-gate 	    pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
4360Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_BOOTSEC);
4370Sstevel@tonic-gate 		exit(-1);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 	(void) fprintf(stdout, WRITE_MBOOT);
4400Sstevel@tonic-gate 	(void) close(fd);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate static void
4440Sstevel@tonic-gate modify_and_write_stage1(int dev_fd)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	if (is_floppy) {
4470Sstevel@tonic-gate 		stage2_first_sector = blocklist[0];
4480Sstevel@tonic-gate 		/* copy bios parameter block (for fat fs) */
4490Sstevel@tonic-gate 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
4500Sstevel@tonic-gate 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
4510Sstevel@tonic-gate 	} else if (is_bootpar) {
4525589Ssy25831 		stage2_first_sector = get_start_sector(dev_fd) + blocklist[0];
4530Sstevel@tonic-gate 		/* copy bios parameter block (for fat fs) and MBR */
4540Sstevel@tonic-gate 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
4550Sstevel@tonic-gate 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
4560Sstevel@tonic-gate 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
4570Sstevel@tonic-gate 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
4580Sstevel@tonic-gate 	} else {
4595589Ssy25831 		stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF;
4600Sstevel@tonic-gate 		/* copy MBR to stage1 in case of overwriting MBR sector */
4610Sstevel@tonic-gate 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
4620Sstevel@tonic-gate 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/* modify default stage1 file generated by GRUB */
4660Sstevel@tonic-gate 	*((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR))
4675589Ssy25831 	    = stage2_first_sector;
4680Sstevel@tonic-gate 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS))
4695589Ssy25831 	    = STAGE2_MEMADDR;
4700Sstevel@tonic-gate 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT))
4715589Ssy25831 	    = STAGE2_MEMADDR >> 4;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * XXX the default grub distribution also:
4750Sstevel@tonic-gate 	 * - Copy the possible MBR/extended part table
4760Sstevel@tonic-gate 	 * - Set the boot drive of stage1
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* write stage1/pboot to 1st sector */
4800Sstevel@tonic-gate 	if (!nowrite &&
4810Sstevel@tonic-gate 	    pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
4820Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_PBOOT);
4830Sstevel@tonic-gate 		exit(-1);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (is_floppy) {
4870Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY);
4880Sstevel@tonic-gate 	} else {
4890Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_PBOOT,
4905589Ssy25831 		    partition, get_start_sector(dev_fd));
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
494*8434SEnrico.Perla@Sun.COM static void check_extended_support(char *stage2)
495*8434SEnrico.Perla@Sun.COM {
496*8434SEnrico.Perla@Sun.COM 	char	*cmp = stage2_buffer + STAGE2_SIGN_OFFSET - 1;
497*8434SEnrico.Perla@Sun.COM 
498*8434SEnrico.Perla@Sun.COM 	if ((*cmp++ != '\xEE') && memcmp(cmp, extended_sig, HASH_SIZE) != 0) {
499*8434SEnrico.Perla@Sun.COM 		fprintf(stderr, "%s does not support extended versioning\n",
500*8434SEnrico.Perla@Sun.COM 		    stage2);
501*8434SEnrico.Perla@Sun.COM 		do_version = 0;
502*8434SEnrico.Perla@Sun.COM 	}
503*8434SEnrico.Perla@Sun.COM }
504*8434SEnrico.Perla@Sun.COM 
505*8434SEnrico.Perla@Sun.COM 
506*8434SEnrico.Perla@Sun.COM static void print_info()
507*8434SEnrico.Perla@Sun.COM {
508*8434SEnrico.Perla@Sun.COM 	int	i;
509*8434SEnrico.Perla@Sun.COM 
510*8434SEnrico.Perla@Sun.COM 	if (strip) {
511*8434SEnrico.Perla@Sun.COM 		fprintf(stdout, "%s\n", verstring);
512*8434SEnrico.Perla@Sun.COM 	} else {
513*8434SEnrico.Perla@Sun.COM 		fprintf(stdout, "Grub extended version information : %s\n",
514*8434SEnrico.Perla@Sun.COM 		    verstring);
515*8434SEnrico.Perla@Sun.COM 		fprintf(stdout, "Grub stage2 (MD5) signature : ");
516*8434SEnrico.Perla@Sun.COM 	}
517*8434SEnrico.Perla@Sun.COM 
518*8434SEnrico.Perla@Sun.COM 	for (i = 0; i < HASH_SIZE; i++)
519*8434SEnrico.Perla@Sun.COM 		fprintf(stdout, "%02x", (unsigned char)signature[i]);
520*8434SEnrico.Perla@Sun.COM 
521*8434SEnrico.Perla@Sun.COM 	fprintf(stdout, "\n");
522*8434SEnrico.Perla@Sun.COM }
523*8434SEnrico.Perla@Sun.COM 
524*8434SEnrico.Perla@Sun.COM static int
525*8434SEnrico.Perla@Sun.COM read_stage2_info(int dev_fd)
526*8434SEnrico.Perla@Sun.COM {
527*8434SEnrico.Perla@Sun.COM 	int 	ret;
528*8434SEnrico.Perla@Sun.COM 	int	first_offset, second_offset;
529*8434SEnrico.Perla@Sun.COM 	char	*sign;
530*8434SEnrico.Perla@Sun.COM 
531*8434SEnrico.Perla@Sun.COM 	if (is_floppy || is_bootpar) {
532*8434SEnrico.Perla@Sun.COM 
533*8434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage1_buffer, SECTOR_SIZE, 0);
534*8434SEnrico.Perla@Sun.COM 		if (ret != SECTOR_SIZE) {
535*8434SEnrico.Perla@Sun.COM 			perror("Error reading stage1 sector");
536*8434SEnrico.Perla@Sun.COM 			return (1);
537*8434SEnrico.Perla@Sun.COM 		}
538*8434SEnrico.Perla@Sun.COM 
539*8434SEnrico.Perla@Sun.COM 		first_offset = *((ulong_t *)(stage1_buffer +
540*8434SEnrico.Perla@Sun.COM 		    STAGE1_STAGE2_SECTOR));
541*8434SEnrico.Perla@Sun.COM 
542*8434SEnrico.Perla@Sun.COM 		/* Start reading in the first sector of stage 2 */
543*8434SEnrico.Perla@Sun.COM 
544*8434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage2_buffer, SECTOR_SIZE, first_offset *
545*8434SEnrico.Perla@Sun.COM 		    SECTOR_SIZE);
546*8434SEnrico.Perla@Sun.COM 		if (ret != SECTOR_SIZE) {
547*8434SEnrico.Perla@Sun.COM 			perror("Error reading stage2 first sector");
548*8434SEnrico.Perla@Sun.COM 			return (1);
549*8434SEnrico.Perla@Sun.COM 		}
550*8434SEnrico.Perla@Sun.COM 
551*8434SEnrico.Perla@Sun.COM 		/* From the block list section grab stage2 second sector */
552*8434SEnrico.Perla@Sun.COM 
553*8434SEnrico.Perla@Sun.COM 		second_offset = *((ulong_t *)(stage2_buffer +
554*8434SEnrico.Perla@Sun.COM 		    STAGE2_BLOCKLIST));
555*8434SEnrico.Perla@Sun.COM 
556*8434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
557*8434SEnrico.Perla@Sun.COM 		    second_offset * SECTOR_SIZE);
558*8434SEnrico.Perla@Sun.COM 		if (ret != SECTOR_SIZE) {
559*8434SEnrico.Perla@Sun.COM 			perror("Error reading stage2 second sector");
560*8434SEnrico.Perla@Sun.COM 			return (1);
561*8434SEnrico.Perla@Sun.COM 		}
562*8434SEnrico.Perla@Sun.COM 	} else {
563*8434SEnrico.Perla@Sun.COM 		ret = pread(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
564*8434SEnrico.Perla@Sun.COM 		    STAGE2_BLKOFF * SECTOR_SIZE);
565*8434SEnrico.Perla@Sun.COM 		if (ret != 2 * SECTOR_SIZE) {
566*8434SEnrico.Perla@Sun.COM 			perror("Error reading stage2 sectors");
567*8434SEnrico.Perla@Sun.COM 			return (1);
568*8434SEnrico.Perla@Sun.COM 		}
569*8434SEnrico.Perla@Sun.COM 	}
570*8434SEnrico.Perla@Sun.COM 
571*8434SEnrico.Perla@Sun.COM 	sign = stage2_buffer + STAGE2_SIGN_OFFSET - 1;
572*8434SEnrico.Perla@Sun.COM 	if (*sign++ != '\xEE')
573*8434SEnrico.Perla@Sun.COM 		return (1);
574*8434SEnrico.Perla@Sun.COM 	(void) memcpy(signature, sign, HASH_SIZE);
575*8434SEnrico.Perla@Sun.COM 	sign = stage2_buffer + STAGE2_PKG_VERSION;
576*8434SEnrico.Perla@Sun.COM 	(void) strncpy(verstring, sign, VERSION_SIZE);
577*8434SEnrico.Perla@Sun.COM 	return (0);
578*8434SEnrico.Perla@Sun.COM }
579*8434SEnrico.Perla@Sun.COM 
580*8434SEnrico.Perla@Sun.COM 
581*8434SEnrico.Perla@Sun.COM static int
582*8434SEnrico.Perla@Sun.COM compute_and_write_md5hash(char *dest)
583*8434SEnrico.Perla@Sun.COM {
584*8434SEnrico.Perla@Sun.COM 	struct stat	sb;
585*8434SEnrico.Perla@Sun.COM 	char		*buffer;
586*8434SEnrico.Perla@Sun.COM 
587*8434SEnrico.Perla@Sun.COM 	if (fstat(stage2_fd, &sb) == -1)
588*8434SEnrico.Perla@Sun.COM 		return (-1);
589*8434SEnrico.Perla@Sun.COM 
590*8434SEnrico.Perla@Sun.COM 	buffer = malloc(sb.st_size);
591*8434SEnrico.Perla@Sun.COM 	if (buffer == NULL)
592*8434SEnrico.Perla@Sun.COM 		return (-1);
593*8434SEnrico.Perla@Sun.COM 
594*8434SEnrico.Perla@Sun.COM 	if (lseek(stage2_fd, 0, SEEK_SET) == -1)
595*8434SEnrico.Perla@Sun.COM 		return (-1);
596*8434SEnrico.Perla@Sun.COM 	if (read(stage2_fd, buffer, sb.st_size) < 0)
597*8434SEnrico.Perla@Sun.COM 		return (-1);
598*8434SEnrico.Perla@Sun.COM 
599*8434SEnrico.Perla@Sun.COM 	md5_calc(dest, buffer, sb.st_size);
600*8434SEnrico.Perla@Sun.COM 	free(buffer);
601*8434SEnrico.Perla@Sun.COM 	return (0);
602*8434SEnrico.Perla@Sun.COM }
603*8434SEnrico.Perla@Sun.COM 
604*8434SEnrico.Perla@Sun.COM 
6050Sstevel@tonic-gate #define	START_BLOCK(pos)	(*(ulong_t *)(pos))
6060Sstevel@tonic-gate #define	NUM_BLOCK(pos)		(*(ushort_t *)((pos) + 4))
6070Sstevel@tonic-gate #define	START_SEG(pos)		(*(ushort_t *)((pos) + 6))
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate static void
6100Sstevel@tonic-gate modify_and_write_stage2(int dev_fd)
6110Sstevel@tonic-gate {
612*8434SEnrico.Perla@Sun.COM 	int 	nrecord;
613*8434SEnrico.Perla@Sun.COM 	off_t 	offset;
614*8434SEnrico.Perla@Sun.COM 	char	*dest;
615*8434SEnrico.Perla@Sun.COM 
616*8434SEnrico.Perla@Sun.COM 	if (do_version) {
617*8434SEnrico.Perla@Sun.COM 		dest = stage2_buffer + STAGE2_SIGN_OFFSET;
618*8434SEnrico.Perla@Sun.COM 		if (compute_and_write_md5hash(dest) < 0)
619*8434SEnrico.Perla@Sun.COM 			perror("MD5 operation");
620*8434SEnrico.Perla@Sun.COM 		dest = stage2_buffer + STAGE2_PKG_VERSION;
621*8434SEnrico.Perla@Sun.COM 		(void) strncpy(dest, verstring, VERSION_SIZE);
622*8434SEnrico.Perla@Sun.COM 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
6250Sstevel@tonic-gate 		int i = 0;
6268333SSuhasini.Peddada@Sun.COM 		uint_t partition_offset;
6278333SSuhasini.Peddada@Sun.COM 		uint_t install_addr = 0x8200;
6280Sstevel@tonic-gate 		uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		stage2_first_sector = blocklist[0];
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		/* figure out the second sector */
6330Sstevel@tonic-gate 		if (blocklist[1] > 1) {
6340Sstevel@tonic-gate 			blocklist[0]++;
6350Sstevel@tonic-gate 			blocklist[1]--;
6360Sstevel@tonic-gate 		} else {
6370Sstevel@tonic-gate 			i += 2;
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 		stage2_second_sector = blocklist[i];
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		if (is_floppy)
6420Sstevel@tonic-gate 			partition_offset = 0;
6430Sstevel@tonic-gate 		else	/* solaris boot partition */
6445589Ssy25831 			partition_offset = get_start_sector(dev_fd);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		/* install the blocklist at the end of stage2_buffer */
6470Sstevel@tonic-gate 		while (blocklist[i]) {
6480Sstevel@tonic-gate 			if (START_BLOCK(pos - 8) != 0 &&
6490Sstevel@tonic-gate 			    START_BLOCK(pos - 8) != blocklist[i + 2]) {
6500Sstevel@tonic-gate 				(void) fprintf(stderr, PCFS_FRAGMENTED);
6510Sstevel@tonic-gate 				exit(-1);
6520Sstevel@tonic-gate 			}
6530Sstevel@tonic-gate 			START_BLOCK(pos) = blocklist[i] + partition_offset;
6540Sstevel@tonic-gate 			START_SEG(pos) = (ushort_t)(install_addr >> 4);
6550Sstevel@tonic-gate 			NUM_BLOCK(pos) = blocklist[i + 1];
6560Sstevel@tonic-gate 			install_addr += blocklist[i + 1] * SECTOR_SIZE;
6570Sstevel@tonic-gate 			pos -= 8;
6580Sstevel@tonic-gate 			i += 2;
6590Sstevel@tonic-gate 		}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	} else {
6620Sstevel@tonic-gate 		/*
6630Sstevel@tonic-gate 		 * In a solaris partition, stage2 is written to contiguous
6640Sstevel@tonic-gate 		 * blocks. So we update the starting block only.
6650Sstevel@tonic-gate 		 */
6660Sstevel@tonic-gate 		*((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) =
6670Sstevel@tonic-gate 		    stage2_first_sector + 1;
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (is_floppy) {
6710Sstevel@tonic-gate 		/* modify the config file to add (fd0) */
6720Sstevel@tonic-gate 		char *config_file = stage2_buffer + STAGE2_VER_STRING;
6730Sstevel@tonic-gate 		while (*config_file++)
6740Sstevel@tonic-gate 			;
6750Sstevel@tonic-gate 		strcpy(config_file, "(fd0)/boot/grub/menu.lst");
6760Sstevel@tonic-gate 	} else {
6770Sstevel@tonic-gate 		/* force lba and set disk partition */
6780Sstevel@tonic-gate 		*((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1;
6790Sstevel@tonic-gate 		*((long *)(stage2_buffer + STAGE2_INSTALLPART))
6800Sstevel@tonic-gate 		    = (partition << 16) | (slice << 8) | 0xff;
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/* modification done, now do the writing */
6840Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
6850Sstevel@tonic-gate 		/* we rewrite block 0 and 1 and that's it */
6860Sstevel@tonic-gate 		if (!nowrite &&
6870Sstevel@tonic-gate 		    (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
6880Sstevel@tonic-gate 		    stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE ||
6890Sstevel@tonic-gate 		    pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
6900Sstevel@tonic-gate 		    stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) {
6910Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2);
6920Sstevel@tonic-gate 			exit(-1);
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_STAGE2_PCFS);
6950Sstevel@tonic-gate 		return;
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	/* for disk, write stage2 starting at STAGE2_BLKOFF sector */
6990Sstevel@tonic-gate 	offset = STAGE2_BLKOFF;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/* write the modified first two sectors */
7020Sstevel@tonic-gate 	if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
7030Sstevel@tonic-gate 	    offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) {
7040Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_STAGE2);
7050Sstevel@tonic-gate 		exit(-1);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/* write the remaining sectors */
7090Sstevel@tonic-gate 	nrecord = 2;
7100Sstevel@tonic-gate 	offset += 2;
7110Sstevel@tonic-gate 	for (;;) {
7120Sstevel@tonic-gate 		int nread, nwrite;
7130Sstevel@tonic-gate 		nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE,
7140Sstevel@tonic-gate 		    nrecord * SECTOR_SIZE);
7150Sstevel@tonic-gate 		if (nread > 0 && !nowrite)
7160Sstevel@tonic-gate 			nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
7170Sstevel@tonic-gate 			    offset * SECTOR_SIZE);
7180Sstevel@tonic-gate 		else
7190Sstevel@tonic-gate 			nwrite = SECTOR_SIZE;
7200Sstevel@tonic-gate 		if (nread < 0 || nwrite != SECTOR_SIZE) {
7210Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
7220Sstevel@tonic-gate 			    nread, nwrite);
7230Sstevel@tonic-gate 			break;
7240Sstevel@tonic-gate 		}
725322Sjongkis 		if (nread > 0) {
726322Sjongkis 			nrecord ++;
727322Sjongkis 			offset ++;
728322Sjongkis 		}
7290Sstevel@tonic-gate 		if (nread < SECTOR_SIZE)
7300Sstevel@tonic-gate 			break;	/* end of file */
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	(void) fprintf(stdout, WRITE_STAGE2_DISK,
7330Sstevel@tonic-gate 	    partition, nrecord, STAGE2_BLKOFF, stage2_first_sector);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static char *
7370Sstevel@tonic-gate get_raw_partition(char *device)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	int len;
7400Sstevel@tonic-gate 	struct mboot *mboot;
7410Sstevel@tonic-gate 	static char *raw = NULL;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (raw)
7440Sstevel@tonic-gate 		return (raw);
7450Sstevel@tonic-gate 	raw = strdup(device);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	if (is_floppy)
7480Sstevel@tonic-gate 		return (raw);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if (is_bootpar) {
7510Sstevel@tonic-gate 		int i;
7520Sstevel@tonic-gate 		char *end = strstr(raw, "p0:boot");
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 		end[2] = 0;		/* chop off :boot */
7550Sstevel@tonic-gate 		read_boot_sect(raw);
7560Sstevel@tonic-gate 		mboot = (struct mboot *)boot_sect;
7570Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
7580Sstevel@tonic-gate 			struct ipart *part = (struct ipart *)mboot->parts + i;
7590Sstevel@tonic-gate 			if (part->systid == 0xbe)	/* solaris boot part */
7600Sstevel@tonic-gate 				break;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		if (i == FD_NUMPART) {
7640Sstevel@tonic-gate 			(void) fprintf(stderr, BOOTPAR_NOTFOUND, device);
7650Sstevel@tonic-gate 			exit(-1);
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 		end[1] = '1' + i;	/* set partition name */
7680Sstevel@tonic-gate 		return (raw);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/* For disk, remember slice and return whole fdisk partition  */
7720Sstevel@tonic-gate 	len = strlen(raw);
7730Sstevel@tonic-gate 	if (raw[len - 2] != 's' || raw[len - 1] == '2') {
7740Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_ROOT_SLICE);
7750Sstevel@tonic-gate 		exit(-1);
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 	slice = atoi(&raw[len - 1]);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	raw[len - 2] = 's';
7800Sstevel@tonic-gate 	raw[len - 1] = '2';
7810Sstevel@tonic-gate 	return (raw);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate #define	TMP_MNTPT	"/tmp/installgrub_pcfs"
7850Sstevel@tonic-gate static void
7860Sstevel@tonic-gate copy_stage2(int dev_fd, char *device)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	FILE *mntfp;
7890Sstevel@tonic-gate 	int i, pcfs_fp;
7900Sstevel@tonic-gate 	char buf[SECTOR_SIZE];
7910Sstevel@tonic-gate 	char *cp;
7920Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/* convert raw to block device name by removing the first 'r' */
7950Sstevel@tonic-gate 	(void) strncpy(buf, device, sizeof (buf));
7960Sstevel@tonic-gate 	buf[sizeof (buf) - 1] = 0;
7970Sstevel@tonic-gate 	cp = strchr(buf, 'r');
7980Sstevel@tonic-gate 	if (cp == NULL) {
7990Sstevel@tonic-gate 		(void) fprintf(stderr, CONVERT_FAIL, device);
8000Sstevel@tonic-gate 		exit(-1);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	do {
8030Sstevel@tonic-gate 		*cp = *(cp + 1);
8040Sstevel@tonic-gate 	} while (*(++cp));
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/* get the mount point, if any */
8070Sstevel@tonic-gate 	mntfp = fopen("/etc/mnttab", "r");
8080Sstevel@tonic-gate 	if (mntfp == NULL) {
8090Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab");
8100Sstevel@tonic-gate 		exit(-1);
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	mpref.mnt_special = buf;
8140Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
8150Sstevel@tonic-gate 		char cmd[128];
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		/* not mounted, try remount */
8180Sstevel@tonic-gate 		(void) mkdir(TMP_MNTPT, S_IRWXU);
8190Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s",
8200Sstevel@tonic-gate 		    buf, TMP_MNTPT);
8210Sstevel@tonic-gate 		(void) system(cmd);
8220Sstevel@tonic-gate 		rewind(mntfp);
8230Sstevel@tonic-gate 		bzero(&mp, sizeof (mp));
8240Sstevel@tonic-gate 		if (getmntany(mntfp, &mp, &mpref) != 0) {
8250Sstevel@tonic-gate 			(void) fprintf(stderr, MOUNT_FAIL, buf);
8260Sstevel@tonic-gate 			exit(-1);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
8310Sstevel@tonic-gate 	    "%s/boot", mp.mnt_mountp);
8320Sstevel@tonic-gate 	(void) mkdir(buf, S_IRWXU);
8330Sstevel@tonic-gate 	(void) strcat(buf, "/grub");
8340Sstevel@tonic-gate 	(void) mkdir(buf, S_IRWXU);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	(void) strcat(buf, "/stage2");
8370Sstevel@tonic-gate 	pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU);
8380Sstevel@tonic-gate 	if (pcfs_fp == -1) {
8390Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL_FILE, buf);
8400Sstevel@tonic-gate 		perror("open:");
8410Sstevel@tonic-gate 		(void) umount(TMP_MNTPT);
8420Sstevel@tonic-gate 		exit(-1);
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	/* write stage2 to pcfs */
8460Sstevel@tonic-gate 	for (i = 0; ; i++) {
8470Sstevel@tonic-gate 		int nread, nwrite;
8480Sstevel@tonic-gate 		nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE);
8490Sstevel@tonic-gate 		if (nowrite)
8500Sstevel@tonic-gate 			nwrite = nread;
8510Sstevel@tonic-gate 		else
8520Sstevel@tonic-gate 			nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE);
8530Sstevel@tonic-gate 		if (nread < 0 || nwrite != nread) {
8540Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
8550Sstevel@tonic-gate 			    nread, nwrite);
8560Sstevel@tonic-gate 			break;
8570Sstevel@tonic-gate 		}
8580Sstevel@tonic-gate 		if (nread < SECTOR_SIZE)
8590Sstevel@tonic-gate 			break;	/* end of file */
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 	(void) close(pcfs_fp);
8620Sstevel@tonic-gate 	(void) umount(TMP_MNTPT);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/*
8650Sstevel@tonic-gate 	 * Now, get the blocklist from the device.
8660Sstevel@tonic-gate 	 */
8670Sstevel@tonic-gate 	bzero(blocklist, sizeof (blocklist));
8680Sstevel@tonic-gate 	if (read_stage2_blocklist(dev_fd, blocklist) != 0)
8690Sstevel@tonic-gate 		exit(-1);
8700Sstevel@tonic-gate }
871