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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/endian.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
45 #include <sys/mutex.h>
48 #include <sys/diskmbr.h>
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
53 #define MBR_CLASS_NAME "MBR"
54 #define MBREXT_CLASS_NAME "MBREXT"
56 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
57 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
58 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
59 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
60 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
63 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
64 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
66 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
67 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
71 g_mbr_print(int i, struct dos_partition *dp)
74 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
75 printf(" s(CHS):%d/%d/%d", DPCYL(dp->dp_scyl, dp->dp_ssect),
76 dp->dp_shd, DPSECT(dp->dp_ssect));
77 printf(" e(CHS):%d/%d/%d", DPCYL(dp->dp_ecyl, dp->dp_esect),
78 dp->dp_ehd, DPSECT(dp->dp_esect));
79 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
90 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0)
94 struct dos_partition ndp[NDOSPART], *dp;
99 if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
103 for (i = 0; i < NDOSPART; i++) {
105 sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
107 if (0 && bootverbose)
108 g_mbr_print(i, dp + i);
110 if ((!bcmp(dp, historical_bogus_partition_table,
111 sizeof historical_bogus_partition_table)) ||
112 (!bcmp(dp, historical_bogus_partition_table_fixed,
113 sizeof historical_bogus_partition_table_fixed))) {
115 * We will not allow people to write these from "the inside",
116 * Since properly selfdestructing takes too much code. If
117 * people really want to do this, they cannot have any
118 * providers of this geom open, and in that case they can just
119 * as easily overwrite the MBR in the parent device.
123 for (i = 0; i < NDOSPART; i++) {
125 * A Protective MBR (PMBR) has a single partition of
126 * type 0xEE spanning the whole disk. Such a MBR
127 * protects a GPT on the disk from MBR tools that
128 * don't know anything about GPT. We're interpreting
129 * it a bit more loosely: any partition of type 0xEE
130 * is to be skipped as it doesn't contain any data
131 * that we should care about. We still allow other
132 * partitions to be present in the MBR. A PMBR will
133 * be handled correctly anyway.
135 if (dp[i].dp_typ == DOSPTYP_PMBR)
137 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
139 else if (dp[i].dp_typ == 0)
142 l[i] = (off_t)dp[i].dp_size * ms->sectorsize;
143 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
144 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
145 ms->sectorsize, "%ss%d", gp->name, 1 + i);
149 for (i = 0; i < NDOSPART; i++) {
150 ms->type[i] = dp[i].dp_typ;
151 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
152 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
153 ms->sectorsize, "%ss%d", gp->name, 1 + i);
155 bcopy(sec0, ms->sec0, 512);
158 * Calculate MD5 from the first sector and use it for avoiding
159 * recursive slices creation.
162 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
163 MD5Final(ms->slicesum, &md5sum);
169 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
172 struct g_mbr_softc *ms;
173 struct g_slicer *gsp;
174 struct g_consumer *cp;
185 if (!(fflag & FWRITE))
189 cp = LIST_FIRST(&gp->consumer);
191 error = g_access(cp, 0, 1, 0);
196 error = g_mbr_modify(gp, ms, data);
198 error = g_write_data(cp, 0, data, 512);
200 g_access(cp, 0, -1 , 0);
211 g_mbr_start(struct bio *bp)
213 struct g_provider *pp;
215 struct g_mbr_softc *mp;
216 struct g_slicer *gsp;
224 if (bp->bio_cmd == BIO_GETATTR) {
225 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
227 if (g_handleattr_off_t(bp, "MBR::offset",
228 gsp->slices[idx].offset))
230 if (g_handleattr(bp, "MBR::slicesum", mp->slicesum,
231 sizeof(mp->slicesum)))
239 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
241 struct g_mbr_softc *mp;
242 struct g_slicer *gsp;
246 g_slice_dumpconf(sb, indent, gp, cp, pp);
249 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
251 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
252 mp->type[pp->index]);
256 static struct g_geom *
257 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
260 struct g_consumer *cp;
262 struct g_mbr_softc *ms;
263 u_int fwsectors, sectorsize;
268 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
270 if (!strcmp(pp->geom->class->name, MBR_CLASS_NAME))
272 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
277 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
280 sectorsize = cp->provider->sectorsize;
281 if (sectorsize < 512)
283 ms->sectorsize = sectorsize;
284 buf = g_read_data(cp, 0, sectorsize, &error);
285 if (buf == NULL || error != 0)
289 * Calculate MD5 from the first sector and use it for avoiding
290 * recursive slices creation.
292 bcopy(buf, ms->sec0, 512);
294 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
295 MD5Final(ms->slicesum, &md5sum);
297 error = g_getattr("MBR::slicesum", cp, &hash);
298 if (!error && !bcmp(ms->slicesum, hash, sizeof(hash))) {
304 g_mbr_modify(gp, ms, buf);
310 g_access(cp, -1, 0, 0);
311 if (LIST_EMPTY(&gp->provider)) {
318 static struct g_class g_mbr_class = {
319 .name = MBR_CLASS_NAME,
320 .version = G_VERSION,
321 .taste = g_mbr_taste,
322 .dumpconf = g_mbr_dumpconf,
323 .ioctl = g_mbr_ioctl,
326 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
328 #define NDOSEXTPART 32
329 struct g_mbrext_softc {
330 int type [NDOSEXTPART];
334 g_mbrext_start(struct bio *bp)
336 struct g_provider *pp;
338 struct g_mbrext_softc *mp;
339 struct g_slicer *gsp;
347 if (bp->bio_cmd == BIO_GETATTR) {
348 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
355 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
357 struct g_mbrext_softc *mp;
358 struct g_slicer *gsp;
360 g_slice_dumpconf(sb, indent, gp, cp, pp);
365 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
367 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
368 mp->type[pp->index]);
372 static struct g_geom *
373 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
376 struct g_consumer *cp;
378 struct g_mbrext_softc *ms;
381 struct dos_partition dp[4];
382 u_int fwsectors, sectorsize;
384 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
386 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
388 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
396 error = g_getattr("MBR::type", cp, &i);
397 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
399 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
402 sectorsize = cp->provider->sectorsize;
403 if (sectorsize != 512)
406 buf = g_read_data(cp, off, sectorsize, &error);
407 if (buf == NULL || error != 0)
409 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
413 for (i = 0; i < NDOSPART; i++)
416 i * sizeof(struct dos_partition), dp + i);
418 if (0 && bootverbose) {
419 printf("MBREXT Slice %d on %s:\n",
420 slice + 5, gp->name);
422 g_mbr_print(1, dp + 1);
424 if ((dp[0].dp_flag & 0x7f) == 0 &&
425 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
427 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
428 (((off_t)dp[0].dp_start) << 9ULL) + off,
429 ((off_t)dp[0].dp_size) << 9ULL,
432 strlen(gp->name) - 1,
433 strlen(gp->name) - 1,
437 ms->type[slice] = dp[0].dp_typ;
440 if (dp[1].dp_flag != 0)
442 if (dp[1].dp_typ != DOSPTYP_EXT &&
443 dp[1].dp_typ != DOSPTYP_EXTLBA)
445 if (dp[1].dp_size == 0)
447 off = ((off_t)dp[1].dp_start) << 9ULL;
452 g_access(cp, -1, 0, 0);
453 if (LIST_EMPTY(&gp->provider)) {
461 static struct g_class g_mbrext_class = {
462 .name = MBREXT_CLASS_NAME,
463 .version = G_VERSION,
464 .taste = g_mbrext_taste,
465 .dumpconf = g_mbrext_dumpconf,
468 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);