Deleted Added
full compact
geom_mbr.c (132911) geom_mbr.c (133314)
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>
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 132911 2004-07-31 01:17:20Z pjd $");
34__FBSDID("$FreeBSD: head/sys/geom/geom_mbr.c 133314 2004-08-08 06:49:07Z 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 int
156g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td)
157{
158 struct g_geom *gp;
159 struct g_mbr_softc *ms;
160 struct g_slicer *gsp;
161 struct g_consumer *cp;
162 int error;
163
164 gp = pp->geom;
165 gsp = gp->softc;
166 ms = gsp->softc;
167
168 switch(cmd) {
169 case DIOCSMBR: {
170 DROP_GIANT();
171 g_topology_lock();
172 /* Validate and modify our slicer instance to match. */
173 error = g_mbr_modify(gp, ms, data);
174 cp = LIST_FIRST(&gp->consumer);
175 error = g_write_data(cp, 0, data, 512);
176 g_topology_unlock();
177 PICKUP_GIANT();
178 return(error);
179 }
180 default:
181 return (ENOIOCTL);
182 }
183}
184
185static int
186g_mbr_start(struct bio *bp)
187{
188 struct g_provider *pp;
189 struct g_geom *gp;
190 struct g_mbr_softc *mp;
191 struct g_slicer *gsp;
192 int idx;
193
194 pp = bp->bio_to;
195 idx = pp->index;
196 gp = pp->geom;
197 gsp = gp->softc;
198 mp = gsp->softc;
199 if (bp->bio_cmd == BIO_GETATTR) {
200 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
201 return (1);
202 if (g_handleattr_off_t(bp, "MBR::offset",
203 gsp->slices[idx].offset))
204 return (1);
205 }
206
207 return (0);
208}
209
210static void
211g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
212{
213 struct g_mbr_softc *mp;
214 struct g_slicer *gsp;
215
216 gsp = gp->softc;
217 mp = gsp->softc;
218 g_slice_dumpconf(sb, indent, gp, cp, pp);
219 if (pp != NULL) {
220 if (indent == NULL)
221 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
222 else
223 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
224 mp->type[pp->index]);
225 }
226}
227
228static struct g_geom *
229g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
230{
231 struct g_geom *gp;
232 struct g_consumer *cp;
233 int error;
234 struct g_mbr_softc *ms;
235 u_int fwsectors, sectorsize;
236 u_char *buf;
237
238 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
239 g_topology_assert();
240 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
241 if (gp == NULL)
242 return (NULL);
243 g_topology_unlock();
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 int
156g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td)
157{
158 struct g_geom *gp;
159 struct g_mbr_softc *ms;
160 struct g_slicer *gsp;
161 struct g_consumer *cp;
162 int error;
163
164 gp = pp->geom;
165 gsp = gp->softc;
166 ms = gsp->softc;
167
168 switch(cmd) {
169 case DIOCSMBR: {
170 DROP_GIANT();
171 g_topology_lock();
172 /* Validate and modify our slicer instance to match. */
173 error = g_mbr_modify(gp, ms, data);
174 cp = LIST_FIRST(&gp->consumer);
175 error = g_write_data(cp, 0, data, 512);
176 g_topology_unlock();
177 PICKUP_GIANT();
178 return(error);
179 }
180 default:
181 return (ENOIOCTL);
182 }
183}
184
185static int
186g_mbr_start(struct bio *bp)
187{
188 struct g_provider *pp;
189 struct g_geom *gp;
190 struct g_mbr_softc *mp;
191 struct g_slicer *gsp;
192 int idx;
193
194 pp = bp->bio_to;
195 idx = pp->index;
196 gp = pp->geom;
197 gsp = gp->softc;
198 mp = gsp->softc;
199 if (bp->bio_cmd == BIO_GETATTR) {
200 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
201 return (1);
202 if (g_handleattr_off_t(bp, "MBR::offset",
203 gsp->slices[idx].offset))
204 return (1);
205 }
206
207 return (0);
208}
209
210static void
211g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
212{
213 struct g_mbr_softc *mp;
214 struct g_slicer *gsp;
215
216 gsp = gp->softc;
217 mp = gsp->softc;
218 g_slice_dumpconf(sb, indent, gp, cp, pp);
219 if (pp != NULL) {
220 if (indent == NULL)
221 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
222 else
223 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
224 mp->type[pp->index]);
225 }
226}
227
228static struct g_geom *
229g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
230{
231 struct g_geom *gp;
232 struct g_consumer *cp;
233 int error;
234 struct g_mbr_softc *ms;
235 u_int fwsectors, sectorsize;
236 u_char *buf;
237
238 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
239 g_topology_assert();
240 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
241 if (gp == NULL)
242 return (NULL);
243 g_topology_unlock();
244 gp->dumpconf = g_mbr_dumpconf;
245 gp->ioctl = g_mbr_ioctl;
246 do {
247 /* XXX: phk think about this! */
248 if (gp->rank != 2 &&
249 strcmp(pp->geom->class->name, "LABEL") != 0 &&
250 strcmp(pp->geom->class->name, "MIRROR") != 0 &&
251 strcmp(pp->geom->class->name, "NOP") != 0) {
252 break;
253 }
254 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
255 if (error)
256 fwsectors = 17;
257 sectorsize = cp->provider->sectorsize;
258 if (sectorsize < 512)
259 break;
260 ms->sectorsize = sectorsize;
261 buf = g_read_data(cp, 0, sectorsize, &error);
262 if (buf == NULL || error != 0)
263 break;
264 g_topology_lock();
265 g_mbr_modify(gp, ms, buf);
266 g_topology_unlock();
267 g_free(buf);
268 break;
269 } while (0);
270 g_topology_lock();
271 g_access(cp, -1, 0, 0);
272 if (LIST_EMPTY(&gp->provider)) {
273 g_slice_spoiled(cp);
274 return (NULL);
275 }
276 return (gp);
277}
278
279static struct g_class g_mbr_class = {
280 .name = MBR_CLASS_NAME,
281 .taste = g_mbr_taste,
244 do {
245 /* XXX: phk think about this! */
246 if (gp->rank != 2 &&
247 strcmp(pp->geom->class->name, "LABEL") != 0 &&
248 strcmp(pp->geom->class->name, "MIRROR") != 0 &&
249 strcmp(pp->geom->class->name, "NOP") != 0) {
250 break;
251 }
252 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
253 if (error)
254 fwsectors = 17;
255 sectorsize = cp->provider->sectorsize;
256 if (sectorsize < 512)
257 break;
258 ms->sectorsize = sectorsize;
259 buf = g_read_data(cp, 0, sectorsize, &error);
260 if (buf == NULL || error != 0)
261 break;
262 g_topology_lock();
263 g_mbr_modify(gp, ms, buf);
264 g_topology_unlock();
265 g_free(buf);
266 break;
267 } while (0);
268 g_topology_lock();
269 g_access(cp, -1, 0, 0);
270 if (LIST_EMPTY(&gp->provider)) {
271 g_slice_spoiled(cp);
272 return (NULL);
273 }
274 return (gp);
275}
276
277static struct g_class g_mbr_class = {
278 .name = MBR_CLASS_NAME,
279 .taste = g_mbr_taste,
280 .dumpconf = g_mbr_dumpconf,
281 .ioctl = g_mbr_ioctl,
282};
283
284DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
285
286#define NDOSEXTPART 32
287struct g_mbrext_softc {
288 int type [NDOSEXTPART];
289};
290
291static int
292g_mbrext_start(struct bio *bp)
293{
294 struct g_provider *pp;
295 struct g_geom *gp;
296 struct g_mbrext_softc *mp;
297 struct g_slicer *gsp;
298 int idx;
299
300 pp = bp->bio_to;
301 idx = pp->index;
302 gp = pp->geom;
303 gsp = gp->softc;
304 mp = gsp->softc;
305 if (bp->bio_cmd == BIO_GETATTR) {
306 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
307 return (1);
308 }
309 return (0);
310}
311
312static void
313g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
314{
315 struct g_mbrext_softc *mp;
316 struct g_slicer *gsp;
317
318 g_slice_dumpconf(sb, indent, gp, cp, pp);
319 gsp = gp->softc;
320 mp = gsp->softc;
321 if (pp != NULL) {
322 if (indent == NULL)
323 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
324 else
325 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
326 mp->type[pp->index]);
327 }
328}
329
330static struct g_geom *
331g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
332{
333 struct g_geom *gp;
334 struct g_consumer *cp;
335 int error, i, slice;
336 struct g_mbrext_softc *ms;
337 off_t off;
338 u_char *buf;
339 struct dos_partition dp[4];
340 u_int fwsectors, sectorsize;
341
342 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
343 g_topology_assert();
344 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
345 return (NULL);
346 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
347 g_mbrext_start);
348 if (gp == NULL)
349 return (NULL);
350 g_topology_unlock();
282};
283
284DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
285
286#define NDOSEXTPART 32
287struct g_mbrext_softc {
288 int type [NDOSEXTPART];
289};
290
291static int
292g_mbrext_start(struct bio *bp)
293{
294 struct g_provider *pp;
295 struct g_geom *gp;
296 struct g_mbrext_softc *mp;
297 struct g_slicer *gsp;
298 int idx;
299
300 pp = bp->bio_to;
301 idx = pp->index;
302 gp = pp->geom;
303 gsp = gp->softc;
304 mp = gsp->softc;
305 if (bp->bio_cmd == BIO_GETATTR) {
306 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
307 return (1);
308 }
309 return (0);
310}
311
312static void
313g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
314{
315 struct g_mbrext_softc *mp;
316 struct g_slicer *gsp;
317
318 g_slice_dumpconf(sb, indent, gp, cp, pp);
319 gsp = gp->softc;
320 mp = gsp->softc;
321 if (pp != NULL) {
322 if (indent == NULL)
323 sbuf_printf(sb, " ty %d", mp->type[pp->index]);
324 else
325 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
326 mp->type[pp->index]);
327 }
328}
329
330static struct g_geom *
331g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
332{
333 struct g_geom *gp;
334 struct g_consumer *cp;
335 int error, i, slice;
336 struct g_mbrext_softc *ms;
337 off_t off;
338 u_char *buf;
339 struct dos_partition dp[4];
340 u_int fwsectors, sectorsize;
341
342 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
343 g_topology_assert();
344 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
345 return (NULL);
346 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
347 g_mbrext_start);
348 if (gp == NULL)
349 return (NULL);
350 g_topology_unlock();
351 gp->dumpconf = g_mbrext_dumpconf;
352 off = 0;
353 slice = 0;
354 do {
355 error = g_getattr("MBR::type", cp, &i);
356 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
357 break;
358 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
359 if (error)
360 fwsectors = 17;
361 sectorsize = cp->provider->sectorsize;
362 if (sectorsize != 512)
363 break;
364 for (;;) {
365 buf = g_read_data(cp, off, sectorsize, &error);
366 if (buf == NULL || error != 0)
367 break;
368 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
369 g_free(buf);
370 break;
371 }
372 for (i = 0; i < NDOSPART; i++)
373 dos_partition_dec(
374 buf + DOSPARTOFF +
375 i * sizeof(struct dos_partition), dp + i);
376 g_free(buf);
377 if (bootverbose) {
378 printf("MBREXT Slice %d on %s:\n",
379 slice + 5, gp->name);
380 g_mbr_print(0, dp);
381 g_mbr_print(1, dp + 1);
382 }
383 if ((dp[0].dp_flag & 0x7f) == 0 &&
384 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
385 g_topology_lock();
386 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
387 (((off_t)dp[0].dp_start) << 9ULL) + off,
388 ((off_t)dp[0].dp_size) << 9ULL,
389 sectorsize,
390 "%*.*s%d",
391 strlen(gp->name) - 1,
392 strlen(gp->name) - 1,
393 gp->name,
394 slice + 5);
395 g_topology_unlock();
396 ms->type[slice] = dp[0].dp_typ;
397 slice++;
398 }
399 if (dp[1].dp_flag != 0)
400 break;
401 if (dp[1].dp_typ != DOSPTYP_EXT &&
402 dp[1].dp_typ != DOSPTYP_EXTLBA)
403 break;
404 if (dp[1].dp_size == 0)
405 break;
406 off = ((off_t)dp[1].dp_start) << 9ULL;
407 }
408 break;
409 } while (0);
410 g_topology_lock();
411 g_access(cp, -1, 0, 0);
412 if (LIST_EMPTY(&gp->provider)) {
413 g_slice_spoiled(cp);
414 return (NULL);
415 }
416 return (gp);
417}
418
419
420static struct g_class g_mbrext_class = {
421 .name = MBREXT_CLASS_NAME,
422 .taste = g_mbrext_taste,
351 off = 0;
352 slice = 0;
353 do {
354 error = g_getattr("MBR::type", cp, &i);
355 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
356 break;
357 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
358 if (error)
359 fwsectors = 17;
360 sectorsize = cp->provider->sectorsize;
361 if (sectorsize != 512)
362 break;
363 for (;;) {
364 buf = g_read_data(cp, off, sectorsize, &error);
365 if (buf == NULL || error != 0)
366 break;
367 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
368 g_free(buf);
369 break;
370 }
371 for (i = 0; i < NDOSPART; i++)
372 dos_partition_dec(
373 buf + DOSPARTOFF +
374 i * sizeof(struct dos_partition), dp + i);
375 g_free(buf);
376 if (bootverbose) {
377 printf("MBREXT Slice %d on %s:\n",
378 slice + 5, gp->name);
379 g_mbr_print(0, dp);
380 g_mbr_print(1, dp + 1);
381 }
382 if ((dp[0].dp_flag & 0x7f) == 0 &&
383 dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
384 g_topology_lock();
385 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
386 (((off_t)dp[0].dp_start) << 9ULL) + off,
387 ((off_t)dp[0].dp_size) << 9ULL,
388 sectorsize,
389 "%*.*s%d",
390 strlen(gp->name) - 1,
391 strlen(gp->name) - 1,
392 gp->name,
393 slice + 5);
394 g_topology_unlock();
395 ms->type[slice] = dp[0].dp_typ;
396 slice++;
397 }
398 if (dp[1].dp_flag != 0)
399 break;
400 if (dp[1].dp_typ != DOSPTYP_EXT &&
401 dp[1].dp_typ != DOSPTYP_EXTLBA)
402 break;
403 if (dp[1].dp_size == 0)
404 break;
405 off = ((off_t)dp[1].dp_start) << 9ULL;
406 }
407 break;
408 } while (0);
409 g_topology_lock();
410 g_access(cp, -1, 0, 0);
411 if (LIST_EMPTY(&gp->provider)) {
412 g_slice_spoiled(cp);
413 return (NULL);
414 }
415 return (gp);
416}
417
418
419static struct g_class g_mbrext_class = {
420 .name = MBREXT_CLASS_NAME,
421 .taste = g_mbrext_taste,
422 .dumpconf = g_mbrext_dumpconf,
423};
424
425DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
423};
424
425DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);