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