fmthard.c revision 3f2f09c1efd66f6d2995998ea72c5df8c70c9a97
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
b7b6b01a0d0622181a4c28dd60401f0ab2480d00Mark Andrews * CDDL HEADER START
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The contents of this file are subject to the terms of the
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Common Development and Distribution License (the "License").
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * You may not use this file except in compliance with the License.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * or http://www.opensolaris.org/os/licensing.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * See the License for the specific language governing permissions
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * and limitations under the License.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * When distributing Covered Code, include this CDDL HEADER in each
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * If applicable, add the following below this CDDL HEADER, with the
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * fields enclosed by brackets "[]" replaced with your own identifying
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * information: Portions Copyright [yyyy] [name of copyright owner]
b7b6b01a0d0622181a4c28dd60401f0ab2480d00Mark Andrews *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * CDDL HEADER END
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/* All Rights Reserved */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Portions of this source code were provided by International
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Computers Limited (ICL) under a development agreement with AT&T.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#pragma ident "%Z%%M% %I% %E% SMI"
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington/*
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews * Use is subject to license terms.
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews */
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence * Sun Microsystems version of fmthard:
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Supports the following arguments:
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence *
d88e1c30a4d4232a1a0520ad0d498b40a40cafd2Mark Andrews * -i Writes VTOC to stdout, rather than disk
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * -q Quick check: exit code 0 if VTOC ok
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * -d <data> Incremental changes to the VTOC
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * -n <vname> Change volume name to <vname>
d358eea93ef0e6a70b6f43b06b3ad24f79f69cd1Andreas Gustafsson * -s <file> Read VTOC information from <file>, or stdin ("-")
ba94ff2e56ff8cafeb5a701adfbe71bf9a5932e7Mark Andrews * -u <state> Reboot after writing VTOC, according to <state>:
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * boot: AD_BOOT (standard reboot)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * firm: AD_IBOOT (interactive reboot)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Note that fmthard cannot write a VTOC on an unlabeled disk.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * You must use format or SunInstall for this purpose.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * (NOTE: the above restriction only applies on Sparc systems).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The primary motivation for fmthard is to duplicate the
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * partitioning from disk to disk:
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * prtvtoc /dev/rdsk/c0t0d0s2 | fmthard -s - /dev/rdsk/c0t1d0s2
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <stdio.h>
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#include <fcntl.h>
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#include <errno.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <string.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <stdlib.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <unistd.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <sys/types.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <sys/param.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <sys/stat.h>
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#include <sys/uadmin.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <sys/open.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <sys/vtoc.h>
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#include <sys/dkio.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <sys/isa_defs.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <sys/efi_partition.h>
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#if defined(_SUNOS_VTOC_16)
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#include <sys/dklabel.h>
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#endif
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson#include <sys/sysmacros.h>
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson#ifndef SECSIZE
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson#define SECSIZE DEV_BSIZE
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson#endif /* SECSIZE */
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * External functions.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonextern int read_vtoc(int, struct vtoc *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonextern int write_vtoc(int, struct vtoc *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Internal functions.
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonextern int main(int, char **);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void display(struct dk_geom *, struct vtoc *, char *);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic void display64(struct dk_gpt *, char *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void insert(char *, struct vtoc *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void insert64(char *, struct dk_gpt *);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void load(FILE *, struct dk_geom *, struct vtoc *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void load64(FILE *, int fd, struct dk_gpt **);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void usage(void);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void validate(struct dk_geom *, struct vtoc *);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic void validate64(struct dk_gpt *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic int vread(int, struct vtoc *, char *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void vread64(int, struct dk_gpt **, char *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void vwrite(int, struct vtoc *, char *);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencestatic void vwrite64(int, struct dk_gpt *, char *);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Static variables.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic char *delta; /* Incremental update */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic short eflag; /* force write of an EFI label */
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonstatic short iflag; /* Prints VTOC w/o updating */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic short qflag; /* Check for a formatted disk */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic short uflag; /* Exit to firmware after writing */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /* new vtoc and reboot. Used during */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /* installation of core floppies */
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellingtonstatic diskaddr_t lastlba = 0; /* last LBA on 64-bit VTOC */
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington#if defined(sparc)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic char *uboot = "boot";
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#elif defined(i386)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/* use installgrub(1M) to install boot blocks */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic char *uboot = "";
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#else
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#error No platform defined.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif /* various platform-specific definitions */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic char *ufirm = "firm";
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson#if defined(_SUNOS_VTOC_16)
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencestatic int sectsiz;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencestatic struct vtoc disk_vtoc;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif /* defined(_SUNOS_VTOC_16) */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonmain(int argc, char **argv)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int fd;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int c;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson char *dfile;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson char *vname;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson struct stat statbuf;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#if defined(_SUNOS_VTOC_8)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews struct vtoc disk_vtoc;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#endif /* defined(_SUNOS_VTOC_8) */
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington struct dk_gpt *disk_efi;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington struct dk_geom disk_geom;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington int n;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington disk_efi = NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington dfile = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence vname = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#if defined(sparc)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while ((c = getopt(argc, argv, "ed:u:in:qs:")) != EOF)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#elif defined(i386)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while ((c = getopt(argc, argv, "ed:u:in:qb:p:s:")) != EOF)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#else
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington#error No platform defined.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson switch (c) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#if defined(i386)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson case 'p':
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson case 'b':
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson (void) fprintf(stderr,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "fmthard: -p and -b no longer supported."
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson " Use installgrub(1M) to install boot blocks\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson break;
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley#endif /* defined(i386) */
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley case 'd':
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson delta = optarg;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson break;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson case 'e':
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ++eflag;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson break;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson case 'i':
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson ++iflag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson break;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson case 'n':
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington vname = optarg;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington break;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington case 'q':
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ++qflag;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington break;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington case 's':
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington dfile = optarg;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington break;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington case 'u':
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (strcmp(uboot, optarg) == 0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ++uflag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson else if (strcmp(ufirm, optarg) == 0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson uflag = 2;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington break;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington default:
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington usage();
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (argc - optind != 1)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington usage();
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (stat(argv[optind], (struct stat *)&statbuf) == -1) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington (void) fprintf(stderr,
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington "fmthard: Cannot stat device %s\n",
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington argv[optind]);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington exit(1);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington (void) fprintf(stderr,
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington "fmthard: %s must be a raw device.\n",
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington argv[optind]);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington exit(1);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if ((fd = open(argv[optind], O_RDWR|O_NDELAY)) < 0) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington (void) fprintf(stderr, "fmthard: Cannot open device %s - %s\n",
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington argv[optind], strerror(errno));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(1);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Get the geometry information for this disk from the driver
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (!eflag && ioctl(fd, DKIOCGGEOM, &disk_geom)) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#ifdef DEBUG
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson perror("DKIOCGGEOM failed");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#endif /* DEBUG */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (errno == ENOTSUP) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson /* disk has EFI labels */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson eflag++;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) fprintf(stderr,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson "%s: Cannot get disk geometry\n", argv[optind]);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson (void) close(fd);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson exit(1);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington /*
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews * Read the vtoc on the disk
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews */
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews if (!eflag) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (vread(fd, &disk_vtoc, argv[optind]) == 1)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington eflag++;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (eflag && ((dfile == NULL) || qflag)) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington vread64(fd, &disk_efi, argv[optind]);
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington }
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson /*
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Quick check for valid disk: 0 if ok, 1 if not
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (qflag) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) close(fd);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (!eflag) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(disk_vtoc.v_sanity == VTOC_SANE ? 0 : 1);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley } else {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington exit(disk_efi->efi_version <= EFI_VERSION102 ? 0 : 1);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews * Incremental changes to the VTOC
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews */
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if (delta) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if (!eflag) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson insert(delta, &disk_vtoc);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews validate(&disk_geom, &disk_vtoc);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vwrite(fd, &disk_vtoc, argv[optind]);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews } else {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson insert64(delta, disk_efi);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson validate64(disk_efi);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vwrite64(fd, disk_efi, argv[optind]);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson (void) close(fd);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(0);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson }
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (!dfile && !vname)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson usage();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Read new VTOC from stdin or data file
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (dfile) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (strcmp(dfile, "-") == 0) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (!eflag)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington load(stdin, &disk_geom, &disk_vtoc);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington else
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington load64(stdin, fd, &disk_efi);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington } else {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington FILE *fp;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if ((fp = fopen(dfile, "r")) == NULL) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington (void) fprintf(stderr, "Cannot open file %s\n",
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington dfile);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington (void) close(fd);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington exit(1);
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (!eflag)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence load(fp, &disk_geom, &disk_vtoc);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley else
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson load64(fp, fd, &disk_efi);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley (void) fclose(fp);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Print the modified VTOC, rather than updating the disk
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (iflag) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (!eflag)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson display(&disk_geom, &disk_vtoc, argv[optind]);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson else
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson display64(disk_efi, argv[optind]);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) close(fd);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (vname) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson n = MIN(strlen(vname) + 1, LEN_DKL_VVOL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (!eflag) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) memcpy(disk_vtoc.v_volume, vname, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (c = 0; c < disk_efi->efi_nparts; c++) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (disk_efi->efi_parts[c].p_tag ==
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson V_RESERVED) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) memcpy(&disk_efi->efi_parts[c].p_name,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vname, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews /*
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * Write the new VTOC on the disk
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (!eflag) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson validate(&disk_geom, &disk_vtoc);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington vwrite(fd, &disk_vtoc, argv[optind]);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington } else {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington validate64(disk_efi);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington vwrite64(fd, disk_efi, argv[optind]);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson /*
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * Shut system down after writing a new vtoc to disk
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * This is used during installation of core floppies.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson */
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (uflag == 1)
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) uadmin(A_REBOOT, AD_BOOT, 0);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence else if (uflag == 2)
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) uadmin(A_REBOOT, AD_IBOOT, 0);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson (void) printf("fmthard: New volume table of contents now in place.\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence return (0);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson}
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence/*
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * display ()
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * display contents of VTOC without writing it to disk
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondisplay(struct dk_geom *geom, struct vtoc *vtoc, char *device)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int i;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence int c;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Print out the VTOC
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* %s default partition map\n", device);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (*vtoc->v_volume) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (void) printf("* Volume Name: ");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington for (i = 0; i < LEN_DKL_VVOL; i++) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if ((c = vtoc->v_volume[i]) == 0)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington break;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf("%c", c);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf("\n");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("*\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* Dimensions:\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) printf("* %d bytes/sector\n", SECSIZE);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (void) printf("* %d sectors/track\n", geom->dkg_nsect);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* %d tracks/cylinder\n", geom->dkg_nhead);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* %d cylinders\n", geom->dkg_pcyl);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* %d accessible cylinders\n", geom->dkg_ncyl);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (void) printf("*\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) printf("* Flags:\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* 1: unmountable\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* 10: read-only\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("*\n");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (void) printf(
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson"\n* Partition Tag Flag First Sector Sector Count\n");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson for (i = 0; i < V_NUMPAR; i++) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (vtoc->v_part[i].p_size > 0)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf(
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington" %d %d 0%x %ld %ld\n",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson i, vtoc->v_part[i].p_tag,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_flag,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence vtoc->v_part[i].p_start,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_size);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence exit(0);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * display64 ()
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * display64 contents of EFI partition without writing it to disk
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington */
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellingtonstatic void
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellingtondisplay64(struct dk_gpt *efi, char *device)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington int i;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence /*
bd703253b745aaec6e45a60108bec513bf79bd2cBrian Wellington * Print out the VTOC
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington */
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf("* %s default partition map\n", device);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf("*\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) printf("* Dimensions:\n");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf("* %d bytes/sector\n", efi->efi_lbasize);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington (void) printf("* N/A sectors/track\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) printf("* N/A tracks/cylinder\n");
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson (void) printf("* N/A cylinders\n");
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson (void) printf("* N/A accessible cylinders\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) printf("*\n");
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson (void) printf("* Flags:\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) printf("* 1: unmountable\n");
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson (void) printf("* 10: read-only\n");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson (void) printf("*\n");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson (void) printf(
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson"\n* Partition Tag Flag First Sector Sector Count\n");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson for (i = 0; i < efi->efi_nparts; i++) {
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson if (efi->efi_parts[i].p_size > 0)
7acfb3bcd29db4f19aa42690826e57dafdd28cdaAndreas Gustafsson (void) printf(
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson" %d %d 0%x %8lld %8lld\n",
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence i, efi->efi_parts[i].p_tag,
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson efi->efi_parts[i].p_flag,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence efi->efi_parts[i].p_start,
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson efi->efi_parts[i].p_size);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson exit(0);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson}
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * insert()
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson *
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * Insert a change into the VTOC.
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson */
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafssonstatic void
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafssoninsert(char *data, struct vtoc *vtoc)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson{
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson int part;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson int tag;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson uint_t flag;
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson daddr_t start;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence long size;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson if (sscanf(data, "%d:%d:%x:%ld:%ld",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson &part, &tag, &flag, &start, &size) != 5) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (part >= V_NUMPAR) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (void) fprintf(stderr,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence "Error in data \"%s\": No such partition %x\n",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson data, part);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(1);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[part].p_tag = (ushort_t)tag;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson vtoc->v_part[part].p_flag = (ushort_t)flag;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson vtoc->v_part[part].p_start = start;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson vtoc->v_part[part].p_size = size;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson}
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson/*
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson * insert64()
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson *
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson * Insert a change into the VTOC.
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoninsert64(char *data, struct dk_gpt *efi)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews int part;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence int tag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson uint_t flag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson diskaddr_t start;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson diskaddr_t size;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (sscanf(data, "%d:%d:%x:%lld:%lld",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson &part, &tag, &flag, &start, &size) != 5) {
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson exit(1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (part >= efi->efi_nparts) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (void) fprintf(stderr,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson "Error in data \"%s\": No such partition %x\n",
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson data, part);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
e485d16462b0a66b835107fae04fe9d562e77af0Bob Halley efi->efi_parts[part].p_tag = (ushort_t)tag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson efi->efi_parts[part].p_flag = (ushort_t)flag;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson efi->efi_parts[part].p_start = start;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson efi->efi_parts[part].p_size = size;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson/*
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * load()
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson *
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * Load VTOC information from a datafile.
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic void
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonload(FILE *fp, struct dk_geom *geom, struct vtoc *vtoc)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson{
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson int part;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int tag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson uint_t flag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson daddr_t start;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson long size;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson char line[256];
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int i;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson long nblks;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson long fullsz;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (i = 0; i < V_NUMPAR; ++i) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence vtoc->v_part[i].p_tag = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_flag = V_UNMNT;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_start = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_size = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * initialize partition 2, by convention it corresponds to whole
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson * disk. It will be overwritten, if specified in the input datafile
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fullsz = geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vtoc->v_part[2].p_tag = V_BACKUP;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vtoc->v_part[2].p_flag = V_UNMNT;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vtoc->v_part[2].p_start = 0;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vtoc->v_part[2].p_size = fullsz;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews nblks = geom->dkg_nsect * geom->dkg_nhead;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews while (fgets(line, sizeof (line) - 1, fp)) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews continue;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson line[strlen(line) - 1] = '\0';
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (sscanf(line, "%d %d %x %ld %ld",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson &part, &tag, &flag, &start, &size) != 5) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (void) fprintf(stderr, "Syntax error: \"%s\"\n",
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson line);
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews exit(1);
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (part >= V_NUMPAR) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (void) fprintf(stderr,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson "No such partition %x: \"%s\"\n",
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson part, line);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson exit(1);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson }
3476dfe9d7e0a13338a16ee096f68d73c5a60bf2Mark Andrews if (!eflag && ((start % nblks) != 0 || (size % nblks) != 0)) {
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson (void) fprintf(stderr,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson"Partition %d not aligned on cylinder boundary: \"%s\"\n",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson part, line);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson vtoc->v_part[part].p_tag = (ushort_t)tag;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson vtoc->v_part[part].p_flag = (ushort_t)flag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[part].p_start = start;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson vtoc->v_part[part].p_size = size;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (part = 0; part < V_NUMPAR; part++) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson vtoc->timestamp[part] = (time_t)0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence/*
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence * load64()
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson *
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * Load VTOC information from a datafile.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrenceload64(FILE *fp, int fd, struct dk_gpt **efi)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence{
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson int part;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int tag;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence uint_t flag;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson diskaddr_t start;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence diskaddr_t size;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int nlines = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson char line[256];
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int i;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence uint_t max_part = 0;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence char **mem = NULL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence while (fgets(line, sizeof (line) - 1, fp)) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence continue;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence line[strlen(line) - 1] = '\0';
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (sscanf(line, "%d %d %x %lld %lld",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence &part, &tag, &flag, &start, &size) != 5) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr, "Syntax error: \"%s\"\n",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence line);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence exit(1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence mem = realloc(mem, sizeof (*mem) * (nlines + 1));
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (mem == NULL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr, "realloc failed\n");
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews exit(1);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence mem[nlines] = strdup(line);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (mem[nlines] == NULL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr, "strdup failed\n");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson exit(1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson nlines++;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (part > max_part)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson max_part = part;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson max_part++;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if ((i = efi_alloc_and_init(fd, max_part, efi)) < 0) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson (void) fprintf(stderr,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence "efi_alloc_and_init failed: %d\n", i);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence exit(1);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence for (i = 0; i < (*efi)->efi_nparts; ++i) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (*efi)->efi_parts[i].p_tag = V_UNASSIGNED;
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews (*efi)->efi_parts[i].p_flag = V_UNMNT;
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews (*efi)->efi_parts[i].p_start = 0;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (*efi)->efi_parts[i].p_size = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson lastlba = (*efi)->efi_last_u_lba;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson for (i = 0; i < nlines; i++) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (sscanf(mem[i], "%d %d %x %lld %lld",
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson &part, &tag, &flag, &start, &size) != 5) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson (void) fprintf(stderr, "Syntax error: \"%s\"\n",
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson line);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson exit(1);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence free(mem[i]);
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson if (part >= (*efi)->efi_nparts) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson "No such partition %x: \"%s\"\n",
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff part, line);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff exit(1);
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson }
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson (*efi)->efi_parts[part].p_tag = (ushort_t)tag;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson (*efi)->efi_parts[part].p_flag = (ushort_t)flag;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson (*efi)->efi_parts[part].p_start = start;
937ccb270ef87dc24e30fcca15dd23ebd20873abAndreas Gustafsson (*efi)->efi_parts[part].p_size = size;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (*efi)->efi_nparts = max_part;
937ccb270ef87dc24e30fcca15dd23ebd20873abAndreas Gustafsson free(mem);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencestatic void
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceusage()
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence{
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (void) fprintf(stderr,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#if defined(sparc)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews"Usage: fmthard [ -i ] [ -n volumename ] [ -s datafile ] [ -d arguments] \
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonraw-device\n");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#elif defined(i386)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews"Usage: fmthard [ -i ] [ -S ] [-I geom_file] \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews-n volumename | -s datafile [ -d arguments] raw-device\n");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#else
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#error No platform defined.
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson#endif
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson exit(2);
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson}
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson/*
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson * validate()
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson *
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence * Validate the new VTOC.
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence */
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonstatic void
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencevalidate(struct dk_geom *geom, struct vtoc *vtoc)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence{
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence int i;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence int j;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence long fullsz;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence long endsect;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence daddr_t istart;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence daddr_t jstart;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence long isize;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence long jsize;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence long nblks;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence nblks = geom->dkg_nsect * geom->dkg_nhead;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson fullsz = geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson#if defined(_SUNOS_VTOC_16)
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson /* make the vtoc look sane - ha ha */
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson vtoc->v_version = V_VERSION;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vtoc->v_sanity = VTOC_SANE;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews vtoc->v_nparts = V_NUMPAR;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (sectsiz == 0)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson sectsiz = SECSIZE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (vtoc->v_sectorsz == 0)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson vtoc->v_sectorsz = sectsiz;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#endif /* defined(_SUNOS_VTOC_16) */
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence for (i = 0; i < V_NUMPAR; i++) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (vtoc->v_part[i].p_tag == V_BACKUP) {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff if (vtoc->v_part[i].p_size != fullsz) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) fprintf(stderr, "\
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencefmthard: Partition %d specifies the full disk and is not equal\n\
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencefull size of disk. The full disk capacity is %lu sectors.\n", i, fullsz);
937ccb270ef87dc24e30fcca15dd23ebd20873abAndreas Gustafsson#if defined(sparc)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence exit(1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#endif
ba94ff2e56ff8cafeb5a701adfbe71bf9a5932e7Mark Andrews }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (vtoc->v_part[i].p_size == 0)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence continue; /* Undefined partition */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if ((vtoc->v_part[i].p_start % nblks) ||
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson (vtoc->v_part[i].p_size % nblks)) {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff (void) fprintf(stderr, "\
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonfmthard: Partition %d not aligned on cylinder boundary \n", i);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson exit(1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (vtoc->v_part[i].p_start > fullsz ||
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_start +
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_size > fullsz) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr, "\
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonfmthard: Partition %d specified as %lu sectors starting at %lu\n\
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence\tdoes not fit. The full disk contains %lu sectors.\n",
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson i, vtoc->v_part[i].p_size,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_start, fullsz);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#if defined(sparc)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson exit(1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson if (vtoc->v_part[i].p_tag != V_BACKUP &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vtoc->v_part[i].p_size != fullsz) {
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence for (j = 0; j < V_NUMPAR; j++) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (vtoc->v_part[j].p_tag == V_BACKUP)
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence continue;
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence if (vtoc->v_part[j].p_size == fullsz)
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence continue;
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence isize = vtoc->v_part[i].p_size;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence jsize = vtoc->v_part[j].p_size;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence istart = vtoc->v_part[i].p_start;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence jstart = vtoc->v_part[j].p_start;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if ((i != j) &&
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (isize != 0) && (jsize != 0)) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence endsect = jstart + jsize -1;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence if ((jstart <= istart) &&
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (istart <= endsect)) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr, "\
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencefmthard: Partition %d overlaps partition %d. Overlap is allowed\n\
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence\tonly on partition on the full disk partition).\n",
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence i, j);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#if defined(sparc)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence exit(1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#endif
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence/*
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence * validate64()
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence *
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence * Validate the new VTOC.
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencevalidate64(struct dk_gpt *efi)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence{
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int i;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int j;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int resv_part = 0;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence diskaddr_t endsect;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence diskaddr_t fullsz;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson diskaddr_t istart;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson diskaddr_t jstart;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson diskaddr_t isize;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence diskaddr_t jsize;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence fullsz = lastlba + 1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence for (i = 0; i < efi->efi_nparts; i++) {
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence if (efi->efi_parts[i].p_size == 0)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson continue; /* Undefined partition */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (efi->efi_parts[i].p_tag == V_RESERVED)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence resv_part++;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (efi->efi_parts[i].p_start > fullsz ||
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence efi->efi_parts[i].p_start +
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews efi->efi_parts[i].p_size > fullsz) {
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews (void) fprintf(stderr, "\
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencefmthard: Partition %d specified as %lld sectors starting at %lld\n\
dee489d6c1487c5fb450e352c48b1a37848e9b6cMark Andrews\tdoes not fit. The full disk contains %lld sectors.\n",
dee489d6c1487c5fb450e352c48b1a37848e9b6cMark Andrews i, efi->efi_parts[i].p_size,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson efi->efi_parts[i].p_start, fullsz);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exit(1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (efi->efi_parts[i].p_tag != V_BACKUP &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson efi->efi_parts[i].p_size != fullsz) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson for (j = 0; j < V_NUMPAR; j++) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (efi->efi_parts[j].p_size == fullsz)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson continue;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isize = efi->efi_parts[i].p_size;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson jsize = efi->efi_parts[j].p_size;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews istart = efi->efi_parts[i].p_start;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews jstart = efi->efi_parts[j].p_start;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if ((i != j) &&
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (isize != 0) && (jsize != 0)) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson endsect = jstart + jsize - 1;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if ((jstart <= istart) &&
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (istart <= endsect)) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) fprintf(stderr, "\
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonfmthard: Partition %d overlaps partition %d. Overlap is allowed\n\
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson\tonly on partition on the full disk partition).\n",
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson i, j);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#if defined(sparc)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson exit(1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson if (resv_part != 1) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson (void) fprintf(stderr,
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence "expected one reserved partition, but found %d\n",
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson resv_part);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson exit(1);
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence}
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence * Read the VTOC
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceint
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvread(int fd, struct vtoc *vtoc, char *devname)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson int i;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if ((i = read_vtoc(fd, vtoc)) < 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (i == VT_ENOTSUP) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (1);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (i == VT_EINVAL) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (void) fprintf(stderr, "%s: Invalid VTOC\n",
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence devname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) fprintf(stderr, "%s: Cannot read VTOC\n",
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews devname);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews exit(1);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews return (0);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews}
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonvoid
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonvread64(int fd, struct dk_gpt **efi_hdr, char *devname)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews{
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews int i;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if ((i = efi_alloc_and_read(fd, efi_hdr)) < 0) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (i == VT_EINVAL)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (void) fprintf(stderr,
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson "%s: this disk must be labeled first\n",
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson devname);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews else
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (void) fprintf(stderr,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "%s: read_efi failed %d\n",
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews devname, i);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews exit(1);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews lastlba = (*efi_hdr)->efi_last_u_lba;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews}
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence/*
ed8daea26475309e1ac8ed8556b7d4ec8ae4432cDavid Lawrence * Write the VTOC
ed8daea26475309e1ac8ed8556b7d4ec8ae4432cDavid Lawrence */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencevoid
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencevwrite(int fd, struct vtoc *vtoc, char *devname)
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence{
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence int i;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if ((i = write_vtoc(fd, vtoc)) != 0) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (i == VT_EINVAL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (void) fprintf(stderr,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence "%s: invalid entry exists in vtoc\n",
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence devname);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence } else {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (void) fprintf(stderr, "%s: Cannot write VTOC\n",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence devname);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence exit(1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence/*
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence * Write the VTOC
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencevoid
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencevwrite64(int fd, struct dk_gpt *efi, char *devname)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence{
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews int i;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if ((i = efi_write(fd, efi)) != 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (i == VT_EINVAL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void) fprintf(stderr,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "%s: invalid entry exists in vtoc\n",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson devname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff (void) fprintf(stderr, "%s: Cannot write EFI\n",
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence devname);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson exit(1);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence}
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff