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