18fae3551SRodney W. Grimes /* 2fd43aa1cSDavid E. O'Brien * Copyright (c) 1994, 1995 Gordon W. Ross 3fd43aa1cSDavid E. O'Brien * Copyright (c) 1994 Theo de Raadt 4fd43aa1cSDavid E. O'Brien * All rights reserved. 58fae3551SRodney W. Grimes * Copyright (c) 1987, 1993 68fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 98fae3551SRodney W. Grimes * Symmetric Computer Systems. 108fae3551SRodney W. Grimes * 118fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 128fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 138fae3551SRodney W. Grimes * are met: 148fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 158fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 168fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 178fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 188fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 198fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 208fae3551SRodney W. Grimes * must display the following acknowledgement: 218fae3551SRodney W. Grimes * This product includes software developed by the University of 228fae3551SRodney W. Grimes * California, Berkeley and its contributors. 23fd43aa1cSDavid E. O'Brien * This product includes software developed by Theo de Raadt. 248fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 258fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 268fae3551SRodney W. Grimes * without specific prior written permission. 278fae3551SRodney W. Grimes * 288fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 298fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 308fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 318fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 328fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 338fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 348fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 358fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 368fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 378fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 388fae3551SRodney W. Grimes * SUCH DAMAGE. 39fd43aa1cSDavid E. O'Brien * 40fd43aa1cSDavid E. O'Brien * from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $ 418fae3551SRodney W. Grimes */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 446bd343a9SPhilippe Charnier static const char copyright[] = 458fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\ 468fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 478fae3551SRodney W. Grimes #endif /* not lint */ 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes #ifndef lint 506bd343a9SPhilippe Charnier #if 0 518fae3551SRodney W. Grimes static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94"; 528fae3551SRodney W. Grimes /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 536bd343a9SPhilippe Charnier #endif 548fae3551SRodney W. Grimes #endif /* not lint */ 558fae3551SRodney W. Grimes 56c8223965SMark Murray #include <sys/cdefs.h> 57c8223965SMark Murray __FBSDID("$FreeBSD$"); 58c8223965SMark Murray 598fae3551SRodney W. Grimes #include <sys/param.h> 608fae3551SRodney W. Grimes #include <sys/file.h> 618fae3551SRodney W. Grimes #include <sys/stat.h> 6261de51caSJoerg Wunsch #include <sys/wait.h> 63b9d05a16SPoul-Henning Kamp #include <sys/disk.h> 648fae3551SRodney W. Grimes #define DKTYPENAMES 65b35e6950SBosko Milekic #define FSTYPENAMES 668fae3551SRodney W. Grimes #include <sys/disklabel.h> 676d8c8fabSYoshihiro Takahashi #ifdef PC98 686d8c8fabSYoshihiro Takahashi #include <sys/diskpc98.h> 696d8c8fabSYoshihiro Takahashi #else 703bb24c35SPoul-Henning Kamp #include <sys/diskmbr.h> 716d8c8fabSYoshihiro Takahashi #endif 72fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 73fd43aa1cSDavid E. O'Brien #include <sys/sun_disklabel.h> 74fd43aa1cSDavid E. O'Brien #endif 75c8223965SMark Murray 768fae3551SRodney W. Grimes #include <unistd.h> 778fae3551SRodney W. Grimes #include <string.h> 788fae3551SRodney W. Grimes #include <stdio.h> 7961de51caSJoerg Wunsch #include <stdlib.h> 8061de51caSJoerg Wunsch #include <signal.h> 8161de51caSJoerg Wunsch #include <stdarg.h> 828fae3551SRodney W. Grimes #include <ctype.h> 83bef2080aSPhilippe Charnier #include <err.h> 8437736675SWarner Losh #include <errno.h> 85c8223965SMark Murray 868fae3551SRodney W. Grimes #include "pathnames.h" 878fae3551SRodney W. Grimes 888fae3551SRodney W. Grimes /* 898fae3551SRodney W. Grimes * Disklabel: read and write disklabels. 908fae3551SRodney W. Grimes * The label is usually placed on one of the first sectors of the disk. 918fae3551SRodney W. Grimes * Many machines also place a bootstrap in the same area, 928fae3551SRodney W. Grimes * in which case the label is embedded in the bootstrap. 938fae3551SRodney W. Grimes * The bootstrap source must leave space at the proper offset 948fae3551SRodney W. Grimes * for the label on such machines. 958fae3551SRodney W. Grimes */ 968fae3551SRodney W. Grimes 978fae3551SRodney W. Grimes #ifndef BBSIZE 988fae3551SRodney W. Grimes #define BBSIZE 8192 /* size of boot area, with label */ 998fae3551SRodney W. Grimes #endif 1008fae3551SRodney W. Grimes 1013233afaeSJohn W. De Boskey /* FIX! These are too low, but are traditional */ 1023233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK 8192U 1033233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG 1024U 1043233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG 16U 1053233afaeSJohn W. De Boskey 1063233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK 16384U 107f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG 2048U 1083233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG 64U 1093233afaeSJohn W. De Boskey 11090b8f6baSDavid E. O'Brien #if defined(__i386__) || defined(__ia64__) 11190b8f6baSDavid E. O'Brien #define NUMBOOT 2 11287efda01SBenno Rice #elif defined(__alpha__) || defined(__sparc64__) || defined(__powerpc__) 1138fae3551SRodney W. Grimes #define NUMBOOT 1 1148fae3551SRodney W. Grimes #else 11590b8f6baSDavid E. O'Brien #error I do not know about this architecture. 1168fae3551SRodney W. Grimes #endif 1178fae3551SRodney W. Grimes 118326c7cdaSWarner Losh void makelabel(const char *, const char *, struct disklabel *); 119326c7cdaSWarner Losh int writelabel(int, const char *, struct disklabel *); 120326c7cdaSWarner Losh void l_perror(const char *); 121326c7cdaSWarner Losh struct disklabel *readlabel(int); 122326c7cdaSWarner Losh struct disklabel *makebootarea(char *, struct disklabel *, int); 123326c7cdaSWarner Losh void display(FILE *, const struct disklabel *); 124326c7cdaSWarner Losh int edit(struct disklabel *, int); 125326c7cdaSWarner Losh int editit(void); 126326c7cdaSWarner Losh char *skip(char *); 127326c7cdaSWarner Losh char *word(char *); 128326c7cdaSWarner Losh int getasciilabel(FILE *, struct disklabel *); 12967b46708SIan Dowse int getasciipartspec(char *, struct disklabel *, int, int); 130326c7cdaSWarner Losh int checklabel(struct disklabel *); 131326c7cdaSWarner Losh void setbootflag(struct disklabel *); 132326c7cdaSWarner Losh void Warning(const char *, ...) __printflike(1, 2); 133326c7cdaSWarner Losh void usage(void); 134326c7cdaSWarner Losh struct disklabel *getvirginlabel(void); 13561de51caSJoerg Wunsch 1368fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 1378fae3551SRodney W. Grimes #define streq(a,b) (strcmp(a,b) == 0) 1388fae3551SRodney W. Grimes 1398fae3551SRodney W. Grimes char *dkname; 1408fae3551SRodney W. Grimes char *specname; 141aaae3130SKris Kennaway char tmpfil[] = PATH_TMPFILE; 1428fae3551SRodney W. Grimes 1438fae3551SRodney W. Grimes char namebuf[BBSIZE], *np = namebuf; 1448fae3551SRodney W. Grimes struct disklabel lab; 1458fae3551SRodney W. Grimes char bootarea[BBSIZE]; 146c8223965SMark Murray char blank[] = ""; 147c8223965SMark Murray char unknown[] = "unknown"; 1488fae3551SRodney W. Grimes 1493233afaeSJohn W. De Boskey #define MAX_PART ('z') 1503233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 1513233afaeSJohn W. De Boskey char part_size_type[MAX_NUM_PARTS]; 1523233afaeSJohn W. De Boskey char part_offset_type[MAX_NUM_PARTS]; 1533233afaeSJohn W. De Boskey int part_set[MAX_NUM_PARTS]; 1543233afaeSJohn W. De Boskey 1558fae3551SRodney W. Grimes #if NUMBOOT > 0 1568fae3551SRodney W. Grimes int installboot; /* non-zero if we should install a boot program */ 1578fae3551SRodney W. Grimes char *bootbuf; /* pointer to buffer with remainder of boot prog */ 1588fae3551SRodney W. Grimes int bootsize; /* size of remaining boot program */ 1598fae3551SRodney W. Grimes char *xxboot; /* primary boot */ 1608fae3551SRodney W. Grimes char *bootxx; /* secondary boot */ 1618fae3551SRodney W. Grimes char boot0[MAXPATHLEN]; 1628fae3551SRodney W. Grimes char boot1[MAXPATHLEN]; 1638fae3551SRodney W. Grimes #endif 1648fae3551SRodney W. Grimes 1658fae3551SRodney W. Grimes enum { 1668fae3551SRodney W. Grimes UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT 1678fae3551SRodney W. Grimes } op = UNSPEC; 1688fae3551SRodney W. Grimes 1698fae3551SRodney W. Grimes int rflag; 1703233afaeSJohn W. De Boskey int disable_write; /* set to disable writing to disk label */ 1718fae3551SRodney W. Grimes 1723233afaeSJohn W. De Boskey #define OPTIONS "BNRWb:enrs:w" 1738fae3551SRodney W. Grimes 17461de51caSJoerg Wunsch int 175326c7cdaSWarner Losh main(int argc, char *argv[]) 1768fae3551SRodney W. Grimes { 177c1fdb43fSWarner Losh struct disklabel *lp; 1788fae3551SRodney W. Grimes FILE *t; 1793b3038a6SBill Fumerola int ch, f = 0, flag, error = 0; 1808fae3551SRodney W. Grimes char *name = 0; 1818fae3551SRodney W. Grimes 1828d64695cSWarner Losh while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1838fae3551SRodney W. Grimes switch (ch) { 1848fae3551SRodney W. Grimes #if NUMBOOT > 0 1858fae3551SRodney W. Grimes case 'B': 1868fae3551SRodney W. Grimes ++installboot; 1878fae3551SRodney W. Grimes break; 1888fae3551SRodney W. Grimes case 'b': 1898fae3551SRodney W. Grimes xxboot = optarg; 1908fae3551SRodney W. Grimes break; 1918fae3551SRodney W. Grimes #if NUMBOOT > 1 1928fae3551SRodney W. Grimes case 's': 1938fae3551SRodney W. Grimes bootxx = optarg; 1948fae3551SRodney W. Grimes break; 1958fae3551SRodney W. Grimes #endif 1968fae3551SRodney W. Grimes #endif 1978fae3551SRodney W. Grimes case 'N': 1988fae3551SRodney W. Grimes if (op != UNSPEC) 1998fae3551SRodney W. Grimes usage(); 2008fae3551SRodney W. Grimes op = NOWRITE; 2018fae3551SRodney W. Grimes break; 2023233afaeSJohn W. De Boskey case 'n': 2033233afaeSJohn W. De Boskey disable_write = 1; 2043233afaeSJohn W. De Boskey break; 2058fae3551SRodney W. Grimes case 'R': 2068fae3551SRodney W. Grimes if (op != UNSPEC) 2078fae3551SRodney W. Grimes usage(); 2088fae3551SRodney W. Grimes op = RESTORE; 2098fae3551SRodney W. Grimes break; 2108fae3551SRodney W. Grimes case 'W': 2118fae3551SRodney W. Grimes if (op != UNSPEC) 2128fae3551SRodney W. Grimes usage(); 2138fae3551SRodney W. Grimes op = WRITEABLE; 2148fae3551SRodney W. Grimes break; 2158fae3551SRodney W. Grimes case 'e': 2168fae3551SRodney W. Grimes if (op != UNSPEC) 2178fae3551SRodney W. Grimes usage(); 2188fae3551SRodney W. Grimes op = EDIT; 2198fae3551SRodney W. Grimes break; 2208fae3551SRodney W. Grimes case 'r': 2218fae3551SRodney W. Grimes ++rflag; 2228fae3551SRodney W. Grimes break; 2238fae3551SRodney W. Grimes case 'w': 2248fae3551SRodney W. Grimes if (op != UNSPEC) 2258fae3551SRodney W. Grimes usage(); 2268fae3551SRodney W. Grimes op = WRITE; 2278fae3551SRodney W. Grimes break; 2288fae3551SRodney W. Grimes case '?': 2298fae3551SRodney W. Grimes default: 2308fae3551SRodney W. Grimes usage(); 2318fae3551SRodney W. Grimes } 2328fae3551SRodney W. Grimes argc -= optind; 2338fae3551SRodney W. Grimes argv += optind; 2348fae3551SRodney W. Grimes #if NUMBOOT > 0 2358fae3551SRodney W. Grimes if (installboot) { 2368fae3551SRodney W. Grimes rflag++; 2378fae3551SRodney W. Grimes if (op == UNSPEC) 2388fae3551SRodney W. Grimes op = WRITEBOOT; 2398fae3551SRodney W. Grimes } else { 2408fae3551SRodney W. Grimes if (op == UNSPEC) 2418fae3551SRodney W. Grimes op = READ; 2428fae3551SRodney W. Grimes xxboot = bootxx = 0; 2438fae3551SRodney W. Grimes } 2448fae3551SRodney W. Grimes #else 2458fae3551SRodney W. Grimes if (op == UNSPEC) 2468fae3551SRodney W. Grimes op = READ; 2478fae3551SRodney W. Grimes #endif 2488fae3551SRodney W. Grimes if (argc < 1) 2498fae3551SRodney W. Grimes usage(); 2508fae3551SRodney W. Grimes 2518fae3551SRodney W. Grimes dkname = argv[0]; 2528fae3551SRodney W. Grimes if (dkname[0] != '/') { 25309c4216dSDavid E. O'Brien (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 2548fae3551SRodney W. Grimes specname = np; 2558fae3551SRodney W. Grimes np += strlen(specname) + 1; 2568fae3551SRodney W. Grimes } else 2578fae3551SRodney W. Grimes specname = dkname; 2588fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2598fae3551SRodney W. Grimes if (f < 0 && errno == ENOENT && dkname[0] != '/') { 26043c6c959SDavid E. O'Brien (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 2618fae3551SRodney W. Grimes np = namebuf + strlen(specname) + 1; 2628fae3551SRodney W. Grimes f = open(specname, op == READ ? O_RDONLY : O_RDWR); 2638fae3551SRodney W. Grimes } 2648fae3551SRodney W. Grimes if (f < 0) 265bef2080aSPhilippe Charnier err(4, "%s", specname); 2668fae3551SRodney W. Grimes 2678fae3551SRodney W. Grimes switch(op) { 2688fae3551SRodney W. Grimes 2693b3038a6SBill Fumerola case UNSPEC: 2703b3038a6SBill Fumerola break; 2713b3038a6SBill Fumerola 2728fae3551SRodney W. Grimes case EDIT: 2738fae3551SRodney W. Grimes if (argc != 1) 2748fae3551SRodney W. Grimes usage(); 2758fae3551SRodney W. Grimes lp = readlabel(f); 2768fae3551SRodney W. Grimes error = edit(lp, f); 2778fae3551SRodney W. Grimes break; 2788fae3551SRodney W. Grimes 2798fae3551SRodney W. Grimes case NOWRITE: 2808fae3551SRodney W. Grimes flag = 0; 2818fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 282bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 2838fae3551SRodney W. Grimes break; 2848fae3551SRodney W. Grimes 2858fae3551SRodney W. Grimes case READ: 2868fae3551SRodney W. Grimes if (argc != 1) 2878fae3551SRodney W. Grimes usage(); 2888fae3551SRodney W. Grimes lp = readlabel(f); 2898fae3551SRodney W. Grimes display(stdout, lp); 2908fae3551SRodney W. Grimes error = checklabel(lp); 2918fae3551SRodney W. Grimes break; 2928fae3551SRodney W. Grimes 2938fae3551SRodney W. Grimes case RESTORE: 2948fae3551SRodney W. Grimes #if NUMBOOT > 0 2958fae3551SRodney W. Grimes if (installboot && argc == 3) { 2968fae3551SRodney W. Grimes makelabel(argv[2], 0, &lab); 2978fae3551SRodney W. Grimes argc--; 2986cabb348SBruce Evans 2996cabb348SBruce Evans /* 3006cabb348SBruce Evans * We only called makelabel() for its side effect 3016cabb348SBruce Evans * of setting the bootstrap file names. Discard 3026cabb348SBruce Evans * all changes to `lab' so that all values in the 3036cabb348SBruce Evans * final label come from the ASCII label. 3046cabb348SBruce Evans */ 3056cabb348SBruce Evans bzero((char *)&lab, sizeof(lab)); 3068fae3551SRodney W. Grimes } 3078fae3551SRodney W. Grimes #endif 3088fae3551SRodney W. Grimes if (argc != 2) 3098fae3551SRodney W. Grimes usage(); 3108fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 311bef2080aSPhilippe Charnier err(4, "%s", argv[1]); 3126cabb348SBruce Evans if (!getasciilabel(t, &lab)) 3136cabb348SBruce Evans exit(1); 3146cabb348SBruce Evans lp = makebootarea(bootarea, &lab, f); 3156cabb348SBruce Evans *lp = lab; 3168fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3178fae3551SRodney W. Grimes break; 3188fae3551SRodney W. Grimes 3198fae3551SRodney W. Grimes case WRITE: 3208fae3551SRodney W. Grimes if (argc == 3) { 3218fae3551SRodney W. Grimes name = argv[2]; 3228fae3551SRodney W. Grimes argc--; 3238fae3551SRodney W. Grimes } 3248fae3551SRodney W. Grimes if (argc != 2) 3258fae3551SRodney W. Grimes usage(); 3268fae3551SRodney W. Grimes makelabel(argv[1], name, &lab); 3278fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3288fae3551SRodney W. Grimes *lp = lab; 3298fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3308fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3318fae3551SRodney W. Grimes break; 3328fae3551SRodney W. Grimes 3338fae3551SRodney W. Grimes case WRITEABLE: 3348fae3551SRodney W. Grimes flag = 1; 3358fae3551SRodney W. Grimes if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 336bef2080aSPhilippe Charnier err(4, "ioctl DIOCWLABEL"); 3378fae3551SRodney W. Grimes break; 3388fae3551SRodney W. Grimes 3398fae3551SRodney W. Grimes #if NUMBOOT > 0 3408fae3551SRodney W. Grimes case WRITEBOOT: 3418fae3551SRodney W. Grimes { 3428fae3551SRodney W. Grimes struct disklabel tlab; 3438fae3551SRodney W. Grimes 3448fae3551SRodney W. Grimes lp = readlabel(f); 3458fae3551SRodney W. Grimes tlab = *lp; 3468fae3551SRodney W. Grimes if (argc == 2) 3478fae3551SRodney W. Grimes makelabel(argv[1], 0, &lab); 3488fae3551SRodney W. Grimes lp = makebootarea(bootarea, &lab, f); 3498fae3551SRodney W. Grimes *lp = tlab; 3508fae3551SRodney W. Grimes if (checklabel(lp) == 0) 3518fae3551SRodney W. Grimes error = writelabel(f, bootarea, lp); 3528fae3551SRodney W. Grimes break; 3538fae3551SRodney W. Grimes } 3548fae3551SRodney W. Grimes #endif 3558fae3551SRodney W. Grimes } 3568fae3551SRodney W. Grimes exit(error); 3578fae3551SRodney W. Grimes } 3588fae3551SRodney W. Grimes 3598fae3551SRodney W. Grimes /* 3608fae3551SRodney W. Grimes * Construct a prototype disklabel from /etc/disktab. As a side 3618fae3551SRodney W. Grimes * effect, set the names of the primary and secondary boot files 3628fae3551SRodney W. Grimes * if specified. 3638fae3551SRodney W. Grimes */ 36461de51caSJoerg Wunsch void 365326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp) 3668fae3551SRodney W. Grimes { 367326c7cdaSWarner Losh struct disklabel *dp; 368425bed3aSJoerg Wunsch 369425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 370425bed3aSJoerg Wunsch dp = getvirginlabel(); 371425bed3aSJoerg Wunsch else 3728fae3551SRodney W. Grimes dp = getdiskbyname(type); 3736bd343a9SPhilippe Charnier if (dp == NULL) 3746bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3758fae3551SRodney W. Grimes *lp = *dp; 3768fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 3778fae3551SRodney W. Grimes if (name) 3788fae3551SRodney W. Grimes (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 3798fae3551SRodney W. Grimes } 3808fae3551SRodney W. Grimes 38161de51caSJoerg Wunsch int 382326c7cdaSWarner Losh writelabel(int f, const char *boot, struct disklabel *lp) 3838fae3551SRodney W. Grimes { 3848fae3551SRodney W. Grimes int flag; 385130cd73aSDoug Rabson #ifdef __alpha__ 386130cd73aSDoug Rabson u_long *p, sum; 387130cd73aSDoug Rabson int i; 388130cd73aSDoug Rabson #endif 389fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 390fd43aa1cSDavid E. O'Brien struct sun_disklabel *sl; 391fd43aa1cSDavid E. O'Brien u_short cksum, *sp1, *sp2; 392fd43aa1cSDavid E. O'Brien struct partition *npp; 393fd43aa1cSDavid E. O'Brien struct sun_dkpart *spp; 394fd43aa1cSDavid E. O'Brien int i, secpercyl; 395cc18e4ccSBruce Evans #endif 396130cd73aSDoug Rabson 3973233afaeSJohn W. De Boskey if (disable_write) { 3983233afaeSJohn W. De Boskey Warning("write to disk label supressed - label was as follows:"); 3993233afaeSJohn W. De Boskey display(stdout, lp); 4003233afaeSJohn W. De Boskey return (0); 4013233afaeSJohn W. De Boskey } else { 4028fae3551SRodney W. Grimes setbootflag(lp); 4038fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 4048fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 4058fae3551SRodney W. Grimes lp->d_checksum = 0; 4068fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 4078fae3551SRodney W. Grimes if (rflag) { 4088fae3551SRodney W. Grimes /* 4098fae3551SRodney W. Grimes * First set the kernel disk label, 4108fae3551SRodney W. Grimes * then write a label to the raw disk. 4118fae3551SRodney W. Grimes * If the SDINFO ioctl fails because it is unimplemented, 4128fae3551SRodney W. Grimes * keep going; otherwise, the kernel consistency checks 4138fae3551SRodney W. Grimes * may prevent us from changing the current (in-core) 4148fae3551SRodney W. Grimes * label. 4158fae3551SRodney W. Grimes */ 4168fae3551SRodney W. Grimes if (ioctl(f, DIOCSDINFO, lp) < 0 && 4178fae3551SRodney W. Grimes errno != ENODEV && errno != ENOTTY) { 4188fae3551SRodney W. Grimes l_perror("ioctl DIOCSDINFO"); 4198fae3551SRodney W. Grimes return (1); 4208fae3551SRodney W. Grimes } 4218fae3551SRodney W. Grimes (void)lseek(f, (off_t)0, SEEK_SET); 422130cd73aSDoug Rabson 423130cd73aSDoug Rabson #ifdef __alpha__ 424cc18e4ccSBruce Evans /* 425cc18e4ccSBruce Evans * Generate the bootblock checksum for the SRM console. 426cc18e4ccSBruce Evans */ 427130cd73aSDoug Rabson for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) 428130cd73aSDoug Rabson sum += p[i]; 429130cd73aSDoug Rabson p[63] = sum; 430130cd73aSDoug Rabson #endif 431fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 432fd43aa1cSDavid E. O'Brien /* 433fd43aa1cSDavid E. O'Brien * Generate a Sun disklabel around the BSD label for 434fd43aa1cSDavid E. O'Brien * PROM compatability. 435fd43aa1cSDavid E. O'Brien */ 436fd43aa1cSDavid E. O'Brien sl = (struct sun_disklabel *)boot; 437fd43aa1cSDavid E. O'Brien memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname)); 438fd43aa1cSDavid E. O'Brien sl->sl_rpm = lp->d_rpm; 439fd43aa1cSDavid E. O'Brien sl->sl_pcylinders = lp->d_ncylinders + 440fd43aa1cSDavid E. O'Brien lp->d_acylinders; /* XXX */ 441fd43aa1cSDavid E. O'Brien sl->sl_sparespercyl = lp->d_sparespercyl; 442fd43aa1cSDavid E. O'Brien sl->sl_interleave = lp->d_interleave; 443fd43aa1cSDavid E. O'Brien sl->sl_ncylinders = lp->d_ncylinders; 444fd43aa1cSDavid E. O'Brien sl->sl_acylinders = lp->d_acylinders; 445fd43aa1cSDavid E. O'Brien sl->sl_ntracks = lp->d_ntracks; 446fd43aa1cSDavid E. O'Brien sl->sl_nsectors = lp->d_nsectors; 447fd43aa1cSDavid E. O'Brien sl->sl_magic = SUN_DKMAGIC; 448fd43aa1cSDavid E. O'Brien secpercyl = sl->sl_nsectors * sl->sl_ntracks; 449fd43aa1cSDavid E. O'Brien for (i = 0; i < 8; i++) { 450fd43aa1cSDavid E. O'Brien spp = &sl->sl_part[i]; 451fd43aa1cSDavid E. O'Brien npp = &lp->d_partitions[i]; 452fd43aa1cSDavid E. O'Brien /* 453fd43aa1cSDavid E. O'Brien * SunOS partitions must start on a cylinder 454fd43aa1cSDavid E. O'Brien * boundary. Note this restriction is forced 455fd43aa1cSDavid E. O'Brien * upon FreeBSD/sparc64 labels too, since we 456fd43aa1cSDavid E. O'Brien * want to keep both labels synchronised. 457fd43aa1cSDavid E. O'Brien */ 458fd43aa1cSDavid E. O'Brien spp->sdkp_cyloffset = npp->p_offset / secpercyl; 459fd43aa1cSDavid E. O'Brien spp->sdkp_nsectors = npp->p_size; 460fd43aa1cSDavid E. O'Brien } 461130cd73aSDoug Rabson 462fd43aa1cSDavid E. O'Brien /* Compute the XOR checksum. */ 463fd43aa1cSDavid E. O'Brien sp1 = (u_short *)sl; 464fd43aa1cSDavid E. O'Brien sp2 = (u_short *)(sl + 1); 465fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum = 0; 466fd43aa1cSDavid E. O'Brien while (sp1 < sp2) 467fd43aa1cSDavid E. O'Brien cksum ^= *sp1++; 468fd43aa1cSDavid E. O'Brien sl->sl_cksum = cksum; 469fd43aa1cSDavid E. O'Brien #endif 470cc18e4ccSBruce Evans /* 471cc18e4ccSBruce Evans * write enable label sector before write (if necessary), 472cc18e4ccSBruce Evans * disable after writing. 473cc18e4ccSBruce Evans */ 474cc18e4ccSBruce Evans flag = 1; 4750cdbe329SPoul-Henning Kamp (void)ioctl(f, DIOCWLABEL, &flag); 476c8223965SMark Murray if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) { 4776bd343a9SPhilippe Charnier warn("write"); 4788fae3551SRodney W. Grimes return (1); 4798fae3551SRodney W. Grimes } 4808fae3551SRodney W. Grimes #if NUMBOOT > 0 4818fae3551SRodney W. Grimes /* 4828fae3551SRodney W. Grimes * Output the remainder of the disklabel 4838fae3551SRodney W. Grimes */ 4848fae3551SRodney W. Grimes if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 4856bd343a9SPhilippe Charnier warn("write"); 4868fae3551SRodney W. Grimes return(1); 4878fae3551SRodney W. Grimes } 4888fae3551SRodney W. Grimes #endif 4898fae3551SRodney W. Grimes flag = 0; 4908fae3551SRodney W. Grimes (void) ioctl(f, DIOCWLABEL, &flag); 4918fae3551SRodney W. Grimes } else if (ioctl(f, DIOCWDINFO, lp) < 0) { 4928fae3551SRodney W. Grimes l_perror("ioctl DIOCWDINFO"); 4938fae3551SRodney W. Grimes return (1); 4948fae3551SRodney W. Grimes } 4953233afaeSJohn W. De Boskey } 4968fae3551SRodney W. Grimes return (0); 4978fae3551SRodney W. Grimes } 4988fae3551SRodney W. Grimes 49961de51caSJoerg Wunsch void 500326c7cdaSWarner Losh l_perror(const char *s) 5018fae3551SRodney W. Grimes { 5026bd343a9SPhilippe Charnier switch (errno) { 5038fae3551SRodney W. Grimes 5048fae3551SRodney W. Grimes case ESRCH: 5053121d4cbSPhilippe Charnier warnx("%s: no disk label on disk;", s); 5060b3f0926SWarner Losh fprintf(stderr, "add \"-r\" to install initial label\n"); 5078fae3551SRodney W. Grimes break; 5088fae3551SRodney W. Grimes 5098fae3551SRodney W. Grimes case EINVAL: 5103121d4cbSPhilippe Charnier warnx("%s: label magic number or checksum is wrong!", s); 5113121d4cbSPhilippe Charnier fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 5128fae3551SRodney W. Grimes break; 5138fae3551SRodney W. Grimes 5148fae3551SRodney W. Grimes case EBUSY: 5156bd343a9SPhilippe Charnier warnx("%s: open partition would move or shrink", s); 5168fae3551SRodney W. Grimes break; 5178fae3551SRodney W. Grimes 5188fae3551SRodney W. Grimes case EXDEV: 5197de06420SBruce Evans warnx("%s: '%c' partition must start at beginning of disk", 5207de06420SBruce Evans s, 'a' + RAW_PART); 5218fae3551SRodney W. Grimes break; 5228fae3551SRodney W. Grimes 5238fae3551SRodney W. Grimes default: 5246bd343a9SPhilippe Charnier warn((char *)NULL); 5258fae3551SRodney W. Grimes break; 5268fae3551SRodney W. Grimes } 5278fae3551SRodney W. Grimes } 5288fae3551SRodney W. Grimes 5298fae3551SRodney W. Grimes /* 5308fae3551SRodney W. Grimes * Fetch disklabel for disk. 5318fae3551SRodney W. Grimes * Use ioctl to get label unless -r flag is given. 5328fae3551SRodney W. Grimes */ 5338fae3551SRodney W. Grimes struct disklabel * 534326c7cdaSWarner Losh readlabel(int f) 5358fae3551SRodney W. Grimes { 536326c7cdaSWarner Losh struct disklabel *lp; 5378fae3551SRodney W. Grimes 5388fae3551SRodney W. Grimes if (rflag) { 5398fae3551SRodney W. Grimes if (read(f, bootarea, BBSIZE) < BBSIZE) 540bef2080aSPhilippe Charnier err(4, "%s", specname); 5418fae3551SRodney W. Grimes for (lp = (struct disklabel *)bootarea; 5428fae3551SRodney W. Grimes lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 5438fae3551SRodney W. Grimes lp = (struct disklabel *)((char *)lp + 16)) 5448fae3551SRodney W. Grimes if (lp->d_magic == DISKMAGIC && 5458fae3551SRodney W. Grimes lp->d_magic2 == DISKMAGIC) 5468fae3551SRodney W. Grimes break; 5478fae3551SRodney W. Grimes if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 5488fae3551SRodney W. Grimes lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 5493121d4cbSPhilippe Charnier dkcksum(lp) != 0) 5503121d4cbSPhilippe Charnier errx(1, 5513121d4cbSPhilippe Charnier "bad pack magic number (label is damaged, or pack is unlabeled)"); 5528fae3551SRodney W. Grimes } else { 5538fae3551SRodney W. Grimes lp = &lab; 5548fae3551SRodney W. Grimes if (ioctl(f, DIOCGDINFO, lp) < 0) 555bef2080aSPhilippe Charnier err(4, "ioctl DIOCGDINFO"); 5568fae3551SRodney W. Grimes } 5578fae3551SRodney W. Grimes return (lp); 5588fae3551SRodney W. Grimes } 5598fae3551SRodney W. Grimes 5608fae3551SRodney W. Grimes /* 5618fae3551SRodney W. Grimes * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 5628fae3551SRodney W. Grimes * Returns a pointer to the disklabel portion of the bootarea. 5638fae3551SRodney W. Grimes */ 5648fae3551SRodney W. Grimes struct disklabel * 565326c7cdaSWarner Losh makebootarea(char *boot, struct disklabel *dp, int f) 5668fae3551SRodney W. Grimes { 56721c729c2SBruce Evans struct disklabel *lp; 568326c7cdaSWarner Losh char *p; 5698fae3551SRodney W. Grimes int b; 5708fae3551SRodney W. Grimes #if NUMBOOT > 0 5718fae3551SRodney W. Grimes char *dkbasename; 5728fae3551SRodney W. Grimes struct stat sb; 573cc18e4ccSBruce Evans #endif 574cc18e4ccSBruce Evans #ifdef __alpha__ 57521c729c2SBruce Evans u_long *bootinfo; 576cc18e4ccSBruce Evans int n; 577cc18e4ccSBruce Evans #endif 578d70e4e53SJoerg Wunsch #ifdef __i386__ 579d70e4e53SJoerg Wunsch char *tmpbuf; 580d70e4e53SJoerg Wunsch int i, found; 58161de51caSJoerg Wunsch #endif 5828fae3551SRodney W. Grimes 5838fae3551SRodney W. Grimes /* XXX */ 5848fae3551SRodney W. Grimes if (dp->d_secsize == 0) { 5858fae3551SRodney W. Grimes dp->d_secsize = DEV_BSIZE; 5868fae3551SRodney W. Grimes dp->d_bbsize = BBSIZE; 5878fae3551SRodney W. Grimes } 5888fae3551SRodney W. Grimes lp = (struct disklabel *) 5898fae3551SRodney W. Grimes (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 5908fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 5918fae3551SRodney W. Grimes #if NUMBOOT > 0 5928fae3551SRodney W. Grimes /* 5938fae3551SRodney W. Grimes * If we are not installing a boot program but we are installing a 5948fae3551SRodney W. Grimes * label on disk then we must read the current bootarea so we don't 5958fae3551SRodney W. Grimes * clobber the existing boot. 5968fae3551SRodney W. Grimes */ 5978fae3551SRodney W. Grimes if (!installboot) { 5988fae3551SRodney W. Grimes if (rflag) { 5998fae3551SRodney W. Grimes if (read(f, boot, BBSIZE) < BBSIZE) 600bef2080aSPhilippe Charnier err(4, "%s", specname); 6018fae3551SRodney W. Grimes bzero((char *)lp, sizeof *lp); 6028fae3551SRodney W. Grimes } 6038fae3551SRodney W. Grimes return (lp); 6048fae3551SRodney W. Grimes } 6058fae3551SRodney W. Grimes /* 6068fae3551SRodney W. Grimes * We are installing a boot program. Determine the name(s) and 6078fae3551SRodney W. Grimes * read them into the appropriate places in the boot area. 6088fae3551SRodney W. Grimes */ 6098fae3551SRodney W. Grimes if (!xxboot || !bootxx) { 6108fae3551SRodney W. Grimes dkbasename = np; 6118fae3551SRodney W. Grimes if ((p = rindex(dkname, '/')) == NULL) 6128fae3551SRodney W. Grimes p = dkname; 6138fae3551SRodney W. Grimes else 6148fae3551SRodney W. Grimes p++; 6158fae3551SRodney W. Grimes while (*p && !isdigit(*p)) 6168fae3551SRodney W. Grimes *np++ = *p++; 6178fae3551SRodney W. Grimes *np++ = '\0'; 6188fae3551SRodney W. Grimes 6198fae3551SRodney W. Grimes if (!xxboot) { 620efba76d7SJordan K. Hubbard (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); 621efba76d7SJordan K. Hubbard xxboot = boot0; 6228fae3551SRodney W. Grimes } 6238fae3551SRodney W. Grimes #if NUMBOOT > 1 6248fae3551SRodney W. Grimes if (!bootxx) { 625efba76d7SJordan K. Hubbard (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); 626efba76d7SJordan K. Hubbard bootxx = boot1; 6278fae3551SRodney W. Grimes } 6288fae3551SRodney W. Grimes #endif 6298fae3551SRodney W. Grimes } 6308fae3551SRodney W. Grimes 6318fae3551SRodney W. Grimes /* 6328fae3551SRodney W. Grimes * Strange rules: 633fd43aa1cSDavid E. O'Brien * 1. One-piece bootstrap (hp300/hp800) 63490b8f6baSDavid E. O'Brien * 1. One-piece bootstrap (alpha/sparc64) 6358fae3551SRodney W. Grimes * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 6368fae3551SRodney W. Grimes * is remembered and written later following the bootarea. 637fd43aa1cSDavid E. O'Brien * 2. Two-piece bootstraps (i386/ia64) 6388fae3551SRodney W. Grimes * up to d_secsize bytes of ``xxboot'' go in first d_secsize 6398fae3551SRodney W. Grimes * bytes of bootarea, remaining d_bbsize-d_secsize filled 6408fae3551SRodney W. Grimes * from ``bootxx''. 6418fae3551SRodney W. Grimes */ 6428fae3551SRodney W. Grimes b = open(xxboot, O_RDONLY); 6438fae3551SRodney W. Grimes if (b < 0) 644bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6458fae3551SRodney W. Grimes #if NUMBOOT > 1 646d70e4e53SJoerg Wunsch #ifdef __i386__ 647d70e4e53SJoerg Wunsch /* 648d70e4e53SJoerg Wunsch * XXX Botch alert. 649d70e4e53SJoerg Wunsch * The i386 has the so-called fdisk table embedded into the 650d70e4e53SJoerg Wunsch * primary bootstrap. We take care to not clobber it, but 651d70e4e53SJoerg Wunsch * only if it does already contain some data. (Otherwise, 652d70e4e53SJoerg Wunsch * the xxboot provides a template.) 653d70e4e53SJoerg Wunsch */ 654d70e4e53SJoerg Wunsch if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 655bef2080aSPhilippe Charnier err(4, "%s", xxboot); 656d70e4e53SJoerg Wunsch memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 657448f7629SAlfred Perlstein #endif /* __i386__ */ 6588fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_secsize) < 0) 659bef2080aSPhilippe Charnier err(4, "%s", xxboot); 6608fae3551SRodney W. Grimes (void)close(b); 661d70e4e53SJoerg Wunsch #ifdef __i386__ 662d70e4e53SJoerg Wunsch for (i = DOSPARTOFF, found = 0; 663c8223965SMark Murray !found && i < (int)(DOSPARTOFF + NDOSPART*sizeof(struct dos_partition)); 664d70e4e53SJoerg Wunsch i++) 665d70e4e53SJoerg Wunsch found = tmpbuf[i] != 0; 666d70e4e53SJoerg Wunsch if (found) 667d70e4e53SJoerg Wunsch memcpy((void *)&boot[DOSPARTOFF], 668d70e4e53SJoerg Wunsch (void *)&tmpbuf[DOSPARTOFF], 669d70e4e53SJoerg Wunsch NDOSPART * sizeof(struct dos_partition)); 670d70e4e53SJoerg Wunsch free(tmpbuf); 671448f7629SAlfred Perlstein #endif /* __i386__ */ 6728fae3551SRodney W. Grimes b = open(bootxx, O_RDONLY); 6738fae3551SRodney W. Grimes if (b < 0) 674bef2080aSPhilippe Charnier err(4, "%s", bootxx); 6756cabb348SBruce Evans if (fstat(b, &sb) != 0) 6766cabb348SBruce Evans err(4, "%s", bootxx); 6776cabb348SBruce Evans if (dp->d_secsize + sb.st_size > dp->d_bbsize) 6786cabb348SBruce Evans errx(4, "%s too large", bootxx); 67961de51caSJoerg Wunsch if (read(b, &boot[dp->d_secsize], 68061de51caSJoerg Wunsch (int)(dp->d_bbsize-dp->d_secsize)) < 0) 681bef2080aSPhilippe Charnier err(4, "%s", bootxx); 682cc18e4ccSBruce Evans #else /* !(NUMBOOT > 1) */ 683130cd73aSDoug Rabson #ifdef __alpha__ 684130cd73aSDoug Rabson /* 685130cd73aSDoug Rabson * On the alpha, the primary bootstrap starts at the 686130cd73aSDoug Rabson * second sector of the boot area. The first sector 687130cd73aSDoug Rabson * contains the label and must be edited to contain the 688130cd73aSDoug Rabson * size and location of the primary bootstrap. 689130cd73aSDoug Rabson */ 690cc18e4ccSBruce Evans n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 691130cd73aSDoug Rabson if (n < 0) 692130cd73aSDoug Rabson err(4, "%s", xxboot); 69321c729c2SBruce Evans bootinfo = (u_long *)(boot + 480); 69421c729c2SBruce Evans bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 69521c729c2SBruce Evans bootinfo[1] = 1; /* start at sector 1 */ 69621c729c2SBruce Evans bootinfo[2] = 0; /* flags (must be zero) */ 697cc18e4ccSBruce Evans #else /* !__alpha__ */ 6988fae3551SRodney W. Grimes if (read(b, boot, (int)dp->d_bbsize) < 0) 699bef2080aSPhilippe Charnier err(4, "%s", xxboot); 700cc18e4ccSBruce Evans #endif /* __alpha__ */ 7016cabb348SBruce Evans if (fstat(b, &sb) != 0) 7026cabb348SBruce Evans err(4, "%s", xxboot); 7038fae3551SRodney W. Grimes bootsize = (int)sb.st_size - dp->d_bbsize; 7048fae3551SRodney W. Grimes if (bootsize > 0) { 7058fae3551SRodney W. Grimes /* XXX assume d_secsize is a power of two */ 7068fae3551SRodney W. Grimes bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 7078fae3551SRodney W. Grimes bootbuf = (char *)malloc((size_t)bootsize); 7088fae3551SRodney W. Grimes if (bootbuf == 0) 709bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7108fae3551SRodney W. Grimes if (read(b, bootbuf, bootsize) < 0) { 7118fae3551SRodney W. Grimes free(bootbuf); 712bef2080aSPhilippe Charnier err(4, "%s", xxboot); 7138fae3551SRodney W. Grimes } 7148fae3551SRodney W. Grimes } 715cc18e4ccSBruce Evans #endif /* NUMBOOT > 1 */ 7168fae3551SRodney W. Grimes (void)close(b); 717cc18e4ccSBruce Evans #endif /* NUMBOOT > 0 */ 7188fae3551SRodney W. Grimes /* 7198fae3551SRodney W. Grimes * Make sure no part of the bootstrap is written in the area 7208fae3551SRodney W. Grimes * reserved for the label. 7218fae3551SRodney W. Grimes */ 7228fae3551SRodney W. Grimes for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 7236bd343a9SPhilippe Charnier if (*p) 7246bd343a9SPhilippe Charnier errx(2, "bootstrap doesn't leave room for disk label"); 7258fae3551SRodney W. Grimes return (lp); 7268fae3551SRodney W. Grimes } 7278fae3551SRodney W. Grimes 72861de51caSJoerg Wunsch void 729326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 7308fae3551SRodney W. Grimes { 731326c7cdaSWarner Losh int i, j; 732326c7cdaSWarner Losh const struct partition *pp; 7338fae3551SRodney W. Grimes 7348fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 735484c7804SJulian Elischer if (lp->d_type < DKMAXTYPES) 7368fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 7378fae3551SRodney W. Grimes else 7382a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 73961de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 74061de51caSJoerg Wunsch lp->d_typename); 74161de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 74261de51caSJoerg Wunsch lp->d_packname); 7438fae3551SRodney W. Grimes fprintf(f, "flags:"); 7448fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 7458fae3551SRodney W. Grimes fprintf(f, " removeable"); 7468fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 7478fae3551SRodney W. Grimes fprintf(f, " ecc"); 7488fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 7498fae3551SRodney W. Grimes fprintf(f, " badsect"); 7508fae3551SRodney W. Grimes fprintf(f, "\n"); 7512a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 7522a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 7532a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 7542a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 7552a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 7562a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 7572a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 7582a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 7592a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 7602a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 7612a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 7622a1deaaaSBruce Evans (u_long)lp->d_headswitch); 76361de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 7642a1deaaaSBruce Evans (u_long)lp->d_trkseek); 7658fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 7668fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 7678fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 7688fae3551SRodney W. Grimes break; 7698fae3551SRodney W. Grimes if (i < 0) 7708fae3551SRodney W. Grimes i = 0; 7718fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 7722a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 7732a1deaaaSBruce Evans fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 7748fae3551SRodney W. Grimes fprintf(f, 775ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 7768fae3551SRodney W. Grimes pp = lp->d_partitions; 7778fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 7788fae3551SRodney W. Grimes if (pp->p_size) { 7792a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 7802a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 781484c7804SJulian Elischer if (pp->p_fstype < FSMAXTYPES) 7828fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 7838fae3551SRodney W. Grimes else 7848fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 7858fae3551SRodney W. Grimes switch (pp->p_fstype) { 7868fae3551SRodney W. Grimes 7878fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 7882a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 7892a1deaaaSBruce Evans (u_long)pp->p_fsize, 7902a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 7918fae3551SRodney W. Grimes break; 7928fae3551SRodney W. Grimes 7938fae3551SRodney W. Grimes case FS_BSDFFS: 7942a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 7952a1deaaaSBruce Evans (u_long)pp->p_fsize, 7962a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 7978fae3551SRodney W. Grimes pp->p_cpg); 7988fae3551SRodney W. Grimes break; 7998fae3551SRodney W. Grimes 800ca4693edSJustin T. Gibbs case FS_BSDLFS: 8012a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 8022a1deaaaSBruce Evans (u_long)pp->p_fsize, 8032a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 804ca4693edSJustin T. Gibbs pp->p_cpg); 805ca4693edSJustin T. Gibbs break; 806ca4693edSJustin T. Gibbs 8078fae3551SRodney W. Grimes default: 8088fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 8098fae3551SRodney W. Grimes break; 8108fae3551SRodney W. Grimes } 8112a1deaaaSBruce Evans fprintf(f, "\t# (Cyl. %4lu", 8122a1deaaaSBruce Evans (u_long)(pp->p_offset / lp->d_secpercyl)); 8138fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 8148fae3551SRodney W. Grimes putc('*', f); 8158fae3551SRodney W. Grimes else 8168fae3551SRodney W. Grimes putc(' ', f); 8172a1deaaaSBruce Evans fprintf(f, "- %lu", 8182a1deaaaSBruce Evans (u_long)((pp->p_offset + pp->p_size + 8192a1deaaaSBruce Evans lp->d_secpercyl - 1) / 8202a1deaaaSBruce Evans lp->d_secpercyl - 1)); 8218fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 8228fae3551SRodney W. Grimes putc('*', f); 8238fae3551SRodney W. Grimes fprintf(f, ")\n"); 8248fae3551SRodney W. Grimes } 8258fae3551SRodney W. Grimes } 8268fae3551SRodney W. Grimes fflush(f); 8278fae3551SRodney W. Grimes } 8288fae3551SRodney W. Grimes 82961de51caSJoerg Wunsch int 830326c7cdaSWarner Losh edit(struct disklabel *lp, int f) 8318fae3551SRodney W. Grimes { 832326c7cdaSWarner Losh int c, fd; 8338fae3551SRodney W. Grimes struct disklabel label; 834722ceb3fSWarner Losh FILE *fp; 8358fae3551SRodney W. Grimes 836722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 837722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 8386bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 8398fae3551SRodney W. Grimes return (1); 8408fae3551SRodney W. Grimes } 841722ceb3fSWarner Losh display(fp, lp); 842722ceb3fSWarner Losh fclose(fp); 8438fae3551SRodney W. Grimes for (;;) { 8448fae3551SRodney W. Grimes if (!editit()) 8458fae3551SRodney W. Grimes break; 846722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 847722ceb3fSWarner Losh if (fp == NULL) { 8486bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 8498fae3551SRodney W. Grimes break; 8508fae3551SRodney W. Grimes } 8518fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 852722ceb3fSWarner Losh if (getasciilabel(fp, &label)) { 8538fae3551SRodney W. Grimes *lp = label; 8548fae3551SRodney W. Grimes if (writelabel(f, bootarea, lp) == 0) { 855722ceb3fSWarner Losh fclose(fp); 8568fae3551SRodney W. Grimes (void) unlink(tmpfil); 8578fae3551SRodney W. Grimes return (0); 8588fae3551SRodney W. Grimes } 8598fae3551SRodney W. Grimes } 860722ceb3fSWarner Losh fclose(fp); 8618fae3551SRodney W. Grimes printf("re-edit the label? [y]: "); fflush(stdout); 8628fae3551SRodney W. Grimes c = getchar(); 8638fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 8648fae3551SRodney W. Grimes while (getchar() != (int)'\n') 8658fae3551SRodney W. Grimes ; 8668fae3551SRodney W. Grimes if (c == (int)'n') 8678fae3551SRodney W. Grimes break; 8688fae3551SRodney W. Grimes } 8698fae3551SRodney W. Grimes (void) unlink(tmpfil); 8708fae3551SRodney W. Grimes return (1); 8718fae3551SRodney W. Grimes } 8728fae3551SRodney W. Grimes 87361de51caSJoerg Wunsch int 874326c7cdaSWarner Losh editit(void) 8758fae3551SRodney W. Grimes { 876326c7cdaSWarner Losh int pid, xpid; 877c8223965SMark Murray int locstat, omask; 878c8223965SMark Murray const char *ed; 8798fae3551SRodney W. Grimes 8808fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 8818fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 8828fae3551SRodney W. Grimes if (errno == EPROCLIM) { 8836bd343a9SPhilippe Charnier warnx("you have too many processes"); 8848fae3551SRodney W. Grimes return(0); 8858fae3551SRodney W. Grimes } 8868fae3551SRodney W. Grimes if (errno != EAGAIN) { 8876bd343a9SPhilippe Charnier warn("fork"); 8888fae3551SRodney W. Grimes return(0); 8898fae3551SRodney W. Grimes } 8908fae3551SRodney W. Grimes sleep(1); 8918fae3551SRodney W. Grimes } 8928fae3551SRodney W. Grimes if (pid == 0) { 8938fae3551SRodney W. Grimes sigsetmask(omask); 8948fae3551SRodney W. Grimes setgid(getgid()); 8958fae3551SRodney W. Grimes setuid(getuid()); 8968fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 8978fae3551SRodney W. Grimes ed = DEFEDITOR; 8987bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 8996bd343a9SPhilippe Charnier err(1, "%s", ed); 9008fae3551SRodney W. Grimes } 901c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 9028fae3551SRodney W. Grimes if (xpid == pid) 9038fae3551SRodney W. Grimes break; 9048fae3551SRodney W. Grimes sigsetmask(omask); 905c8223965SMark Murray return(!locstat); 9068fae3551SRodney W. Grimes } 9078fae3551SRodney W. Grimes 9088fae3551SRodney W. Grimes char * 909326c7cdaSWarner Losh skip(char *cp) 9108fae3551SRodney W. Grimes { 9118fae3551SRodney W. Grimes 9128fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 9138fae3551SRodney W. Grimes cp++; 9148fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 915326c7cdaSWarner Losh return (NULL); 9168fae3551SRodney W. Grimes return (cp); 9178fae3551SRodney W. Grimes } 9188fae3551SRodney W. Grimes 9198fae3551SRodney W. Grimes char * 920326c7cdaSWarner Losh word(char *cp) 9218fae3551SRodney W. Grimes { 922326c7cdaSWarner Losh char c; 9238fae3551SRodney W. Grimes 9248fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 9258fae3551SRodney W. Grimes cp++; 9268fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 9278fae3551SRodney W. Grimes *cp++ = '\0'; 9288fae3551SRodney W. Grimes if (c != '#') 9298fae3551SRodney W. Grimes return (skip(cp)); 9308fae3551SRodney W. Grimes } 931326c7cdaSWarner Losh return (NULL); 9328fae3551SRodney W. Grimes } 9338fae3551SRodney W. Grimes 9348fae3551SRodney W. Grimes /* 9358fae3551SRodney W. Grimes * Read an ascii label in from fd f, 9368fae3551SRodney W. Grimes * in the same format as that put out by display(), 9378fae3551SRodney W. Grimes * and fill in lp. 9388fae3551SRodney W. Grimes */ 93961de51caSJoerg Wunsch int 940326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 9418fae3551SRodney W. Grimes { 9426b0ff5f5SPoul-Henning Kamp char *cp; 9436b0ff5f5SPoul-Henning Kamp const char **cpp; 944484c7804SJulian Elischer u_int part; 9456b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 946484c7804SJulian Elischer u_long v; 947484c7804SJulian Elischer int lineno = 0, errors = 0; 948326c7cdaSWarner Losh int i; 9498fae3551SRodney W. Grimes 9508fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 95177068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 9528fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 9538fae3551SRodney W. Grimes lineno++; 95461de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 9558fae3551SRodney W. Grimes *cp = '\0'; 9568fae3551SRodney W. Grimes cp = skip(line); 9578fae3551SRodney W. Grimes if (cp == NULL) 9588fae3551SRodney W. Grimes continue; 9598fae3551SRodney W. Grimes tp = index(cp, ':'); 9608fae3551SRodney W. Grimes if (tp == NULL) { 9618fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 9628fae3551SRodney W. Grimes errors++; 9638fae3551SRodney W. Grimes continue; 9648fae3551SRodney W. Grimes } 9658fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 9668fae3551SRodney W. Grimes if (streq(cp, "type")) { 9678fae3551SRodney W. Grimes if (tp == NULL) 968c8223965SMark Murray tp = unknown; 9698fae3551SRodney W. Grimes cpp = dktypenames; 9708fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 9716b0ff5f5SPoul-Henning Kamp if (*cpp && streq(*cpp, tp)) { 9728fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 97309dbd070SIan Dowse break; 9748fae3551SRodney W. Grimes } 97509dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 97609dbd070SIan Dowse continue; 977484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 978484c7804SJulian Elischer if (v >= DKMAXTYPES) 979484c7804SJulian Elischer fprintf(stderr, "line %d:%s %lu\n", lineno, 9808fae3551SRodney W. Grimes "Warning, unknown disk type", v); 9818fae3551SRodney W. Grimes lp->d_type = v; 9828fae3551SRodney W. Grimes continue; 9838fae3551SRodney W. Grimes } 9848fae3551SRodney W. Grimes if (streq(cp, "flags")) { 9858fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 9868fae3551SRodney W. Grimes tp = word(cp); 9878fae3551SRodney W. Grimes if (streq(cp, "removeable")) 9888fae3551SRodney W. Grimes v |= D_REMOVABLE; 9898fae3551SRodney W. Grimes else if (streq(cp, "ecc")) 9908fae3551SRodney W. Grimes v |= D_ECC; 9918fae3551SRodney W. Grimes else if (streq(cp, "badsect")) 9928fae3551SRodney W. Grimes v |= D_BADSECT; 9938fae3551SRodney W. Grimes else { 9948fae3551SRodney W. Grimes fprintf(stderr, 9958fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 9968fae3551SRodney W. Grimes lineno, cp); 9978fae3551SRodney W. Grimes errors++; 9988fae3551SRodney W. Grimes } 9998fae3551SRodney W. Grimes } 10008fae3551SRodney W. Grimes lp->d_flags = v; 10018fae3551SRodney W. Grimes continue; 10028fae3551SRodney W. Grimes } 10038fae3551SRodney W. Grimes if (streq(cp, "drivedata")) { 10048fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 1005484c7804SJulian Elischer lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 10068fae3551SRodney W. Grimes tp = word(cp); 10078fae3551SRodney W. Grimes } 10088fae3551SRodney W. Grimes continue; 10098fae3551SRodney W. Grimes } 1010484c7804SJulian Elischer if (sscanf(cp, "%lu partitions", &v) == 1) { 1011484c7804SJulian Elischer if (v == 0 || v > MAXPARTITIONS) { 10128fae3551SRodney W. Grimes fprintf(stderr, 10138fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 10148fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 10158fae3551SRodney W. Grimes errors++; 10168fae3551SRodney W. Grimes } else 10178fae3551SRodney W. Grimes lp->d_npartitions = v; 10188fae3551SRodney W. Grimes continue; 10198fae3551SRodney W. Grimes } 10208fae3551SRodney W. Grimes if (tp == NULL) 1021c8223965SMark Murray tp = blank; 10228fae3551SRodney W. Grimes if (streq(cp, "disk")) { 10238fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 10248fae3551SRodney W. Grimes continue; 10258fae3551SRodney W. Grimes } 10268fae3551SRodney W. Grimes if (streq(cp, "label")) { 10278fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 10288fae3551SRodney W. Grimes continue; 10298fae3551SRodney W. Grimes } 10308fae3551SRodney W. Grimes if (streq(cp, "bytes/sector")) { 1031484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1032484c7804SJulian Elischer if (v == 0 || (v % DEV_BSIZE) != 0) { 10338fae3551SRodney W. Grimes fprintf(stderr, 10348fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 10358fae3551SRodney W. Grimes lineno, tp); 10368fae3551SRodney W. Grimes errors++; 10378fae3551SRodney W. Grimes } else 10388fae3551SRodney W. Grimes lp->d_secsize = v; 10398fae3551SRodney W. Grimes continue; 10408fae3551SRodney W. Grimes } 10418fae3551SRodney W. Grimes if (streq(cp, "sectors/track")) { 1042484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1043484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL) 1044484c7804SJulian Elischer if (v == 0 || v > 0xffffffff) { 1045484c7804SJulian Elischer #else 1046484c7804SJulian Elischer if (v == 0) { 1047484c7804SJulian Elischer #endif 10488fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10498fae3551SRodney W. Grimes lineno, tp, cp); 10508fae3551SRodney W. Grimes errors++; 10518fae3551SRodney W. Grimes } else 10528fae3551SRodney W. Grimes lp->d_nsectors = v; 10538fae3551SRodney W. Grimes continue; 10548fae3551SRodney W. Grimes } 10558fae3551SRodney W. Grimes if (streq(cp, "sectors/cylinder")) { 1056484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1057484c7804SJulian Elischer if (v == 0) { 10588fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10598fae3551SRodney W. Grimes lineno, tp, cp); 10608fae3551SRodney W. Grimes errors++; 10618fae3551SRodney W. Grimes } else 10628fae3551SRodney W. Grimes lp->d_secpercyl = v; 10638fae3551SRodney W. Grimes continue; 10648fae3551SRodney W. Grimes } 10658fae3551SRodney W. Grimes if (streq(cp, "tracks/cylinder")) { 1066484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1067484c7804SJulian Elischer if (v == 0) { 10688fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10698fae3551SRodney W. Grimes lineno, tp, cp); 10708fae3551SRodney W. Grimes errors++; 10718fae3551SRodney W. Grimes } else 10728fae3551SRodney W. Grimes lp->d_ntracks = v; 10738fae3551SRodney W. Grimes continue; 10748fae3551SRodney W. Grimes } 10758fae3551SRodney W. Grimes if (streq(cp, "cylinders")) { 1076484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1077484c7804SJulian Elischer if (v == 0) { 10788fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10798fae3551SRodney W. Grimes lineno, tp, cp); 10808fae3551SRodney W. Grimes errors++; 10818fae3551SRodney W. Grimes } else 10828fae3551SRodney W. Grimes lp->d_ncylinders = v; 10838fae3551SRodney W. Grimes continue; 10848fae3551SRodney W. Grimes } 1085f75dd518SBruce Evans if (streq(cp, "sectors/unit")) { 1086484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1087484c7804SJulian Elischer if (v == 0) { 1088f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 1089f75dd518SBruce Evans lineno, tp, cp); 1090f75dd518SBruce Evans errors++; 1091f75dd518SBruce Evans } else 1092f75dd518SBruce Evans lp->d_secperunit = v; 1093f75dd518SBruce Evans continue; 1094f75dd518SBruce Evans } 10958fae3551SRodney W. Grimes if (streq(cp, "rpm")) { 1096484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1097484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 10988fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 10998fae3551SRodney W. Grimes lineno, tp, cp); 11008fae3551SRodney W. Grimes errors++; 11018fae3551SRodney W. Grimes } else 11028fae3551SRodney W. Grimes lp->d_rpm = v; 11038fae3551SRodney W. Grimes continue; 11048fae3551SRodney W. Grimes } 11058fae3551SRodney W. Grimes if (streq(cp, "interleave")) { 1106484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1107484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 11088fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11098fae3551SRodney W. Grimes lineno, tp, cp); 11108fae3551SRodney W. Grimes errors++; 11118fae3551SRodney W. Grimes } else 11128fae3551SRodney W. Grimes lp->d_interleave = v; 11138fae3551SRodney W. Grimes continue; 11148fae3551SRodney W. Grimes } 11158fae3551SRodney W. Grimes if (streq(cp, "trackskew")) { 1116484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1117484c7804SJulian Elischer if (v > USHRT_MAX) { 11188fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11198fae3551SRodney W. Grimes lineno, tp, cp); 11208fae3551SRodney W. Grimes errors++; 11218fae3551SRodney W. Grimes } else 11228fae3551SRodney W. Grimes lp->d_trackskew = v; 11238fae3551SRodney W. Grimes continue; 11248fae3551SRodney W. Grimes } 11258fae3551SRodney W. Grimes if (streq(cp, "cylinderskew")) { 1126484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 1127484c7804SJulian Elischer if (v > USHRT_MAX) { 11288fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 11298fae3551SRodney W. Grimes lineno, tp, cp); 11308fae3551SRodney W. Grimes errors++; 11318fae3551SRodney W. Grimes } else 11328fae3551SRodney W. Grimes lp->d_cylskew = v; 11338fae3551SRodney W. Grimes continue; 11348fae3551SRodney W. Grimes } 11358fae3551SRodney W. Grimes if (streq(cp, "headswitch")) { 1136484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 11378fae3551SRodney W. Grimes lp->d_headswitch = v; 11388fae3551SRodney W. Grimes continue; 11398fae3551SRodney W. Grimes } 11408fae3551SRodney W. Grimes if (streq(cp, "track-to-track seek")) { 1141484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 11428fae3551SRodney W. Grimes lp->d_trkseek = v; 11438fae3551SRodney W. Grimes continue; 11448fae3551SRodney W. Grimes } 11453233afaeSJohn W. De Boskey /* the ':' was removed above */ 114667b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 114767b46708SIan Dowse fprintf(stderr, 114867b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 114967b46708SIan Dowse cp); 115067b46708SIan Dowse errors++; 115167b46708SIan Dowse continue; 115267b46708SIan Dowse } 115367b46708SIan Dowse 115467b46708SIan Dowse /* Process a partition specification line. */ 11553233afaeSJohn W. De Boskey part = *cp - 'a'; 11563233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 11578fae3551SRodney W. Grimes fprintf(stderr, 11583233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 11593233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 11608fae3551SRodney W. Grimes errors++; 11618fae3551SRodney W. Grimes continue; 11628fae3551SRodney W. Grimes } 11633233afaeSJohn W. De Boskey part_set[part] = 1; 116467b46708SIan Dowse 116567b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 116667b46708SIan Dowse errors++; 116767b46708SIan Dowse break; 116867b46708SIan Dowse } 116967b46708SIan Dowse } 117067b46708SIan Dowse errors += checklabel(lp); 117167b46708SIan Dowse return (errors == 0); 117267b46708SIan Dowse } 117367b46708SIan Dowse 117467b46708SIan Dowse #define NXTNUM(n) do { \ 117513e0abcbSPaul Traina if (tp == NULL) { \ 117613e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 117767b46708SIan Dowse return (1); \ 117813e0abcbSPaul Traina } else { \ 11798fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 1180484c7804SJulian Elischer (n) = strtoul(cp, NULL, 10); \ 118113e0abcbSPaul Traina } \ 118267b46708SIan Dowse } while (0) 118367b46708SIan Dowse 11843233afaeSJohn W. De Boskey /* retain 1 character following number */ 118567b46708SIan Dowse #define NXTWORD(w,n) do { \ 11863233afaeSJohn W. De Boskey if (tp == NULL) { \ 11873233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 118867b46708SIan Dowse return (1); \ 11893233afaeSJohn W. De Boskey } else { \ 11903233afaeSJohn W. De Boskey char *tmp; \ 11913233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 1192484c7804SJulian Elischer (n) = strtoul(cp, &tmp, 10); \ 11933233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 11943233afaeSJohn W. De Boskey } \ 119567b46708SIan Dowse } while (0) 119667b46708SIan Dowse 119767b46708SIan Dowse /* 119867b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 119967b46708SIan Dowse * Return 0 on success, 1 on failure. 120067b46708SIan Dowse */ 120167b46708SIan Dowse int 120267b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 120367b46708SIan Dowse { 120467b46708SIan Dowse struct partition *pp; 120567b46708SIan Dowse char *cp; 120667b46708SIan Dowse const char **cpp; 1207484c7804SJulian Elischer u_long v; 120867b46708SIan Dowse 120967b46708SIan Dowse pp = &lp->d_partitions[part]; 121067b46708SIan Dowse cp = NULL; 121167b46708SIan Dowse 12123233afaeSJohn W. De Boskey v = 0; 12133233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 1214484c7804SJulian Elischer if (v == 0 && part_size_type[part] != '*') { 1215484c7804SJulian Elischer fprintf(stderr, 1216484c7804SJulian Elischer "line %d: %s: bad partition size\n", lineno, cp); 121767b46708SIan Dowse return (1); 121867b46708SIan Dowse } 12198fae3551SRodney W. Grimes pp->p_size = v; 12203233afaeSJohn W. De Boskey 12213233afaeSJohn W. De Boskey v = 0; 12223233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 1223484c7804SJulian Elischer if (v == 0 && part_offset_type[part] != '*' && 1224484c7804SJulian Elischer part_offset_type[part] != '\0') { 1225484c7804SJulian Elischer fprintf(stderr, 1226484c7804SJulian Elischer "line %d: %s: bad partition offset\n", lineno, cp); 122767b46708SIan Dowse return (1); 122867b46708SIan Dowse } 12298fae3551SRodney W. Grimes pp->p_offset = v; 12308fae3551SRodney W. Grimes cp = tp, tp = word(cp); 123167b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 123267b46708SIan Dowse if (*cpp && streq(*cpp, cp)) 123367b46708SIan Dowse break; 123467b46708SIan Dowse if (*cpp != NULL) { 123567b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 123667b46708SIan Dowse } else { 12378fae3551SRodney W. Grimes if (isdigit(*cp)) 1238484c7804SJulian Elischer v = strtoul(cp, NULL, 10); 12398fae3551SRodney W. Grimes else 12408fae3551SRodney W. Grimes v = FSMAXTYPES; 1241484c7804SJulian Elischer if (v >= FSMAXTYPES) { 12423233afaeSJohn W. De Boskey fprintf(stderr, 124367b46708SIan Dowse "line %d: Warning, unknown file system type %s\n", 12443233afaeSJohn W. De Boskey lineno, cp); 12458fae3551SRodney W. Grimes v = FS_UNUSED; 12468fae3551SRodney W. Grimes } 12478fae3551SRodney W. Grimes pp->p_fstype = v; 124867b46708SIan Dowse } 124967b46708SIan Dowse 12508fae3551SRodney W. Grimes switch (pp->p_fstype) { 12513233afaeSJohn W. De Boskey case FS_UNUSED: 12523233afaeSJohn W. De Boskey /* 12533233afaeSJohn W. De Boskey * allow us to accept defaults for 12543233afaeSJohn W. De Boskey * fsize/frag/cpg 12553233afaeSJohn W. De Boskey */ 12563233afaeSJohn W. De Boskey if (tp) { 12578fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 12588fae3551SRodney W. Grimes if (pp->p_fsize == 0) 12598fae3551SRodney W. Grimes break; 12608fae3551SRodney W. Grimes NXTNUM(v); 12618fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 12623233afaeSJohn W. De Boskey } 12633233afaeSJohn W. De Boskey /* else default to 0's */ 12648fae3551SRodney W. Grimes break; 12658fae3551SRodney W. Grimes 12663233afaeSJohn W. De Boskey /* These happen to be the same */ 12678fae3551SRodney W. Grimes case FS_BSDFFS: 1268ca4693edSJustin T. Gibbs case FS_BSDLFS: 12693233afaeSJohn W. De Boskey if (tp) { 1270ca4693edSJustin T. Gibbs NXTNUM(pp->p_fsize); 1271ca4693edSJustin T. Gibbs if (pp->p_fsize == 0) 1272ca4693edSJustin T. Gibbs break; 1273ca4693edSJustin T. Gibbs NXTNUM(v); 1274ca4693edSJustin T. Gibbs pp->p_frag = v / pp->p_fsize; 1275ca4693edSJustin T. Gibbs NXTNUM(pp->p_cpg); 12763233afaeSJohn W. De Boskey } else { 12773233afaeSJohn W. De Boskey /* 127867b46708SIan Dowse * FIX! poor attempt at adaptive 12793233afaeSJohn W. De Boskey */ 12803233afaeSJohn W. De Boskey /* 1 GB */ 128167b46708SIan Dowse if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 128267b46708SIan Dowse /* 128367b46708SIan Dowse * FIX! These are too low, but are traditional 128467b46708SIan Dowse */ 1285f1a7b7eeSIan Dowse pp->p_fsize = DEFAULT_NEWFS_FRAG; 1286f1a7b7eeSIan Dowse pp->p_frag = DEFAULT_NEWFS_BLOCK / 1287f1a7b7eeSIan Dowse DEFAULT_NEWFS_FRAG; 12883233afaeSJohn W. De Boskey pp->p_cpg = DEFAULT_NEWFS_CPG; 12893233afaeSJohn W. De Boskey } else { 1290f1a7b7eeSIan Dowse pp->p_fsize = BIG_NEWFS_FRAG; 1291f1a7b7eeSIan Dowse pp->p_frag = BIG_NEWFS_BLOCK / 1292f1a7b7eeSIan Dowse BIG_NEWFS_FRAG; 12933233afaeSJohn W. De Boskey pp->p_cpg = BIG_NEWFS_CPG; 12943233afaeSJohn W. De Boskey } 12953233afaeSJohn W. De Boskey } 12968fae3551SRodney W. Grimes default: 12978fae3551SRodney W. Grimes break; 12988fae3551SRodney W. Grimes } 129967b46708SIan Dowse return (0); 13008fae3551SRodney W. Grimes } 13018fae3551SRodney W. Grimes 13028fae3551SRodney W. Grimes /* 13038fae3551SRodney W. Grimes * Check disklabel for errors and fill in 13048fae3551SRodney W. Grimes * derived fields according to supplied values. 13058fae3551SRodney W. Grimes */ 130661de51caSJoerg Wunsch int 1307326c7cdaSWarner Losh checklabel(struct disklabel *lp) 13088fae3551SRodney W. Grimes { 1309326c7cdaSWarner Losh struct partition *pp; 13108fae3551SRodney W. Grimes int i, errors = 0; 13118fae3551SRodney W. Grimes char part; 1312484c7804SJulian Elischer u_long total_size, total_percent, current_offset; 13133233afaeSJohn W. De Boskey int seen_default_offset; 13143233afaeSJohn W. De Boskey int hog_part; 13153233afaeSJohn W. De Boskey int j; 13163233afaeSJohn W. De Boskey struct partition *pp2; 13178fae3551SRodney W. Grimes 13188fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 13192a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 13208fae3551SRodney W. Grimes return (1); 13218fae3551SRodney W. Grimes } 13228fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 13232a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 13248fae3551SRodney W. Grimes return (1); 13258fae3551SRodney W. Grimes } 13268fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 13272a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 13288fae3551SRodney W. Grimes return (1); 13298fae3551SRodney W. Grimes } 13308fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 13312a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 13328fae3551SRodney W. Grimes errors++; 13338fae3551SRodney W. Grimes } 13348fae3551SRodney W. Grimes if (lp->d_rpm == 0) 13352a1deaaaSBruce Evans Warning("revolutions/minute 0"); 13368fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 13378fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 13388fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 13398fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 13408fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 13412a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 13428fae3551SRodney W. Grimes errors++; 13438fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 13448fae3551SRodney W. Grimes Warning("boot block size %% sector-size != 0"); 13458fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 13462a1deaaaSBruce Evans Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 13472a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 13483233afaeSJohn W. De Boskey 13493233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 13503233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 13513233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 13523233afaeSJohn W. De Boskey hog_part = -1; 13533233afaeSJohn W. De Boskey /* find all fixed partitions */ 13543233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13553233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13563233afaeSJohn W. De Boskey if (part_set[i]) { 13573233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 13586b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 13593233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 13603233afaeSJohn W. De Boskey } else { 13613233afaeSJohn W. De Boskey if (hog_part != -1) 13623233afaeSJohn W. De Boskey Warning("Too many '*' partitions (%c and %c)", 13633233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 13643233afaeSJohn W. De Boskey else 13653233afaeSJohn W. De Boskey hog_part = i; 13663233afaeSJohn W. De Boskey } 13673233afaeSJohn W. De Boskey } else { 13688d3105e8SWarner Losh off_t size; 13693233afaeSJohn W. De Boskey 13703233afaeSJohn W. De Boskey size = pp->p_size; 13713233afaeSJohn W. De Boskey switch (part_size_type[i]) { 13723233afaeSJohn W. De Boskey case '%': 13733233afaeSJohn W. De Boskey total_percent += size; 13743233afaeSJohn W. De Boskey break; 13753233afaeSJohn W. De Boskey case 'k': 13763233afaeSJohn W. De Boskey case 'K': 13778d3105e8SWarner Losh size *= 1024ULL; 13783233afaeSJohn W. De Boskey break; 13793233afaeSJohn W. De Boskey case 'm': 13803233afaeSJohn W. De Boskey case 'M': 13818d3105e8SWarner Losh size *= 1024ULL * 1024ULL; 13823233afaeSJohn W. De Boskey break; 13833233afaeSJohn W. De Boskey case 'g': 13843233afaeSJohn W. De Boskey case 'G': 13858d3105e8SWarner Losh size *= 1024ULL * 1024ULL * 1024ULL; 13863233afaeSJohn W. De Boskey break; 13873233afaeSJohn W. De Boskey case '\0': 13883233afaeSJohn W. De Boskey break; 13893233afaeSJohn W. De Boskey default: 13903233afaeSJohn W. De Boskey Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 13913233afaeSJohn W. De Boskey break; 13923233afaeSJohn W. De Boskey } 13933233afaeSJohn W. De Boskey /* don't count %'s yet */ 13943233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 13953233afaeSJohn W. De Boskey /* 13963233afaeSJohn W. De Boskey * for all not in sectors, convert to 13973233afaeSJohn W. De Boskey * sectors 13983233afaeSJohn W. De Boskey */ 13993233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 14003233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 14013233afaeSJohn W. De Boskey Warning("partition %c not an integer number of sectors", 14023233afaeSJohn W. De Boskey i + 'a'); 14033233afaeSJohn W. De Boskey size /= lp->d_secsize; 14043233afaeSJohn W. De Boskey pp->p_size = size; 14053233afaeSJohn W. De Boskey } 14063233afaeSJohn W. De Boskey /* else already in sectors */ 14076b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 14083233afaeSJohn W. De Boskey total_size += size; 14093233afaeSJohn W. De Boskey } 14103233afaeSJohn W. De Boskey } 14113233afaeSJohn W. De Boskey } 14123233afaeSJohn W. De Boskey } 14133233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 14143233afaeSJohn W. De Boskey if (total_percent != 0) { 14153233afaeSJohn W. De Boskey long free_space = lp->d_secperunit - total_size; 14163233afaeSJohn W. De Boskey if (total_percent > 100) { 14176b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 14183233afaeSJohn W. De Boskey total_percent); 14193233afaeSJohn W. De Boskey errors++; 14203233afaeSJohn W. De Boskey } 14213233afaeSJohn W. De Boskey 14223233afaeSJohn W. De Boskey if (free_space > 0) { 14233233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14243233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14253233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 14263233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 14273233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 14283233afaeSJohn W. De Boskey total_size += pp->p_size; 14293233afaeSJohn W. De Boskey 14303233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 14313233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 14323233afaeSJohn W. De Boskey } 14333233afaeSJohn W. De Boskey } 14343233afaeSJohn W. De Boskey } else { 14353233afaeSJohn W. De Boskey fprintf(stderr, 14366b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 14373233afaeSJohn W. De Boskey free_space); 14383233afaeSJohn W. De Boskey errors++; 14393233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 14403233afaeSJohn W. De Boskey } 14413233afaeSJohn W. De Boskey } 14423233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 14433233afaeSJohn W. De Boskey if (hog_part != -1) { 14443233afaeSJohn W. De Boskey lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 14453233afaeSJohn W. De Boskey total_size = lp->d_secperunit; 14463233afaeSJohn W. De Boskey } 14473233afaeSJohn W. De Boskey 14483233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 14493233afaeSJohn W. De Boskey current_offset = 0; /* in sectors */ 14503233afaeSJohn W. De Boskey seen_default_offset = 0; 14513233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 14523233afaeSJohn W. De Boskey part = 'a' + i; 14533233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 14543233afaeSJohn W. De Boskey if (part_set[i]) { 14553233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 14566b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 14573233afaeSJohn W. De Boskey pp->p_offset = 0; 14583233afaeSJohn W. De Boskey } else { 14593233afaeSJohn W. De Boskey pp->p_offset = current_offset; 14603233afaeSJohn W. De Boskey seen_default_offset = 1; 14613233afaeSJohn W. De Boskey } 14623233afaeSJohn W. De Boskey } else { 14633233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 14643233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 14656b0ff5f5SPoul-Henning Kamp seen_default_offset && i != RAW_PART) { 14663233afaeSJohn W. De Boskey fprintf(stderr, 14676b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 14686b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 14693233afaeSJohn W. De Boskey fprintf(stderr, 14703233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 14713233afaeSJohn W. De Boskey errors++; 14723233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 14736b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 14743233afaeSJohn W. De Boskey /* 14753233afaeSJohn W. De Boskey * this may give unneeded warnings if 14763233afaeSJohn W. De Boskey * partitions are out-of-order 14773233afaeSJohn W. De Boskey */ 14783233afaeSJohn W. De Boskey Warning( 14793233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 14806b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 14813233afaeSJohn W. De Boskey } 14823233afaeSJohn W. De Boskey } 14836b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 14843233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 14853233afaeSJohn W. De Boskey } 14863233afaeSJohn W. De Boskey } 14873233afaeSJohn W. De Boskey 14888fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 14898fae3551SRodney W. Grimes part = 'a' + i; 14908fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 14918fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 14922a1deaaaSBruce Evans Warning("partition %c: size 0, but offset %lu", 14932a1deaaaSBruce Evans part, (u_long)pp->p_offset); 1494fd43aa1cSDavid E. O'Brien #ifdef __sparc64__ 1495fd43aa1cSDavid E. O'Brien /* See comment in writelabel(). */ 1496fd43aa1cSDavid E. O'Brien if (pp->p_offset % lp->d_secpercyl != 0) { 1497fd43aa1cSDavid E. O'Brien fprintf(stderr, "partition %c: does not start on a " 1498fd43aa1cSDavid E. O'Brien "cylinder boundary!\n", part); 1499fd43aa1cSDavid E. O'Brien errors++; 1500fd43aa1cSDavid E. O'Brien } 1501fd43aa1cSDavid E. O'Brien #endif 15028fae3551SRodney W. Grimes #ifdef notdef 15038fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 15048fae3551SRodney W. Grimes Warning("partition %c: size %% cylinder-size != 0", 15058fae3551SRodney W. Grimes part); 15068fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 15078fae3551SRodney W. Grimes Warning("partition %c: offset %% cylinder-size != 0", 15088fae3551SRodney W. Grimes part); 15098fae3551SRodney W. Grimes #endif 15108fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 15118fae3551SRodney W. Grimes fprintf(stderr, 15128fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 15138fae3551SRodney W. Grimes errors++; 15148fae3551SRodney W. Grimes } 15158fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 15168fae3551SRodney W. Grimes fprintf(stderr, 15178fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 15188fae3551SRodney W. Grimes part); 15198fae3551SRodney W. Grimes errors++; 15208fae3551SRodney W. Grimes } 15216b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) 15223233afaeSJohn W. De Boskey { 15233233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 15243233afaeSJohn W. De Boskey Warning("partition %c is not marked as unused!",part); 15253233afaeSJohn W. De Boskey if (pp->p_offset != 0) 15263233afaeSJohn W. De Boskey Warning("partition %c doesn't start at 0!",part); 15273233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 15283233afaeSJohn W. De Boskey Warning("partition %c doesn't cover the whole unit!",part); 15293233afaeSJohn W. De Boskey 15303233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 15313233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 15323233afaeSJohn W. De Boskey Warning("An incorrect partition %c may cause problems for " 15333233afaeSJohn W. De Boskey "standard system utilities",part); 15343233afaeSJohn W. De Boskey } 15353233afaeSJohn W. De Boskey } 15363233afaeSJohn W. De Boskey 15373233afaeSJohn W. De Boskey /* check for overlaps */ 15383233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 15393233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 15406b0ff5f5SPoul-Henning Kamp if (j != RAW_PART && i != RAW_PART && 15413233afaeSJohn W. De Boskey part_set[i] && part_set[j]) { 15423233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 15433233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 15443233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 15453233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 15463233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 15473233afaeSJohn W. De Boskey j + 'a', i + 'a'); 15483233afaeSJohn W. De Boskey errors++; 15493233afaeSJohn W. De Boskey } 15503233afaeSJohn W. De Boskey } 15513233afaeSJohn W. De Boskey } 15528fae3551SRodney W. Grimes } 15538fae3551SRodney W. Grimes for (; i < MAXPARTITIONS; i++) { 15548fae3551SRodney W. Grimes part = 'a' + i; 15558fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 15568fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 15572a1deaaaSBruce Evans Warning("unused partition %c: size %d offset %lu", 15582a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 15598fae3551SRodney W. Grimes } 15608fae3551SRodney W. Grimes return (errors); 15618fae3551SRodney W. Grimes } 15628fae3551SRodney W. Grimes 15638fae3551SRodney W. Grimes /* 1564425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1565425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1566425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1567425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1568425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1569425bed3aSJoerg Wunsch * 1570425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1571425bed3aSJoerg Wunsch */ 1572425bed3aSJoerg Wunsch struct disklabel * 1573425bed3aSJoerg Wunsch getvirginlabel(void) 1574425bed3aSJoerg Wunsch { 1575c8223965SMark Murray static struct disklabel loclab; 1576b9d05a16SPoul-Henning Kamp struct partition *dp; 1577c8223965SMark Murray char lnamebuf[BBSIZE]; 1578425bed3aSJoerg Wunsch int f; 1579b9d05a16SPoul-Henning Kamp u_int secsize, u; 1580b9d05a16SPoul-Henning Kamp off_t mediasize; 1581425bed3aSJoerg Wunsch 1582425bed3aSJoerg Wunsch if (dkname[0] == '/') { 1583e18fb238SBruce Evans warnx("\"auto\" requires the usage of a canonical disk name"); 158443be698cSBruce Evans return (NULL); 1585425bed3aSJoerg Wunsch } 1586c8223965SMark Murray (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1587c8223965SMark Murray if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1588c8223965SMark Murray warn("cannot open %s", lnamebuf); 158943be698cSBruce Evans return (NULL); 1590425bed3aSJoerg Wunsch } 1591ff7d5162SJordan K. Hubbard 1592b9d05a16SPoul-Henning Kamp /* New world order */ 1593b9d05a16SPoul-Henning Kamp if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1594b9d05a16SPoul-Henning Kamp (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1595425bed3aSJoerg Wunsch close (f); 159643be698cSBruce Evans return (NULL); 1597425bed3aSJoerg Wunsch } 1598b9d05a16SPoul-Henning Kamp memset(&loclab, 0, sizeof loclab); 1599b9d05a16SPoul-Henning Kamp loclab.d_magic = DISKMAGIC; 1600b9d05a16SPoul-Henning Kamp loclab.d_magic2 = DISKMAGIC; 1601b9d05a16SPoul-Henning Kamp loclab.d_secsize = secsize; 1602b9d05a16SPoul-Henning Kamp loclab.d_secperunit = mediasize / secsize; 1603b9d05a16SPoul-Henning Kamp 1604b9d05a16SPoul-Henning Kamp /* 1605b9d05a16SPoul-Henning Kamp * Nobody in these enligthened days uses the CHS geometry for 1606b9d05a16SPoul-Henning Kamp * anything, but nontheless try to get it right. If we fail 1607b9d05a16SPoul-Henning Kamp * to get any good ideas from the device, construct something 1608b9d05a16SPoul-Henning Kamp * which is IBM-PC friendly. 1609b9d05a16SPoul-Henning Kamp */ 1610b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1611b9d05a16SPoul-Henning Kamp loclab.d_nsectors = u; 1612b9d05a16SPoul-Henning Kamp else 1613b9d05a16SPoul-Henning Kamp loclab.d_nsectors = 63; 1614b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1615b9d05a16SPoul-Henning Kamp loclab.d_ntracks = u; 1616b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*1*1024) 1617b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 1; 1618b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*16*1024) 1619b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 16; 1620b9d05a16SPoul-Henning Kamp else 1621b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 255; 1622b9d05a16SPoul-Henning Kamp loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1623b9d05a16SPoul-Henning Kamp loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1624b9d05a16SPoul-Henning Kamp loclab.d_npartitions = MAXPARTITIONS; 1625b9d05a16SPoul-Henning Kamp 1626b9d05a16SPoul-Henning Kamp /* Various (unneeded) compat stuff */ 1627b9d05a16SPoul-Henning Kamp loclab.d_rpm = 3600; 1628b9d05a16SPoul-Henning Kamp loclab.d_bbsize = BBSIZE; 1629b9d05a16SPoul-Henning Kamp loclab.d_interleave = 1;; 1630b9d05a16SPoul-Henning Kamp strncpy(loclab.d_typename, "amnesiac", 1631b9d05a16SPoul-Henning Kamp sizeof(loclab.d_typename)); 1632b9d05a16SPoul-Henning Kamp 1633b9d05a16SPoul-Henning Kamp dp = &loclab.d_partitions[RAW_PART]; 1634b9d05a16SPoul-Henning Kamp dp->p_size = loclab.d_secperunit; 1635b9d05a16SPoul-Henning Kamp loclab.d_checksum = dkcksum(&loclab); 1636425bed3aSJoerg Wunsch close (f); 1637c8223965SMark Murray return (&loclab); 1638425bed3aSJoerg Wunsch } 1639425bed3aSJoerg Wunsch 1640425bed3aSJoerg Wunsch /* 16418fae3551SRodney W. Grimes * If we are installing a boot program that doesn't fit in d_bbsize 16428fae3551SRodney W. Grimes * we need to mark those partitions that the boot overflows into. 16438fae3551SRodney W. Grimes * This allows newfs to prevent creation of a file system where it might 16448fae3551SRodney W. Grimes * clobber bootstrap code. 16458fae3551SRodney W. Grimes */ 164661de51caSJoerg Wunsch void 1647326c7cdaSWarner Losh setbootflag(struct disklabel *lp) 16488fae3551SRodney W. Grimes { 1649326c7cdaSWarner Losh struct partition *pp; 16508fae3551SRodney W. Grimes int i, errors = 0; 16518fae3551SRodney W. Grimes char part; 16528fae3551SRodney W. Grimes u_long boffset; 16538fae3551SRodney W. Grimes 16548fae3551SRodney W. Grimes if (bootbuf == 0) 16558fae3551SRodney W. Grimes return; 16568fae3551SRodney W. Grimes boffset = bootsize / lp->d_secsize; 16578fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 16588fae3551SRodney W. Grimes part = 'a' + i; 16598fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 16608fae3551SRodney W. Grimes if (pp->p_size == 0) 16618fae3551SRodney W. Grimes continue; 16628fae3551SRodney W. Grimes if (boffset <= pp->p_offset) { 16638fae3551SRodney W. Grimes if (pp->p_fstype == FS_BOOT) 16648fae3551SRodney W. Grimes pp->p_fstype = FS_UNUSED; 16658fae3551SRodney W. Grimes } else if (pp->p_fstype != FS_BOOT) { 16668fae3551SRodney W. Grimes if (pp->p_fstype != FS_UNUSED) { 16678fae3551SRodney W. Grimes fprintf(stderr, 16688fae3551SRodney W. Grimes "boot overlaps used partition %c\n", 16698fae3551SRodney W. Grimes part); 16708fae3551SRodney W. Grimes errors++; 16718fae3551SRodney W. Grimes } else { 16728fae3551SRodney W. Grimes pp->p_fstype = FS_BOOT; 16738fae3551SRodney W. Grimes Warning("boot overlaps partition %c, %s", 16748fae3551SRodney W. Grimes part, "marked as FS_BOOT"); 16758fae3551SRodney W. Grimes } 16768fae3551SRodney W. Grimes } 16778fae3551SRodney W. Grimes } 16786bd343a9SPhilippe Charnier if (errors) 16796bd343a9SPhilippe Charnier errx(4, "cannot install boot program"); 16808fae3551SRodney W. Grimes } 16818fae3551SRodney W. Grimes 16828fae3551SRodney W. Grimes /*VARARGS1*/ 168361de51caSJoerg Wunsch void 1684326c7cdaSWarner Losh Warning(const char *fmt, ...) 16858fae3551SRodney W. Grimes { 168661de51caSJoerg Wunsch va_list ap; 16878fae3551SRodney W. Grimes 16888fae3551SRodney W. Grimes fprintf(stderr, "Warning, "); 168961de51caSJoerg Wunsch va_start(ap, fmt); 169061de51caSJoerg Wunsch vfprintf(stderr, fmt, ap); 16918fae3551SRodney W. Grimes fprintf(stderr, "\n"); 169261de51caSJoerg Wunsch va_end(ap); 16938fae3551SRodney W. Grimes } 16948fae3551SRodney W. Grimes 169561de51caSJoerg Wunsch void 1696326c7cdaSWarner Losh usage(void) 16978fae3551SRodney W. Grimes { 16988fae3551SRodney W. Grimes #if NUMBOOT > 0 1699bef2080aSPhilippe 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", 17008fae3551SRodney W. Grimes "usage: disklabel [-r] disk", 1701bef2080aSPhilippe Charnier "\t\t(to read label)", 17023233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1703bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 17043233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1705bef2080aSPhilippe Charnier "\t\t(to edit label)", 17063233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1707bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 17088fae3551SRodney W. Grimes #if NUMBOOT > 1 17093233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1710bef2080aSPhilippe Charnier "\t\t(to install boot program with existing label)", 17113233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1712bef2080aSPhilippe Charnier "\t\t(to write label and boot program)", 17133233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1714bef2080aSPhilippe Charnier "\t\t(to restore label and boot program)", 17158fae3551SRodney W. Grimes #else 17163233afaeSJohn W. De Boskey " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1717bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 17183233afaeSJohn W. De Boskey " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1719bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 17203233afaeSJohn W. De Boskey " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 1721bef2080aSPhilippe Charnier "\t\t(to restore label and install boot program)", 17228fae3551SRodney W. Grimes #endif 1723bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1724bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17258fae3551SRodney W. Grimes #else 1726bef2080aSPhilippe Charnier fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 17278fae3551SRodney W. Grimes "usage: disklabel [-r] disk", "(to read label)", 17283233afaeSJohn W. De Boskey " disklabel -w [-r] [-n] disk type [ packid ]", 1729bef2080aSPhilippe Charnier "\t\t(to write label)", 17303233afaeSJohn W. De Boskey " disklabel -e [-r] [-n] disk", 1731bef2080aSPhilippe Charnier "\t\t(to edit label)", 17323233afaeSJohn W. De Boskey " disklabel -R [-r] [-n] disk protofile", 1733bef2080aSPhilippe Charnier "\t\t(to restore label)", 1734bef2080aSPhilippe Charnier " disklabel [-NW] disk", 1735bef2080aSPhilippe Charnier "\t\t(to write disable/enable label)"); 17368fae3551SRodney W. Grimes #endif 17378fae3551SRodney W. Grimes exit(1); 17388fae3551SRodney W. Grimes } 1739