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/sysctl.h>
41 #include <sys/kernel.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
46 #include <sys/mutex.h>
50 #include <sys/diskmbr.h>
52 #include <geom/geom.h>
53 #include <geom/geom_slice.h>
55 FEATURE(geom_mbr
, "GEOM DOS/MBR partitioning support");
57 #define MBR_CLASS_NAME "MBR"
58 #define MBREXT_CLASS_NAME "MBREXT"
60 static struct dos_partition historical_bogus_partition_table
[NDOSPART
] = {
61 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
62 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
63 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
64 { 0x80, 0, 1, 0, DOSPTYP_386BSD
, 255, 255, 255, 0, 50000, },
67 static struct dos_partition historical_bogus_partition_table_fixed
[NDOSPART
] = {
68 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
69 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
70 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
71 { 0x80, 0, 1, 0, DOSPTYP_386BSD
, 254, 255, 255, 0, 50000, },
75 g_mbr_print(int i
, struct dos_partition
*dp
)
78 printf("[%d] f:%02x typ:%d", i
, dp
->dp_flag
, dp
->dp_typ
);
79 printf(" s(CHS):%d/%d/%d", DPCYL(dp
->dp_scyl
, dp
->dp_ssect
),
80 dp
->dp_shd
, DPSECT(dp
->dp_ssect
));
81 printf(" e(CHS):%d/%d/%d", DPCYL(dp
->dp_ecyl
, dp
->dp_esect
),
82 dp
->dp_ehd
, DPSECT(dp
->dp_esect
));
83 printf(" s:%d l:%d\n", dp
->dp_start
, dp
->dp_size
);
94 * XXX: Add gctl_req arg and give good error msgs.
95 * XXX: Check that length argument does not bring boot code inside any slice.
98 g_mbr_modify(struct g_geom
*gp
, struct g_mbr_softc
*ms
, u_char
*sec0
, int len __unused
)
102 struct dos_partition ndp
[NDOSPART
], *dp
;
107 if (sec0
[0x1fe] != 0x55 && sec0
[0x1ff] != 0xaa)
111 for (i
= 0; i
< NDOSPART
; i
++) {
113 sec0
+ DOSPARTOFF
+ i
* sizeof(struct dos_partition
),
116 if ((!bcmp(dp
, historical_bogus_partition_table
,
117 sizeof historical_bogus_partition_table
)) ||
118 (!bcmp(dp
, historical_bogus_partition_table_fixed
,
119 sizeof historical_bogus_partition_table_fixed
))) {
121 * We will not allow people to write these from "the inside",
122 * Since properly selfdestructing takes too much code. If
123 * people really want to do this, they cannot have any
124 * providers of this geom open, and in that case they can just
125 * as easily overwrite the MBR in the parent device.
129 for (i
= 0; i
< NDOSPART
; i
++) {
131 * A Protective MBR (PMBR) has a single partition of
132 * type 0xEE spanning the whole disk. Such a MBR
133 * protects a GPT on the disk from MBR tools that
134 * don't know anything about GPT. We're interpreting
135 * it a bit more loosely: any partition of type 0xEE
136 * is to be skipped as it doesn't contain any data
137 * that we should care about. We still allow other
138 * partitions to be present in the MBR. A PMBR will
139 * be handled correctly anyway.
141 if (dp
[i
].dp_typ
== DOSPTYP_PMBR
)
143 else if (dp
[i
].dp_flag
!= 0 && dp
[i
].dp_flag
!= 0x80)
145 else if (dp
[i
].dp_typ
== 0)
148 l
[i
] = (off_t
)dp
[i
].dp_size
* ms
->sectorsize
;
149 error
= g_slice_config(gp
, i
, G_SLICE_CONFIG_CHECK
,
150 (off_t
)dp
[i
].dp_start
* ms
->sectorsize
, l
[i
],
151 ms
->sectorsize
, "%ss%d", gp
->name
, 1 + i
);
155 for (i
= 0; i
< NDOSPART
; i
++) {
156 ms
->type
[i
] = dp
[i
].dp_typ
;
157 g_slice_config(gp
, i
, G_SLICE_CONFIG_SET
,
158 (off_t
)dp
[i
].dp_start
* ms
->sectorsize
, l
[i
],
159 ms
->sectorsize
, "%ss%d", gp
->name
, 1 + i
);
161 bcopy(sec0
, ms
->sec0
, 512);
164 * Calculate MD5 from the first sector and use it for avoiding
165 * recursive slices creation.
168 MD5Update(&md5sum
, ms
->sec0
, sizeof(ms
->sec0
));
169 MD5Final(ms
->slicesum
, &md5sum
);
175 g_mbr_ioctl(struct g_provider
*pp
, u_long cmd
, void *data
, int fflag
, struct thread
*td
)
178 struct g_mbr_softc
*ms
;
179 struct g_slicer
*gsp
;
180 struct g_consumer
*cp
;
191 if (!(fflag
& FWRITE
))
194 cp
= LIST_FIRST(&gp
->consumer
);
196 error
= g_access(cp
, 0, 1, 0);
201 error
= g_mbr_modify(gp
, ms
, data
, 512);
203 error
= g_write_data(cp
, 0, data
, 512);
205 g_access(cp
, 0, -1 , 0);
215 g_mbr_start(struct bio
*bp
)
217 struct g_provider
*pp
;
219 struct g_mbr_softc
*mp
;
220 struct g_slicer
*gsp
;
228 if (bp
->bio_cmd
== BIO_GETATTR
) {
229 if (g_handleattr_int(bp
, "MBR::type", mp
->type
[idx
]))
231 if (g_handleattr_off_t(bp
, "MBR::offset",
232 gsp
->slices
[idx
].offset
))
234 if (g_handleattr(bp
, "MBR::slicesum", mp
->slicesum
,
235 sizeof(mp
->slicesum
)))
243 g_mbr_dumpconf(struct sbuf
*sb
, const char *indent
, struct g_geom
*gp
, struct g_consumer
*cp __unused
, struct g_provider
*pp
)
245 struct g_mbr_softc
*mp
;
246 struct g_slicer
*gsp
;
250 g_slice_dumpconf(sb
, indent
, gp
, cp
, pp
);
253 sbuf_printf(sb
, " ty %d", mp
->type
[pp
->index
]);
255 sbuf_printf(sb
, "%s<type>%d</type>\n", indent
,
256 mp
->type
[pp
->index
]);
260 static struct g_geom
*
261 g_mbr_taste(struct g_class
*mp
, struct g_provider
*pp
, int insist
)
264 struct g_consumer
*cp
;
266 struct g_mbr_softc
*ms
;
267 u_int fwsectors
, sectorsize
;
272 g_trace(G_T_TOPOLOGY
, "mbr_taste(%s,%s)", mp
->name
, pp
->name
);
274 if (!strcmp(pp
->geom
->class->name
, MBR_CLASS_NAME
))
276 gp
= g_slice_new(mp
, NDOSPART
, pp
, &cp
, &ms
, sizeof *ms
, g_mbr_start
);
281 error
= g_getattr("GEOM::fwsectors", cp
, &fwsectors
);
284 sectorsize
= cp
->provider
->sectorsize
;
285 if (sectorsize
< 512)
287 ms
->sectorsize
= sectorsize
;
288 buf
= g_read_data(cp
, 0, sectorsize
, NULL
);
293 * Calculate MD5 from the first sector and use it for avoiding
294 * recursive slices creation.
296 bcopy(buf
, ms
->sec0
, 512);
298 MD5Update(&md5sum
, ms
->sec0
, sizeof(ms
->sec0
));
299 MD5Final(ms
->slicesum
, &md5sum
);
301 error
= g_getattr("MBR::slicesum", cp
, &hash
);
302 if (!error
&& !bcmp(ms
->slicesum
, hash
, sizeof(hash
))) {
308 g_mbr_modify(gp
, ms
, buf
, 512);
314 g_access(cp
, -1, 0, 0);
315 if (LIST_EMPTY(&gp
->provider
)) {
323 g_mbr_config(struct gctl_req
*req
, struct g_class
*mp
, const char *verb
)
326 struct g_consumer
*cp
;
327 struct g_mbr_softc
*ms
;
328 struct g_slicer
*gsp
;
329 int opened
= 0, error
= 0;
334 gp
= gctl_get_geom(req
, mp
, "geom");
337 if (strcmp(verb
, "write MBR")) {
338 gctl_error(req
, "Unknown verb");
343 data
= gctl_get_param(req
, "data", &len
);
346 if (len
< 512 || (len
% 512)) {
347 gctl_error(req
, "Wrong request length");
350 cp
= LIST_FIRST(&gp
->consumer
);
352 error
= g_access(cp
, 0, 1, 0);
357 error
= g_mbr_modify(gp
, ms
, data
, len
);
359 gctl_error(req
, "conflict with open slices");
361 error
= g_write_data(cp
, 0, data
, len
);
363 gctl_error(req
, "sector zero write failed");
365 g_access(cp
, 0, -1 , 0);
369 static struct g_class g_mbr_class
= {
370 .name
= MBR_CLASS_NAME
,
371 .version
= G_VERSION
,
372 .taste
= g_mbr_taste
,
373 .dumpconf
= g_mbr_dumpconf
,
374 .ctlreq
= g_mbr_config
,
375 .ioctl
= g_mbr_ioctl
,
378 DECLARE_GEOM_CLASS(g_mbr_class
, g_mbr
);
380 #define NDOSEXTPART 32
381 struct g_mbrext_softc
{
382 int type
[NDOSEXTPART
];
386 g_mbrext_start(struct bio
*bp
)
388 struct g_provider
*pp
;
390 struct g_mbrext_softc
*mp
;
391 struct g_slicer
*gsp
;
399 if (bp
->bio_cmd
== BIO_GETATTR
) {
400 if (g_handleattr_int(bp
, "MBR::type", mp
->type
[idx
]))
407 g_mbrext_dumpconf(struct sbuf
*sb
, const char *indent
, struct g_geom
*gp
, struct g_consumer
*cp __unused
, struct g_provider
*pp
)
409 struct g_mbrext_softc
*mp
;
410 struct g_slicer
*gsp
;
412 g_slice_dumpconf(sb
, indent
, gp
, cp
, pp
);
417 sbuf_printf(sb
, " ty %d", mp
->type
[pp
->index
]);
419 sbuf_printf(sb
, "%s<type>%d</type>\n", indent
,
420 mp
->type
[pp
->index
]);
424 static struct g_geom
*
425 g_mbrext_taste(struct g_class
*mp
, struct g_provider
*pp
, int insist __unused
)
428 struct g_consumer
*cp
;
430 struct g_mbrext_softc
*ms
;
433 struct dos_partition dp
[4];
434 u_int fwsectors
, sectorsize
;
436 g_trace(G_T_TOPOLOGY
, "g_mbrext_taste(%s,%s)", mp
->name
, pp
->name
);
438 if (strcmp(pp
->geom
->class->name
, MBR_CLASS_NAME
))
440 gp
= g_slice_new(mp
, NDOSEXTPART
, pp
, &cp
, &ms
, sizeof *ms
,
448 error
= g_getattr("MBR::type", cp
, &i
);
449 if (error
|| (i
!= DOSPTYP_EXT
&& i
!= DOSPTYP_EXTLBA
))
451 error
= g_getattr("GEOM::fwsectors", cp
, &fwsectors
);
454 sectorsize
= cp
->provider
->sectorsize
;
455 if (sectorsize
!= 512)
458 buf
= g_read_data(cp
, off
, sectorsize
, NULL
);
461 if (buf
[0x1fe] != 0x55 && buf
[0x1ff] != 0xaa) {
465 for (i
= 0; i
< NDOSPART
; i
++)
468 i
* sizeof(struct dos_partition
), dp
+ i
);
470 if (0 && bootverbose
) {
471 printf("MBREXT Slice %d on %s:\n",
472 slice
+ 5, gp
->name
);
474 g_mbr_print(1, dp
+ 1);
476 if ((dp
[0].dp_flag
& 0x7f) == 0 &&
477 dp
[0].dp_size
!= 0 && dp
[0].dp_typ
!= 0) {
479 g_slice_config(gp
, slice
, G_SLICE_CONFIG_SET
,
480 (((off_t
)dp
[0].dp_start
) << 9ULL) + off
,
481 ((off_t
)dp
[0].dp_size
) << 9ULL,
484 (int)strlen(gp
->name
) - 1,
485 (int)strlen(gp
->name
) - 1,
489 ms
->type
[slice
] = dp
[0].dp_typ
;
492 if (dp
[1].dp_flag
!= 0)
494 if (dp
[1].dp_typ
!= DOSPTYP_EXT
&&
495 dp
[1].dp_typ
!= DOSPTYP_EXTLBA
)
497 if (dp
[1].dp_size
== 0)
499 off
= ((off_t
)dp
[1].dp_start
) << 9ULL;
504 g_access(cp
, -1, 0, 0);
505 if (LIST_EMPTY(&gp
->provider
)) {
513 static struct g_class g_mbrext_class
= {
514 .name
= MBREXT_CLASS_NAME
,
515 .version
= G_VERSION
,
516 .taste
= g_mbrext_taste
,
517 .dumpconf
= g_mbrext_dumpconf
,
520 DECLARE_GEOM_CLASS(g_mbrext_class
, g_mbrext
);