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