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 * $FreeBSD: head/sys/geom/geom_mbr.c 106397 2002-11-04 06:29:05Z phk $
33 */
34
35#include <sys/param.h>
36#ifndef _KERNEL
37#include <stdio.h>
38#include <string.h>
39#include <signal.h>
40#include <sys/param.h>
41#include <stdlib.h>
42#include <err.h>
43#else
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/bio.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#endif
51
52#include <sys/diskmbr.h>
53#include <sys/sbuf.h>
54#include <geom/geom.h>
55#include <geom/geom_slice.h>
56
57#define MBR_CLASS_NAME "MBR"
58#define MBREXT_CLASS_NAME "MBREXT"
59
60static void
61g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
62{
63
64 d->dp_flag = ptr[0];
65 d->dp_shd = ptr[1];
66 d->dp_ssect = ptr[2];
67 d->dp_scyl = ptr[3];
68 d->dp_typ = ptr[4];
69 d->dp_ehd = ptr[5];
70 d->dp_esect = ptr[6];
71 d->dp_ecyl = ptr[7];
72 d->dp_start = g_dec_le4(ptr + 8);
73 d->dp_size = g_dec_le4(ptr + 12);
74}
75
76#if 0
77static void
78g_enc_dos_partition(u_char *ptr, struct dos_partition *d)
79{
80
81 ptr[0] = d->dp_flag;
82 ptr[1] = d->dp_shd;
83 ptr[2] = d->dp_ssect;
84 ptr[3] = d->dp_scyl;
85 ptr[4] = d->dp_typ;
86 ptr[5] = d->dp_ehd;
87 ptr[6] = d->dp_esect;
88 ptr[7] = d->dp_ecyl;
89 g_enc_le4(ptr + 8, d->dp_start);
90 g_enc_le4(ptr + 12, d->dp_size);
91}
92#endif
93
94struct g_mbr_softc {
95 int type [NDOSPART];
96 struct dos_partition dospart[NDOSPART];
97};
98
99static int
100g_mbr_start(struct bio *bp)
101{
102 struct g_provider *pp;
103 struct g_geom *gp;
104 struct g_mbr_softc *mp;
105 struct g_slicer *gsp;
106 int index;
107
108 pp = bp->bio_to;
109 index = pp->index;
110 gp = pp->geom;
111 gsp = gp->softc;
112 mp = gsp->softc;
113 if (bp->bio_cmd == BIO_GETATTR) {
114 if (g_handleattr_int(bp, "MBR::type", mp->type[index]))
115 return (1);
116 if (g_handleattr_off_t(bp, "MBR::offset",
117 gsp->slices[index].offset))
118 return (1);
119 }
120 return (0);
121}
122
123static void
124g_mbr_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
125{
126 struct g_mbr_softc *mp;
127 struct g_slicer *gsp;
128
129 gsp = gp->softc;
130 mp = gsp->softc;
131 g_slice_dumpconf(sb, indent, gp, cp, pp);
132 if (pp != NULL) {
133 if (indent == NULL)
134 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
135 else
136 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
137 mp->type[pp->index]);
138 }
139}
140
141
142static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
144 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
146 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
147};
148static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
149 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
150 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
151 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
152 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
153};
154
155static void
156g_mbr_print(int i, struct dos_partition *dp)
157{
158
159 g_hexdump(dp, sizeof(dp[0]));
160 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
161 printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
162 printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
163 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
164}
165
166static struct g_geom *
167g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
168{
169 struct g_geom *gp;
170 struct g_consumer *cp;
171 struct g_provider *pp2;
172 int error, i, npart;
173 struct dos_partition dp[NDOSPART];
174 struct g_mbr_softc *ms;
175 struct g_slicer *gsp;
176 u_int fwsectors, sectorsize;
177 u_char *buf;
178
179 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
180 g_topology_assert();
181 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
182 if (gp == NULL)
183 return (NULL);
184 gsp = gp->softc;
185 g_topology_unlock();
186 gp->dumpconf = g_mbr_dumpconf;
187 npart = 0;
188 while (1) { /* a trick to allow us to use break */
189 if (gp->rank != 2 && insist == 0)
190 break;
191 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
192 if (error)
193 fwsectors = 17;
194 sectorsize = cp->provider->sectorsize;
195 if (sectorsize != 512)
196 break;
197 gsp->frontstuff = sectorsize * fwsectors;
198 buf = g_read_data(cp, 0, sectorsize, &error);
199 if (buf == NULL || error != 0)
200 break;
201 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
202 g_free(buf);
203 break;
204 }
205 for (i = 0; i < NDOSPART; i++)
206 g_dec_dos_partition(
207 buf + DOSPARTOFF + i * sizeof(struct dos_partition),
208 dp + i);
209 g_free(buf);
210 if (bcmp(dp, historical_bogus_partition_table,
211 sizeof historical_bogus_partition_table) == 0) {
212 if (bootverbose)
213 printf("Ignoring known bogus MBR #0\n");
214 break;
215 }
216 if (bcmp(dp, historical_bogus_partition_table_fixed,
217 sizeof historical_bogus_partition_table_fixed) == 0) {
218 if (bootverbose)
219 printf("Ignoring known bogus MBR #1\n");
220 break;
221 }
222 npart = 0;
223 for (i = 0; i < NDOSPART; i++) {
224 /*
225 * A Protective MBR (PMBR) has a single partition of
226 * type 0xEE spanning the whole disk. Such a MBR
227 * protects a GPT on the disk from MBR tools that
228 * don't know anything about GPT. We're interpreting
229 * it a bit more loosely: any partition of type 0xEE
230 * is to be skipped as it doesn't contain any data
231 * that we should care about. We still allow other
232 * partitions to be present in the MBR. A PMBR will
233 * be handled correctly anyway.
234 */
235 if (dp[i].dp_typ == 0xee)
236 continue;
237 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
238 continue;
239 if (dp[i].dp_typ == 0)
240 continue;
241 if (dp[i].dp_size == 0)
242 continue;
243 if (bootverbose) {
244 printf("MBR Slice %d on %s:\n", i + 1, gp->name);
245 g_mbr_print(i, dp + i);
246 }
247 npart++;
248 ms->type[i] = dp[i].dp_typ;
249 g_topology_lock();
250 pp2 = g_slice_addslice(gp, i,
251 (off_t)dp[i].dp_start << 9ULL,
252 (off_t)dp[i].dp_size << 9ULL,
253 sectorsize,
254 "%ss%d", gp->name, i + 1);
255 g_topology_unlock();
256 }
257 break;
258 }
259 g_topology_lock();
260 error = g_access_rel(cp, -1, 0, 0);
261 if (npart > 0) {
262 LIST_FOREACH(pp, &gp->provider, provider)
263 g_error_provider(pp, 0);
264 return (gp);
265 }
266 g_std_spoiled(cp);
267 return (NULL);
268}
269
270
271static struct g_class g_mbr_class = {
272 MBR_CLASS_NAME,
273 g_mbr_taste,
274 NULL,
275 G_CLASS_INITIALIZER
276};
277
278DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
279
280#define NDOSEXTPART 32
281struct g_mbrext_softc {
282 int type [NDOSEXTPART];
283};
284
285static int
286g_mbrext_start(struct bio *bp)
287{
288 struct g_provider *pp;
289 struct g_geom *gp;
290 struct g_mbrext_softc *mp;
291 struct g_slicer *gsp;
292 int index;
293
294 pp = bp->bio_to;
295 index = pp->index;
296 gp = pp->geom;
297 gsp = gp->softc;
298 mp = gsp->softc;
299 if (bp->bio_cmd == BIO_GETATTR) {
300 if (g_handleattr_int(bp, "MBR::type", mp->type[index]))
301 return (1);
302 }
303 return (0);
304}
305
306static void
307g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
308{
309 struct g_mbrext_softc *mp;
310 struct g_slicer *gsp;
311
312 g_slice_dumpconf(sb, indent, gp, cp, pp);
313 gsp = gp->softc;
314 mp = gsp->softc;
315 if (pp != NULL) {
316 if (indent == NULL)
317 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
318 else
319 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
320 mp->type[pp->index]);
321 }
322}
323
324static struct g_geom *
325g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
326{
327 struct g_geom *gp;
328 struct g_consumer *cp;
329 struct g_provider *pp2;
330 int error, i, slice;
331 struct g_mbrext_softc *ms;
332 off_t off;
333 u_char *buf;
334 struct dos_partition dp[4];
335 u_int fwsectors, sectorsize;
336 struct g_slicer *gsp;
337
338 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
339 g_topology_assert();
340 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
341 return (NULL);
342 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
343 if (gp == NULL)
344 return (NULL);
345 gsp = gp->softc;
346 g_topology_unlock();
347 gp->dumpconf = g_mbrext_dumpconf;
348 off = 0;
349 slice = 0;
350 while (1) { /* a trick to allow us to use break */
351 error = g_getattr("MBR::type", cp, &i);
352 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
353 break;
354 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
355 if (error)
356 fwsectors = 17;
357 sectorsize = cp->provider->sectorsize;
358 if (sectorsize != 512)
359 break;
360 gsp->frontstuff = sectorsize * fwsectors;
361 for (;;) {
362 buf = g_read_data(cp, off, sectorsize, &error);
363 if (buf == NULL || error != 0)
364 break;
365 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
366 break;
367 for (i = 0; i < NDOSPART; i++)
368 g_dec_dos_partition(
369 buf + DOSPARTOFF +
370 i * sizeof(struct dos_partition), dp + i);
371 g_free(buf);
372 printf("MBREXT Slice %d on %s:\n", slice + 5, gp->name);
373 g_mbr_print(0, dp);
374 g_mbr_print(1, dp + 1);
375 if ((dp[0].dp_flag & 0x7f) == 0 &&
376 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
377 g_topology_lock();
378 pp2 = g_slice_addslice(gp, slice,
379 (((off_t)dp[0].dp_start) << 9ULL) + off,
380 ((off_t)dp[0].dp_size) << 9ULL,
381 sectorsize,
382 "%*.*s%d",
383 strlen(gp->name) - 1,
384 strlen(gp->name) - 1,
385 gp->name,
386 slice + 5);
387 g_topology_unlock();
388 ms->type[slice] = dp[0].dp_typ;
389 slice++;
390 g_error_provider(pp2, 0);
391 }
392 if (dp[1].dp_flag != 0)
393 break;
394 if (dp[1].dp_typ != DOSPTYP_EXT)
395 break;
396 if (dp[1].dp_size == 0)
397 break;
398 off = ((off_t)dp[1].dp_start) << 9ULL;
399 }
400 break;
401 }
402 g_topology_lock();
403 error = g_access_rel(cp, -1, 0, 0);
404 if (slice > 0) {
405 /* XXX: add magic spaces */
406 return (gp);
407 }
408
409 g_topology_assert();
410 g_std_spoiled(cp);
411 g_topology_assert();
412 return (NULL);
413}
414
415
416static struct g_class g_mbrext_class = {
417 MBREXT_CLASS_NAME,
418 g_mbrext_taste,
419 NULL,
420 G_CLASS_INITIALIZER
421};
422
423DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
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 * $FreeBSD: head/sys/geom/geom_mbr.c 106397 2002-11-04 06:29:05Z phk $
33 */
34
35#include <sys/param.h>
36#ifndef _KERNEL
37#include <stdio.h>
38#include <string.h>
39#include <signal.h>
40#include <sys/param.h>
41#include <stdlib.h>
42#include <err.h>
43#else
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/bio.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#endif
51
52#include <sys/diskmbr.h>
53#include <sys/sbuf.h>
54#include <geom/geom.h>
55#include <geom/geom_slice.h>
56
57#define MBR_CLASS_NAME "MBR"
58#define MBREXT_CLASS_NAME "MBREXT"
59
60static void
61g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
62{
63
64 d->dp_flag = ptr[0];
65 d->dp_shd = ptr[1];
66 d->dp_ssect = ptr[2];
67 d->dp_scyl = ptr[3];
68 d->dp_typ = ptr[4];
69 d->dp_ehd = ptr[5];
70 d->dp_esect = ptr[6];
71 d->dp_ecyl = ptr[7];
72 d->dp_start = g_dec_le4(ptr + 8);
73 d->dp_size = g_dec_le4(ptr + 12);
74}
75
76#if 0
77static void
78g_enc_dos_partition(u_char *ptr, struct dos_partition *d)
79{
80
81 ptr[0] = d->dp_flag;
82 ptr[1] = d->dp_shd;
83 ptr[2] = d->dp_ssect;
84 ptr[3] = d->dp_scyl;
85 ptr[4] = d->dp_typ;
86 ptr[5] = d->dp_ehd;
87 ptr[6] = d->dp_esect;
88 ptr[7] = d->dp_ecyl;
89 g_enc_le4(ptr + 8, d->dp_start);
90 g_enc_le4(ptr + 12, d->dp_size);
91}
92#endif
93
94struct g_mbr_softc {
95 int type [NDOSPART];
96 struct dos_partition dospart[NDOSPART];
97};
98
99static int
100g_mbr_start(struct bio *bp)
101{
102 struct g_provider *pp;
103 struct g_geom *gp;
104 struct g_mbr_softc *mp;
105 struct g_slicer *gsp;
106 int index;
107
108 pp = bp->bio_to;
109 index = pp->index;
110 gp = pp->geom;
111 gsp = gp->softc;
112 mp = gsp->softc;
113 if (bp->bio_cmd == BIO_GETATTR) {
114 if (g_handleattr_int(bp, "MBR::type", mp->type[index]))
115 return (1);
116 if (g_handleattr_off_t(bp, "MBR::offset",
117 gsp->slices[index].offset))
118 return (1);
119 }
120 return (0);
121}
122
123static void
124g_mbr_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
125{
126 struct g_mbr_softc *mp;
127 struct g_slicer *gsp;
128
129 gsp = gp->softc;
130 mp = gsp->softc;
131 g_slice_dumpconf(sb, indent, gp, cp, pp);
132 if (pp != NULL) {
133 if (indent == NULL)
134 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
135 else
136 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
137 mp->type[pp->index]);
138 }
139}
140
141
142static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
144 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
146 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
147};
148static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
149 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
150 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
151 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
152 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
153};
154
155static void
156g_mbr_print(int i, struct dos_partition *dp)
157{
158
159 g_hexdump(dp, sizeof(dp[0]));
160 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
161 printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
162 printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
163 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
164}
165
166static struct g_geom *
167g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
168{
169 struct g_geom *gp;
170 struct g_consumer *cp;
171 struct g_provider *pp2;
172 int error, i, npart;
173 struct dos_partition dp[NDOSPART];
174 struct g_mbr_softc *ms;
175 struct g_slicer *gsp;
176 u_int fwsectors, sectorsize;
177 u_char *buf;
178
179 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
180 g_topology_assert();
181 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
182 if (gp == NULL)
183 return (NULL);
184 gsp = gp->softc;
185 g_topology_unlock();
186 gp->dumpconf = g_mbr_dumpconf;
187 npart = 0;
188 while (1) { /* a trick to allow us to use break */
189 if (gp->rank != 2 && insist == 0)
190 break;
191 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
192 if (error)
193 fwsectors = 17;
194 sectorsize = cp->provider->sectorsize;
195 if (sectorsize != 512)
196 break;
197 gsp->frontstuff = sectorsize * fwsectors;
198 buf = g_read_data(cp, 0, sectorsize, &error);
199 if (buf == NULL || error != 0)
200 break;
201 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
202 g_free(buf);
203 break;
204 }
205 for (i = 0; i < NDOSPART; i++)
206 g_dec_dos_partition(
207 buf + DOSPARTOFF + i * sizeof(struct dos_partition),
208 dp + i);
209 g_free(buf);
210 if (bcmp(dp, historical_bogus_partition_table,
211 sizeof historical_bogus_partition_table) == 0) {
212 if (bootverbose)
213 printf("Ignoring known bogus MBR #0\n");
214 break;
215 }
216 if (bcmp(dp, historical_bogus_partition_table_fixed,
217 sizeof historical_bogus_partition_table_fixed) == 0) {
218 if (bootverbose)
219 printf("Ignoring known bogus MBR #1\n");
220 break;
221 }
222 npart = 0;
223 for (i = 0; i < NDOSPART; i++) {
224 /*
225 * A Protective MBR (PMBR) has a single partition of
226 * type 0xEE spanning the whole disk. Such a MBR
227 * protects a GPT on the disk from MBR tools that
228 * don't know anything about GPT. We're interpreting
229 * it a bit more loosely: any partition of type 0xEE
230 * is to be skipped as it doesn't contain any data
231 * that we should care about. We still allow other
232 * partitions to be present in the MBR. A PMBR will
233 * be handled correctly anyway.
234 */
235 if (dp[i].dp_typ == 0xee)
236 continue;
237 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
238 continue;
239 if (dp[i].dp_typ == 0)
240 continue;
241 if (dp[i].dp_size == 0)
242 continue;
243 if (bootverbose) {
244 printf("MBR Slice %d on %s:\n", i + 1, gp->name);
245 g_mbr_print(i, dp + i);
246 }
247 npart++;
248 ms->type[i] = dp[i].dp_typ;
249 g_topology_lock();
250 pp2 = g_slice_addslice(gp, i,
251 (off_t)dp[i].dp_start << 9ULL,
252 (off_t)dp[i].dp_size << 9ULL,
253 sectorsize,
254 "%ss%d", gp->name, i + 1);
255 g_topology_unlock();
256 }
257 break;
258 }
259 g_topology_lock();
260 error = g_access_rel(cp, -1, 0, 0);
261 if (npart > 0) {
262 LIST_FOREACH(pp, &gp->provider, provider)
263 g_error_provider(pp, 0);
264 return (gp);
265 }
266 g_std_spoiled(cp);
267 return (NULL);
268}
269
270
271static struct g_class g_mbr_class = {
272 MBR_CLASS_NAME,
273 g_mbr_taste,
274 NULL,
275 G_CLASS_INITIALIZER
276};
277
278DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
279
280#define NDOSEXTPART 32
281struct g_mbrext_softc {
282 int type [NDOSEXTPART];
283};
284
285static int
286g_mbrext_start(struct bio *bp)
287{
288 struct g_provider *pp;
289 struct g_geom *gp;
290 struct g_mbrext_softc *mp;
291 struct g_slicer *gsp;
292 int index;
293
294 pp = bp->bio_to;
295 index = pp->index;
296 gp = pp->geom;
297 gsp = gp->softc;
298 mp = gsp->softc;
299 if (bp->bio_cmd == BIO_GETATTR) {
300 if (g_handleattr_int(bp, "MBR::type", mp->type[index]))
301 return (1);
302 }
303 return (0);
304}
305
306static void
307g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
308{
309 struct g_mbrext_softc *mp;
310 struct g_slicer *gsp;
311
312 g_slice_dumpconf(sb, indent, gp, cp, pp);
313 gsp = gp->softc;
314 mp = gsp->softc;
315 if (pp != NULL) {
316 if (indent == NULL)
317 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
318 else
319 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
320 mp->type[pp->index]);
321 }
322}
323
324static struct g_geom *
325g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
326{
327 struct g_geom *gp;
328 struct g_consumer *cp;
329 struct g_provider *pp2;
330 int error, i, slice;
331 struct g_mbrext_softc *ms;
332 off_t off;
333 u_char *buf;
334 struct dos_partition dp[4];
335 u_int fwsectors, sectorsize;
336 struct g_slicer *gsp;
337
338 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
339 g_topology_assert();
340 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
341 return (NULL);
342 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
343 if (gp == NULL)
344 return (NULL);
345 gsp = gp->softc;
346 g_topology_unlock();
347 gp->dumpconf = g_mbrext_dumpconf;
348 off = 0;
349 slice = 0;
350 while (1) { /* a trick to allow us to use break */
351 error = g_getattr("MBR::type", cp, &i);
352 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
353 break;
354 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
355 if (error)
356 fwsectors = 17;
357 sectorsize = cp->provider->sectorsize;
358 if (sectorsize != 512)
359 break;
360 gsp->frontstuff = sectorsize * fwsectors;
361 for (;;) {
362 buf = g_read_data(cp, off, sectorsize, &error);
363 if (buf == NULL || error != 0)
364 break;
365 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
366 break;
367 for (i = 0; i < NDOSPART; i++)
368 g_dec_dos_partition(
369 buf + DOSPARTOFF +
370 i * sizeof(struct dos_partition), dp + i);
371 g_free(buf);
372 printf("MBREXT Slice %d on %s:\n", slice + 5, gp->name);
373 g_mbr_print(0, dp);
374 g_mbr_print(1, dp + 1);
375 if ((dp[0].dp_flag & 0x7f) == 0 &&
376 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
377 g_topology_lock();
378 pp2 = g_slice_addslice(gp, slice,
379 (((off_t)dp[0].dp_start) << 9ULL) + off,
380 ((off_t)dp[0].dp_size) << 9ULL,
381 sectorsize,
382 "%*.*s%d",
383 strlen(gp->name) - 1,
384 strlen(gp->name) - 1,
385 gp->name,
386 slice + 5);
387 g_topology_unlock();
388 ms->type[slice] = dp[0].dp_typ;
389 slice++;
390 g_error_provider(pp2, 0);
391 }
392 if (dp[1].dp_flag != 0)
393 break;
394 if (dp[1].dp_typ != DOSPTYP_EXT)
395 break;
396 if (dp[1].dp_size == 0)
397 break;
398 off = ((off_t)dp[1].dp_start) << 9ULL;
399 }
400 break;
401 }
402 g_topology_lock();
403 error = g_access_rel(cp, -1, 0, 0);
404 if (slice > 0) {
405 /* XXX: add magic spaces */
406 return (gp);
407 }
408
409 g_topology_assert();
410 g_std_spoiled(cp);
411 g_topology_assert();
412 return (NULL);
413}
414
415
416static struct g_class g_mbrext_class = {
417 MBREXT_CLASS_NAME,
418 g_mbrext_taste,
419 NULL,
420 G_CLASS_INITIALIZER
421};
422
423DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);