disklabel.c revision 1.10 1 /* $NetBSD: disklabel.c,v 1.10 1995/05/13 06:58:20 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)disklabel.c 8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: disklabel.c,v 1.10 1995/05/13 06:58:20 jtc Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include <sys/param.h>
45 #define DKTYPENAMES
46 #include <sys/disklabel.h>
47 #include <ufs/ffs/fs.h>
48
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 static void error __P((int));
58 static int gettype __P((char *, char **));
59
60 struct disklabel *
61 getdiskbyname(name)
62 const char *name;
63 {
64 static struct disklabel disk;
65 register struct disklabel *dp = &disk;
66 register struct partition *pp;
67 char *buf;
68 char *db_array[2] = { _PATH_DISKTAB, 0 };
69 char *cp, *cq; /* can't be register */
70 char p, max, psize[3], pbsize[3],
71 pfsize[3], poffset[3], ptype[3];
72 u_int32_t *dx;
73
74 if (cgetent(&buf, db_array, (char *) name) < 0)
75 return NULL;
76
77 bzero((char *)&disk, sizeof(disk));
78 /*
79 * typename
80 */
81 cq = dp->d_typename;
82 cp = buf;
83 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
84 (*cq = *cp) && *cq != '|' && *cq != ':')
85 cq++, cp++;
86 *cq = '\0';
87 /*
88 * boot name (optional) xxboot, bootxx
89 */
90 cgetstr(buf, "b0", &dp->d_boot0);
91 cgetstr(buf, "b1", &dp->d_boot1);
92
93 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
94 dp->d_flags |= D_REMOVABLE;
95 else if (cq && strcmp(cq, "simulated") == 0)
96 dp->d_flags |= D_RAMDISK;
97 if (cgetcap(buf, "sf", ':') != NULL)
98 dp->d_flags |= D_BADSECT;
99
100 #define getnumdflt(field, dname, dflt) \
101 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
102
103 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
104 cgetnum(buf, "nt",(long *) &dp->d_ntracks);
105 cgetnum(buf, "ns",(long *) &dp->d_nsectors);
106 cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
107
108 if (cgetstr(buf, "dt", &cq) > 0)
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 if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
132 pp->p_size = 0;
133 else {
134 cgetnum(buf, poffset, (long *) &pp->p_offset);
135 getnumdflt(pp->p_fsize, pfsize, 0);
136 if (pp->p_fsize) {
137 long bsize;
138
139 if (cgetnum(buf, pbsize, &bsize) == 0)
140 pp->p_frag = bsize / pp->p_fsize;
141 else
142 pp->p_frag = 8;
143 }
144 getnumdflt(pp->p_fstype, ptype, 0);
145 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
146 pp->p_fstype = gettype(cq, fstypenames);
147 max = p;
148 }
149 }
150 dp->d_npartitions = max + 1 - 'a';
151 (void)strcpy(psize, "dx");
152 dx = dp->d_drivedata;
153 for (p = '0'; p < '0' + NDDATA; p++, dx++) {
154 psize[1] = p;
155 getnumdflt(*dx, psize, 0);
156 }
157 dp->d_magic = DISKMAGIC;
158 dp->d_magic2 = DISKMAGIC;
159 free(buf);
160 return (dp);
161 }
162
163 static int
164 gettype(t, names)
165 char *t;
166 char **names;
167 {
168 register char **nm;
169
170 for (nm = names; *nm; nm++)
171 if (strcasecmp(t, *nm) == 0)
172 return (nm - names);
173 if (isdigit(*t))
174 return (atoi(t));
175 return (0);
176 }
177
178 static void
179 error(err)
180 int err;
181 {
182 char *p;
183
184 (void)write(STDERR_FILENO, "disktab: ", 9);
185 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
186 (void)write(STDERR_FILENO, ": ", 2);
187 p = strerror(err);
188 (void)write(STDERR_FILENO, p, strlen(p));
189 (void)write(STDERR_FILENO, "\n", 1);
190 }
191