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 437c4d80f2SDavid E. O'Brien #if 0 448fae3551SRodney W. Grimes #ifndef lint 456bd343a9SPhilippe Charnier static const char copyright[] = 468fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\ 478fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 488fae3551SRodney W. Grimes #endif /* not lint */ 498fae3551SRodney W. Grimes 508fae3551SRodney W. Grimes #ifndef lint 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"; */ 538fae3551SRodney W. Grimes #endif /* not lint */ 547c4d80f2SDavid E. O'Brien #endif 55c8223965SMark Murray #include <sys/cdefs.h> 56c8223965SMark Murray __FBSDID("$FreeBSD$"); 57c8223965SMark Murray 588fae3551SRodney W. Grimes #include <sys/param.h> 59dfbc3f0cSPoul-Henning Kamp #include <stdint.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 66d5718812SMarcel Moolenaar #define MAXPARTITIONS 26 678fae3551SRodney W. Grimes #include <sys/disklabel.h> 68bc33ea1aSRuslan Ermilov 698fae3551SRodney W. Grimes #include <unistd.h> 708fae3551SRodney W. Grimes #include <string.h> 718fae3551SRodney W. Grimes #include <stdio.h> 725daa806dSPoul-Henning Kamp #include <libgeom.h> 7361de51caSJoerg Wunsch #include <stdlib.h> 7461de51caSJoerg Wunsch #include <signal.h> 7561de51caSJoerg Wunsch #include <stdarg.h> 768fae3551SRodney W. Grimes #include <ctype.h> 77bef2080aSPhilippe Charnier #include <err.h> 7837736675SWarner Losh #include <errno.h> 79c8223965SMark Murray 808fae3551SRodney W. Grimes #include "pathnames.h" 818fae3551SRodney W. Grimes 82d2fe97c7SPoul-Henning Kamp static void makelabel(const char *, struct disklabel *); 83d2fe97c7SPoul-Henning Kamp static int writelabel(void); 84d2fe97c7SPoul-Henning Kamp static int readlabel(int flag); 85d2fe97c7SPoul-Henning Kamp static void display(FILE *, const struct disklabel *); 86d2fe97c7SPoul-Henning Kamp static int edit(void); 87d2fe97c7SPoul-Henning Kamp static int editit(void); 88427823d5SPoul-Henning Kamp static void fixlabel(struct disklabel *); 89d2fe97c7SPoul-Henning Kamp static char *skip(char *); 90d2fe97c7SPoul-Henning Kamp static char *word(char *); 91d2fe97c7SPoul-Henning Kamp static int getasciilabel(FILE *, struct disklabel *); 92d2fe97c7SPoul-Henning Kamp static int getasciipartspec(char *, struct disklabel *, int, int); 93d2fe97c7SPoul-Henning Kamp static int checklabel(struct disklabel *); 94d2fe97c7SPoul-Henning Kamp static void usage(void); 95d2fe97c7SPoul-Henning Kamp static struct disklabel *getvirginlabel(void); 9661de51caSJoerg Wunsch 978fae3551SRodney W. Grimes #define DEFEDITOR _PATH_VI 98d5718812SMarcel Moolenaar #define DEFPARTITIONS 8 998fae3551SRodney W. Grimes 100d2fe97c7SPoul-Henning Kamp static char *specname; 1017a4b0bb2SUlf Lilleengen static char *pname; 102d2fe97c7SPoul-Henning Kamp static char tmpfil[] = PATH_TMPFILE; 1038fae3551SRodney W. Grimes 104d2fe97c7SPoul-Henning Kamp static struct disklabel lab; 105d2fe97c7SPoul-Henning Kamp static u_char bootarea[BBSIZE]; 10657dfbec5SPoul-Henning Kamp static off_t mediasize; 10757dfbec5SPoul-Henning Kamp static u_int secsize; 108d2fe97c7SPoul-Henning Kamp static char blank[] = ""; 109d2fe97c7SPoul-Henning Kamp static char unknown[] = "unknown"; 1108fae3551SRodney W. Grimes 1113233afaeSJohn W. De Boskey #define MAX_PART ('z') 1123233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 113d2fe97c7SPoul-Henning Kamp static char part_size_type[MAX_NUM_PARTS]; 114d2fe97c7SPoul-Henning Kamp static char part_offset_type[MAX_NUM_PARTS]; 115d2fe97c7SPoul-Henning Kamp static int part_set[MAX_NUM_PARTS]; 1163233afaeSJohn W. De Boskey 117d2fe97c7SPoul-Henning Kamp static int installboot; /* non-zero if we should install a boot program */ 11857dfbec5SPoul-Henning Kamp static int allfields; /* present all fields in edit */ 119d2fe97c7SPoul-Henning Kamp static char const *xxboot; /* primary boot */ 1208fae3551SRodney W. Grimes 121eb0ea23eSMarcel Moolenaar static uint32_t lba_offset; 1229ef521ecSPoul-Henning Kamp #ifndef LABELSECTOR 1239ef521ecSPoul-Henning Kamp #define LABELSECTOR -1 1249ef521ecSPoul-Henning Kamp #endif 1259ef521ecSPoul-Henning Kamp #ifndef LABELOFFSET 1269ef521ecSPoul-Henning Kamp #define LABELOFFSET -1 1279ef521ecSPoul-Henning Kamp #endif 128b2bb9f9bSPoul-Henning Kamp static int labelsoffset = LABELSECTOR; 129b2bb9f9bSPoul-Henning Kamp static int labeloffset = LABELOFFSET; 1307838fd0eSPoul-Henning Kamp static int bbsize = BBSIZE; 1317838fd0eSPoul-Henning Kamp static int alphacksum = 1327838fd0eSPoul-Henning Kamp #if defined(__alpha__) 1337838fd0eSPoul-Henning Kamp 1; 1347838fd0eSPoul-Henning Kamp #else 1357838fd0eSPoul-Henning Kamp 0; 1367838fd0eSPoul-Henning Kamp #endif 137c80f9755SPoul-Henning Kamp 1388fae3551SRodney W. Grimes enum { 139f080d33bSPoul-Henning Kamp UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT 1408fae3551SRodney W. Grimes } op = UNSPEC; 1418fae3551SRodney W. Grimes 142bc33ea1aSRuslan Ermilov 143d2fe97c7SPoul-Henning Kamp static int disable_write; /* set to disable writing to disk label */ 1447747c959SLuigi Rizzo static int is_file; /* work on a file (abs. pathname), "-f" opt. */ 1458fae3551SRodney W. Grimes 14661de51caSJoerg Wunsch int 147326c7cdaSWarner Losh main(int argc, char *argv[]) 1488fae3551SRodney W. Grimes { 1498fae3551SRodney W. Grimes FILE *t; 1507a4b0bb2SUlf Lilleengen int ch, error, fd; 1517a4b0bb2SUlf Lilleengen const char *name; 1527a4b0bb2SUlf Lilleengen 1537a4b0bb2SUlf Lilleengen error = 0; 1547a4b0bb2SUlf Lilleengen name = NULL; 1558fae3551SRodney W. Grimes 156f72bbf97SMaxim Konovalov while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1) 1578fae3551SRodney W. Grimes switch (ch) { 15857dfbec5SPoul-Henning Kamp case 'A': 15957dfbec5SPoul-Henning Kamp allfields = 1; 16057dfbec5SPoul-Henning Kamp break; 1618fae3551SRodney W. Grimes case 'B': 1628fae3551SRodney W. Grimes ++installboot; 1638fae3551SRodney W. Grimes break; 1648fae3551SRodney W. Grimes case 'b': 1658fae3551SRodney W. Grimes xxboot = optarg; 1668fae3551SRodney W. Grimes break; 1677747c959SLuigi Rizzo case 'f': 1687747c959SLuigi Rizzo is_file=1; 1697747c959SLuigi Rizzo break; 170bc33ea1aSRuslan Ermilov case 'm': 171246300f2SPoul-Henning Kamp if (!strcmp(optarg, "i386") || 172246300f2SPoul-Henning Kamp !strcmp(optarg, "amd64") || 173246300f2SPoul-Henning Kamp !strcmp(optarg, "ia64") || 174246300f2SPoul-Henning Kamp !strcmp(optarg, "pc98")) { 175b2bb9f9bSPoul-Henning Kamp labelsoffset = 1; 176b2bb9f9bSPoul-Henning Kamp labeloffset = 0; 1773ecb3802SPoul-Henning Kamp bbsize = 8192; 1783ecb3802SPoul-Henning Kamp alphacksum = 0; 179c80f9755SPoul-Henning Kamp } else if (!strcmp(optarg, "alpha")) { 180b2bb9f9bSPoul-Henning Kamp labelsoffset = 0; 181c80f9755SPoul-Henning Kamp labeloffset = 64; 182c80f9755SPoul-Henning Kamp bbsize = 8192; 183c80f9755SPoul-Henning Kamp alphacksum = 1; 184d2fe97c7SPoul-Henning Kamp } else { 185d2fe97c7SPoul-Henning Kamp errx(1, "Unsupported architecture"); 186c80f9755SPoul-Henning Kamp } 187bc33ea1aSRuslan Ermilov break; 1883233afaeSJohn W. De Boskey case 'n': 1893233afaeSJohn W. De Boskey disable_write = 1; 1903233afaeSJohn W. De Boskey break; 1918fae3551SRodney W. Grimes case 'R': 1928fae3551SRodney W. Grimes if (op != UNSPEC) 1938fae3551SRodney W. Grimes usage(); 1948fae3551SRodney W. Grimes op = RESTORE; 1958fae3551SRodney W. Grimes break; 1968fae3551SRodney W. Grimes case 'e': 1978fae3551SRodney W. Grimes if (op != UNSPEC) 1988fae3551SRodney W. Grimes usage(); 1998fae3551SRodney W. Grimes op = EDIT; 2008fae3551SRodney W. Grimes break; 2018fae3551SRodney W. Grimes case 'r': 202d2fe97c7SPoul-Henning Kamp /* 203d2fe97c7SPoul-Henning Kamp * We accept and ignode -r for compatibility with 204d2fe97c7SPoul-Henning Kamp * historically disklabel usage. 205d2fe97c7SPoul-Henning Kamp */ 2068fae3551SRodney W. Grimes break; 2078fae3551SRodney W. Grimes case 'w': 2088fae3551SRodney W. Grimes if (op != UNSPEC) 2098fae3551SRodney W. Grimes usage(); 2108fae3551SRodney W. Grimes op = WRITE; 2118fae3551SRodney W. Grimes break; 2128fae3551SRodney W. Grimes case '?': 2138fae3551SRodney W. Grimes default: 2148fae3551SRodney W. Grimes usage(); 2158fae3551SRodney W. Grimes } 2168fae3551SRodney W. Grimes argc -= optind; 2178fae3551SRodney W. Grimes argv += optind; 218d2fe97c7SPoul-Henning Kamp 2198fae3551SRodney W. Grimes if (argc < 1) 2208fae3551SRodney W. Grimes usage(); 2219ef521ecSPoul-Henning Kamp if (labelsoffset < 0 || labeloffset < 0) 2229ef521ecSPoul-Henning Kamp errx(1, "a -m <architecture> option must be specified"); 2238fae3551SRodney W. Grimes 2245daa806dSPoul-Henning Kamp /* Figure out the names of the thing we're working on */ 2257747c959SLuigi Rizzo if (is_file) { 226ccdd2fceSRalf S. Engelschall specname = argv[0]; 2275daa806dSPoul-Henning Kamp } else { 2287a4b0bb2SUlf Lilleengen specname = g_device_path(argv[0]); 2297a4b0bb2SUlf Lilleengen if (specname == NULL) { 2307a4b0bb2SUlf Lilleengen warn("unable to get correct path for %s", argv[0]); 2317a4b0bb2SUlf Lilleengen return(1); 2327a4b0bb2SUlf Lilleengen } 2337a4b0bb2SUlf Lilleengen fd = open(specname, O_RDONLY); 2347a4b0bb2SUlf Lilleengen if (fd < 0) { 2357a4b0bb2SUlf Lilleengen warn("error opening %s", specname); 2367a4b0bb2SUlf Lilleengen return(1); 2377a4b0bb2SUlf Lilleengen } 2387a4b0bb2SUlf Lilleengen pname = g_providername(fd); 2397a4b0bb2SUlf Lilleengen if (pname == NULL) { 240bd5add58SUlf Lilleengen warn("error getting providername for %s", specname); 2419d8ce078SUlf Lilleengen close(fd); 2427a4b0bb2SUlf Lilleengen return(1); 2437a4b0bb2SUlf Lilleengen } 2449d8ce078SUlf Lilleengen close(fd); 2458fae3551SRodney W. Grimes } 246d2fe97c7SPoul-Henning Kamp 247d2fe97c7SPoul-Henning Kamp if (installboot && op == UNSPEC) 248d2fe97c7SPoul-Henning Kamp op = WRITEBOOT; 249d2fe97c7SPoul-Henning Kamp else if (op == UNSPEC) 250d2fe97c7SPoul-Henning Kamp op = READ; 2518fae3551SRodney W. Grimes 2528fae3551SRodney W. Grimes switch(op) { 2538fae3551SRodney W. Grimes 2543b3038a6SBill Fumerola case UNSPEC: 2553b3038a6SBill Fumerola break; 2563b3038a6SBill Fumerola 2578fae3551SRodney W. Grimes case EDIT: 2588fae3551SRodney W. Grimes if (argc != 1) 2598fae3551SRodney W. Grimes usage(); 260d2fe97c7SPoul-Henning Kamp readlabel(1); 261427823d5SPoul-Henning Kamp fixlabel(&lab); 262d2fe97c7SPoul-Henning Kamp error = edit(); 2638fae3551SRodney W. Grimes break; 2648fae3551SRodney W. Grimes 2658fae3551SRodney W. Grimes case READ: 2668fae3551SRodney W. Grimes if (argc != 1) 2678fae3551SRodney W. Grimes usage(); 268d2fe97c7SPoul-Henning Kamp readlabel(1); 269d2fe97c7SPoul-Henning Kamp display(stdout, NULL); 270d2fe97c7SPoul-Henning Kamp error = checklabel(NULL); 2718fae3551SRodney W. Grimes break; 2728fae3551SRodney W. Grimes 2738fae3551SRodney W. Grimes case RESTORE: 2748fae3551SRodney W. Grimes if (argc != 2) 2758fae3551SRodney W. Grimes usage(); 2768fae3551SRodney W. Grimes if (!(t = fopen(argv[1], "r"))) 2775daa806dSPoul-Henning Kamp err(4, "fopen %s", argv[1]); 278d2fe97c7SPoul-Henning Kamp readlabel(0); 2796cabb348SBruce Evans if (!getasciilabel(t, &lab)) 2806cabb348SBruce Evans exit(1); 281d2fe97c7SPoul-Henning Kamp error = writelabel(); 2828fae3551SRodney W. Grimes break; 2838fae3551SRodney W. Grimes 2848fae3551SRodney W. Grimes case WRITE: 285d2fe97c7SPoul-Henning Kamp if (argc == 2) 286d2fe97c7SPoul-Henning Kamp name = argv[1]; 287d2fe97c7SPoul-Henning Kamp else if (argc == 1) 288d2fe97c7SPoul-Henning Kamp name = "auto"; 289d2fe97c7SPoul-Henning Kamp else 2908fae3551SRodney W. Grimes usage(); 291d2fe97c7SPoul-Henning Kamp readlabel(0); 292d2fe97c7SPoul-Henning Kamp makelabel(name, &lab); 293427823d5SPoul-Henning Kamp fixlabel(&lab); 294d2fe97c7SPoul-Henning Kamp if (checklabel(NULL) == 0) 295d2fe97c7SPoul-Henning Kamp error = writelabel(); 2968fae3551SRodney W. Grimes break; 2978fae3551SRodney W. Grimes 2988fae3551SRodney W. Grimes case WRITEBOOT: 2998fae3551SRodney W. Grimes 300d2fe97c7SPoul-Henning Kamp readlabel(1); 301427823d5SPoul-Henning Kamp fixlabel(&lab); 3028fae3551SRodney W. Grimes if (argc == 2) 303d2fe97c7SPoul-Henning Kamp makelabel(argv[1], &lab); 304d2fe97c7SPoul-Henning Kamp if (checklabel(NULL) == 0) 305d2fe97c7SPoul-Henning Kamp error = writelabel(); 3068fae3551SRodney W. Grimes break; 3078fae3551SRodney W. Grimes } 3088fae3551SRodney W. Grimes exit(error); 3098fae3551SRodney W. Grimes } 3108fae3551SRodney W. Grimes 311427823d5SPoul-Henning Kamp static void 312427823d5SPoul-Henning Kamp fixlabel(struct disklabel *lp) 313427823d5SPoul-Henning Kamp { 314427823d5SPoul-Henning Kamp struct partition *dp; 315427823d5SPoul-Henning Kamp int i; 316427823d5SPoul-Henning Kamp 317d5718812SMarcel Moolenaar for (i = 0; i < lp->d_npartitions; i++) { 318427823d5SPoul-Henning Kamp if (i == RAW_PART) 319427823d5SPoul-Henning Kamp continue; 320427823d5SPoul-Henning Kamp if (lp->d_partitions[i].p_size) 321427823d5SPoul-Henning Kamp return; 322427823d5SPoul-Henning Kamp } 323427823d5SPoul-Henning Kamp 324427823d5SPoul-Henning Kamp dp = &lp->d_partitions[0]; 325427823d5SPoul-Henning Kamp dp->p_offset = BBSIZE / secsize; 326427823d5SPoul-Henning Kamp dp->p_size = lp->d_secperunit - dp->p_offset; 327427823d5SPoul-Henning Kamp } 328427823d5SPoul-Henning Kamp 3298fae3551SRodney W. Grimes /* 330ef9ab0b3SRuslan Ermilov * Construct a prototype disklabel from /etc/disktab. 3318fae3551SRodney W. Grimes */ 332d2fe97c7SPoul-Henning Kamp static void 333d2fe97c7SPoul-Henning Kamp makelabel(const char *type, struct disklabel *lp) 3348fae3551SRodney W. Grimes { 335326c7cdaSWarner Losh struct disklabel *dp; 336425bed3aSJoerg Wunsch 337425bed3aSJoerg Wunsch if (strcmp(type, "auto") == 0) 338425bed3aSJoerg Wunsch dp = getvirginlabel(); 339425bed3aSJoerg Wunsch else 3408fae3551SRodney W. Grimes dp = getdiskbyname(type); 3416bd343a9SPhilippe Charnier if (dp == NULL) 3426bd343a9SPhilippe Charnier errx(1, "%s: unknown disk type", type); 3438fae3551SRodney W. Grimes *lp = *dp; 3448fae3551SRodney W. Grimes bzero(lp->d_packname, sizeof(lp->d_packname)); 3458fae3551SRodney W. Grimes } 3468fae3551SRodney W. Grimes 347d2fe97c7SPoul-Henning Kamp static void 348d2fe97c7SPoul-Henning Kamp readboot(void) 349d2fe97c7SPoul-Henning Kamp { 350d2fe97c7SPoul-Henning Kamp int fd, i; 351d2fe97c7SPoul-Henning Kamp struct stat st; 352c8785325SJohn Baldwin uint64_t *p; 353d2fe97c7SPoul-Henning Kamp 354d2fe97c7SPoul-Henning Kamp if (xxboot == NULL) 355d2fe97c7SPoul-Henning Kamp xxboot = "/boot/boot"; 356d2fe97c7SPoul-Henning Kamp fd = open(xxboot, O_RDONLY); 357d2fe97c7SPoul-Henning Kamp if (fd < 0) 358d2fe97c7SPoul-Henning Kamp err(1, "cannot open %s", xxboot); 359d2fe97c7SPoul-Henning Kamp fstat(fd, &st); 36054f445fbSPoul-Henning Kamp if (alphacksum && st.st_size <= BBSIZE - 512) { 36154f445fbSPoul-Henning Kamp i = read(fd, bootarea + 512, st.st_size); 36254f445fbSPoul-Henning Kamp if (i != st.st_size) 363d2fe97c7SPoul-Henning Kamp err(1, "read error %s", xxboot); 364c8785325SJohn Baldwin 365c8785325SJohn Baldwin /* 366c8785325SJohn Baldwin * Set the location and length so SRM can find the 367c8785325SJohn Baldwin * boot blocks. 368c8785325SJohn Baldwin */ 369c8785325SJohn Baldwin p = (uint64_t *)bootarea; 370c8785325SJohn Baldwin p[60] = (st.st_size + secsize - 1) / secsize; 371c8785325SJohn Baldwin p[61] = 1; 372c8785325SJohn Baldwin p[62] = 0; 373d2fe97c7SPoul-Henning Kamp return; 37454f445fbSPoul-Henning Kamp } else if ((!alphacksum) && st.st_size <= BBSIZE) { 37554f445fbSPoul-Henning Kamp i = read(fd, bootarea, st.st_size); 37654f445fbSPoul-Henning Kamp if (i != st.st_size) 377d2fe97c7SPoul-Henning Kamp err(1, "read error %s", xxboot); 378d2fe97c7SPoul-Henning Kamp return; 379d2fe97c7SPoul-Henning Kamp } 380d2fe97c7SPoul-Henning Kamp errx(1, "boot code %s is wrong size", xxboot); 381d2fe97c7SPoul-Henning Kamp } 382d2fe97c7SPoul-Henning Kamp 383d2fe97c7SPoul-Henning Kamp static int 384d2fe97c7SPoul-Henning Kamp writelabel(void) 3858fae3551SRodney W. Grimes { 386bc33ea1aSRuslan Ermilov uint64_t *p, sum; 3875daa806dSPoul-Henning Kamp int i, fd; 3885daa806dSPoul-Henning Kamp struct gctl_req *grq; 3895daa806dSPoul-Henning Kamp char const *errstr; 390d2fe97c7SPoul-Henning Kamp struct disklabel *lp = &lab; 391130cd73aSDoug Rabson 3923233afaeSJohn W. De Boskey if (disable_write) { 39357dfbec5SPoul-Henning Kamp warnx("write to disk label supressed - label was as follows:"); 394d2fe97c7SPoul-Henning Kamp display(stdout, NULL); 3953233afaeSJohn W. De Boskey return (0); 39680baf8ceSPoul-Henning Kamp } 39780baf8ceSPoul-Henning Kamp 3988fae3551SRodney W. Grimes lp->d_magic = DISKMAGIC; 3998fae3551SRodney W. Grimes lp->d_magic2 = DISKMAGIC; 4008fae3551SRodney W. Grimes lp->d_checksum = 0; 4018fae3551SRodney W. Grimes lp->d_checksum = dkcksum(lp); 402d2fe97c7SPoul-Henning Kamp if (installboot) 403d2fe97c7SPoul-Henning Kamp readboot(); 4045d853216SPoul-Henning Kamp for (i = 0; i < lab.d_npartitions; i++) 4055d853216SPoul-Henning Kamp if (lab.d_partitions[i].p_size) 406eb0ea23eSMarcel Moolenaar lab.d_partitions[i].p_offset += lba_offset; 407b2bb9f9bSPoul-Henning Kamp bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize, 408b2bb9f9bSPoul-Henning Kamp lp); 409dfbc3f0cSPoul-Henning Kamp if (alphacksum) { 4105daa806dSPoul-Henning Kamp /* Generate the bootblock checksum for the SRM console. */ 411d2fe97c7SPoul-Henning Kamp for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++) 412130cd73aSDoug Rabson sum += p[i]; 413130cd73aSDoug Rabson p[63] = sum; 414bc33ea1aSRuslan Ermilov } 4155daa806dSPoul-Henning Kamp 4165daa806dSPoul-Henning Kamp fd = open(specname, O_RDWR); 4175daa806dSPoul-Henning Kamp if (fd < 0) { 4187747c959SLuigi Rizzo if (is_file) { 4197747c959SLuigi Rizzo warn("cannot open file %s for writing label", specname); 4207747c959SLuigi Rizzo return(1); 4217747c959SLuigi Rizzo } 4227a4b0bb2SUlf Lilleengen 42383d771deSPoul-Henning Kamp grq = gctl_get_handle(); 42483d771deSPoul-Henning Kamp gctl_ro_param(grq, "verb", -1, "write label"); 4255daa806dSPoul-Henning Kamp gctl_ro_param(grq, "class", -1, "BSD"); 4267a4b0bb2SUlf Lilleengen gctl_ro_param(grq, "geom", -1, pname); 427b2bb9f9bSPoul-Henning Kamp gctl_ro_param(grq, "label", 148+16*8, 428b2bb9f9bSPoul-Henning Kamp bootarea + labeloffset + labelsoffset * secsize); 4295daa806dSPoul-Henning Kamp errstr = gctl_issue(grq); 430d2fe97c7SPoul-Henning Kamp if (errstr != NULL) { 431d2fe97c7SPoul-Henning Kamp warnx("%s", errstr); 432d2fe97c7SPoul-Henning Kamp gctl_free(grq); 433d2fe97c7SPoul-Henning Kamp return(1); 434d2fe97c7SPoul-Henning Kamp } 4355daa806dSPoul-Henning Kamp gctl_free(grq); 4365daa806dSPoul-Henning Kamp if (installboot) { 43783d771deSPoul-Henning Kamp grq = gctl_get_handle(); 43883d771deSPoul-Henning Kamp gctl_ro_param(grq, "verb", -1, "write bootcode"); 4395daa806dSPoul-Henning Kamp gctl_ro_param(grq, "class", -1, "BSD"); 4407a4b0bb2SUlf Lilleengen gctl_ro_param(grq, "geom", -1, pname); 441d2fe97c7SPoul-Henning Kamp gctl_ro_param(grq, "bootcode", BBSIZE, bootarea); 4425daa806dSPoul-Henning Kamp errstr = gctl_issue(grq); 443d2fe97c7SPoul-Henning Kamp if (errstr != NULL) { 444d2fe97c7SPoul-Henning Kamp warnx("%s", errstr); 445d2fe97c7SPoul-Henning Kamp gctl_free(grq); 446d2fe97c7SPoul-Henning Kamp return (1); 447d2fe97c7SPoul-Henning Kamp } 4485daa806dSPoul-Henning Kamp gctl_free(grq); 4495daa806dSPoul-Henning Kamp } 4505daa806dSPoul-Henning Kamp } else { 451d2fe97c7SPoul-Henning Kamp if (write(fd, bootarea, bbsize) != bbsize) { 4525daa806dSPoul-Henning Kamp warn("write %s", specname); 4535daa806dSPoul-Henning Kamp close (fd); 4548fae3551SRodney W. Grimes return (1); 4558fae3551SRodney W. Grimes } 4565daa806dSPoul-Henning Kamp close (fd); 4575daa806dSPoul-Henning Kamp } 4588fae3551SRodney W. Grimes return (0); 4598fae3551SRodney W. Grimes } 4608fae3551SRodney W. Grimes 4617747c959SLuigi Rizzo static void 4627747c959SLuigi Rizzo get_file_parms(int f) 4637747c959SLuigi Rizzo { 4647747c959SLuigi Rizzo int i; 4657747c959SLuigi Rizzo struct stat sb; 4667747c959SLuigi Rizzo 4677747c959SLuigi Rizzo if (fstat(f, &sb) != 0) 4687747c959SLuigi Rizzo err(4, "fstat failed"); 4697747c959SLuigi Rizzo i = sb.st_mode & S_IFMT; 4707747c959SLuigi Rizzo if (i != S_IFREG && i != S_IFLNK) 4717747c959SLuigi Rizzo errx(4, "%s is not a valid file or link", specname); 4727747c959SLuigi Rizzo secsize = DEV_BSIZE; 4737747c959SLuigi Rizzo mediasize = sb.st_size; 4747747c959SLuigi Rizzo } 4757747c959SLuigi Rizzo 4768fae3551SRodney W. Grimes /* 4778fae3551SRodney W. Grimes * Fetch disklabel for disk. 4788fae3551SRodney W. Grimes */ 479d2fe97c7SPoul-Henning Kamp static int 480d2fe97c7SPoul-Henning Kamp readlabel(int flag) 4818fae3551SRodney W. Grimes { 482eb0ea23eSMarcel Moolenaar uint32_t lba; 4835d853216SPoul-Henning Kamp int f, i; 4845daa806dSPoul-Henning Kamp int error; 4858fae3551SRodney W. Grimes 4865daa806dSPoul-Henning Kamp f = open(specname, O_RDONLY); 4875daa806dSPoul-Henning Kamp if (f < 0) 4885daa806dSPoul-Henning Kamp err(1, specname); 4897747c959SLuigi Rizzo if (is_file) 4907747c959SLuigi Rizzo get_file_parms(f); 4917a4b0bb2SUlf Lilleengen else { 4927a4b0bb2SUlf Lilleengen mediasize = g_mediasize(f); 4937a4b0bb2SUlf Lilleengen secsize = g_sectorsize(f); 4947a4b0bb2SUlf Lilleengen if (secsize < 0 || mediasize < 0) 495b2bb9f9bSPoul-Henning Kamp err(4, "cannot get disk geometry"); 496b2bb9f9bSPoul-Henning Kamp } 49786e0bd0dSBrooks Davis if (mediasize > (off_t)0xffffffff * secsize) 49886e0bd0dSBrooks Davis errx(1, 49986e0bd0dSBrooks Davis "disks with more than 2^32-1 sectors are not supported"); 500c80f9755SPoul-Henning Kamp (void)lseek(f, (off_t)0, SEEK_SET); 501d2fe97c7SPoul-Henning Kamp if (read(f, bootarea, BBSIZE) != BBSIZE) 5025daa806dSPoul-Henning Kamp err(4, "%s read", specname); 5035daa806dSPoul-Henning Kamp close (f); 504b2bb9f9bSPoul-Henning Kamp error = bsd_disklabel_le_dec( 505b2bb9f9bSPoul-Henning Kamp bootarea + (labeloffset + labelsoffset * secsize), 506b2bb9f9bSPoul-Henning Kamp &lab, MAXPARTITIONS); 507d2fe97c7SPoul-Henning Kamp if (flag && error) 508d2fe97c7SPoul-Henning Kamp errx(1, "%s: no valid label found", specname); 5095d853216SPoul-Henning Kamp 5107a4b0bb2SUlf Lilleengen if (is_file) 5117a4b0bb2SUlf Lilleengen return(0); 512eb0ea23eSMarcel Moolenaar 513eb0ea23eSMarcel Moolenaar /* 514eb0ea23eSMarcel Moolenaar * Compensate for absolute block addressing by finding the 515eb0ea23eSMarcel Moolenaar * smallest partition offset and if the offset of the 'c' 516eb0ea23eSMarcel Moolenaar * partition is equal to that, subtract it from all offsets. 517eb0ea23eSMarcel Moolenaar */ 518eb0ea23eSMarcel Moolenaar lba = ~0; 519eb0ea23eSMarcel Moolenaar for (i = 0; i < lab.d_npartitions; i++) { 5205d853216SPoul-Henning Kamp if (lab.d_partitions[i].p_size) 521eb0ea23eSMarcel Moolenaar lba = MIN(lba, lab.d_partitions[i].p_offset); 522eb0ea23eSMarcel Moolenaar } 523eb0ea23eSMarcel Moolenaar if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) { 524eb0ea23eSMarcel Moolenaar for (i = 0; i < lab.d_npartitions; i++) { 525eb0ea23eSMarcel Moolenaar if (lab.d_partitions[i].p_size) 526eb0ea23eSMarcel Moolenaar lab.d_partitions[i].p_offset -= lba; 527eb0ea23eSMarcel Moolenaar } 528eb0ea23eSMarcel Moolenaar /* 529eb0ea23eSMarcel Moolenaar * Save the offset so that we can write the label 530eb0ea23eSMarcel Moolenaar * back with absolute block addresses. 531eb0ea23eSMarcel Moolenaar */ 532eb0ea23eSMarcel Moolenaar lba_offset = lba; 533eb0ea23eSMarcel Moolenaar } 534d2fe97c7SPoul-Henning Kamp return (error); 5358fae3551SRodney W. Grimes } 5368fae3551SRodney W. Grimes 5378fae3551SRodney W. Grimes 538d2fe97c7SPoul-Henning Kamp static void 539326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp) 5408fae3551SRodney W. Grimes { 541326c7cdaSWarner Losh int i, j; 542326c7cdaSWarner Losh const struct partition *pp; 5438fae3551SRodney W. Grimes 544d2fe97c7SPoul-Henning Kamp if (lp == NULL) 545d2fe97c7SPoul-Henning Kamp lp = &lab; 546d2fe97c7SPoul-Henning Kamp 5478fae3551SRodney W. Grimes fprintf(f, "# %s:\n", specname); 54857dfbec5SPoul-Henning Kamp if (allfields) { 549484c7804SJulian Elischer if (lp->d_type < DKMAXTYPES) 5508fae3551SRodney W. Grimes fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 5518fae3551SRodney W. Grimes else 5522a1deaaaSBruce Evans fprintf(f, "type: %u\n", lp->d_type); 55361de51caSJoerg Wunsch fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 55461de51caSJoerg Wunsch lp->d_typename); 55561de51caSJoerg Wunsch fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 55661de51caSJoerg Wunsch lp->d_packname); 5578fae3551SRodney W. Grimes fprintf(f, "flags:"); 5588fae3551SRodney W. Grimes if (lp->d_flags & D_REMOVABLE) 5598fae3551SRodney W. Grimes fprintf(f, " removeable"); 5608fae3551SRodney W. Grimes if (lp->d_flags & D_ECC) 5618fae3551SRodney W. Grimes fprintf(f, " ecc"); 5628fae3551SRodney W. Grimes if (lp->d_flags & D_BADSECT) 5638fae3551SRodney W. Grimes fprintf(f, " badsect"); 5648fae3551SRodney W. Grimes fprintf(f, "\n"); 5652a1deaaaSBruce Evans fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 5662a1deaaaSBruce Evans fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 5672a1deaaaSBruce Evans fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 5682a1deaaaSBruce Evans fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 5692a1deaaaSBruce Evans fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 5702a1deaaaSBruce Evans fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 5712a1deaaaSBruce Evans fprintf(f, "rpm: %u\n", lp->d_rpm); 5722a1deaaaSBruce Evans fprintf(f, "interleave: %u\n", lp->d_interleave); 5732a1deaaaSBruce Evans fprintf(f, "trackskew: %u\n", lp->d_trackskew); 5742a1deaaaSBruce Evans fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 5752a1deaaaSBruce Evans fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 5762a1deaaaSBruce Evans (u_long)lp->d_headswitch); 57761de51caSJoerg Wunsch fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 5782a1deaaaSBruce Evans (u_long)lp->d_trkseek); 5798fae3551SRodney W. Grimes fprintf(f, "drivedata: "); 5808fae3551SRodney W. Grimes for (i = NDDATA - 1; i >= 0; i--) 5818fae3551SRodney W. Grimes if (lp->d_drivedata[i]) 5828fae3551SRodney W. Grimes break; 5838fae3551SRodney W. Grimes if (i < 0) 5848fae3551SRodney W. Grimes i = 0; 5858fae3551SRodney W. Grimes for (j = 0; j <= i; j++) 5862a1deaaaSBruce Evans fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 58757dfbec5SPoul-Henning Kamp fprintf(f, "\n\n"); 58857dfbec5SPoul-Henning Kamp } 58957dfbec5SPoul-Henning Kamp fprintf(f, "%u partitions:\n", lp->d_npartitions); 5908fae3551SRodney W. Grimes fprintf(f, 591ca4693edSJustin T. Gibbs "# size offset fstype [fsize bsize bps/cpg]\n"); 5928fae3551SRodney W. Grimes pp = lp->d_partitions; 5938fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++, pp++) { 5948fae3551SRodney W. Grimes if (pp->p_size) { 5952a1deaaaSBruce Evans fprintf(f, " %c: %8lu %8lu ", 'a' + i, 5962a1deaaaSBruce Evans (u_long)pp->p_size, (u_long)pp->p_offset); 597484c7804SJulian Elischer if (pp->p_fstype < FSMAXTYPES) 5988fae3551SRodney W. Grimes fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 5998fae3551SRodney W. Grimes else 6008fae3551SRodney W. Grimes fprintf(f, "%8d", pp->p_fstype); 6018fae3551SRodney W. Grimes switch (pp->p_fstype) { 6028fae3551SRodney W. Grimes 6038fae3551SRodney W. Grimes case FS_UNUSED: /* XXX */ 6042a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5.5s ", 6052a1deaaaSBruce Evans (u_long)pp->p_fsize, 6062a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), ""); 6078fae3551SRodney W. Grimes break; 6088fae3551SRodney W. Grimes 6098fae3551SRodney W. Grimes case FS_BSDFFS: 6102a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5u ", 6112a1deaaaSBruce Evans (u_long)pp->p_fsize, 6122a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 6138fae3551SRodney W. Grimes pp->p_cpg); 6148fae3551SRodney W. Grimes break; 6158fae3551SRodney W. Grimes 616ca4693edSJustin T. Gibbs case FS_BSDLFS: 6172a1deaaaSBruce Evans fprintf(f, " %5lu %5lu %5d", 6182a1deaaaSBruce Evans (u_long)pp->p_fsize, 6192a1deaaaSBruce Evans (u_long)(pp->p_fsize * pp->p_frag), 620ca4693edSJustin T. Gibbs pp->p_cpg); 621ca4693edSJustin T. Gibbs break; 622ca4693edSJustin T. Gibbs 6238fae3551SRodney W. Grimes default: 6248fae3551SRodney W. Grimes fprintf(f, "%20.20s", ""); 6258fae3551SRodney W. Grimes break; 6268fae3551SRodney W. Grimes } 62757dfbec5SPoul-Henning Kamp if (i == RAW_PART) { 62857dfbec5SPoul-Henning Kamp fprintf(f, " # \"raw\" part, don't edit"); 62957dfbec5SPoul-Henning Kamp } 630d2fe97c7SPoul-Henning Kamp fprintf(f, "\n"); 6318fae3551SRodney W. Grimes } 6328fae3551SRodney W. Grimes } 6338fae3551SRodney W. Grimes fflush(f); 6348fae3551SRodney W. Grimes } 6358fae3551SRodney W. Grimes 636d2fe97c7SPoul-Henning Kamp static int 637d2fe97c7SPoul-Henning Kamp edit(void) 6388fae3551SRodney W. Grimes { 639326c7cdaSWarner Losh int c, fd; 6408fae3551SRodney W. Grimes struct disklabel label; 641722ceb3fSWarner Losh FILE *fp; 6428fae3551SRodney W. Grimes 643722ceb3fSWarner Losh if ((fd = mkstemp(tmpfil)) == -1 || 644722ceb3fSWarner Losh (fp = fdopen(fd, "w")) == NULL) { 6456bd343a9SPhilippe Charnier warnx("can't create %s", tmpfil); 6468fae3551SRodney W. Grimes return (1); 6478fae3551SRodney W. Grimes } 648d2fe97c7SPoul-Henning Kamp display(fp, NULL); 649722ceb3fSWarner Losh fclose(fp); 6508fae3551SRodney W. Grimes for (;;) { 6518fae3551SRodney W. Grimes if (!editit()) 6528fae3551SRodney W. Grimes break; 653722ceb3fSWarner Losh fp = fopen(tmpfil, "r"); 654722ceb3fSWarner Losh if (fp == NULL) { 6556bd343a9SPhilippe Charnier warnx("can't reopen %s for reading", tmpfil); 6568fae3551SRodney W. Grimes break; 6578fae3551SRodney W. Grimes } 6588fae3551SRodney W. Grimes bzero((char *)&label, sizeof(label)); 659d2fe97c7SPoul-Henning Kamp c = getasciilabel(fp, &label); 660722ceb3fSWarner Losh fclose(fp); 661d2fe97c7SPoul-Henning Kamp if (c) { 662d2fe97c7SPoul-Henning Kamp lab = label; 663d2fe97c7SPoul-Henning Kamp if (writelabel() == 0) { 6648fae3551SRodney W. Grimes (void) unlink(tmpfil); 6658fae3551SRodney W. Grimes return (0); 6668fae3551SRodney W. Grimes } 6678fae3551SRodney W. Grimes } 668d2fe97c7SPoul-Henning Kamp printf("re-edit the label? [y]: "); 669d2fe97c7SPoul-Henning Kamp fflush(stdout); 6708fae3551SRodney W. Grimes c = getchar(); 6718fae3551SRodney W. Grimes if (c != EOF && c != (int)'\n') 6728fae3551SRodney W. Grimes while (getchar() != (int)'\n') 6738fae3551SRodney W. Grimes ; 6748fae3551SRodney W. Grimes if (c == (int)'n') 6758fae3551SRodney W. Grimes break; 6768fae3551SRodney W. Grimes } 6778fae3551SRodney W. Grimes (void) unlink(tmpfil); 6788fae3551SRodney W. Grimes return (1); 6798fae3551SRodney W. Grimes } 6808fae3551SRodney W. Grimes 681d2fe97c7SPoul-Henning Kamp static int 682326c7cdaSWarner Losh editit(void) 6838fae3551SRodney W. Grimes { 684326c7cdaSWarner Losh int pid, xpid; 685c8223965SMark Murray int locstat, omask; 686c8223965SMark Murray const char *ed; 687*73d6722dSKevin Lo uid_t uid; 688*73d6722dSKevin Lo gid_t gid; 6898fae3551SRodney W. Grimes 6908fae3551SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 6918fae3551SRodney W. Grimes while ((pid = fork()) < 0) { 6928fae3551SRodney W. Grimes if (errno == EPROCLIM) { 6936bd343a9SPhilippe Charnier warnx("you have too many processes"); 6948fae3551SRodney W. Grimes return(0); 6958fae3551SRodney W. Grimes } 6968fae3551SRodney W. Grimes if (errno != EAGAIN) { 6976bd343a9SPhilippe Charnier warn("fork"); 6988fae3551SRodney W. Grimes return(0); 6998fae3551SRodney W. Grimes } 7008fae3551SRodney W. Grimes sleep(1); 7018fae3551SRodney W. Grimes } 7028fae3551SRodney W. Grimes if (pid == 0) { 7038fae3551SRodney W. Grimes sigsetmask(omask); 704*73d6722dSKevin Lo gid = getgid(); 705*73d6722dSKevin Lo if (setresgid(gid, gid, gid) == -1) 706*73d6722dSKevin Lo err(1, "setresgid"); 707*73d6722dSKevin Lo uid = getuid(); 708*73d6722dSKevin Lo if (setresuid(uid, uid, uid) == -1) 709*73d6722dSKevin Lo err(1, "setresuid"); 7108fae3551SRodney W. Grimes if ((ed = getenv("EDITOR")) == (char *)0) 7118fae3551SRodney W. Grimes ed = DEFEDITOR; 7127bc6d015SBrian Somers execlp(ed, ed, tmpfil, (char *)0); 7136bd343a9SPhilippe Charnier err(1, "%s", ed); 7148fae3551SRodney W. Grimes } 715c8223965SMark Murray while ((xpid = wait(&locstat)) >= 0) 7168fae3551SRodney W. Grimes if (xpid == pid) 7178fae3551SRodney W. Grimes break; 7188fae3551SRodney W. Grimes sigsetmask(omask); 719c8223965SMark Murray return(!locstat); 7208fae3551SRodney W. Grimes } 7218fae3551SRodney W. Grimes 722d2fe97c7SPoul-Henning Kamp static char * 723326c7cdaSWarner Losh skip(char *cp) 7248fae3551SRodney W. Grimes { 7258fae3551SRodney W. Grimes 7268fae3551SRodney W. Grimes while (*cp != '\0' && isspace(*cp)) 7278fae3551SRodney W. Grimes cp++; 7288fae3551SRodney W. Grimes if (*cp == '\0' || *cp == '#') 729326c7cdaSWarner Losh return (NULL); 7308fae3551SRodney W. Grimes return (cp); 7318fae3551SRodney W. Grimes } 7328fae3551SRodney W. Grimes 733d2fe97c7SPoul-Henning Kamp static char * 734326c7cdaSWarner Losh word(char *cp) 7358fae3551SRodney W. Grimes { 736326c7cdaSWarner Losh char c; 7378fae3551SRodney W. Grimes 7388fae3551SRodney W. Grimes while (*cp != '\0' && !isspace(*cp) && *cp != '#') 7398fae3551SRodney W. Grimes cp++; 7408fae3551SRodney W. Grimes if ((c = *cp) != '\0') { 7418fae3551SRodney W. Grimes *cp++ = '\0'; 7428fae3551SRodney W. Grimes if (c != '#') 7438fae3551SRodney W. Grimes return (skip(cp)); 7448fae3551SRodney W. Grimes } 745326c7cdaSWarner Losh return (NULL); 7468fae3551SRodney W. Grimes } 7478fae3551SRodney W. Grimes 7488fae3551SRodney W. Grimes /* 7498fae3551SRodney W. Grimes * Read an ascii label in from fd f, 7508fae3551SRodney W. Grimes * in the same format as that put out by display(), 7518fae3551SRodney W. Grimes * and fill in lp. 7528fae3551SRodney W. Grimes */ 753d2fe97c7SPoul-Henning Kamp static int 754326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp) 7558fae3551SRodney W. Grimes { 7566b0ff5f5SPoul-Henning Kamp char *cp; 7576b0ff5f5SPoul-Henning Kamp const char **cpp; 758484c7804SJulian Elischer u_int part; 7596b0ff5f5SPoul-Henning Kamp char *tp, line[BUFSIZ]; 760484c7804SJulian Elischer u_long v; 761484c7804SJulian Elischer int lineno = 0, errors = 0; 762326c7cdaSWarner Losh int i; 7638fae3551SRodney W. Grimes 7648970f1a4SPoul-Henning Kamp makelabel("auto", lp); 765b0459c58SDag-Erling Smørgrav bzero(&part_set, sizeof(part_set)); 766b0459c58SDag-Erling Smørgrav bzero(&part_size_type, sizeof(part_size_type)); 767b0459c58SDag-Erling Smørgrav bzero(&part_offset_type, sizeof(part_offset_type)); 7688fae3551SRodney W. Grimes lp->d_bbsize = BBSIZE; /* XXX */ 76977068a7fSPoul-Henning Kamp lp->d_sbsize = 0; /* XXX */ 7708fae3551SRodney W. Grimes while (fgets(line, sizeof(line) - 1, f)) { 7718fae3551SRodney W. Grimes lineno++; 77261de51caSJoerg Wunsch if ((cp = index(line,'\n')) != 0) 7738fae3551SRodney W. Grimes *cp = '\0'; 7748fae3551SRodney W. Grimes cp = skip(line); 7758fae3551SRodney W. Grimes if (cp == NULL) 7768fae3551SRodney W. Grimes continue; 7778fae3551SRodney W. Grimes tp = index(cp, ':'); 7788fae3551SRodney W. Grimes if (tp == NULL) { 7798fae3551SRodney W. Grimes fprintf(stderr, "line %d: syntax error\n", lineno); 7808fae3551SRodney W. Grimes errors++; 7818fae3551SRodney W. Grimes continue; 7828fae3551SRodney W. Grimes } 7838fae3551SRodney W. Grimes *tp++ = '\0', tp = skip(tp); 784d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "type")) { 7858fae3551SRodney W. Grimes if (tp == NULL) 786c8223965SMark Murray tp = unknown; 7878fae3551SRodney W. Grimes cpp = dktypenames; 7888fae3551SRodney W. Grimes for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 789d2fe97c7SPoul-Henning Kamp if (*cpp && !strcmp(*cpp, tp)) { 7908fae3551SRodney W. Grimes lp->d_type = cpp - dktypenames; 79109dbd070SIan Dowse break; 7928fae3551SRodney W. Grimes } 79309dbd070SIan Dowse if (cpp < &dktypenames[DKMAXTYPES]) 79409dbd070SIan Dowse continue; 795484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 796484c7804SJulian Elischer if (v >= DKMAXTYPES) 797484c7804SJulian Elischer fprintf(stderr, "line %d:%s %lu\n", lineno, 7988fae3551SRodney W. Grimes "Warning, unknown disk type", v); 7998fae3551SRodney W. Grimes lp->d_type = v; 8008fae3551SRodney W. Grimes continue; 8018fae3551SRodney W. Grimes } 802d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "flags")) { 8038fae3551SRodney W. Grimes for (v = 0; (cp = tp) && *cp != '\0';) { 8048fae3551SRodney W. Grimes tp = word(cp); 805d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "removeable")) 8068fae3551SRodney W. Grimes v |= D_REMOVABLE; 807d2fe97c7SPoul-Henning Kamp else if (!strcmp(cp, "ecc")) 8088fae3551SRodney W. Grimes v |= D_ECC; 809d2fe97c7SPoul-Henning Kamp else if (!strcmp(cp, "badsect")) 8108fae3551SRodney W. Grimes v |= D_BADSECT; 8118fae3551SRodney W. Grimes else { 8128fae3551SRodney W. Grimes fprintf(stderr, 8138fae3551SRodney W. Grimes "line %d: %s: bad flag\n", 8148fae3551SRodney W. Grimes lineno, cp); 8158fae3551SRodney W. Grimes errors++; 8168fae3551SRodney W. Grimes } 8178fae3551SRodney W. Grimes } 8188fae3551SRodney W. Grimes lp->d_flags = v; 8198fae3551SRodney W. Grimes continue; 8208fae3551SRodney W. Grimes } 821d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "drivedata")) { 8228fae3551SRodney W. Grimes for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 823484c7804SJulian Elischer lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 8248fae3551SRodney W. Grimes tp = word(cp); 8258fae3551SRodney W. Grimes } 8268fae3551SRodney W. Grimes continue; 8278fae3551SRodney W. Grimes } 828484c7804SJulian Elischer if (sscanf(cp, "%lu partitions", &v) == 1) { 829484c7804SJulian Elischer if (v == 0 || v > MAXPARTITIONS) { 8308fae3551SRodney W. Grimes fprintf(stderr, 8318fae3551SRodney W. Grimes "line %d: bad # of partitions\n", lineno); 8328fae3551SRodney W. Grimes lp->d_npartitions = MAXPARTITIONS; 8338fae3551SRodney W. Grimes errors++; 8348fae3551SRodney W. Grimes } else 8358fae3551SRodney W. Grimes lp->d_npartitions = v; 8368fae3551SRodney W. Grimes continue; 8378fae3551SRodney W. Grimes } 8388fae3551SRodney W. Grimes if (tp == NULL) 839c8223965SMark Murray tp = blank; 840d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "disk")) { 8418fae3551SRodney W. Grimes strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 8428fae3551SRodney W. Grimes continue; 8438fae3551SRodney W. Grimes } 844d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "label")) { 8458fae3551SRodney W. Grimes strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 8468fae3551SRodney W. Grimes continue; 8478fae3551SRodney W. Grimes } 848d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "bytes/sector")) { 849484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 850484c7804SJulian Elischer if (v == 0 || (v % DEV_BSIZE) != 0) { 8518fae3551SRodney W. Grimes fprintf(stderr, 8528fae3551SRodney W. Grimes "line %d: %s: bad sector size\n", 8538fae3551SRodney W. Grimes lineno, tp); 8548fae3551SRodney W. Grimes errors++; 8558fae3551SRodney W. Grimes } else 8568fae3551SRodney W. Grimes lp->d_secsize = v; 8578fae3551SRodney W. Grimes continue; 8588fae3551SRodney W. Grimes } 859d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "sectors/track")) { 860484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 861484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL) 862266e7997SDag-Erling Smørgrav if (v == 0 || v > 0xffffffff) 863484c7804SJulian Elischer #else 864266e7997SDag-Erling Smørgrav if (v == 0) 865484c7804SJulian Elischer #endif 866266e7997SDag-Erling Smørgrav { 8678fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 8688fae3551SRodney W. Grimes lineno, tp, cp); 8698fae3551SRodney W. Grimes errors++; 8708fae3551SRodney W. Grimes } else 8718fae3551SRodney W. Grimes lp->d_nsectors = v; 8728fae3551SRodney W. Grimes continue; 8738fae3551SRodney W. Grimes } 874d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "sectors/cylinder")) { 875484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 876484c7804SJulian Elischer if (v == 0) { 8778fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 8788fae3551SRodney W. Grimes lineno, tp, cp); 8798fae3551SRodney W. Grimes errors++; 8808fae3551SRodney W. Grimes } else 8818fae3551SRodney W. Grimes lp->d_secpercyl = v; 8828fae3551SRodney W. Grimes continue; 8838fae3551SRodney W. Grimes } 884d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "tracks/cylinder")) { 885484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 886484c7804SJulian Elischer if (v == 0) { 8878fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 8888fae3551SRodney W. Grimes lineno, tp, cp); 8898fae3551SRodney W. Grimes errors++; 8908fae3551SRodney W. Grimes } else 8918fae3551SRodney W. Grimes lp->d_ntracks = v; 8928fae3551SRodney W. Grimes continue; 8938fae3551SRodney W. Grimes } 894d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "cylinders")) { 895484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 896484c7804SJulian Elischer if (v == 0) { 8978fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 8988fae3551SRodney W. Grimes lineno, tp, cp); 8998fae3551SRodney W. Grimes errors++; 9008fae3551SRodney W. Grimes } else 9018fae3551SRodney W. Grimes lp->d_ncylinders = v; 9028fae3551SRodney W. Grimes continue; 9038fae3551SRodney W. Grimes } 904d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "sectors/unit")) { 905484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 906484c7804SJulian Elischer if (v == 0) { 907f75dd518SBruce Evans fprintf(stderr, "line %d: %s: bad %s\n", 908f75dd518SBruce Evans lineno, tp, cp); 909f75dd518SBruce Evans errors++; 910f75dd518SBruce Evans } else 911f75dd518SBruce Evans lp->d_secperunit = v; 912f75dd518SBruce Evans continue; 913f75dd518SBruce Evans } 914d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "rpm")) { 915484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 916484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 9178fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9188fae3551SRodney W. Grimes lineno, tp, cp); 9198fae3551SRodney W. Grimes errors++; 9208fae3551SRodney W. Grimes } else 9218fae3551SRodney W. Grimes lp->d_rpm = v; 9228fae3551SRodney W. Grimes continue; 9238fae3551SRodney W. Grimes } 924d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "interleave")) { 925484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 926484c7804SJulian Elischer if (v == 0 || v > USHRT_MAX) { 9278fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9288fae3551SRodney W. Grimes lineno, tp, cp); 9298fae3551SRodney W. Grimes errors++; 9308fae3551SRodney W. Grimes } else 9318fae3551SRodney W. Grimes lp->d_interleave = v; 9328fae3551SRodney W. Grimes continue; 9338fae3551SRodney W. Grimes } 934d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "trackskew")) { 935484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 936484c7804SJulian Elischer if (v > USHRT_MAX) { 9378fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9388fae3551SRodney W. Grimes lineno, tp, cp); 9398fae3551SRodney W. Grimes errors++; 9408fae3551SRodney W. Grimes } else 9418fae3551SRodney W. Grimes lp->d_trackskew = v; 9428fae3551SRodney W. Grimes continue; 9438fae3551SRodney W. Grimes } 944d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "cylinderskew")) { 945484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 946484c7804SJulian Elischer if (v > USHRT_MAX) { 9478fae3551SRodney W. Grimes fprintf(stderr, "line %d: %s: bad %s\n", 9488fae3551SRodney W. Grimes lineno, tp, cp); 9498fae3551SRodney W. Grimes errors++; 9508fae3551SRodney W. Grimes } else 9518fae3551SRodney W. Grimes lp->d_cylskew = v; 9528fae3551SRodney W. Grimes continue; 9538fae3551SRodney W. Grimes } 954d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "headswitch")) { 955484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 9568fae3551SRodney W. Grimes lp->d_headswitch = v; 9578fae3551SRodney W. Grimes continue; 9588fae3551SRodney W. Grimes } 959d2fe97c7SPoul-Henning Kamp if (!strcmp(cp, "track-to-track seek")) { 960484c7804SJulian Elischer v = strtoul(tp, NULL, 10); 9618fae3551SRodney W. Grimes lp->d_trkseek = v; 9628fae3551SRodney W. Grimes continue; 9638fae3551SRodney W. Grimes } 9643233afaeSJohn W. De Boskey /* the ':' was removed above */ 96567b46708SIan Dowse if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 96667b46708SIan Dowse fprintf(stderr, 96767b46708SIan Dowse "line %d: %s: Unknown disklabel field\n", lineno, 96867b46708SIan Dowse cp); 96967b46708SIan Dowse errors++; 97067b46708SIan Dowse continue; 97167b46708SIan Dowse } 97267b46708SIan Dowse 97367b46708SIan Dowse /* Process a partition specification line. */ 9743233afaeSJohn W. De Boskey part = *cp - 'a'; 9753233afaeSJohn W. De Boskey if (part >= lp->d_npartitions) { 9768fae3551SRodney W. Grimes fprintf(stderr, 9773233afaeSJohn W. De Boskey "line %d: partition name out of range a-%c: %s\n", 9783233afaeSJohn W. De Boskey lineno, 'a' + lp->d_npartitions - 1, cp); 9798fae3551SRodney W. Grimes errors++; 9808fae3551SRodney W. Grimes continue; 9818fae3551SRodney W. Grimes } 9823233afaeSJohn W. De Boskey part_set[part] = 1; 98367b46708SIan Dowse 98467b46708SIan Dowse if (getasciipartspec(tp, lp, part, lineno) != 0) { 98567b46708SIan Dowse errors++; 98667b46708SIan Dowse break; 98767b46708SIan Dowse } 98867b46708SIan Dowse } 98967b46708SIan Dowse errors += checklabel(lp); 99067b46708SIan Dowse return (errors == 0); 99167b46708SIan Dowse } 99267b46708SIan Dowse 99367b46708SIan Dowse #define NXTNUM(n) do { \ 99413e0abcbSPaul Traina if (tp == NULL) { \ 99513e0abcbSPaul Traina fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 99667b46708SIan Dowse return (1); \ 99713e0abcbSPaul Traina } else { \ 9988fae3551SRodney W. Grimes cp = tp, tp = word(cp); \ 999484c7804SJulian Elischer (n) = strtoul(cp, NULL, 10); \ 100013e0abcbSPaul Traina } \ 100167b46708SIan Dowse } while (0) 100267b46708SIan Dowse 10033233afaeSJohn W. De Boskey /* retain 1 character following number */ 100467b46708SIan Dowse #define NXTWORD(w,n) do { \ 10053233afaeSJohn W. De Boskey if (tp == NULL) { \ 10063233afaeSJohn W. De Boskey fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 100767b46708SIan Dowse return (1); \ 10083233afaeSJohn W. De Boskey } else { \ 10093233afaeSJohn W. De Boskey char *tmp; \ 10103233afaeSJohn W. De Boskey cp = tp, tp = word(cp); \ 1011484c7804SJulian Elischer (n) = strtoul(cp, &tmp, 10); \ 10123233afaeSJohn W. De Boskey if (tmp) (w) = *tmp; \ 10133233afaeSJohn W. De Boskey } \ 101467b46708SIan Dowse } while (0) 101567b46708SIan Dowse 101667b46708SIan Dowse /* 101767b46708SIan Dowse * Read a partition line into partition `part' in the specified disklabel. 101867b46708SIan Dowse * Return 0 on success, 1 on failure. 101967b46708SIan Dowse */ 1020d2fe97c7SPoul-Henning Kamp static int 102167b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 102267b46708SIan Dowse { 102367b46708SIan Dowse struct partition *pp; 102467b46708SIan Dowse char *cp; 102567b46708SIan Dowse const char **cpp; 1026484c7804SJulian Elischer u_long v; 102767b46708SIan Dowse 102867b46708SIan Dowse pp = &lp->d_partitions[part]; 102967b46708SIan Dowse cp = NULL; 103067b46708SIan Dowse 10313233afaeSJohn W. De Boskey v = 0; 10323233afaeSJohn W. De Boskey NXTWORD(part_size_type[part],v); 1033484c7804SJulian Elischer if (v == 0 && part_size_type[part] != '*') { 1034484c7804SJulian Elischer fprintf(stderr, 1035484c7804SJulian Elischer "line %d: %s: bad partition size\n", lineno, cp); 103667b46708SIan Dowse return (1); 103767b46708SIan Dowse } 10388fae3551SRodney W. Grimes pp->p_size = v; 10393233afaeSJohn W. De Boskey 10403233afaeSJohn W. De Boskey v = 0; 10413233afaeSJohn W. De Boskey NXTWORD(part_offset_type[part],v); 1042484c7804SJulian Elischer if (v == 0 && part_offset_type[part] != '*' && 1043484c7804SJulian Elischer part_offset_type[part] != '\0') { 1044484c7804SJulian Elischer fprintf(stderr, 1045484c7804SJulian Elischer "line %d: %s: bad partition offset\n", lineno, cp); 104667b46708SIan Dowse return (1); 104767b46708SIan Dowse } 10488fae3551SRodney W. Grimes pp->p_offset = v; 104929f3f095SYaroslav Tykhiy if (tp == NULL) { 105029f3f095SYaroslav Tykhiy fprintf(stderr, "line %d: missing file system type\n", lineno); 105129f3f095SYaroslav Tykhiy return (1); 105229f3f095SYaroslav Tykhiy } 10538fae3551SRodney W. Grimes cp = tp, tp = word(cp); 105467b46708SIan Dowse for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 1055d2fe97c7SPoul-Henning Kamp if (*cpp && !strcmp(*cpp, cp)) 105667b46708SIan Dowse break; 105767b46708SIan Dowse if (*cpp != NULL) { 105867b46708SIan Dowse pp->p_fstype = cpp - fstypenames; 105967b46708SIan Dowse } else { 10608fae3551SRodney W. Grimes if (isdigit(*cp)) 1061484c7804SJulian Elischer v = strtoul(cp, NULL, 10); 10628fae3551SRodney W. Grimes else 10638fae3551SRodney W. Grimes v = FSMAXTYPES; 1064484c7804SJulian Elischer if (v >= FSMAXTYPES) { 10653233afaeSJohn W. De Boskey fprintf(stderr, 106667b46708SIan Dowse "line %d: Warning, unknown file system type %s\n", 10673233afaeSJohn W. De Boskey lineno, cp); 10688fae3551SRodney W. Grimes v = FS_UNUSED; 10698fae3551SRodney W. Grimes } 10708fae3551SRodney W. Grimes pp->p_fstype = v; 107167b46708SIan Dowse } 107267b46708SIan Dowse 10738fae3551SRodney W. Grimes switch (pp->p_fstype) { 10743233afaeSJohn W. De Boskey case FS_UNUSED: 1075640c9cb2SIan Dowse case FS_BSDFFS: 1076640c9cb2SIan Dowse case FS_BSDLFS: 1077640c9cb2SIan Dowse /* accept defaults for fsize/frag/cpg */ 10783233afaeSJohn W. De Boskey if (tp) { 10798fae3551SRodney W. Grimes NXTNUM(pp->p_fsize); 10808fae3551SRodney W. Grimes if (pp->p_fsize == 0) 10818fae3551SRodney W. Grimes break; 10828fae3551SRodney W. Grimes NXTNUM(v); 10838fae3551SRodney W. Grimes pp->p_frag = v / pp->p_fsize; 1084640c9cb2SIan Dowse if (tp != NULL) 1085640c9cb2SIan Dowse NXTNUM(pp->p_cpg); 10863233afaeSJohn W. De Boskey } 10873233afaeSJohn W. De Boskey /* else default to 0's */ 10888fae3551SRodney W. Grimes break; 10898fae3551SRodney W. Grimes default: 10908fae3551SRodney W. Grimes break; 10918fae3551SRodney W. Grimes } 109267b46708SIan Dowse return (0); 10938fae3551SRodney W. Grimes } 10948fae3551SRodney W. Grimes 10958fae3551SRodney W. Grimes /* 10968fae3551SRodney W. Grimes * Check disklabel for errors and fill in 10978fae3551SRodney W. Grimes * derived fields according to supplied values. 10988fae3551SRodney W. Grimes */ 1099d2fe97c7SPoul-Henning Kamp static int 1100326c7cdaSWarner Losh checklabel(struct disklabel *lp) 11018fae3551SRodney W. Grimes { 1102326c7cdaSWarner Losh struct partition *pp; 11038fae3551SRodney W. Grimes int i, errors = 0; 11048fae3551SRodney W. Grimes char part; 11053b89beb1SIan Dowse u_long base_offset, needed, total_size, total_percent, current_offset; 11063b89beb1SIan Dowse long free_space; 11073233afaeSJohn W. De Boskey int seen_default_offset; 11083233afaeSJohn W. De Boskey int hog_part; 11093233afaeSJohn W. De Boskey int j; 11103233afaeSJohn W. De Boskey struct partition *pp2; 11118fae3551SRodney W. Grimes 1112d2fe97c7SPoul-Henning Kamp if (lp == NULL) 1113d2fe97c7SPoul-Henning Kamp lp = &lab; 1114d2fe97c7SPoul-Henning Kamp 111557dfbec5SPoul-Henning Kamp if (allfields) { 111657dfbec5SPoul-Henning Kamp 11178fae3551SRodney W. Grimes if (lp->d_secsize == 0) { 11182a1deaaaSBruce Evans fprintf(stderr, "sector size 0\n"); 11198fae3551SRodney W. Grimes return (1); 11208fae3551SRodney W. Grimes } 11218fae3551SRodney W. Grimes if (lp->d_nsectors == 0) { 11222a1deaaaSBruce Evans fprintf(stderr, "sectors/track 0\n"); 11238fae3551SRodney W. Grimes return (1); 11248fae3551SRodney W. Grimes } 11258fae3551SRodney W. Grimes if (lp->d_ntracks == 0) { 11262a1deaaaSBruce Evans fprintf(stderr, "tracks/cylinder 0\n"); 11278fae3551SRodney W. Grimes return (1); 11288fae3551SRodney W. Grimes } 11298fae3551SRodney W. Grimes if (lp->d_ncylinders == 0) { 11302a1deaaaSBruce Evans fprintf(stderr, "cylinders/unit 0\n"); 11318fae3551SRodney W. Grimes errors++; 11328fae3551SRodney W. Grimes } 11338fae3551SRodney W. Grimes if (lp->d_rpm == 0) 113457dfbec5SPoul-Henning Kamp warnx("revolutions/minute 0"); 11358fae3551SRodney W. Grimes if (lp->d_secpercyl == 0) 11368fae3551SRodney W. Grimes lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 11378fae3551SRodney W. Grimes if (lp->d_secperunit == 0) 11388fae3551SRodney W. Grimes lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 11398fae3551SRodney W. Grimes if (lp->d_bbsize == 0) { 11402a1deaaaSBruce Evans fprintf(stderr, "boot block size 0\n"); 11418fae3551SRodney W. Grimes errors++; 11428fae3551SRodney W. Grimes } else if (lp->d_bbsize % lp->d_secsize) 114357dfbec5SPoul-Henning Kamp warnx("boot block size %% sector-size != 0"); 11448fae3551SRodney W. Grimes if (lp->d_npartitions > MAXPARTITIONS) 114557dfbec5SPoul-Henning Kamp warnx("number of partitions (%lu) > MAXPARTITIONS (%d)", 11462a1deaaaSBruce Evans (u_long)lp->d_npartitions, MAXPARTITIONS); 114757dfbec5SPoul-Henning Kamp } else { 114857dfbec5SPoul-Henning Kamp struct disklabel *vl; 114957dfbec5SPoul-Henning Kamp 115057dfbec5SPoul-Henning Kamp vl = getvirginlabel(); 115157dfbec5SPoul-Henning Kamp lp->d_secsize = vl->d_secsize; 115257dfbec5SPoul-Henning Kamp lp->d_nsectors = vl->d_nsectors; 115357dfbec5SPoul-Henning Kamp lp->d_ntracks = vl->d_ntracks; 115457dfbec5SPoul-Henning Kamp lp->d_ncylinders = vl->d_ncylinders; 115557dfbec5SPoul-Henning Kamp lp->d_rpm = vl->d_rpm; 115657dfbec5SPoul-Henning Kamp lp->d_interleave = vl->d_interleave; 115757dfbec5SPoul-Henning Kamp lp->d_secpercyl = vl->d_secpercyl; 115857dfbec5SPoul-Henning Kamp lp->d_secperunit = vl->d_secperunit; 115957dfbec5SPoul-Henning Kamp lp->d_bbsize = vl->d_bbsize; 116057dfbec5SPoul-Henning Kamp lp->d_npartitions = vl->d_npartitions; 116157dfbec5SPoul-Henning Kamp } 116257dfbec5SPoul-Henning Kamp 11633233afaeSJohn W. De Boskey 11643233afaeSJohn W. De Boskey /* first allocate space to the partitions, then offsets */ 11653233afaeSJohn W. De Boskey total_size = 0; /* in sectors */ 11663233afaeSJohn W. De Boskey total_percent = 0; /* in percent */ 11673233afaeSJohn W. De Boskey hog_part = -1; 11683233afaeSJohn W. De Boskey /* find all fixed partitions */ 11693233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 11703233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 11713233afaeSJohn W. De Boskey if (part_set[i]) { 11723233afaeSJohn W. De Boskey if (part_size_type[i] == '*') { 11736b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 11743233afaeSJohn W. De Boskey pp->p_size = lp->d_secperunit; 11753233afaeSJohn W. De Boskey } else { 11763233afaeSJohn W. De Boskey if (hog_part != -1) 117757dfbec5SPoul-Henning Kamp warnx("Too many '*' partitions (%c and %c)", 11783233afaeSJohn W. De Boskey hog_part + 'a',i + 'a'); 11793233afaeSJohn W. De Boskey else 11803233afaeSJohn W. De Boskey hog_part = i; 11813233afaeSJohn W. De Boskey } 11823233afaeSJohn W. De Boskey } else { 11838d3105e8SWarner Losh off_t size; 11843233afaeSJohn W. De Boskey 11853233afaeSJohn W. De Boskey size = pp->p_size; 11863233afaeSJohn W. De Boskey switch (part_size_type[i]) { 11873233afaeSJohn W. De Boskey case '%': 11883233afaeSJohn W. De Boskey total_percent += size; 11893233afaeSJohn W. De Boskey break; 11904c814dfcSDag-Erling Smørgrav case 't': 11914c814dfcSDag-Erling Smørgrav case 'T': 11924c814dfcSDag-Erling Smørgrav size *= 1024ULL; 11934c814dfcSDag-Erling Smørgrav /* FALLTHROUGH */ 11944c814dfcSDag-Erling Smørgrav case 'g': 11954c814dfcSDag-Erling Smørgrav case 'G': 11964c814dfcSDag-Erling Smørgrav size *= 1024ULL; 11974c814dfcSDag-Erling Smørgrav /* FALLTHROUGH */ 11984c814dfcSDag-Erling Smørgrav case 'm': 11994c814dfcSDag-Erling Smørgrav case 'M': 12004c814dfcSDag-Erling Smørgrav size *= 1024ULL; 12014c814dfcSDag-Erling Smørgrav /* FALLTHROUGH */ 12023233afaeSJohn W. De Boskey case 'k': 12033233afaeSJohn W. De Boskey case 'K': 12048d3105e8SWarner Losh size *= 1024ULL; 12053233afaeSJohn W. De Boskey break; 12063233afaeSJohn W. De Boskey case '\0': 12073233afaeSJohn W. De Boskey break; 12083233afaeSJohn W. De Boskey default: 120981807292SDag-Erling Smørgrav warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)", 12104c814dfcSDag-Erling Smørgrav part_size_type[i], i + 'a'); 12113233afaeSJohn W. De Boskey break; 12123233afaeSJohn W. De Boskey } 12133233afaeSJohn W. De Boskey /* don't count %'s yet */ 12143233afaeSJohn W. De Boskey if (part_size_type[i] != '%') { 12153233afaeSJohn W. De Boskey /* 12163233afaeSJohn W. De Boskey * for all not in sectors, convert to 12173233afaeSJohn W. De Boskey * sectors 12183233afaeSJohn W. De Boskey */ 12193233afaeSJohn W. De Boskey if (part_size_type[i] != '\0') { 12203233afaeSJohn W. De Boskey if (size % lp->d_secsize != 0) 122157dfbec5SPoul-Henning Kamp warnx("partition %c not an integer number of sectors", 12223233afaeSJohn W. De Boskey i + 'a'); 12233233afaeSJohn W. De Boskey size /= lp->d_secsize; 12243233afaeSJohn W. De Boskey pp->p_size = size; 12253233afaeSJohn W. De Boskey } 12263233afaeSJohn W. De Boskey /* else already in sectors */ 12276b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 12283233afaeSJohn W. De Boskey total_size += size; 12293233afaeSJohn W. De Boskey } 12303233afaeSJohn W. De Boskey } 12313233afaeSJohn W. De Boskey } 12323233afaeSJohn W. De Boskey } 12333b89beb1SIan Dowse 12343b89beb1SIan Dowse /* Find out the total free space, excluding the boot block area. */ 12353b89beb1SIan Dowse base_offset = BBSIZE / secsize; 12363b89beb1SIan Dowse free_space = 0; 12373b89beb1SIan Dowse for (i = 0; i < lp->d_npartitions; i++) { 12383b89beb1SIan Dowse pp = &lp->d_partitions[i]; 12393b89beb1SIan Dowse if (!part_set[i] || i == RAW_PART || 12403b89beb1SIan Dowse part_size_type[i] == '%' || part_size_type[i] == '*') 12413b89beb1SIan Dowse continue; 12423b89beb1SIan Dowse if (pp->p_offset > base_offset) 12433b89beb1SIan Dowse free_space += pp->p_offset - base_offset; 12443b89beb1SIan Dowse if (pp->p_offset + pp->p_size > base_offset) 12453b89beb1SIan Dowse base_offset = pp->p_offset + pp->p_size; 12463b89beb1SIan Dowse } 12473b89beb1SIan Dowse if (base_offset < lp->d_secperunit) 12483b89beb1SIan Dowse free_space += lp->d_secperunit - base_offset; 12493b89beb1SIan Dowse 12503233afaeSJohn W. De Boskey /* handle % partitions - note %'s don't need to add up to 100! */ 12513233afaeSJohn W. De Boskey if (total_percent != 0) { 12523233afaeSJohn W. De Boskey if (total_percent > 100) { 12536b0ff5f5SPoul-Henning Kamp fprintf(stderr,"total percentage %lu is greater than 100\n", 12543233afaeSJohn W. De Boskey total_percent); 12553233afaeSJohn W. De Boskey errors++; 12563233afaeSJohn W. De Boskey } 12573233afaeSJohn W. De Boskey 12583233afaeSJohn W. De Boskey if (free_space > 0) { 12593233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 12603233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 12613233afaeSJohn W. De Boskey if (part_set[i] && part_size_type[i] == '%') { 12623233afaeSJohn W. De Boskey /* careful of overflows! and integer roundoff */ 12633233afaeSJohn W. De Boskey pp->p_size = ((double)pp->p_size/100) * free_space; 12643233afaeSJohn W. De Boskey total_size += pp->p_size; 12653233afaeSJohn W. De Boskey 12663233afaeSJohn W. De Boskey /* FIX we can lose a sector or so due to roundoff per 12673233afaeSJohn W. De Boskey partition. A more complex algorithm could avoid that */ 12683233afaeSJohn W. De Boskey } 12693233afaeSJohn W. De Boskey } 12703233afaeSJohn W. De Boskey } else { 12713233afaeSJohn W. De Boskey fprintf(stderr, 12726b0ff5f5SPoul-Henning Kamp "%ld sectors available to give to '*' and '%%' partitions\n", 12733233afaeSJohn W. De Boskey free_space); 12743233afaeSJohn W. De Boskey errors++; 12753233afaeSJohn W. De Boskey /* fix? set all % partitions to size 0? */ 12763233afaeSJohn W. De Boskey } 12773233afaeSJohn W. De Boskey } 12783233afaeSJohn W. De Boskey /* give anything remaining to the hog partition */ 12793233afaeSJohn W. De Boskey if (hog_part != -1) { 12803b89beb1SIan Dowse /* 12813b89beb1SIan Dowse * Find the range of offsets usable by '*' partitions around 12823b89beb1SIan Dowse * the hog partition and how much space they need. 12833b89beb1SIan Dowse */ 12843b89beb1SIan Dowse needed = 0; 12853b89beb1SIan Dowse base_offset = BBSIZE / secsize; 12863b89beb1SIan Dowse for (i = hog_part - 1; i >= 0; i--) { 12873b89beb1SIan Dowse pp = &lp->d_partitions[i]; 12883b89beb1SIan Dowse if (!part_set[i] || i == RAW_PART) 12893b89beb1SIan Dowse continue; 12903b89beb1SIan Dowse if (part_offset_type[i] == '*') { 12913b89beb1SIan Dowse needed += pp->p_size; 12923b89beb1SIan Dowse continue; 12933b89beb1SIan Dowse } 12943b89beb1SIan Dowse base_offset = pp->p_offset + pp->p_size; 12953b89beb1SIan Dowse break; 12963b89beb1SIan Dowse } 12973b89beb1SIan Dowse current_offset = lp->d_secperunit; 12983b89beb1SIan Dowse for (i = lp->d_npartitions - 1; i > hog_part; i--) { 12993b89beb1SIan Dowse pp = &lp->d_partitions[i]; 13003b89beb1SIan Dowse if (!part_set[i] || i == RAW_PART) 13013b89beb1SIan Dowse continue; 13023b89beb1SIan Dowse if (part_offset_type[i] == '*') { 13033b89beb1SIan Dowse needed += pp->p_size; 13043b89beb1SIan Dowse continue; 13053b89beb1SIan Dowse } 13063b89beb1SIan Dowse current_offset = pp->p_offset; 13073b89beb1SIan Dowse } 13083b89beb1SIan Dowse 13093b89beb1SIan Dowse if (current_offset - base_offset <= needed) { 13103b89beb1SIan Dowse fprintf(stderr, "Cannot find space for partition %c\n", 13113b89beb1SIan Dowse hog_part + 'a'); 13123b89beb1SIan Dowse fprintf(stderr, 13133b89beb1SIan Dowse "Need more than %lu sectors between %lu and %lu\n", 13143b89beb1SIan Dowse needed, base_offset, current_offset); 13153b89beb1SIan Dowse errors++; 13163b89beb1SIan Dowse lp->d_partitions[hog_part].p_size = 0; 13173b89beb1SIan Dowse } else { 13183b89beb1SIan Dowse lp->d_partitions[hog_part].p_size = current_offset - 13193b89beb1SIan Dowse base_offset - needed; 13203b89beb1SIan Dowse total_size += lp->d_partitions[hog_part].p_size; 13213b89beb1SIan Dowse } 13223233afaeSJohn W. De Boskey } 13233233afaeSJohn W. De Boskey 13243233afaeSJohn W. De Boskey /* Now set the offsets for each partition */ 13253b89beb1SIan Dowse current_offset = BBSIZE / secsize; /* in sectors */ 13263233afaeSJohn W. De Boskey seen_default_offset = 0; 13273233afaeSJohn W. De Boskey for (i = 0; i < lp->d_npartitions; i++) { 13283233afaeSJohn W. De Boskey part = 'a' + i; 13293233afaeSJohn W. De Boskey pp = &lp->d_partitions[i]; 13303233afaeSJohn W. De Boskey if (part_set[i]) { 13313233afaeSJohn W. De Boskey if (part_offset_type[i] == '*') { 13326b0ff5f5SPoul-Henning Kamp if (i == RAW_PART) { 13333233afaeSJohn W. De Boskey pp->p_offset = 0; 13343233afaeSJohn W. De Boskey } else { 13353233afaeSJohn W. De Boskey pp->p_offset = current_offset; 13363233afaeSJohn W. De Boskey seen_default_offset = 1; 13373233afaeSJohn W. De Boskey } 13383233afaeSJohn W. De Boskey } else { 13393233afaeSJohn W. De Boskey /* allow them to be out of order for old-style tables */ 13403233afaeSJohn W. De Boskey if (pp->p_offset < current_offset && 1341f2f63257SGreg Lehey seen_default_offset && i != RAW_PART && 1342f2f63257SGreg Lehey pp->p_fstype != FS_VINUM) { 13433233afaeSJohn W. De Boskey fprintf(stderr, 13446b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 13456b0ff5f5SPoul-Henning Kamp (long)pp->p_offset,i+'a',current_offset); 13463233afaeSJohn W. De Boskey fprintf(stderr, 13473233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n"); 13483233afaeSJohn W. De Boskey errors++; 13493233afaeSJohn W. De Boskey } else if (pp->p_offset != current_offset && 13506b0ff5f5SPoul-Henning Kamp i != RAW_PART && seen_default_offset) { 13513233afaeSJohn W. De Boskey /* 13523233afaeSJohn W. De Boskey * this may give unneeded warnings if 13533233afaeSJohn W. De Boskey * partitions are out-of-order 13543233afaeSJohn W. De Boskey */ 135557dfbec5SPoul-Henning Kamp warnx( 13563233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld", 13576b0ff5f5SPoul-Henning Kamp (long)pp->p_offset, i + 'a', current_offset); 13583233afaeSJohn W. De Boskey } 13593233afaeSJohn W. De Boskey } 13606b0ff5f5SPoul-Henning Kamp if (i != RAW_PART) 13613233afaeSJohn W. De Boskey current_offset = pp->p_offset + pp->p_size; 13623233afaeSJohn W. De Boskey } 13633233afaeSJohn W. De Boskey } 13643233afaeSJohn W. De Boskey 13658fae3551SRodney W. Grimes for (i = 0; i < lp->d_npartitions; i++) { 13668fae3551SRodney W. Grimes part = 'a' + i; 13678fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 13688fae3551SRodney W. Grimes if (pp->p_size == 0 && pp->p_offset != 0) 136957dfbec5SPoul-Henning Kamp warnx("partition %c: size 0, but offset %lu", 13702a1deaaaSBruce Evans part, (u_long)pp->p_offset); 13718fae3551SRodney W. Grimes #ifdef notdef 13728fae3551SRodney W. Grimes if (pp->p_size % lp->d_secpercyl) 137357dfbec5SPoul-Henning Kamp warnx("partition %c: size %% cylinder-size != 0", 13748fae3551SRodney W. Grimes part); 13758fae3551SRodney W. Grimes if (pp->p_offset % lp->d_secpercyl) 137657dfbec5SPoul-Henning Kamp warnx("partition %c: offset %% cylinder-size != 0", 13778fae3551SRodney W. Grimes part); 13788fae3551SRodney W. Grimes #endif 13798fae3551SRodney W. Grimes if (pp->p_offset > lp->d_secperunit) { 13808fae3551SRodney W. Grimes fprintf(stderr, 13818fae3551SRodney W. Grimes "partition %c: offset past end of unit\n", part); 13828fae3551SRodney W. Grimes errors++; 13838fae3551SRodney W. Grimes } 13848fae3551SRodney W. Grimes if (pp->p_offset + pp->p_size > lp->d_secperunit) { 13858fae3551SRodney W. Grimes fprintf(stderr, 13868fae3551SRodney W. Grimes "partition %c: partition extends past end of unit\n", 13878fae3551SRodney W. Grimes part); 13888fae3551SRodney W. Grimes errors++; 13898fae3551SRodney W. Grimes } 139057dfbec5SPoul-Henning Kamp if (i == RAW_PART) { 13913233afaeSJohn W. De Boskey if (pp->p_fstype != FS_UNUSED) 139257dfbec5SPoul-Henning Kamp warnx("partition %c is not marked as unused!",part); 13933233afaeSJohn W. De Boskey if (pp->p_offset != 0) 139457dfbec5SPoul-Henning Kamp warnx("partition %c doesn't start at 0!",part); 13953233afaeSJohn W. De Boskey if (pp->p_size != lp->d_secperunit) 139657dfbec5SPoul-Henning Kamp warnx("partition %c doesn't cover the whole unit!",part); 13973233afaeSJohn W. De Boskey 13983233afaeSJohn W. De Boskey if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 13993233afaeSJohn W. De Boskey (pp->p_size != lp->d_secperunit)) { 140057dfbec5SPoul-Henning Kamp warnx("An incorrect partition %c may cause problems for " 14013233afaeSJohn W. De Boskey "standard system utilities",part); 14023233afaeSJohn W. De Boskey } 14033233afaeSJohn W. De Boskey } 14043233afaeSJohn W. De Boskey 14053233afaeSJohn W. De Boskey /* check for overlaps */ 14063233afaeSJohn W. De Boskey /* this will check for all possible overlaps once and only once */ 14073233afaeSJohn W. De Boskey for (j = 0; j < i; j++) { 14083233afaeSJohn W. De Boskey pp2 = &lp->d_partitions[j]; 1409f2f63257SGreg Lehey if (j != RAW_PART && i != RAW_PART && 1410f2f63257SGreg Lehey pp->p_fstype != FS_VINUM && 1411f2f63257SGreg Lehey pp2->p_fstype != FS_VINUM && 1412f2f63257SGreg Lehey part_set[i] && part_set[j]) { 14133233afaeSJohn W. De Boskey if (pp2->p_offset < pp->p_offset + pp->p_size && 14143233afaeSJohn W. De Boskey (pp2->p_offset + pp2->p_size > pp->p_offset || 14153233afaeSJohn W. De Boskey pp2->p_offset >= pp->p_offset)) { 14163233afaeSJohn W. De Boskey fprintf(stderr,"partitions %c and %c overlap!\n", 14173233afaeSJohn W. De Boskey j + 'a', i + 'a'); 14183233afaeSJohn W. De Boskey errors++; 14193233afaeSJohn W. De Boskey } 14203233afaeSJohn W. De Boskey } 14213233afaeSJohn W. De Boskey } 14228fae3551SRodney W. Grimes } 1423d5718812SMarcel Moolenaar for (; i < lp->d_npartitions; i++) { 14248fae3551SRodney W. Grimes part = 'a' + i; 14258fae3551SRodney W. Grimes pp = &lp->d_partitions[i]; 14268fae3551SRodney W. Grimes if (pp->p_size || pp->p_offset) 142757dfbec5SPoul-Henning Kamp warnx("unused partition %c: size %d offset %lu", 14282a1deaaaSBruce Evans 'a' + i, pp->p_size, (u_long)pp->p_offset); 14298fae3551SRodney W. Grimes } 14308fae3551SRodney W. Grimes return (errors); 14318fae3551SRodney W. Grimes } 14328fae3551SRodney W. Grimes 14338fae3551SRodney W. Grimes /* 1434425bed3aSJoerg Wunsch * When operating on a "virgin" disk, try getting an initial label 1435425bed3aSJoerg Wunsch * from the associated device driver. This might work for all device 1436425bed3aSJoerg Wunsch * drivers that are able to fetch some initial device parameters 1437425bed3aSJoerg Wunsch * without even having access to a (BSD) disklabel, like SCSI disks, 1438425bed3aSJoerg Wunsch * most IDE drives, or vn devices. 1439425bed3aSJoerg Wunsch * 1440425bed3aSJoerg Wunsch * The device name must be given in its "canonical" form. 1441425bed3aSJoerg Wunsch */ 1442d2fe97c7SPoul-Henning Kamp static struct disklabel * 1443425bed3aSJoerg Wunsch getvirginlabel(void) 1444425bed3aSJoerg Wunsch { 1445c8223965SMark Murray static struct disklabel loclab; 1446b9d05a16SPoul-Henning Kamp struct partition *dp; 1447425bed3aSJoerg Wunsch int f; 144857dfbec5SPoul-Henning Kamp u_int u; 1449425bed3aSJoerg Wunsch 1450d2fe97c7SPoul-Henning Kamp if ((f = open(specname, O_RDONLY)) == -1) { 1451d2fe97c7SPoul-Henning Kamp warn("cannot open %s", specname); 145243be698cSBruce Evans return (NULL); 1453425bed3aSJoerg Wunsch } 1454ff7d5162SJordan K. Hubbard 14557747c959SLuigi Rizzo if (is_file) 14567747c959SLuigi Rizzo get_file_parms(f); 14577a4b0bb2SUlf Lilleengen else { 14587a4b0bb2SUlf Lilleengen mediasize = g_mediasize(f); 14597a4b0bb2SUlf Lilleengen secsize = g_sectorsize(f); 14607a4b0bb2SUlf Lilleengen if (secsize < 0 || mediasize < 0) { 1461425bed3aSJoerg Wunsch close (f); 146243be698cSBruce Evans return (NULL); 1463425bed3aSJoerg Wunsch } 14647a4b0bb2SUlf Lilleengen } 1465b9d05a16SPoul-Henning Kamp memset(&loclab, 0, sizeof loclab); 1466b9d05a16SPoul-Henning Kamp loclab.d_magic = DISKMAGIC; 1467b9d05a16SPoul-Henning Kamp loclab.d_magic2 = DISKMAGIC; 1468b9d05a16SPoul-Henning Kamp loclab.d_secsize = secsize; 1469b9d05a16SPoul-Henning Kamp loclab.d_secperunit = mediasize / secsize; 1470b9d05a16SPoul-Henning Kamp 1471b9d05a16SPoul-Henning Kamp /* 1472b9d05a16SPoul-Henning Kamp * Nobody in these enligthened days uses the CHS geometry for 1473b9d05a16SPoul-Henning Kamp * anything, but nontheless try to get it right. If we fail 1474b9d05a16SPoul-Henning Kamp * to get any good ideas from the device, construct something 1475b9d05a16SPoul-Henning Kamp * which is IBM-PC friendly. 1476b9d05a16SPoul-Henning Kamp */ 1477b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1478b9d05a16SPoul-Henning Kamp loclab.d_nsectors = u; 1479b9d05a16SPoul-Henning Kamp else 1480b9d05a16SPoul-Henning Kamp loclab.d_nsectors = 63; 1481b9d05a16SPoul-Henning Kamp if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1482b9d05a16SPoul-Henning Kamp loclab.d_ntracks = u; 1483b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*1*1024) 1484b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 1; 1485b9d05a16SPoul-Henning Kamp else if (loclab.d_secperunit <= 63*16*1024) 1486b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 16; 1487b9d05a16SPoul-Henning Kamp else 1488b9d05a16SPoul-Henning Kamp loclab.d_ntracks = 255; 1489b9d05a16SPoul-Henning Kamp loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1490b9d05a16SPoul-Henning Kamp loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1491d5718812SMarcel Moolenaar loclab.d_npartitions = DEFPARTITIONS; 1492b9d05a16SPoul-Henning Kamp 1493b9d05a16SPoul-Henning Kamp /* Various (unneeded) compat stuff */ 1494b9d05a16SPoul-Henning Kamp loclab.d_rpm = 3600; 1495b9d05a16SPoul-Henning Kamp loclab.d_bbsize = BBSIZE; 149657dfbec5SPoul-Henning Kamp loclab.d_interleave = 1; 1497b9d05a16SPoul-Henning Kamp strncpy(loclab.d_typename, "amnesiac", 1498b9d05a16SPoul-Henning Kamp sizeof(loclab.d_typename)); 1499b9d05a16SPoul-Henning Kamp 1500b9d05a16SPoul-Henning Kamp dp = &loclab.d_partitions[RAW_PART]; 1501b9d05a16SPoul-Henning Kamp dp->p_size = loclab.d_secperunit; 1502b9d05a16SPoul-Henning Kamp loclab.d_checksum = dkcksum(&loclab); 1503425bed3aSJoerg Wunsch close (f); 1504c8223965SMark Murray return (&loclab); 1505425bed3aSJoerg Wunsch } 1506425bed3aSJoerg Wunsch 1507d2fe97c7SPoul-Henning Kamp static void 1508326c7cdaSWarner Losh usage(void) 15098fae3551SRodney W. Grimes { 1510bc33ea1aSRuslan Ermilov 1511bc33ea1aSRuslan Ermilov fprintf(stderr, 1512bc33ea1aSRuslan Ermilov "%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", 15135daa806dSPoul-Henning Kamp "usage: bsdlabel disk", 1514bef2080aSPhilippe Charnier "\t\t(to read label)", 1515d2fe97c7SPoul-Henning Kamp " bsdlabel -w [-n] [-m machine] disk [type]", 1516bef2080aSPhilippe Charnier "\t\t(to write label with existing boot program)", 15175daa806dSPoul-Henning Kamp " bsdlabel -e [-n] [-m machine] disk", 1518bef2080aSPhilippe Charnier "\t\t(to edit label)", 15195daa806dSPoul-Henning Kamp " bsdlabel -R [-n] [-m machine] disk protofile", 1520bef2080aSPhilippe Charnier "\t\t(to restore label with existing boot program)", 15212c60b668SPoul-Henning Kamp " bsdlabel -B [-b boot] [-m machine] disk", 1522bef2080aSPhilippe Charnier "\t\t(to install boot program with existing on-disk label)", 1523d2fe97c7SPoul-Henning Kamp " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]", 1524bef2080aSPhilippe Charnier "\t\t(to write label and install boot program)", 15252c60b668SPoul-Henning Kamp " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile", 152680baf8ceSPoul-Henning Kamp "\t\t(to restore label and install boot program)" 152780baf8ceSPoul-Henning Kamp ); 15288fae3551SRodney W. Grimes exit(1); 15298fae3551SRodney W. Grimes } 1530