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> 676d8c8fabSYoshihiro Takahashi #ifdef PC98 686d8c8fabSYoshihiro Takahashi #include <sys/diskpc98.h> 696d8c8fabSYoshihiro Takahashi #else 703bb24c35SPoul-Henning Kamp #include <sys/diskmbr.h> 716d8c8fabSYoshihiro Takahashi #endif 72fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 73fd43aa1cSDavid E. O'Brien #include <sys/sun_disklabel.h> 74fd43aa1cSDavid E. O'Brien #endif 75c8223965SMark Murray 768fae3551SRodney W. Grimes #include <unistd.h> 778fae3551SRodney W. Grimes #include <string.h> 788fae3551SRodney W. Grimes #include <stdio.h> 7961de51caSJoerg Wunsch #include <stdlib.h> 8061de51caSJoerg Wunsch #include <signal.h> 8161de51caSJoerg Wunsch #include <stdarg.h> 828fae3551SRodney W. Grimes #include <ctype.h> 83bef2080aSPhilippe Charnier #include <err.h> 8437736675SWarner Losh #include <errno.h> 85c8223965SMark Murray 868fae3551SRodney W. Grimes #include "pathnames.h" 878fae3551SRodney W. Grimes 888fae3551SRodney W. Grimes /* 898fae3551SRodney W. Grimes * Disklabel: read and write disklabels. 908fae3551SRodney W. Grimes * The label is usually placed on one of the first sectors of the disk. 918fae3551SRodney W. Grimes * Many machines also place a bootstrap in the same area, 928fae3551SRodney W. Grimes * in which case the label is embedded in the bootstrap. 938fae3551SRodney W. Grimes * The bootstrap source must leave space at the proper offset 948fae3551SRodney W. Grimes * for the label on such machines. 958fae3551SRodney W. Grimes */ 968fae3551SRodney W. Grimes 978fae3551SRodney W. Grimes #ifndef BBSIZE 988fae3551SRodney W. Grimes #define BBSIZE 8192 /* size of boot area, with label */ 998fae3551SRodney W. Grimes #endif 1008fae3551SRodney W. Grimes 1013233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 1023233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 1033233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 1043233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 1053233afaeSJohn W. De Boskey 1063233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 107f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG 2048U 1083233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 1093233afaeSJohn W. De Boskey 11090b8f6baSDavid E. O'Brien #if defined(__i386__) || defined(__ia64__) 11190b8f6baSDavid E. O'Brien #define NUMBOOT 2 11287efda01SBenno Rice #elif defined(__alpha__) || defined(__sparc64__) || defined(__powerpc__) 1138fae3551SRodney W. Grimes #define NUMBOOT 1 1148fae3551SRodney W. Grimes #else 11590b8f6baSDavid E. O'Brien #error I do not know about this architecture. 1168fae3551SRodney W. Grimes #endif 1178fae3551SRodney W. Grimes 118326c7cdaSWarner Losh void makelabel(const char *, const char *, struct disklabel *); 119326c7cdaSWarner Losh int writelabel(int, const char *, struct disklabel *); 120326c7cdaSWarner Losh void l_perror(const char *); 121326c7cdaSWarner Losh struct disklabel *readlabel(int); 122326c7cdaSWarner Losh struct disklabel *makebootarea(char *, struct disklabel *, int); 123326c7cdaSWarner Losh void display(FILE *, const struct disklabel *); 124326c7cdaSWarner Losh int edit(struct disklabel *, int); 125326c7cdaSWarner Losh int editit(void); 126326c7cdaSWarner Losh char *skip(char *); 127326c7cdaSWarner Losh char *word(char *); 128326c7cdaSWarner Losh int getasciilabel(FILE *, struct disklabel *); 12967b46708SIan Dowse int getasciipartspec(char *, struct disklabel *, int, int); 130326c7cdaSWarner Losh int checklabel(struct disklabel *); 131326c7cdaSWarner Losh void setbootflag(struct disklabel *); 132326c7cdaSWarner Losh void Warning(const char *, ...) __printflike(1, 2); 133326c7cdaSWarner Losh void usage(void); 134326c7cdaSWarner Losh struct disklabel *getvirginlabel(void); 13561de51caSJoerg Wunsch 1368fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1378fae3551SRodney W. Grimes #define streq(a,b) (strcmp(a,b) == 0) 1388fae3551SRodney W. Grimes 1398fae3551SRodney W. Grimes char *dkname; 1408fae3551SRodney W. Grimes char *specname; 141aaae3130SKris Kennaway char tmpfil[] = PATH_TMPFILE; 1428fae3551SRodney W. Grimes 1438fae3551SRodney W. Grimes char namebuf[BBSIZE], *np = namebuf; 1448fae3551SRodney W. Grimes struct disklabel lab; 1458fae3551SRodney W. Grimes char bootarea[BBSIZE]; 146c8223965SMark Murray char blank[] = ""; 147c8223965SMark Murray char unknown[] = "unknown"; 1488fae3551SRodney W. Grimes 1493233afaeSJohn W. De Boskey #define MAX_PART ('z') 1503233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 1513233afaeSJohn W. De Boskey char part_size_type[MAX_NUM_PARTS]; 1523233afaeSJohn W. De Boskey char part_offset_type[MAX_NUM_PARTS]; 1533233afaeSJohn W. De Boskey int part_set[MAX_NUM_PARTS]; 1543233afaeSJohn W. De Boskey 1558fae3551SRodney W. Grimes #if NUMBOOT > 0 1568fae3551SRodney W. Grimes int installboot; /* non-zero if we should install a boot program */ 1578fae3551SRodney W. Grimes char *bootbuf; /* pointer to buffer with remainder of boot prog */ 1588fae3551SRodney W. Grimes int bootsize; /* size of remaining boot program */ 1598fae3551SRodney W. Grimes char *xxboot; /* primary boot */ 1608fae3551SRodney W. Grimes char *bootxx; /* secondary boot */ 1618fae3551SRodney W. Grimes char boot0[MAXPATHLEN]; 1628fae3551SRodney W. Grimes char boot1[MAXPATHLEN]; 1638fae3551SRodney W. Grimes #endif 1648fae3551SRodney W. Grimes 1658fae3551SRodney W. Grimes enum { 166f080d33bSPoul-Henning Kamp UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT 1678fae3551SRodney W. Grimes } op = UNSPEC; 1688fae3551SRodney W. Grimes 1698fae3551SRodney W. Grimes int rflag; 1703233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1718fae3551SRodney W. Grimes 172f080d33bSPoul-Henning Kamp #define OPTIONS "BRb:enrs:w" 1738fae3551SRodney W. Grimes 17461de51caSJoerg Wunsch int 175326c7cdaSWarner Losh main(int argc, char *argv[]) 1768fae3551SRodney W. Grimes { 177c1fdb43fSWarner Losh struct disklabel *lp; 1788fae3551SRodney W. Grimes FILE *t; 179f080d33bSPoul-Henning Kamp int ch, f = 0, error = 0; 1808fae3551SRodney W. Grimes char *name = 0; 1818fae3551SRodney W. Grimes 1828d64695cSWarner Losh while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1838fae3551SRodney W. Grimes switch (ch) { 1848fae3551SRodney W. Grimes #if NUMBOOT > 0 1858fae3551SRodney W. Grimes case 'B': 1868fae3551SRodney W. Grimes ++installboot; 1878fae3551SRodney W. Grimes break; 1888fae3551SRodney W. Grimes case 'b': 1898fae3551SRodney W. Grimes xxboot = optarg; 1908fae3551SRodney W. Grimes break; 1918fae3551SRodney W. Grimes #if NUMBOOT > 1 1928fae3551SRodney W. Grimes case 's': 1938fae3551SRodney W. Grimes bootxx = optarg; 1948fae3551SRodney W. Grimes break; 1958fae3551SRodney W. Grimes #endif 1968fae3551SRodney W. Grimes #endif 1973233afaeSJohn W. De Boskey case 'n': 1983233afaeSJohn W. De Boskey disable_write = 1; 1993233afaeSJohn W. De Boskey break; 2008fae3551SRodney W. Grimes case 'R': 2018fae3551SRodney W. Grimes if (op != UNSPEC) 2028fae3551SRodney W. Grimes usage(); 2038fae3551SRodney W. Grimes op = RESTORE; 2048fae3551SRodney W. Grimes break; 2058fae3551SRodney W. Grimes case 'e': 2068fae3551SRodney W. Grimes if (op != UNSPEC) 2078fae3551SRodney W. Grimes usage(); 2088fae3551SRodney W. Grimes op = EDIT; 2098fae3551SRodney W. Grimes break; 2108fae3551SRodney W. Grimes case 'r': 2118fae3551SRodney W. Grimes ++rflag; 2128fae3551SRodney W. Grimes break; 2138fae3551SRodney W. Grimes case 'w': 2148fae3551SRodney W. Grimes if (op != UNSPEC) 2158fae3551SRodney W. Grimes usage(); 2168fae3551SRodney W. Grimes op = WRITE; 2178fae3551SRodney W. Grimes break; 2188fae3551SRodney W. Grimes case '?': 2198fae3551SRodney W. Grimes default: 2208fae3551SRodney W. Grimes usage(); 2218fae3551SRodney W. Grimes } 2228fae3551SRodney W. Grimes argc -= optind; 2238fae3551SRodney W. Grimes argv += optind; 2248fae3551SRodney W. Grimes #if NUMBOOT > 0 2258fae3551SRodney W. Grimes if (installboot) { 2268fae3551SRodney W. Grimes rflag++; 2278fae3551SRodney W. Grimes if (op == UNSPEC) 2288fae3551SRodney W. Grimes op = WRITEBOOT; 2298fae3551SRodney W. Grimes } else { 2308fae3551SRodney W. Grimes if (op == UNSPEC) 2318fae3551SRodney W. Grimes op = READ; 2328fae3551SRodney W. Grimes xxboot = bootxx = 0; 2338fae3551SRodney W. Grimes } 2348fae3551SRodney W. Grimes #else 2358fae3551SRodney W. Grimes if (op == UNSPEC) 2368fae3551SRodney W. Grimes op = READ; 2378fae3551SRodney W. Grimes #endif 2388fae3551SRodney W. Grimes if (argc < 1) 2398fae3551SRodney W. Grimes usage(); 2408fae3551SRodney W. Grimes 2418fae3551SRodney W. Grimes dkname = argv[0]; 2428fae3551SRodney W. Grimes if (dkname[0] != '/') { 24309c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2448fae3551SRodney W. Grimes specname = np; 2458fae3551SRodney W. Grimes np += strlen(specname) + 1; 2468fae3551SRodney W. Grimes } else 2478fae3551SRodney W. Grimes specname = dkname; 2488fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2498fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 25043c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 2518fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 2528fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2538fae3551SRodney W. Grimes } 2548fae3551SRodney W. Grimes if (f < 0) 255bef2080aSPhilippe Charnier err(4, "%s", specname); 2568fae3551SRodney W. Grimes 2578fae3551SRodney W. Grimes switch(op) { 2588fae3551SRodney W. Grimes 2593b3038a6SBill Fumerola case UNSPEC: 2603b3038a6SBill Fumerola break; 2613b3038a6SBill Fumerola 2628fae3551SRodney W. Grimes case EDIT: 2638fae3551SRodney W. Grimes if (argc != 1) 2648fae3551SRodney W. Grimes usage(); 2658fae3551SRodney W. Grimes lp = readlabel(f); 2668fae3551SRodney W. Grimes error = edit(lp, f); 2678fae3551SRodney W. Grimes break; 2688fae3551SRodney W. Grimes 2698fae3551SRodney W. Grimes case READ: 2708fae3551SRodney W. Grimes if (argc != 1) 2718fae3551SRodney W. Grimes usage(); 2728fae3551SRodney W. Grimes lp = readlabel(f); 2738fae3551SRodney W. Grimes display(stdout, lp); 2748fae3551SRodney W. Grimes error = checklabel(lp); 2758fae3551SRodney W. Grimes break; 2768fae3551SRodney W. Grimes 2778fae3551SRodney W. Grimes case RESTORE: 2788fae3551SRodney W. Grimes #if NUMBOOT > 0 2798fae3551SRodney W. Grimes if (installboot && argc == 3) { 2808fae3551SRodney W. Grimes makelabel(argv[2], 0, &lab); 2818fae3551SRodney W. Grimes argc--; 2826cabb348SBruce Evans 2836cabb348SBruce Evans /* 2846cabb348SBruce Evans * We only called makelabel() for its side effect 2856cabb348SBruce Evans * of setting the bootstrap file names. Discard 2866cabb348SBruce Evans * all changes to `lab' so that all values in the 2876cabb348SBruce Evans * final label come from the ASCII label. 2886cabb348SBruce Evans */ 2896cabb348SBruce Evans bzero((char *)&lab, sizeof(lab)); 2908fae3551SRodney W. Grimes } 2918fae3551SRodney W. Grimes #endif 2928fae3551SRodney W. Grimes if (argc != 2) 2938fae3551SRodney W. Grimes usage(); 2948fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 295bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 2966cabb348SBruce Evans if (!getasciilabel(t, &lab)) 2976cabb348SBruce Evans exit(1); 2986cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 2996cabb348SBruce Evans *lp = lab; 3008fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3018fae3551SRodney W. Grimes break; 3028fae3551SRodney W. Grimes 3038fae3551SRodney W. Grimes case WRITE: 3048fae3551SRodney W. Grimes if (argc == 3) { 3058fae3551SRodney W. Grimes name = argv[2]; 3068fae3551SRodney W. Grimes argc--; 3078fae3551SRodney W. Grimes } 3088fae3551SRodney W. Grimes if (argc != 2) 3098fae3551SRodney W. Grimes usage(); 3108fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3118fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3128fae3551SRodney W. Grimes *lp = lab; 3138fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3148fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3158fae3551SRodney W. Grimes break; 3168fae3551SRodney W. Grimes 3178fae3551SRodney W. Grimes #if NUMBOOT > 0 3188fae3551SRodney W. Grimes case WRITEBOOT: 3198fae3551SRodney W. Grimes { 3208fae3551SRodney W. Grimes struct disklabel tlab; 3218fae3551SRodney W. Grimes 3228fae3551SRodney W. Grimes lp = readlabel(f); 3238fae3551SRodney W. Grimes tlab = *lp; 3248fae3551SRodney W. Grimes if (argc == 2) 3258fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3268fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3278fae3551SRodney W. Grimes *lp = tlab; 3288fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3298fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3308fae3551SRodney W. Grimes break; 3318fae3551SRodney W. Grimes } 3328fae3551SRodney W. Grimes #endif 3338fae3551SRodney W. Grimes } 3348fae3551SRodney W. Grimes exit(error); 3358fae3551SRodney W. Grimes } 3368fae3551SRodney W. Grimes 3378fae3551SRodney W. Grimes /* 3388fae3551SRodney W. Grimes * Construct a prototype disklabel from /etc/disktab. As a side 3398fae3551SRodney W. Grimes * effect, set the names of the primary and secondary boot files 3408fae3551SRodney W. Grimes * if specified. 3418fae3551SRodney W. Grimes */ 34261de51caSJoerg Wunsch void 343326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp) 3448fae3551SRodney W. Grimes { 345326c7cdaSWarner Losh struct disklabel *dp; 346425bed3aSJoerg Wunsch 347425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 348425bed3aSJoerg Wunsch dp = getvirginlabel(); 349425bed3aSJoerg Wunsch else 3508fae3551SRodney W. Grimes dp = getdiskbyname(type); 3516bd343a9SPhilippe Charnier if (dp == NULL) 3526bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3538fae3551SRodney W. Grimes *lp = *dp; 3548fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 3558fae3551SRodney W. Grimes if (name) 3568fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 3578fae3551SRodney W. Grimes } 3588fae3551SRodney W. Grimes 35961de51caSJoerg Wunsch int 360326c7cdaSWarner Losh writelabel(int f, const char *boot, struct disklabel *lp) 3618fae3551SRodney W. Grimes { 362130cd73aSDoug Rabson #ifdef __alpha__ 363130cd73aSDoug Rabson u_long *p, sum; 364130cd73aSDoug Rabson int i; 365130cd73aSDoug Rabson #endif 366fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 367fd43aa1cSDavid E. O'Brien struct sun_disklabel *sl; 368fd43aa1cSDavid E. O'Brien u_short cksum, *sp1, *sp2; 369fd43aa1cSDavid E. O'Brien struct partition *npp; 370fd43aa1cSDavid E. O'Brien struct sun_dkpart *spp; 371fd43aa1cSDavid E. O'Brien int i, secpercyl; 372cc18e4ccSBruce Evans #endif 373130cd73aSDoug Rabson 3743233afaeSJohn W. De Boskey if (disable_write) { 3753233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 3763233afaeSJohn W. De Boskey display(stdout, lp); 3773233afaeSJohn W. De Boskey return (0); 37880baf8ceSPoul-Henning Kamp } 37980baf8ceSPoul-Henning Kamp 3808fae3551SRodney W. Grimes setbootflag(lp); 3818fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 3828fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 3838fae3551SRodney W. Grimes lp->d_checksum = 0; 3848fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 38580baf8ceSPoul-Henning Kamp if (!rflag) { 38680baf8ceSPoul-Henning Kamp if (ioctl(f, DIOCWDINFO, lp) < 0) { 38780baf8ceSPoul-Henning Kamp l_perror("ioctl DIOCWDINFO"); 38880baf8ceSPoul-Henning Kamp return (1); 38980baf8ceSPoul-Henning Kamp } 39080baf8ceSPoul-Henning Kamp return (0); 39180baf8ceSPoul-Henning Kamp } 39280baf8ceSPoul-Henning Kamp 3938fae3551SRodney W. Grimes /* 3948fae3551SRodney W. Grimes * First set the kernel disk label, 3958fae3551SRodney W. Grimes * then write a label to the raw disk. 3968fae3551SRodney W. Grimes * If the SDINFO ioctl fails because it is unimplemented, 3978fae3551SRodney W. Grimes * keep going; otherwise, the kernel consistency checks 3988fae3551SRodney W. Grimes * may prevent us from changing the current (in-core) 3998fae3551SRodney W. Grimes * label. 4008fae3551SRodney W. Grimes */ 4018fae3551SRodney W. Grimes if (ioctl(f, DIOCSDINFO, lp) < 0 && 4028fae3551SRodney W. Grimes errno != ENODEV && errno != ENOTTY) { 4038fae3551SRodney W. Grimes l_perror("ioctl DIOCSDINFO"); 4048fae3551SRodney W. Grimes return (1); 4058fae3551SRodney W. Grimes } 4068fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 407130cd73aSDoug Rabson 408130cd73aSDoug Rabson #ifdef __alpha__ 409cc18e4ccSBruce Evans /* 410cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 411cc18e4ccSBruce Evans */ 412130cd73aSDoug Rabson for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) 413130cd73aSDoug Rabson sum += p[i]; 414130cd73aSDoug Rabson p[63] = sum; 415130cd73aSDoug Rabson #endif 416fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 417fd43aa1cSDavid E. O'Brien /* 418fd43aa1cSDavid E. O'Brien * Generate a Sun disklabel around the BSD label for 419fd43aa1cSDavid E. O'Brien * PROM compatability. 420fd43aa1cSDavid E. O'Brien */ 421fd43aa1cSDavid E. O'Brien sl = (struct sun_disklabel *)boot; 422fd43aa1cSDavid E. O'Brien memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname)); 423fd43aa1cSDavid E. O'Brien sl->sl_rpm = lp->d_rpm; 424fd43aa1cSDavid E. O'Brien sl->sl_pcylinders = lp->d_ncylinders + 425fd43aa1cSDavid E. O'Brien lp->d_acylinders; /* XXX */ 426fd43aa1cSDavid E. O'Brien sl->sl_sparespercyl = lp->d_sparespercyl; 427fd43aa1cSDavid E. O'Brien sl->sl_interleave = lp->d_interleave; 428fd43aa1cSDavid E. O'Brien sl->sl_ncylinders = lp->d_ncylinders; 429fd43aa1cSDavid E. O'Brien sl->sl_acylinders = lp->d_acylinders; 430fd43aa1cSDavid E. O'Brien sl->sl_ntracks = lp->d_ntracks; 431fd43aa1cSDavid E. O'Brien sl->sl_nsectors = lp->d_nsectors; 432fd43aa1cSDavid E. O'Brien sl->sl_magic = SUN_DKMAGIC; 433fd43aa1cSDavid E. O'Brien secpercyl = sl->sl_nsectors * sl->sl_ntracks; 434fd43aa1cSDavid E. O'Brien for (i = 0; i < 8; i++) { 435fd43aa1cSDavid E. O'Brien spp = &sl->sl_part[i]; 436fd43aa1cSDavid E. O'Brien npp = &lp->d_partitions[i]; 437fd43aa1cSDavid E. O'Brien /* 438fd43aa1cSDavid E. O'Brien * SunOS partitions must start on a cylinder 439fd43aa1cSDavid E. O'Brien * boundary. Note this restriction is forced 440fd43aa1cSDavid E. O'Brien * upon FreeBSD/sparc64 labels too, since we 441fd43aa1cSDavid E. O'Brien * want to keep both labels synchronised. 442fd43aa1cSDavid E. O'Brien */ 443fd43aa1cSDavid E. O'Brien spp->sdkp_cyloffset = npp->p_offset / secpercyl; 444fd43aa1cSDavid E. O'Brien spp->sdkp_nsectors = npp->p_size; 445fd43aa1cSDavid E. O'Brien } 446130cd73aSDoug Rabson 447fd43aa1cSDavid E. O'Brien /* Compute the XOR checksum. */ 448fd43aa1cSDavid E. O'Brien sp1 = (u_short *)sl; 449fd43aa1cSDavid E. O'Brien sp2 = (u_short *)(sl + 1); 450fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum = 0; 451fd43aa1cSDavid E. O'Brien while (sp1 < sp2) 452fd43aa1cSDavid E. O'Brien cksum ^= *sp1++; 453fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum; 454fd43aa1cSDavid E. O'Brien #endif 455c8223965SMark Murray if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) { 4566bd343a9SPhilippe Charnier warn("write"); 4578fae3551SRodney W. Grimes return (1); 4588fae3551SRodney W. Grimes } 4598fae3551SRodney W. Grimes #if NUMBOOT > 0 4608fae3551SRodney W. Grimes /* 4618fae3551SRodney W. Grimes * Output the remainder of the disklabel 4628fae3551SRodney W. Grimes */ 4638fae3551SRodney W. Grimes if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 4646bd343a9SPhilippe Charnier warn("write"); 4658fae3551SRodney W. Grimes return(1); 4668fae3551SRodney W. Grimes } 4678fae3551SRodney W. Grimes #endif 4688fae3551SRodney W. Grimes return (0); 4698fae3551SRodney W. Grimes } 4708fae3551SRodney W. Grimes 47161de51caSJoerg Wunsch void 472326c7cdaSWarner Losh l_perror(const char *s) 4738fae3551SRodney W. Grimes { 4746bd343a9SPhilippe Charnier switch (errno) { 4758fae3551SRodney W. Grimes 4768fae3551SRodney W. Grimes case ESRCH: 4773121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 4780b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 4798fae3551SRodney W. Grimes break; 4808fae3551SRodney W. Grimes 4818fae3551SRodney W. Grimes case EINVAL: 4823121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 4833121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 4848fae3551SRodney W. Grimes break; 4858fae3551SRodney W. Grimes 4868fae3551SRodney W. Grimes case EBUSY: 4876bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 4888fae3551SRodney W. Grimes break; 4898fae3551SRodney W. Grimes 4908fae3551SRodney W. Grimes case EXDEV: 4917de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 4927de06420SBruce Evans s, 'a' + RAW_PART); 4938fae3551SRodney W. Grimes break; 4948fae3551SRodney W. Grimes 4958fae3551SRodney W. Grimes default: 4966bd343a9SPhilippe Charnier warn((char *)NULL); 4978fae3551SRodney W. Grimes break; 4988fae3551SRodney W. Grimes } 4998fae3551SRodney W. Grimes } 5008fae3551SRodney W. Grimes 5018fae3551SRodney W. Grimes /* 5028fae3551SRodney W. Grimes * Fetch disklabel for disk. 5038fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 5048fae3551SRodney W. Grimes */ 5058fae3551SRodney W. Grimes struct disklabel * 506326c7cdaSWarner Losh readlabel(int f) 5078fae3551SRodney W. Grimes { 508326c7cdaSWarner Losh struct disklabel *lp; 5098fae3551SRodney W. Grimes 5108fae3551SRodney W. Grimes if (rflag) { 5118fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 512bef2080aSPhilippe Charnier err(4, "%s", specname); 5138fae3551SRodney W. Grimes for (lp = (struct disklabel *)bootarea; 5148fae3551SRodney W. Grimes lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 5158fae3551SRodney W. Grimes lp = (struct disklabel *)((char *)lp + 16)) 5168fae3551SRodney W. Grimes if (lp->d_magic == DISKMAGIC && 5178fae3551SRodney W. Grimes lp->d_magic2 == DISKMAGIC) 5188fae3551SRodney W. Grimes break; 5198fae3551SRodney W. Grimes if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 5208fae3551SRodney W. Grimes lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 5213121d4cbSPhilippe Charnier dkcksum(lp) != 0) 5223121d4cbSPhilippe Charnier errx(1, 5233121d4cbSPhilippe Charnier "bad pack magic number (label is damaged, or pack is unlabeled)"); 5248fae3551SRodney W. Grimes } else { 5258fae3551SRodney W. Grimes lp = &lab; 5268fae3551SRodney W. Grimes if (ioctl(f, DIOCGDINFO, lp) < 0) 527bef2080aSPhilippe Charnier err(4, "ioctl DIOCGDINFO"); 5288fae3551SRodney W. Grimes } 5298fae3551SRodney W. Grimes return (lp); 5308fae3551SRodney W. Grimes } 5318fae3551SRodney W. Grimes 5328fae3551SRodney W. Grimes /* 5338fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 5348fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 5358fae3551SRodney W. Grimes */ 5368fae3551SRodney W. Grimes struct disklabel * 537326c7cdaSWarner Losh makebootarea(char *boot, struct disklabel *dp, int f) 5388fae3551SRodney W. Grimes { 53921c729c2SBruce Evans struct disklabel *lp; 540326c7cdaSWarner Losh char *p; 5418fae3551SRodney W. Grimes int b; 5428fae3551SRodney W. Grimes #if NUMBOOT > 0 5438fae3551SRodney W. Grimes char *dkbasename; 5448fae3551SRodney W. Grimes struct stat sb; 545cc18e4ccSBruce Evans #endif 546cc18e4ccSBruce Evans #ifdef __alpha__ 54721c729c2SBruce Evans u_long *bootinfo; 548cc18e4ccSBruce Evans int n; 549cc18e4ccSBruce Evans #endif 550d70e4e53SJoerg Wunsch #ifdef __i386__ 551d70e4e53SJoerg Wunsch char *tmpbuf; 552d70e4e53SJoerg Wunsch int i, found; 55361de51caSJoerg Wunsch #endif 5548fae3551SRodney W. Grimes 5558fae3551SRodney W. Grimes /* XXX */ 5568fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 5578fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 5588fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 5598fae3551SRodney W. Grimes } 5608fae3551SRodney W. Grimes lp = (struct disklabel *) 5618fae3551SRodney W. Grimes (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 5628fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5638fae3551SRodney W. Grimes #if NUMBOOT > 0 5648fae3551SRodney W. Grimes /* 5658fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 5668fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 5678fae3551SRodney W. Grimes * clobber the existing boot. 5688fae3551SRodney W. Grimes */ 5698fae3551SRodney W. Grimes if (!installboot) { 5708fae3551SRodney W. Grimes if (rflag) { 5718fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 572bef2080aSPhilippe Charnier err(4, "%s", specname); 5738fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5748fae3551SRodney W. Grimes } 5758fae3551SRodney W. Grimes return (lp); 5768fae3551SRodney W. Grimes } 5778fae3551SRodney W. Grimes /* 5788fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 5798fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 5808fae3551SRodney W. Grimes */ 5818fae3551SRodney W. Grimes if (!xxboot || !bootxx) { 5828fae3551SRodney W. Grimes dkbasename = np; 5838fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 5848fae3551SRodney W. Grimes p = dkname; 5858fae3551SRodney W. Grimes else 5868fae3551SRodney W. Grimes p++; 5878fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 5888fae3551SRodney W. Grimes *np++ = *p++; 5898fae3551SRodney W. Grimes *np++ = '\0'; 5908fae3551SRodney W. Grimes 5918fae3551SRodney W. Grimes if (!xxboot) { 592efba76d7SJordan K. Hubbard (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); 593efba76d7SJordan K. Hubbard xxboot = boot0; 5948fae3551SRodney W. Grimes } 5958fae3551SRodney W. Grimes #if NUMBOOT > 1 5968fae3551SRodney W. Grimes if (!bootxx) { 597efba76d7SJordan K. Hubbard (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); 598efba76d7SJordan K. Hubbard bootxx = boot1; 5998fae3551SRodney W. Grimes } 6008fae3551SRodney W. Grimes #endif 6018fae3551SRodney W. Grimes } 6028fae3551SRodney W. Grimes 6038fae3551SRodney W. Grimes /* 6048fae3551SRodney W. Grimes * Strange rules: 605fd43aa1cSDavid E. O'Brien * 1. One-piece bootstrap (hp300/hp800) 60690b8f6baSDavid E. O'Brien * 1. One-piece bootstrap (alpha/sparc64) 6078fae3551SRodney W. Grimes * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 6088fae3551SRodney W. Grimes * is remembered and written later following the bootarea. 609fd43aa1cSDavid E. O'Brien * 2. Two-piece bootstraps (i386/ia64) 6108fae3551SRodney W. Grimes * up to d_secsize bytes of ``xxboot'' go in first d_secsize 6118fae3551SRodney W. Grimes * bytes of bootarea, remaining d_bbsize-d_secsize filled 6128fae3551SRodney W. Grimes * from ``bootxx''. 6138fae3551SRodney W. Grimes */ 6148fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 6158fae3551SRodney W. Grimes if (b < 0) 616bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6178fae3551SRodney W. Grimes #if NUMBOOT > 1 618d70e4e53SJoerg Wunsch #ifdef __i386__ 619d70e4e53SJoerg Wunsch /* 620d70e4e53SJoerg Wunsch * XXX Botch alert. 621d70e4e53SJoerg Wunsch * The i386 has the so-called fdisk table embedded into the 622d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 623d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 624d70e4e53SJoerg Wunsch * the xxboot provides a template.) 625d70e4e53SJoerg Wunsch */ 626d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 627bef2080aSPhilippe Charnier err(4, "%s", xxboot); 628d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 629448f7629SAlfred Perlstein #endif /* __i386__ */ 6308fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_secsize) < 0) 631bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6328fae3551SRodney W. Grimes (void)close(b); 633404a379eSYoshihiro Takahashi #ifdef PC98 634404a379eSYoshihiro Takahashi for (i = DOSPARTOFF, found = 0; 635404a379eSYoshihiro Takahashi !found && i < (int)(DOSPARTOFF + NDOSPART * sizeof(struct pc98_partition)); 636404a379eSYoshihiro Takahashi i++) 637404a379eSYoshihiro Takahashi found = tmpbuf[i] != 0; 638404a379eSYoshihiro Takahashi if (found) 639404a379eSYoshihiro Takahashi memcpy((void *)&boot[DOSPARTOFF], 640404a379eSYoshihiro Takahashi (void *)&tmpbuf[DOSPARTOFF], 641404a379eSYoshihiro Takahashi NDOSPART * sizeof(struct pc98_partition)); 642404a379eSYoshihiro Takahashi free(tmpbuf); 643404a379eSYoshihiro Takahashi #elif defined(__i386__) 644d70e4e53SJoerg Wunsch for (i = DOSPARTOFF, found = 0; 645c8223965SMark Murray !found && i < (int)(DOSPARTOFF + NDOSPART*sizeof(struct dos_partition)); 646d70e4e53SJoerg Wunsch i++) 647d70e4e53SJoerg Wunsch found = tmpbuf[i] != 0; 648d70e4e53SJoerg Wunsch if (found) 649d70e4e53SJoerg Wunsch memcpy((void *)&boot[DOSPARTOFF], 650d70e4e53SJoerg Wunsch (void *)&tmpbuf[DOSPARTOFF], 651d70e4e53SJoerg Wunsch NDOSPART * sizeof(struct dos_partition)); 652d70e4e53SJoerg Wunsch free(tmpbuf); 653448f7629SAlfred Perlstein #endif /* __i386__ */ 6548fae3551SRodney W. Grimes b = open(bootxx, O_RDONLY); 6558fae3551SRodney W. Grimes if (b < 0) 656bef2080aSPhilippe Charnier err(4, "%s", bootxx); 6576cabb348SBruce Evans if (fstat(b, &sb) != 0) 6586cabb348SBruce Evans err(4, "%s", bootxx); 6596cabb348SBruce Evans if (dp->d_secsize + sb.st_size > dp->d_bbsize) 6606cabb348SBruce Evans errx(4, "%s too large", bootxx); 66161de51caSJoerg Wunsch if (read(b, &boot[dp->d_secsize], 66261de51caSJoerg Wunsch (int)(dp->d_bbsize-dp->d_secsize)) < 0) 663bef2080aSPhilippe Charnier err(4, "%s", bootxx); 664cc18e4ccSBruce Evans #else /* !(NUMBOOT > 1) */ 665130cd73aSDoug Rabson #ifdef __alpha__ 666130cd73aSDoug Rabson /* 667130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 668130cd73aSDoug Rabson * second sector of the boot area. The first sector 669130cd73aSDoug Rabson * contains the label and must be edited to contain the 670130cd73aSDoug Rabson * size and location of the primary bootstrap. 671130cd73aSDoug Rabson */ 672cc18e4ccSBruce Evans n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 673130cd73aSDoug Rabson if (n < 0) 674130cd73aSDoug Rabson err(4, "%s", xxboot); 67521c729c2SBruce Evans bootinfo = (u_long *)(boot + 480); 67621c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 67721c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 67821c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 679cc18e4ccSBruce Evans #else /* !__alpha__ */ 6808fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_bbsize) < 0) 681bef2080aSPhilippe Charnier err(4, "%s", xxboot); 682cc18e4ccSBruce Evans #endif /* __alpha__ */ 6836cabb348SBruce Evans if (fstat(b, &sb) != 0) 6846cabb348SBruce Evans err(4, "%s", xxboot); 6858fae3551SRodney W. Grimes bootsize = (int)sb.st_size - dp->d_bbsize; 6868fae3551SRodney W. Grimes if (bootsize > 0) { 6878fae3551SRodney W. Grimes /* XXX assume d_secsize is a power of two */ 6888fae3551SRodney W. Grimes bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 6898fae3551SRodney W. Grimes bootbuf = (char *)malloc((size_t)bootsize); 6908fae3551SRodney W. Grimes if (bootbuf == 0) 691bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6928fae3551SRodney W. Grimes if (read(b, bootbuf, bootsize) < 0) { 6938fae3551SRodney W. Grimes free(bootbuf); 694bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6958fae3551SRodney W. Grimes } 6968fae3551SRodney W. Grimes } 697cc18e4ccSBruce Evans #endif /* NUMBOOT > 1 */ 6988fae3551SRodney W. Grimes (void)close(b); 699cc18e4ccSBruce Evans #endif /* NUMBOOT > 0 */ 7008fae3551SRodney W. Grimes /* 7018fae3551SRodney W. Grimes * Make sure no part of the bootstrap is written in the area 7028fae3551SRodney W. Grimes * reserved for the label. 7038fae3551SRodney W. Grimes */ 7048fae3551SRodney W. Grimes for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 7056bd343a9SPhilippe Charnier if (*p) 7066bd343a9SPhilippe Charnier errx(2, "bootstrap doesn't leave room for disk label"); 7078fae3551SRodney W. Grimes return (lp); 7088fae3551SRodney W. Grimes } 7098fae3551SRodney W. Grimes 71061de51caSJoerg Wunsch void 711326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 7128fae3551SRodney W. Grimes { 713326c7cdaSWarner Losh int i, j; 714326c7cdaSWarner Losh const struct partition *pp; 7158fae3551SRodney W. Grimes 7168fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 717484c7804SJulian Elischer if (lp->d_type < DKMAXTYPES) 7188fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 7198fae3551SRodney W. Grimes else 7202a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 72161de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 72261de51caSJoerg Wunsch lp->d_typename); 72361de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 72461de51caSJoerg Wunsch lp->d_packname); 7258fae3551SRodney W. Grimes fprintf(f, "flags:"); 7268fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 7278fae3551SRodney W. Grimes fprintf(f, " removeable"); 7288fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 7298fae3551SRodney W. Grimes fprintf(f, " ecc"); 7308fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 7318fae3551SRodney W. Grimes fprintf(f, " badsect"); 7328fae3551SRodney W. Grimes fprintf(f, "\n"); 7332a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 7342a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 7352a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 7362a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 7372a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 7382a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 7392a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 7402a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 7412a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 7422a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 7432a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 7442a1deaaaSBruce Evans (u_long)lp->d_headswitch); 74561de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 7462a1deaaaSBruce Evans (u_long)lp->d_trkseek); 7478fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 7488fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 7498fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 7508fae3551SRodney W. Grimes break; 7518fae3551SRodney W. Grimes if (i < 0) 7528fae3551SRodney W. Grimes i = 0; 7538fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 7542a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 7552a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 7568fae3551SRodney W. Grimes fprintf(f, 757ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 7588fae3551SRodney W. Grimes pp = lp->d_partitions; 7598fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 7608fae3551SRodney W. Grimes if (pp->p_size) { 7612a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 7622a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 763484c7804SJulian Elischer if (pp->p_fstype < FSMAXTYPES) 7648fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 7658fae3551SRodney W. Grimes else 7668fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 7678fae3551SRodney W. Grimes switch (pp->p_fstype) { 7688fae3551SRodney W. Grimes 7698fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 7702a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 7712a1deaaaSBruce Evans (u_long)pp->p_fsize, 7722a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 7738fae3551SRodney W. Grimes break; 7748fae3551SRodney W. Grimes 7758fae3551SRodney W. Grimes case FS_BSDFFS: 7762a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 7772a1deaaaSBruce Evans (u_long)pp->p_fsize, 7782a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 7798fae3551SRodney W. Grimes pp->p_cpg); 7808fae3551SRodney W. Grimes break; 7818fae3551SRodney W. Grimes 782ca4693edSJustin T. Gibbs case FS_BSDLFS: 7832a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 7842a1deaaaSBruce Evans (u_long)pp->p_fsize, 7852a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 786ca4693edSJustin T. Gibbs pp->p_cpg); 787ca4693edSJustin T. Gibbs break; 788ca4693edSJustin T. Gibbs 7898fae3551SRodney W. Grimes default: 7908fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 7918fae3551SRodney W. Grimes break; 7928fae3551SRodney W. Grimes } 7932a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 7942a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 7958fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 7968fae3551SRodney W. Grimes putc('*', f); 7978fae3551SRodney W. Grimes else 7988fae3551SRodney W. Grimes putc(' ', f); 7992a1deaaaSBruce Evans fprintf(f, "- %lu", 8002a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 8012a1deaaaSBruce Evans lp->d_secpercyl - 1) / 8022a1deaaaSBruce Evans lp->d_secpercyl - 1)); 8038fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 8048fae3551SRodney W. Grimes putc('*', f); 8058fae3551SRodney W. Grimes fprintf(f, ")\n"); 8068fae3551SRodney W. Grimes } 8078fae3551SRodney W. Grimes } 8088fae3551SRodney W. Grimes fflush(f); 8098fae3551SRodney W. Grimes } 8108fae3551SRodney W. Grimes 81161de51caSJoerg Wunsch int 812326c7cdaSWarner Losh edit(struct disklabel *lp, int f) 8138fae3551SRodney W. Grimes { 814326c7cdaSWarner Losh int c, fd; 8158fae3551SRodney W. Grimes struct disklabel label; 816722ceb3fSWarner Losh FILE *fp; 8178fae3551SRodney W. Grimes 818722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 819722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 8206bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 8218fae3551SRodney W. Grimes return (1); 8228fae3551SRodney W. Grimes } 823722ceb3fSWarner Losh display(fp, lp); 824722ceb3fSWarner Losh fclose(fp); 8258fae3551SRodney W. Grimes for (;;) { 8268fae3551SRodney W. Grimes if (!editit()) 8278fae3551SRodney W. Grimes break; 828722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 829722ceb3fSWarner Losh if (fp == NULL) { 8306bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 8318fae3551SRodney W. Grimes break; 8328fae3551SRodney W. Grimes } 8338fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 834722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 8358fae3551SRodney W. Grimes *lp = label; 8368fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 837722ceb3fSWarner Losh fclose(fp); 8388fae3551SRodney W. Grimes (void) unlink(tmpfil); 8398fae3551SRodney W. Grimes return (0); 8408fae3551SRodney W. Grimes } 8418fae3551SRodney W. Grimes } 842722ceb3fSWarner Losh fclose(fp); 8438fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 8448fae3551SRodney W. Grimes c = getchar(); 8458fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 8468fae3551SRodney W. Grimes while (getchar() != (int)'\n') 8478fae3551SRodney W. Grimes ; 8488fae3551SRodney W. Grimes if (c == (int)'n') 8498fae3551SRodney W. Grimes break; 8508fae3551SRodney W. Grimes } 8518fae3551SRodney W. Grimes (void) unlink(tmpfil); 8528fae3551SRodney W. Grimes return (1); 8538fae3551SRodney W. Grimes } 8548fae3551SRodney W. Grimes 85561de51caSJoerg Wunsch int 856326c7cdaSWarner Losh editit(void) 8578fae3551SRodney W. Grimes { 858326c7cdaSWarner Losh int pid, xpid; 859c8223965SMark Murray int locstat, omask; 860c8223965SMark Murray const char *ed; 8618fae3551SRodney W. Grimes 8628fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 8638fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 8648fae3551SRodney W. Grimes if (errno == EPROCLIM) { 8656bd343a9SPhilippe Charnier warnx("you have too many processes"); 8668fae3551SRodney W. Grimes return(0); 8678fae3551SRodney W. Grimes } 8688fae3551SRodney W. Grimes if (errno != EAGAIN) { 8696bd343a9SPhilippe Charnier warn("fork"); 8708fae3551SRodney W. Grimes return(0); 8718fae3551SRodney W. Grimes } 8728fae3551SRodney W. Grimes sleep(1); 8738fae3551SRodney W. Grimes } 8748fae3551SRodney W. Grimes if (pid == 0) { 8758fae3551SRodney W. Grimes sigsetmask(omask); 8768fae3551SRodney W. Grimes setgid(getgid()); 8778fae3551SRodney W. Grimes setuid(getuid()); 8788fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 8798fae3551SRodney W. Grimes ed = DEFEDITOR; 8807bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 8816bd343a9SPhilippe Charnier err(1, "%s", ed); 8828fae3551SRodney W. Grimes } 883c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 8848fae3551SRodney W. Grimes if (xpid == pid) 8858fae3551SRodney W. Grimes break; 8868fae3551SRodney W. Grimes sigsetmask(omask); 887c8223965SMark Murray return(!locstat); 8888fae3551SRodney W. Grimes } 8898fae3551SRodney W. Grimes 8908fae3551SRodney W. Grimes char * 891326c7cdaSWarner Losh skip(char *cp) 8928fae3551SRodney W. Grimes { 8938fae3551SRodney W. Grimes 8948fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 8958fae3551SRodney W. Grimes cp++; 8968fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 897326c7cdaSWarner Losh return (NULL); 8988fae3551SRodney W. Grimes return (cp); 8998fae3551SRodney W. Grimes } 9008fae3551SRodney W. Grimes 9018fae3551SRodney W. Grimes char * 902326c7cdaSWarner Losh word(char *cp) 9038fae3551SRodney W. Grimes { 904326c7cdaSWarner Losh char c; 9058fae3551SRodney W. Grimes 9068fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 9078fae3551SRodney W. Grimes cp++; 9088fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 9098fae3551SRodney W. Grimes *cp++ = '\0'; 9108fae3551SRodney W. Grimes if (c != '#') 9118fae3551SRodney W. Grimes return (skip(cp)); 9128fae3551SRodney W. Grimes } 913326c7cdaSWarner Losh return (NULL); 9148fae3551SRodney W. Grimes } 9158fae3551SRodney W. Grimes 9168fae3551SRodney W. Grimes /* 9178fae3551SRodney W. Grimes * Read an ascii label in from fd f, 9188fae3551SRodney W. Grimes * in the same format as that put out by display(), 9198fae3551SRodney W. Grimes * and fill in lp. 9208fae3551SRodney W. Grimes */ 92161de51caSJoerg Wunsch int 922326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 9238fae3551SRodney W. Grimes { 9246b0ff5f5SPoul-Henning Kamp char *cp; 9256b0ff5f5SPoul-Henning Kamp const char **cpp; 926484c7804SJulian Elischer u_int part; 9276b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 928484c7804SJulian Elischer u_long v; 929484c7804SJulian Elischer int lineno = 0, errors = 0; 930326c7cdaSWarner Losh int i; 9318fae3551SRodney W. Grimes 932b0459c58SDag-Erling Smørgrav bzero(&part_set, sizeof(part_set)); 933b0459c58SDag-Erling Smørgrav bzero(&part_size_type, sizeof(part_size_type)); 934b0459c58SDag-Erling Smørgrav bzero(&part_offset_type, sizeof(part_offset_type)); 9358fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 93677068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 9378fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 9388fae3551SRodney W. Grimes lineno++; 93961de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 9408fae3551SRodney W. Grimes *cp = '\0'; 9418fae3551SRodney W. Grimes cp = skip(line); 9428fae3551SRodney W. Grimes if (cp == NULL) 9438fae3551SRodney W. Grimes continue; 9448fae3551SRodney W. Grimes tp = index(cp, ':'); 9458fae3551SRodney W. Grimes if (tp == NULL) { 9468fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 9478fae3551SRodney W. Grimes errors++; 9488fae3551SRodney W. Grimes continue; 9498fae3551SRodney W. Grimes } 9508fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 9518fae3551SRodney W. Grimes if (streq(cp, "type")) { 9528fae3551SRodney W. Grimes if (tp == NULL) 953c8223965SMark Murray tp = unknown; 9548fae3551SRodney W. Grimes cpp = dktypenames; 9558fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 9566b0ff5f5SPoul-Henning Kamp if (*cpp && streq(*cpp, tp)) { 9578fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 95809dbd070SIan Dowse break; 9598fae3551SRodney W. Grimes } 96009dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 96109dbd070SIan Dowse continue; 962484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 963484c7804SJulian Elischer if (v >= DKMAXTYPES) 964484c7804SJulian Elischer fprintf(stderr, "line %d:%s %lu\n", lineno, 9658fae3551SRodney W. Grimes "Warning, unknown disk type", v); 9668fae3551SRodney W. Grimes lp->d_type = v; 9678fae3551SRodney W. Grimes continue; 9688fae3551SRodney W. Grimes } 9698fae3551SRodney W. Grimes if (streq(cp, "flags")) { 9708fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 9718fae3551SRodney W. Grimes tp = word(cp); 9728fae3551SRodney W. Grimes if (streq(cp, "removeable")) 9738fae3551SRodney W. Grimes v |= D_REMOVABLE; 9748fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 9758fae3551SRodney W. Grimes v |= D_ECC; 9768fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 9778fae3551SRodney W. Grimes v |= D_BADSECT; 9788fae3551SRodney W. Grimes else { 9798fae3551SRodney W. Grimes fprintf(stderr, 9808fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 9818fae3551SRodney W. Grimes lineno, cp); 9828fae3551SRodney W. Grimes errors++; 9838fae3551SRodney W. Grimes } 9848fae3551SRodney W. Grimes } 9858fae3551SRodney W. Grimes lp->d_flags = v; 9868fae3551SRodney W. Grimes continue; 9878fae3551SRodney W. Grimes } 9888fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 9898fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 990484c7804SJulian Elischer lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 9918fae3551SRodney W. Grimes tp = word(cp); 9928fae3551SRodney W. Grimes } 9938fae3551SRodney W. Grimes continue; 9948fae3551SRodney W. Grimes } 995484c7804SJulian Elischer if (sscanf(cp, "%lu partitions", &v) == 1) { 996484c7804SJulian Elischer if (v == 0 || v > MAXPARTITIONS) { 9978fae3551SRodney W. Grimes fprintf(stderr, 9988fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 9998fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 10008fae3551SRodney W. Grimes errors++; 10018fae3551SRodney W. Grimes } else 10028fae3551SRodney W. Grimes lp->d_npartitions = v; 10038fae3551SRodney W. Grimes continue; 10048fae3551SRodney W. Grimes } 10058fae3551SRodney W. Grimes if (tp == NULL) 1006c8223965SMark Murray tp = blank; 10078fae3551SRodney W. Grimes if (streq(cp, "disk")) { 10088fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 10098fae3551SRodney W. Grimes continue; 10108fae3551SRodney W. Grimes } 10118fae3551SRodney W. Grimes if (streq(cp, "label")) { 10128fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 10138fae3551SRodney W. Grimes continue; 10148fae3551SRodney W. Grimes } 10158fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 1016484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1017484c7804SJulian Elischer if (v == 0 || (v % DEV_BSIZE) != 0) { 10188fae3551SRodney W. Grimes fprintf(stderr, 10198fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 10208fae3551SRodney W. Grimes lineno, tp); 10218fae3551SRodney W. Grimes errors++; 10228fae3551SRodney W. Grimes } else 10238fae3551SRodney W. Grimes lp->d_secsize = v; 10248fae3551SRodney W. Grimes continue; 10258fae3551SRodney W. Grimes } 10268fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 1027484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1028484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL) 1029484c7804SJulian Elischer if (v == 0 || v > 0xffffffff) { 1030484c7804SJulian Elischer #else 1031484c7804SJulian Elischer if (v == 0) { 1032484c7804SJulian Elischer #endif 10338fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10348fae3551SRodney W. Grimes lineno, tp, cp); 10358fae3551SRodney W. Grimes errors++; 10368fae3551SRodney W. Grimes } else 10378fae3551SRodney W. Grimes lp->d_nsectors = v; 10388fae3551SRodney W. Grimes continue; 10398fae3551SRodney W. Grimes } 10408fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 1041484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1042484c7804SJulian Elischer if (v == 0) { 10438fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10448fae3551SRodney W. Grimes lineno, tp, cp); 10458fae3551SRodney W. Grimes errors++; 10468fae3551SRodney W. Grimes } else 10478fae3551SRodney W. Grimes lp->d_secpercyl = v; 10488fae3551SRodney W. Grimes continue; 10498fae3551SRodney W. Grimes } 10508fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 1051484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1052484c7804SJulian Elischer if (v == 0) { 10538fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10548fae3551SRodney W. Grimes lineno, tp, cp); 10558fae3551SRodney W. Grimes errors++; 10568fae3551SRodney W. Grimes } else 10578fae3551SRodney W. Grimes lp->d_ntracks = v; 10588fae3551SRodney W. Grimes continue; 10598fae3551SRodney W. Grimes } 10608fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 1061484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1062484c7804SJulian Elischer if (v == 0) { 10638fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10648fae3551SRodney W. Grimes lineno, tp, cp); 10658fae3551SRodney W. Grimes errors++; 10668fae3551SRodney W. Grimes } else 10678fae3551SRodney W. Grimes lp->d_ncylinders = v; 10688fae3551SRodney W. Grimes continue; 10698fae3551SRodney W. Grimes } 1070f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 1071484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1072484c7804SJulian Elischer if (v == 0) { 1073f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 1074f75dd518SBruce Evans lineno, tp, cp); 1075f75dd518SBruce Evans errors++; 1076f75dd518SBruce Evans } else 1077f75dd518SBruce Evans lp->d_secperunit = v; 1078f75dd518SBruce Evans continue; 1079f75dd518SBruce Evans } 10808fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 1081484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1082484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 10838fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10848fae3551SRodney W. Grimes lineno, tp, cp); 10858fae3551SRodney W. Grimes errors++; 10868fae3551SRodney W. Grimes } else 10878fae3551SRodney W. Grimes lp->d_rpm = v; 10888fae3551SRodney W. Grimes continue; 10898fae3551SRodney W. Grimes } 10908fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 1091484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1092484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 10938fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10948fae3551SRodney W. Grimes lineno, tp, cp); 10958fae3551SRodney W. Grimes errors++; 10968fae3551SRodney W. Grimes } else 10978fae3551SRodney W. Grimes lp->d_interleave = v; 10988fae3551SRodney W. Grimes continue; 10998fae3551SRodney W. Grimes } 11008fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 1101484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1102484c7804SJulian Elischer if (v > USHRT_MAX) { 11038fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11048fae3551SRodney W. Grimes lineno, tp, cp); 11058fae3551SRodney W. Grimes errors++; 11068fae3551SRodney W. Grimes } else 11078fae3551SRodney W. Grimes lp->d_trackskew = v; 11088fae3551SRodney W. Grimes continue; 11098fae3551SRodney W. Grimes } 11108fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 1111484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1112484c7804SJulian Elischer if (v > USHRT_MAX) { 11138fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11148fae3551SRodney W. Grimes lineno, tp, cp); 11158fae3551SRodney W. Grimes errors++; 11168fae3551SRodney W. Grimes } else 11178fae3551SRodney W. Grimes lp->d_cylskew = v; 11188fae3551SRodney W. Grimes continue; 11198fae3551SRodney W. Grimes } 11208fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 1121484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 11228fae3551SRodney W. Grimes lp->d_headswitch = v; 11238fae3551SRodney W. Grimes continue; 11248fae3551SRodney W. Grimes } 11258fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 1126484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 11278fae3551SRodney W. Grimes lp->d_trkseek = v; 11288fae3551SRodney W. Grimes continue; 11298fae3551SRodney W. Grimes } 11303233afaeSJohn W. De Boskey /* the ':' was removed above */ 113167b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 113267b46708SIan Dowse fprintf(stderr, 113367b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 113467b46708SIan Dowse cp); 113567b46708SIan Dowse errors++; 113667b46708SIan Dowse continue; 113767b46708SIan Dowse } 113867b46708SIan Dowse 113967b46708SIan Dowse /* Process a partition specification line. */ 11403233afaeSJohn W. De Boskey part = *cp - 'a'; 11413233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 11428fae3551SRodney W. Grimes fprintf(stderr, 11433233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 11443233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 11458fae3551SRodney W. Grimes errors++; 11468fae3551SRodney W. Grimes continue; 11478fae3551SRodney W. Grimes } 11483233afaeSJohn W. De Boskey part_set[part] = 1; 114967b46708SIan Dowse 115067b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 115167b46708SIan Dowse errors++; 115267b46708SIan Dowse break; 115367b46708SIan Dowse } 115467b46708SIan Dowse } 115567b46708SIan Dowse errors += checklabel(lp); 115667b46708SIan Dowse return (errors == 0); 115767b46708SIan Dowse } 115867b46708SIan Dowse 115967b46708SIan Dowse #define NXTNUM(n) do { \ 116013e0abcbSPaul Traina if (tp == NULL) { \ 116113e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 116267b46708SIan Dowse return (1); \ 116313e0abcbSPaul Traina } else { \ 11648fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 1165484c7804SJulian Elischer (n) = strtoul(cp, NULL, 10); \ 116613e0abcbSPaul Traina } \ 116767b46708SIan Dowse } while (0) 116867b46708SIan Dowse 11693233afaeSJohn W. De Boskey /* retain 1 character following number */ 117067b46708SIan Dowse #define NXTWORD(w,n) do { \ 11713233afaeSJohn W. De Boskey if (tp == NULL) { \ 11723233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 117367b46708SIan Dowse return (1); \ 11743233afaeSJohn W. De Boskey } else { \ 11753233afaeSJohn W. De Boskey char *tmp; \ 11763233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 1177484c7804SJulian Elischer (n) = strtoul(cp, &tmp, 10); \ 11783233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 11793233afaeSJohn W. De Boskey } \ 118067b46708SIan Dowse } while (0) 118167b46708SIan Dowse 118267b46708SIan Dowse /* 118367b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 118467b46708SIan Dowse * Return 0 on success, 1 on failure. 118567b46708SIan Dowse */ 118667b46708SIan Dowse int 118767b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 118867b46708SIan Dowse { 118967b46708SIan Dowse struct partition *pp; 119067b46708SIan Dowse char *cp; 119167b46708SIan Dowse const char **cpp; 1192484c7804SJulian Elischer u_long v; 119367b46708SIan Dowse 119467b46708SIan Dowse pp = &lp->d_partitions[part]; 119567b46708SIan Dowse cp = NULL; 119667b46708SIan Dowse 11973233afaeSJohn W. De Boskey v = 0; 11983233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 1199484c7804SJulian Elischer if (v == 0 && part_size_type[part] != '*') { 1200484c7804SJulian Elischer fprintf(stderr, 1201484c7804SJulian Elischer "line %d: %s: bad partition size\n", lineno, cp); 120267b46708SIan Dowse return (1); 120367b46708SIan Dowse } 12048fae3551SRodney W. Grimes pp->p_size = v; 12053233afaeSJohn W. De Boskey 12063233afaeSJohn W. De Boskey v = 0; 12073233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 1208484c7804SJulian Elischer if (v == 0 && part_offset_type[part] != '*' && 1209484c7804SJulian Elischer part_offset_type[part] != '\0') { 1210484c7804SJulian Elischer fprintf(stderr, 1211484c7804SJulian Elischer "line %d: %s: bad partition offset\n", lineno, cp); 121267b46708SIan Dowse return (1); 121367b46708SIan Dowse } 12148fae3551SRodney W. Grimes pp->p_offset = v; 12158fae3551SRodney W. Grimes cp = tp, tp = word(cp); 121667b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 121767b46708SIan Dowse if (*cpp && streq(*cpp, cp)) 121867b46708SIan Dowse break; 121967b46708SIan Dowse if (*cpp != NULL) { 122067b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 122167b46708SIan Dowse } else { 12228fae3551SRodney W. Grimes if (isdigit(*cp)) 1223484c7804SJulian Elischer v = strtoul(cp, NULL, 10); 12248fae3551SRodney W. Grimes else 12258fae3551SRodney W. Grimes v = FSMAXTYPES; 1226484c7804SJulian Elischer if (v >= FSMAXTYPES) { 12273233afaeSJohn W. De Boskey fprintf(stderr, 122867b46708SIan Dowse "line %d: Warning, unknown file system type %s\n", 12293233afaeSJohn W. De Boskey lineno, cp); 12308fae3551SRodney W. Grimes v = FS_UNUSED; 12318fae3551SRodney W. Grimes } 12328fae3551SRodney W. Grimes pp->p_fstype = v; 123367b46708SIan Dowse } 123467b46708SIan Dowse 12358fae3551SRodney W. Grimes switch (pp->p_fstype) { 12363233afaeSJohn W. De Boskey case FS_UNUSED: 12373233afaeSJohn W. De Boskey /* 12383233afaeSJohn W. De Boskey * allow us to accept defaults for 12393233afaeSJohn W. De Boskey * fsize/frag/cpg 12403233afaeSJohn W. De Boskey */ 12413233afaeSJohn W. De Boskey if (tp) { 12428fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 12438fae3551SRodney W. Grimes if (pp->p_fsize == 0) 12448fae3551SRodney W. Grimes break; 12458fae3551SRodney W. Grimes NXTNUM(v); 12468fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 12473233afaeSJohn W. De Boskey } 12483233afaeSJohn W. De Boskey /* else default to 0's */ 12498fae3551SRodney W. Grimes break; 12508fae3551SRodney W. Grimes 12513233afaeSJohn W. De Boskey /* These happen to be the same */ 12528fae3551SRodney W. Grimes case FS_BSDFFS: 1253ca4693edSJustin T. Gibbs case FS_BSDLFS: 12543233afaeSJohn W. De Boskey if (tp) { 1255ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1256ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1257ca4693edSJustin T. Gibbs break; 1258ca4693edSJustin T. Gibbs NXTNUM(v); 1259ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1260ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 12613233afaeSJohn W. De Boskey } else { 12623233afaeSJohn W. De Boskey /* 126367b46708SIan Dowse * FIX! poor attempt at adaptive 12643233afaeSJohn W. De Boskey */ 12653233afaeSJohn W. De Boskey /* 1 GB */ 126667b46708SIan Dowse if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 126767b46708SIan Dowse /* 126867b46708SIan Dowse * FIX! These are too low, but are traditional 126967b46708SIan Dowse */ 1270f1a7b7eeSIan Dowse pp->p_fsize = DEFAULT_NEWFS_FRAG; 1271f1a7b7eeSIan Dowse pp->p_frag = DEFAULT_NEWFS_BLOCK / 1272f1a7b7eeSIan Dowse DEFAULT_NEWFS_FRAG; 12733233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 12743233afaeSJohn W. De Boskey } else { 1275f1a7b7eeSIan Dowse pp->p_fsize = BIG_NEWFS_FRAG; 1276f1a7b7eeSIan Dowse pp->p_frag = BIG_NEWFS_BLOCK / 1277f1a7b7eeSIan Dowse BIG_NEWFS_FRAG; 12783233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 12793233afaeSJohn W. De Boskey } 12803233afaeSJohn W. De Boskey } 12818fae3551SRodney W. Grimes default: 12828fae3551SRodney W. Grimes break; 12838fae3551SRodney W. Grimes } 128467b46708SIan Dowse return (0); 12858fae3551SRodney W. Grimes } 12868fae3551SRodney W. Grimes 12878fae3551SRodney W. Grimes /* 12888fae3551SRodney W. Grimes * Check disklabel for errors and fill in 12898fae3551SRodney W. Grimes * derived fields according to supplied values. 12908fae3551SRodney W. Grimes */ 129161de51caSJoerg Wunsch int 1292326c7cdaSWarner Losh checklabel(struct disklabel *lp) 12938fae3551SRodney W. Grimes { 1294326c7cdaSWarner Losh struct partition *pp; 12958fae3551SRodney W. Grimes int i, errors = 0; 12968fae3551SRodney W. Grimes char part; 1297484c7804SJulian Elischer u_long total_size, total_percent, current_offset; 12983233afaeSJohn W. De Boskey int seen_default_offset; 12993233afaeSJohn W. De Boskey int hog_part; 13003233afaeSJohn W. De Boskey int j; 13013233afaeSJohn W. De Boskey struct partition *pp2; 13028fae3551SRodney W. Grimes 13038fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 13042a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 13058fae3551SRodney W. Grimes return (1); 13068fae3551SRodney W. Grimes } 13078fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 13082a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 13098fae3551SRodney W. Grimes return (1); 13108fae3551SRodney W. Grimes } 13118fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 13122a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 13138fae3551SRodney W. Grimes return (1); 13148fae3551SRodney W. Grimes } 13158fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 13162a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 13178fae3551SRodney W. Grimes errors++; 13188fae3551SRodney W. Grimes } 13198fae3551SRodney W. Grimes if (lp->d_rpm == 0) 13202a1deaaaSBruce Evans Warning("revolutions/minute 0"); 13218fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 13228fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 13238fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 13248fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 13258fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 13262a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 13278fae3551SRodney W. Grimes errors++; 13288fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 13298fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 13308fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 13312a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 13322a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 13333233afaeSJohn W. De Boskey 13343233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 13353233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 13363233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 13373233afaeSJohn W. De Boskey hog_part = -1; 13383233afaeSJohn W. De Boskey /* find all fixed partitions */ 13393233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13403233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13413233afaeSJohn W. De Boskey if (part_set[i]) { 13423233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 13436b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 13443233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 13453233afaeSJohn W. De Boskey } else { 13463233afaeSJohn W. De Boskey if (hog_part != -1) 13473233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 13483233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 13493233afaeSJohn W. De Boskey else 13503233afaeSJohn W. De Boskey hog_part = i; 13513233afaeSJohn W. De Boskey } 13523233afaeSJohn W. De Boskey } else { 13538d3105e8SWarner Losh off_t size; 13543233afaeSJohn W. De Boskey 13553233afaeSJohn W. De Boskey size = pp->p_size; 13563233afaeSJohn W. De Boskey switch (part_size_type[i]) { 13573233afaeSJohn W. De Boskey case '%': 13583233afaeSJohn W. De Boskey total_percent += size; 13593233afaeSJohn W. De Boskey break; 13603233afaeSJohn W. De Boskey case 'k': 13613233afaeSJohn W. De Boskey case 'K': 13628d3105e8SWarner Losh size *= 1024ULL; 13633233afaeSJohn W. De Boskey break; 13643233afaeSJohn W. De Boskey case 'm': 13653233afaeSJohn W. De Boskey case 'M': 13668d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 13673233afaeSJohn W. De Boskey break; 13683233afaeSJohn W. De Boskey case 'g': 13693233afaeSJohn W. De Boskey case 'G': 13708d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 13713233afaeSJohn W. De Boskey break; 13723233afaeSJohn W. De Boskey case '\0': 13733233afaeSJohn W. De Boskey break; 13743233afaeSJohn W. De Boskey default: 13753233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 13763233afaeSJohn W. De Boskey break; 13773233afaeSJohn W. De Boskey } 13783233afaeSJohn W. De Boskey /* don't count %'s yet */ 13793233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 13803233afaeSJohn W. De Boskey /* 13813233afaeSJohn W. De Boskey * for all not in sectors, convert to 13823233afaeSJohn W. De Boskey * sectors 13833233afaeSJohn W. De Boskey */ 13843233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 13853233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 13863233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 13873233afaeSJohn W. De Boskey i + 'a'); 13883233afaeSJohn W. De Boskey size /= lp->d_secsize; 13893233afaeSJohn W. De Boskey pp->p_size = size; 13903233afaeSJohn W. De Boskey } 13913233afaeSJohn W. De Boskey /* else already in sectors */ 13926b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 13933233afaeSJohn W. De Boskey total_size += size; 13943233afaeSJohn W. De Boskey } 13953233afaeSJohn W. De Boskey } 13963233afaeSJohn W. De Boskey } 13973233afaeSJohn W. De Boskey } 13983233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 13993233afaeSJohn W. De Boskey if (total_percent != 0) { 14003233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 14013233afaeSJohn W. De Boskey if (total_percent > 100) { 14026b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 14033233afaeSJohn W. De Boskey total_percent); 14043233afaeSJohn W. De Boskey errors++; 14053233afaeSJohn W. De Boskey } 14063233afaeSJohn W. De Boskey 14073233afaeSJohn W. De Boskey if (free_space > 0) { 14083233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14093233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14103233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 14113233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 14123233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 14133233afaeSJohn W. De Boskey total_size += pp->p_size; 14143233afaeSJohn W. De Boskey 14153233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 14163233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 14173233afaeSJohn W. De Boskey } 14183233afaeSJohn W. De Boskey } 14193233afaeSJohn W. De Boskey } else { 14203233afaeSJohn W. De Boskey fprintf(stderr, 14216b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 14223233afaeSJohn W. De Boskey free_space); 14233233afaeSJohn W. De Boskey errors++; 14243233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 14253233afaeSJohn W. De Boskey } 14263233afaeSJohn W. De Boskey } 14273233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 14283233afaeSJohn W. De Boskey if (hog_part != -1) { 14293233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 14303233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 14313233afaeSJohn W. De Boskey } 14323233afaeSJohn W. De Boskey 14333233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 14343233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 14353233afaeSJohn W. De Boskey seen_default_offset = 0; 14363233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14373233afaeSJohn W. De Boskey part = 'a' + i; 14383233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14393233afaeSJohn W. De Boskey if (part_set[i]) { 14403233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 14416b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 14423233afaeSJohn W. De Boskey pp->p_offset = 0; 14433233afaeSJohn W. De Boskey } else { 14443233afaeSJohn W. De Boskey pp->p_offset = current_offset; 14453233afaeSJohn W. De Boskey seen_default_offset = 1; 14463233afaeSJohn W. De Boskey } 14473233afaeSJohn W. De Boskey } else { 14483233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 14493233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 1450f2f63257SGreg Lehey seen_default_offset && i != RAW_PART && 1451f2f63257SGreg Lehey pp->p_fstype != FS_VINUM) { 14523233afaeSJohn W. De Boskey fprintf(stderr, 14536b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 14546b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 14553233afaeSJohn W. De Boskey fprintf(stderr, 14563233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 14573233afaeSJohn W. De Boskey errors++; 14583233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 14596b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 14603233afaeSJohn W. De Boskey /* 14613233afaeSJohn W. De Boskey * this may give unneeded warnings if 14623233afaeSJohn W. De Boskey * partitions are out-of-order 14633233afaeSJohn W. De Boskey */ 14643233afaeSJohn W. De Boskey Warning( 14653233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 14666b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 14673233afaeSJohn W. De Boskey } 14683233afaeSJohn W. De Boskey } 14696b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 14703233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 14713233afaeSJohn W. De Boskey } 14723233afaeSJohn W. De Boskey } 14733233afaeSJohn W. De Boskey 14748fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 14758fae3551SRodney W. Grimes part = 'a' + i; 14768fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 14778fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 14782a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 14792a1deaaaSBruce Evans part, (u_long)pp->p_offset); 1480fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 1481fd43aa1cSDavid E. O'Brien /* See comment in writelabel(). */ 1482fd43aa1cSDavid E. O'Brien if (pp->p_offset % lp->d_secpercyl != 0) { 1483fd43aa1cSDavid E. O'Brien fprintf(stderr, "partition %c: does not start on a " 1484fd43aa1cSDavid E. O'Brien "cylinder boundary!\n", part); 1485fd43aa1cSDavid E. O'Brien errors++; 1486fd43aa1cSDavid E. O'Brien } 1487fd43aa1cSDavid E. O'Brien #endif 14888fae3551SRodney W. Grimes #ifdef notdef 14898fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 14908fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 14918fae3551SRodney W. Grimes part); 14928fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 14938fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 14948fae3551SRodney W. Grimes part); 14958fae3551SRodney W. Grimes #endif 14968fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 14978fae3551SRodney W. Grimes fprintf(stderr, 14988fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 14998fae3551SRodney W. Grimes errors++; 15008fae3551SRodney W. Grimes } 15018fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 15028fae3551SRodney W. Grimes fprintf(stderr, 15038fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 15048fae3551SRodney W. Grimes part); 15058fae3551SRodney W. Grimes errors++; 15068fae3551SRodney W. Grimes } 15076b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) 15083233afaeSJohn W. De Boskey { 15093233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 15103233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 15113233afaeSJohn W. De Boskey if (pp->p_offset != 0) 15123233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 15133233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 15143233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 15153233afaeSJohn W. De Boskey 15163233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 15173233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 15183233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 15193233afaeSJohn W. De Boskey "standard system utilities",part); 15203233afaeSJohn W. De Boskey } 15213233afaeSJohn W. De Boskey } 15223233afaeSJohn W. De Boskey 15233233afaeSJohn W. De Boskey /* check for overlaps */ 15243233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 15253233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 15263233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 1527f2f63257SGreg Lehey if (j != RAW_PART && i != RAW_PART && 1528f2f63257SGreg Lehey pp->p_fstype != FS_VINUM && 1529f2f63257SGreg Lehey pp2->p_fstype != FS_VINUM && 1530f2f63257SGreg Lehey part_set[i] && part_set[j]) { 15313233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 15323233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 15333233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 15343233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 15353233afaeSJohn W. De Boskey j + 'a', i + 'a'); 15363233afaeSJohn W. De Boskey errors++; 15373233afaeSJohn W. De Boskey } 15383233afaeSJohn W. De Boskey } 15393233afaeSJohn W. De Boskey } 15408fae3551SRodney W. Grimes } 15418fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 15428fae3551SRodney W. Grimes part = 'a' + i; 15438fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 15448fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 15452a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 15462a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 15478fae3551SRodney W. Grimes } 15488fae3551SRodney W. Grimes return (errors); 15498fae3551SRodney W. Grimes } 15508fae3551SRodney W. Grimes 15518fae3551SRodney W. Grimes /* 1552425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1553425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1554425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1555425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1556425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1557425bed3aSJoerg Wunsch * 1558425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1559425bed3aSJoerg Wunsch */ 1560425bed3aSJoerg Wunsch struct disklabel * 1561425bed3aSJoerg Wunsch getvirginlabel(void) 1562425bed3aSJoerg Wunsch { 1563c8223965SMark Murray static struct disklabel loclab; 1564b9d05a16SPoul-Henning Kamp struct partition *dp; 1565c8223965SMark Murray char lnamebuf[BBSIZE]; 1566425bed3aSJoerg Wunsch int f; 1567b9d05a16SPoul-Henning Kamp u_int secsize, u; 1568b9d05a16SPoul-Henning Kamp off_t mediasize; 1569425bed3aSJoerg Wunsch 1570425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1571e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 157243be698cSBruce Evans return (NULL); 1573425bed3aSJoerg Wunsch } 1574c8223965SMark Murray (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1575c8223965SMark Murray if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1576c8223965SMark Murray warn("cannot open %s", lnamebuf); 157743be698cSBruce Evans return (NULL); 1578425bed3aSJoerg Wunsch } 1579ff7d5162SJordan K. Hubbard 1580b9d05a16SPoul-Henning Kamp /* New world order */ 1581b9d05a16SPoul-Henning Kamp if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1582b9d05a16SPoul-Henning Kamp (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1583425bed3aSJoerg Wunsch close (f); 158443be698cSBruce Evans return (NULL); 1585425bed3aSJoerg Wunsch } 1586b9d05a16SPoul-Henning Kamp memset(&loclab, 0, sizeof loclab); 1587b9d05a16SPoul-Henning Kamp loclab.d_magic = DISKMAGIC; 1588b9d05a16SPoul-Henning Kamp loclab.d_magic2 = DISKMAGIC; 1589b9d05a16SPoul-Henning Kamp loclab.d_secsize = secsize; 1590b9d05a16SPoul-Henning Kamp loclab.d_secperunit = mediasize / secsize; 1591b9d05a16SPoul-Henning Kamp 1592b9d05a16SPoul-Henning Kamp /* 1593b9d05a16SPoul-Henning Kamp * Nobody in these enligthened days uses the CHS geometry for 1594b9d05a16SPoul-Henning Kamp * anything, but nontheless try to get it right. If we fail 1595b9d05a16SPoul-Henning Kamp * to get any good ideas from the device, construct something 1596b9d05a16SPoul-Henning Kamp * which is IBM-PC friendly. 1597b9d05a16SPoul-Henning Kamp */ 1598b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1599b9d05a16SPoul-Henning Kamp loclab.d_nsectors = u; 1600b9d05a16SPoul-Henning Kamp else 1601b9d05a16SPoul-Henning Kamp loclab.d_nsectors = 63; 1602b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1603b9d05a16SPoul-Henning Kamp loclab.d_ntracks = u; 1604b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*1*1024) 1605b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 1; 1606b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*16*1024) 1607b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 16; 1608b9d05a16SPoul-Henning Kamp else 1609b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 255; 1610b9d05a16SPoul-Henning Kamp loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1611b9d05a16SPoul-Henning Kamp loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1612b9d05a16SPoul-Henning Kamp loclab.d_npartitions = MAXPARTITIONS; 1613b9d05a16SPoul-Henning Kamp 1614b9d05a16SPoul-Henning Kamp /* Various (unneeded) compat stuff */ 1615b9d05a16SPoul-Henning Kamp loclab.d_rpm = 3600; 1616b9d05a16SPoul-Henning Kamp loclab.d_bbsize = BBSIZE; 1617b9d05a16SPoul-Henning Kamp loclab.d_interleave = 1;; 1618b9d05a16SPoul-Henning Kamp strncpy(loclab.d_typename, "amnesiac", 1619b9d05a16SPoul-Henning Kamp sizeof(loclab.d_typename)); 1620b9d05a16SPoul-Henning Kamp 1621b9d05a16SPoul-Henning Kamp dp = &loclab.d_partitions[RAW_PART]; 1622b9d05a16SPoul-Henning Kamp dp->p_size = loclab.d_secperunit; 1623b9d05a16SPoul-Henning Kamp loclab.d_checksum = dkcksum(&loclab); 1624425bed3aSJoerg Wunsch close (f); 1625c8223965SMark Murray return (&loclab); 1626425bed3aSJoerg Wunsch } 1627425bed3aSJoerg Wunsch 1628425bed3aSJoerg Wunsch /* 16298fae3551SRodney W. Grimes * If we are installing a boot program that doesn't fit in d_bbsize 16308fae3551SRodney W. Grimes * we need to mark those partitions that the boot overflows into. 16318fae3551SRodney W. Grimes * This allows newfs to prevent creation of a file system where it might 16328fae3551SRodney W. Grimes * clobber bootstrap code. 16338fae3551SRodney W. Grimes */ 163461de51caSJoerg Wunsch void 1635326c7cdaSWarner Losh setbootflag(struct disklabel *lp) 16368fae3551SRodney W. Grimes { 1637326c7cdaSWarner Losh struct partition *pp; 16388fae3551SRodney W. Grimes int i, errors = 0; 16398fae3551SRodney W. Grimes char part; 16408fae3551SRodney W. Grimes u_long boffset; 16418fae3551SRodney W. Grimes 16428fae3551SRodney W. Grimes if (bootbuf == 0) 16438fae3551SRodney W. Grimes return; 16448fae3551SRodney W. Grimes boffset = bootsize / lp->d_secsize; 16458fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 16468fae3551SRodney W. Grimes part = 'a' + i; 16478fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 16488fae3551SRodney W. Grimes if (pp->p_size == 0) 16498fae3551SRodney W. Grimes continue; 16508fae3551SRodney W. Grimes if (boffset <= pp->p_offset) { 16518fae3551SRodney W. Grimes if (pp->p_fstype == FS_BOOT) 16528fae3551SRodney W. Grimes pp->p_fstype = FS_UNUSED; 16538fae3551SRodney W. Grimes } else if (pp->p_fstype != FS_BOOT) { 16548fae3551SRodney W. Grimes if (pp->p_fstype != FS_UNUSED) { 16558fae3551SRodney W. Grimes fprintf(stderr, 16568fae3551SRodney W. Grimes "boot overlaps used partition %c\n", 16578fae3551SRodney W. Grimes part); 16588fae3551SRodney W. Grimes errors++; 16598fae3551SRodney W. Grimes } else { 16608fae3551SRodney W. Grimes pp->p_fstype = FS_BOOT; 16618fae3551SRodney W. Grimes Warning("boot overlaps partition %c, %s", 16628fae3551SRodney W. Grimes part, "marked as FS_BOOT"); 16638fae3551SRodney W. Grimes } 16648fae3551SRodney W. Grimes } 16658fae3551SRodney W. Grimes } 16666bd343a9SPhilippe Charnier if (errors) 16676bd343a9SPhilippe Charnier errx(4, "cannot install boot program"); 16688fae3551SRodney W. Grimes } 16698fae3551SRodney W. Grimes 16708fae3551SRodney W. Grimes /*VARARGS1*/ 167161de51caSJoerg Wunsch void 1672326c7cdaSWarner Losh Warning(const char *fmt, ...) 16738fae3551SRodney W. Grimes { 167461de51caSJoerg Wunsch va_list ap; 16758fae3551SRodney W. Grimes 16768fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 167761de51caSJoerg Wunsch va_start(ap, fmt); 167861de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 16798fae3551SRodney W. Grimes fprintf(stderr, "\n"); 168061de51caSJoerg Wunsch va_end(ap); 16818fae3551SRodney W. Grimes } 16828fae3551SRodney W. Grimes 168361de51caSJoerg Wunsch void 1684326c7cdaSWarner Losh usage(void) 16858fae3551SRodney W. Grimes { 16868fae3551SRodney W. Grimes #if NUMBOOT > 0 168780baf8ceSPoul-Henning Kamp fprintf(stderr, "%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", 16888fae3551SRodney W. Grimes "usage: disklabel [-r] disk", 1689bef2080aSPhilippe Charnier "\t\t(to read label)", 16903233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1691bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 16923233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1693bef2080aSPhilippe Charnier "\t\t(to edit label)", 16943233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1695bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 16968fae3551SRodney W. Grimes #if NUMBOOT > 1 16973233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1698bef2080aSPhilippe Charnier "\t\t(to install boot program with existing label)", 16993233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1700bef2080aSPhilippe Charnier "\t\t(to write label and boot program)", 17013233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 170280baf8ceSPoul-Henning Kamp "\t\t(to restore label and boot program)" 17038fae3551SRodney W. Grimes #else 17043233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1705bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 17063233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1707bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 17083233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 170980baf8ceSPoul-Henning Kamp "\t\t(to restore label and install boot program)" 17108fae3551SRodney W. Grimes #endif 171180baf8ceSPoul-Henning Kamp ); 17128fae3551SRodney W. Grimes #else 1713bef2080aSPhilippe Charnier fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 17148fae3551SRodney W. Grimes "usage: disklabel [-r] disk", "(to read label)", 17153233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1716bef2080aSPhilippe Charnier "\t\t(to write label)", 17173233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1718bef2080aSPhilippe Charnier "\t\t(to edit label)", 17193233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1720bef2080aSPhilippe Charnier "\t\t(to restore label)", 1721bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1722bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17238fae3551SRodney W. Grimes #endif 17248fae3551SRodney W. Grimes exit(1); 17258fae3551SRodney W. Grimes } 1726