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>
49 #include <sys/diskmbr.h>
51 #include <geom/geom.h>
52 #include <geom/geom_slice.h>
54 #define MBR_CLASS_NAME "MBR"
55 #define MBREXT_CLASS_NAME "MBREXT"
57 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
58 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
59 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
60 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
61 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
64 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
66 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
67 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
68 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
72 g_mbr_print(int i, struct dos_partition *dp)
75 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
76 printf(" s(CHS):%d/%d/%d", DPCYL(dp->dp_scyl, dp->dp_ssect),
77 dp->dp_shd, DPSECT(dp->dp_ssect));
78 printf(" e(CHS):%d/%d/%d", DPCYL(dp->dp_ecyl, dp->dp_esect),
79 dp->dp_ehd, DPSECT(dp->dp_esect));
80 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
91 * XXX: Add gctl_req arg and give good error msgs.
92 * XXX: Check that length argument does not bring boot code inside any slice.
95 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0, int len __unused)
99 struct dos_partition ndp[NDOSPART], *dp;
104 if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
108 for (i = 0; i < NDOSPART; i++) {
110 sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
113 if ((!bcmp(dp, historical_bogus_partition_table,
114 sizeof historical_bogus_partition_table)) ||
115 (!bcmp(dp, historical_bogus_partition_table_fixed,
116 sizeof historical_bogus_partition_table_fixed))) {
118 * We will not allow people to write these from "the inside",
119 * Since properly selfdestructing takes too much code. If
120 * people really want to do this, they cannot have any
121 * providers of this geom open, and in that case they can just
122 * as easily overwrite the MBR in the parent device.
126 for (i = 0; i < NDOSPART; i++) {
128 * A Protective MBR (PMBR) has a single partition of
129 * type 0xEE spanning the whole disk. Such a MBR
130 * protects a GPT on the disk from MBR tools that
131 * don't know anything about GPT. We're interpreting
132 * it a bit more loosely: any partition of type 0xEE
133 * is to be skipped as it doesn't contain any data
134 * that we should care about. We still allow other
135 * partitions to be present in the MBR. A PMBR will
136 * be handled correctly anyway.
138 if (dp[i].dp_typ == DOSPTYP_PMBR)
140 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
142 else if (dp[i].dp_typ == 0)
145 l[i] = (off_t)dp[i].dp_size * ms->sectorsize;
146 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
147 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
148 ms->sectorsize, "%ss%d", gp->name, 1 + i);
152 for (i = 0; i < NDOSPART; i++) {
153 ms->type[i] = dp[i].dp_typ;
154 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
155 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
156 ms->sectorsize, "%ss%d", gp->name, 1 + i);
158 bcopy(sec0, ms->sec0, 512);
161 * Calculate MD5 from the first sector and use it for avoiding
162 * recursive slices creation.
165 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
166 MD5Final(ms->slicesum, &md5sum);
172 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
175 struct g_mbr_softc *ms;
176 struct g_slicer *gsp;
177 struct g_consumer *cp;
188 if (!(fflag & FWRITE))
192 cp = LIST_FIRST(&gp->consumer);
194 error = g_access(cp, 0, 1, 0);
199 error = g_mbr_modify(gp, ms, data, 512);
201 error = g_write_data(cp, 0, data, 512);
203 g_access(cp, 0, -1 , 0);
214 g_mbr_start(struct bio *bp)
216 struct g_provider *pp;
218 struct g_mbr_softc *mp;
219 struct g_slicer *gsp;
227 if (bp->bio_cmd == BIO_GETATTR) {
228 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
230 if (g_handleattr_off_t(bp, "MBR::offset",
231 gsp->slices[idx].offset))
233 if (g_handleattr(bp, "MBR::slicesum", mp->slicesum,
234 sizeof(mp->slicesum)))
242 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
244 struct g_mbr_softc *mp;
245 struct g_slicer *gsp;
249 g_slice_dumpconf(sb, indent, gp, cp, pp);
252 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
254 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
255 mp->type[pp->index]);
259 static struct g_geom *
260 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
263 struct g_consumer *cp;
265 struct g_mbr_softc *ms;
266 u_int fwsectors, sectorsize;
271 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
273 if (!strcmp(pp->geom->class->name, MBR_CLASS_NAME))
275 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
280 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
283 sectorsize = cp->provider->sectorsize;
284 if (sectorsize < 512)
286 ms->sectorsize = sectorsize;
287 buf = g_read_data(cp, 0, sectorsize, NULL);
292 * Calculate MD5 from the first sector and use it for avoiding
293 * recursive slices creation.
295 bcopy(buf, ms->sec0, 512);
297 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
298 MD5Final(ms->slicesum, &md5sum);
300 error = g_getattr("MBR::slicesum", cp, &hash);
301 if (!error && !bcmp(ms->slicesum, hash, sizeof(hash))) {
307 g_mbr_modify(gp, ms, buf, 512);
313 g_access(cp, -1, 0, 0);
314 if (LIST_EMPTY(&gp->provider)) {
322 g_mbr_config(struct gctl_req *req, struct g_class *mp, const char *verb)
325 struct g_consumer *cp;
326 struct g_mbr_softc *ms;
327 struct g_slicer *gsp;
328 int opened = 0, error = 0;
333 gp = gctl_get_geom(req, mp, "geom");
336 if (strcmp(verb, "write MBR")) {
337 gctl_error(req, "Unknown verb");
342 data = gctl_get_param(req, "data", &len);
345 if (len < 512 || (len % 512)) {
346 gctl_error(req, "Wrong request length");
349 cp = LIST_FIRST(&gp->consumer);
351 error = g_access(cp, 0, 1, 0);
356 error = g_mbr_modify(gp, ms, data, len);
358 gctl_error(req, "conflict with open slices");
360 error = g_write_data(cp, 0, data, len);
362 gctl_error(req, "sector zero write failed");
364 g_access(cp, 0, -1 , 0);
368 static struct g_class g_mbr_class = {
369 .name = MBR_CLASS_NAME,
370 .version = G_VERSION,
371 .taste = g_mbr_taste,
372 .dumpconf = g_mbr_dumpconf,
373 .ctlreq = g_mbr_config,
374 .ioctl = g_mbr_ioctl,
377 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
379 #define NDOSEXTPART 32
380 struct g_mbrext_softc {
381 int type [NDOSEXTPART];
385 g_mbrext_start(struct bio *bp)
387 struct g_provider *pp;
389 struct g_mbrext_softc *mp;
390 struct g_slicer *gsp;
398 if (bp->bio_cmd == BIO_GETATTR) {
399 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
406 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
408 struct g_mbrext_softc *mp;
409 struct g_slicer *gsp;
411 g_slice_dumpconf(sb, indent, gp, cp, pp);
416 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
418 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
419 mp->type[pp->index]);
423 static struct g_geom *
424 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
427 struct g_consumer *cp;
429 struct g_mbrext_softc *ms;
432 struct dos_partition dp[4];
433 u_int fwsectors, sectorsize;
435 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
437 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
439 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
447 error = g_getattr("MBR::type", cp, &i);
448 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
450 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
453 sectorsize = cp->provider->sectorsize;
454 if (sectorsize != 512)
457 buf = g_read_data(cp, off, sectorsize, NULL);
460 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
464 for (i = 0; i < NDOSPART; i++)
467 i * sizeof(struct dos_partition), dp + i);
469 if (0 && bootverbose) {
470 printf("MBREXT Slice %d on %s:\n",
471 slice + 5, gp->name);
473 g_mbr_print(1, dp + 1);
475 if ((dp[0].dp_flag & 0x7f) == 0 &&
476 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
478 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
479 (((off_t)dp[0].dp_start) << 9ULL) + off,
480 ((off_t)dp[0].dp_size) << 9ULL,
483 strlen(gp->name) - 1,
484 strlen(gp->name) - 1,
488 ms->type[slice] = dp[0].dp_typ;
491 if (dp[1].dp_flag != 0)
493 if (dp[1].dp_typ != DOSPTYP_EXT &&
494 dp[1].dp_typ != DOSPTYP_EXTLBA)
496 if (dp[1].dp_size == 0)
498 off = ((off_t)dp[1].dp_start) << 9ULL;
503 g_access(cp, -1, 0, 0);
504 if (LIST_EMPTY(&gp->provider)) {
512 static struct g_class g_mbrext_class = {
513 .name = MBREXT_CLASS_NAME,
514 .version = G_VERSION,
515 .taste = g_mbrext_taste,
516 .dumpconf = g_mbrext_dumpconf,
519 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);