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 5*5589Ssy25831 * Common Development and Distribution License (the "License"). 6*5589Ssy25831 * 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*5589Ssy25831 * Copyright 2007 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> 41*5589Ssy25831 #include <sys/dkio.h> 42*5589Ssy25831 #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); 93*5589Ssy25831 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 173*5589Ssy25831 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; 190*5589Ssy25831 } 191*5589Ssy25831 } 192*5589Ssy25831 193*5589Ssy25831 /* 194*5589Ssy25831 * If there is no boot partition, find the solaris partition 195*5589Ssy25831 */ 196*5589Ssy25831 197*5589Ssy25831 if (i == FD_NUMPART) { 198*5589Ssy25831 struct part_info dkpi; 199*5589Ssy25831 200*5589Ssy25831 /* 201*5589Ssy25831 * Get the solaris partition information from the device 202*5589Ssy25831 * and compare the offset of S2 with offset of solaris partition 203*5589Ssy25831 * from fdisk partition table. 204*5589Ssy25831 */ 205*5589Ssy25831 if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) { 206*5589Ssy25831 (void) fprintf(stderr, PART_FAIL); 207*5589Ssy25831 exit(-1); 208*5589Ssy25831 } 209*5589Ssy25831 210*5589Ssy25831 for (i = 0; i < FD_NUMPART; i++) { 211*5589Ssy25831 part = (struct ipart *)mboot->parts + i; 212*5589Ssy25831 213*5589Ssy25831 if (part->relsect == 0) { 214*5589Ssy25831 (void) fprintf(stderr, BAD_PART, i); 215*5589Ssy25831 exit(-1); 216*5589Ssy25831 } 217*5589Ssy25831 if (part->relsect == dkpi.p_start) { 218*5589Ssy25831 /* Found the partition */ 2190Sstevel@tonic-gate break; 220*5589Ssy25831 } else if (part->relsect > dkpi.p_start) { 221*5589Ssy25831 /* 222*5589Ssy25831 * The next fdisk partition starts beyond 223*5589Ssy25831 * offset of Solaris partition. 224*5589Ssy25831 * So the previous partition is the right one. 225*5589Ssy25831 */ 226*5589Ssy25831 i--; 227*5589Ssy25831 part = (struct ipart *)mboot->parts + i; 228*5589Ssy25831 break; 229*5589Ssy25831 } 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if (i == FD_NUMPART) { 2340Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR); 2350Sstevel@tonic-gate exit(-1); 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /* get confirmation for -m */ 2390Sstevel@tonic-gate if (write_mboot && !force_mboot) { 2400Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 2410Sstevel@tonic-gate if (getchar() != 'y') { 2420Sstevel@tonic-gate write_mboot = 0; 2430Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate start_sect = part->relsect; 2480Sstevel@tonic-gate if (part->bootid != 128 && write_mboot == 0) { 2490Sstevel@tonic-gate (void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate partition = i; 2530Sstevel@tonic-gate return (start_sect); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate static void 2570Sstevel@tonic-gate usage(char *progname) 2580Sstevel@tonic-gate { 2590Sstevel@tonic-gate (void) fprintf(stderr, USAGE, basename(progname)); 2600Sstevel@tonic-gate exit(-1); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate static int 2640Sstevel@tonic-gate open_device(char *device) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate int dev_fd; 2670Sstevel@tonic-gate struct stat stat; 2680Sstevel@tonic-gate char *raw_part; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 2710Sstevel@tonic-gate strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* handle boot partition specification */ 2740Sstevel@tonic-gate if (!is_floppy && strstr(device, "p0:boot")) { 2750Sstevel@tonic-gate is_bootpar = 1; 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate raw_part = get_raw_partition(device); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate if (nowrite) 2810Sstevel@tonic-gate dev_fd = open(raw_part, O_RDONLY); 2820Sstevel@tonic-gate else 2830Sstevel@tonic-gate dev_fd = open(raw_part, O_RDWR); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 2860Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw_part); 2870Sstevel@tonic-gate exit(-1); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate if (S_ISCHR(stat.st_mode) == 0) { 2900Sstevel@tonic-gate (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 2910Sstevel@tonic-gate exit(-1); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate return (dev_fd); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate static void 2980Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2) 2990Sstevel@tonic-gate { 3000Sstevel@tonic-gate int fd; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate /* read the stage1 file from filesystem */ 3030Sstevel@tonic-gate fd = open(stage1, O_RDONLY); 3040Sstevel@tonic-gate if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 3050Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 3060Sstevel@tonic-gate exit(-1); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate (void) close(fd); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* read first two blocks of stage 2 from filesystem */ 3110Sstevel@tonic-gate stage2_fd = open(stage2, O_RDONLY); 3120Sstevel@tonic-gate if (stage2_fd == -1 || 3130Sstevel@tonic-gate read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 3140Sstevel@tonic-gate != 2 * SECTOR_SIZE) { 3150Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 3160Sstevel@tonic-gate exit(-1); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate /* leave the stage2 file open for later */ 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate static void 3220Sstevel@tonic-gate read_bpb_sect(int dev_fd) 3230Sstevel@tonic-gate { 3240Sstevel@tonic-gate if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 3250Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_BPB); 3260Sstevel@tonic-gate exit(-1); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate static void 3310Sstevel@tonic-gate read_boot_sect(char *device) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate static int read_mbr = 0; 3340Sstevel@tonic-gate int i, fd; 3350Sstevel@tonic-gate char save[2]; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate if (read_mbr) 3380Sstevel@tonic-gate return; 3390Sstevel@tonic-gate read_mbr = 1; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate /* get the whole disk (p0) */ 3420Sstevel@tonic-gate i = strlen(device); 3430Sstevel@tonic-gate save[0] = device[i - 2]; 3440Sstevel@tonic-gate save[1] = device[i - 1]; 3450Sstevel@tonic-gate device[i - 2] = 'p'; 3460Sstevel@tonic-gate device[i - 1] = '0'; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate fd = open(device, O_RDONLY); 3490Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 3500Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_MBR, device); 3510Sstevel@tonic-gate if (fd == -1) 3520Sstevel@tonic-gate perror("open"); 3530Sstevel@tonic-gate else 3540Sstevel@tonic-gate perror("read"); 3550Sstevel@tonic-gate exit(-1); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate (void) close(fd); 3580Sstevel@tonic-gate device[i - 2] = save[0]; 3590Sstevel@tonic-gate device[i - 1] = save[1]; 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate static void 3630Sstevel@tonic-gate write_boot_sect(char *device) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate int fd, len; 3660Sstevel@tonic-gate char *raw, *end; 3670Sstevel@tonic-gate struct stat stat; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* make a copy and chop off ":boot" */ 3700Sstevel@tonic-gate raw = strdup(device); 3710Sstevel@tonic-gate end = strstr(raw, "p0:boot"); 3720Sstevel@tonic-gate if (end) 3730Sstevel@tonic-gate end[2] = 0; 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate /* open p0 (whole disk) */ 3760Sstevel@tonic-gate len = strlen(raw); 3770Sstevel@tonic-gate raw[len - 2] = 'p'; 3780Sstevel@tonic-gate raw[len - 1] = '0'; 3790Sstevel@tonic-gate fd = open(raw, O_WRONLY); 3800Sstevel@tonic-gate if (fd == -1 || fstat(fd, &stat) != 0) { 3810Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw); 3820Sstevel@tonic-gate exit(-1); 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate if (!nowrite && 3850Sstevel@tonic-gate pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 3860Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 3870Sstevel@tonic-gate exit(-1); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate (void) fprintf(stdout, WRITE_MBOOT); 3900Sstevel@tonic-gate (void) close(fd); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate static void 3940Sstevel@tonic-gate modify_and_write_stage1(int dev_fd) 3950Sstevel@tonic-gate { 3960Sstevel@tonic-gate if (is_floppy) { 3970Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 3980Sstevel@tonic-gate /* copy bios parameter block (for fat fs) */ 3990Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 4000Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 4010Sstevel@tonic-gate } else if (is_bootpar) { 402*5589Ssy25831 stage2_first_sector = get_start_sector(dev_fd) + blocklist[0]; 4030Sstevel@tonic-gate /* copy bios parameter block (for fat fs) and MBR */ 4040Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 4050Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 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 } else { 409*5589Ssy25831 stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF; 4100Sstevel@tonic-gate /* copy MBR to stage1 in case of overwriting MBR sector */ 4110Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 4120Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate /* modify default stage1 file generated by GRUB */ 4160Sstevel@tonic-gate *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 417*5589Ssy25831 = stage2_first_sector; 4180Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 419*5589Ssy25831 = STAGE2_MEMADDR; 4200Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 421*5589Ssy25831 = STAGE2_MEMADDR >> 4; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * XXX the default grub distribution also: 4250Sstevel@tonic-gate * - Copy the possible MBR/extended part table 4260Sstevel@tonic-gate * - Set the boot drive of stage1 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate /* write stage1/pboot to 1st sector */ 4300Sstevel@tonic-gate if (!nowrite && 4310Sstevel@tonic-gate pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4320Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_PBOOT); 4330Sstevel@tonic-gate exit(-1); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate if (is_floppy) { 4370Sstevel@tonic-gate (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 4380Sstevel@tonic-gate } else { 4390Sstevel@tonic-gate (void) fprintf(stdout, WRITE_PBOOT, 440*5589Ssy25831 partition, get_start_sector(dev_fd)); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 4450Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 4460Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate static void 4490Sstevel@tonic-gate modify_and_write_stage2(int dev_fd) 4500Sstevel@tonic-gate { 4510Sstevel@tonic-gate int nrecord; 4520Sstevel@tonic-gate off_t offset; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate if (is_floppy || is_bootpar) { 4550Sstevel@tonic-gate int i = 0; 4560Sstevel@tonic-gate uint_t partition_offset; 4570Sstevel@tonic-gate uint_t install_addr = 0x8200; 4580Sstevel@tonic-gate uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate /* figure out the second sector */ 4630Sstevel@tonic-gate if (blocklist[1] > 1) { 4640Sstevel@tonic-gate blocklist[0]++; 4650Sstevel@tonic-gate blocklist[1]--; 4660Sstevel@tonic-gate } else { 4670Sstevel@tonic-gate i += 2; 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate stage2_second_sector = blocklist[i]; 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate if (is_floppy) 4720Sstevel@tonic-gate partition_offset = 0; 4730Sstevel@tonic-gate else /* solaris boot partition */ 474*5589Ssy25831 partition_offset = get_start_sector(dev_fd); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* install the blocklist at the end of stage2_buffer */ 4770Sstevel@tonic-gate while (blocklist[i]) { 4780Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 4790Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 4800Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 4810Sstevel@tonic-gate exit(-1); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate START_BLOCK(pos) = blocklist[i] + partition_offset; 4840Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 4850Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 4860Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 4870Sstevel@tonic-gate pos -= 8; 4880Sstevel@tonic-gate i += 2; 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate } else { 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 4940Sstevel@tonic-gate * blocks. So we update the starting block only. 4950Sstevel@tonic-gate */ 4960Sstevel@tonic-gate *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 4970Sstevel@tonic-gate stage2_first_sector + 1; 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate if (is_floppy) { 5010Sstevel@tonic-gate /* modify the config file to add (fd0) */ 5020Sstevel@tonic-gate char *config_file = stage2_buffer + STAGE2_VER_STRING; 5030Sstevel@tonic-gate while (*config_file++) 5040Sstevel@tonic-gate ; 5050Sstevel@tonic-gate strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 5060Sstevel@tonic-gate } else { 5070Sstevel@tonic-gate /* force lba and set disk partition */ 5080Sstevel@tonic-gate *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 5090Sstevel@tonic-gate *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 5100Sstevel@tonic-gate = (partition << 16) | (slice << 8) | 0xff; 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate /* modification done, now do the writing */ 5140Sstevel@tonic-gate if (is_floppy || is_bootpar) { 5150Sstevel@tonic-gate /* we rewrite block 0 and 1 and that's it */ 5160Sstevel@tonic-gate if (!nowrite && 5170Sstevel@tonic-gate (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 5180Sstevel@tonic-gate stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 5190Sstevel@tonic-gate pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 5200Sstevel@tonic-gate stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 5210Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 5220Sstevel@tonic-gate exit(-1); 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_PCFS); 5250Sstevel@tonic-gate return; 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 5290Sstevel@tonic-gate offset = STAGE2_BLKOFF; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* write the modified first two sectors */ 5320Sstevel@tonic-gate if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 5330Sstevel@tonic-gate offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 5340Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 5350Sstevel@tonic-gate exit(-1); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* write the remaining sectors */ 5390Sstevel@tonic-gate nrecord = 2; 5400Sstevel@tonic-gate offset += 2; 5410Sstevel@tonic-gate for (;;) { 5420Sstevel@tonic-gate int nread, nwrite; 5430Sstevel@tonic-gate nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 5440Sstevel@tonic-gate nrecord * SECTOR_SIZE); 5450Sstevel@tonic-gate if (nread > 0 && !nowrite) 5460Sstevel@tonic-gate nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 5470Sstevel@tonic-gate offset * SECTOR_SIZE); 5480Sstevel@tonic-gate else 5490Sstevel@tonic-gate nwrite = SECTOR_SIZE; 5500Sstevel@tonic-gate if (nread < 0 || nwrite != SECTOR_SIZE) { 5510Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 5520Sstevel@tonic-gate nread, nwrite); 5530Sstevel@tonic-gate break; 5540Sstevel@tonic-gate } 555322Sjongkis if (nread > 0) { 556322Sjongkis nrecord ++; 557322Sjongkis offset ++; 558322Sjongkis } 5590Sstevel@tonic-gate if (nread < SECTOR_SIZE) 5600Sstevel@tonic-gate break; /* end of file */ 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_DISK, 5630Sstevel@tonic-gate partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate static char * 5670Sstevel@tonic-gate get_raw_partition(char *device) 5680Sstevel@tonic-gate { 5690Sstevel@tonic-gate int len; 5700Sstevel@tonic-gate struct mboot *mboot; 5710Sstevel@tonic-gate static char *raw = NULL; 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if (raw) 5740Sstevel@tonic-gate return (raw); 5750Sstevel@tonic-gate raw = strdup(device); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate if (is_floppy) 5780Sstevel@tonic-gate return (raw); 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate if (is_bootpar) { 5810Sstevel@tonic-gate int i; 5820Sstevel@tonic-gate char *end = strstr(raw, "p0:boot"); 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate end[2] = 0; /* chop off :boot */ 5850Sstevel@tonic-gate read_boot_sect(raw); 5860Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 5870Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 5880Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i; 5890Sstevel@tonic-gate if (part->systid == 0xbe) /* solaris boot part */ 5900Sstevel@tonic-gate break; 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate if (i == FD_NUMPART) { 5940Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 5950Sstevel@tonic-gate exit(-1); 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate end[1] = '1' + i; /* set partition name */ 5980Sstevel@tonic-gate return (raw); 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 6020Sstevel@tonic-gate len = strlen(raw); 6030Sstevel@tonic-gate if (raw[len - 2] != 's' || raw[len - 1] == '2') { 6040Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 6050Sstevel@tonic-gate exit(-1); 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate slice = atoi(&raw[len - 1]); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate raw[len - 2] = 's'; 6100Sstevel@tonic-gate raw[len - 1] = '2'; 6110Sstevel@tonic-gate return (raw); 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 6150Sstevel@tonic-gate static void 6160Sstevel@tonic-gate copy_stage2(int dev_fd, char *device) 6170Sstevel@tonic-gate { 6180Sstevel@tonic-gate FILE *mntfp; 6190Sstevel@tonic-gate int i, pcfs_fp; 6200Sstevel@tonic-gate char buf[SECTOR_SIZE]; 6210Sstevel@tonic-gate char *cp; 6220Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 6250Sstevel@tonic-gate (void) strncpy(buf, device, sizeof (buf)); 6260Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 6270Sstevel@tonic-gate cp = strchr(buf, 'r'); 6280Sstevel@tonic-gate if (cp == NULL) { 6290Sstevel@tonic-gate (void) fprintf(stderr, CONVERT_FAIL, device); 6300Sstevel@tonic-gate exit(-1); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate do { 6330Sstevel@tonic-gate *cp = *(cp + 1); 6340Sstevel@tonic-gate } while (*(++cp)); 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate /* get the mount point, if any */ 6370Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 6380Sstevel@tonic-gate if (mntfp == NULL) { 6390Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 6400Sstevel@tonic-gate exit(-1); 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate mpref.mnt_special = buf; 6440Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 6450Sstevel@tonic-gate char cmd[128]; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* not mounted, try remount */ 6480Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 6490Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 6500Sstevel@tonic-gate buf, TMP_MNTPT); 6510Sstevel@tonic-gate (void) system(cmd); 6520Sstevel@tonic-gate rewind(mntfp); 6530Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 6540Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 6550Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 6560Sstevel@tonic-gate exit(-1); 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 6610Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 6620Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 6630Sstevel@tonic-gate (void) strcat(buf, "/grub"); 6640Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 6670Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 6680Sstevel@tonic-gate if (pcfs_fp == -1) { 6690Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 6700Sstevel@tonic-gate perror("open:"); 6710Sstevel@tonic-gate (void) umount(TMP_MNTPT); 6720Sstevel@tonic-gate exit(-1); 6730Sstevel@tonic-gate } 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate /* write stage2 to pcfs */ 6760Sstevel@tonic-gate for (i = 0; ; i++) { 6770Sstevel@tonic-gate int nread, nwrite; 6780Sstevel@tonic-gate nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 6790Sstevel@tonic-gate if (nowrite) 6800Sstevel@tonic-gate nwrite = nread; 6810Sstevel@tonic-gate else 6820Sstevel@tonic-gate nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 6830Sstevel@tonic-gate if (nread < 0 || nwrite != nread) { 6840Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 6850Sstevel@tonic-gate nread, nwrite); 6860Sstevel@tonic-gate break; 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate if (nread < SECTOR_SIZE) 6890Sstevel@tonic-gate break; /* end of file */ 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate (void) close(pcfs_fp); 6920Sstevel@tonic-gate (void) umount(TMP_MNTPT); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate /* 6950Sstevel@tonic-gate * Now, get the blocklist from the device. 6960Sstevel@tonic-gate */ 6970Sstevel@tonic-gate bzero(blocklist, sizeof (blocklist)); 6980Sstevel@tonic-gate if (read_stage2_blocklist(dev_fd, blocklist) != 0) 6990Sstevel@tonic-gate exit(-1); 7000Sstevel@tonic-gate } 701