18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1987, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 68fae3551SRodney W. Grimes * Symmetric Computer Systems. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 98fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 108fae3551SRodney W. Grimes * are met: 118fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 128fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 138fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 158fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 168fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 178fae3551SRodney W. Grimes * must display the following acknowledgement: 188fae3551SRodney W. Grimes * This product includes software developed by the University of 198fae3551SRodney W. Grimes * California, Berkeley and its contributors. 208fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 218fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 228fae3551SRodney W. Grimes * without specific prior written permission. 238fae3551SRodney W. Grimes * 248fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 258fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 288fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 298fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 308fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 318fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 328fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 338fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 348fae3551SRodney W. Grimes * SUCH DAMAGE. 358fae3551SRodney W. Grimes */ 368fae3551SRodney W. Grimes 378fae3551SRodney W. Grimes #ifndef lint 386bd343a9SPhilippe Charnier static const char copyright[] = 398fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\ 408fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 418fae3551SRodney W. Grimes #endif /* not lint */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 446bd343a9SPhilippe Charnier #if 0 458fae3551SRodney W. Grimes static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94"; 468fae3551SRodney W. Grimes /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 476bd343a9SPhilippe Charnier #endif 486bd343a9SPhilippe Charnier static const char rcsid[] = 497f3dea24SPeter Wemm "$FreeBSD$"; 508fae3551SRodney W. Grimes #endif /* not lint */ 518fae3551SRodney W. Grimes 528fae3551SRodney W. Grimes #include <sys/param.h> 538fae3551SRodney W. Grimes #include <sys/file.h> 548fae3551SRodney W. Grimes #include <sys/stat.h> 5561de51caSJoerg Wunsch #include <sys/wait.h> 568fae3551SRodney W. Grimes #define DKTYPENAMES 578fae3551SRodney W. Grimes #include <sys/disklabel.h> 588fae3551SRodney W. Grimes #include <ufs/ffs/fs.h> 598fae3551SRodney W. Grimes #include <unistd.h> 608fae3551SRodney W. Grimes #include <string.h> 618fae3551SRodney W. Grimes #include <stdio.h> 6261de51caSJoerg Wunsch #include <stdlib.h> 6361de51caSJoerg Wunsch #include <signal.h> 6461de51caSJoerg Wunsch #include <stdarg.h> 658fae3551SRodney W. Grimes #include <ctype.h> 66bef2080aSPhilippe Charnier #include <err.h> 6737736675SWarner Losh #include <errno.h> 688fae3551SRodney W. Grimes #include "pathnames.h" 698fae3551SRodney W. Grimes 708fae3551SRodney W. Grimes /* 718fae3551SRodney W. Grimes * Disklabel: read and write disklabels. 728fae3551SRodney W. Grimes * The label is usually placed on one of the first sectors of the disk. 738fae3551SRodney W. Grimes * Many machines also place a bootstrap in the same area, 748fae3551SRodney W. Grimes * in which case the label is embedded in the bootstrap. 758fae3551SRodney W. Grimes * The bootstrap source must leave space at the proper offset 768fae3551SRodney W. Grimes * for the label on such machines. 778fae3551SRodney W. Grimes */ 788fae3551SRodney W. Grimes 798fae3551SRodney W. Grimes #ifndef BBSIZE 808fae3551SRodney W. Grimes #define BBSIZE 8192 /* size of boot area, with label */ 818fae3551SRodney W. Grimes #endif 828fae3551SRodney W. Grimes 833233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 843233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 853233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 863233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 873233afaeSJohn W. De Boskey 883233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 893233afaeSJohn W. De Boskey #define BIG_NEWFS_FRAG 4096U 903233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 913233afaeSJohn W. De Boskey 928fae3551SRodney W. Grimes #ifdef tahoe 938fae3551SRodney W. Grimes #define NUMBOOT 0 948fae3551SRodney W. Grimes #else 95cc18e4ccSBruce Evans #if defined(__alpha__) || defined(hp300) || defined(hp800) 968fae3551SRodney W. Grimes #define NUMBOOT 1 978fae3551SRodney W. Grimes #else 988fae3551SRodney W. Grimes #define NUMBOOT 2 998fae3551SRodney W. Grimes #endif 1008fae3551SRodney W. Grimes #endif 1018fae3551SRodney W. Grimes 10261de51caSJoerg Wunsch void makelabel __P((char *, char *, struct disklabel *)); 10361de51caSJoerg Wunsch int writelabel __P((int, char *, struct disklabel *)); 10461de51caSJoerg Wunsch void l_perror __P((char *)); 10561de51caSJoerg Wunsch struct disklabel * readlabel __P((int)); 10661de51caSJoerg Wunsch struct disklabel * makebootarea __P((char *, struct disklabel *, int)); 10761de51caSJoerg Wunsch void display __P((FILE *, struct disklabel *)); 10861de51caSJoerg Wunsch int edit __P((struct disklabel *, int)); 10961de51caSJoerg Wunsch int editit __P((void)); 11061de51caSJoerg Wunsch char * skip __P((char *)); 11161de51caSJoerg Wunsch char * word __P((char *)); 11261de51caSJoerg Wunsch int getasciilabel __P((FILE *, struct disklabel *)); 11361de51caSJoerg Wunsch int checklabel __P((struct disklabel *)); 11461de51caSJoerg Wunsch void setbootflag __P((struct disklabel *)); 11561de51caSJoerg Wunsch void Warning (char *, ...); 11661de51caSJoerg Wunsch void usage __P((void)); 117425bed3aSJoerg Wunsch struct disklabel * getvirginlabel __P((void)); 11861de51caSJoerg Wunsch 1198fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1208fae3551SRodney W. Grimes #define streq(a,b) (strcmp(a,b) == 0) 1218fae3551SRodney W. Grimes 1228fae3551SRodney W. Grimes char *dkname; 1238fae3551SRodney W. Grimes char *specname; 124aaae3130SKris Kennaway char tmpfil[] = PATH_TMPFILE; 1258fae3551SRodney W. Grimes 1268fae3551SRodney W. Grimes char namebuf[BBSIZE], *np = namebuf; 1278fae3551SRodney W. Grimes struct disklabel lab; 1288fae3551SRodney W. Grimes char bootarea[BBSIZE]; 1298fae3551SRodney W. Grimes 1303233afaeSJohn W. De Boskey /* partition 'c' is the full disk and is special */ 1313233afaeSJohn W. De Boskey #define FULL_DISK_PART 2 1323233afaeSJohn W. De Boskey #define MAX_PART ('z') 1333233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 1343233afaeSJohn W. De Boskey char part_size_type[MAX_NUM_PARTS]; 1353233afaeSJohn W. De Boskey char part_offset_type[MAX_NUM_PARTS]; 1363233afaeSJohn W. De Boskey int part_set[MAX_NUM_PARTS]; 1373233afaeSJohn W. De Boskey 1388fae3551SRodney W. Grimes #if NUMBOOT > 0 1398fae3551SRodney W. Grimes int installboot; /* non-zero if we should install a boot program */ 1408fae3551SRodney W. Grimes char *bootbuf; /* pointer to buffer with remainder of boot prog */ 1418fae3551SRodney W. Grimes int bootsize; /* size of remaining boot program */ 1428fae3551SRodney W. Grimes char *xxboot; /* primary boot */ 1438fae3551SRodney W. Grimes char *bootxx; /* secondary boot */ 1448fae3551SRodney W. Grimes char boot0[MAXPATHLEN]; 1458fae3551SRodney W. Grimes char boot1[MAXPATHLEN]; 1468fae3551SRodney W. Grimes #endif 1478fae3551SRodney W. Grimes 1488fae3551SRodney W. Grimes enum { 1498fae3551SRodney W. Grimes UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT 1508fae3551SRodney W. Grimes } op = UNSPEC; 1518fae3551SRodney W. Grimes 1528fae3551SRodney W. Grimes int rflag; 1533233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1548fae3551SRodney W. Grimes 1558fae3551SRodney W. Grimes #ifdef DEBUG 1568fae3551SRodney W. Grimes int debug; 1573233afaeSJohn W. De Boskey #define OPTIONS "BNRWb:denrs:w" 1588fae3551SRodney W. Grimes #else 1593233afaeSJohn W. De Boskey #define OPTIONS "BNRWb:enrs:w" 1608fae3551SRodney W. Grimes #endif 1618fae3551SRodney W. Grimes 16261de51caSJoerg Wunsch int 1638fae3551SRodney W. Grimes main(argc, argv) 1648fae3551SRodney W. Grimes int argc; 1658fae3551SRodney W. Grimes char *argv[]; 1668fae3551SRodney W. Grimes { 1678fae3551SRodney W. Grimes register struct disklabel *lp; 1688fae3551SRodney W. Grimes FILE *t; 1693b3038a6SBill Fumerola int ch, f = 0, flag, error = 0; 1708fae3551SRodney W. Grimes char *name = 0; 1718fae3551SRodney W. Grimes 1728d64695cSWarner Losh while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1738fae3551SRodney W. Grimes switch (ch) { 1748fae3551SRodney W. Grimes #if NUMBOOT > 0 1758fae3551SRodney W. Grimes case 'B': 1768fae3551SRodney W. Grimes ++installboot; 1778fae3551SRodney W. Grimes break; 1788fae3551SRodney W. Grimes case 'b': 1798fae3551SRodney W. Grimes xxboot = optarg; 1808fae3551SRodney W. Grimes break; 1818fae3551SRodney W. Grimes #if NUMBOOT > 1 1828fae3551SRodney W. Grimes case 's': 1838fae3551SRodney W. Grimes bootxx = optarg; 1848fae3551SRodney W. Grimes break; 1858fae3551SRodney W. Grimes #endif 1868fae3551SRodney W. Grimes #endif 1878fae3551SRodney W. Grimes case 'N': 1888fae3551SRodney W. Grimes if (op != UNSPEC) 1898fae3551SRodney W. Grimes usage(); 1908fae3551SRodney W. Grimes op = NOWRITE; 1918fae3551SRodney W. Grimes break; 1923233afaeSJohn W. De Boskey case 'n': 1933233afaeSJohn W. De Boskey disable_write = 1; 1943233afaeSJohn W. De Boskey break; 1958fae3551SRodney W. Grimes case 'R': 1968fae3551SRodney W. Grimes if (op != UNSPEC) 1978fae3551SRodney W. Grimes usage(); 1988fae3551SRodney W. Grimes op = RESTORE; 1998fae3551SRodney W. Grimes break; 2008fae3551SRodney W. Grimes case 'W': 2018fae3551SRodney W. Grimes if (op != UNSPEC) 2028fae3551SRodney W. Grimes usage(); 2038fae3551SRodney W. Grimes op = WRITEABLE; 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 #ifdef DEBUG 2198fae3551SRodney W. Grimes case 'd': 2208fae3551SRodney W. Grimes debug++; 2218fae3551SRodney W. Grimes break; 2228fae3551SRodney W. Grimes #endif 2238fae3551SRodney W. Grimes case '?': 2248fae3551SRodney W. Grimes default: 2258fae3551SRodney W. Grimes usage(); 2268fae3551SRodney W. Grimes } 2278fae3551SRodney W. Grimes argc -= optind; 2288fae3551SRodney W. Grimes argv += optind; 2298fae3551SRodney W. Grimes #if NUMBOOT > 0 2308fae3551SRodney W. Grimes if (installboot) { 2318fae3551SRodney W. Grimes rflag++; 2328fae3551SRodney W. Grimes if (op == UNSPEC) 2338fae3551SRodney W. Grimes op = WRITEBOOT; 2348fae3551SRodney W. Grimes } else { 2358fae3551SRodney W. Grimes if (op == UNSPEC) 2368fae3551SRodney W. Grimes op = READ; 2378fae3551SRodney W. Grimes xxboot = bootxx = 0; 2388fae3551SRodney W. Grimes } 2398fae3551SRodney W. Grimes #else 2408fae3551SRodney W. Grimes if (op == UNSPEC) 2418fae3551SRodney W. Grimes op = READ; 2428fae3551SRodney W. Grimes #endif 2438fae3551SRodney W. Grimes if (argc < 1) 2448fae3551SRodney W. Grimes usage(); 2458fae3551SRodney W. Grimes 2468fae3551SRodney W. Grimes dkname = argv[0]; 2478fae3551SRodney W. Grimes if (dkname[0] != '/') { 24809c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2498fae3551SRodney W. Grimes specname = np; 2508fae3551SRodney W. Grimes np += strlen(specname) + 1; 2518fae3551SRodney W. Grimes } else 2528fae3551SRodney W. Grimes specname = dkname; 2538fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2548fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 25543c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 2568fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 2578fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2588fae3551SRodney W. Grimes } 2598fae3551SRodney W. Grimes if (f < 0) 260bef2080aSPhilippe Charnier err(4, "%s", specname); 2618fae3551SRodney W. Grimes 2628fae3551SRodney W. Grimes switch(op) { 2638fae3551SRodney W. Grimes 2643b3038a6SBill Fumerola case UNSPEC: 2653b3038a6SBill Fumerola break; 2663b3038a6SBill Fumerola 2678fae3551SRodney W. Grimes case EDIT: 2688fae3551SRodney W. Grimes if (argc != 1) 2698fae3551SRodney W. Grimes usage(); 2708fae3551SRodney W. Grimes lp = readlabel(f); 2718fae3551SRodney W. Grimes error = edit(lp, f); 2728fae3551SRodney W. Grimes break; 2738fae3551SRodney W. Grimes 2748fae3551SRodney W. Grimes case NOWRITE: 2758fae3551SRodney W. Grimes flag = 0; 2768fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 277bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 2788fae3551SRodney W. Grimes break; 2798fae3551SRodney W. Grimes 2808fae3551SRodney W. Grimes case READ: 2818fae3551SRodney W. Grimes if (argc != 1) 2828fae3551SRodney W. Grimes usage(); 2838fae3551SRodney W. Grimes lp = readlabel(f); 2848fae3551SRodney W. Grimes display(stdout, lp); 2858fae3551SRodney W. Grimes error = checklabel(lp); 2868fae3551SRodney W. Grimes break; 2878fae3551SRodney W. Grimes 2888fae3551SRodney W. Grimes case RESTORE: 2898fae3551SRodney W. Grimes #if NUMBOOT > 0 2908fae3551SRodney W. Grimes if (installboot && argc == 3) { 2918fae3551SRodney W. Grimes makelabel(argv[2], 0, &lab); 2928fae3551SRodney W. Grimes argc--; 2936cabb348SBruce Evans 2946cabb348SBruce Evans /* 2956cabb348SBruce Evans * We only called makelabel() for its side effect 2966cabb348SBruce Evans * of setting the bootstrap file names. Discard 2976cabb348SBruce Evans * all changes to `lab' so that all values in the 2986cabb348SBruce Evans * final label come from the ASCII label. 2996cabb348SBruce Evans */ 3006cabb348SBruce Evans bzero((char *)&lab, sizeof(lab)); 3018fae3551SRodney W. Grimes } 3028fae3551SRodney W. Grimes #endif 3038fae3551SRodney W. Grimes if (argc != 2) 3048fae3551SRodney W. Grimes usage(); 3058fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 306bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 3076cabb348SBruce Evans if (!getasciilabel(t, &lab)) 3086cabb348SBruce Evans exit(1); 3096cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 3106cabb348SBruce Evans *lp = lab; 3118fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3128fae3551SRodney W. Grimes break; 3138fae3551SRodney W. Grimes 3148fae3551SRodney W. Grimes case WRITE: 3158fae3551SRodney W. Grimes if (argc == 3) { 3168fae3551SRodney W. Grimes name = argv[2]; 3178fae3551SRodney W. Grimes argc--; 3188fae3551SRodney W. Grimes } 3198fae3551SRodney W. Grimes if (argc != 2) 3208fae3551SRodney W. Grimes usage(); 3218fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3228fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3238fae3551SRodney W. Grimes *lp = lab; 3248fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3258fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3268fae3551SRodney W. Grimes break; 3278fae3551SRodney W. Grimes 3288fae3551SRodney W. Grimes case WRITEABLE: 3298fae3551SRodney W. Grimes flag = 1; 3308fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 331bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 3328fae3551SRodney W. Grimes break; 3338fae3551SRodney W. Grimes 3348fae3551SRodney W. Grimes #if NUMBOOT > 0 3358fae3551SRodney W. Grimes case WRITEBOOT: 3368fae3551SRodney W. Grimes { 3378fae3551SRodney W. Grimes struct disklabel tlab; 3388fae3551SRodney W. Grimes 3398fae3551SRodney W. Grimes lp = readlabel(f); 3408fae3551SRodney W. Grimes tlab = *lp; 3418fae3551SRodney W. Grimes if (argc == 2) 3428fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3438fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3448fae3551SRodney W. Grimes *lp = tlab; 3458fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3468fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3478fae3551SRodney W. Grimes break; 3488fae3551SRodney W. Grimes } 3498fae3551SRodney W. Grimes #endif 3508fae3551SRodney W. Grimes } 3518fae3551SRodney W. Grimes exit(error); 3528fae3551SRodney W. Grimes } 3538fae3551SRodney W. Grimes 3548fae3551SRodney W. Grimes /* 3558fae3551SRodney W. Grimes * Construct a prototype disklabel from /etc/disktab. As a side 3568fae3551SRodney W. Grimes * effect, set the names of the primary and secondary boot files 3578fae3551SRodney W. Grimes * if specified. 3588fae3551SRodney W. Grimes */ 35961de51caSJoerg Wunsch void 3608fae3551SRodney W. Grimes makelabel(type, name, lp) 3618fae3551SRodney W. Grimes char *type, *name; 3628fae3551SRodney W. Grimes register struct disklabel *lp; 3638fae3551SRodney W. Grimes { 3648fae3551SRodney W. Grimes register struct disklabel *dp; 365425bed3aSJoerg Wunsch 366425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 367425bed3aSJoerg Wunsch dp = getvirginlabel(); 368425bed3aSJoerg Wunsch else 3698fae3551SRodney W. Grimes dp = getdiskbyname(type); 3706bd343a9SPhilippe Charnier if (dp == NULL) 3716bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3728fae3551SRodney W. Grimes *lp = *dp; 3738fae3551SRodney W. Grimes #if NUMBOOT > 0 3748fae3551SRodney W. Grimes /* 3758fae3551SRodney W. Grimes * Set bootstrap name(s). 3768fae3551SRodney W. Grimes * 1. If set from command line, use those, 3778fae3551SRodney W. Grimes * 2. otherwise, check if disktab specifies them (b0 or b1), 3788fae3551SRodney W. Grimes * 3. otherwise, makebootarea() will choose ones based on the name 3798fae3551SRodney W. Grimes * of the disk special file. E.g. /dev/ra0 -> raboot, bootra 3808fae3551SRodney W. Grimes */ 3818fae3551SRodney W. Grimes if (!xxboot && lp->d_boot0) { 3828fae3551SRodney W. Grimes if (*lp->d_boot0 != '/') 3838fae3551SRodney W. Grimes (void)sprintf(boot0, "%s/%s", 3848fae3551SRodney W. Grimes _PATH_BOOTDIR, lp->d_boot0); 3858fae3551SRodney W. Grimes else 3868fae3551SRodney W. Grimes (void)strcpy(boot0, lp->d_boot0); 3878fae3551SRodney W. Grimes xxboot = boot0; 3888fae3551SRodney W. Grimes } 3898fae3551SRodney W. Grimes #if NUMBOOT > 1 3908fae3551SRodney W. Grimes if (!bootxx && lp->d_boot1) { 3918fae3551SRodney W. Grimes if (*lp->d_boot1 != '/') 3928fae3551SRodney W. Grimes (void)sprintf(boot1, "%s/%s", 3938fae3551SRodney W. Grimes _PATH_BOOTDIR, lp->d_boot1); 3948fae3551SRodney W. Grimes else 3958fae3551SRodney W. Grimes (void)strcpy(boot1, lp->d_boot1); 3968fae3551SRodney W. Grimes bootxx = boot1; 3978fae3551SRodney W. Grimes } 3988fae3551SRodney W. Grimes #endif 3998fae3551SRodney W. Grimes #endif 4008fae3551SRodney W. Grimes /* d_packname is union d_boot[01], so zero */ 4018fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 4028fae3551SRodney W. Grimes if (name) 4038fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 4048fae3551SRodney W. Grimes } 4058fae3551SRodney W. Grimes 40661de51caSJoerg Wunsch int 4078fae3551SRodney W. Grimes writelabel(f, boot, lp) 4088fae3551SRodney W. Grimes int f; 4098fae3551SRodney W. Grimes char *boot; 4108fae3551SRodney W. Grimes register 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 417cc18e4ccSBruce Evans #ifdef vax 418cc18e4ccSBruce Evans register int i; 419cc18e4ccSBruce Evans #endif 420130cd73aSDoug Rabson 4213233afaeSJohn W. De Boskey if (disable_write) { 4223233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 4233233afaeSJohn W. De Boskey display(stdout, lp); 4243233afaeSJohn W. De Boskey return (0); 4253233afaeSJohn W. De Boskey } else { 4268fae3551SRodney W. Grimes setbootflag(lp); 4278fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 4288fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 4298fae3551SRodney W. Grimes lp->d_checksum = 0; 4308fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 4318fae3551SRodney W. Grimes if (rflag) { 4328fae3551SRodney W. Grimes /* 4338fae3551SRodney W. Grimes * First set the kernel disk label, 4348fae3551SRodney W. Grimes * then write a label to the raw disk. 4358fae3551SRodney W. Grimes * If the SDINFO ioctl fails because it is unimplemented, 4368fae3551SRodney W. Grimes * keep going; otherwise, the kernel consistency checks 4378fae3551SRodney W. Grimes * may prevent us from changing the current (in-core) 4388fae3551SRodney W. Grimes * label. 4398fae3551SRodney W. Grimes */ 4408fae3551SRodney W. Grimes if (ioctl(f, DIOCSDINFO, lp) < 0 && 4418fae3551SRodney W. Grimes errno != ENODEV && errno != ENOTTY) { 4428fae3551SRodney W. Grimes l_perror("ioctl DIOCSDINFO"); 4438fae3551SRodney W. Grimes return (1); 4448fae3551SRodney W. Grimes } 4458fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 446130cd73aSDoug Rabson 447130cd73aSDoug Rabson #ifdef __alpha__ 448cc18e4ccSBruce Evans /* 449cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 450cc18e4ccSBruce Evans */ 451130cd73aSDoug Rabson for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) 452130cd73aSDoug Rabson sum += p[i]; 453130cd73aSDoug Rabson p[63] = sum; 454130cd73aSDoug Rabson #endif 455130cd73aSDoug Rabson 456cc18e4ccSBruce Evans /* 457cc18e4ccSBruce Evans * write enable label sector before write (if necessary), 458cc18e4ccSBruce Evans * disable after writing. 459cc18e4ccSBruce Evans */ 460cc18e4ccSBruce Evans flag = 1; 4618fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, &flag) < 0) 4626bd343a9SPhilippe Charnier warn("ioctl DIOCWLABEL"); 4638fae3551SRodney W. Grimes if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) { 4646bd343a9SPhilippe Charnier warn("write"); 4658fae3551SRodney W. Grimes return (1); 4668fae3551SRodney W. Grimes } 4678fae3551SRodney W. Grimes #if NUMBOOT > 0 4688fae3551SRodney W. Grimes /* 4698fae3551SRodney W. Grimes * Output the remainder of the disklabel 4708fae3551SRodney W. Grimes */ 4718fae3551SRodney W. Grimes if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 4726bd343a9SPhilippe Charnier warn("write"); 4738fae3551SRodney W. Grimes return(1); 4748fae3551SRodney W. Grimes } 4758fae3551SRodney W. Grimes #endif 4768fae3551SRodney W. Grimes flag = 0; 4778fae3551SRodney W. Grimes (void) ioctl(f, DIOCWLABEL, &flag); 4788fae3551SRodney W. Grimes } else if (ioctl(f, DIOCWDINFO, lp) < 0) { 4798fae3551SRodney W. Grimes l_perror("ioctl DIOCWDINFO"); 4808fae3551SRodney W. Grimes return (1); 4818fae3551SRodney W. Grimes } 4828fae3551SRodney W. Grimes #ifdef vax 4838fae3551SRodney W. Grimes if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { 4848fae3551SRodney W. Grimes daddr_t alt; 4858fae3551SRodney W. Grimes 4868fae3551SRodney W. Grimes alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; 4878fae3551SRodney W. Grimes for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { 4888fae3551SRodney W. Grimes (void)lseek(f, (off_t)((alt + i) * lp->d_secsize), 4898fae3551SRodney W. Grimes SEEK_SET); 4906bd343a9SPhilippe Charnier if (write(f, boot, lp->d_secsize) < lp->d_secsize) 4916bd343a9SPhilippe Charnier warn("alternate label %d write", i/2); 4928fae3551SRodney W. Grimes } 4938fae3551SRodney W. Grimes } 4948fae3551SRodney W. Grimes #endif 4953233afaeSJohn W. De Boskey } 4968fae3551SRodney W. Grimes return (0); 4978fae3551SRodney W. Grimes } 4988fae3551SRodney W. Grimes 49961de51caSJoerg Wunsch void 5008fae3551SRodney W. Grimes l_perror(s) 5018fae3551SRodney W. Grimes char *s; 5028fae3551SRodney W. Grimes { 5036bd343a9SPhilippe Charnier switch (errno) { 5048fae3551SRodney W. Grimes 5058fae3551SRodney W. Grimes case ESRCH: 5063121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 5070b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 5088fae3551SRodney W. Grimes break; 5098fae3551SRodney W. Grimes 5108fae3551SRodney W. Grimes case EINVAL: 5113121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 5123121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 5138fae3551SRodney W. Grimes break; 5148fae3551SRodney W. Grimes 5158fae3551SRodney W. Grimes case EBUSY: 5166bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 5178fae3551SRodney W. Grimes break; 5188fae3551SRodney W. Grimes 5198fae3551SRodney W. Grimes case EXDEV: 5207de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 5217de06420SBruce Evans s, 'a' + RAW_PART); 5228fae3551SRodney W. Grimes break; 5238fae3551SRodney W. Grimes 5248fae3551SRodney W. Grimes default: 5256bd343a9SPhilippe Charnier warn((char *)NULL); 5268fae3551SRodney W. Grimes break; 5278fae3551SRodney W. Grimes } 5288fae3551SRodney W. Grimes } 5298fae3551SRodney W. Grimes 5308fae3551SRodney W. Grimes /* 5318fae3551SRodney W. Grimes * Fetch disklabel for disk. 5328fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 5338fae3551SRodney W. Grimes */ 5348fae3551SRodney W. Grimes struct disklabel * 5358fae3551SRodney W. Grimes readlabel(f) 5368fae3551SRodney W. Grimes int f; 5378fae3551SRodney W. Grimes { 5388fae3551SRodney W. Grimes register struct disklabel *lp; 5398fae3551SRodney W. Grimes 5408fae3551SRodney W. Grimes if (rflag) { 5418fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 542bef2080aSPhilippe Charnier err(4, "%s", specname); 5438fae3551SRodney W. Grimes for (lp = (struct disklabel *)bootarea; 5448fae3551SRodney W. Grimes lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 5458fae3551SRodney W. Grimes lp = (struct disklabel *)((char *)lp + 16)) 5468fae3551SRodney W. Grimes if (lp->d_magic == DISKMAGIC && 5478fae3551SRodney W. Grimes lp->d_magic2 == DISKMAGIC) 5488fae3551SRodney W. Grimes break; 5498fae3551SRodney W. Grimes if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 5508fae3551SRodney W. Grimes lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 5513121d4cbSPhilippe Charnier dkcksum(lp) != 0) 5523121d4cbSPhilippe Charnier errx(1, 5533121d4cbSPhilippe Charnier "bad pack magic number (label is damaged, or pack is unlabeled)"); 5548fae3551SRodney W. Grimes } else { 5558fae3551SRodney W. Grimes lp = &lab; 5568fae3551SRodney W. Grimes if (ioctl(f, DIOCGDINFO, lp) < 0) 557bef2080aSPhilippe Charnier err(4, "ioctl DIOCGDINFO"); 5588fae3551SRodney W. Grimes } 5598fae3551SRodney W. Grimes return (lp); 5608fae3551SRodney W. Grimes } 5618fae3551SRodney W. Grimes 5628fae3551SRodney W. Grimes /* 5638fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 5648fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 5658fae3551SRodney W. Grimes */ 5668fae3551SRodney W. Grimes struct disklabel * 5678fae3551SRodney W. Grimes makebootarea(boot, dp, f) 5688fae3551SRodney W. Grimes char *boot; 5698fae3551SRodney W. Grimes register struct disklabel *dp; 5708fae3551SRodney W. Grimes int f; 5718fae3551SRodney W. Grimes { 57221c729c2SBruce Evans struct disklabel *lp; 5738fae3551SRodney W. Grimes register char *p; 5748fae3551SRodney W. Grimes int b; 5758fae3551SRodney W. Grimes #if NUMBOOT > 0 5768fae3551SRodney W. Grimes char *dkbasename; 5778fae3551SRodney W. Grimes struct stat sb; 578cc18e4ccSBruce Evans #endif 579cc18e4ccSBruce Evans #ifdef __alpha__ 58021c729c2SBruce Evans u_long *bootinfo; 581cc18e4ccSBruce Evans int n; 582cc18e4ccSBruce Evans #endif 583d70e4e53SJoerg Wunsch #ifdef __i386__ 584d70e4e53SJoerg Wunsch char *tmpbuf; 585d70e4e53SJoerg Wunsch int i, found; 58661de51caSJoerg Wunsch #endif 5878fae3551SRodney W. Grimes 5888fae3551SRodney W. Grimes /* XXX */ 5898fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 5908fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 5918fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 5928fae3551SRodney W. Grimes } 5938fae3551SRodney W. Grimes lp = (struct disklabel *) 5948fae3551SRodney W. Grimes (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 5958fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5968fae3551SRodney W. Grimes #if NUMBOOT > 0 5978fae3551SRodney W. Grimes /* 5988fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 5998fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 6008fae3551SRodney W. Grimes * clobber the existing boot. 6018fae3551SRodney W. Grimes */ 6028fae3551SRodney W. Grimes if (!installboot) { 6038fae3551SRodney W. Grimes if (rflag) { 6048fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 605bef2080aSPhilippe Charnier err(4, "%s", specname); 6068fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 6078fae3551SRodney W. Grimes } 6088fae3551SRodney W. Grimes return (lp); 6098fae3551SRodney W. Grimes } 6108fae3551SRodney W. Grimes /* 6118fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 6128fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 6138fae3551SRodney W. Grimes */ 6148fae3551SRodney W. Grimes if (!xxboot || !bootxx) { 6158fae3551SRodney W. Grimes dkbasename = np; 6168fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 6178fae3551SRodney W. Grimes p = dkname; 6188fae3551SRodney W. Grimes else 6198fae3551SRodney W. Grimes p++; 6208fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 6218fae3551SRodney W. Grimes *np++ = *p++; 6228fae3551SRodney W. Grimes *np++ = '\0'; 6238fae3551SRodney W. Grimes 6248fae3551SRodney W. Grimes if (!xxboot) { 625efba76d7SJordan K. Hubbard (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); 626efba76d7SJordan K. Hubbard xxboot = boot0; 6278fae3551SRodney W. Grimes } 6288fae3551SRodney W. Grimes #if NUMBOOT > 1 6298fae3551SRodney W. Grimes if (!bootxx) { 630efba76d7SJordan K. Hubbard (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); 631efba76d7SJordan K. Hubbard bootxx = boot1; 6328fae3551SRodney W. Grimes } 6338fae3551SRodney W. Grimes #endif 6348fae3551SRodney W. Grimes } 6358fae3551SRodney W. Grimes #ifdef DEBUG 6368fae3551SRodney W. Grimes if (debug) 6378fae3551SRodney W. Grimes fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n", 6388fae3551SRodney W. Grimes xxboot, bootxx ? bootxx : "NONE"); 6398fae3551SRodney W. Grimes #endif 6408fae3551SRodney W. Grimes 6418fae3551SRodney W. Grimes /* 6428fae3551SRodney W. Grimes * Strange rules: 6438fae3551SRodney W. Grimes * 1. One-piece bootstrap (hp300/hp800) 6448fae3551SRodney W. Grimes * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 6458fae3551SRodney W. Grimes * is remembered and written later following the bootarea. 6468fae3551SRodney W. Grimes * 2. Two-piece bootstraps (vax/i386?/mips?) 6478fae3551SRodney W. Grimes * up to d_secsize bytes of ``xxboot'' go in first d_secsize 6488fae3551SRodney W. Grimes * bytes of bootarea, remaining d_bbsize-d_secsize filled 6498fae3551SRodney W. Grimes * from ``bootxx''. 6508fae3551SRodney W. Grimes */ 6518fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 6528fae3551SRodney W. Grimes if (b < 0) 653bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6548fae3551SRodney W. Grimes #if NUMBOOT > 1 655d70e4e53SJoerg Wunsch #ifdef __i386__ 656d70e4e53SJoerg Wunsch /* 657d70e4e53SJoerg Wunsch * XXX Botch alert. 658d70e4e53SJoerg Wunsch * The i386 has the so-called fdisk table embedded into the 659d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 660d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 661d70e4e53SJoerg Wunsch * the xxboot provides a template.) 662d70e4e53SJoerg Wunsch */ 663d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 664bef2080aSPhilippe Charnier err(4, "%s", xxboot); 665d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 666d70e4e53SJoerg Wunsch #endif /* i386 */ 6678fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_secsize) < 0) 668bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6698fae3551SRodney W. Grimes (void)close(b); 670d70e4e53SJoerg Wunsch #ifdef __i386__ 671d70e4e53SJoerg Wunsch for (i = DOSPARTOFF, found = 0; 672d70e4e53SJoerg Wunsch !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition); 673d70e4e53SJoerg Wunsch i++) 674d70e4e53SJoerg Wunsch found = tmpbuf[i] != 0; 675d70e4e53SJoerg Wunsch if (found) 676d70e4e53SJoerg Wunsch memcpy((void *)&boot[DOSPARTOFF], 677d70e4e53SJoerg Wunsch (void *)&tmpbuf[DOSPARTOFF], 678d70e4e53SJoerg Wunsch NDOSPART * sizeof(struct dos_partition)); 679d70e4e53SJoerg Wunsch free(tmpbuf); 680d70e4e53SJoerg Wunsch #endif /* i386 */ 6818fae3551SRodney W. Grimes b = open(bootxx, O_RDONLY); 6828fae3551SRodney W. Grimes if (b < 0) 683bef2080aSPhilippe Charnier err(4, "%s", bootxx); 6846cabb348SBruce Evans if (fstat(b, &sb) != 0) 6856cabb348SBruce Evans err(4, "%s", bootxx); 6866cabb348SBruce Evans if (dp->d_secsize + sb.st_size > dp->d_bbsize) 6876cabb348SBruce Evans errx(4, "%s too large", bootxx); 68861de51caSJoerg Wunsch if (read(b, &boot[dp->d_secsize], 68961de51caSJoerg Wunsch (int)(dp->d_bbsize-dp->d_secsize)) < 0) 690bef2080aSPhilippe Charnier err(4, "%s", bootxx); 691cc18e4ccSBruce Evans #else /* !(NUMBOOT > 1) */ 692130cd73aSDoug Rabson #ifdef __alpha__ 693130cd73aSDoug Rabson /* 694130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 695130cd73aSDoug Rabson * second sector of the boot area. The first sector 696130cd73aSDoug Rabson * contains the label and must be edited to contain the 697130cd73aSDoug Rabson * size and location of the primary bootstrap. 698130cd73aSDoug Rabson */ 699cc18e4ccSBruce Evans n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 700130cd73aSDoug Rabson if (n < 0) 701130cd73aSDoug Rabson err(4, "%s", xxboot); 70221c729c2SBruce Evans bootinfo = (u_long *)(boot + 480); 70321c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 70421c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 70521c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 706cc18e4ccSBruce Evans #else /* !__alpha__ */ 7078fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_bbsize) < 0) 708bef2080aSPhilippe Charnier err(4, "%s", xxboot); 709cc18e4ccSBruce Evans #endif /* __alpha__ */ 7106cabb348SBruce Evans if (fstat(b, &sb) != 0) 7116cabb348SBruce Evans err(4, "%s", xxboot); 7128fae3551SRodney W. Grimes bootsize = (int)sb.st_size - dp->d_bbsize; 7138fae3551SRodney W. Grimes if (bootsize > 0) { 7148fae3551SRodney W. Grimes /* XXX assume d_secsize is a power of two */ 7158fae3551SRodney W. Grimes bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 7168fae3551SRodney W. Grimes bootbuf = (char *)malloc((size_t)bootsize); 7178fae3551SRodney W. Grimes if (bootbuf == 0) 718bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7198fae3551SRodney W. Grimes if (read(b, bootbuf, bootsize) < 0) { 7208fae3551SRodney W. Grimes free(bootbuf); 721bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7228fae3551SRodney W. Grimes } 7238fae3551SRodney W. Grimes } 724cc18e4ccSBruce Evans #endif /* NUMBOOT > 1 */ 7258fae3551SRodney W. Grimes (void)close(b); 726cc18e4ccSBruce Evans #endif /* NUMBOOT > 0 */ 7278fae3551SRodney W. Grimes /* 7288fae3551SRodney W. Grimes * Make sure no part of the bootstrap is written in the area 7298fae3551SRodney W. Grimes * reserved for the label. 7308fae3551SRodney W. Grimes */ 7318fae3551SRodney W. Grimes for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 7326bd343a9SPhilippe Charnier if (*p) 7336bd343a9SPhilippe Charnier errx(2, "bootstrap doesn't leave room for disk label"); 7348fae3551SRodney W. Grimes return (lp); 7358fae3551SRodney W. Grimes } 7368fae3551SRodney W. Grimes 73761de51caSJoerg Wunsch void 7388fae3551SRodney W. Grimes display(f, lp) 7398fae3551SRodney W. Grimes FILE *f; 7408fae3551SRodney W. Grimes register struct disklabel *lp; 7418fae3551SRodney W. Grimes { 7428fae3551SRodney W. Grimes register int i, j; 7438fae3551SRodney W. Grimes register struct partition *pp; 7448fae3551SRodney W. Grimes 7458fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 7468fae3551SRodney W. Grimes if ((unsigned) lp->d_type < DKMAXTYPES) 7478fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 7488fae3551SRodney W. Grimes else 7492a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 75061de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 75161de51caSJoerg Wunsch lp->d_typename); 75261de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 75361de51caSJoerg Wunsch lp->d_packname); 7548fae3551SRodney W. Grimes fprintf(f, "flags:"); 7558fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 7568fae3551SRodney W. Grimes fprintf(f, " removeable"); 7578fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 7588fae3551SRodney W. Grimes fprintf(f, " ecc"); 7598fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 7608fae3551SRodney W. Grimes fprintf(f, " badsect"); 7618fae3551SRodney W. Grimes fprintf(f, "\n"); 7622a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 7632a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 7642a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 7652a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 7662a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 7672a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 7682a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 7692a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 7702a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 7712a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 7722a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 7732a1deaaaSBruce Evans (u_long)lp->d_headswitch); 77461de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 7752a1deaaaSBruce Evans (u_long)lp->d_trkseek); 7768fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 7778fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 7788fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 7798fae3551SRodney W. Grimes break; 7808fae3551SRodney W. Grimes if (i < 0) 7818fae3551SRodney W. Grimes i = 0; 7828fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 7832a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 7842a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 7858fae3551SRodney W. Grimes fprintf(f, 786ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 7878fae3551SRodney W. Grimes pp = lp->d_partitions; 7888fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 7898fae3551SRodney W. Grimes if (pp->p_size) { 7902a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 7912a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 7928fae3551SRodney W. Grimes if ((unsigned) pp->p_fstype < FSMAXTYPES) 7938fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 7948fae3551SRodney W. Grimes else 7958fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 7968fae3551SRodney W. Grimes switch (pp->p_fstype) { 7978fae3551SRodney W. Grimes 7988fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 7992a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 8002a1deaaaSBruce Evans (u_long)pp->p_fsize, 8012a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 8028fae3551SRodney W. Grimes break; 8038fae3551SRodney W. Grimes 8048fae3551SRodney W. Grimes case FS_BSDFFS: 8052a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 8062a1deaaaSBruce Evans (u_long)pp->p_fsize, 8072a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 8088fae3551SRodney W. Grimes pp->p_cpg); 8098fae3551SRodney W. Grimes break; 8108fae3551SRodney W. Grimes 811ca4693edSJustin T. Gibbs case FS_BSDLFS: 8122a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 8132a1deaaaSBruce Evans (u_long)pp->p_fsize, 8142a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 815ca4693edSJustin T. Gibbs pp->p_cpg); 816ca4693edSJustin T. Gibbs break; 817ca4693edSJustin T. Gibbs 8188fae3551SRodney W. Grimes default: 8198fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 8208fae3551SRodney W. Grimes break; 8218fae3551SRodney W. Grimes } 8222a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 8232a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 8248fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 8258fae3551SRodney W. Grimes putc('*', f); 8268fae3551SRodney W. Grimes else 8278fae3551SRodney W. Grimes putc(' ', f); 8282a1deaaaSBruce Evans fprintf(f, "- %lu", 8292a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 8302a1deaaaSBruce Evans lp->d_secpercyl - 1) / 8312a1deaaaSBruce Evans lp->d_secpercyl - 1)); 8328fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 8338fae3551SRodney W. Grimes putc('*', f); 8348fae3551SRodney W. Grimes fprintf(f, ")\n"); 8358fae3551SRodney W. Grimes } 8368fae3551SRodney W. Grimes } 8378fae3551SRodney W. Grimes fflush(f); 8388fae3551SRodney W. Grimes } 8398fae3551SRodney W. Grimes 84061de51caSJoerg Wunsch int 8418fae3551SRodney W. Grimes edit(lp, f) 8428fae3551SRodney W. Grimes struct disklabel *lp; 8438fae3551SRodney W. Grimes int f; 8448fae3551SRodney W. Grimes { 845722ceb3fSWarner Losh register int c, fd; 8468fae3551SRodney W. Grimes struct disklabel label; 847722ceb3fSWarner Losh FILE *fp; 8488fae3551SRodney W. Grimes 849722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 850722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 8516bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 8528fae3551SRodney W. Grimes return (1); 8538fae3551SRodney W. Grimes } 854722ceb3fSWarner Losh display(fp, lp); 855722ceb3fSWarner Losh fclose(fp); 8568fae3551SRodney W. Grimes for (;;) { 8578fae3551SRodney W. Grimes if (!editit()) 8588fae3551SRodney W. Grimes break; 859722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 860722ceb3fSWarner Losh if (fp == NULL) { 8616bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 8628fae3551SRodney W. Grimes break; 8638fae3551SRodney W. Grimes } 8648fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 865722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 8668fae3551SRodney W. Grimes *lp = label; 8678fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 868722ceb3fSWarner Losh fclose(fp); 8698fae3551SRodney W. Grimes (void) unlink(tmpfil); 8708fae3551SRodney W. Grimes return (0); 8718fae3551SRodney W. Grimes } 8728fae3551SRodney W. Grimes } 873722ceb3fSWarner Losh fclose(fp); 8748fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 8758fae3551SRodney W. Grimes c = getchar(); 8768fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 8778fae3551SRodney W. Grimes while (getchar() != (int)'\n') 8788fae3551SRodney W. Grimes ; 8798fae3551SRodney W. Grimes if (c == (int)'n') 8808fae3551SRodney W. Grimes break; 8818fae3551SRodney W. Grimes } 8828fae3551SRodney W. Grimes (void) unlink(tmpfil); 8838fae3551SRodney W. Grimes return (1); 8848fae3551SRodney W. Grimes } 8858fae3551SRodney W. Grimes 88661de51caSJoerg Wunsch int 8878fae3551SRodney W. Grimes editit() 8888fae3551SRodney W. Grimes { 8898fae3551SRodney W. Grimes register int pid, xpid; 8908fae3551SRodney W. Grimes int stat, omask; 8918fae3551SRodney W. Grimes 8928fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 8938fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 8948fae3551SRodney W. Grimes if (errno == EPROCLIM) { 8956bd343a9SPhilippe Charnier warnx("you have too many processes"); 8968fae3551SRodney W. Grimes return(0); 8978fae3551SRodney W. Grimes } 8988fae3551SRodney W. Grimes if (errno != EAGAIN) { 8996bd343a9SPhilippe Charnier warn("fork"); 9008fae3551SRodney W. Grimes return(0); 9018fae3551SRodney W. Grimes } 9028fae3551SRodney W. Grimes sleep(1); 9038fae3551SRodney W. Grimes } 9048fae3551SRodney W. Grimes if (pid == 0) { 9058fae3551SRodney W. Grimes register char *ed; 9068fae3551SRodney W. Grimes 9078fae3551SRodney W. Grimes sigsetmask(omask); 9088fae3551SRodney W. Grimes setgid(getgid()); 9098fae3551SRodney W. Grimes setuid(getuid()); 9108fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 9118fae3551SRodney W. Grimes ed = DEFEDITOR; 9127bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 9136bd343a9SPhilippe Charnier err(1, "%s", ed); 9148fae3551SRodney W. Grimes } 9158fae3551SRodney W. Grimes while ((xpid = wait(&stat)) >= 0) 9168fae3551SRodney W. Grimes if (xpid == pid) 9178fae3551SRodney W. Grimes break; 9188fae3551SRodney W. Grimes sigsetmask(omask); 9198fae3551SRodney W. Grimes return(!stat); 9208fae3551SRodney W. Grimes } 9218fae3551SRodney W. Grimes 9228fae3551SRodney W. Grimes char * 9238fae3551SRodney W. Grimes skip(cp) 9248fae3551SRodney W. Grimes register char *cp; 9258fae3551SRodney W. Grimes { 9268fae3551SRodney W. Grimes 9278fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 9288fae3551SRodney W. Grimes cp++; 9298fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 9308fae3551SRodney W. Grimes return ((char *)NULL); 9318fae3551SRodney W. Grimes return (cp); 9328fae3551SRodney W. Grimes } 9338fae3551SRodney W. Grimes 9348fae3551SRodney W. Grimes char * 9358fae3551SRodney W. Grimes word(cp) 9368fae3551SRodney W. Grimes register char *cp; 9378fae3551SRodney W. Grimes { 9388fae3551SRodney W. Grimes register char c; 9398fae3551SRodney W. Grimes 9408fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 9418fae3551SRodney W. Grimes cp++; 9428fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 9438fae3551SRodney W. Grimes *cp++ = '\0'; 9448fae3551SRodney W. Grimes if (c != '#') 9458fae3551SRodney W. Grimes return (skip(cp)); 9468fae3551SRodney W. Grimes } 9478fae3551SRodney W. Grimes return ((char *)NULL); 9488fae3551SRodney W. Grimes } 9498fae3551SRodney W. Grimes 9508fae3551SRodney W. Grimes /* 9518fae3551SRodney W. Grimes * Read an ascii label in from fd f, 9528fae3551SRodney W. Grimes * in the same format as that put out by display(), 9538fae3551SRodney W. Grimes * and fill in lp. 9548fae3551SRodney W. Grimes */ 95561de51caSJoerg Wunsch int 9568fae3551SRodney W. Grimes getasciilabel(f, lp) 9578fae3551SRodney W. Grimes FILE *f; 9588fae3551SRodney W. Grimes register struct disklabel *lp; 9598fae3551SRodney W. Grimes { 9608fae3551SRodney W. Grimes register char **cpp, *cp; 9618fae3551SRodney W. Grimes register struct partition *pp; 9623233afaeSJohn W. De Boskey unsigned int part; 9638fae3551SRodney W. Grimes char *tp, *s, line[BUFSIZ]; 9648fae3551SRodney W. Grimes int v, lineno = 0, errors = 0; 9658fae3551SRodney W. Grimes 9668fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 9678fae3551SRodney W. Grimes lp->d_sbsize = SBSIZE; /* XXX */ 9688fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 9698fae3551SRodney W. Grimes lineno++; 97061de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 9718fae3551SRodney W. Grimes *cp = '\0'; 9728fae3551SRodney W. Grimes cp = skip(line); 9738fae3551SRodney W. Grimes if (cp == NULL) 9748fae3551SRodney W. Grimes continue; 9758fae3551SRodney W. Grimes tp = index(cp, ':'); 9768fae3551SRodney W. Grimes if (tp == NULL) { 9778fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 9788fae3551SRodney W. Grimes errors++; 9798fae3551SRodney W. Grimes continue; 9808fae3551SRodney W. Grimes } 9818fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 9828fae3551SRodney W. Grimes if (streq(cp, "type")) { 9838fae3551SRodney W. Grimes if (tp == NULL) 9848fae3551SRodney W. Grimes tp = "unknown"; 9858fae3551SRodney W. Grimes cpp = dktypenames; 9868fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 9878fae3551SRodney W. Grimes if ((s = *cpp) && streq(s, tp)) { 9888fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 9898fae3551SRodney W. Grimes goto next; 9908fae3551SRodney W. Grimes } 9918fae3551SRodney W. Grimes v = atoi(tp); 9928fae3551SRodney W. Grimes if ((unsigned)v >= DKMAXTYPES) 9938fae3551SRodney W. Grimes fprintf(stderr, "line %d:%s %d\n", lineno, 9948fae3551SRodney W. Grimes "Warning, unknown disk type", v); 9958fae3551SRodney W. Grimes lp->d_type = v; 9968fae3551SRodney W. Grimes continue; 9978fae3551SRodney W. Grimes } 9988fae3551SRodney W. Grimes if (streq(cp, "flags")) { 9998fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 10008fae3551SRodney W. Grimes tp = word(cp); 10018fae3551SRodney W. Grimes if (streq(cp, "removeable")) 10028fae3551SRodney W. Grimes v |= D_REMOVABLE; 10038fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 10048fae3551SRodney W. Grimes v |= D_ECC; 10058fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 10068fae3551SRodney W. Grimes v |= D_BADSECT; 10078fae3551SRodney W. Grimes else { 10088fae3551SRodney W. Grimes fprintf(stderr, 10098fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 10108fae3551SRodney W. Grimes lineno, cp); 10118fae3551SRodney W. Grimes errors++; 10128fae3551SRodney W. Grimes } 10138fae3551SRodney W. Grimes } 10148fae3551SRodney W. Grimes lp->d_flags = v; 10158fae3551SRodney W. Grimes continue; 10168fae3551SRodney W. Grimes } 10178fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 10188fae3551SRodney W. Grimes register int i; 10198fae3551SRodney W. Grimes 10208fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 10218fae3551SRodney W. Grimes lp->d_drivedata[i++] = atoi(cp); 10228fae3551SRodney W. Grimes tp = word(cp); 10238fae3551SRodney W. Grimes } 10248fae3551SRodney W. Grimes continue; 10258fae3551SRodney W. Grimes } 10268fae3551SRodney W. Grimes if (sscanf(cp, "%d partitions", &v) == 1) { 10278fae3551SRodney W. Grimes if (v == 0 || (unsigned)v > MAXPARTITIONS) { 10288fae3551SRodney W. Grimes fprintf(stderr, 10298fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 10308fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 10318fae3551SRodney W. Grimes errors++; 10328fae3551SRodney W. Grimes } else 10338fae3551SRodney W. Grimes lp->d_npartitions = v; 10348fae3551SRodney W. Grimes continue; 10358fae3551SRodney W. Grimes } 10368fae3551SRodney W. Grimes if (tp == NULL) 10378fae3551SRodney W. Grimes tp = ""; 10388fae3551SRodney W. Grimes if (streq(cp, "disk")) { 10398fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 10408fae3551SRodney W. Grimes continue; 10418fae3551SRodney W. Grimes } 10428fae3551SRodney W. Grimes if (streq(cp, "label")) { 10438fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 10448fae3551SRodney W. Grimes continue; 10458fae3551SRodney W. Grimes } 10468fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 10478fae3551SRodney W. Grimes v = atoi(tp); 10486cabb348SBruce Evans if (v <= 0 || (v % DEV_BSIZE) != 0) { 10498fae3551SRodney W. Grimes fprintf(stderr, 10508fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 10518fae3551SRodney W. Grimes lineno, tp); 10528fae3551SRodney W. Grimes errors++; 10538fae3551SRodney W. Grimes } else 10548fae3551SRodney W. Grimes lp->d_secsize = v; 10558fae3551SRodney W. Grimes continue; 10568fae3551SRodney W. Grimes } 10578fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 10588fae3551SRodney W. Grimes v = atoi(tp); 10598fae3551SRodney W. Grimes if (v <= 0) { 10608fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10618fae3551SRodney W. Grimes lineno, tp, cp); 10628fae3551SRodney W. Grimes errors++; 10638fae3551SRodney W. Grimes } else 10648fae3551SRodney W. Grimes lp->d_nsectors = v; 10658fae3551SRodney W. Grimes continue; 10668fae3551SRodney W. Grimes } 10678fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 10688fae3551SRodney W. Grimes v = atoi(tp); 10698fae3551SRodney W. Grimes if (v <= 0) { 10708fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10718fae3551SRodney W. Grimes lineno, tp, cp); 10728fae3551SRodney W. Grimes errors++; 10738fae3551SRodney W. Grimes } else 10748fae3551SRodney W. Grimes lp->d_secpercyl = v; 10758fae3551SRodney W. Grimes continue; 10768fae3551SRodney W. Grimes } 10778fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 10788fae3551SRodney W. Grimes v = atoi(tp); 10798fae3551SRodney W. Grimes if (v <= 0) { 10808fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10818fae3551SRodney W. Grimes lineno, tp, cp); 10828fae3551SRodney W. Grimes errors++; 10838fae3551SRodney W. Grimes } else 10848fae3551SRodney W. Grimes lp->d_ntracks = v; 10858fae3551SRodney W. Grimes continue; 10868fae3551SRodney W. Grimes } 10878fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 10888fae3551SRodney W. Grimes v = atoi(tp); 10898fae3551SRodney W. Grimes if (v <= 0) { 10908fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10918fae3551SRodney W. Grimes lineno, tp, cp); 10928fae3551SRodney W. Grimes errors++; 10938fae3551SRodney W. Grimes } else 10948fae3551SRodney W. Grimes lp->d_ncylinders = v; 10958fae3551SRodney W. Grimes continue; 10968fae3551SRodney W. Grimes } 1097f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 1098f75dd518SBruce Evans v = atoi(tp); 1099f75dd518SBruce Evans if (v <= 0) { 1100f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 1101f75dd518SBruce Evans lineno, tp, cp); 1102f75dd518SBruce Evans errors++; 1103f75dd518SBruce Evans } else 1104f75dd518SBruce Evans lp->d_secperunit = v; 1105f75dd518SBruce Evans continue; 1106f75dd518SBruce Evans } 11078fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 11088fae3551SRodney W. Grimes v = atoi(tp); 11098fae3551SRodney W. Grimes if (v <= 0) { 11108fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11118fae3551SRodney W. Grimes lineno, tp, cp); 11128fae3551SRodney W. Grimes errors++; 11138fae3551SRodney W. Grimes } else 11148fae3551SRodney W. Grimes lp->d_rpm = v; 11158fae3551SRodney W. Grimes continue; 11168fae3551SRodney W. Grimes } 11178fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 11188fae3551SRodney W. Grimes v = atoi(tp); 11198fae3551SRodney W. Grimes if (v <= 0) { 11208fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11218fae3551SRodney W. Grimes lineno, tp, cp); 11228fae3551SRodney W. Grimes errors++; 11238fae3551SRodney W. Grimes } else 11248fae3551SRodney W. Grimes lp->d_interleave = v; 11258fae3551SRodney W. Grimes continue; 11268fae3551SRodney W. Grimes } 11278fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 11288fae3551SRodney W. Grimes v = atoi(tp); 11298fae3551SRodney W. Grimes if (v < 0) { 11308fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11318fae3551SRodney W. Grimes lineno, tp, cp); 11328fae3551SRodney W. Grimes errors++; 11338fae3551SRodney W. Grimes } else 11348fae3551SRodney W. Grimes lp->d_trackskew = v; 11358fae3551SRodney W. Grimes continue; 11368fae3551SRodney W. Grimes } 11378fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 11388fae3551SRodney W. Grimes v = atoi(tp); 11398fae3551SRodney W. Grimes if (v < 0) { 11408fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11418fae3551SRodney W. Grimes lineno, tp, cp); 11428fae3551SRodney W. Grimes errors++; 11438fae3551SRodney W. Grimes } else 11448fae3551SRodney W. Grimes lp->d_cylskew = v; 11458fae3551SRodney W. Grimes continue; 11468fae3551SRodney W. Grimes } 11478fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 11488fae3551SRodney W. Grimes v = atoi(tp); 11498fae3551SRodney W. Grimes if (v < 0) { 11508fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11518fae3551SRodney W. Grimes lineno, tp, cp); 11528fae3551SRodney W. Grimes errors++; 11538fae3551SRodney W. Grimes } else 11548fae3551SRodney W. Grimes lp->d_headswitch = v; 11558fae3551SRodney W. Grimes continue; 11568fae3551SRodney W. Grimes } 11578fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 11588fae3551SRodney W. Grimes v = atoi(tp); 11598fae3551SRodney W. Grimes if (v < 0) { 11608fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11618fae3551SRodney W. Grimes lineno, tp, cp); 11628fae3551SRodney W. Grimes errors++; 11638fae3551SRodney W. Grimes } else 11648fae3551SRodney W. Grimes lp->d_trkseek = v; 11658fae3551SRodney W. Grimes continue; 11668fae3551SRodney W. Grimes } 11673233afaeSJohn W. De Boskey /* the ':' was removed above */ 11683233afaeSJohn W. De Boskey if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') { 11693233afaeSJohn W. De Boskey part = *cp - 'a'; 11703233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 11718fae3551SRodney W. Grimes fprintf(stderr, 11723233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 11733233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 11748fae3551SRodney W. Grimes errors++; 11758fae3551SRodney W. Grimes continue; 11768fae3551SRodney W. Grimes } 11778fae3551SRodney W. Grimes pp = &lp->d_partitions[part]; 11783233afaeSJohn W. De Boskey part_set[part] = 1; 11798fae3551SRodney W. Grimes #define NXTNUM(n) { \ 118013e0abcbSPaul Traina if (tp == NULL) { \ 118113e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 118213e0abcbSPaul Traina errors++; \ 118313e0abcbSPaul Traina break; \ 118413e0abcbSPaul Traina } else { \ 11858fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 11868fae3551SRodney W. Grimes if (tp == NULL) \ 11878fae3551SRodney W. Grimes tp = cp; \ 11888fae3551SRodney W. Grimes (n) = atoi(cp); \ 118913e0abcbSPaul Traina } \ 11908fae3551SRodney W. Grimes } 11913233afaeSJohn W. De Boskey /* retain 1 character following number */ 11923233afaeSJohn W. De Boskey #define NXTWORD(w,n) { \ 11933233afaeSJohn W. De Boskey if (tp == NULL) { \ 11943233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 11953233afaeSJohn W. De Boskey errors++; \ 11963233afaeSJohn W. De Boskey break; \ 11973233afaeSJohn W. De Boskey } else { \ 11983233afaeSJohn W. De Boskey char *tmp; \ 11993233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 12003233afaeSJohn W. De Boskey if (tp == NULL) \ 12013233afaeSJohn W. De Boskey tp = cp; \ 12023233afaeSJohn W. De Boskey (n) = strtol(cp,&tmp,10); \ 12033233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 12043233afaeSJohn W. De Boskey } \ 12053233afaeSJohn W. De Boskey } 12063233afaeSJohn W. De Boskey v = 0; 12073233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 12083233afaeSJohn W. De Boskey if (v < 0 || (v == 0 && part_size_type[part] != '*')) { 12098fae3551SRodney W. Grimes fprintf(stderr, 12108fae3551SRodney W. Grimes "line %d: %s: bad partition size\n", 12118fae3551SRodney W. Grimes lineno, cp); 12128fae3551SRodney W. Grimes errors++; 12133233afaeSJohn W. De Boskey break; 12143233afaeSJohn W. De Boskey } else { 12158fae3551SRodney W. Grimes pp->p_size = v; 12163233afaeSJohn W. De Boskey 12173233afaeSJohn W. De Boskey v = 0; 12183233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 12193233afaeSJohn W. De Boskey if (v < 0 || (v == 0 && 12203233afaeSJohn W. De Boskey part_offset_type[part] != '*' && 12213233afaeSJohn W. De Boskey part_offset_type[part] != '\0')) { 12228fae3551SRodney W. Grimes fprintf(stderr, 12238fae3551SRodney W. Grimes "line %d: %s: bad partition offset\n", 12248fae3551SRodney W. Grimes lineno, cp); 12258fae3551SRodney W. Grimes errors++; 12263233afaeSJohn W. De Boskey break; 12273233afaeSJohn W. De Boskey } else { 12288fae3551SRodney W. Grimes pp->p_offset = v; 12298fae3551SRodney W. Grimes cp = tp, tp = word(cp); 12308fae3551SRodney W. Grimes cpp = fstypenames; 12318fae3551SRodney W. Grimes for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) 12328fae3551SRodney W. Grimes if ((s = *cpp) && streq(s, cp)) { 12333233afaeSJohn W. De Boskey pp->p_fstype = cpp - 12343233afaeSJohn W. De Boskey fstypenames; 12358fae3551SRodney W. Grimes goto gottype; 12368fae3551SRodney W. Grimes } 12378fae3551SRodney W. Grimes if (isdigit(*cp)) 12388fae3551SRodney W. Grimes v = atoi(cp); 12398fae3551SRodney W. Grimes else 12408fae3551SRodney W. Grimes v = FSMAXTYPES; 12418fae3551SRodney W. Grimes if ((unsigned)v >= FSMAXTYPES) { 12423233afaeSJohn W. De Boskey fprintf(stderr, 12433233afaeSJohn W. De Boskey "line %d: Warning, unknown " 12443233afaeSJohn W. De Boskey "filesystem type %s\n", 12453233afaeSJohn W. De Boskey lineno, cp); 12468fae3551SRodney W. Grimes v = FS_UNUSED; 12478fae3551SRodney W. Grimes } 12488fae3551SRodney W. Grimes pp->p_fstype = v; 12493233afaeSJohn W. De Boskey gottype:; 12503233afaeSJohn W. De Boskey /* 12513233afaeSJohn W. De Boskey * Note: NXTNUM will break us out of the 12523233afaeSJohn W. De Boskey * switch only! 12533233afaeSJohn W. De Boskey */ 12548fae3551SRodney W. Grimes switch (pp->p_fstype) { 12553233afaeSJohn W. De Boskey case FS_UNUSED: 12563233afaeSJohn W. De Boskey /* 12573233afaeSJohn W. De Boskey * allow us to accept defaults for 12583233afaeSJohn W. De Boskey * fsize/frag/cpg 12593233afaeSJohn W. De Boskey */ 12603233afaeSJohn W. De Boskey if (tp) { 12618fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 12628fae3551SRodney W. Grimes if (pp->p_fsize == 0) 12638fae3551SRodney W. Grimes break; 12648fae3551SRodney W. Grimes NXTNUM(v); 12658fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 12663233afaeSJohn W. De Boskey } 12673233afaeSJohn W. De Boskey /* else default to 0's */ 12688fae3551SRodney W. Grimes break; 12698fae3551SRodney W. Grimes 12703233afaeSJohn W. De Boskey /* These happen to be the same */ 12718fae3551SRodney W. Grimes case FS_BSDFFS: 1272ca4693edSJustin T. Gibbs case FS_BSDLFS: 12733233afaeSJohn W. De Boskey if (tp) { 1274ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1275ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1276ca4693edSJustin T. Gibbs break; 1277ca4693edSJustin T. Gibbs NXTNUM(v); 1278ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1279ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 12803233afaeSJohn W. De Boskey } else { 12813233afaeSJohn W. De Boskey /* 12823233afaeSJohn W. De Boskey * FIX! poor attempt at 12833233afaeSJohn W. De Boskey * adaptive 12843233afaeSJohn W. De Boskey */ 12853233afaeSJohn W. De Boskey /* 1 GB */ 12863233afaeSJohn W. De Boskey if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) { 12873233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 12883233afaeSJohn W. De Boskey pp->p_fsize = DEFAULT_NEWFS_BLOCK; 12893233afaeSJohn W. De Boskey pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG; 12903233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 12913233afaeSJohn W. De Boskey } else { 12923233afaeSJohn W. De Boskey pp->p_fsize = BIG_NEWFS_BLOCK; 12933233afaeSJohn W. De Boskey pp->p_frag = (unsigned char) BIG_NEWFS_FRAG; 12943233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 12953233afaeSJohn W. De Boskey } 12963233afaeSJohn W. De Boskey } 1297ca4693edSJustin T. Gibbs break; 12988fae3551SRodney W. Grimes default: 12998fae3551SRodney W. Grimes break; 13008fae3551SRodney W. Grimes } 13013233afaeSJohn W. De Boskey 13023233afaeSJohn W. De Boskey /* 13033233afaeSJohn W. De Boskey * note: we may not have 13043233afaeSJohn W. De Boskey * gotten all the entries for 13053233afaeSJohn W. De Boskey * the fs though if we didn't, 13063233afaeSJohn W. De Boskey * errors will be set. 13073233afaeSJohn W. De Boskey */ 13083233afaeSJohn W. De Boskey } 13093233afaeSJohn W. De Boskey } 13108fae3551SRodney W. Grimes continue; 13118fae3551SRodney W. Grimes } 13128fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: Unknown disklabel field\n", 13138fae3551SRodney W. Grimes lineno, cp); 13148fae3551SRodney W. Grimes errors++; 13153233afaeSJohn W. De Boskey next:; 13168fae3551SRodney W. Grimes } 13178fae3551SRodney W. Grimes errors += checklabel(lp); 13188fae3551SRodney W. Grimes return (errors == 0); 13198fae3551SRodney W. Grimes } 13208fae3551SRodney W. Grimes 13218fae3551SRodney W. Grimes /* 13228fae3551SRodney W. Grimes * Check disklabel for errors and fill in 13238fae3551SRodney W. Grimes * derived fields according to supplied values. 13248fae3551SRodney W. Grimes */ 132561de51caSJoerg Wunsch int 13268fae3551SRodney W. Grimes checklabel(lp) 13278fae3551SRodney W. Grimes register struct disklabel *lp; 13288fae3551SRodney W. Grimes { 13298fae3551SRodney W. Grimes register struct partition *pp; 13308fae3551SRodney W. Grimes int i, errors = 0; 13318fae3551SRodney W. Grimes char part; 13323233afaeSJohn W. De Boskey unsigned long total_size, total_percent, current_offset; 13333233afaeSJohn W. De Boskey int seen_default_offset; 13343233afaeSJohn W. De Boskey int hog_part; 13353233afaeSJohn W. De Boskey int j; 13363233afaeSJohn W. De Boskey struct partition *pp2; 13378fae3551SRodney W. Grimes 13388fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 13392a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 13408fae3551SRodney W. Grimes return (1); 13418fae3551SRodney W. Grimes } 13428fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 13432a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 13448fae3551SRodney W. Grimes return (1); 13458fae3551SRodney W. Grimes } 13468fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 13472a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 13488fae3551SRodney W. Grimes return (1); 13498fae3551SRodney W. Grimes } 13508fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 13512a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 13528fae3551SRodney W. Grimes errors++; 13538fae3551SRodney W. Grimes } 13548fae3551SRodney W. Grimes if (lp->d_rpm == 0) 13552a1deaaaSBruce Evans Warning("revolutions/minute 0"); 13568fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 13578fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 13588fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 13598fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 13608fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 13612a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 13628fae3551SRodney W. Grimes errors++; 13638fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 13648fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 13658fae3551SRodney W. Grimes if (lp->d_sbsize == 0) { 13662a1deaaaSBruce Evans fprintf(stderr, "super block size 0\n"); 13678fae3551SRodney W. Grimes errors++; 13688fae3551SRodney W. Grimes } else if (lp->d_sbsize % lp->d_secsize) 13698fae3551SRodney W. Grimes Warning("super block size %% sector-size != 0"); 13708fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 13712a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 13722a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 13733233afaeSJohn W. De Boskey 13743233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 13753233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 13763233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 13773233afaeSJohn W. De Boskey hog_part = -1; 13783233afaeSJohn W. De Boskey /* find all fixed partitions */ 13793233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13803233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13813233afaeSJohn W. De Boskey if (part_set[i]) { 13823233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 13833233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 13843233afaeSJohn W. De Boskey if (i == FULL_DISK_PART) { 13853233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 13863233afaeSJohn W. De Boskey } else { 13873233afaeSJohn W. De Boskey if (hog_part != -1) 13883233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 13893233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 13903233afaeSJohn W. De Boskey else 13913233afaeSJohn W. De Boskey hog_part = i; 13923233afaeSJohn W. De Boskey } 13933233afaeSJohn W. De Boskey } else { 13948d3105e8SWarner Losh off_t size; 13953233afaeSJohn W. De Boskey 13963233afaeSJohn W. De Boskey size = pp->p_size; 13973233afaeSJohn W. De Boskey switch (part_size_type[i]) { 13983233afaeSJohn W. De Boskey case '%': 13993233afaeSJohn W. De Boskey total_percent += size; 14003233afaeSJohn W. De Boskey break; 14013233afaeSJohn W. De Boskey case 'k': 14023233afaeSJohn W. De Boskey case 'K': 14038d3105e8SWarner Losh size *= 1024ULL; 14043233afaeSJohn W. De Boskey break; 14053233afaeSJohn W. De Boskey case 'm': 14063233afaeSJohn W. De Boskey case 'M': 14078d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 14083233afaeSJohn W. De Boskey break; 14093233afaeSJohn W. De Boskey case 'g': 14103233afaeSJohn W. De Boskey case 'G': 14118d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 14123233afaeSJohn W. De Boskey break; 14133233afaeSJohn W. De Boskey case '\0': 14143233afaeSJohn W. De Boskey break; 14153233afaeSJohn W. De Boskey default: 14163233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 14173233afaeSJohn W. De Boskey break; 14183233afaeSJohn W. De Boskey } 14193233afaeSJohn W. De Boskey /* don't count %'s yet */ 14203233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 14213233afaeSJohn W. De Boskey /* 14223233afaeSJohn W. De Boskey * for all not in sectors, convert to 14233233afaeSJohn W. De Boskey * sectors 14243233afaeSJohn W. De Boskey */ 14253233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 14263233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 14273233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 14283233afaeSJohn W. De Boskey i + 'a'); 14293233afaeSJohn W. De Boskey size /= lp->d_secsize; 14303233afaeSJohn W. De Boskey pp->p_size = size; 14313233afaeSJohn W. De Boskey } 14323233afaeSJohn W. De Boskey /* else already in sectors */ 14333233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 14343233afaeSJohn W. De Boskey if (i != FULL_DISK_PART) 14353233afaeSJohn W. De Boskey total_size += size; 14363233afaeSJohn W. De Boskey } 14373233afaeSJohn W. De Boskey } 14383233afaeSJohn W. De Boskey } 14393233afaeSJohn W. De Boskey } 14403233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 14413233afaeSJohn W. De Boskey if (total_percent != 0) { 14423233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 14433233afaeSJohn W. De Boskey if (total_percent > 100) { 14443233afaeSJohn W. De Boskey fprintf(stderr,"total percentage %d is greater than 100\n", 14453233afaeSJohn W. De Boskey total_percent); 14463233afaeSJohn W. De Boskey errors++; 14473233afaeSJohn W. De Boskey } 14483233afaeSJohn W. De Boskey 14493233afaeSJohn W. De Boskey if (free_space > 0) { 14503233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14513233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14523233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 14533233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 14543233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 14553233afaeSJohn W. De Boskey total_size += pp->p_size; 14563233afaeSJohn W. De Boskey 14573233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 14583233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 14593233afaeSJohn W. De Boskey } 14603233afaeSJohn W. De Boskey } 14613233afaeSJohn W. De Boskey } else { 14623233afaeSJohn W. De Boskey fprintf(stderr, 14633233afaeSJohn W. De Boskey "%ld sectors available to give to '*' and '%' partitions\n", 14643233afaeSJohn W. De Boskey free_space); 14653233afaeSJohn W. De Boskey errors++; 14663233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 14673233afaeSJohn W. De Boskey } 14683233afaeSJohn W. De Boskey } 14693233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 14703233afaeSJohn W. De Boskey if (hog_part != -1) { 14713233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 14723233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 14733233afaeSJohn W. De Boskey } 14743233afaeSJohn W. De Boskey 14753233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 14763233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 14773233afaeSJohn W. De Boskey seen_default_offset = 0; 14783233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14793233afaeSJohn W. De Boskey part = 'a' + i; 14803233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14813233afaeSJohn W. De Boskey if (part_set[i]) { 14823233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 14833233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 14843233afaeSJohn W. De Boskey if (i == FULL_DISK_PART) { 14853233afaeSJohn W. De Boskey pp->p_offset = 0; 14863233afaeSJohn W. De Boskey } else { 14873233afaeSJohn W. De Boskey pp->p_offset = current_offset; 14883233afaeSJohn W. De Boskey seen_default_offset = 1; 14893233afaeSJohn W. De Boskey } 14903233afaeSJohn W. De Boskey } else { 14913233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 14923233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 14933233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 14943233afaeSJohn W. De Boskey seen_default_offset && i != FULL_DISK_PART) { 14953233afaeSJohn W. De Boskey fprintf(stderr, 14963233afaeSJohn W. De Boskey "Offset %ld for partition %c overlaps previous partition which ends at %ld\n", 14973233afaeSJohn W. De Boskey pp->p_offset,i+'a',current_offset); 14983233afaeSJohn W. De Boskey fprintf(stderr, 14993233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 15003233afaeSJohn W. De Boskey errors++; 15013233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 15023233afaeSJohn W. De Boskey i != FULL_DISK_PART && seen_default_offset) { 15033233afaeSJohn W. De Boskey /* 15043233afaeSJohn W. De Boskey * this may give unneeded warnings if 15053233afaeSJohn W. De Boskey * partitions are out-of-order 15063233afaeSJohn W. De Boskey */ 15073233afaeSJohn W. De Boskey Warning( 15083233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 15093233afaeSJohn W. De Boskey pp->p_offset, i + 'a', current_offset); 15103233afaeSJohn W. De Boskey } 15113233afaeSJohn W. De Boskey } 15123233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 15133233afaeSJohn W. De Boskey if (i != FULL_DISK_PART) 15143233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 15153233afaeSJohn W. De Boskey } 15163233afaeSJohn W. De Boskey } 15173233afaeSJohn W. De Boskey 15188fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 15198fae3551SRodney W. Grimes part = 'a' + i; 15208fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 15218fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 15222a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 15232a1deaaaSBruce Evans part, (u_long)pp->p_offset); 15248fae3551SRodney W. Grimes #ifdef notdef 15258fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 15268fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 15278fae3551SRodney W. Grimes part); 15288fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 15298fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 15308fae3551SRodney W. Grimes part); 15318fae3551SRodney W. Grimes #endif 15328fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 15338fae3551SRodney W. Grimes fprintf(stderr, 15348fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 15358fae3551SRodney W. Grimes errors++; 15368fae3551SRodney W. Grimes } 15378fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 15388fae3551SRodney W. Grimes fprintf(stderr, 15398fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 15408fae3551SRodney W. Grimes part); 15418fae3551SRodney W. Grimes errors++; 15428fae3551SRodney W. Grimes } 15433233afaeSJohn W. De Boskey if (i == FULL_DISK_PART) 15443233afaeSJohn W. De Boskey { 15453233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 15463233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 15473233afaeSJohn W. De Boskey if (pp->p_offset != 0) 15483233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 15493233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 15503233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 15513233afaeSJohn W. De Boskey 15523233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 15533233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 15543233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 15553233afaeSJohn W. De Boskey "standard system utilities",part); 15563233afaeSJohn W. De Boskey } 15573233afaeSJohn W. De Boskey } 15583233afaeSJohn W. De Boskey 15593233afaeSJohn W. De Boskey /* check for overlaps */ 15603233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 15613233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 15623233afaeSJohn W. De Boskey /* partition 2 ('c') is special */ 15633233afaeSJohn W. De Boskey if (j != FULL_DISK_PART && i != FULL_DISK_PART && 15643233afaeSJohn W. De Boskey part_set[i] && part_set[j]) { 15653233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 15663233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 15673233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 15683233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 15693233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 15703233afaeSJohn W. De Boskey j + 'a', i + 'a'); 15713233afaeSJohn W. De Boskey errors++; 15723233afaeSJohn W. De Boskey } 15733233afaeSJohn W. De Boskey } 15743233afaeSJohn W. De Boskey } 15758fae3551SRodney W. Grimes } 15768fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 15778fae3551SRodney W. Grimes part = 'a' + i; 15788fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 15798fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 15802a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 15812a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 15828fae3551SRodney W. Grimes } 15838fae3551SRodney W. Grimes return (errors); 15848fae3551SRodney W. Grimes } 15858fae3551SRodney W. Grimes 15868fae3551SRodney W. Grimes /* 1587425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1588425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1589425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1590425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1591425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1592425bed3aSJoerg Wunsch * 1593425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1594425bed3aSJoerg Wunsch */ 1595425bed3aSJoerg Wunsch struct disklabel * 1596425bed3aSJoerg Wunsch getvirginlabel(void) 1597425bed3aSJoerg Wunsch { 1598425bed3aSJoerg Wunsch static struct disklabel lab; 1599425bed3aSJoerg Wunsch char namebuf[BBSIZE]; 1600425bed3aSJoerg Wunsch int f; 1601425bed3aSJoerg Wunsch 1602425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1603e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 160443be698cSBruce Evans return (NULL); 1605425bed3aSJoerg Wunsch } 160643c6c959SDavid E. O'Brien (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1607e18fb238SBruce Evans if ((f = open(namebuf, O_RDONLY)) == -1) { 1608e18fb238SBruce Evans warn("cannot open %s", namebuf); 160943be698cSBruce Evans return (NULL); 1610425bed3aSJoerg Wunsch } 1611ff7d5162SJordan K. Hubbard 1612ff7d5162SJordan K. Hubbard /* 1613ff7d5162SJordan K. Hubbard * Try to use the new get-virgin-label ioctl. If it fails, 1614ff7d5162SJordan K. Hubbard * fallback to the old get-disdk-info ioctl. 1615ff7d5162SJordan K. Hubbard */ 1616ff7d5162SJordan K. Hubbard if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) { 1617425bed3aSJoerg Wunsch if (ioctl(f, DIOCGDINFO, &lab) < 0) { 1618e18fb238SBruce Evans warn("ioctl DIOCGDINFO"); 1619425bed3aSJoerg Wunsch close(f); 162043be698cSBruce Evans return (NULL); 1621425bed3aSJoerg Wunsch } 1622ff7d5162SJordan K. Hubbard } 1623425bed3aSJoerg Wunsch close(f); 1624e18fb238SBruce Evans lab.d_boot0 = NULL; 1625e18fb238SBruce Evans lab.d_boot1 = NULL; 162643be698cSBruce Evans return (&lab); 1627425bed3aSJoerg Wunsch } 1628425bed3aSJoerg Wunsch 1629425bed3aSJoerg Wunsch /* 16308fae3551SRodney W. Grimes * If we are installing a boot program that doesn't fit in d_bbsize 16318fae3551SRodney W. Grimes * we need to mark those partitions that the boot overflows into. 16328fae3551SRodney W. Grimes * This allows newfs to prevent creation of a filesystem where it might 16338fae3551SRodney W. Grimes * clobber bootstrap code. 16348fae3551SRodney W. Grimes */ 163561de51caSJoerg Wunsch void 16368fae3551SRodney W. Grimes setbootflag(lp) 16378fae3551SRodney W. Grimes register struct disklabel *lp; 16388fae3551SRodney W. Grimes { 16398fae3551SRodney W. Grimes register struct partition *pp; 16408fae3551SRodney W. Grimes int i, errors = 0; 16418fae3551SRodney W. Grimes char part; 16428fae3551SRodney W. Grimes u_long boffset; 16438fae3551SRodney W. Grimes 16448fae3551SRodney W. Grimes if (bootbuf == 0) 16458fae3551SRodney W. Grimes return; 16468fae3551SRodney W. Grimes boffset = bootsize / lp->d_secsize; 16478fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 16488fae3551SRodney W. Grimes part = 'a' + i; 16498fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 16508fae3551SRodney W. Grimes if (pp->p_size == 0) 16518fae3551SRodney W. Grimes continue; 16528fae3551SRodney W. Grimes if (boffset <= pp->p_offset) { 16538fae3551SRodney W. Grimes if (pp->p_fstype == FS_BOOT) 16548fae3551SRodney W. Grimes pp->p_fstype = FS_UNUSED; 16558fae3551SRodney W. Grimes } else if (pp->p_fstype != FS_BOOT) { 16568fae3551SRodney W. Grimes if (pp->p_fstype != FS_UNUSED) { 16578fae3551SRodney W. Grimes fprintf(stderr, 16588fae3551SRodney W. Grimes "boot overlaps used partition %c\n", 16598fae3551SRodney W. Grimes part); 16608fae3551SRodney W. Grimes errors++; 16618fae3551SRodney W. Grimes } else { 16628fae3551SRodney W. Grimes pp->p_fstype = FS_BOOT; 16638fae3551SRodney W. Grimes Warning("boot overlaps partition %c, %s", 16648fae3551SRodney W. Grimes part, "marked as FS_BOOT"); 16658fae3551SRodney W. Grimes } 16668fae3551SRodney W. Grimes } 16678fae3551SRodney W. Grimes } 16686bd343a9SPhilippe Charnier if (errors) 16696bd343a9SPhilippe Charnier errx(4, "cannot install boot program"); 16708fae3551SRodney W. Grimes } 16718fae3551SRodney W. Grimes 16728fae3551SRodney W. Grimes /*VARARGS1*/ 167361de51caSJoerg Wunsch void 167461de51caSJoerg Wunsch Warning(char *fmt, ...) 16758fae3551SRodney W. Grimes { 167661de51caSJoerg Wunsch va_list ap; 16778fae3551SRodney W. Grimes 16788fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 167961de51caSJoerg Wunsch va_start(ap, fmt); 168061de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 16818fae3551SRodney W. Grimes fprintf(stderr, "\n"); 168261de51caSJoerg Wunsch va_end(ap); 16838fae3551SRodney W. Grimes } 16848fae3551SRodney W. Grimes 168561de51caSJoerg Wunsch void 16868fae3551SRodney W. Grimes usage() 16878fae3551SRodney W. Grimes { 16888fae3551SRodney W. Grimes #if NUMBOOT > 0 1689bef2080aSPhilippe 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", 16908fae3551SRodney W. Grimes "usage: disklabel [-r] disk", 1691bef2080aSPhilippe Charnier "\t\t(to read label)", 16923233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1693bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 16943233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1695bef2080aSPhilippe Charnier "\t\t(to edit label)", 16963233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1697bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 16988fae3551SRodney W. Grimes #if NUMBOOT > 1 16993233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1700bef2080aSPhilippe Charnier "\t\t(to install boot program with existing label)", 17013233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1702bef2080aSPhilippe Charnier "\t\t(to write label and boot program)", 17033233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1704bef2080aSPhilippe Charnier "\t\t(to restore label and boot program)", 17058fae3551SRodney W. Grimes #else 17063233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1707bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 17083233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1709bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 17103233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 1711bef2080aSPhilippe Charnier "\t\t(to restore label and install boot program)", 17128fae3551SRodney W. Grimes #endif 1713bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1714bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17158fae3551SRodney W. Grimes #else 1716bef2080aSPhilippe Charnier fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 17178fae3551SRodney W. Grimes "usage: disklabel [-r] disk", "(to read label)", 17183233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1719bef2080aSPhilippe Charnier "\t\t(to write label)", 17203233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1721bef2080aSPhilippe Charnier "\t\t(to edit label)", 17223233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1723bef2080aSPhilippe Charnier "\t\t(to restore label)", 1724bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1725bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17268fae3551SRodney W. Grimes #endif 17278fae3551SRodney W. Grimes exit(1); 17288fae3551SRodney W. Grimes } 1729