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