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/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/diskmbr.h>
48 #include <geom/geom.h>
49 #include <geom/geom_slice.h>
51 #define MBR_CLASS_NAME "MBR"
52 #define MBREXT_CLASS_NAME "MBREXT"
54 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
55 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
56 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
57 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
58 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
61 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
62 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
63 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
64 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
65 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
69 g_mbr_print(int i, struct dos_partition *dp)
72 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
73 printf(" s(CHS):%d/%d/%d", DPCYL(dp->dp_scyl, dp->dp_ssect),
74 dp->dp_shd, DPSECT(dp->dp_ssect));
75 printf(" e(CHS):%d/%d/%d", DPCYL(dp->dp_ecyl, dp->dp_esect),
76 dp->dp_ehd, DPSECT(dp->dp_esect));
77 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
87 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0)
91 struct dos_partition ndp[NDOSPART], *dp;
95 if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
99 for (i = 0; i < NDOSPART; i++) {
101 sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
104 g_mbr_print(i, dp + i);
106 if ((!bcmp(dp, historical_bogus_partition_table,
107 sizeof historical_bogus_partition_table)) ||
108 (!bcmp(dp, historical_bogus_partition_table_fixed,
109 sizeof historical_bogus_partition_table_fixed))) {
111 * We will not allow people to write these from "the inside",
112 * Since properly selfdestructing takes too much code. If
113 * people really want to do this, they cannot have any
114 * providers of this geom open, and in that case they can just
115 * as easily overwrite the MBR in the parent device.
119 for (i = 0; i < NDOSPART; i++) {
121 * A Protective MBR (PMBR) has a single partition of
122 * type 0xEE spanning the whole disk. Such a MBR
123 * protects a GPT on the disk from MBR tools that
124 * don't know anything about GPT. We're interpreting
125 * it a bit more loosely: any partition of type 0xEE
126 * is to be skipped as it doesn't contain any data
127 * that we should care about. We still allow other
128 * partitions to be present in the MBR. A PMBR will
129 * be handled correctly anyway.
131 if (dp[i].dp_typ == DOSPTYP_PMBR)
133 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
135 else if (dp[i].dp_typ == 0)
138 l[i] = (off_t)dp[i].dp_size * ms->sectorsize;
139 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
140 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
141 ms->sectorsize, "%ss%d", gp->name, 1 + i);
145 for (i = 0; i < NDOSPART; i++) {
146 ms->type[i] = dp[i].dp_typ;
147 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
148 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
149 ms->sectorsize, "%ss%d", gp->name, 1 + i);
151 bcopy(sec0, ms->sec0, 512);
156 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td)
159 struct g_mbr_softc *ms;
160 struct g_slicer *gsp;
161 struct g_consumer *cp;
172 /* Validate and modify our slicer instance to match. */
173 error = g_mbr_modify(gp, ms, data);
174 cp = LIST_FIRST(&gp->consumer);
175 error = g_write_data(cp, 0, data, 512);
186 g_mbr_start(struct bio *bp)
188 struct g_provider *pp;
190 struct g_mbr_softc *mp;
191 struct g_slicer *gsp;
199 if (bp->bio_cmd == BIO_GETATTR) {
200 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
202 if (g_handleattr_off_t(bp, "MBR::offset",
203 gsp->slices[idx].offset))
211 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
213 struct g_mbr_softc *mp;
214 struct g_slicer *gsp;
218 g_slice_dumpconf(sb, indent, gp, cp, pp);
221 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
223 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
224 mp->type[pp->index]);
228 static struct g_geom *
229 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
232 struct g_consumer *cp;
234 struct g_mbr_softc *ms;
235 u_int fwsectors, sectorsize;
238 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
240 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
244 gp->dumpconf = g_mbr_dumpconf;
245 gp->ioctl = g_mbr_ioctl;
247 if (gp->rank != 2 && insist == 0)
249 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
252 sectorsize = cp->provider->sectorsize;
253 if (sectorsize < 512)
255 ms->sectorsize = sectorsize;
256 buf = g_read_data(cp, 0, sectorsize, &error);
257 if (buf == NULL || error != 0)
260 g_mbr_modify(gp, ms, buf);
266 g_access(cp, -1, 0, 0);
267 if (LIST_EMPTY(&gp->provider)) {
274 static struct g_class g_mbr_class = {
275 .name = MBR_CLASS_NAME,
276 .taste = g_mbr_taste,
279 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
281 #define NDOSEXTPART 32
282 struct g_mbrext_softc {
283 int type [NDOSEXTPART];
287 g_mbrext_start(struct bio *bp)
289 struct g_provider *pp;
291 struct g_mbrext_softc *mp;
292 struct g_slicer *gsp;
300 if (bp->bio_cmd == BIO_GETATTR) {
301 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
308 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
310 struct g_mbrext_softc *mp;
311 struct g_slicer *gsp;
313 g_slice_dumpconf(sb, indent, gp, cp, pp);
318 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
320 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
321 mp->type[pp->index]);
325 static struct g_geom *
326 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
329 struct g_consumer *cp;
331 struct g_mbrext_softc *ms;
334 struct dos_partition dp[4];
335 u_int fwsectors, sectorsize;
337 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
339 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
341 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
346 gp->dumpconf = g_mbrext_dumpconf;
350 error = g_getattr("MBR::type", cp, &i);
351 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
353 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
356 sectorsize = cp->provider->sectorsize;
357 if (sectorsize != 512)
360 buf = g_read_data(cp, off, sectorsize, &error);
361 if (buf == NULL || error != 0)
363 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
367 for (i = 0; i < NDOSPART; i++)
370 i * sizeof(struct dos_partition), dp + i);
373 printf("MBREXT Slice %d on %s:\n",
374 slice + 5, gp->name);
376 g_mbr_print(1, dp + 1);
378 if ((dp[0].dp_flag & 0x7f) == 0 &&
379 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
381 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
382 (((off_t)dp[0].dp_start) << 9ULL) + off,
383 ((off_t)dp[0].dp_size) << 9ULL,
386 strlen(gp->name) - 1,
387 strlen(gp->name) - 1,
391 ms->type[slice] = dp[0].dp_typ;
394 if (dp[1].dp_flag != 0)
396 if (dp[1].dp_typ != DOSPTYP_EXT &&
397 dp[1].dp_typ != DOSPTYP_EXTLBA)
399 if (dp[1].dp_size == 0)
401 off = ((off_t)dp[1].dp_start) << 9ULL;
406 g_access(cp, -1, 0, 0);
407 if (LIST_EMPTY(&gp->provider)) {
415 static struct g_class g_mbrext_class = {
416 .name = MBREXT_CLASS_NAME,
417 .taste = g_mbrext_taste,
420 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);