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 /* 22*aa1b14e7SSheshadri Vasudevan * Copyright 2009 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*aa1b14e7SSheshadri Vasudevan #include <libfdisk.h> 477ce76caaSEnrico Perla - Sun Microsystems #include <md5.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 507c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 517c478bd9Sstevel@tonic-gate #endif 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 0x200 547ce76caaSEnrico Perla - Sun Microsystems #define HASH_SIZE 0x10 557ce76caaSEnrico Perla - Sun Microsystems #define VERSION_SIZE 0x50 567c478bd9Sstevel@tonic-gate #define STAGE2_MEMADDR 0x8000 /* loading addr of stage2 */ 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #define STAGE1_BPB_OFFSET 0x3 597c478bd9Sstevel@tonic-gate #define STAGE1_BPB_SIZE 0x3B 607c478bd9Sstevel@tonic-gate #define STAGE1_BOOT_DRIVE 0x40 617c478bd9Sstevel@tonic-gate #define STAGE1_FORCE_LBA 0x41 627c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_ADDRESS 0x42 637c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_SECTOR 0x44 647c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_SEGMENT 0x48 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #define STAGE2_BLOCKLIST (SECTOR_SIZE - 0x8) 677c478bd9Sstevel@tonic-gate #define STAGE2_INSTALLPART (SECTOR_SIZE + 0x8) 687c478bd9Sstevel@tonic-gate #define STAGE2_FORCE_LBA (SECTOR_SIZE + 0x11) 697c478bd9Sstevel@tonic-gate #define STAGE2_VER_STRING (SECTOR_SIZE + 0x12) 707ce76caaSEnrico Perla - Sun Microsystems #define STAGE2_SIGN_OFFSET (SECTOR_SIZE + 0x60) 717ce76caaSEnrico Perla - Sun Microsystems #define STAGE2_PKG_VERSION (SECTOR_SIZE + 0x70) 727c478bd9Sstevel@tonic-gate #define STAGE2_BLKOFF 50 /* offset from start of fdisk part */ 737c478bd9Sstevel@tonic-gate 747ce76caaSEnrico Perla - Sun Microsystems static char extended_sig[] = "\xCC\xCC\xCC\xCC\xAA\xAA\xAA\xAA\xBB\xBB\xBB\xBB" 757ce76caaSEnrico Perla - Sun Microsystems "\xBB\xBB\xBB\xBB"; 767ce76caaSEnrico Perla - Sun Microsystems 777c478bd9Sstevel@tonic-gate static int nowrite = 0; 787c478bd9Sstevel@tonic-gate static int write_mboot = 0; 797c478bd9Sstevel@tonic-gate static int force_mboot = 0; 807ce76caaSEnrico Perla - Sun Microsystems static int getinfo = 0; 817ce76caaSEnrico Perla - Sun Microsystems static int do_version = 0; 827c478bd9Sstevel@tonic-gate static int is_floppy = 0; 837c478bd9Sstevel@tonic-gate static int is_bootpar = 0; 847ce76caaSEnrico Perla - Sun Microsystems static int strip = 0; 857c478bd9Sstevel@tonic-gate static int stage2_fd; 867c478bd9Sstevel@tonic-gate static int partition, slice = 0xff; 87*aa1b14e7SSheshadri Vasudevan static char *device_p0; 88*aa1b14e7SSheshadri Vasudevan static uint32_t stage2_first_sector, stage2_second_sector; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE]; 927c478bd9Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE]; 937c478bd9Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE]; 947c478bd9Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE]; 957ce76caaSEnrico Perla - Sun Microsystems static char signature[HASH_SIZE]; 967ce76caaSEnrico Perla - Sun Microsystems static char verstring[VERSION_SIZE]; 97342440ecSPrasad Singamsetty static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)]; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate static int open_device(char *); 1007c478bd9Sstevel@tonic-gate static void read_bpb_sect(int); 1017c478bd9Sstevel@tonic-gate static void read_boot_sect(char *); 1027c478bd9Sstevel@tonic-gate static void write_boot_sect(char *); 1037c478bd9Sstevel@tonic-gate static void read_stage1_stage2(char *, char *); 1047c478bd9Sstevel@tonic-gate static void modify_and_write_stage1(int); 1057c478bd9Sstevel@tonic-gate static void modify_and_write_stage2(int); 106342440ecSPrasad Singamsetty static unsigned int get_start_sector(int); 1077c478bd9Sstevel@tonic-gate static void copy_stage2(int, char *); 1087c478bd9Sstevel@tonic-gate static char *get_raw_partition(char *); 1097c478bd9Sstevel@tonic-gate static void usage(char *); 1107ce76caaSEnrico Perla - Sun Microsystems static void print_info(); 1117ce76caaSEnrico Perla - Sun Microsystems static int read_stage2_info(int); 1127ce76caaSEnrico Perla - Sun Microsystems static void check_extended_support(); 1137c478bd9Sstevel@tonic-gate 114342440ecSPrasad Singamsetty extern int read_stage2_blocklist(int, unsigned int *); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate int 1177c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1187c478bd9Sstevel@tonic-gate { 1197ce76caaSEnrico Perla - Sun Microsystems int dev_fd, opt, params = 3; 1207c478bd9Sstevel@tonic-gate char *stage1, *stage2, *device; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1237c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1247c478bd9Sstevel@tonic-gate 1257ce76caaSEnrico Perla - Sun Microsystems while ((opt = getopt(argc, argv, "fmneis:")) != EOF) { 1267c478bd9Sstevel@tonic-gate switch (opt) { 1277c478bd9Sstevel@tonic-gate case 'm': 1287c478bd9Sstevel@tonic-gate write_mboot = 1; 1297c478bd9Sstevel@tonic-gate break; 1307c478bd9Sstevel@tonic-gate case 'n': 1317c478bd9Sstevel@tonic-gate nowrite = 1; 1327c478bd9Sstevel@tonic-gate break; 1337c478bd9Sstevel@tonic-gate case 'f': 1347c478bd9Sstevel@tonic-gate force_mboot = 1; 1357c478bd9Sstevel@tonic-gate break; 1367ce76caaSEnrico Perla - Sun Microsystems case 'i': 1377ce76caaSEnrico Perla - Sun Microsystems getinfo = 1; 1387ce76caaSEnrico Perla - Sun Microsystems params = 1; 1397ce76caaSEnrico Perla - Sun Microsystems break; 1407ce76caaSEnrico Perla - Sun Microsystems case 'e': 1417ce76caaSEnrico Perla - Sun Microsystems strip = 1; 1427ce76caaSEnrico Perla - Sun Microsystems break; 1437ce76caaSEnrico Perla - Sun Microsystems case 's': 1447ce76caaSEnrico Perla - Sun Microsystems do_version = 1; 1457ce76caaSEnrico Perla - Sun Microsystems (void) snprintf(verstring, sizeof (verstring), "%s", 1467ce76caaSEnrico Perla - Sun Microsystems optarg); 1477ce76caaSEnrico Perla - Sun Microsystems break; 1487c478bd9Sstevel@tonic-gate default: 1497c478bd9Sstevel@tonic-gate /* fall through to process non-optional args */ 1507c478bd9Sstevel@tonic-gate break; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* check arguments */ 1557ce76caaSEnrico Perla - Sun Microsystems if (argc != optind + params) { 1567c478bd9Sstevel@tonic-gate usage(argv[0]); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (nowrite) { 1607c478bd9Sstevel@tonic-gate (void) fprintf(stdout, DRY_RUN); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637ce76caaSEnrico Perla - Sun Microsystems if (params == 1) { 1647ce76caaSEnrico Perla - Sun Microsystems device = strdup(argv[optind]); 1657ce76caaSEnrico Perla - Sun Microsystems if (!device) { 1667ce76caaSEnrico Perla - Sun Microsystems usage(argv[0]); 1677ce76caaSEnrico Perla - Sun Microsystems } 1687ce76caaSEnrico Perla - Sun Microsystems } else if (params == 3) { 1697c478bd9Sstevel@tonic-gate stage1 = strdup(argv[optind]); 1707c478bd9Sstevel@tonic-gate stage2 = strdup(argv[optind + 1]); 1717c478bd9Sstevel@tonic-gate device = strdup(argv[optind + 2]); 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate if (!stage1 || !stage2 || !device) { 1747c478bd9Sstevel@tonic-gate usage(argv[0]); 1757c478bd9Sstevel@tonic-gate } 1767ce76caaSEnrico Perla - Sun Microsystems } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate /* open and check device type */ 1797c478bd9Sstevel@tonic-gate dev_fd = open_device(device); 1807c478bd9Sstevel@tonic-gate 1817ce76caaSEnrico Perla - Sun Microsystems if (getinfo) { 1827ce76caaSEnrico Perla - Sun Microsystems if (read_stage2_info(dev_fd) != 0) { 1837ce76caaSEnrico Perla - Sun Microsystems fprintf(stderr, "Unable to read extended information" 1847ce76caaSEnrico Perla - Sun Microsystems " from %s\n", device); 1857ce76caaSEnrico Perla - Sun Microsystems exit(1); 1867ce76caaSEnrico Perla - Sun Microsystems } 1877ce76caaSEnrico Perla - Sun Microsystems print_info(); 1887ce76caaSEnrico Perla - Sun Microsystems (void) free(device); 1897ce76caaSEnrico Perla - Sun Microsystems (void) close(dev_fd); 1907ce76caaSEnrico Perla - Sun Microsystems return (0); 1917ce76caaSEnrico Perla - Sun Microsystems } 1927ce76caaSEnrico Perla - Sun Microsystems 1937c478bd9Sstevel@tonic-gate /* read in stage1 and stage2 into buffer */ 1947c478bd9Sstevel@tonic-gate read_stage1_stage2(stage1, stage2); 1957c478bd9Sstevel@tonic-gate 1967ce76caaSEnrico Perla - Sun Microsystems /* check if stage2 supports extended versioning */ 1977ce76caaSEnrico Perla - Sun Microsystems if (do_version) 1987ce76caaSEnrico Perla - Sun Microsystems check_extended_support(stage2); 1997ce76caaSEnrico Perla - Sun Microsystems 2007c478bd9Sstevel@tonic-gate /* In the pcfs case, write a fresh stage2 */ 2017c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 2027c478bd9Sstevel@tonic-gate copy_stage2(dev_fd, device); 2037c478bd9Sstevel@tonic-gate read_bpb_sect(dev_fd); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* read in boot sector */ 2077c478bd9Sstevel@tonic-gate if (!is_floppy) 2087c478bd9Sstevel@tonic-gate read_boot_sect(device); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* modify stage1 based on grub needs */ 2117c478bd9Sstevel@tonic-gate modify_and_write_stage1(dev_fd); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* modify stage2 and write to media */ 2147c478bd9Sstevel@tonic-gate modify_and_write_stage2(dev_fd); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if (!is_floppy && write_mboot) 2177c478bd9Sstevel@tonic-gate write_boot_sect(device); 2187ce76caaSEnrico Perla - Sun Microsystems 2197c478bd9Sstevel@tonic-gate (void) close(dev_fd); 2207ce76caaSEnrico Perla - Sun Microsystems free(device); 2217ce76caaSEnrico Perla - Sun Microsystems free(stage1); 2227ce76caaSEnrico Perla - Sun Microsystems free(stage2); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate return (0); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate 227342440ecSPrasad Singamsetty static unsigned int 228d33344bbSsy25831 get_start_sector(int fd) 2297c478bd9Sstevel@tonic-gate { 230342440ecSPrasad Singamsetty static unsigned int start_sect = 0; 231*aa1b14e7SSheshadri Vasudevan uint32_t secnum, numsec; 232*aa1b14e7SSheshadri Vasudevan int i, pno, rval, ext_sol_part_found = 0; 2337c478bd9Sstevel@tonic-gate struct mboot *mboot; 2347c478bd9Sstevel@tonic-gate struct ipart *part; 235*aa1b14e7SSheshadri Vasudevan ext_part_t *epp; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate if (start_sect) 2387c478bd9Sstevel@tonic-gate return (start_sect); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 2417c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2427c478bd9Sstevel@tonic-gate part = (struct ipart *)mboot->parts + i; 2437c478bd9Sstevel@tonic-gate if (is_bootpar) { 2447c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) 2457c478bd9Sstevel@tonic-gate break; 246d33344bbSsy25831 } 247d33344bbSsy25831 } 248d33344bbSsy25831 249*aa1b14e7SSheshadri Vasudevan /* Read extended partition to find a solaris partition */ 250*aa1b14e7SSheshadri Vasudevan if ((rval = libfdisk_init(&epp, device_p0, NULL, FDISK_READ_DISK)) 251*aa1b14e7SSheshadri Vasudevan != FDISK_SUCCESS) { 252*aa1b14e7SSheshadri Vasudevan switch (rval) { 253*aa1b14e7SSheshadri Vasudevan /* 254*aa1b14e7SSheshadri Vasudevan * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can 255*aa1b14e7SSheshadri Vasudevan * be considered as soft errors and hence 256*aa1b14e7SSheshadri Vasudevan * we do not exit 257*aa1b14e7SSheshadri Vasudevan */ 258*aa1b14e7SSheshadri Vasudevan case FDISK_EBADLOGDRIVE: 259*aa1b14e7SSheshadri Vasudevan break; 260*aa1b14e7SSheshadri Vasudevan case FDISK_ENOLOGDRIVE: 261*aa1b14e7SSheshadri Vasudevan break; 262*aa1b14e7SSheshadri Vasudevan case FDISK_ENOVGEOM: 263*aa1b14e7SSheshadri Vasudevan (void) fprintf(stderr, NO_VIRT_GEOM); 264*aa1b14e7SSheshadri Vasudevan exit(1); 265*aa1b14e7SSheshadri Vasudevan break; 266*aa1b14e7SSheshadri Vasudevan case FDISK_ENOPGEOM: 267*aa1b14e7SSheshadri Vasudevan (void) fprintf(stderr, NO_PHYS_GEOM); 268*aa1b14e7SSheshadri Vasudevan exit(1); 269*aa1b14e7SSheshadri Vasudevan break; 270*aa1b14e7SSheshadri Vasudevan case FDISK_ENOLGEOM: 271*aa1b14e7SSheshadri Vasudevan (void) fprintf(stderr, NO_LABEL_GEOM); 272*aa1b14e7SSheshadri Vasudevan exit(1); 273*aa1b14e7SSheshadri Vasudevan break; 274*aa1b14e7SSheshadri Vasudevan default: 275*aa1b14e7SSheshadri Vasudevan (void) fprintf(stderr, LIBFDISK_INIT_FAIL); 276*aa1b14e7SSheshadri Vasudevan exit(1); 277*aa1b14e7SSheshadri Vasudevan break; 278*aa1b14e7SSheshadri Vasudevan } 279*aa1b14e7SSheshadri Vasudevan } 280*aa1b14e7SSheshadri Vasudevan 281*aa1b14e7SSheshadri Vasudevan rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec); 282*aa1b14e7SSheshadri Vasudevan if (rval == FDISK_SUCCESS) { 283*aa1b14e7SSheshadri Vasudevan ext_sol_part_found = 1; 284*aa1b14e7SSheshadri Vasudevan } 285*aa1b14e7SSheshadri Vasudevan libfdisk_fini(&epp); 286*aa1b14e7SSheshadri Vasudevan 287d33344bbSsy25831 /* 288d33344bbSsy25831 * If there is no boot partition, find the solaris partition 289d33344bbSsy25831 */ 290d33344bbSsy25831 291d33344bbSsy25831 if (i == FD_NUMPART) { 292d33344bbSsy25831 struct part_info dkpi; 293342440ecSPrasad Singamsetty struct extpart_info edkpi; 294d33344bbSsy25831 295d33344bbSsy25831 /* 296d33344bbSsy25831 * Get the solaris partition information from the device 297d33344bbSsy25831 * and compare the offset of S2 with offset of solaris partition 298d33344bbSsy25831 * from fdisk partition table. 299d33344bbSsy25831 */ 300342440ecSPrasad Singamsetty if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) { 301d33344bbSsy25831 if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) { 302d33344bbSsy25831 (void) fprintf(stderr, PART_FAIL); 303d33344bbSsy25831 exit(-1); 304342440ecSPrasad Singamsetty } else { 305342440ecSPrasad Singamsetty edkpi.p_start = dkpi.p_start; 306342440ecSPrasad Singamsetty } 307d33344bbSsy25831 } 308d33344bbSsy25831 309d33344bbSsy25831 for (i = 0; i < FD_NUMPART; i++) { 310d33344bbSsy25831 part = (struct ipart *)mboot->parts + i; 311d33344bbSsy25831 312d33344bbSsy25831 if (part->relsect == 0) { 313d33344bbSsy25831 (void) fprintf(stderr, BAD_PART, i); 314d33344bbSsy25831 exit(-1); 315d33344bbSsy25831 } 316*aa1b14e7SSheshadri Vasudevan 317342440ecSPrasad Singamsetty if (edkpi.p_start >= part->relsect && 318342440ecSPrasad Singamsetty edkpi.p_start < (part->relsect + part->numsect)) { 319d33344bbSsy25831 /* Found the partition */ 3207c478bd9Sstevel@tonic-gate break; 321d33344bbSsy25831 } 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 325*aa1b14e7SSheshadri Vasudevan if ((i == FD_NUMPART) && (!ext_sol_part_found)) { 3267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR); 3277c478bd9Sstevel@tonic-gate exit(-1); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* get confirmation for -m */ 3317c478bd9Sstevel@tonic-gate if (write_mboot && !force_mboot) { 3327c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 3337c478bd9Sstevel@tonic-gate if (getchar() != 'y') { 3347c478bd9Sstevel@tonic-gate write_mboot = 0; 3357c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 339*aa1b14e7SSheshadri Vasudevan if (fdisk_is_dos_extended(part->systid)) { 340*aa1b14e7SSheshadri Vasudevan start_sect = secnum; 341*aa1b14e7SSheshadri Vasudevan partition = pno; 342*aa1b14e7SSheshadri Vasudevan } else { 3437c478bd9Sstevel@tonic-gate start_sect = part->relsect; 344*aa1b14e7SSheshadri Vasudevan partition = i; 345*aa1b14e7SSheshadri Vasudevan } 346*aa1b14e7SSheshadri Vasudevan 3477c478bd9Sstevel@tonic-gate if (part->bootid != 128 && write_mboot == 0) { 3487c478bd9Sstevel@tonic-gate (void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate return (start_sect); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate static void 3557c478bd9Sstevel@tonic-gate usage(char *progname) 3567c478bd9Sstevel@tonic-gate { 3577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, USAGE, basename(progname)); 3587c478bd9Sstevel@tonic-gate exit(-1); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate static int 3627c478bd9Sstevel@tonic-gate open_device(char *device) 3637c478bd9Sstevel@tonic-gate { 3647c478bd9Sstevel@tonic-gate int dev_fd; 3657c478bd9Sstevel@tonic-gate struct stat stat; 3667c478bd9Sstevel@tonic-gate char *raw_part; 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 3697c478bd9Sstevel@tonic-gate strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate /* handle boot partition specification */ 3727c478bd9Sstevel@tonic-gate if (!is_floppy && strstr(device, "p0:boot")) { 3737c478bd9Sstevel@tonic-gate is_bootpar = 1; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate raw_part = get_raw_partition(device); 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate if (nowrite) 3797c478bd9Sstevel@tonic-gate dev_fd = open(raw_part, O_RDONLY); 3807c478bd9Sstevel@tonic-gate else 3817c478bd9Sstevel@tonic-gate dev_fd = open(raw_part, O_RDWR); 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 3847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw_part); 3857c478bd9Sstevel@tonic-gate exit(-1); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate if (S_ISCHR(stat.st_mode) == 0) { 3887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 3897c478bd9Sstevel@tonic-gate exit(-1); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate return (dev_fd); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate static void 3967c478bd9Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate int fd; 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate /* read the stage1 file from filesystem */ 4017c478bd9Sstevel@tonic-gate fd = open(stage1, O_RDONLY); 4027c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 4037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 4047c478bd9Sstevel@tonic-gate exit(-1); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate (void) close(fd); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* read first two blocks of stage 2 from filesystem */ 4097c478bd9Sstevel@tonic-gate stage2_fd = open(stage2, O_RDONLY); 4107c478bd9Sstevel@tonic-gate if (stage2_fd == -1 || 4117c478bd9Sstevel@tonic-gate read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 4127c478bd9Sstevel@tonic-gate != 2 * SECTOR_SIZE) { 4137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 4147c478bd9Sstevel@tonic-gate exit(-1); 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate /* leave the stage2 file open for later */ 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate static void 4207c478bd9Sstevel@tonic-gate read_bpb_sect(int dev_fd) 4217c478bd9Sstevel@tonic-gate { 4227c478bd9Sstevel@tonic-gate if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_BPB); 4247c478bd9Sstevel@tonic-gate exit(-1); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate static void 4297c478bd9Sstevel@tonic-gate read_boot_sect(char *device) 4307c478bd9Sstevel@tonic-gate { 4317c478bd9Sstevel@tonic-gate static int read_mbr = 0; 4327c478bd9Sstevel@tonic-gate int i, fd; 4337c478bd9Sstevel@tonic-gate char save[2]; 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate if (read_mbr) 4367c478bd9Sstevel@tonic-gate return; 4377c478bd9Sstevel@tonic-gate read_mbr = 1; 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* get the whole disk (p0) */ 4407c478bd9Sstevel@tonic-gate i = strlen(device); 4417c478bd9Sstevel@tonic-gate save[0] = device[i - 2]; 4427c478bd9Sstevel@tonic-gate save[1] = device[i - 1]; 4437c478bd9Sstevel@tonic-gate device[i - 2] = 'p'; 4447c478bd9Sstevel@tonic-gate device[i - 1] = '0'; 4457c478bd9Sstevel@tonic-gate 446*aa1b14e7SSheshadri Vasudevan device_p0 = strdup(device); 4477c478bd9Sstevel@tonic-gate fd = open(device, O_RDONLY); 4487c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 4497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_MBR, device); 4507c478bd9Sstevel@tonic-gate if (fd == -1) 4517c478bd9Sstevel@tonic-gate perror("open"); 4527c478bd9Sstevel@tonic-gate else 4537c478bd9Sstevel@tonic-gate perror("read"); 4547c478bd9Sstevel@tonic-gate exit(-1); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate (void) close(fd); 4577c478bd9Sstevel@tonic-gate device[i - 2] = save[0]; 4587c478bd9Sstevel@tonic-gate device[i - 1] = save[1]; 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate static void 4627c478bd9Sstevel@tonic-gate write_boot_sect(char *device) 4637c478bd9Sstevel@tonic-gate { 4647c478bd9Sstevel@tonic-gate int fd, len; 4657c478bd9Sstevel@tonic-gate char *raw, *end; 4667c478bd9Sstevel@tonic-gate struct stat stat; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* make a copy and chop off ":boot" */ 4697c478bd9Sstevel@tonic-gate raw = strdup(device); 4707c478bd9Sstevel@tonic-gate end = strstr(raw, "p0:boot"); 4717c478bd9Sstevel@tonic-gate if (end) 4727c478bd9Sstevel@tonic-gate end[2] = 0; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate /* open p0 (whole disk) */ 4757c478bd9Sstevel@tonic-gate len = strlen(raw); 4767c478bd9Sstevel@tonic-gate raw[len - 2] = 'p'; 4777c478bd9Sstevel@tonic-gate raw[len - 1] = '0'; 4787c478bd9Sstevel@tonic-gate fd = open(raw, O_WRONLY); 4797c478bd9Sstevel@tonic-gate if (fd == -1 || fstat(fd, &stat) != 0) { 4807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw); 4817c478bd9Sstevel@tonic-gate exit(-1); 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate if (!nowrite && 4847c478bd9Sstevel@tonic-gate pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 4867c478bd9Sstevel@tonic-gate exit(-1); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_MBOOT); 4897c478bd9Sstevel@tonic-gate (void) close(fd); 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate static void 4937c478bd9Sstevel@tonic-gate modify_and_write_stage1(int dev_fd) 4947c478bd9Sstevel@tonic-gate { 4957c478bd9Sstevel@tonic-gate if (is_floppy) { 4967c478bd9Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 4977c478bd9Sstevel@tonic-gate /* copy bios parameter block (for fat fs) */ 4987c478bd9Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 4997c478bd9Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 5007c478bd9Sstevel@tonic-gate } else if (is_bootpar) { 501d33344bbSsy25831 stage2_first_sector = get_start_sector(dev_fd) + blocklist[0]; 5027c478bd9Sstevel@tonic-gate /* copy bios parameter block (for fat fs) and MBR */ 5037c478bd9Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 5047c478bd9Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 5057c478bd9Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 5067c478bd9Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 5077c478bd9Sstevel@tonic-gate } else { 508d33344bbSsy25831 stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF; 5097c478bd9Sstevel@tonic-gate /* copy MBR to stage1 in case of overwriting MBR sector */ 5107c478bd9Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 5117c478bd9Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /* modify default stage1 file generated by GRUB */ 5157c478bd9Sstevel@tonic-gate *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 5167c478bd9Sstevel@tonic-gate = stage2_first_sector; 5177c478bd9Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 5187c478bd9Sstevel@tonic-gate = STAGE2_MEMADDR; 5197c478bd9Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 5207c478bd9Sstevel@tonic-gate = STAGE2_MEMADDR >> 4; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* 5237c478bd9Sstevel@tonic-gate * XXX the default grub distribution also: 5247c478bd9Sstevel@tonic-gate * - Copy the possible MBR/extended part table 5257c478bd9Sstevel@tonic-gate * - Set the boot drive of stage1 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate /* write stage1/pboot to 1st sector */ 5297c478bd9Sstevel@tonic-gate if (!nowrite && 5307c478bd9Sstevel@tonic-gate pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 5317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_PBOOT); 5327c478bd9Sstevel@tonic-gate exit(-1); 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate if (is_floppy) { 5367c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 5377c478bd9Sstevel@tonic-gate } else { 5387c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_PBOOT, 539d33344bbSsy25831 partition, get_start_sector(dev_fd)); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 5437ce76caaSEnrico Perla - Sun Microsystems static void check_extended_support(char *stage2) 5447ce76caaSEnrico Perla - Sun Microsystems { 5457ce76caaSEnrico Perla - Sun Microsystems char *cmp = stage2_buffer + STAGE2_SIGN_OFFSET - 1; 5467ce76caaSEnrico Perla - Sun Microsystems 5477ce76caaSEnrico Perla - Sun Microsystems if ((*cmp++ != '\xEE') && memcmp(cmp, extended_sig, HASH_SIZE) != 0) { 5487ce76caaSEnrico Perla - Sun Microsystems fprintf(stderr, "%s does not support extended versioning\n", 5497ce76caaSEnrico Perla - Sun Microsystems stage2); 5507ce76caaSEnrico Perla - Sun Microsystems do_version = 0; 5517ce76caaSEnrico Perla - Sun Microsystems } 5527ce76caaSEnrico Perla - Sun Microsystems } 5537ce76caaSEnrico Perla - Sun Microsystems 5547ce76caaSEnrico Perla - Sun Microsystems 5557ce76caaSEnrico Perla - Sun Microsystems static void print_info() 5567ce76caaSEnrico Perla - Sun Microsystems { 5577ce76caaSEnrico Perla - Sun Microsystems int i; 5587ce76caaSEnrico Perla - Sun Microsystems 5597ce76caaSEnrico Perla - Sun Microsystems if (strip) { 5607ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "%s\n", verstring); 5617ce76caaSEnrico Perla - Sun Microsystems } else { 5627ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "Grub extended version information : %s\n", 5637ce76caaSEnrico Perla - Sun Microsystems verstring); 5647ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "Grub stage2 (MD5) signature : "); 5657ce76caaSEnrico Perla - Sun Microsystems } 5667ce76caaSEnrico Perla - Sun Microsystems 5677ce76caaSEnrico Perla - Sun Microsystems for (i = 0; i < HASH_SIZE; i++) 5687ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "%02x", (unsigned char)signature[i]); 5697ce76caaSEnrico Perla - Sun Microsystems 5707ce76caaSEnrico Perla - Sun Microsystems fprintf(stdout, "\n"); 5717ce76caaSEnrico Perla - Sun Microsystems } 5727ce76caaSEnrico Perla - Sun Microsystems 5737ce76caaSEnrico Perla - Sun Microsystems static int 5747ce76caaSEnrico Perla - Sun Microsystems read_stage2_info(int dev_fd) 5757ce76caaSEnrico Perla - Sun Microsystems { 5767ce76caaSEnrico Perla - Sun Microsystems int ret; 5777ce76caaSEnrico Perla - Sun Microsystems int first_offset, second_offset; 5787ce76caaSEnrico Perla - Sun Microsystems char *sign; 5797ce76caaSEnrico Perla - Sun Microsystems 5807ce76caaSEnrico Perla - Sun Microsystems if (is_floppy || is_bootpar) { 5817ce76caaSEnrico Perla - Sun Microsystems 5827ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage1_buffer, SECTOR_SIZE, 0); 5837ce76caaSEnrico Perla - Sun Microsystems if (ret != SECTOR_SIZE) { 5847ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage1 sector"); 5857ce76caaSEnrico Perla - Sun Microsystems return (1); 5867ce76caaSEnrico Perla - Sun Microsystems } 5877ce76caaSEnrico Perla - Sun Microsystems 5887ce76caaSEnrico Perla - Sun Microsystems first_offset = *((ulong_t *)(stage1_buffer + 5897ce76caaSEnrico Perla - Sun Microsystems STAGE1_STAGE2_SECTOR)); 5907ce76caaSEnrico Perla - Sun Microsystems 5917ce76caaSEnrico Perla - Sun Microsystems /* Start reading in the first sector of stage 2 */ 5927ce76caaSEnrico Perla - Sun Microsystems 5937ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage2_buffer, SECTOR_SIZE, first_offset * 5947ce76caaSEnrico Perla - Sun Microsystems SECTOR_SIZE); 5957ce76caaSEnrico Perla - Sun Microsystems if (ret != SECTOR_SIZE) { 5967ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage2 first sector"); 5977ce76caaSEnrico Perla - Sun Microsystems return (1); 5987ce76caaSEnrico Perla - Sun Microsystems } 5997ce76caaSEnrico Perla - Sun Microsystems 6007ce76caaSEnrico Perla - Sun Microsystems /* From the block list section grab stage2 second sector */ 6017ce76caaSEnrico Perla - Sun Microsystems 6027ce76caaSEnrico Perla - Sun Microsystems second_offset = *((ulong_t *)(stage2_buffer + 6037ce76caaSEnrico Perla - Sun Microsystems STAGE2_BLOCKLIST)); 6047ce76caaSEnrico Perla - Sun Microsystems 6057ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 6067ce76caaSEnrico Perla - Sun Microsystems second_offset * SECTOR_SIZE); 6077ce76caaSEnrico Perla - Sun Microsystems if (ret != SECTOR_SIZE) { 6087ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage2 second sector"); 6097ce76caaSEnrico Perla - Sun Microsystems return (1); 6107ce76caaSEnrico Perla - Sun Microsystems } 6117ce76caaSEnrico Perla - Sun Microsystems } else { 6127ce76caaSEnrico Perla - Sun Microsystems ret = pread(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 6137ce76caaSEnrico Perla - Sun Microsystems STAGE2_BLKOFF * SECTOR_SIZE); 6147ce76caaSEnrico Perla - Sun Microsystems if (ret != 2 * SECTOR_SIZE) { 6157ce76caaSEnrico Perla - Sun Microsystems perror("Error reading stage2 sectors"); 6167ce76caaSEnrico Perla - Sun Microsystems return (1); 6177ce76caaSEnrico Perla - Sun Microsystems } 6187ce76caaSEnrico Perla - Sun Microsystems } 6197ce76caaSEnrico Perla - Sun Microsystems 6207ce76caaSEnrico Perla - Sun Microsystems sign = stage2_buffer + STAGE2_SIGN_OFFSET - 1; 6217ce76caaSEnrico Perla - Sun Microsystems if (*sign++ != '\xEE') 6227ce76caaSEnrico Perla - Sun Microsystems return (1); 6237ce76caaSEnrico Perla - Sun Microsystems (void) memcpy(signature, sign, HASH_SIZE); 6247ce76caaSEnrico Perla - Sun Microsystems sign = stage2_buffer + STAGE2_PKG_VERSION; 6257ce76caaSEnrico Perla - Sun Microsystems (void) strncpy(verstring, sign, VERSION_SIZE); 6267ce76caaSEnrico Perla - Sun Microsystems return (0); 6277ce76caaSEnrico Perla - Sun Microsystems } 6287ce76caaSEnrico Perla - Sun Microsystems 6297ce76caaSEnrico Perla - Sun Microsystems 6307ce76caaSEnrico Perla - Sun Microsystems static int 6317ce76caaSEnrico Perla - Sun Microsystems compute_and_write_md5hash(char *dest) 6327ce76caaSEnrico Perla - Sun Microsystems { 6337ce76caaSEnrico Perla - Sun Microsystems struct stat sb; 6347ce76caaSEnrico Perla - Sun Microsystems char *buffer; 6357ce76caaSEnrico Perla - Sun Microsystems 6367ce76caaSEnrico Perla - Sun Microsystems if (fstat(stage2_fd, &sb) == -1) 6377ce76caaSEnrico Perla - Sun Microsystems return (-1); 6387ce76caaSEnrico Perla - Sun Microsystems 6397ce76caaSEnrico Perla - Sun Microsystems buffer = malloc(sb.st_size); 6407ce76caaSEnrico Perla - Sun Microsystems if (buffer == NULL) 6417ce76caaSEnrico Perla - Sun Microsystems return (-1); 6427ce76caaSEnrico Perla - Sun Microsystems 6437ce76caaSEnrico Perla - Sun Microsystems if (lseek(stage2_fd, 0, SEEK_SET) == -1) 6447ce76caaSEnrico Perla - Sun Microsystems return (-1); 6457ce76caaSEnrico Perla - Sun Microsystems if (read(stage2_fd, buffer, sb.st_size) < 0) 6467ce76caaSEnrico Perla - Sun Microsystems return (-1); 6477ce76caaSEnrico Perla - Sun Microsystems 6487ce76caaSEnrico Perla - Sun Microsystems md5_calc(dest, buffer, sb.st_size); 6497ce76caaSEnrico Perla - Sun Microsystems free(buffer); 6507ce76caaSEnrico Perla - Sun Microsystems return (0); 6517ce76caaSEnrico Perla - Sun Microsystems } 6527ce76caaSEnrico Perla - Sun Microsystems 6537ce76caaSEnrico Perla - Sun Microsystems 6547c478bd9Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 6557c478bd9Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 6567c478bd9Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate static void 6597c478bd9Sstevel@tonic-gate modify_and_write_stage2(int dev_fd) 6607c478bd9Sstevel@tonic-gate { 6617c478bd9Sstevel@tonic-gate int nrecord; 6627c478bd9Sstevel@tonic-gate off_t offset; 6637ce76caaSEnrico Perla - Sun Microsystems char *dest; 6647ce76caaSEnrico Perla - Sun Microsystems 6657ce76caaSEnrico Perla - Sun Microsystems if (do_version) { 6667ce76caaSEnrico Perla - Sun Microsystems dest = stage2_buffer + STAGE2_SIGN_OFFSET; 6677ce76caaSEnrico Perla - Sun Microsystems if (compute_and_write_md5hash(dest) < 0) 6687ce76caaSEnrico Perla - Sun Microsystems perror("MD5 operation"); 6697ce76caaSEnrico Perla - Sun Microsystems dest = stage2_buffer + STAGE2_PKG_VERSION; 6707ce76caaSEnrico Perla - Sun Microsystems (void) strncpy(dest, verstring, VERSION_SIZE); 6717ce76caaSEnrico Perla - Sun Microsystems } 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 6747c478bd9Sstevel@tonic-gate int i = 0; 675*aa1b14e7SSheshadri Vasudevan uint32_t partition_offset; 676*aa1b14e7SSheshadri Vasudevan uint32_t install_addr = 0x8200; 6777c478bd9Sstevel@tonic-gate uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate /* figure out the second sector */ 6827c478bd9Sstevel@tonic-gate if (blocklist[1] > 1) { 6837c478bd9Sstevel@tonic-gate blocklist[0]++; 6847c478bd9Sstevel@tonic-gate blocklist[1]--; 6857c478bd9Sstevel@tonic-gate } else { 6867c478bd9Sstevel@tonic-gate i += 2; 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate stage2_second_sector = blocklist[i]; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate if (is_floppy) 6917c478bd9Sstevel@tonic-gate partition_offset = 0; 6927c478bd9Sstevel@tonic-gate else /* solaris boot partition */ 693d33344bbSsy25831 partition_offset = get_start_sector(dev_fd); 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate /* install the blocklist at the end of stage2_buffer */ 6967c478bd9Sstevel@tonic-gate while (blocklist[i]) { 6977c478bd9Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 6987c478bd9Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 6997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 7007c478bd9Sstevel@tonic-gate exit(-1); 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate START_BLOCK(pos) = blocklist[i] + partition_offset; 7037c478bd9Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 7047c478bd9Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 7057c478bd9Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 7067c478bd9Sstevel@tonic-gate pos -= 8; 7077c478bd9Sstevel@tonic-gate i += 2; 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate } else { 7117c478bd9Sstevel@tonic-gate /* 7127c478bd9Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 7137c478bd9Sstevel@tonic-gate * blocks. So we update the starting block only. 7147c478bd9Sstevel@tonic-gate */ 7157c478bd9Sstevel@tonic-gate *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 7167c478bd9Sstevel@tonic-gate stage2_first_sector + 1; 7177c478bd9Sstevel@tonic-gate } 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate if (is_floppy) { 7207c478bd9Sstevel@tonic-gate /* modify the config file to add (fd0) */ 7217c478bd9Sstevel@tonic-gate char *config_file = stage2_buffer + STAGE2_VER_STRING; 7227c478bd9Sstevel@tonic-gate while (*config_file++) 7237c478bd9Sstevel@tonic-gate ; 7247c478bd9Sstevel@tonic-gate strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 7257c478bd9Sstevel@tonic-gate } else { 7267c478bd9Sstevel@tonic-gate /* force lba and set disk partition */ 7277c478bd9Sstevel@tonic-gate *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 7287c478bd9Sstevel@tonic-gate *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 7297c478bd9Sstevel@tonic-gate = (partition << 16) | (slice << 8) | 0xff; 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate /* modification done, now do the writing */ 7337c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 7347c478bd9Sstevel@tonic-gate /* we rewrite block 0 and 1 and that's it */ 7357c478bd9Sstevel@tonic-gate if (!nowrite && 7367c478bd9Sstevel@tonic-gate (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 7377c478bd9Sstevel@tonic-gate stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 7387c478bd9Sstevel@tonic-gate pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 7397c478bd9Sstevel@tonic-gate stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 7407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 7417c478bd9Sstevel@tonic-gate exit(-1); 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_PCFS); 7447c478bd9Sstevel@tonic-gate return; 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 7487c478bd9Sstevel@tonic-gate offset = STAGE2_BLKOFF; 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate /* write the modified first two sectors */ 7517c478bd9Sstevel@tonic-gate if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 7527c478bd9Sstevel@tonic-gate offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 7537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 7547c478bd9Sstevel@tonic-gate exit(-1); 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate /* write the remaining sectors */ 7587c478bd9Sstevel@tonic-gate nrecord = 2; 7597c478bd9Sstevel@tonic-gate offset += 2; 7607c478bd9Sstevel@tonic-gate for (;;) { 7617c478bd9Sstevel@tonic-gate int nread, nwrite; 7627c478bd9Sstevel@tonic-gate nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 7637c478bd9Sstevel@tonic-gate nrecord * SECTOR_SIZE); 7647c478bd9Sstevel@tonic-gate if (nread > 0 && !nowrite) 7657c478bd9Sstevel@tonic-gate nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 7667c478bd9Sstevel@tonic-gate offset * SECTOR_SIZE); 7677c478bd9Sstevel@tonic-gate else 7687c478bd9Sstevel@tonic-gate nwrite = SECTOR_SIZE; 7697c478bd9Sstevel@tonic-gate if (nread < 0 || nwrite != SECTOR_SIZE) { 7707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 7717c478bd9Sstevel@tonic-gate nread, nwrite); 7727c478bd9Sstevel@tonic-gate break; 7737c478bd9Sstevel@tonic-gate } 774c6fe1048Sjongkis if (nread > 0) { 7757c478bd9Sstevel@tonic-gate nrecord ++; 7767c478bd9Sstevel@tonic-gate offset ++; 777c6fe1048Sjongkis } 7787c478bd9Sstevel@tonic-gate if (nread < SECTOR_SIZE) 7797c478bd9Sstevel@tonic-gate break; /* end of file */ 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_DISK, 7827c478bd9Sstevel@tonic-gate partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate static char * 7867c478bd9Sstevel@tonic-gate get_raw_partition(char *device) 7877c478bd9Sstevel@tonic-gate { 7887c478bd9Sstevel@tonic-gate int len; 7897c478bd9Sstevel@tonic-gate struct mboot *mboot; 7907c478bd9Sstevel@tonic-gate static char *raw = NULL; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate if (raw) 7937c478bd9Sstevel@tonic-gate return (raw); 7947c478bd9Sstevel@tonic-gate raw = strdup(device); 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate if (is_floppy) 7977c478bd9Sstevel@tonic-gate return (raw); 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate if (is_bootpar) { 8007c478bd9Sstevel@tonic-gate int i; 8017c478bd9Sstevel@tonic-gate char *end = strstr(raw, "p0:boot"); 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate end[2] = 0; /* chop off :boot */ 8047c478bd9Sstevel@tonic-gate read_boot_sect(raw); 8057c478bd9Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 8067c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 8077c478bd9Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i; 8087c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) /* solaris boot part */ 8097c478bd9Sstevel@tonic-gate break; 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate if (i == FD_NUMPART) { 8137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 8147c478bd9Sstevel@tonic-gate exit(-1); 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate end[1] = '1' + i; /* set partition name */ 8177c478bd9Sstevel@tonic-gate return (raw); 8187c478bd9Sstevel@tonic-gate } 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 8217c478bd9Sstevel@tonic-gate len = strlen(raw); 8227c478bd9Sstevel@tonic-gate if (raw[len - 2] != 's' || raw[len - 1] == '2') { 8237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 8247c478bd9Sstevel@tonic-gate exit(-1); 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate slice = atoi(&raw[len - 1]); 8277c478bd9Sstevel@tonic-gate 8287c478bd9Sstevel@tonic-gate raw[len - 2] = 's'; 8297c478bd9Sstevel@tonic-gate raw[len - 1] = '2'; 8307c478bd9Sstevel@tonic-gate return (raw); 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 8347c478bd9Sstevel@tonic-gate static void 8357c478bd9Sstevel@tonic-gate copy_stage2(int dev_fd, char *device) 8367c478bd9Sstevel@tonic-gate { 8377c478bd9Sstevel@tonic-gate FILE *mntfp; 8387c478bd9Sstevel@tonic-gate int i, pcfs_fp; 8397c478bd9Sstevel@tonic-gate char buf[SECTOR_SIZE]; 8407c478bd9Sstevel@tonic-gate char *cp; 8417c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 8447c478bd9Sstevel@tonic-gate (void) strncpy(buf, device, sizeof (buf)); 8457c478bd9Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 8467c478bd9Sstevel@tonic-gate cp = strchr(buf, 'r'); 8477c478bd9Sstevel@tonic-gate if (cp == NULL) { 8487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, CONVERT_FAIL, device); 8497c478bd9Sstevel@tonic-gate exit(-1); 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate do { 8527c478bd9Sstevel@tonic-gate *cp = *(cp + 1); 8537c478bd9Sstevel@tonic-gate } while (*(++cp)); 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate /* get the mount point, if any */ 8567c478bd9Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 8577c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 8587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 8597c478bd9Sstevel@tonic-gate exit(-1); 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate mpref.mnt_special = buf; 8637c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 8647c478bd9Sstevel@tonic-gate char cmd[128]; 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate /* not mounted, try remount */ 8677c478bd9Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 8687c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 8697c478bd9Sstevel@tonic-gate buf, TMP_MNTPT); 8707c478bd9Sstevel@tonic-gate (void) system(cmd); 8717c478bd9Sstevel@tonic-gate rewind(mntfp); 8727c478bd9Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 8737c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 8747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 8757c478bd9Sstevel@tonic-gate exit(-1); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 8807c478bd9Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 8817c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 8827c478bd9Sstevel@tonic-gate (void) strcat(buf, "/grub"); 8837c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 8867c478bd9Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 8877c478bd9Sstevel@tonic-gate if (pcfs_fp == -1) { 8887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 8897c478bd9Sstevel@tonic-gate perror("open:"); 8907c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 8917c478bd9Sstevel@tonic-gate exit(-1); 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate /* write stage2 to pcfs */ 8957c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 8967c478bd9Sstevel@tonic-gate int nread, nwrite; 8977c478bd9Sstevel@tonic-gate nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 8987c478bd9Sstevel@tonic-gate if (nowrite) 8997c478bd9Sstevel@tonic-gate nwrite = nread; 9007c478bd9Sstevel@tonic-gate else 9017c478bd9Sstevel@tonic-gate nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 9027c478bd9Sstevel@tonic-gate if (nread < 0 || nwrite != nread) { 9037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 9047c478bd9Sstevel@tonic-gate nread, nwrite); 9057c478bd9Sstevel@tonic-gate break; 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate if (nread < SECTOR_SIZE) 9087c478bd9Sstevel@tonic-gate break; /* end of file */ 9097c478bd9Sstevel@tonic-gate } 9107c478bd9Sstevel@tonic-gate (void) close(pcfs_fp); 9117c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate /* 9147c478bd9Sstevel@tonic-gate * Now, get the blocklist from the device. 9157c478bd9Sstevel@tonic-gate */ 9167c478bd9Sstevel@tonic-gate bzero(blocklist, sizeof (blocklist)); 9177c478bd9Sstevel@tonic-gate if (read_stage2_blocklist(dev_fd, blocklist) != 0) 9187c478bd9Sstevel@tonic-gate exit(-1); 9197c478bd9Sstevel@tonic-gate } 920