1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdio.h> 30*0Sstevel@tonic-gate #include <stdlib.h> 31*0Sstevel@tonic-gate #include <libgen.h> 32*0Sstevel@tonic-gate #include <malloc.h> 33*0Sstevel@tonic-gate #include <string.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/stat.h> 36*0Sstevel@tonic-gate #include <fcntl.h> 37*0Sstevel@tonic-gate #include <unistd.h> 38*0Sstevel@tonic-gate #include <strings.h> 39*0Sstevel@tonic-gate #include <sys/mount.h> 40*0Sstevel@tonic-gate #include <sys/mnttab.h> 41*0Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include <libintl.h> 44*0Sstevel@tonic-gate #include <locale.h> 45*0Sstevel@tonic-gate #include "message.h" 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate #ifndef TEXT_DOMAIN 48*0Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 49*0Sstevel@tonic-gate #endif 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate #define SECTOR_SIZE 0x200 52*0Sstevel@tonic-gate #define STAGE2_MEMADDR 0x8000 /* loading addr of stage2 */ 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate #define STAGE1_BPB_OFFSET 0x3 55*0Sstevel@tonic-gate #define STAGE1_BPB_SIZE 0x3B 56*0Sstevel@tonic-gate #define STAGE1_BOOT_DRIVE 0x40 57*0Sstevel@tonic-gate #define STAGE1_FORCE_LBA 0x41 58*0Sstevel@tonic-gate #define STAGE1_STAGE2_ADDRESS 0x42 59*0Sstevel@tonic-gate #define STAGE1_STAGE2_SECTOR 0x44 60*0Sstevel@tonic-gate #define STAGE1_STAGE2_SEGMENT 0x48 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate #define STAGE2_BLOCKLIST (SECTOR_SIZE - 0x8) 63*0Sstevel@tonic-gate #define STAGE2_INSTALLPART (SECTOR_SIZE + 0x8) 64*0Sstevel@tonic-gate #define STAGE2_FORCE_LBA (SECTOR_SIZE + 0x11) 65*0Sstevel@tonic-gate #define STAGE2_VER_STRING (SECTOR_SIZE + 0x12) 66*0Sstevel@tonic-gate #define STAGE2_BLKOFF 50 /* offset from start of fdisk part */ 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate static int nowrite = 0; 69*0Sstevel@tonic-gate static int write_mboot = 0; 70*0Sstevel@tonic-gate static int force_mboot = 0; 71*0Sstevel@tonic-gate static int is_floppy = 0; 72*0Sstevel@tonic-gate static int is_bootpar = 0; 73*0Sstevel@tonic-gate static int stage2_fd; 74*0Sstevel@tonic-gate static int partition, slice = 0xff; 75*0Sstevel@tonic-gate static int stage2_first_sector, stage2_second_sector; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE]; 79*0Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE]; 80*0Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE]; 81*0Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE]; 82*0Sstevel@tonic-gate static int blocklist[SECTOR_SIZE / sizeof (int)]; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate static int open_device(char *); 85*0Sstevel@tonic-gate static void read_bpb_sect(int); 86*0Sstevel@tonic-gate static void read_boot_sect(char *); 87*0Sstevel@tonic-gate static void write_boot_sect(char *); 88*0Sstevel@tonic-gate static void read_stage1_stage2(char *, char *); 89*0Sstevel@tonic-gate static void modify_and_write_stage1(int); 90*0Sstevel@tonic-gate static void modify_and_write_stage2(int); 91*0Sstevel@tonic-gate static int get_start_sector(); 92*0Sstevel@tonic-gate static void copy_stage2(int, char *); 93*0Sstevel@tonic-gate static char *get_raw_partition(char *); 94*0Sstevel@tonic-gate static void usage(char *); 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate extern int read_stage2_blocklist(int, int *); 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate int 99*0Sstevel@tonic-gate main(int argc, char *argv[]) 100*0Sstevel@tonic-gate { 101*0Sstevel@tonic-gate int dev_fd, opt; 102*0Sstevel@tonic-gate char *stage1, *stage2, *device; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 105*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate while ((opt = getopt(argc, argv, "fmn")) != EOF) { 108*0Sstevel@tonic-gate switch (opt) { 109*0Sstevel@tonic-gate case 'm': 110*0Sstevel@tonic-gate write_mboot = 1; 111*0Sstevel@tonic-gate break; 112*0Sstevel@tonic-gate case 'n': 113*0Sstevel@tonic-gate nowrite = 1; 114*0Sstevel@tonic-gate break; 115*0Sstevel@tonic-gate case 'f': 116*0Sstevel@tonic-gate force_mboot = 1; 117*0Sstevel@tonic-gate break; 118*0Sstevel@tonic-gate default: 119*0Sstevel@tonic-gate /* fall through to process non-optional args */ 120*0Sstevel@tonic-gate break; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate /* check arguments */ 125*0Sstevel@tonic-gate if (argc != optind + 3) { 126*0Sstevel@tonic-gate usage(argv[0]); 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate if (nowrite) { 130*0Sstevel@tonic-gate (void) fprintf(stdout, DRY_RUN); 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate stage1 = strdup(argv[optind]); 134*0Sstevel@tonic-gate stage2 = strdup(argv[optind + 1]); 135*0Sstevel@tonic-gate device = strdup(argv[optind + 2]); 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate if (!stage1 || !stage2 || !device) { 138*0Sstevel@tonic-gate usage(argv[0]); 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate /* open and check device type */ 142*0Sstevel@tonic-gate dev_fd = open_device(device); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /* read in stage1 and stage2 into buffer */ 145*0Sstevel@tonic-gate read_stage1_stage2(stage1, stage2); 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate /* In the pcfs case, write a fresh stage2 */ 148*0Sstevel@tonic-gate if (is_floppy || is_bootpar) { 149*0Sstevel@tonic-gate copy_stage2(dev_fd, device); 150*0Sstevel@tonic-gate read_bpb_sect(dev_fd); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate /* read in boot sector */ 154*0Sstevel@tonic-gate if (!is_floppy) 155*0Sstevel@tonic-gate read_boot_sect(device); 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate /* modify stage1 based on grub needs */ 158*0Sstevel@tonic-gate modify_and_write_stage1(dev_fd); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate /* modify stage2 and write to media */ 161*0Sstevel@tonic-gate modify_and_write_stage2(dev_fd); 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate if (!is_floppy && write_mboot) 164*0Sstevel@tonic-gate write_boot_sect(device); 165*0Sstevel@tonic-gate (void) close(dev_fd); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate return (0); 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate static int 171*0Sstevel@tonic-gate get_start_sector() 172*0Sstevel@tonic-gate { 173*0Sstevel@tonic-gate static int start_sect = 0; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate int i; 176*0Sstevel@tonic-gate struct mboot *mboot; 177*0Sstevel@tonic-gate struct ipart *part; 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate if (start_sect) 180*0Sstevel@tonic-gate return (start_sect); 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 183*0Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 184*0Sstevel@tonic-gate part = (struct ipart *)mboot->parts + i; 185*0Sstevel@tonic-gate if (is_bootpar) { 186*0Sstevel@tonic-gate if (part->systid == 0xbe) 187*0Sstevel@tonic-gate break; 188*0Sstevel@tonic-gate } else { 189*0Sstevel@tonic-gate if (part->systid == 0x82 || part->systid == 0xbf) 190*0Sstevel@tonic-gate break; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate if (i == FD_NUMPART) { 195*0Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR); 196*0Sstevel@tonic-gate exit(-1); 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate /* get confirmation for -m */ 200*0Sstevel@tonic-gate if (write_mboot && !force_mboot) { 201*0Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 202*0Sstevel@tonic-gate if (getchar() != 'y') { 203*0Sstevel@tonic-gate write_mboot = 0; 204*0Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate start_sect = part->relsect; 209*0Sstevel@tonic-gate if (part->bootid != 128 && write_mboot == 0) { 210*0Sstevel@tonic-gate (void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate partition = i; 214*0Sstevel@tonic-gate return (start_sect); 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate static void 218*0Sstevel@tonic-gate usage(char *progname) 219*0Sstevel@tonic-gate { 220*0Sstevel@tonic-gate (void) fprintf(stderr, USAGE, basename(progname)); 221*0Sstevel@tonic-gate exit(-1); 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate static int 225*0Sstevel@tonic-gate open_device(char *device) 226*0Sstevel@tonic-gate { 227*0Sstevel@tonic-gate int dev_fd; 228*0Sstevel@tonic-gate struct stat stat; 229*0Sstevel@tonic-gate char *raw_part; 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 232*0Sstevel@tonic-gate strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate /* handle boot partition specification */ 235*0Sstevel@tonic-gate if (!is_floppy && strstr(device, "p0:boot")) { 236*0Sstevel@tonic-gate is_bootpar = 1; 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate raw_part = get_raw_partition(device); 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate if (nowrite) 242*0Sstevel@tonic-gate dev_fd = open(raw_part, O_RDONLY); 243*0Sstevel@tonic-gate else 244*0Sstevel@tonic-gate dev_fd = open(raw_part, O_RDWR); 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 247*0Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw_part); 248*0Sstevel@tonic-gate exit(-1); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate if (S_ISCHR(stat.st_mode) == 0) { 251*0Sstevel@tonic-gate (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 252*0Sstevel@tonic-gate exit(-1); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate return (dev_fd); 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate static void 259*0Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2) 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate int fd; 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate /* read the stage1 file from filesystem */ 264*0Sstevel@tonic-gate fd = open(stage1, O_RDONLY); 265*0Sstevel@tonic-gate if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 266*0Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 267*0Sstevel@tonic-gate exit(-1); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate (void) close(fd); 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate /* read first two blocks of stage 2 from filesystem */ 272*0Sstevel@tonic-gate stage2_fd = open(stage2, O_RDONLY); 273*0Sstevel@tonic-gate if (stage2_fd == -1 || 274*0Sstevel@tonic-gate read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 275*0Sstevel@tonic-gate != 2 * SECTOR_SIZE) { 276*0Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 277*0Sstevel@tonic-gate exit(-1); 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate /* leave the stage2 file open for later */ 280*0Sstevel@tonic-gate } 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate static void 283*0Sstevel@tonic-gate read_bpb_sect(int dev_fd) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 286*0Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_BPB); 287*0Sstevel@tonic-gate exit(-1); 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate static void 292*0Sstevel@tonic-gate read_boot_sect(char *device) 293*0Sstevel@tonic-gate { 294*0Sstevel@tonic-gate static int read_mbr = 0; 295*0Sstevel@tonic-gate int i, fd; 296*0Sstevel@tonic-gate char save[2]; 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate if (read_mbr) 299*0Sstevel@tonic-gate return; 300*0Sstevel@tonic-gate read_mbr = 1; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate /* get the whole disk (p0) */ 303*0Sstevel@tonic-gate i = strlen(device); 304*0Sstevel@tonic-gate save[0] = device[i - 2]; 305*0Sstevel@tonic-gate save[1] = device[i - 1]; 306*0Sstevel@tonic-gate device[i - 2] = 'p'; 307*0Sstevel@tonic-gate device[i - 1] = '0'; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate fd = open(device, O_RDONLY); 310*0Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 311*0Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_MBR, device); 312*0Sstevel@tonic-gate if (fd == -1) 313*0Sstevel@tonic-gate perror("open"); 314*0Sstevel@tonic-gate else 315*0Sstevel@tonic-gate perror("read"); 316*0Sstevel@tonic-gate exit(-1); 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate (void) close(fd); 319*0Sstevel@tonic-gate device[i - 2] = save[0]; 320*0Sstevel@tonic-gate device[i - 1] = save[1]; 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate static void 324*0Sstevel@tonic-gate write_boot_sect(char *device) 325*0Sstevel@tonic-gate { 326*0Sstevel@tonic-gate int fd, len; 327*0Sstevel@tonic-gate char *raw, *end; 328*0Sstevel@tonic-gate struct stat stat; 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate /* make a copy and chop off ":boot" */ 331*0Sstevel@tonic-gate raw = strdup(device); 332*0Sstevel@tonic-gate end = strstr(raw, "p0:boot"); 333*0Sstevel@tonic-gate if (end) 334*0Sstevel@tonic-gate end[2] = 0; 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate /* open p0 (whole disk) */ 337*0Sstevel@tonic-gate len = strlen(raw); 338*0Sstevel@tonic-gate raw[len - 2] = 'p'; 339*0Sstevel@tonic-gate raw[len - 1] = '0'; 340*0Sstevel@tonic-gate fd = open(raw, O_WRONLY); 341*0Sstevel@tonic-gate if (fd == -1 || fstat(fd, &stat) != 0) { 342*0Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw); 343*0Sstevel@tonic-gate exit(-1); 344*0Sstevel@tonic-gate } 345*0Sstevel@tonic-gate if (!nowrite && 346*0Sstevel@tonic-gate pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 347*0Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 348*0Sstevel@tonic-gate exit(-1); 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate (void) fprintf(stdout, WRITE_MBOOT); 351*0Sstevel@tonic-gate (void) close(fd); 352*0Sstevel@tonic-gate } 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gate static void 355*0Sstevel@tonic-gate modify_and_write_stage1(int dev_fd) 356*0Sstevel@tonic-gate { 357*0Sstevel@tonic-gate if (is_floppy) { 358*0Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 359*0Sstevel@tonic-gate /* copy bios parameter block (for fat fs) */ 360*0Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 361*0Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 362*0Sstevel@tonic-gate } else if (is_bootpar) { 363*0Sstevel@tonic-gate stage2_first_sector = get_start_sector() + blocklist[0]; 364*0Sstevel@tonic-gate /* copy bios parameter block (for fat fs) and MBR */ 365*0Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 366*0Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 367*0Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 368*0Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 369*0Sstevel@tonic-gate } else { 370*0Sstevel@tonic-gate stage2_first_sector = get_start_sector() + STAGE2_BLKOFF; 371*0Sstevel@tonic-gate /* copy MBR to stage1 in case of overwriting MBR sector */ 372*0Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 373*0Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate /* modify default stage1 file generated by GRUB */ 377*0Sstevel@tonic-gate *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 378*0Sstevel@tonic-gate = stage2_first_sector; 379*0Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 380*0Sstevel@tonic-gate = STAGE2_MEMADDR; 381*0Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 382*0Sstevel@tonic-gate = STAGE2_MEMADDR >> 4; 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate /* 385*0Sstevel@tonic-gate * XXX the default grub distribution also: 386*0Sstevel@tonic-gate * - Copy the possible MBR/extended part table 387*0Sstevel@tonic-gate * - Set the boot drive of stage1 388*0Sstevel@tonic-gate */ 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate /* write stage1/pboot to 1st sector */ 391*0Sstevel@tonic-gate if (!nowrite && 392*0Sstevel@tonic-gate pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 393*0Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_PBOOT); 394*0Sstevel@tonic-gate exit(-1); 395*0Sstevel@tonic-gate } 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate if (is_floppy) { 398*0Sstevel@tonic-gate (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 399*0Sstevel@tonic-gate } else { 400*0Sstevel@tonic-gate (void) fprintf(stdout, WRITE_PBOOT, 401*0Sstevel@tonic-gate partition, get_start_sector()); 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate } 404*0Sstevel@tonic-gate 405*0Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 406*0Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 407*0Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate static void 410*0Sstevel@tonic-gate modify_and_write_stage2(int dev_fd) 411*0Sstevel@tonic-gate { 412*0Sstevel@tonic-gate int nrecord; 413*0Sstevel@tonic-gate off_t offset; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if (is_floppy || is_bootpar) { 416*0Sstevel@tonic-gate int i = 0; 417*0Sstevel@tonic-gate uint_t partition_offset; 418*0Sstevel@tonic-gate uint_t install_addr = 0x8200; 419*0Sstevel@tonic-gate uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate /* figure out the second sector */ 424*0Sstevel@tonic-gate if (blocklist[1] > 1) { 425*0Sstevel@tonic-gate blocklist[0]++; 426*0Sstevel@tonic-gate blocklist[1]--; 427*0Sstevel@tonic-gate } else { 428*0Sstevel@tonic-gate i += 2; 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate stage2_second_sector = blocklist[i]; 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate if (is_floppy) 433*0Sstevel@tonic-gate partition_offset = 0; 434*0Sstevel@tonic-gate else /* solaris boot partition */ 435*0Sstevel@tonic-gate partition_offset = get_start_sector(); 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate /* install the blocklist at the end of stage2_buffer */ 438*0Sstevel@tonic-gate while (blocklist[i]) { 439*0Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 440*0Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 441*0Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 442*0Sstevel@tonic-gate exit(-1); 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate START_BLOCK(pos) = blocklist[i] + partition_offset; 445*0Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 446*0Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 447*0Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 448*0Sstevel@tonic-gate pos -= 8; 449*0Sstevel@tonic-gate i += 2; 450*0Sstevel@tonic-gate } 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate } else { 453*0Sstevel@tonic-gate /* 454*0Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 455*0Sstevel@tonic-gate * blocks. So we update the starting block only. 456*0Sstevel@tonic-gate */ 457*0Sstevel@tonic-gate *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 458*0Sstevel@tonic-gate stage2_first_sector + 1; 459*0Sstevel@tonic-gate } 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate if (is_floppy) { 462*0Sstevel@tonic-gate /* modify the config file to add (fd0) */ 463*0Sstevel@tonic-gate char *config_file = stage2_buffer + STAGE2_VER_STRING; 464*0Sstevel@tonic-gate while (*config_file++) 465*0Sstevel@tonic-gate ; 466*0Sstevel@tonic-gate strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 467*0Sstevel@tonic-gate } else { 468*0Sstevel@tonic-gate /* force lba and set disk partition */ 469*0Sstevel@tonic-gate *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 470*0Sstevel@tonic-gate *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 471*0Sstevel@tonic-gate = (partition << 16) | (slice << 8) | 0xff; 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate /* modification done, now do the writing */ 475*0Sstevel@tonic-gate if (is_floppy || is_bootpar) { 476*0Sstevel@tonic-gate /* we rewrite block 0 and 1 and that's it */ 477*0Sstevel@tonic-gate if (!nowrite && 478*0Sstevel@tonic-gate (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 479*0Sstevel@tonic-gate stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 480*0Sstevel@tonic-gate pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 481*0Sstevel@tonic-gate stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 482*0Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 483*0Sstevel@tonic-gate exit(-1); 484*0Sstevel@tonic-gate } 485*0Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_PCFS); 486*0Sstevel@tonic-gate return; 487*0Sstevel@tonic-gate } 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 490*0Sstevel@tonic-gate offset = STAGE2_BLKOFF; 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate /* write the modified first two sectors */ 493*0Sstevel@tonic-gate if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 494*0Sstevel@tonic-gate offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 495*0Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 496*0Sstevel@tonic-gate exit(-1); 497*0Sstevel@tonic-gate } 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate /* write the remaining sectors */ 500*0Sstevel@tonic-gate nrecord = 2; 501*0Sstevel@tonic-gate offset += 2; 502*0Sstevel@tonic-gate for (;;) { 503*0Sstevel@tonic-gate int nread, nwrite; 504*0Sstevel@tonic-gate nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 505*0Sstevel@tonic-gate nrecord * SECTOR_SIZE); 506*0Sstevel@tonic-gate if (nread > 0 && !nowrite) 507*0Sstevel@tonic-gate nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 508*0Sstevel@tonic-gate offset * SECTOR_SIZE); 509*0Sstevel@tonic-gate else 510*0Sstevel@tonic-gate nwrite = SECTOR_SIZE; 511*0Sstevel@tonic-gate if (nread < 0 || nwrite != SECTOR_SIZE) { 512*0Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 513*0Sstevel@tonic-gate nread, nwrite); 514*0Sstevel@tonic-gate break; 515*0Sstevel@tonic-gate } 516*0Sstevel@tonic-gate nrecord ++; 517*0Sstevel@tonic-gate offset ++; 518*0Sstevel@tonic-gate if (nread < SECTOR_SIZE) 519*0Sstevel@tonic-gate break; /* end of file */ 520*0Sstevel@tonic-gate } 521*0Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_DISK, 522*0Sstevel@tonic-gate partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate static char * 526*0Sstevel@tonic-gate get_raw_partition(char *device) 527*0Sstevel@tonic-gate { 528*0Sstevel@tonic-gate int len; 529*0Sstevel@tonic-gate struct mboot *mboot; 530*0Sstevel@tonic-gate static char *raw = NULL; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate if (raw) 533*0Sstevel@tonic-gate return (raw); 534*0Sstevel@tonic-gate raw = strdup(device); 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate if (is_floppy) 537*0Sstevel@tonic-gate return (raw); 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate if (is_bootpar) { 540*0Sstevel@tonic-gate int i; 541*0Sstevel@tonic-gate char *end = strstr(raw, "p0:boot"); 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate end[2] = 0; /* chop off :boot */ 544*0Sstevel@tonic-gate read_boot_sect(raw); 545*0Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 546*0Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 547*0Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i; 548*0Sstevel@tonic-gate if (part->systid == 0xbe) /* solaris boot part */ 549*0Sstevel@tonic-gate break; 550*0Sstevel@tonic-gate } 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gate if (i == FD_NUMPART) { 553*0Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 554*0Sstevel@tonic-gate exit(-1); 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate end[1] = '1' + i; /* set partition name */ 557*0Sstevel@tonic-gate return (raw); 558*0Sstevel@tonic-gate } 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 561*0Sstevel@tonic-gate len = strlen(raw); 562*0Sstevel@tonic-gate if (raw[len - 2] != 's' || raw[len - 1] == '2') { 563*0Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 564*0Sstevel@tonic-gate exit(-1); 565*0Sstevel@tonic-gate } 566*0Sstevel@tonic-gate slice = atoi(&raw[len - 1]); 567*0Sstevel@tonic-gate 568*0Sstevel@tonic-gate raw[len - 2] = 's'; 569*0Sstevel@tonic-gate raw[len - 1] = '2'; 570*0Sstevel@tonic-gate return (raw); 571*0Sstevel@tonic-gate } 572*0Sstevel@tonic-gate 573*0Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 574*0Sstevel@tonic-gate static void 575*0Sstevel@tonic-gate copy_stage2(int dev_fd, char *device) 576*0Sstevel@tonic-gate { 577*0Sstevel@tonic-gate FILE *mntfp; 578*0Sstevel@tonic-gate int i, pcfs_fp; 579*0Sstevel@tonic-gate char buf[SECTOR_SIZE]; 580*0Sstevel@tonic-gate char *cp; 581*0Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 582*0Sstevel@tonic-gate 583*0Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 584*0Sstevel@tonic-gate (void) strncpy(buf, device, sizeof (buf)); 585*0Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 586*0Sstevel@tonic-gate cp = strchr(buf, 'r'); 587*0Sstevel@tonic-gate if (cp == NULL) { 588*0Sstevel@tonic-gate (void) fprintf(stderr, CONVERT_FAIL, device); 589*0Sstevel@tonic-gate exit(-1); 590*0Sstevel@tonic-gate } 591*0Sstevel@tonic-gate do { 592*0Sstevel@tonic-gate *cp = *(cp + 1); 593*0Sstevel@tonic-gate } while (*(++cp)); 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gate /* get the mount point, if any */ 596*0Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 597*0Sstevel@tonic-gate if (mntfp == NULL) { 598*0Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 599*0Sstevel@tonic-gate exit(-1); 600*0Sstevel@tonic-gate } 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate mpref.mnt_special = buf; 603*0Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 604*0Sstevel@tonic-gate char cmd[128]; 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate /* not mounted, try remount */ 607*0Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 608*0Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 609*0Sstevel@tonic-gate buf, TMP_MNTPT); 610*0Sstevel@tonic-gate (void) system(cmd); 611*0Sstevel@tonic-gate rewind(mntfp); 612*0Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 613*0Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 614*0Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 615*0Sstevel@tonic-gate exit(-1); 616*0Sstevel@tonic-gate } 617*0Sstevel@tonic-gate } 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 620*0Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 621*0Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 622*0Sstevel@tonic-gate (void) strcat(buf, "/grub"); 623*0Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 624*0Sstevel@tonic-gate 625*0Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 626*0Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 627*0Sstevel@tonic-gate if (pcfs_fp == -1) { 628*0Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 629*0Sstevel@tonic-gate perror("open:"); 630*0Sstevel@tonic-gate (void) umount(TMP_MNTPT); 631*0Sstevel@tonic-gate exit(-1); 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gate /* write stage2 to pcfs */ 635*0Sstevel@tonic-gate for (i = 0; ; i++) { 636*0Sstevel@tonic-gate int nread, nwrite; 637*0Sstevel@tonic-gate nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 638*0Sstevel@tonic-gate if (nowrite) 639*0Sstevel@tonic-gate nwrite = nread; 640*0Sstevel@tonic-gate else 641*0Sstevel@tonic-gate nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 642*0Sstevel@tonic-gate if (nread < 0 || nwrite != nread) { 643*0Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 644*0Sstevel@tonic-gate nread, nwrite); 645*0Sstevel@tonic-gate break; 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate if (nread < SECTOR_SIZE) 648*0Sstevel@tonic-gate break; /* end of file */ 649*0Sstevel@tonic-gate } 650*0Sstevel@tonic-gate (void) close(pcfs_fp); 651*0Sstevel@tonic-gate (void) umount(TMP_MNTPT); 652*0Sstevel@tonic-gate 653*0Sstevel@tonic-gate /* 654*0Sstevel@tonic-gate * Now, get the blocklist from the device. 655*0Sstevel@tonic-gate */ 656*0Sstevel@tonic-gate bzero(blocklist, sizeof (blocklist)); 657*0Sstevel@tonic-gate if (read_stage2_blocklist(dev_fd, blocklist) != 0) 658*0Sstevel@tonic-gate exit(-1); 659*0Sstevel@tonic-gate } 660