FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_pc98.c
1 /*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/5.4/sys/geom/geom_pc98.c 145335 2005-04-20 19:11:07Z cvs2svn $");
35
36 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
42 #include <sys/bio.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45
46 #include <sys/diskpc98.h>
47 #include <geom/geom.h>
48 #include <geom/geom_slice.h>
49
50 #define PC98_CLASS_NAME "PC98"
51
52 struct g_pc98_softc {
53 u_int fwsectors, fwheads, sectorsize;
54 int type[NDOSPART];
55 u_char sec[8192];
56 };
57
58 static void
59 g_pc98_print(int i, struct pc98_partition *dp)
60 {
61 char sname[17];
62
63 strncpy(sname, dp->dp_name, 16);
64 sname[16] = '\0';
65
66 hexdump(dp, sizeof(dp[0]), NULL, 0);
67 printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
68 i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
69 printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
70 printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
71 printf(" sname:%s\n", sname);
72 }
73
74 static int
75 g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec)
76 {
77 int i, error;
78 off_t s[NDOSPART], l[NDOSPART];
79 struct pc98_partition dp[NDOSPART];
80
81 g_topology_assert();
82
83 if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa)
84 return (EBUSY);
85
86 #if 0
87 /*
88 * XXX: Some sources indicate this is a magic sequence, but appearantly
89 * XXX: it is not universal. Documentation would be wonderful to have.
90 */
91 if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1')
92 return (EBUSY);
93 #endif
94
95 for (i = 0; i < NDOSPART; i++)
96 pc98_partition_dec(
97 sec + 512 + i * sizeof(struct pc98_partition), &dp[i]);
98
99 for (i = 0; i < NDOSPART; i++) {
100 /* If start and end are identical it's bogus */
101 if (dp[i].dp_ssect == dp[i].dp_esect &&
102 dp[i].dp_shd == dp[i].dp_ehd &&
103 dp[i].dp_scyl == dp[i].dp_ecyl)
104 s[i] = l[i] = 0;
105 else if (dp[i].dp_ecyl == 0)
106 s[i] = l[i] = 0;
107 else {
108 s[i] = (off_t)dp[i].dp_scyl *
109 ms->fwsectors * ms->fwheads * ms->sectorsize;
110 l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) *
111 ms->fwsectors * ms->fwheads * ms->sectorsize;
112 }
113 if (bootverbose) {
114 printf("PC98 Slice %d on %s:\n", i + 1, gp->name);
115 g_pc98_print(i, dp + i);
116 }
117 if (s[i] < 0 || l[i] < 0)
118 error = EBUSY;
119 else
120 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
121 s[i], l[i], ms->sectorsize,
122 "%ss%d", gp->name, i + 1);
123 if (error)
124 return (error);
125 }
126
127 for (i = 0; i < NDOSPART; i++) {
128 ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
129 g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i],
130 ms->sectorsize, "%ss%d", gp->name, i + 1);
131 }
132
133 bcopy(sec, ms->sec, sizeof (ms->sec));
134
135 return (0);
136 }
137
138 static int
139 g_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
140 {
141 struct g_geom *gp;
142 struct g_pc98_softc *ms;
143 struct g_slicer *gsp;
144 struct g_consumer *cp;
145 int error;
146
147 gp = pp->geom;
148 gsp = gp->softc;
149 ms = gsp->softc;
150
151 switch(cmd) {
152 case DIOCSPC98: {
153 if (!(fflag & FWRITE))
154 return (EPERM);
155 DROP_GIANT();
156 g_topology_lock();
157 /* Validate and modify our slicer instance to match. */
158 error = g_pc98_modify(gp, ms, data);
159 cp = LIST_FIRST(&gp->consumer);
160 error = g_write_data(cp, 0, data, 8192);
161 g_topology_unlock();
162 PICKUP_GIANT();
163 return(error);
164 }
165 default:
166 return (ENOIOCTL);
167 }
168 }
169
170 static int
171 g_pc98_start(struct bio *bp)
172 {
173 struct g_provider *pp;
174 struct g_geom *gp;
175 struct g_pc98_softc *mp;
176 struct g_slicer *gsp;
177 int idx;
178
179 pp = bp->bio_to;
180 idx = pp->index;
181 gp = pp->geom;
182 gsp = gp->softc;
183 mp = gsp->softc;
184 if (bp->bio_cmd == BIO_GETATTR) {
185 if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
186 return (1);
187 if (g_handleattr_off_t(bp, "PC98::offset",
188 gsp->slices[idx].offset))
189 return (1);
190 }
191
192 return (0);
193 }
194
195 static void
196 g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
197 struct g_consumer *cp __unused, struct g_provider *pp)
198 {
199 struct g_pc98_softc *mp;
200 struct g_slicer *gsp;
201 struct pc98_partition dp;
202 char sname[17];
203
204 gsp = gp->softc;
205 mp = gsp->softc;
206 g_slice_dumpconf(sb, indent, gp, cp, pp);
207 if (pp != NULL) {
208 pc98_partition_dec(
209 mp->sec + 512 +
210 pp->index * sizeof(struct pc98_partition), &dp);
211 strncpy(sname, dp.dp_name, 16);
212 sname[16] = '\0';
213 if (indent == NULL) {
214 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
215 sbuf_printf(sb, " sn %s", sname);
216 } else {
217 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
218 mp->type[pp->index]);
219 sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
220 sname);
221 }
222 }
223 }
224
225 static struct g_geom *
226 g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
227 {
228 struct g_geom *gp;
229 struct g_consumer *cp;
230 int error;
231 struct g_pc98_softc *ms;
232 u_int fwsectors, fwheads, sectorsize;
233 u_char *buf;
234
235 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
236 g_topology_assert();
237 if (flags == G_TF_NORMAL &&
238 !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
239 return (NULL);
240 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
241 if (gp == NULL)
242 return (NULL);
243 g_topology_unlock();
244 do {
245 if (gp->rank != 2 && flags == G_TF_NORMAL)
246 break;
247 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
248 if (error || fwsectors == 0) {
249 fwsectors = 17;
250 if (bootverbose)
251 printf("g_pc98_taste: guessing %d sectors\n",
252 fwsectors);
253 }
254 error = g_getattr("GEOM::fwheads", cp, &fwheads);
255 if (error || fwheads == 0) {
256 fwheads = 8;
257 if (bootverbose)
258 printf("g_pc98_taste: guessing %d heads\n",
259 fwheads);
260 }
261 sectorsize = cp->provider->sectorsize;
262 if (sectorsize < 512)
263 break;
264 buf = g_read_data(cp, 0, 8192, &error);
265 if (buf == NULL || error != 0)
266 break;
267 ms->fwsectors = fwsectors;
268 ms->fwheads = fwheads;
269 ms->sectorsize = sectorsize;
270 g_topology_lock();
271 g_pc98_modify(gp, ms, buf);
272 g_topology_unlock();
273 g_free(buf);
274 break;
275 } while (0);
276 g_topology_lock();
277 g_access(cp, -1, 0, 0);
278 if (LIST_EMPTY(&gp->provider)) {
279 g_slice_spoiled(cp);
280 return (NULL);
281 }
282 return (gp);
283 }
284
285 static struct g_class g_pc98_class = {
286 .name = PC98_CLASS_NAME,
287 .version = G_VERSION,
288 .taste = g_pc98_taste,
289 .dumpconf = g_pc98_dumpconf,
290 .ioctl = g_pc98_ioctl,
291 };
292
293 DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
Cache object: dac0087bfcee3a9baa751821cc581c23
|