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 /* 1102c60b668SPoul-Henning Kamp * Disklabel: read and write bsdlabels. 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 171c80f9755SPoul-Henning Kamp static int labeloffset; 172c80f9755SPoul-Henning Kamp static int bbsize; 173c80f9755SPoul-Henning Kamp static int alphacksum; 174c80f9755SPoul-Henning Kamp 1758fae3551SRodney W. Grimes enum { 176f080d33bSPoul-Henning Kamp UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT 1778fae3551SRodney W. Grimes } op = UNSPEC; 1788fae3551SRodney W. Grimes 179bc33ea1aSRuslan Ermilov enum { ARCH_I386, ARCH_ALPHA, ARCH_IA64 }; 180bc33ea1aSRuslan Ermilov 181bc33ea1aSRuslan Ermilov enum { MACH_I386, MACH_PC98 }; 182bc33ea1aSRuslan Ermilov 183bc33ea1aSRuslan Ermilov struct { 184bc33ea1aSRuslan Ermilov const char *name; 185bc33ea1aSRuslan Ermilov int arch; 186bc33ea1aSRuslan Ermilov int mach; 187bc33ea1aSRuslan Ermilov off_t label_sector; 188bc33ea1aSRuslan Ermilov off_t label_offset; 189bc33ea1aSRuslan Ermilov } arches[] = { 190bc33ea1aSRuslan Ermilov { "i386", ARCH_I386, MACH_I386, 1, 0 }, 191bc33ea1aSRuslan Ermilov { "pc98", ARCH_I386, MACH_PC98, 1, 0 }, 192bc33ea1aSRuslan Ermilov { "alpha", ARCH_ALPHA, ARCH_ALPHA, 0, 64 }, 193bc33ea1aSRuslan Ermilov { "ia64", ARCH_IA64, ARCH_IA64, 1, 0 }, 194bc33ea1aSRuslan Ermilov }, *arch; 195bc33ea1aSRuslan Ermilov #define NARCHES (int)(sizeof(arches) / sizeof(*arches)) 196bc33ea1aSRuslan Ermilov 1978fae3551SRodney W. Grimes int rflag; 1983233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1998fae3551SRodney W. Grimes 20061de51caSJoerg Wunsch int 201326c7cdaSWarner Losh main(int argc, char *argv[]) 2028fae3551SRodney W. Grimes { 203c1fdb43fSWarner Losh struct disklabel *lp; 2048fae3551SRodney W. Grimes FILE *t; 205bc33ea1aSRuslan Ermilov int ch, f = 0, error = 0, i; 2068fae3551SRodney W. Grimes char *name = 0; 2078fae3551SRodney W. Grimes 208bc33ea1aSRuslan Ermilov while ((ch = getopt(argc, argv, "Bb:em:nRrs:w")) != -1) 2098fae3551SRodney W. Grimes switch (ch) { 2108fae3551SRodney W. Grimes case 'B': 2118fae3551SRodney W. Grimes ++installboot; 2128fae3551SRodney W. Grimes break; 2138fae3551SRodney W. Grimes case 'b': 2148fae3551SRodney W. Grimes xxboot = optarg; 2158fae3551SRodney W. Grimes break; 216bc33ea1aSRuslan Ermilov case 'm': 217c80f9755SPoul-Henning Kamp if (!strcmp(optarg, "i386")) { 218c80f9755SPoul-Henning Kamp labeloffset = 512; 219c80f9755SPoul-Henning Kamp bbsize = 8192; 220c80f9755SPoul-Henning Kamp } else if (!strcmp(optarg, "alpha")) { 221c80f9755SPoul-Henning Kamp labeloffset = 64; 222c80f9755SPoul-Henning Kamp bbsize = 8192; 223c80f9755SPoul-Henning Kamp alphacksum = 1; 224c80f9755SPoul-Henning Kamp } 225bc33ea1aSRuslan Ermilov for (i = 0; i < NARCHES && 226bc33ea1aSRuslan Ermilov strcmp(arches[i].name, optarg) != 0; 227bc33ea1aSRuslan Ermilov i++); 228bc33ea1aSRuslan Ermilov if (i == NARCHES) 229bc33ea1aSRuslan Ermilov errx(1, "%s: unknown architecture", 230bc33ea1aSRuslan Ermilov optarg); 231bc33ea1aSRuslan Ermilov arch = &arches[i]; 232bc33ea1aSRuslan Ermilov break; 2333233afaeSJohn W. De Boskey case 'n': 2343233afaeSJohn W. De Boskey disable_write = 1; 2353233afaeSJohn W. De Boskey break; 2368fae3551SRodney W. Grimes case 'R': 2378fae3551SRodney W. Grimes if (op != UNSPEC) 2388fae3551SRodney W. Grimes usage(); 2398fae3551SRodney W. Grimes op = RESTORE; 2408fae3551SRodney W. Grimes break; 2418fae3551SRodney W. Grimes case 'e': 2428fae3551SRodney W. Grimes if (op != UNSPEC) 2438fae3551SRodney W. Grimes usage(); 2448fae3551SRodney W. Grimes op = EDIT; 2458fae3551SRodney W. Grimes break; 2468fae3551SRodney W. Grimes case 'r': 2478fae3551SRodney W. Grimes ++rflag; 2488fae3551SRodney W. Grimes break; 2498fae3551SRodney W. Grimes case 'w': 2508fae3551SRodney W. Grimes if (op != UNSPEC) 2518fae3551SRodney W. Grimes usage(); 2528fae3551SRodney W. Grimes op = WRITE; 2538fae3551SRodney W. Grimes break; 2548fae3551SRodney W. Grimes case '?': 2558fae3551SRodney W. Grimes default: 2568fae3551SRodney W. Grimes usage(); 2578fae3551SRodney W. Grimes } 2588fae3551SRodney W. Grimes argc -= optind; 2598fae3551SRodney W. Grimes argv += optind; 2608fae3551SRodney W. Grimes if (installboot) { 2618fae3551SRodney W. Grimes rflag++; 2628fae3551SRodney W. Grimes if (op == UNSPEC) 2638fae3551SRodney W. Grimes op = WRITEBOOT; 2648fae3551SRodney W. Grimes } else { 2658fae3551SRodney W. Grimes if (op == UNSPEC) 2668fae3551SRodney W. Grimes op = READ; 2678d48318bSPoul-Henning Kamp xxboot = 0; 2688fae3551SRodney W. Grimes } 2698fae3551SRodney W. Grimes if (argc < 1) 2708fae3551SRodney W. Grimes usage(); 2718fae3551SRodney W. Grimes 272bc33ea1aSRuslan Ermilov if (arch == NULL) { 273bc33ea1aSRuslan Ermilov for (i = 0; i < NARCHES; i++) 274bc33ea1aSRuslan Ermilov if (strcmp(arches[i].name, 275bc33ea1aSRuslan Ermilov #if defined(__i386__) 276bc33ea1aSRuslan Ermilov #ifdef PC98 277bc33ea1aSRuslan Ermilov "pc98" 278bc33ea1aSRuslan Ermilov #else 279bc33ea1aSRuslan Ermilov "i386" 280bc33ea1aSRuslan Ermilov #endif 281bc33ea1aSRuslan Ermilov #elif defined(__alpha__) 282bc33ea1aSRuslan Ermilov "alpha" 283bc33ea1aSRuslan Ermilov #elif defined(__ia64__) 284bc33ea1aSRuslan Ermilov "ia64" 285bc33ea1aSRuslan Ermilov #else 286bc33ea1aSRuslan Ermilov "unknown" 287bc33ea1aSRuslan Ermilov #endif 288bc33ea1aSRuslan Ermilov ) == 0) { 289bc33ea1aSRuslan Ermilov arch = &arches[i]; 290bc33ea1aSRuslan Ermilov break; 291bc33ea1aSRuslan Ermilov } 292bc33ea1aSRuslan Ermilov if (i == NARCHES) 293bc33ea1aSRuslan Ermilov errx(1, "unsupported architecture"); 294bc33ea1aSRuslan Ermilov } 295bc33ea1aSRuslan Ermilov 2968fae3551SRodney W. Grimes dkname = argv[0]; 2978fae3551SRodney W. Grimes if (dkname[0] != '/') { 29809c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2998fae3551SRodney W. Grimes specname = np; 3008fae3551SRodney W. Grimes np += strlen(specname) + 1; 3018fae3551SRodney W. Grimes } else 3028fae3551SRodney W. Grimes specname = dkname; 3038fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 3048fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 30543c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 3068fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 3078fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 3088fae3551SRodney W. Grimes } 30974c041acSPoul-Henning Kamp if (f < 0 && errno == EBUSY) { 31074c041acSPoul-Henning Kamp /* lets try to get by with ioctls */ 31174c041acSPoul-Henning Kamp f = open(specname, O_RDONLY); 31274c041acSPoul-Henning Kamp } 3138fae3551SRodney W. Grimes if (f < 0) 314bef2080aSPhilippe Charnier err(4, "%s", specname); 3158fae3551SRodney W. Grimes 3168fae3551SRodney W. Grimes switch(op) { 3178fae3551SRodney W. Grimes 3183b3038a6SBill Fumerola case UNSPEC: 3193b3038a6SBill Fumerola break; 3203b3038a6SBill Fumerola 3218fae3551SRodney W. Grimes case EDIT: 3228fae3551SRodney W. Grimes if (argc != 1) 3238fae3551SRodney W. Grimes usage(); 3248fae3551SRodney W. Grimes lp = readlabel(f); 3258fae3551SRodney W. Grimes error = edit(lp, f); 3268fae3551SRodney W. Grimes break; 3278fae3551SRodney W. Grimes 3288fae3551SRodney W. Grimes case READ: 3298fae3551SRodney W. Grimes if (argc != 1) 3308fae3551SRodney W. Grimes usage(); 3318fae3551SRodney W. Grimes lp = readlabel(f); 3328fae3551SRodney W. Grimes display(stdout, lp); 3338fae3551SRodney W. Grimes error = checklabel(lp); 3348fae3551SRodney W. Grimes break; 3358fae3551SRodney W. Grimes 3368fae3551SRodney W. Grimes case RESTORE: 3378fae3551SRodney W. Grimes if (argc != 2) 3388fae3551SRodney W. Grimes usage(); 3398fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 340bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 3416cabb348SBruce Evans if (!getasciilabel(t, &lab)) 3426cabb348SBruce Evans exit(1); 3436cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 3446cabb348SBruce Evans *lp = lab; 3458fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3468fae3551SRodney W. Grimes break; 3478fae3551SRodney W. Grimes 3488fae3551SRodney W. Grimes case WRITE: 3498fae3551SRodney W. Grimes if (argc == 3) { 3508fae3551SRodney W. Grimes name = argv[2]; 3518fae3551SRodney W. Grimes argc--; 3528fae3551SRodney W. Grimes } 3538fae3551SRodney W. Grimes if (argc != 2) 3548fae3551SRodney W. Grimes usage(); 3558fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3568fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3578fae3551SRodney W. Grimes *lp = lab; 3588fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3598fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3608fae3551SRodney W. Grimes break; 3618fae3551SRodney W. Grimes 3628fae3551SRodney W. Grimes case WRITEBOOT: 3638fae3551SRodney W. Grimes { 3648fae3551SRodney W. Grimes struct disklabel tlab; 3658fae3551SRodney W. Grimes 3668fae3551SRodney W. Grimes lp = readlabel(f); 3678fae3551SRodney W. Grimes tlab = *lp; 3688fae3551SRodney W. Grimes if (argc == 2) 3698fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3708fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3718fae3551SRodney W. Grimes *lp = tlab; 3728fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3738fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3748fae3551SRodney W. Grimes break; 3758fae3551SRodney W. Grimes } 3768fae3551SRodney W. Grimes } 3778fae3551SRodney W. Grimes exit(error); 3788fae3551SRodney W. Grimes } 3798fae3551SRodney W. Grimes 3808fae3551SRodney W. Grimes /* 381ef9ab0b3SRuslan Ermilov * Construct a prototype disklabel from /etc/disktab. 3828fae3551SRodney W. Grimes */ 38361de51caSJoerg Wunsch void 384326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp) 3858fae3551SRodney W. Grimes { 386326c7cdaSWarner Losh struct disklabel *dp; 387425bed3aSJoerg Wunsch 388425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 389425bed3aSJoerg Wunsch dp = getvirginlabel(); 390425bed3aSJoerg Wunsch else 3918fae3551SRodney W. Grimes dp = getdiskbyname(type); 3926bd343a9SPhilippe Charnier if (dp == NULL) 3936bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3948fae3551SRodney W. Grimes *lp = *dp; 3958fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 3968fae3551SRodney W. Grimes if (name) 3978fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 3988fae3551SRodney W. Grimes } 3998fae3551SRodney W. Grimes 40061de51caSJoerg Wunsch int 401bc33ea1aSRuslan Ermilov writelabel(int f, void *boot, struct disklabel *lp) 4028fae3551SRodney W. Grimes { 403bc33ea1aSRuslan Ermilov uint64_t *p, sum; 404130cd73aSDoug Rabson int i; 405130cd73aSDoug Rabson 4063233afaeSJohn W. De Boskey if (disable_write) { 4073233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 4083233afaeSJohn W. De Boskey display(stdout, lp); 4093233afaeSJohn W. De Boskey return (0); 41080baf8ceSPoul-Henning Kamp } 41180baf8ceSPoul-Henning Kamp 4128fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 4138fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 4148fae3551SRodney W. Grimes lp->d_checksum = 0; 4158fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 41680baf8ceSPoul-Henning Kamp if (!rflag) { 41780baf8ceSPoul-Henning Kamp if (ioctl(f, DIOCWDINFO, lp) < 0) { 41880baf8ceSPoul-Henning Kamp l_perror("ioctl DIOCWDINFO"); 41980baf8ceSPoul-Henning Kamp return (1); 42080baf8ceSPoul-Henning Kamp } 42180baf8ceSPoul-Henning Kamp return (0); 42280baf8ceSPoul-Henning Kamp } 42380baf8ceSPoul-Henning Kamp 424c80f9755SPoul-Henning Kamp bsd_disklabel_le_enc((u_char *)boot + labeloffset, lp); 425c80f9755SPoul-Henning Kamp 4268fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 427130cd73aSDoug Rabson 428bc33ea1aSRuslan Ermilov if (arch->arch == ARCH_ALPHA) { 429cc18e4ccSBruce Evans /* 430cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 431cc18e4ccSBruce Evans */ 432bc33ea1aSRuslan Ermilov for (p = (uint64_t *)boot, i = 0, sum = 0; i < 63; i++) 433130cd73aSDoug Rabson sum += p[i]; 434130cd73aSDoug Rabson p[63] = sum; 435bc33ea1aSRuslan Ermilov } 43674c041acSPoul-Henning Kamp if (ioctl(f, DIOCBSDBB, &boot) == 0) 43774c041acSPoul-Henning Kamp return (0); 438c80f9755SPoul-Henning Kamp if (write(f, boot, bbsize) != bbsize) { 4396bd343a9SPhilippe Charnier warn("write"); 4408fae3551SRodney W. Grimes return (1); 4418fae3551SRodney W. Grimes } 4428fae3551SRodney W. Grimes return (0); 4438fae3551SRodney W. Grimes } 4448fae3551SRodney W. Grimes 44561de51caSJoerg Wunsch void 446326c7cdaSWarner Losh l_perror(const char *s) 4478fae3551SRodney W. Grimes { 4486bd343a9SPhilippe Charnier switch (errno) { 4498fae3551SRodney W. Grimes 4508fae3551SRodney W. Grimes case ESRCH: 4513121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 4520b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 4538fae3551SRodney W. Grimes break; 4548fae3551SRodney W. Grimes 4558fae3551SRodney W. Grimes case EINVAL: 4563121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 4573121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 4588fae3551SRodney W. Grimes break; 4598fae3551SRodney W. Grimes 4608fae3551SRodney W. Grimes case EBUSY: 4616bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 4628fae3551SRodney W. Grimes break; 4638fae3551SRodney W. Grimes 4648fae3551SRodney W. Grimes case EXDEV: 4657de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 4667de06420SBruce Evans s, 'a' + RAW_PART); 4678fae3551SRodney W. Grimes break; 4688fae3551SRodney W. Grimes 4698fae3551SRodney W. Grimes default: 4706bd343a9SPhilippe Charnier warn((char *)NULL); 4718fae3551SRodney W. Grimes break; 4728fae3551SRodney W. Grimes } 4738fae3551SRodney W. Grimes } 4748fae3551SRodney W. Grimes 4758fae3551SRodney W. Grimes /* 4768fae3551SRodney W. Grimes * Fetch disklabel for disk. 4778fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 4788fae3551SRodney W. Grimes */ 4798fae3551SRodney W. Grimes struct disklabel * 480326c7cdaSWarner Losh readlabel(int f) 4818fae3551SRodney W. Grimes { 4828fae3551SRodney W. Grimes 483c80f9755SPoul-Henning Kamp (void)lseek(f, (off_t)0, SEEK_SET); 4848fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 485bef2080aSPhilippe Charnier err(4, "%s", specname); 486c80f9755SPoul-Henning Kamp bsd_disklabel_le_dec((u_char *)bootarea + labeloffset, &lab); 487c80f9755SPoul-Henning Kamp return (&lab); 4888fae3551SRodney W. Grimes } 4898fae3551SRodney W. Grimes 4908fae3551SRodney W. Grimes /* 4918fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 4928fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 4938fae3551SRodney W. Grimes */ 4948fae3551SRodney W. Grimes struct disklabel * 495bc33ea1aSRuslan Ermilov makebootarea(void *boot, struct disklabel *dp, int f) 4968fae3551SRodney W. Grimes { 49721c729c2SBruce Evans struct disklabel *lp; 498326c7cdaSWarner Losh char *p; 4998fae3551SRodney W. Grimes int b; 5008fae3551SRodney W. Grimes char *dkbasename; 5018fae3551SRodney W. Grimes struct stat sb; 502bc33ea1aSRuslan Ermilov uint64_t *bootinfo; 503cc18e4ccSBruce Evans int n; 504d70e4e53SJoerg Wunsch char *tmpbuf; 5058d48318bSPoul-Henning Kamp int i, found, dps; 5068fae3551SRodney W. Grimes 5078fae3551SRodney W. Grimes /* XXX */ 5088fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 5098fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 5108fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 5118fae3551SRodney W. Grimes } 5128fae3551SRodney W. Grimes lp = (struct disklabel *) 513bc33ea1aSRuslan Ermilov ((char *)boot + (arch->label_sector * dp->d_secsize) + 514bc33ea1aSRuslan Ermilov arch->label_offset); 5158fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5168fae3551SRodney W. Grimes /* 5178fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 5188fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 5198fae3551SRodney W. Grimes * clobber the existing boot. 5208fae3551SRodney W. Grimes */ 5218fae3551SRodney W. Grimes if (!installboot) { 5228fae3551SRodney W. Grimes if (rflag) { 5238fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 524bef2080aSPhilippe Charnier err(4, "%s", specname); 5258fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5268fae3551SRodney W. Grimes } 5278fae3551SRodney W. Grimes return (lp); 5288fae3551SRodney W. Grimes } 5298fae3551SRodney W. Grimes /* 5308fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 5318fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 5328fae3551SRodney W. Grimes */ 5338d48318bSPoul-Henning Kamp if (!xxboot) { 5348fae3551SRodney W. Grimes dkbasename = np; 5358fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 5368fae3551SRodney W. Grimes p = dkname; 5378fae3551SRodney W. Grimes else 5388fae3551SRodney W. Grimes p++; 5398fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 5408fae3551SRodney W. Grimes *np++ = *p++; 5418fae3551SRodney W. Grimes *np++ = '\0'; 5428fae3551SRodney W. Grimes 5438fae3551SRodney W. Grimes if (!xxboot) { 5448d48318bSPoul-Henning Kamp (void)sprintf(boot0, "%s/boot", _PATH_BOOTDIR); 545efba76d7SJordan K. Hubbard xxboot = boot0; 5468fae3551SRodney W. Grimes } 5478fae3551SRodney W. Grimes } 5488fae3551SRodney W. Grimes 5498fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 5508fae3551SRodney W. Grimes if (b < 0) 551bef2080aSPhilippe Charnier err(4, "%s", xxboot); 5528d48318bSPoul-Henning Kamp if (fstat(b, &sb) != 0) 5538d48318bSPoul-Henning Kamp err(4, "%s", xxboot); 554bc33ea1aSRuslan Ermilov if (arch->arch == ARCH_I386) { 5558d48318bSPoul-Henning Kamp if (sb.st_size > BBSIZE) 5568d48318bSPoul-Henning Kamp errx(4, "%s too large", xxboot); 557d70e4e53SJoerg Wunsch /* 558d70e4e53SJoerg Wunsch * XXX Botch alert. 5598d48318bSPoul-Henning Kamp * The i386/PC98 has the so-called fdisk table embedded into the 560d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 561d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 562d70e4e53SJoerg Wunsch * the xxboot provides a template.) 563d70e4e53SJoerg Wunsch */ 564d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 565bef2080aSPhilippe Charnier err(4, "%s", xxboot); 566d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 5678d48318bSPoul-Henning Kamp 5688d48318bSPoul-Henning Kamp if (read(b, boot, BBSIZE) < 0) 569bef2080aSPhilippe Charnier err(4, "%s", xxboot); 5708d48318bSPoul-Henning Kamp 5718d48318bSPoul-Henning Kamp /* XXX: rely on some very precise overlaps in definitions */ 572bc33ea1aSRuslan Ermilov dps = IS_PC98 ? sizeof(struct pc98_partition) : 573bc33ea1aSRuslan Ermilov sizeof(struct dos_partition); 574404a379eSYoshihiro Takahashi for (i = DOSPARTOFF, found = 0; 5758d48318bSPoul-Henning Kamp !found && i < (int)(DOSPARTOFF + NDOSPART * dps); 576404a379eSYoshihiro Takahashi i++) 577404a379eSYoshihiro Takahashi found = tmpbuf[i] != 0; 578404a379eSYoshihiro Takahashi if (found) 579bc33ea1aSRuslan Ermilov memcpy((void *)&((char *)boot)[DOSPARTOFF], 580404a379eSYoshihiro Takahashi (void *)&tmpbuf[DOSPARTOFF], 5818d48318bSPoul-Henning Kamp NDOSPART * dps); 582d70e4e53SJoerg Wunsch free(tmpbuf); 583bc33ea1aSRuslan Ermilov } 5848d48318bSPoul-Henning Kamp 585bc33ea1aSRuslan Ermilov if (arch->arch == ARCH_ALPHA) { 5868d48318bSPoul-Henning Kamp if (sb.st_size > BBSIZE - dp->d_secsize) 5878d48318bSPoul-Henning Kamp errx(4, "%s too large", xxboot); 588130cd73aSDoug Rabson /* 589130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 590130cd73aSDoug Rabson * second sector of the boot area. The first sector 591130cd73aSDoug Rabson * contains the label and must be edited to contain the 592130cd73aSDoug Rabson * size and location of the primary bootstrap. 593130cd73aSDoug Rabson */ 594bc33ea1aSRuslan Ermilov n = read(b, (char *)boot + dp->d_secsize, 595bc33ea1aSRuslan Ermilov BBSIZE - dp->d_secsize); 596130cd73aSDoug Rabson if (n < 0) 597130cd73aSDoug Rabson err(4, "%s", xxboot); 598bc33ea1aSRuslan Ermilov bootinfo = (uint64_t *)((char *)boot + 480); 59921c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 60021c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 60121c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 602bc33ea1aSRuslan Ermilov } 6038d48318bSPoul-Henning Kamp 6048fae3551SRodney W. Grimes (void)close(b); 6058fae3551SRodney W. Grimes return (lp); 6068fae3551SRodney W. Grimes } 6078fae3551SRodney W. Grimes 60861de51caSJoerg Wunsch void 609326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 6108fae3551SRodney W. Grimes { 611326c7cdaSWarner Losh int i, j; 612326c7cdaSWarner Losh const struct partition *pp; 6138fae3551SRodney W. Grimes 6148fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 615484c7804SJulian Elischer if (lp->d_type < DKMAXTYPES) 6168fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 6178fae3551SRodney W. Grimes else 6182a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 61961de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 62061de51caSJoerg Wunsch lp->d_typename); 62161de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 62261de51caSJoerg Wunsch lp->d_packname); 6238fae3551SRodney W. Grimes fprintf(f, "flags:"); 6248fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 6258fae3551SRodney W. Grimes fprintf(f, " removeable"); 6268fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 6278fae3551SRodney W. Grimes fprintf(f, " ecc"); 6288fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 6298fae3551SRodney W. Grimes fprintf(f, " badsect"); 6308fae3551SRodney W. Grimes fprintf(f, "\n"); 6312a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 6322a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 6332a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 6342a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 6352a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 6362a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 6372a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 6382a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 6392a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 6402a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 6412a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 6422a1deaaaSBruce Evans (u_long)lp->d_headswitch); 64361de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 6442a1deaaaSBruce Evans (u_long)lp->d_trkseek); 6458fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 6468fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 6478fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 6488fae3551SRodney W. Grimes break; 6498fae3551SRodney W. Grimes if (i < 0) 6508fae3551SRodney W. Grimes i = 0; 6518fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 6522a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 6532a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 6548fae3551SRodney W. Grimes fprintf(f, 655ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 6568fae3551SRodney W. Grimes pp = lp->d_partitions; 6578fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 6588fae3551SRodney W. Grimes if (pp->p_size) { 6592a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 6602a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 661484c7804SJulian Elischer if (pp->p_fstype < FSMAXTYPES) 6628fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 6638fae3551SRodney W. Grimes else 6648fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 6658fae3551SRodney W. Grimes switch (pp->p_fstype) { 6668fae3551SRodney W. Grimes 6678fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 6682a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 6692a1deaaaSBruce Evans (u_long)pp->p_fsize, 6702a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 6718fae3551SRodney W. Grimes break; 6728fae3551SRodney W. Grimes 6738fae3551SRodney W. Grimes case FS_BSDFFS: 6742a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 6752a1deaaaSBruce Evans (u_long)pp->p_fsize, 6762a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 6778fae3551SRodney W. Grimes pp->p_cpg); 6788fae3551SRodney W. Grimes break; 6798fae3551SRodney W. Grimes 680ca4693edSJustin T. Gibbs case FS_BSDLFS: 6812a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 6822a1deaaaSBruce Evans (u_long)pp->p_fsize, 6832a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 684ca4693edSJustin T. Gibbs pp->p_cpg); 685ca4693edSJustin T. Gibbs break; 686ca4693edSJustin T. Gibbs 6878fae3551SRodney W. Grimes default: 6888fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 6898fae3551SRodney W. Grimes break; 6908fae3551SRodney W. Grimes } 6912a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 6922a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 6938fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 6948fae3551SRodney W. Grimes putc('*', f); 6958fae3551SRodney W. Grimes else 6968fae3551SRodney W. Grimes putc(' ', f); 6972a1deaaaSBruce Evans fprintf(f, "- %lu", 6982a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 6992a1deaaaSBruce Evans lp->d_secpercyl - 1) / 7002a1deaaaSBruce Evans lp->d_secpercyl - 1)); 7018fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 7028fae3551SRodney W. Grimes putc('*', f); 7038fae3551SRodney W. Grimes fprintf(f, ")\n"); 7048fae3551SRodney W. Grimes } 7058fae3551SRodney W. Grimes } 7068fae3551SRodney W. Grimes fflush(f); 7078fae3551SRodney W. Grimes } 7088fae3551SRodney W. Grimes 70961de51caSJoerg Wunsch int 710326c7cdaSWarner Losh edit(struct disklabel *lp, int f) 7118fae3551SRodney W. Grimes { 712326c7cdaSWarner Losh int c, fd; 7138fae3551SRodney W. Grimes struct disklabel label; 714722ceb3fSWarner Losh FILE *fp; 7158fae3551SRodney W. Grimes 716722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 717722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 7186bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 7198fae3551SRodney W. Grimes return (1); 7208fae3551SRodney W. Grimes } 721722ceb3fSWarner Losh display(fp, lp); 722722ceb3fSWarner Losh fclose(fp); 7238fae3551SRodney W. Grimes for (;;) { 7248fae3551SRodney W. Grimes if (!editit()) 7258fae3551SRodney W. Grimes break; 726722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 727722ceb3fSWarner Losh if (fp == NULL) { 7286bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 7298fae3551SRodney W. Grimes break; 7308fae3551SRodney W. Grimes } 7318fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 732722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 7338fae3551SRodney W. Grimes *lp = label; 7348fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 735722ceb3fSWarner Losh fclose(fp); 7368fae3551SRodney W. Grimes (void) unlink(tmpfil); 7378fae3551SRodney W. Grimes return (0); 7388fae3551SRodney W. Grimes } 7398fae3551SRodney W. Grimes } 740722ceb3fSWarner Losh fclose(fp); 7418fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 7428fae3551SRodney W. Grimes c = getchar(); 7438fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 7448fae3551SRodney W. Grimes while (getchar() != (int)'\n') 7458fae3551SRodney W. Grimes ; 7468fae3551SRodney W. Grimes if (c == (int)'n') 7478fae3551SRodney W. Grimes break; 7488fae3551SRodney W. Grimes } 7498fae3551SRodney W. Grimes (void) unlink(tmpfil); 7508fae3551SRodney W. Grimes return (1); 7518fae3551SRodney W. Grimes } 7528fae3551SRodney W. Grimes 75361de51caSJoerg Wunsch int 754326c7cdaSWarner Losh editit(void) 7558fae3551SRodney W. Grimes { 756326c7cdaSWarner Losh int pid, xpid; 757c8223965SMark Murray int locstat, omask; 758c8223965SMark Murray const char *ed; 7598fae3551SRodney W. Grimes 7608fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 7618fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 7628fae3551SRodney W. Grimes if (errno == EPROCLIM) { 7636bd343a9SPhilippe Charnier warnx("you have too many processes"); 7648fae3551SRodney W. Grimes return(0); 7658fae3551SRodney W. Grimes } 7668fae3551SRodney W. Grimes if (errno != EAGAIN) { 7676bd343a9SPhilippe Charnier warn("fork"); 7688fae3551SRodney W. Grimes return(0); 7698fae3551SRodney W. Grimes } 7708fae3551SRodney W. Grimes sleep(1); 7718fae3551SRodney W. Grimes } 7728fae3551SRodney W. Grimes if (pid == 0) { 7738fae3551SRodney W. Grimes sigsetmask(omask); 7748fae3551SRodney W. Grimes setgid(getgid()); 7758fae3551SRodney W. Grimes setuid(getuid()); 7768fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 7778fae3551SRodney W. Grimes ed = DEFEDITOR; 7787bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 7796bd343a9SPhilippe Charnier err(1, "%s", ed); 7808fae3551SRodney W. Grimes } 781c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 7828fae3551SRodney W. Grimes if (xpid == pid) 7838fae3551SRodney W. Grimes break; 7848fae3551SRodney W. Grimes sigsetmask(omask); 785c8223965SMark Murray return(!locstat); 7868fae3551SRodney W. Grimes } 7878fae3551SRodney W. Grimes 7888fae3551SRodney W. Grimes char * 789326c7cdaSWarner Losh skip(char *cp) 7908fae3551SRodney W. Grimes { 7918fae3551SRodney W. Grimes 7928fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 7938fae3551SRodney W. Grimes cp++; 7948fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 795326c7cdaSWarner Losh return (NULL); 7968fae3551SRodney W. Grimes return (cp); 7978fae3551SRodney W. Grimes } 7988fae3551SRodney W. Grimes 7998fae3551SRodney W. Grimes char * 800326c7cdaSWarner Losh word(char *cp) 8018fae3551SRodney W. Grimes { 802326c7cdaSWarner Losh char c; 8038fae3551SRodney W. Grimes 8048fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 8058fae3551SRodney W. Grimes cp++; 8068fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 8078fae3551SRodney W. Grimes *cp++ = '\0'; 8088fae3551SRodney W. Grimes if (c != '#') 8098fae3551SRodney W. Grimes return (skip(cp)); 8108fae3551SRodney W. Grimes } 811326c7cdaSWarner Losh return (NULL); 8128fae3551SRodney W. Grimes } 8138fae3551SRodney W. Grimes 8148fae3551SRodney W. Grimes /* 8158fae3551SRodney W. Grimes * Read an ascii label in from fd f, 8168fae3551SRodney W. Grimes * in the same format as that put out by display(), 8178fae3551SRodney W. Grimes * and fill in lp. 8188fae3551SRodney W. Grimes */ 81961de51caSJoerg Wunsch int 820326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 8218fae3551SRodney W. Grimes { 8226b0ff5f5SPoul-Henning Kamp char *cp; 8236b0ff5f5SPoul-Henning Kamp const char **cpp; 824484c7804SJulian Elischer u_int part; 8256b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 826484c7804SJulian Elischer u_long v; 827484c7804SJulian Elischer int lineno = 0, errors = 0; 828326c7cdaSWarner Losh int i; 8298fae3551SRodney W. Grimes 830b0459c58SDag-Erling Smørgrav bzero(&part_set, sizeof(part_set)); 831b0459c58SDag-Erling Smørgrav bzero(&part_size_type, sizeof(part_size_type)); 832b0459c58SDag-Erling Smørgrav bzero(&part_offset_type, sizeof(part_offset_type)); 8338fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 83477068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 8358fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 8368fae3551SRodney W. Grimes lineno++; 83761de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 8388fae3551SRodney W. Grimes *cp = '\0'; 8398fae3551SRodney W. Grimes cp = skip(line); 8408fae3551SRodney W. Grimes if (cp == NULL) 8418fae3551SRodney W. Grimes continue; 8428fae3551SRodney W. Grimes tp = index(cp, ':'); 8438fae3551SRodney W. Grimes if (tp == NULL) { 8448fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 8458fae3551SRodney W. Grimes errors++; 8468fae3551SRodney W. Grimes continue; 8478fae3551SRodney W. Grimes } 8488fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 8498fae3551SRodney W. Grimes if (streq(cp, "type")) { 8508fae3551SRodney W. Grimes if (tp == NULL) 851c8223965SMark Murray tp = unknown; 8528fae3551SRodney W. Grimes cpp = dktypenames; 8538fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 8546b0ff5f5SPoul-Henning Kamp if (*cpp && streq(*cpp, tp)) { 8558fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 85609dbd070SIan Dowse break; 8578fae3551SRodney W. Grimes } 85809dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 85909dbd070SIan Dowse continue; 860484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 861484c7804SJulian Elischer if (v >= DKMAXTYPES) 862484c7804SJulian Elischer fprintf(stderr, "line %d:%s %lu\n", lineno, 8638fae3551SRodney W. Grimes "Warning, unknown disk type", v); 8648fae3551SRodney W. Grimes lp->d_type = v; 8658fae3551SRodney W. Grimes continue; 8668fae3551SRodney W. Grimes } 8678fae3551SRodney W. Grimes if (streq(cp, "flags")) { 8688fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 8698fae3551SRodney W. Grimes tp = word(cp); 8708fae3551SRodney W. Grimes if (streq(cp, "removeable")) 8718fae3551SRodney W. Grimes v |= D_REMOVABLE; 8728fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 8738fae3551SRodney W. Grimes v |= D_ECC; 8748fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 8758fae3551SRodney W. Grimes v |= D_BADSECT; 8768fae3551SRodney W. Grimes else { 8778fae3551SRodney W. Grimes fprintf(stderr, 8788fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 8798fae3551SRodney W. Grimes lineno, cp); 8808fae3551SRodney W. Grimes errors++; 8818fae3551SRodney W. Grimes } 8828fae3551SRodney W. Grimes } 8838fae3551SRodney W. Grimes lp->d_flags = v; 8848fae3551SRodney W. Grimes continue; 8858fae3551SRodney W. Grimes } 8868fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 8878fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 888484c7804SJulian Elischer lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 8898fae3551SRodney W. Grimes tp = word(cp); 8908fae3551SRodney W. Grimes } 8918fae3551SRodney W. Grimes continue; 8928fae3551SRodney W. Grimes } 893484c7804SJulian Elischer if (sscanf(cp, "%lu partitions", &v) == 1) { 894484c7804SJulian Elischer if (v == 0 || v > MAXPARTITIONS) { 8958fae3551SRodney W. Grimes fprintf(stderr, 8968fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 8978fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 8988fae3551SRodney W. Grimes errors++; 8998fae3551SRodney W. Grimes } else 9008fae3551SRodney W. Grimes lp->d_npartitions = v; 9018fae3551SRodney W. Grimes continue; 9028fae3551SRodney W. Grimes } 9038fae3551SRodney W. Grimes if (tp == NULL) 904c8223965SMark Murray tp = blank; 9058fae3551SRodney W. Grimes if (streq(cp, "disk")) { 9068fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 9078fae3551SRodney W. Grimes continue; 9088fae3551SRodney W. Grimes } 9098fae3551SRodney W. Grimes if (streq(cp, "label")) { 9108fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 9118fae3551SRodney W. Grimes continue; 9128fae3551SRodney W. Grimes } 9138fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 914484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 915484c7804SJulian Elischer if (v == 0 || (v % DEV_BSIZE) != 0) { 9168fae3551SRodney W. Grimes fprintf(stderr, 9178fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 9188fae3551SRodney W. Grimes lineno, tp); 9198fae3551SRodney W. Grimes errors++; 9208fae3551SRodney W. Grimes } else 9218fae3551SRodney W. Grimes lp->d_secsize = v; 9228fae3551SRodney W. Grimes continue; 9238fae3551SRodney W. Grimes } 9248fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 925484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 926484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL) 927484c7804SJulian Elischer if (v == 0 || v > 0xffffffff) { 928484c7804SJulian Elischer #else 929484c7804SJulian Elischer if (v == 0) { 930484c7804SJulian Elischer #endif 9318fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9328fae3551SRodney W. Grimes lineno, tp, cp); 9338fae3551SRodney W. Grimes errors++; 9348fae3551SRodney W. Grimes } else 9358fae3551SRodney W. Grimes lp->d_nsectors = v; 9368fae3551SRodney W. Grimes continue; 9378fae3551SRodney W. Grimes } 9388fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 939484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 940484c7804SJulian Elischer if (v == 0) { 9418fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9428fae3551SRodney W. Grimes lineno, tp, cp); 9438fae3551SRodney W. Grimes errors++; 9448fae3551SRodney W. Grimes } else 9458fae3551SRodney W. Grimes lp->d_secpercyl = v; 9468fae3551SRodney W. Grimes continue; 9478fae3551SRodney W. Grimes } 9488fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 949484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 950484c7804SJulian Elischer if (v == 0) { 9518fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9528fae3551SRodney W. Grimes lineno, tp, cp); 9538fae3551SRodney W. Grimes errors++; 9548fae3551SRodney W. Grimes } else 9558fae3551SRodney W. Grimes lp->d_ntracks = v; 9568fae3551SRodney W. Grimes continue; 9578fae3551SRodney W. Grimes } 9588fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 959484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 960484c7804SJulian Elischer if (v == 0) { 9618fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9628fae3551SRodney W. Grimes lineno, tp, cp); 9638fae3551SRodney W. Grimes errors++; 9648fae3551SRodney W. Grimes } else 9658fae3551SRodney W. Grimes lp->d_ncylinders = v; 9668fae3551SRodney W. Grimes continue; 9678fae3551SRodney W. Grimes } 968f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 969484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 970484c7804SJulian Elischer if (v == 0) { 971f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 972f75dd518SBruce Evans lineno, tp, cp); 973f75dd518SBruce Evans errors++; 974f75dd518SBruce Evans } else 975f75dd518SBruce Evans lp->d_secperunit = v; 976f75dd518SBruce Evans continue; 977f75dd518SBruce Evans } 9788fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 979484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 980484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 9818fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9828fae3551SRodney W. Grimes lineno, tp, cp); 9838fae3551SRodney W. Grimes errors++; 9848fae3551SRodney W. Grimes } else 9858fae3551SRodney W. Grimes lp->d_rpm = v; 9868fae3551SRodney W. Grimes continue; 9878fae3551SRodney W. Grimes } 9888fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 989484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 990484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 9918fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9928fae3551SRodney W. Grimes lineno, tp, cp); 9938fae3551SRodney W. Grimes errors++; 9948fae3551SRodney W. Grimes } else 9958fae3551SRodney W. Grimes lp->d_interleave = v; 9968fae3551SRodney W. Grimes continue; 9978fae3551SRodney W. Grimes } 9988fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 999484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1000484c7804SJulian Elischer if (v > USHRT_MAX) { 10018fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10028fae3551SRodney W. Grimes lineno, tp, cp); 10038fae3551SRodney W. Grimes errors++; 10048fae3551SRodney W. Grimes } else 10058fae3551SRodney W. Grimes lp->d_trackskew = v; 10068fae3551SRodney W. Grimes continue; 10078fae3551SRodney W. Grimes } 10088fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 1009484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1010484c7804SJulian Elischer if (v > USHRT_MAX) { 10118fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10128fae3551SRodney W. Grimes lineno, tp, cp); 10138fae3551SRodney W. Grimes errors++; 10148fae3551SRodney W. Grimes } else 10158fae3551SRodney W. Grimes lp->d_cylskew = v; 10168fae3551SRodney W. Grimes continue; 10178fae3551SRodney W. Grimes } 10188fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 1019484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 10208fae3551SRodney W. Grimes lp->d_headswitch = v; 10218fae3551SRodney W. Grimes continue; 10228fae3551SRodney W. Grimes } 10238fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 1024484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 10258fae3551SRodney W. Grimes lp->d_trkseek = v; 10268fae3551SRodney W. Grimes continue; 10278fae3551SRodney W. Grimes } 10283233afaeSJohn W. De Boskey /* the ':' was removed above */ 102967b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 103067b46708SIan Dowse fprintf(stderr, 103167b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 103267b46708SIan Dowse cp); 103367b46708SIan Dowse errors++; 103467b46708SIan Dowse continue; 103567b46708SIan Dowse } 103667b46708SIan Dowse 103767b46708SIan Dowse /* Process a partition specification line. */ 10383233afaeSJohn W. De Boskey part = *cp - 'a'; 10393233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 10408fae3551SRodney W. Grimes fprintf(stderr, 10413233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 10423233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 10438fae3551SRodney W. Grimes errors++; 10448fae3551SRodney W. Grimes continue; 10458fae3551SRodney W. Grimes } 10463233afaeSJohn W. De Boskey part_set[part] = 1; 104767b46708SIan Dowse 104867b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 104967b46708SIan Dowse errors++; 105067b46708SIan Dowse break; 105167b46708SIan Dowse } 105267b46708SIan Dowse } 105367b46708SIan Dowse errors += checklabel(lp); 105467b46708SIan Dowse return (errors == 0); 105567b46708SIan Dowse } 105667b46708SIan Dowse 105767b46708SIan Dowse #define NXTNUM(n) do { \ 105813e0abcbSPaul Traina if (tp == NULL) { \ 105913e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 106067b46708SIan Dowse return (1); \ 106113e0abcbSPaul Traina } else { \ 10628fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 1063484c7804SJulian Elischer (n) = strtoul(cp, NULL, 10); \ 106413e0abcbSPaul Traina } \ 106567b46708SIan Dowse } while (0) 106667b46708SIan Dowse 10673233afaeSJohn W. De Boskey /* retain 1 character following number */ 106867b46708SIan Dowse #define NXTWORD(w,n) do { \ 10693233afaeSJohn W. De Boskey if (tp == NULL) { \ 10703233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 107167b46708SIan Dowse return (1); \ 10723233afaeSJohn W. De Boskey } else { \ 10733233afaeSJohn W. De Boskey char *tmp; \ 10743233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 1075484c7804SJulian Elischer (n) = strtoul(cp, &tmp, 10); \ 10763233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 10773233afaeSJohn W. De Boskey } \ 107867b46708SIan Dowse } while (0) 107967b46708SIan Dowse 108067b46708SIan Dowse /* 108167b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 108267b46708SIan Dowse * Return 0 on success, 1 on failure. 108367b46708SIan Dowse */ 108467b46708SIan Dowse int 108567b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 108667b46708SIan Dowse { 108767b46708SIan Dowse struct partition *pp; 108867b46708SIan Dowse char *cp; 108967b46708SIan Dowse const char **cpp; 1090484c7804SJulian Elischer u_long v; 109167b46708SIan Dowse 109267b46708SIan Dowse pp = &lp->d_partitions[part]; 109367b46708SIan Dowse cp = NULL; 109467b46708SIan Dowse 10953233afaeSJohn W. De Boskey v = 0; 10963233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 1097484c7804SJulian Elischer if (v == 0 && part_size_type[part] != '*') { 1098484c7804SJulian Elischer fprintf(stderr, 1099484c7804SJulian Elischer "line %d: %s: bad partition size\n", lineno, cp); 110067b46708SIan Dowse return (1); 110167b46708SIan Dowse } 11028fae3551SRodney W. Grimes pp->p_size = v; 11033233afaeSJohn W. De Boskey 11043233afaeSJohn W. De Boskey v = 0; 11053233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 1106484c7804SJulian Elischer if (v == 0 && part_offset_type[part] != '*' && 1107484c7804SJulian Elischer part_offset_type[part] != '\0') { 1108484c7804SJulian Elischer fprintf(stderr, 1109484c7804SJulian Elischer "line %d: %s: bad partition offset\n", lineno, cp); 111067b46708SIan Dowse return (1); 111167b46708SIan Dowse } 11128fae3551SRodney W. Grimes pp->p_offset = v; 111329f3f095SYaroslav Tykhiy if (tp == NULL) { 111429f3f095SYaroslav Tykhiy fprintf(stderr, "line %d: missing file system type\n", lineno); 111529f3f095SYaroslav Tykhiy return (1); 111629f3f095SYaroslav Tykhiy } 11178fae3551SRodney W. Grimes cp = tp, tp = word(cp); 111867b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 111967b46708SIan Dowse if (*cpp && streq(*cpp, cp)) 112067b46708SIan Dowse break; 112167b46708SIan Dowse if (*cpp != NULL) { 112267b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 112367b46708SIan Dowse } else { 11248fae3551SRodney W. Grimes if (isdigit(*cp)) 1125484c7804SJulian Elischer v = strtoul(cp, NULL, 10); 11268fae3551SRodney W. Grimes else 11278fae3551SRodney W. Grimes v = FSMAXTYPES; 1128484c7804SJulian Elischer if (v >= FSMAXTYPES) { 11293233afaeSJohn W. De Boskey fprintf(stderr, 113067b46708SIan Dowse "line %d: Warning, unknown file system type %s\n", 11313233afaeSJohn W. De Boskey lineno, cp); 11328fae3551SRodney W. Grimes v = FS_UNUSED; 11338fae3551SRodney W. Grimes } 11348fae3551SRodney W. Grimes pp->p_fstype = v; 113567b46708SIan Dowse } 113667b46708SIan Dowse 11378fae3551SRodney W. Grimes switch (pp->p_fstype) { 11383233afaeSJohn W. De Boskey case FS_UNUSED: 11393233afaeSJohn W. De Boskey /* 11403233afaeSJohn W. De Boskey * allow us to accept defaults for 11413233afaeSJohn W. De Boskey * fsize/frag/cpg 11423233afaeSJohn W. De Boskey */ 11433233afaeSJohn W. De Boskey if (tp) { 11448fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 11458fae3551SRodney W. Grimes if (pp->p_fsize == 0) 11468fae3551SRodney W. Grimes break; 11478fae3551SRodney W. Grimes NXTNUM(v); 11488fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 11493233afaeSJohn W. De Boskey } 11503233afaeSJohn W. De Boskey /* else default to 0's */ 11518fae3551SRodney W. Grimes break; 11528fae3551SRodney W. Grimes 11533233afaeSJohn W. De Boskey /* These happen to be the same */ 11548fae3551SRodney W. Grimes case FS_BSDFFS: 1155ca4693edSJustin T. Gibbs case FS_BSDLFS: 11563233afaeSJohn W. De Boskey if (tp) { 1157ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1158ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1159ca4693edSJustin T. Gibbs break; 1160ca4693edSJustin T. Gibbs NXTNUM(v); 1161ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1162ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 11633233afaeSJohn W. De Boskey } else { 11643233afaeSJohn W. De Boskey /* 116567b46708SIan Dowse * FIX! poor attempt at adaptive 11663233afaeSJohn W. De Boskey */ 11673233afaeSJohn W. De Boskey /* 1 GB */ 116867b46708SIan Dowse if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 116967b46708SIan Dowse /* 117067b46708SIan Dowse * FIX! These are too low, but are traditional 117167b46708SIan Dowse */ 1172f1a7b7eeSIan Dowse pp->p_fsize = DEFAULT_NEWFS_FRAG; 1173f1a7b7eeSIan Dowse pp->p_frag = DEFAULT_NEWFS_BLOCK / 1174f1a7b7eeSIan Dowse DEFAULT_NEWFS_FRAG; 11753233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 11763233afaeSJohn W. De Boskey } else { 1177f1a7b7eeSIan Dowse pp->p_fsize = BIG_NEWFS_FRAG; 1178f1a7b7eeSIan Dowse pp->p_frag = BIG_NEWFS_BLOCK / 1179f1a7b7eeSIan Dowse BIG_NEWFS_FRAG; 11803233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 11813233afaeSJohn W. De Boskey } 11823233afaeSJohn W. De Boskey } 11838fae3551SRodney W. Grimes default: 11848fae3551SRodney W. Grimes break; 11858fae3551SRodney W. Grimes } 118667b46708SIan Dowse return (0); 11878fae3551SRodney W. Grimes } 11888fae3551SRodney W. Grimes 11898fae3551SRodney W. Grimes /* 11908fae3551SRodney W. Grimes * Check disklabel for errors and fill in 11918fae3551SRodney W. Grimes * derived fields according to supplied values. 11928fae3551SRodney W. Grimes */ 119361de51caSJoerg Wunsch int 1194326c7cdaSWarner Losh checklabel(struct disklabel *lp) 11958fae3551SRodney W. Grimes { 1196326c7cdaSWarner Losh struct partition *pp; 11978fae3551SRodney W. Grimes int i, errors = 0; 11988fae3551SRodney W. Grimes char part; 1199484c7804SJulian Elischer u_long total_size, total_percent, current_offset; 12003233afaeSJohn W. De Boskey int seen_default_offset; 12013233afaeSJohn W. De Boskey int hog_part; 12023233afaeSJohn W. De Boskey int j; 12033233afaeSJohn W. De Boskey struct partition *pp2; 12048fae3551SRodney W. Grimes 12058fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 12062a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 12078fae3551SRodney W. Grimes return (1); 12088fae3551SRodney W. Grimes } 12098fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 12102a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 12118fae3551SRodney W. Grimes return (1); 12128fae3551SRodney W. Grimes } 12138fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 12142a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 12158fae3551SRodney W. Grimes return (1); 12168fae3551SRodney W. Grimes } 12178fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 12182a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 12198fae3551SRodney W. Grimes errors++; 12208fae3551SRodney W. Grimes } 12218fae3551SRodney W. Grimes if (lp->d_rpm == 0) 12222a1deaaaSBruce Evans Warning("revolutions/minute 0"); 12238fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 12248fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 12258fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 12268fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 12278fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 12282a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 12298fae3551SRodney W. Grimes errors++; 12308fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 12318fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 12328fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 12332a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 12342a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 12353233afaeSJohn W. De Boskey 12363233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 12373233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 12383233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 12393233afaeSJohn W. De Boskey hog_part = -1; 12403233afaeSJohn W. De Boskey /* find all fixed partitions */ 12413233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 12423233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 12433233afaeSJohn W. De Boskey if (part_set[i]) { 12443233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 12456b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 12463233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 12473233afaeSJohn W. De Boskey } else { 12483233afaeSJohn W. De Boskey if (hog_part != -1) 12493233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 12503233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 12513233afaeSJohn W. De Boskey else 12523233afaeSJohn W. De Boskey hog_part = i; 12533233afaeSJohn W. De Boskey } 12543233afaeSJohn W. De Boskey } else { 12558d3105e8SWarner Losh off_t size; 12563233afaeSJohn W. De Boskey 12573233afaeSJohn W. De Boskey size = pp->p_size; 12583233afaeSJohn W. De Boskey switch (part_size_type[i]) { 12593233afaeSJohn W. De Boskey case '%': 12603233afaeSJohn W. De Boskey total_percent += size; 12613233afaeSJohn W. De Boskey break; 12623233afaeSJohn W. De Boskey case 'k': 12633233afaeSJohn W. De Boskey case 'K': 12648d3105e8SWarner Losh size *= 1024ULL; 12653233afaeSJohn W. De Boskey break; 12663233afaeSJohn W. De Boskey case 'm': 12673233afaeSJohn W. De Boskey case 'M': 12688d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 12693233afaeSJohn W. De Boskey break; 12703233afaeSJohn W. De Boskey case 'g': 12713233afaeSJohn W. De Boskey case 'G': 12728d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 12733233afaeSJohn W. De Boskey break; 12743233afaeSJohn W. De Boskey case '\0': 12753233afaeSJohn W. De Boskey break; 12763233afaeSJohn W. De Boskey default: 12773233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 12783233afaeSJohn W. De Boskey break; 12793233afaeSJohn W. De Boskey } 12803233afaeSJohn W. De Boskey /* don't count %'s yet */ 12813233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 12823233afaeSJohn W. De Boskey /* 12833233afaeSJohn W. De Boskey * for all not in sectors, convert to 12843233afaeSJohn W. De Boskey * sectors 12853233afaeSJohn W. De Boskey */ 12863233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 12873233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 12883233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 12893233afaeSJohn W. De Boskey i + 'a'); 12903233afaeSJohn W. De Boskey size /= lp->d_secsize; 12913233afaeSJohn W. De Boskey pp->p_size = size; 12923233afaeSJohn W. De Boskey } 12933233afaeSJohn W. De Boskey /* else already in sectors */ 12946b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 12953233afaeSJohn W. De Boskey total_size += size; 12963233afaeSJohn W. De Boskey } 12973233afaeSJohn W. De Boskey } 12983233afaeSJohn W. De Boskey } 12993233afaeSJohn W. De Boskey } 13003233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 13013233afaeSJohn W. De Boskey if (total_percent != 0) { 13023233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 13033233afaeSJohn W. De Boskey if (total_percent > 100) { 13046b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 13053233afaeSJohn W. De Boskey total_percent); 13063233afaeSJohn W. De Boskey errors++; 13073233afaeSJohn W. De Boskey } 13083233afaeSJohn W. De Boskey 13093233afaeSJohn W. De Boskey if (free_space > 0) { 13103233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13113233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13123233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 13133233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 13143233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 13153233afaeSJohn W. De Boskey total_size += pp->p_size; 13163233afaeSJohn W. De Boskey 13173233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 13183233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 13193233afaeSJohn W. De Boskey } 13203233afaeSJohn W. De Boskey } 13213233afaeSJohn W. De Boskey } else { 13223233afaeSJohn W. De Boskey fprintf(stderr, 13236b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 13243233afaeSJohn W. De Boskey free_space); 13253233afaeSJohn W. De Boskey errors++; 13263233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 13273233afaeSJohn W. De Boskey } 13283233afaeSJohn W. De Boskey } 13293233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 13303233afaeSJohn W. De Boskey if (hog_part != -1) { 13313233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 13323233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 13333233afaeSJohn W. De Boskey } 13343233afaeSJohn W. De Boskey 13353233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 13363233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 13373233afaeSJohn W. De Boskey seen_default_offset = 0; 13383233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13393233afaeSJohn W. De Boskey part = 'a' + i; 13403233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13413233afaeSJohn W. De Boskey if (part_set[i]) { 13423233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 13436b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 13443233afaeSJohn W. De Boskey pp->p_offset = 0; 13453233afaeSJohn W. De Boskey } else { 13463233afaeSJohn W. De Boskey pp->p_offset = current_offset; 13473233afaeSJohn W. De Boskey seen_default_offset = 1; 13483233afaeSJohn W. De Boskey } 13493233afaeSJohn W. De Boskey } else { 13503233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 13513233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 1352f2f63257SGreg Lehey seen_default_offset && i != RAW_PART && 1353f2f63257SGreg Lehey pp->p_fstype != FS_VINUM) { 13543233afaeSJohn W. De Boskey fprintf(stderr, 13556b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 13566b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 13573233afaeSJohn W. De Boskey fprintf(stderr, 13583233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 13593233afaeSJohn W. De Boskey errors++; 13603233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 13616b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 13623233afaeSJohn W. De Boskey /* 13633233afaeSJohn W. De Boskey * this may give unneeded warnings if 13643233afaeSJohn W. De Boskey * partitions are out-of-order 13653233afaeSJohn W. De Boskey */ 13663233afaeSJohn W. De Boskey Warning( 13673233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 13686b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 13693233afaeSJohn W. De Boskey } 13703233afaeSJohn W. De Boskey } 13716b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 13723233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 13733233afaeSJohn W. De Boskey } 13743233afaeSJohn W. De Boskey } 13753233afaeSJohn W. De Boskey 13768fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 13778fae3551SRodney W. Grimes part = 'a' + i; 13788fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 13798fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 13802a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 13812a1deaaaSBruce Evans part, (u_long)pp->p_offset); 13828fae3551SRodney W. Grimes #ifdef notdef 13838fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 13848fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 13858fae3551SRodney W. Grimes part); 13868fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 13878fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 13888fae3551SRodney W. Grimes part); 13898fae3551SRodney W. Grimes #endif 13908fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 13918fae3551SRodney W. Grimes fprintf(stderr, 13928fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 13938fae3551SRodney W. Grimes errors++; 13948fae3551SRodney W. Grimes } 13958fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 13968fae3551SRodney W. Grimes fprintf(stderr, 13978fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 13988fae3551SRodney W. Grimes part); 13998fae3551SRodney W. Grimes errors++; 14008fae3551SRodney W. Grimes } 14016b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) 14023233afaeSJohn W. De Boskey { 14033233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 14043233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 14053233afaeSJohn W. De Boskey if (pp->p_offset != 0) 14063233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 14073233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 14083233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 14093233afaeSJohn W. De Boskey 14103233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 14113233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 14123233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 14133233afaeSJohn W. De Boskey "standard system utilities",part); 14143233afaeSJohn W. De Boskey } 14153233afaeSJohn W. De Boskey } 14163233afaeSJohn W. De Boskey 14173233afaeSJohn W. De Boskey /* check for overlaps */ 14183233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 14193233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 14203233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 1421f2f63257SGreg Lehey if (j != RAW_PART && i != RAW_PART && 1422f2f63257SGreg Lehey pp->p_fstype != FS_VINUM && 1423f2f63257SGreg Lehey pp2->p_fstype != FS_VINUM && 1424f2f63257SGreg Lehey part_set[i] && part_set[j]) { 14253233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 14263233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 14273233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 14283233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 14293233afaeSJohn W. De Boskey j + 'a', i + 'a'); 14303233afaeSJohn W. De Boskey errors++; 14313233afaeSJohn W. De Boskey } 14323233afaeSJohn W. De Boskey } 14333233afaeSJohn W. De Boskey } 14348fae3551SRodney W. Grimes } 14358fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 14368fae3551SRodney W. Grimes part = 'a' + i; 14378fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 14388fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 14392a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 14402a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 14418fae3551SRodney W. Grimes } 14428fae3551SRodney W. Grimes return (errors); 14438fae3551SRodney W. Grimes } 14448fae3551SRodney W. Grimes 14458fae3551SRodney W. Grimes /* 1446425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1447425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1448425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1449425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1450425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1451425bed3aSJoerg Wunsch * 1452425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1453425bed3aSJoerg Wunsch */ 1454425bed3aSJoerg Wunsch struct disklabel * 1455425bed3aSJoerg Wunsch getvirginlabel(void) 1456425bed3aSJoerg Wunsch { 1457c8223965SMark Murray static struct disklabel loclab; 1458b9d05a16SPoul-Henning Kamp struct partition *dp; 1459c8223965SMark Murray char lnamebuf[BBSIZE]; 1460425bed3aSJoerg Wunsch int f; 1461b9d05a16SPoul-Henning Kamp u_int secsize, u; 1462b9d05a16SPoul-Henning Kamp off_t mediasize; 1463425bed3aSJoerg Wunsch 1464425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1465e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 146643be698cSBruce Evans return (NULL); 1467425bed3aSJoerg Wunsch } 1468c8223965SMark Murray (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1469c8223965SMark Murray if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1470c8223965SMark Murray warn("cannot open %s", lnamebuf); 147143be698cSBruce Evans return (NULL); 1472425bed3aSJoerg Wunsch } 1473ff7d5162SJordan K. Hubbard 1474b9d05a16SPoul-Henning Kamp /* New world order */ 1475b9d05a16SPoul-Henning Kamp if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1476b9d05a16SPoul-Henning Kamp (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1477425bed3aSJoerg Wunsch close (f); 147843be698cSBruce Evans return (NULL); 1479425bed3aSJoerg Wunsch } 1480b9d05a16SPoul-Henning Kamp memset(&loclab, 0, sizeof loclab); 1481b9d05a16SPoul-Henning Kamp loclab.d_magic = DISKMAGIC; 1482b9d05a16SPoul-Henning Kamp loclab.d_magic2 = DISKMAGIC; 1483b9d05a16SPoul-Henning Kamp loclab.d_secsize = secsize; 1484b9d05a16SPoul-Henning Kamp loclab.d_secperunit = mediasize / secsize; 1485b9d05a16SPoul-Henning Kamp 1486b9d05a16SPoul-Henning Kamp /* 1487b9d05a16SPoul-Henning Kamp * Nobody in these enligthened days uses the CHS geometry for 1488b9d05a16SPoul-Henning Kamp * anything, but nontheless try to get it right. If we fail 1489b9d05a16SPoul-Henning Kamp * to get any good ideas from the device, construct something 1490b9d05a16SPoul-Henning Kamp * which is IBM-PC friendly. 1491b9d05a16SPoul-Henning Kamp */ 1492b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1493b9d05a16SPoul-Henning Kamp loclab.d_nsectors = u; 1494b9d05a16SPoul-Henning Kamp else 1495b9d05a16SPoul-Henning Kamp loclab.d_nsectors = 63; 1496b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1497b9d05a16SPoul-Henning Kamp loclab.d_ntracks = u; 1498b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*1*1024) 1499b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 1; 1500b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*16*1024) 1501b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 16; 1502b9d05a16SPoul-Henning Kamp else 1503b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 255; 1504b9d05a16SPoul-Henning Kamp loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1505b9d05a16SPoul-Henning Kamp loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1506b9d05a16SPoul-Henning Kamp loclab.d_npartitions = MAXPARTITIONS; 1507b9d05a16SPoul-Henning Kamp 1508b9d05a16SPoul-Henning Kamp /* Various (unneeded) compat stuff */ 1509b9d05a16SPoul-Henning Kamp loclab.d_rpm = 3600; 1510b9d05a16SPoul-Henning Kamp loclab.d_bbsize = BBSIZE; 1511b9d05a16SPoul-Henning Kamp loclab.d_interleave = 1;; 1512b9d05a16SPoul-Henning Kamp strncpy(loclab.d_typename, "amnesiac", 1513b9d05a16SPoul-Henning Kamp sizeof(loclab.d_typename)); 1514b9d05a16SPoul-Henning Kamp 1515b9d05a16SPoul-Henning Kamp dp = &loclab.d_partitions[RAW_PART]; 1516b9d05a16SPoul-Henning Kamp dp->p_size = loclab.d_secperunit; 1517b9d05a16SPoul-Henning Kamp loclab.d_checksum = dkcksum(&loclab); 1518425bed3aSJoerg Wunsch close (f); 1519c8223965SMark Murray return (&loclab); 1520425bed3aSJoerg Wunsch } 1521425bed3aSJoerg Wunsch 15228fae3551SRodney W. Grimes 15238fae3551SRodney W. Grimes /*VARARGS1*/ 152461de51caSJoerg Wunsch void 1525326c7cdaSWarner Losh Warning(const char *fmt, ...) 15268fae3551SRodney W. Grimes { 152761de51caSJoerg Wunsch va_list ap; 15288fae3551SRodney W. Grimes 15298fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 153061de51caSJoerg Wunsch va_start(ap, fmt); 153161de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 15328fae3551SRodney W. Grimes fprintf(stderr, "\n"); 153361de51caSJoerg Wunsch va_end(ap); 15348fae3551SRodney W. Grimes } 15358fae3551SRodney W. Grimes 153661de51caSJoerg Wunsch void 1537326c7cdaSWarner Losh usage(void) 15388fae3551SRodney W. Grimes { 1539bc33ea1aSRuslan Ermilov 1540bc33ea1aSRuslan Ermilov fprintf(stderr, 1541bc33ea1aSRuslan 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", 15422c60b668SPoul-Henning Kamp "usage: bsdlabel [-r] disk", 1543bef2080aSPhilippe Charnier "\t\t(to read label)", 15442c60b668SPoul-Henning Kamp " bsdlabel -w [-nr] [-m machine] disk type [packid]", 1545bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 15462c60b668SPoul-Henning Kamp " bsdlabel -e [-nr] [-m machine] disk", 1547bef2080aSPhilippe Charnier "\t\t(to edit label)", 15482c60b668SPoul-Henning Kamp " bsdlabel -R [-nr] [-m machine] disk protofile", 1549bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 15502c60b668SPoul-Henning Kamp " bsdlabel -B [-b boot] [-m machine] disk", 1551bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 15522c60b668SPoul-Henning Kamp " bsdlabel -w -B [-n] [-b boot] [-m machine] disk type [packid]", 1553bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 15542c60b668SPoul-Henning Kamp " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile", 155580baf8ceSPoul-Henning Kamp "\t\t(to restore label and install boot program)" 155680baf8ceSPoul-Henning Kamp ); 15578fae3551SRodney W. Grimes exit(1); 15588fae3551SRodney W. Grimes } 1559