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.
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
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
38 #include <sys/param.h>
43 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
52 #include <sys/mutex.h>
55 #include <sys/errno.h>
56 #include <sys/disklabel.h>
58 #include <geom/geom.h>
59 #include <geom/geom_slice.h>
61 #define MBR_CLASS_NAME "MBR-class"
62 #define MBREXT_CLASS_NAME "MBREXT-class"
65 g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
76 d->dp_start = g_dec_le4(ptr + 8);
77 d->dp_size = g_dec_le4(ptr + 12);
82 g_enc_dos_partition(u_char *ptr, struct dos_partition *d)
93 g_enc_le4(ptr + 8, d->dp_start);
94 g_enc_le4(ptr + 12, d->dp_size);
100 struct dos_partition dospart[NDOSPART];
104 g_mbr_start(struct bio *bp)
106 struct g_provider *pp;
108 struct g_mbr_softc *mp;
109 struct g_slicer *gsp;
117 if (bp->bio_cmd == BIO_GETATTR) {
118 if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
125 g_mbr_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
127 struct g_mbr_softc *mp;
128 struct g_slicer *gsp;
130 g_slice_dumpconf(sb, indent, gp, cp, pp);
134 sbuf_printf(sb, "%s<type>%d</type>\n",
135 indent, mp->type[pp->index]);
140 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
141 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
142 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
144 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
146 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
148 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
149 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
150 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
154 g_mbr_print(int i __unused, struct dos_partition *dp __unused)
158 g_hexdump(dp, sizeof(dp[0]));
159 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
160 printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
161 printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
162 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
166 static struct g_geom *
167 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
170 struct g_consumer *cp;
171 struct g_provider *pp2;
172 int error, i, j, npart;
173 struct dos_partition dp[NDOSPART];
174 struct g_mbr_softc *ms;
177 if (sizeof(struct dos_partition) != 16) {
178 printf("WARNING: struct dos_partition compiles to %d bytes, should be 16.\n",
179 (int)sizeof(struct dos_partition));
182 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
184 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
188 gp->dumpconf = g_mbr_dumpconf;
190 while (1) { /* a trick to allow us to use break */
191 if (gp->rank != 2 && insist == 0)
194 /* For now we only support 512 bytes sectors */
195 error = g_io_getattr("GEOM::sectorsize", cp, &j, &i);
196 if (!error && i != 512)
198 buf = g_read_data(cp, 0, 512, &error);
199 if (buf == NULL || error != 0)
201 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
205 for (i = 0; i < NDOSPART; i++)
207 buf + DOSPARTOFF + i * sizeof(struct dos_partition),
210 if (bcmp(dp, historical_bogus_partition_table,
211 sizeof historical_bogus_partition_table) == 0)
213 if (bcmp(dp, historical_bogus_partition_table_fixed,
214 sizeof historical_bogus_partition_table_fixed) == 0)
217 for (i = 0; i < NDOSPART; i++) {
218 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
220 if (dp[i].dp_size == 0)
222 g_mbr_print(i, dp + i);
224 ms->type[i] = dp[i].dp_typ;
225 pp2 = g_slice_addslice(gp, i,
226 ((off_t)dp[i].dp_start) << 9ULL,
227 ((off_t)dp[i].dp_size) << 9ULL,
228 "%ss%d", gp->name, i + 1);
233 error = g_access_rel(cp, -1, 0, 0);
235 LIST_FOREACH(pp, &gp->provider, provider)
236 g_error_provider(pp, 0);
244 static struct g_class g_mbr_class = {
251 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
253 #define NDOSEXTPART 32
254 struct g_mbrext_softc {
255 int type [NDOSEXTPART];
259 g_mbrext_start(struct bio *bp)
261 struct g_provider *pp;
263 struct g_mbrext_softc *mp;
264 struct g_slicer *gsp;
272 if (bp->bio_cmd == BIO_GETATTR) {
273 if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
280 g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
282 struct g_mbrext_softc *mp;
283 struct g_slicer *gsp;
285 g_slice_dumpconf(sb, indent, gp, cp, pp);
289 sbuf_printf(sb, "%s<type>%d</type>\n",
290 indent, mp->type[pp->index]);
295 g_mbrext_print(int i, struct dos_partition *dp)
297 g_hexdump(dp, sizeof(dp[0]));
298 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
299 printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
300 printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
301 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
304 static struct g_geom *
305 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
308 struct g_consumer *cp;
309 struct g_provider *pp2;
310 int error, i, j, slice;
311 struct g_mbrext_softc *ms;
314 struct dos_partition dp[4];
316 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
318 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
320 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
324 gp->dumpconf = g_mbrext_dumpconf;
327 while (1) { /* a trick to allow us to use break */
329 error = g_io_getattr("MBR::type", cp, &j, &i);
330 if (error || i != DOSPTYP_EXT)
333 buf = g_read_data(cp, off, DEV_BSIZE, &error);
334 if (buf == NULL || error != 0)
336 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
338 for (i = 0; i < NDOSPART; i++)
340 buf + DOSPARTOFF + i * sizeof(struct dos_partition),
343 g_mbrext_print(0, dp);
344 g_mbrext_print(1, dp + 1);
345 if (dp[0].dp_flag == 0 && dp[0].dp_size != 0) {
346 pp2 = g_slice_addslice(gp, slice,
347 (((off_t)dp[0].dp_start) << 9ULL) + off,
348 ((off_t)dp[0].dp_size) << 9ULL,
350 strlen(gp->name) - 1,
351 strlen(gp->name) - 1,
354 ms->type[slice] = dp[0].dp_typ;
356 g_error_provider(pp2, 0);
358 if (dp[1].dp_flag != 0)
360 if (dp[1].dp_typ != DOSPTYP_EXT)
362 if (dp[1].dp_size == 0)
364 off = ((off_t)dp[1].dp_start) << 9ULL;
369 error = g_access_rel(cp, -1, 0, 0);
380 static struct g_class g_mbrext_class = {
387 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);