17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d33344bbSsy25831 * Common Development and Distribution License (the "License"). 6d33344bbSsy25831 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22d46ed901Ssetje * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <stdio.h> 277c478bd9Sstevel@tonic-gate #include <stdlib.h> 287c478bd9Sstevel@tonic-gate #include <libgen.h> 297c478bd9Sstevel@tonic-gate #include <malloc.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/stat.h> 337c478bd9Sstevel@tonic-gate #include <fcntl.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <strings.h> 367c478bd9Sstevel@tonic-gate #include <sys/mount.h> 377c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 387c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 39d33344bbSsy25831 #include <sys/dkio.h> 40d33344bbSsy25831 #include <sys/vtoc.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <libintl.h> 437c478bd9Sstevel@tonic-gate #include <locale.h> 447c478bd9Sstevel@tonic-gate #include "message.h" 45c6fe1048Sjongkis #include <errno.h> 46*7ce76caaSEnrico Perla - Sun Microsystems #include <md5.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 497c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 507c478bd9Sstevel@tonic-gate #endif 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 0x200 53*7ce76caaSEnrico Perla - Sun Microsystems #define HASH_SIZE 0x10 54*7ce76caaSEnrico Perla - Sun Microsystems #define VERSION_SIZE 0x50 557c478bd9Sstevel@tonic-gate #define STAGE2_MEMADDR 0x8000 /* loading addr of stage2 */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define STAGE1_BPB_OFFSET 0x3 587c478bd9Sstevel@tonic-gate #define STAGE1_BPB_SIZE 0x3B 597c478bd9Sstevel@tonic-gate #define STAGE1_BOOT_DRIVE 0x40 607c478bd9Sstevel@tonic-gate #define STAGE1_FORCE_LBA 0x41 617c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_ADDRESS 0x42 627c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_SECTOR 0x44 637c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_SEGMENT 0x48 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #define STAGE2_BLOCKLIST (SECTOR_SIZE - 0x8) 667c478bd9Sstevel@tonic-gate #define STAGE2_INSTALLPART (SECTOR_SIZE + 0x8) 677c478bd9Sstevel@tonic-gate #define STAGE2_FORCE_LBA (SECTOR_SIZE + 0x11) 687c478bd9Sstevel@tonic-gate #define STAGE2_VER_STRING (SECTOR_SIZE + 0x12) 69*7ce76caaSEnrico Perla - Sun Microsystems #define STAGE2_SIGN_OFFSET (SECTOR_SIZE + 0x60) 70*7ce76caaSEnrico Perla - Sun Microsystems #define STAGE2_PKG_VERSION (SECTOR_SIZE + 0x70) 717c478bd9Sstevel@tonic-gate #define STAGE2_BLKOFF 50 /* offset from start of fdisk part */ 727c478bd9Sstevel@tonic-gate 73*7ce76caaSEnrico Perla - Sun Microsystems static char extended_sig[] = "\xCC\xCC\xCC\xCC\xAA\xAA\xAA\xAA\xBB\xBB\xBB\xBB" 74*7ce76caaSEnrico Perla - Sun Microsystems "\xBB\xBB\xBB\xBB"; 75*7ce76caaSEnrico Perla - Sun Microsystems 767c478bd9Sstevel@tonic-gate static int nowrite = 0; 777c478bd9Sstevel@tonic-gate static int write_mboot = 0; 787c478bd9Sstevel@tonic-gate static int force_mboot = 0; 79*7ce76caaSEnrico Perla - Sun Microsystems static int getinfo = 0; 80*7ce76caaSEnrico Perla - Sun Microsystems static int do_version = 0; 817c478bd9Sstevel@tonic-gate static int is_floppy = 0; 827c478bd9Sstevel@tonic-gate static int is_bootpar = 0; 83*7ce76caaSEnrico Perla - Sun Microsystems static int strip = 0; 847c478bd9Sstevel@tonic-gate static int stage2_fd; 857c478bd9Sstevel@tonic-gate static int partition, slice = 0xff; 86f85c7842SSuhasini Peddada static unsigned int stage2_first_sector, stage2_second_sector; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE]; 907c478bd9Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE]; 917c478bd9Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE]; 927c478bd9Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE]; 93*7ce76caaSEnrico Perla - Sun Microsystems static char signature[HASH_SIZE]; 94*7ce76caaSEnrico Perla - Sun Microsystems static char verstring[VERSION_SIZE]; 95342440ecSPrasad Singamsetty static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)]; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate static int open_device(char *); 987c478bd9Sstevel@tonic-gate static void read_bpb_sect(int); 997c478bd9Sstevel@tonic-gate static void read_boot_sect(char *); 1007c478bd9Sstevel@tonic-gate static void write_boot_sect(char *); 1017c478bd9Sstevel@tonic-gate static void read_stage1_stage2(char *, char *); 1027c478bd9Sstevel@tonic-gate static void modify_and_write_stage1(int); 1037c478bd9Sstevel@tonic-gate static void modify_and_write_stage2(int); 104342440ecSPrasad Singamsetty static unsigned int get_start_sector(int); 1057c478bd9Sstevel@tonic-gate static void copy_stage2(int, char *); 1067c478bd9Sstevel@tonic-gate static char *get_raw_partition(char *); 1077c478bd9Sstevel@tonic-gate static void usage(char *); 108*7ce76caaSEnrico Perla - Sun Microsystems static void print_info(); 109*7ce76caaSEnrico Perla - Sun Microsystems static int read_stage2_info(int); 110*7ce76caaSEnrico Perla - Sun Microsystems static void check_extended_support(); 1117c478bd9Sstevel@tonic-gate 112342440ecSPrasad Singamsetty extern int read_stage2_blocklist(int, unsigned int *); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate int 1157c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1167c478bd9Sstevel@tonic-gate { 117*7ce76caaSEnrico Perla - Sun Microsystems int dev_fd, opt, params = 3; 1187c478bd9Sstevel@tonic-gate char *stage1, *stage2, *device; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1217c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1227c478bd9Sstevel@tonic-gate 123*7ce76caaSEnrico Perla - Sun Microsystems while ((opt = getopt(argc, argv, "fmneis:")) != EOF) { 1247c478bd9Sstevel@tonic-gate switch (opt) { 1257c478bd9Sstevel@tonic-gate case 'm': 1267c478bd9Sstevel@tonic-gate write_mboot = 1; 1277c478bd9Sstevel@tonic-gate break; 1287c478bd9Sstevel@tonic-gate case 'n': 1297c478bd9Sstevel@tonic-gate nowrite = 1; 1307c478bd9Sstevel@tonic-gate break; 1317c478bd9Sstevel@tonic-gate case 'f': 1327c478bd9Sstevel@tonic-gate force_mboot = 1; 1337c478bd9Sstevel@tonic-gate break; 134*7ce76caaSEnrico Perla - Sun Microsystems case 'i': 135*7ce76caaSEnrico Perla - Sun Microsystems getinfo = 1; 136*7ce76caaSEnrico Perla - Sun Microsystems params = 1; 137*7ce76caaSEnrico Perla - Sun Microsystems break; 138*7ce76caaSEnrico Perla - Sun Microsystems case 'e': 139*7ce76caaSEnrico Perla - Sun Microsystems strip = 1; 140*7ce76caaSEnrico Perla - Sun Microsystems break; 141*7ce76caaSEnrico Perla - Sun Microsystems case 's': 142*7ce76caaSEnrico Perla - Sun Microsystems do_version = 1; 143*7ce76caaSEnrico Perla - Sun Microsystems (void) snprintf(verstring, sizeof (verstring), "%s", 144*7ce76caaSEnrico Perla - Sun Microsystems optarg); 145*7ce76caaSEnrico Perla - Sun Microsystems break; 1467c478bd9Sstevel@tonic-gate default: 1477c478bd9Sstevel@tonic-gate /* fall through to process non-optional args */ 1487c478bd9Sstevel@tonic-gate break; 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* check arguments */ 153*7ce76caaSEnrico Perla - Sun Microsystems if (argc != optind + params) { 1547c478bd9Sstevel@tonic-gate usage(argv[0]); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (nowrite) { 1587c478bd9Sstevel@tonic-gate (void) fprintf(stdout, DRY_RUN); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 161*7ce76caaSEnrico Perla - Sun Microsystems if (params == 1) { 162*7ce76caaSEnrico Perla - Sun Microsystems device = strdup(argv[optind]); 163*7ce76caaSEnrico Perla - Sun Microsystems if (!device) { 164*7ce76caaSEnrico Perla - Sun Microsystems usage(argv[0]); 165*7ce76caaSEnrico Perla - Sun Microsystems } 166*7ce76caaSEnrico Perla - Sun Microsystems } else if (params == 3) { 1677c478bd9Sstevel@tonic-gate stage1 = strdup(argv[optind]); 1687c478bd9Sstevel@tonic-gate stage2 = strdup(argv[optind + 1]); 1697c478bd9Sstevel@tonic-gate device = strdup(argv[optind + 2]); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (!stage1 || !stage2 || !device) { 1727c478bd9Sstevel@tonic-gate usage(argv[0]); 1737c478bd9Sstevel@tonic-gate } 174*7ce76caaSEnrico Perla - Sun Microsystems } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* open and check device type */ 1777c478bd9Sstevel@tonic-gate dev_fd = open_device(device); 1787c478bd9Sstevel@tonic-gate 179*7ce76caaSEnrico Perla - Sun Microsystems if (getinfo) { 180*7ce76caaSEnrico Perla - Sun Microsystems if (read_stage2_info(dev_fd) != 0) { 181*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stderr, "Unable to read extended information" 182*7ce76caaSEnrico Perla - Sun Microsystems " from %s\n", device); 183*7ce76caaSEnrico Perla - Sun Microsystems exit(1); 184*7ce76caaSEnrico Perla - Sun Microsystems } 185*7ce76caaSEnrico Perla - Sun Microsystems print_info(); 186*7ce76caaSEnrico Perla - Sun Microsystems (void) free(device); 187*7ce76caaSEnrico Perla - Sun Microsystems (void) close(dev_fd); 188*7ce76caaSEnrico Perla - Sun Microsystems return (0); 189*7ce76caaSEnrico Perla - Sun Microsystems } 190*7ce76caaSEnrico Perla - Sun Microsystems 1917c478bd9Sstevel@tonic-gate /* read in stage1 and stage2 into buffer */ 1927c478bd9Sstevel@tonic-gate read_stage1_stage2(stage1, stage2); 1937c478bd9Sstevel@tonic-gate 194*7ce76caaSEnrico Perla - Sun Microsystems /* check if stage2 supports extended versioning */ 195*7ce76caaSEnrico Perla - Sun Microsystems if (do_version) 196*7ce76caaSEnrico Perla - Sun Microsystems check_extended_support(stage2); 197*7ce76caaSEnrico Perla - Sun Microsystems 1987c478bd9Sstevel@tonic-gate /* In the pcfs case, write a fresh stage2 */ 1997c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 2007c478bd9Sstevel@tonic-gate copy_stage2(dev_fd, device); 2017c478bd9Sstevel@tonic-gate read_bpb_sect(dev_fd); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* read in boot sector */ 2057c478bd9Sstevel@tonic-gate if (!is_floppy) 2067c478bd9Sstevel@tonic-gate read_boot_sect(device); 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* modify stage1 based on grub needs */ 2097c478bd9Sstevel@tonic-gate modify_and_write_stage1(dev_fd); 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate /* modify stage2 and write to media */ 2127c478bd9Sstevel@tonic-gate modify_and_write_stage2(dev_fd); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if (!is_floppy && write_mboot) 2157c478bd9Sstevel@tonic-gate write_boot_sect(device); 216*7ce76caaSEnrico Perla - Sun Microsystems 2177c478bd9Sstevel@tonic-gate (void) close(dev_fd); 218*7ce76caaSEnrico Perla - Sun Microsystems free(device); 219*7ce76caaSEnrico Perla - Sun Microsystems free(stage1); 220*7ce76caaSEnrico Perla - Sun Microsystems free(stage2); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate return (0); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 225342440ecSPrasad Singamsetty static unsigned int 226d33344bbSsy25831 get_start_sector(int fd) 2277c478bd9Sstevel@tonic-gate { 228342440ecSPrasad Singamsetty static unsigned int start_sect = 0; 229f85c7842SSuhasini Peddada 230f85c7842SSuhasini Peddada int i; 2317c478bd9Sstevel@tonic-gate struct mboot *mboot; 2327c478bd9Sstevel@tonic-gate struct ipart *part; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (start_sect) 2357c478bd9Sstevel@tonic-gate return (start_sect); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 2387c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2397c478bd9Sstevel@tonic-gate part = (struct ipart *)mboot->parts + i; 2407c478bd9Sstevel@tonic-gate if (is_bootpar) { 2417c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) 2427c478bd9Sstevel@tonic-gate break; 243d33344bbSsy25831 } 244d33344bbSsy25831 } 245d33344bbSsy25831 246d33344bbSsy25831 /* 247d33344bbSsy25831 * If there is no boot partition, find the solaris partition 248d33344bbSsy25831 */ 249d33344bbSsy25831 250d33344bbSsy25831 if (i == FD_NUMPART) { 251d33344bbSsy25831 struct part_info dkpi; 252342440ecSPrasad Singamsetty struct extpart_info edkpi; 253d33344bbSsy25831 254d33344bbSsy25831 /* 255d33344bbSsy25831 * Get the solaris partition information from the device 256d33344bbSsy25831 * and compare the offset of S2 with offset of solaris partition 257d33344bbSsy25831 * from fdisk partition table. 258d33344bbSsy25831 */ 259342440ecSPrasad Singamsetty if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) { 260d33344bbSsy25831 if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) { 261d33344bbSsy25831 (void) fprintf(stderr, PART_FAIL); 262d33344bbSsy25831 exit(-1); 263342440ecSPrasad Singamsetty } else { 264342440ecSPrasad Singamsetty edkpi.p_start = dkpi.p_start; 265342440ecSPrasad Singamsetty } 266d33344bbSsy25831 } 267d33344bbSsy25831 268d33344bbSsy25831 for (i = 0; i < FD_NUMPART; i++) { 269d33344bbSsy25831 part = (struct ipart *)mboot->parts + i; 270d33344bbSsy25831 271d33344bbSsy25831 if (part->relsect == 0) { 272d33344bbSsy25831 (void) fprintf(stderr, BAD_PART, i); 273d33344bbSsy25831 exit(-1); 274d33344bbSsy25831 } 275342440ecSPrasad Singamsetty if (edkpi.p_start >= part->relsect && 276342440ecSPrasad Singamsetty edkpi.p_start < (part->relsect + part->numsect)) { 277d33344bbSsy25831 /* Found the partition */ 2787c478bd9Sstevel@tonic-gate break; 279d33344bbSsy25831 } 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 283f85c7842SSuhasini Peddada if (i == FD_NUMPART) { 2847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR); 2857c478bd9Sstevel@tonic-gate exit(-1); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate /* get confirmation for -m */ 2897c478bd9Sstevel@tonic-gate if (write_mboot && !force_mboot) { 2907c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 2917c478bd9Sstevel@tonic-gate if (getchar() != 'y') { 2927c478bd9Sstevel@tonic-gate write_mboot = 0; 2937c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate start_sect = part->relsect; 2987c478bd9Sstevel@tonic-gate if (part->bootid != 128 && write_mboot == 0) { 2997c478bd9Sstevel@tonic-gate (void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate 302f85c7842SSuhasini Peddada partition = i; 3037c478bd9Sstevel@tonic-gate return (start_sect); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate static void 3077c478bd9Sstevel@tonic-gate usage(char *progname) 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, USAGE, basename(progname)); 3107c478bd9Sstevel@tonic-gate exit(-1); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate static int 3147c478bd9Sstevel@tonic-gate open_device(char *device) 3157c478bd9Sstevel@tonic-gate { 3167c478bd9Sstevel@tonic-gate int dev_fd; 3177c478bd9Sstevel@tonic-gate struct stat stat; 3187c478bd9Sstevel@tonic-gate char *raw_part; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 3217c478bd9Sstevel@tonic-gate strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate /* handle boot partition specification */ 3247c478bd9Sstevel@tonic-gate if (!is_floppy && strstr(device, "p0:boot")) { 3257c478bd9Sstevel@tonic-gate is_bootpar = 1; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate raw_part = get_raw_partition(device); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate if (nowrite) 3317c478bd9Sstevel@tonic-gate dev_fd = open(raw_part, O_RDONLY); 3327c478bd9Sstevel@tonic-gate else 3337c478bd9Sstevel@tonic-gate dev_fd = open(raw_part, O_RDWR); 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 3367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw_part); 3377c478bd9Sstevel@tonic-gate exit(-1); 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate if (S_ISCHR(stat.st_mode) == 0) { 3407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 3417c478bd9Sstevel@tonic-gate exit(-1); 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate return (dev_fd); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate static void 3487c478bd9Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate int fd; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /* read the stage1 file from filesystem */ 3537c478bd9Sstevel@tonic-gate fd = open(stage1, O_RDONLY); 3547c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 3557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 3567c478bd9Sstevel@tonic-gate exit(-1); 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate (void) close(fd); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate /* read first two blocks of stage 2 from filesystem */ 3617c478bd9Sstevel@tonic-gate stage2_fd = open(stage2, O_RDONLY); 3627c478bd9Sstevel@tonic-gate if (stage2_fd == -1 || 3637c478bd9Sstevel@tonic-gate read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 3647c478bd9Sstevel@tonic-gate != 2 * SECTOR_SIZE) { 3657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 3667c478bd9Sstevel@tonic-gate exit(-1); 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate /* leave the stage2 file open for later */ 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate static void 3727c478bd9Sstevel@tonic-gate read_bpb_sect(int dev_fd) 3737c478bd9Sstevel@tonic-gate { 3747c478bd9Sstevel@tonic-gate if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 3757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_BPB); 3767c478bd9Sstevel@tonic-gate exit(-1); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate static void 3817c478bd9Sstevel@tonic-gate read_boot_sect(char *device) 3827c478bd9Sstevel@tonic-gate { 3837c478bd9Sstevel@tonic-gate static int read_mbr = 0; 3847c478bd9Sstevel@tonic-gate int i, fd; 3857c478bd9Sstevel@tonic-gate char save[2]; 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if (read_mbr) 3887c478bd9Sstevel@tonic-gate return; 3897c478bd9Sstevel@tonic-gate read_mbr = 1; 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate /* get the whole disk (p0) */ 3927c478bd9Sstevel@tonic-gate i = strlen(device); 3937c478bd9Sstevel@tonic-gate save[0] = device[i - 2]; 3947c478bd9Sstevel@tonic-gate save[1] = device[i - 1]; 3957c478bd9Sstevel@tonic-gate device[i - 2] = 'p'; 3967c478bd9Sstevel@tonic-gate device[i - 1] = '0'; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate fd = open(device, O_RDONLY); 3997c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 4007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_MBR, device); 4017c478bd9Sstevel@tonic-gate if (fd == -1) 4027c478bd9Sstevel@tonic-gate perror("open"); 4037c478bd9Sstevel@tonic-gate else 4047c478bd9Sstevel@tonic-gate perror("read"); 4057c478bd9Sstevel@tonic-gate exit(-1); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate (void) close(fd); 4087c478bd9Sstevel@tonic-gate device[i - 2] = save[0]; 4097c478bd9Sstevel@tonic-gate device[i - 1] = save[1]; 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate static void 4137c478bd9Sstevel@tonic-gate write_boot_sect(char *device) 4147c478bd9Sstevel@tonic-gate { 4157c478bd9Sstevel@tonic-gate int fd, len; 4167c478bd9Sstevel@tonic-gate char *raw, *end; 4177c478bd9Sstevel@tonic-gate struct stat stat; 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate /* make a copy and chop off ":boot" */ 4207c478bd9Sstevel@tonic-gate raw = strdup(device); 4217c478bd9Sstevel@tonic-gate end = strstr(raw, "p0:boot"); 4227c478bd9Sstevel@tonic-gate if (end) 4237c478bd9Sstevel@tonic-gate end[2] = 0; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* open p0 (whole disk) */ 4267c478bd9Sstevel@tonic-gate len = strlen(raw); 4277c478bd9Sstevel@tonic-gate raw[len - 2] = 'p'; 4287c478bd9Sstevel@tonic-gate raw[len - 1] = '0'; 4297c478bd9Sstevel@tonic-gate fd = open(raw, O_WRONLY); 4307c478bd9Sstevel@tonic-gate if (fd == -1 || fstat(fd, &stat) != 0) { 4317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw); 4327c478bd9Sstevel@tonic-gate exit(-1); 4337c478bd9Sstevel@tonic-gate } 4347c478bd9Sstevel@tonic-gate if (!nowrite && 4357c478bd9Sstevel@tonic-gate pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 4377c478bd9Sstevel@tonic-gate exit(-1); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_MBOOT); 4407c478bd9Sstevel@tonic-gate (void) close(fd); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate static void 4447c478bd9Sstevel@tonic-gate modify_and_write_stage1(int dev_fd) 4457c478bd9Sstevel@tonic-gate { 4467c478bd9Sstevel@tonic-gate if (is_floppy) { 4477c478bd9Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 4487c478bd9Sstevel@tonic-gate /* copy bios parameter block (for fat fs) */ 4497c478bd9Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 4507c478bd9Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 4517c478bd9Sstevel@tonic-gate } else if (is_bootpar) { 452d33344bbSsy25831 stage2_first_sector = get_start_sector(dev_fd) + blocklist[0]; 4537c478bd9Sstevel@tonic-gate /* copy bios parameter block (for fat fs) and MBR */ 4547c478bd9Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 4557c478bd9Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 4567c478bd9Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 4577c478bd9Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 4587c478bd9Sstevel@tonic-gate } else { 459d33344bbSsy25831 stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF; 4607c478bd9Sstevel@tonic-gate /* copy MBR to stage1 in case of overwriting MBR sector */ 4617c478bd9Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 4627c478bd9Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate /* modify default stage1 file generated by GRUB */ 4667c478bd9Sstevel@tonic-gate *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 4677c478bd9Sstevel@tonic-gate = stage2_first_sector; 4687c478bd9Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 4697c478bd9Sstevel@tonic-gate = STAGE2_MEMADDR; 4707c478bd9Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 4717c478bd9Sstevel@tonic-gate = STAGE2_MEMADDR >> 4; 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * XXX the default grub distribution also: 4757c478bd9Sstevel@tonic-gate * - Copy the possible MBR/extended part table 4767c478bd9Sstevel@tonic-gate * - Set the boot drive of stage1 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate /* write stage1/pboot to 1st sector */ 4807c478bd9Sstevel@tonic-gate if (!nowrite && 4817c478bd9Sstevel@tonic-gate pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_PBOOT); 4837c478bd9Sstevel@tonic-gate exit(-1); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate if (is_floppy) { 4877c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 4887c478bd9Sstevel@tonic-gate } else { 4897c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_PBOOT, 490d33344bbSsy25831 partition, get_start_sector(dev_fd)); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 494*7ce76caaSEnrico Perla - Sun Microsystems static void check_extended_support(char *stage2) 495*7ce76caaSEnrico Perla - Sun Microsystems { 496*7ce76caaSEnrico Perla - Sun Microsystems char *cmp = stage2_buffer + STAGE2_SIGN_OFFSET - 1; 497*7ce76caaSEnrico Perla - Sun Microsystems 498*7ce76caaSEnrico Perla - Sun Microsystems if ((*cmp++ != '\xEE') && memcmp(cmp, extended_sig, HASH_SIZE) != 0) { 499*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stderr, "%s does not support extended versioning\n", 500*7ce76caaSEnrico Perla - Sun Microsystems stage2); 501*7ce76caaSEnrico Perla - Sun Microsystems do_version = 0; 502*7ce76caaSEnrico Perla - Sun Microsystems } 503*7ce76caaSEnrico Perla - Sun Microsystems } 504*7ce76caaSEnrico Perla - Sun Microsystems 505*7ce76caaSEnrico Perla - Sun Microsystems 506*7ce76caaSEnrico Perla - Sun Microsystems static void print_info() 507*7ce76caaSEnrico Perla - Sun Microsystems { 508*7ce76caaSEnrico Perla - Sun Microsystems int i; 509*7ce76caaSEnrico Perla - Sun Microsystems 510*7ce76caaSEnrico Perla - Sun Microsystems if (strip) { 511*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "%s\n", verstring); 512*7ce76caaSEnrico Perla - Sun Microsystems } else { 513*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "Grub extended version information : %s\n", 514*7ce76caaSEnrico Perla - Sun Microsystems verstring); 515*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "Grub stage2 (MD5) signature : "); 516*7ce76caaSEnrico Perla - Sun Microsystems } 517*7ce76caaSEnrico Perla - Sun Microsystems 518*7ce76caaSEnrico Perla - Sun Microsystems for (i = 0; i < HASH_SIZE; i++) 519*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "%02x", (unsigned char)signature[i]); 520*7ce76caaSEnrico Perla - Sun Microsystems 521*7ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "\n"); 522*7ce76caaSEnrico Perla - Sun Microsystems } 523*7ce76caaSEnrico Perla - Sun Microsystems 524*7ce76caaSEnrico Perla - Sun Microsystems static int 525*7ce76caaSEnrico Perla - Sun Microsystems read_stage2_info(int dev_fd) 526*7ce76caaSEnrico Perla - Sun Microsystems { 527*7ce76caaSEnrico Perla - Sun Microsystems int ret; 528*7ce76caaSEnrico Perla - Sun Microsystems int first_offset, second_offset; 529*7ce76caaSEnrico Perla - Sun Microsystems char *sign; 530*7ce76caaSEnrico Perla - Sun Microsystems 531*7ce76caaSEnrico Perla - Sun Microsystems if (is_floppy || is_bootpar) { 532*7ce76caaSEnrico Perla - Sun Microsystems 533*7ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage1_buffer, SECTOR_SIZE, 0); 534*7ce76caaSEnrico Perla - Sun Microsystems if (ret != SECTOR_SIZE) { 535*7ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage1 sector"); 536*7ce76caaSEnrico Perla - Sun Microsystems return (1); 537*7ce76caaSEnrico Perla - Sun Microsystems } 538*7ce76caaSEnrico Perla - Sun Microsystems 539*7ce76caaSEnrico Perla - Sun Microsystems first_offset = *((ulong_t *)(stage1_buffer + 540*7ce76caaSEnrico Perla - Sun Microsystems STAGE1_STAGE2_SECTOR)); 541*7ce76caaSEnrico Perla - Sun Microsystems 542*7ce76caaSEnrico Perla - Sun Microsystems /* Start reading in the first sector of stage 2 */ 543*7ce76caaSEnrico Perla - Sun Microsystems 544*7ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage2_buffer, SECTOR_SIZE, first_offset * 545*7ce76caaSEnrico Perla - Sun Microsystems SECTOR_SIZE); 546*7ce76caaSEnrico Perla - Sun Microsystems if (ret != SECTOR_SIZE) { 547*7ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage2 first sector"); 548*7ce76caaSEnrico Perla - Sun Microsystems return (1); 549*7ce76caaSEnrico Perla - Sun Microsystems } 550*7ce76caaSEnrico Perla - Sun Microsystems 551*7ce76caaSEnrico Perla - Sun Microsystems /* From the block list section grab stage2 second sector */ 552*7ce76caaSEnrico Perla - Sun Microsystems 553*7ce76caaSEnrico Perla - Sun Microsystems second_offset = *((ulong_t *)(stage2_buffer + 554*7ce76caaSEnrico Perla - Sun Microsystems STAGE2_BLOCKLIST)); 555*7ce76caaSEnrico Perla - Sun Microsystems 556*7ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 557*7ce76caaSEnrico Perla - Sun Microsystems second_offset * SECTOR_SIZE); 558*7ce76caaSEnrico Perla - Sun Microsystems if (ret != SECTOR_SIZE) { 559*7ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage2 second sector"); 560*7ce76caaSEnrico Perla - Sun Microsystems return (1); 561*7ce76caaSEnrico Perla - Sun Microsystems } 562*7ce76caaSEnrico Perla - Sun Microsystems } else { 563*7ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 564*7ce76caaSEnrico Perla - Sun Microsystems STAGE2_BLKOFF * SECTOR_SIZE); 565*7ce76caaSEnrico Perla - Sun Microsystems if (ret != 2 * SECTOR_SIZE) { 566*7ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage2 sectors"); 567*7ce76caaSEnrico Perla - Sun Microsystems return (1); 568*7ce76caaSEnrico Perla - Sun Microsystems } 569*7ce76caaSEnrico Perla - Sun Microsystems } 570*7ce76caaSEnrico Perla - Sun Microsystems 571*7ce76caaSEnrico Perla - Sun Microsystems sign = stage2_buffer + STAGE2_SIGN_OFFSET - 1; 572*7ce76caaSEnrico Perla - Sun Microsystems if (*sign++ != '\xEE') 573*7ce76caaSEnrico Perla - Sun Microsystems return (1); 574*7ce76caaSEnrico Perla - Sun Microsystems (void) memcpy(signature, sign, HASH_SIZE); 575*7ce76caaSEnrico Perla - Sun Microsystems sign = stage2_buffer + STAGE2_PKG_VERSION; 576*7ce76caaSEnrico Perla - Sun Microsystems (void) strncpy(verstring, sign, VERSION_SIZE); 577*7ce76caaSEnrico Perla - Sun Microsystems return (0); 578*7ce76caaSEnrico Perla - Sun Microsystems } 579*7ce76caaSEnrico Perla - Sun Microsystems 580*7ce76caaSEnrico Perla - Sun Microsystems 581*7ce76caaSEnrico Perla - Sun Microsystems static int 582*7ce76caaSEnrico Perla - Sun Microsystems compute_and_write_md5hash(char *dest) 583*7ce76caaSEnrico Perla - Sun Microsystems { 584*7ce76caaSEnrico Perla - Sun Microsystems struct stat sb; 585*7ce76caaSEnrico Perla - Sun Microsystems char *buffer; 586*7ce76caaSEnrico Perla - Sun Microsystems 587*7ce76caaSEnrico Perla - Sun Microsystems if (fstat(stage2_fd, &sb) == -1) 588*7ce76caaSEnrico Perla - Sun Microsystems return (-1); 589*7ce76caaSEnrico Perla - Sun Microsystems 590*7ce76caaSEnrico Perla - Sun Microsystems buffer = malloc(sb.st_size); 591*7ce76caaSEnrico Perla - Sun Microsystems if (buffer == NULL) 592*7ce76caaSEnrico Perla - Sun Microsystems return (-1); 593*7ce76caaSEnrico Perla - Sun Microsystems 594*7ce76caaSEnrico Perla - Sun Microsystems if (lseek(stage2_fd, 0, SEEK_SET) == -1) 595*7ce76caaSEnrico Perla - Sun Microsystems return (-1); 596*7ce76caaSEnrico Perla - Sun Microsystems if (read(stage2_fd, buffer, sb.st_size) < 0) 597*7ce76caaSEnrico Perla - Sun Microsystems return (-1); 598*7ce76caaSEnrico Perla - Sun Microsystems 599*7ce76caaSEnrico Perla - Sun Microsystems md5_calc(dest, buffer, sb.st_size); 600*7ce76caaSEnrico Perla - Sun Microsystems free(buffer); 601*7ce76caaSEnrico Perla - Sun Microsystems return (0); 602*7ce76caaSEnrico Perla - Sun Microsystems } 603*7ce76caaSEnrico Perla - Sun Microsystems 604*7ce76caaSEnrico Perla - Sun Microsystems 6057c478bd9Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 6067c478bd9Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 6077c478bd9Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate static void 6107c478bd9Sstevel@tonic-gate modify_and_write_stage2(int dev_fd) 6117c478bd9Sstevel@tonic-gate { 6127c478bd9Sstevel@tonic-gate int nrecord; 6137c478bd9Sstevel@tonic-gate off_t offset; 614*7ce76caaSEnrico Perla - Sun Microsystems char *dest; 615*7ce76caaSEnrico Perla - Sun Microsystems 616*7ce76caaSEnrico Perla - Sun Microsystems if (do_version) { 617*7ce76caaSEnrico Perla - Sun Microsystems dest = stage2_buffer + STAGE2_SIGN_OFFSET; 618*7ce76caaSEnrico Perla - Sun Microsystems if (compute_and_write_md5hash(dest) < 0) 619*7ce76caaSEnrico Perla - Sun Microsystems perror("MD5 operation"); 620*7ce76caaSEnrico Perla - Sun Microsystems dest = stage2_buffer + STAGE2_PKG_VERSION; 621*7ce76caaSEnrico Perla - Sun Microsystems (void) strncpy(dest, verstring, VERSION_SIZE); 622*7ce76caaSEnrico Perla - Sun Microsystems } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 6257c478bd9Sstevel@tonic-gate int i = 0; 626f85c7842SSuhasini Peddada uint_t partition_offset; 627f85c7842SSuhasini Peddada uint_t install_addr = 0x8200; 6287c478bd9Sstevel@tonic-gate uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate /* figure out the second sector */ 6337c478bd9Sstevel@tonic-gate if (blocklist[1] > 1) { 6347c478bd9Sstevel@tonic-gate blocklist[0]++; 6357c478bd9Sstevel@tonic-gate blocklist[1]--; 6367c478bd9Sstevel@tonic-gate } else { 6377c478bd9Sstevel@tonic-gate i += 2; 6387c478bd9Sstevel@tonic-gate } 6397c478bd9Sstevel@tonic-gate stage2_second_sector = blocklist[i]; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate if (is_floppy) 6427c478bd9Sstevel@tonic-gate partition_offset = 0; 6437c478bd9Sstevel@tonic-gate else /* solaris boot partition */ 644d33344bbSsy25831 partition_offset = get_start_sector(dev_fd); 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate /* install the blocklist at the end of stage2_buffer */ 6477c478bd9Sstevel@tonic-gate while (blocklist[i]) { 6487c478bd9Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 6497c478bd9Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 6507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 6517c478bd9Sstevel@tonic-gate exit(-1); 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate START_BLOCK(pos) = blocklist[i] + partition_offset; 6547c478bd9Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 6557c478bd9Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 6567c478bd9Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 6577c478bd9Sstevel@tonic-gate pos -= 8; 6587c478bd9Sstevel@tonic-gate i += 2; 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate } else { 6627c478bd9Sstevel@tonic-gate /* 6637c478bd9Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 6647c478bd9Sstevel@tonic-gate * blocks. So we update the starting block only. 6657c478bd9Sstevel@tonic-gate */ 6667c478bd9Sstevel@tonic-gate *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 6677c478bd9Sstevel@tonic-gate stage2_first_sector + 1; 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate if (is_floppy) { 6717c478bd9Sstevel@tonic-gate /* modify the config file to add (fd0) */ 6727c478bd9Sstevel@tonic-gate char *config_file = stage2_buffer + STAGE2_VER_STRING; 6737c478bd9Sstevel@tonic-gate while (*config_file++) 6747c478bd9Sstevel@tonic-gate ; 6757c478bd9Sstevel@tonic-gate strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 6767c478bd9Sstevel@tonic-gate } else { 6777c478bd9Sstevel@tonic-gate /* force lba and set disk partition */ 6787c478bd9Sstevel@tonic-gate *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 6797c478bd9Sstevel@tonic-gate *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 6807c478bd9Sstevel@tonic-gate = (partition << 16) | (slice << 8) | 0xff; 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate /* modification done, now do the writing */ 6847c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 6857c478bd9Sstevel@tonic-gate /* we rewrite block 0 and 1 and that's it */ 6867c478bd9Sstevel@tonic-gate if (!nowrite && 6877c478bd9Sstevel@tonic-gate (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 6887c478bd9Sstevel@tonic-gate stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 6897c478bd9Sstevel@tonic-gate pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 6907c478bd9Sstevel@tonic-gate stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 6917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 6927c478bd9Sstevel@tonic-gate exit(-1); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_PCFS); 6957c478bd9Sstevel@tonic-gate return; 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 6997c478bd9Sstevel@tonic-gate offset = STAGE2_BLKOFF; 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate /* write the modified first two sectors */ 7027c478bd9Sstevel@tonic-gate if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 7037c478bd9Sstevel@tonic-gate offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 7047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 7057c478bd9Sstevel@tonic-gate exit(-1); 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate /* write the remaining sectors */ 7097c478bd9Sstevel@tonic-gate nrecord = 2; 7107c478bd9Sstevel@tonic-gate offset += 2; 7117c478bd9Sstevel@tonic-gate for (;;) { 7127c478bd9Sstevel@tonic-gate int nread, nwrite; 7137c478bd9Sstevel@tonic-gate nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 7147c478bd9Sstevel@tonic-gate nrecord * SECTOR_SIZE); 7157c478bd9Sstevel@tonic-gate if (nread > 0 && !nowrite) 7167c478bd9Sstevel@tonic-gate nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 7177c478bd9Sstevel@tonic-gate offset * SECTOR_SIZE); 7187c478bd9Sstevel@tonic-gate else 7197c478bd9Sstevel@tonic-gate nwrite = SECTOR_SIZE; 7207c478bd9Sstevel@tonic-gate if (nread < 0 || nwrite != SECTOR_SIZE) { 7217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 7227c478bd9Sstevel@tonic-gate nread, nwrite); 7237c478bd9Sstevel@tonic-gate break; 7247c478bd9Sstevel@tonic-gate } 725c6fe1048Sjongkis if (nread > 0) { 7267c478bd9Sstevel@tonic-gate nrecord ++; 7277c478bd9Sstevel@tonic-gate offset ++; 728c6fe1048Sjongkis } 7297c478bd9Sstevel@tonic-gate if (nread < SECTOR_SIZE) 7307c478bd9Sstevel@tonic-gate break; /* end of file */ 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_DISK, 7337c478bd9Sstevel@tonic-gate partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate static char * 7377c478bd9Sstevel@tonic-gate get_raw_partition(char *device) 7387c478bd9Sstevel@tonic-gate { 7397c478bd9Sstevel@tonic-gate int len; 7407c478bd9Sstevel@tonic-gate struct mboot *mboot; 7417c478bd9Sstevel@tonic-gate static char *raw = NULL; 7427c478bd9Sstevel@tonic-gate 7437c478bd9Sstevel@tonic-gate if (raw) 7447c478bd9Sstevel@tonic-gate return (raw); 7457c478bd9Sstevel@tonic-gate raw = strdup(device); 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate if (is_floppy) 7487c478bd9Sstevel@tonic-gate return (raw); 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate if (is_bootpar) { 7517c478bd9Sstevel@tonic-gate int i; 7527c478bd9Sstevel@tonic-gate char *end = strstr(raw, "p0:boot"); 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate end[2] = 0; /* chop off :boot */ 7557c478bd9Sstevel@tonic-gate read_boot_sect(raw); 7567c478bd9Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 7577c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 7587c478bd9Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i; 7597c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) /* solaris boot part */ 7607c478bd9Sstevel@tonic-gate break; 7617c478bd9Sstevel@tonic-gate } 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate if (i == FD_NUMPART) { 7647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 7657c478bd9Sstevel@tonic-gate exit(-1); 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate end[1] = '1' + i; /* set partition name */ 7687c478bd9Sstevel@tonic-gate return (raw); 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 7727c478bd9Sstevel@tonic-gate len = strlen(raw); 7737c478bd9Sstevel@tonic-gate if (raw[len - 2] != 's' || raw[len - 1] == '2') { 7747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 7757c478bd9Sstevel@tonic-gate exit(-1); 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate slice = atoi(&raw[len - 1]); 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate raw[len - 2] = 's'; 7807c478bd9Sstevel@tonic-gate raw[len - 1] = '2'; 7817c478bd9Sstevel@tonic-gate return (raw); 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 7857c478bd9Sstevel@tonic-gate static void 7867c478bd9Sstevel@tonic-gate copy_stage2(int dev_fd, char *device) 7877c478bd9Sstevel@tonic-gate { 7887c478bd9Sstevel@tonic-gate FILE *mntfp; 7897c478bd9Sstevel@tonic-gate int i, pcfs_fp; 7907c478bd9Sstevel@tonic-gate char buf[SECTOR_SIZE]; 7917c478bd9Sstevel@tonic-gate char *cp; 7927c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 7957c478bd9Sstevel@tonic-gate (void) strncpy(buf, device, sizeof (buf)); 7967c478bd9Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 7977c478bd9Sstevel@tonic-gate cp = strchr(buf, 'r'); 7987c478bd9Sstevel@tonic-gate if (cp == NULL) { 7997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, CONVERT_FAIL, device); 8007c478bd9Sstevel@tonic-gate exit(-1); 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate do { 8037c478bd9Sstevel@tonic-gate *cp = *(cp + 1); 8047c478bd9Sstevel@tonic-gate } while (*(++cp)); 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate /* get the mount point, if any */ 8077c478bd9Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 8087c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 8097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 8107c478bd9Sstevel@tonic-gate exit(-1); 8117c478bd9Sstevel@tonic-gate } 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate mpref.mnt_special = buf; 8147c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 8157c478bd9Sstevel@tonic-gate char cmd[128]; 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate /* not mounted, try remount */ 8187c478bd9Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 8197c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 8207c478bd9Sstevel@tonic-gate buf, TMP_MNTPT); 8217c478bd9Sstevel@tonic-gate (void) system(cmd); 8227c478bd9Sstevel@tonic-gate rewind(mntfp); 8237c478bd9Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 8247c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 8257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 8267c478bd9Sstevel@tonic-gate exit(-1); 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 8317c478bd9Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 8327c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 8337c478bd9Sstevel@tonic-gate (void) strcat(buf, "/grub"); 8347c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 8377c478bd9Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 8387c478bd9Sstevel@tonic-gate if (pcfs_fp == -1) { 8397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 8407c478bd9Sstevel@tonic-gate perror("open:"); 8417c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 8427c478bd9Sstevel@tonic-gate exit(-1); 8437c478bd9Sstevel@tonic-gate } 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate /* write stage2 to pcfs */ 8467c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 8477c478bd9Sstevel@tonic-gate int nread, nwrite; 8487c478bd9Sstevel@tonic-gate nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 8497c478bd9Sstevel@tonic-gate if (nowrite) 8507c478bd9Sstevel@tonic-gate nwrite = nread; 8517c478bd9Sstevel@tonic-gate else 8527c478bd9Sstevel@tonic-gate nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 8537c478bd9Sstevel@tonic-gate if (nread < 0 || nwrite != nread) { 8547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 8557c478bd9Sstevel@tonic-gate nread, nwrite); 8567c478bd9Sstevel@tonic-gate break; 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate if (nread < SECTOR_SIZE) 8597c478bd9Sstevel@tonic-gate break; /* end of file */ 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate (void) close(pcfs_fp); 8627c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate /* 8657c478bd9Sstevel@tonic-gate * Now, get the blocklist from the device. 8667c478bd9Sstevel@tonic-gate */ 8677c478bd9Sstevel@tonic-gate bzero(blocklist, sizeof (blocklist)); 8687c478bd9Sstevel@tonic-gate if (read_stage2_blocklist(dev_fd, blocklist) != 0) 8697c478bd9Sstevel@tonic-gate exit(-1); 8707c478bd9Sstevel@tonic-gate } 871