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