11573Srgrimes/* 21573Srgrimes * Copyright (c) 1983, 1987, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 4. Neither the name of the University nor the names of its contributors 141573Srgrimes * may be used to endorse or promote products derived from this software 151573Srgrimes * without specific prior written permission. 161573Srgrimes * 171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271573Srgrimes * SUCH DAMAGE. 281573Srgrimes */ 291573Srgrimes 3090049Sobrien#if defined(LIBC_SCCS) && !defined(lint) 3114698Sbdestatic char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; 3290049Sobrien#endif /* LIBC_SCCS and not lint */ 3390045Sobrien#include <sys/cdefs.h> 3490045Sobrien__FBSDID("$FreeBSD$"); 351573Srgrimes 361573Srgrimes#include <sys/param.h> 371573Srgrimes#define DKTYPENAMES 38101993Sbmilekic#define FSTYPENAMES 391573Srgrimes#include <sys/disklabel.h> 401573Srgrimes 411573Srgrimes#include <fcntl.h> 421573Srgrimes#include <stdio.h> 431573Srgrimes#include <stdlib.h> 441573Srgrimes#include <string.h> 451573Srgrimes#include <unistd.h> 4611659Sphk#include <ctype.h> 471573Srgrimes 4894181Sphkstatic int 4994181Sphkgettype(char *t, const char **names) 5094181Sphk{ 5194181Sphk const char **nm; 521573Srgrimes 5394181Sphk for (nm = names; *nm; nm++) 5494181Sphk if (strcasecmp(t, *nm) == 0) 5594181Sphk return (nm - names); 5694181Sphk if (isdigit((unsigned char)*t)) 5794181Sphk return (atoi(t)); 5894181Sphk return (0); 5994181Sphk} 6094181Sphk 611573Srgrimesstruct disklabel * 6294181Sphkgetdiskbyname(const char *name) 631573Srgrimes{ 641573Srgrimes static struct disklabel disk; 6590045Sobrien struct disklabel *dp = &disk; 6690045Sobrien struct partition *pp; 671573Srgrimes char *buf; 681573Srgrimes char *db_array[2] = { _PATH_DISKTAB, 0 }; 691573Srgrimes char *cp, *cq; /* can't be register */ 701573Srgrimes char p, max, psize[3], pbsize[3], 711573Srgrimes pfsize[3], poffset[3], ptype[3]; 7214698Sbde u_int32_t *dx; 731573Srgrimes 741573Srgrimes if (cgetent(&buf, db_array, (char *) name) < 0) 751573Srgrimes return NULL; 761573Srgrimes 771573Srgrimes bzero((char *)&disk, sizeof(disk)); 781573Srgrimes /* 791573Srgrimes * typename 801573Srgrimes */ 811573Srgrimes cq = dp->d_typename; 821573Srgrimes cp = buf; 831573Srgrimes while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && 841573Srgrimes (*cq = *cp) && *cq != '|' && *cq != ':') 851573Srgrimes cq++, cp++; 861573Srgrimes *cq = '\0'; 871573Srgrimes 881573Srgrimes if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) 891573Srgrimes dp->d_flags |= D_REMOVABLE; 901573Srgrimes else if (cq && strcmp(cq, "simulated") == 0) 911573Srgrimes dp->d_flags |= D_RAMDISK; 921573Srgrimes if (cgetcap(buf, "sf", ':') != NULL) 931573Srgrimes dp->d_flags |= D_BADSECT; 941573Srgrimes 951573Srgrimes#define getnumdflt(field, dname, dflt) \ 961573Srgrimes { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } 971573Srgrimes 981573Srgrimes getnumdflt(dp->d_secsize, "se", DEV_BSIZE); 9939684Sdfr getnumdflt(dp->d_ntracks, "nt", 0); 10039684Sdfr getnumdflt(dp->d_nsectors, "ns", 0); 10139684Sdfr getnumdflt(dp->d_ncylinders, "nc", 0); 1021573Srgrimes 1031573Srgrimes if (cgetstr(buf, "dt", &cq) > 0) 1041573Srgrimes dp->d_type = gettype(cq, dktypenames); 1051573Srgrimes else 1061573Srgrimes getnumdflt(dp->d_type, "dt", 0); 1071573Srgrimes getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); 1081573Srgrimes getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); 1091573Srgrimes getnumdflt(dp->d_rpm, "rm", 3600); 1101573Srgrimes getnumdflt(dp->d_interleave, "il", 1); 1111573Srgrimes getnumdflt(dp->d_trackskew, "sk", 0); 1121573Srgrimes getnumdflt(dp->d_cylskew, "cs", 0); 1131573Srgrimes getnumdflt(dp->d_headswitch, "hs", 0); 1141573Srgrimes getnumdflt(dp->d_trkseek, "ts", 0); 1151573Srgrimes getnumdflt(dp->d_bbsize, "bs", BBSIZE); 11696475Sphk getnumdflt(dp->d_sbsize, "sb", 0); 1171573Srgrimes strcpy(psize, "px"); 1181573Srgrimes strcpy(pbsize, "bx"); 1191573Srgrimes strcpy(pfsize, "fx"); 1201573Srgrimes strcpy(poffset, "ox"); 1211573Srgrimes strcpy(ptype, "tx"); 1221573Srgrimes max = 'a' - 1; 1231573Srgrimes pp = &dp->d_partitions[0]; 1241573Srgrimes for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { 12539684Sdfr long l; 1261573Srgrimes psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; 12739684Sdfr if (cgetnum(buf, psize, &l) == -1) 1281573Srgrimes pp->p_size = 0; 1291573Srgrimes else { 13039684Sdfr pp->p_size = l; 13139684Sdfr cgetnum(buf, poffset, &l); 13239684Sdfr pp->p_offset = l; 1331573Srgrimes getnumdflt(pp->p_fsize, pfsize, 0); 1341573Srgrimes if (pp->p_fsize) { 1351573Srgrimes long bsize; 1361573Srgrimes 1371573Srgrimes if (cgetnum(buf, pbsize, &bsize) == 0) 1381573Srgrimes pp->p_frag = bsize / pp->p_fsize; 1391573Srgrimes else 1401573Srgrimes pp->p_frag = 8; 1411573Srgrimes } 1421573Srgrimes getnumdflt(pp->p_fstype, ptype, 0); 1431573Srgrimes if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) 1441573Srgrimes pp->p_fstype = gettype(cq, fstypenames); 1451573Srgrimes max = p; 1461573Srgrimes } 1471573Srgrimes } 1481573Srgrimes dp->d_npartitions = max + 1 - 'a'; 1491573Srgrimes (void)strcpy(psize, "dx"); 1501573Srgrimes dx = dp->d_drivedata; 1511573Srgrimes for (p = '0'; p < '0' + NDDATA; p++, dx++) { 1521573Srgrimes psize[1] = p; 1531573Srgrimes getnumdflt(*dx, psize, 0); 1541573Srgrimes } 1551573Srgrimes dp->d_magic = DISKMAGIC; 1561573Srgrimes dp->d_magic2 = DISKMAGIC; 1571573Srgrimes free(buf); 1581573Srgrimes return (dp); 1591573Srgrimes} 160