18fae3551SRodney W. Grimes /* 2fd43aa1cSDavid E. O'Brien * Copyright (c) 1994, 1995 Gordon W. Ross 3fd43aa1cSDavid E. O'Brien * Copyright (c) 1994 Theo de Raadt 4fd43aa1cSDavid E. O'Brien * All rights reserved. 58fae3551SRodney W. Grimes * Copyright (c) 1987, 1993 68fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 98fae3551SRodney W. Grimes * Symmetric Computer Systems. 108fae3551SRodney W. Grimes * 118fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 128fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 138fae3551SRodney W. Grimes * are met: 148fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 158fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 168fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 178fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 188fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 198fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 208fae3551SRodney W. Grimes * must display the following acknowledgement: 218fae3551SRodney W. Grimes * This product includes software developed by the University of 228fae3551SRodney W. Grimes * California, Berkeley and its contributors. 23fd43aa1cSDavid E. O'Brien * This product includes software developed by Theo de Raadt. 248fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 258fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 268fae3551SRodney W. Grimes * without specific prior written permission. 278fae3551SRodney W. Grimes * 288fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 298fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 308fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 318fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 328fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 338fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 348fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 358fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 368fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 378fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 388fae3551SRodney W. Grimes * SUCH DAMAGE. 39fd43aa1cSDavid E. O'Brien * 40fd43aa1cSDavid E. O'Brien * from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $ 418fae3551SRodney W. Grimes */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 446bd343a9SPhilippe Charnier static const char copyright[] = 458fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\ 468fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 478fae3551SRodney W. Grimes #endif /* not lint */ 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes #ifndef lint 506bd343a9SPhilippe Charnier #if 0 518fae3551SRodney W. Grimes static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94"; 528fae3551SRodney W. Grimes /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 536bd343a9SPhilippe Charnier #endif 548fae3551SRodney W. Grimes #endif /* not lint */ 558fae3551SRodney W. Grimes 56c8223965SMark Murray #include <sys/cdefs.h> 57c8223965SMark Murray __FBSDID("$FreeBSD$"); 58c8223965SMark Murray 598fae3551SRodney W. Grimes #include <sys/param.h> 60dfbc3f0cSPoul-Henning Kamp #include <stdint.h> 618fae3551SRodney W. Grimes #include <sys/file.h> 628fae3551SRodney W. Grimes #include <sys/stat.h> 6361de51caSJoerg Wunsch #include <sys/wait.h> 64b9d05a16SPoul-Henning Kamp #include <sys/disk.h> 658fae3551SRodney W. Grimes #define DKTYPENAMES 66b35e6950SBosko Milekic #define FSTYPENAMES 678fae3551SRodney W. Grimes #include <sys/disklabel.h> 68bc33ea1aSRuslan Ermilov 698fae3551SRodney W. Grimes #include <unistd.h> 708fae3551SRodney W. Grimes #include <string.h> 718fae3551SRodney W. Grimes #include <stdio.h> 725daa806dSPoul-Henning Kamp #include <libgeom.h> 7361de51caSJoerg Wunsch #include <stdlib.h> 7461de51caSJoerg Wunsch #include <signal.h> 7561de51caSJoerg Wunsch #include <stdarg.h> 768fae3551SRodney W. Grimes #include <ctype.h> 77bef2080aSPhilippe Charnier #include <err.h> 7837736675SWarner Losh #include <errno.h> 79c8223965SMark Murray 808fae3551SRodney W. Grimes #include "pathnames.h" 818fae3551SRodney W. Grimes 823233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 833233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 843233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 853233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 863233afaeSJohn W. De Boskey 873233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 88f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG 2048U 893233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 903233afaeSJohn W. De Boskey 91d2fe97c7SPoul-Henning Kamp static void makelabel(const char *, struct disklabel *); 92d2fe97c7SPoul-Henning Kamp static int writelabel(void); 93d2fe97c7SPoul-Henning Kamp static int readlabel(int flag); 94d2fe97c7SPoul-Henning Kamp static void display(FILE *, const struct disklabel *); 95d2fe97c7SPoul-Henning Kamp static int edit(void); 96d2fe97c7SPoul-Henning Kamp static int editit(void); 97d2fe97c7SPoul-Henning Kamp static char *skip(char *); 98d2fe97c7SPoul-Henning Kamp static char *word(char *); 99d2fe97c7SPoul-Henning Kamp static int getasciilabel(FILE *, struct disklabel *); 100d2fe97c7SPoul-Henning Kamp static int getasciipartspec(char *, struct disklabel *, int, int); 101d2fe97c7SPoul-Henning Kamp static int checklabel(struct disklabel *); 102d2fe97c7SPoul-Henning Kamp static void Warning(const char *, ...) __printflike(1, 2); 103d2fe97c7SPoul-Henning Kamp static void usage(void); 104d2fe97c7SPoul-Henning Kamp static struct disklabel *getvirginlabel(void); 10561de51caSJoerg Wunsch 1068fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1078fae3551SRodney W. Grimes 108d2fe97c7SPoul-Henning Kamp static char *dkname; 109d2fe97c7SPoul-Henning Kamp static char *specname; 110d2fe97c7SPoul-Henning Kamp static char tmpfil[] = PATH_TMPFILE; 1118fae3551SRodney W. Grimes 112d2fe97c7SPoul-Henning Kamp static struct disklabel lab; 113d2fe97c7SPoul-Henning Kamp static u_char bootarea[BBSIZE]; 114d2fe97c7SPoul-Henning Kamp static char blank[] = ""; 115d2fe97c7SPoul-Henning Kamp static char unknown[] = "unknown"; 1168fae3551SRodney W. Grimes 1173233afaeSJohn W. De Boskey #define MAX_PART ('z') 1183233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 119d2fe97c7SPoul-Henning Kamp static char part_size_type[MAX_NUM_PARTS]; 120d2fe97c7SPoul-Henning Kamp static char part_offset_type[MAX_NUM_PARTS]; 121d2fe97c7SPoul-Henning Kamp static int part_set[MAX_NUM_PARTS]; 1223233afaeSJohn W. De Boskey 123d2fe97c7SPoul-Henning Kamp static int installboot; /* non-zero if we should install a boot program */ 124d2fe97c7SPoul-Henning Kamp static char const *xxboot; /* primary boot */ 1258fae3551SRodney W. Grimes 126d2fe97c7SPoul-Henning Kamp static int labeloffset = LABELOFFSET + LABELSECTOR * DEV_BSIZE; 1277838fd0eSPoul-Henning Kamp static int bbsize = BBSIZE; 1287838fd0eSPoul-Henning Kamp static int alphacksum = 1297838fd0eSPoul-Henning Kamp #if defined(__alpha__) 1307838fd0eSPoul-Henning Kamp 1; 1317838fd0eSPoul-Henning Kamp #else 1327838fd0eSPoul-Henning Kamp 0; 1337838fd0eSPoul-Henning Kamp #endif 134c80f9755SPoul-Henning Kamp 1358fae3551SRodney W. Grimes enum { 136f080d33bSPoul-Henning Kamp UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT 1378fae3551SRodney W. Grimes } op = UNSPEC; 1388fae3551SRodney W. Grimes 139bc33ea1aSRuslan Ermilov 140d2fe97c7SPoul-Henning Kamp static int disable_write; /* set to disable writing to disk label */ 1418fae3551SRodney W. Grimes 14261de51caSJoerg Wunsch int 143326c7cdaSWarner Losh main(int argc, char *argv[]) 1448fae3551SRodney W. Grimes { 1458fae3551SRodney W. Grimes FILE *t; 1465daa806dSPoul-Henning Kamp int ch, error = 0; 147d2fe97c7SPoul-Henning Kamp char const *name = 0; 1488fae3551SRodney W. Grimes 149bc33ea1aSRuslan Ermilov while ((ch = getopt(argc, argv, "Bb:em:nRrs:w")) != -1) 1508fae3551SRodney W. Grimes switch (ch) { 1518fae3551SRodney W. Grimes case 'B': 1528fae3551SRodney W. Grimes ++installboot; 1538fae3551SRodney W. Grimes break; 1548fae3551SRodney W. Grimes case 'b': 1558fae3551SRodney W. Grimes xxboot = optarg; 1568fae3551SRodney W. Grimes break; 157bc33ea1aSRuslan Ermilov case 'm': 158c80f9755SPoul-Henning Kamp if (!strcmp(optarg, "i386")) { 159c80f9755SPoul-Henning Kamp labeloffset = 512; 160c80f9755SPoul-Henning Kamp bbsize = 8192; 1617838fd0eSPoul-Henning Kamp alphacksum = 0; 162c80f9755SPoul-Henning Kamp } else if (!strcmp(optarg, "alpha")) { 163c80f9755SPoul-Henning Kamp labeloffset = 64; 164c80f9755SPoul-Henning Kamp bbsize = 8192; 165c80f9755SPoul-Henning Kamp alphacksum = 1; 166d2fe97c7SPoul-Henning Kamp } else { 167d2fe97c7SPoul-Henning Kamp errx(1, "Unsupported architecture"); 168c80f9755SPoul-Henning Kamp } 169bc33ea1aSRuslan Ermilov break; 1703233afaeSJohn W. De Boskey case 'n': 1713233afaeSJohn W. De Boskey disable_write = 1; 1723233afaeSJohn W. De Boskey break; 1738fae3551SRodney W. Grimes case 'R': 1748fae3551SRodney W. Grimes if (op != UNSPEC) 1758fae3551SRodney W. Grimes usage(); 1768fae3551SRodney W. Grimes op = RESTORE; 1778fae3551SRodney W. Grimes break; 1788fae3551SRodney W. Grimes case 'e': 1798fae3551SRodney W. Grimes if (op != UNSPEC) 1808fae3551SRodney W. Grimes usage(); 1818fae3551SRodney W. Grimes op = EDIT; 1828fae3551SRodney W. Grimes break; 1838fae3551SRodney W. Grimes case 'r': 184d2fe97c7SPoul-Henning Kamp /* 185d2fe97c7SPoul-Henning Kamp * We accept and ignode -r for compatibility with 186d2fe97c7SPoul-Henning Kamp * historically disklabel usage. 187d2fe97c7SPoul-Henning Kamp */ 1888fae3551SRodney W. Grimes break; 1898fae3551SRodney W. Grimes case 'w': 1908fae3551SRodney W. Grimes if (op != UNSPEC) 1918fae3551SRodney W. Grimes usage(); 1928fae3551SRodney W. Grimes op = WRITE; 1938fae3551SRodney W. Grimes break; 1948fae3551SRodney W. Grimes case '?': 1958fae3551SRodney W. Grimes default: 1968fae3551SRodney W. Grimes usage(); 1978fae3551SRodney W. Grimes } 1988fae3551SRodney W. Grimes argc -= optind; 1998fae3551SRodney W. Grimes argv += optind; 200d2fe97c7SPoul-Henning Kamp 2018fae3551SRodney W. Grimes if (argc < 1) 2028fae3551SRodney W. Grimes usage(); 2038fae3551SRodney W. Grimes 2045daa806dSPoul-Henning Kamp /* Figure out the names of the thing we're working on */ 2055daa806dSPoul-Henning Kamp if (argv[0][0] != '/') { 2068fae3551SRodney W. Grimes dkname = argv[0]; 2075daa806dSPoul-Henning Kamp asprintf(&specname, "%s%s", _PATH_DEV, argv[0]); 2085daa806dSPoul-Henning Kamp } else { 2095daa806dSPoul-Henning Kamp dkname = strrchr(argv[0], '/'); 2105daa806dSPoul-Henning Kamp dkname++; 2115daa806dSPoul-Henning Kamp specname = argv[0]; 2128fae3551SRodney W. Grimes } 213d2fe97c7SPoul-Henning Kamp 214d2fe97c7SPoul-Henning Kamp if (installboot && op == UNSPEC) 215d2fe97c7SPoul-Henning Kamp op = WRITEBOOT; 216d2fe97c7SPoul-Henning Kamp else if (op == UNSPEC) 217d2fe97c7SPoul-Henning Kamp op = READ; 2188fae3551SRodney W. Grimes 2198fae3551SRodney W. Grimes switch(op) { 2208fae3551SRodney W. Grimes 2213b3038a6SBill Fumerola case UNSPEC: 2223b3038a6SBill Fumerola break; 2233b3038a6SBill Fumerola 2248fae3551SRodney W. Grimes case EDIT: 2258fae3551SRodney W. Grimes if (argc != 1) 2268fae3551SRodney W. Grimes usage(); 227d2fe97c7SPoul-Henning Kamp readlabel(1); 228d2fe97c7SPoul-Henning Kamp error = edit(); 2298fae3551SRodney W. Grimes break; 2308fae3551SRodney W. Grimes 2318fae3551SRodney W. Grimes case READ: 2328fae3551SRodney W. Grimes if (argc != 1) 2338fae3551SRodney W. Grimes usage(); 234d2fe97c7SPoul-Henning Kamp readlabel(1); 235d2fe97c7SPoul-Henning Kamp display(stdout, NULL); 236d2fe97c7SPoul-Henning Kamp error = checklabel(NULL); 2378fae3551SRodney W. Grimes break; 2388fae3551SRodney W. Grimes 2398fae3551SRodney W. Grimes case RESTORE: 2408fae3551SRodney W. Grimes if (argc != 2) 2418fae3551SRodney W. Grimes usage(); 2428fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 2435daa806dSPoul-Henning Kamp err(4, "fopen %s", argv[1]); 244d2fe97c7SPoul-Henning Kamp readlabel(0); 2456cabb348SBruce Evans if (!getasciilabel(t, &lab)) 2466cabb348SBruce Evans exit(1); 247d2fe97c7SPoul-Henning Kamp error = writelabel(); 2488fae3551SRodney W. Grimes break; 2498fae3551SRodney W. Grimes 2508fae3551SRodney W. Grimes case WRITE: 251d2fe97c7SPoul-Henning Kamp if (argc == 2) 252d2fe97c7SPoul-Henning Kamp name = argv[1]; 253d2fe97c7SPoul-Henning Kamp else if (argc == 1) 254d2fe97c7SPoul-Henning Kamp name = "auto"; 255d2fe97c7SPoul-Henning Kamp else 2568fae3551SRodney W. Grimes usage(); 257d2fe97c7SPoul-Henning Kamp readlabel(0); 258d2fe97c7SPoul-Henning Kamp makelabel(name, &lab); 259d2fe97c7SPoul-Henning Kamp if (checklabel(NULL) == 0) 260d2fe97c7SPoul-Henning Kamp error = writelabel(); 2618fae3551SRodney W. Grimes break; 2628fae3551SRodney W. Grimes 2638fae3551SRodney W. Grimes case WRITEBOOT: 2648fae3551SRodney W. Grimes 265d2fe97c7SPoul-Henning Kamp readlabel(1); 2668fae3551SRodney W. Grimes if (argc == 2) 267d2fe97c7SPoul-Henning Kamp makelabel(argv[1], &lab); 268d2fe97c7SPoul-Henning Kamp if (checklabel(NULL) == 0) 269d2fe97c7SPoul-Henning Kamp error = writelabel(); 2708fae3551SRodney W. Grimes break; 2718fae3551SRodney W. Grimes } 2728fae3551SRodney W. Grimes exit(error); 2738fae3551SRodney W. Grimes } 2748fae3551SRodney W. Grimes 2758fae3551SRodney W. Grimes /* 276ef9ab0b3SRuslan Ermilov * Construct a prototype disklabel from /etc/disktab. 2778fae3551SRodney W. Grimes */ 278d2fe97c7SPoul-Henning Kamp static void 279d2fe97c7SPoul-Henning Kamp makelabel(const char *type, struct disklabel *lp) 2808fae3551SRodney W. Grimes { 281326c7cdaSWarner Losh struct disklabel *dp; 282425bed3aSJoerg Wunsch 283425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 284425bed3aSJoerg Wunsch dp = getvirginlabel(); 285425bed3aSJoerg Wunsch else 2868fae3551SRodney W. Grimes dp = getdiskbyname(type); 2876bd343a9SPhilippe Charnier if (dp == NULL) 2886bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 2898fae3551SRodney W. Grimes *lp = *dp; 2908fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 2918fae3551SRodney W. Grimes } 2928fae3551SRodney W. Grimes 293d2fe97c7SPoul-Henning Kamp static void 294d2fe97c7SPoul-Henning Kamp readboot(void) 295d2fe97c7SPoul-Henning Kamp { 296d2fe97c7SPoul-Henning Kamp int fd, i; 297d2fe97c7SPoul-Henning Kamp struct stat st; 298d2fe97c7SPoul-Henning Kamp 299d2fe97c7SPoul-Henning Kamp if (xxboot == NULL) 300d2fe97c7SPoul-Henning Kamp xxboot = "/boot/boot"; 301d2fe97c7SPoul-Henning Kamp fd = open(xxboot, O_RDONLY); 302d2fe97c7SPoul-Henning Kamp if (fd < 0) 303d2fe97c7SPoul-Henning Kamp err(1, "cannot open %s", xxboot); 304d2fe97c7SPoul-Henning Kamp fstat(fd, &st); 305d2fe97c7SPoul-Henning Kamp if (st.st_size == BBSIZE) { 306d2fe97c7SPoul-Henning Kamp i = read(fd, bootarea, BBSIZE); 307d2fe97c7SPoul-Henning Kamp if (i != BBSIZE) 308d2fe97c7SPoul-Henning Kamp err(1, "read error %s", xxboot); 309d2fe97c7SPoul-Henning Kamp return; 310d2fe97c7SPoul-Henning Kamp } 311d2fe97c7SPoul-Henning Kamp if (alphacksum && st.st_size == BBSIZE - 512) { 312d2fe97c7SPoul-Henning Kamp i = read(fd, bootarea + 512, BBSIZE - 512); 313d2fe97c7SPoul-Henning Kamp if (i != BBSIZE - 512) 314d2fe97c7SPoul-Henning Kamp err(1, "read error %s", xxboot); 315d2fe97c7SPoul-Henning Kamp return; 316d2fe97c7SPoul-Henning Kamp } 317d2fe97c7SPoul-Henning Kamp errx(1, "boot code %s is wrong size", xxboot); 318d2fe97c7SPoul-Henning Kamp } 319d2fe97c7SPoul-Henning Kamp 320d2fe97c7SPoul-Henning Kamp static int 321d2fe97c7SPoul-Henning Kamp writelabel(void) 3228fae3551SRodney W. Grimes { 323bc33ea1aSRuslan Ermilov uint64_t *p, sum; 3245daa806dSPoul-Henning Kamp int i, fd; 3255daa806dSPoul-Henning Kamp struct gctl_req *grq; 3265daa806dSPoul-Henning Kamp char const *errstr; 327d2fe97c7SPoul-Henning Kamp struct disklabel *lp = &lab; 328130cd73aSDoug Rabson 3293233afaeSJohn W. De Boskey if (disable_write) { 3303233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 331d2fe97c7SPoul-Henning Kamp display(stdout, NULL); 3323233afaeSJohn W. De Boskey return (0); 33380baf8ceSPoul-Henning Kamp } 33480baf8ceSPoul-Henning Kamp 3358fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 3368fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 3378fae3551SRodney W. Grimes lp->d_checksum = 0; 3388fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 339d2fe97c7SPoul-Henning Kamp if (installboot) 340d2fe97c7SPoul-Henning Kamp readboot(); 341d2fe97c7SPoul-Henning Kamp bsd_disklabel_le_enc(bootarea + labeloffset, lp); 342dfbc3f0cSPoul-Henning Kamp if (alphacksum) { 3435daa806dSPoul-Henning Kamp /* Generate the bootblock checksum for the SRM console. */ 344d2fe97c7SPoul-Henning Kamp for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++) 345130cd73aSDoug Rabson sum += p[i]; 346130cd73aSDoug Rabson p[63] = sum; 347bc33ea1aSRuslan Ermilov } 3485daa806dSPoul-Henning Kamp 3495daa806dSPoul-Henning Kamp fd = open(specname, O_RDWR); 3505daa806dSPoul-Henning Kamp if (fd < 0) { 3515daa806dSPoul-Henning Kamp grq = gctl_get_handle(GCTL_CONFIG_GEOM); 3525daa806dSPoul-Henning Kamp gctl_ro_param(grq, "class", -1, "BSD"); 3535daa806dSPoul-Henning Kamp gctl_ro_param(grq, "geom", -1, dkname); 3545daa806dSPoul-Henning Kamp gctl_ro_param(grq, "verb", -1, "write label"); 355d2fe97c7SPoul-Henning Kamp gctl_ro_param(grq, "label", 148+16*8, bootarea + labeloffset); 3565daa806dSPoul-Henning Kamp errstr = gctl_issue(grq); 357d2fe97c7SPoul-Henning Kamp if (errstr != NULL) { 358d2fe97c7SPoul-Henning Kamp warnx("%s", errstr); 359d2fe97c7SPoul-Henning Kamp gctl_free(grq); 360d2fe97c7SPoul-Henning Kamp return(1); 361d2fe97c7SPoul-Henning Kamp } 3625daa806dSPoul-Henning Kamp gctl_free(grq); 3635daa806dSPoul-Henning Kamp if (installboot) { 3645daa806dSPoul-Henning Kamp grq = gctl_get_handle(GCTL_CONFIG_GEOM); 3655daa806dSPoul-Henning Kamp gctl_ro_param(grq, "class", -1, "BSD"); 3665daa806dSPoul-Henning Kamp gctl_ro_param(grq, "geom", -1, dkname); 3675daa806dSPoul-Henning Kamp gctl_ro_param(grq, "verb", -1, "write bootcode"); 368d2fe97c7SPoul-Henning Kamp gctl_ro_param(grq, "bootcode", BBSIZE, bootarea); 3695daa806dSPoul-Henning Kamp errstr = gctl_issue(grq); 370d2fe97c7SPoul-Henning Kamp if (errstr != NULL) { 371d2fe97c7SPoul-Henning Kamp warnx("%s", errstr); 372d2fe97c7SPoul-Henning Kamp gctl_free(grq); 373d2fe97c7SPoul-Henning Kamp return (1); 374d2fe97c7SPoul-Henning Kamp } 3755daa806dSPoul-Henning Kamp gctl_free(grq); 3765daa806dSPoul-Henning Kamp } 3775daa806dSPoul-Henning Kamp } else { 378d2fe97c7SPoul-Henning Kamp if (write(fd, bootarea, bbsize) != bbsize) { 3795daa806dSPoul-Henning Kamp warn("write %s", specname); 3805daa806dSPoul-Henning Kamp close (fd); 3818fae3551SRodney W. Grimes return (1); 3828fae3551SRodney W. Grimes } 3835daa806dSPoul-Henning Kamp close (fd); 3845daa806dSPoul-Henning Kamp } 3858fae3551SRodney W. Grimes return (0); 3868fae3551SRodney W. Grimes } 3878fae3551SRodney W. Grimes 3888fae3551SRodney W. Grimes /* 3898fae3551SRodney W. Grimes * Fetch disklabel for disk. 3908fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 3918fae3551SRodney W. Grimes */ 392d2fe97c7SPoul-Henning Kamp static int 393d2fe97c7SPoul-Henning Kamp readlabel(int flag) 3948fae3551SRodney W. Grimes { 3955daa806dSPoul-Henning Kamp int f; 3965daa806dSPoul-Henning Kamp int error; 3978fae3551SRodney W. Grimes 3985daa806dSPoul-Henning Kamp f = open(specname, O_RDONLY); 3995daa806dSPoul-Henning Kamp if (f < 0) 4005daa806dSPoul-Henning Kamp err(1, specname); 401c80f9755SPoul-Henning Kamp (void)lseek(f, (off_t)0, SEEK_SET); 402d2fe97c7SPoul-Henning Kamp if (read(f, bootarea, BBSIZE) != BBSIZE) 4035daa806dSPoul-Henning Kamp err(4, "%s read", specname); 4045daa806dSPoul-Henning Kamp close (f); 405d2fe97c7SPoul-Henning Kamp error = bsd_disklabel_le_dec(bootarea + labeloffset, &lab, MAXPARTITIONS); 406d2fe97c7SPoul-Henning Kamp if (flag && error) 407d2fe97c7SPoul-Henning Kamp errx(1, "%s: no valid label found", specname); 408d2fe97c7SPoul-Henning Kamp return (error); 4098fae3551SRodney W. Grimes } 4108fae3551SRodney W. Grimes 4118fae3551SRodney W. Grimes 412d2fe97c7SPoul-Henning Kamp static void 413326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 4148fae3551SRodney W. Grimes { 415326c7cdaSWarner Losh int i, j; 416326c7cdaSWarner Losh const struct partition *pp; 4178fae3551SRodney W. Grimes 418d2fe97c7SPoul-Henning Kamp if (lp == NULL) 419d2fe97c7SPoul-Henning Kamp lp = &lab; 420d2fe97c7SPoul-Henning Kamp 4218fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 422484c7804SJulian Elischer if (lp->d_type < DKMAXTYPES) 4238fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 4248fae3551SRodney W. Grimes else 4252a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 42661de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 42761de51caSJoerg Wunsch lp->d_typename); 42861de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 42961de51caSJoerg Wunsch lp->d_packname); 4308fae3551SRodney W. Grimes fprintf(f, "flags:"); 4318fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 4328fae3551SRodney W. Grimes fprintf(f, " removeable"); 4338fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 4348fae3551SRodney W. Grimes fprintf(f, " ecc"); 4358fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 4368fae3551SRodney W. Grimes fprintf(f, " badsect"); 4378fae3551SRodney W. Grimes fprintf(f, "\n"); 4382a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 4392a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 4402a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 4412a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 4422a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 4432a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 4442a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 4452a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 4462a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 4472a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 4482a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 4492a1deaaaSBruce Evans (u_long)lp->d_headswitch); 45061de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 4512a1deaaaSBruce Evans (u_long)lp->d_trkseek); 4528fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 4538fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 4548fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 4558fae3551SRodney W. Grimes break; 4568fae3551SRodney W. Grimes if (i < 0) 4578fae3551SRodney W. Grimes i = 0; 4588fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 4592a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 4602a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 4618fae3551SRodney W. Grimes fprintf(f, 462ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 4638fae3551SRodney W. Grimes pp = lp->d_partitions; 4648fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 4658fae3551SRodney W. Grimes if (pp->p_size) { 4662a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 4672a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 468484c7804SJulian Elischer if (pp->p_fstype < FSMAXTYPES) 4698fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 4708fae3551SRodney W. Grimes else 4718fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 4728fae3551SRodney W. Grimes switch (pp->p_fstype) { 4738fae3551SRodney W. Grimes 4748fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 4752a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 4762a1deaaaSBruce Evans (u_long)pp->p_fsize, 4772a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 4788fae3551SRodney W. Grimes break; 4798fae3551SRodney W. Grimes 4808fae3551SRodney W. Grimes case FS_BSDFFS: 4812a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 4822a1deaaaSBruce Evans (u_long)pp->p_fsize, 4832a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 4848fae3551SRodney W. Grimes pp->p_cpg); 4858fae3551SRodney W. Grimes break; 4868fae3551SRodney W. Grimes 487ca4693edSJustin T. Gibbs case FS_BSDLFS: 4882a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 4892a1deaaaSBruce Evans (u_long)pp->p_fsize, 4902a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 491ca4693edSJustin T. Gibbs pp->p_cpg); 492ca4693edSJustin T. Gibbs break; 493ca4693edSJustin T. Gibbs 4948fae3551SRodney W. Grimes default: 4958fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 4968fae3551SRodney W. Grimes break; 4978fae3551SRodney W. Grimes } 498d2fe97c7SPoul-Henning Kamp fprintf(f, "\n"); 4998fae3551SRodney W. Grimes } 5008fae3551SRodney W. Grimes } 5018fae3551SRodney W. Grimes fflush(f); 5028fae3551SRodney W. Grimes } 5038fae3551SRodney W. Grimes 504d2fe97c7SPoul-Henning Kamp static int 505d2fe97c7SPoul-Henning Kamp edit(void) 5068fae3551SRodney W. Grimes { 507326c7cdaSWarner Losh int c, fd; 5088fae3551SRodney W. Grimes struct disklabel label; 509722ceb3fSWarner Losh FILE *fp; 5108fae3551SRodney W. Grimes 511722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 512722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 5136bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 5148fae3551SRodney W. Grimes return (1); 5158fae3551SRodney W. Grimes } 516d2fe97c7SPoul-Henning Kamp display(fp, NULL); 517722ceb3fSWarner Losh fclose(fp); 5188fae3551SRodney W. Grimes for (;;) { 5198fae3551SRodney W. Grimes if (!editit()) 5208fae3551SRodney W. Grimes break; 521722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 522722ceb3fSWarner Losh if (fp == NULL) { 5236bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 5248fae3551SRodney W. Grimes break; 5258fae3551SRodney W. Grimes } 5268fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 527d2fe97c7SPoul-Henning Kamp c = getasciilabel(fp, &label); 528722ceb3fSWarner Losh fclose(fp); 529d2fe97c7SPoul-Henning Kamp if (c) { 530d2fe97c7SPoul-Henning Kamp lab = label; 531d2fe97c7SPoul-Henning Kamp if (writelabel() == 0) { 5328fae3551SRodney W. Grimes (void) unlink(tmpfil); 5338fae3551SRodney W. Grimes return (0); 5348fae3551SRodney W. Grimes } 5358fae3551SRodney W. Grimes } 536d2fe97c7SPoul-Henning Kamp printf("re-edit the label? [y]: "); 537d2fe97c7SPoul-Henning Kamp fflush(stdout); 5388fae3551SRodney W. Grimes c = getchar(); 5398fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 5408fae3551SRodney W. Grimes while (getchar() != (int)'\n') 5418fae3551SRodney W. Grimes ; 5428fae3551SRodney W. Grimes if (c == (int)'n') 5438fae3551SRodney W. Grimes break; 5448fae3551SRodney W. Grimes } 5458fae3551SRodney W. Grimes (void) unlink(tmpfil); 5468fae3551SRodney W. Grimes return (1); 5478fae3551SRodney W. Grimes } 5488fae3551SRodney W. Grimes 549d2fe97c7SPoul-Henning Kamp static int 550326c7cdaSWarner Losh editit(void) 5518fae3551SRodney W. Grimes { 552326c7cdaSWarner Losh int pid, xpid; 553c8223965SMark Murray int locstat, omask; 554c8223965SMark Murray const char *ed; 5558fae3551SRodney W. Grimes 5568fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 5578fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 5588fae3551SRodney W. Grimes if (errno == EPROCLIM) { 5596bd343a9SPhilippe Charnier warnx("you have too many processes"); 5608fae3551SRodney W. Grimes return(0); 5618fae3551SRodney W. Grimes } 5628fae3551SRodney W. Grimes if (errno != EAGAIN) { 5636bd343a9SPhilippe Charnier warn("fork"); 5648fae3551SRodney W. Grimes return(0); 5658fae3551SRodney W. Grimes } 5668fae3551SRodney W. Grimes sleep(1); 5678fae3551SRodney W. Grimes } 5688fae3551SRodney W. Grimes if (pid == 0) { 5698fae3551SRodney W. Grimes sigsetmask(omask); 5708fae3551SRodney W. Grimes setgid(getgid()); 5718fae3551SRodney W. Grimes setuid(getuid()); 5728fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 5738fae3551SRodney W. Grimes ed = DEFEDITOR; 5747bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 5756bd343a9SPhilippe Charnier err(1, "%s", ed); 5768fae3551SRodney W. Grimes } 577c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 5788fae3551SRodney W. Grimes if (xpid == pid) 5798fae3551SRodney W. Grimes break; 5808fae3551SRodney W. Grimes sigsetmask(omask); 581c8223965SMark Murray return(!locstat); 5828fae3551SRodney W. Grimes } 5838fae3551SRodney W. Grimes 584d2fe97c7SPoul-Henning Kamp static char * 585326c7cdaSWarner Losh skip(char *cp) 5868fae3551SRodney W. Grimes { 5878fae3551SRodney W. Grimes 5888fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 5898fae3551SRodney W. Grimes cp++; 5908fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 591326c7cdaSWarner Losh return (NULL); 5928fae3551SRodney W. Grimes return (cp); 5938fae3551SRodney W. Grimes } 5948fae3551SRodney W. Grimes 595d2fe97c7SPoul-Henning Kamp static char * 596326c7cdaSWarner Losh word(char *cp) 5978fae3551SRodney W. Grimes { 598326c7cdaSWarner Losh char c; 5998fae3551SRodney W. Grimes 6008fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 6018fae3551SRodney W. Grimes cp++; 6028fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 6038fae3551SRodney W. Grimes *cp++ = '\0'; 6048fae3551SRodney W. Grimes if (c != '#') 6058fae3551SRodney W. Grimes return (skip(cp)); 6068fae3551SRodney W. Grimes } 607326c7cdaSWarner Losh return (NULL); 6088fae3551SRodney W. Grimes } 6098fae3551SRodney W. Grimes 6108fae3551SRodney W. Grimes /* 6118fae3551SRodney W. Grimes * Read an ascii label in from fd f, 6128fae3551SRodney W. Grimes * in the same format as that put out by display(), 6138fae3551SRodney W. Grimes * and fill in lp. 6148fae3551SRodney W. Grimes */ 615d2fe97c7SPoul-Henning Kamp static int 616326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 6178fae3551SRodney W. Grimes { 6186b0ff5f5SPoul-Henning Kamp char *cp; 6196b0ff5f5SPoul-Henning Kamp const char **cpp; 620484c7804SJulian Elischer u_int part; 6216b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 622484c7804SJulian Elischer u_long v; 623484c7804SJulian Elischer int lineno = 0, errors = 0; 624326c7cdaSWarner Losh int i; 6258fae3551SRodney W. Grimes 626b0459c58SDag-Erling Smørgrav bzero(&part_set, sizeof(part_set)); 627b0459c58SDag-Erling Smørgrav bzero(&part_size_type, sizeof(part_size_type)); 628b0459c58SDag-Erling Smørgrav bzero(&part_offset_type, sizeof(part_offset_type)); 6298fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 63077068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 6318fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 6328fae3551SRodney W. Grimes lineno++; 63361de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 6348fae3551SRodney W. Grimes *cp = '\0'; 6358fae3551SRodney W. Grimes cp = skip(line); 6368fae3551SRodney W. Grimes if (cp == NULL) 6378fae3551SRodney W. Grimes continue; 6388fae3551SRodney W. Grimes tp = index(cp, ':'); 6398fae3551SRodney W. Grimes if (tp == NULL) { 6408fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 6418fae3551SRodney W. Grimes errors++; 6428fae3551SRodney W. Grimes continue; 6438fae3551SRodney W. Grimes } 6448fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 645d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "type")) { 6468fae3551SRodney W. Grimes if (tp == NULL) 647c8223965SMark Murray tp = unknown; 6488fae3551SRodney W. Grimes cpp = dktypenames; 6498fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 650d2fe97c7SPoul-Henning Kamp if (*cpp && !strcmp(*cpp, tp)) { 6518fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 65209dbd070SIan Dowse break; 6538fae3551SRodney W. Grimes } 65409dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 65509dbd070SIan Dowse continue; 656484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 657484c7804SJulian Elischer if (v >= DKMAXTYPES) 658484c7804SJulian Elischer fprintf(stderr, "line %d:%s %lu\n", lineno, 6598fae3551SRodney W. Grimes "Warning, unknown disk type", v); 6608fae3551SRodney W. Grimes lp->d_type = v; 6618fae3551SRodney W. Grimes continue; 6628fae3551SRodney W. Grimes } 663d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "flags")) { 6648fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 6658fae3551SRodney W. Grimes tp = word(cp); 666d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "removeable")) 6678fae3551SRodney W. Grimes v |= D_REMOVABLE; 668d2fe97c7SPoul-Henning Kamp else if (!strcmp(cp, "ecc")) 6698fae3551SRodney W. Grimes v |= D_ECC; 670d2fe97c7SPoul-Henning Kamp else if (!strcmp(cp, "badsect")) 6718fae3551SRodney W. Grimes v |= D_BADSECT; 6728fae3551SRodney W. Grimes else { 6738fae3551SRodney W. Grimes fprintf(stderr, 6748fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 6758fae3551SRodney W. Grimes lineno, cp); 6768fae3551SRodney W. Grimes errors++; 6778fae3551SRodney W. Grimes } 6788fae3551SRodney W. Grimes } 6798fae3551SRodney W. Grimes lp->d_flags = v; 6808fae3551SRodney W. Grimes continue; 6818fae3551SRodney W. Grimes } 682d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "drivedata")) { 6838fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 684484c7804SJulian Elischer lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 6858fae3551SRodney W. Grimes tp = word(cp); 6868fae3551SRodney W. Grimes } 6878fae3551SRodney W. Grimes continue; 6888fae3551SRodney W. Grimes } 689484c7804SJulian Elischer if (sscanf(cp, "%lu partitions", &v) == 1) { 690484c7804SJulian Elischer if (v == 0 || v > MAXPARTITIONS) { 6918fae3551SRodney W. Grimes fprintf(stderr, 6928fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 6938fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 6948fae3551SRodney W. Grimes errors++; 6958fae3551SRodney W. Grimes } else 6968fae3551SRodney W. Grimes lp->d_npartitions = v; 6978fae3551SRodney W. Grimes continue; 6988fae3551SRodney W. Grimes } 6998fae3551SRodney W. Grimes if (tp == NULL) 700c8223965SMark Murray tp = blank; 701d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "disk")) { 7028fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 7038fae3551SRodney W. Grimes continue; 7048fae3551SRodney W. Grimes } 705d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "label")) { 7068fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 7078fae3551SRodney W. Grimes continue; 7088fae3551SRodney W. Grimes } 709d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "bytes/sector")) { 710484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 711484c7804SJulian Elischer if (v == 0 || (v % DEV_BSIZE) != 0) { 7128fae3551SRodney W. Grimes fprintf(stderr, 7138fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 7148fae3551SRodney W. Grimes lineno, tp); 7158fae3551SRodney W. Grimes errors++; 7168fae3551SRodney W. Grimes } else 7178fae3551SRodney W. Grimes lp->d_secsize = v; 7188fae3551SRodney W. Grimes continue; 7198fae3551SRodney W. Grimes } 720d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "sectors/track")) { 721484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 722484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL) 723484c7804SJulian Elischer if (v == 0 || v > 0xffffffff) { 724484c7804SJulian Elischer #else 725484c7804SJulian Elischer if (v == 0) { 726484c7804SJulian Elischer #endif 7278fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7288fae3551SRodney W. Grimes lineno, tp, cp); 7298fae3551SRodney W. Grimes errors++; 7308fae3551SRodney W. Grimes } else 7318fae3551SRodney W. Grimes lp->d_nsectors = v; 7328fae3551SRodney W. Grimes continue; 7338fae3551SRodney W. Grimes } 734d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "sectors/cylinder")) { 735484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 736484c7804SJulian Elischer if (v == 0) { 7378fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7388fae3551SRodney W. Grimes lineno, tp, cp); 7398fae3551SRodney W. Grimes errors++; 7408fae3551SRodney W. Grimes } else 7418fae3551SRodney W. Grimes lp->d_secpercyl = v; 7428fae3551SRodney W. Grimes continue; 7438fae3551SRodney W. Grimes } 744d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "tracks/cylinder")) { 745484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 746484c7804SJulian Elischer if (v == 0) { 7478fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7488fae3551SRodney W. Grimes lineno, tp, cp); 7498fae3551SRodney W. Grimes errors++; 7508fae3551SRodney W. Grimes } else 7518fae3551SRodney W. Grimes lp->d_ntracks = v; 7528fae3551SRodney W. Grimes continue; 7538fae3551SRodney W. Grimes } 754d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "cylinders")) { 755484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 756484c7804SJulian Elischer if (v == 0) { 7578fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7588fae3551SRodney W. Grimes lineno, tp, cp); 7598fae3551SRodney W. Grimes errors++; 7608fae3551SRodney W. Grimes } else 7618fae3551SRodney W. Grimes lp->d_ncylinders = v; 7628fae3551SRodney W. Grimes continue; 7638fae3551SRodney W. Grimes } 764d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "sectors/unit")) { 765484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 766484c7804SJulian Elischer if (v == 0) { 767f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 768f75dd518SBruce Evans lineno, tp, cp); 769f75dd518SBruce Evans errors++; 770f75dd518SBruce Evans } else 771f75dd518SBruce Evans lp->d_secperunit = v; 772f75dd518SBruce Evans continue; 773f75dd518SBruce Evans } 774d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "rpm")) { 775484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 776484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 7778fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7788fae3551SRodney W. Grimes lineno, tp, cp); 7798fae3551SRodney W. Grimes errors++; 7808fae3551SRodney W. Grimes } else 7818fae3551SRodney W. Grimes lp->d_rpm = v; 7828fae3551SRodney W. Grimes continue; 7838fae3551SRodney W. Grimes } 784d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "interleave")) { 785484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 786484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 7878fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7888fae3551SRodney W. Grimes lineno, tp, cp); 7898fae3551SRodney W. Grimes errors++; 7908fae3551SRodney W. Grimes } else 7918fae3551SRodney W. Grimes lp->d_interleave = v; 7928fae3551SRodney W. Grimes continue; 7938fae3551SRodney W. Grimes } 794d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "trackskew")) { 795484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 796484c7804SJulian Elischer if (v > USHRT_MAX) { 7978fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 7988fae3551SRodney W. Grimes lineno, tp, cp); 7998fae3551SRodney W. Grimes errors++; 8008fae3551SRodney W. Grimes } else 8018fae3551SRodney W. Grimes lp->d_trackskew = v; 8028fae3551SRodney W. Grimes continue; 8038fae3551SRodney W. Grimes } 804d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "cylinderskew")) { 805484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 806484c7804SJulian Elischer if (v > USHRT_MAX) { 8078fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 8088fae3551SRodney W. Grimes lineno, tp, cp); 8098fae3551SRodney W. Grimes errors++; 8108fae3551SRodney W. Grimes } else 8118fae3551SRodney W. Grimes lp->d_cylskew = v; 8128fae3551SRodney W. Grimes continue; 8138fae3551SRodney W. Grimes } 814d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "headswitch")) { 815484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 8168fae3551SRodney W. Grimes lp->d_headswitch = v; 8178fae3551SRodney W. Grimes continue; 8188fae3551SRodney W. Grimes } 819d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "track-to-track seek")) { 820484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 8218fae3551SRodney W. Grimes lp->d_trkseek = v; 8228fae3551SRodney W. Grimes continue; 8238fae3551SRodney W. Grimes } 8243233afaeSJohn W. De Boskey /* the ':' was removed above */ 82567b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 82667b46708SIan Dowse fprintf(stderr, 82767b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 82867b46708SIan Dowse cp); 82967b46708SIan Dowse errors++; 83067b46708SIan Dowse continue; 83167b46708SIan Dowse } 83267b46708SIan Dowse 83367b46708SIan Dowse /* Process a partition specification line. */ 8343233afaeSJohn W. De Boskey part = *cp - 'a'; 8353233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 8368fae3551SRodney W. Grimes fprintf(stderr, 8373233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 8383233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 8398fae3551SRodney W. Grimes errors++; 8408fae3551SRodney W. Grimes continue; 8418fae3551SRodney W. Grimes } 8423233afaeSJohn W. De Boskey part_set[part] = 1; 84367b46708SIan Dowse 84467b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 84567b46708SIan Dowse errors++; 84667b46708SIan Dowse break; 84767b46708SIan Dowse } 84867b46708SIan Dowse } 84967b46708SIan Dowse errors += checklabel(lp); 85067b46708SIan Dowse return (errors == 0); 85167b46708SIan Dowse } 85267b46708SIan Dowse 85367b46708SIan Dowse #define NXTNUM(n) do { \ 85413e0abcbSPaul Traina if (tp == NULL) { \ 85513e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 85667b46708SIan Dowse return (1); \ 85713e0abcbSPaul Traina } else { \ 8588fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 859484c7804SJulian Elischer (n) = strtoul(cp, NULL, 10); \ 86013e0abcbSPaul Traina } \ 86167b46708SIan Dowse } while (0) 86267b46708SIan Dowse 8633233afaeSJohn W. De Boskey /* retain 1 character following number */ 86467b46708SIan Dowse #define NXTWORD(w,n) do { \ 8653233afaeSJohn W. De Boskey if (tp == NULL) { \ 8663233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 86767b46708SIan Dowse return (1); \ 8683233afaeSJohn W. De Boskey } else { \ 8693233afaeSJohn W. De Boskey char *tmp; \ 8703233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 871484c7804SJulian Elischer (n) = strtoul(cp, &tmp, 10); \ 8723233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 8733233afaeSJohn W. De Boskey } \ 87467b46708SIan Dowse } while (0) 87567b46708SIan Dowse 87667b46708SIan Dowse /* 87767b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 87867b46708SIan Dowse * Return 0 on success, 1 on failure. 87967b46708SIan Dowse */ 880d2fe97c7SPoul-Henning Kamp static int 88167b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 88267b46708SIan Dowse { 88367b46708SIan Dowse struct partition *pp; 88467b46708SIan Dowse char *cp; 88567b46708SIan Dowse const char **cpp; 886484c7804SJulian Elischer u_long v; 88767b46708SIan Dowse 88867b46708SIan Dowse pp = &lp->d_partitions[part]; 88967b46708SIan Dowse cp = NULL; 89067b46708SIan Dowse 8913233afaeSJohn W. De Boskey v = 0; 8923233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 893484c7804SJulian Elischer if (v == 0 && part_size_type[part] != '*') { 894484c7804SJulian Elischer fprintf(stderr, 895484c7804SJulian Elischer "line %d: %s: bad partition size\n", lineno, cp); 89667b46708SIan Dowse return (1); 89767b46708SIan Dowse } 8988fae3551SRodney W. Grimes pp->p_size = v; 8993233afaeSJohn W. De Boskey 9003233afaeSJohn W. De Boskey v = 0; 9013233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 902484c7804SJulian Elischer if (v == 0 && part_offset_type[part] != '*' && 903484c7804SJulian Elischer part_offset_type[part] != '\0') { 904484c7804SJulian Elischer fprintf(stderr, 905484c7804SJulian Elischer "line %d: %s: bad partition offset\n", lineno, cp); 90667b46708SIan Dowse return (1); 90767b46708SIan Dowse } 9088fae3551SRodney W. Grimes pp->p_offset = v; 90929f3f095SYaroslav Tykhiy if (tp == NULL) { 91029f3f095SYaroslav Tykhiy fprintf(stderr, "line %d: missing file system type\n", lineno); 91129f3f095SYaroslav Tykhiy return (1); 91229f3f095SYaroslav Tykhiy } 9138fae3551SRodney W. Grimes cp = tp, tp = word(cp); 91467b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 915d2fe97c7SPoul-Henning Kamp if (*cpp && !strcmp(*cpp, cp)) 91667b46708SIan Dowse break; 91767b46708SIan Dowse if (*cpp != NULL) { 91867b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 91967b46708SIan Dowse } else { 9208fae3551SRodney W. Grimes if (isdigit(*cp)) 921484c7804SJulian Elischer v = strtoul(cp, NULL, 10); 9228fae3551SRodney W. Grimes else 9238fae3551SRodney W. Grimes v = FSMAXTYPES; 924484c7804SJulian Elischer if (v >= FSMAXTYPES) { 9253233afaeSJohn W. De Boskey fprintf(stderr, 92667b46708SIan Dowse "line %d: Warning, unknown file system type %s\n", 9273233afaeSJohn W. De Boskey lineno, cp); 9288fae3551SRodney W. Grimes v = FS_UNUSED; 9298fae3551SRodney W. Grimes } 9308fae3551SRodney W. Grimes pp->p_fstype = v; 93167b46708SIan Dowse } 93267b46708SIan Dowse 9338fae3551SRodney W. Grimes switch (pp->p_fstype) { 9343233afaeSJohn W. De Boskey case FS_UNUSED: 9353233afaeSJohn W. De Boskey /* 9363233afaeSJohn W. De Boskey * allow us to accept defaults for 9373233afaeSJohn W. De Boskey * fsize/frag/cpg 9383233afaeSJohn W. De Boskey */ 9393233afaeSJohn W. De Boskey if (tp) { 9408fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 9418fae3551SRodney W. Grimes if (pp->p_fsize == 0) 9428fae3551SRodney W. Grimes break; 9438fae3551SRodney W. Grimes NXTNUM(v); 9448fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 9453233afaeSJohn W. De Boskey } 9463233afaeSJohn W. De Boskey /* else default to 0's */ 9478fae3551SRodney W. Grimes break; 9488fae3551SRodney W. Grimes 9493233afaeSJohn W. De Boskey /* These happen to be the same */ 9508fae3551SRodney W. Grimes case FS_BSDFFS: 951ca4693edSJustin T. Gibbs case FS_BSDLFS: 9523233afaeSJohn W. De Boskey if (tp) { 953ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 954ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 955ca4693edSJustin T. Gibbs break; 956ca4693edSJustin T. Gibbs NXTNUM(v); 957ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 958ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 9593233afaeSJohn W. De Boskey } else { 9603233afaeSJohn W. De Boskey /* 96167b46708SIan Dowse * FIX! poor attempt at adaptive 9623233afaeSJohn W. De Boskey */ 9633233afaeSJohn W. De Boskey /* 1 GB */ 96467b46708SIan Dowse if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 96567b46708SIan Dowse /* 96667b46708SIan Dowse * FIX! These are too low, but are traditional 96767b46708SIan Dowse */ 968f1a7b7eeSIan Dowse pp->p_fsize = DEFAULT_NEWFS_FRAG; 969f1a7b7eeSIan Dowse pp->p_frag = DEFAULT_NEWFS_BLOCK / 970f1a7b7eeSIan Dowse DEFAULT_NEWFS_FRAG; 9713233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 9723233afaeSJohn W. De Boskey } else { 973f1a7b7eeSIan Dowse pp->p_fsize = BIG_NEWFS_FRAG; 974f1a7b7eeSIan Dowse pp->p_frag = BIG_NEWFS_BLOCK / 975f1a7b7eeSIan Dowse BIG_NEWFS_FRAG; 9763233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 9773233afaeSJohn W. De Boskey } 9783233afaeSJohn W. De Boskey } 9798fae3551SRodney W. Grimes default: 9808fae3551SRodney W. Grimes break; 9818fae3551SRodney W. Grimes } 98267b46708SIan Dowse return (0); 9838fae3551SRodney W. Grimes } 9848fae3551SRodney W. Grimes 9858fae3551SRodney W. Grimes /* 9868fae3551SRodney W. Grimes * Check disklabel for errors and fill in 9878fae3551SRodney W. Grimes * derived fields according to supplied values. 9888fae3551SRodney W. Grimes */ 989d2fe97c7SPoul-Henning Kamp static int 990326c7cdaSWarner Losh checklabel(struct disklabel *lp) 9918fae3551SRodney W. Grimes { 992326c7cdaSWarner Losh struct partition *pp; 9938fae3551SRodney W. Grimes int i, errors = 0; 9948fae3551SRodney W. Grimes char part; 995484c7804SJulian Elischer u_long total_size, total_percent, current_offset; 9963233afaeSJohn W. De Boskey int seen_default_offset; 9973233afaeSJohn W. De Boskey int hog_part; 9983233afaeSJohn W. De Boskey int j; 9993233afaeSJohn W. De Boskey struct partition *pp2; 10008fae3551SRodney W. Grimes 1001d2fe97c7SPoul-Henning Kamp if (lp == NULL) 1002d2fe97c7SPoul-Henning Kamp lp = &lab; 1003d2fe97c7SPoul-Henning Kamp 10048fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 10052a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 10068fae3551SRodney W. Grimes return (1); 10078fae3551SRodney W. Grimes } 10088fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 10092a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 10108fae3551SRodney W. Grimes return (1); 10118fae3551SRodney W. Grimes } 10128fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 10132a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 10148fae3551SRodney W. Grimes return (1); 10158fae3551SRodney W. Grimes } 10168fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 10172a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 10188fae3551SRodney W. Grimes errors++; 10198fae3551SRodney W. Grimes } 10208fae3551SRodney W. Grimes if (lp->d_rpm == 0) 10212a1deaaaSBruce Evans Warning("revolutions/minute 0"); 10228fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 10238fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 10248fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 10258fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 10268fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 10272a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 10288fae3551SRodney W. Grimes errors++; 10298fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 10308fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 10318fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 10322a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 10332a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 10343233afaeSJohn W. De Boskey 10353233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 10363233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 10373233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 10383233afaeSJohn W. De Boskey hog_part = -1; 10393233afaeSJohn W. De Boskey /* find all fixed partitions */ 10403233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 10413233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 10423233afaeSJohn W. De Boskey if (part_set[i]) { 10433233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 10446b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 10453233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 10463233afaeSJohn W. De Boskey } else { 10473233afaeSJohn W. De Boskey if (hog_part != -1) 10483233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 10493233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 10503233afaeSJohn W. De Boskey else 10513233afaeSJohn W. De Boskey hog_part = i; 10523233afaeSJohn W. De Boskey } 10533233afaeSJohn W. De Boskey } else { 10548d3105e8SWarner Losh off_t size; 10553233afaeSJohn W. De Boskey 10563233afaeSJohn W. De Boskey size = pp->p_size; 10573233afaeSJohn W. De Boskey switch (part_size_type[i]) { 10583233afaeSJohn W. De Boskey case '%': 10593233afaeSJohn W. De Boskey total_percent += size; 10603233afaeSJohn W. De Boskey break; 10613233afaeSJohn W. De Boskey case 'k': 10623233afaeSJohn W. De Boskey case 'K': 10638d3105e8SWarner Losh size *= 1024ULL; 10643233afaeSJohn W. De Boskey break; 10653233afaeSJohn W. De Boskey case 'm': 10663233afaeSJohn W. De Boskey case 'M': 10678d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 10683233afaeSJohn W. De Boskey break; 10693233afaeSJohn W. De Boskey case 'g': 10703233afaeSJohn W. De Boskey case 'G': 10718d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 10723233afaeSJohn W. De Boskey break; 10733233afaeSJohn W. De Boskey case '\0': 10743233afaeSJohn W. De Boskey break; 10753233afaeSJohn W. De Boskey default: 10763233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 10773233afaeSJohn W. De Boskey break; 10783233afaeSJohn W. De Boskey } 10793233afaeSJohn W. De Boskey /* don't count %'s yet */ 10803233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 10813233afaeSJohn W. De Boskey /* 10823233afaeSJohn W. De Boskey * for all not in sectors, convert to 10833233afaeSJohn W. De Boskey * sectors 10843233afaeSJohn W. De Boskey */ 10853233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 10863233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 10873233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 10883233afaeSJohn W. De Boskey i + 'a'); 10893233afaeSJohn W. De Boskey size /= lp->d_secsize; 10903233afaeSJohn W. De Boskey pp->p_size = size; 10913233afaeSJohn W. De Boskey } 10923233afaeSJohn W. De Boskey /* else already in sectors */ 10936b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 10943233afaeSJohn W. De Boskey total_size += size; 10953233afaeSJohn W. De Boskey } 10963233afaeSJohn W. De Boskey } 10973233afaeSJohn W. De Boskey } 10983233afaeSJohn W. De Boskey } 10993233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 11003233afaeSJohn W. De Boskey if (total_percent != 0) { 11013233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 11023233afaeSJohn W. De Boskey if (total_percent > 100) { 11036b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 11043233afaeSJohn W. De Boskey total_percent); 11053233afaeSJohn W. De Boskey errors++; 11063233afaeSJohn W. De Boskey } 11073233afaeSJohn W. De Boskey 11083233afaeSJohn W. De Boskey if (free_space > 0) { 11093233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 11103233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 11113233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 11123233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 11133233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 11143233afaeSJohn W. De Boskey total_size += pp->p_size; 11153233afaeSJohn W. De Boskey 11163233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 11173233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 11183233afaeSJohn W. De Boskey } 11193233afaeSJohn W. De Boskey } 11203233afaeSJohn W. De Boskey } else { 11213233afaeSJohn W. De Boskey fprintf(stderr, 11226b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 11233233afaeSJohn W. De Boskey free_space); 11243233afaeSJohn W. De Boskey errors++; 11253233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 11263233afaeSJohn W. De Boskey } 11273233afaeSJohn W. De Boskey } 11283233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 11293233afaeSJohn W. De Boskey if (hog_part != -1) { 11303233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 11313233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 11323233afaeSJohn W. De Boskey } 11333233afaeSJohn W. De Boskey 11343233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 11353233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 11363233afaeSJohn W. De Boskey seen_default_offset = 0; 11373233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 11383233afaeSJohn W. De Boskey part = 'a' + i; 11393233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 11403233afaeSJohn W. De Boskey if (part_set[i]) { 11413233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 11426b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 11433233afaeSJohn W. De Boskey pp->p_offset = 0; 11443233afaeSJohn W. De Boskey } else { 11453233afaeSJohn W. De Boskey pp->p_offset = current_offset; 11463233afaeSJohn W. De Boskey seen_default_offset = 1; 11473233afaeSJohn W. De Boskey } 11483233afaeSJohn W. De Boskey } else { 11493233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 11503233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 1151f2f63257SGreg Lehey seen_default_offset && i != RAW_PART && 1152f2f63257SGreg Lehey pp->p_fstype != FS_VINUM) { 11533233afaeSJohn W. De Boskey fprintf(stderr, 11546b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 11556b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 11563233afaeSJohn W. De Boskey fprintf(stderr, 11573233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 11583233afaeSJohn W. De Boskey errors++; 11593233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 11606b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 11613233afaeSJohn W. De Boskey /* 11623233afaeSJohn W. De Boskey * this may give unneeded warnings if 11633233afaeSJohn W. De Boskey * partitions are out-of-order 11643233afaeSJohn W. De Boskey */ 11653233afaeSJohn W. De Boskey Warning( 11663233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 11676b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 11683233afaeSJohn W. De Boskey } 11693233afaeSJohn W. De Boskey } 11706b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 11713233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 11723233afaeSJohn W. De Boskey } 11733233afaeSJohn W. De Boskey } 11743233afaeSJohn W. De Boskey 11758fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 11768fae3551SRodney W. Grimes part = 'a' + i; 11778fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 11788fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 11792a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 11802a1deaaaSBruce Evans part, (u_long)pp->p_offset); 11818fae3551SRodney W. Grimes #ifdef notdef 11828fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 11838fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 11848fae3551SRodney W. Grimes part); 11858fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 11868fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 11878fae3551SRodney W. Grimes part); 11888fae3551SRodney W. Grimes #endif 11898fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 11908fae3551SRodney W. Grimes fprintf(stderr, 11918fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 11928fae3551SRodney W. Grimes errors++; 11938fae3551SRodney W. Grimes } 11948fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 11958fae3551SRodney W. Grimes fprintf(stderr, 11968fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 11978fae3551SRodney W. Grimes part); 11988fae3551SRodney W. Grimes errors++; 11998fae3551SRodney W. Grimes } 12006b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) 12013233afaeSJohn W. De Boskey { 12023233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 12033233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 12043233afaeSJohn W. De Boskey if (pp->p_offset != 0) 12053233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 12063233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 12073233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 12083233afaeSJohn W. De Boskey 12093233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 12103233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 12113233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 12123233afaeSJohn W. De Boskey "standard system utilities",part); 12133233afaeSJohn W. De Boskey } 12143233afaeSJohn W. De Boskey } 12153233afaeSJohn W. De Boskey 12163233afaeSJohn W. De Boskey /* check for overlaps */ 12173233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 12183233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 12193233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 1220f2f63257SGreg Lehey if (j != RAW_PART && i != RAW_PART && 1221f2f63257SGreg Lehey pp->p_fstype != FS_VINUM && 1222f2f63257SGreg Lehey pp2->p_fstype != FS_VINUM && 1223f2f63257SGreg Lehey part_set[i] && part_set[j]) { 12243233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 12253233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 12263233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 12273233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 12283233afaeSJohn W. De Boskey j + 'a', i + 'a'); 12293233afaeSJohn W. De Boskey errors++; 12303233afaeSJohn W. De Boskey } 12313233afaeSJohn W. De Boskey } 12323233afaeSJohn W. De Boskey } 12338fae3551SRodney W. Grimes } 12348fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 12358fae3551SRodney W. Grimes part = 'a' + i; 12368fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 12378fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 12382a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 12392a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 12408fae3551SRodney W. Grimes } 12418fae3551SRodney W. Grimes return (errors); 12428fae3551SRodney W. Grimes } 12438fae3551SRodney W. Grimes 12448fae3551SRodney W. Grimes /* 1245425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1246425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1247425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1248425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1249425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1250425bed3aSJoerg Wunsch * 1251425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1252425bed3aSJoerg Wunsch */ 1253d2fe97c7SPoul-Henning Kamp static struct disklabel * 1254425bed3aSJoerg Wunsch getvirginlabel(void) 1255425bed3aSJoerg Wunsch { 1256c8223965SMark Murray static struct disklabel loclab; 1257b9d05a16SPoul-Henning Kamp struct partition *dp; 1258425bed3aSJoerg Wunsch int f; 1259b9d05a16SPoul-Henning Kamp u_int secsize, u; 1260b9d05a16SPoul-Henning Kamp off_t mediasize; 1261425bed3aSJoerg Wunsch 1262d2fe97c7SPoul-Henning Kamp if ((f = open(specname, O_RDONLY)) == -1) { 1263d2fe97c7SPoul-Henning Kamp warn("cannot open %s", specname); 126443be698cSBruce Evans return (NULL); 1265425bed3aSJoerg Wunsch } 1266ff7d5162SJordan K. Hubbard 1267b9d05a16SPoul-Henning Kamp /* New world order */ 1268b9d05a16SPoul-Henning Kamp if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1269b9d05a16SPoul-Henning Kamp (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1270425bed3aSJoerg Wunsch close (f); 127143be698cSBruce Evans return (NULL); 1272425bed3aSJoerg Wunsch } 1273b9d05a16SPoul-Henning Kamp memset(&loclab, 0, sizeof loclab); 1274b9d05a16SPoul-Henning Kamp loclab.d_magic = DISKMAGIC; 1275b9d05a16SPoul-Henning Kamp loclab.d_magic2 = DISKMAGIC; 1276b9d05a16SPoul-Henning Kamp loclab.d_secsize = secsize; 1277b9d05a16SPoul-Henning Kamp loclab.d_secperunit = mediasize / secsize; 1278b9d05a16SPoul-Henning Kamp 1279b9d05a16SPoul-Henning Kamp /* 1280b9d05a16SPoul-Henning Kamp * Nobody in these enligthened days uses the CHS geometry for 1281b9d05a16SPoul-Henning Kamp * anything, but nontheless try to get it right. If we fail 1282b9d05a16SPoul-Henning Kamp * to get any good ideas from the device, construct something 1283b9d05a16SPoul-Henning Kamp * which is IBM-PC friendly. 1284b9d05a16SPoul-Henning Kamp */ 1285b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1286b9d05a16SPoul-Henning Kamp loclab.d_nsectors = u; 1287b9d05a16SPoul-Henning Kamp else 1288b9d05a16SPoul-Henning Kamp loclab.d_nsectors = 63; 1289b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1290b9d05a16SPoul-Henning Kamp loclab.d_ntracks = u; 1291b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*1*1024) 1292b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 1; 1293b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*16*1024) 1294b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 16; 1295b9d05a16SPoul-Henning Kamp else 1296b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 255; 1297b9d05a16SPoul-Henning Kamp loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1298b9d05a16SPoul-Henning Kamp loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1299b9d05a16SPoul-Henning Kamp loclab.d_npartitions = MAXPARTITIONS; 1300b9d05a16SPoul-Henning Kamp 1301b9d05a16SPoul-Henning Kamp /* Various (unneeded) compat stuff */ 1302b9d05a16SPoul-Henning Kamp loclab.d_rpm = 3600; 1303b9d05a16SPoul-Henning Kamp loclab.d_bbsize = BBSIZE; 1304b9d05a16SPoul-Henning Kamp loclab.d_interleave = 1;; 1305b9d05a16SPoul-Henning Kamp strncpy(loclab.d_typename, "amnesiac", 1306b9d05a16SPoul-Henning Kamp sizeof(loclab.d_typename)); 1307b9d05a16SPoul-Henning Kamp 1308b9d05a16SPoul-Henning Kamp dp = &loclab.d_partitions[RAW_PART]; 1309b9d05a16SPoul-Henning Kamp dp->p_size = loclab.d_secperunit; 1310b9d05a16SPoul-Henning Kamp loclab.d_checksum = dkcksum(&loclab); 1311425bed3aSJoerg Wunsch close (f); 1312c8223965SMark Murray return (&loclab); 1313425bed3aSJoerg Wunsch } 1314425bed3aSJoerg Wunsch 13158fae3551SRodney W. Grimes 13168fae3551SRodney W. Grimes /*VARARGS1*/ 1317d2fe97c7SPoul-Henning Kamp static void 1318326c7cdaSWarner Losh Warning(const char *fmt, ...) 13198fae3551SRodney W. Grimes { 132061de51caSJoerg Wunsch va_list ap; 13218fae3551SRodney W. Grimes 13228fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 132361de51caSJoerg Wunsch va_start(ap, fmt); 132461de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 13258fae3551SRodney W. Grimes fprintf(stderr, "\n"); 132661de51caSJoerg Wunsch va_end(ap); 13278fae3551SRodney W. Grimes } 13288fae3551SRodney W. Grimes 1329d2fe97c7SPoul-Henning Kamp static void 1330326c7cdaSWarner Losh usage(void) 13318fae3551SRodney W. Grimes { 1332bc33ea1aSRuslan Ermilov 1333bc33ea1aSRuslan Ermilov fprintf(stderr, 1334bc33ea1aSRuslan Ermilov "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 13355daa806dSPoul-Henning Kamp "usage: bsdlabel disk", 1336bef2080aSPhilippe Charnier "\t\t(to read label)", 1337d2fe97c7SPoul-Henning Kamp " bsdlabel -w [-n] [-m machine] disk [type]", 1338bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 13395daa806dSPoul-Henning Kamp " bsdlabel -e [-n] [-m machine] disk", 1340bef2080aSPhilippe Charnier "\t\t(to edit label)", 13415daa806dSPoul-Henning Kamp " bsdlabel -R [-n] [-m machine] disk protofile", 1342bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 13432c60b668SPoul-Henning Kamp " bsdlabel -B [-b boot] [-m machine] disk", 1344bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 1345d2fe97c7SPoul-Henning Kamp " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]", 1346bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 13472c60b668SPoul-Henning Kamp " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile", 134880baf8ceSPoul-Henning Kamp "\t\t(to restore label and install boot program)" 134980baf8ceSPoul-Henning Kamp ); 13508fae3551SRodney W. Grimes exit(1); 13518fae3551SRodney W. Grimes } 1352