FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_mbr.c
1 /*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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 *
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
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/11.2/sys/geom/geom_mbr.c 332640 2018-04-17 02:18:04Z kevans $");
35
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>
44 #include <sys/bio.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/md5.h>
48 #include <sys/proc.h>
49
50 #include <sys/diskmbr.h>
51 #include <sys/sbuf.h>
52 #include <geom/geom.h>
53 #include <geom/geom_slice.h>
54
55 FEATURE(geom_mbr, "GEOM DOS/MBR partitioning support");
56
57 #define MBR_CLASS_NAME "MBR"
58 #define MBREXT_CLASS_NAME "MBREXT"
59
60 static int g_mbr_once = 0;
61
62 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
63 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
64 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
66 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
67 };
68
69 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
70 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
71 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
72 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
73 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
74 };
75
76 static void
77 g_mbr_print(int i, struct dos_partition *dp)
78 {
79
80 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
81 printf(" s(CHS):%d/%d/%d", DPCYL(dp->dp_scyl, dp->dp_ssect),
82 dp->dp_shd, DPSECT(dp->dp_ssect));
83 printf(" e(CHS):%d/%d/%d", DPCYL(dp->dp_ecyl, dp->dp_esect),
84 dp->dp_ehd, DPSECT(dp->dp_esect));
85 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
86 }
87
88 struct g_mbr_softc {
89 int type [NDOSPART];
90 u_int sectorsize;
91 u_char sec0[512];
92 u_char slicesum[16];
93 };
94
95 /*
96 * XXX: Add gctl_req arg and give good error msgs.
97 * XXX: Check that length argument does not bring boot code inside any slice.
98 */
99 static int
100 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0, int len __unused)
101 {
102 int i, error;
103 off_t l[NDOSPART];
104 struct dos_partition ndp[NDOSPART], *dp;
105 MD5_CTX md5sum;
106
107 g_topology_assert();
108
109 if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
110 return (EBUSY);
111
112 dp = ndp;
113 for (i = 0; i < NDOSPART; i++) {
114 dos_partition_dec(
115 sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
116 dp + i);
117 }
118 if ((!bcmp(dp, historical_bogus_partition_table,
119 sizeof historical_bogus_partition_table)) ||
120 (!bcmp(dp, historical_bogus_partition_table_fixed,
121 sizeof historical_bogus_partition_table_fixed))) {
122 /*
123 * We will not allow people to write these from "the inside",
124 * Since properly selfdestructing takes too much code. If
125 * people really want to do this, they cannot have any
126 * providers of this geom open, and in that case they can just
127 * as easily overwrite the MBR in the parent device.
128 */
129 return(EBUSY);
130 }
131 for (i = 0; i < NDOSPART; i++) {
132 /*
133 * A Protective MBR (PMBR) has a single partition of
134 * type 0xEE spanning the whole disk. Such a MBR
135 * protects a GPT on the disk from MBR tools that
136 * don't know anything about GPT. We're interpreting
137 * it a bit more loosely: any partition of type 0xEE
138 * is to be skipped as it doesn't contain any data
139 * that we should care about. We still allow other
140 * partitions to be present in the MBR. A PMBR will
141 * be handled correctly anyway.
142 */
143 if (dp[i].dp_typ == DOSPTYP_PMBR)
144 l[i] = 0;
145 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
146 l[i] = 0;
147 else if (dp[i].dp_typ == 0)
148 l[i] = 0;
149 else
150 l[i] = (off_t)dp[i].dp_size * ms->sectorsize;
151 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
152 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
153 ms->sectorsize, "%ss%d", gp->name, 1 + i);
154 if (error)
155 return (error);
156 }
157 for (i = 0; i < NDOSPART; i++) {
158 ms->type[i] = dp[i].dp_typ;
159 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
160 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
161 ms->sectorsize, "%ss%d", gp->name, 1 + i);
162 }
163 bcopy(sec0, ms->sec0, 512);
164
165 /*
166 * Calculate MD5 from the first sector and use it for avoiding
167 * recursive slices creation.
168 */
169 MD5Init(&md5sum);
170 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
171 MD5Final(ms->slicesum, &md5sum);
172
173 return (0);
174 }
175
176 static int
177 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
178 {
179 struct g_geom *gp;
180 struct g_mbr_softc *ms;
181 struct g_slicer *gsp;
182 struct g_consumer *cp;
183 int error, opened;
184
185 gp = pp->geom;
186 gsp = gp->softc;
187 ms = gsp->softc;
188
189 opened = 0;
190 error = 0;
191 switch(cmd) {
192 case DIOCSMBR: {
193 if (!(fflag & FWRITE))
194 return (EPERM);
195 g_topology_lock();
196 cp = LIST_FIRST(&gp->consumer);
197 if (cp->acw == 0) {
198 error = g_access(cp, 0, 1, 0);
199 if (error == 0)
200 opened = 1;
201 }
202 if (!error)
203 error = g_mbr_modify(gp, ms, data, 512);
204 if (!error)
205 error = g_write_data(cp, 0, data, 512);
206 if (opened)
207 g_access(cp, 0, -1 , 0);
208 g_topology_unlock();
209 return(error);
210 }
211 default:
212 return (ENOIOCTL);
213 }
214 }
215
216 static int
217 g_mbr_start(struct bio *bp)
218 {
219 struct g_provider *pp;
220 struct g_geom *gp;
221 struct g_mbr_softc *mp;
222 struct g_slicer *gsp;
223 int idx;
224
225 pp = bp->bio_to;
226 idx = pp->index;
227 gp = pp->geom;
228 gsp = gp->softc;
229 mp = gsp->softc;
230 if (bp->bio_cmd == BIO_GETATTR) {
231 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
232 return (1);
233 if (g_handleattr_off_t(bp, "MBR::offset",
234 gsp->slices[idx].offset))
235 return (1);
236 if (g_handleattr(bp, "MBR::slicesum", mp->slicesum,
237 sizeof(mp->slicesum)))
238 return (1);
239 }
240
241 return (0);
242 }
243
244 static void
245 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
246 {
247 struct g_mbr_softc *mp;
248 struct g_slicer *gsp;
249
250 gsp = gp->softc;
251 mp = gsp->softc;
252 g_slice_dumpconf(sb, indent, gp, cp, pp);
253 if (pp != NULL) {
254 if (indent == NULL)
255 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
256 else
257 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
258 mp->type[pp->index]);
259 }
260 }
261
262 static struct g_geom *
263 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
264 {
265 struct g_geom *gp;
266 struct g_consumer *cp;
267 int error;
268 struct g_mbr_softc *ms;
269 u_int fwsectors, sectorsize;
270 u_char *buf;
271 u_char hash[16];
272 MD5_CTX md5sum;
273
274 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
275 g_topology_assert();
276 if (!strcmp(pp->geom->class->name, MBR_CLASS_NAME))
277 return (NULL);
278 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
279 if (gp == NULL)
280 return (NULL);
281 g_topology_unlock();
282 do {
283 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
284 if (error)
285 fwsectors = 17;
286 sectorsize = cp->provider->sectorsize;
287 if (sectorsize < 512)
288 break;
289 ms->sectorsize = sectorsize;
290 buf = g_read_data(cp, 0, sectorsize, NULL);
291 if (buf == NULL)
292 break;
293
294 /*
295 * Calculate MD5 from the first sector and use it for avoiding
296 * recursive slices creation.
297 */
298 bcopy(buf, ms->sec0, 512);
299 MD5Init(&md5sum);
300 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
301 MD5Final(ms->slicesum, &md5sum);
302
303 error = g_getattr("MBR::slicesum", cp, &hash);
304 if (!error && !bcmp(ms->slicesum, hash, sizeof(hash))) {
305 g_free(buf);
306 break;
307 }
308
309 g_topology_lock();
310 g_mbr_modify(gp, ms, buf, 512);
311 g_topology_unlock();
312 g_free(buf);
313 break;
314 } while (0);
315 g_topology_lock();
316 g_access(cp, -1, 0, 0);
317 if (LIST_EMPTY(&gp->provider)) {
318 g_slice_spoiled(cp);
319 return (NULL);
320 }
321 if (!g_mbr_once) {
322 g_mbr_once = 1;
323 printf(
324 "WARNING: geom_mbr (geom %s) is deprecated, "
325 "use gpart instead.\n", gp->name);
326 }
327 return (gp);
328 }
329
330 static void
331 g_mbr_config(struct gctl_req *req, struct g_class *mp, const char *verb)
332 {
333 struct g_geom *gp;
334 struct g_consumer *cp;
335 struct g_mbr_softc *ms;
336 struct g_slicer *gsp;
337 int opened = 0, error = 0;
338 void *data;
339 int len;
340
341 g_topology_assert();
342 gp = gctl_get_geom(req, mp, "geom");
343 if (gp == NULL)
344 return;
345 if (strcmp(verb, "write MBR")) {
346 gctl_error(req, "Unknown verb");
347 return;
348 }
349 gsp = gp->softc;
350 ms = gsp->softc;
351 data = gctl_get_param(req, "data", &len);
352 if (data == NULL)
353 return;
354 if (len < 512 || (len % 512)) {
355 gctl_error(req, "Wrong request length");
356 return;
357 }
358 cp = LIST_FIRST(&gp->consumer);
359 if (cp->acw == 0) {
360 error = g_access(cp, 0, 1, 0);
361 if (error == 0)
362 opened = 1;
363 }
364 if (!error)
365 error = g_mbr_modify(gp, ms, data, len);
366 if (error)
367 gctl_error(req, "conflict with open slices");
368 if (!error)
369 error = g_write_data(cp, 0, data, len);
370 if (error)
371 gctl_error(req, "sector zero write failed");
372 if (opened)
373 g_access(cp, 0, -1 , 0);
374 return;
375 }
376
377 static struct g_class g_mbr_class = {
378 .name = MBR_CLASS_NAME,
379 .version = G_VERSION,
380 .taste = g_mbr_taste,
381 .dumpconf = g_mbr_dumpconf,
382 .ctlreq = g_mbr_config,
383 .ioctl = g_mbr_ioctl,
384 };
385
386 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
387
388 #define NDOSEXTPART 32
389 struct g_mbrext_softc {
390 int type [NDOSEXTPART];
391 };
392
393 static int
394 g_mbrext_start(struct bio *bp)
395 {
396 struct g_provider *pp;
397 struct g_geom *gp;
398 struct g_mbrext_softc *mp;
399 struct g_slicer *gsp;
400 int idx;
401
402 pp = bp->bio_to;
403 idx = pp->index;
404 gp = pp->geom;
405 gsp = gp->softc;
406 mp = gsp->softc;
407 if (bp->bio_cmd == BIO_GETATTR) {
408 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
409 return (1);
410 }
411 return (0);
412 }
413
414 static void
415 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
416 {
417 struct g_mbrext_softc *mp;
418 struct g_slicer *gsp;
419
420 g_slice_dumpconf(sb, indent, gp, cp, pp);
421 gsp = gp->softc;
422 mp = gsp->softc;
423 if (pp != NULL) {
424 if (indent == NULL)
425 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
426 else
427 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
428 mp->type[pp->index]);
429 }
430 }
431
432 static struct g_geom *
433 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
434 {
435 struct g_geom *gp;
436 struct g_consumer *cp;
437 int error, i, slice;
438 struct g_mbrext_softc *ms;
439 off_t off;
440 u_char *buf;
441 struct dos_partition dp[4];
442 u_int fwsectors, sectorsize;
443
444 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
445 g_topology_assert();
446 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
447 return (NULL);
448 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
449 g_mbrext_start);
450 if (gp == NULL)
451 return (NULL);
452 g_topology_unlock();
453 off = 0;
454 slice = 0;
455 do {
456 error = g_getattr("MBR::type", cp, &i);
457 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
458 break;
459 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
460 if (error)
461 fwsectors = 17;
462 sectorsize = cp->provider->sectorsize;
463 if (sectorsize != 512)
464 break;
465 for (;;) {
466 buf = g_read_data(cp, off, sectorsize, NULL);
467 if (buf == NULL)
468 break;
469 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
470 g_free(buf);
471 break;
472 }
473 for (i = 0; i < NDOSPART; i++)
474 dos_partition_dec(
475 buf + DOSPARTOFF +
476 i * sizeof(struct dos_partition), dp + i);
477 g_free(buf);
478 if (0 && bootverbose) {
479 printf("MBREXT Slice %d on %s:\n",
480 slice + 5, gp->name);
481 g_mbr_print(0, dp);
482 g_mbr_print(1, dp + 1);
483 }
484 if ((dp[0].dp_flag & 0x7f) == 0 &&
485 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
486 g_topology_lock();
487 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
488 (((off_t)dp[0].dp_start) << 9ULL) + off,
489 ((off_t)dp[0].dp_size) << 9ULL,
490 sectorsize,
491 "%*.*s%d",
492 (int)strlen(gp->name) - 1,
493 (int)strlen(gp->name) - 1,
494 gp->name,
495 slice + 5);
496 g_topology_unlock();
497 ms->type[slice] = dp[0].dp_typ;
498 slice++;
499 }
500 if (dp[1].dp_flag != 0)
501 break;
502 if (dp[1].dp_typ != DOSPTYP_EXT &&
503 dp[1].dp_typ != DOSPTYP_EXTLBA)
504 break;
505 if (dp[1].dp_size == 0)
506 break;
507 off = ((off_t)dp[1].dp_start) << 9ULL;
508 }
509 break;
510 } while (0);
511 g_topology_lock();
512 g_access(cp, -1, 0, 0);
513 if (LIST_EMPTY(&gp->provider)) {
514 g_slice_spoiled(cp);
515 return (NULL);
516 }
517 return (gp);
518 }
519
520
521 static struct g_class g_mbrext_class = {
522 .name = MBREXT_CLASS_NAME,
523 .version = G_VERSION,
524 .taste = g_mbrext_taste,
525 .dumpconf = g_mbrext_dumpconf,
526 };
527
528 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
529 MODULE_VERSION(geom_mbr, 0);
Cache object: b8eaeb750510e4d4ba643bb5b831dd2c
|