10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55589Ssy25831 * Common Development and Distribution License (the "License"). 65589Ssy25831 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 2210021SSheshadri.Vasudevan@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <stdio.h> 270Sstevel@tonic-gate #include <stdlib.h> 280Sstevel@tonic-gate #include <libgen.h> 290Sstevel@tonic-gate #include <malloc.h> 300Sstevel@tonic-gate #include <string.h> 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <sys/stat.h> 330Sstevel@tonic-gate #include <fcntl.h> 340Sstevel@tonic-gate #include <unistd.h> 350Sstevel@tonic-gate #include <strings.h> 360Sstevel@tonic-gate #include <sys/mount.h> 370Sstevel@tonic-gate #include <sys/mnttab.h> 380Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 395589Ssy25831 #include <sys/dkio.h> 405589Ssy25831 #include <sys/vtoc.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #include <libintl.h> 430Sstevel@tonic-gate #include <locale.h> 440Sstevel@tonic-gate #include "message.h" 45322Sjongkis #include <errno.h> 4610021SSheshadri.Vasudevan@Sun.COM #include <libfdisk.h> 478434SEnrico.Perla@Sun.COM #include <md5.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #ifndef TEXT_DOMAIN 500Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 510Sstevel@tonic-gate #endif 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define SECTOR_SIZE 0x200 548434SEnrico.Perla@Sun.COM #define HASH_SIZE 0x10 558434SEnrico.Perla@Sun.COM #define VERSION_SIZE 0x50 560Sstevel@tonic-gate #define STAGE2_MEMADDR 0x8000 /* loading addr of stage2 */ 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define STAGE1_BPB_OFFSET 0x3 590Sstevel@tonic-gate #define STAGE1_BPB_SIZE 0x3B 600Sstevel@tonic-gate #define STAGE1_BOOT_DRIVE 0x40 610Sstevel@tonic-gate #define STAGE1_FORCE_LBA 0x41 620Sstevel@tonic-gate #define STAGE1_STAGE2_ADDRESS 0x42 630Sstevel@tonic-gate #define STAGE1_STAGE2_SECTOR 0x44 640Sstevel@tonic-gate #define STAGE1_STAGE2_SEGMENT 0x48 650Sstevel@tonic-gate 660Sstevel@tonic-gate #define STAGE2_BLOCKLIST (SECTOR_SIZE - 0x8) 670Sstevel@tonic-gate #define STAGE2_INSTALLPART (SECTOR_SIZE + 0x8) 680Sstevel@tonic-gate #define STAGE2_FORCE_LBA (SECTOR_SIZE + 0x11) 690Sstevel@tonic-gate #define STAGE2_VER_STRING (SECTOR_SIZE + 0x12) 708434SEnrico.Perla@Sun.COM #define STAGE2_SIGN_OFFSET (SECTOR_SIZE + 0x60) 718434SEnrico.Perla@Sun.COM #define STAGE2_PKG_VERSION (SECTOR_SIZE + 0x70) 720Sstevel@tonic-gate #define STAGE2_BLKOFF 50 /* offset from start of fdisk part */ 730Sstevel@tonic-gate 748434SEnrico.Perla@Sun.COM static char extended_sig[] = "\xCC\xCC\xCC\xCC\xAA\xAA\xAA\xAA\xBB\xBB\xBB\xBB" 758434SEnrico.Perla@Sun.COM "\xBB\xBB\xBB\xBB"; 768434SEnrico.Perla@Sun.COM 770Sstevel@tonic-gate static int nowrite = 0; 780Sstevel@tonic-gate static int write_mboot = 0; 790Sstevel@tonic-gate static int force_mboot = 0; 808434SEnrico.Perla@Sun.COM static int getinfo = 0; 818434SEnrico.Perla@Sun.COM static int do_version = 0; 820Sstevel@tonic-gate static int is_floppy = 0; 830Sstevel@tonic-gate static int is_bootpar = 0; 848434SEnrico.Perla@Sun.COM static int strip = 0; 850Sstevel@tonic-gate static int stage2_fd; 860Sstevel@tonic-gate static int partition, slice = 0xff; 8710021SSheshadri.Vasudevan@Sun.COM static char *device_p0; 8810021SSheshadri.Vasudevan@Sun.COM static uint32_t stage2_first_sector, stage2_second_sector; 890Sstevel@tonic-gate 900Sstevel@tonic-gate 910Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE]; 920Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE]; 930Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE]; 940Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE]; 958434SEnrico.Perla@Sun.COM static char signature[HASH_SIZE]; 968434SEnrico.Perla@Sun.COM static char verstring[VERSION_SIZE]; 977563SPrasad.Singamsetty@Sun.COM static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)]; 980Sstevel@tonic-gate 990Sstevel@tonic-gate static int open_device(char *); 1000Sstevel@tonic-gate static void read_bpb_sect(int); 1010Sstevel@tonic-gate static void read_boot_sect(char *); 1020Sstevel@tonic-gate static void write_boot_sect(char *); 1030Sstevel@tonic-gate static void read_stage1_stage2(char *, char *); 1040Sstevel@tonic-gate static void modify_and_write_stage1(int); 1050Sstevel@tonic-gate static void modify_and_write_stage2(int); 1067563SPrasad.Singamsetty@Sun.COM static unsigned int get_start_sector(int); 1070Sstevel@tonic-gate static void copy_stage2(int, char *); 1080Sstevel@tonic-gate static char *get_raw_partition(char *); 1090Sstevel@tonic-gate static void usage(char *); 1108434SEnrico.Perla@Sun.COM static void print_info(); 1118434SEnrico.Perla@Sun.COM static int read_stage2_info(int); 1128434SEnrico.Perla@Sun.COM static void check_extended_support(); 1130Sstevel@tonic-gate 1147563SPrasad.Singamsetty@Sun.COM extern int read_stage2_blocklist(int, unsigned int *); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate int 1170Sstevel@tonic-gate main(int argc, char *argv[]) 1180Sstevel@tonic-gate { 1198434SEnrico.Perla@Sun.COM int dev_fd, opt, params = 3; 1200Sstevel@tonic-gate char *stage1, *stage2, *device; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1230Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1240Sstevel@tonic-gate 1258434SEnrico.Perla@Sun.COM while ((opt = getopt(argc, argv, "fmneis:")) != EOF) { 1260Sstevel@tonic-gate switch (opt) { 1270Sstevel@tonic-gate case 'm': 1280Sstevel@tonic-gate write_mboot = 1; 1290Sstevel@tonic-gate break; 1300Sstevel@tonic-gate case 'n': 1310Sstevel@tonic-gate nowrite = 1; 1320Sstevel@tonic-gate break; 1330Sstevel@tonic-gate case 'f': 1340Sstevel@tonic-gate force_mboot = 1; 1350Sstevel@tonic-gate break; 1368434SEnrico.Perla@Sun.COM case 'i': 1378434SEnrico.Perla@Sun.COM getinfo = 1; 1388434SEnrico.Perla@Sun.COM params = 1; 1398434SEnrico.Perla@Sun.COM break; 1408434SEnrico.Perla@Sun.COM case 'e': 1418434SEnrico.Perla@Sun.COM strip = 1; 1428434SEnrico.Perla@Sun.COM break; 1438434SEnrico.Perla@Sun.COM case 's': 1448434SEnrico.Perla@Sun.COM do_version = 1; 1458434SEnrico.Perla@Sun.COM (void) snprintf(verstring, sizeof (verstring), "%s", 1468434SEnrico.Perla@Sun.COM optarg); 1478434SEnrico.Perla@Sun.COM break; 1480Sstevel@tonic-gate default: 1490Sstevel@tonic-gate /* fall through to process non-optional args */ 1500Sstevel@tonic-gate break; 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* check arguments */ 1558434SEnrico.Perla@Sun.COM if (argc != optind + params) { 1560Sstevel@tonic-gate usage(argv[0]); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate if (nowrite) { 1600Sstevel@tonic-gate (void) fprintf(stdout, DRY_RUN); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1638434SEnrico.Perla@Sun.COM if (params == 1) { 1648434SEnrico.Perla@Sun.COM device = strdup(argv[optind]); 1658434SEnrico.Perla@Sun.COM if (!device) { 1668434SEnrico.Perla@Sun.COM usage(argv[0]); 1678434SEnrico.Perla@Sun.COM } 1688434SEnrico.Perla@Sun.COM } else if (params == 3) { 1698434SEnrico.Perla@Sun.COM stage1 = strdup(argv[optind]); 1708434SEnrico.Perla@Sun.COM stage2 = strdup(argv[optind + 1]); 1718434SEnrico.Perla@Sun.COM device = strdup(argv[optind + 2]); 1720Sstevel@tonic-gate 1738434SEnrico.Perla@Sun.COM if (!stage1 || !stage2 || !device) { 1748434SEnrico.Perla@Sun.COM usage(argv[0]); 1758434SEnrico.Perla@Sun.COM } 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* open and check device type */ 1790Sstevel@tonic-gate dev_fd = open_device(device); 1800Sstevel@tonic-gate 1818434SEnrico.Perla@Sun.COM if (getinfo) { 1828434SEnrico.Perla@Sun.COM if (read_stage2_info(dev_fd) != 0) { 1838434SEnrico.Perla@Sun.COM fprintf(stderr, "Unable to read extended information" 1848434SEnrico.Perla@Sun.COM " from %s\n", device); 1858434SEnrico.Perla@Sun.COM exit(1); 1868434SEnrico.Perla@Sun.COM } 1878434SEnrico.Perla@Sun.COM print_info(); 1888434SEnrico.Perla@Sun.COM (void) free(device); 1898434SEnrico.Perla@Sun.COM (void) close(dev_fd); 1908434SEnrico.Perla@Sun.COM return (0); 1918434SEnrico.Perla@Sun.COM } 1928434SEnrico.Perla@Sun.COM 1930Sstevel@tonic-gate /* read in stage1 and stage2 into buffer */ 1940Sstevel@tonic-gate read_stage1_stage2(stage1, stage2); 1950Sstevel@tonic-gate 1968434SEnrico.Perla@Sun.COM /* check if stage2 supports extended versioning */ 1978434SEnrico.Perla@Sun.COM if (do_version) 1988434SEnrico.Perla@Sun.COM check_extended_support(stage2); 1998434SEnrico.Perla@Sun.COM 2000Sstevel@tonic-gate /* In the pcfs case, write a fresh stage2 */ 2010Sstevel@tonic-gate if (is_floppy || is_bootpar) { 2020Sstevel@tonic-gate copy_stage2(dev_fd, device); 2030Sstevel@tonic-gate read_bpb_sect(dev_fd); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* read in boot sector */ 2070Sstevel@tonic-gate if (!is_floppy) 2080Sstevel@tonic-gate read_boot_sect(device); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* modify stage1 based on grub needs */ 2110Sstevel@tonic-gate modify_and_write_stage1(dev_fd); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* modify stage2 and write to media */ 2140Sstevel@tonic-gate modify_and_write_stage2(dev_fd); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (!is_floppy && write_mboot) 2170Sstevel@tonic-gate write_boot_sect(device); 2188434SEnrico.Perla@Sun.COM 2190Sstevel@tonic-gate (void) close(dev_fd); 2208434SEnrico.Perla@Sun.COM free(device); 2218434SEnrico.Perla@Sun.COM free(stage1); 2228434SEnrico.Perla@Sun.COM free(stage2); 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate return (0); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2277563SPrasad.Singamsetty@Sun.COM static unsigned int 2285589Ssy25831 get_start_sector(int fd) 2290Sstevel@tonic-gate { 2307563SPrasad.Singamsetty@Sun.COM static unsigned int start_sect = 0; 23110568SVikram.Hegde@Sun.COM uint32_t secnum = 0, numsec = 0; 23210568SVikram.Hegde@Sun.COM int i, pno, rval, log_part = 0; 2330Sstevel@tonic-gate struct mboot *mboot; 2340Sstevel@tonic-gate struct ipart *part; 23510021SSheshadri.Vasudevan@Sun.COM ext_part_t *epp; 23610568SVikram.Hegde@Sun.COM struct part_info dkpi; 23710568SVikram.Hegde@Sun.COM struct extpart_info edkpi; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate if (start_sect) 2400Sstevel@tonic-gate return (start_sect); 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 2430Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2440Sstevel@tonic-gate part = (struct ipart *)mboot->parts + i; 2450Sstevel@tonic-gate if (is_bootpar) { 24610568SVikram.Hegde@Sun.COM if (part->systid == 0xbe) { 24710568SVikram.Hegde@Sun.COM start_sect = part->relsect; 24810568SVikram.Hegde@Sun.COM partition = i; 24910568SVikram.Hegde@Sun.COM goto found_part; 25010568SVikram.Hegde@Sun.COM } 25110568SVikram.Hegde@Sun.COM } 25210568SVikram.Hegde@Sun.COM } 25310568SVikram.Hegde@Sun.COM 25410568SVikram.Hegde@Sun.COM /* 25510568SVikram.Hegde@Sun.COM * We will not support x86 boot partition on extended partitions 25610568SVikram.Hegde@Sun.COM */ 25710568SVikram.Hegde@Sun.COM if (is_bootpar) { 25810568SVikram.Hegde@Sun.COM (void) fprintf(stderr, NOBOOTPAR); 25910568SVikram.Hegde@Sun.COM exit(-1); 26010568SVikram.Hegde@Sun.COM } 26110568SVikram.Hegde@Sun.COM 26210568SVikram.Hegde@Sun.COM /* 26310568SVikram.Hegde@Sun.COM * Not an x86 boot partition. Search for Solaris fdisk partition 26410568SVikram.Hegde@Sun.COM * Get the solaris partition information from the device 26510568SVikram.Hegde@Sun.COM * and compare the offset of S2 with offset of solaris partition 26610568SVikram.Hegde@Sun.COM * from fdisk partition table. 26710568SVikram.Hegde@Sun.COM */ 26810568SVikram.Hegde@Sun.COM if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) { 26910568SVikram.Hegde@Sun.COM if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) { 27010568SVikram.Hegde@Sun.COM (void) fprintf(stderr, PART_FAIL); 27110568SVikram.Hegde@Sun.COM exit(-1); 27210568SVikram.Hegde@Sun.COM } else { 27310568SVikram.Hegde@Sun.COM edkpi.p_start = dkpi.p_start; 2745589Ssy25831 } 2755589Ssy25831 } 2765589Ssy25831 27710568SVikram.Hegde@Sun.COM for (i = 0; i < FD_NUMPART; i++) { 27810568SVikram.Hegde@Sun.COM part = (struct ipart *)mboot->parts + i; 27910568SVikram.Hegde@Sun.COM 28010568SVikram.Hegde@Sun.COM if (part->relsect == 0) { 28110568SVikram.Hegde@Sun.COM (void) fprintf(stderr, BAD_PART, i); 28210568SVikram.Hegde@Sun.COM exit(-1); 28310568SVikram.Hegde@Sun.COM } 28410568SVikram.Hegde@Sun.COM 28510568SVikram.Hegde@Sun.COM if (edkpi.p_start >= part->relsect && 28610568SVikram.Hegde@Sun.COM edkpi.p_start < (part->relsect + part->numsect)) { 28710568SVikram.Hegde@Sun.COM /* Found the partition */ 28810568SVikram.Hegde@Sun.COM break; 28910568SVikram.Hegde@Sun.COM } 29010568SVikram.Hegde@Sun.COM } 29110568SVikram.Hegde@Sun.COM 29210568SVikram.Hegde@Sun.COM if (i == FD_NUMPART) { 29310568SVikram.Hegde@Sun.COM /* No solaris fdisk partitions (primary or logical) */ 29410568SVikram.Hegde@Sun.COM (void) fprintf(stderr, NOSOLPAR); 29510568SVikram.Hegde@Sun.COM exit(-1); 29610568SVikram.Hegde@Sun.COM } 29710568SVikram.Hegde@Sun.COM 29810568SVikram.Hegde@Sun.COM /* 29910568SVikram.Hegde@Sun.COM * We have found a Solaris fdisk partition (primary or extended) 30010568SVikram.Hegde@Sun.COM * Handle the simple case first: Solaris in a primary partition 30110568SVikram.Hegde@Sun.COM */ 30210568SVikram.Hegde@Sun.COM if (!fdisk_is_dos_extended(part->systid)) { 30310568SVikram.Hegde@Sun.COM start_sect = part->relsect; 30410568SVikram.Hegde@Sun.COM partition = i; 30510568SVikram.Hegde@Sun.COM goto found_part; 30610568SVikram.Hegde@Sun.COM } 30710568SVikram.Hegde@Sun.COM 30810568SVikram.Hegde@Sun.COM /* 30910568SVikram.Hegde@Sun.COM * Solaris in a logical partition. Find that partition in the 31010568SVikram.Hegde@Sun.COM * extended part. 31110568SVikram.Hegde@Sun.COM */ 31210021SSheshadri.Vasudevan@Sun.COM if ((rval = libfdisk_init(&epp, device_p0, NULL, FDISK_READ_DISK)) 31310021SSheshadri.Vasudevan@Sun.COM != FDISK_SUCCESS) { 314*11246SSharath.Srinivasan@Sun.COM libfdisk_fini(&epp); 31510021SSheshadri.Vasudevan@Sun.COM switch (rval) { 31610021SSheshadri.Vasudevan@Sun.COM /* 317*11246SSharath.Srinivasan@Sun.COM * The first 3 cases are not an error per-se, just that 31810568SVikram.Hegde@Sun.COM * there is no Solaris logical partition 31910021SSheshadri.Vasudevan@Sun.COM */ 32010021SSheshadri.Vasudevan@Sun.COM case FDISK_EBADLOGDRIVE: 32110021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOLOGDRIVE: 322*11246SSharath.Srinivasan@Sun.COM case FDISK_EBADMAGIC: 32310568SVikram.Hegde@Sun.COM (void) fprintf(stderr, NOSOLPAR); 32410568SVikram.Hegde@Sun.COM exit(-1); 32510568SVikram.Hegde@Sun.COM /*NOTREACHED*/ 32610021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOVGEOM: 32710021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, NO_VIRT_GEOM); 32810021SSheshadri.Vasudevan@Sun.COM exit(1); 32910021SSheshadri.Vasudevan@Sun.COM break; 33010021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOPGEOM: 33110021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, NO_PHYS_GEOM); 33210021SSheshadri.Vasudevan@Sun.COM exit(1); 33310021SSheshadri.Vasudevan@Sun.COM break; 33410021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOLGEOM: 33510021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, NO_LABEL_GEOM); 33610021SSheshadri.Vasudevan@Sun.COM exit(1); 33710021SSheshadri.Vasudevan@Sun.COM break; 33810021SSheshadri.Vasudevan@Sun.COM default: 33910021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, LIBFDISK_INIT_FAIL); 34010021SSheshadri.Vasudevan@Sun.COM exit(1); 34110021SSheshadri.Vasudevan@Sun.COM break; 34210021SSheshadri.Vasudevan@Sun.COM } 34310021SSheshadri.Vasudevan@Sun.COM } 34410021SSheshadri.Vasudevan@Sun.COM 34510021SSheshadri.Vasudevan@Sun.COM rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec); 346*11246SSharath.Srinivasan@Sun.COM libfdisk_fini(&epp); 34710568SVikram.Hegde@Sun.COM if (rval != FDISK_SUCCESS) { 34810568SVikram.Hegde@Sun.COM /* No solaris logical partition */ 34910568SVikram.Hegde@Sun.COM (void) fprintf(stderr, NOSOLPAR); 35010568SVikram.Hegde@Sun.COM exit(-1); 35110021SSheshadri.Vasudevan@Sun.COM } 35210021SSheshadri.Vasudevan@Sun.COM 35310568SVikram.Hegde@Sun.COM start_sect = secnum; 35410568SVikram.Hegde@Sun.COM partition = pno - 1; 35510568SVikram.Hegde@Sun.COM log_part = 1; 3565589Ssy25831 35710568SVikram.Hegde@Sun.COM found_part: 3580Sstevel@tonic-gate /* get confirmation for -m */ 3590Sstevel@tonic-gate if (write_mboot && !force_mboot) { 3600Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 3610Sstevel@tonic-gate if (getchar() != 'y') { 3620Sstevel@tonic-gate write_mboot = 0; 3630Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 36710568SVikram.Hegde@Sun.COM /* 36810568SVikram.Hegde@Sun.COM * Currently if Solaris is in an extended partition we need to 36910568SVikram.Hegde@Sun.COM * write GRUB to the MBR. Check for this. 37010568SVikram.Hegde@Sun.COM */ 37110568SVikram.Hegde@Sun.COM if (log_part && !write_mboot) { 37210568SVikram.Hegde@Sun.COM (void) fprintf(stderr, EXTSOLPAR); 37310568SVikram.Hegde@Sun.COM exit(-1); 37410021SSheshadri.Vasudevan@Sun.COM } 37510021SSheshadri.Vasudevan@Sun.COM 37610568SVikram.Hegde@Sun.COM /* 37710568SVikram.Hegde@Sun.COM * warn, if Solaris in primary partition and GRUB not in MBR and 37810568SVikram.Hegde@Sun.COM * partition is not active 37910568SVikram.Hegde@Sun.COM */ 38010568SVikram.Hegde@Sun.COM if (!log_part && part->bootid != 128 && !write_mboot) { 38110568SVikram.Hegde@Sun.COM (void) fprintf(stdout, SOLPAR_INACTIVE, partition + 1); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate return (start_sect); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate static void 3880Sstevel@tonic-gate usage(char *progname) 3890Sstevel@tonic-gate { 3900Sstevel@tonic-gate (void) fprintf(stderr, USAGE, basename(progname)); 3910Sstevel@tonic-gate exit(-1); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate static int 3950Sstevel@tonic-gate open_device(char *device) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate int dev_fd; 3980Sstevel@tonic-gate struct stat stat; 3990Sstevel@tonic-gate char *raw_part; 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 4020Sstevel@tonic-gate strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* handle boot partition specification */ 4050Sstevel@tonic-gate if (!is_floppy && strstr(device, "p0:boot")) { 4060Sstevel@tonic-gate is_bootpar = 1; 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate raw_part = get_raw_partition(device); 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate if (nowrite) 4120Sstevel@tonic-gate dev_fd = open(raw_part, O_RDONLY); 4130Sstevel@tonic-gate else 4140Sstevel@tonic-gate dev_fd = open(raw_part, O_RDWR); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 4170Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw_part); 4180Sstevel@tonic-gate exit(-1); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate if (S_ISCHR(stat.st_mode) == 0) { 4210Sstevel@tonic-gate (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 4220Sstevel@tonic-gate exit(-1); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate return (dev_fd); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate static void 4290Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate int fd; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* read the stage1 file from filesystem */ 4340Sstevel@tonic-gate fd = open(stage1, O_RDONLY); 4350Sstevel@tonic-gate if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 4360Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 4370Sstevel@tonic-gate exit(-1); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate (void) close(fd); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* read first two blocks of stage 2 from filesystem */ 4420Sstevel@tonic-gate stage2_fd = open(stage2, O_RDONLY); 4430Sstevel@tonic-gate if (stage2_fd == -1 || 4440Sstevel@tonic-gate read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 4450Sstevel@tonic-gate != 2 * SECTOR_SIZE) { 4460Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 4470Sstevel@tonic-gate exit(-1); 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate /* leave the stage2 file open for later */ 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate static void 4530Sstevel@tonic-gate read_bpb_sect(int dev_fd) 4540Sstevel@tonic-gate { 4550Sstevel@tonic-gate if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4560Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_BPB); 4570Sstevel@tonic-gate exit(-1); 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate static void 4620Sstevel@tonic-gate read_boot_sect(char *device) 4630Sstevel@tonic-gate { 4640Sstevel@tonic-gate static int read_mbr = 0; 4650Sstevel@tonic-gate int i, fd; 4660Sstevel@tonic-gate char save[2]; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate if (read_mbr) 4690Sstevel@tonic-gate return; 4700Sstevel@tonic-gate read_mbr = 1; 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* get the whole disk (p0) */ 4730Sstevel@tonic-gate i = strlen(device); 4740Sstevel@tonic-gate save[0] = device[i - 2]; 4750Sstevel@tonic-gate save[1] = device[i - 1]; 4760Sstevel@tonic-gate device[i - 2] = 'p'; 4770Sstevel@tonic-gate device[i - 1] = '0'; 4780Sstevel@tonic-gate 47910021SSheshadri.Vasudevan@Sun.COM device_p0 = strdup(device); 4800Sstevel@tonic-gate fd = open(device, O_RDONLY); 4810Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 4820Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_MBR, device); 4830Sstevel@tonic-gate if (fd == -1) 4840Sstevel@tonic-gate perror("open"); 4850Sstevel@tonic-gate else 4860Sstevel@tonic-gate perror("read"); 4870Sstevel@tonic-gate exit(-1); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate (void) close(fd); 4900Sstevel@tonic-gate device[i - 2] = save[0]; 4910Sstevel@tonic-gate device[i - 1] = save[1]; 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate static void 4950Sstevel@tonic-gate write_boot_sect(char *device) 4960Sstevel@tonic-gate { 4970Sstevel@tonic-gate int fd, len; 4980Sstevel@tonic-gate char *raw, *end; 4990Sstevel@tonic-gate struct stat stat; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate /* make a copy and chop off ":boot" */ 5020Sstevel@tonic-gate raw = strdup(device); 5030Sstevel@tonic-gate end = strstr(raw, "p0:boot"); 5040Sstevel@tonic-gate if (end) 5050Sstevel@tonic-gate end[2] = 0; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* open p0 (whole disk) */ 5080Sstevel@tonic-gate len = strlen(raw); 5090Sstevel@tonic-gate raw[len - 2] = 'p'; 5100Sstevel@tonic-gate raw[len - 1] = '0'; 5110Sstevel@tonic-gate fd = open(raw, O_WRONLY); 5120Sstevel@tonic-gate if (fd == -1 || fstat(fd, &stat) != 0) { 5130Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw); 5140Sstevel@tonic-gate exit(-1); 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate if (!nowrite && 5170Sstevel@tonic-gate pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 5180Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 5190Sstevel@tonic-gate exit(-1); 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate (void) fprintf(stdout, WRITE_MBOOT); 5220Sstevel@tonic-gate (void) close(fd); 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate static void 5260Sstevel@tonic-gate modify_and_write_stage1(int dev_fd) 5270Sstevel@tonic-gate { 5280Sstevel@tonic-gate if (is_floppy) { 5290Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 5300Sstevel@tonic-gate /* copy bios parameter block (for fat fs) */ 5310Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 5320Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 5330Sstevel@tonic-gate } else if (is_bootpar) { 5345589Ssy25831 stage2_first_sector = get_start_sector(dev_fd) + blocklist[0]; 5350Sstevel@tonic-gate /* copy bios parameter block (for fat fs) and MBR */ 5360Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 5370Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 5380Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 5390Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 5400Sstevel@tonic-gate } else { 5415589Ssy25831 stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF; 5420Sstevel@tonic-gate /* copy MBR to stage1 in case of overwriting MBR sector */ 5430Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 5440Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate /* modify default stage1 file generated by GRUB */ 5480Sstevel@tonic-gate *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 5495589Ssy25831 = stage2_first_sector; 5500Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 5515589Ssy25831 = STAGE2_MEMADDR; 5520Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 5535589Ssy25831 = STAGE2_MEMADDR >> 4; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate /* 5560Sstevel@tonic-gate * XXX the default grub distribution also: 5570Sstevel@tonic-gate * - Copy the possible MBR/extended part table 5580Sstevel@tonic-gate * - Set the boot drive of stage1 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate /* write stage1/pboot to 1st sector */ 5620Sstevel@tonic-gate if (!nowrite && 5630Sstevel@tonic-gate pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 5640Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_PBOOT); 5650Sstevel@tonic-gate exit(-1); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate if (is_floppy) { 5690Sstevel@tonic-gate (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 5700Sstevel@tonic-gate } else { 5710Sstevel@tonic-gate (void) fprintf(stdout, WRITE_PBOOT, 5725589Ssy25831 partition, get_start_sector(dev_fd)); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5768434SEnrico.Perla@Sun.COM static void check_extended_support(char *stage2) 5778434SEnrico.Perla@Sun.COM { 5788434SEnrico.Perla@Sun.COM char *cmp = stage2_buffer + STAGE2_SIGN_OFFSET - 1; 5798434SEnrico.Perla@Sun.COM 5808434SEnrico.Perla@Sun.COM if ((*cmp++ != '\xEE') && memcmp(cmp, extended_sig, HASH_SIZE) != 0) { 5818434SEnrico.Perla@Sun.COM fprintf(stderr, "%s does not support extended versioning\n", 5828434SEnrico.Perla@Sun.COM stage2); 5838434SEnrico.Perla@Sun.COM do_version = 0; 5848434SEnrico.Perla@Sun.COM } 5858434SEnrico.Perla@Sun.COM } 5868434SEnrico.Perla@Sun.COM 5878434SEnrico.Perla@Sun.COM 5888434SEnrico.Perla@Sun.COM static void print_info() 5898434SEnrico.Perla@Sun.COM { 5908434SEnrico.Perla@Sun.COM int i; 5918434SEnrico.Perla@Sun.COM 5928434SEnrico.Perla@Sun.COM if (strip) { 5938434SEnrico.Perla@Sun.COM fprintf(stdout, "%s\n", verstring); 5948434SEnrico.Perla@Sun.COM } else { 5958434SEnrico.Perla@Sun.COM fprintf(stdout, "Grub extended version information : %s\n", 5968434SEnrico.Perla@Sun.COM verstring); 5978434SEnrico.Perla@Sun.COM fprintf(stdout, "Grub stage2 (MD5) signature : "); 5988434SEnrico.Perla@Sun.COM } 5998434SEnrico.Perla@Sun.COM 6008434SEnrico.Perla@Sun.COM for (i = 0; i < HASH_SIZE; i++) 6018434SEnrico.Perla@Sun.COM fprintf(stdout, "%02x", (unsigned char)signature[i]); 6028434SEnrico.Perla@Sun.COM 6038434SEnrico.Perla@Sun.COM fprintf(stdout, "\n"); 6048434SEnrico.Perla@Sun.COM } 6058434SEnrico.Perla@Sun.COM 6068434SEnrico.Perla@Sun.COM static int 6078434SEnrico.Perla@Sun.COM read_stage2_info(int dev_fd) 6088434SEnrico.Perla@Sun.COM { 6098434SEnrico.Perla@Sun.COM int ret; 6108434SEnrico.Perla@Sun.COM int first_offset, second_offset; 6118434SEnrico.Perla@Sun.COM char *sign; 6128434SEnrico.Perla@Sun.COM 6138434SEnrico.Perla@Sun.COM if (is_floppy || is_bootpar) { 6148434SEnrico.Perla@Sun.COM 6158434SEnrico.Perla@Sun.COM ret = pread(dev_fd, stage1_buffer, SECTOR_SIZE, 0); 6168434SEnrico.Perla@Sun.COM if (ret != SECTOR_SIZE) { 6178434SEnrico.Perla@Sun.COM perror("Error reading stage1 sector"); 6188434SEnrico.Perla@Sun.COM return (1); 6198434SEnrico.Perla@Sun.COM } 6208434SEnrico.Perla@Sun.COM 6218434SEnrico.Perla@Sun.COM first_offset = *((ulong_t *)(stage1_buffer + 6228434SEnrico.Perla@Sun.COM STAGE1_STAGE2_SECTOR)); 6238434SEnrico.Perla@Sun.COM 6248434SEnrico.Perla@Sun.COM /* Start reading in the first sector of stage 2 */ 6258434SEnrico.Perla@Sun.COM 6268434SEnrico.Perla@Sun.COM ret = pread(dev_fd, stage2_buffer, SECTOR_SIZE, first_offset * 6278434SEnrico.Perla@Sun.COM SECTOR_SIZE); 6288434SEnrico.Perla@Sun.COM if (ret != SECTOR_SIZE) { 6298434SEnrico.Perla@Sun.COM perror("Error reading stage2 first sector"); 6308434SEnrico.Perla@Sun.COM return (1); 6318434SEnrico.Perla@Sun.COM } 6328434SEnrico.Perla@Sun.COM 6338434SEnrico.Perla@Sun.COM /* From the block list section grab stage2 second sector */ 6348434SEnrico.Perla@Sun.COM 6358434SEnrico.Perla@Sun.COM second_offset = *((ulong_t *)(stage2_buffer + 6368434SEnrico.Perla@Sun.COM STAGE2_BLOCKLIST)); 6378434SEnrico.Perla@Sun.COM 6388434SEnrico.Perla@Sun.COM ret = pread(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 6398434SEnrico.Perla@Sun.COM second_offset * SECTOR_SIZE); 6408434SEnrico.Perla@Sun.COM if (ret != SECTOR_SIZE) { 6418434SEnrico.Perla@Sun.COM perror("Error reading stage2 second sector"); 6428434SEnrico.Perla@Sun.COM return (1); 6438434SEnrico.Perla@Sun.COM } 6448434SEnrico.Perla@Sun.COM } else { 6458434SEnrico.Perla@Sun.COM ret = pread(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 6468434SEnrico.Perla@Sun.COM STAGE2_BLKOFF * SECTOR_SIZE); 6478434SEnrico.Perla@Sun.COM if (ret != 2 * SECTOR_SIZE) { 6488434SEnrico.Perla@Sun.COM perror("Error reading stage2 sectors"); 6498434SEnrico.Perla@Sun.COM return (1); 6508434SEnrico.Perla@Sun.COM } 6518434SEnrico.Perla@Sun.COM } 6528434SEnrico.Perla@Sun.COM 6538434SEnrico.Perla@Sun.COM sign = stage2_buffer + STAGE2_SIGN_OFFSET - 1; 6548434SEnrico.Perla@Sun.COM if (*sign++ != '\xEE') 6558434SEnrico.Perla@Sun.COM return (1); 6568434SEnrico.Perla@Sun.COM (void) memcpy(signature, sign, HASH_SIZE); 6578434SEnrico.Perla@Sun.COM sign = stage2_buffer + STAGE2_PKG_VERSION; 6588434SEnrico.Perla@Sun.COM (void) strncpy(verstring, sign, VERSION_SIZE); 6598434SEnrico.Perla@Sun.COM return (0); 6608434SEnrico.Perla@Sun.COM } 6618434SEnrico.Perla@Sun.COM 6628434SEnrico.Perla@Sun.COM 6638434SEnrico.Perla@Sun.COM static int 6648434SEnrico.Perla@Sun.COM compute_and_write_md5hash(char *dest) 6658434SEnrico.Perla@Sun.COM { 6668434SEnrico.Perla@Sun.COM struct stat sb; 6678434SEnrico.Perla@Sun.COM char *buffer; 6688434SEnrico.Perla@Sun.COM 6698434SEnrico.Perla@Sun.COM if (fstat(stage2_fd, &sb) == -1) 6708434SEnrico.Perla@Sun.COM return (-1); 6718434SEnrico.Perla@Sun.COM 6728434SEnrico.Perla@Sun.COM buffer = malloc(sb.st_size); 6738434SEnrico.Perla@Sun.COM if (buffer == NULL) 6748434SEnrico.Perla@Sun.COM return (-1); 6758434SEnrico.Perla@Sun.COM 6768434SEnrico.Perla@Sun.COM if (lseek(stage2_fd, 0, SEEK_SET) == -1) 6778434SEnrico.Perla@Sun.COM return (-1); 6788434SEnrico.Perla@Sun.COM if (read(stage2_fd, buffer, sb.st_size) < 0) 6798434SEnrico.Perla@Sun.COM return (-1); 6808434SEnrico.Perla@Sun.COM 6818434SEnrico.Perla@Sun.COM md5_calc(dest, buffer, sb.st_size); 6828434SEnrico.Perla@Sun.COM free(buffer); 6838434SEnrico.Perla@Sun.COM return (0); 6848434SEnrico.Perla@Sun.COM } 6858434SEnrico.Perla@Sun.COM 6868434SEnrico.Perla@Sun.COM 6870Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 6880Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 6890Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate static void 6920Sstevel@tonic-gate modify_and_write_stage2(int dev_fd) 6930Sstevel@tonic-gate { 6948434SEnrico.Perla@Sun.COM int nrecord; 6958434SEnrico.Perla@Sun.COM off_t offset; 6968434SEnrico.Perla@Sun.COM char *dest; 6978434SEnrico.Perla@Sun.COM 6988434SEnrico.Perla@Sun.COM if (do_version) { 6998434SEnrico.Perla@Sun.COM dest = stage2_buffer + STAGE2_SIGN_OFFSET; 7008434SEnrico.Perla@Sun.COM if (compute_and_write_md5hash(dest) < 0) 7018434SEnrico.Perla@Sun.COM perror("MD5 operation"); 7028434SEnrico.Perla@Sun.COM dest = stage2_buffer + STAGE2_PKG_VERSION; 7038434SEnrico.Perla@Sun.COM (void) strncpy(dest, verstring, VERSION_SIZE); 7048434SEnrico.Perla@Sun.COM } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate if (is_floppy || is_bootpar) { 7070Sstevel@tonic-gate int i = 0; 70810021SSheshadri.Vasudevan@Sun.COM uint32_t partition_offset; 70910021SSheshadri.Vasudevan@Sun.COM uint32_t install_addr = 0x8200; 7100Sstevel@tonic-gate uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* figure out the second sector */ 7150Sstevel@tonic-gate if (blocklist[1] > 1) { 7160Sstevel@tonic-gate blocklist[0]++; 7170Sstevel@tonic-gate blocklist[1]--; 7180Sstevel@tonic-gate } else { 7190Sstevel@tonic-gate i += 2; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate stage2_second_sector = blocklist[i]; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (is_floppy) 7240Sstevel@tonic-gate partition_offset = 0; 7250Sstevel@tonic-gate else /* solaris boot partition */ 7265589Ssy25831 partition_offset = get_start_sector(dev_fd); 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate /* install the blocklist at the end of stage2_buffer */ 7290Sstevel@tonic-gate while (blocklist[i]) { 7300Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 7310Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 7320Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 7330Sstevel@tonic-gate exit(-1); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate START_BLOCK(pos) = blocklist[i] + partition_offset; 7360Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 7370Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 7380Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 7390Sstevel@tonic-gate pos -= 8; 7400Sstevel@tonic-gate i += 2; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate } else { 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 7460Sstevel@tonic-gate * blocks. So we update the starting block only. 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 7490Sstevel@tonic-gate stage2_first_sector + 1; 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate if (is_floppy) { 7530Sstevel@tonic-gate /* modify the config file to add (fd0) */ 7540Sstevel@tonic-gate char *config_file = stage2_buffer + STAGE2_VER_STRING; 7550Sstevel@tonic-gate while (*config_file++) 7560Sstevel@tonic-gate ; 7570Sstevel@tonic-gate strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 7580Sstevel@tonic-gate } else { 7590Sstevel@tonic-gate /* force lba and set disk partition */ 7600Sstevel@tonic-gate *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 7610Sstevel@tonic-gate *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 7620Sstevel@tonic-gate = (partition << 16) | (slice << 8) | 0xff; 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* modification done, now do the writing */ 7660Sstevel@tonic-gate if (is_floppy || is_bootpar) { 7670Sstevel@tonic-gate /* we rewrite block 0 and 1 and that's it */ 7680Sstevel@tonic-gate if (!nowrite && 7690Sstevel@tonic-gate (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 7700Sstevel@tonic-gate stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 7710Sstevel@tonic-gate pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 7720Sstevel@tonic-gate stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 7730Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 7740Sstevel@tonic-gate exit(-1); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_PCFS); 7770Sstevel@tonic-gate return; 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 7810Sstevel@tonic-gate offset = STAGE2_BLKOFF; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* write the modified first two sectors */ 7840Sstevel@tonic-gate if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 7850Sstevel@tonic-gate offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 7860Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 7870Sstevel@tonic-gate exit(-1); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* write the remaining sectors */ 7910Sstevel@tonic-gate nrecord = 2; 7920Sstevel@tonic-gate offset += 2; 7930Sstevel@tonic-gate for (;;) { 7940Sstevel@tonic-gate int nread, nwrite; 7950Sstevel@tonic-gate nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 7960Sstevel@tonic-gate nrecord * SECTOR_SIZE); 7970Sstevel@tonic-gate if (nread > 0 && !nowrite) 7980Sstevel@tonic-gate nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 7990Sstevel@tonic-gate offset * SECTOR_SIZE); 8000Sstevel@tonic-gate else 8010Sstevel@tonic-gate nwrite = SECTOR_SIZE; 8020Sstevel@tonic-gate if (nread < 0 || nwrite != SECTOR_SIZE) { 8030Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 8040Sstevel@tonic-gate nread, nwrite); 8050Sstevel@tonic-gate break; 8060Sstevel@tonic-gate } 807322Sjongkis if (nread > 0) { 808322Sjongkis nrecord ++; 809322Sjongkis offset ++; 810322Sjongkis } 8110Sstevel@tonic-gate if (nread < SECTOR_SIZE) 8120Sstevel@tonic-gate break; /* end of file */ 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_DISK, 8150Sstevel@tonic-gate partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate static char * 8190Sstevel@tonic-gate get_raw_partition(char *device) 8200Sstevel@tonic-gate { 8210Sstevel@tonic-gate int len; 8220Sstevel@tonic-gate struct mboot *mboot; 8230Sstevel@tonic-gate static char *raw = NULL; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (raw) 8260Sstevel@tonic-gate return (raw); 8270Sstevel@tonic-gate raw = strdup(device); 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate if (is_floppy) 8300Sstevel@tonic-gate return (raw); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate if (is_bootpar) { 8330Sstevel@tonic-gate int i; 8340Sstevel@tonic-gate char *end = strstr(raw, "p0:boot"); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate end[2] = 0; /* chop off :boot */ 8370Sstevel@tonic-gate read_boot_sect(raw); 8380Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 8390Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 8400Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i; 8410Sstevel@tonic-gate if (part->systid == 0xbe) /* solaris boot part */ 8420Sstevel@tonic-gate break; 8430Sstevel@tonic-gate } 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate if (i == FD_NUMPART) { 8460Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 8470Sstevel@tonic-gate exit(-1); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate end[1] = '1' + i; /* set partition name */ 8500Sstevel@tonic-gate return (raw); 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 8540Sstevel@tonic-gate len = strlen(raw); 8550Sstevel@tonic-gate if (raw[len - 2] != 's' || raw[len - 1] == '2') { 8560Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 8570Sstevel@tonic-gate exit(-1); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate slice = atoi(&raw[len - 1]); 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate raw[len - 2] = 's'; 8620Sstevel@tonic-gate raw[len - 1] = '2'; 8630Sstevel@tonic-gate return (raw); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 8670Sstevel@tonic-gate static void 8680Sstevel@tonic-gate copy_stage2(int dev_fd, char *device) 8690Sstevel@tonic-gate { 8700Sstevel@tonic-gate FILE *mntfp; 8710Sstevel@tonic-gate int i, pcfs_fp; 8720Sstevel@tonic-gate char buf[SECTOR_SIZE]; 8730Sstevel@tonic-gate char *cp; 8740Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 8770Sstevel@tonic-gate (void) strncpy(buf, device, sizeof (buf)); 8780Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 8790Sstevel@tonic-gate cp = strchr(buf, 'r'); 8800Sstevel@tonic-gate if (cp == NULL) { 8810Sstevel@tonic-gate (void) fprintf(stderr, CONVERT_FAIL, device); 8820Sstevel@tonic-gate exit(-1); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate do { 8850Sstevel@tonic-gate *cp = *(cp + 1); 8860Sstevel@tonic-gate } while (*(++cp)); 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate /* get the mount point, if any */ 8890Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 8900Sstevel@tonic-gate if (mntfp == NULL) { 8910Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 8920Sstevel@tonic-gate exit(-1); 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate mpref.mnt_special = buf; 8960Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 8970Sstevel@tonic-gate char cmd[128]; 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate /* not mounted, try remount */ 9000Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 9010Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 9020Sstevel@tonic-gate buf, TMP_MNTPT); 9030Sstevel@tonic-gate (void) system(cmd); 9040Sstevel@tonic-gate rewind(mntfp); 9050Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 9060Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 9070Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 9080Sstevel@tonic-gate exit(-1); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 9130Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 9140Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 9150Sstevel@tonic-gate (void) strcat(buf, "/grub"); 9160Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 9190Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 9200Sstevel@tonic-gate if (pcfs_fp == -1) { 9210Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 9220Sstevel@tonic-gate perror("open:"); 9230Sstevel@tonic-gate (void) umount(TMP_MNTPT); 9240Sstevel@tonic-gate exit(-1); 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate /* write stage2 to pcfs */ 9280Sstevel@tonic-gate for (i = 0; ; i++) { 9290Sstevel@tonic-gate int nread, nwrite; 9300Sstevel@tonic-gate nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 9310Sstevel@tonic-gate if (nowrite) 9320Sstevel@tonic-gate nwrite = nread; 9330Sstevel@tonic-gate else 9340Sstevel@tonic-gate nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 9350Sstevel@tonic-gate if (nread < 0 || nwrite != nread) { 9360Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 9370Sstevel@tonic-gate nread, nwrite); 9380Sstevel@tonic-gate break; 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate if (nread < SECTOR_SIZE) 9410Sstevel@tonic-gate break; /* end of file */ 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate (void) close(pcfs_fp); 9440Sstevel@tonic-gate (void) umount(TMP_MNTPT); 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate /* 9470Sstevel@tonic-gate * Now, get the blocklist from the device. 9480Sstevel@tonic-gate */ 9490Sstevel@tonic-gate bzero(blocklist, sizeof (blocklist)); 9500Sstevel@tonic-gate if (read_stage2_blocklist(dev_fd, blocklist) != 0) 9510Sstevel@tonic-gate exit(-1); 9520Sstevel@tonic-gate } 953