xref: /csrg-svn/lib/libc/gen/disklabel.c (revision 38498)
121417Sdist /*
235104Sbostic  * Copyright (c) 1983, 1987 Regents of the University of California.
335104Sbostic  * All rights reserved.
435104Sbostic  *
535104Sbostic  * Redistribution and use in source and binary forms are permitted
635104Sbostic  * provided that the above copyright notice and this paragraph are
735104Sbostic  * duplicated in all such forms and that any documentation,
835104Sbostic  * advertising materials, and other materials related to such
935104Sbostic  * distribution and use acknowledge that the software was developed
1035104Sbostic  * by the University of California, Berkeley.  The name of the
1135104Sbostic  * University may not be used to endorse or promote products derived
1235104Sbostic  * from this software without specific prior written permission.
1335104Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435104Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535104Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621417Sdist  */
1710748Ssam 
1826675Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*38498Sbostic static char sccsid[] = "@(#)disklabel.c	5.11 (Berkeley) 07/30/89";
2035104Sbostic #endif /* LIBC_SCCS and not lint */
2121417Sdist 
2230420Skarels #include <sys/param.h>
23*38498Sbostic #include <ufs/fs.h>
2430420Skarels #include <sys/file.h>
2530420Skarels #define DKTYPENAMES
2630420Skarels #include <sys/disklabel.h>
2710748Ssam #include <stdio.h>
2830681Sbostic #include <strings.h>
2910748Ssam 
3010748Ssam static	char *dgetstr();
3110748Ssam 
3230420Skarels struct disklabel *
3310748Ssam getdiskbyname(name)
3410748Ssam 	char *name;
3510748Ssam {
3633399Smarc 	static struct	disklabel disk;
3733399Smarc 	static char 	boot[BUFSIZ];
3833399Smarc 	char	localbuf[BUFSIZ];
3933399Smarc 	char	buf[BUFSIZ];
4033399Smarc 	char	*cp, *cq;	/* can't be register */
4130420Skarels 	register struct	disklabel *dp = &disk;
4210760Ssam 	register struct partition *pp;
4333399Smarc 	char	p, max, psize[3], pbsize[3],
4433399Smarc 		pfsize[3], poffset[3], ptype[3];
4533399Smarc 	u_long	*dx;
4610748Ssam 
4710748Ssam 	if (dgetent(buf, name) <= 0)
4830420Skarels 		return ((struct disklabel *)0);
4930420Skarels 	bzero((char *)&disk, sizeof(disk));
5033399Smarc 	/*
5133399Smarc 	 * typename
5233399Smarc 	 */
5330420Skarels 	cq = dp->d_typename;
5430420Skarels 	cp = buf;
5530420Skarels 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
5630420Skarels 	    (*cq = *cp) && *cq != '|' && *cq != ':')
5730420Skarels 		cq++, cp++;
5830420Skarels 	*cq = '\0';
5933399Smarc 	/*
6033399Smarc 	 * boot name (optional)  xxboot, bootxx
6133399Smarc 	 */
6233399Smarc 	cp = boot;
6333399Smarc 	dp->d_boot0 = dgetstr("b0", &cp);
6433399Smarc 	dp->d_boot1 = dgetstr("b1", &cp);
6530420Skarels 	cp = localbuf;
6630420Skarels 	cq = dgetstr("ty", &cp);
6730420Skarels 	if (cq && strcmp(cq, "removable") == 0)
6830420Skarels 		dp->d_flags |= D_REMOVABLE;
6930420Skarels 	else  if (cq && strcmp(cq, "simulated") == 0)
7030420Skarels 		dp->d_flags |= D_RAMDISK;
7130420Skarels 	if (dgetflag("sf"))
7230420Skarels 		dp->d_flags |= D_BADSECT;
7333399Smarc 
7430420Skarels #define getnumdflt(field, dname, dflt) \
7530420Skarels 	{ int f = dgetnum(dname); \
7630420Skarels 	(field) = f == -1 ? (dflt) : f; }
7730420Skarels 
7830420Skarels 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
7910748Ssam 	dp->d_ntracks = dgetnum("nt");
8010748Ssam 	dp->d_nsectors = dgetnum("ns");
8110748Ssam 	dp->d_ncylinders = dgetnum("nc");
8230420Skarels 	cq = dgetstr("dt", &cp);
8330420Skarels 	if (cq)
8430420Skarels 		dp->d_type = gettype(cq, dktypenames);
8530420Skarels 	else
8630420Skarels 		getnumdflt(dp->d_type, "dt", 0);
8730420Skarels 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
8830420Skarels 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
8930420Skarels 	getnumdflt(dp->d_rpm, "rm", 3600);
9030420Skarels 	getnumdflt(dp->d_interleave, "il", 1);
9130420Skarels 	getnumdflt(dp->d_trackskew, "sk", 0);
9230420Skarels 	getnumdflt(dp->d_cylskew, "cs", 0);
9330420Skarels 	getnumdflt(dp->d_headswitch, "hs", 0);
9430420Skarels 	getnumdflt(dp->d_trkseek, "ts", 0);
9530420Skarels 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
9630420Skarels 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
9710760Ssam 	strcpy(psize, "px");
9810760Ssam 	strcpy(pbsize, "bx");
9910760Ssam 	strcpy(pfsize, "fx");
10030420Skarels 	strcpy(poffset, "ox");
10130420Skarels 	strcpy(ptype, "tx");
10230420Skarels 	max = 'a' - 1;
10330420Skarels 	pp = &dp->d_partitions[0];
10430420Skarels 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
10530420Skarels 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
10610760Ssam 		pp->p_size = dgetnum(psize);
10730420Skarels 		if (pp->p_size == -1)
10830420Skarels 			pp->p_size = 0;
10930420Skarels 		else {
11030420Skarels 			pp->p_offset = dgetnum(poffset);
11130420Skarels 			getnumdflt(pp->p_fsize, pfsize, 0);
11230420Skarels 			if (pp->p_fsize)
11330420Skarels 				pp->p_frag = dgetnum(pbsize) / pp->p_fsize;
11430420Skarels 			getnumdflt(pp->p_fstype, ptype, 0);
11530420Skarels 			if (pp->p_fstype == 0 && (cq = dgetstr(ptype, &cp)))
11630420Skarels 				pp->p_fstype = gettype(cq, fstypenames);
11730420Skarels 			max = p;
11830420Skarels 		}
11910748Ssam 	}
12030420Skarels 	dp->d_npartitions = max + 1 - 'a';
12130681Sbostic 	(void)strcpy(psize, "dx");
12230420Skarels 	dx = dp->d_drivedata;
12330420Skarels 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
12430420Skarels 		psize[1] = p;
12530420Skarels 		getnumdflt(*dx, psize, 0);
12630420Skarels 	}
12730420Skarels 	dp->d_magic = DISKMAGIC;
12830420Skarels 	dp->d_magic2 = DISKMAGIC;
12910748Ssam 	return (dp);
13010748Ssam }
13110748Ssam 
13210748Ssam #include <ctype.h>
13310748Ssam 
13410748Ssam static	char *tbuf;
13510748Ssam static	char *dskip();
13610748Ssam static	char *ddecode();
13710748Ssam 
13810748Ssam /*
13910748Ssam  * Get an entry for disk name in buffer bp,
14010748Ssam  * from the diskcap file.  Parse is very rudimentary;
14110748Ssam  * we just notice escaped newlines.
14210748Ssam  */
14310748Ssam static
14410748Ssam dgetent(bp, name)
14510748Ssam 	char *bp, *name;
14610748Ssam {
14710748Ssam 	register char *cp;
14810748Ssam 	register int c;
14910748Ssam 	register int i = 0, cnt = 0;
15010748Ssam 	char ibuf[BUFSIZ];
15110748Ssam 	int tf;
15210748Ssam 
15310748Ssam 	tbuf = bp;
15410748Ssam 	tf = open(DISKTAB, 0);
15510748Ssam 	if (tf < 0)
15610748Ssam 		return (-1);
15710748Ssam 	for (;;) {
15810748Ssam 		cp = bp;
15910748Ssam 		for (;;) {
16010748Ssam 			if (i == cnt) {
16110748Ssam 				cnt = read(tf, ibuf, BUFSIZ);
16210748Ssam 				if (cnt <= 0) {
16310748Ssam 					close(tf);
16410748Ssam 					return (0);
16510748Ssam 				}
16610748Ssam 				i = 0;
16710748Ssam 			}
16810748Ssam 			c = ibuf[i++];
16910748Ssam 			if (c == '\n') {
17010748Ssam 				if (cp > bp && cp[-1] == '\\'){
17110748Ssam 					cp--;
17210748Ssam 					continue;
17310748Ssam 				}
17410748Ssam 				break;
17510748Ssam 			}
17610748Ssam 			if (cp >= bp+BUFSIZ) {
17710748Ssam 				write(2,"Disktab entry too long\n", 23);
17810748Ssam 				break;
17910748Ssam 			} else
18010748Ssam 				*cp++ = c;
18110748Ssam 		}
18210748Ssam 		*cp = 0;
18310748Ssam 
18410748Ssam 		/*
18510748Ssam 		 * The real work for the match.
18610748Ssam 		 */
18710748Ssam 		if (dnamatch(name)) {
18810748Ssam 			close(tf);
18910748Ssam 			return (1);
19010748Ssam 		}
19110748Ssam 	}
19210748Ssam }
19310748Ssam 
19410748Ssam /*
19510748Ssam  * Dnamatch deals with name matching.  The first field of the disktab
19610748Ssam  * entry is a sequence of names separated by |'s, so we compare
19710748Ssam  * against each such name.  The normal : terminator after the last
19810748Ssam  * name (before the first field) stops us.
19910748Ssam  */
20010748Ssam static
20110748Ssam dnamatch(np)
20210748Ssam 	char *np;
20310748Ssam {
20410748Ssam 	register char *Np, *Bp;
20510748Ssam 
20610748Ssam 	Bp = tbuf;
20710748Ssam 	if (*Bp == '#')
20810748Ssam 		return (0);
20910748Ssam 	for (;;) {
21010748Ssam 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
21110748Ssam 			continue;
21210748Ssam 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
21310748Ssam 			return (1);
21410748Ssam 		while (*Bp && *Bp != ':' && *Bp != '|')
21510748Ssam 			Bp++;
21610748Ssam 		if (*Bp == 0 || *Bp == ':')
21710748Ssam 			return (0);
21810748Ssam 		Bp++;
21910748Ssam 	}
22010748Ssam }
22110748Ssam 
22210748Ssam /*
22310748Ssam  * Skip to the next field.  Notice that this is very dumb, not
22410748Ssam  * knowing about \: escapes or any such.  If necessary, :'s can be put
22510748Ssam  * into the diskcap file in octal.
22610748Ssam  */
22710748Ssam static char *
22810748Ssam dskip(bp)
22910748Ssam 	register char *bp;
23010748Ssam {
23110748Ssam 
23210748Ssam 	while (*bp && *bp != ':')
23310748Ssam 		bp++;
23410748Ssam 	if (*bp == ':')
23510748Ssam 		bp++;
23610748Ssam 	return (bp);
23710748Ssam }
23810748Ssam 
23910748Ssam /*
24010748Ssam  * Return the (numeric) option id.
24110748Ssam  * Numeric options look like
24210748Ssam  *	li#80
24310748Ssam  * i.e. the option string is separated from the numeric value by
24410748Ssam  * a # character.  If the option is not found we return -1.
24510748Ssam  * Note that we handle octal numbers beginning with 0.
24610748Ssam  */
24710748Ssam static
24810748Ssam dgetnum(id)
24910748Ssam 	char *id;
25010748Ssam {
25110748Ssam 	register int i, base;
25210748Ssam 	register char *bp = tbuf;
25310748Ssam 
25410748Ssam 	for (;;) {
25510748Ssam 		bp = dskip(bp);
25610748Ssam 		if (*bp == 0)
25710748Ssam 			return (-1);
25810748Ssam 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
25910748Ssam 			continue;
26010748Ssam 		if (*bp == '@')
26110748Ssam 			return (-1);
26210748Ssam 		if (*bp != '#')
26310748Ssam 			continue;
26410748Ssam 		bp++;
26510748Ssam 		base = 10;
26610748Ssam 		if (*bp == '0')
26710748Ssam 			base = 8;
26810748Ssam 		i = 0;
26910748Ssam 		while (isdigit(*bp))
27010748Ssam 			i *= base, i += *bp++ - '0';
27110748Ssam 		return (i);
27210748Ssam 	}
27310748Ssam }
27410748Ssam 
27510748Ssam /*
27610748Ssam  * Handle a flag option.
27710748Ssam  * Flag options are given "naked", i.e. followed by a : or the end
27810748Ssam  * of the buffer.  Return 1 if we find the option, or 0 if it is
27910748Ssam  * not given.
28010748Ssam  */
28110748Ssam static
28210748Ssam dgetflag(id)
28310748Ssam 	char *id;
28410748Ssam {
28510748Ssam 	register char *bp = tbuf;
28610748Ssam 
28710748Ssam 	for (;;) {
28810748Ssam 		bp = dskip(bp);
28910748Ssam 		if (!*bp)
29010748Ssam 			return (0);
29110748Ssam 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
29210748Ssam 			if (!*bp || *bp == ':')
29310748Ssam 				return (1);
29410748Ssam 			else if (*bp == '@')
29510748Ssam 				return (0);
29610748Ssam 		}
29710748Ssam 	}
29810748Ssam }
29910748Ssam 
30010748Ssam /*
30110748Ssam  * Get a string valued option.
30210748Ssam  * These are given as
30310748Ssam  *	cl=^Z
30410748Ssam  * Much decoding is done on the strings, and the strings are
30510748Ssam  * placed in area, which is a ref parameter which is updated.
30610748Ssam  * No checking on area overflow.
30710748Ssam  */
30810748Ssam static char *
30910748Ssam dgetstr(id, area)
31010748Ssam 	char *id, **area;
31110748Ssam {
31210748Ssam 	register char *bp = tbuf;
31310748Ssam 
31410748Ssam 	for (;;) {
31510748Ssam 		bp = dskip(bp);
31610748Ssam 		if (!*bp)
31710748Ssam 			return (0);
31810748Ssam 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
31910748Ssam 			continue;
32010748Ssam 		if (*bp == '@')
32110748Ssam 			return (0);
32210748Ssam 		if (*bp != '=')
32310748Ssam 			continue;
32410748Ssam 		bp++;
32510748Ssam 		return (ddecode(bp, area));
32610748Ssam 	}
32710748Ssam }
32810748Ssam 
32910748Ssam /*
33010748Ssam  * Tdecode does the grung work to decode the
33110748Ssam  * string capability escapes.
33210748Ssam  */
33310748Ssam static char *
33410748Ssam ddecode(str, area)
33510748Ssam 	register char *str;
33610748Ssam 	char **area;
33710748Ssam {
33810748Ssam 	register char *cp;
33910748Ssam 	register int c;
34010748Ssam 	register char *dp;
34110748Ssam 	int i;
34210748Ssam 
34310748Ssam 	cp = *area;
34410748Ssam 	while ((c = *str++) && c != ':') {
34510748Ssam 		switch (c) {
34610748Ssam 
34710748Ssam 		case '^':
34810748Ssam 			c = *str++ & 037;
34910748Ssam 			break;
35010748Ssam 
35110748Ssam 		case '\\':
35210748Ssam 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
35310748Ssam 			c = *str++;
35410748Ssam nextc:
35510748Ssam 			if (*dp++ == c) {
35610748Ssam 				c = *dp++;
35710748Ssam 				break;
35810748Ssam 			}
35910748Ssam 			dp++;
36010748Ssam 			if (*dp)
36110748Ssam 				goto nextc;
36210748Ssam 			if (isdigit(c)) {
36310748Ssam 				c -= '0', i = 2;
36410748Ssam 				do
36510748Ssam 					c <<= 3, c |= *str++ - '0';
36610748Ssam 				while (--i && isdigit(*str));
36710748Ssam 			}
36810748Ssam 			break;
36910748Ssam 		}
37010748Ssam 		*cp++ = c;
37110748Ssam 	}
37210748Ssam 	*cp++ = 0;
37310748Ssam 	str = *area;
37410748Ssam 	*area = cp;
37510748Ssam 	return (str);
37610748Ssam }
37730420Skarels 
37830420Skarels static
37930420Skarels gettype(t, names)
38030420Skarels 	char *t;
38130420Skarels 	char **names;
38230420Skarels {
38330420Skarels 	register char **nm;
38430420Skarels 
38530420Skarels 	for (nm = names; *nm; nm++)
38633261Smckusick 		if (strcasecmp(t, *nm) == 0)
38730420Skarels 			return (nm - names);
38830420Skarels 	if (isdigit(*t))
38930420Skarels 		return (atoi(t));
39030420Skarels 	return (0);
39130420Skarels }
39230420Skarels 
39330420Skarels dkcksum(lp)
39430420Skarels 	register struct disklabel *lp;
39530420Skarels {
39630420Skarels 	register u_short *start, *end;
39730420Skarels 	register u_short sum = 0;
39830420Skarels 
39930420Skarels 	start = (u_short *)lp;
40030420Skarels 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
40130420Skarels 	while (start < end)
40230420Skarels 		sum ^= *start++;
40330420Skarels 	return (sum);
40430420Skarels }
405