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/5.4/sys/geom/geom_mbr.c 145335 2005-04-20 19:11:07Z cvs2svn $");
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
48 #include <sys/diskmbr.h>
49 #include <sys/sbuf.h>
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
52
53 #define MBR_CLASS_NAME "MBR"
54 #define MBREXT_CLASS_NAME "MBREXT"
55
56 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
57 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
58 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
59 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
60 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
61 };
62
63 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
64 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
66 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
67 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
68 };
69
70 static void
71 g_mbr_print(int i, struct dos_partition *dp)
72 {
73
74 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
75 printf(" s(CHS):%d/%d/%d", DPCYL(dp->dp_scyl, dp->dp_ssect),
76 dp->dp_shd, DPSECT(dp->dp_ssect));
77 printf(" e(CHS):%d/%d/%d", DPCYL(dp->dp_ecyl, dp->dp_esect),
78 dp->dp_ehd, DPSECT(dp->dp_esect));
79 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
80 }
81
82 struct g_mbr_softc {
83 int type [NDOSPART];
84 u_int sectorsize;
85 u_char sec0[512];
86 u_char slicesum[16];
87 };
88
89 static int
90 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0)
91 {
92 int i, error;
93 off_t l[NDOSPART];
94 struct dos_partition ndp[NDOSPART], *dp;
95 MD5_CTX md5sum;
96
97 g_topology_assert();
98
99 if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
100 return (EBUSY);
101
102 dp = ndp;
103 for (i = 0; i < NDOSPART; i++) {
104 dos_partition_dec(
105 sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
106 dp + i);
107 if (bootverbose)
108 g_mbr_print(i, dp + i);
109 }
110 if ((!bcmp(dp, historical_bogus_partition_table,
111 sizeof historical_bogus_partition_table)) ||
112 (!bcmp(dp, historical_bogus_partition_table_fixed,
113 sizeof historical_bogus_partition_table_fixed))) {
114 /*
115 * We will not allow people to write these from "the inside",
116 * Since properly selfdestructing takes too much code. If
117 * people really want to do this, they cannot have any
118 * providers of this geom open, and in that case they can just
119 * as easily overwrite the MBR in the parent device.
120 */
121 return(EBUSY);
122 }
123 for (i = 0; i < NDOSPART; i++) {
124 /*
125 * A Protective MBR (PMBR) has a single partition of
126 * type 0xEE spanning the whole disk. Such a MBR
127 * protects a GPT on the disk from MBR tools that
128 * don't know anything about GPT. We're interpreting
129 * it a bit more loosely: any partition of type 0xEE
130 * is to be skipped as it doesn't contain any data
131 * that we should care about. We still allow other
132 * partitions to be present in the MBR. A PMBR will
133 * be handled correctly anyway.
134 */
135 if (dp[i].dp_typ == DOSPTYP_PMBR)
136 l[i] = 0;
137 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
138 l[i] = 0;
139 else if (dp[i].dp_typ == 0)
140 l[i] = 0;
141 else
142 l[i] = (off_t)dp[i].dp_size * ms->sectorsize;
143 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
144 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
145 ms->sectorsize, "%ss%d", gp->name, 1 + i);
146 if (error)
147 return (error);
148 }
149 for (i = 0; i < NDOSPART; i++) {
150 ms->type[i] = dp[i].dp_typ;
151 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
152 (off_t)dp[i].dp_start * ms->sectorsize, l[i],
153 ms->sectorsize, "%ss%d", gp->name, 1 + i);
154 }
155 bcopy(sec0, ms->sec0, 512);
156
157 /*
158 * Calculate MD5 from the first sector and use it for avoiding
159 * recursive slices creation.
160 */
161 MD5Init(&md5sum);
162 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
163 MD5Final(ms->slicesum, &md5sum);
164
165 return (0);
166 }
167
168 static int
169 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
170 {
171 struct g_geom *gp;
172 struct g_mbr_softc *ms;
173 struct g_slicer *gsp;
174 struct g_consumer *cp;
175 int error;
176
177 gp = pp->geom;
178 gsp = gp->softc;
179 ms = gsp->softc;
180
181 switch(cmd) {
182 case DIOCSMBR: {
183 if (!(fflag & FWRITE))
184 return (EPERM);
185 DROP_GIANT();
186 g_topology_lock();
187 /* Validate and modify our slicer instance to match. */
188 error = g_mbr_modify(gp, ms, data);
189 cp = LIST_FIRST(&gp->consumer);
190 error = g_write_data(cp, 0, data, 512);
191 g_topology_unlock();
192 PICKUP_GIANT();
193 return(error);
194 }
195 default:
196 return (ENOIOCTL);
197 }
198 }
199
200 static int
201 g_mbr_start(struct bio *bp)
202 {
203 struct g_provider *pp;
204 struct g_geom *gp;
205 struct g_mbr_softc *mp;
206 struct g_slicer *gsp;
207 int idx;
208
209 pp = bp->bio_to;
210 idx = pp->index;
211 gp = pp->geom;
212 gsp = gp->softc;
213 mp = gsp->softc;
214 if (bp->bio_cmd == BIO_GETATTR) {
215 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
216 return (1);
217 if (g_handleattr_off_t(bp, "MBR::offset",
218 gsp->slices[idx].offset))
219 return (1);
220 if (g_handleattr(bp, "MBR::slicesum", mp->slicesum,
221 sizeof(mp->slicesum)))
222 return (1);
223 }
224
225 return (0);
226 }
227
228 static void
229 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
230 {
231 struct g_mbr_softc *mp;
232 struct g_slicer *gsp;
233
234 gsp = gp->softc;
235 mp = gsp->softc;
236 g_slice_dumpconf(sb, indent, gp, cp, pp);
237 if (pp != NULL) {
238 if (indent == NULL)
239 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
240 else
241 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
242 mp->type[pp->index]);
243 }
244 }
245
246 static struct g_geom *
247 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
248 {
249 struct g_geom *gp;
250 struct g_consumer *cp;
251 int error;
252 struct g_mbr_softc *ms;
253 u_int fwsectors, sectorsize;
254 u_char *buf;
255 u_char hash[16];
256 MD5_CTX md5sum;
257
258 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
259 g_topology_assert();
260 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
261 if (gp == NULL)
262 return (NULL);
263 g_topology_unlock();
264 do {
265 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
266 if (error)
267 fwsectors = 17;
268 sectorsize = cp->provider->sectorsize;
269 if (sectorsize < 512)
270 break;
271 ms->sectorsize = sectorsize;
272 buf = g_read_data(cp, 0, sectorsize, &error);
273 if (buf == NULL || error != 0)
274 break;
275
276 /*
277 * Calculate MD5 from the first sector and use it for avoiding
278 * recursive slices creation.
279 */
280 bcopy(buf, ms->sec0, 512);
281 MD5Init(&md5sum);
282 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
283 MD5Final(ms->slicesum, &md5sum);
284
285 error = g_getattr("MBR::slicesum", cp, &hash);
286 if (!error && !bcmp(ms->slicesum, hash, sizeof(hash))) {
287 g_free(buf);
288 break;
289 }
290
291 g_topology_lock();
292 g_mbr_modify(gp, ms, buf);
293 g_topology_unlock();
294 g_free(buf);
295 break;
296 } while (0);
297 g_topology_lock();
298 g_access(cp, -1, 0, 0);
299 if (LIST_EMPTY(&gp->provider)) {
300 g_slice_spoiled(cp);
301 return (NULL);
302 }
303 return (gp);
304 }
305
306 static struct g_class g_mbr_class = {
307 .name = MBR_CLASS_NAME,
308 .version = G_VERSION,
309 .taste = g_mbr_taste,
310 .dumpconf = g_mbr_dumpconf,
311 .ioctl = g_mbr_ioctl,
312 };
313
314 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
315
316 #define NDOSEXTPART 32
317 struct g_mbrext_softc {
318 int type [NDOSEXTPART];
319 };
320
321 static int
322 g_mbrext_start(struct bio *bp)
323 {
324 struct g_provider *pp;
325 struct g_geom *gp;
326 struct g_mbrext_softc *mp;
327 struct g_slicer *gsp;
328 int idx;
329
330 pp = bp->bio_to;
331 idx = pp->index;
332 gp = pp->geom;
333 gsp = gp->softc;
334 mp = gsp->softc;
335 if (bp->bio_cmd == BIO_GETATTR) {
336 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
337 return (1);
338 }
339 return (0);
340 }
341
342 static void
343 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
344 {
345 struct g_mbrext_softc *mp;
346 struct g_slicer *gsp;
347
348 g_slice_dumpconf(sb, indent, gp, cp, pp);
349 gsp = gp->softc;
350 mp = gsp->softc;
351 if (pp != NULL) {
352 if (indent == NULL)
353 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
354 else
355 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
356 mp->type[pp->index]);
357 }
358 }
359
360 static struct g_geom *
361 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
362 {
363 struct g_geom *gp;
364 struct g_consumer *cp;
365 int error, i, slice;
366 struct g_mbrext_softc *ms;
367 off_t off;
368 u_char *buf;
369 struct dos_partition dp[4];
370 u_int fwsectors, sectorsize;
371
372 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
373 g_topology_assert();
374 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
375 return (NULL);
376 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
377 g_mbrext_start);
378 if (gp == NULL)
379 return (NULL);
380 g_topology_unlock();
381 off = 0;
382 slice = 0;
383 do {
384 error = g_getattr("MBR::type", cp, &i);
385 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
386 break;
387 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
388 if (error)
389 fwsectors = 17;
390 sectorsize = cp->provider->sectorsize;
391 if (sectorsize != 512)
392 break;
393 for (;;) {
394 buf = g_read_data(cp, off, sectorsize, &error);
395 if (buf == NULL || error != 0)
396 break;
397 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
398 g_free(buf);
399 break;
400 }
401 for (i = 0; i < NDOSPART; i++)
402 dos_partition_dec(
403 buf + DOSPARTOFF +
404 i * sizeof(struct dos_partition), dp + i);
405 g_free(buf);
406 if (bootverbose) {
407 printf("MBREXT Slice %d on %s:\n",
408 slice + 5, gp->name);
409 g_mbr_print(0, dp);
410 g_mbr_print(1, dp + 1);
411 }
412 if ((dp[0].dp_flag & 0x7f) == 0 &&
413 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
414 g_topology_lock();
415 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
416 (((off_t)dp[0].dp_start) << 9ULL) + off,
417 ((off_t)dp[0].dp_size) << 9ULL,
418 sectorsize,
419 "%*.*s%d",
420 strlen(gp->name) - 1,
421 strlen(gp->name) - 1,
422 gp->name,
423 slice + 5);
424 g_topology_unlock();
425 ms->type[slice] = dp[0].dp_typ;
426 slice++;
427 }
428 if (dp[1].dp_flag != 0)
429 break;
430 if (dp[1].dp_typ != DOSPTYP_EXT &&
431 dp[1].dp_typ != DOSPTYP_EXTLBA)
432 break;
433 if (dp[1].dp_size == 0)
434 break;
435 off = ((off_t)dp[1].dp_start) << 9ULL;
436 }
437 break;
438 } while (0);
439 g_topology_lock();
440 g_access(cp, -1, 0, 0);
441 if (LIST_EMPTY(&gp->provider)) {
442 g_slice_spoiled(cp);
443 return (NULL);
444 }
445 return (gp);
446 }
447
448
449 static struct g_class g_mbrext_class = {
450 .name = MBREXT_CLASS_NAME,
451 .version = G_VERSION,
452 .taste = g_mbrext_taste,
453 .dumpconf = g_mbrext_dumpconf,
454 };
455
456 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
Cache object: 7dc366f2e9ded832f469e6112d7109f3
|