geom_pc98.c revision 108650
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 * $FreeBSD: head/sys/geom/geom_pc98.c 108650 2003-01-04 08:50:48Z nyan $
33 */
34
35#include <sys/param.h>
36#ifndef _KERNEL
37#include <signal.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <err.h>
42#else
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#include <sys/bio.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#endif
50
51#include <sys/diskpc98.h>
52#include <geom/geom.h>
53#include <geom/geom_slice.h>
54
55#define PC98_CLASS_NAME "PC98"
56
57static void
58g_dec_pc98_partition(u_char *ptr, struct pc98_partition *d)
59{
60	u_int u;
61
62	d->dp_mid = ptr[0];
63	d->dp_sid = ptr[1];
64	d->dp_dum1 = ptr[2];
65	d->dp_dum2 = ptr[3];
66	d->dp_ipl_sct = ptr[4];
67	d->dp_ipl_head = ptr[5];
68	d->dp_ipl_cyl = g_dec_le2(ptr + 6);
69	d->dp_ssect = ptr[8];
70	d->dp_shd = ptr[9];
71	d->dp_scyl = g_dec_le2(ptr + 10);
72	d->dp_esect = ptr[12];
73	d->dp_ehd = ptr[13];
74	d->dp_ecyl = g_dec_le2(ptr + 14);
75	for (u = 0; u < sizeof(d->dp_name); u++)
76		d->dp_name[u] = ptr[16 + u];
77}
78
79struct g_pc98_softc {
80	u_int fwsectors, fwheads, sectorsize;
81	int type[NDOSPART];
82	u_char sec[8192];
83};
84
85static void
86g_pc98_print(int i, struct pc98_partition *dp)
87{
88	char sname[17];
89
90	strncpy(sname, dp->dp_name, 16);
91	sname[16] = '\0';
92
93	g_hexdump(dp, sizeof(dp[0]));
94	printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
95	       i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
96	printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
97	printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
98	printf(" sname:%s\n", sname);
99}
100
101static int
102g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec)
103{
104	int i, error;
105	off_t s[NDOSPART], l[NDOSPART];
106	struct pc98_partition dp[NDOSPART];
107
108	g_topology_assert();
109
110	if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa)
111		return (EBUSY);
112
113#if 0
114	/*
115	 * XXX: Some sources indicate this is a magic sequence, but appearantly
116	 * XXX: it is not universal. Documentation would be wonderfule to have.
117	 */
118	if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1')
119		return (EBUSY);
120#endif
121
122	for (i = 0; i < NDOSPART; i++)
123		g_dec_pc98_partition(
124			sec + 512 + i * sizeof(struct pc98_partition), &dp[i]);
125
126	for (i = 0; i < NDOSPART; i++) {
127		/* If start and end are identical it's bogus */
128		if (dp[i].dp_ssect == dp[i].dp_esect &&
129		    dp[i].dp_shd == dp[i].dp_ehd &&
130		    dp[i].dp_scyl == dp[i].dp_ecyl)
131			s[i] = l[i] = 0;
132		else if (dp[i].dp_ecyl == 0)
133			s[i] = l[i] = 0;
134		else {
135			s[i] = (off_t)dp[i].dp_scyl *
136				ms->fwsectors * ms->fwheads * ms->sectorsize;
137			l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) *
138				ms->fwsectors * ms->fwheads * ms->sectorsize;
139		}
140		if (bootverbose) {
141			printf("PC98 Slice %d on %s:\n", i + 1, gp->name);
142			g_pc98_print(i, dp + i);
143		}
144		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
145				       s[i], l[i], ms->sectorsize,
146				       "%ss%d", gp->name, i + 1);
147		if (error)
148			return (error);
149	}
150
151	for (i = 0; i < NDOSPART; i++) {
152		ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
153		g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i],
154			       ms->sectorsize, "%ss%d", gp->name, i + 1);
155	}
156
157	bcopy(sec, ms->sec, sizeof (ms->sec));
158
159	return (0);
160}
161
162static void
163g_pc98_ioctl(void *arg)
164{
165	struct bio *bp;
166	struct g_geom *gp;
167	struct g_slicer *gsp;
168	struct g_pc98_softc *ms;
169	struct g_ioctl *gio;
170	struct g_consumer *cp;
171	u_char *sec;
172	int error;
173
174	/* Get hold of the interesting bits from the bio. */
175	bp = arg;
176	gp = bp->bio_to->geom;
177	gsp = gp->softc;
178	ms = gsp->softc;
179	gio = (struct g_ioctl *)bp->bio_data;
180
181	/* The disklabel to set is the ioctl argument. */
182	sec = gio->data;
183
184	error = g_pc98_modify(gp, ms, sec);
185	if (error) {
186		g_io_deliver(bp, error);
187		return;
188	}
189	cp = LIST_FIRST(&gp->consumer);
190	error = g_write_data(cp, 0, sec, 8192);
191	g_io_deliver(bp, error);
192}
193
194static int
195g_pc98_start(struct bio *bp)
196{
197	struct g_provider *pp;
198	struct g_geom *gp;
199	struct g_pc98_softc *mp;
200	struct g_slicer *gsp;
201	struct g_ioctl *gio;
202	int idx, error;
203
204	pp = bp->bio_to;
205	idx = pp->index;
206	gp = pp->geom;
207	gsp = gp->softc;
208	mp = gsp->softc;
209	if (bp->bio_cmd == BIO_GETATTR) {
210		if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
211			return (1);
212		if (g_handleattr_off_t(bp, "PC98::offset",
213				       gsp->slices[idx].offset))
214			return (1);
215	}
216
217	/* We only handle ioctl(2) requests of the right format. */
218	if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
219		return (0);
220	else if (bp->bio_length != sizeof(*gio))
221		return (0);
222	/* Get hold of the ioctl parameters. */
223	gio = (struct g_ioctl *)bp->bio_data;
224
225	switch (gio->cmd) {
226	case DIOCGPC98:
227		/* Return a copy of the disklabel to userland. */
228		bcopy(mp->sec, gio->data, 8192);
229		g_io_deliver(bp, 0);
230		return (1);
231	case DIOCSPC98:
232		/*
233		 * These we cannot do without the topology lock and some
234		 * some I/O requests.  Ask the event-handler to schedule
235		 * us in a less restricted environment.
236		 */
237		error = g_call_me(g_pc98_ioctl, bp);
238		if (error)
239			g_io_deliver(bp, error);
240		/*
241		 * We must return non-zero to indicate that we will deal
242		 * with this bio, even though we have not done so yet.
243		 */
244		return (1);
245	default:
246		return (0);
247	}
248
249	return (0);
250}
251
252static void
253g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
254		struct g_consumer *cp __unused, struct g_provider *pp)
255{
256	struct g_pc98_softc *mp;
257	struct g_slicer *gsp;
258	struct pc98_partition dp;
259	char sname[17];
260
261	gsp = gp->softc;
262	mp = gsp->softc;
263	g_slice_dumpconf(sb, indent, gp, cp, pp);
264	if (pp != NULL) {
265		g_dec_pc98_partition(
266			mp->sec + 512 +
267			pp->index * sizeof(struct pc98_partition), &dp);
268		strncpy(sname, dp.dp_name, 16);
269		sname[16] = '\0';
270		if (indent == NULL) {
271			sbuf_printf(sb, " ty %d", mp->type[pp->index]);
272			sbuf_printf(sb, " sn %s", sname);
273		} else {
274			sbuf_printf(sb, "%s<type>%d</type>\n", indent,
275				    mp->type[pp->index]);
276			sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
277				    sname);
278		}
279	}
280}
281
282static struct g_geom *
283g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
284{
285	struct g_geom *gp;
286	struct g_consumer *cp;
287	int error;
288	struct g_pc98_softc *ms;
289	struct g_slicer *gsp;
290	u_int fwsectors, fwheads, sectorsize;
291	u_char *buf;
292
293	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
294	g_topology_assert();
295	if (flags == G_TF_NORMAL &&
296	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
297		return (NULL);
298	gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
299	if (gp == NULL)
300		return (NULL);
301	gsp = gp->softc;
302	g_topology_unlock();
303	gp->dumpconf = g_pc98_dumpconf;
304	while (1) {	/* a trick to allow us to use break */
305		if (gp->rank != 2 && flags == G_TF_NORMAL)
306			break;
307		error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
308		if (error || fwsectors == 0) {
309			fwsectors = 17;
310			printf("g_pc98_taste: error %d guessing %d sectors\n",
311			    error, fwsectors);
312		}
313		error = g_getattr("GEOM::fwheads", cp, &fwheads);
314		if (error || fwheads == 0) {
315			fwheads = 8;
316			printf("g_pc98_taste: error %d guessing %d heads\n",
317			    error, fwheads);
318		}
319		sectorsize = cp->provider->sectorsize;
320		if (sectorsize < 512)
321			break;
322		gsp->frontstuff = sectorsize * fwsectors;
323		buf = g_read_data(cp, 0, 8192, &error);
324		if (buf == NULL || error != 0)
325			break;
326		ms->fwsectors = fwsectors;
327		ms->fwheads = fwheads;
328		ms->sectorsize = sectorsize;
329		g_topology_lock();
330		g_pc98_modify(gp, ms, buf);
331		g_topology_unlock();
332		g_free(buf);
333		break;
334	}
335	g_topology_lock();
336	g_access_rel(cp, -1, 0, 0);
337	if (LIST_EMPTY(&gp->provider)) {
338		g_std_spoiled(cp);
339		return (NULL);
340	}
341	return (gp);
342}
343
344static struct g_class g_pc98_class = {
345	PC98_CLASS_NAME,
346	g_pc98_taste,
347	NULL,
348	G_CLASS_INITIALIZER
349};
350
351DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
352