2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/diskpc98.h>
52 #include <geom/geom.h>
53 #include <geom/geom_slice.h>
55 #define PC98_CLASS_NAME "PC98"
58 g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
66 d->dp_ipl_sct = ptr[4];
67 d->dp_ipl_head = ptr[5];
68 d->dp_ipl_cyl = g_dec_le2(ptr + 6);
71 d->dp_scyl = g_dec_le2(ptr + 10);
72 d->dp_esect = ptr[12];
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];
81 struct dos_partition dp[NDOSPART];
85 g_pc98_start(struct bio *bp)
87 struct g_provider *pp;
89 struct g_pc98_softc *mp;
98 if (bp->bio_cmd == BIO_GETATTR) {
99 if (g_handleattr_int(bp, "PC98::type", mp->type[index]))
101 if (g_handleattr_off_t(bp, "PC98::offset",
102 gsp->slices[index].offset))
109 g_pc98_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp,
110 struct g_consumer *cp __unused, struct g_provider *pp)
112 struct g_pc98_softc *mp;
113 struct g_slicer *gsp;
118 g_slice_dumpconf(sb, indent, gp, cp, pp);
120 strncpy(sname, mp->dp[pp->index].dp_name, 16);
122 if (indent == NULL) {
123 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
124 sbuf_printf(sb, " sn %s", sname);
126 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
127 mp->type[pp->index]);
128 sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
135 g_pc98_print(int i, struct dos_partition *dp)
139 strncpy(sname, dp->dp_name, 16);
142 g_hexdump(dp, sizeof(dp[0]));
143 printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
144 i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
145 printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
146 printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
147 printf(" sname:%s\n", sname);
150 static struct g_geom *
151 g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
154 struct g_consumer *cp;
155 struct g_provider *pp2;
157 struct g_pc98_softc *ms;
158 struct g_slicer *gsp;
159 u_int fwsectors, fwheads, sectorsize;
163 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
165 if (flags == G_TF_NORMAL &&
166 !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
168 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
173 gp->dumpconf = g_pc98_dumpconf;
175 while (1) { /* a trick to allow us to use break */
176 if (gp->rank != 2 && flags == G_TF_NORMAL)
178 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
179 if (error || fwsectors == 0) {
181 printf("g_pc98_taste: error %d guessing %d sectors\n",
184 error = g_getattr("GEOM::fwheads", cp, &fwheads);
185 if (error || fwheads == 0) {
187 printf("g_pc98_taste: error %d guessing %d heads\n",
190 sectorsize = cp->provider->sectorsize;
191 if (sectorsize < 512)
193 gsp->frontstuff = sectorsize * fwsectors;
194 spercyl = (off_t)fwsectors * fwheads * sectorsize;
195 buf = g_read_data(cp, 0,
196 sectorsize < 1024 ? 1024 : sectorsize, &error);
197 if (buf == NULL || error != 0)
199 if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
205 * XXX: Some sources indicate this is a magic sequence, but appearantly
206 * XXX: it is not universal. Documentation would be wonderfule to have.
208 if (buf[4] != 'I' || buf[5] != 'P' ||
209 buf[6] != 'L' || buf[7] != '1') {
214 for (i = 0; i < NDOSPART; i++)
216 buf + 512 + i * sizeof(struct dos_partition),
219 for (i = 0; i < NDOSPART; i++) {
220 /* If start and end are identical it's bogus */
221 if (ms->dp[i].dp_ssect == ms->dp[i].dp_esect &&
222 ms->dp[i].dp_shd == ms->dp[i].dp_ehd &&
223 ms->dp[i].dp_scyl == ms->dp[i].dp_ecyl)
225 if (ms->dp[i].dp_ecyl == 0)
228 printf("PC98 Slice %d on %s:\n",
230 g_pc98_print(i, ms->dp + i);
233 ms->type[i] = (ms->dp[i].dp_sid << 8) |
236 pp2 = g_slice_addslice(gp, i,
237 ms->dp[i].dp_scyl * spercyl,
238 (ms->dp[i].dp_ecyl - ms->dp[i].dp_scyl + 1) *
241 "%ss%d", gp->name, i + 1);
247 error = g_access_rel(cp, -1, 0, 0);
249 LIST_FOREACH(pp, &gp->provider, provider)
250 g_error_provider(pp, 0);
257 static struct g_class g_pc98_class = {
264 DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);