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