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