geom_pc98.c revision 106559
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 106559 2002-11-07 16:42:37Z nyan $
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 <err.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_dos_partition(u_char *ptr, struct dos_partition *d)
59{
60	int i;
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 = g_dec_le2(ptr + 6);
69	d->dp_ssect = ptr[8];
70	d->dp_shd = ptr[9];
71	d->dp_scyl = g_dec_le2(ptr + 10);
72	d->dp_esect = ptr[12];
73	d->dp_ehd = ptr[13];
74	d->dp_ecyl = g_dec_le2(ptr + 14);
75	for (i = 0; i < sizeof(d->dp_name); i++)
76		d->dp_name[i] = ptr[16 + i];
77}
78
79struct g_pc98_softc {
80	int type [NDOSPART];
81	struct dos_partition dospart[NDOSPART];
82};
83
84static int
85g_pc98_start(struct bio *bp)
86{
87	struct g_provider *pp;
88	struct g_geom *gp;
89	struct g_pc98_softc *mp;
90	struct g_slicer *gsp;
91	int index;
92
93	pp = bp->bio_to;
94	index = pp->index;
95	gp = pp->geom;
96	gsp = gp->softc;
97	mp = gsp->softc;
98	if (bp->bio_cmd == BIO_GETATTR) {
99		if (g_handleattr_int(bp, "PC98::type", mp->type[index]))
100			return (1);
101		if (g_handleattr_off_t(bp, "PC98::offset",
102				       gsp->slices[index].offset))
103			return (1);
104	}
105	return (0);
106}
107
108static void
109g_pc98_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp,
110		struct g_consumer *cp __unused, struct g_provider *pp)
111{
112	struct g_pc98_softc *mp;
113	struct g_slicer *gsp;
114
115	gsp = gp->softc;
116	mp = gsp->softc;
117	g_slice_dumpconf(sb, indent, gp, cp, pp);
118	if (pp != NULL) {
119		if (indent == NULL)
120			sbuf_printf(sb, " ty %d", mp->type[pp->index]);
121		else
122			sbuf_printf(sb, "%s<type>%d</type>\n", indent,
123				    mp->type[pp->index]);
124	}
125}
126
127static void
128g_pc98_print(int i, struct dos_partition *dp)
129{
130
131	g_hexdump(dp, sizeof(dp[0]));
132	printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
133	       i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
134	printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
135	printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
136	printf(" name:%s\n", dp->dp_name);
137}
138
139static struct g_geom *
140g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
141{
142	struct g_geom *gp;
143	struct g_consumer *cp;
144	struct g_provider *pp2;
145	int error, i, npart;
146	struct dos_partition dp[NDOSPART];
147	struct g_pc98_softc *ms;
148	struct g_slicer *gsp;
149	u_int fwsectors, fwheads, sectorsize;
150	u_char *buf;
151	off_t spercyl;
152
153	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
154	g_topology_assert();
155	if (flags == G_TF_NORMAL &&
156	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
157		return (NULL);
158	gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
159	if (gp == NULL)
160		return (NULL);
161	gsp = gp->softc;
162	g_topology_unlock();
163	gp->dumpconf = g_pc98_dumpconf;
164	npart = 0;
165	while (1) {	/* a trick to allow us to use break */
166		if (gp->rank != 2 && flags == G_TF_NORMAL)
167			break;
168		error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
169		if (error || fwsectors == 0) {
170			fwsectors = 17;
171			printf("g_pc98_taste: error %d guessing %d sectors\n",
172			    error, fwsectors);
173		}
174		error = g_getattr("GEOM::fwheads", cp, &fwheads);
175		if (error || fwheads == 0) {
176			fwheads = 8;
177			printf("g_pc98_taste: error %d guessing %d heads\n",
178			    error, fwheads);
179		}
180		sectorsize = cp->provider->sectorsize;
181		if (sectorsize < 512)
182			break;
183		gsp->frontstuff = sectorsize * fwsectors;
184		spercyl = (off_t)fwsectors * fwheads * sectorsize;
185		buf = g_read_data(cp, 0,
186		    sectorsize < 1024 ? 1024 : sectorsize, &error);
187		if (buf == NULL || error != 0)
188			break;
189		if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
190			g_free(buf);
191			break;
192		}
193#if 0
194/*
195 * XXX: Some sources indicate this is a magic sequence, but appearantly
196 * XXX: it is not universal.  Documentation would be wonderfule to have.
197 */
198		if (buf[4] != 'I' || buf[5] != 'P' ||
199		    buf[6] != 'L' || buf[7] != '1') {
200			g_free(buf);
201			break;
202		}
203#endif
204		for (i = 0; i < NDOSPART; i++)
205			g_dec_dos_partition(
206				buf + 512 + i * sizeof(struct dos_partition),
207				dp + i);
208		g_free(buf);
209		for (i = 0; i < NDOSPART; i++) {
210			/* If start and end are identical it's bogus */
211			if (dp[i].dp_ssect == dp[i].dp_esect &&
212			    dp[i].dp_shd == dp[i].dp_ehd &&
213			    dp[i].dp_scyl == dp[i].dp_ecyl)
214				continue;
215			if (dp[i].dp_ecyl == 0)
216				continue;
217                        if (bootverbose) {
218				printf("PC98 Slice %d on %s:\n",
219				       i + 1, gp->name);
220				g_pc98_print(i, dp + i);
221			}
222			npart++;
223			ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
224			g_topology_lock();
225			pp2 = g_slice_addslice(gp, i,
226			    dp[i].dp_scyl * spercyl,
227			    (dp[i].dp_ecyl - dp[i].dp_scyl + 1) * spercyl,
228			    sectorsize,
229			    "%ss%d", gp->name, i + 1);
230			g_topology_unlock();
231		}
232		break;
233	}
234	g_topology_lock();
235	error = g_access_rel(cp, -1, 0, 0);
236	if (npart > 0) {
237		LIST_FOREACH(pp, &gp->provider, provider)
238			g_error_provider(pp, 0);
239		return (gp);
240	}
241	g_std_spoiled(cp);
242	return (NULL);
243}
244
245static struct g_class g_pc98_class = {
246	PC98_CLASS_NAME,
247	g_pc98_taste,
248	NULL,
249	G_CLASS_INITIALIZER
250};
251
252DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
253