disklabel.c revision 1.14 1 /* $NetBSD: disklabel.c,v 1.14 1997/07/13 18:54:50 christos 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 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)disklabel.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: disklabel.c,v 1.14 1997/07/13 18:54:50 christos Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include <sys/param.h>
46 #define DKTYPENAMES
47 #include <sys/disklabel.h>
48 #include <ufs/ffs/fs.h>
49
50 #include <ctype.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #if 0
59 static void error __P((int));
60 #endif
61 static int gettype __P((char *, char **));
62
63 struct disklabel *
64 getdiskbyname(name)
65 const char *name;
66 {
67 static struct disklabel disk;
68 register struct disklabel *dp = &disk;
69 register struct partition *pp;
70 char *buf;
71 char *db_array[2] = { _PATH_DISKTAB, 0 };
72 char *cp, *cq; /* can't be register */
73 char p, max, psize[3], pbsize[3],
74 pfsize[3], poffset[3], ptype[3];
75 u_int32_t *dx;
76
77 if (cgetent(&buf, db_array, (char *) name) < 0)
78 return NULL;
79
80 bzero((char *)&disk, sizeof(disk));
81 /*
82 * typename
83 */
84 cq = dp->d_typename;
85 cp = buf;
86 while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
87 (*cq = *cp) && *cq != '|' && *cq != ':')
88 cq++, cp++;
89 *cq = '\0';
90 /*
91 * boot name (optional) xxboot, bootxx
92 */
93 cgetstr(buf, "b0", &dp->d_boot0);
94 cgetstr(buf, "b1", &dp->d_boot1);
95
96 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
97 dp->d_flags |= D_REMOVABLE;
98 else if (cq && strcmp(cq, "simulated") == 0)
99 dp->d_flags |= D_RAMDISK;
100 if (cgetcap(buf, "sf", ':') != NULL)
101 dp->d_flags |= D_BADSECT;
102
103 #define getnumdflt(field, dname, dflt) \
104 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
105 #define getnum(field, dname) \
106 { long f; cgetnum(buf, dname, &f); field = f; }
107
108 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
109 getnum(dp->d_ntracks, "nt");
110 getnum(dp->d_nsectors, "ns");
111 getnum(dp->d_ncylinders, "nc");
112
113 if (cgetstr(buf, "dt", &cq) > 0)
114 dp->d_type = gettype(cq, dktypenames);
115 else
116 getnumdflt(dp->d_type, "dt", 0);
117 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
118 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
119 getnumdflt(dp->d_rpm, "rm", 3600);
120 getnumdflt(dp->d_interleave, "il", 1);
121 getnumdflt(dp->d_trackskew, "sk", 0);
122 getnumdflt(dp->d_cylskew, "cs", 0);
123 getnumdflt(dp->d_headswitch, "hs", 0);
124 getnumdflt(dp->d_trkseek, "ts", 0);
125 getnumdflt(dp->d_bbsize, "bs", BBSIZE);
126 getnumdflt(dp->d_sbsize, "sb", SBSIZE);
127 strcpy(psize, "px"); /* XXX: strcpy is safe */
128 strcpy(pbsize, "bx"); /* XXX: strcpy is safe */
129 strcpy(pfsize, "fx"); /* XXX: strcpy is safe */
130 strcpy(poffset, "ox"); /* XXX: strcpy is safe */
131 strcpy(ptype, "tx"); /* XXX: strcpy is safe */
132 max = 'a' - 1;
133 pp = &dp->d_partitions[0];
134 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
135 long f;
136
137 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
138 if (cgetnum(buf, psize, &f) == -1)
139 pp->p_size = 0;
140 else {
141 pp->p_size = f;
142 getnum(pp->p_offset, poffset);
143 getnumdflt(pp->p_fsize, pfsize, 0);
144 if (pp->p_fsize) {
145 long bsize;
146
147 if (cgetnum(buf, pbsize, &bsize) == 0)
148 pp->p_frag = bsize / pp->p_fsize;
149 else
150 pp->p_frag = 8;
151 }
152 getnumdflt(pp->p_fstype, ptype, 0);
153 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
154 pp->p_fstype = gettype(cq, fstypenames);
155 max = p;
156 }
157 }
158 dp->d_npartitions = max + 1 - 'a';
159 strcpy(psize, "dx"); /* XXX: strcpy is safe */
160 dx = dp->d_drivedata;
161 for (p = '0'; p < '0' + NDDATA; p++, dx++) {
162 psize[1] = p;
163 getnumdflt(*dx, psize, 0);
164 }
165 dp->d_magic = DISKMAGIC;
166 dp->d_magic2 = DISKMAGIC;
167 free(buf);
168 return (dp);
169 }
170
171 static int
172 gettype(t, names)
173 char *t;
174 char **names;
175 {
176 register char **nm;
177
178 for (nm = names; *nm; nm++)
179 if (strcasecmp(t, *nm) == 0)
180 return (nm - names);
181 if (isdigit(*t))
182 return (atoi(t));
183 return (0);
184 }
185
186 #if 0
187 static void
188 error(err)
189 int err;
190 {
191 char *p;
192
193 (void)write(STDERR_FILENO, "disktab: ", 9);
194 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
195 (void)write(STDERR_FILENO, ": ", 2);
196 p = strerror(err);
197 (void)write(STDERR_FILENO, p, strlen(p));
198 (void)write(STDERR_FILENO, "\n", 1);
199 }
200 #endif
201