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