Home | History | Annotate | Line # | Download | only in gen
disklabel.c revision 1.1.1.3
      1 /*
      2  * Copyright (c) 1983, 1987, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 5/3/95";
     36 #endif /* not lint */
     37 
     38 #include <sys/param.h>
     39 #define DKTYPENAMES
     40 #include <sys/disklabel.h>
     41 #include <ufs/ufs/dinode.h>
     42 #include <ufs/ffs/fs.h>
     43 
     44 #include <errno.h>
     45 #include <fcntl.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <unistd.h>
     50 
     51 static int	error __P((int));
     52 static int	gettype __P((char *, char **));
     53 
     54 struct disklabel *
     55 getdiskbyname(name)
     56 	const char *name;
     57 {
     58 	static struct	disklabel disk;
     59 	register struct	disklabel *dp = &disk;
     60 	register struct partition *pp;
     61 	char	*buf;
     62 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
     63 	char	*cp, *cq;	/* can't be register */
     64 	char	p, max, psize[3], pbsize[3],
     65 		pfsize[3], poffset[3], ptype[3];
     66 	u_int32_t *dx;
     67 
     68 	if (cgetent(&buf, db_array, (char *) name) < 0)
     69 		return NULL;
     70 
     71 	bzero((char *)&disk, sizeof(disk));
     72 	/*
     73 	 * typename
     74 	 */
     75 	cq = dp->d_typename;
     76 	cp = buf;
     77 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
     78 	    (*cq = *cp) && *cq != '|' && *cq != ':')
     79 		cq++, cp++;
     80 	*cq = '\0';
     81 	/*
     82 	 * boot name (optional)  xxboot, bootxx
     83 	 */
     84 	cgetstr(buf, "b0", &dp->d_boot0);
     85 	cgetstr(buf, "b1", &dp->d_boot1);
     86 
     87 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
     88 		dp->d_flags |= D_REMOVABLE;
     89 	else  if (cq && strcmp(cq, "simulated") == 0)
     90 		dp->d_flags |= D_RAMDISK;
     91 	if (cgetcap(buf, "sf", ':') != NULL)
     92 		dp->d_flags |= D_BADSECT;
     93 
     94 #define getnumdflt(field, dname, dflt) \
     95         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
     96 
     97 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
     98 	cgetnum(buf, "nt",(long *) &dp->d_ntracks);
     99 	cgetnum(buf, "ns",(long *) &dp->d_nsectors);
    100 	cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
    101 
    102 	if (cgetstr(buf, "dt", &cq) > 0)
    103 		dp->d_type = gettype(cq, dktypenames);
    104 	else
    105 		getnumdflt(dp->d_type, "dt", 0);
    106 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
    107 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
    108 	getnumdflt(dp->d_rpm, "rm", 3600);
    109 	getnumdflt(dp->d_interleave, "il", 1);
    110 	getnumdflt(dp->d_trackskew, "sk", 0);
    111 	getnumdflt(dp->d_cylskew, "cs", 0);
    112 	getnumdflt(dp->d_headswitch, "hs", 0);
    113 	getnumdflt(dp->d_trkseek, "ts", 0);
    114 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
    115 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
    116 	strcpy(psize, "px");
    117 	strcpy(pbsize, "bx");
    118 	strcpy(pfsize, "fx");
    119 	strcpy(poffset, "ox");
    120 	strcpy(ptype, "tx");
    121 	max = 'a' - 1;
    122 	pp = &dp->d_partitions[0];
    123 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
    124 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
    125 		if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
    126 			pp->p_size = 0;
    127 		else {
    128 			cgetnum(buf, poffset, (long *) &pp->p_offset);
    129 			getnumdflt(pp->p_fsize, pfsize, 0);
    130 			if (pp->p_fsize) {
    131 				long bsize;
    132 
    133 				if (cgetnum(buf, pbsize, &bsize) == 0)
    134 					pp->p_frag = bsize / pp->p_fsize;
    135 				else
    136 					pp->p_frag = 8;
    137 			}
    138 			getnumdflt(pp->p_fstype, ptype, 0);
    139 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
    140 				pp->p_fstype = gettype(cq, fstypenames);
    141 			max = p;
    142 		}
    143 	}
    144 	dp->d_npartitions = max + 1 - 'a';
    145 	(void)strcpy(psize, "dx");
    146 	dx = dp->d_drivedata;
    147 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
    148 		psize[1] = p;
    149 		getnumdflt(*dx, psize, 0);
    150 	}
    151 	dp->d_magic = DISKMAGIC;
    152 	dp->d_magic2 = DISKMAGIC;
    153 	free(buf);
    154 	return (dp);
    155 }
    156 
    157 static int
    158 gettype(t, names)
    159 	char *t;
    160 	char **names;
    161 {
    162 	register char **nm;
    163 
    164 	for (nm = names; *nm; nm++)
    165 		if (strcasecmp(t, *nm) == 0)
    166 			return (nm - names);
    167 	if (isdigit(*t))
    168 		return (atoi(t));
    169 	return (0);
    170 }
    171 
    172 static int
    173 error(err)
    174 	int err;
    175 {
    176 	char *p;
    177 
    178 	(void)write(STDERR_FILENO, "disktab: ", 9);
    179 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
    180 	(void)write(STDERR_FILENO, ": ", 2);
    181 	p = strerror(err);
    182 	(void)write(STDERR_FILENO, p, strlen(p));
    183 	(void)write(STDERR_FILENO, "\n", 1);
    184 }
    185