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