17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d33344bbSsy25831 * Common Development and Distribution License (the "License"). 6d33344bbSsy25831 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22d46ed901Ssetje * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <stdio.h> 277c478bd9Sstevel@tonic-gate #include <stdlib.h> 287c478bd9Sstevel@tonic-gate #include <libgen.h> 297c478bd9Sstevel@tonic-gate #include <malloc.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/stat.h> 337c478bd9Sstevel@tonic-gate #include <fcntl.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <strings.h> 367c478bd9Sstevel@tonic-gate #include <sys/mount.h> 377c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 387c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 39d33344bbSsy25831 #include <sys/dkio.h> 40d33344bbSsy25831 #include <sys/vtoc.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <libintl.h> 437c478bd9Sstevel@tonic-gate #include <locale.h> 447c478bd9Sstevel@tonic-gate #include "message.h" 45c6fe1048Sjongkis #include <errno.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 487c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 497c478bd9Sstevel@tonic-gate #endif 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 0x200 527c478bd9Sstevel@tonic-gate #define STAGE2_MEMADDR 0x8000 /* loading addr of stage2 */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #define STAGE1_BPB_OFFSET 0x3 557c478bd9Sstevel@tonic-gate #define STAGE1_BPB_SIZE 0x3B 567c478bd9Sstevel@tonic-gate #define STAGE1_BOOT_DRIVE 0x40 577c478bd9Sstevel@tonic-gate #define STAGE1_FORCE_LBA 0x41 587c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_ADDRESS 0x42 597c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_SECTOR 0x44 607c478bd9Sstevel@tonic-gate #define STAGE1_STAGE2_SEGMENT 0x48 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #define STAGE2_BLOCKLIST (SECTOR_SIZE - 0x8) 637c478bd9Sstevel@tonic-gate #define STAGE2_INSTALLPART (SECTOR_SIZE + 0x8) 647c478bd9Sstevel@tonic-gate #define STAGE2_FORCE_LBA (SECTOR_SIZE + 0x11) 657c478bd9Sstevel@tonic-gate #define STAGE2_VER_STRING (SECTOR_SIZE + 0x12) 667c478bd9Sstevel@tonic-gate #define STAGE2_BLKOFF 50 /* offset from start of fdisk part */ 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static int nowrite = 0; 697c478bd9Sstevel@tonic-gate static int write_mboot = 0; 707c478bd9Sstevel@tonic-gate static int force_mboot = 0; 717c478bd9Sstevel@tonic-gate static int is_floppy = 0; 727c478bd9Sstevel@tonic-gate static int is_bootpar = 0; 737c478bd9Sstevel@tonic-gate static int stage2_fd; 747c478bd9Sstevel@tonic-gate static int partition, slice = 0xff; 75*342440ecSPrasad Singamsetty static unsigned int stage2_first_sector, stage2_second_sector; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate static char bpb_sect[SECTOR_SIZE]; 797c478bd9Sstevel@tonic-gate static char boot_sect[SECTOR_SIZE]; 807c478bd9Sstevel@tonic-gate static char stage1_buffer[SECTOR_SIZE]; 817c478bd9Sstevel@tonic-gate static char stage2_buffer[2 * SECTOR_SIZE]; 82*342440ecSPrasad Singamsetty static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)]; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static int open_device(char *); 857c478bd9Sstevel@tonic-gate static void read_bpb_sect(int); 867c478bd9Sstevel@tonic-gate static void read_boot_sect(char *); 877c478bd9Sstevel@tonic-gate static void write_boot_sect(char *); 887c478bd9Sstevel@tonic-gate static void read_stage1_stage2(char *, char *); 897c478bd9Sstevel@tonic-gate static void modify_and_write_stage1(int); 907c478bd9Sstevel@tonic-gate static void modify_and_write_stage2(int); 91*342440ecSPrasad Singamsetty static unsigned int get_start_sector(int); 927c478bd9Sstevel@tonic-gate static void copy_stage2(int, char *); 937c478bd9Sstevel@tonic-gate static char *get_raw_partition(char *); 947c478bd9Sstevel@tonic-gate static void usage(char *); 957c478bd9Sstevel@tonic-gate 96*342440ecSPrasad Singamsetty extern int read_stage2_blocklist(int, unsigned int *); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate int 997c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1007c478bd9Sstevel@tonic-gate { 1017c478bd9Sstevel@tonic-gate int dev_fd, opt; 1027c478bd9Sstevel@tonic-gate char *stage1, *stage2, *device; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1057c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "fmn")) != EOF) { 1087c478bd9Sstevel@tonic-gate switch (opt) { 1097c478bd9Sstevel@tonic-gate case 'm': 1107c478bd9Sstevel@tonic-gate write_mboot = 1; 1117c478bd9Sstevel@tonic-gate break; 1127c478bd9Sstevel@tonic-gate case 'n': 1137c478bd9Sstevel@tonic-gate nowrite = 1; 1147c478bd9Sstevel@tonic-gate break; 1157c478bd9Sstevel@tonic-gate case 'f': 1167c478bd9Sstevel@tonic-gate force_mboot = 1; 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate default: 1197c478bd9Sstevel@tonic-gate /* fall through to process non-optional args */ 1207c478bd9Sstevel@tonic-gate break; 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* check arguments */ 1257c478bd9Sstevel@tonic-gate if (argc != optind + 3) { 1267c478bd9Sstevel@tonic-gate usage(argv[0]); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate if (nowrite) { 1307c478bd9Sstevel@tonic-gate (void) fprintf(stdout, DRY_RUN); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate stage1 = strdup(argv[optind]); 1347c478bd9Sstevel@tonic-gate stage2 = strdup(argv[optind + 1]); 1357c478bd9Sstevel@tonic-gate device = strdup(argv[optind + 2]); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate if (!stage1 || !stage2 || !device) { 1387c478bd9Sstevel@tonic-gate usage(argv[0]); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* open and check device type */ 1427c478bd9Sstevel@tonic-gate dev_fd = open_device(device); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* read in stage1 and stage2 into buffer */ 1457c478bd9Sstevel@tonic-gate read_stage1_stage2(stage1, stage2); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* In the pcfs case, write a fresh stage2 */ 1487c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 1497c478bd9Sstevel@tonic-gate copy_stage2(dev_fd, device); 1507c478bd9Sstevel@tonic-gate read_bpb_sect(dev_fd); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* read in boot sector */ 1547c478bd9Sstevel@tonic-gate if (!is_floppy) 1557c478bd9Sstevel@tonic-gate read_boot_sect(device); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* modify stage1 based on grub needs */ 1587c478bd9Sstevel@tonic-gate modify_and_write_stage1(dev_fd); 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* modify stage2 and write to media */ 1617c478bd9Sstevel@tonic-gate modify_and_write_stage2(dev_fd); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (!is_floppy && write_mboot) 1647c478bd9Sstevel@tonic-gate write_boot_sect(device); 1657c478bd9Sstevel@tonic-gate (void) close(dev_fd); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate return (0); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 170*342440ecSPrasad Singamsetty static unsigned int 171d33344bbSsy25831 get_start_sector(int fd) 1727c478bd9Sstevel@tonic-gate { 173*342440ecSPrasad Singamsetty static unsigned int start_sect = 0; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate int i; 1767c478bd9Sstevel@tonic-gate struct mboot *mboot; 1777c478bd9Sstevel@tonic-gate struct ipart *part; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (start_sect) 1807c478bd9Sstevel@tonic-gate return (start_sect); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 1837c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 1847c478bd9Sstevel@tonic-gate part = (struct ipart *)mboot->parts + i; 1857c478bd9Sstevel@tonic-gate if (is_bootpar) { 1867c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) 1877c478bd9Sstevel@tonic-gate break; 188d33344bbSsy25831 } 189d33344bbSsy25831 } 190d33344bbSsy25831 191d33344bbSsy25831 /* 192d33344bbSsy25831 * If there is no boot partition, find the solaris partition 193d33344bbSsy25831 */ 194d33344bbSsy25831 195d33344bbSsy25831 if (i == FD_NUMPART) { 196d33344bbSsy25831 struct part_info dkpi; 197*342440ecSPrasad Singamsetty struct extpart_info edkpi; 198d33344bbSsy25831 199d33344bbSsy25831 /* 200d33344bbSsy25831 * Get the solaris partition information from the device 201d33344bbSsy25831 * and compare the offset of S2 with offset of solaris partition 202d33344bbSsy25831 * from fdisk partition table. 203d33344bbSsy25831 */ 204*342440ecSPrasad Singamsetty if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) { 205d33344bbSsy25831 if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) { 206d33344bbSsy25831 (void) fprintf(stderr, PART_FAIL); 207d33344bbSsy25831 exit(-1); 208*342440ecSPrasad Singamsetty } else { 209*342440ecSPrasad Singamsetty edkpi.p_start = dkpi.p_start; 210*342440ecSPrasad Singamsetty } 211d33344bbSsy25831 } 212d33344bbSsy25831 213d33344bbSsy25831 for (i = 0; i < FD_NUMPART; i++) { 214d33344bbSsy25831 part = (struct ipart *)mboot->parts + i; 215d33344bbSsy25831 216d33344bbSsy25831 if (part->relsect == 0) { 217d33344bbSsy25831 (void) fprintf(stderr, BAD_PART, i); 218d33344bbSsy25831 exit(-1); 219d33344bbSsy25831 } 220*342440ecSPrasad Singamsetty if (edkpi.p_start >= part->relsect && 221*342440ecSPrasad Singamsetty edkpi.p_start < (part->relsect + part->numsect)) { 222d33344bbSsy25831 /* Found the partition */ 2237c478bd9Sstevel@tonic-gate break; 224d33344bbSsy25831 } 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate if (i == FD_NUMPART) { 2297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR); 2307c478bd9Sstevel@tonic-gate exit(-1); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* get confirmation for -m */ 2347c478bd9Sstevel@tonic-gate if (write_mboot && !force_mboot) { 2357c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_PROMPT); 2367c478bd9Sstevel@tonic-gate if (getchar() != 'y') { 2377c478bd9Sstevel@tonic-gate write_mboot = 0; 2387c478bd9Sstevel@tonic-gate (void) fprintf(stdout, MBOOT_NOT_UPDATED); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate start_sect = part->relsect; 2437c478bd9Sstevel@tonic-gate if (part->bootid != 128 && write_mboot == 0) { 2447c478bd9Sstevel@tonic-gate (void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate partition = i; 2487c478bd9Sstevel@tonic-gate return (start_sect); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate static void 2527c478bd9Sstevel@tonic-gate usage(char *progname) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, USAGE, basename(progname)); 2557c478bd9Sstevel@tonic-gate exit(-1); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate static int 2597c478bd9Sstevel@tonic-gate open_device(char *device) 2607c478bd9Sstevel@tonic-gate { 2617c478bd9Sstevel@tonic-gate int dev_fd; 2627c478bd9Sstevel@tonic-gate struct stat stat; 2637c478bd9Sstevel@tonic-gate char *raw_part; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 2667c478bd9Sstevel@tonic-gate strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* handle boot partition specification */ 2697c478bd9Sstevel@tonic-gate if (!is_floppy && strstr(device, "p0:boot")) { 2707c478bd9Sstevel@tonic-gate is_bootpar = 1; 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate raw_part = get_raw_partition(device); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate if (nowrite) 2767c478bd9Sstevel@tonic-gate dev_fd = open(raw_part, O_RDONLY); 2777c478bd9Sstevel@tonic-gate else 2787c478bd9Sstevel@tonic-gate dev_fd = open(raw_part, O_RDWR); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 2817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw_part); 2827c478bd9Sstevel@tonic-gate exit(-1); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate if (S_ISCHR(stat.st_mode) == 0) { 2857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 2867c478bd9Sstevel@tonic-gate exit(-1); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate return (dev_fd); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate static void 2937c478bd9Sstevel@tonic-gate read_stage1_stage2(char *stage1, char *stage2) 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate int fd; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* read the stage1 file from filesystem */ 2987c478bd9Sstevel@tonic-gate fd = open(stage1, O_RDONLY); 2997c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 3007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 3017c478bd9Sstevel@tonic-gate exit(-1); 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate (void) close(fd); 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* read first two blocks of stage 2 from filesystem */ 3067c478bd9Sstevel@tonic-gate stage2_fd = open(stage2, O_RDONLY); 3077c478bd9Sstevel@tonic-gate if (stage2_fd == -1 || 3087c478bd9Sstevel@tonic-gate read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 3097c478bd9Sstevel@tonic-gate != 2 * SECTOR_SIZE) { 3107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 3117c478bd9Sstevel@tonic-gate exit(-1); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate /* leave the stage2 file open for later */ 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate static void 3177c478bd9Sstevel@tonic-gate read_bpb_sect(int dev_fd) 3187c478bd9Sstevel@tonic-gate { 3197c478bd9Sstevel@tonic-gate if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 3207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_BPB); 3217c478bd9Sstevel@tonic-gate exit(-1); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate static void 3267c478bd9Sstevel@tonic-gate read_boot_sect(char *device) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate static int read_mbr = 0; 3297c478bd9Sstevel@tonic-gate int i, fd; 3307c478bd9Sstevel@tonic-gate char save[2]; 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if (read_mbr) 3337c478bd9Sstevel@tonic-gate return; 3347c478bd9Sstevel@tonic-gate read_mbr = 1; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* get the whole disk (p0) */ 3377c478bd9Sstevel@tonic-gate i = strlen(device); 3387c478bd9Sstevel@tonic-gate save[0] = device[i - 2]; 3397c478bd9Sstevel@tonic-gate save[1] = device[i - 1]; 3407c478bd9Sstevel@tonic-gate device[i - 2] = 'p'; 3417c478bd9Sstevel@tonic-gate device[i - 1] = '0'; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate fd = open(device, O_RDONLY); 3447c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 3457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, READ_FAIL_MBR, device); 3467c478bd9Sstevel@tonic-gate if (fd == -1) 3477c478bd9Sstevel@tonic-gate perror("open"); 3487c478bd9Sstevel@tonic-gate else 3497c478bd9Sstevel@tonic-gate perror("read"); 3507c478bd9Sstevel@tonic-gate exit(-1); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate (void) close(fd); 3537c478bd9Sstevel@tonic-gate device[i - 2] = save[0]; 3547c478bd9Sstevel@tonic-gate device[i - 1] = save[1]; 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate static void 3587c478bd9Sstevel@tonic-gate write_boot_sect(char *device) 3597c478bd9Sstevel@tonic-gate { 3607c478bd9Sstevel@tonic-gate int fd, len; 3617c478bd9Sstevel@tonic-gate char *raw, *end; 3627c478bd9Sstevel@tonic-gate struct stat stat; 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* make a copy and chop off ":boot" */ 3657c478bd9Sstevel@tonic-gate raw = strdup(device); 3667c478bd9Sstevel@tonic-gate end = strstr(raw, "p0:boot"); 3677c478bd9Sstevel@tonic-gate if (end) 3687c478bd9Sstevel@tonic-gate end[2] = 0; 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* open p0 (whole disk) */ 3717c478bd9Sstevel@tonic-gate len = strlen(raw); 3727c478bd9Sstevel@tonic-gate raw[len - 2] = 'p'; 3737c478bd9Sstevel@tonic-gate raw[len - 1] = '0'; 3747c478bd9Sstevel@tonic-gate fd = open(raw, O_WRONLY); 3757c478bd9Sstevel@tonic-gate if (fd == -1 || fstat(fd, &stat) != 0) { 3767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL, raw); 3777c478bd9Sstevel@tonic-gate exit(-1); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate if (!nowrite && 3807c478bd9Sstevel@tonic-gate pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 3817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 3827c478bd9Sstevel@tonic-gate exit(-1); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_MBOOT); 3857c478bd9Sstevel@tonic-gate (void) close(fd); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate static void 3897c478bd9Sstevel@tonic-gate modify_and_write_stage1(int dev_fd) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate if (is_floppy) { 3927c478bd9Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 3937c478bd9Sstevel@tonic-gate /* copy bios parameter block (for fat fs) */ 3947c478bd9Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 3957c478bd9Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 3967c478bd9Sstevel@tonic-gate } else if (is_bootpar) { 397d33344bbSsy25831 stage2_first_sector = get_start_sector(dev_fd) + blocklist[0]; 3987c478bd9Sstevel@tonic-gate /* copy bios parameter block (for fat fs) and MBR */ 3997c478bd9Sstevel@tonic-gate bcopy(bpb_sect + STAGE1_BPB_OFFSET, 4007c478bd9Sstevel@tonic-gate stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 4017c478bd9Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 4027c478bd9Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 4037c478bd9Sstevel@tonic-gate } else { 404d33344bbSsy25831 stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF; 4057c478bd9Sstevel@tonic-gate /* copy MBR to stage1 in case of overwriting MBR sector */ 4067c478bd9Sstevel@tonic-gate bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 4077c478bd9Sstevel@tonic-gate *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate /* modify default stage1 file generated by GRUB */ 4117c478bd9Sstevel@tonic-gate *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 4127c478bd9Sstevel@tonic-gate = stage2_first_sector; 4137c478bd9Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 4147c478bd9Sstevel@tonic-gate = STAGE2_MEMADDR; 4157c478bd9Sstevel@tonic-gate *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 4167c478bd9Sstevel@tonic-gate = STAGE2_MEMADDR >> 4; 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * XXX the default grub distribution also: 4207c478bd9Sstevel@tonic-gate * - Copy the possible MBR/extended part table 4217c478bd9Sstevel@tonic-gate * - Set the boot drive of stage1 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* write stage1/pboot to 1st sector */ 4257c478bd9Sstevel@tonic-gate if (!nowrite && 4267c478bd9Sstevel@tonic-gate pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 4277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_PBOOT); 4287c478bd9Sstevel@tonic-gate exit(-1); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate if (is_floppy) { 4327c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 4337c478bd9Sstevel@tonic-gate } else { 4347c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_PBOOT, 435d33344bbSsy25831 partition, get_start_sector(dev_fd)); 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate #define START_BLOCK(pos) (*(ulong_t *)(pos)) 4407c478bd9Sstevel@tonic-gate #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 4417c478bd9Sstevel@tonic-gate #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate static void 4447c478bd9Sstevel@tonic-gate modify_and_write_stage2(int dev_fd) 4457c478bd9Sstevel@tonic-gate { 4467c478bd9Sstevel@tonic-gate int nrecord; 4477c478bd9Sstevel@tonic-gate off_t offset; 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 4507c478bd9Sstevel@tonic-gate int i = 0; 4517c478bd9Sstevel@tonic-gate uint_t partition_offset; 4527c478bd9Sstevel@tonic-gate uint_t install_addr = 0x8200; 4537c478bd9Sstevel@tonic-gate uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate stage2_first_sector = blocklist[0]; 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate /* figure out the second sector */ 4587c478bd9Sstevel@tonic-gate if (blocklist[1] > 1) { 4597c478bd9Sstevel@tonic-gate blocklist[0]++; 4607c478bd9Sstevel@tonic-gate blocklist[1]--; 4617c478bd9Sstevel@tonic-gate } else { 4627c478bd9Sstevel@tonic-gate i += 2; 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate stage2_second_sector = blocklist[i]; 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate if (is_floppy) 4677c478bd9Sstevel@tonic-gate partition_offset = 0; 4687c478bd9Sstevel@tonic-gate else /* solaris boot partition */ 469d33344bbSsy25831 partition_offset = get_start_sector(dev_fd); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate /* install the blocklist at the end of stage2_buffer */ 4727c478bd9Sstevel@tonic-gate while (blocklist[i]) { 4737c478bd9Sstevel@tonic-gate if (START_BLOCK(pos - 8) != 0 && 4747c478bd9Sstevel@tonic-gate START_BLOCK(pos - 8) != blocklist[i + 2]) { 4757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PCFS_FRAGMENTED); 4767c478bd9Sstevel@tonic-gate exit(-1); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate START_BLOCK(pos) = blocklist[i] + partition_offset; 4797c478bd9Sstevel@tonic-gate START_SEG(pos) = (ushort_t)(install_addr >> 4); 4807c478bd9Sstevel@tonic-gate NUM_BLOCK(pos) = blocklist[i + 1]; 4817c478bd9Sstevel@tonic-gate install_addr += blocklist[i + 1] * SECTOR_SIZE; 4827c478bd9Sstevel@tonic-gate pos -= 8; 4837c478bd9Sstevel@tonic-gate i += 2; 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate } else { 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * In a solaris partition, stage2 is written to contiguous 4897c478bd9Sstevel@tonic-gate * blocks. So we update the starting block only. 4907c478bd9Sstevel@tonic-gate */ 4917c478bd9Sstevel@tonic-gate *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 4927c478bd9Sstevel@tonic-gate stage2_first_sector + 1; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate if (is_floppy) { 4967c478bd9Sstevel@tonic-gate /* modify the config file to add (fd0) */ 4977c478bd9Sstevel@tonic-gate char *config_file = stage2_buffer + STAGE2_VER_STRING; 4987c478bd9Sstevel@tonic-gate while (*config_file++) 4997c478bd9Sstevel@tonic-gate ; 5007c478bd9Sstevel@tonic-gate strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 5017c478bd9Sstevel@tonic-gate } else { 5027c478bd9Sstevel@tonic-gate /* force lba and set disk partition */ 5037c478bd9Sstevel@tonic-gate *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 5047c478bd9Sstevel@tonic-gate *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 5057c478bd9Sstevel@tonic-gate = (partition << 16) | (slice << 8) | 0xff; 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate /* modification done, now do the writing */ 5097c478bd9Sstevel@tonic-gate if (is_floppy || is_bootpar) { 5107c478bd9Sstevel@tonic-gate /* we rewrite block 0 and 1 and that's it */ 5117c478bd9Sstevel@tonic-gate if (!nowrite && 5127c478bd9Sstevel@tonic-gate (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 5137c478bd9Sstevel@tonic-gate stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 5147c478bd9Sstevel@tonic-gate pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 5157c478bd9Sstevel@tonic-gate stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 5167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 5177c478bd9Sstevel@tonic-gate exit(-1); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_PCFS); 5207c478bd9Sstevel@tonic-gate return; 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 5247c478bd9Sstevel@tonic-gate offset = STAGE2_BLKOFF; 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* write the modified first two sectors */ 5277c478bd9Sstevel@tonic-gate if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 5287c478bd9Sstevel@tonic-gate offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 5297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2); 5307c478bd9Sstevel@tonic-gate exit(-1); 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate /* write the remaining sectors */ 5347c478bd9Sstevel@tonic-gate nrecord = 2; 5357c478bd9Sstevel@tonic-gate offset += 2; 5367c478bd9Sstevel@tonic-gate for (;;) { 5377c478bd9Sstevel@tonic-gate int nread, nwrite; 5387c478bd9Sstevel@tonic-gate nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 5397c478bd9Sstevel@tonic-gate nrecord * SECTOR_SIZE); 5407c478bd9Sstevel@tonic-gate if (nread > 0 && !nowrite) 5417c478bd9Sstevel@tonic-gate nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 5427c478bd9Sstevel@tonic-gate offset * SECTOR_SIZE); 5437c478bd9Sstevel@tonic-gate else 5447c478bd9Sstevel@tonic-gate nwrite = SECTOR_SIZE; 5457c478bd9Sstevel@tonic-gate if (nread < 0 || nwrite != SECTOR_SIZE) { 5467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 5477c478bd9Sstevel@tonic-gate nread, nwrite); 5487c478bd9Sstevel@tonic-gate break; 5497c478bd9Sstevel@tonic-gate } 550c6fe1048Sjongkis if (nread > 0) { 5517c478bd9Sstevel@tonic-gate nrecord ++; 5527c478bd9Sstevel@tonic-gate offset ++; 553c6fe1048Sjongkis } 5547c478bd9Sstevel@tonic-gate if (nread < SECTOR_SIZE) 5557c478bd9Sstevel@tonic-gate break; /* end of file */ 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate (void) fprintf(stdout, WRITE_STAGE2_DISK, 5587c478bd9Sstevel@tonic-gate partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate static char * 5627c478bd9Sstevel@tonic-gate get_raw_partition(char *device) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate int len; 5657c478bd9Sstevel@tonic-gate struct mboot *mboot; 5667c478bd9Sstevel@tonic-gate static char *raw = NULL; 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (raw) 5697c478bd9Sstevel@tonic-gate return (raw); 5707c478bd9Sstevel@tonic-gate raw = strdup(device); 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate if (is_floppy) 5737c478bd9Sstevel@tonic-gate return (raw); 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate if (is_bootpar) { 5767c478bd9Sstevel@tonic-gate int i; 5777c478bd9Sstevel@tonic-gate char *end = strstr(raw, "p0:boot"); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate end[2] = 0; /* chop off :boot */ 5807c478bd9Sstevel@tonic-gate read_boot_sect(raw); 5817c478bd9Sstevel@tonic-gate mboot = (struct mboot *)boot_sect; 5827c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 5837c478bd9Sstevel@tonic-gate struct ipart *part = (struct ipart *)mboot->parts + i; 5847c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) /* solaris boot part */ 5857c478bd9Sstevel@tonic-gate break; 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate if (i == FD_NUMPART) { 5897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 5907c478bd9Sstevel@tonic-gate exit(-1); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate end[1] = '1' + i; /* set partition name */ 5937c478bd9Sstevel@tonic-gate return (raw); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* For disk, remember slice and return whole fdisk partition */ 5977c478bd9Sstevel@tonic-gate len = strlen(raw); 5987c478bd9Sstevel@tonic-gate if (raw[len - 2] != 's' || raw[len - 1] == '2') { 5997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, NOT_ROOT_SLICE); 6007c478bd9Sstevel@tonic-gate exit(-1); 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate slice = atoi(&raw[len - 1]); 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate raw[len - 2] = 's'; 6057c478bd9Sstevel@tonic-gate raw[len - 1] = '2'; 6067c478bd9Sstevel@tonic-gate return (raw); 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate #define TMP_MNTPT "/tmp/installgrub_pcfs" 6107c478bd9Sstevel@tonic-gate static void 6117c478bd9Sstevel@tonic-gate copy_stage2(int dev_fd, char *device) 6127c478bd9Sstevel@tonic-gate { 6137c478bd9Sstevel@tonic-gate FILE *mntfp; 6147c478bd9Sstevel@tonic-gate int i, pcfs_fp; 6157c478bd9Sstevel@tonic-gate char buf[SECTOR_SIZE]; 6167c478bd9Sstevel@tonic-gate char *cp; 6177c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* convert raw to block device name by removing the first 'r' */ 6207c478bd9Sstevel@tonic-gate (void) strncpy(buf, device, sizeof (buf)); 6217c478bd9Sstevel@tonic-gate buf[sizeof (buf) - 1] = 0; 6227c478bd9Sstevel@tonic-gate cp = strchr(buf, 'r'); 6237c478bd9Sstevel@tonic-gate if (cp == NULL) { 6247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, CONVERT_FAIL, device); 6257c478bd9Sstevel@tonic-gate exit(-1); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate do { 6287c478bd9Sstevel@tonic-gate *cp = *(cp + 1); 6297c478bd9Sstevel@tonic-gate } while (*(++cp)); 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate /* get the mount point, if any */ 6327c478bd9Sstevel@tonic-gate mntfp = fopen("/etc/mnttab", "r"); 6337c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 6347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 6357c478bd9Sstevel@tonic-gate exit(-1); 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate mpref.mnt_special = buf; 6397c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 6407c478bd9Sstevel@tonic-gate char cmd[128]; 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate /* not mounted, try remount */ 6437c478bd9Sstevel@tonic-gate (void) mkdir(TMP_MNTPT, S_IRWXU); 6447c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 6457c478bd9Sstevel@tonic-gate buf, TMP_MNTPT); 6467c478bd9Sstevel@tonic-gate (void) system(cmd); 6477c478bd9Sstevel@tonic-gate rewind(mntfp); 6487c478bd9Sstevel@tonic-gate bzero(&mp, sizeof (mp)); 6497c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 6507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, MOUNT_FAIL, buf); 6517c478bd9Sstevel@tonic-gate exit(-1); 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate } 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 6567c478bd9Sstevel@tonic-gate "%s/boot", mp.mnt_mountp); 6577c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 6587c478bd9Sstevel@tonic-gate (void) strcat(buf, "/grub"); 6597c478bd9Sstevel@tonic-gate (void) mkdir(buf, S_IRWXU); 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate (void) strcat(buf, "/stage2"); 6627c478bd9Sstevel@tonic-gate pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 6637c478bd9Sstevel@tonic-gate if (pcfs_fp == -1) { 6647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 6657c478bd9Sstevel@tonic-gate perror("open:"); 6667c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 6677c478bd9Sstevel@tonic-gate exit(-1); 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate /* write stage2 to pcfs */ 6717c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 6727c478bd9Sstevel@tonic-gate int nread, nwrite; 6737c478bd9Sstevel@tonic-gate nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 6747c478bd9Sstevel@tonic-gate if (nowrite) 6757c478bd9Sstevel@tonic-gate nwrite = nread; 6767c478bd9Sstevel@tonic-gate else 6777c478bd9Sstevel@tonic-gate nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 6787c478bd9Sstevel@tonic-gate if (nread < 0 || nwrite != nread) { 6797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 6807c478bd9Sstevel@tonic-gate nread, nwrite); 6817c478bd9Sstevel@tonic-gate break; 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate if (nread < SECTOR_SIZE) 6847c478bd9Sstevel@tonic-gate break; /* end of file */ 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate (void) close(pcfs_fp); 6877c478bd9Sstevel@tonic-gate (void) umount(TMP_MNTPT); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate /* 6907c478bd9Sstevel@tonic-gate * Now, get the blocklist from the device. 6917c478bd9Sstevel@tonic-gate */ 6927c478bd9Sstevel@tonic-gate bzero(blocklist, sizeof (blocklist)); 6937c478bd9Sstevel@tonic-gate if (read_stage2_blocklist(dev_fd, blocklist) != 0) 6947c478bd9Sstevel@tonic-gate exit(-1); 6957c478bd9Sstevel@tonic-gate } 696