xref: /onnv-gate/usr/src/cmd/boot/installgrub/installgrub.c (revision 8333:de8038a7796e)
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>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
480Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
490Sstevel@tonic-gate #endif
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	SECTOR_SIZE	0x200
520Sstevel@tonic-gate #define	STAGE2_MEMADDR	0x8000	/* loading addr of stage2 */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	STAGE1_BPB_OFFSET	0x3
550Sstevel@tonic-gate #define	STAGE1_BPB_SIZE		0x3B
560Sstevel@tonic-gate #define	STAGE1_BOOT_DRIVE	0x40
570Sstevel@tonic-gate #define	STAGE1_FORCE_LBA	0x41
580Sstevel@tonic-gate #define	STAGE1_STAGE2_ADDRESS	0x42
590Sstevel@tonic-gate #define	STAGE1_STAGE2_SECTOR	0x44
600Sstevel@tonic-gate #define	STAGE1_STAGE2_SEGMENT	0x48
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	STAGE2_BLOCKLIST	(SECTOR_SIZE - 0x8)
630Sstevel@tonic-gate #define	STAGE2_INSTALLPART	(SECTOR_SIZE + 0x8)
640Sstevel@tonic-gate #define	STAGE2_FORCE_LBA	(SECTOR_SIZE + 0x11)
650Sstevel@tonic-gate #define	STAGE2_VER_STRING	(SECTOR_SIZE + 0x12)
660Sstevel@tonic-gate #define	STAGE2_BLKOFF		50	/* offset from start of fdisk part */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int nowrite = 0;
690Sstevel@tonic-gate static int write_mboot = 0;
700Sstevel@tonic-gate static int force_mboot = 0;
710Sstevel@tonic-gate static int is_floppy = 0;
720Sstevel@tonic-gate static int is_bootpar = 0;
730Sstevel@tonic-gate static int stage2_fd;
740Sstevel@tonic-gate static int partition, slice = 0xff;
75*8333SSuhasini.Peddada@Sun.COM static unsigned int stage2_first_sector, stage2_second_sector;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE];
790Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE];
800Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE];
810Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE];
827563SPrasad.Singamsetty@Sun.COM static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)];
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int open_device(char *);
850Sstevel@tonic-gate static void read_bpb_sect(int);
860Sstevel@tonic-gate static void read_boot_sect(char *);
870Sstevel@tonic-gate static void write_boot_sect(char *);
880Sstevel@tonic-gate static void read_stage1_stage2(char *, char *);
890Sstevel@tonic-gate static void modify_and_write_stage1(int);
900Sstevel@tonic-gate static void modify_and_write_stage2(int);
917563SPrasad.Singamsetty@Sun.COM static unsigned int get_start_sector(int);
920Sstevel@tonic-gate static void copy_stage2(int, char *);
930Sstevel@tonic-gate static char *get_raw_partition(char *);
940Sstevel@tonic-gate static void usage(char *);
950Sstevel@tonic-gate 
967563SPrasad.Singamsetty@Sun.COM extern int read_stage2_blocklist(int, unsigned int *);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate int
990Sstevel@tonic-gate main(int argc, char *argv[])
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	int dev_fd, opt;
1020Sstevel@tonic-gate 	char *stage1, *stage2, *device;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1050Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fmn")) != EOF) {
1080Sstevel@tonic-gate 		switch (opt) {
1090Sstevel@tonic-gate 		case 'm':
1100Sstevel@tonic-gate 			write_mboot = 1;
1110Sstevel@tonic-gate 			break;
1120Sstevel@tonic-gate 		case 'n':
1130Sstevel@tonic-gate 			nowrite = 1;
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 		case 'f':
1160Sstevel@tonic-gate 			force_mboot = 1;
1170Sstevel@tonic-gate 			break;
1180Sstevel@tonic-gate 		default:
1190Sstevel@tonic-gate 			/* fall through to process non-optional args */
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/* check arguments */
1250Sstevel@tonic-gate 	if (argc != optind + 3) {
1260Sstevel@tonic-gate 		usage(argv[0]);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (nowrite) {
1300Sstevel@tonic-gate 		(void) fprintf(stdout, DRY_RUN);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	stage1 = strdup(argv[optind]);
1340Sstevel@tonic-gate 	stage2 = strdup(argv[optind + 1]);
1350Sstevel@tonic-gate 	device = strdup(argv[optind + 2]);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (!stage1 || !stage2 || !device) {
1380Sstevel@tonic-gate 		usage(argv[0]);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/* open and check device type */
1420Sstevel@tonic-gate 	dev_fd = open_device(device);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/* read in stage1 and stage2 into buffer */
1450Sstevel@tonic-gate 	read_stage1_stage2(stage1, stage2);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* In the pcfs case, write a fresh stage2 */
1480Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
1490Sstevel@tonic-gate 		copy_stage2(dev_fd, device);
1500Sstevel@tonic-gate 		read_bpb_sect(dev_fd);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* read in boot sector */
1540Sstevel@tonic-gate 	if (!is_floppy)
1550Sstevel@tonic-gate 		read_boot_sect(device);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/* modify stage1 based on grub needs */
1580Sstevel@tonic-gate 	modify_and_write_stage1(dev_fd);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* modify stage2 and write to media */
1610Sstevel@tonic-gate 	modify_and_write_stage2(dev_fd);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (!is_floppy && write_mboot)
1640Sstevel@tonic-gate 		write_boot_sect(device);
1650Sstevel@tonic-gate 	(void) close(dev_fd);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	return (0);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1707563SPrasad.Singamsetty@Sun.COM static unsigned int
1715589Ssy25831 get_start_sector(int fd)
1720Sstevel@tonic-gate {
1737563SPrasad.Singamsetty@Sun.COM 	static unsigned int start_sect = 0;
174*8333SSuhasini.Peddada@Sun.COM 
175*8333SSuhasini.Peddada@Sun.COM 	int i;
1760Sstevel@tonic-gate 	struct mboot *mboot;
1770Sstevel@tonic-gate 	struct ipart *part;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (start_sect)
1800Sstevel@tonic-gate 		return (start_sect);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	mboot = (struct mboot *)boot_sect;
1830Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
1840Sstevel@tonic-gate 		part = (struct ipart *)mboot->parts + i;
1850Sstevel@tonic-gate 		if (is_bootpar) {
1860Sstevel@tonic-gate 			if (part->systid == 0xbe)
1870Sstevel@tonic-gate 				break;
1885589Ssy25831 		}
1895589Ssy25831 	}
1905589Ssy25831 
1915589Ssy25831 	/*
1925589Ssy25831 	 * If there is no boot partition, find the solaris partition
1935589Ssy25831 	 */
1945589Ssy25831 
1955589Ssy25831 	if (i == FD_NUMPART) {
1965589Ssy25831 		struct part_info dkpi;
1977563SPrasad.Singamsetty@Sun.COM 		struct extpart_info edkpi;
1985589Ssy25831 
1995589Ssy25831 		/*
2005589Ssy25831 		 * Get the solaris partition information from the device
2015589Ssy25831 		 * and compare the offset of S2 with offset of solaris partition
2025589Ssy25831 		 * from fdisk partition table.
2035589Ssy25831 		 */
2047563SPrasad.Singamsetty@Sun.COM 		if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
2057563SPrasad.Singamsetty@Sun.COM 			if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) {
2067563SPrasad.Singamsetty@Sun.COM 				(void) fprintf(stderr, PART_FAIL);
2077563SPrasad.Singamsetty@Sun.COM 				exit(-1);
2087563SPrasad.Singamsetty@Sun.COM 			} else {
2097563SPrasad.Singamsetty@Sun.COM 				edkpi.p_start = dkpi.p_start;
2107563SPrasad.Singamsetty@Sun.COM 			}
2115589Ssy25831 		}
2125589Ssy25831 
2135589Ssy25831 		for (i = 0; i < FD_NUMPART; i++) {
2145589Ssy25831 			part = (struct ipart *)mboot->parts + i;
2155589Ssy25831 
2165589Ssy25831 			if (part->relsect == 0) {
2175589Ssy25831 				(void) fprintf(stderr, BAD_PART, i);
2185589Ssy25831 				exit(-1);
2195589Ssy25831 			}
2207563SPrasad.Singamsetty@Sun.COM 			if (edkpi.p_start >= part->relsect &&
2217563SPrasad.Singamsetty@Sun.COM 			    edkpi.p_start < (part->relsect + part->numsect)) {
2225589Ssy25831 				/* Found the partition */
2230Sstevel@tonic-gate 				break;
2245589Ssy25831 			}
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
228*8333SSuhasini.Peddada@Sun.COM 	if (i == FD_NUMPART) {
2290Sstevel@tonic-gate 		(void) fprintf(stderr, BOOTPAR);
2300Sstevel@tonic-gate 		exit(-1);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/* get confirmation for -m */
2340Sstevel@tonic-gate 	if (write_mboot && !force_mboot) {
2350Sstevel@tonic-gate 		(void) fprintf(stdout, MBOOT_PROMPT);
2360Sstevel@tonic-gate 		if (getchar() != 'y') {
2370Sstevel@tonic-gate 			write_mboot = 0;
2380Sstevel@tonic-gate 			(void) fprintf(stdout, MBOOT_NOT_UPDATED);
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
242*8333SSuhasini.Peddada@Sun.COM 	start_sect = part->relsect;
2430Sstevel@tonic-gate 	if (part->bootid != 128 && write_mboot == 0) {
2440Sstevel@tonic-gate 		(void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
247*8333SSuhasini.Peddada@Sun.COM 	partition = i;
2480Sstevel@tonic-gate 	return (start_sect);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static void
2520Sstevel@tonic-gate usage(char *progname)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	(void) fprintf(stderr, USAGE, basename(progname));
2550Sstevel@tonic-gate 	exit(-1);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static int
2590Sstevel@tonic-gate open_device(char *device)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	int dev_fd;
2620Sstevel@tonic-gate 	struct stat stat;
2630Sstevel@tonic-gate 	char *raw_part;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) &&
2660Sstevel@tonic-gate 	    strncmp(device, "/dev/dsk", strlen("/dev/dsk"));
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/* handle boot partition specification */
2690Sstevel@tonic-gate 	if (!is_floppy && strstr(device, "p0:boot")) {
2700Sstevel@tonic-gate 		is_bootpar = 1;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	raw_part = get_raw_partition(device);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (nowrite)
2760Sstevel@tonic-gate 		dev_fd = open(raw_part, O_RDONLY);
2770Sstevel@tonic-gate 	else
2780Sstevel@tonic-gate 		dev_fd = open(raw_part, O_RDWR);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) {
2810Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL, raw_part);
2820Sstevel@tonic-gate 		exit(-1);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	if (S_ISCHR(stat.st_mode) == 0) {
2850Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_RAW_DEVICE, raw_part);
2860Sstevel@tonic-gate 		exit(-1);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	return (dev_fd);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static void
2930Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	int fd;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/* read the stage1 file from filesystem */
2980Sstevel@tonic-gate 	fd = open(stage1, O_RDONLY);
2990Sstevel@tonic-gate 	if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) {
3000Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_STAGE1, stage1);
3010Sstevel@tonic-gate 		exit(-1);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	(void) close(fd);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	/* read first two blocks of stage 2 from filesystem */
3060Sstevel@tonic-gate 	stage2_fd = open(stage2, O_RDONLY);
3070Sstevel@tonic-gate 	if (stage2_fd == -1 ||
3080Sstevel@tonic-gate 	    read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE)
3090Sstevel@tonic-gate 	    != 2 * SECTOR_SIZE) {
3100Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_STAGE2, stage2);
3110Sstevel@tonic-gate 		exit(-1);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	/* leave the stage2 file open for later */
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static void
3170Sstevel@tonic-gate read_bpb_sect(int dev_fd)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) {
3200Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_BPB);
3210Sstevel@tonic-gate 		exit(-1);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static void
3260Sstevel@tonic-gate read_boot_sect(char *device)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	static int read_mbr = 0;
3290Sstevel@tonic-gate 	int i, fd;
3300Sstevel@tonic-gate 	char save[2];
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (read_mbr)
3330Sstevel@tonic-gate 		return;
3340Sstevel@tonic-gate 	read_mbr = 1;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/* get the whole disk (p0) */
3370Sstevel@tonic-gate 	i = strlen(device);
3380Sstevel@tonic-gate 	save[0] = device[i - 2];
3390Sstevel@tonic-gate 	save[1] = device[i - 1];
3400Sstevel@tonic-gate 	device[i - 2] = 'p';
3410Sstevel@tonic-gate 	device[i - 1] = '0';
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	fd = open(device, O_RDONLY);
3440Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
3450Sstevel@tonic-gate 		(void) fprintf(stderr, READ_FAIL_MBR, device);
3460Sstevel@tonic-gate 		if (fd == -1)
3470Sstevel@tonic-gate 			perror("open");
3480Sstevel@tonic-gate 		else
3490Sstevel@tonic-gate 			perror("read");
3500Sstevel@tonic-gate 		exit(-1);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	(void) close(fd);
3530Sstevel@tonic-gate 	device[i - 2] = save[0];
3540Sstevel@tonic-gate 	device[i - 1] = save[1];
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static void
3580Sstevel@tonic-gate write_boot_sect(char *device)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	int fd, len;
3610Sstevel@tonic-gate 	char *raw, *end;
3620Sstevel@tonic-gate 	struct stat stat;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/* make a copy and chop off ":boot" */
3650Sstevel@tonic-gate 	raw = strdup(device);
3660Sstevel@tonic-gate 	end = strstr(raw, "p0:boot");
3670Sstevel@tonic-gate 	if (end)
3680Sstevel@tonic-gate 		end[2] = 0;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/* open p0 (whole disk) */
3710Sstevel@tonic-gate 	len = strlen(raw);
3720Sstevel@tonic-gate 	raw[len - 2] = 'p';
3730Sstevel@tonic-gate 	raw[len - 1] = '0';
3740Sstevel@tonic-gate 	fd = open(raw, O_WRONLY);
3750Sstevel@tonic-gate 	if (fd == -1 || fstat(fd, &stat) != 0) {
3760Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL, raw);
3770Sstevel@tonic-gate 		exit(-1);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	if (!nowrite &&
3800Sstevel@tonic-gate 	    pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
3810Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_BOOTSEC);
3820Sstevel@tonic-gate 		exit(-1);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	(void) fprintf(stdout, WRITE_MBOOT);
3850Sstevel@tonic-gate 	(void) close(fd);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate static void
3890Sstevel@tonic-gate modify_and_write_stage1(int dev_fd)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	if (is_floppy) {
3920Sstevel@tonic-gate 		stage2_first_sector = blocklist[0];
3930Sstevel@tonic-gate 		/* copy bios parameter block (for fat fs) */
3940Sstevel@tonic-gate 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
3950Sstevel@tonic-gate 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
3960Sstevel@tonic-gate 	} else if (is_bootpar) {
3975589Ssy25831 		stage2_first_sector = get_start_sector(dev_fd) + blocklist[0];
3980Sstevel@tonic-gate 		/* copy bios parameter block (for fat fs) and MBR */
3990Sstevel@tonic-gate 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
4000Sstevel@tonic-gate 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
4010Sstevel@tonic-gate 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
4020Sstevel@tonic-gate 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
4030Sstevel@tonic-gate 	} else {
4045589Ssy25831 		stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF;
4050Sstevel@tonic-gate 		/* copy MBR to stage1 in case of overwriting MBR sector */
4060Sstevel@tonic-gate 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
4070Sstevel@tonic-gate 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/* modify default stage1 file generated by GRUB */
4110Sstevel@tonic-gate 	*((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR))
4125589Ssy25831 	    = stage2_first_sector;
4130Sstevel@tonic-gate 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS))
4145589Ssy25831 	    = STAGE2_MEMADDR;
4150Sstevel@tonic-gate 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT))
4165589Ssy25831 	    = STAGE2_MEMADDR >> 4;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * XXX the default grub distribution also:
4200Sstevel@tonic-gate 	 * - Copy the possible MBR/extended part table
4210Sstevel@tonic-gate 	 * - Set the boot drive of stage1
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/* write stage1/pboot to 1st sector */
4250Sstevel@tonic-gate 	if (!nowrite &&
4260Sstevel@tonic-gate 	    pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
4270Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_PBOOT);
4280Sstevel@tonic-gate 		exit(-1);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (is_floppy) {
4320Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY);
4330Sstevel@tonic-gate 	} else {
4340Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_PBOOT,
4355589Ssy25831 		    partition, get_start_sector(dev_fd));
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate #define	START_BLOCK(pos)	(*(ulong_t *)(pos))
4400Sstevel@tonic-gate #define	NUM_BLOCK(pos)		(*(ushort_t *)((pos) + 4))
4410Sstevel@tonic-gate #define	START_SEG(pos)		(*(ushort_t *)((pos) + 6))
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate static void
4440Sstevel@tonic-gate modify_and_write_stage2(int dev_fd)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	int nrecord;
4470Sstevel@tonic-gate 	off_t offset;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
4500Sstevel@tonic-gate 		int i = 0;
451*8333SSuhasini.Peddada@Sun.COM 		uint_t partition_offset;
452*8333SSuhasini.Peddada@Sun.COM 		uint_t install_addr = 0x8200;
4530Sstevel@tonic-gate 		uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		stage2_first_sector = blocklist[0];
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		/* figure out the second sector */
4580Sstevel@tonic-gate 		if (blocklist[1] > 1) {
4590Sstevel@tonic-gate 			blocklist[0]++;
4600Sstevel@tonic-gate 			blocklist[1]--;
4610Sstevel@tonic-gate 		} else {
4620Sstevel@tonic-gate 			i += 2;
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 		stage2_second_sector = blocklist[i];
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		if (is_floppy)
4670Sstevel@tonic-gate 			partition_offset = 0;
4680Sstevel@tonic-gate 		else	/* solaris boot partition */
4695589Ssy25831 			partition_offset = get_start_sector(dev_fd);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/* install the blocklist at the end of stage2_buffer */
4720Sstevel@tonic-gate 		while (blocklist[i]) {
4730Sstevel@tonic-gate 			if (START_BLOCK(pos - 8) != 0 &&
4740Sstevel@tonic-gate 			    START_BLOCK(pos - 8) != blocklist[i + 2]) {
4750Sstevel@tonic-gate 				(void) fprintf(stderr, PCFS_FRAGMENTED);
4760Sstevel@tonic-gate 				exit(-1);
4770Sstevel@tonic-gate 			}
4780Sstevel@tonic-gate 			START_BLOCK(pos) = blocklist[i] + partition_offset;
4790Sstevel@tonic-gate 			START_SEG(pos) = (ushort_t)(install_addr >> 4);
4800Sstevel@tonic-gate 			NUM_BLOCK(pos) = blocklist[i + 1];
4810Sstevel@tonic-gate 			install_addr += blocklist[i + 1] * SECTOR_SIZE;
4820Sstevel@tonic-gate 			pos -= 8;
4830Sstevel@tonic-gate 			i += 2;
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	} else {
4870Sstevel@tonic-gate 		/*
4880Sstevel@tonic-gate 		 * In a solaris partition, stage2 is written to contiguous
4890Sstevel@tonic-gate 		 * blocks. So we update the starting block only.
4900Sstevel@tonic-gate 		 */
4910Sstevel@tonic-gate 		*((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) =
4920Sstevel@tonic-gate 		    stage2_first_sector + 1;
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	if (is_floppy) {
4960Sstevel@tonic-gate 		/* modify the config file to add (fd0) */
4970Sstevel@tonic-gate 		char *config_file = stage2_buffer + STAGE2_VER_STRING;
4980Sstevel@tonic-gate 		while (*config_file++)
4990Sstevel@tonic-gate 			;
5000Sstevel@tonic-gate 		strcpy(config_file, "(fd0)/boot/grub/menu.lst");
5010Sstevel@tonic-gate 	} else {
5020Sstevel@tonic-gate 		/* force lba and set disk partition */
5030Sstevel@tonic-gate 		*((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1;
5040Sstevel@tonic-gate 		*((long *)(stage2_buffer + STAGE2_INSTALLPART))
5050Sstevel@tonic-gate 		    = (partition << 16) | (slice << 8) | 0xff;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/* modification done, now do the writing */
5090Sstevel@tonic-gate 	if (is_floppy || is_bootpar) {
5100Sstevel@tonic-gate 		/* we rewrite block 0 and 1 and that's it */
5110Sstevel@tonic-gate 		if (!nowrite &&
5120Sstevel@tonic-gate 		    (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
5130Sstevel@tonic-gate 		    stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE ||
5140Sstevel@tonic-gate 		    pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
5150Sstevel@tonic-gate 		    stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) {
5160Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2);
5170Sstevel@tonic-gate 			exit(-1);
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 		(void) fprintf(stdout, WRITE_STAGE2_PCFS);
5200Sstevel@tonic-gate 		return;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/* for disk, write stage2 starting at STAGE2_BLKOFF sector */
5240Sstevel@tonic-gate 	offset = STAGE2_BLKOFF;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/* write the modified first two sectors */
5270Sstevel@tonic-gate 	if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
5280Sstevel@tonic-gate 	    offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) {
5290Sstevel@tonic-gate 		(void) fprintf(stderr, WRITE_FAIL_STAGE2);
5300Sstevel@tonic-gate 		exit(-1);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/* write the remaining sectors */
5340Sstevel@tonic-gate 	nrecord = 2;
5350Sstevel@tonic-gate 	offset += 2;
5360Sstevel@tonic-gate 	for (;;) {
5370Sstevel@tonic-gate 		int nread, nwrite;
5380Sstevel@tonic-gate 		nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE,
5390Sstevel@tonic-gate 		    nrecord * SECTOR_SIZE);
5400Sstevel@tonic-gate 		if (nread > 0 && !nowrite)
5410Sstevel@tonic-gate 			nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
5420Sstevel@tonic-gate 			    offset * SECTOR_SIZE);
5430Sstevel@tonic-gate 		else
5440Sstevel@tonic-gate 			nwrite = SECTOR_SIZE;
5450Sstevel@tonic-gate 		if (nread < 0 || nwrite != SECTOR_SIZE) {
5460Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
5470Sstevel@tonic-gate 			    nread, nwrite);
5480Sstevel@tonic-gate 			break;
5490Sstevel@tonic-gate 		}
550322Sjongkis 		if (nread > 0) {
551322Sjongkis 			nrecord ++;
552322Sjongkis 			offset ++;
553322Sjongkis 		}
5540Sstevel@tonic-gate 		if (nread < SECTOR_SIZE)
5550Sstevel@tonic-gate 			break;	/* end of file */
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	(void) fprintf(stdout, WRITE_STAGE2_DISK,
5580Sstevel@tonic-gate 	    partition, nrecord, STAGE2_BLKOFF, stage2_first_sector);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate static char *
5620Sstevel@tonic-gate get_raw_partition(char *device)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	int len;
5650Sstevel@tonic-gate 	struct mboot *mboot;
5660Sstevel@tonic-gate 	static char *raw = NULL;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (raw)
5690Sstevel@tonic-gate 		return (raw);
5700Sstevel@tonic-gate 	raw = strdup(device);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (is_floppy)
5730Sstevel@tonic-gate 		return (raw);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if (is_bootpar) {
5760Sstevel@tonic-gate 		int i;
5770Sstevel@tonic-gate 		char *end = strstr(raw, "p0:boot");
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		end[2] = 0;		/* chop off :boot */
5800Sstevel@tonic-gate 		read_boot_sect(raw);
5810Sstevel@tonic-gate 		mboot = (struct mboot *)boot_sect;
5820Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
5830Sstevel@tonic-gate 			struct ipart *part = (struct ipart *)mboot->parts + i;
5840Sstevel@tonic-gate 			if (part->systid == 0xbe)	/* solaris boot part */
5850Sstevel@tonic-gate 				break;
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		if (i == FD_NUMPART) {
5890Sstevel@tonic-gate 			(void) fprintf(stderr, BOOTPAR_NOTFOUND, device);
5900Sstevel@tonic-gate 			exit(-1);
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 		end[1] = '1' + i;	/* set partition name */
5930Sstevel@tonic-gate 		return (raw);
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	/* For disk, remember slice and return whole fdisk partition  */
5970Sstevel@tonic-gate 	len = strlen(raw);
5980Sstevel@tonic-gate 	if (raw[len - 2] != 's' || raw[len - 1] == '2') {
5990Sstevel@tonic-gate 		(void) fprintf(stderr, NOT_ROOT_SLICE);
6000Sstevel@tonic-gate 		exit(-1);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 	slice = atoi(&raw[len - 1]);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	raw[len - 2] = 's';
6050Sstevel@tonic-gate 	raw[len - 1] = '2';
6060Sstevel@tonic-gate 	return (raw);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate #define	TMP_MNTPT	"/tmp/installgrub_pcfs"
6100Sstevel@tonic-gate static void
6110Sstevel@tonic-gate copy_stage2(int dev_fd, char *device)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	FILE *mntfp;
6140Sstevel@tonic-gate 	int i, pcfs_fp;
6150Sstevel@tonic-gate 	char buf[SECTOR_SIZE];
6160Sstevel@tonic-gate 	char *cp;
6170Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/* convert raw to block device name by removing the first 'r' */
6200Sstevel@tonic-gate 	(void) strncpy(buf, device, sizeof (buf));
6210Sstevel@tonic-gate 	buf[sizeof (buf) - 1] = 0;
6220Sstevel@tonic-gate 	cp = strchr(buf, 'r');
6230Sstevel@tonic-gate 	if (cp == NULL) {
6240Sstevel@tonic-gate 		(void) fprintf(stderr, CONVERT_FAIL, device);
6250Sstevel@tonic-gate 		exit(-1);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	do {
6280Sstevel@tonic-gate 		*cp = *(cp + 1);
6290Sstevel@tonic-gate 	} while (*(++cp));
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/* get the mount point, if any */
6320Sstevel@tonic-gate 	mntfp = fopen("/etc/mnttab", "r");
6330Sstevel@tonic-gate 	if (mntfp == NULL) {
6340Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab");
6350Sstevel@tonic-gate 		exit(-1);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	mpref.mnt_special = buf;
6390Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
6400Sstevel@tonic-gate 		char cmd[128];
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		/* not mounted, try remount */
6430Sstevel@tonic-gate 		(void) mkdir(TMP_MNTPT, S_IRWXU);
6440Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s",
6450Sstevel@tonic-gate 		    buf, TMP_MNTPT);
6460Sstevel@tonic-gate 		(void) system(cmd);
6470Sstevel@tonic-gate 		rewind(mntfp);
6480Sstevel@tonic-gate 		bzero(&mp, sizeof (mp));
6490Sstevel@tonic-gate 		if (getmntany(mntfp, &mp, &mpref) != 0) {
6500Sstevel@tonic-gate 			(void) fprintf(stderr, MOUNT_FAIL, buf);
6510Sstevel@tonic-gate 			exit(-1);
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
6560Sstevel@tonic-gate 	    "%s/boot", mp.mnt_mountp);
6570Sstevel@tonic-gate 	(void) mkdir(buf, S_IRWXU);
6580Sstevel@tonic-gate 	(void) strcat(buf, "/grub");
6590Sstevel@tonic-gate 	(void) mkdir(buf, S_IRWXU);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	(void) strcat(buf, "/stage2");
6620Sstevel@tonic-gate 	pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU);
6630Sstevel@tonic-gate 	if (pcfs_fp == -1) {
6640Sstevel@tonic-gate 		(void) fprintf(stderr, OPEN_FAIL_FILE, buf);
6650Sstevel@tonic-gate 		perror("open:");
6660Sstevel@tonic-gate 		(void) umount(TMP_MNTPT);
6670Sstevel@tonic-gate 		exit(-1);
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/* write stage2 to pcfs */
6710Sstevel@tonic-gate 	for (i = 0; ; i++) {
6720Sstevel@tonic-gate 		int nread, nwrite;
6730Sstevel@tonic-gate 		nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE);
6740Sstevel@tonic-gate 		if (nowrite)
6750Sstevel@tonic-gate 			nwrite = nread;
6760Sstevel@tonic-gate 		else
6770Sstevel@tonic-gate 			nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE);
6780Sstevel@tonic-gate 		if (nread < 0 || nwrite != nread) {
6790Sstevel@tonic-gate 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
6800Sstevel@tonic-gate 			    nread, nwrite);
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 		if (nread < SECTOR_SIZE)
6840Sstevel@tonic-gate 			break;	/* end of file */
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 	(void) close(pcfs_fp);
6870Sstevel@tonic-gate 	(void) umount(TMP_MNTPT);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * Now, get the blocklist from the device.
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	bzero(blocklist, sizeof (blocklist));
6930Sstevel@tonic-gate 	if (read_stage2_blocklist(dev_fd, blocklist) != 0)
6940Sstevel@tonic-gate 		exit(-1);
6950Sstevel@tonic-gate }
696