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 * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3426a2df73SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint) 35cc0ea346SBruce Evans static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; 3626a2df73SDavid E. O'Brien #endif /* LIBC_SCCS and not lint */ 37b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 38b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #include <sys/param.h> 4158f0484fSRodney W. Grimes #define DKTYPENAMES 42b8606fe6SBosko Milekic #define FSTYPENAMES 4358f0484fSRodney W. Grimes #include <sys/disklabel.h> 4458f0484fSRodney W. Grimes 4558f0484fSRodney W. Grimes #include <fcntl.h> 4658f0484fSRodney W. Grimes #include <stdio.h> 4758f0484fSRodney W. Grimes #include <stdlib.h> 4858f0484fSRodney W. Grimes #include <string.h> 4958f0484fSRodney W. Grimes #include <unistd.h> 508b102407SPoul-Henning Kamp #include <ctype.h> 5158f0484fSRodney W. Grimes 52674a5ae3SPoul-Henning Kamp static int 53674a5ae3SPoul-Henning Kamp gettype(char *t, const char **names) 54674a5ae3SPoul-Henning Kamp { 55674a5ae3SPoul-Henning Kamp const char **nm; 56674a5ae3SPoul-Henning Kamp 57674a5ae3SPoul-Henning Kamp for (nm = names; *nm; nm++) 58674a5ae3SPoul-Henning Kamp if (strcasecmp(t, *nm) == 0) 59674a5ae3SPoul-Henning Kamp return (nm - names); 60674a5ae3SPoul-Henning Kamp if (isdigit((unsigned char)*t)) 61674a5ae3SPoul-Henning Kamp return (atoi(t)); 62674a5ae3SPoul-Henning Kamp return (0); 63674a5ae3SPoul-Henning Kamp } 6458f0484fSRodney W. Grimes 6558f0484fSRodney W. Grimes struct disklabel * 66674a5ae3SPoul-Henning Kamp getdiskbyname(const char *name) 6758f0484fSRodney W. Grimes { 6858f0484fSRodney W. Grimes static struct disklabel disk; 69b231cb39SDavid E. O'Brien struct disklabel *dp = &disk; 70b231cb39SDavid E. O'Brien struct partition *pp; 7158f0484fSRodney W. Grimes char *buf; 7258f0484fSRodney W. Grimes char *db_array[2] = { _PATH_DISKTAB, 0 }; 7358f0484fSRodney W. Grimes char *cp, *cq; /* can't be register */ 7458f0484fSRodney W. Grimes char p, max, psize[3], pbsize[3], 7558f0484fSRodney W. Grimes pfsize[3], poffset[3], ptype[3]; 76cc0ea346SBruce Evans u_int32_t *dx; 7758f0484fSRodney W. Grimes 7858f0484fSRodney W. Grimes if (cgetent(&buf, db_array, (char *) name) < 0) 7958f0484fSRodney W. Grimes return NULL; 8058f0484fSRodney W. Grimes 8158f0484fSRodney W. Grimes bzero((char *)&disk, sizeof(disk)); 8258f0484fSRodney W. Grimes /* 8358f0484fSRodney W. Grimes * typename 8458f0484fSRodney W. Grimes */ 8558f0484fSRodney W. Grimes cq = dp->d_typename; 8658f0484fSRodney W. Grimes cp = buf; 8758f0484fSRodney W. Grimes while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 8858f0484fSRodney W. Grimes (*cq = *cp) && *cq != '|' && *cq != ':') 8958f0484fSRodney W. Grimes cq++, cp++; 9058f0484fSRodney W. Grimes *cq = '\0'; 9158f0484fSRodney W. Grimes 9258f0484fSRodney W. Grimes if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) 9358f0484fSRodney W. Grimes dp->d_flags |= D_REMOVABLE; 9458f0484fSRodney W. Grimes else if (cq && strcmp(cq, "simulated") == 0) 9558f0484fSRodney W. Grimes dp->d_flags |= D_RAMDISK; 9658f0484fSRodney W. Grimes if (cgetcap(buf, "sf", ':') != NULL) 9758f0484fSRodney W. Grimes dp->d_flags |= D_BADSECT; 9858f0484fSRodney W. Grimes 9958f0484fSRodney W. Grimes #define getnumdflt(field, dname, dflt) \ 10058f0484fSRodney W. Grimes { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 10158f0484fSRodney W. Grimes 10258f0484fSRodney W. Grimes getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 103f0a36920SDoug Rabson getnumdflt(dp->d_ntracks, "nt", 0); 104f0a36920SDoug Rabson getnumdflt(dp->d_nsectors, "ns", 0); 105f0a36920SDoug Rabson getnumdflt(dp->d_ncylinders, "nc", 0); 10658f0484fSRodney W. Grimes 10758f0484fSRodney W. Grimes if (cgetstr(buf, "dt", &cq) > 0) 10858f0484fSRodney W. Grimes dp->d_type = gettype(cq, dktypenames); 10958f0484fSRodney W. Grimes 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); 14758f0484fSRodney W. Grimes if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 14858f0484fSRodney W. Grimes pp->p_fstype = gettype(cq, fstypenames); 14958f0484fSRodney W. Grimes max = p; 15058f0484fSRodney W. Grimes } 15158f0484fSRodney W. Grimes } 15258f0484fSRodney W. Grimes dp->d_npartitions = max + 1 - 'a'; 15358f0484fSRodney W. Grimes (void)strcpy(psize, "dx"); 15458f0484fSRodney W. Grimes dx = dp->d_drivedata; 15558f0484fSRodney W. Grimes for (p = '0'; p < '0' + NDDATA; p++, dx++) { 15658f0484fSRodney W. Grimes psize[1] = p; 15758f0484fSRodney W. Grimes getnumdflt(*dx, psize, 0); 15858f0484fSRodney W. Grimes } 15958f0484fSRodney W. Grimes dp->d_magic = DISKMAGIC; 16058f0484fSRodney W. Grimes dp->d_magic2 = DISKMAGIC; 16158f0484fSRodney W. Grimes free(buf); 16258f0484fSRodney W. Grimes return (dp); 16358f0484fSRodney W. Grimes } 164