geom_pc98.c revision 106398
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 106398 2002-11-04 06:30:38Z phk $
33 */
34
35
36#include <sys/param.h>
37#include <sys/stdint.h>
38#ifndef _KERNEL
39#include <stdio.h>
40#include <string.h>
41#include <stdlib.h>
42#include <signal.h>
43#include <err.h>
44#else
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/conf.h>
48#include <sys/bio.h>
49#include <sys/malloc.h>
50#include <sys/lock.h>
51#include <sys/mutex.h>
52#endif
53#include <sys/stdint.h>
54#include <geom/geom.h>
55#include <geom/geom_slice.h>
56#include <machine/endian.h>
57
58#define PC98_CLASS_NAME "PC98"
59
60struct g_pc98_softc {
61	int foo;
62};
63
64static int
65g_pc98_start(struct bio *bp)
66{
67	struct g_geom *gp;
68	struct g_pc98_softc *ms;
69	struct g_slicer *gsp;
70
71	gp = bp->bio_to->geom;
72	gsp = gp->softc;
73	ms = gsp->softc;
74	return (0);
75}
76
77static void
78g_pc98_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
79{
80
81	g_slice_dumpconf(sb, indent, gp, cp, pp);
82}
83
84static struct g_geom *
85g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
86{
87	struct g_geom *gp;
88	struct g_consumer *cp;
89	struct g_provider *pp2;
90	int error, i, npart;
91	u_char *buf, *p;
92	struct g_pc98_softc *ms;
93	u_int sectorsize, u, v;
94	u_int fwsect, fwhead;
95	off_t mediasize, start, length;
96	struct g_slicer *gsp;
97
98	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
99	g_topology_assert();
100	if (flags == G_TF_NORMAL &&
101	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
102		return (NULL);
103	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_pc98_start);
104	if (gp == NULL)
105		return (NULL);
106	gsp = gp->softc;
107	g_topology_unlock();
108	gp->dumpconf = g_pc98_dumpconf;
109	npart = 0;
110	while (1) {	/* a trick to allow us to use break */
111		if (gp->rank != 2 && flags == G_TF_NORMAL)
112			break;
113		sectorsize = cp->provider->sectorsize;
114		if (sectorsize < 512)
115			break;
116		mediasize = cp->provider->mediasize;
117		error = g_getattr("GEOM::fwsectors", cp, &fwsect);
118		if (error || fwsect == 0) {
119			fwsect = 17;
120			printf("g_pc98_taste: error %d guessing %d sectors\n",
121			    error, fwsect);
122		}
123		error = g_getattr("GEOM::fwheads", cp, &fwhead);
124		if (error || fwhead == 0) {
125			fwhead = 8;
126			printf("g_pc98_taste: error %d guessing %d heads\n",
127			    error, fwhead);
128		}
129		gsp->frontstuff = fwsect * sectorsize;
130		buf = g_read_data(cp, 0,
131		    sectorsize < 1024 ? 1024 : sectorsize, &error);
132		if (buf == NULL || error != 0)
133			break;
134
135		if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa)
136			break;
137#if 0
138/*
139 * XXX: Some sources indicate this is a magic sequence, but appearantly
140 * XXX: it is not universal.  Documentation would be wonderfule to have.
141 */
142		if (buf[4] != 'I' || buf[5] != 'P' ||
143		    buf[6] != 'L' || buf[7] != '1')
144			break;
145#endif
146
147		for (i = 0; i < 16; i++) {
148			p = buf + 512 + i * 32;
149			/* If start and end are identical it's bogus */
150			if (!bcmp(p + 8, p + 12, 4))
151				continue;
152			v = g_dec_le2(p + 10);
153			u = g_dec_le2(p + 14);
154			if (u == 0)
155				continue;
156			g_hexdump(p, 32);
157			start = v * fwsect * fwhead * sectorsize;
158			length = (off_t)(1 + u - v) * fwsect *
159			     fwhead * sectorsize;
160			printf("i %d S %d H %d L %d b %d/%d/%d e %d/%d/%d\n",
161				i, fwsect, fwhead, sectorsize,
162				p[8], p[9], v, p[12], p[13], u);
163			npart++;
164			g_topology_lock();
165			pp2 = g_slice_addslice(gp, i,
166			    start, length,
167			    sectorsize,
168			    "%ss%d", pp->name, 1 + i);
169			g_topology_unlock();
170			g_error_provider(pp2, 0);
171		}
172		break;
173	}
174	g_topology_lock();
175	error = g_access_rel(cp, -1, 0, 0);
176	if (npart > 0)
177		return (gp);
178	g_std_spoiled(cp);
179	return (NULL);
180}
181
182static struct g_class g_pc98_class = {
183	PC98_CLASS_NAME,
184	g_pc98_taste,
185	NULL,
186	G_CLASS_INITIALIZER
187};
188
189DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
190