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