Home | History | Annotate | Line # | Download | only in gen
disklabel.c revision 1.4
      1 /*
      2  * Copyright (c) 1983, 1987 Regents of the University of California.
      3  * 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 #if defined(LIBC_SCCS) && !defined(lint)
     35 /*static char *sccsid = "from: @(#)disklabel.c	5.17 (Berkeley) 2/23/91";*/
     36 static char *rcsid = "$Id: disklabel.c,v 1.4 1993/11/11 19:04:22 jtc Exp $";
     37 #endif /* LIBC_SCCS and not lint */
     38 
     39 #include <sys/param.h>
     40 #include <sys/errno.h>
     41 #include <ufs/fs.h>
     42 #include <sys/file.h>
     43 #define DKTYPENAMES
     44 #include <sys/disklabel.h>
     45 #include <stdio.h>
     46 #include <string.h>
     47 #include <stdlib.h>
     48 #include <unistd.h>
     49 
     50 static char    *dgetstr();
     51 static int	dgetent();
     52 static int	dnamatch();
     53 static int	dgetnum();
     54 static int	dgetflag();
     55 static int	gettype();
     56 static void	error();
     57 
     58 struct disklabel *
     59 getdiskbyname(name)
     60 	const char *name;
     61 {
     62 	static struct	disklabel disk;
     63 	static char 	boot[BUFSIZ];
     64 	char	localbuf[BUFSIZ];
     65 	char	buf[BUFSIZ];
     66 	char	*cp, *cq;	/* can't be register */
     67 	register struct	disklabel *dp = &disk;
     68 	register struct partition *pp;
     69 	char	p, max, psize[3], pbsize[3],
     70 		pfsize[3], poffset[3], ptype[3];
     71 	u_long	*dx;
     72 
     73 	if (dgetent(buf, name) <= 0)
     74 		return ((struct disklabel *)0);
     75 	bzero((char *)&disk, sizeof(disk));
     76 	/*
     77 	 * typename
     78 	 */
     79 	cq = dp->d_typename;
     80 	cp = buf;
     81 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
     82 	    (*cq = *cp) && *cq != '|' && *cq != ':')
     83 		cq++, cp++;
     84 	*cq = '\0';
     85 	/*
     86 	 * boot name (optional)  xxboot, bootxx
     87 	 */
     88 	cp = boot;
     89 	dp->d_boot0 = dgetstr("b0", &cp);
     90 	dp->d_boot1 = dgetstr("b1", &cp);
     91 	cp = localbuf;
     92 	cq = dgetstr("ty", &cp);
     93 	if (cq && strcmp(cq, "removable") == 0)
     94 		dp->d_flags |= D_REMOVABLE;
     95 	else  if (cq && strcmp(cq, "simulated") == 0)
     96 		dp->d_flags |= D_RAMDISK;
     97 	if (dgetflag("sf"))
     98 		dp->d_flags |= D_BADSECT;
     99 
    100 #define getnumdflt(field, dname, dflt) \
    101 	{ int f = dgetnum(dname); \
    102 	(field) = f == -1 ? (dflt) : f; }
    103 
    104 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
    105 	dp->d_ntracks = dgetnum("nt");
    106 	dp->d_nsectors = dgetnum("ns");
    107 	dp->d_ncylinders = dgetnum("nc");
    108 	cq = dgetstr("dt", &cp);
    109 	if (cq)
    110 		dp->d_type = gettype(cq, dktypenames);
    111 	else
    112 		getnumdflt(dp->d_type, "dt", 0);
    113 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
    114 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
    115 	getnumdflt(dp->d_rpm, "rm", 3600);
    116 	getnumdflt(dp->d_interleave, "il", 1);
    117 	getnumdflt(dp->d_trackskew, "sk", 0);
    118 	getnumdflt(dp->d_cylskew, "cs", 0);
    119 	getnumdflt(dp->d_headswitch, "hs", 0);
    120 	getnumdflt(dp->d_trkseek, "ts", 0);
    121 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
    122 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
    123 	strcpy(psize, "px");
    124 	strcpy(pbsize, "bx");
    125 	strcpy(pfsize, "fx");
    126 	strcpy(poffset, "ox");
    127 	strcpy(ptype, "tx");
    128 	max = 'a' - 1;
    129 	pp = &dp->d_partitions[0];
    130 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
    131 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
    132 		pp->p_size = dgetnum(psize);
    133 		if (pp->p_size == -1)
    134 			pp->p_size = 0;
    135 		else {
    136 			pp->p_offset = dgetnum(poffset);
    137 			getnumdflt(pp->p_fsize, pfsize, 0);
    138 			if (pp->p_fsize)
    139 				pp->p_frag = dgetnum(pbsize) / pp->p_fsize;
    140 			getnumdflt(pp->p_fstype, ptype, 0);
    141 			if (pp->p_fstype == 0 && (cq = dgetstr(ptype, &cp)))
    142 				pp->p_fstype = gettype(cq, fstypenames);
    143 			max = p;
    144 		}
    145 	}
    146 	dp->d_npartitions = max + 1 - 'a';
    147 	(void)strcpy(psize, "dx");
    148 	dx = dp->d_drivedata;
    149 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
    150 		psize[1] = p;
    151 		getnumdflt(*dx, psize, 0);
    152 	}
    153 	dp->d_magic = DISKMAGIC;
    154 	dp->d_magic2 = DISKMAGIC;
    155 	return (dp);
    156 }
    157 
    158 #include <ctype.h>
    159 
    160 static	char *tbuf;
    161 static	char *dskip();
    162 static	char *ddecode();
    163 
    164 /*
    165  * Get an entry for disk name in buffer bp,
    166  * from the diskcap file.  Parse is very rudimentary;
    167  * we just notice escaped newlines.
    168  */
    169 static int
    170 dgetent(bp, name)
    171 	char *bp, *name;
    172 {
    173 	register char *cp;
    174 	register int c;
    175 	register int i = 0, cnt = 0;
    176 	char ibuf[BUFSIZ];
    177 	int tf;
    178 
    179 	tbuf = bp;
    180 	tf = open(_PATH_DISKTAB, 0);
    181 	if (tf < 0) {
    182 		error(errno);
    183 		return (-1);
    184 	}
    185 	for (;;) {
    186 		cp = bp;
    187 		for (;;) {
    188 			if (i == cnt) {
    189 				cnt = read(tf, ibuf, BUFSIZ);
    190 				if (cnt <= 0) {
    191 					error(errno);
    192 					close(tf);
    193 					return (0);
    194 				}
    195 				i = 0;
    196 			}
    197 			c = ibuf[i++];
    198 			if (c == '\n') {
    199 				if (cp > bp && cp[-1] == '\\'){
    200 					cp--;
    201 					continue;
    202 				}
    203 				break;
    204 			}
    205 			if (cp >= bp+BUFSIZ) {
    206 				error(EFTYPE);
    207 				break;
    208 			} else
    209 				*cp++ = c;
    210 		}
    211 		*cp = 0;
    212 
    213 		/*
    214 		 * The real work for the match.
    215 		 */
    216 		if (dnamatch(name)) {
    217 			close(tf);
    218 			return (1);
    219 		}
    220 	}
    221 }
    222 
    223 /*
    224  * Dnamatch deals with name matching.  The first field of the disktab
    225  * entry is a sequence of names separated by |'s, so we compare
    226  * against each such name.  The normal : terminator after the last
    227  * name (before the first field) stops us.
    228  */
    229 static int
    230 dnamatch(np)
    231 	char *np;
    232 {
    233 	register char *Np, *Bp;
    234 
    235 	Bp = tbuf;
    236 	if (*Bp == '#')
    237 		return (0);
    238 	for (;;) {
    239 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
    240 			continue;
    241 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
    242 			return (1);
    243 		while (*Bp && *Bp != ':' && *Bp != '|')
    244 			Bp++;
    245 		if (*Bp == 0 || *Bp == ':')
    246 			return (0);
    247 		Bp++;
    248 	}
    249 }
    250 
    251 /*
    252  * Skip to the next field.  Notice that this is very dumb, not
    253  * knowing about \: escapes or any such.  If necessary, :'s can be put
    254  * into the diskcap file in octal.
    255  */
    256 static char *
    257 dskip(bp)
    258 	register char *bp;
    259 {
    260 
    261 	while (*bp && *bp != ':')
    262 		bp++;
    263 	if (*bp == ':')
    264 		bp++;
    265 	return (bp);
    266 }
    267 
    268 /*
    269  * Return the (numeric) option id.
    270  * Numeric options look like
    271  *	li#80
    272  * i.e. the option string is separated from the numeric value by
    273  * a # character.  If the option is not found we return -1.
    274  * Note that we handle octal numbers beginning with 0.
    275  */
    276 static int
    277 dgetnum(id)
    278 	char *id;
    279 {
    280 	register int i, base;
    281 	register char *bp = tbuf;
    282 
    283 	for (;;) {
    284 		bp = dskip(bp);
    285 		if (*bp == 0)
    286 			return (-1);
    287 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
    288 			continue;
    289 		if (*bp == '@')
    290 			return (-1);
    291 		if (*bp != '#')
    292 			continue;
    293 		bp++;
    294 		base = 10;
    295 		if (*bp == '0')
    296 			base = 8;
    297 		i = 0;
    298 		while (isdigit(*bp))
    299 			i *= base, i += *bp++ - '0';
    300 		return (i);
    301 	}
    302 }
    303 
    304 /*
    305  * Handle a flag option.
    306  * Flag options are given "naked", i.e. followed by a : or the end
    307  * of the buffer.  Return 1 if we find the option, or 0 if it is
    308  * not given.
    309  */
    310 static int
    311 dgetflag(id)
    312 	char *id;
    313 {
    314 	register char *bp = tbuf;
    315 
    316 	for (;;) {
    317 		bp = dskip(bp);
    318 		if (!*bp)
    319 			return (0);
    320 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
    321 			if (!*bp || *bp == ':')
    322 				return (1);
    323 			else if (*bp == '@')
    324 				return (0);
    325 		}
    326 	}
    327 }
    328 
    329 /*
    330  * Get a string valued option.
    331  * These are given as
    332  *	cl=^Z
    333  * Much decoding is done on the strings, and the strings are
    334  * placed in area, which is a ref parameter which is updated.
    335  * No checking on area overflow.
    336  */
    337 static char *
    338 dgetstr(id, area)
    339 	char *id, **area;
    340 {
    341 	register char *bp = tbuf;
    342 
    343 	for (;;) {
    344 		bp = dskip(bp);
    345 		if (!*bp)
    346 			return (0);
    347 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
    348 			continue;
    349 		if (*bp == '@')
    350 			return (0);
    351 		if (*bp != '=')
    352 			continue;
    353 		bp++;
    354 		return (ddecode(bp, area));
    355 	}
    356 }
    357 
    358 /*
    359  * Tdecode does the grung work to decode the
    360  * string capability escapes.
    361  */
    362 static char *
    363 ddecode(str, area)
    364 	register char *str;
    365 	char **area;
    366 {
    367 	register char *cp;
    368 	register int c;
    369 	register char *dp;
    370 	int i;
    371 
    372 	cp = *area;
    373 	while ((c = *str++) && c != ':') {
    374 		switch (c) {
    375 
    376 		case '^':
    377 			c = *str++ & 037;
    378 			break;
    379 
    380 		case '\\':
    381 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
    382 			c = *str++;
    383 nextc:
    384 			if (*dp++ == c) {
    385 				c = *dp++;
    386 				break;
    387 			}
    388 			dp++;
    389 			if (*dp)
    390 				goto nextc;
    391 			if (isdigit(c)) {
    392 				c -= '0', i = 2;
    393 				do
    394 					c <<= 3, c |= *str++ - '0';
    395 				while (--i && isdigit(*str));
    396 			}
    397 			break;
    398 		}
    399 		*cp++ = c;
    400 	}
    401 	*cp++ = 0;
    402 	str = *area;
    403 	*area = cp;
    404 	return (str);
    405 }
    406 
    407 static int
    408 gettype(t, names)
    409 	char *t;
    410 	char **names;
    411 {
    412 	register char **nm;
    413 
    414 	for (nm = names; *nm; nm++)
    415 		if (strcasecmp(t, *nm) == 0)
    416 			return (nm - names);
    417 	if (isdigit(*t))
    418 		return (atoi(t));
    419 	return (0);
    420 }
    421 
    422 static void
    423 error(err)
    424 	int err;
    425 {
    426 	char *p;
    427 
    428 	(void)write(STDERR_FILENO, "disktab: ", 9);
    429 	(void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
    430 	p = strerror(err);
    431 	(void)write(STDERR_FILENO, p, strlen(p));
    432 	(void)write(STDERR_FILENO, "\n", 1);
    433 }
    434