geom_pc98.c revision 106001
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 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_pc98.c 106001 2002-10-26 20:17:59Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/stdint.h>
41#ifndef _KERNEL
42#include <stdio.h>
43#include <string.h>
44#include <stdlib.h>
45#include <signal.h>
46#include <err.h>
47#else
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/conf.h>
51#include <sys/bio.h>
52#include <sys/malloc.h>
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#endif
56#include <sys/stdint.h>
57#include <geom/geom.h>
58#include <geom/geom_slice.h>
59#include <machine/endian.h>
60
61#define PC98_CLASS_NAME "PC98"
62
63struct g_pc98_softc {
64	int foo;
65};
66
67static int
68g_pc98_start(struct bio *bp)
69{
70	struct g_geom *gp;
71	struct g_pc98_softc *ms;
72	struct g_slicer *gsp;
73
74	gp = bp->bio_to->geom;
75	gsp = gp->softc;
76	ms = gsp->softc;
77	return (0);
78}
79
80static void
81g_pc98_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
82{
83
84	g_slice_dumpconf(sb, indent, gp, cp, pp);
85}
86
87static struct g_geom *
88g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
89{
90	struct g_geom *gp;
91	struct g_consumer *cp;
92	struct g_provider *pp2;
93	int error, i, npart;
94	u_char *buf;
95	struct g_pc98_softc *ms;
96	u_int sectorsize, u, v;
97	u_int fwsect, fwhead;
98	off_t mediasize, start, length;
99	struct g_slicer *gsp;
100
101	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
102	g_topology_assert();
103	if (flags == G_TF_NORMAL &&
104	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
105		return (NULL);
106	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_pc98_start);
107	if (gp == NULL)
108		return (NULL);
109	gsp = gp->softc;
110	g_topology_unlock();
111	gp->dumpconf = g_pc98_dumpconf;
112	npart = 0;
113	while (1) {	/* a trick to allow us to use break */
114		if (gp->rank != 2 && flags == G_TF_NORMAL)
115			break;
116		sectorsize = cp->provider->sectorsize;
117		if (sectorsize < 512)
118			break;
119		mediasize = cp->provider->mediasize;
120		error = g_getattr("GEOM::fwsectors", cp, &fwsect);
121		if (error || fwsect == 0) {
122			fwsect = 17;
123			printf("g_pc98_taste: error %d guessing %d sectors\n",
124			    error, fwsect);
125		}
126		error = g_getattr("GEOM::fwheads", cp, &fwhead);
127		if (error || fwhead == 0) {
128			fwhead = 8;
129			printf("g_pc98_taste: error %d guessing %d heads\n",
130			    error, fwhead);
131		}
132		gsp->frontstuff = fwsect * sectorsize;
133		buf = g_read_data(cp, 0,
134		    sectorsize < 1024 ? 1024 : sectorsize, &error);
135		if (buf == NULL || error != 0)
136			break;
137
138		if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa)
139			break;
140#if 0
141/*
142 * XXX: Some sources indicate this is a magic sequence, but appearantly
143 * XXX: it is not universal.  Documentation would be wonderfule to have.
144 */
145		if (buf[4] != 'I' || buf[5] != 'P' ||
146		    buf[6] != 'L' || buf[7] != '1')
147			break;
148#endif
149
150		for (i = 0; i < 16; i++) {
151			v = g_dec_le2(buf + 512 + 10 + i * 32);
152			u = g_dec_le2(buf + 512 + 14 + i * 32);
153			if (u == 0)
154				continue;
155			g_hexdump(buf+512 + i * 32, 32);
156			start = v * fwsect * fwhead * sectorsize;
157			length = (1 + u - v) * fwsect * fwhead * sectorsize;
158			npart++;
159			g_topology_lock();
160			pp2 = g_slice_addslice(gp, i,
161			    start, length,
162			    sectorsize,
163			    "%ss%d", pp->name, 1 + i);
164			g_topology_unlock();
165			g_error_provider(pp2, 0);
166		}
167		break;
168	}
169	g_topology_lock();
170	error = g_access_rel(cp, -1, 0, 0);
171	if (npart > 0)
172		return (gp);
173	g_std_spoiled(cp);
174	return (NULL);
175}
176
177static struct g_class g_pc98_class = {
178	PC98_CLASS_NAME,
179	g_pc98_taste,
180	NULL,
181	G_CLASS_INITIALIZER
182};
183
184DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
185