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 5*3f2f09c1Sdp * Common Development and Distribution License (the "License"). 6*3f2f09c1Sdp * 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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 227c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate * 277c478bd9Sstevel@tonic-gate * Portions of this source code were provided by International 287c478bd9Sstevel@tonic-gate * Computers Limited (ICL) under a development agreement with AT&T. 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 34*3f2f09c1Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 357c478bd9Sstevel@tonic-gate * Use is subject to license terms. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * Sun Microsystems version of fmthard: 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * Supports the following arguments: 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * -i Writes VTOC to stdout, rather than disk 447c478bd9Sstevel@tonic-gate * -q Quick check: exit code 0 if VTOC ok 457c478bd9Sstevel@tonic-gate * -d <data> Incremental changes to the VTOC 467c478bd9Sstevel@tonic-gate * -n <vname> Change volume name to <vname> 477c478bd9Sstevel@tonic-gate * -s <file> Read VTOC information from <file>, or stdin ("-") 487c478bd9Sstevel@tonic-gate * -u <state> Reboot after writing VTOC, according to <state>: 497c478bd9Sstevel@tonic-gate * boot: AD_BOOT (standard reboot) 507c478bd9Sstevel@tonic-gate * firm: AD_IBOOT (interactive reboot) 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * Note that fmthard cannot write a VTOC on an unlabeled disk. 537c478bd9Sstevel@tonic-gate * You must use format or SunInstall for this purpose. 547c478bd9Sstevel@tonic-gate * (NOTE: the above restriction only applies on Sparc systems). 557c478bd9Sstevel@tonic-gate * 567c478bd9Sstevel@tonic-gate * The primary motivation for fmthard is to duplicate the 577c478bd9Sstevel@tonic-gate * partitioning from disk to disk: 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * prtvtoc /dev/rdsk/c0t0d0s2 | fmthard -s - /dev/rdsk/c0t1d0s2 607c478bd9Sstevel@tonic-gate */ 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #include <stdio.h> 637c478bd9Sstevel@tonic-gate #include <fcntl.h> 647c478bd9Sstevel@tonic-gate #include <errno.h> 657c478bd9Sstevel@tonic-gate #include <string.h> 667c478bd9Sstevel@tonic-gate #include <stdlib.h> 677c478bd9Sstevel@tonic-gate #include <unistd.h> 687c478bd9Sstevel@tonic-gate #include <sys/types.h> 697c478bd9Sstevel@tonic-gate #include <sys/param.h> 707c478bd9Sstevel@tonic-gate #include <sys/stat.h> 717c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 727c478bd9Sstevel@tonic-gate #include <sys/open.h> 737c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 747c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 757c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 767c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h> 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 797c478bd9Sstevel@tonic-gate #include <sys/dklabel.h> 807c478bd9Sstevel@tonic-gate #endif 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate #ifndef SECSIZE 857c478bd9Sstevel@tonic-gate #define SECSIZE DEV_BSIZE 867c478bd9Sstevel@tonic-gate #endif /* SECSIZE */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * External functions. 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate extern int read_vtoc(int, struct vtoc *); 937c478bd9Sstevel@tonic-gate extern int write_vtoc(int, struct vtoc *); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Internal functions. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate extern int main(int, char **); 997c478bd9Sstevel@tonic-gate static void display(struct dk_geom *, struct vtoc *, char *); 1007c478bd9Sstevel@tonic-gate static void display64(struct dk_gpt *, char *); 1017c478bd9Sstevel@tonic-gate static void insert(char *, struct vtoc *); 1027c478bd9Sstevel@tonic-gate static void insert64(char *, struct dk_gpt *); 1037c478bd9Sstevel@tonic-gate static void load(FILE *, struct dk_geom *, struct vtoc *); 1047c478bd9Sstevel@tonic-gate static void load64(FILE *, int fd, struct dk_gpt **); 1057c478bd9Sstevel@tonic-gate static void usage(void); 1067c478bd9Sstevel@tonic-gate static void validate(struct dk_geom *, struct vtoc *); 1077c478bd9Sstevel@tonic-gate static void validate64(struct dk_gpt *); 1087c478bd9Sstevel@tonic-gate static int vread(int, struct vtoc *, char *); 1097c478bd9Sstevel@tonic-gate static void vread64(int, struct dk_gpt **, char *); 1107c478bd9Sstevel@tonic-gate static void vwrite(int, struct vtoc *, char *); 1117c478bd9Sstevel@tonic-gate static void vwrite64(int, struct dk_gpt *, char *); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* 1147c478bd9Sstevel@tonic-gate * Static variables. 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate static char *delta; /* Incremental update */ 1177c478bd9Sstevel@tonic-gate static short eflag; /* force write of an EFI label */ 1187c478bd9Sstevel@tonic-gate static short iflag; /* Prints VTOC w/o updating */ 1197c478bd9Sstevel@tonic-gate static short qflag; /* Check for a formatted disk */ 1207c478bd9Sstevel@tonic-gate static short uflag; /* Exit to firmware after writing */ 1217c478bd9Sstevel@tonic-gate /* new vtoc and reboot. Used during */ 1227c478bd9Sstevel@tonic-gate /* installation of core floppies */ 1237c478bd9Sstevel@tonic-gate static diskaddr_t lastlba = 0; /* last LBA on 64-bit VTOC */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate #if defined(sparc) 1267c478bd9Sstevel@tonic-gate static char *uboot = "boot"; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #elif defined(i386) 1297c478bd9Sstevel@tonic-gate /* use installgrub(1M) to install boot blocks */ 1307c478bd9Sstevel@tonic-gate static char *uboot = ""; 1317c478bd9Sstevel@tonic-gate #else 1327c478bd9Sstevel@tonic-gate #error No platform defined. 1337c478bd9Sstevel@tonic-gate #endif /* various platform-specific definitions */ 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate static char *ufirm = "firm"; 1367c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1377c478bd9Sstevel@tonic-gate static int sectsiz; 1387c478bd9Sstevel@tonic-gate static struct vtoc disk_vtoc; 1397c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */ 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate int 1427c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate int fd; 1457c478bd9Sstevel@tonic-gate int c; 1467c478bd9Sstevel@tonic-gate char *dfile; 1477c478bd9Sstevel@tonic-gate char *vname; 1487c478bd9Sstevel@tonic-gate struct stat statbuf; 1497c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8) 1507c478bd9Sstevel@tonic-gate struct vtoc disk_vtoc; 1517c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_8) */ 1527c478bd9Sstevel@tonic-gate struct dk_gpt *disk_efi; 1537c478bd9Sstevel@tonic-gate struct dk_geom disk_geom; 1547c478bd9Sstevel@tonic-gate int n; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate 1572f7fafa7Szl149053 disk_efi = NULL; 1587c478bd9Sstevel@tonic-gate dfile = NULL; 1597c478bd9Sstevel@tonic-gate vname = NULL; 1607c478bd9Sstevel@tonic-gate #if defined(sparc) 1617c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ed:u:in:qs:")) != EOF) 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate #elif defined(i386) 1647c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ed:u:in:qb:p:s:")) != EOF) 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate #else 1677c478bd9Sstevel@tonic-gate #error No platform defined. 1687c478bd9Sstevel@tonic-gate #endif 1697c478bd9Sstevel@tonic-gate switch (c) { 1707c478bd9Sstevel@tonic-gate #if defined(i386) 1717c478bd9Sstevel@tonic-gate case 'p': 1727c478bd9Sstevel@tonic-gate case 'b': 1737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1747c478bd9Sstevel@tonic-gate "fmthard: -p and -b no longer supported." 1757c478bd9Sstevel@tonic-gate " Use installgrub(1M) to install boot blocks\n"); 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate #endif /* defined(i386) */ 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate case 'd': 1807c478bd9Sstevel@tonic-gate delta = optarg; 1817c478bd9Sstevel@tonic-gate break; 1827c478bd9Sstevel@tonic-gate case 'e': 1837c478bd9Sstevel@tonic-gate ++eflag; 1847c478bd9Sstevel@tonic-gate break; 1857c478bd9Sstevel@tonic-gate case 'i': 1867c478bd9Sstevel@tonic-gate ++iflag; 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate case 'n': 1897c478bd9Sstevel@tonic-gate vname = optarg; 1907c478bd9Sstevel@tonic-gate break; 1917c478bd9Sstevel@tonic-gate case 'q': 1927c478bd9Sstevel@tonic-gate ++qflag; 1937c478bd9Sstevel@tonic-gate break; 1947c478bd9Sstevel@tonic-gate case 's': 1957c478bd9Sstevel@tonic-gate dfile = optarg; 1967c478bd9Sstevel@tonic-gate break; 1977c478bd9Sstevel@tonic-gate case 'u': 1987c478bd9Sstevel@tonic-gate if (strcmp(uboot, optarg) == 0) 1997c478bd9Sstevel@tonic-gate ++uflag; 2007c478bd9Sstevel@tonic-gate else if (strcmp(ufirm, optarg) == 0) 2017c478bd9Sstevel@tonic-gate uflag = 2; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate break; 2047c478bd9Sstevel@tonic-gate default: 2057c478bd9Sstevel@tonic-gate usage(); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate if (argc - optind != 1) 2107c478bd9Sstevel@tonic-gate usage(); 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate if (stat(argv[optind], (struct stat *)&statbuf) == -1) { 2137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2147c478bd9Sstevel@tonic-gate "fmthard: Cannot stat device %s\n", 2157c478bd9Sstevel@tonic-gate argv[optind]); 2167c478bd9Sstevel@tonic-gate exit(1); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 2207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2217c478bd9Sstevel@tonic-gate "fmthard: %s must be a raw device.\n", 2227c478bd9Sstevel@tonic-gate argv[optind]); 2237c478bd9Sstevel@tonic-gate exit(1); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate if ((fd = open(argv[optind], O_RDWR|O_NDELAY)) < 0) { 2277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fmthard: Cannot open device %s - %s\n", 228ace1a5f1Sdp argv[optind], strerror(errno)); 2297c478bd9Sstevel@tonic-gate exit(1); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate /* 2337c478bd9Sstevel@tonic-gate * Get the geometry information for this disk from the driver 2347c478bd9Sstevel@tonic-gate */ 2357c478bd9Sstevel@tonic-gate if (!eflag && ioctl(fd, DKIOCGGEOM, &disk_geom)) { 2367c478bd9Sstevel@tonic-gate #ifdef DEBUG 2377c478bd9Sstevel@tonic-gate perror("DKIOCGGEOM failed"); 2387c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 2397c478bd9Sstevel@tonic-gate if (errno == ENOTSUP) { 2407c478bd9Sstevel@tonic-gate /* disk has EFI labels */ 2417c478bd9Sstevel@tonic-gate eflag++; 2427c478bd9Sstevel@tonic-gate } else { 2437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2447c478bd9Sstevel@tonic-gate "%s: Cannot get disk geometry\n", argv[optind]); 2457c478bd9Sstevel@tonic-gate (void) close(fd); 2467c478bd9Sstevel@tonic-gate exit(1); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* 2517c478bd9Sstevel@tonic-gate * Read the vtoc on the disk 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate if (!eflag) { 2547c478bd9Sstevel@tonic-gate if (vread(fd, &disk_vtoc, argv[optind]) == 1) 2557c478bd9Sstevel@tonic-gate eflag++; 2567c478bd9Sstevel@tonic-gate } 2572f7fafa7Szl149053 if (eflag && ((dfile == NULL) || qflag)) { 2587c478bd9Sstevel@tonic-gate vread64(fd, &disk_efi, argv[optind]); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * Quick check for valid disk: 0 if ok, 1 if not 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate if (qflag) { 2657c478bd9Sstevel@tonic-gate (void) close(fd); 2667c478bd9Sstevel@tonic-gate if (!eflag) { 2677c478bd9Sstevel@tonic-gate exit(disk_vtoc.v_sanity == VTOC_SANE ? 0 : 1); 2687c478bd9Sstevel@tonic-gate } else { 2697c478bd9Sstevel@tonic-gate exit(disk_efi->efi_version <= EFI_VERSION102 ? 0 : 1); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Incremental changes to the VTOC 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate if (delta) { 2777c478bd9Sstevel@tonic-gate if (!eflag) { 2787c478bd9Sstevel@tonic-gate insert(delta, &disk_vtoc); 2797c478bd9Sstevel@tonic-gate validate(&disk_geom, &disk_vtoc); 2807c478bd9Sstevel@tonic-gate vwrite(fd, &disk_vtoc, argv[optind]); 2817c478bd9Sstevel@tonic-gate } else { 2827c478bd9Sstevel@tonic-gate insert64(delta, disk_efi); 2837c478bd9Sstevel@tonic-gate validate64(disk_efi); 2847c478bd9Sstevel@tonic-gate vwrite64(fd, disk_efi, argv[optind]); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate (void) close(fd); 2877c478bd9Sstevel@tonic-gate exit(0); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (!dfile && !vname) 2917c478bd9Sstevel@tonic-gate usage(); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* 2947c478bd9Sstevel@tonic-gate * Read new VTOC from stdin or data file 2957c478bd9Sstevel@tonic-gate */ 2967c478bd9Sstevel@tonic-gate if (dfile) { 2977c478bd9Sstevel@tonic-gate if (strcmp(dfile, "-") == 0) { 2987c478bd9Sstevel@tonic-gate if (!eflag) 2997c478bd9Sstevel@tonic-gate load(stdin, &disk_geom, &disk_vtoc); 3007c478bd9Sstevel@tonic-gate else 3017c478bd9Sstevel@tonic-gate load64(stdin, fd, &disk_efi); 3027c478bd9Sstevel@tonic-gate } else { 3037c478bd9Sstevel@tonic-gate FILE *fp; 3047c478bd9Sstevel@tonic-gate if ((fp = fopen(dfile, "r")) == NULL) { 3057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Cannot open file %s\n", 3067c478bd9Sstevel@tonic-gate dfile); 3077c478bd9Sstevel@tonic-gate (void) close(fd); 3087c478bd9Sstevel@tonic-gate exit(1); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate if (!eflag) 3117c478bd9Sstevel@tonic-gate load(fp, &disk_geom, &disk_vtoc); 3127c478bd9Sstevel@tonic-gate else 3137c478bd9Sstevel@tonic-gate load64(fp, fd, &disk_efi); 3147c478bd9Sstevel@tonic-gate (void) fclose(fp); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* 3207c478bd9Sstevel@tonic-gate * Print the modified VTOC, rather than updating the disk 3217c478bd9Sstevel@tonic-gate */ 3227c478bd9Sstevel@tonic-gate if (iflag) { 3237c478bd9Sstevel@tonic-gate if (!eflag) 3247c478bd9Sstevel@tonic-gate display(&disk_geom, &disk_vtoc, argv[optind]); 3257c478bd9Sstevel@tonic-gate else 3267c478bd9Sstevel@tonic-gate display64(disk_efi, argv[optind]); 3277c478bd9Sstevel@tonic-gate (void) close(fd); 3287c478bd9Sstevel@tonic-gate exit(0); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate if (vname) { 3327c478bd9Sstevel@tonic-gate n = MIN(strlen(vname) + 1, LEN_DKL_VVOL); 3337c478bd9Sstevel@tonic-gate if (!eflag) { 3347c478bd9Sstevel@tonic-gate (void) memcpy(disk_vtoc.v_volume, vname, n); 3357c478bd9Sstevel@tonic-gate } else { 3367c478bd9Sstevel@tonic-gate for (c = 0; c < disk_efi->efi_nparts; c++) { 3377c478bd9Sstevel@tonic-gate if (disk_efi->efi_parts[c].p_tag == 3387c478bd9Sstevel@tonic-gate V_RESERVED) { 3397c478bd9Sstevel@tonic-gate (void) memcpy(&disk_efi->efi_parts[c].p_name, 3407c478bd9Sstevel@tonic-gate vname, n); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate /* 3477c478bd9Sstevel@tonic-gate * Write the new VTOC on the disk 3487c478bd9Sstevel@tonic-gate */ 3497c478bd9Sstevel@tonic-gate if (!eflag) { 3507c478bd9Sstevel@tonic-gate validate(&disk_geom, &disk_vtoc); 3517c478bd9Sstevel@tonic-gate vwrite(fd, &disk_vtoc, argv[optind]); 3527c478bd9Sstevel@tonic-gate } else { 3537c478bd9Sstevel@tonic-gate validate64(disk_efi); 3547c478bd9Sstevel@tonic-gate vwrite64(fd, disk_efi, argv[optind]); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * Shut system down after writing a new vtoc to disk 3597c478bd9Sstevel@tonic-gate * This is used during installation of core floppies. 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate if (uflag == 1) 362*3f2f09c1Sdp (void) uadmin(A_REBOOT, AD_BOOT, 0); 3637c478bd9Sstevel@tonic-gate else if (uflag == 2) 364*3f2f09c1Sdp (void) uadmin(A_REBOOT, AD_IBOOT, 0); 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate (void) printf("fmthard: New volume table of contents now in place.\n"); 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate return (0); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate /* 3747c478bd9Sstevel@tonic-gate * display () 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate * display contents of VTOC without writing it to disk 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate static void 3797c478bd9Sstevel@tonic-gate display(struct dk_geom *geom, struct vtoc *vtoc, char *device) 3807c478bd9Sstevel@tonic-gate { 3817c478bd9Sstevel@tonic-gate int i; 3827c478bd9Sstevel@tonic-gate int c; 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate /* 3857c478bd9Sstevel@tonic-gate * Print out the VTOC 3867c478bd9Sstevel@tonic-gate */ 3877c478bd9Sstevel@tonic-gate (void) printf("* %s default partition map\n", device); 3887c478bd9Sstevel@tonic-gate if (*vtoc->v_volume) { 3897c478bd9Sstevel@tonic-gate (void) printf("* Volume Name: "); 3907c478bd9Sstevel@tonic-gate for (i = 0; i < LEN_DKL_VVOL; i++) { 3917c478bd9Sstevel@tonic-gate if ((c = vtoc->v_volume[i]) == 0) 3927c478bd9Sstevel@tonic-gate break; 3937c478bd9Sstevel@tonic-gate (void) printf("%c", c); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate (void) printf("\n"); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate (void) printf("*\n"); 3987c478bd9Sstevel@tonic-gate (void) printf("* Dimensions:\n"); 3997c478bd9Sstevel@tonic-gate (void) printf("* %d bytes/sector\n", SECSIZE); 4007c478bd9Sstevel@tonic-gate (void) printf("* %d sectors/track\n", geom->dkg_nsect); 4017c478bd9Sstevel@tonic-gate (void) printf("* %d tracks/cylinder\n", geom->dkg_nhead); 4027c478bd9Sstevel@tonic-gate (void) printf("* %d cylinders\n", geom->dkg_pcyl); 4037c478bd9Sstevel@tonic-gate (void) printf("* %d accessible cylinders\n", geom->dkg_ncyl); 4047c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4057c478bd9Sstevel@tonic-gate (void) printf("* Flags:\n"); 4067c478bd9Sstevel@tonic-gate (void) printf("* 1: unmountable\n"); 4077c478bd9Sstevel@tonic-gate (void) printf("* 10: read-only\n"); 4087c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4097c478bd9Sstevel@tonic-gate (void) printf( 4107c478bd9Sstevel@tonic-gate "\n* Partition Tag Flag First Sector Sector Count\n"); 4117c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 4127c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size > 0) 4137c478bd9Sstevel@tonic-gate (void) printf( 4147c478bd9Sstevel@tonic-gate " %d %d 0%x %ld %ld\n", 4157c478bd9Sstevel@tonic-gate i, vtoc->v_part[i].p_tag, 4167c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_flag, 4177c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start, 4187c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate exit(0); 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * display64 () 4257c478bd9Sstevel@tonic-gate * 4267c478bd9Sstevel@tonic-gate * display64 contents of EFI partition without writing it to disk 4277c478bd9Sstevel@tonic-gate */ 4287c478bd9Sstevel@tonic-gate static void 4297c478bd9Sstevel@tonic-gate display64(struct dk_gpt *efi, char *device) 4307c478bd9Sstevel@tonic-gate { 4317c478bd9Sstevel@tonic-gate int i; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Print out the VTOC 4357c478bd9Sstevel@tonic-gate */ 4367c478bd9Sstevel@tonic-gate (void) printf("* %s default partition map\n", device); 4377c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4387c478bd9Sstevel@tonic-gate (void) printf("* Dimensions:\n"); 4397c478bd9Sstevel@tonic-gate (void) printf("* %d bytes/sector\n", efi->efi_lbasize); 4407c478bd9Sstevel@tonic-gate (void) printf("* N/A sectors/track\n"); 4417c478bd9Sstevel@tonic-gate (void) printf("* N/A tracks/cylinder\n"); 4427c478bd9Sstevel@tonic-gate (void) printf("* N/A cylinders\n"); 4437c478bd9Sstevel@tonic-gate (void) printf("* N/A accessible cylinders\n"); 4447c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4457c478bd9Sstevel@tonic-gate (void) printf("* Flags:\n"); 4467c478bd9Sstevel@tonic-gate (void) printf("* 1: unmountable\n"); 4477c478bd9Sstevel@tonic-gate (void) printf("* 10: read-only\n"); 4487c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4497c478bd9Sstevel@tonic-gate (void) printf( 4507c478bd9Sstevel@tonic-gate "\n* Partition Tag Flag First Sector Sector Count\n"); 4517c478bd9Sstevel@tonic-gate for (i = 0; i < efi->efi_nparts; i++) { 4527c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_size > 0) 4537c478bd9Sstevel@tonic-gate (void) printf( 4547c478bd9Sstevel@tonic-gate " %d %d 0%x %8lld %8lld\n", 4557c478bd9Sstevel@tonic-gate i, efi->efi_parts[i].p_tag, 4567c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_flag, 4577c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start, 4587c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate exit(0); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate /* 4657c478bd9Sstevel@tonic-gate * insert() 4667c478bd9Sstevel@tonic-gate * 4677c478bd9Sstevel@tonic-gate * Insert a change into the VTOC. 4687c478bd9Sstevel@tonic-gate */ 4697c478bd9Sstevel@tonic-gate static void 4707c478bd9Sstevel@tonic-gate insert(char *data, struct vtoc *vtoc) 4717c478bd9Sstevel@tonic-gate { 4727c478bd9Sstevel@tonic-gate int part; 4737c478bd9Sstevel@tonic-gate int tag; 4747c478bd9Sstevel@tonic-gate uint_t flag; 4757c478bd9Sstevel@tonic-gate daddr_t start; 4767c478bd9Sstevel@tonic-gate long size; 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate if (sscanf(data, "%d:%d:%x:%ld:%ld", 4797c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 4807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data); 4817c478bd9Sstevel@tonic-gate exit(1); 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate if (part >= V_NUMPAR) { 4847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4857c478bd9Sstevel@tonic-gate "Error in data \"%s\": No such partition %x\n", 4867c478bd9Sstevel@tonic-gate data, part); 4877c478bd9Sstevel@tonic-gate exit(1); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_tag = (ushort_t)tag; 4907c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_flag = (ushort_t)flag; 4917c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_start = start; 4927c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_size = size; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* 4967c478bd9Sstevel@tonic-gate * insert64() 4977c478bd9Sstevel@tonic-gate * 4987c478bd9Sstevel@tonic-gate * Insert a change into the VTOC. 4997c478bd9Sstevel@tonic-gate */ 5007c478bd9Sstevel@tonic-gate static void 5017c478bd9Sstevel@tonic-gate insert64(char *data, struct dk_gpt *efi) 5027c478bd9Sstevel@tonic-gate { 5037c478bd9Sstevel@tonic-gate int part; 5047c478bd9Sstevel@tonic-gate int tag; 5057c478bd9Sstevel@tonic-gate uint_t flag; 5067c478bd9Sstevel@tonic-gate diskaddr_t start; 5077c478bd9Sstevel@tonic-gate diskaddr_t size; 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate if (sscanf(data, "%d:%d:%x:%lld:%lld", 5107c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 5117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data); 5127c478bd9Sstevel@tonic-gate exit(1); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate if (part >= efi->efi_nparts) { 5157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5167c478bd9Sstevel@tonic-gate "Error in data \"%s\": No such partition %x\n", 5177c478bd9Sstevel@tonic-gate data, part); 5187c478bd9Sstevel@tonic-gate exit(1); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_tag = (ushort_t)tag; 5217c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_flag = (ushort_t)flag; 5227c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_start = start; 5237c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_size = size; 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * load() 5287c478bd9Sstevel@tonic-gate * 5297c478bd9Sstevel@tonic-gate * Load VTOC information from a datafile. 5307c478bd9Sstevel@tonic-gate */ 5317c478bd9Sstevel@tonic-gate static void 5327c478bd9Sstevel@tonic-gate load(FILE *fp, struct dk_geom *geom, struct vtoc *vtoc) 5337c478bd9Sstevel@tonic-gate { 5347c478bd9Sstevel@tonic-gate int part; 5357c478bd9Sstevel@tonic-gate int tag; 5367c478bd9Sstevel@tonic-gate uint_t flag; 5377c478bd9Sstevel@tonic-gate daddr_t start; 5387c478bd9Sstevel@tonic-gate long size; 5397c478bd9Sstevel@tonic-gate char line[256]; 5407c478bd9Sstevel@tonic-gate int i; 5417c478bd9Sstevel@tonic-gate long nblks; 5427c478bd9Sstevel@tonic-gate long fullsz; 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; ++i) { 5457c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_tag = 0; 5467c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_flag = V_UNMNT; 5477c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start = 0; 5487c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size = 0; 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate /* 5517c478bd9Sstevel@tonic-gate * initialize partition 2, by convention it corresponds to whole 5527c478bd9Sstevel@tonic-gate * disk. It will be overwritten, if specified in the input datafile 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate fullsz = geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead; 5557c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_tag = V_BACKUP; 5567c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_flag = V_UNMNT; 5577c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_start = 0; 5587c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_size = fullsz; 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate nblks = geom->dkg_nsect * geom->dkg_nhead; 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 5637c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 5647c478bd9Sstevel@tonic-gate continue; 5657c478bd9Sstevel@tonic-gate line[strlen(line) - 1] = '\0'; 5667c478bd9Sstevel@tonic-gate if (sscanf(line, "%d %d %x %ld %ld", 5677c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 5687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n", 5697c478bd9Sstevel@tonic-gate line); 5707c478bd9Sstevel@tonic-gate exit(1); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate if (part >= V_NUMPAR) { 5737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5747c478bd9Sstevel@tonic-gate "No such partition %x: \"%s\"\n", 5757c478bd9Sstevel@tonic-gate part, line); 5767c478bd9Sstevel@tonic-gate exit(1); 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate if (!eflag && ((start % nblks) != 0 || (size % nblks) != 0)) { 5797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5807c478bd9Sstevel@tonic-gate "Partition %d not aligned on cylinder boundary: \"%s\"\n", 5817c478bd9Sstevel@tonic-gate part, line); 5827c478bd9Sstevel@tonic-gate exit(1); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_tag = (ushort_t)tag; 5857c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_flag = (ushort_t)flag; 5867c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_start = start; 5877c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_size = size; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate for (part = 0; part < V_NUMPAR; part++) { 5907c478bd9Sstevel@tonic-gate vtoc->timestamp[part] = (time_t)0; 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate /* 5957c478bd9Sstevel@tonic-gate * load64() 5967c478bd9Sstevel@tonic-gate * 5977c478bd9Sstevel@tonic-gate * Load VTOC information from a datafile. 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate static void 6007c478bd9Sstevel@tonic-gate load64(FILE *fp, int fd, struct dk_gpt **efi) 6017c478bd9Sstevel@tonic-gate { 6027c478bd9Sstevel@tonic-gate int part; 6037c478bd9Sstevel@tonic-gate int tag; 6047c478bd9Sstevel@tonic-gate uint_t flag; 6057c478bd9Sstevel@tonic-gate diskaddr_t start; 6067c478bd9Sstevel@tonic-gate diskaddr_t size; 6077c478bd9Sstevel@tonic-gate int nlines = 0; 6087c478bd9Sstevel@tonic-gate char line[256]; 6097c478bd9Sstevel@tonic-gate int i; 6107c478bd9Sstevel@tonic-gate uint_t max_part = 0; 6117c478bd9Sstevel@tonic-gate char **mem = NULL; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 6147c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 6157c478bd9Sstevel@tonic-gate continue; 6167c478bd9Sstevel@tonic-gate line[strlen(line) - 1] = '\0'; 6177c478bd9Sstevel@tonic-gate if (sscanf(line, "%d %d %x %lld %lld", 6187c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 6197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n", 6207c478bd9Sstevel@tonic-gate line); 6217c478bd9Sstevel@tonic-gate exit(1); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate mem = realloc(mem, sizeof (*mem) * (nlines + 1)); 6247c478bd9Sstevel@tonic-gate if (mem == NULL) { 6257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "realloc failed\n"); 6267c478bd9Sstevel@tonic-gate exit(1); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate mem[nlines] = strdup(line); 6297c478bd9Sstevel@tonic-gate if (mem[nlines] == NULL) { 6307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "strdup failed\n"); 6317c478bd9Sstevel@tonic-gate exit(1); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate nlines++; 6347c478bd9Sstevel@tonic-gate if (part > max_part) 6357c478bd9Sstevel@tonic-gate max_part = part; 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate max_part++; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate if ((i = efi_alloc_and_init(fd, max_part, efi)) < 0) { 6407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6417c478bd9Sstevel@tonic-gate "efi_alloc_and_init failed: %d\n", i); 6427c478bd9Sstevel@tonic-gate exit(1); 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate for (i = 0; i < (*efi)->efi_nparts; ++i) { 6457c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_tag = V_UNASSIGNED; 6467c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_flag = V_UNMNT; 6477c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_start = 0; 6487c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_size = 0; 6497c478bd9Sstevel@tonic-gate } 6507c478bd9Sstevel@tonic-gate lastlba = (*efi)->efi_last_u_lba; 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate for (i = 0; i < nlines; i++) { 6537c478bd9Sstevel@tonic-gate if (sscanf(mem[i], "%d %d %x %lld %lld", 6547c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 6557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n", 6567c478bd9Sstevel@tonic-gate line); 6577c478bd9Sstevel@tonic-gate exit(1); 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate free(mem[i]); 6607c478bd9Sstevel@tonic-gate if (part >= (*efi)->efi_nparts) { 6617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6627c478bd9Sstevel@tonic-gate "No such partition %x: \"%s\"\n", 6637c478bd9Sstevel@tonic-gate part, line); 6647c478bd9Sstevel@tonic-gate exit(1); 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_tag = (ushort_t)tag; 6677c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_flag = (ushort_t)flag; 6687c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_start = start; 6697c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_size = size; 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate (*efi)->efi_nparts = max_part; 6727c478bd9Sstevel@tonic-gate free(mem); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate static void 6777c478bd9Sstevel@tonic-gate usage() 6787c478bd9Sstevel@tonic-gate { 6797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6807c478bd9Sstevel@tonic-gate #if defined(sparc) 6817c478bd9Sstevel@tonic-gate "Usage: fmthard [ -i ] [ -n volumename ] [ -s datafile ] [ -d arguments] \ 6827c478bd9Sstevel@tonic-gate raw-device\n"); 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate #elif defined(i386) 6857c478bd9Sstevel@tonic-gate "Usage: fmthard [ -i ] [ -S ] [-I geom_file] \ 6867c478bd9Sstevel@tonic-gate -n volumename | -s datafile [ -d arguments] raw-device\n"); 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate #else 6897c478bd9Sstevel@tonic-gate #error No platform defined. 6907c478bd9Sstevel@tonic-gate #endif 6917c478bd9Sstevel@tonic-gate exit(2); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate /* 6957c478bd9Sstevel@tonic-gate * validate() 6967c478bd9Sstevel@tonic-gate * 6977c478bd9Sstevel@tonic-gate * Validate the new VTOC. 6987c478bd9Sstevel@tonic-gate */ 6997c478bd9Sstevel@tonic-gate static void 7007c478bd9Sstevel@tonic-gate validate(struct dk_geom *geom, struct vtoc *vtoc) 7017c478bd9Sstevel@tonic-gate { 7027c478bd9Sstevel@tonic-gate int i; 7037c478bd9Sstevel@tonic-gate int j; 7047c478bd9Sstevel@tonic-gate long fullsz; 7057c478bd9Sstevel@tonic-gate long endsect; 7067c478bd9Sstevel@tonic-gate daddr_t istart; 7077c478bd9Sstevel@tonic-gate daddr_t jstart; 7087c478bd9Sstevel@tonic-gate long isize; 7097c478bd9Sstevel@tonic-gate long jsize; 7107c478bd9Sstevel@tonic-gate long nblks; 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate nblks = geom->dkg_nsect * geom->dkg_nhead; 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate fullsz = geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 7177c478bd9Sstevel@tonic-gate /* make the vtoc look sane - ha ha */ 7187c478bd9Sstevel@tonic-gate vtoc->v_version = V_VERSION; 7197c478bd9Sstevel@tonic-gate vtoc->v_sanity = VTOC_SANE; 7207c478bd9Sstevel@tonic-gate vtoc->v_nparts = V_NUMPAR; 7217c478bd9Sstevel@tonic-gate if (sectsiz == 0) 7227c478bd9Sstevel@tonic-gate sectsiz = SECSIZE; 7237c478bd9Sstevel@tonic-gate if (vtoc->v_sectorsz == 0) 7247c478bd9Sstevel@tonic-gate vtoc->v_sectorsz = sectsiz; 7257c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */ 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 7287c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_tag == V_BACKUP) { 7297c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size != fullsz) { 7307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7317c478bd9Sstevel@tonic-gate fmthard: Partition %d specifies the full disk and is not equal\n\ 7327c478bd9Sstevel@tonic-gate full size of disk. The full disk capacity is %lu sectors.\n", i, fullsz); 7337c478bd9Sstevel@tonic-gate #if defined(sparc) 7347c478bd9Sstevel@tonic-gate exit(1); 7357c478bd9Sstevel@tonic-gate #endif 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size == 0) 7397c478bd9Sstevel@tonic-gate continue; /* Undefined partition */ 7407c478bd9Sstevel@tonic-gate if ((vtoc->v_part[i].p_start % nblks) || 7417c478bd9Sstevel@tonic-gate (vtoc->v_part[i].p_size % nblks)) { 7427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7437c478bd9Sstevel@tonic-gate fmthard: Partition %d not aligned on cylinder boundary \n", i); 7447c478bd9Sstevel@tonic-gate exit(1); 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_start > fullsz || 7477c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start + 7487c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size > fullsz) { 7497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7507c478bd9Sstevel@tonic-gate fmthard: Partition %d specified as %lu sectors starting at %lu\n\ 7517c478bd9Sstevel@tonic-gate \tdoes not fit. The full disk contains %lu sectors.\n", 7527c478bd9Sstevel@tonic-gate i, vtoc->v_part[i].p_size, 7537c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start, fullsz); 7547c478bd9Sstevel@tonic-gate #if defined(sparc) 7557c478bd9Sstevel@tonic-gate exit(1); 7567c478bd9Sstevel@tonic-gate #endif 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_tag != V_BACKUP && 7607c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size != fullsz) { 7617c478bd9Sstevel@tonic-gate for (j = 0; j < V_NUMPAR; j++) { 7627c478bd9Sstevel@tonic-gate if (vtoc->v_part[j].p_tag == V_BACKUP) 7637c478bd9Sstevel@tonic-gate continue; 7647c478bd9Sstevel@tonic-gate if (vtoc->v_part[j].p_size == fullsz) 7657c478bd9Sstevel@tonic-gate continue; 7667c478bd9Sstevel@tonic-gate isize = vtoc->v_part[i].p_size; 7677c478bd9Sstevel@tonic-gate jsize = vtoc->v_part[j].p_size; 7687c478bd9Sstevel@tonic-gate istart = vtoc->v_part[i].p_start; 7697c478bd9Sstevel@tonic-gate jstart = vtoc->v_part[j].p_start; 7707c478bd9Sstevel@tonic-gate if ((i != j) && 7717c478bd9Sstevel@tonic-gate (isize != 0) && (jsize != 0)) { 7727c478bd9Sstevel@tonic-gate endsect = jstart + jsize -1; 7737c478bd9Sstevel@tonic-gate if ((jstart <= istart) && 7747c478bd9Sstevel@tonic-gate (istart <= endsect)) { 7757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7767c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\ 7777c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n", 7787c478bd9Sstevel@tonic-gate i, j); 7797c478bd9Sstevel@tonic-gate #if defined(sparc) 7807c478bd9Sstevel@tonic-gate exit(1); 7817c478bd9Sstevel@tonic-gate #endif 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate /* 7907c478bd9Sstevel@tonic-gate * validate64() 7917c478bd9Sstevel@tonic-gate * 7927c478bd9Sstevel@tonic-gate * Validate the new VTOC. 7937c478bd9Sstevel@tonic-gate */ 7947c478bd9Sstevel@tonic-gate static void 7957c478bd9Sstevel@tonic-gate validate64(struct dk_gpt *efi) 7967c478bd9Sstevel@tonic-gate { 7977c478bd9Sstevel@tonic-gate int i; 7987c478bd9Sstevel@tonic-gate int j; 7997c478bd9Sstevel@tonic-gate int resv_part = 0; 8007c478bd9Sstevel@tonic-gate diskaddr_t endsect; 8017c478bd9Sstevel@tonic-gate diskaddr_t fullsz; 8027c478bd9Sstevel@tonic-gate diskaddr_t istart; 8037c478bd9Sstevel@tonic-gate diskaddr_t jstart; 8047c478bd9Sstevel@tonic-gate diskaddr_t isize; 8057c478bd9Sstevel@tonic-gate diskaddr_t jsize; 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate fullsz = lastlba + 1; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate for (i = 0; i < efi->efi_nparts; i++) { 8107c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_size == 0) 8117c478bd9Sstevel@tonic-gate continue; /* Undefined partition */ 8127c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_tag == V_RESERVED) 8137c478bd9Sstevel@tonic-gate resv_part++; 8147c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_start > fullsz || 8157c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start + 8167c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size > fullsz) { 8177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 8187c478bd9Sstevel@tonic-gate fmthard: Partition %d specified as %lld sectors starting at %lld\n\ 8197c478bd9Sstevel@tonic-gate \tdoes not fit. The full disk contains %lld sectors.\n", 8207c478bd9Sstevel@tonic-gate i, efi->efi_parts[i].p_size, 8217c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start, fullsz); 8227c478bd9Sstevel@tonic-gate exit(1); 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_tag != V_BACKUP && 8267c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size != fullsz) { 8277c478bd9Sstevel@tonic-gate for (j = 0; j < V_NUMPAR; j++) { 8287c478bd9Sstevel@tonic-gate if (efi->efi_parts[j].p_size == fullsz) 8297c478bd9Sstevel@tonic-gate continue; 8307c478bd9Sstevel@tonic-gate isize = efi->efi_parts[i].p_size; 8317c478bd9Sstevel@tonic-gate jsize = efi->efi_parts[j].p_size; 8327c478bd9Sstevel@tonic-gate istart = efi->efi_parts[i].p_start; 8337c478bd9Sstevel@tonic-gate jstart = efi->efi_parts[j].p_start; 8347c478bd9Sstevel@tonic-gate if ((i != j) && 8357c478bd9Sstevel@tonic-gate (isize != 0) && (jsize != 0)) { 8367c478bd9Sstevel@tonic-gate endsect = jstart + jsize - 1; 8377c478bd9Sstevel@tonic-gate if ((jstart <= istart) && 8387c478bd9Sstevel@tonic-gate (istart <= endsect)) { 8397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 8407c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\ 8417c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n", 8427c478bd9Sstevel@tonic-gate i, j); 8437c478bd9Sstevel@tonic-gate #if defined(sparc) 8447c478bd9Sstevel@tonic-gate exit(1); 8457c478bd9Sstevel@tonic-gate #endif 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate } 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate if (resv_part != 1) { 8527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8537c478bd9Sstevel@tonic-gate "expected one reserved partition, but found %d\n", 8547c478bd9Sstevel@tonic-gate resv_part); 8557c478bd9Sstevel@tonic-gate exit(1); 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate /* 8617c478bd9Sstevel@tonic-gate * Read the VTOC 8627c478bd9Sstevel@tonic-gate */ 8637c478bd9Sstevel@tonic-gate int 8647c478bd9Sstevel@tonic-gate vread(int fd, struct vtoc *vtoc, char *devname) 8657c478bd9Sstevel@tonic-gate { 8667c478bd9Sstevel@tonic-gate int i; 8677c478bd9Sstevel@tonic-gate 8687c478bd9Sstevel@tonic-gate if ((i = read_vtoc(fd, vtoc)) < 0) { 8697c478bd9Sstevel@tonic-gate if (i == VT_ENOTSUP) { 8707c478bd9Sstevel@tonic-gate return (1); 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 8737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Invalid VTOC\n", 8747c478bd9Sstevel@tonic-gate devname); 8757c478bd9Sstevel@tonic-gate } else { 8767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot read VTOC\n", 8777c478bd9Sstevel@tonic-gate devname); 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate exit(1); 8807c478bd9Sstevel@tonic-gate } 8817c478bd9Sstevel@tonic-gate return (0); 8827c478bd9Sstevel@tonic-gate } 8837c478bd9Sstevel@tonic-gate 8847c478bd9Sstevel@tonic-gate void 8857c478bd9Sstevel@tonic-gate vread64(int fd, struct dk_gpt **efi_hdr, char *devname) 8867c478bd9Sstevel@tonic-gate { 8877c478bd9Sstevel@tonic-gate int i; 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate if ((i = efi_alloc_and_read(fd, efi_hdr)) < 0) { 8907c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) 8917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8927c478bd9Sstevel@tonic-gate "%s: this disk must be labeled first\n", 8937c478bd9Sstevel@tonic-gate devname); 8947c478bd9Sstevel@tonic-gate else 8957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8967c478bd9Sstevel@tonic-gate "%s: read_efi failed %d\n", 8977c478bd9Sstevel@tonic-gate devname, i); 8987c478bd9Sstevel@tonic-gate exit(1); 8997c478bd9Sstevel@tonic-gate } 9007c478bd9Sstevel@tonic-gate lastlba = (*efi_hdr)->efi_last_u_lba; 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate /* 9047c478bd9Sstevel@tonic-gate * Write the VTOC 9057c478bd9Sstevel@tonic-gate */ 9067c478bd9Sstevel@tonic-gate void 9077c478bd9Sstevel@tonic-gate vwrite(int fd, struct vtoc *vtoc, char *devname) 9087c478bd9Sstevel@tonic-gate { 9097c478bd9Sstevel@tonic-gate int i; 9107c478bd9Sstevel@tonic-gate 9117c478bd9Sstevel@tonic-gate if ((i = write_vtoc(fd, vtoc)) != 0) { 9127c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 9137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9147c478bd9Sstevel@tonic-gate "%s: invalid entry exists in vtoc\n", 9157c478bd9Sstevel@tonic-gate devname); 9167c478bd9Sstevel@tonic-gate } else { 9177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot write VTOC\n", 9187c478bd9Sstevel@tonic-gate devname); 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate exit(1); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate /* 9257c478bd9Sstevel@tonic-gate * Write the VTOC 9267c478bd9Sstevel@tonic-gate */ 9277c478bd9Sstevel@tonic-gate void 9287c478bd9Sstevel@tonic-gate vwrite64(int fd, struct dk_gpt *efi, char *devname) 9297c478bd9Sstevel@tonic-gate { 9307c478bd9Sstevel@tonic-gate int i; 9317c478bd9Sstevel@tonic-gate 9327c478bd9Sstevel@tonic-gate if ((i = efi_write(fd, efi)) != 0) { 9337c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 9347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9357c478bd9Sstevel@tonic-gate "%s: invalid entry exists in vtoc\n", 9367c478bd9Sstevel@tonic-gate devname); 9377c478bd9Sstevel@tonic-gate } else { 9387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot write EFI\n", 9397c478bd9Sstevel@tonic-gate devname); 9407c478bd9Sstevel@tonic-gate } 9417c478bd9Sstevel@tonic-gate exit(1); 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate } 944