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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 237c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * 287c478bd9Sstevel@tonic-gate * Portions of this source code were provided by International 297c478bd9Sstevel@tonic-gate * Computers Limited (ICL) under a development agreement with AT&T. 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 367c478bd9Sstevel@tonic-gate * Use is subject to license terms. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Sun Microsystems version of fmthard: 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * Supports the following arguments: 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * -i Writes VTOC to stdout, rather than disk 457c478bd9Sstevel@tonic-gate * -q Quick check: exit code 0 if VTOC ok 467c478bd9Sstevel@tonic-gate * -d <data> Incremental changes to the VTOC 477c478bd9Sstevel@tonic-gate * -n <vname> Change volume name to <vname> 487c478bd9Sstevel@tonic-gate * -s <file> Read VTOC information from <file>, or stdin ("-") 497c478bd9Sstevel@tonic-gate * -u <state> Reboot after writing VTOC, according to <state>: 507c478bd9Sstevel@tonic-gate * boot: AD_BOOT (standard reboot) 517c478bd9Sstevel@tonic-gate * firm: AD_IBOOT (interactive reboot) 527c478bd9Sstevel@tonic-gate * 537c478bd9Sstevel@tonic-gate * Note that fmthard cannot write a VTOC on an unlabeled disk. 547c478bd9Sstevel@tonic-gate * You must use format or SunInstall for this purpose. 557c478bd9Sstevel@tonic-gate * (NOTE: the above restriction only applies on Sparc systems). 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * The primary motivation for fmthard is to duplicate the 587c478bd9Sstevel@tonic-gate * partitioning from disk to disk: 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * prtvtoc /dev/rdsk/c0t0d0s2 | fmthard -s - /dev/rdsk/c0t1d0s2 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #include <stdio.h> 647c478bd9Sstevel@tonic-gate #include <fcntl.h> 657c478bd9Sstevel@tonic-gate #include <errno.h> 667c478bd9Sstevel@tonic-gate #include <string.h> 677c478bd9Sstevel@tonic-gate #include <stdlib.h> 687c478bd9Sstevel@tonic-gate #include <unistd.h> 697c478bd9Sstevel@tonic-gate #include <sys/types.h> 707c478bd9Sstevel@tonic-gate #include <sys/param.h> 717c478bd9Sstevel@tonic-gate #include <sys/stat.h> 727c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 737c478bd9Sstevel@tonic-gate #include <sys/open.h> 747c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 757c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 767c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 777c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h> 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 807c478bd9Sstevel@tonic-gate #include <sys/dklabel.h> 817c478bd9Sstevel@tonic-gate #endif 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #ifndef SECSIZE 867c478bd9Sstevel@tonic-gate #define SECSIZE DEV_BSIZE 877c478bd9Sstevel@tonic-gate #endif /* SECSIZE */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * External functions. 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate extern int read_vtoc(int, struct vtoc *); 947c478bd9Sstevel@tonic-gate extern int write_vtoc(int, struct vtoc *); 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* 977c478bd9Sstevel@tonic-gate * Internal functions. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate extern int main(int, char **); 1007c478bd9Sstevel@tonic-gate static void display(struct dk_geom *, struct vtoc *, char *); 1017c478bd9Sstevel@tonic-gate static void display64(struct dk_gpt *, char *); 1027c478bd9Sstevel@tonic-gate static void insert(char *, struct vtoc *); 1037c478bd9Sstevel@tonic-gate static void insert64(char *, struct dk_gpt *); 1047c478bd9Sstevel@tonic-gate static void load(FILE *, struct dk_geom *, struct vtoc *); 1057c478bd9Sstevel@tonic-gate static void load64(FILE *, int fd, struct dk_gpt **); 1067c478bd9Sstevel@tonic-gate static void usage(void); 1077c478bd9Sstevel@tonic-gate static void validate(struct dk_geom *, struct vtoc *); 1087c478bd9Sstevel@tonic-gate static void validate64(struct dk_gpt *); 1097c478bd9Sstevel@tonic-gate static int vread(int, struct vtoc *, char *); 1107c478bd9Sstevel@tonic-gate static void vread64(int, struct dk_gpt **, char *); 1117c478bd9Sstevel@tonic-gate static void vwrite(int, struct vtoc *, char *); 1127c478bd9Sstevel@tonic-gate static void vwrite64(int, struct dk_gpt *, char *); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* 1157c478bd9Sstevel@tonic-gate * Static variables. 1167c478bd9Sstevel@tonic-gate */ 1177c478bd9Sstevel@tonic-gate static char *delta; /* Incremental update */ 1187c478bd9Sstevel@tonic-gate static short eflag; /* force write of an EFI label */ 1197c478bd9Sstevel@tonic-gate static short iflag; /* Prints VTOC w/o updating */ 1207c478bd9Sstevel@tonic-gate static short qflag; /* Check for a formatted disk */ 1217c478bd9Sstevel@tonic-gate static short uflag; /* Exit to firmware after writing */ 1227c478bd9Sstevel@tonic-gate /* new vtoc and reboot. Used during */ 1237c478bd9Sstevel@tonic-gate /* installation of core floppies */ 1247c478bd9Sstevel@tonic-gate static diskaddr_t lastlba = 0; /* last LBA on 64-bit VTOC */ 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate #if defined(sparc) 1277c478bd9Sstevel@tonic-gate static char *uboot = "boot"; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate #elif defined(i386) 1307c478bd9Sstevel@tonic-gate /* use installgrub(1M) to install boot blocks */ 1317c478bd9Sstevel@tonic-gate static char *uboot = ""; 1327c478bd9Sstevel@tonic-gate #else 1337c478bd9Sstevel@tonic-gate #error No platform defined. 1347c478bd9Sstevel@tonic-gate #endif /* various platform-specific definitions */ 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate static char *ufirm = "firm"; 1377c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1387c478bd9Sstevel@tonic-gate static int sectsiz; 1397c478bd9Sstevel@tonic-gate static struct vtoc disk_vtoc; 1407c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */ 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate int 1437c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate int fd; 1467c478bd9Sstevel@tonic-gate int c; 1477c478bd9Sstevel@tonic-gate char *dfile; 1487c478bd9Sstevel@tonic-gate char *vname; 1497c478bd9Sstevel@tonic-gate struct stat statbuf; 1507c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8) 1517c478bd9Sstevel@tonic-gate struct vtoc disk_vtoc; 1527c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_8) */ 1537c478bd9Sstevel@tonic-gate struct dk_gpt *disk_efi; 1547c478bd9Sstevel@tonic-gate struct dk_geom disk_geom; 1557c478bd9Sstevel@tonic-gate int n; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate 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", 228*ace1a5f1Sdp 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 } 2577c478bd9Sstevel@tonic-gate if (eflag && (dfile == NULL)) { 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) 3627c478bd9Sstevel@tonic-gate uadmin(A_REBOOT, AD_BOOT, 0); 3637c478bd9Sstevel@tonic-gate else if (uflag == 2) 3647c478bd9Sstevel@tonic-gate 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 /*NOTREACHED*/ 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * display () 3767c478bd9Sstevel@tonic-gate * 3777c478bd9Sstevel@tonic-gate * display contents of VTOC without writing it to disk 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate static void 3807c478bd9Sstevel@tonic-gate display(struct dk_geom *geom, struct vtoc *vtoc, char *device) 3817c478bd9Sstevel@tonic-gate { 3827c478bd9Sstevel@tonic-gate int i; 3837c478bd9Sstevel@tonic-gate int c; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Print out the VTOC 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate (void) printf("* %s default partition map\n", device); 3897c478bd9Sstevel@tonic-gate if (*vtoc->v_volume) { 3907c478bd9Sstevel@tonic-gate (void) printf("* Volume Name: "); 3917c478bd9Sstevel@tonic-gate for (i = 0; i < LEN_DKL_VVOL; i++) { 3927c478bd9Sstevel@tonic-gate if ((c = vtoc->v_volume[i]) == 0) 3937c478bd9Sstevel@tonic-gate break; 3947c478bd9Sstevel@tonic-gate (void) printf("%c", c); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate (void) printf("\n"); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate (void) printf("*\n"); 3997c478bd9Sstevel@tonic-gate (void) printf("* Dimensions:\n"); 4007c478bd9Sstevel@tonic-gate (void) printf("* %d bytes/sector\n", SECSIZE); 4017c478bd9Sstevel@tonic-gate (void) printf("* %d sectors/track\n", geom->dkg_nsect); 4027c478bd9Sstevel@tonic-gate (void) printf("* %d tracks/cylinder\n", geom->dkg_nhead); 4037c478bd9Sstevel@tonic-gate (void) printf("* %d cylinders\n", geom->dkg_pcyl); 4047c478bd9Sstevel@tonic-gate (void) printf("* %d accessible cylinders\n", geom->dkg_ncyl); 4057c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4067c478bd9Sstevel@tonic-gate (void) printf("* Flags:\n"); 4077c478bd9Sstevel@tonic-gate (void) printf("* 1: unmountable\n"); 4087c478bd9Sstevel@tonic-gate (void) printf("* 10: read-only\n"); 4097c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4107c478bd9Sstevel@tonic-gate (void) printf( 4117c478bd9Sstevel@tonic-gate "\n* Partition Tag Flag First Sector Sector Count\n"); 4127c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 4137c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size > 0) 4147c478bd9Sstevel@tonic-gate (void) printf( 4157c478bd9Sstevel@tonic-gate " %d %d 0%x %ld %ld\n", 4167c478bd9Sstevel@tonic-gate i, vtoc->v_part[i].p_tag, 4177c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_flag, 4187c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start, 4197c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size); 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate exit(0); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* 4257c478bd9Sstevel@tonic-gate * display64 () 4267c478bd9Sstevel@tonic-gate * 4277c478bd9Sstevel@tonic-gate * display64 contents of EFI partition without writing it to disk 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate static void 4307c478bd9Sstevel@tonic-gate display64(struct dk_gpt *efi, char *device) 4317c478bd9Sstevel@tonic-gate { 4327c478bd9Sstevel@tonic-gate int i; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* 4357c478bd9Sstevel@tonic-gate * Print out the VTOC 4367c478bd9Sstevel@tonic-gate */ 4377c478bd9Sstevel@tonic-gate (void) printf("* %s default partition map\n", device); 4387c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4397c478bd9Sstevel@tonic-gate (void) printf("* Dimensions:\n"); 4407c478bd9Sstevel@tonic-gate (void) printf("* %d bytes/sector\n", efi->efi_lbasize); 4417c478bd9Sstevel@tonic-gate (void) printf("* N/A sectors/track\n"); 4427c478bd9Sstevel@tonic-gate (void) printf("* N/A tracks/cylinder\n"); 4437c478bd9Sstevel@tonic-gate (void) printf("* N/A cylinders\n"); 4447c478bd9Sstevel@tonic-gate (void) printf("* N/A accessible cylinders\n"); 4457c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4467c478bd9Sstevel@tonic-gate (void) printf("* Flags:\n"); 4477c478bd9Sstevel@tonic-gate (void) printf("* 1: unmountable\n"); 4487c478bd9Sstevel@tonic-gate (void) printf("* 10: read-only\n"); 4497c478bd9Sstevel@tonic-gate (void) printf("*\n"); 4507c478bd9Sstevel@tonic-gate (void) printf( 4517c478bd9Sstevel@tonic-gate "\n* Partition Tag Flag First Sector Sector Count\n"); 4527c478bd9Sstevel@tonic-gate for (i = 0; i < efi->efi_nparts; i++) { 4537c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_size > 0) 4547c478bd9Sstevel@tonic-gate (void) printf( 4557c478bd9Sstevel@tonic-gate " %d %d 0%x %8lld %8lld\n", 4567c478bd9Sstevel@tonic-gate i, efi->efi_parts[i].p_tag, 4577c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_flag, 4587c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start, 4597c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate exit(0); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate /* 4667c478bd9Sstevel@tonic-gate * insert() 4677c478bd9Sstevel@tonic-gate * 4687c478bd9Sstevel@tonic-gate * Insert a change into the VTOC. 4697c478bd9Sstevel@tonic-gate */ 4707c478bd9Sstevel@tonic-gate static void 4717c478bd9Sstevel@tonic-gate insert(char *data, struct vtoc *vtoc) 4727c478bd9Sstevel@tonic-gate { 4737c478bd9Sstevel@tonic-gate int part; 4747c478bd9Sstevel@tonic-gate int tag; 4757c478bd9Sstevel@tonic-gate uint_t flag; 4767c478bd9Sstevel@tonic-gate daddr_t start; 4777c478bd9Sstevel@tonic-gate long size; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate if (sscanf(data, "%d:%d:%x:%ld:%ld", 4807c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 4817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data); 4827c478bd9Sstevel@tonic-gate exit(1); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate if (part >= V_NUMPAR) { 4857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4867c478bd9Sstevel@tonic-gate "Error in data \"%s\": No such partition %x\n", 4877c478bd9Sstevel@tonic-gate data, part); 4887c478bd9Sstevel@tonic-gate exit(1); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_tag = (ushort_t)tag; 4917c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_flag = (ushort_t)flag; 4927c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_start = start; 4937c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_size = size; 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * insert64() 4987c478bd9Sstevel@tonic-gate * 4997c478bd9Sstevel@tonic-gate * Insert a change into the VTOC. 5007c478bd9Sstevel@tonic-gate */ 5017c478bd9Sstevel@tonic-gate static void 5027c478bd9Sstevel@tonic-gate insert64(char *data, struct dk_gpt *efi) 5037c478bd9Sstevel@tonic-gate { 5047c478bd9Sstevel@tonic-gate int part; 5057c478bd9Sstevel@tonic-gate int tag; 5067c478bd9Sstevel@tonic-gate uint_t flag; 5077c478bd9Sstevel@tonic-gate diskaddr_t start; 5087c478bd9Sstevel@tonic-gate diskaddr_t size; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate if (sscanf(data, "%d:%d:%x:%lld:%lld", 5117c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 5127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data); 5137c478bd9Sstevel@tonic-gate exit(1); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate if (part >= efi->efi_nparts) { 5167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5177c478bd9Sstevel@tonic-gate "Error in data \"%s\": No such partition %x\n", 5187c478bd9Sstevel@tonic-gate data, part); 5197c478bd9Sstevel@tonic-gate exit(1); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_tag = (ushort_t)tag; 5227c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_flag = (ushort_t)flag; 5237c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_start = start; 5247c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_size = size; 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate /* 5287c478bd9Sstevel@tonic-gate * load() 5297c478bd9Sstevel@tonic-gate * 5307c478bd9Sstevel@tonic-gate * Load VTOC information from a datafile. 5317c478bd9Sstevel@tonic-gate */ 5327c478bd9Sstevel@tonic-gate static void 5337c478bd9Sstevel@tonic-gate load(FILE *fp, struct dk_geom *geom, struct vtoc *vtoc) 5347c478bd9Sstevel@tonic-gate { 5357c478bd9Sstevel@tonic-gate int part; 5367c478bd9Sstevel@tonic-gate int tag; 5377c478bd9Sstevel@tonic-gate uint_t flag; 5387c478bd9Sstevel@tonic-gate daddr_t start; 5397c478bd9Sstevel@tonic-gate long size; 5407c478bd9Sstevel@tonic-gate char line[256]; 5417c478bd9Sstevel@tonic-gate int i; 5427c478bd9Sstevel@tonic-gate long nblks; 5437c478bd9Sstevel@tonic-gate long fullsz; 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; ++i) { 5467c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_tag = 0; 5477c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_flag = V_UNMNT; 5487c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start = 0; 5497c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size = 0; 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * initialize partition 2, by convention it corresponds to whole 5537c478bd9Sstevel@tonic-gate * disk. It will be overwritten, if specified in the input datafile 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate fullsz = geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead; 5567c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_tag = V_BACKUP; 5577c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_flag = V_UNMNT; 5587c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_start = 0; 5597c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_size = fullsz; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate nblks = geom->dkg_nsect * geom->dkg_nhead; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 5647c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 5657c478bd9Sstevel@tonic-gate continue; 5667c478bd9Sstevel@tonic-gate line[strlen(line) - 1] = '\0'; 5677c478bd9Sstevel@tonic-gate if (sscanf(line, "%d %d %x %ld %ld", 5687c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 5697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n", 5707c478bd9Sstevel@tonic-gate line); 5717c478bd9Sstevel@tonic-gate exit(1); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate if (part >= V_NUMPAR) { 5747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5757c478bd9Sstevel@tonic-gate "No such partition %x: \"%s\"\n", 5767c478bd9Sstevel@tonic-gate part, line); 5777c478bd9Sstevel@tonic-gate exit(1); 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate if (!eflag && ((start % nblks) != 0 || (size % nblks) != 0)) { 5807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5817c478bd9Sstevel@tonic-gate "Partition %d not aligned on cylinder boundary: \"%s\"\n", 5827c478bd9Sstevel@tonic-gate part, line); 5837c478bd9Sstevel@tonic-gate exit(1); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_tag = (ushort_t)tag; 5867c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_flag = (ushort_t)flag; 5877c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_start = start; 5887c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_size = size; 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate for (part = 0; part < V_NUMPAR; part++) { 5917c478bd9Sstevel@tonic-gate vtoc->timestamp[part] = (time_t)0; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate /* 5967c478bd9Sstevel@tonic-gate * load64() 5977c478bd9Sstevel@tonic-gate * 5987c478bd9Sstevel@tonic-gate * Load VTOC information from a datafile. 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate static void 6017c478bd9Sstevel@tonic-gate load64(FILE *fp, int fd, struct dk_gpt **efi) 6027c478bd9Sstevel@tonic-gate { 6037c478bd9Sstevel@tonic-gate int part; 6047c478bd9Sstevel@tonic-gate int tag; 6057c478bd9Sstevel@tonic-gate uint_t flag; 6067c478bd9Sstevel@tonic-gate diskaddr_t start; 6077c478bd9Sstevel@tonic-gate diskaddr_t size; 6087c478bd9Sstevel@tonic-gate int nlines = 0; 6097c478bd9Sstevel@tonic-gate char line[256]; 6107c478bd9Sstevel@tonic-gate int i; 6117c478bd9Sstevel@tonic-gate uint_t max_part = 0; 6127c478bd9Sstevel@tonic-gate char **mem = NULL; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 6157c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 6167c478bd9Sstevel@tonic-gate continue; 6177c478bd9Sstevel@tonic-gate line[strlen(line) - 1] = '\0'; 6187c478bd9Sstevel@tonic-gate if (sscanf(line, "%d %d %x %lld %lld", 6197c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 6207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n", 6217c478bd9Sstevel@tonic-gate line); 6227c478bd9Sstevel@tonic-gate exit(1); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate mem = realloc(mem, sizeof (*mem) * (nlines + 1)); 6257c478bd9Sstevel@tonic-gate if (mem == NULL) { 6267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "realloc failed\n"); 6277c478bd9Sstevel@tonic-gate exit(1); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate mem[nlines] = strdup(line); 6307c478bd9Sstevel@tonic-gate if (mem[nlines] == NULL) { 6317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "strdup failed\n"); 6327c478bd9Sstevel@tonic-gate exit(1); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate nlines++; 6357c478bd9Sstevel@tonic-gate if (part > max_part) 6367c478bd9Sstevel@tonic-gate max_part = part; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate max_part++; 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate if ((i = efi_alloc_and_init(fd, max_part, efi)) < 0) { 6417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6427c478bd9Sstevel@tonic-gate "efi_alloc_and_init failed: %d\n", i); 6437c478bd9Sstevel@tonic-gate exit(1); 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate for (i = 0; i < (*efi)->efi_nparts; ++i) { 6467c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_tag = V_UNASSIGNED; 6477c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_flag = V_UNMNT; 6487c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_start = 0; 6497c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_size = 0; 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate lastlba = (*efi)->efi_last_u_lba; 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate for (i = 0; i < nlines; i++) { 6547c478bd9Sstevel@tonic-gate if (sscanf(mem[i], "%d %d %x %lld %lld", 6557c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) { 6567c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n", 6577c478bd9Sstevel@tonic-gate line); 6587c478bd9Sstevel@tonic-gate exit(1); 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate free(mem[i]); 6617c478bd9Sstevel@tonic-gate if (part >= (*efi)->efi_nparts) { 6627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6637c478bd9Sstevel@tonic-gate "No such partition %x: \"%s\"\n", 6647c478bd9Sstevel@tonic-gate part, line); 6657c478bd9Sstevel@tonic-gate exit(1); 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_tag = (ushort_t)tag; 6687c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_flag = (ushort_t)flag; 6697c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_start = start; 6707c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_size = size; 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate (*efi)->efi_nparts = max_part; 6737c478bd9Sstevel@tonic-gate free(mem); 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate static void 6787c478bd9Sstevel@tonic-gate usage() 6797c478bd9Sstevel@tonic-gate { 6807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6817c478bd9Sstevel@tonic-gate #if defined(sparc) 6827c478bd9Sstevel@tonic-gate "Usage: fmthard [ -i ] [ -n volumename ] [ -s datafile ] [ -d arguments] \ 6837c478bd9Sstevel@tonic-gate raw-device\n"); 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate #elif defined(i386) 6867c478bd9Sstevel@tonic-gate "Usage: fmthard [ -i ] [ -S ] [-I geom_file] \ 6877c478bd9Sstevel@tonic-gate -n volumename | -s datafile [ -d arguments] raw-device\n"); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate #else 6907c478bd9Sstevel@tonic-gate #error No platform defined. 6917c478bd9Sstevel@tonic-gate #endif 6927c478bd9Sstevel@tonic-gate exit(2); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate /* 6967c478bd9Sstevel@tonic-gate * validate() 6977c478bd9Sstevel@tonic-gate * 6987c478bd9Sstevel@tonic-gate * Validate the new VTOC. 6997c478bd9Sstevel@tonic-gate */ 7007c478bd9Sstevel@tonic-gate static void 7017c478bd9Sstevel@tonic-gate validate(struct dk_geom *geom, struct vtoc *vtoc) 7027c478bd9Sstevel@tonic-gate { 7037c478bd9Sstevel@tonic-gate int i; 7047c478bd9Sstevel@tonic-gate int j; 7057c478bd9Sstevel@tonic-gate long fullsz; 7067c478bd9Sstevel@tonic-gate long endsect; 7077c478bd9Sstevel@tonic-gate daddr_t istart; 7087c478bd9Sstevel@tonic-gate daddr_t jstart; 7097c478bd9Sstevel@tonic-gate long isize; 7107c478bd9Sstevel@tonic-gate long jsize; 7117c478bd9Sstevel@tonic-gate long nblks; 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate nblks = geom->dkg_nsect * geom->dkg_nhead; 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate fullsz = geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 7187c478bd9Sstevel@tonic-gate /* make the vtoc look sane - ha ha */ 7197c478bd9Sstevel@tonic-gate vtoc->v_version = V_VERSION; 7207c478bd9Sstevel@tonic-gate vtoc->v_sanity = VTOC_SANE; 7217c478bd9Sstevel@tonic-gate vtoc->v_nparts = V_NUMPAR; 7227c478bd9Sstevel@tonic-gate if (sectsiz == 0) 7237c478bd9Sstevel@tonic-gate sectsiz = SECSIZE; 7247c478bd9Sstevel@tonic-gate if (vtoc->v_sectorsz == 0) 7257c478bd9Sstevel@tonic-gate vtoc->v_sectorsz = sectsiz; 7267c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */ 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 7297c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_tag == V_BACKUP) { 7307c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size != fullsz) { 7317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7327c478bd9Sstevel@tonic-gate fmthard: Partition %d specifies the full disk and is not equal\n\ 7337c478bd9Sstevel@tonic-gate full size of disk. The full disk capacity is %lu sectors.\n", i, fullsz); 7347c478bd9Sstevel@tonic-gate #if defined(sparc) 7357c478bd9Sstevel@tonic-gate exit(1); 7367c478bd9Sstevel@tonic-gate #endif 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size == 0) 7407c478bd9Sstevel@tonic-gate continue; /* Undefined partition */ 7417c478bd9Sstevel@tonic-gate if ((vtoc->v_part[i].p_start % nblks) || 7427c478bd9Sstevel@tonic-gate (vtoc->v_part[i].p_size % nblks)) { 7437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7447c478bd9Sstevel@tonic-gate fmthard: Partition %d not aligned on cylinder boundary \n", i); 7457c478bd9Sstevel@tonic-gate exit(1); 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_start > fullsz || 7487c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start + 7497c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size > fullsz) { 7507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7517c478bd9Sstevel@tonic-gate fmthard: Partition %d specified as %lu sectors starting at %lu\n\ 7527c478bd9Sstevel@tonic-gate \tdoes not fit. The full disk contains %lu sectors.\n", 7537c478bd9Sstevel@tonic-gate i, vtoc->v_part[i].p_size, 7547c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start, fullsz); 7557c478bd9Sstevel@tonic-gate #if defined(sparc) 7567c478bd9Sstevel@tonic-gate exit(1); 7577c478bd9Sstevel@tonic-gate #endif 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_tag != V_BACKUP && 7617c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size != fullsz) { 7627c478bd9Sstevel@tonic-gate for (j = 0; j < V_NUMPAR; j++) { 7637c478bd9Sstevel@tonic-gate if (vtoc->v_part[j].p_tag == V_BACKUP) 7647c478bd9Sstevel@tonic-gate continue; 7657c478bd9Sstevel@tonic-gate if (vtoc->v_part[j].p_size == fullsz) 7667c478bd9Sstevel@tonic-gate continue; 7677c478bd9Sstevel@tonic-gate isize = vtoc->v_part[i].p_size; 7687c478bd9Sstevel@tonic-gate jsize = vtoc->v_part[j].p_size; 7697c478bd9Sstevel@tonic-gate istart = vtoc->v_part[i].p_start; 7707c478bd9Sstevel@tonic-gate jstart = vtoc->v_part[j].p_start; 7717c478bd9Sstevel@tonic-gate if ((i != j) && 7727c478bd9Sstevel@tonic-gate (isize != 0) && (jsize != 0)) { 7737c478bd9Sstevel@tonic-gate endsect = jstart + jsize -1; 7747c478bd9Sstevel@tonic-gate if ((jstart <= istart) && 7757c478bd9Sstevel@tonic-gate (istart <= endsect)) { 7767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 7777c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\ 7787c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n", 7797c478bd9Sstevel@tonic-gate i, j); 7807c478bd9Sstevel@tonic-gate #if defined(sparc) 7817c478bd9Sstevel@tonic-gate exit(1); 7827c478bd9Sstevel@tonic-gate #endif 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* 7917c478bd9Sstevel@tonic-gate * validate64() 7927c478bd9Sstevel@tonic-gate * 7937c478bd9Sstevel@tonic-gate * Validate the new VTOC. 7947c478bd9Sstevel@tonic-gate */ 7957c478bd9Sstevel@tonic-gate static void 7967c478bd9Sstevel@tonic-gate validate64(struct dk_gpt *efi) 7977c478bd9Sstevel@tonic-gate { 7987c478bd9Sstevel@tonic-gate int i; 7997c478bd9Sstevel@tonic-gate int j; 8007c478bd9Sstevel@tonic-gate int resv_part = 0; 8017c478bd9Sstevel@tonic-gate diskaddr_t endsect; 8027c478bd9Sstevel@tonic-gate diskaddr_t fullsz; 8037c478bd9Sstevel@tonic-gate diskaddr_t istart; 8047c478bd9Sstevel@tonic-gate diskaddr_t jstart; 8057c478bd9Sstevel@tonic-gate diskaddr_t isize; 8067c478bd9Sstevel@tonic-gate diskaddr_t jsize; 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate fullsz = lastlba + 1; 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate for (i = 0; i < efi->efi_nparts; i++) { 8117c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_size == 0) 8127c478bd9Sstevel@tonic-gate continue; /* Undefined partition */ 8137c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_tag == V_RESERVED) 8147c478bd9Sstevel@tonic-gate resv_part++; 8157c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_start > fullsz || 8167c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start + 8177c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size > fullsz) { 8187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 8197c478bd9Sstevel@tonic-gate fmthard: Partition %d specified as %lld sectors starting at %lld\n\ 8207c478bd9Sstevel@tonic-gate \tdoes not fit. The full disk contains %lld sectors.\n", 8217c478bd9Sstevel@tonic-gate i, efi->efi_parts[i].p_size, 8227c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start, fullsz); 8237c478bd9Sstevel@tonic-gate exit(1); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_tag != V_BACKUP && 8277c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size != fullsz) { 8287c478bd9Sstevel@tonic-gate for (j = 0; j < V_NUMPAR; j++) { 8297c478bd9Sstevel@tonic-gate if (efi->efi_parts[j].p_size == fullsz) 8307c478bd9Sstevel@tonic-gate continue; 8317c478bd9Sstevel@tonic-gate isize = efi->efi_parts[i].p_size; 8327c478bd9Sstevel@tonic-gate jsize = efi->efi_parts[j].p_size; 8337c478bd9Sstevel@tonic-gate istart = efi->efi_parts[i].p_start; 8347c478bd9Sstevel@tonic-gate jstart = efi->efi_parts[j].p_start; 8357c478bd9Sstevel@tonic-gate if ((i != j) && 8367c478bd9Sstevel@tonic-gate (isize != 0) && (jsize != 0)) { 8377c478bd9Sstevel@tonic-gate endsect = jstart + jsize - 1; 8387c478bd9Sstevel@tonic-gate if ((jstart <= istart) && 8397c478bd9Sstevel@tonic-gate (istart <= endsect)) { 8407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\ 8417c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\ 8427c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n", 8437c478bd9Sstevel@tonic-gate i, j); 8447c478bd9Sstevel@tonic-gate #if defined(sparc) 8457c478bd9Sstevel@tonic-gate exit(1); 8467c478bd9Sstevel@tonic-gate #endif 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate } 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate if (resv_part != 1) { 8537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8547c478bd9Sstevel@tonic-gate "expected one reserved partition, but found %d\n", 8557c478bd9Sstevel@tonic-gate resv_part); 8567c478bd9Sstevel@tonic-gate exit(1); 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate /* 8627c478bd9Sstevel@tonic-gate * Read the VTOC 8637c478bd9Sstevel@tonic-gate */ 8647c478bd9Sstevel@tonic-gate int 8657c478bd9Sstevel@tonic-gate vread(int fd, struct vtoc *vtoc, char *devname) 8667c478bd9Sstevel@tonic-gate { 8677c478bd9Sstevel@tonic-gate int i; 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate if ((i = read_vtoc(fd, vtoc)) < 0) { 8707c478bd9Sstevel@tonic-gate if (i == VT_ENOTSUP) { 8717c478bd9Sstevel@tonic-gate return (1); 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 8747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Invalid VTOC\n", 8757c478bd9Sstevel@tonic-gate devname); 8767c478bd9Sstevel@tonic-gate } else { 8777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot read VTOC\n", 8787c478bd9Sstevel@tonic-gate devname); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate exit(1); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate return (0); 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate void 8867c478bd9Sstevel@tonic-gate vread64(int fd, struct dk_gpt **efi_hdr, char *devname) 8877c478bd9Sstevel@tonic-gate { 8887c478bd9Sstevel@tonic-gate int i; 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate if ((i = efi_alloc_and_read(fd, efi_hdr)) < 0) { 8917c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) 8927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8937c478bd9Sstevel@tonic-gate "%s: this disk must be labeled first\n", 8947c478bd9Sstevel@tonic-gate devname); 8957c478bd9Sstevel@tonic-gate else 8967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8977c478bd9Sstevel@tonic-gate "%s: read_efi failed %d\n", 8987c478bd9Sstevel@tonic-gate devname, i); 8997c478bd9Sstevel@tonic-gate exit(1); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate lastlba = (*efi_hdr)->efi_last_u_lba; 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate /* 9057c478bd9Sstevel@tonic-gate * Write the VTOC 9067c478bd9Sstevel@tonic-gate */ 9077c478bd9Sstevel@tonic-gate void 9087c478bd9Sstevel@tonic-gate vwrite(int fd, struct vtoc *vtoc, char *devname) 9097c478bd9Sstevel@tonic-gate { 9107c478bd9Sstevel@tonic-gate int i; 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate if ((i = write_vtoc(fd, vtoc)) != 0) { 9137c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 9147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9157c478bd9Sstevel@tonic-gate "%s: invalid entry exists in vtoc\n", 9167c478bd9Sstevel@tonic-gate devname); 9177c478bd9Sstevel@tonic-gate } else { 9187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot write VTOC\n", 9197c478bd9Sstevel@tonic-gate devname); 9207c478bd9Sstevel@tonic-gate } 9217c478bd9Sstevel@tonic-gate exit(1); 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Write the VTOC 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate void 9297c478bd9Sstevel@tonic-gate vwrite64(int fd, struct dk_gpt *efi, char *devname) 9307c478bd9Sstevel@tonic-gate { 9317c478bd9Sstevel@tonic-gate int i; 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate if ((i = efi_write(fd, efi)) != 0) { 9347c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 9357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9367c478bd9Sstevel@tonic-gate "%s: invalid entry exists in vtoc\n", 9377c478bd9Sstevel@tonic-gate devname); 9387c478bd9Sstevel@tonic-gate } else { 9397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot write EFI\n", 9407c478bd9Sstevel@tonic-gate devname); 9417c478bd9Sstevel@tonic-gate } 9427c478bd9Sstevel@tonic-gate exit(1); 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate } 945