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> 608fae3551SRodney W. Grimes #include <sys/file.h> 618fae3551SRodney W. Grimes #include <sys/stat.h> 6261de51caSJoerg Wunsch #include <sys/wait.h> 63b9d05a16SPoul-Henning Kamp #include <sys/disk.h> 648fae3551SRodney W. Grimes #define DKTYPENAMES 65b35e6950SBosko Milekic #define FSTYPENAMES 668fae3551SRodney W. Grimes #include <sys/disklabel.h> 67bc33ea1aSRuslan Ermilov 683bb24c35SPoul-Henning Kamp #include <sys/diskmbr.h> 69bc33ea1aSRuslan Ermilov #if (DOSPARTOFF != 446 || NDOSPART != 4 || DOSPTYP_386BSD != 0xa5) 70bc33ea1aSRuslan Ermilov #error <sys/diskmbr.h> has changed 71bc33ea1aSRuslan Ermilov #else 72bc33ea1aSRuslan Ermilov #define I386_DOSPARTOFF 446 73bc33ea1aSRuslan Ermilov #define I386_NDOSPART 4 74bc33ea1aSRuslan Ermilov #define I386_DOSPTYP_386BSD 0xa5 756d8c8fabSYoshihiro Takahashi #endif 76bc33ea1aSRuslan Ermilov #undef DOSPARTOFF 77bc33ea1aSRuslan Ermilov #undef NDOSPART 78bc33ea1aSRuslan Ermilov #undef DOSPTYP_386BSD 79bc33ea1aSRuslan Ermilov 80bc33ea1aSRuslan Ermilov #include <sys/diskpc98.h> 81bc33ea1aSRuslan Ermilov #if (DOSPARTOFF != 0 || NDOSPART != 16 || DOSPTYP_386BSD != 0x94) 82bc33ea1aSRuslan Ermilov #error <sys/diskpc98.h> has changed 83bc33ea1aSRuslan Ermilov #else 84bc33ea1aSRuslan Ermilov #define PC98_DOSPARTOFF 0 85bc33ea1aSRuslan Ermilov #define PC98_NDOSPART 16 86bc33ea1aSRuslan Ermilov #define PC98_DOSPTYP_386BSD 0x94 87bc33ea1aSRuslan Ermilov #endif 88bc33ea1aSRuslan Ermilov #undef DOSPARTOFF 89bc33ea1aSRuslan Ermilov #undef NDOSPART 90bc33ea1aSRuslan Ermilov #undef DOSPTYP_386BSD 91bc33ea1aSRuslan Ermilov 92bc33ea1aSRuslan Ermilov #define IS_PC98 (arch->mach == MACH_PC98) 93bc33ea1aSRuslan Ermilov #define DOSPARTOFF (IS_PC98 ? PC98_DOSPARTOFF : I386_DOSPARTOFF) 94bc33ea1aSRuslan Ermilov #define NDOSPART (IS_PC98 ? PC98_NDOSPART : I386_NDOSPART) 95bc33ea1aSRuslan Ermilov #define DOSPTYP_386BSD (IS_PC98 ? PC98_DOSPTYP_386BSD : I386_DOSPTYP_386BSD) 96c8223965SMark Murray 978fae3551SRodney W. Grimes #include <unistd.h> 988fae3551SRodney W. Grimes #include <string.h> 998fae3551SRodney W. Grimes #include <stdio.h> 10061de51caSJoerg Wunsch #include <stdlib.h> 10161de51caSJoerg Wunsch #include <signal.h> 10261de51caSJoerg Wunsch #include <stdarg.h> 1038fae3551SRodney W. Grimes #include <ctype.h> 104bef2080aSPhilippe Charnier #include <err.h> 10537736675SWarner Losh #include <errno.h> 106c8223965SMark Murray 1078fae3551SRodney W. Grimes #include "pathnames.h" 1088fae3551SRodney W. Grimes 1098fae3551SRodney W. Grimes /* 1108fae3551SRodney W. Grimes * Disklabel: read and write disklabels. 1118fae3551SRodney W. Grimes * The label is usually placed on one of the first sectors of the disk. 1128fae3551SRodney W. Grimes * Many machines also place a bootstrap in the same area, 1138fae3551SRodney W. Grimes * in which case the label is embedded in the bootstrap. 1148fae3551SRodney W. Grimes * The bootstrap source must leave space at the proper offset 1158fae3551SRodney W. Grimes * for the label on such machines. 1168fae3551SRodney W. Grimes */ 1178fae3551SRodney W. Grimes 1188fae3551SRodney W. Grimes #ifndef BBSIZE 1198fae3551SRodney W. Grimes #define BBSIZE 8192 /* size of boot area, with label */ 1208fae3551SRodney W. Grimes #endif 1218fae3551SRodney W. Grimes 1223233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 1233233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 1243233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 1253233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 1263233afaeSJohn W. De Boskey 1273233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 128f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG 2048U 1293233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 1303233afaeSJohn W. De Boskey 131326c7cdaSWarner Losh void makelabel(const char *, const char *, struct disklabel *); 132bc33ea1aSRuslan Ermilov int writelabel(int, void *, struct disklabel *); 133326c7cdaSWarner Losh void l_perror(const char *); 134326c7cdaSWarner Losh struct disklabel *readlabel(int); 135bc33ea1aSRuslan Ermilov struct disklabel *makebootarea(void *, struct disklabel *, int); 136326c7cdaSWarner Losh void display(FILE *, const struct disklabel *); 137326c7cdaSWarner Losh int edit(struct disklabel *, int); 138326c7cdaSWarner Losh int editit(void); 139326c7cdaSWarner Losh char *skip(char *); 140326c7cdaSWarner Losh char *word(char *); 141326c7cdaSWarner Losh int getasciilabel(FILE *, struct disklabel *); 14267b46708SIan Dowse int getasciipartspec(char *, struct disklabel *, int, int); 143326c7cdaSWarner Losh int checklabel(struct disklabel *); 144326c7cdaSWarner Losh void Warning(const char *, ...) __printflike(1, 2); 145326c7cdaSWarner Losh void usage(void); 146326c7cdaSWarner Losh struct disklabel *getvirginlabel(void); 14761de51caSJoerg Wunsch 1488fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1498fae3551SRodney W. Grimes #define streq(a,b) (strcmp(a,b) == 0) 1508fae3551SRodney W. Grimes 1518fae3551SRodney W. Grimes char *dkname; 1528fae3551SRodney W. Grimes char *specname; 153aaae3130SKris Kennaway char tmpfil[] = PATH_TMPFILE; 1548fae3551SRodney W. Grimes 1558fae3551SRodney W. Grimes char namebuf[BBSIZE], *np = namebuf; 1568fae3551SRodney W. Grimes struct disklabel lab; 157bc33ea1aSRuslan Ermilov int64_t bootarea[BBSIZE / 8]; 158c8223965SMark Murray char blank[] = ""; 159c8223965SMark Murray char unknown[] = "unknown"; 1608fae3551SRodney W. Grimes 1613233afaeSJohn W. De Boskey #define MAX_PART ('z') 1623233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 1633233afaeSJohn W. De Boskey char part_size_type[MAX_NUM_PARTS]; 1643233afaeSJohn W. De Boskey char part_offset_type[MAX_NUM_PARTS]; 1653233afaeSJohn W. De Boskey int part_set[MAX_NUM_PARTS]; 1663233afaeSJohn W. De Boskey 1678fae3551SRodney W. Grimes int installboot; /* non-zero if we should install a boot program */ 1688fae3551SRodney W. Grimes char *xxboot; /* primary boot */ 1698fae3551SRodney W. Grimes char boot0[MAXPATHLEN]; 1708fae3551SRodney W. Grimes 1718fae3551SRodney W. Grimes enum { 172f080d33bSPoul-Henning Kamp UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT 1738fae3551SRodney W. Grimes } op = UNSPEC; 1748fae3551SRodney W. Grimes 175bc33ea1aSRuslan Ermilov enum { ARCH_I386, ARCH_ALPHA, ARCH_IA64 }; 176bc33ea1aSRuslan Ermilov 177bc33ea1aSRuslan Ermilov enum { MACH_I386, MACH_PC98 }; 178bc33ea1aSRuslan Ermilov 179bc33ea1aSRuslan Ermilov struct { 180bc33ea1aSRuslan Ermilov const char *name; 181bc33ea1aSRuslan Ermilov int arch; 182bc33ea1aSRuslan Ermilov int mach; 183bc33ea1aSRuslan Ermilov off_t label_sector; 184bc33ea1aSRuslan Ermilov off_t label_offset; 185bc33ea1aSRuslan Ermilov } arches[] = { 186bc33ea1aSRuslan Ermilov { "i386", ARCH_I386, MACH_I386, 1, 0 }, 187bc33ea1aSRuslan Ermilov { "pc98", ARCH_I386, MACH_PC98, 1, 0 }, 188bc33ea1aSRuslan Ermilov { "alpha", ARCH_ALPHA, ARCH_ALPHA, 0, 64 }, 189bc33ea1aSRuslan Ermilov { "ia64", ARCH_IA64, ARCH_IA64, 1, 0 }, 190bc33ea1aSRuslan Ermilov }, *arch; 191bc33ea1aSRuslan Ermilov #define NARCHES (int)(sizeof(arches) / sizeof(*arches)) 192bc33ea1aSRuslan Ermilov 1938fae3551SRodney W. Grimes int rflag; 1943233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1958fae3551SRodney W. Grimes 19661de51caSJoerg Wunsch int 197326c7cdaSWarner Losh main(int argc, char *argv[]) 1988fae3551SRodney W. Grimes { 199c1fdb43fSWarner Losh struct disklabel *lp; 2008fae3551SRodney W. Grimes FILE *t; 201bc33ea1aSRuslan Ermilov int ch, f = 0, error = 0, i; 2028fae3551SRodney W. Grimes char *name = 0; 2038fae3551SRodney W. Grimes 204bc33ea1aSRuslan Ermilov while ((ch = getopt(argc, argv, "Bb:em:nRrs:w")) != -1) 2058fae3551SRodney W. Grimes switch (ch) { 2068fae3551SRodney W. Grimes case 'B': 2078fae3551SRodney W. Grimes ++installboot; 2088fae3551SRodney W. Grimes break; 2098fae3551SRodney W. Grimes case 'b': 2108fae3551SRodney W. Grimes xxboot = optarg; 2118fae3551SRodney W. Grimes break; 212bc33ea1aSRuslan Ermilov case 'm': 213bc33ea1aSRuslan Ermilov for (i = 0; i < NARCHES && 214bc33ea1aSRuslan Ermilov strcmp(arches[i].name, optarg) != 0; 215bc33ea1aSRuslan Ermilov i++); 216bc33ea1aSRuslan Ermilov if (i == NARCHES) 217bc33ea1aSRuslan Ermilov errx(1, "%s: unknown architecture", 218bc33ea1aSRuslan Ermilov optarg); 219bc33ea1aSRuslan Ermilov arch = &arches[i]; 220bc33ea1aSRuslan Ermilov break; 2213233afaeSJohn W. De Boskey case 'n': 2223233afaeSJohn W. De Boskey disable_write = 1; 2233233afaeSJohn W. De Boskey break; 2248fae3551SRodney W. Grimes case 'R': 2258fae3551SRodney W. Grimes if (op != UNSPEC) 2268fae3551SRodney W. Grimes usage(); 2278fae3551SRodney W. Grimes op = RESTORE; 2288fae3551SRodney W. Grimes break; 2298fae3551SRodney W. Grimes case 'e': 2308fae3551SRodney W. Grimes if (op != UNSPEC) 2318fae3551SRodney W. Grimes usage(); 2328fae3551SRodney W. Grimes op = EDIT; 2338fae3551SRodney W. Grimes break; 2348fae3551SRodney W. Grimes case 'r': 2358fae3551SRodney W. Grimes ++rflag; 2368fae3551SRodney W. Grimes break; 2378fae3551SRodney W. Grimes case 'w': 2388fae3551SRodney W. Grimes if (op != UNSPEC) 2398fae3551SRodney W. Grimes usage(); 2408fae3551SRodney W. Grimes op = WRITE; 2418fae3551SRodney W. Grimes break; 2428fae3551SRodney W. Grimes case '?': 2438fae3551SRodney W. Grimes default: 2448fae3551SRodney W. Grimes usage(); 2458fae3551SRodney W. Grimes } 2468fae3551SRodney W. Grimes argc -= optind; 2478fae3551SRodney W. Grimes argv += optind; 2488fae3551SRodney W. Grimes if (installboot) { 2498fae3551SRodney W. Grimes rflag++; 2508fae3551SRodney W. Grimes if (op == UNSPEC) 2518fae3551SRodney W. Grimes op = WRITEBOOT; 2528fae3551SRodney W. Grimes } else { 2538fae3551SRodney W. Grimes if (op == UNSPEC) 2548fae3551SRodney W. Grimes op = READ; 2558d48318bSPoul-Henning Kamp xxboot = 0; 2568fae3551SRodney W. Grimes } 2578fae3551SRodney W. Grimes if (argc < 1) 2588fae3551SRodney W. Grimes usage(); 2598fae3551SRodney W. Grimes 260bc33ea1aSRuslan Ermilov if (arch == NULL) { 261bc33ea1aSRuslan Ermilov for (i = 0; i < NARCHES; i++) 262bc33ea1aSRuslan Ermilov if (strcmp(arches[i].name, 263bc33ea1aSRuslan Ermilov #if defined(__i386__) 264bc33ea1aSRuslan Ermilov #ifdef PC98 265bc33ea1aSRuslan Ermilov "pc98" 266bc33ea1aSRuslan Ermilov #else 267bc33ea1aSRuslan Ermilov "i386" 268bc33ea1aSRuslan Ermilov #endif 269bc33ea1aSRuslan Ermilov #elif defined(__alpha__) 270bc33ea1aSRuslan Ermilov "alpha" 271bc33ea1aSRuslan Ermilov #elif defined(__ia64__) 272bc33ea1aSRuslan Ermilov "ia64" 273bc33ea1aSRuslan Ermilov #else 274bc33ea1aSRuslan Ermilov "unknown" 275bc33ea1aSRuslan Ermilov #endif 276bc33ea1aSRuslan Ermilov ) == 0) { 277bc33ea1aSRuslan Ermilov arch = &arches[i]; 278bc33ea1aSRuslan Ermilov break; 279bc33ea1aSRuslan Ermilov } 280bc33ea1aSRuslan Ermilov if (i == NARCHES) 281bc33ea1aSRuslan Ermilov errx(1, "unsupported architecture"); 282bc33ea1aSRuslan Ermilov } 283bc33ea1aSRuslan Ermilov 2848fae3551SRodney W. Grimes dkname = argv[0]; 2858fae3551SRodney W. Grimes if (dkname[0] != '/') { 28609c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2878fae3551SRodney W. Grimes specname = np; 2888fae3551SRodney W. Grimes np += strlen(specname) + 1; 2898fae3551SRodney W. Grimes } else 2908fae3551SRodney W. Grimes specname = dkname; 2918fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2928fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 29343c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 2948fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 2958fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2968fae3551SRodney W. Grimes } 29774c041acSPoul-Henning Kamp if (f < 0 && errno == EBUSY) { 29874c041acSPoul-Henning Kamp /* lets try to get by with ioctls */ 29974c041acSPoul-Henning Kamp f = open(specname, O_RDONLY); 30074c041acSPoul-Henning Kamp } 3018fae3551SRodney W. Grimes if (f < 0) 302bef2080aSPhilippe Charnier err(4, "%s", specname); 3038fae3551SRodney W. Grimes 3048fae3551SRodney W. Grimes switch(op) { 3058fae3551SRodney W. Grimes 3063b3038a6SBill Fumerola case UNSPEC: 3073b3038a6SBill Fumerola break; 3083b3038a6SBill Fumerola 3098fae3551SRodney W. Grimes case EDIT: 3108fae3551SRodney W. Grimes if (argc != 1) 3118fae3551SRodney W. Grimes usage(); 3128fae3551SRodney W. Grimes lp = readlabel(f); 3138fae3551SRodney W. Grimes error = edit(lp, f); 3148fae3551SRodney W. Grimes break; 3158fae3551SRodney W. Grimes 3168fae3551SRodney W. Grimes case READ: 3178fae3551SRodney W. Grimes if (argc != 1) 3188fae3551SRodney W. Grimes usage(); 3198fae3551SRodney W. Grimes lp = readlabel(f); 3208fae3551SRodney W. Grimes display(stdout, lp); 3218fae3551SRodney W. Grimes error = checklabel(lp); 3228fae3551SRodney W. Grimes break; 3238fae3551SRodney W. Grimes 3248fae3551SRodney W. Grimes case RESTORE: 3258fae3551SRodney W. Grimes if (argc != 2) 3268fae3551SRodney W. Grimes usage(); 3278fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 328bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 3296cabb348SBruce Evans if (!getasciilabel(t, &lab)) 3306cabb348SBruce Evans exit(1); 3316cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 3326cabb348SBruce Evans *lp = lab; 3338fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3348fae3551SRodney W. Grimes break; 3358fae3551SRodney W. Grimes 3368fae3551SRodney W. Grimes case WRITE: 3378fae3551SRodney W. Grimes if (argc == 3) { 3388fae3551SRodney W. Grimes name = argv[2]; 3398fae3551SRodney W. Grimes argc--; 3408fae3551SRodney W. Grimes } 3418fae3551SRodney W. Grimes if (argc != 2) 3428fae3551SRodney W. Grimes usage(); 3438fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3448fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3458fae3551SRodney W. Grimes *lp = lab; 3468fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3478fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3488fae3551SRodney W. Grimes break; 3498fae3551SRodney W. Grimes 3508fae3551SRodney W. Grimes case WRITEBOOT: 3518fae3551SRodney W. Grimes { 3528fae3551SRodney W. Grimes struct disklabel tlab; 3538fae3551SRodney W. Grimes 3548fae3551SRodney W. Grimes lp = readlabel(f); 3558fae3551SRodney W. Grimes tlab = *lp; 3568fae3551SRodney W. Grimes if (argc == 2) 3578fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3588fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3598fae3551SRodney W. Grimes *lp = tlab; 3608fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3618fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3628fae3551SRodney W. Grimes break; 3638fae3551SRodney W. Grimes } 3648fae3551SRodney W. Grimes } 3658fae3551SRodney W. Grimes exit(error); 3668fae3551SRodney W. Grimes } 3678fae3551SRodney W. Grimes 3688fae3551SRodney W. Grimes /* 369ef9ab0b3SRuslan Ermilov * Construct a prototype disklabel from /etc/disktab. 3708fae3551SRodney W. Grimes */ 37161de51caSJoerg Wunsch void 372326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp) 3738fae3551SRodney W. Grimes { 374326c7cdaSWarner Losh struct disklabel *dp; 375425bed3aSJoerg Wunsch 376425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 377425bed3aSJoerg Wunsch dp = getvirginlabel(); 378425bed3aSJoerg Wunsch else 3798fae3551SRodney W. Grimes dp = getdiskbyname(type); 3806bd343a9SPhilippe Charnier if (dp == NULL) 3816bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3828fae3551SRodney W. Grimes *lp = *dp; 3838fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 3848fae3551SRodney W. Grimes if (name) 3858fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 3868fae3551SRodney W. Grimes } 3878fae3551SRodney W. Grimes 38861de51caSJoerg Wunsch int 389bc33ea1aSRuslan Ermilov writelabel(int f, void *boot, struct disklabel *lp) 3908fae3551SRodney W. Grimes { 391bc33ea1aSRuslan Ermilov uint64_t *p, sum; 392130cd73aSDoug Rabson int i; 393130cd73aSDoug Rabson 3943233afaeSJohn W. De Boskey if (disable_write) { 3953233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 3963233afaeSJohn W. De Boskey display(stdout, lp); 3973233afaeSJohn W. De Boskey return (0); 39880baf8ceSPoul-Henning Kamp } 39980baf8ceSPoul-Henning Kamp 4008fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 4018fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 4028fae3551SRodney W. Grimes lp->d_checksum = 0; 4038fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 40480baf8ceSPoul-Henning Kamp if (!rflag) { 40580baf8ceSPoul-Henning Kamp if (ioctl(f, DIOCWDINFO, lp) < 0) { 40680baf8ceSPoul-Henning Kamp l_perror("ioctl DIOCWDINFO"); 40780baf8ceSPoul-Henning Kamp return (1); 40880baf8ceSPoul-Henning Kamp } 40980baf8ceSPoul-Henning Kamp return (0); 41080baf8ceSPoul-Henning Kamp } 41180baf8ceSPoul-Henning Kamp 4128fae3551SRodney W. Grimes /* 4138fae3551SRodney W. Grimes * First set the kernel disk label, 4148fae3551SRodney W. Grimes * then write a label to the raw disk. 4158fae3551SRodney W. Grimes * If the SDINFO ioctl fails because it is unimplemented, 4168fae3551SRodney W. Grimes * keep going; otherwise, the kernel consistency checks 4178fae3551SRodney W. Grimes * may prevent us from changing the current (in-core) 4188fae3551SRodney W. Grimes * label. 4198fae3551SRodney W. Grimes */ 4208fae3551SRodney W. Grimes if (ioctl(f, DIOCSDINFO, lp) < 0 && 4218fae3551SRodney W. Grimes errno != ENODEV && errno != ENOTTY) { 4228fae3551SRodney W. Grimes l_perror("ioctl DIOCSDINFO"); 4238fae3551SRodney W. Grimes return (1); 4248fae3551SRodney W. Grimes } 4258fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 426130cd73aSDoug Rabson 427bc33ea1aSRuslan Ermilov if (arch->arch == ARCH_ALPHA) { 428cc18e4ccSBruce Evans /* 429cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 430cc18e4ccSBruce Evans */ 431bc33ea1aSRuslan Ermilov for (p = (uint64_t *)boot, i = 0, sum = 0; i < 63; i++) 432130cd73aSDoug Rabson sum += p[i]; 433130cd73aSDoug Rabson p[63] = sum; 434bc33ea1aSRuslan Ermilov } 43574c041acSPoul-Henning Kamp if (ioctl(f, DIOCBSDBB, &boot) == 0) 43674c041acSPoul-Henning Kamp return (0); 437c8223965SMark Murray if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) { 4386bd343a9SPhilippe Charnier warn("write"); 4398fae3551SRodney W. Grimes return (1); 4408fae3551SRodney W. Grimes } 4418fae3551SRodney W. Grimes return (0); 4428fae3551SRodney W. Grimes } 4438fae3551SRodney W. Grimes 44461de51caSJoerg Wunsch void 445326c7cdaSWarner Losh l_perror(const char *s) 4468fae3551SRodney W. Grimes { 4476bd343a9SPhilippe Charnier switch (errno) { 4488fae3551SRodney W. Grimes 4498fae3551SRodney W. Grimes case ESRCH: 4503121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 4510b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 4528fae3551SRodney W. Grimes break; 4538fae3551SRodney W. Grimes 4548fae3551SRodney W. Grimes case EINVAL: 4553121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 4563121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 4578fae3551SRodney W. Grimes break; 4588fae3551SRodney W. Grimes 4598fae3551SRodney W. Grimes case EBUSY: 4606bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 4618fae3551SRodney W. Grimes break; 4628fae3551SRodney W. Grimes 4638fae3551SRodney W. Grimes case EXDEV: 4647de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 4657de06420SBruce Evans s, 'a' + RAW_PART); 4668fae3551SRodney W. Grimes break; 4678fae3551SRodney W. Grimes 4688fae3551SRodney W. Grimes default: 4696bd343a9SPhilippe Charnier warn((char *)NULL); 4708fae3551SRodney W. Grimes break; 4718fae3551SRodney W. Grimes } 4728fae3551SRodney W. Grimes } 4738fae3551SRodney W. Grimes 4748fae3551SRodney W. Grimes /* 4758fae3551SRodney W. Grimes * Fetch disklabel for disk. 4768fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 4778fae3551SRodney W. Grimes */ 4788fae3551SRodney W. Grimes struct disklabel * 479326c7cdaSWarner Losh readlabel(int f) 4808fae3551SRodney W. Grimes { 481326c7cdaSWarner Losh struct disklabel *lp; 4828fae3551SRodney W. Grimes 4838fae3551SRodney W. Grimes if (rflag) { 4848fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 485bef2080aSPhilippe Charnier err(4, "%s", specname); 4868fae3551SRodney W. Grimes for (lp = (struct disklabel *)bootarea; 487bc33ea1aSRuslan Ermilov lp <= (struct disklabel *) 488bc33ea1aSRuslan Ermilov ((char *)bootarea + BBSIZE - sizeof(*lp)); 4898fae3551SRodney W. Grimes lp = (struct disklabel *)((char *)lp + 16)) 4908fae3551SRodney W. Grimes if (lp->d_magic == DISKMAGIC && 4918fae3551SRodney W. Grimes lp->d_magic2 == DISKMAGIC) 4928fae3551SRodney W. Grimes break; 493bc33ea1aSRuslan Ermilov if (lp > (struct disklabel *) 494bc33ea1aSRuslan Ermilov ((char *)bootarea + BBSIZE - sizeof(*lp)) || 4958fae3551SRodney W. Grimes lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 4963121d4cbSPhilippe Charnier dkcksum(lp) != 0) 4973121d4cbSPhilippe Charnier errx(1, 4983121d4cbSPhilippe Charnier "bad pack magic number (label is damaged, or pack is unlabeled)"); 4998fae3551SRodney W. Grimes } else { 5008fae3551SRodney W. Grimes lp = &lab; 5018fae3551SRodney W. Grimes if (ioctl(f, DIOCGDINFO, lp) < 0) 502bef2080aSPhilippe Charnier err(4, "ioctl DIOCGDINFO"); 5038fae3551SRodney W. Grimes } 5048fae3551SRodney W. Grimes return (lp); 5058fae3551SRodney W. Grimes } 5068fae3551SRodney W. Grimes 5078fae3551SRodney W. Grimes /* 5088fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 5098fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 5108fae3551SRodney W. Grimes */ 5118fae3551SRodney W. Grimes struct disklabel * 512bc33ea1aSRuslan Ermilov makebootarea(void *boot, struct disklabel *dp, int f) 5138fae3551SRodney W. Grimes { 51421c729c2SBruce Evans struct disklabel *lp; 515326c7cdaSWarner Losh char *p; 5168fae3551SRodney W. Grimes int b; 5178fae3551SRodney W. Grimes char *dkbasename; 5188fae3551SRodney W. Grimes struct stat sb; 519bc33ea1aSRuslan Ermilov uint64_t *bootinfo; 520cc18e4ccSBruce Evans int n; 521d70e4e53SJoerg Wunsch char *tmpbuf; 5228d48318bSPoul-Henning Kamp int i, found, dps; 5238fae3551SRodney W. Grimes 5248fae3551SRodney W. Grimes /* XXX */ 5258fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 5268fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 5278fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 5288fae3551SRodney W. Grimes } 5298fae3551SRodney W. Grimes lp = (struct disklabel *) 530bc33ea1aSRuslan Ermilov ((char *)boot + (arch->label_sector * dp->d_secsize) + 531bc33ea1aSRuslan Ermilov arch->label_offset); 5328fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5338fae3551SRodney W. Grimes /* 5348fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 5358fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 5368fae3551SRodney W. Grimes * clobber the existing boot. 5378fae3551SRodney W. Grimes */ 5388fae3551SRodney W. Grimes if (!installboot) { 5398fae3551SRodney W. Grimes if (rflag) { 5408fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 541bef2080aSPhilippe Charnier err(4, "%s", specname); 5428fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5438fae3551SRodney W. Grimes } 5448fae3551SRodney W. Grimes return (lp); 5458fae3551SRodney W. Grimes } 5468fae3551SRodney W. Grimes /* 5478fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 5488fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 5498fae3551SRodney W. Grimes */ 5508d48318bSPoul-Henning Kamp if (!xxboot) { 5518fae3551SRodney W. Grimes dkbasename = np; 5528fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 5538fae3551SRodney W. Grimes p = dkname; 5548fae3551SRodney W. Grimes else 5558fae3551SRodney W. Grimes p++; 5568fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 5578fae3551SRodney W. Grimes *np++ = *p++; 5588fae3551SRodney W. Grimes *np++ = '\0'; 5598fae3551SRodney W. Grimes 5608fae3551SRodney W. Grimes if (!xxboot) { 5618d48318bSPoul-Henning Kamp (void)sprintf(boot0, "%s/boot", _PATH_BOOTDIR); 562efba76d7SJordan K. Hubbard xxboot = boot0; 5638fae3551SRodney W. Grimes } 5648fae3551SRodney W. Grimes } 5658fae3551SRodney W. Grimes 5668fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 5678fae3551SRodney W. Grimes if (b < 0) 568bef2080aSPhilippe Charnier err(4, "%s", xxboot); 5698d48318bSPoul-Henning Kamp if (fstat(b, &sb) != 0) 5708d48318bSPoul-Henning Kamp err(4, "%s", xxboot); 571bc33ea1aSRuslan Ermilov if (arch->arch == ARCH_I386) { 5728d48318bSPoul-Henning Kamp if (sb.st_size > BBSIZE) 5738d48318bSPoul-Henning Kamp errx(4, "%s too large", xxboot); 574d70e4e53SJoerg Wunsch /* 575d70e4e53SJoerg Wunsch * XXX Botch alert. 5768d48318bSPoul-Henning Kamp * The i386/PC98 has the so-called fdisk table embedded into the 577d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 578d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 579d70e4e53SJoerg Wunsch * the xxboot provides a template.) 580d70e4e53SJoerg Wunsch */ 581d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 582bef2080aSPhilippe Charnier err(4, "%s", xxboot); 583d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 5848d48318bSPoul-Henning Kamp 5858d48318bSPoul-Henning Kamp if (read(b, boot, BBSIZE) < 0) 586bef2080aSPhilippe Charnier err(4, "%s", xxboot); 5878d48318bSPoul-Henning Kamp 5888d48318bSPoul-Henning Kamp /* XXX: rely on some very precise overlaps in definitions */ 589bc33ea1aSRuslan Ermilov dps = IS_PC98 ? sizeof(struct pc98_partition) : 590bc33ea1aSRuslan Ermilov sizeof(struct dos_partition); 591404a379eSYoshihiro Takahashi for (i = DOSPARTOFF, found = 0; 5928d48318bSPoul-Henning Kamp !found && i < (int)(DOSPARTOFF + NDOSPART * dps); 593404a379eSYoshihiro Takahashi i++) 594404a379eSYoshihiro Takahashi found = tmpbuf[i] != 0; 595404a379eSYoshihiro Takahashi if (found) 596bc33ea1aSRuslan Ermilov memcpy((void *)&((char *)boot)[DOSPARTOFF], 597404a379eSYoshihiro Takahashi (void *)&tmpbuf[DOSPARTOFF], 5988d48318bSPoul-Henning Kamp NDOSPART * dps); 599d70e4e53SJoerg Wunsch free(tmpbuf); 600bc33ea1aSRuslan Ermilov } 6018d48318bSPoul-Henning Kamp 602bc33ea1aSRuslan Ermilov if (arch->arch == ARCH_ALPHA) { 6038d48318bSPoul-Henning Kamp if (sb.st_size > BBSIZE - dp->d_secsize) 6048d48318bSPoul-Henning Kamp errx(4, "%s too large", xxboot); 605130cd73aSDoug Rabson /* 606130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 607130cd73aSDoug Rabson * second sector of the boot area. The first sector 608130cd73aSDoug Rabson * contains the label and must be edited to contain the 609130cd73aSDoug Rabson * size and location of the primary bootstrap. 610130cd73aSDoug Rabson */ 611bc33ea1aSRuslan Ermilov n = read(b, (char *)boot + dp->d_secsize, 612bc33ea1aSRuslan Ermilov BBSIZE - dp->d_secsize); 613130cd73aSDoug Rabson if (n < 0) 614130cd73aSDoug Rabson err(4, "%s", xxboot); 615bc33ea1aSRuslan Ermilov bootinfo = (uint64_t *)((char *)boot + 480); 61621c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 61721c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 61821c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 619bc33ea1aSRuslan Ermilov } 6208d48318bSPoul-Henning Kamp 6218fae3551SRodney W. Grimes (void)close(b); 6228fae3551SRodney W. Grimes /* 6238fae3551SRodney W. Grimes * Make sure no part of the bootstrap is written in the area 6248fae3551SRodney W. Grimes * reserved for the label. 6258fae3551SRodney W. Grimes */ 6268fae3551SRodney W. Grimes for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 6276bd343a9SPhilippe Charnier if (*p) 6286bd343a9SPhilippe Charnier errx(2, "bootstrap doesn't leave room for disk label"); 6298fae3551SRodney W. Grimes return (lp); 6308fae3551SRodney W. Grimes } 6318fae3551SRodney W. Grimes 63261de51caSJoerg Wunsch void 633326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 6348fae3551SRodney W. Grimes { 635326c7cdaSWarner Losh int i, j; 636326c7cdaSWarner Losh const struct partition *pp; 6378fae3551SRodney W. Grimes 6388fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 639484c7804SJulian Elischer if (lp->d_type < DKMAXTYPES) 6408fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 6418fae3551SRodney W. Grimes else 6422a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 64361de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 64461de51caSJoerg Wunsch lp->d_typename); 64561de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 64661de51caSJoerg Wunsch lp->d_packname); 6478fae3551SRodney W. Grimes fprintf(f, "flags:"); 6488fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 6498fae3551SRodney W. Grimes fprintf(f, " removeable"); 6508fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 6518fae3551SRodney W. Grimes fprintf(f, " ecc"); 6528fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 6538fae3551SRodney W. Grimes fprintf(f, " badsect"); 6548fae3551SRodney W. Grimes fprintf(f, "\n"); 6552a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 6562a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 6572a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 6582a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 6592a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 6602a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 6612a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 6622a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 6632a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 6642a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 6652a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 6662a1deaaaSBruce Evans (u_long)lp->d_headswitch); 66761de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 6682a1deaaaSBruce Evans (u_long)lp->d_trkseek); 6698fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 6708fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 6718fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 6728fae3551SRodney W. Grimes break; 6738fae3551SRodney W. Grimes if (i < 0) 6748fae3551SRodney W. Grimes i = 0; 6758fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 6762a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 6772a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 6788fae3551SRodney W. Grimes fprintf(f, 679ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 6808fae3551SRodney W. Grimes pp = lp->d_partitions; 6818fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 6828fae3551SRodney W. Grimes if (pp->p_size) { 6832a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 6842a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 685484c7804SJulian Elischer if (pp->p_fstype < FSMAXTYPES) 6868fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 6878fae3551SRodney W. Grimes else 6888fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 6898fae3551SRodney W. Grimes switch (pp->p_fstype) { 6908fae3551SRodney W. Grimes 6918fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 6922a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 6932a1deaaaSBruce Evans (u_long)pp->p_fsize, 6942a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 6958fae3551SRodney W. Grimes break; 6968fae3551SRodney W. Grimes 6978fae3551SRodney W. Grimes case FS_BSDFFS: 6982a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 6992a1deaaaSBruce Evans (u_long)pp->p_fsize, 7002a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 7018fae3551SRodney W. Grimes pp->p_cpg); 7028fae3551SRodney W. Grimes break; 7038fae3551SRodney W. Grimes 704ca4693edSJustin T. Gibbs case FS_BSDLFS: 7052a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 7062a1deaaaSBruce Evans (u_long)pp->p_fsize, 7072a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 708ca4693edSJustin T. Gibbs pp->p_cpg); 709ca4693edSJustin T. Gibbs break; 710ca4693edSJustin T. Gibbs 7118fae3551SRodney W. Grimes default: 7128fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 7138fae3551SRodney W. Grimes break; 7148fae3551SRodney W. Grimes } 7152a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 7162a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 7178fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 7188fae3551SRodney W. Grimes putc('*', f); 7198fae3551SRodney W. Grimes else 7208fae3551SRodney W. Grimes putc(' ', f); 7212a1deaaaSBruce Evans fprintf(f, "- %lu", 7222a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 7232a1deaaaSBruce Evans lp->d_secpercyl - 1) / 7242a1deaaaSBruce Evans lp->d_secpercyl - 1)); 7258fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 7268fae3551SRodney W. Grimes putc('*', f); 7278fae3551SRodney W. Grimes fprintf(f, ")\n"); 7288fae3551SRodney W. Grimes } 7298fae3551SRodney W. Grimes } 7308fae3551SRodney W. Grimes fflush(f); 7318fae3551SRodney W. Grimes } 7328fae3551SRodney W. Grimes 73361de51caSJoerg Wunsch int 734326c7cdaSWarner Losh edit(struct disklabel *lp, int f) 7358fae3551SRodney W. Grimes { 736326c7cdaSWarner Losh int c, fd; 7378fae3551SRodney W. Grimes struct disklabel label; 738722ceb3fSWarner Losh FILE *fp; 7398fae3551SRodney W. Grimes 740722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 741722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 7426bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 7438fae3551SRodney W. Grimes return (1); 7448fae3551SRodney W. Grimes } 745722ceb3fSWarner Losh display(fp, lp); 746722ceb3fSWarner Losh fclose(fp); 7478fae3551SRodney W. Grimes for (;;) { 7488fae3551SRodney W. Grimes if (!editit()) 7498fae3551SRodney W. Grimes break; 750722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 751722ceb3fSWarner Losh if (fp == NULL) { 7526bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 7538fae3551SRodney W. Grimes break; 7548fae3551SRodney W. Grimes } 7558fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 756722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 7578fae3551SRodney W. Grimes *lp = label; 7588fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 759722ceb3fSWarner Losh fclose(fp); 7608fae3551SRodney W. Grimes (void) unlink(tmpfil); 7618fae3551SRodney W. Grimes return (0); 7628fae3551SRodney W. Grimes } 7638fae3551SRodney W. Grimes } 764722ceb3fSWarner Losh fclose(fp); 7658fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 7668fae3551SRodney W. Grimes c = getchar(); 7678fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 7688fae3551SRodney W. Grimes while (getchar() != (int)'\n') 7698fae3551SRodney W. Grimes ; 7708fae3551SRodney W. Grimes if (c == (int)'n') 7718fae3551SRodney W. Grimes break; 7728fae3551SRodney W. Grimes } 7738fae3551SRodney W. Grimes (void) unlink(tmpfil); 7748fae3551SRodney W. Grimes return (1); 7758fae3551SRodney W. Grimes } 7768fae3551SRodney W. Grimes 77761de51caSJoerg Wunsch int 778326c7cdaSWarner Losh editit(void) 7798fae3551SRodney W. Grimes { 780326c7cdaSWarner Losh int pid, xpid; 781c8223965SMark Murray int locstat, omask; 782c8223965SMark Murray const char *ed; 7838fae3551SRodney W. Grimes 7848fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 7858fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 7868fae3551SRodney W. Grimes if (errno == EPROCLIM) { 7876bd343a9SPhilippe Charnier warnx("you have too many processes"); 7888fae3551SRodney W. Grimes return(0); 7898fae3551SRodney W. Grimes } 7908fae3551SRodney W. Grimes if (errno != EAGAIN) { 7916bd343a9SPhilippe Charnier warn("fork"); 7928fae3551SRodney W. Grimes return(0); 7938fae3551SRodney W. Grimes } 7948fae3551SRodney W. Grimes sleep(1); 7958fae3551SRodney W. Grimes } 7968fae3551SRodney W. Grimes if (pid == 0) { 7978fae3551SRodney W. Grimes sigsetmask(omask); 7988fae3551SRodney W. Grimes setgid(getgid()); 7998fae3551SRodney W. Grimes setuid(getuid()); 8008fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 8018fae3551SRodney W. Grimes ed = DEFEDITOR; 8027bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 8036bd343a9SPhilippe Charnier err(1, "%s", ed); 8048fae3551SRodney W. Grimes } 805c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 8068fae3551SRodney W. Grimes if (xpid == pid) 8078fae3551SRodney W. Grimes break; 8088fae3551SRodney W. Grimes sigsetmask(omask); 809c8223965SMark Murray return(!locstat); 8108fae3551SRodney W. Grimes } 8118fae3551SRodney W. Grimes 8128fae3551SRodney W. Grimes char * 813326c7cdaSWarner Losh skip(char *cp) 8148fae3551SRodney W. Grimes { 8158fae3551SRodney W. Grimes 8168fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 8178fae3551SRodney W. Grimes cp++; 8188fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 819326c7cdaSWarner Losh return (NULL); 8208fae3551SRodney W. Grimes return (cp); 8218fae3551SRodney W. Grimes } 8228fae3551SRodney W. Grimes 8238fae3551SRodney W. Grimes char * 824326c7cdaSWarner Losh word(char *cp) 8258fae3551SRodney W. Grimes { 826326c7cdaSWarner Losh char c; 8278fae3551SRodney W. Grimes 8288fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 8298fae3551SRodney W. Grimes cp++; 8308fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 8318fae3551SRodney W. Grimes *cp++ = '\0'; 8328fae3551SRodney W. Grimes if (c != '#') 8338fae3551SRodney W. Grimes return (skip(cp)); 8348fae3551SRodney W. Grimes } 835326c7cdaSWarner Losh return (NULL); 8368fae3551SRodney W. Grimes } 8378fae3551SRodney W. Grimes 8388fae3551SRodney W. Grimes /* 8398fae3551SRodney W. Grimes * Read an ascii label in from fd f, 8408fae3551SRodney W. Grimes * in the same format as that put out by display(), 8418fae3551SRodney W. Grimes * and fill in lp. 8428fae3551SRodney W. Grimes */ 84361de51caSJoerg Wunsch int 844326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 8458fae3551SRodney W. Grimes { 8466b0ff5f5SPoul-Henning Kamp char *cp; 8476b0ff5f5SPoul-Henning Kamp const char **cpp; 848484c7804SJulian Elischer u_int part; 8496b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 850484c7804SJulian Elischer u_long v; 851484c7804SJulian Elischer int lineno = 0, errors = 0; 852326c7cdaSWarner Losh int i; 8538fae3551SRodney W. Grimes 854b0459c58SDag-Erling Smørgrav bzero(&part_set, sizeof(part_set)); 855b0459c58SDag-Erling Smørgrav bzero(&part_size_type, sizeof(part_size_type)); 856b0459c58SDag-Erling Smørgrav bzero(&part_offset_type, sizeof(part_offset_type)); 8578fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 85877068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 8598fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 8608fae3551SRodney W. Grimes lineno++; 86161de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 8628fae3551SRodney W. Grimes *cp = '\0'; 8638fae3551SRodney W. Grimes cp = skip(line); 8648fae3551SRodney W. Grimes if (cp == NULL) 8658fae3551SRodney W. Grimes continue; 8668fae3551SRodney W. Grimes tp = index(cp, ':'); 8678fae3551SRodney W. Grimes if (tp == NULL) { 8688fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 8698fae3551SRodney W. Grimes errors++; 8708fae3551SRodney W. Grimes continue; 8718fae3551SRodney W. Grimes } 8728fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 8738fae3551SRodney W. Grimes if (streq(cp, "type")) { 8748fae3551SRodney W. Grimes if (tp == NULL) 875c8223965SMark Murray tp = unknown; 8768fae3551SRodney W. Grimes cpp = dktypenames; 8778fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 8786b0ff5f5SPoul-Henning Kamp if (*cpp && streq(*cpp, tp)) { 8798fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 88009dbd070SIan Dowse break; 8818fae3551SRodney W. Grimes } 88209dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 88309dbd070SIan Dowse continue; 884484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 885484c7804SJulian Elischer if (v >= DKMAXTYPES) 886484c7804SJulian Elischer fprintf(stderr, "line %d:%s %lu\n", lineno, 8878fae3551SRodney W. Grimes "Warning, unknown disk type", v); 8888fae3551SRodney W. Grimes lp->d_type = v; 8898fae3551SRodney W. Grimes continue; 8908fae3551SRodney W. Grimes } 8918fae3551SRodney W. Grimes if (streq(cp, "flags")) { 8928fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 8938fae3551SRodney W. Grimes tp = word(cp); 8948fae3551SRodney W. Grimes if (streq(cp, "removeable")) 8958fae3551SRodney W. Grimes v |= D_REMOVABLE; 8968fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 8978fae3551SRodney W. Grimes v |= D_ECC; 8988fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 8998fae3551SRodney W. Grimes v |= D_BADSECT; 9008fae3551SRodney W. Grimes else { 9018fae3551SRodney W. Grimes fprintf(stderr, 9028fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 9038fae3551SRodney W. Grimes lineno, cp); 9048fae3551SRodney W. Grimes errors++; 9058fae3551SRodney W. Grimes } 9068fae3551SRodney W. Grimes } 9078fae3551SRodney W. Grimes lp->d_flags = v; 9088fae3551SRodney W. Grimes continue; 9098fae3551SRodney W. Grimes } 9108fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 9118fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 912484c7804SJulian Elischer lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 9138fae3551SRodney W. Grimes tp = word(cp); 9148fae3551SRodney W. Grimes } 9158fae3551SRodney W. Grimes continue; 9168fae3551SRodney W. Grimes } 917484c7804SJulian Elischer if (sscanf(cp, "%lu partitions", &v) == 1) { 918484c7804SJulian Elischer if (v == 0 || v > MAXPARTITIONS) { 9198fae3551SRodney W. Grimes fprintf(stderr, 9208fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 9218fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 9228fae3551SRodney W. Grimes errors++; 9238fae3551SRodney W. Grimes } else 9248fae3551SRodney W. Grimes lp->d_npartitions = v; 9258fae3551SRodney W. Grimes continue; 9268fae3551SRodney W. Grimes } 9278fae3551SRodney W. Grimes if (tp == NULL) 928c8223965SMark Murray tp = blank; 9298fae3551SRodney W. Grimes if (streq(cp, "disk")) { 9308fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 9318fae3551SRodney W. Grimes continue; 9328fae3551SRodney W. Grimes } 9338fae3551SRodney W. Grimes if (streq(cp, "label")) { 9348fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 9358fae3551SRodney W. Grimes continue; 9368fae3551SRodney W. Grimes } 9378fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 938484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 939484c7804SJulian Elischer if (v == 0 || (v % DEV_BSIZE) != 0) { 9408fae3551SRodney W. Grimes fprintf(stderr, 9418fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 9428fae3551SRodney W. Grimes lineno, tp); 9438fae3551SRodney W. Grimes errors++; 9448fae3551SRodney W. Grimes } else 9458fae3551SRodney W. Grimes lp->d_secsize = v; 9468fae3551SRodney W. Grimes continue; 9478fae3551SRodney W. Grimes } 9488fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 949484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 950484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL) 951484c7804SJulian Elischer if (v == 0 || v > 0xffffffff) { 952484c7804SJulian Elischer #else 953484c7804SJulian Elischer if (v == 0) { 954484c7804SJulian Elischer #endif 9558fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9568fae3551SRodney W. Grimes lineno, tp, cp); 9578fae3551SRodney W. Grimes errors++; 9588fae3551SRodney W. Grimes } else 9598fae3551SRodney W. Grimes lp->d_nsectors = v; 9608fae3551SRodney W. Grimes continue; 9618fae3551SRodney W. Grimes } 9628fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 963484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 964484c7804SJulian Elischer if (v == 0) { 9658fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9668fae3551SRodney W. Grimes lineno, tp, cp); 9678fae3551SRodney W. Grimes errors++; 9688fae3551SRodney W. Grimes } else 9698fae3551SRodney W. Grimes lp->d_secpercyl = v; 9708fae3551SRodney W. Grimes continue; 9718fae3551SRodney W. Grimes } 9728fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 973484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 974484c7804SJulian Elischer if (v == 0) { 9758fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9768fae3551SRodney W. Grimes lineno, tp, cp); 9778fae3551SRodney W. Grimes errors++; 9788fae3551SRodney W. Grimes } else 9798fae3551SRodney W. Grimes lp->d_ntracks = v; 9808fae3551SRodney W. Grimes continue; 9818fae3551SRodney W. Grimes } 9828fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 983484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 984484c7804SJulian Elischer if (v == 0) { 9858fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9868fae3551SRodney W. Grimes lineno, tp, cp); 9878fae3551SRodney W. Grimes errors++; 9888fae3551SRodney W. Grimes } else 9898fae3551SRodney W. Grimes lp->d_ncylinders = v; 9908fae3551SRodney W. Grimes continue; 9918fae3551SRodney W. Grimes } 992f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 993484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 994484c7804SJulian Elischer if (v == 0) { 995f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 996f75dd518SBruce Evans lineno, tp, cp); 997f75dd518SBruce Evans errors++; 998f75dd518SBruce Evans } else 999f75dd518SBruce Evans lp->d_secperunit = v; 1000f75dd518SBruce Evans continue; 1001f75dd518SBruce Evans } 10028fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 1003484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1004484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 10058fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10068fae3551SRodney W. Grimes lineno, tp, cp); 10078fae3551SRodney W. Grimes errors++; 10088fae3551SRodney W. Grimes } else 10098fae3551SRodney W. Grimes lp->d_rpm = v; 10108fae3551SRodney W. Grimes continue; 10118fae3551SRodney W. Grimes } 10128fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 1013484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1014484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 10158fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10168fae3551SRodney W. Grimes lineno, tp, cp); 10178fae3551SRodney W. Grimes errors++; 10188fae3551SRodney W. Grimes } else 10198fae3551SRodney W. Grimes lp->d_interleave = v; 10208fae3551SRodney W. Grimes continue; 10218fae3551SRodney W. Grimes } 10228fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 1023484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1024484c7804SJulian Elischer if (v > USHRT_MAX) { 10258fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10268fae3551SRodney W. Grimes lineno, tp, cp); 10278fae3551SRodney W. Grimes errors++; 10288fae3551SRodney W. Grimes } else 10298fae3551SRodney W. Grimes lp->d_trackskew = v; 10308fae3551SRodney W. Grimes continue; 10318fae3551SRodney W. Grimes } 10328fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 1033484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1034484c7804SJulian Elischer if (v > USHRT_MAX) { 10358fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10368fae3551SRodney W. Grimes lineno, tp, cp); 10378fae3551SRodney W. Grimes errors++; 10388fae3551SRodney W. Grimes } else 10398fae3551SRodney W. Grimes lp->d_cylskew = v; 10408fae3551SRodney W. Grimes continue; 10418fae3551SRodney W. Grimes } 10428fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 1043484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 10448fae3551SRodney W. Grimes lp->d_headswitch = v; 10458fae3551SRodney W. Grimes continue; 10468fae3551SRodney W. Grimes } 10478fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 1048484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 10498fae3551SRodney W. Grimes lp->d_trkseek = v; 10508fae3551SRodney W. Grimes continue; 10518fae3551SRodney W. Grimes } 10523233afaeSJohn W. De Boskey /* the ':' was removed above */ 105367b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 105467b46708SIan Dowse fprintf(stderr, 105567b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 105667b46708SIan Dowse cp); 105767b46708SIan Dowse errors++; 105867b46708SIan Dowse continue; 105967b46708SIan Dowse } 106067b46708SIan Dowse 106167b46708SIan Dowse /* Process a partition specification line. */ 10623233afaeSJohn W. De Boskey part = *cp - 'a'; 10633233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 10648fae3551SRodney W. Grimes fprintf(stderr, 10653233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 10663233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 10678fae3551SRodney W. Grimes errors++; 10688fae3551SRodney W. Grimes continue; 10698fae3551SRodney W. Grimes } 10703233afaeSJohn W. De Boskey part_set[part] = 1; 107167b46708SIan Dowse 107267b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 107367b46708SIan Dowse errors++; 107467b46708SIan Dowse break; 107567b46708SIan Dowse } 107667b46708SIan Dowse } 107767b46708SIan Dowse errors += checklabel(lp); 107867b46708SIan Dowse return (errors == 0); 107967b46708SIan Dowse } 108067b46708SIan Dowse 108167b46708SIan Dowse #define NXTNUM(n) do { \ 108213e0abcbSPaul Traina if (tp == NULL) { \ 108313e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 108467b46708SIan Dowse return (1); \ 108513e0abcbSPaul Traina } else { \ 10868fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 1087484c7804SJulian Elischer (n) = strtoul(cp, NULL, 10); \ 108813e0abcbSPaul Traina } \ 108967b46708SIan Dowse } while (0) 109067b46708SIan Dowse 10913233afaeSJohn W. De Boskey /* retain 1 character following number */ 109267b46708SIan Dowse #define NXTWORD(w,n) do { \ 10933233afaeSJohn W. De Boskey if (tp == NULL) { \ 10943233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 109567b46708SIan Dowse return (1); \ 10963233afaeSJohn W. De Boskey } else { \ 10973233afaeSJohn W. De Boskey char *tmp; \ 10983233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 1099484c7804SJulian Elischer (n) = strtoul(cp, &tmp, 10); \ 11003233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 11013233afaeSJohn W. De Boskey } \ 110267b46708SIan Dowse } while (0) 110367b46708SIan Dowse 110467b46708SIan Dowse /* 110567b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 110667b46708SIan Dowse * Return 0 on success, 1 on failure. 110767b46708SIan Dowse */ 110867b46708SIan Dowse int 110967b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 111067b46708SIan Dowse { 111167b46708SIan Dowse struct partition *pp; 111267b46708SIan Dowse char *cp; 111367b46708SIan Dowse const char **cpp; 1114484c7804SJulian Elischer u_long v; 111567b46708SIan Dowse 111667b46708SIan Dowse pp = &lp->d_partitions[part]; 111767b46708SIan Dowse cp = NULL; 111867b46708SIan Dowse 11193233afaeSJohn W. De Boskey v = 0; 11203233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 1121484c7804SJulian Elischer if (v == 0 && part_size_type[part] != '*') { 1122484c7804SJulian Elischer fprintf(stderr, 1123484c7804SJulian Elischer "line %d: %s: bad partition size\n", lineno, cp); 112467b46708SIan Dowse return (1); 112567b46708SIan Dowse } 11268fae3551SRodney W. Grimes pp->p_size = v; 11273233afaeSJohn W. De Boskey 11283233afaeSJohn W. De Boskey v = 0; 11293233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 1130484c7804SJulian Elischer if (v == 0 && part_offset_type[part] != '*' && 1131484c7804SJulian Elischer part_offset_type[part] != '\0') { 1132484c7804SJulian Elischer fprintf(stderr, 1133484c7804SJulian Elischer "line %d: %s: bad partition offset\n", lineno, cp); 113467b46708SIan Dowse return (1); 113567b46708SIan Dowse } 11368fae3551SRodney W. Grimes pp->p_offset = v; 11378fae3551SRodney W. Grimes cp = tp, tp = word(cp); 113867b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 113967b46708SIan Dowse if (*cpp && streq(*cpp, cp)) 114067b46708SIan Dowse break; 114167b46708SIan Dowse if (*cpp != NULL) { 114267b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 114367b46708SIan Dowse } else { 11448fae3551SRodney W. Grimes if (isdigit(*cp)) 1145484c7804SJulian Elischer v = strtoul(cp, NULL, 10); 11468fae3551SRodney W. Grimes else 11478fae3551SRodney W. Grimes v = FSMAXTYPES; 1148484c7804SJulian Elischer if (v >= FSMAXTYPES) { 11493233afaeSJohn W. De Boskey fprintf(stderr, 115067b46708SIan Dowse "line %d: Warning, unknown file system type %s\n", 11513233afaeSJohn W. De Boskey lineno, cp); 11528fae3551SRodney W. Grimes v = FS_UNUSED; 11538fae3551SRodney W. Grimes } 11548fae3551SRodney W. Grimes pp->p_fstype = v; 115567b46708SIan Dowse } 115667b46708SIan Dowse 11578fae3551SRodney W. Grimes switch (pp->p_fstype) { 11583233afaeSJohn W. De Boskey case FS_UNUSED: 11593233afaeSJohn W. De Boskey /* 11603233afaeSJohn W. De Boskey * allow us to accept defaults for 11613233afaeSJohn W. De Boskey * fsize/frag/cpg 11623233afaeSJohn W. De Boskey */ 11633233afaeSJohn W. De Boskey if (tp) { 11648fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 11658fae3551SRodney W. Grimes if (pp->p_fsize == 0) 11668fae3551SRodney W. Grimes break; 11678fae3551SRodney W. Grimes NXTNUM(v); 11688fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 11693233afaeSJohn W. De Boskey } 11703233afaeSJohn W. De Boskey /* else default to 0's */ 11718fae3551SRodney W. Grimes break; 11728fae3551SRodney W. Grimes 11733233afaeSJohn W. De Boskey /* These happen to be the same */ 11748fae3551SRodney W. Grimes case FS_BSDFFS: 1175ca4693edSJustin T. Gibbs case FS_BSDLFS: 11763233afaeSJohn W. De Boskey if (tp) { 1177ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1178ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1179ca4693edSJustin T. Gibbs break; 1180ca4693edSJustin T. Gibbs NXTNUM(v); 1181ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1182ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 11833233afaeSJohn W. De Boskey } else { 11843233afaeSJohn W. De Boskey /* 118567b46708SIan Dowse * FIX! poor attempt at adaptive 11863233afaeSJohn W. De Boskey */ 11873233afaeSJohn W. De Boskey /* 1 GB */ 118867b46708SIan Dowse if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 118967b46708SIan Dowse /* 119067b46708SIan Dowse * FIX! These are too low, but are traditional 119167b46708SIan Dowse */ 1192f1a7b7eeSIan Dowse pp->p_fsize = DEFAULT_NEWFS_FRAG; 1193f1a7b7eeSIan Dowse pp->p_frag = DEFAULT_NEWFS_BLOCK / 1194f1a7b7eeSIan Dowse DEFAULT_NEWFS_FRAG; 11953233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 11963233afaeSJohn W. De Boskey } else { 1197f1a7b7eeSIan Dowse pp->p_fsize = BIG_NEWFS_FRAG; 1198f1a7b7eeSIan Dowse pp->p_frag = BIG_NEWFS_BLOCK / 1199f1a7b7eeSIan Dowse BIG_NEWFS_FRAG; 12003233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 12013233afaeSJohn W. De Boskey } 12023233afaeSJohn W. De Boskey } 12038fae3551SRodney W. Grimes default: 12048fae3551SRodney W. Grimes break; 12058fae3551SRodney W. Grimes } 120667b46708SIan Dowse return (0); 12078fae3551SRodney W. Grimes } 12088fae3551SRodney W. Grimes 12098fae3551SRodney W. Grimes /* 12108fae3551SRodney W. Grimes * Check disklabel for errors and fill in 12118fae3551SRodney W. Grimes * derived fields according to supplied values. 12128fae3551SRodney W. Grimes */ 121361de51caSJoerg Wunsch int 1214326c7cdaSWarner Losh checklabel(struct disklabel *lp) 12158fae3551SRodney W. Grimes { 1216326c7cdaSWarner Losh struct partition *pp; 12178fae3551SRodney W. Grimes int i, errors = 0; 12188fae3551SRodney W. Grimes char part; 1219484c7804SJulian Elischer u_long total_size, total_percent, current_offset; 12203233afaeSJohn W. De Boskey int seen_default_offset; 12213233afaeSJohn W. De Boskey int hog_part; 12223233afaeSJohn W. De Boskey int j; 12233233afaeSJohn W. De Boskey struct partition *pp2; 12248fae3551SRodney W. Grimes 12258fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 12262a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 12278fae3551SRodney W. Grimes return (1); 12288fae3551SRodney W. Grimes } 12298fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 12302a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 12318fae3551SRodney W. Grimes return (1); 12328fae3551SRodney W. Grimes } 12338fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 12342a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 12358fae3551SRodney W. Grimes return (1); 12368fae3551SRodney W. Grimes } 12378fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 12382a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 12398fae3551SRodney W. Grimes errors++; 12408fae3551SRodney W. Grimes } 12418fae3551SRodney W. Grimes if (lp->d_rpm == 0) 12422a1deaaaSBruce Evans Warning("revolutions/minute 0"); 12438fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 12448fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 12458fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 12468fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 12478fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 12482a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 12498fae3551SRodney W. Grimes errors++; 12508fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 12518fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 12528fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 12532a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 12542a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 12553233afaeSJohn W. De Boskey 12563233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 12573233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 12583233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 12593233afaeSJohn W. De Boskey hog_part = -1; 12603233afaeSJohn W. De Boskey /* find all fixed partitions */ 12613233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 12623233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 12633233afaeSJohn W. De Boskey if (part_set[i]) { 12643233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 12656b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 12663233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 12673233afaeSJohn W. De Boskey } else { 12683233afaeSJohn W. De Boskey if (hog_part != -1) 12693233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 12703233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 12713233afaeSJohn W. De Boskey else 12723233afaeSJohn W. De Boskey hog_part = i; 12733233afaeSJohn W. De Boskey } 12743233afaeSJohn W. De Boskey } else { 12758d3105e8SWarner Losh off_t size; 12763233afaeSJohn W. De Boskey 12773233afaeSJohn W. De Boskey size = pp->p_size; 12783233afaeSJohn W. De Boskey switch (part_size_type[i]) { 12793233afaeSJohn W. De Boskey case '%': 12803233afaeSJohn W. De Boskey total_percent += size; 12813233afaeSJohn W. De Boskey break; 12823233afaeSJohn W. De Boskey case 'k': 12833233afaeSJohn W. De Boskey case 'K': 12848d3105e8SWarner Losh size *= 1024ULL; 12853233afaeSJohn W. De Boskey break; 12863233afaeSJohn W. De Boskey case 'm': 12873233afaeSJohn W. De Boskey case 'M': 12888d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 12893233afaeSJohn W. De Boskey break; 12903233afaeSJohn W. De Boskey case 'g': 12913233afaeSJohn W. De Boskey case 'G': 12928d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 12933233afaeSJohn W. De Boskey break; 12943233afaeSJohn W. De Boskey case '\0': 12953233afaeSJohn W. De Boskey break; 12963233afaeSJohn W. De Boskey default: 12973233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 12983233afaeSJohn W. De Boskey break; 12993233afaeSJohn W. De Boskey } 13003233afaeSJohn W. De Boskey /* don't count %'s yet */ 13013233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 13023233afaeSJohn W. De Boskey /* 13033233afaeSJohn W. De Boskey * for all not in sectors, convert to 13043233afaeSJohn W. De Boskey * sectors 13053233afaeSJohn W. De Boskey */ 13063233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 13073233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 13083233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 13093233afaeSJohn W. De Boskey i + 'a'); 13103233afaeSJohn W. De Boskey size /= lp->d_secsize; 13113233afaeSJohn W. De Boskey pp->p_size = size; 13123233afaeSJohn W. De Boskey } 13133233afaeSJohn W. De Boskey /* else already in sectors */ 13146b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 13153233afaeSJohn W. De Boskey total_size += size; 13163233afaeSJohn W. De Boskey } 13173233afaeSJohn W. De Boskey } 13183233afaeSJohn W. De Boskey } 13193233afaeSJohn W. De Boskey } 13203233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 13213233afaeSJohn W. De Boskey if (total_percent != 0) { 13223233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 13233233afaeSJohn W. De Boskey if (total_percent > 100) { 13246b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 13253233afaeSJohn W. De Boskey total_percent); 13263233afaeSJohn W. De Boskey errors++; 13273233afaeSJohn W. De Boskey } 13283233afaeSJohn W. De Boskey 13293233afaeSJohn W. De Boskey if (free_space > 0) { 13303233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13313233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13323233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 13333233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 13343233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 13353233afaeSJohn W. De Boskey total_size += pp->p_size; 13363233afaeSJohn W. De Boskey 13373233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 13383233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 13393233afaeSJohn W. De Boskey } 13403233afaeSJohn W. De Boskey } 13413233afaeSJohn W. De Boskey } else { 13423233afaeSJohn W. De Boskey fprintf(stderr, 13436b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 13443233afaeSJohn W. De Boskey free_space); 13453233afaeSJohn W. De Boskey errors++; 13463233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 13473233afaeSJohn W. De Boskey } 13483233afaeSJohn W. De Boskey } 13493233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 13503233afaeSJohn W. De Boskey if (hog_part != -1) { 13513233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 13523233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 13533233afaeSJohn W. De Boskey } 13543233afaeSJohn W. De Boskey 13553233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 13563233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 13573233afaeSJohn W. De Boskey seen_default_offset = 0; 13583233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13593233afaeSJohn W. De Boskey part = 'a' + i; 13603233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13613233afaeSJohn W. De Boskey if (part_set[i]) { 13623233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 13636b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 13643233afaeSJohn W. De Boskey pp->p_offset = 0; 13653233afaeSJohn W. De Boskey } else { 13663233afaeSJohn W. De Boskey pp->p_offset = current_offset; 13673233afaeSJohn W. De Boskey seen_default_offset = 1; 13683233afaeSJohn W. De Boskey } 13693233afaeSJohn W. De Boskey } else { 13703233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 13713233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 1372f2f63257SGreg Lehey seen_default_offset && i != RAW_PART && 1373f2f63257SGreg Lehey pp->p_fstype != FS_VINUM) { 13743233afaeSJohn W. De Boskey fprintf(stderr, 13756b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 13766b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 13773233afaeSJohn W. De Boskey fprintf(stderr, 13783233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 13793233afaeSJohn W. De Boskey errors++; 13803233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 13816b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 13823233afaeSJohn W. De Boskey /* 13833233afaeSJohn W. De Boskey * this may give unneeded warnings if 13843233afaeSJohn W. De Boskey * partitions are out-of-order 13853233afaeSJohn W. De Boskey */ 13863233afaeSJohn W. De Boskey Warning( 13873233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 13886b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 13893233afaeSJohn W. De Boskey } 13903233afaeSJohn W. De Boskey } 13916b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 13923233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 13933233afaeSJohn W. De Boskey } 13943233afaeSJohn W. De Boskey } 13953233afaeSJohn W. De Boskey 13968fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 13978fae3551SRodney W. Grimes part = 'a' + i; 13988fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 13998fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 14002a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 14012a1deaaaSBruce Evans part, (u_long)pp->p_offset); 14028fae3551SRodney W. Grimes #ifdef notdef 14038fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 14048fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 14058fae3551SRodney W. Grimes part); 14068fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 14078fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 14088fae3551SRodney W. Grimes part); 14098fae3551SRodney W. Grimes #endif 14108fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 14118fae3551SRodney W. Grimes fprintf(stderr, 14128fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 14138fae3551SRodney W. Grimes errors++; 14148fae3551SRodney W. Grimes } 14158fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 14168fae3551SRodney W. Grimes fprintf(stderr, 14178fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 14188fae3551SRodney W. Grimes part); 14198fae3551SRodney W. Grimes errors++; 14208fae3551SRodney W. Grimes } 14216b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) 14223233afaeSJohn W. De Boskey { 14233233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 14243233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 14253233afaeSJohn W. De Boskey if (pp->p_offset != 0) 14263233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 14273233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 14283233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 14293233afaeSJohn W. De Boskey 14303233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 14313233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 14323233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 14333233afaeSJohn W. De Boskey "standard system utilities",part); 14343233afaeSJohn W. De Boskey } 14353233afaeSJohn W. De Boskey } 14363233afaeSJohn W. De Boskey 14373233afaeSJohn W. De Boskey /* check for overlaps */ 14383233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 14393233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 14403233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 1441f2f63257SGreg Lehey if (j != RAW_PART && i != RAW_PART && 1442f2f63257SGreg Lehey pp->p_fstype != FS_VINUM && 1443f2f63257SGreg Lehey pp2->p_fstype != FS_VINUM && 1444f2f63257SGreg Lehey part_set[i] && part_set[j]) { 14453233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 14463233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 14473233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 14483233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 14493233afaeSJohn W. De Boskey j + 'a', i + 'a'); 14503233afaeSJohn W. De Boskey errors++; 14513233afaeSJohn W. De Boskey } 14523233afaeSJohn W. De Boskey } 14533233afaeSJohn W. De Boskey } 14548fae3551SRodney W. Grimes } 14558fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 14568fae3551SRodney W. Grimes part = 'a' + i; 14578fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 14588fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 14592a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 14602a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 14618fae3551SRodney W. Grimes } 14628fae3551SRodney W. Grimes return (errors); 14638fae3551SRodney W. Grimes } 14648fae3551SRodney W. Grimes 14658fae3551SRodney W. Grimes /* 1466425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1467425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1468425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1469425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1470425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1471425bed3aSJoerg Wunsch * 1472425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1473425bed3aSJoerg Wunsch */ 1474425bed3aSJoerg Wunsch struct disklabel * 1475425bed3aSJoerg Wunsch getvirginlabel(void) 1476425bed3aSJoerg Wunsch { 1477c8223965SMark Murray static struct disklabel loclab; 1478b9d05a16SPoul-Henning Kamp struct partition *dp; 1479c8223965SMark Murray char lnamebuf[BBSIZE]; 1480425bed3aSJoerg Wunsch int f; 1481b9d05a16SPoul-Henning Kamp u_int secsize, u; 1482b9d05a16SPoul-Henning Kamp off_t mediasize; 1483425bed3aSJoerg Wunsch 1484425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1485e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 148643be698cSBruce Evans return (NULL); 1487425bed3aSJoerg Wunsch } 1488c8223965SMark Murray (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1489c8223965SMark Murray if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1490c8223965SMark Murray warn("cannot open %s", lnamebuf); 149143be698cSBruce Evans return (NULL); 1492425bed3aSJoerg Wunsch } 1493ff7d5162SJordan K. Hubbard 1494b9d05a16SPoul-Henning Kamp /* New world order */ 1495b9d05a16SPoul-Henning Kamp if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1496b9d05a16SPoul-Henning Kamp (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1497425bed3aSJoerg Wunsch close (f); 149843be698cSBruce Evans return (NULL); 1499425bed3aSJoerg Wunsch } 1500b9d05a16SPoul-Henning Kamp memset(&loclab, 0, sizeof loclab); 1501b9d05a16SPoul-Henning Kamp loclab.d_magic = DISKMAGIC; 1502b9d05a16SPoul-Henning Kamp loclab.d_magic2 = DISKMAGIC; 1503b9d05a16SPoul-Henning Kamp loclab.d_secsize = secsize; 1504b9d05a16SPoul-Henning Kamp loclab.d_secperunit = mediasize / secsize; 1505b9d05a16SPoul-Henning Kamp 1506b9d05a16SPoul-Henning Kamp /* 1507b9d05a16SPoul-Henning Kamp * Nobody in these enligthened days uses the CHS geometry for 1508b9d05a16SPoul-Henning Kamp * anything, but nontheless try to get it right. If we fail 1509b9d05a16SPoul-Henning Kamp * to get any good ideas from the device, construct something 1510b9d05a16SPoul-Henning Kamp * which is IBM-PC friendly. 1511b9d05a16SPoul-Henning Kamp */ 1512b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1513b9d05a16SPoul-Henning Kamp loclab.d_nsectors = u; 1514b9d05a16SPoul-Henning Kamp else 1515b9d05a16SPoul-Henning Kamp loclab.d_nsectors = 63; 1516b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1517b9d05a16SPoul-Henning Kamp loclab.d_ntracks = u; 1518b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*1*1024) 1519b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 1; 1520b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*16*1024) 1521b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 16; 1522b9d05a16SPoul-Henning Kamp else 1523b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 255; 1524b9d05a16SPoul-Henning Kamp loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1525b9d05a16SPoul-Henning Kamp loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1526b9d05a16SPoul-Henning Kamp loclab.d_npartitions = MAXPARTITIONS; 1527b9d05a16SPoul-Henning Kamp 1528b9d05a16SPoul-Henning Kamp /* Various (unneeded) compat stuff */ 1529b9d05a16SPoul-Henning Kamp loclab.d_rpm = 3600; 1530b9d05a16SPoul-Henning Kamp loclab.d_bbsize = BBSIZE; 1531b9d05a16SPoul-Henning Kamp loclab.d_interleave = 1;; 1532b9d05a16SPoul-Henning Kamp strncpy(loclab.d_typename, "amnesiac", 1533b9d05a16SPoul-Henning Kamp sizeof(loclab.d_typename)); 1534b9d05a16SPoul-Henning Kamp 1535b9d05a16SPoul-Henning Kamp dp = &loclab.d_partitions[RAW_PART]; 1536b9d05a16SPoul-Henning Kamp dp->p_size = loclab.d_secperunit; 1537b9d05a16SPoul-Henning Kamp loclab.d_checksum = dkcksum(&loclab); 1538425bed3aSJoerg Wunsch close (f); 1539c8223965SMark Murray return (&loclab); 1540425bed3aSJoerg Wunsch } 1541425bed3aSJoerg Wunsch 15428fae3551SRodney W. Grimes 15438fae3551SRodney W. Grimes /*VARARGS1*/ 154461de51caSJoerg Wunsch void 1545326c7cdaSWarner Losh Warning(const char *fmt, ...) 15468fae3551SRodney W. Grimes { 154761de51caSJoerg Wunsch va_list ap; 15488fae3551SRodney W. Grimes 15498fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 155061de51caSJoerg Wunsch va_start(ap, fmt); 155161de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 15528fae3551SRodney W. Grimes fprintf(stderr, "\n"); 155361de51caSJoerg Wunsch va_end(ap); 15548fae3551SRodney W. Grimes } 15558fae3551SRodney W. Grimes 155661de51caSJoerg Wunsch void 1557326c7cdaSWarner Losh usage(void) 15588fae3551SRodney W. Grimes { 1559bc33ea1aSRuslan Ermilov 1560bc33ea1aSRuslan Ermilov fprintf(stderr, 1561bc33ea1aSRuslan 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", 15628fae3551SRodney W. Grimes "usage: disklabel [-r] disk", 1563bef2080aSPhilippe Charnier "\t\t(to read label)", 1564bc33ea1aSRuslan Ermilov " disklabel -w [-nr] [-m machine] disk type [packid]", 1565bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 1566bc33ea1aSRuslan Ermilov " disklabel -e [-nr] [-m machine] disk", 1567bef2080aSPhilippe Charnier "\t\t(to edit label)", 1568bc33ea1aSRuslan Ermilov " disklabel -R [-nr] [-m machine] disk protofile", 1569bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 1570bc33ea1aSRuslan Ermilov " disklabel -B [-b boot] [-m machine] disk", 1571bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 1572bc33ea1aSRuslan Ermilov " disklabel -w -B [-n] [-b boot] [-m machine] disk type [packid]", 1573bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 1574bc33ea1aSRuslan Ermilov " disklabel -R -B [-n] [-b boot] [-m machine] disk protofile", 157580baf8ceSPoul-Henning Kamp "\t\t(to restore label and install boot program)" 157680baf8ceSPoul-Henning Kamp ); 15778fae3551SRodney W. Grimes exit(1); 15788fae3551SRodney W. Grimes } 1579