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$");
35
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>
43 #include <sys/bio.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/md5.h>
47 #include <sys/proc.h>
48
49 #include <sys/diskmbr.h>
50 #include <sys/sbuf.h>
51 #include <geom/geom.h>
52 #include <geom/geom_slice.h>
53
54 #define MBR_CLASS_NAME "MBR"
55 #define MBREXT_CLASS_NAME "MBREXT"
56
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, },
62 };
63
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, },
69 };
70
71 static void
72 g_mbr_print(int i, struct dos_partition *dp)
73 {
74
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);
81 }
82
83 struct g_mbr_softc {
84 int type [NDOSPART];
85 u_int sectorsize;
86 u_char sec0[512];
87 u_char slicesum[16];
88 };
89
90 /*
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.
93 */
94 static int
95 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0, int len __unused)
96 {
97 int i, error;
98 off_t l[NDOSPART];
99 struct dos_partition ndp[NDOSPART], *dp;
100 MD5_CTX md5sum;
101
102 g_topology_assert();
103
104 if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
105 return (EBUSY);
106
107 dp = ndp;
108 for (i = 0; i < NDOSPART; i++) {
109 dos_partition_dec(
110 sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
111 dp + i);
112 }
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))) {
117 /*
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.
123 */
124 return(EBUSY);
125 }
126 for (i = 0; i < NDOSPART; i++) {
127 /*
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.
137 */
138 if (dp[i].dp_typ == DOSPTYP_PMBR)
139 l[i] = 0;
140 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
141 l[i] = 0;
142 else if (dp[i].dp_typ == 0)
143 l[i] = 0;
144 else
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);
149 if (error)
150 return (error);
151 }
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);
157 }
158 bcopy(sec0, ms->sec0, 512);
159
160 /*
161 * Calculate MD5 from the first sector and use it for avoiding
162 * recursive slices creation.
163 */
164 MD5Init(&md5sum);
165 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
166 MD5Final(ms->slicesum, &md5sum);
167
168 return (0);
169 }
170
171 static int
172 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
173 {
174 struct g_geom *gp;
175 struct g_mbr_softc *ms;
176 struct g_slicer *gsp;
177 struct g_consumer *cp;
178 int error, opened;
179
180 gp = pp->geom;
181 gsp = gp->softc;
182 ms = gsp->softc;
183
184 opened = 0;
185 error = 0;
186 switch(cmd) {
187 case DIOCSMBR: {
188 if (!(fflag & FWRITE))
189 return (EPERM);
190 DROP_GIANT();
191 g_topology_lock();
192 cp = LIST_FIRST(&gp->consumer);
193 if (cp->acw == 0) {
194 error = g_access(cp, 0, 1, 0);
195 if (error == 0)
196 opened = 1;
197 }
198 if (!error)
199 error = g_mbr_modify(gp, ms, data, 512);
200 if (!error)
201 error = g_write_data(cp, 0, data, 512);
202 if (opened)
203 g_access(cp, 0, -1 , 0);
204 g_topology_unlock();
205 PICKUP_GIANT();
206 return(error);
207 }
208 default:
209 return (ENOIOCTL);
210 }
211 }
212
213 static int
214 g_mbr_start(struct bio *bp)
215 {
216 struct g_provider *pp;
217 struct g_geom *gp;
218 struct g_mbr_softc *mp;
219 struct g_slicer *gsp;
220 int idx;
221
222 pp = bp->bio_to;
223 idx = pp->index;
224 gp = pp->geom;
225 gsp = gp->softc;
226 mp = gsp->softc;
227 if (bp->bio_cmd == BIO_GETATTR) {
228 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
229 return (1);
230 if (g_handleattr_off_t(bp, "MBR::offset",
231 gsp->slices[idx].offset))
232 return (1);
233 if (g_handleattr(bp, "MBR::slicesum", mp->slicesum,
234 sizeof(mp->slicesum)))
235 return (1);
236 }
237
238 return (0);
239 }
240
241 static void
242 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
243 {
244 struct g_mbr_softc *mp;
245 struct g_slicer *gsp;
246
247 gsp = gp->softc;
248 mp = gsp->softc;
249 g_slice_dumpconf(sb, indent, gp, cp, pp);
250 if (pp != NULL) {
251 if (indent == NULL)
252 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
253 else
254 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
255 mp->type[pp->index]);
256 }
257 }
258
259 static struct g_geom *
260 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
261 {
262 struct g_geom *gp;
263 struct g_consumer *cp;
264 int error;
265 struct g_mbr_softc *ms;
266 u_int fwsectors, sectorsize;
267 u_char *buf;
268 u_char hash[16];
269 MD5_CTX md5sum;
270
271 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
272 g_topology_assert();
273 if (!strcmp(pp->geom->class->name, MBR_CLASS_NAME))
274 return (NULL);
275 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
276 if (gp == NULL)
277 return (NULL);
278 g_topology_unlock();
279 do {
280 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
281 if (error)
282 fwsectors = 17;
283 sectorsize = cp->provider->sectorsize;
284 if (sectorsize < 512)
285 break;
286 ms->sectorsize = sectorsize;
287 buf = g_read_data(cp, 0, sectorsize, NULL);
288 if (buf == NULL)
289 break;
290
291 /*
292 * Calculate MD5 from the first sector and use it for avoiding
293 * recursive slices creation.
294 */
295 bcopy(buf, ms->sec0, 512);
296 MD5Init(&md5sum);
297 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
298 MD5Final(ms->slicesum, &md5sum);
299
300 error = g_getattr("MBR::slicesum", cp, &hash);
301 if (!error && !bcmp(ms->slicesum, hash, sizeof(hash))) {
302 g_free(buf);
303 break;
304 }
305
306 g_topology_lock();
307 g_mbr_modify(gp, ms, buf, 512);
308 g_topology_unlock();
309 g_free(buf);
310 break;
311 } while (0);
312 g_topology_lock();
313 g_access(cp, -1, 0, 0);
314 if (LIST_EMPTY(&gp->provider)) {
315 g_slice_spoiled(cp);
316 return (NULL);
317 }
318 return (gp);
319 }
320
321 static void
322 g_mbr_config(struct gctl_req *req, struct g_class *mp, const char *verb)
323 {
324 struct g_geom *gp;
325 struct g_consumer *cp;
326 struct g_mbr_softc *ms;
327 struct g_slicer *gsp;
328 int opened = 0, error = 0;
329 void *data;
330 int len;
331
332 g_topology_assert();
333 gp = gctl_get_geom(req, mp, "geom");
334 if (gp == NULL)
335 return;
336 if (strcmp(verb, "write MBR")) {
337 gctl_error(req, "Unknown verb");
338 return;
339 }
340 gsp = gp->softc;
341 ms = gsp->softc;
342 data = gctl_get_param(req, "data", &len);
343 if (data == NULL)
344 return;
345 if (len < 512 || (len % 512)) {
346 gctl_error(req, "Wrong request length");
347 return;
348 }
349 cp = LIST_FIRST(&gp->consumer);
350 if (cp->acw == 0) {
351 error = g_access(cp, 0, 1, 0);
352 if (error == 0)
353 opened = 1;
354 }
355 if (!error)
356 error = g_mbr_modify(gp, ms, data, len);
357 if (error)
358 gctl_error(req, "conflict with open slices");
359 if (!error)
360 error = g_write_data(cp, 0, data, len);
361 if (error)
362 gctl_error(req, "sector zero write failed");
363 if (opened)
364 g_access(cp, 0, -1 , 0);
365 return;
366 }
367
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,
375 };
376
377 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
378
379 #define NDOSEXTPART 32
380 struct g_mbrext_softc {
381 int type [NDOSEXTPART];
382 };
383
384 static int
385 g_mbrext_start(struct bio *bp)
386 {
387 struct g_provider *pp;
388 struct g_geom *gp;
389 struct g_mbrext_softc *mp;
390 struct g_slicer *gsp;
391 int idx;
392
393 pp = bp->bio_to;
394 idx = pp->index;
395 gp = pp->geom;
396 gsp = gp->softc;
397 mp = gsp->softc;
398 if (bp->bio_cmd == BIO_GETATTR) {
399 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
400 return (1);
401 }
402 return (0);
403 }
404
405 static void
406 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
407 {
408 struct g_mbrext_softc *mp;
409 struct g_slicer *gsp;
410
411 g_slice_dumpconf(sb, indent, gp, cp, pp);
412 gsp = gp->softc;
413 mp = gsp->softc;
414 if (pp != NULL) {
415 if (indent == NULL)
416 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
417 else
418 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
419 mp->type[pp->index]);
420 }
421 }
422
423 static struct g_geom *
424 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
425 {
426 struct g_geom *gp;
427 struct g_consumer *cp;
428 int error, i, slice;
429 struct g_mbrext_softc *ms;
430 off_t off;
431 u_char *buf;
432 struct dos_partition dp[4];
433 u_int fwsectors, sectorsize;
434
435 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
436 g_topology_assert();
437 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
438 return (NULL);
439 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
440 g_mbrext_start);
441 if (gp == NULL)
442 return (NULL);
443 g_topology_unlock();
444 off = 0;
445 slice = 0;
446 do {
447 error = g_getattr("MBR::type", cp, &i);
448 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
449 break;
450 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
451 if (error)
452 fwsectors = 17;
453 sectorsize = cp->provider->sectorsize;
454 if (sectorsize != 512)
455 break;
456 for (;;) {
457 buf = g_read_data(cp, off, sectorsize, NULL);
458 if (buf == NULL)
459 break;
460 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
461 g_free(buf);
462 break;
463 }
464 for (i = 0; i < NDOSPART; i++)
465 dos_partition_dec(
466 buf + DOSPARTOFF +
467 i * sizeof(struct dos_partition), dp + i);
468 g_free(buf);
469 if (0 && bootverbose) {
470 printf("MBREXT Slice %d on %s:\n",
471 slice + 5, gp->name);
472 g_mbr_print(0, dp);
473 g_mbr_print(1, dp + 1);
474 }
475 if ((dp[0].dp_flag & 0x7f) == 0 &&
476 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
477 g_topology_lock();
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,
481 sectorsize,
482 "%*.*s%d",
483 strlen(gp->name) - 1,
484 strlen(gp->name) - 1,
485 gp->name,
486 slice + 5);
487 g_topology_unlock();
488 ms->type[slice] = dp[0].dp_typ;
489 slice++;
490 }
491 if (dp[1].dp_flag != 0)
492 break;
493 if (dp[1].dp_typ != DOSPTYP_EXT &&
494 dp[1].dp_typ != DOSPTYP_EXTLBA)
495 break;
496 if (dp[1].dp_size == 0)
497 break;
498 off = ((off_t)dp[1].dp_start) << 9ULL;
499 }
500 break;
501 } while (0);
502 g_topology_lock();
503 g_access(cp, -1, 0, 0);
504 if (LIST_EMPTY(&gp->provider)) {
505 g_slice_spoiled(cp);
506 return (NULL);
507 }
508 return (gp);
509 }
510
511
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,
517 };
518
519 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
Cache object: 07c12862ae38e7b330cc9b8402d2eb6f
|