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