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