disklabel.c revision 1.8 1 /* $NetBSD: disklabel.c,v 1.8 1995/02/25 17:17:42 cgd 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 #ifndef 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.8 1995/02/25 17:17:42 cgd Exp $";
41 #endif
42 #endif /* 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 <errno.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 static void error __P((int));
57 static int gettype __P((char *, char **));
58
59 struct disklabel *
60 getdiskbyname(name)
61 const char *name;
62 {
63 static struct disklabel disk;
64 register struct disklabel *dp = &disk;
65 register struct partition *pp;
66 char *buf;
67 char *db_array[2] = { _PATH_DISKTAB, 0 };
68 char *cp, *cq; /* can't be register */
69 char p, max, psize[3], pbsize[3],
70 pfsize[3], poffset[3], ptype[3];
71 u_int32_t *dx;
72
73 if (cgetent(&buf, db_array, (char *) name) < 0)
74 return NULL;
75
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 cgetstr(buf, "b0", &dp->d_boot0);
90 cgetstr(buf, "b1", &dp->d_boot1);
91
92 if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
93 dp->d_flags |= D_REMOVABLE;
94 else if (cq && strcmp(cq, "simulated") == 0)
95 dp->d_flags |= D_RAMDISK;
96 if (cgetcap(buf, "sf", ':') != NULL)
97 dp->d_flags |= D_BADSECT;
98
99 #define getnumdflt(field, dname, dflt) \
100 { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
101
102 getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
103 cgetnum(buf, "nt",(long *) &dp->d_ntracks);
104 cgetnum(buf, "ns",(long *) &dp->d_nsectors);
105 cgetnum(buf, "nc",(long *) &dp->d_ncylinders);
106
107 if (cgetstr(buf, "dt", &cq) > 0)
108 dp->d_type = gettype(cq, dktypenames);
109 else
110 getnumdflt(dp->d_type, "dt", 0);
111 getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
112 getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
113 getnumdflt(dp->d_rpm, "rm", 3600);
114 getnumdflt(dp->d_interleave, "il", 1);
115 getnumdflt(dp->d_trackskew, "sk", 0);
116 getnumdflt(dp->d_cylskew, "cs", 0);
117 getnumdflt(dp->d_headswitch, "hs", 0);
118 getnumdflt(dp->d_trkseek, "ts", 0);
119 getnumdflt(dp->d_bbsize, "bs", BBSIZE);
120 getnumdflt(dp->d_sbsize, "sb", SBSIZE);
121 strcpy(psize, "px");
122 strcpy(pbsize, "bx");
123 strcpy(pfsize, "fx");
124 strcpy(poffset, "ox");
125 strcpy(ptype, "tx");
126 max = 'a' - 1;
127 pp = &dp->d_partitions[0];
128 for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
129 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
130 if (cgetnum(buf, psize,(long *) &pp->p_size) == -1)
131 pp->p_size = 0;
132 else {
133 cgetnum(buf, poffset, (long *) &pp->p_offset);
134 getnumdflt(pp->p_fsize, pfsize, 0);
135 if (pp->p_fsize) {
136 long bsize;
137
138 if (cgetnum(buf, pbsize, &bsize) == 0)
139 pp->p_frag = bsize / pp->p_fsize;
140 else
141 pp->p_frag = 8;
142 }
143 getnumdflt(pp->p_fstype, ptype, 0);
144 if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
145 pp->p_fstype = gettype(cq, fstypenames);
146 max = p;
147 }
148 }
149 dp->d_npartitions = max + 1 - 'a';
150 (void)strcpy(psize, "dx");
151 dx = dp->d_drivedata;
152 for (p = '0'; p < '0' + NDDATA; p++, dx++) {
153 psize[1] = p;
154 getnumdflt(*dx, psize, 0);
155 }
156 dp->d_magic = DISKMAGIC;
157 dp->d_magic2 = DISKMAGIC;
158 free(buf);
159 return (dp);
160 }
161
162 static int
163 gettype(t, names)
164 char *t;
165 char **names;
166 {
167 register char **nm;
168
169 for (nm = names; *nm; nm++)
170 if (strcasecmp(t, *nm) == 0)
171 return (nm - names);
172 if (isdigit(*t))
173 return (atoi(t));
174 return (0);
175 }
176
177 static void
178 error(err)
179 int err;
180 {
181 char *p;
182
183 (void)write(STDERR_FILENO, "disktab: ", 9);
184 (void)write(STDERR_FILENO, _PATH_DISKTAB, sizeof(_PATH_DISKTAB) - 1);
185 (void)write(STDERR_FILENO, ": ", 2);
186 p = strerror(err);
187 (void)write(STDERR_FILENO, p, strlen(p));
188 (void)write(STDERR_FILENO, "\n", 1);
189 }
190