18fae3551SRodney W. Grimes /* 2fd43aa1cSDavid E. O'Brien * Copyright (c) 1994, 1995 Gordon W. Ross 3fd43aa1cSDavid E. O'Brien * Copyright (c) 1994 Theo de Raadt 4fd43aa1cSDavid E. O'Brien * All rights reserved. 58fae3551SRodney W. Grimes * Copyright (c) 1987, 1993 68fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 98fae3551SRodney W. Grimes * Symmetric Computer Systems. 108fae3551SRodney W. Grimes * 118fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 128fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 138fae3551SRodney W. Grimes * are met: 148fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 158fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 168fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 178fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 188fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 198fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 208fae3551SRodney W. Grimes * must display the following acknowledgement: 218fae3551SRodney W. Grimes * This product includes software developed by the University of 228fae3551SRodney W. Grimes * California, Berkeley and its contributors. 23fd43aa1cSDavid E. O'Brien * This product includes software developed by Theo de Raadt. 248fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 258fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 268fae3551SRodney W. Grimes * without specific prior written permission. 278fae3551SRodney W. Grimes * 288fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 298fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 308fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 318fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 328fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 338fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 348fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 358fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 368fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 378fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 388fae3551SRodney W. Grimes * SUCH DAMAGE. 39fd43aa1cSDavid E. O'Brien * 40fd43aa1cSDavid E. O'Brien * from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $ 418fae3551SRodney W. Grimes */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 446bd343a9SPhilippe Charnier static const char copyright[] = 458fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\ 468fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 478fae3551SRodney W. Grimes #endif /* not lint */ 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes #ifndef lint 506bd343a9SPhilippe Charnier #if 0 518fae3551SRodney W. Grimes static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94"; 528fae3551SRodney W. Grimes /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 536bd343a9SPhilippe Charnier #endif 548fae3551SRodney W. Grimes #endif /* not lint */ 558fae3551SRodney W. Grimes 56c8223965SMark Murray #include <sys/cdefs.h> 57c8223965SMark Murray __FBSDID("$FreeBSD$"); 58c8223965SMark Murray 598fae3551SRodney W. Grimes #include <sys/param.h> 608fae3551SRodney W. Grimes #include <sys/file.h> 618fae3551SRodney W. Grimes #include <sys/stat.h> 6261de51caSJoerg Wunsch #include <sys/wait.h> 638fae3551SRodney W. Grimes #define DKTYPENAMES 648fae3551SRodney W. Grimes #include <sys/disklabel.h> 65fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 66fd43aa1cSDavid E. O'Brien #include <sys/sun_disklabel.h> 67fd43aa1cSDavid E. O'Brien #endif 68c8223965SMark Murray 698fae3551SRodney W. Grimes #include <unistd.h> 708fae3551SRodney W. Grimes #include <string.h> 718fae3551SRodney W. Grimes #include <stdio.h> 7261de51caSJoerg Wunsch #include <stdlib.h> 7361de51caSJoerg Wunsch #include <signal.h> 7461de51caSJoerg Wunsch #include <stdarg.h> 758fae3551SRodney W. Grimes #include <ctype.h> 76bef2080aSPhilippe Charnier #include <err.h> 7737736675SWarner Losh #include <errno.h> 78c8223965SMark Murray 798fae3551SRodney W. Grimes #include "pathnames.h" 808fae3551SRodney W. Grimes 818fae3551SRodney W. Grimes /* 828fae3551SRodney W. Grimes * Disklabel: read and write disklabels. 838fae3551SRodney W. Grimes * The label is usually placed on one of the first sectors of the disk. 848fae3551SRodney W. Grimes * Many machines also place a bootstrap in the same area, 858fae3551SRodney W. Grimes * in which case the label is embedded in the bootstrap. 868fae3551SRodney W. Grimes * The bootstrap source must leave space at the proper offset 878fae3551SRodney W. Grimes * for the label on such machines. 888fae3551SRodney W. Grimes */ 898fae3551SRodney W. Grimes 908fae3551SRodney W. Grimes #ifndef BBSIZE 918fae3551SRodney W. Grimes #define BBSIZE 8192 /* size of boot area, with label */ 928fae3551SRodney W. Grimes #endif 938fae3551SRodney W. Grimes 943233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 953233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 963233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 973233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 983233afaeSJohn W. De Boskey 993233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 100f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG 2048U 1013233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 1023233afaeSJohn W. De Boskey 10390b8f6baSDavid E. O'Brien #if defined(__i386__) || defined(__ia64__) 10490b8f6baSDavid E. O'Brien #define NUMBOOT 2 10587efda01SBenno Rice #elif defined(__alpha__) || defined(__sparc64__) || defined(__powerpc__) 1068fae3551SRodney W. Grimes #define NUMBOOT 1 1078fae3551SRodney W. Grimes #else 10890b8f6baSDavid E. O'Brien #error I do not know about this architecture. 1098fae3551SRodney W. Grimes #endif 1108fae3551SRodney W. Grimes 111326c7cdaSWarner Losh void makelabel(const char *, const char *, struct disklabel *); 112326c7cdaSWarner Losh int writelabel(int, const char *, struct disklabel *); 113326c7cdaSWarner Losh void l_perror(const char *); 114326c7cdaSWarner Losh struct disklabel *readlabel(int); 115326c7cdaSWarner Losh struct disklabel *makebootarea(char *, struct disklabel *, int); 116326c7cdaSWarner Losh void display(FILE *, const struct disklabel *); 117326c7cdaSWarner Losh int edit(struct disklabel *, int); 118326c7cdaSWarner Losh int editit(void); 119326c7cdaSWarner Losh char *skip(char *); 120326c7cdaSWarner Losh char *word(char *); 121326c7cdaSWarner Losh int getasciilabel(FILE *, struct disklabel *); 12267b46708SIan Dowse int getasciipartspec(char *, struct disklabel *, int, int); 123326c7cdaSWarner Losh int checklabel(struct disklabel *); 124326c7cdaSWarner Losh void setbootflag(struct disklabel *); 125326c7cdaSWarner Losh void Warning(const char *, ...) __printflike(1, 2); 126326c7cdaSWarner Losh void usage(void); 127326c7cdaSWarner Losh struct disklabel *getvirginlabel(void); 12861de51caSJoerg Wunsch 1298fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1308fae3551SRodney W. Grimes #define streq(a,b) (strcmp(a,b) == 0) 1318fae3551SRodney W. Grimes 1328fae3551SRodney W. Grimes char *dkname; 1338fae3551SRodney W. Grimes char *specname; 134aaae3130SKris Kennaway char tmpfil[] = PATH_TMPFILE; 1358fae3551SRodney W. Grimes 1368fae3551SRodney W. Grimes char namebuf[BBSIZE], *np = namebuf; 1378fae3551SRodney W. Grimes struct disklabel lab; 1388fae3551SRodney W. Grimes char bootarea[BBSIZE]; 139c8223965SMark Murray char blank[] = ""; 140c8223965SMark Murray char unknown[] = "unknown"; 1418fae3551SRodney W. Grimes 1423233afaeSJohn W. De Boskey #define MAX_PART ('z') 1433233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 1443233afaeSJohn W. De Boskey char part_size_type[MAX_NUM_PARTS]; 1453233afaeSJohn W. De Boskey char part_offset_type[MAX_NUM_PARTS]; 1463233afaeSJohn W. De Boskey int part_set[MAX_NUM_PARTS]; 1473233afaeSJohn W. De Boskey 1488fae3551SRodney W. Grimes #if NUMBOOT > 0 1498fae3551SRodney W. Grimes int installboot; /* non-zero if we should install a boot program */ 1508fae3551SRodney W. Grimes char *bootbuf; /* pointer to buffer with remainder of boot prog */ 1518fae3551SRodney W. Grimes int bootsize; /* size of remaining boot program */ 1528fae3551SRodney W. Grimes char *xxboot; /* primary boot */ 1538fae3551SRodney W. Grimes char *bootxx; /* secondary boot */ 1548fae3551SRodney W. Grimes char boot0[MAXPATHLEN]; 1558fae3551SRodney W. Grimes char boot1[MAXPATHLEN]; 1568fae3551SRodney W. Grimes #endif 1578fae3551SRodney W. Grimes 1588fae3551SRodney W. Grimes enum { 1598fae3551SRodney W. Grimes UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT 1608fae3551SRodney W. Grimes } op = UNSPEC; 1618fae3551SRodney W. Grimes 1628fae3551SRodney W. Grimes int rflag; 1633233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1648fae3551SRodney W. Grimes 1653233afaeSJohn W. De Boskey #define OPTIONS "BNRWb:enrs:w" 1668fae3551SRodney W. Grimes 16761de51caSJoerg Wunsch int 168326c7cdaSWarner Losh main(int argc, char *argv[]) 1698fae3551SRodney W. Grimes { 170c1fdb43fSWarner Losh struct disklabel *lp; 1718fae3551SRodney W. Grimes FILE *t; 1723b3038a6SBill Fumerola int ch, f = 0, flag, error = 0; 1738fae3551SRodney W. Grimes char *name = 0; 1748fae3551SRodney W. Grimes 1758d64695cSWarner Losh while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1768fae3551SRodney W. Grimes switch (ch) { 1778fae3551SRodney W. Grimes #if NUMBOOT > 0 1788fae3551SRodney W. Grimes case 'B': 1798fae3551SRodney W. Grimes ++installboot; 1808fae3551SRodney W. Grimes break; 1818fae3551SRodney W. Grimes case 'b': 1828fae3551SRodney W. Grimes xxboot = optarg; 1838fae3551SRodney W. Grimes break; 1848fae3551SRodney W. Grimes #if NUMBOOT > 1 1858fae3551SRodney W. Grimes case 's': 1868fae3551SRodney W. Grimes bootxx = optarg; 1878fae3551SRodney W. Grimes break; 1888fae3551SRodney W. Grimes #endif 1898fae3551SRodney W. Grimes #endif 1908fae3551SRodney W. Grimes case 'N': 1918fae3551SRodney W. Grimes if (op != UNSPEC) 1928fae3551SRodney W. Grimes usage(); 1938fae3551SRodney W. Grimes op = NOWRITE; 1948fae3551SRodney W. Grimes break; 1953233afaeSJohn W. De Boskey case 'n': 1963233afaeSJohn W. De Boskey disable_write = 1; 1973233afaeSJohn W. De Boskey break; 1988fae3551SRodney W. Grimes case 'R': 1998fae3551SRodney W. Grimes if (op != UNSPEC) 2008fae3551SRodney W. Grimes usage(); 2018fae3551SRodney W. Grimes op = RESTORE; 2028fae3551SRodney W. Grimes break; 2038fae3551SRodney W. Grimes case 'W': 2048fae3551SRodney W. Grimes if (op != UNSPEC) 2058fae3551SRodney W. Grimes usage(); 2068fae3551SRodney W. Grimes op = WRITEABLE; 2078fae3551SRodney W. Grimes break; 2088fae3551SRodney W. Grimes case 'e': 2098fae3551SRodney W. Grimes if (op != UNSPEC) 2108fae3551SRodney W. Grimes usage(); 2118fae3551SRodney W. Grimes op = EDIT; 2128fae3551SRodney W. Grimes break; 2138fae3551SRodney W. Grimes case 'r': 2148fae3551SRodney W. Grimes ++rflag; 2158fae3551SRodney W. Grimes break; 2168fae3551SRodney W. Grimes case 'w': 2178fae3551SRodney W. Grimes if (op != UNSPEC) 2188fae3551SRodney W. Grimes usage(); 2198fae3551SRodney W. Grimes op = WRITE; 2208fae3551SRodney W. Grimes break; 2218fae3551SRodney W. Grimes case '?': 2228fae3551SRodney W. Grimes default: 2238fae3551SRodney W. Grimes usage(); 2248fae3551SRodney W. Grimes } 2258fae3551SRodney W. Grimes argc -= optind; 2268fae3551SRodney W. Grimes argv += optind; 2278fae3551SRodney W. Grimes #if NUMBOOT > 0 2288fae3551SRodney W. Grimes if (installboot) { 2298fae3551SRodney W. Grimes rflag++; 2308fae3551SRodney W. Grimes if (op == UNSPEC) 2318fae3551SRodney W. Grimes op = WRITEBOOT; 2328fae3551SRodney W. Grimes } else { 2338fae3551SRodney W. Grimes if (op == UNSPEC) 2348fae3551SRodney W. Grimes op = READ; 2358fae3551SRodney W. Grimes xxboot = bootxx = 0; 2368fae3551SRodney W. Grimes } 2378fae3551SRodney W. Grimes #else 2388fae3551SRodney W. Grimes if (op == UNSPEC) 2398fae3551SRodney W. Grimes op = READ; 2408fae3551SRodney W. Grimes #endif 2418fae3551SRodney W. Grimes if (argc < 1) 2428fae3551SRodney W. Grimes usage(); 2438fae3551SRodney W. Grimes 2448fae3551SRodney W. Grimes dkname = argv[0]; 2458fae3551SRodney W. Grimes if (dkname[0] != '/') { 24609c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2478fae3551SRodney W. Grimes specname = np; 2488fae3551SRodney W. Grimes np += strlen(specname) + 1; 2498fae3551SRodney W. Grimes } else 2508fae3551SRodney W. Grimes specname = dkname; 2518fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2528fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 25343c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 2548fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 2558fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2568fae3551SRodney W. Grimes } 2578fae3551SRodney W. Grimes if (f < 0) 258bef2080aSPhilippe Charnier err(4, "%s", specname); 2598fae3551SRodney W. Grimes 2608fae3551SRodney W. Grimes switch(op) { 2618fae3551SRodney W. Grimes 2623b3038a6SBill Fumerola case UNSPEC: 2633b3038a6SBill Fumerola break; 2643b3038a6SBill Fumerola 2658fae3551SRodney W. Grimes case EDIT: 2668fae3551SRodney W. Grimes if (argc != 1) 2678fae3551SRodney W. Grimes usage(); 2688fae3551SRodney W. Grimes lp = readlabel(f); 2698fae3551SRodney W. Grimes error = edit(lp, f); 2708fae3551SRodney W. Grimes break; 2718fae3551SRodney W. Grimes 2728fae3551SRodney W. Grimes case NOWRITE: 2738fae3551SRodney W. Grimes flag = 0; 2748fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 275bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 2768fae3551SRodney W. Grimes break; 2778fae3551SRodney W. Grimes 2788fae3551SRodney W. Grimes case READ: 2798fae3551SRodney W. Grimes if (argc != 1) 2808fae3551SRodney W. Grimes usage(); 2818fae3551SRodney W. Grimes lp = readlabel(f); 2828fae3551SRodney W. Grimes display(stdout, lp); 2838fae3551SRodney W. Grimes error = checklabel(lp); 2848fae3551SRodney W. Grimes break; 2858fae3551SRodney W. Grimes 2868fae3551SRodney W. Grimes case RESTORE: 2878fae3551SRodney W. Grimes #if NUMBOOT > 0 2888fae3551SRodney W. Grimes if (installboot && argc == 3) { 2898fae3551SRodney W. Grimes makelabel(argv[2], 0, &lab); 2908fae3551SRodney W. Grimes argc--; 2916cabb348SBruce Evans 2926cabb348SBruce Evans /* 2936cabb348SBruce Evans * We only called makelabel() for its side effect 2946cabb348SBruce Evans * of setting the bootstrap file names. Discard 2956cabb348SBruce Evans * all changes to `lab' so that all values in the 2966cabb348SBruce Evans * final label come from the ASCII label. 2976cabb348SBruce Evans */ 2986cabb348SBruce Evans bzero((char *)&lab, sizeof(lab)); 2998fae3551SRodney W. Grimes } 3008fae3551SRodney W. Grimes #endif 3018fae3551SRodney W. Grimes if (argc != 2) 3028fae3551SRodney W. Grimes usage(); 3038fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 304bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 3056cabb348SBruce Evans if (!getasciilabel(t, &lab)) 3066cabb348SBruce Evans exit(1); 3076cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 3086cabb348SBruce Evans *lp = lab; 3098fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3108fae3551SRodney W. Grimes break; 3118fae3551SRodney W. Grimes 3128fae3551SRodney W. Grimes case WRITE: 3138fae3551SRodney W. Grimes if (argc == 3) { 3148fae3551SRodney W. Grimes name = argv[2]; 3158fae3551SRodney W. Grimes argc--; 3168fae3551SRodney W. Grimes } 3178fae3551SRodney W. Grimes if (argc != 2) 3188fae3551SRodney W. Grimes usage(); 3198fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3208fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3218fae3551SRodney W. Grimes *lp = lab; 3228fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3238fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3248fae3551SRodney W. Grimes break; 3258fae3551SRodney W. Grimes 3268fae3551SRodney W. Grimes case WRITEABLE: 3278fae3551SRodney W. Grimes flag = 1; 3288fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 329bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 3308fae3551SRodney W. Grimes break; 3318fae3551SRodney W. Grimes 3328fae3551SRodney W. Grimes #if NUMBOOT > 0 3338fae3551SRodney W. Grimes case WRITEBOOT: 3348fae3551SRodney W. Grimes { 3358fae3551SRodney W. Grimes struct disklabel tlab; 3368fae3551SRodney W. Grimes 3378fae3551SRodney W. Grimes lp = readlabel(f); 3388fae3551SRodney W. Grimes tlab = *lp; 3398fae3551SRodney W. Grimes if (argc == 2) 3408fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3418fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3428fae3551SRodney W. Grimes *lp = tlab; 3438fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3448fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3458fae3551SRodney W. Grimes break; 3468fae3551SRodney W. Grimes } 3478fae3551SRodney W. Grimes #endif 3488fae3551SRodney W. Grimes } 3498fae3551SRodney W. Grimes exit(error); 3508fae3551SRodney W. Grimes } 3518fae3551SRodney W. Grimes 3528fae3551SRodney W. Grimes /* 3538fae3551SRodney W. Grimes * Construct a prototype disklabel from /etc/disktab. As a side 3548fae3551SRodney W. Grimes * effect, set the names of the primary and secondary boot files 3558fae3551SRodney W. Grimes * if specified. 3568fae3551SRodney W. Grimes */ 35761de51caSJoerg Wunsch void 358326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp) 3598fae3551SRodney W. Grimes { 360326c7cdaSWarner Losh struct disklabel *dp; 361425bed3aSJoerg Wunsch 362425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 363425bed3aSJoerg Wunsch dp = getvirginlabel(); 364425bed3aSJoerg Wunsch else 3658fae3551SRodney W. Grimes dp = getdiskbyname(type); 3666bd343a9SPhilippe Charnier if (dp == NULL) 3676bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3688fae3551SRodney W. Grimes *lp = *dp; 3698fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 3708fae3551SRodney W. Grimes if (name) 3718fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 3728fae3551SRodney W. Grimes } 3738fae3551SRodney W. Grimes 37461de51caSJoerg Wunsch int 375326c7cdaSWarner Losh writelabel(int f, const char *boot, struct disklabel *lp) 3768fae3551SRodney W. Grimes { 3778fae3551SRodney W. Grimes int flag; 378130cd73aSDoug Rabson #ifdef __alpha__ 379130cd73aSDoug Rabson u_long *p, sum; 380130cd73aSDoug Rabson int i; 381130cd73aSDoug Rabson #endif 382fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 383fd43aa1cSDavid E. O'Brien struct sun_disklabel *sl; 384fd43aa1cSDavid E. O'Brien u_short cksum, *sp1, *sp2; 385fd43aa1cSDavid E. O'Brien struct partition *npp; 386fd43aa1cSDavid E. O'Brien struct sun_dkpart *spp; 387fd43aa1cSDavid E. O'Brien int i, secpercyl; 388cc18e4ccSBruce Evans #endif 389130cd73aSDoug Rabson 3903233afaeSJohn W. De Boskey if (disable_write) { 3913233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 3923233afaeSJohn W. De Boskey display(stdout, lp); 3933233afaeSJohn W. De Boskey return (0); 3943233afaeSJohn W. De Boskey } else { 3958fae3551SRodney W. Grimes setbootflag(lp); 3968fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 3978fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 3988fae3551SRodney W. Grimes lp->d_checksum = 0; 3998fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 4008fae3551SRodney W. Grimes if (rflag) { 4018fae3551SRodney W. Grimes /* 4028fae3551SRodney W. Grimes * First set the kernel disk label, 4038fae3551SRodney W. Grimes * then write a label to the raw disk. 4048fae3551SRodney W. Grimes * If the SDINFO ioctl fails because it is unimplemented, 4058fae3551SRodney W. Grimes * keep going; otherwise, the kernel consistency checks 4068fae3551SRodney W. Grimes * may prevent us from changing the current (in-core) 4078fae3551SRodney W. Grimes * label. 4088fae3551SRodney W. Grimes */ 4098fae3551SRodney W. Grimes if (ioctl(f, DIOCSDINFO, lp) < 0 && 4108fae3551SRodney W. Grimes errno != ENODEV && errno != ENOTTY) { 4118fae3551SRodney W. Grimes l_perror("ioctl DIOCSDINFO"); 4128fae3551SRodney W. Grimes return (1); 4138fae3551SRodney W. Grimes } 4148fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 415130cd73aSDoug Rabson 416130cd73aSDoug Rabson #ifdef __alpha__ 417cc18e4ccSBruce Evans /* 418cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 419cc18e4ccSBruce Evans */ 420130cd73aSDoug Rabson for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) 421130cd73aSDoug Rabson sum += p[i]; 422130cd73aSDoug Rabson p[63] = sum; 423130cd73aSDoug Rabson #endif 424fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 425fd43aa1cSDavid E. O'Brien /* 426fd43aa1cSDavid E. O'Brien * Generate a Sun disklabel around the BSD label for 427fd43aa1cSDavid E. O'Brien * PROM compatability. 428fd43aa1cSDavid E. O'Brien */ 429fd43aa1cSDavid E. O'Brien sl = (struct sun_disklabel *)boot; 430fd43aa1cSDavid E. O'Brien memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname)); 431fd43aa1cSDavid E. O'Brien sl->sl_rpm = lp->d_rpm; 432fd43aa1cSDavid E. O'Brien sl->sl_pcylinders = lp->d_ncylinders + 433fd43aa1cSDavid E. O'Brien lp->d_acylinders; /* XXX */ 434fd43aa1cSDavid E. O'Brien sl->sl_sparespercyl = lp->d_sparespercyl; 435fd43aa1cSDavid E. O'Brien sl->sl_interleave = lp->d_interleave; 436fd43aa1cSDavid E. O'Brien sl->sl_ncylinders = lp->d_ncylinders; 437fd43aa1cSDavid E. O'Brien sl->sl_acylinders = lp->d_acylinders; 438fd43aa1cSDavid E. O'Brien sl->sl_ntracks = lp->d_ntracks; 439fd43aa1cSDavid E. O'Brien sl->sl_nsectors = lp->d_nsectors; 440fd43aa1cSDavid E. O'Brien sl->sl_magic = SUN_DKMAGIC; 441fd43aa1cSDavid E. O'Brien secpercyl = sl->sl_nsectors * sl->sl_ntracks; 442fd43aa1cSDavid E. O'Brien for (i = 0; i < 8; i++) { 443fd43aa1cSDavid E. O'Brien spp = &sl->sl_part[i]; 444fd43aa1cSDavid E. O'Brien npp = &lp->d_partitions[i]; 445fd43aa1cSDavid E. O'Brien /* 446fd43aa1cSDavid E. O'Brien * SunOS partitions must start on a cylinder 447fd43aa1cSDavid E. O'Brien * boundary. Note this restriction is forced 448fd43aa1cSDavid E. O'Brien * upon FreeBSD/sparc64 labels too, since we 449fd43aa1cSDavid E. O'Brien * want to keep both labels synchronised. 450fd43aa1cSDavid E. O'Brien */ 451fd43aa1cSDavid E. O'Brien spp->sdkp_cyloffset = npp->p_offset / secpercyl; 452fd43aa1cSDavid E. O'Brien spp->sdkp_nsectors = npp->p_size; 453fd43aa1cSDavid E. O'Brien } 454130cd73aSDoug Rabson 455fd43aa1cSDavid E. O'Brien /* Compute the XOR checksum. */ 456fd43aa1cSDavid E. O'Brien sp1 = (u_short *)sl; 457fd43aa1cSDavid E. O'Brien sp2 = (u_short *)(sl + 1); 458fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum = 0; 459fd43aa1cSDavid E. O'Brien while (sp1 < sp2) 460fd43aa1cSDavid E. O'Brien cksum ^= *sp1++; 461fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum; 462fd43aa1cSDavid E. O'Brien #endif 463cc18e4ccSBruce Evans /* 464cc18e4ccSBruce Evans * write enable label sector before write (if necessary), 465cc18e4ccSBruce Evans * disable after writing. 466cc18e4ccSBruce Evans */ 467cc18e4ccSBruce Evans flag = 1; 4688fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, &flag) < 0) 4696bd343a9SPhilippe Charnier warn("ioctl DIOCWLABEL"); 470c8223965SMark Murray if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) { 4716bd343a9SPhilippe Charnier warn("write"); 4728fae3551SRodney W. Grimes return (1); 4738fae3551SRodney W. Grimes } 4748fae3551SRodney W. Grimes #if NUMBOOT > 0 4758fae3551SRodney W. Grimes /* 4768fae3551SRodney W. Grimes * Output the remainder of the disklabel 4778fae3551SRodney W. Grimes */ 4788fae3551SRodney W. Grimes if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 4796bd343a9SPhilippe Charnier warn("write"); 4808fae3551SRodney W. Grimes return(1); 4818fae3551SRodney W. Grimes } 4828fae3551SRodney W. Grimes #endif 4838fae3551SRodney W. Grimes flag = 0; 4848fae3551SRodney W. Grimes (void) ioctl(f, DIOCWLABEL, &flag); 4858fae3551SRodney W. Grimes } else if (ioctl(f, DIOCWDINFO, lp) < 0) { 4868fae3551SRodney W. Grimes l_perror("ioctl DIOCWDINFO"); 4878fae3551SRodney W. Grimes return (1); 4888fae3551SRodney W. Grimes } 4893233afaeSJohn W. De Boskey } 4908fae3551SRodney W. Grimes return (0); 4918fae3551SRodney W. Grimes } 4928fae3551SRodney W. Grimes 49361de51caSJoerg Wunsch void 494326c7cdaSWarner Losh l_perror(const char *s) 4958fae3551SRodney W. Grimes { 4966bd343a9SPhilippe Charnier switch (errno) { 4978fae3551SRodney W. Grimes 4988fae3551SRodney W. Grimes case ESRCH: 4993121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 5000b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 5018fae3551SRodney W. Grimes break; 5028fae3551SRodney W. Grimes 5038fae3551SRodney W. Grimes case EINVAL: 5043121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 5053121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 5068fae3551SRodney W. Grimes break; 5078fae3551SRodney W. Grimes 5088fae3551SRodney W. Grimes case EBUSY: 5096bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 5108fae3551SRodney W. Grimes break; 5118fae3551SRodney W. Grimes 5128fae3551SRodney W. Grimes case EXDEV: 5137de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 5147de06420SBruce Evans s, 'a' + RAW_PART); 5158fae3551SRodney W. Grimes break; 5168fae3551SRodney W. Grimes 5178fae3551SRodney W. Grimes default: 5186bd343a9SPhilippe Charnier warn((char *)NULL); 5198fae3551SRodney W. Grimes break; 5208fae3551SRodney W. Grimes } 5218fae3551SRodney W. Grimes } 5228fae3551SRodney W. Grimes 5238fae3551SRodney W. Grimes /* 5248fae3551SRodney W. Grimes * Fetch disklabel for disk. 5258fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 5268fae3551SRodney W. Grimes */ 5278fae3551SRodney W. Grimes struct disklabel * 528326c7cdaSWarner Losh readlabel(int f) 5298fae3551SRodney W. Grimes { 530326c7cdaSWarner Losh struct disklabel *lp; 5318fae3551SRodney W. Grimes 5328fae3551SRodney W. Grimes if (rflag) { 5338fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 534bef2080aSPhilippe Charnier err(4, "%s", specname); 5358fae3551SRodney W. Grimes for (lp = (struct disklabel *)bootarea; 5368fae3551SRodney W. Grimes lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 5378fae3551SRodney W. Grimes lp = (struct disklabel *)((char *)lp + 16)) 5388fae3551SRodney W. Grimes if (lp->d_magic == DISKMAGIC && 5398fae3551SRodney W. Grimes lp->d_magic2 == DISKMAGIC) 5408fae3551SRodney W. Grimes break; 5418fae3551SRodney W. Grimes if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 5428fae3551SRodney W. Grimes lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 5433121d4cbSPhilippe Charnier dkcksum(lp) != 0) 5443121d4cbSPhilippe Charnier errx(1, 5453121d4cbSPhilippe Charnier "bad pack magic number (label is damaged, or pack is unlabeled)"); 5468fae3551SRodney W. Grimes } else { 5478fae3551SRodney W. Grimes lp = &lab; 5488fae3551SRodney W. Grimes if (ioctl(f, DIOCGDINFO, lp) < 0) 549bef2080aSPhilippe Charnier err(4, "ioctl DIOCGDINFO"); 5508fae3551SRodney W. Grimes } 5518fae3551SRodney W. Grimes return (lp); 5528fae3551SRodney W. Grimes } 5538fae3551SRodney W. Grimes 5548fae3551SRodney W. Grimes /* 5558fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 5568fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 5578fae3551SRodney W. Grimes */ 5588fae3551SRodney W. Grimes struct disklabel * 559326c7cdaSWarner Losh makebootarea(char *boot, struct disklabel *dp, int f) 5608fae3551SRodney W. Grimes { 56121c729c2SBruce Evans struct disklabel *lp; 562326c7cdaSWarner Losh char *p; 5638fae3551SRodney W. Grimes int b; 5648fae3551SRodney W. Grimes #if NUMBOOT > 0 5658fae3551SRodney W. Grimes char *dkbasename; 5668fae3551SRodney W. Grimes struct stat sb; 567cc18e4ccSBruce Evans #endif 568cc18e4ccSBruce Evans #ifdef __alpha__ 56921c729c2SBruce Evans u_long *bootinfo; 570cc18e4ccSBruce Evans int n; 571cc18e4ccSBruce Evans #endif 572d70e4e53SJoerg Wunsch #ifdef __i386__ 573d70e4e53SJoerg Wunsch char *tmpbuf; 574d70e4e53SJoerg Wunsch int i, found; 57561de51caSJoerg Wunsch #endif 5768fae3551SRodney W. Grimes 5778fae3551SRodney W. Grimes /* XXX */ 5788fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 5798fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 5808fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 5818fae3551SRodney W. Grimes } 5828fae3551SRodney W. Grimes lp = (struct disklabel *) 5838fae3551SRodney W. Grimes (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 5848fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5858fae3551SRodney W. Grimes #if NUMBOOT > 0 5868fae3551SRodney W. Grimes /* 5878fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 5888fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 5898fae3551SRodney W. Grimes * clobber the existing boot. 5908fae3551SRodney W. Grimes */ 5918fae3551SRodney W. Grimes if (!installboot) { 5928fae3551SRodney W. Grimes if (rflag) { 5938fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 594bef2080aSPhilippe Charnier err(4, "%s", specname); 5958fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5968fae3551SRodney W. Grimes } 5978fae3551SRodney W. Grimes return (lp); 5988fae3551SRodney W. Grimes } 5998fae3551SRodney W. Grimes /* 6008fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 6018fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 6028fae3551SRodney W. Grimes */ 6038fae3551SRodney W. Grimes if (!xxboot || !bootxx) { 6048fae3551SRodney W. Grimes dkbasename = np; 6058fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 6068fae3551SRodney W. Grimes p = dkname; 6078fae3551SRodney W. Grimes else 6088fae3551SRodney W. Grimes p++; 6098fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 6108fae3551SRodney W. Grimes *np++ = *p++; 6118fae3551SRodney W. Grimes *np++ = '\0'; 6128fae3551SRodney W. Grimes 6138fae3551SRodney W. Grimes if (!xxboot) { 614efba76d7SJordan K. Hubbard (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); 615efba76d7SJordan K. Hubbard xxboot = boot0; 6168fae3551SRodney W. Grimes } 6178fae3551SRodney W. Grimes #if NUMBOOT > 1 6188fae3551SRodney W. Grimes if (!bootxx) { 619efba76d7SJordan K. Hubbard (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); 620efba76d7SJordan K. Hubbard bootxx = boot1; 6218fae3551SRodney W. Grimes } 6228fae3551SRodney W. Grimes #endif 6238fae3551SRodney W. Grimes } 6248fae3551SRodney W. Grimes 6258fae3551SRodney W. Grimes /* 6268fae3551SRodney W. Grimes * Strange rules: 627fd43aa1cSDavid E. O'Brien * 1. One-piece bootstrap (hp300/hp800) 62890b8f6baSDavid E. O'Brien * 1. One-piece bootstrap (alpha/sparc64) 6298fae3551SRodney W. Grimes * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 6308fae3551SRodney W. Grimes * is remembered and written later following the bootarea. 631fd43aa1cSDavid E. O'Brien * 2. Two-piece bootstraps (i386/ia64) 6328fae3551SRodney W. Grimes * up to d_secsize bytes of ``xxboot'' go in first d_secsize 6338fae3551SRodney W. Grimes * bytes of bootarea, remaining d_bbsize-d_secsize filled 6348fae3551SRodney W. Grimes * from ``bootxx''. 6358fae3551SRodney W. Grimes */ 6368fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 6378fae3551SRodney W. Grimes if (b < 0) 638bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6398fae3551SRodney W. Grimes #if NUMBOOT > 1 640d70e4e53SJoerg Wunsch #ifdef __i386__ 641d70e4e53SJoerg Wunsch /* 642d70e4e53SJoerg Wunsch * XXX Botch alert. 643d70e4e53SJoerg Wunsch * The i386 has the so-called fdisk table embedded into the 644d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 645d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 646d70e4e53SJoerg Wunsch * the xxboot provides a template.) 647d70e4e53SJoerg Wunsch */ 648d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 649bef2080aSPhilippe Charnier err(4, "%s", xxboot); 650d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 651448f7629SAlfred Perlstein #endif /* __i386__ */ 6528fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_secsize) < 0) 653bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6548fae3551SRodney W. Grimes (void)close(b); 655d70e4e53SJoerg Wunsch #ifdef __i386__ 656d70e4e53SJoerg Wunsch for (i = DOSPARTOFF, found = 0; 657c8223965SMark Murray !found && i < (int)(DOSPARTOFF + NDOSPART*sizeof(struct dos_partition)); 658d70e4e53SJoerg Wunsch i++) 659d70e4e53SJoerg Wunsch found = tmpbuf[i] != 0; 660d70e4e53SJoerg Wunsch if (found) 661d70e4e53SJoerg Wunsch memcpy((void *)&boot[DOSPARTOFF], 662d70e4e53SJoerg Wunsch (void *)&tmpbuf[DOSPARTOFF], 663d70e4e53SJoerg Wunsch NDOSPART * sizeof(struct dos_partition)); 664d70e4e53SJoerg Wunsch free(tmpbuf); 665448f7629SAlfred Perlstein #endif /* __i386__ */ 6668fae3551SRodney W. Grimes b = open(bootxx, O_RDONLY); 6678fae3551SRodney W. Grimes if (b < 0) 668bef2080aSPhilippe Charnier err(4, "%s", bootxx); 6696cabb348SBruce Evans if (fstat(b, &sb) != 0) 6706cabb348SBruce Evans err(4, "%s", bootxx); 6716cabb348SBruce Evans if (dp->d_secsize + sb.st_size > dp->d_bbsize) 6726cabb348SBruce Evans errx(4, "%s too large", bootxx); 67361de51caSJoerg Wunsch if (read(b, &boot[dp->d_secsize], 67461de51caSJoerg Wunsch (int)(dp->d_bbsize-dp->d_secsize)) < 0) 675bef2080aSPhilippe Charnier err(4, "%s", bootxx); 676cc18e4ccSBruce Evans #else /* !(NUMBOOT > 1) */ 677130cd73aSDoug Rabson #ifdef __alpha__ 678130cd73aSDoug Rabson /* 679130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 680130cd73aSDoug Rabson * second sector of the boot area. The first sector 681130cd73aSDoug Rabson * contains the label and must be edited to contain the 682130cd73aSDoug Rabson * size and location of the primary bootstrap. 683130cd73aSDoug Rabson */ 684cc18e4ccSBruce Evans n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 685130cd73aSDoug Rabson if (n < 0) 686130cd73aSDoug Rabson err(4, "%s", xxboot); 68721c729c2SBruce Evans bootinfo = (u_long *)(boot + 480); 68821c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 68921c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 69021c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 691cc18e4ccSBruce Evans #else /* !__alpha__ */ 6928fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_bbsize) < 0) 693bef2080aSPhilippe Charnier err(4, "%s", xxboot); 694cc18e4ccSBruce Evans #endif /* __alpha__ */ 6956cabb348SBruce Evans if (fstat(b, &sb) != 0) 6966cabb348SBruce Evans err(4, "%s", xxboot); 6978fae3551SRodney W. Grimes bootsize = (int)sb.st_size - dp->d_bbsize; 6988fae3551SRodney W. Grimes if (bootsize > 0) { 6998fae3551SRodney W. Grimes /* XXX assume d_secsize is a power of two */ 7008fae3551SRodney W. Grimes bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 7018fae3551SRodney W. Grimes bootbuf = (char *)malloc((size_t)bootsize); 7028fae3551SRodney W. Grimes if (bootbuf == 0) 703bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7048fae3551SRodney W. Grimes if (read(b, bootbuf, bootsize) < 0) { 7058fae3551SRodney W. Grimes free(bootbuf); 706bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7078fae3551SRodney W. Grimes } 7088fae3551SRodney W. Grimes } 709cc18e4ccSBruce Evans #endif /* NUMBOOT > 1 */ 7108fae3551SRodney W. Grimes (void)close(b); 711cc18e4ccSBruce Evans #endif /* NUMBOOT > 0 */ 7128fae3551SRodney W. Grimes /* 7138fae3551SRodney W. Grimes * Make sure no part of the bootstrap is written in the area 7148fae3551SRodney W. Grimes * reserved for the label. 7158fae3551SRodney W. Grimes */ 7168fae3551SRodney W. Grimes for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 7176bd343a9SPhilippe Charnier if (*p) 7186bd343a9SPhilippe Charnier errx(2, "bootstrap doesn't leave room for disk label"); 7198fae3551SRodney W. Grimes return (lp); 7208fae3551SRodney W. Grimes } 7218fae3551SRodney W. Grimes 72261de51caSJoerg Wunsch void 723326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 7248fae3551SRodney W. Grimes { 725326c7cdaSWarner Losh int i, j; 726326c7cdaSWarner Losh const struct partition *pp; 7278fae3551SRodney W. Grimes 7288fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 7298fae3551SRodney W. Grimes if ((unsigned) lp->d_type < DKMAXTYPES) 7308fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 7318fae3551SRodney W. Grimes else 7322a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 73361de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 73461de51caSJoerg Wunsch lp->d_typename); 73561de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 73661de51caSJoerg Wunsch lp->d_packname); 7378fae3551SRodney W. Grimes fprintf(f, "flags:"); 7388fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 7398fae3551SRodney W. Grimes fprintf(f, " removeable"); 7408fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 7418fae3551SRodney W. Grimes fprintf(f, " ecc"); 7428fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 7438fae3551SRodney W. Grimes fprintf(f, " badsect"); 7448fae3551SRodney W. Grimes fprintf(f, "\n"); 7452a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 7462a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 7472a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 7482a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 7492a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 7502a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 7512a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 7522a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 7532a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 7542a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 7552a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 7562a1deaaaSBruce Evans (u_long)lp->d_headswitch); 75761de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 7582a1deaaaSBruce Evans (u_long)lp->d_trkseek); 7598fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 7608fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 7618fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 7628fae3551SRodney W. Grimes break; 7638fae3551SRodney W. Grimes if (i < 0) 7648fae3551SRodney W. Grimes i = 0; 7658fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 7662a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 7672a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 7688fae3551SRodney W. Grimes fprintf(f, 769ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 7708fae3551SRodney W. Grimes pp = lp->d_partitions; 7718fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 7728fae3551SRodney W. Grimes if (pp->p_size) { 7732a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 7742a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 7758fae3551SRodney W. Grimes if ((unsigned) pp->p_fstype < FSMAXTYPES) 7768fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 7778fae3551SRodney W. Grimes else 7788fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 7798fae3551SRodney W. Grimes switch (pp->p_fstype) { 7808fae3551SRodney W. Grimes 7818fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 7822a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 7832a1deaaaSBruce Evans (u_long)pp->p_fsize, 7842a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 7858fae3551SRodney W. Grimes break; 7868fae3551SRodney W. Grimes 7878fae3551SRodney W. Grimes case FS_BSDFFS: 7882a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 7892a1deaaaSBruce Evans (u_long)pp->p_fsize, 7902a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 7918fae3551SRodney W. Grimes pp->p_cpg); 7928fae3551SRodney W. Grimes break; 7938fae3551SRodney W. Grimes 794ca4693edSJustin T. Gibbs case FS_BSDLFS: 7952a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 7962a1deaaaSBruce Evans (u_long)pp->p_fsize, 7972a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 798ca4693edSJustin T. Gibbs pp->p_cpg); 799ca4693edSJustin T. Gibbs break; 800ca4693edSJustin T. Gibbs 8018fae3551SRodney W. Grimes default: 8028fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 8038fae3551SRodney W. Grimes break; 8048fae3551SRodney W. Grimes } 8052a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 8062a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 8078fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 8088fae3551SRodney W. Grimes putc('*', f); 8098fae3551SRodney W. Grimes else 8108fae3551SRodney W. Grimes putc(' ', f); 8112a1deaaaSBruce Evans fprintf(f, "- %lu", 8122a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 8132a1deaaaSBruce Evans lp->d_secpercyl - 1) / 8142a1deaaaSBruce Evans lp->d_secpercyl - 1)); 8158fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 8168fae3551SRodney W. Grimes putc('*', f); 8178fae3551SRodney W. Grimes fprintf(f, ")\n"); 8188fae3551SRodney W. Grimes } 8198fae3551SRodney W. Grimes } 8208fae3551SRodney W. Grimes fflush(f); 8218fae3551SRodney W. Grimes } 8228fae3551SRodney W. Grimes 82361de51caSJoerg Wunsch int 824326c7cdaSWarner Losh edit(struct disklabel *lp, int f) 8258fae3551SRodney W. Grimes { 826326c7cdaSWarner Losh int c, fd; 8278fae3551SRodney W. Grimes struct disklabel label; 828722ceb3fSWarner Losh FILE *fp; 8298fae3551SRodney W. Grimes 830722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 831722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 8326bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 8338fae3551SRodney W. Grimes return (1); 8348fae3551SRodney W. Grimes } 835722ceb3fSWarner Losh display(fp, lp); 836722ceb3fSWarner Losh fclose(fp); 8378fae3551SRodney W. Grimes for (;;) { 8388fae3551SRodney W. Grimes if (!editit()) 8398fae3551SRodney W. Grimes break; 840722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 841722ceb3fSWarner Losh if (fp == NULL) { 8426bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 8438fae3551SRodney W. Grimes break; 8448fae3551SRodney W. Grimes } 8458fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 846722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 8478fae3551SRodney W. Grimes *lp = label; 8488fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 849722ceb3fSWarner Losh fclose(fp); 8508fae3551SRodney W. Grimes (void) unlink(tmpfil); 8518fae3551SRodney W. Grimes return (0); 8528fae3551SRodney W. Grimes } 8538fae3551SRodney W. Grimes } 854722ceb3fSWarner Losh fclose(fp); 8558fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 8568fae3551SRodney W. Grimes c = getchar(); 8578fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 8588fae3551SRodney W. Grimes while (getchar() != (int)'\n') 8598fae3551SRodney W. Grimes ; 8608fae3551SRodney W. Grimes if (c == (int)'n') 8618fae3551SRodney W. Grimes break; 8628fae3551SRodney W. Grimes } 8638fae3551SRodney W. Grimes (void) unlink(tmpfil); 8648fae3551SRodney W. Grimes return (1); 8658fae3551SRodney W. Grimes } 8668fae3551SRodney W. Grimes 86761de51caSJoerg Wunsch int 868326c7cdaSWarner Losh editit(void) 8698fae3551SRodney W. Grimes { 870326c7cdaSWarner Losh int pid, xpid; 871c8223965SMark Murray int locstat, omask; 872c8223965SMark Murray const char *ed; 8738fae3551SRodney W. Grimes 8748fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 8758fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 8768fae3551SRodney W. Grimes if (errno == EPROCLIM) { 8776bd343a9SPhilippe Charnier warnx("you have too many processes"); 8788fae3551SRodney W. Grimes return(0); 8798fae3551SRodney W. Grimes } 8808fae3551SRodney W. Grimes if (errno != EAGAIN) { 8816bd343a9SPhilippe Charnier warn("fork"); 8828fae3551SRodney W. Grimes return(0); 8838fae3551SRodney W. Grimes } 8848fae3551SRodney W. Grimes sleep(1); 8858fae3551SRodney W. Grimes } 8868fae3551SRodney W. Grimes if (pid == 0) { 8878fae3551SRodney W. Grimes sigsetmask(omask); 8888fae3551SRodney W. Grimes setgid(getgid()); 8898fae3551SRodney W. Grimes setuid(getuid()); 8908fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 8918fae3551SRodney W. Grimes ed = DEFEDITOR; 8927bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 8936bd343a9SPhilippe Charnier err(1, "%s", ed); 8948fae3551SRodney W. Grimes } 895c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 8968fae3551SRodney W. Grimes if (xpid == pid) 8978fae3551SRodney W. Grimes break; 8988fae3551SRodney W. Grimes sigsetmask(omask); 899c8223965SMark Murray return(!locstat); 9008fae3551SRodney W. Grimes } 9018fae3551SRodney W. Grimes 9028fae3551SRodney W. Grimes char * 903326c7cdaSWarner Losh skip(char *cp) 9048fae3551SRodney W. Grimes { 9058fae3551SRodney W. Grimes 9068fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 9078fae3551SRodney W. Grimes cp++; 9088fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 909326c7cdaSWarner Losh return (NULL); 9108fae3551SRodney W. Grimes return (cp); 9118fae3551SRodney W. Grimes } 9128fae3551SRodney W. Grimes 9138fae3551SRodney W. Grimes char * 914326c7cdaSWarner Losh word(char *cp) 9158fae3551SRodney W. Grimes { 916326c7cdaSWarner Losh char c; 9178fae3551SRodney W. Grimes 9188fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 9198fae3551SRodney W. Grimes cp++; 9208fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 9218fae3551SRodney W. Grimes *cp++ = '\0'; 9228fae3551SRodney W. Grimes if (c != '#') 9238fae3551SRodney W. Grimes return (skip(cp)); 9248fae3551SRodney W. Grimes } 925326c7cdaSWarner Losh return (NULL); 9268fae3551SRodney W. Grimes } 9278fae3551SRodney W. Grimes 9288fae3551SRodney W. Grimes /* 9298fae3551SRodney W. Grimes * Read an ascii label in from fd f, 9308fae3551SRodney W. Grimes * in the same format as that put out by display(), 9318fae3551SRodney W. Grimes * and fill in lp. 9328fae3551SRodney W. Grimes */ 93361de51caSJoerg Wunsch int 934326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 9358fae3551SRodney W. Grimes { 9366b0ff5f5SPoul-Henning Kamp char *cp; 9376b0ff5f5SPoul-Henning Kamp const char **cpp; 9383233afaeSJohn W. De Boskey unsigned int part; 9396b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 9408fae3551SRodney W. Grimes int v, lineno = 0, errors = 0; 941326c7cdaSWarner Losh int i; 9428fae3551SRodney W. Grimes 9438fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 94477068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 9458fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 9468fae3551SRodney W. Grimes lineno++; 94761de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 9488fae3551SRodney W. Grimes *cp = '\0'; 9498fae3551SRodney W. Grimes cp = skip(line); 9508fae3551SRodney W. Grimes if (cp == NULL) 9518fae3551SRodney W. Grimes continue; 9528fae3551SRodney W. Grimes tp = index(cp, ':'); 9538fae3551SRodney W. Grimes if (tp == NULL) { 9548fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 9558fae3551SRodney W. Grimes errors++; 9568fae3551SRodney W. Grimes continue; 9578fae3551SRodney W. Grimes } 9588fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 9598fae3551SRodney W. Grimes if (streq(cp, "type")) { 9608fae3551SRodney W. Grimes if (tp == NULL) 961c8223965SMark Murray tp = unknown; 9628fae3551SRodney W. Grimes cpp = dktypenames; 9638fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 9646b0ff5f5SPoul-Henning Kamp if (*cpp && streq(*cpp, tp)) { 9658fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 96609dbd070SIan Dowse break; 9678fae3551SRodney W. Grimes } 96809dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 96909dbd070SIan Dowse continue; 9708fae3551SRodney W. Grimes v = atoi(tp); 9718fae3551SRodney W. Grimes if ((unsigned)v >= DKMAXTYPES) 9728fae3551SRodney W. Grimes fprintf(stderr, "line %d:%s %d\n", lineno, 9738fae3551SRodney W. Grimes "Warning, unknown disk type", v); 9748fae3551SRodney W. Grimes lp->d_type = v; 9758fae3551SRodney W. Grimes continue; 9768fae3551SRodney W. Grimes } 9778fae3551SRodney W. Grimes if (streq(cp, "flags")) { 9788fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 9798fae3551SRodney W. Grimes tp = word(cp); 9808fae3551SRodney W. Grimes if (streq(cp, "removeable")) 9818fae3551SRodney W. Grimes v |= D_REMOVABLE; 9828fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 9838fae3551SRodney W. Grimes v |= D_ECC; 9848fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 9858fae3551SRodney W. Grimes v |= D_BADSECT; 9868fae3551SRodney W. Grimes else { 9878fae3551SRodney W. Grimes fprintf(stderr, 9888fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 9898fae3551SRodney W. Grimes lineno, cp); 9908fae3551SRodney W. Grimes errors++; 9918fae3551SRodney W. Grimes } 9928fae3551SRodney W. Grimes } 9938fae3551SRodney W. Grimes lp->d_flags = v; 9948fae3551SRodney W. Grimes continue; 9958fae3551SRodney W. Grimes } 9968fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 9978fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 9988fae3551SRodney W. Grimes lp->d_drivedata[i++] = atoi(cp); 9998fae3551SRodney W. Grimes tp = word(cp); 10008fae3551SRodney W. Grimes } 10018fae3551SRodney W. Grimes continue; 10028fae3551SRodney W. Grimes } 10038fae3551SRodney W. Grimes if (sscanf(cp, "%d partitions", &v) == 1) { 10048fae3551SRodney W. Grimes if (v == 0 || (unsigned)v > MAXPARTITIONS) { 10058fae3551SRodney W. Grimes fprintf(stderr, 10068fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 10078fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 10088fae3551SRodney W. Grimes errors++; 10098fae3551SRodney W. Grimes } else 10108fae3551SRodney W. Grimes lp->d_npartitions = v; 10118fae3551SRodney W. Grimes continue; 10128fae3551SRodney W. Grimes } 10138fae3551SRodney W. Grimes if (tp == NULL) 1014c8223965SMark Murray tp = blank; 10158fae3551SRodney W. Grimes if (streq(cp, "disk")) { 10168fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 10178fae3551SRodney W. Grimes continue; 10188fae3551SRodney W. Grimes } 10198fae3551SRodney W. Grimes if (streq(cp, "label")) { 10208fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 10218fae3551SRodney W. Grimes continue; 10228fae3551SRodney W. Grimes } 10238fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 10248fae3551SRodney W. Grimes v = atoi(tp); 10256cabb348SBruce Evans if (v <= 0 || (v % DEV_BSIZE) != 0) { 10268fae3551SRodney W. Grimes fprintf(stderr, 10278fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 10288fae3551SRodney W. Grimes lineno, tp); 10298fae3551SRodney W. Grimes errors++; 10308fae3551SRodney W. Grimes } else 10318fae3551SRodney W. Grimes lp->d_secsize = v; 10328fae3551SRodney W. Grimes continue; 10338fae3551SRodney W. Grimes } 10348fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 10358fae3551SRodney W. Grimes v = atoi(tp); 10368fae3551SRodney W. Grimes if (v <= 0) { 10378fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10388fae3551SRodney W. Grimes lineno, tp, cp); 10398fae3551SRodney W. Grimes errors++; 10408fae3551SRodney W. Grimes } else 10418fae3551SRodney W. Grimes lp->d_nsectors = v; 10428fae3551SRodney W. Grimes continue; 10438fae3551SRodney W. Grimes } 10448fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 10458fae3551SRodney W. Grimes v = atoi(tp); 10468fae3551SRodney W. Grimes if (v <= 0) { 10478fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10488fae3551SRodney W. Grimes lineno, tp, cp); 10498fae3551SRodney W. Grimes errors++; 10508fae3551SRodney W. Grimes } else 10518fae3551SRodney W. Grimes lp->d_secpercyl = v; 10528fae3551SRodney W. Grimes continue; 10538fae3551SRodney W. Grimes } 10548fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 10558fae3551SRodney W. Grimes v = atoi(tp); 10568fae3551SRodney W. Grimes if (v <= 0) { 10578fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10588fae3551SRodney W. Grimes lineno, tp, cp); 10598fae3551SRodney W. Grimes errors++; 10608fae3551SRodney W. Grimes } else 10618fae3551SRodney W. Grimes lp->d_ntracks = v; 10628fae3551SRodney W. Grimes continue; 10638fae3551SRodney W. Grimes } 10648fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 10658fae3551SRodney W. Grimes v = atoi(tp); 10668fae3551SRodney W. Grimes if (v <= 0) { 10678fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10688fae3551SRodney W. Grimes lineno, tp, cp); 10698fae3551SRodney W. Grimes errors++; 10708fae3551SRodney W. Grimes } else 10718fae3551SRodney W. Grimes lp->d_ncylinders = v; 10728fae3551SRodney W. Grimes continue; 10738fae3551SRodney W. Grimes } 1074f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 1075f75dd518SBruce Evans v = atoi(tp); 1076f75dd518SBruce Evans if (v <= 0) { 1077f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 1078f75dd518SBruce Evans lineno, tp, cp); 1079f75dd518SBruce Evans errors++; 1080f75dd518SBruce Evans } else 1081f75dd518SBruce Evans lp->d_secperunit = v; 1082f75dd518SBruce Evans continue; 1083f75dd518SBruce Evans } 10848fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 10858fae3551SRodney W. Grimes v = atoi(tp); 10868fae3551SRodney W. Grimes if (v <= 0) { 10878fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10888fae3551SRodney W. Grimes lineno, tp, cp); 10898fae3551SRodney W. Grimes errors++; 10908fae3551SRodney W. Grimes } else 10918fae3551SRodney W. Grimes lp->d_rpm = v; 10928fae3551SRodney W. Grimes continue; 10938fae3551SRodney W. Grimes } 10948fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 10958fae3551SRodney W. Grimes v = atoi(tp); 10968fae3551SRodney W. Grimes if (v <= 0) { 10978fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10988fae3551SRodney W. Grimes lineno, tp, cp); 10998fae3551SRodney W. Grimes errors++; 11008fae3551SRodney W. Grimes } else 11018fae3551SRodney W. Grimes lp->d_interleave = v; 11028fae3551SRodney W. Grimes continue; 11038fae3551SRodney W. Grimes } 11048fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 11058fae3551SRodney W. Grimes v = atoi(tp); 11068fae3551SRodney W. Grimes if (v < 0) { 11078fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11088fae3551SRodney W. Grimes lineno, tp, cp); 11098fae3551SRodney W. Grimes errors++; 11108fae3551SRodney W. Grimes } else 11118fae3551SRodney W. Grimes lp->d_trackskew = v; 11128fae3551SRodney W. Grimes continue; 11138fae3551SRodney W. Grimes } 11148fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 11158fae3551SRodney W. Grimes v = atoi(tp); 11168fae3551SRodney W. Grimes if (v < 0) { 11178fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11188fae3551SRodney W. Grimes lineno, tp, cp); 11198fae3551SRodney W. Grimes errors++; 11208fae3551SRodney W. Grimes } else 11218fae3551SRodney W. Grimes lp->d_cylskew = v; 11228fae3551SRodney W. Grimes continue; 11238fae3551SRodney W. Grimes } 11248fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 11258fae3551SRodney W. Grimes v = atoi(tp); 11268fae3551SRodney W. Grimes if (v < 0) { 11278fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11288fae3551SRodney W. Grimes lineno, tp, cp); 11298fae3551SRodney W. Grimes errors++; 11308fae3551SRodney W. Grimes } else 11318fae3551SRodney W. Grimes lp->d_headswitch = v; 11328fae3551SRodney W. Grimes continue; 11338fae3551SRodney W. Grimes } 11348fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 11358fae3551SRodney W. Grimes v = atoi(tp); 11368fae3551SRodney W. Grimes if (v < 0) { 11378fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11388fae3551SRodney W. Grimes lineno, tp, cp); 11398fae3551SRodney W. Grimes errors++; 11408fae3551SRodney W. Grimes } else 11418fae3551SRodney W. Grimes lp->d_trkseek = v; 11428fae3551SRodney W. Grimes continue; 11438fae3551SRodney W. Grimes } 11443233afaeSJohn W. De Boskey /* the ':' was removed above */ 114567b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 114667b46708SIan Dowse fprintf(stderr, 114767b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 114867b46708SIan Dowse cp); 114967b46708SIan Dowse errors++; 115067b46708SIan Dowse continue; 115167b46708SIan Dowse } 115267b46708SIan Dowse 115367b46708SIan Dowse /* Process a partition specification line. */ 11543233afaeSJohn W. De Boskey part = *cp - 'a'; 11553233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 11568fae3551SRodney W. Grimes fprintf(stderr, 11573233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 11583233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 11598fae3551SRodney W. Grimes errors++; 11608fae3551SRodney W. Grimes continue; 11618fae3551SRodney W. Grimes } 11623233afaeSJohn W. De Boskey part_set[part] = 1; 116367b46708SIan Dowse 116467b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 116567b46708SIan Dowse errors++; 116667b46708SIan Dowse break; 116767b46708SIan Dowse } 116867b46708SIan Dowse } 116967b46708SIan Dowse errors += checklabel(lp); 117067b46708SIan Dowse return (errors == 0); 117167b46708SIan Dowse } 117267b46708SIan Dowse 117367b46708SIan Dowse #define NXTNUM(n) do { \ 117413e0abcbSPaul Traina if (tp == NULL) { \ 117513e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 117667b46708SIan Dowse return (1); \ 117713e0abcbSPaul Traina } else { \ 11788fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 11798fae3551SRodney W. Grimes (n) = atoi(cp); \ 118013e0abcbSPaul Traina } \ 118167b46708SIan Dowse } while (0) 118267b46708SIan Dowse 11833233afaeSJohn W. De Boskey /* retain 1 character following number */ 118467b46708SIan Dowse #define NXTWORD(w,n) do { \ 11853233afaeSJohn W. De Boskey if (tp == NULL) { \ 11863233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 118767b46708SIan Dowse return (1); \ 11883233afaeSJohn W. De Boskey } else { \ 11893233afaeSJohn W. De Boskey char *tmp; \ 11903233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 11913233afaeSJohn W. De Boskey (n) = strtol(cp,&tmp,10); \ 11923233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 11933233afaeSJohn W. De Boskey } \ 119467b46708SIan Dowse } while (0) 119567b46708SIan Dowse 119667b46708SIan Dowse /* 119767b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 119867b46708SIan Dowse * Return 0 on success, 1 on failure. 119967b46708SIan Dowse */ 120067b46708SIan Dowse int 120167b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 120267b46708SIan Dowse { 120367b46708SIan Dowse struct partition *pp; 120467b46708SIan Dowse char *cp; 120567b46708SIan Dowse const char **cpp; 120667b46708SIan Dowse int v; 120767b46708SIan Dowse 120867b46708SIan Dowse pp = &lp->d_partitions[part]; 120967b46708SIan Dowse cp = NULL; 121067b46708SIan Dowse 12113233afaeSJohn W. De Boskey v = 0; 12123233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 12133233afaeSJohn W. De Boskey if (v < 0 || (v == 0 && part_size_type[part] != '*')) { 121467b46708SIan Dowse fprintf(stderr, "line %d: %s: bad partition size\n", lineno, 121567b46708SIan Dowse cp); 121667b46708SIan Dowse return (1); 121767b46708SIan Dowse } 12188fae3551SRodney W. Grimes pp->p_size = v; 12193233afaeSJohn W. De Boskey 12203233afaeSJohn W. De Boskey v = 0; 12213233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 122267b46708SIan Dowse if (v < 0 || (v == 0 && part_offset_type[part] != '*' && 12233233afaeSJohn W. De Boskey part_offset_type[part] != '\0')) { 122467b46708SIan Dowse fprintf(stderr, "line %d: %s: bad partition offset\n", lineno, 122567b46708SIan Dowse cp); 122667b46708SIan Dowse return (1); 122767b46708SIan Dowse } 12288fae3551SRodney W. Grimes pp->p_offset = v; 12298fae3551SRodney W. Grimes cp = tp, tp = word(cp); 123067b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 123167b46708SIan Dowse if (*cpp && streq(*cpp, cp)) 123267b46708SIan Dowse break; 123367b46708SIan Dowse if (*cpp != NULL) { 123467b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 123567b46708SIan Dowse } else { 12368fae3551SRodney W. Grimes if (isdigit(*cp)) 12378fae3551SRodney W. Grimes v = atoi(cp); 12388fae3551SRodney W. Grimes else 12398fae3551SRodney W. Grimes v = FSMAXTYPES; 12408fae3551SRodney W. Grimes if ((unsigned)v >= FSMAXTYPES) { 12413233afaeSJohn W. De Boskey fprintf(stderr, 124267b46708SIan Dowse "line %d: Warning, unknown filesystem type %s\n", 12433233afaeSJohn W. De Boskey lineno, cp); 12448fae3551SRodney W. Grimes v = FS_UNUSED; 12458fae3551SRodney W. Grimes } 12468fae3551SRodney W. Grimes pp->p_fstype = v; 124767b46708SIan Dowse } 124867b46708SIan Dowse 12498fae3551SRodney W. Grimes switch (pp->p_fstype) { 12503233afaeSJohn W. De Boskey case FS_UNUSED: 12513233afaeSJohn W. De Boskey /* 12523233afaeSJohn W. De Boskey * allow us to accept defaults for 12533233afaeSJohn W. De Boskey * fsize/frag/cpg 12543233afaeSJohn W. De Boskey */ 12553233afaeSJohn W. De Boskey if (tp) { 12568fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 12578fae3551SRodney W. Grimes if (pp->p_fsize == 0) 12588fae3551SRodney W. Grimes break; 12598fae3551SRodney W. Grimes NXTNUM(v); 12608fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 12613233afaeSJohn W. De Boskey } 12623233afaeSJohn W. De Boskey /* else default to 0's */ 12638fae3551SRodney W. Grimes break; 12648fae3551SRodney W. Grimes 12653233afaeSJohn W. De Boskey /* These happen to be the same */ 12668fae3551SRodney W. Grimes case FS_BSDFFS: 1267ca4693edSJustin T. Gibbs case FS_BSDLFS: 12683233afaeSJohn W. De Boskey if (tp) { 1269ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1270ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1271ca4693edSJustin T. Gibbs break; 1272ca4693edSJustin T. Gibbs NXTNUM(v); 1273ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1274ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 12753233afaeSJohn W. De Boskey } else { 12763233afaeSJohn W. De Boskey /* 127767b46708SIan Dowse * FIX! poor attempt at adaptive 12783233afaeSJohn W. De Boskey */ 12793233afaeSJohn W. De Boskey /* 1 GB */ 128067b46708SIan Dowse if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 128167b46708SIan Dowse /* 128267b46708SIan Dowse * FIX! These are too low, but are traditional 128367b46708SIan Dowse */ 1284f1a7b7eeSIan Dowse pp->p_fsize = DEFAULT_NEWFS_FRAG; 1285f1a7b7eeSIan Dowse pp->p_frag = DEFAULT_NEWFS_BLOCK / 1286f1a7b7eeSIan Dowse DEFAULT_NEWFS_FRAG; 12873233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 12883233afaeSJohn W. De Boskey } else { 1289f1a7b7eeSIan Dowse pp->p_fsize = BIG_NEWFS_FRAG; 1290f1a7b7eeSIan Dowse pp->p_frag = BIG_NEWFS_BLOCK / 1291f1a7b7eeSIan Dowse BIG_NEWFS_FRAG; 12923233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 12933233afaeSJohn W. De Boskey } 12943233afaeSJohn W. De Boskey } 12958fae3551SRodney W. Grimes default: 12968fae3551SRodney W. Grimes break; 12978fae3551SRodney W. Grimes } 129867b46708SIan Dowse return (0); 12998fae3551SRodney W. Grimes } 13008fae3551SRodney W. Grimes 13018fae3551SRodney W. Grimes /* 13028fae3551SRodney W. Grimes * Check disklabel for errors and fill in 13038fae3551SRodney W. Grimes * derived fields according to supplied values. 13048fae3551SRodney W. Grimes */ 130561de51caSJoerg Wunsch int 1306326c7cdaSWarner Losh checklabel(struct disklabel *lp) 13078fae3551SRodney W. Grimes { 1308326c7cdaSWarner Losh struct partition *pp; 13098fae3551SRodney W. Grimes int i, errors = 0; 13108fae3551SRodney W. Grimes char part; 13113233afaeSJohn W. De Boskey unsigned long total_size, total_percent, current_offset; 13123233afaeSJohn W. De Boskey int seen_default_offset; 13133233afaeSJohn W. De Boskey int hog_part; 13143233afaeSJohn W. De Boskey int j; 13153233afaeSJohn W. De Boskey struct partition *pp2; 13168fae3551SRodney W. Grimes 13178fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 13182a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 13198fae3551SRodney W. Grimes return (1); 13208fae3551SRodney W. Grimes } 13218fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 13222a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 13238fae3551SRodney W. Grimes return (1); 13248fae3551SRodney W. Grimes } 13258fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 13262a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 13278fae3551SRodney W. Grimes return (1); 13288fae3551SRodney W. Grimes } 13298fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 13302a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 13318fae3551SRodney W. Grimes errors++; 13328fae3551SRodney W. Grimes } 13338fae3551SRodney W. Grimes if (lp->d_rpm == 0) 13342a1deaaaSBruce Evans Warning("revolutions/minute 0"); 13358fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 13368fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 13378fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 13388fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 13398fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 13402a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 13418fae3551SRodney W. Grimes errors++; 13428fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 13438fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 13448fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 13452a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 13462a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 13473233afaeSJohn W. De Boskey 13483233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 13493233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 13503233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 13513233afaeSJohn W. De Boskey hog_part = -1; 13523233afaeSJohn W. De Boskey /* find all fixed partitions */ 13533233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13543233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13553233afaeSJohn W. De Boskey if (part_set[i]) { 13563233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 13576b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 13583233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 13593233afaeSJohn W. De Boskey } else { 13603233afaeSJohn W. De Boskey if (hog_part != -1) 13613233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 13623233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 13633233afaeSJohn W. De Boskey else 13643233afaeSJohn W. De Boskey hog_part = i; 13653233afaeSJohn W. De Boskey } 13663233afaeSJohn W. De Boskey } else { 13678d3105e8SWarner Losh off_t size; 13683233afaeSJohn W. De Boskey 13693233afaeSJohn W. De Boskey size = pp->p_size; 13703233afaeSJohn W. De Boskey switch (part_size_type[i]) { 13713233afaeSJohn W. De Boskey case '%': 13723233afaeSJohn W. De Boskey total_percent += size; 13733233afaeSJohn W. De Boskey break; 13743233afaeSJohn W. De Boskey case 'k': 13753233afaeSJohn W. De Boskey case 'K': 13768d3105e8SWarner Losh size *= 1024ULL; 13773233afaeSJohn W. De Boskey break; 13783233afaeSJohn W. De Boskey case 'm': 13793233afaeSJohn W. De Boskey case 'M': 13808d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 13813233afaeSJohn W. De Boskey break; 13823233afaeSJohn W. De Boskey case 'g': 13833233afaeSJohn W. De Boskey case 'G': 13848d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 13853233afaeSJohn W. De Boskey break; 13863233afaeSJohn W. De Boskey case '\0': 13873233afaeSJohn W. De Boskey break; 13883233afaeSJohn W. De Boskey default: 13893233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 13903233afaeSJohn W. De Boskey break; 13913233afaeSJohn W. De Boskey } 13923233afaeSJohn W. De Boskey /* don't count %'s yet */ 13933233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 13943233afaeSJohn W. De Boskey /* 13953233afaeSJohn W. De Boskey * for all not in sectors, convert to 13963233afaeSJohn W. De Boskey * sectors 13973233afaeSJohn W. De Boskey */ 13983233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 13993233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 14003233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 14013233afaeSJohn W. De Boskey i + 'a'); 14023233afaeSJohn W. De Boskey size /= lp->d_secsize; 14033233afaeSJohn W. De Boskey pp->p_size = size; 14043233afaeSJohn W. De Boskey } 14053233afaeSJohn W. De Boskey /* else already in sectors */ 14066b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 14073233afaeSJohn W. De Boskey total_size += size; 14083233afaeSJohn W. De Boskey } 14093233afaeSJohn W. De Boskey } 14103233afaeSJohn W. De Boskey } 14113233afaeSJohn W. De Boskey } 14123233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 14133233afaeSJohn W. De Boskey if (total_percent != 0) { 14143233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 14153233afaeSJohn W. De Boskey if (total_percent > 100) { 14166b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 14173233afaeSJohn W. De Boskey total_percent); 14183233afaeSJohn W. De Boskey errors++; 14193233afaeSJohn W. De Boskey } 14203233afaeSJohn W. De Boskey 14213233afaeSJohn W. De Boskey if (free_space > 0) { 14223233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14233233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14243233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 14253233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 14263233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 14273233afaeSJohn W. De Boskey total_size += pp->p_size; 14283233afaeSJohn W. De Boskey 14293233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 14303233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 14313233afaeSJohn W. De Boskey } 14323233afaeSJohn W. De Boskey } 14333233afaeSJohn W. De Boskey } else { 14343233afaeSJohn W. De Boskey fprintf(stderr, 14356b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 14363233afaeSJohn W. De Boskey free_space); 14373233afaeSJohn W. De Boskey errors++; 14383233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 14393233afaeSJohn W. De Boskey } 14403233afaeSJohn W. De Boskey } 14413233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 14423233afaeSJohn W. De Boskey if (hog_part != -1) { 14433233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 14443233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 14453233afaeSJohn W. De Boskey } 14463233afaeSJohn W. De Boskey 14473233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 14483233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 14493233afaeSJohn W. De Boskey seen_default_offset = 0; 14503233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14513233afaeSJohn W. De Boskey part = 'a' + i; 14523233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14533233afaeSJohn W. De Boskey if (part_set[i]) { 14543233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 14556b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 14563233afaeSJohn W. De Boskey pp->p_offset = 0; 14573233afaeSJohn W. De Boskey } else { 14583233afaeSJohn W. De Boskey pp->p_offset = current_offset; 14593233afaeSJohn W. De Boskey seen_default_offset = 1; 14603233afaeSJohn W. De Boskey } 14613233afaeSJohn W. De Boskey } else { 14623233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 14633233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 14646b0ff5f5SPoul-Henning Kamp seen_default_offset && i != RAW_PART) { 14653233afaeSJohn W. De Boskey fprintf(stderr, 14666b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 14676b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 14683233afaeSJohn W. De Boskey fprintf(stderr, 14693233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 14703233afaeSJohn W. De Boskey errors++; 14713233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 14726b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 14733233afaeSJohn W. De Boskey /* 14743233afaeSJohn W. De Boskey * this may give unneeded warnings if 14753233afaeSJohn W. De Boskey * partitions are out-of-order 14763233afaeSJohn W. De Boskey */ 14773233afaeSJohn W. De Boskey Warning( 14783233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 14796b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 14803233afaeSJohn W. De Boskey } 14813233afaeSJohn W. De Boskey } 14826b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 14833233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 14843233afaeSJohn W. De Boskey } 14853233afaeSJohn W. De Boskey } 14863233afaeSJohn W. De Boskey 14878fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 14888fae3551SRodney W. Grimes part = 'a' + i; 14898fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 14908fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 14912a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 14922a1deaaaSBruce Evans part, (u_long)pp->p_offset); 1493fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 1494fd43aa1cSDavid E. O'Brien /* See comment in writelabel(). */ 1495fd43aa1cSDavid E. O'Brien if (pp->p_offset % lp->d_secpercyl != 0) { 1496fd43aa1cSDavid E. O'Brien fprintf(stderr, "partition %c: does not start on a " 1497fd43aa1cSDavid E. O'Brien "cylinder boundary!\n", part); 1498fd43aa1cSDavid E. O'Brien errors++; 1499fd43aa1cSDavid E. O'Brien } 1500fd43aa1cSDavid E. O'Brien #endif 15018fae3551SRodney W. Grimes #ifdef notdef 15028fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 15038fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 15048fae3551SRodney W. Grimes part); 15058fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 15068fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 15078fae3551SRodney W. Grimes part); 15088fae3551SRodney W. Grimes #endif 15098fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 15108fae3551SRodney W. Grimes fprintf(stderr, 15118fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 15128fae3551SRodney W. Grimes errors++; 15138fae3551SRodney W. Grimes } 15148fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 15158fae3551SRodney W. Grimes fprintf(stderr, 15168fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 15178fae3551SRodney W. Grimes part); 15188fae3551SRodney W. Grimes errors++; 15198fae3551SRodney W. Grimes } 15206b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) 15213233afaeSJohn W. De Boskey { 15223233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 15233233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 15243233afaeSJohn W. De Boskey if (pp->p_offset != 0) 15253233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 15263233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 15273233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 15283233afaeSJohn W. De Boskey 15293233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 15303233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 15313233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 15323233afaeSJohn W. De Boskey "standard system utilities",part); 15333233afaeSJohn W. De Boskey } 15343233afaeSJohn W. De Boskey } 15353233afaeSJohn W. De Boskey 15363233afaeSJohn W. De Boskey /* check for overlaps */ 15373233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 15383233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 15396b0ff5f5SPoul-Henning Kamp if (j != RAW_PART && i != RAW_PART && 15403233afaeSJohn W. De Boskey part_set[i] && part_set[j]) { 15413233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 15423233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 15433233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 15443233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 15453233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 15463233afaeSJohn W. De Boskey j + 'a', i + 'a'); 15473233afaeSJohn W. De Boskey errors++; 15483233afaeSJohn W. De Boskey } 15493233afaeSJohn W. De Boskey } 15503233afaeSJohn W. De Boskey } 15518fae3551SRodney W. Grimes } 15528fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 15538fae3551SRodney W. Grimes part = 'a' + i; 15548fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 15558fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 15562a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 15572a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 15588fae3551SRodney W. Grimes } 15598fae3551SRodney W. Grimes return (errors); 15608fae3551SRodney W. Grimes } 15618fae3551SRodney W. Grimes 15628fae3551SRodney W. Grimes /* 1563425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1564425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1565425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1566425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1567425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1568425bed3aSJoerg Wunsch * 1569425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1570425bed3aSJoerg Wunsch */ 1571425bed3aSJoerg Wunsch struct disklabel * 1572425bed3aSJoerg Wunsch getvirginlabel(void) 1573425bed3aSJoerg Wunsch { 1574c8223965SMark Murray static struct disklabel loclab; 1575c8223965SMark Murray char lnamebuf[BBSIZE]; 1576425bed3aSJoerg Wunsch int f; 1577425bed3aSJoerg Wunsch 1578425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1579e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 158043be698cSBruce Evans return (NULL); 1581425bed3aSJoerg Wunsch } 1582c8223965SMark Murray (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1583c8223965SMark Murray if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1584c8223965SMark Murray warn("cannot open %s", lnamebuf); 158543be698cSBruce Evans return (NULL); 1586425bed3aSJoerg Wunsch } 1587ff7d5162SJordan K. Hubbard 1588ff7d5162SJordan K. Hubbard /* 1589ff7d5162SJordan K. Hubbard * Try to use the new get-virgin-label ioctl. If it fails, 1590ff7d5162SJordan K. Hubbard * fallback to the old get-disdk-info ioctl. 1591ff7d5162SJordan K. Hubbard */ 1592c8223965SMark Murray if (ioctl(f, DIOCGDVIRGIN, &loclab) == 0) 15935b445518SPoul-Henning Kamp goto out; 1594c8223965SMark Murray if (ioctl(f, DIOCGDINFO, &loclab) == 0) 15955b445518SPoul-Henning Kamp goto out; 1596425bed3aSJoerg Wunsch close(f); 1597c8223965SMark Murray (void)snprintf(lnamebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname, 15985b445518SPoul-Henning Kamp 'a' + RAW_PART); 1599c8223965SMark Murray if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1600c8223965SMark Murray warn("cannot open %s", lnamebuf); 160143be698cSBruce Evans return (NULL); 1602425bed3aSJoerg Wunsch } 1603c8223965SMark Murray if (ioctl(f, DIOCGDINFO, &loclab) == 0) 16045b445518SPoul-Henning Kamp goto out; 16055b445518SPoul-Henning Kamp close(f); 1606c8223965SMark Murray warn("No virgin disklabel found %s", lnamebuf); 16075b445518SPoul-Henning Kamp return (NULL); 16085b445518SPoul-Henning Kamp out: 1609425bed3aSJoerg Wunsch close(f); 1610c8223965SMark Murray return (&loclab); 1611425bed3aSJoerg Wunsch } 1612425bed3aSJoerg Wunsch 1613425bed3aSJoerg Wunsch /* 16148fae3551SRodney W. Grimes * If we are installing a boot program that doesn't fit in d_bbsize 16158fae3551SRodney W. Grimes * we need to mark those partitions that the boot overflows into. 16168fae3551SRodney W. Grimes * This allows newfs to prevent creation of a filesystem where it might 16178fae3551SRodney W. Grimes * clobber bootstrap code. 16188fae3551SRodney W. Grimes */ 161961de51caSJoerg Wunsch void 1620326c7cdaSWarner Losh setbootflag(struct disklabel *lp) 16218fae3551SRodney W. Grimes { 1622326c7cdaSWarner Losh struct partition *pp; 16238fae3551SRodney W. Grimes int i, errors = 0; 16248fae3551SRodney W. Grimes char part; 16258fae3551SRodney W. Grimes u_long boffset; 16268fae3551SRodney W. Grimes 16278fae3551SRodney W. Grimes if (bootbuf == 0) 16288fae3551SRodney W. Grimes return; 16298fae3551SRodney W. Grimes boffset = bootsize / lp->d_secsize; 16308fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 16318fae3551SRodney W. Grimes part = 'a' + i; 16328fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 16338fae3551SRodney W. Grimes if (pp->p_size == 0) 16348fae3551SRodney W. Grimes continue; 16358fae3551SRodney W. Grimes if (boffset <= pp->p_offset) { 16368fae3551SRodney W. Grimes if (pp->p_fstype == FS_BOOT) 16378fae3551SRodney W. Grimes pp->p_fstype = FS_UNUSED; 16388fae3551SRodney W. Grimes } else if (pp->p_fstype != FS_BOOT) { 16398fae3551SRodney W. Grimes if (pp->p_fstype != FS_UNUSED) { 16408fae3551SRodney W. Grimes fprintf(stderr, 16418fae3551SRodney W. Grimes "boot overlaps used partition %c\n", 16428fae3551SRodney W. Grimes part); 16438fae3551SRodney W. Grimes errors++; 16448fae3551SRodney W. Grimes } else { 16458fae3551SRodney W. Grimes pp->p_fstype = FS_BOOT; 16468fae3551SRodney W. Grimes Warning("boot overlaps partition %c, %s", 16478fae3551SRodney W. Grimes part, "marked as FS_BOOT"); 16488fae3551SRodney W. Grimes } 16498fae3551SRodney W. Grimes } 16508fae3551SRodney W. Grimes } 16516bd343a9SPhilippe Charnier if (errors) 16526bd343a9SPhilippe Charnier errx(4, "cannot install boot program"); 16538fae3551SRodney W. Grimes } 16548fae3551SRodney W. Grimes 16558fae3551SRodney W. Grimes /*VARARGS1*/ 165661de51caSJoerg Wunsch void 1657326c7cdaSWarner Losh Warning(const char *fmt, ...) 16588fae3551SRodney W. Grimes { 165961de51caSJoerg Wunsch va_list ap; 16608fae3551SRodney W. Grimes 16618fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 166261de51caSJoerg Wunsch va_start(ap, fmt); 166361de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 16648fae3551SRodney W. Grimes fprintf(stderr, "\n"); 166561de51caSJoerg Wunsch va_end(ap); 16668fae3551SRodney W. Grimes } 16678fae3551SRodney W. Grimes 166861de51caSJoerg Wunsch void 1669326c7cdaSWarner Losh usage(void) 16708fae3551SRodney W. Grimes { 16718fae3551SRodney W. Grimes #if NUMBOOT > 0 1672bef2080aSPhilippe 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", 16738fae3551SRodney W. Grimes "usage: disklabel [-r] disk", 1674bef2080aSPhilippe Charnier "\t\t(to read label)", 16753233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1676bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 16773233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1678bef2080aSPhilippe Charnier "\t\t(to edit label)", 16793233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1680bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 16818fae3551SRodney W. Grimes #if NUMBOOT > 1 16823233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1683bef2080aSPhilippe Charnier "\t\t(to install boot program with existing label)", 16843233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1685bef2080aSPhilippe Charnier "\t\t(to write label and boot program)", 16863233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1687bef2080aSPhilippe Charnier "\t\t(to restore label and boot program)", 16888fae3551SRodney W. Grimes #else 16893233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1690bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 16913233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1692bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 16933233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 1694bef2080aSPhilippe Charnier "\t\t(to restore label and install boot program)", 16958fae3551SRodney W. Grimes #endif 1696bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1697bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 16988fae3551SRodney W. Grimes #else 1699bef2080aSPhilippe Charnier fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 17008fae3551SRodney W. Grimes "usage: disklabel [-r] disk", "(to read label)", 17013233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1702bef2080aSPhilippe Charnier "\t\t(to write label)", 17033233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1704bef2080aSPhilippe Charnier "\t\t(to edit label)", 17053233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1706bef2080aSPhilippe Charnier "\t\t(to restore label)", 1707bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1708bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17098fae3551SRodney W. Grimes #endif 17108fae3551SRodney W. Grimes exit(1); 17118fae3551SRodney W. Grimes } 1712