158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1983, 1987, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1558f0484fSRodney W. Grimes * without specific prior written permission. 1658f0484fSRodney W. Grimes * 1758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2758f0484fSRodney W. Grimes * SUCH DAMAGE. 2858f0484fSRodney W. Grimes */ 2958f0484fSRodney W. Grimes 3026a2df73SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint) 31cc0ea346SBruce Evans static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; 3226a2df73SDavid E. O'Brien #endif /* LIBC_SCCS and not lint */ 33b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 34b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3558f0484fSRodney W. Grimes 3658f0484fSRodney W. Grimes #include <sys/param.h> 3758f0484fSRodney W. Grimes #define DKTYPENAMES 38b8606fe6SBosko Milekic #define FSTYPENAMES 3958f0484fSRodney W. Grimes #include <sys/disklabel.h> 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <fcntl.h> 4258f0484fSRodney W. Grimes #include <stdio.h> 4358f0484fSRodney W. Grimes #include <stdlib.h> 4458f0484fSRodney W. Grimes #include <string.h> 4558f0484fSRodney W. Grimes #include <unistd.h> 468b102407SPoul-Henning Kamp #include <ctype.h> 4758f0484fSRodney W. Grimes 48674a5ae3SPoul-Henning Kamp static int 49674a5ae3SPoul-Henning Kamp gettype(char *t, const char **names) 50674a5ae3SPoul-Henning Kamp { 51674a5ae3SPoul-Henning Kamp const char **nm; 52674a5ae3SPoul-Henning Kamp 53674a5ae3SPoul-Henning Kamp for (nm = names; *nm; nm++) 54674a5ae3SPoul-Henning Kamp if (strcasecmp(t, *nm) == 0) 55674a5ae3SPoul-Henning Kamp return (nm - names); 56674a5ae3SPoul-Henning Kamp if (isdigit((unsigned char)*t)) 57674a5ae3SPoul-Henning Kamp return (atoi(t)); 58674a5ae3SPoul-Henning Kamp return (0); 59674a5ae3SPoul-Henning Kamp } 6058f0484fSRodney W. Grimes 6158f0484fSRodney W. Grimes struct disklabel * 62674a5ae3SPoul-Henning Kamp getdiskbyname(const char *name) 6358f0484fSRodney W. Grimes { 6458f0484fSRodney W. Grimes static struct disklabel disk; 65b231cb39SDavid E. O'Brien struct disklabel *dp = &disk; 66b231cb39SDavid E. O'Brien struct partition *pp; 6758f0484fSRodney W. Grimes char *buf; 6858f0484fSRodney W. Grimes char *db_array[2] = { _PATH_DISKTAB, 0 }; 6958f0484fSRodney W. Grimes char *cp, *cq; /* can't be register */ 7058f0484fSRodney W. Grimes char p, max, psize[3], pbsize[3], 7158f0484fSRodney W. Grimes pfsize[3], poffset[3], ptype[3]; 72cc0ea346SBruce Evans u_int32_t *dx; 7358f0484fSRodney W. Grimes 7458f0484fSRodney W. Grimes if (cgetent(&buf, db_array, (char *) name) < 0) 7558f0484fSRodney W. Grimes return NULL; 7658f0484fSRodney W. Grimes 7758f0484fSRodney W. Grimes bzero((char *)&disk, sizeof(disk)); 7858f0484fSRodney W. Grimes /* 7958f0484fSRodney W. Grimes * typename 8058f0484fSRodney W. Grimes */ 8158f0484fSRodney W. Grimes cq = dp->d_typename; 8258f0484fSRodney W. Grimes cp = buf; 8358f0484fSRodney W. Grimes while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 8458f0484fSRodney W. Grimes (*cq = *cp) && *cq != '|' && *cq != ':') 8558f0484fSRodney W. Grimes cq++, cp++; 8658f0484fSRodney W. Grimes *cq = '\0'; 8758f0484fSRodney W. Grimes 88*5262b957SPedro F. Giffuni if (cgetstr(buf, "ty", &cq) > 0) { 89*5262b957SPedro F. Giffuni if (strcmp(cq, "removable") == 0) 9058f0484fSRodney W. Grimes dp->d_flags |= D_REMOVABLE; 9158f0484fSRodney W. Grimes else if (cq && strcmp(cq, "simulated") == 0) 9258f0484fSRodney W. Grimes dp->d_flags |= D_RAMDISK; 93*5262b957SPedro F. Giffuni free(cq); 94*5262b957SPedro F. Giffuni } 9558f0484fSRodney W. Grimes if (cgetcap(buf, "sf", ':') != NULL) 9658f0484fSRodney W. Grimes dp->d_flags |= D_BADSECT; 9758f0484fSRodney W. Grimes 9858f0484fSRodney W. Grimes #define getnumdflt(field, dname, dflt) \ 9958f0484fSRodney W. Grimes { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 10058f0484fSRodney W. Grimes 10158f0484fSRodney W. Grimes getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 102f0a36920SDoug Rabson getnumdflt(dp->d_ntracks, "nt", 0); 103f0a36920SDoug Rabson getnumdflt(dp->d_nsectors, "ns", 0); 104f0a36920SDoug Rabson getnumdflt(dp->d_ncylinders, "nc", 0); 10558f0484fSRodney W. Grimes 106*5262b957SPedro F. Giffuni if (cgetstr(buf, "dt", &cq) > 0) { 10758f0484fSRodney W. Grimes dp->d_type = gettype(cq, dktypenames); 108*5262b957SPedro F. Giffuni free(cq); 109*5262b957SPedro F. Giffuni } else 11058f0484fSRodney W. Grimes getnumdflt(dp->d_type, "dt", 0); 11158f0484fSRodney W. Grimes getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 11258f0484fSRodney W. Grimes getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 11358f0484fSRodney W. Grimes getnumdflt(dp->d_rpm, "rm", 3600); 11458f0484fSRodney W. Grimes getnumdflt(dp->d_interleave, "il", 1); 11558f0484fSRodney W. Grimes getnumdflt(dp->d_trackskew, "sk", 0); 11658f0484fSRodney W. Grimes getnumdflt(dp->d_cylskew, "cs", 0); 11758f0484fSRodney W. Grimes getnumdflt(dp->d_headswitch, "hs", 0); 11858f0484fSRodney W. Grimes getnumdflt(dp->d_trkseek, "ts", 0); 11958f0484fSRodney W. Grimes getnumdflt(dp->d_bbsize, "bs", BBSIZE); 12077068a7fSPoul-Henning Kamp getnumdflt(dp->d_sbsize, "sb", 0); 12158f0484fSRodney W. Grimes strcpy(psize, "px"); 12258f0484fSRodney W. Grimes strcpy(pbsize, "bx"); 12358f0484fSRodney W. Grimes strcpy(pfsize, "fx"); 12458f0484fSRodney W. Grimes strcpy(poffset, "ox"); 12558f0484fSRodney W. Grimes strcpy(ptype, "tx"); 12658f0484fSRodney W. Grimes max = 'a' - 1; 12758f0484fSRodney W. Grimes pp = &dp->d_partitions[0]; 12858f0484fSRodney W. Grimes for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 129f0a36920SDoug Rabson long l; 13058f0484fSRodney W. Grimes psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 131f0a36920SDoug Rabson if (cgetnum(buf, psize, &l) == -1) 13258f0484fSRodney W. Grimes pp->p_size = 0; 13358f0484fSRodney W. Grimes else { 134f0a36920SDoug Rabson pp->p_size = l; 135f0a36920SDoug Rabson cgetnum(buf, poffset, &l); 136f0a36920SDoug Rabson pp->p_offset = l; 13758f0484fSRodney W. Grimes getnumdflt(pp->p_fsize, pfsize, 0); 13858f0484fSRodney W. Grimes if (pp->p_fsize) { 13958f0484fSRodney W. Grimes long bsize; 14058f0484fSRodney W. Grimes 14158f0484fSRodney W. Grimes if (cgetnum(buf, pbsize, &bsize) == 0) 14258f0484fSRodney W. Grimes pp->p_frag = bsize / pp->p_fsize; 14358f0484fSRodney W. Grimes else 14458f0484fSRodney W. Grimes pp->p_frag = 8; 14558f0484fSRodney W. Grimes } 14658f0484fSRodney W. Grimes getnumdflt(pp->p_fstype, ptype, 0); 147*5262b957SPedro F. Giffuni if (pp->p_fstype == 0) 148*5262b957SPedro F. Giffuni if (cgetstr(buf, ptype, &cq) >= 0) { 14958f0484fSRodney W. Grimes pp->p_fstype = gettype(cq, fstypenames); 150*5262b957SPedro F. Giffuni free(cq); 151*5262b957SPedro F. Giffuni } 15258f0484fSRodney W. Grimes max = p; 15358f0484fSRodney W. Grimes } 15458f0484fSRodney W. Grimes } 15558f0484fSRodney W. Grimes dp->d_npartitions = max + 1 - 'a'; 15658f0484fSRodney W. Grimes (void)strcpy(psize, "dx"); 15758f0484fSRodney W. Grimes dx = dp->d_drivedata; 15858f0484fSRodney W. Grimes for (p = '0'; p < '0' + NDDATA; p++, dx++) { 15958f0484fSRodney W. Grimes psize[1] = p; 16058f0484fSRodney W. Grimes getnumdflt(*dx, psize, 0); 16158f0484fSRodney W. Grimes } 16258f0484fSRodney W. Grimes dp->d_magic = DISKMAGIC; 16358f0484fSRodney W. Grimes dp->d_magic2 = DISKMAGIC; 16458f0484fSRodney W. Grimes free(buf); 165*5262b957SPedro F. Giffuni (void)cgetclose(); 16658f0484fSRodney W. Grimes return (dp); 16758f0484fSRodney W. Grimes } 168