geom_pc98.c revision 113011
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 113011 2003-04-03 11:36:53Z phk $
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 <errno.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 = le16dec(ptr + 6);
69	d->dp_ssect = ptr[8];
70	d->dp_shd = ptr[9];
71	d->dp_scyl = le16dec(ptr + 10);
72	d->dp_esect = ptr[12];
73	d->dp_ehd = ptr[13];
74	d->dp_ecyl = le16dec(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, int flag)
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	bp = arg;
175	if (flag == EV_CANCEL) {
176		g_io_deliver(bp, ENXIO);
177		return;
178	}
179	gp = bp->bio_to->geom;
180	gsp = gp->softc;
181	ms = gsp->softc;
182	gio = (struct g_ioctl *)bp->bio_data;
183
184	/* The disklabel to set is the ioctl argument. */
185	sec = gio->data;
186
187	error = g_pc98_modify(gp, ms, sec);
188	if (error) {
189		g_io_deliver(bp, error);
190		return;
191	}
192	cp = LIST_FIRST(&gp->consumer);
193	error = g_write_data(cp, 0, sec, 8192);
194	g_io_deliver(bp, error);
195}
196
197static int
198g_pc98_start(struct bio *bp)
199{
200	struct g_provider *pp;
201	struct g_geom *gp;
202	struct g_pc98_softc *mp;
203	struct g_slicer *gsp;
204	struct g_ioctl *gio;
205	int idx, error;
206
207	pp = bp->bio_to;
208	idx = pp->index;
209	gp = pp->geom;
210	gsp = gp->softc;
211	mp = gsp->softc;
212	if (bp->bio_cmd == BIO_GETATTR) {
213		if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
214			return (1);
215		if (g_handleattr_off_t(bp, "PC98::offset",
216				       gsp->slices[idx].offset))
217			return (1);
218	}
219
220	/* We only handle ioctl(2) requests of the right format. */
221	if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
222		return (0);
223	else if (bp->bio_length != sizeof(*gio))
224		return (0);
225	/* Get hold of the ioctl parameters. */
226	gio = (struct g_ioctl *)bp->bio_data;
227
228	switch (gio->cmd) {
229	case DIOCGPC98:
230		/* Return a copy of the disklabel to userland. */
231		bcopy(mp->sec, gio->data, 8192);
232		g_io_deliver(bp, 0);
233		return (1);
234	case DIOCSPC98:
235		/*
236		 * These we cannot do without the topology lock and some
237		 * some I/O requests.  Ask the event-handler to schedule
238		 * us in a less restricted environment.
239		 */
240		error = g_call_me(g_pc98_ioctl, bp, gp, NULL);
241		if (error)
242			g_io_deliver(bp, error);
243		/*
244		 * We must return non-zero to indicate that we will deal
245		 * with this bio, even though we have not done so yet.
246		 */
247		return (1);
248	default:
249		return (0);
250	}
251
252	return (0);
253}
254
255static void
256g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
257		struct g_consumer *cp __unused, struct g_provider *pp)
258{
259	struct g_pc98_softc *mp;
260	struct g_slicer *gsp;
261	struct pc98_partition dp;
262	char sname[17];
263
264	gsp = gp->softc;
265	mp = gsp->softc;
266	g_slice_dumpconf(sb, indent, gp, cp, pp);
267	if (pp != NULL) {
268		g_dec_pc98_partition(
269			mp->sec + 512 +
270			pp->index * sizeof(struct pc98_partition), &dp);
271		strncpy(sname, dp.dp_name, 16);
272		sname[16] = '\0';
273		if (indent == NULL) {
274			sbuf_printf(sb, " ty %d", mp->type[pp->index]);
275			sbuf_printf(sb, " sn %s", sname);
276		} else {
277			sbuf_printf(sb, "%s<type>%d</type>\n", indent,
278				    mp->type[pp->index]);
279			sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
280				    sname);
281		}
282	}
283}
284
285static struct g_geom *
286g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
287{
288	struct g_geom *gp;
289	struct g_consumer *cp;
290	int error;
291	struct g_pc98_softc *ms;
292	struct g_slicer *gsp;
293	u_int fwsectors, fwheads, sectorsize;
294	u_char *buf;
295
296	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
297	g_topology_assert();
298	if (flags == G_TF_NORMAL &&
299	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
300		return (NULL);
301	gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
302	if (gp == NULL)
303		return (NULL);
304	gsp = gp->softc;
305	g_topology_unlock();
306	gp->dumpconf = g_pc98_dumpconf;
307	while (1) {	/* a trick to allow us to use break */
308		if (gp->rank != 2 && flags == G_TF_NORMAL)
309			break;
310		error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
311		if (error || fwsectors == 0) {
312			fwsectors = 17;
313			printf("g_pc98_taste: error %d guessing %d sectors\n",
314			    error, fwsectors);
315		}
316		error = g_getattr("GEOM::fwheads", cp, &fwheads);
317		if (error || fwheads == 0) {
318			fwheads = 8;
319			printf("g_pc98_taste: error %d guessing %d heads\n",
320			    error, fwheads);
321		}
322		sectorsize = cp->provider->sectorsize;
323		if (sectorsize < 512)
324			break;
325		gsp->frontstuff = sectorsize * fwsectors;
326		buf = g_read_data(cp, 0, 8192, &error);
327		if (buf == NULL || error != 0)
328			break;
329		ms->fwsectors = fwsectors;
330		ms->fwheads = fwheads;
331		ms->sectorsize = sectorsize;
332		g_topology_lock();
333		g_pc98_modify(gp, ms, buf);
334		g_topology_unlock();
335		g_free(buf);
336		break;
337	}
338	g_topology_lock();
339	g_access_rel(cp, -1, 0, 0);
340	if (LIST_EMPTY(&gp->provider)) {
341		g_std_spoiled(cp);
342		return (NULL);
343	}
344	return (gp);
345}
346
347static struct g_class g_pc98_class = {
348	.name = PC98_CLASS_NAME,
349	.taste = g_pc98_taste,
350	G_CLASS_INITIALIZER
351};
352
353DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
354