xref: /csrg-svn/lib/libc/gen/disklabel.c (revision 33399)
121417Sdist /*
230420Skarels  * Copyright (c) 1983,1987 Regents of the University of California.
321417Sdist  * All rights reserved.  The Berkeley software License Agreement
421417Sdist  * specifies the terms and conditions for redistribution.
521417Sdist  */
610748Ssam 
726675Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*33399Smarc static char sccsid[] = "@(#)disklabel.c	5.9 (Berkeley) 01/27/88";
926675Sdonn #endif LIBC_SCCS and not lint
1021417Sdist 
1130420Skarels #include <sys/param.h>
1230420Skarels #include <sys/fs.h>
1330420Skarels #include <sys/file.h>
1430420Skarels #define DKTYPENAMES
1530420Skarels #include <sys/disklabel.h>
1610748Ssam #include <stdio.h>
1730681Sbostic #include <strings.h>
1810748Ssam 
1910748Ssam static	char *dgetstr();
2010748Ssam 
2130420Skarels struct disklabel *
2210748Ssam getdiskbyname(name)
2310748Ssam 	char *name;
2410748Ssam {
25*33399Smarc 	static struct	disklabel disk;
26*33399Smarc 	static char 	boot[BUFSIZ];
27*33399Smarc 	char	localbuf[BUFSIZ];
28*33399Smarc 	char	buf[BUFSIZ];
29*33399Smarc 	char	*cp, *cq;	/* can't be register */
3030420Skarels 	register struct	disklabel *dp = &disk;
3110760Ssam 	register struct partition *pp;
32*33399Smarc 	char	p, max, psize[3], pbsize[3],
33*33399Smarc 		pfsize[3], poffset[3], ptype[3];
34*33399Smarc 	u_long	*dx;
3510748Ssam 
3610748Ssam 	if (dgetent(buf, name) <= 0)
3730420Skarels 		return ((struct disklabel *)0);
3830420Skarels 	bzero((char *)&disk, sizeof(disk));
39*33399Smarc 	/*
40*33399Smarc 	 * typename
41*33399Smarc 	 */
4230420Skarels 	cq = dp->d_typename;
4330420Skarels 	cp = buf;
4430420Skarels 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
4530420Skarels 	    (*cq = *cp) && *cq != '|' && *cq != ':')
4630420Skarels 		cq++, cp++;
4730420Skarels 	*cq = '\0';
48*33399Smarc 	/*
49*33399Smarc 	 * boot name (optional)  xxboot, bootxx
50*33399Smarc 	 */
51*33399Smarc 	cp = boot;
52*33399Smarc 	dp->d_boot0 = dgetstr("b0", &cp);
53*33399Smarc 	dp->d_boot1 = dgetstr("b1", &cp);
5430420Skarels 	cp = localbuf;
5530420Skarels 	cq = dgetstr("ty", &cp);
5630420Skarels 	if (cq && strcmp(cq, "removable") == 0)
5730420Skarels 		dp->d_flags |= D_REMOVABLE;
5830420Skarels 	else  if (cq && strcmp(cq, "simulated") == 0)
5930420Skarels 		dp->d_flags |= D_RAMDISK;
6030420Skarels 	if (dgetflag("sf"))
6130420Skarels 		dp->d_flags |= D_BADSECT;
62*33399Smarc 
6330420Skarels #define getnumdflt(field, dname, dflt) \
6430420Skarels 	{ int f = dgetnum(dname); \
6530420Skarels 	(field) = f == -1 ? (dflt) : f; }
6630420Skarels 
6730420Skarels 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
6810748Ssam 	dp->d_ntracks = dgetnum("nt");
6910748Ssam 	dp->d_nsectors = dgetnum("ns");
7010748Ssam 	dp->d_ncylinders = dgetnum("nc");
7130420Skarels 	cq = dgetstr("dt", &cp);
7230420Skarels 	if (cq)
7330420Skarels 		dp->d_type = gettype(cq, dktypenames);
7430420Skarels 	else
7530420Skarels 		getnumdflt(dp->d_type, "dt", 0);
7630420Skarels 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
7730420Skarels 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
7830420Skarels 	getnumdflt(dp->d_rpm, "rm", 3600);
7930420Skarels 	getnumdflt(dp->d_interleave, "il", 1);
8030420Skarels 	getnumdflt(dp->d_trackskew, "sk", 0);
8130420Skarels 	getnumdflt(dp->d_cylskew, "cs", 0);
8230420Skarels 	getnumdflt(dp->d_headswitch, "hs", 0);
8330420Skarels 	getnumdflt(dp->d_trkseek, "ts", 0);
8430420Skarels 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
8530420Skarels 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
8610760Ssam 	strcpy(psize, "px");
8710760Ssam 	strcpy(pbsize, "bx");
8810760Ssam 	strcpy(pfsize, "fx");
8930420Skarels 	strcpy(poffset, "ox");
9030420Skarels 	strcpy(ptype, "tx");
9130420Skarels 	max = 'a' - 1;
9230420Skarels 	pp = &dp->d_partitions[0];
9330420Skarels 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
9430420Skarels 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
9510760Ssam 		pp->p_size = dgetnum(psize);
9630420Skarels 		if (pp->p_size == -1)
9730420Skarels 			pp->p_size = 0;
9830420Skarels 		else {
9930420Skarels 			pp->p_offset = dgetnum(poffset);
10030420Skarels 			getnumdflt(pp->p_fsize, pfsize, 0);
10130420Skarels 			if (pp->p_fsize)
10230420Skarels 				pp->p_frag = dgetnum(pbsize) / pp->p_fsize;
10330420Skarels 			getnumdflt(pp->p_fstype, ptype, 0);
10430420Skarels 			if (pp->p_fstype == 0 && (cq = dgetstr(ptype, &cp)))
10530420Skarels 				pp->p_fstype = gettype(cq, fstypenames);
10630420Skarels 			max = p;
10730420Skarels 		}
10810748Ssam 	}
10930420Skarels 	dp->d_npartitions = max + 1 - 'a';
11030681Sbostic 	(void)strcpy(psize, "dx");
11130420Skarels 	dx = dp->d_drivedata;
11230420Skarels 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
11330420Skarels 		psize[1] = p;
11430420Skarels 		getnumdflt(*dx, psize, 0);
11530420Skarels 	}
11630420Skarels 	dp->d_magic = DISKMAGIC;
11730420Skarels 	dp->d_magic2 = DISKMAGIC;
11810748Ssam 	return (dp);
11910748Ssam }
12010748Ssam 
12110748Ssam #include <ctype.h>
12210748Ssam 
12310748Ssam static	char *tbuf;
12410748Ssam static	char *dskip();
12510748Ssam static	char *ddecode();
12610748Ssam 
12710748Ssam /*
12810748Ssam  * Get an entry for disk name in buffer bp,
12910748Ssam  * from the diskcap file.  Parse is very rudimentary;
13010748Ssam  * we just notice escaped newlines.
13110748Ssam  */
13210748Ssam static
13310748Ssam dgetent(bp, name)
13410748Ssam 	char *bp, *name;
13510748Ssam {
13610748Ssam 	register char *cp;
13710748Ssam 	register int c;
13810748Ssam 	register int i = 0, cnt = 0;
13910748Ssam 	char ibuf[BUFSIZ];
14010748Ssam 	int tf;
14110748Ssam 
14210748Ssam 	tbuf = bp;
14310748Ssam 	tf = open(DISKTAB, 0);
14410748Ssam 	if (tf < 0)
14510748Ssam 		return (-1);
14610748Ssam 	for (;;) {
14710748Ssam 		cp = bp;
14810748Ssam 		for (;;) {
14910748Ssam 			if (i == cnt) {
15010748Ssam 				cnt = read(tf, ibuf, BUFSIZ);
15110748Ssam 				if (cnt <= 0) {
15210748Ssam 					close(tf);
15310748Ssam 					return (0);
15410748Ssam 				}
15510748Ssam 				i = 0;
15610748Ssam 			}
15710748Ssam 			c = ibuf[i++];
15810748Ssam 			if (c == '\n') {
15910748Ssam 				if (cp > bp && cp[-1] == '\\'){
16010748Ssam 					cp--;
16110748Ssam 					continue;
16210748Ssam 				}
16310748Ssam 				break;
16410748Ssam 			}
16510748Ssam 			if (cp >= bp+BUFSIZ) {
16610748Ssam 				write(2,"Disktab entry too long\n", 23);
16710748Ssam 				break;
16810748Ssam 			} else
16910748Ssam 				*cp++ = c;
17010748Ssam 		}
17110748Ssam 		*cp = 0;
17210748Ssam 
17310748Ssam 		/*
17410748Ssam 		 * The real work for the match.
17510748Ssam 		 */
17610748Ssam 		if (dnamatch(name)) {
17710748Ssam 			close(tf);
17810748Ssam 			return (1);
17910748Ssam 		}
18010748Ssam 	}
18110748Ssam }
18210748Ssam 
18310748Ssam /*
18410748Ssam  * Dnamatch deals with name matching.  The first field of the disktab
18510748Ssam  * entry is a sequence of names separated by |'s, so we compare
18610748Ssam  * against each such name.  The normal : terminator after the last
18710748Ssam  * name (before the first field) stops us.
18810748Ssam  */
18910748Ssam static
19010748Ssam dnamatch(np)
19110748Ssam 	char *np;
19210748Ssam {
19310748Ssam 	register char *Np, *Bp;
19410748Ssam 
19510748Ssam 	Bp = tbuf;
19610748Ssam 	if (*Bp == '#')
19710748Ssam 		return (0);
19810748Ssam 	for (;;) {
19910748Ssam 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
20010748Ssam 			continue;
20110748Ssam 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
20210748Ssam 			return (1);
20310748Ssam 		while (*Bp && *Bp != ':' && *Bp != '|')
20410748Ssam 			Bp++;
20510748Ssam 		if (*Bp == 0 || *Bp == ':')
20610748Ssam 			return (0);
20710748Ssam 		Bp++;
20810748Ssam 	}
20910748Ssam }
21010748Ssam 
21110748Ssam /*
21210748Ssam  * Skip to the next field.  Notice that this is very dumb, not
21310748Ssam  * knowing about \: escapes or any such.  If necessary, :'s can be put
21410748Ssam  * into the diskcap file in octal.
21510748Ssam  */
21610748Ssam static char *
21710748Ssam dskip(bp)
21810748Ssam 	register char *bp;
21910748Ssam {
22010748Ssam 
22110748Ssam 	while (*bp && *bp != ':')
22210748Ssam 		bp++;
22310748Ssam 	if (*bp == ':')
22410748Ssam 		bp++;
22510748Ssam 	return (bp);
22610748Ssam }
22710748Ssam 
22810748Ssam /*
22910748Ssam  * Return the (numeric) option id.
23010748Ssam  * Numeric options look like
23110748Ssam  *	li#80
23210748Ssam  * i.e. the option string is separated from the numeric value by
23310748Ssam  * a # character.  If the option is not found we return -1.
23410748Ssam  * Note that we handle octal numbers beginning with 0.
23510748Ssam  */
23610748Ssam static
23710748Ssam dgetnum(id)
23810748Ssam 	char *id;
23910748Ssam {
24010748Ssam 	register int i, base;
24110748Ssam 	register char *bp = tbuf;
24210748Ssam 
24310748Ssam 	for (;;) {
24410748Ssam 		bp = dskip(bp);
24510748Ssam 		if (*bp == 0)
24610748Ssam 			return (-1);
24710748Ssam 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
24810748Ssam 			continue;
24910748Ssam 		if (*bp == '@')
25010748Ssam 			return (-1);
25110748Ssam 		if (*bp != '#')
25210748Ssam 			continue;
25310748Ssam 		bp++;
25410748Ssam 		base = 10;
25510748Ssam 		if (*bp == '0')
25610748Ssam 			base = 8;
25710748Ssam 		i = 0;
25810748Ssam 		while (isdigit(*bp))
25910748Ssam 			i *= base, i += *bp++ - '0';
26010748Ssam 		return (i);
26110748Ssam 	}
26210748Ssam }
26310748Ssam 
26410748Ssam /*
26510748Ssam  * Handle a flag option.
26610748Ssam  * Flag options are given "naked", i.e. followed by a : or the end
26710748Ssam  * of the buffer.  Return 1 if we find the option, or 0 if it is
26810748Ssam  * not given.
26910748Ssam  */
27010748Ssam static
27110748Ssam dgetflag(id)
27210748Ssam 	char *id;
27310748Ssam {
27410748Ssam 	register char *bp = tbuf;
27510748Ssam 
27610748Ssam 	for (;;) {
27710748Ssam 		bp = dskip(bp);
27810748Ssam 		if (!*bp)
27910748Ssam 			return (0);
28010748Ssam 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
28110748Ssam 			if (!*bp || *bp == ':')
28210748Ssam 				return (1);
28310748Ssam 			else if (*bp == '@')
28410748Ssam 				return (0);
28510748Ssam 		}
28610748Ssam 	}
28710748Ssam }
28810748Ssam 
28910748Ssam /*
29010748Ssam  * Get a string valued option.
29110748Ssam  * These are given as
29210748Ssam  *	cl=^Z
29310748Ssam  * Much decoding is done on the strings, and the strings are
29410748Ssam  * placed in area, which is a ref parameter which is updated.
29510748Ssam  * No checking on area overflow.
29610748Ssam  */
29710748Ssam static char *
29810748Ssam dgetstr(id, area)
29910748Ssam 	char *id, **area;
30010748Ssam {
30110748Ssam 	register char *bp = tbuf;
30210748Ssam 
30310748Ssam 	for (;;) {
30410748Ssam 		bp = dskip(bp);
30510748Ssam 		if (!*bp)
30610748Ssam 			return (0);
30710748Ssam 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
30810748Ssam 			continue;
30910748Ssam 		if (*bp == '@')
31010748Ssam 			return (0);
31110748Ssam 		if (*bp != '=')
31210748Ssam 			continue;
31310748Ssam 		bp++;
31410748Ssam 		return (ddecode(bp, area));
31510748Ssam 	}
31610748Ssam }
31710748Ssam 
31810748Ssam /*
31910748Ssam  * Tdecode does the grung work to decode the
32010748Ssam  * string capability escapes.
32110748Ssam  */
32210748Ssam static char *
32310748Ssam ddecode(str, area)
32410748Ssam 	register char *str;
32510748Ssam 	char **area;
32610748Ssam {
32710748Ssam 	register char *cp;
32810748Ssam 	register int c;
32910748Ssam 	register char *dp;
33010748Ssam 	int i;
33110748Ssam 
33210748Ssam 	cp = *area;
33310748Ssam 	while ((c = *str++) && c != ':') {
33410748Ssam 		switch (c) {
33510748Ssam 
33610748Ssam 		case '^':
33710748Ssam 			c = *str++ & 037;
33810748Ssam 			break;
33910748Ssam 
34010748Ssam 		case '\\':
34110748Ssam 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
34210748Ssam 			c = *str++;
34310748Ssam nextc:
34410748Ssam 			if (*dp++ == c) {
34510748Ssam 				c = *dp++;
34610748Ssam 				break;
34710748Ssam 			}
34810748Ssam 			dp++;
34910748Ssam 			if (*dp)
35010748Ssam 				goto nextc;
35110748Ssam 			if (isdigit(c)) {
35210748Ssam 				c -= '0', i = 2;
35310748Ssam 				do
35410748Ssam 					c <<= 3, c |= *str++ - '0';
35510748Ssam 				while (--i && isdigit(*str));
35610748Ssam 			}
35710748Ssam 			break;
35810748Ssam 		}
35910748Ssam 		*cp++ = c;
36010748Ssam 	}
36110748Ssam 	*cp++ = 0;
36210748Ssam 	str = *area;
36310748Ssam 	*area = cp;
36410748Ssam 	return (str);
36510748Ssam }
36630420Skarels 
36730420Skarels static
36830420Skarels gettype(t, names)
36930420Skarels 	char *t;
37030420Skarels 	char **names;
37130420Skarels {
37230420Skarels 	register char **nm;
37330420Skarels 
37430420Skarels 	for (nm = names; *nm; nm++)
37533261Smckusick 		if (strcasecmp(t, *nm) == 0)
37630420Skarels 			return (nm - names);
37730420Skarels 	if (isdigit(*t))
37830420Skarels 		return (atoi(t));
37930420Skarels 	return (0);
38030420Skarels }
38130420Skarels 
38230420Skarels dkcksum(lp)
38330420Skarels 	register struct disklabel *lp;
38430420Skarels {
38530420Skarels 	register u_short *start, *end;
38630420Skarels 	register u_short sum = 0;
38730420Skarels 
38830420Skarels 	start = (u_short *)lp;
38930420Skarels 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
39030420Skarels 	while (start < end)
39130420Skarels 		sum ^= *start++;
39230420Skarels 	return (sum);
39330420Skarels }
394