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 546bd343a9SPhilippe Charnier static const char rcsid[] = 557f3dea24SPeter Wemm "$FreeBSD$"; 568fae3551SRodney W. Grimes #endif /* not lint */ 578fae3551SRodney W. Grimes 588fae3551SRodney W. Grimes #include <sys/param.h> 598fae3551SRodney W. Grimes #include <sys/file.h> 608fae3551SRodney W. Grimes #include <sys/stat.h> 6161de51caSJoerg Wunsch #include <sys/wait.h> 628fae3551SRodney W. Grimes #define DKTYPENAMES 638fae3551SRodney W. Grimes #include <sys/disklabel.h> 64fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 65fd43aa1cSDavid E. O'Brien #include <sys/sun_disklabel.h> 66fd43aa1cSDavid E. O'Brien #endif 678fae3551SRodney W. Grimes #include <ufs/ffs/fs.h> 688fae3551SRodney W. Grimes #include <unistd.h> 698fae3551SRodney W. Grimes #include <string.h> 708fae3551SRodney W. Grimes #include <stdio.h> 7161de51caSJoerg Wunsch #include <stdlib.h> 7261de51caSJoerg Wunsch #include <signal.h> 7361de51caSJoerg Wunsch #include <stdarg.h> 748fae3551SRodney W. Grimes #include <ctype.h> 75bef2080aSPhilippe Charnier #include <err.h> 7637736675SWarner Losh #include <errno.h> 778fae3551SRodney W. Grimes #include "pathnames.h" 788fae3551SRodney W. Grimes 798fae3551SRodney W. Grimes /* 808fae3551SRodney W. Grimes * Disklabel: read and write disklabels. 818fae3551SRodney W. Grimes * The label is usually placed on one of the first sectors of the disk. 828fae3551SRodney W. Grimes * Many machines also place a bootstrap in the same area, 838fae3551SRodney W. Grimes * in which case the label is embedded in the bootstrap. 848fae3551SRodney W. Grimes * The bootstrap source must leave space at the proper offset 858fae3551SRodney W. Grimes * for the label on such machines. 868fae3551SRodney W. Grimes */ 878fae3551SRodney W. Grimes 888fae3551SRodney W. Grimes #ifndef BBSIZE 898fae3551SRodney W. Grimes #define BBSIZE 8192 /* size of boot area, with label */ 908fae3551SRodney W. Grimes #endif 918fae3551SRodney W. Grimes 923233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 933233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 943233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 953233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 963233afaeSJohn W. De Boskey 973233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 983233afaeSJohn W. De Boskey #define BIG_NEWFS_FRAG 4096U 993233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 1003233afaeSJohn W. De Boskey 10190b8f6baSDavid E. O'Brien #if defined(__i386__) || defined(__ia64__) 10290b8f6baSDavid E. O'Brien #define NUMBOOT 2 10390b8f6baSDavid E. O'Brien #elif defined(__alpha__) || defined(__sparc64__) 1048fae3551SRodney W. Grimes #define NUMBOOT 1 1058fae3551SRodney W. Grimes #else 10690b8f6baSDavid E. O'Brien #error I do not know about this architecture. 1078fae3551SRodney W. Grimes #endif 1088fae3551SRodney W. Grimes 109326c7cdaSWarner Losh void makelabel(const char *, const char *, struct disklabel *); 110326c7cdaSWarner Losh int writelabel(int, const char *, struct disklabel *); 111326c7cdaSWarner Losh void l_perror(const char *); 112326c7cdaSWarner Losh struct disklabel *readlabel(int); 113326c7cdaSWarner Losh struct disklabel *makebootarea(char *, struct disklabel *, int); 114326c7cdaSWarner Losh void display(FILE *, const struct disklabel *); 115326c7cdaSWarner Losh int edit(struct disklabel *, int); 116326c7cdaSWarner Losh int editit(void); 117326c7cdaSWarner Losh char *skip(char *); 118326c7cdaSWarner Losh char *word(char *); 119326c7cdaSWarner Losh int getasciilabel(FILE *, struct disklabel *); 120326c7cdaSWarner Losh int checklabel(struct disklabel *); 121326c7cdaSWarner Losh void setbootflag(struct disklabel *); 122326c7cdaSWarner Losh void Warning(const char *, ...) __printflike(1, 2); 123326c7cdaSWarner Losh void usage(void); 124326c7cdaSWarner Losh struct disklabel *getvirginlabel(void); 12561de51caSJoerg Wunsch 1268fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1278fae3551SRodney W. Grimes #define streq(a,b) (strcmp(a,b) == 0) 1288fae3551SRodney W. Grimes 1298fae3551SRodney W. Grimes char *dkname; 1308fae3551SRodney W. Grimes char *specname; 131aaae3130SKris Kennaway char tmpfil[] = PATH_TMPFILE; 1328fae3551SRodney W. Grimes 1338fae3551SRodney W. Grimes char namebuf[BBSIZE], *np = namebuf; 1348fae3551SRodney W. Grimes struct disklabel lab; 1358fae3551SRodney W. Grimes char bootarea[BBSIZE]; 1368fae3551SRodney W. Grimes 1373233afaeSJohn W. De Boskey /* partition 'c' is the full disk and is special */ 1383233afaeSJohn W. De Boskey #define FULL_DISK_PART 2 1393233afaeSJohn W. De Boskey #define MAX_PART ('z') 1403233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 1413233afaeSJohn W. De Boskey char part_size_type[MAX_NUM_PARTS]; 1423233afaeSJohn W. De Boskey char part_offset_type[MAX_NUM_PARTS]; 1433233afaeSJohn W. De Boskey int part_set[MAX_NUM_PARTS]; 1443233afaeSJohn W. De Boskey 1458fae3551SRodney W. Grimes #if NUMBOOT > 0 1468fae3551SRodney W. Grimes int installboot; /* non-zero if we should install a boot program */ 1478fae3551SRodney W. Grimes char *bootbuf; /* pointer to buffer with remainder of boot prog */ 1488fae3551SRodney W. Grimes int bootsize; /* size of remaining boot program */ 1498fae3551SRodney W. Grimes char *xxboot; /* primary boot */ 1508fae3551SRodney W. Grimes char *bootxx; /* secondary boot */ 1518fae3551SRodney W. Grimes char boot0[MAXPATHLEN]; 1528fae3551SRodney W. Grimes char boot1[MAXPATHLEN]; 1538fae3551SRodney W. Grimes #endif 1548fae3551SRodney W. Grimes 1558fae3551SRodney W. Grimes enum { 1568fae3551SRodney W. Grimes UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT 1578fae3551SRodney W. Grimes } op = UNSPEC; 1588fae3551SRodney W. Grimes 1598fae3551SRodney W. Grimes int rflag; 1603233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1618fae3551SRodney W. Grimes 1628fae3551SRodney W. Grimes #ifdef DEBUG 1638fae3551SRodney W. Grimes int debug; 1643233afaeSJohn W. De Boskey #define OPTIONS "BNRWb:denrs:w" 1658fae3551SRodney W. Grimes #else 1663233afaeSJohn W. De Boskey #define OPTIONS "BNRWb:enrs:w" 1678fae3551SRodney W. Grimes #endif 1688fae3551SRodney W. Grimes 16961de51caSJoerg Wunsch int 170326c7cdaSWarner Losh main(int argc, char *argv[]) 1718fae3551SRodney W. Grimes { 1728fae3551SRodney W. Grimes register struct disklabel *lp; 1738fae3551SRodney W. Grimes FILE *t; 1743b3038a6SBill Fumerola int ch, f = 0, flag, error = 0; 1758fae3551SRodney W. Grimes char *name = 0; 1768fae3551SRodney W. Grimes 1778d64695cSWarner Losh while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1788fae3551SRodney W. Grimes switch (ch) { 1798fae3551SRodney W. Grimes #if NUMBOOT > 0 1808fae3551SRodney W. Grimes case 'B': 1818fae3551SRodney W. Grimes ++installboot; 1828fae3551SRodney W. Grimes break; 1838fae3551SRodney W. Grimes case 'b': 1848fae3551SRodney W. Grimes xxboot = optarg; 1858fae3551SRodney W. Grimes break; 1868fae3551SRodney W. Grimes #if NUMBOOT > 1 1878fae3551SRodney W. Grimes case 's': 1888fae3551SRodney W. Grimes bootxx = optarg; 1898fae3551SRodney W. Grimes break; 1908fae3551SRodney W. Grimes #endif 1918fae3551SRodney W. Grimes #endif 1928fae3551SRodney W. Grimes case 'N': 1938fae3551SRodney W. Grimes if (op != UNSPEC) 1948fae3551SRodney W. Grimes usage(); 1958fae3551SRodney W. Grimes op = NOWRITE; 1968fae3551SRodney W. Grimes break; 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 'W': 2068fae3551SRodney W. Grimes if (op != UNSPEC) 2078fae3551SRodney W. Grimes usage(); 2088fae3551SRodney W. Grimes op = WRITEABLE; 2098fae3551SRodney W. Grimes break; 2108fae3551SRodney W. Grimes case 'e': 2118fae3551SRodney W. Grimes if (op != UNSPEC) 2128fae3551SRodney W. Grimes usage(); 2138fae3551SRodney W. Grimes op = EDIT; 2148fae3551SRodney W. Grimes break; 2158fae3551SRodney W. Grimes case 'r': 2168fae3551SRodney W. Grimes ++rflag; 2178fae3551SRodney W. Grimes break; 2188fae3551SRodney W. Grimes case 'w': 2198fae3551SRodney W. Grimes if (op != UNSPEC) 2208fae3551SRodney W. Grimes usage(); 2218fae3551SRodney W. Grimes op = WRITE; 2228fae3551SRodney W. Grimes break; 2238fae3551SRodney W. Grimes #ifdef DEBUG 2248fae3551SRodney W. Grimes case 'd': 2258fae3551SRodney W. Grimes debug++; 2268fae3551SRodney W. Grimes break; 2278fae3551SRodney W. Grimes #endif 2288fae3551SRodney W. Grimes case '?': 2298fae3551SRodney W. Grimes default: 2308fae3551SRodney W. Grimes usage(); 2318fae3551SRodney W. Grimes } 2328fae3551SRodney W. Grimes argc -= optind; 2338fae3551SRodney W. Grimes argv += optind; 2348fae3551SRodney W. Grimes #if NUMBOOT > 0 2358fae3551SRodney W. Grimes if (installboot) { 2368fae3551SRodney W. Grimes rflag++; 2378fae3551SRodney W. Grimes if (op == UNSPEC) 2388fae3551SRodney W. Grimes op = WRITEBOOT; 2398fae3551SRodney W. Grimes } else { 2408fae3551SRodney W. Grimes if (op == UNSPEC) 2418fae3551SRodney W. Grimes op = READ; 2428fae3551SRodney W. Grimes xxboot = bootxx = 0; 2438fae3551SRodney W. Grimes } 2448fae3551SRodney W. Grimes #else 2458fae3551SRodney W. Grimes if (op == UNSPEC) 2468fae3551SRodney W. Grimes op = READ; 2478fae3551SRodney W. Grimes #endif 2488fae3551SRodney W. Grimes if (argc < 1) 2498fae3551SRodney W. Grimes usage(); 2508fae3551SRodney W. Grimes 2518fae3551SRodney W. Grimes dkname = argv[0]; 2528fae3551SRodney W. Grimes if (dkname[0] != '/') { 25309c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2548fae3551SRodney W. Grimes specname = np; 2558fae3551SRodney W. Grimes np += strlen(specname) + 1; 2568fae3551SRodney W. Grimes } else 2578fae3551SRodney W. Grimes specname = dkname; 2588fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2598fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 26043c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 2618fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 2628fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2638fae3551SRodney W. Grimes } 2648fae3551SRodney W. Grimes if (f < 0) 265bef2080aSPhilippe Charnier err(4, "%s", specname); 2668fae3551SRodney W. Grimes 2678fae3551SRodney W. Grimes switch(op) { 2688fae3551SRodney W. Grimes 2693b3038a6SBill Fumerola case UNSPEC: 2703b3038a6SBill Fumerola break; 2713b3038a6SBill Fumerola 2728fae3551SRodney W. Grimes case EDIT: 2738fae3551SRodney W. Grimes if (argc != 1) 2748fae3551SRodney W. Grimes usage(); 2758fae3551SRodney W. Grimes lp = readlabel(f); 2768fae3551SRodney W. Grimes error = edit(lp, f); 2778fae3551SRodney W. Grimes break; 2788fae3551SRodney W. Grimes 2798fae3551SRodney W. Grimes case NOWRITE: 2808fae3551SRodney W. Grimes flag = 0; 2818fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 282bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 2838fae3551SRodney W. Grimes break; 2848fae3551SRodney W. Grimes 2858fae3551SRodney W. Grimes case READ: 2868fae3551SRodney W. Grimes if (argc != 1) 2878fae3551SRodney W. Grimes usage(); 2888fae3551SRodney W. Grimes lp = readlabel(f); 2898fae3551SRodney W. Grimes display(stdout, lp); 2908fae3551SRodney W. Grimes error = checklabel(lp); 2918fae3551SRodney W. Grimes break; 2928fae3551SRodney W. Grimes 2938fae3551SRodney W. Grimes case RESTORE: 2948fae3551SRodney W. Grimes #if NUMBOOT > 0 2958fae3551SRodney W. Grimes if (installboot && argc == 3) { 2968fae3551SRodney W. Grimes makelabel(argv[2], 0, &lab); 2978fae3551SRodney W. Grimes argc--; 2986cabb348SBruce Evans 2996cabb348SBruce Evans /* 3006cabb348SBruce Evans * We only called makelabel() for its side effect 3016cabb348SBruce Evans * of setting the bootstrap file names. Discard 3026cabb348SBruce Evans * all changes to `lab' so that all values in the 3036cabb348SBruce Evans * final label come from the ASCII label. 3046cabb348SBruce Evans */ 3056cabb348SBruce Evans bzero((char *)&lab, sizeof(lab)); 3068fae3551SRodney W. Grimes } 3078fae3551SRodney W. Grimes #endif 3088fae3551SRodney W. Grimes if (argc != 2) 3098fae3551SRodney W. Grimes usage(); 3108fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 311bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 3126cabb348SBruce Evans if (!getasciilabel(t, &lab)) 3136cabb348SBruce Evans exit(1); 3146cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 3156cabb348SBruce Evans *lp = lab; 3168fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3178fae3551SRodney W. Grimes break; 3188fae3551SRodney W. Grimes 3198fae3551SRodney W. Grimes case WRITE: 3208fae3551SRodney W. Grimes if (argc == 3) { 3218fae3551SRodney W. Grimes name = argv[2]; 3228fae3551SRodney W. Grimes argc--; 3238fae3551SRodney W. Grimes } 3248fae3551SRodney W. Grimes if (argc != 2) 3258fae3551SRodney W. Grimes usage(); 3268fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3278fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3288fae3551SRodney W. Grimes *lp = lab; 3298fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3308fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3318fae3551SRodney W. Grimes break; 3328fae3551SRodney W. Grimes 3338fae3551SRodney W. Grimes case WRITEABLE: 3348fae3551SRodney W. Grimes flag = 1; 3358fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 336bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 3378fae3551SRodney W. Grimes break; 3388fae3551SRodney W. Grimes 3398fae3551SRodney W. Grimes #if NUMBOOT > 0 3408fae3551SRodney W. Grimes case WRITEBOOT: 3418fae3551SRodney W. Grimes { 3428fae3551SRodney W. Grimes struct disklabel tlab; 3438fae3551SRodney W. Grimes 3448fae3551SRodney W. Grimes lp = readlabel(f); 3458fae3551SRodney W. Grimes tlab = *lp; 3468fae3551SRodney W. Grimes if (argc == 2) 3478fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3488fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3498fae3551SRodney W. Grimes *lp = tlab; 3508fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3518fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3528fae3551SRodney W. Grimes break; 3538fae3551SRodney W. Grimes } 3548fae3551SRodney W. Grimes #endif 3558fae3551SRodney W. Grimes } 3568fae3551SRodney W. Grimes exit(error); 3578fae3551SRodney W. Grimes } 3588fae3551SRodney W. Grimes 3598fae3551SRodney W. Grimes /* 3608fae3551SRodney W. Grimes * Construct a prototype disklabel from /etc/disktab. As a side 3618fae3551SRodney W. Grimes * effect, set the names of the primary and secondary boot files 3628fae3551SRodney W. Grimes * if specified. 3638fae3551SRodney W. Grimes */ 36461de51caSJoerg Wunsch void 365326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp) 3668fae3551SRodney W. Grimes { 367326c7cdaSWarner Losh struct disklabel *dp; 368425bed3aSJoerg Wunsch 369425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 370425bed3aSJoerg Wunsch dp = getvirginlabel(); 371425bed3aSJoerg Wunsch else 3728fae3551SRodney W. Grimes dp = getdiskbyname(type); 3736bd343a9SPhilippe Charnier if (dp == NULL) 3746bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3758fae3551SRodney W. Grimes *lp = *dp; 3768fae3551SRodney W. Grimes #if NUMBOOT > 0 3778fae3551SRodney W. Grimes /* 3788fae3551SRodney W. Grimes * Set bootstrap name(s). 3798fae3551SRodney W. Grimes * 1. If set from command line, use those, 3808fae3551SRodney W. Grimes * 2. otherwise, check if disktab specifies them (b0 or b1), 3818fae3551SRodney W. Grimes * 3. otherwise, makebootarea() will choose ones based on the name 3828fae3551SRodney W. Grimes * of the disk special file. E.g. /dev/ra0 -> raboot, bootra 3838fae3551SRodney W. Grimes */ 3848fae3551SRodney W. Grimes if (!xxboot && lp->d_boot0) { 3858fae3551SRodney W. Grimes if (*lp->d_boot0 != '/') 3868fae3551SRodney W. Grimes (void)sprintf(boot0, "%s/%s", 3878fae3551SRodney W. Grimes _PATH_BOOTDIR, lp->d_boot0); 3888fae3551SRodney W. Grimes else 3898fae3551SRodney W. Grimes (void)strcpy(boot0, lp->d_boot0); 3908fae3551SRodney W. Grimes xxboot = boot0; 3918fae3551SRodney W. Grimes } 3928fae3551SRodney W. Grimes #if NUMBOOT > 1 3938fae3551SRodney W. Grimes if (!bootxx && lp->d_boot1) { 3948fae3551SRodney W. Grimes if (*lp->d_boot1 != '/') 3958fae3551SRodney W. Grimes (void)sprintf(boot1, "%s/%s", 3968fae3551SRodney W. Grimes _PATH_BOOTDIR, lp->d_boot1); 3978fae3551SRodney W. Grimes else 3988fae3551SRodney W. Grimes (void)strcpy(boot1, lp->d_boot1); 3998fae3551SRodney W. Grimes bootxx = boot1; 4008fae3551SRodney W. Grimes } 4018fae3551SRodney W. Grimes #endif 4028fae3551SRodney W. Grimes #endif 4038fae3551SRodney W. Grimes /* d_packname is union d_boot[01], so zero */ 4048fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 4058fae3551SRodney W. Grimes if (name) 4068fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 4078fae3551SRodney W. Grimes } 4088fae3551SRodney W. Grimes 40961de51caSJoerg Wunsch int 410326c7cdaSWarner Losh writelabel(int f, const char *boot, struct disklabel *lp) 4118fae3551SRodney W. Grimes { 4128fae3551SRodney W. Grimes int flag; 413130cd73aSDoug Rabson #ifdef __alpha__ 414130cd73aSDoug Rabson u_long *p, sum; 415130cd73aSDoug Rabson int i; 416130cd73aSDoug Rabson #endif 417fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 418fd43aa1cSDavid E. O'Brien struct sun_disklabel *sl; 419fd43aa1cSDavid E. O'Brien u_short cksum, *sp1, *sp2; 420fd43aa1cSDavid E. O'Brien struct partition *npp; 421fd43aa1cSDavid E. O'Brien struct sun_dkpart *spp; 422fd43aa1cSDavid E. O'Brien int i, secpercyl; 423cc18e4ccSBruce Evans #endif 424130cd73aSDoug Rabson 4253233afaeSJohn W. De Boskey if (disable_write) { 4263233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 4273233afaeSJohn W. De Boskey display(stdout, lp); 4283233afaeSJohn W. De Boskey return (0); 4293233afaeSJohn W. De Boskey } else { 4308fae3551SRodney W. Grimes setbootflag(lp); 4318fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 4328fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 4338fae3551SRodney W. Grimes lp->d_checksum = 0; 4348fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 4358fae3551SRodney W. Grimes if (rflag) { 4368fae3551SRodney W. Grimes /* 4378fae3551SRodney W. Grimes * First set the kernel disk label, 4388fae3551SRodney W. Grimes * then write a label to the raw disk. 4398fae3551SRodney W. Grimes * If the SDINFO ioctl fails because it is unimplemented, 4408fae3551SRodney W. Grimes * keep going; otherwise, the kernel consistency checks 4418fae3551SRodney W. Grimes * may prevent us from changing the current (in-core) 4428fae3551SRodney W. Grimes * label. 4438fae3551SRodney W. Grimes */ 4448fae3551SRodney W. Grimes if (ioctl(f, DIOCSDINFO, lp) < 0 && 4458fae3551SRodney W. Grimes errno != ENODEV && errno != ENOTTY) { 4468fae3551SRodney W. Grimes l_perror("ioctl DIOCSDINFO"); 4478fae3551SRodney W. Grimes return (1); 4488fae3551SRodney W. Grimes } 4498fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 450130cd73aSDoug Rabson 451130cd73aSDoug Rabson #ifdef __alpha__ 452cc18e4ccSBruce Evans /* 453cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 454cc18e4ccSBruce Evans */ 455130cd73aSDoug Rabson for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) 456130cd73aSDoug Rabson sum += p[i]; 457130cd73aSDoug Rabson p[63] = sum; 458130cd73aSDoug Rabson #endif 459fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 460fd43aa1cSDavid E. O'Brien /* 461fd43aa1cSDavid E. O'Brien * Generate a Sun disklabel around the BSD label for 462fd43aa1cSDavid E. O'Brien * PROM compatability. 463fd43aa1cSDavid E. O'Brien */ 464fd43aa1cSDavid E. O'Brien sl = (struct sun_disklabel *)boot; 465fd43aa1cSDavid E. O'Brien memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname)); 466fd43aa1cSDavid E. O'Brien sl->sl_rpm = lp->d_rpm; 467fd43aa1cSDavid E. O'Brien sl->sl_pcylinders = lp->d_ncylinders + 468fd43aa1cSDavid E. O'Brien lp->d_acylinders; /* XXX */ 469fd43aa1cSDavid E. O'Brien sl->sl_sparespercyl = lp->d_sparespercyl; 470fd43aa1cSDavid E. O'Brien sl->sl_interleave = lp->d_interleave; 471fd43aa1cSDavid E. O'Brien sl->sl_ncylinders = lp->d_ncylinders; 472fd43aa1cSDavid E. O'Brien sl->sl_acylinders = lp->d_acylinders; 473fd43aa1cSDavid E. O'Brien sl->sl_ntracks = lp->d_ntracks; 474fd43aa1cSDavid E. O'Brien sl->sl_nsectors = lp->d_nsectors; 475fd43aa1cSDavid E. O'Brien sl->sl_magic = SUN_DKMAGIC; 476fd43aa1cSDavid E. O'Brien secpercyl = sl->sl_nsectors * sl->sl_ntracks; 477fd43aa1cSDavid E. O'Brien for (i = 0; i < 8; i++) { 478fd43aa1cSDavid E. O'Brien spp = &sl->sl_part[i]; 479fd43aa1cSDavid E. O'Brien npp = &lp->d_partitions[i]; 480fd43aa1cSDavid E. O'Brien /* 481fd43aa1cSDavid E. O'Brien * SunOS partitions must start on a cylinder 482fd43aa1cSDavid E. O'Brien * boundary. Note this restriction is forced 483fd43aa1cSDavid E. O'Brien * upon FreeBSD/sparc64 labels too, since we 484fd43aa1cSDavid E. O'Brien * want to keep both labels synchronised. 485fd43aa1cSDavid E. O'Brien */ 486fd43aa1cSDavid E. O'Brien spp->sdkp_cyloffset = npp->p_offset / secpercyl; 487fd43aa1cSDavid E. O'Brien spp->sdkp_nsectors = npp->p_size; 488fd43aa1cSDavid E. O'Brien } 489130cd73aSDoug Rabson 490fd43aa1cSDavid E. O'Brien /* Compute the XOR checksum. */ 491fd43aa1cSDavid E. O'Brien sp1 = (u_short *)sl; 492fd43aa1cSDavid E. O'Brien sp2 = (u_short *)(sl + 1); 493fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum = 0; 494fd43aa1cSDavid E. O'Brien while (sp1 < sp2) 495fd43aa1cSDavid E. O'Brien cksum ^= *sp1++; 496fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum; 497fd43aa1cSDavid E. O'Brien #endif 498cc18e4ccSBruce Evans /* 499cc18e4ccSBruce Evans * write enable label sector before write (if necessary), 500cc18e4ccSBruce Evans * disable after writing. 501cc18e4ccSBruce Evans */ 502cc18e4ccSBruce Evans flag = 1; 5038fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, &flag) < 0) 5046bd343a9SPhilippe Charnier warn("ioctl DIOCWLABEL"); 5058fae3551SRodney W. Grimes if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) { 5066bd343a9SPhilippe Charnier warn("write"); 5078fae3551SRodney W. Grimes return (1); 5088fae3551SRodney W. Grimes } 5098fae3551SRodney W. Grimes #if NUMBOOT > 0 5108fae3551SRodney W. Grimes /* 5118fae3551SRodney W. Grimes * Output the remainder of the disklabel 5128fae3551SRodney W. Grimes */ 5138fae3551SRodney W. Grimes if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 5146bd343a9SPhilippe Charnier warn("write"); 5158fae3551SRodney W. Grimes return(1); 5168fae3551SRodney W. Grimes } 5178fae3551SRodney W. Grimes #endif 5188fae3551SRodney W. Grimes flag = 0; 5198fae3551SRodney W. Grimes (void) ioctl(f, DIOCWLABEL, &flag); 5208fae3551SRodney W. Grimes } else if (ioctl(f, DIOCWDINFO, lp) < 0) { 5218fae3551SRodney W. Grimes l_perror("ioctl DIOCWDINFO"); 5228fae3551SRodney W. Grimes return (1); 5238fae3551SRodney W. Grimes } 5243233afaeSJohn W. De Boskey } 5258fae3551SRodney W. Grimes return (0); 5268fae3551SRodney W. Grimes } 5278fae3551SRodney W. Grimes 52861de51caSJoerg Wunsch void 529326c7cdaSWarner Losh l_perror(const char *s) 5308fae3551SRodney W. Grimes { 5316bd343a9SPhilippe Charnier switch (errno) { 5328fae3551SRodney W. Grimes 5338fae3551SRodney W. Grimes case ESRCH: 5343121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 5350b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 5368fae3551SRodney W. Grimes break; 5378fae3551SRodney W. Grimes 5388fae3551SRodney W. Grimes case EINVAL: 5393121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 5403121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 5418fae3551SRodney W. Grimes break; 5428fae3551SRodney W. Grimes 5438fae3551SRodney W. Grimes case EBUSY: 5446bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 5458fae3551SRodney W. Grimes break; 5468fae3551SRodney W. Grimes 5478fae3551SRodney W. Grimes case EXDEV: 5487de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 5497de06420SBruce Evans s, 'a' + RAW_PART); 5508fae3551SRodney W. Grimes break; 5518fae3551SRodney W. Grimes 5528fae3551SRodney W. Grimes default: 5536bd343a9SPhilippe Charnier warn((char *)NULL); 5548fae3551SRodney W. Grimes break; 5558fae3551SRodney W. Grimes } 5568fae3551SRodney W. Grimes } 5578fae3551SRodney W. Grimes 5588fae3551SRodney W. Grimes /* 5598fae3551SRodney W. Grimes * Fetch disklabel for disk. 5608fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 5618fae3551SRodney W. Grimes */ 5628fae3551SRodney W. Grimes struct disklabel * 563326c7cdaSWarner Losh readlabel(int f) 5648fae3551SRodney W. Grimes { 565326c7cdaSWarner Losh struct disklabel *lp; 5668fae3551SRodney W. Grimes 5678fae3551SRodney W. Grimes if (rflag) { 5688fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 569bef2080aSPhilippe Charnier err(4, "%s", specname); 5708fae3551SRodney W. Grimes for (lp = (struct disklabel *)bootarea; 5718fae3551SRodney W. Grimes lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 5728fae3551SRodney W. Grimes lp = (struct disklabel *)((char *)lp + 16)) 5738fae3551SRodney W. Grimes if (lp->d_magic == DISKMAGIC && 5748fae3551SRodney W. Grimes lp->d_magic2 == DISKMAGIC) 5758fae3551SRodney W. Grimes break; 5768fae3551SRodney W. Grimes if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 5778fae3551SRodney W. Grimes lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 5783121d4cbSPhilippe Charnier dkcksum(lp) != 0) 5793121d4cbSPhilippe Charnier errx(1, 5803121d4cbSPhilippe Charnier "bad pack magic number (label is damaged, or pack is unlabeled)"); 5818fae3551SRodney W. Grimes } else { 5828fae3551SRodney W. Grimes lp = &lab; 5838fae3551SRodney W. Grimes if (ioctl(f, DIOCGDINFO, lp) < 0) 584bef2080aSPhilippe Charnier err(4, "ioctl DIOCGDINFO"); 5858fae3551SRodney W. Grimes } 5868fae3551SRodney W. Grimes return (lp); 5878fae3551SRodney W. Grimes } 5888fae3551SRodney W. Grimes 5898fae3551SRodney W. Grimes /* 5908fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 5918fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 5928fae3551SRodney W. Grimes */ 5938fae3551SRodney W. Grimes struct disklabel * 594326c7cdaSWarner Losh makebootarea(char *boot, struct disklabel *dp, int f) 5958fae3551SRodney W. Grimes { 59621c729c2SBruce Evans struct disklabel *lp; 597326c7cdaSWarner Losh char *p; 5988fae3551SRodney W. Grimes int b; 5998fae3551SRodney W. Grimes #if NUMBOOT > 0 6008fae3551SRodney W. Grimes char *dkbasename; 6018fae3551SRodney W. Grimes struct stat sb; 602cc18e4ccSBruce Evans #endif 603cc18e4ccSBruce Evans #ifdef __alpha__ 60421c729c2SBruce Evans u_long *bootinfo; 605cc18e4ccSBruce Evans int n; 606cc18e4ccSBruce Evans #endif 607d70e4e53SJoerg Wunsch #ifdef __i386__ 608d70e4e53SJoerg Wunsch char *tmpbuf; 609d70e4e53SJoerg Wunsch int i, found; 61061de51caSJoerg Wunsch #endif 6118fae3551SRodney W. Grimes 6128fae3551SRodney W. Grimes /* XXX */ 6138fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 6148fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 6158fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 6168fae3551SRodney W. Grimes } 6178fae3551SRodney W. Grimes lp = (struct disklabel *) 6188fae3551SRodney W. Grimes (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 6198fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 6208fae3551SRodney W. Grimes #if NUMBOOT > 0 6218fae3551SRodney W. Grimes /* 6228fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 6238fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 6248fae3551SRodney W. Grimes * clobber the existing boot. 6258fae3551SRodney W. Grimes */ 6268fae3551SRodney W. Grimes if (!installboot) { 6278fae3551SRodney W. Grimes if (rflag) { 6288fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 629bef2080aSPhilippe Charnier err(4, "%s", specname); 6308fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 6318fae3551SRodney W. Grimes } 6328fae3551SRodney W. Grimes return (lp); 6338fae3551SRodney W. Grimes } 6348fae3551SRodney W. Grimes /* 6358fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 6368fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 6378fae3551SRodney W. Grimes */ 6388fae3551SRodney W. Grimes if (!xxboot || !bootxx) { 6398fae3551SRodney W. Grimes dkbasename = np; 6408fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 6418fae3551SRodney W. Grimes p = dkname; 6428fae3551SRodney W. Grimes else 6438fae3551SRodney W. Grimes p++; 6448fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 6458fae3551SRodney W. Grimes *np++ = *p++; 6468fae3551SRodney W. Grimes *np++ = '\0'; 6478fae3551SRodney W. Grimes 6488fae3551SRodney W. Grimes if (!xxboot) { 649efba76d7SJordan K. Hubbard (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); 650efba76d7SJordan K. Hubbard xxboot = boot0; 6518fae3551SRodney W. Grimes } 6528fae3551SRodney W. Grimes #if NUMBOOT > 1 6538fae3551SRodney W. Grimes if (!bootxx) { 654efba76d7SJordan K. Hubbard (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); 655efba76d7SJordan K. Hubbard bootxx = boot1; 6568fae3551SRodney W. Grimes } 6578fae3551SRodney W. Grimes #endif 6588fae3551SRodney W. Grimes } 6598fae3551SRodney W. Grimes #ifdef DEBUG 6608fae3551SRodney W. Grimes if (debug) 6618fae3551SRodney W. Grimes fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n", 6628fae3551SRodney W. Grimes xxboot, bootxx ? bootxx : "NONE"); 6638fae3551SRodney W. Grimes #endif 6648fae3551SRodney W. Grimes 6658fae3551SRodney W. Grimes /* 6668fae3551SRodney W. Grimes * Strange rules: 667fd43aa1cSDavid E. O'Brien * 1. One-piece bootstrap (hp300/hp800) 66890b8f6baSDavid E. O'Brien * 1. One-piece bootstrap (alpha/sparc64) 6698fae3551SRodney W. Grimes * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 6708fae3551SRodney W. Grimes * is remembered and written later following the bootarea. 671fd43aa1cSDavid E. O'Brien * 2. Two-piece bootstraps (i386/ia64) 6728fae3551SRodney W. Grimes * up to d_secsize bytes of ``xxboot'' go in first d_secsize 6738fae3551SRodney W. Grimes * bytes of bootarea, remaining d_bbsize-d_secsize filled 6748fae3551SRodney W. Grimes * from ``bootxx''. 6758fae3551SRodney W. Grimes */ 6768fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 6778fae3551SRodney W. Grimes if (b < 0) 678bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6798fae3551SRodney W. Grimes #if NUMBOOT > 1 680d70e4e53SJoerg Wunsch #ifdef __i386__ 681d70e4e53SJoerg Wunsch /* 682d70e4e53SJoerg Wunsch * XXX Botch alert. 683d70e4e53SJoerg Wunsch * The i386 has the so-called fdisk table embedded into the 684d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 685d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 686d70e4e53SJoerg Wunsch * the xxboot provides a template.) 687d70e4e53SJoerg Wunsch */ 688d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 689bef2080aSPhilippe Charnier err(4, "%s", xxboot); 690d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 691d70e4e53SJoerg Wunsch #endif /* i386 */ 6928fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_secsize) < 0) 693bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6948fae3551SRodney W. Grimes (void)close(b); 695d70e4e53SJoerg Wunsch #ifdef __i386__ 696d70e4e53SJoerg Wunsch for (i = DOSPARTOFF, found = 0; 697d70e4e53SJoerg Wunsch !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition); 698d70e4e53SJoerg Wunsch i++) 699d70e4e53SJoerg Wunsch found = tmpbuf[i] != 0; 700d70e4e53SJoerg Wunsch if (found) 701d70e4e53SJoerg Wunsch memcpy((void *)&boot[DOSPARTOFF], 702d70e4e53SJoerg Wunsch (void *)&tmpbuf[DOSPARTOFF], 703d70e4e53SJoerg Wunsch NDOSPART * sizeof(struct dos_partition)); 704d70e4e53SJoerg Wunsch free(tmpbuf); 705d70e4e53SJoerg Wunsch #endif /* i386 */ 7068fae3551SRodney W. Grimes b = open(bootxx, O_RDONLY); 7078fae3551SRodney W. Grimes if (b < 0) 708bef2080aSPhilippe Charnier err(4, "%s", bootxx); 7096cabb348SBruce Evans if (fstat(b, &sb) != 0) 7106cabb348SBruce Evans err(4, "%s", bootxx); 7116cabb348SBruce Evans if (dp->d_secsize + sb.st_size > dp->d_bbsize) 7126cabb348SBruce Evans errx(4, "%s too large", bootxx); 71361de51caSJoerg Wunsch if (read(b, &boot[dp->d_secsize], 71461de51caSJoerg Wunsch (int)(dp->d_bbsize-dp->d_secsize)) < 0) 715bef2080aSPhilippe Charnier err(4, "%s", bootxx); 716cc18e4ccSBruce Evans #else /* !(NUMBOOT > 1) */ 717130cd73aSDoug Rabson #ifdef __alpha__ 718130cd73aSDoug Rabson /* 719130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 720130cd73aSDoug Rabson * second sector of the boot area. The first sector 721130cd73aSDoug Rabson * contains the label and must be edited to contain the 722130cd73aSDoug Rabson * size and location of the primary bootstrap. 723130cd73aSDoug Rabson */ 724cc18e4ccSBruce Evans n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 725130cd73aSDoug Rabson if (n < 0) 726130cd73aSDoug Rabson err(4, "%s", xxboot); 72721c729c2SBruce Evans bootinfo = (u_long *)(boot + 480); 72821c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 72921c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 73021c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 731cc18e4ccSBruce Evans #else /* !__alpha__ */ 7328fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_bbsize) < 0) 733bef2080aSPhilippe Charnier err(4, "%s", xxboot); 734cc18e4ccSBruce Evans #endif /* __alpha__ */ 7356cabb348SBruce Evans if (fstat(b, &sb) != 0) 7366cabb348SBruce Evans err(4, "%s", xxboot); 7378fae3551SRodney W. Grimes bootsize = (int)sb.st_size - dp->d_bbsize; 7388fae3551SRodney W. Grimes if (bootsize > 0) { 7398fae3551SRodney W. Grimes /* XXX assume d_secsize is a power of two */ 7408fae3551SRodney W. Grimes bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 7418fae3551SRodney W. Grimes bootbuf = (char *)malloc((size_t)bootsize); 7428fae3551SRodney W. Grimes if (bootbuf == 0) 743bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7448fae3551SRodney W. Grimes if (read(b, bootbuf, bootsize) < 0) { 7458fae3551SRodney W. Grimes free(bootbuf); 746bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7478fae3551SRodney W. Grimes } 7488fae3551SRodney W. Grimes } 749cc18e4ccSBruce Evans #endif /* NUMBOOT > 1 */ 7508fae3551SRodney W. Grimes (void)close(b); 751cc18e4ccSBruce Evans #endif /* NUMBOOT > 0 */ 7528fae3551SRodney W. Grimes /* 7538fae3551SRodney W. Grimes * Make sure no part of the bootstrap is written in the area 7548fae3551SRodney W. Grimes * reserved for the label. 7558fae3551SRodney W. Grimes */ 7568fae3551SRodney W. Grimes for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 7576bd343a9SPhilippe Charnier if (*p) 7586bd343a9SPhilippe Charnier errx(2, "bootstrap doesn't leave room for disk label"); 7598fae3551SRodney W. Grimes return (lp); 7608fae3551SRodney W. Grimes } 7618fae3551SRodney W. Grimes 76261de51caSJoerg Wunsch void 763326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 7648fae3551SRodney W. Grimes { 765326c7cdaSWarner Losh int i, j; 766326c7cdaSWarner Losh const struct partition *pp; 7678fae3551SRodney W. Grimes 7688fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 7698fae3551SRodney W. Grimes if ((unsigned) lp->d_type < DKMAXTYPES) 7708fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 7718fae3551SRodney W. Grimes else 7722a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 77361de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 77461de51caSJoerg Wunsch lp->d_typename); 77561de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 77661de51caSJoerg Wunsch lp->d_packname); 7778fae3551SRodney W. Grimes fprintf(f, "flags:"); 7788fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 7798fae3551SRodney W. Grimes fprintf(f, " removeable"); 7808fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 7818fae3551SRodney W. Grimes fprintf(f, " ecc"); 7828fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 7838fae3551SRodney W. Grimes fprintf(f, " badsect"); 7848fae3551SRodney W. Grimes fprintf(f, "\n"); 7852a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 7862a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 7872a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 7882a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 7892a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 7902a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 7912a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 7922a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 7932a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 7942a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 7952a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 7962a1deaaaSBruce Evans (u_long)lp->d_headswitch); 79761de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 7982a1deaaaSBruce Evans (u_long)lp->d_trkseek); 7998fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 8008fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 8018fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 8028fae3551SRodney W. Grimes break; 8038fae3551SRodney W. Grimes if (i < 0) 8048fae3551SRodney W. Grimes i = 0; 8058fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 8062a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 8072a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 8088fae3551SRodney W. Grimes fprintf(f, 809ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 8108fae3551SRodney W. Grimes pp = lp->d_partitions; 8118fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 8128fae3551SRodney W. Grimes if (pp->p_size) { 8132a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 8142a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 8158fae3551SRodney W. Grimes if ((unsigned) pp->p_fstype < FSMAXTYPES) 8168fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 8178fae3551SRodney W. Grimes else 8188fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 8198fae3551SRodney W. Grimes switch (pp->p_fstype) { 8208fae3551SRodney W. Grimes 8218fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 8222a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 8232a1deaaaSBruce Evans (u_long)pp->p_fsize, 8242a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 8258fae3551SRodney W. Grimes break; 8268fae3551SRodney W. Grimes 8278fae3551SRodney W. Grimes case FS_BSDFFS: 8282a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 8292a1deaaaSBruce Evans (u_long)pp->p_fsize, 8302a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 8318fae3551SRodney W. Grimes pp->p_cpg); 8328fae3551SRodney W. Grimes break; 8338fae3551SRodney W. Grimes 834ca4693edSJustin T. Gibbs case FS_BSDLFS: 8352a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 8362a1deaaaSBruce Evans (u_long)pp->p_fsize, 8372a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 838ca4693edSJustin T. Gibbs pp->p_cpg); 839ca4693edSJustin T. Gibbs break; 840ca4693edSJustin T. Gibbs 8418fae3551SRodney W. Grimes default: 8428fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 8438fae3551SRodney W. Grimes break; 8448fae3551SRodney W. Grimes } 8452a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 8462a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 8478fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 8488fae3551SRodney W. Grimes putc('*', f); 8498fae3551SRodney W. Grimes else 8508fae3551SRodney W. Grimes putc(' ', f); 8512a1deaaaSBruce Evans fprintf(f, "- %lu", 8522a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 8532a1deaaaSBruce Evans lp->d_secpercyl - 1) / 8542a1deaaaSBruce Evans lp->d_secpercyl - 1)); 8558fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 8568fae3551SRodney W. Grimes putc('*', f); 8578fae3551SRodney W. Grimes fprintf(f, ")\n"); 8588fae3551SRodney W. Grimes } 8598fae3551SRodney W. Grimes } 8608fae3551SRodney W. Grimes fflush(f); 8618fae3551SRodney W. Grimes } 8628fae3551SRodney W. Grimes 86361de51caSJoerg Wunsch int 864326c7cdaSWarner Losh edit(struct disklabel *lp, int f) 8658fae3551SRodney W. Grimes { 866326c7cdaSWarner Losh int c, fd; 8678fae3551SRodney W. Grimes struct disklabel label; 868722ceb3fSWarner Losh FILE *fp; 8698fae3551SRodney W. Grimes 870722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 871722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 8726bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 8738fae3551SRodney W. Grimes return (1); 8748fae3551SRodney W. Grimes } 875722ceb3fSWarner Losh display(fp, lp); 876722ceb3fSWarner Losh fclose(fp); 8778fae3551SRodney W. Grimes for (;;) { 8788fae3551SRodney W. Grimes if (!editit()) 8798fae3551SRodney W. Grimes break; 880722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 881722ceb3fSWarner Losh if (fp == NULL) { 8826bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 8838fae3551SRodney W. Grimes break; 8848fae3551SRodney W. Grimes } 8858fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 886722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 8878fae3551SRodney W. Grimes *lp = label; 8888fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 889722ceb3fSWarner Losh fclose(fp); 8908fae3551SRodney W. Grimes (void) unlink(tmpfil); 8918fae3551SRodney W. Grimes return (0); 8928fae3551SRodney W. Grimes } 8938fae3551SRodney W. Grimes } 894722ceb3fSWarner Losh fclose(fp); 8958fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 8968fae3551SRodney W. Grimes c = getchar(); 8978fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 8988fae3551SRodney W. Grimes while (getchar() != (int)'\n') 8998fae3551SRodney W. Grimes ; 9008fae3551SRodney W. Grimes if (c == (int)'n') 9018fae3551SRodney W. Grimes break; 9028fae3551SRodney W. Grimes } 9038fae3551SRodney W. Grimes (void) unlink(tmpfil); 9048fae3551SRodney W. Grimes return (1); 9058fae3551SRodney W. Grimes } 9068fae3551SRodney W. Grimes 90761de51caSJoerg Wunsch int 908326c7cdaSWarner Losh editit(void) 9098fae3551SRodney W. Grimes { 910326c7cdaSWarner Losh int pid, xpid; 9118fae3551SRodney W. Grimes int stat, omask; 912326c7cdaSWarner Losh char *ed; 9138fae3551SRodney W. Grimes 9148fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 9158fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 9168fae3551SRodney W. Grimes if (errno == EPROCLIM) { 9176bd343a9SPhilippe Charnier warnx("you have too many processes"); 9188fae3551SRodney W. Grimes return(0); 9198fae3551SRodney W. Grimes } 9208fae3551SRodney W. Grimes if (errno != EAGAIN) { 9216bd343a9SPhilippe Charnier warn("fork"); 9228fae3551SRodney W. Grimes return(0); 9238fae3551SRodney W. Grimes } 9248fae3551SRodney W. Grimes sleep(1); 9258fae3551SRodney W. Grimes } 9268fae3551SRodney W. Grimes if (pid == 0) { 9278fae3551SRodney W. Grimes sigsetmask(omask); 9288fae3551SRodney W. Grimes setgid(getgid()); 9298fae3551SRodney W. Grimes setuid(getuid()); 9308fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 9318fae3551SRodney W. Grimes ed = DEFEDITOR; 9327bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 9336bd343a9SPhilippe Charnier err(1, "%s", ed); 9348fae3551SRodney W. Grimes } 9358fae3551SRodney W. Grimes while ((xpid = wait(&stat)) >= 0) 9368fae3551SRodney W. Grimes if (xpid == pid) 9378fae3551SRodney W. Grimes break; 9388fae3551SRodney W. Grimes sigsetmask(omask); 9398fae3551SRodney W. Grimes return(!stat); 9408fae3551SRodney W. Grimes } 9418fae3551SRodney W. Grimes 9428fae3551SRodney W. Grimes char * 943326c7cdaSWarner Losh skip(char *cp) 9448fae3551SRodney W. Grimes { 9458fae3551SRodney W. Grimes 9468fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 9478fae3551SRodney W. Grimes cp++; 9488fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 949326c7cdaSWarner Losh return (NULL); 9508fae3551SRodney W. Grimes return (cp); 9518fae3551SRodney W. Grimes } 9528fae3551SRodney W. Grimes 9538fae3551SRodney W. Grimes char * 954326c7cdaSWarner Losh word(char *cp) 9558fae3551SRodney W. Grimes { 956326c7cdaSWarner Losh char c; 9578fae3551SRodney W. Grimes 9588fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 9598fae3551SRodney W. Grimes cp++; 9608fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 9618fae3551SRodney W. Grimes *cp++ = '\0'; 9628fae3551SRodney W. Grimes if (c != '#') 9638fae3551SRodney W. Grimes return (skip(cp)); 9648fae3551SRodney W. Grimes } 965326c7cdaSWarner Losh return (NULL); 9668fae3551SRodney W. Grimes } 9678fae3551SRodney W. Grimes 9688fae3551SRodney W. Grimes /* 9698fae3551SRodney W. Grimes * Read an ascii label in from fd f, 9708fae3551SRodney W. Grimes * in the same format as that put out by display(), 9718fae3551SRodney W. Grimes * and fill in lp. 9728fae3551SRodney W. Grimes */ 97361de51caSJoerg Wunsch int 974326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 9758fae3551SRodney W. Grimes { 976326c7cdaSWarner Losh char **cpp, *cp; 977326c7cdaSWarner Losh struct partition *pp; 9783233afaeSJohn W. De Boskey unsigned int part; 9798fae3551SRodney W. Grimes char *tp, *s, line[BUFSIZ]; 9808fae3551SRodney W. Grimes int v, lineno = 0, errors = 0; 981326c7cdaSWarner Losh int i; 9828fae3551SRodney W. Grimes 9838fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 9848fae3551SRodney W. Grimes lp->d_sbsize = SBSIZE; /* XXX */ 9858fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 9868fae3551SRodney W. Grimes lineno++; 98761de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 9888fae3551SRodney W. Grimes *cp = '\0'; 9898fae3551SRodney W. Grimes cp = skip(line); 9908fae3551SRodney W. Grimes if (cp == NULL) 9918fae3551SRodney W. Grimes continue; 9928fae3551SRodney W. Grimes tp = index(cp, ':'); 9938fae3551SRodney W. Grimes if (tp == NULL) { 9948fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 9958fae3551SRodney W. Grimes errors++; 9968fae3551SRodney W. Grimes continue; 9978fae3551SRodney W. Grimes } 9988fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 9998fae3551SRodney W. Grimes if (streq(cp, "type")) { 10008fae3551SRodney W. Grimes if (tp == NULL) 10018fae3551SRodney W. Grimes tp = "unknown"; 10028fae3551SRodney W. Grimes cpp = dktypenames; 10038fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 10048fae3551SRodney W. Grimes if ((s = *cpp) && streq(s, tp)) { 10058fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 10068fae3551SRodney W. Grimes goto next; 10078fae3551SRodney W. Grimes } 10088fae3551SRodney W. Grimes v = atoi(tp); 10098fae3551SRodney W. Grimes if ((unsigned)v >= DKMAXTYPES) 10108fae3551SRodney W. Grimes fprintf(stderr, "line %d:%s %d\n", lineno, 10118fae3551SRodney W. Grimes "Warning, unknown disk type", v); 10128fae3551SRodney W. Grimes lp->d_type = v; 10138fae3551SRodney W. Grimes continue; 10148fae3551SRodney W. Grimes } 10158fae3551SRodney W. Grimes if (streq(cp, "flags")) { 10168fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 10178fae3551SRodney W. Grimes tp = word(cp); 10188fae3551SRodney W. Grimes if (streq(cp, "removeable")) 10198fae3551SRodney W. Grimes v |= D_REMOVABLE; 10208fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 10218fae3551SRodney W. Grimes v |= D_ECC; 10228fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 10238fae3551SRodney W. Grimes v |= D_BADSECT; 10248fae3551SRodney W. Grimes else { 10258fae3551SRodney W. Grimes fprintf(stderr, 10268fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 10278fae3551SRodney W. Grimes lineno, cp); 10288fae3551SRodney W. Grimes errors++; 10298fae3551SRodney W. Grimes } 10308fae3551SRodney W. Grimes } 10318fae3551SRodney W. Grimes lp->d_flags = v; 10328fae3551SRodney W. Grimes continue; 10338fae3551SRodney W. Grimes } 10348fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 10358fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 10368fae3551SRodney W. Grimes lp->d_drivedata[i++] = atoi(cp); 10378fae3551SRodney W. Grimes tp = word(cp); 10388fae3551SRodney W. Grimes } 10398fae3551SRodney W. Grimes continue; 10408fae3551SRodney W. Grimes } 10418fae3551SRodney W. Grimes if (sscanf(cp, "%d partitions", &v) == 1) { 10428fae3551SRodney W. Grimes if (v == 0 || (unsigned)v > MAXPARTITIONS) { 10438fae3551SRodney W. Grimes fprintf(stderr, 10448fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 10458fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 10468fae3551SRodney W. Grimes errors++; 10478fae3551SRodney W. Grimes } else 10488fae3551SRodney W. Grimes lp->d_npartitions = v; 10498fae3551SRodney W. Grimes continue; 10508fae3551SRodney W. Grimes } 10518fae3551SRodney W. Grimes if (tp == NULL) 10528fae3551SRodney W. Grimes tp = ""; 10538fae3551SRodney W. Grimes if (streq(cp, "disk")) { 10548fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 10558fae3551SRodney W. Grimes continue; 10568fae3551SRodney W. Grimes } 10578fae3551SRodney W. Grimes if (streq(cp, "label")) { 10588fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 10598fae3551SRodney W. Grimes continue; 10608fae3551SRodney W. Grimes } 10618fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 10628fae3551SRodney W. Grimes v = atoi(tp); 10636cabb348SBruce Evans if (v <= 0 || (v % DEV_BSIZE) != 0) { 10648fae3551SRodney W. Grimes fprintf(stderr, 10658fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 10668fae3551SRodney W. Grimes lineno, tp); 10678fae3551SRodney W. Grimes errors++; 10688fae3551SRodney W. Grimes } else 10698fae3551SRodney W. Grimes lp->d_secsize = v; 10708fae3551SRodney W. Grimes continue; 10718fae3551SRodney W. Grimes } 10728fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 10738fae3551SRodney W. Grimes v = atoi(tp); 10748fae3551SRodney W. Grimes if (v <= 0) { 10758fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10768fae3551SRodney W. Grimes lineno, tp, cp); 10778fae3551SRodney W. Grimes errors++; 10788fae3551SRodney W. Grimes } else 10798fae3551SRodney W. Grimes lp->d_nsectors = v; 10808fae3551SRodney W. Grimes continue; 10818fae3551SRodney W. Grimes } 10828fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 10838fae3551SRodney W. Grimes v = atoi(tp); 10848fae3551SRodney W. Grimes if (v <= 0) { 10858fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10868fae3551SRodney W. Grimes lineno, tp, cp); 10878fae3551SRodney W. Grimes errors++; 10888fae3551SRodney W. Grimes } else 10898fae3551SRodney W. Grimes lp->d_secpercyl = v; 10908fae3551SRodney W. Grimes continue; 10918fae3551SRodney W. Grimes } 10928fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 10938fae3551SRodney W. Grimes v = atoi(tp); 10948fae3551SRodney W. Grimes if (v <= 0) { 10958fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10968fae3551SRodney W. Grimes lineno, tp, cp); 10978fae3551SRodney W. Grimes errors++; 10988fae3551SRodney W. Grimes } else 10998fae3551SRodney W. Grimes lp->d_ntracks = v; 11008fae3551SRodney W. Grimes continue; 11018fae3551SRodney W. Grimes } 11028fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 11038fae3551SRodney W. Grimes v = atoi(tp); 11048fae3551SRodney W. Grimes if (v <= 0) { 11058fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11068fae3551SRodney W. Grimes lineno, tp, cp); 11078fae3551SRodney W. Grimes errors++; 11088fae3551SRodney W. Grimes } else 11098fae3551SRodney W. Grimes lp->d_ncylinders = v; 11108fae3551SRodney W. Grimes continue; 11118fae3551SRodney W. Grimes } 1112f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 1113f75dd518SBruce Evans v = atoi(tp); 1114f75dd518SBruce Evans if (v <= 0) { 1115f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 1116f75dd518SBruce Evans lineno, tp, cp); 1117f75dd518SBruce Evans errors++; 1118f75dd518SBruce Evans } else 1119f75dd518SBruce Evans lp->d_secperunit = v; 1120f75dd518SBruce Evans continue; 1121f75dd518SBruce Evans } 11228fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 11238fae3551SRodney W. Grimes v = atoi(tp); 11248fae3551SRodney W. Grimes if (v <= 0) { 11258fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11268fae3551SRodney W. Grimes lineno, tp, cp); 11278fae3551SRodney W. Grimes errors++; 11288fae3551SRodney W. Grimes } else 11298fae3551SRodney W. Grimes lp->d_rpm = v; 11308fae3551SRodney W. Grimes continue; 11318fae3551SRodney W. Grimes } 11328fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 11338fae3551SRodney W. Grimes v = atoi(tp); 11348fae3551SRodney W. Grimes if (v <= 0) { 11358fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11368fae3551SRodney W. Grimes lineno, tp, cp); 11378fae3551SRodney W. Grimes errors++; 11388fae3551SRodney W. Grimes } else 11398fae3551SRodney W. Grimes lp->d_interleave = v; 11408fae3551SRodney W. Grimes continue; 11418fae3551SRodney W. Grimes } 11428fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 11438fae3551SRodney W. Grimes v = atoi(tp); 11448fae3551SRodney W. Grimes if (v < 0) { 11458fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11468fae3551SRodney W. Grimes lineno, tp, cp); 11478fae3551SRodney W. Grimes errors++; 11488fae3551SRodney W. Grimes } else 11498fae3551SRodney W. Grimes lp->d_trackskew = v; 11508fae3551SRodney W. Grimes continue; 11518fae3551SRodney W. Grimes } 11528fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 11538fae3551SRodney W. Grimes v = atoi(tp); 11548fae3551SRodney W. Grimes if (v < 0) { 11558fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11568fae3551SRodney W. Grimes lineno, tp, cp); 11578fae3551SRodney W. Grimes errors++; 11588fae3551SRodney W. Grimes } else 11598fae3551SRodney W. Grimes lp->d_cylskew = v; 11608fae3551SRodney W. Grimes continue; 11618fae3551SRodney W. Grimes } 11628fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 11638fae3551SRodney W. Grimes v = atoi(tp); 11648fae3551SRodney W. Grimes if (v < 0) { 11658fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11668fae3551SRodney W. Grimes lineno, tp, cp); 11678fae3551SRodney W. Grimes errors++; 11688fae3551SRodney W. Grimes } else 11698fae3551SRodney W. Grimes lp->d_headswitch = v; 11708fae3551SRodney W. Grimes continue; 11718fae3551SRodney W. Grimes } 11728fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 11738fae3551SRodney W. Grimes v = atoi(tp); 11748fae3551SRodney W. Grimes if (v < 0) { 11758fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11768fae3551SRodney W. Grimes lineno, tp, cp); 11778fae3551SRodney W. Grimes errors++; 11788fae3551SRodney W. Grimes } else 11798fae3551SRodney W. Grimes lp->d_trkseek = v; 11808fae3551SRodney W. Grimes continue; 11818fae3551SRodney W. Grimes } 11823233afaeSJohn W. De Boskey /* the ':' was removed above */ 11833233afaeSJohn W. De Boskey if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') { 11843233afaeSJohn W. De Boskey part = *cp - 'a'; 11853233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 11868fae3551SRodney W. Grimes fprintf(stderr, 11873233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 11883233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 11898fae3551SRodney W. Grimes errors++; 11908fae3551SRodney W. Grimes continue; 11918fae3551SRodney W. Grimes } 11928fae3551SRodney W. Grimes pp = &lp->d_partitions[part]; 11933233afaeSJohn W. De Boskey part_set[part] = 1; 11948fae3551SRodney W. Grimes #define NXTNUM(n) { \ 119513e0abcbSPaul Traina if (tp == NULL) { \ 119613e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 119713e0abcbSPaul Traina errors++; \ 119813e0abcbSPaul Traina break; \ 119913e0abcbSPaul Traina } else { \ 12008fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 12018fae3551SRodney W. Grimes if (tp == NULL) \ 12028fae3551SRodney W. Grimes tp = cp; \ 12038fae3551SRodney W. Grimes (n) = atoi(cp); \ 120413e0abcbSPaul Traina } \ 12058fae3551SRodney W. Grimes } 12063233afaeSJohn W. De Boskey /* retain 1 character following number */ 12073233afaeSJohn W. De Boskey #define NXTWORD(w,n) { \ 12083233afaeSJohn W. De Boskey if (tp == NULL) { \ 12093233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 12103233afaeSJohn W. De Boskey errors++; \ 12113233afaeSJohn W. De Boskey break; \ 12123233afaeSJohn W. De Boskey } else { \ 12133233afaeSJohn W. De Boskey char *tmp; \ 12143233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 12153233afaeSJohn W. De Boskey if (tp == NULL) \ 12163233afaeSJohn W. De Boskey tp = cp; \ 12173233afaeSJohn W. De Boskey (n) = strtol(cp,&tmp,10); \ 12183233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 12193233afaeSJohn W. De Boskey } \ 12203233afaeSJohn W. De Boskey } 12213233afaeSJohn W. De Boskey v = 0; 12223233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 12233233afaeSJohn W. De Boskey if (v < 0 || (v == 0 && part_size_type[part] != '*')) { 12248fae3551SRodney W. Grimes fprintf(stderr, 12258fae3551SRodney W. Grimes "line %d: %s: bad partition size\n", 12268fae3551SRodney W. Grimes lineno, cp); 12278fae3551SRodney W. Grimes errors++; 12283233afaeSJohn W. De Boskey break; 12293233afaeSJohn W. De Boskey } else { 12308fae3551SRodney W. Grimes pp->p_size = v; 12313233afaeSJohn W. De Boskey 12323233afaeSJohn W. De Boskey v = 0; 12333233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 12343233afaeSJohn W. De Boskey if (v < 0 || (v == 0 && 12353233afaeSJohn W. De Boskey part_offset_type[part] != '*' && 12363233afaeSJohn W. De Boskey part_offset_type[part] != '\0')) { 12378fae3551SRodney W. Grimes fprintf(stderr, 12388fae3551SRodney W. Grimes "line %d: %s: bad partition offset\n", 12398fae3551SRodney W. Grimes lineno, cp); 12408fae3551SRodney W. Grimes errors++; 12413233afaeSJohn W. De Boskey break; 12423233afaeSJohn W. De Boskey } else { 12438fae3551SRodney W. Grimes pp->p_offset = v; 12448fae3551SRodney W. Grimes cp = tp, tp = word(cp); 12458fae3551SRodney W. Grimes cpp = fstypenames; 12468fae3551SRodney W. Grimes for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) 12478fae3551SRodney W. Grimes if ((s = *cpp) && streq(s, cp)) { 12483233afaeSJohn W. De Boskey pp->p_fstype = cpp - 12493233afaeSJohn W. De Boskey fstypenames; 12508fae3551SRodney W. Grimes goto gottype; 12518fae3551SRodney W. Grimes } 12528fae3551SRodney W. Grimes if (isdigit(*cp)) 12538fae3551SRodney W. Grimes v = atoi(cp); 12548fae3551SRodney W. Grimes else 12558fae3551SRodney W. Grimes v = FSMAXTYPES; 12568fae3551SRodney W. Grimes if ((unsigned)v >= FSMAXTYPES) { 12573233afaeSJohn W. De Boskey fprintf(stderr, 12583233afaeSJohn W. De Boskey "line %d: Warning, unknown " 12593233afaeSJohn W. De Boskey "filesystem type %s\n", 12603233afaeSJohn W. De Boskey lineno, cp); 12618fae3551SRodney W. Grimes v = FS_UNUSED; 12628fae3551SRodney W. Grimes } 12638fae3551SRodney W. Grimes pp->p_fstype = v; 12643233afaeSJohn W. De Boskey gottype:; 12653233afaeSJohn W. De Boskey /* 12663233afaeSJohn W. De Boskey * Note: NXTNUM will break us out of the 12673233afaeSJohn W. De Boskey * switch only! 12683233afaeSJohn W. De Boskey */ 12698fae3551SRodney W. Grimes switch (pp->p_fstype) { 12703233afaeSJohn W. De Boskey case FS_UNUSED: 12713233afaeSJohn W. De Boskey /* 12723233afaeSJohn W. De Boskey * allow us to accept defaults for 12733233afaeSJohn W. De Boskey * fsize/frag/cpg 12743233afaeSJohn W. De Boskey */ 12753233afaeSJohn W. De Boskey if (tp) { 12768fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 12778fae3551SRodney W. Grimes if (pp->p_fsize == 0) 12788fae3551SRodney W. Grimes break; 12798fae3551SRodney W. Grimes NXTNUM(v); 12808fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 12813233afaeSJohn W. De Boskey } 12823233afaeSJohn W. De Boskey /* else default to 0's */ 12838fae3551SRodney W. Grimes break; 12848fae3551SRodney W. Grimes 12853233afaeSJohn W. De Boskey /* These happen to be the same */ 12868fae3551SRodney W. Grimes case FS_BSDFFS: 1287ca4693edSJustin T. Gibbs case FS_BSDLFS: 12883233afaeSJohn W. De Boskey if (tp) { 1289ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1290ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1291ca4693edSJustin T. Gibbs break; 1292ca4693edSJustin T. Gibbs NXTNUM(v); 1293ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1294ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 12953233afaeSJohn W. De Boskey } else { 12963233afaeSJohn W. De Boskey /* 12973233afaeSJohn W. De Boskey * FIX! poor attempt at 12983233afaeSJohn W. De Boskey * adaptive 12993233afaeSJohn W. De Boskey */ 13003233afaeSJohn W. De Boskey /* 1 GB */ 13013233afaeSJohn W. De Boskey if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) { 13023233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 13033233afaeSJohn W. De Boskey pp->p_fsize = DEFAULT_NEWFS_BLOCK; 13043233afaeSJohn W. De Boskey pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG; 13053233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 13063233afaeSJohn W. De Boskey } else { 13073233afaeSJohn W. De Boskey pp->p_fsize = BIG_NEWFS_BLOCK; 13083233afaeSJohn W. De Boskey pp->p_frag = (unsigned char) BIG_NEWFS_FRAG; 13093233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 13103233afaeSJohn W. De Boskey } 13113233afaeSJohn W. De Boskey } 1312ca4693edSJustin T. Gibbs break; 13138fae3551SRodney W. Grimes default: 13148fae3551SRodney W. Grimes break; 13158fae3551SRodney W. Grimes } 13163233afaeSJohn W. De Boskey 13173233afaeSJohn W. De Boskey /* 13183233afaeSJohn W. De Boskey * note: we may not have 13193233afaeSJohn W. De Boskey * gotten all the entries for 13203233afaeSJohn W. De Boskey * the fs though if we didn't, 13213233afaeSJohn W. De Boskey * errors will be set. 13223233afaeSJohn W. De Boskey */ 13233233afaeSJohn W. De Boskey } 13243233afaeSJohn W. De Boskey } 13258fae3551SRodney W. Grimes continue; 13268fae3551SRodney W. Grimes } 13278fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: Unknown disklabel field\n", 13288fae3551SRodney W. Grimes lineno, cp); 13298fae3551SRodney W. Grimes errors++; 13303233afaeSJohn W. De Boskey next:; 13318fae3551SRodney W. Grimes } 13328fae3551SRodney W. Grimes errors += checklabel(lp); 13338fae3551SRodney W. Grimes return (errors == 0); 13348fae3551SRodney W. Grimes } 13358fae3551SRodney W. Grimes 13368fae3551SRodney W. Grimes /* 13378fae3551SRodney W. Grimes * Check disklabel for errors and fill in 13388fae3551SRodney W. Grimes * derived fields according to supplied values. 13398fae3551SRodney W. Grimes */ 134061de51caSJoerg Wunsch int 1341326c7cdaSWarner Losh checklabel(struct disklabel *lp) 13428fae3551SRodney W. Grimes { 1343326c7cdaSWarner Losh struct partition *pp; 13448fae3551SRodney W. Grimes int i, errors = 0; 13458fae3551SRodney W. Grimes char part; 13463233afaeSJohn W. De Boskey unsigned long total_size, total_percent, current_offset; 13473233afaeSJohn W. De Boskey int seen_default_offset; 13483233afaeSJohn W. De Boskey int hog_part; 13493233afaeSJohn W. De Boskey int j; 13503233afaeSJohn W. De Boskey struct partition *pp2; 13518fae3551SRodney W. Grimes 13528fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 13532a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 13548fae3551SRodney W. Grimes return (1); 13558fae3551SRodney W. Grimes } 13568fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 13572a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 13588fae3551SRodney W. Grimes return (1); 13598fae3551SRodney W. Grimes } 13608fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 13612a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 13628fae3551SRodney W. Grimes return (1); 13638fae3551SRodney W. Grimes } 13648fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 13652a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 13668fae3551SRodney W. Grimes errors++; 13678fae3551SRodney W. Grimes } 13688fae3551SRodney W. Grimes if (lp->d_rpm == 0) 13692a1deaaaSBruce Evans Warning("revolutions/minute 0"); 13708fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 13718fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 13728fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 13738fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 13748fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 13752a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 13768fae3551SRodney W. Grimes errors++; 13778fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 13788fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 13798fae3551SRodney W. Grimes if (lp->d_sbsize == 0) { 13802a1deaaaSBruce Evans fprintf(stderr, "super block size 0\n"); 13818fae3551SRodney W. Grimes errors++; 13828fae3551SRodney W. Grimes } else if (lp->d_sbsize % lp->d_secsize) 13838fae3551SRodney W. Grimes Warning("super block size %% sector-size != 0"); 13848fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 13852a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 13862a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 13873233afaeSJohn W. De Boskey 13883233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 13893233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 13903233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 13913233afaeSJohn W. De Boskey hog_part = -1; 13923233afaeSJohn W. De Boskey /* find all fixed partitions */ 13933233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13943233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13953233afaeSJohn W. De Boskey if (part_set[i]) { 13963233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 13973233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 13983233afaeSJohn W. De Boskey if (i == FULL_DISK_PART) { 13993233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 14003233afaeSJohn W. De Boskey } else { 14013233afaeSJohn W. De Boskey if (hog_part != -1) 14023233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 14033233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 14043233afaeSJohn W. De Boskey else 14053233afaeSJohn W. De Boskey hog_part = i; 14063233afaeSJohn W. De Boskey } 14073233afaeSJohn W. De Boskey } else { 14088d3105e8SWarner Losh off_t size; 14093233afaeSJohn W. De Boskey 14103233afaeSJohn W. De Boskey size = pp->p_size; 14113233afaeSJohn W. De Boskey switch (part_size_type[i]) { 14123233afaeSJohn W. De Boskey case '%': 14133233afaeSJohn W. De Boskey total_percent += size; 14143233afaeSJohn W. De Boskey break; 14153233afaeSJohn W. De Boskey case 'k': 14163233afaeSJohn W. De Boskey case 'K': 14178d3105e8SWarner Losh size *= 1024ULL; 14183233afaeSJohn W. De Boskey break; 14193233afaeSJohn W. De Boskey case 'm': 14203233afaeSJohn W. De Boskey case 'M': 14218d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 14223233afaeSJohn W. De Boskey break; 14233233afaeSJohn W. De Boskey case 'g': 14243233afaeSJohn W. De Boskey case 'G': 14258d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 14263233afaeSJohn W. De Boskey break; 14273233afaeSJohn W. De Boskey case '\0': 14283233afaeSJohn W. De Boskey break; 14293233afaeSJohn W. De Boskey default: 14303233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 14313233afaeSJohn W. De Boskey break; 14323233afaeSJohn W. De Boskey } 14333233afaeSJohn W. De Boskey /* don't count %'s yet */ 14343233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 14353233afaeSJohn W. De Boskey /* 14363233afaeSJohn W. De Boskey * for all not in sectors, convert to 14373233afaeSJohn W. De Boskey * sectors 14383233afaeSJohn W. De Boskey */ 14393233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 14403233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 14413233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 14423233afaeSJohn W. De Boskey i + 'a'); 14433233afaeSJohn W. De Boskey size /= lp->d_secsize; 14443233afaeSJohn W. De Boskey pp->p_size = size; 14453233afaeSJohn W. De Boskey } 14463233afaeSJohn W. De Boskey /* else already in sectors */ 14473233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 14483233afaeSJohn W. De Boskey if (i != FULL_DISK_PART) 14493233afaeSJohn W. De Boskey total_size += size; 14503233afaeSJohn W. De Boskey } 14513233afaeSJohn W. De Boskey } 14523233afaeSJohn W. De Boskey } 14533233afaeSJohn W. De Boskey } 14543233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 14553233afaeSJohn W. De Boskey if (total_percent != 0) { 14563233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 14573233afaeSJohn W. De Boskey if (total_percent > 100) { 14583233afaeSJohn W. De Boskey fprintf(stderr,"total percentage %d is greater than 100\n", 14593233afaeSJohn W. De Boskey total_percent); 14603233afaeSJohn W. De Boskey errors++; 14613233afaeSJohn W. De Boskey } 14623233afaeSJohn W. De Boskey 14633233afaeSJohn W. De Boskey if (free_space > 0) { 14643233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14653233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14663233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 14673233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 14683233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 14693233afaeSJohn W. De Boskey total_size += pp->p_size; 14703233afaeSJohn W. De Boskey 14713233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 14723233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 14733233afaeSJohn W. De Boskey } 14743233afaeSJohn W. De Boskey } 14753233afaeSJohn W. De Boskey } else { 14763233afaeSJohn W. De Boskey fprintf(stderr, 14773233afaeSJohn W. De Boskey "%ld sectors available to give to '*' and '%' partitions\n", 14783233afaeSJohn W. De Boskey free_space); 14793233afaeSJohn W. De Boskey errors++; 14803233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 14813233afaeSJohn W. De Boskey } 14823233afaeSJohn W. De Boskey } 14833233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 14843233afaeSJohn W. De Boskey if (hog_part != -1) { 14853233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 14863233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 14873233afaeSJohn W. De Boskey } 14883233afaeSJohn W. De Boskey 14893233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 14903233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 14913233afaeSJohn W. De Boskey seen_default_offset = 0; 14923233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14933233afaeSJohn W. De Boskey part = 'a' + i; 14943233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14953233afaeSJohn W. De Boskey if (part_set[i]) { 14963233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 14973233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 14983233afaeSJohn W. De Boskey if (i == FULL_DISK_PART) { 14993233afaeSJohn W. De Boskey pp->p_offset = 0; 15003233afaeSJohn W. De Boskey } else { 15013233afaeSJohn W. De Boskey pp->p_offset = current_offset; 15023233afaeSJohn W. De Boskey seen_default_offset = 1; 15033233afaeSJohn W. De Boskey } 15043233afaeSJohn W. De Boskey } else { 15053233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 15063233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 15073233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 15083233afaeSJohn W. De Boskey seen_default_offset && i != FULL_DISK_PART) { 15093233afaeSJohn W. De Boskey fprintf(stderr, 15103233afaeSJohn W. De Boskey "Offset %ld for partition %c overlaps previous partition which ends at %ld\n", 15113233afaeSJohn W. De Boskey pp->p_offset,i+'a',current_offset); 15123233afaeSJohn W. De Boskey fprintf(stderr, 15133233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 15143233afaeSJohn W. De Boskey errors++; 15153233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 15163233afaeSJohn W. De Boskey i != FULL_DISK_PART && seen_default_offset) { 15173233afaeSJohn W. De Boskey /* 15183233afaeSJohn W. De Boskey * this may give unneeded warnings if 15193233afaeSJohn W. De Boskey * partitions are out-of-order 15203233afaeSJohn W. De Boskey */ 15213233afaeSJohn W. De Boskey Warning( 15223233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 15233233afaeSJohn W. De Boskey pp->p_offset, i + 'a', current_offset); 15243233afaeSJohn W. De Boskey } 15253233afaeSJohn W. De Boskey } 15263233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 15273233afaeSJohn W. De Boskey if (i != FULL_DISK_PART) 15283233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 15293233afaeSJohn W. De Boskey } 15303233afaeSJohn W. De Boskey } 15313233afaeSJohn W. De Boskey 15328fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 15338fae3551SRodney W. Grimes part = 'a' + i; 15348fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 15358fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 15362a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 15372a1deaaaSBruce Evans part, (u_long)pp->p_offset); 1538fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 1539fd43aa1cSDavid E. O'Brien /* See comment in writelabel(). */ 1540fd43aa1cSDavid E. O'Brien if (pp->p_offset % lp->d_secpercyl != 0) { 1541fd43aa1cSDavid E. O'Brien fprintf(stderr, "partition %c: does not start on a " 1542fd43aa1cSDavid E. O'Brien "cylinder boundary!\n", part); 1543fd43aa1cSDavid E. O'Brien errors++; 1544fd43aa1cSDavid E. O'Brien } 1545fd43aa1cSDavid E. O'Brien #endif 15468fae3551SRodney W. Grimes #ifdef notdef 15478fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 15488fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 15498fae3551SRodney W. Grimes part); 15508fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 15518fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 15528fae3551SRodney W. Grimes part); 15538fae3551SRodney W. Grimes #endif 15548fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 15558fae3551SRodney W. Grimes fprintf(stderr, 15568fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 15578fae3551SRodney W. Grimes errors++; 15588fae3551SRodney W. Grimes } 15598fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 15608fae3551SRodney W. Grimes fprintf(stderr, 15618fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 15628fae3551SRodney W. Grimes part); 15638fae3551SRodney W. Grimes errors++; 15648fae3551SRodney W. Grimes } 15653233afaeSJohn W. De Boskey if (i == FULL_DISK_PART) 15663233afaeSJohn W. De Boskey { 15673233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 15683233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 15693233afaeSJohn W. De Boskey if (pp->p_offset != 0) 15703233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 15713233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 15723233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 15733233afaeSJohn W. De Boskey 15743233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 15753233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 15763233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 15773233afaeSJohn W. De Boskey "standard system utilities",part); 15783233afaeSJohn W. De Boskey } 15793233afaeSJohn W. De Boskey } 15803233afaeSJohn W. De Boskey 15813233afaeSJohn W. De Boskey /* check for overlaps */ 15823233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 15833233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 15843233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 15853233afaeSJohn W. De Boskey if (j != FULL_DISK_PART && i != FULL_DISK_PART && 15863233afaeSJohn W. De Boskey part_set[i] && part_set[j]) { 15873233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 15883233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 15893233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 15903233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 15913233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 15923233afaeSJohn W. De Boskey j + 'a', i + 'a'); 15933233afaeSJohn W. De Boskey errors++; 15943233afaeSJohn W. De Boskey } 15953233afaeSJohn W. De Boskey } 15963233afaeSJohn W. De Boskey } 15978fae3551SRodney W. Grimes } 15988fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 15998fae3551SRodney W. Grimes part = 'a' + i; 16008fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 16018fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 16022a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 16032a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 16048fae3551SRodney W. Grimes } 16058fae3551SRodney W. Grimes return (errors); 16068fae3551SRodney W. Grimes } 16078fae3551SRodney W. Grimes 16088fae3551SRodney W. Grimes /* 1609425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1610425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1611425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1612425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1613425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1614425bed3aSJoerg Wunsch * 1615425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1616425bed3aSJoerg Wunsch */ 1617425bed3aSJoerg Wunsch struct disklabel * 1618425bed3aSJoerg Wunsch getvirginlabel(void) 1619425bed3aSJoerg Wunsch { 1620425bed3aSJoerg Wunsch static struct disklabel lab; 1621425bed3aSJoerg Wunsch char namebuf[BBSIZE]; 1622425bed3aSJoerg Wunsch int f; 1623425bed3aSJoerg Wunsch 1624425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1625e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 162643be698cSBruce Evans return (NULL); 1627425bed3aSJoerg Wunsch } 162843c6c959SDavid E. O'Brien (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1629e18fb238SBruce Evans if ((f = open(namebuf, O_RDONLY)) == -1) { 1630e18fb238SBruce Evans warn("cannot open %s", namebuf); 163143be698cSBruce Evans return (NULL); 1632425bed3aSJoerg Wunsch } 1633ff7d5162SJordan K. Hubbard 1634ff7d5162SJordan K. Hubbard /* 1635ff7d5162SJordan K. Hubbard * Try to use the new get-virgin-label ioctl. If it fails, 1636ff7d5162SJordan K. Hubbard * fallback to the old get-disdk-info ioctl. 1637ff7d5162SJordan K. Hubbard */ 16385b445518SPoul-Henning Kamp if (ioctl(f, DIOCGDVIRGIN, &lab) == 0) 16395b445518SPoul-Henning Kamp goto out; 16405b445518SPoul-Henning Kamp if (ioctl(f, DIOCGDINFO, &lab) == 0) 16415b445518SPoul-Henning Kamp goto out; 1642425bed3aSJoerg Wunsch close(f); 16435b445518SPoul-Henning Kamp (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname, 16445b445518SPoul-Henning Kamp 'a' + RAW_PART); 16455b445518SPoul-Henning Kamp if ((f = open(namebuf, O_RDONLY)) == -1) { 16465b445518SPoul-Henning Kamp warn("cannot open %s", namebuf); 164743be698cSBruce Evans return (NULL); 1648425bed3aSJoerg Wunsch } 16495b445518SPoul-Henning Kamp if (ioctl(f, DIOCGDINFO, &lab) == 0) 16505b445518SPoul-Henning Kamp goto out; 16515b445518SPoul-Henning Kamp close(f); 16525b445518SPoul-Henning Kamp warn("No virgin disklabel found %s", namebuf); 16535b445518SPoul-Henning Kamp return (NULL); 16545b445518SPoul-Henning Kamp out: 1655425bed3aSJoerg Wunsch close(f); 1656e18fb238SBruce Evans lab.d_boot0 = NULL; 1657e18fb238SBruce Evans lab.d_boot1 = NULL; 165843be698cSBruce Evans return (&lab); 1659425bed3aSJoerg Wunsch } 1660425bed3aSJoerg Wunsch 1661425bed3aSJoerg Wunsch /* 16628fae3551SRodney W. Grimes * If we are installing a boot program that doesn't fit in d_bbsize 16638fae3551SRodney W. Grimes * we need to mark those partitions that the boot overflows into. 16648fae3551SRodney W. Grimes * This allows newfs to prevent creation of a filesystem where it might 16658fae3551SRodney W. Grimes * clobber bootstrap code. 16668fae3551SRodney W. Grimes */ 166761de51caSJoerg Wunsch void 1668326c7cdaSWarner Losh setbootflag(struct disklabel *lp) 16698fae3551SRodney W. Grimes { 1670326c7cdaSWarner Losh struct partition *pp; 16718fae3551SRodney W. Grimes int i, errors = 0; 16728fae3551SRodney W. Grimes char part; 16738fae3551SRodney W. Grimes u_long boffset; 16748fae3551SRodney W. Grimes 16758fae3551SRodney W. Grimes if (bootbuf == 0) 16768fae3551SRodney W. Grimes return; 16778fae3551SRodney W. Grimes boffset = bootsize / lp->d_secsize; 16788fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 16798fae3551SRodney W. Grimes part = 'a' + i; 16808fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 16818fae3551SRodney W. Grimes if (pp->p_size == 0) 16828fae3551SRodney W. Grimes continue; 16838fae3551SRodney W. Grimes if (boffset <= pp->p_offset) { 16848fae3551SRodney W. Grimes if (pp->p_fstype == FS_BOOT) 16858fae3551SRodney W. Grimes pp->p_fstype = FS_UNUSED; 16868fae3551SRodney W. Grimes } else if (pp->p_fstype != FS_BOOT) { 16878fae3551SRodney W. Grimes if (pp->p_fstype != FS_UNUSED) { 16888fae3551SRodney W. Grimes fprintf(stderr, 16898fae3551SRodney W. Grimes "boot overlaps used partition %c\n", 16908fae3551SRodney W. Grimes part); 16918fae3551SRodney W. Grimes errors++; 16928fae3551SRodney W. Grimes } else { 16938fae3551SRodney W. Grimes pp->p_fstype = FS_BOOT; 16948fae3551SRodney W. Grimes Warning("boot overlaps partition %c, %s", 16958fae3551SRodney W. Grimes part, "marked as FS_BOOT"); 16968fae3551SRodney W. Grimes } 16978fae3551SRodney W. Grimes } 16988fae3551SRodney W. Grimes } 16996bd343a9SPhilippe Charnier if (errors) 17006bd343a9SPhilippe Charnier errx(4, "cannot install boot program"); 17018fae3551SRodney W. Grimes } 17028fae3551SRodney W. Grimes 17038fae3551SRodney W. Grimes /*VARARGS1*/ 170461de51caSJoerg Wunsch void 1705326c7cdaSWarner Losh Warning(const char *fmt, ...) 17068fae3551SRodney W. Grimes { 170761de51caSJoerg Wunsch va_list ap; 17088fae3551SRodney W. Grimes 17098fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 171061de51caSJoerg Wunsch va_start(ap, fmt); 171161de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 17128fae3551SRodney W. Grimes fprintf(stderr, "\n"); 171361de51caSJoerg Wunsch va_end(ap); 17148fae3551SRodney W. Grimes } 17158fae3551SRodney W. Grimes 171661de51caSJoerg Wunsch void 1717326c7cdaSWarner Losh usage(void) 17188fae3551SRodney W. Grimes { 17198fae3551SRodney W. Grimes #if NUMBOOT > 0 1720bef2080aSPhilippe Charnier 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%s\n%s\n", 17218fae3551SRodney W. Grimes "usage: disklabel [-r] disk", 1722bef2080aSPhilippe Charnier "\t\t(to read label)", 17233233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1724bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 17253233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1726bef2080aSPhilippe Charnier "\t\t(to edit label)", 17273233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1728bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 17298fae3551SRodney W. Grimes #if NUMBOOT > 1 17303233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1731bef2080aSPhilippe Charnier "\t\t(to install boot program with existing label)", 17323233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1733bef2080aSPhilippe Charnier "\t\t(to write label and boot program)", 17343233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1735bef2080aSPhilippe Charnier "\t\t(to restore label and boot program)", 17368fae3551SRodney W. Grimes #else 17373233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1738bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 17393233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1740bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 17413233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 1742bef2080aSPhilippe Charnier "\t\t(to restore label and install boot program)", 17438fae3551SRodney W. Grimes #endif 1744bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1745bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17468fae3551SRodney W. Grimes #else 1747bef2080aSPhilippe Charnier fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 17488fae3551SRodney W. Grimes "usage: disklabel [-r] disk", "(to read label)", 17493233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1750bef2080aSPhilippe Charnier "\t\t(to write label)", 17513233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1752bef2080aSPhilippe Charnier "\t\t(to edit label)", 17533233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1754bef2080aSPhilippe Charnier "\t\t(to restore label)", 1755bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1756bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17578fae3551SRodney W. Grimes #endif 17588fae3551SRodney W. Grimes exit(1); 17598fae3551SRodney W. Grimes } 1760