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 107562 2002-12-03 20:20:44Z sos $
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 dp[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 char sname[17];
115
116 gsp = gp->softc;
117 mp = gsp->softc;
118 g_slice_dumpconf(sb, indent, gp, cp, pp);
119 if (pp != NULL) {
120 strncpy(sname, mp->dp[pp->index].dp_name, 16);
121 sname[16] = '\0';
122 if (indent == NULL) {
123 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
124 sbuf_printf(sb, " sn %s", sname);
125 } else {
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,
129 sname);
130 }
131 }
132}
133
134static void
135g_pc98_print(int i, struct dos_partition *dp)
136{
137 char sname[17];
138
139 strncpy(sname, dp->dp_name, 16);
140 sname[16] = '\0';
141
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);
148}
149
150static struct g_geom *
151g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
152{
153 struct g_geom *gp;
154 struct g_consumer *cp;
155 struct g_provider *pp2;
156 int error, i, npart;
157 struct g_pc98_softc *ms;
158 struct g_slicer *gsp;
159 u_int fwsectors, fwheads, sectorsize;
160 u_char *buf;
161 off_t spercyl;
162
163 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
164 g_topology_assert();
165 if (flags == G_TF_NORMAL &&
166 !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
167 return (NULL);
168 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
169 if (gp == NULL)
170 return (NULL);
171 gsp = gp->softc;
172 g_topology_unlock();
173 gp->dumpconf = g_pc98_dumpconf;
174 npart = 0;
175 while (1) { /* a trick to allow us to use break */
176 if (gp->rank != 2 && flags == G_TF_NORMAL)
177 break;
178 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
179 if (error || fwsectors == 0) {
180 fwsectors = 17;
181 printf("g_pc98_taste: error %d guessing %d sectors\n",
182 error, fwsectors);
183 }
184 error = g_getattr("GEOM::fwheads", cp, &fwheads);
185 if (error || fwheads == 0) {
186 fwheads = 8;
187 printf("g_pc98_taste: error %d guessing %d heads\n",
188 error, fwheads);
189 }
190 sectorsize = cp->provider->sectorsize;
191 if (sectorsize < 512)
192 break;
193 if (cp->provider->mediasize / sectorsize < 17 * 8 * 65536) {
194 fwsectors = 17;
195 fwheads = 8;
196 }
197 gsp->frontstuff = sectorsize * fwsectors;
198 spercyl = (off_t)fwsectors * fwheads * sectorsize;
199 buf = g_read_data(cp, 0,
200 sectorsize < 1024 ? 1024 : sectorsize, &error);
201 if (buf == NULL || error != 0)
202 break;
203 if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
204 g_free(buf);
205 break;
206 }
207#if 0
208/*
209 * XXX: Some sources indicate this is a magic sequence, but appearantly
210 * XXX: it is not universal. Documentation would be wonderfule to have.
211 */
212 if (buf[4] != 'I' || buf[5] != 'P' ||
213 buf[6] != 'L' || buf[7] != '1') {
214 g_free(buf);
215 break;
216 }
217#endif
218 for (i = 0; i < NDOSPART; i++)
219 g_dec_dos_partition(
220 buf + 512 + i * sizeof(struct dos_partition),
221 ms->dp + i);
222 g_free(buf);
223 for (i = 0; i < NDOSPART; i++) {
224 /* If start and end are identical it's bogus */
225 if (ms->dp[i].dp_ssect == ms->dp[i].dp_esect &&
226 ms->dp[i].dp_shd == ms->dp[i].dp_ehd &&
227 ms->dp[i].dp_scyl == ms->dp[i].dp_ecyl)
228 continue;
229 if (ms->dp[i].dp_ecyl == 0)
230 continue;
231 if (bootverbose) {
232 printf("PC98 Slice %d on %s:\n",
233 i + 1, gp->name);
234 g_pc98_print(i, ms->dp + i);
235 }
236 npart++;
237 ms->type[i] = (ms->dp[i].dp_sid << 8) |
238 ms->dp[i].dp_mid;
239 g_topology_lock();
240 pp2 = g_slice_addslice(gp, i,
241 ms->dp[i].dp_scyl * spercyl,
242 (ms->dp[i].dp_ecyl - ms->dp[i].dp_scyl + 1) *
243 spercyl,
244 sectorsize,
245 "%ss%d", gp->name, i + 1);
246 g_topology_unlock();
247 }
248 break;
249 }
250 g_topology_lock();
251 error = g_access_rel(cp, -1, 0, 0);
252 if (npart > 0) {
253 LIST_FOREACH(pp, &gp->provider, provider)
254 g_error_provider(pp, 0);
255 return (gp);
256 }
257 g_std_spoiled(cp);
258 return (NULL);
259}
260
261static struct g_class g_pc98_class = {
262 PC98_CLASS_NAME,
263 g_pc98_taste,
264 NULL,
265 G_CLASS_INITIALIZER
266};
267
268DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
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 107562 2002-12-03 20:20:44Z sos $
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 dp[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 char sname[17];
115
116 gsp = gp->softc;
117 mp = gsp->softc;
118 g_slice_dumpconf(sb, indent, gp, cp, pp);
119 if (pp != NULL) {
120 strncpy(sname, mp->dp[pp->index].dp_name, 16);
121 sname[16] = '\0';
122 if (indent == NULL) {
123 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
124 sbuf_printf(sb, " sn %s", sname);
125 } else {
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,
129 sname);
130 }
131 }
132}
133
134static void
135g_pc98_print(int i, struct dos_partition *dp)
136{
137 char sname[17];
138
139 strncpy(sname, dp->dp_name, 16);
140 sname[16] = '\0';
141
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);
148}
149
150static struct g_geom *
151g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
152{
153 struct g_geom *gp;
154 struct g_consumer *cp;
155 struct g_provider *pp2;
156 int error, i, npart;
157 struct g_pc98_softc *ms;
158 struct g_slicer *gsp;
159 u_int fwsectors, fwheads, sectorsize;
160 u_char *buf;
161 off_t spercyl;
162
163 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
164 g_topology_assert();
165 if (flags == G_TF_NORMAL &&
166 !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
167 return (NULL);
168 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
169 if (gp == NULL)
170 return (NULL);
171 gsp = gp->softc;
172 g_topology_unlock();
173 gp->dumpconf = g_pc98_dumpconf;
174 npart = 0;
175 while (1) { /* a trick to allow us to use break */
176 if (gp->rank != 2 && flags == G_TF_NORMAL)
177 break;
178 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
179 if (error || fwsectors == 0) {
180 fwsectors = 17;
181 printf("g_pc98_taste: error %d guessing %d sectors\n",
182 error, fwsectors);
183 }
184 error = g_getattr("GEOM::fwheads", cp, &fwheads);
185 if (error || fwheads == 0) {
186 fwheads = 8;
187 printf("g_pc98_taste: error %d guessing %d heads\n",
188 error, fwheads);
189 }
190 sectorsize = cp->provider->sectorsize;
191 if (sectorsize < 512)
192 break;
193 if (cp->provider->mediasize / sectorsize < 17 * 8 * 65536) {
194 fwsectors = 17;
195 fwheads = 8;
196 }
197 gsp->frontstuff = sectorsize * fwsectors;
198 spercyl = (off_t)fwsectors * fwheads * sectorsize;
199 buf = g_read_data(cp, 0,
200 sectorsize < 1024 ? 1024 : sectorsize, &error);
201 if (buf == NULL || error != 0)
202 break;
203 if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
204 g_free(buf);
205 break;
206 }
207#if 0
208/*
209 * XXX: Some sources indicate this is a magic sequence, but appearantly
210 * XXX: it is not universal. Documentation would be wonderfule to have.
211 */
212 if (buf[4] != 'I' || buf[5] != 'P' ||
213 buf[6] != 'L' || buf[7] != '1') {
214 g_free(buf);
215 break;
216 }
217#endif
218 for (i = 0; i < NDOSPART; i++)
219 g_dec_dos_partition(
220 buf + 512 + i * sizeof(struct dos_partition),
221 ms->dp + i);
222 g_free(buf);
223 for (i = 0; i < NDOSPART; i++) {
224 /* If start and end are identical it's bogus */
225 if (ms->dp[i].dp_ssect == ms->dp[i].dp_esect &&
226 ms->dp[i].dp_shd == ms->dp[i].dp_ehd &&
227 ms->dp[i].dp_scyl == ms->dp[i].dp_ecyl)
228 continue;
229 if (ms->dp[i].dp_ecyl == 0)
230 continue;
231 if (bootverbose) {
232 printf("PC98 Slice %d on %s:\n",
233 i + 1, gp->name);
234 g_pc98_print(i, ms->dp + i);
235 }
236 npart++;
237 ms->type[i] = (ms->dp[i].dp_sid << 8) |
238 ms->dp[i].dp_mid;
239 g_topology_lock();
240 pp2 = g_slice_addslice(gp, i,
241 ms->dp[i].dp_scyl * spercyl,
242 (ms->dp[i].dp_ecyl - ms->dp[i].dp_scyl + 1) *
243 spercyl,
244 sectorsize,
245 "%ss%d", gp->name, i + 1);
246 g_topology_unlock();
247 }
248 break;
249 }
250 g_topology_lock();
251 error = g_access_rel(cp, -1, 0, 0);
252 if (npart > 0) {
253 LIST_FOREACH(pp, &gp->provider, provider)
254 g_error_provider(pp, 0);
255 return (gp);
256 }
257 g_std_spoiled(cp);
258 return (NULL);
259}
260
261static struct g_class g_pc98_class = {
262 PC98_CLASS_NAME,
263 g_pc98_taste,
264 NULL,
265 G_CLASS_INITIALIZER
266};
267
268DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);