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