]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_mbr.c
One less user of the bogus DIOCGPART ioctl.
[FreeBSD/FreeBSD.git] / 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  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37
38 #include <sys/param.h>
39 #ifndef _KERNEL
40 #include <stdio.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <sys/param.h>
44 #include <stdlib.h>
45 #include <err.h>
46 #else
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/bio.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #endif
54
55 #include <sys/errno.h>
56 #include <sys/disklabel.h>
57 #include <sys/sbuf.h>
58 #include <geom/geom.h>
59 #include <geom/geom_slice.h>
60
61 #define MBR_CLASS_NAME "MBR-class"
62 #define MBREXT_CLASS_NAME "MBREXT-class"
63
64 static void
65 g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
66 {
67
68         d->dp_flag = ptr[0];
69         d->dp_shd = ptr[1];
70         d->dp_ssect = ptr[2];
71         d->dp_scyl = ptr[3];
72         d->dp_typ = ptr[4];
73         d->dp_ehd = ptr[5];
74         d->dp_esect = ptr[6];
75         d->dp_ecyl = ptr[7];
76         d->dp_start = g_dec_le4(ptr + 8);
77         d->dp_size = g_dec_le4(ptr + 12);
78 }
79
80 #if 0
81 static void
82 g_enc_dos_partition(u_char *ptr, struct dos_partition *d)
83 {
84
85         ptr[0] = d->dp_flag;
86         ptr[1] = d->dp_shd;
87         ptr[2] = d->dp_ssect;
88         ptr[3] = d->dp_scyl;
89         ptr[4] = d->dp_typ;
90         ptr[5] = d->dp_ehd;
91         ptr[6] = d->dp_esect;
92         ptr[7] = d->dp_ecyl;
93         g_enc_le4(ptr + 8, d->dp_start);
94         g_enc_le4(ptr + 12, d->dp_size);
95 }
96 #endif
97
98 struct g_mbr_softc {
99         int             type [NDOSPART];
100         struct dos_partition dospart[NDOSPART];
101 };
102
103 static int
104 g_mbr_start(struct bio *bp)
105 {
106         struct g_provider *pp;
107         struct g_geom *gp;
108         struct g_mbr_softc *mp;
109         struct g_slicer *gsp;
110         int index;
111
112         pp = bp->bio_to;
113         index = pp->index;
114         gp = pp->geom;
115         gsp = gp->softc;
116         mp = gsp->softc;
117         if (bp->bio_cmd == BIO_GETATTR) {
118                 if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
119                         return (1);
120         }
121         return (0);
122 }
123
124 static void
125 g_mbr_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
126 {
127         struct g_mbr_softc *mp;
128         struct g_slicer *gsp;
129
130         g_slice_dumpconf(sb, indent, gp, cp, pp);
131         gsp = gp->softc;
132         mp = gsp->softc;
133         if (pp != NULL) {
134                 sbuf_printf(sb, "%s<type>%d</type>\n",
135                     indent, mp->type[pp->index]);
136         }
137 }
138
139
140 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
141         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
142         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
143         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
144         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
145 };
146 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
147         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
148         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
149         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
150         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
151 };
152
153 static void
154 g_mbr_print(int i __unused, struct dos_partition *dp __unused)
155 {
156
157 #if 0
158         g_hexdump(dp, sizeof(dp[0]));
159         printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
160         printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
161         printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
162         printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
163 #endif
164 }
165
166 static struct g_geom *
167 g_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, j, npart;
173         struct dos_partition dp[NDOSPART];
174         struct g_mbr_softc *ms;
175         u_char *buf;
176
177         if (sizeof(struct dos_partition) != 16) {
178                 printf("WARNING: struct dos_partition compiles to %d bytes, should be 16.\n",
179                     (int)sizeof(struct dos_partition));
180                 return (NULL);
181         }
182         g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
183         g_topology_assert();
184         gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
185         if (gp == NULL)
186                 return (NULL);
187         g_topology_unlock();
188         gp->dumpconf = g_mbr_dumpconf;
189         npart = 0;
190         while (1) {     /* a trick to allow us to use break */
191                 if (gp->rank != 2 && insist == 0)
192                         break;
193                 j = sizeof i;
194                 /* For now we only support 512 bytes sectors */
195                 error = g_io_getattr("GEOM::sectorsize", cp, &j, &i);
196                 if (!error && i != 512)
197                         break;
198                 buf = g_read_data(cp, 0, 512, &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                         break;
213                 if (bcmp(dp, historical_bogus_partition_table_fixed,
214                     sizeof historical_bogus_partition_table_fixed) == 0)
215                         break;
216                 npart = 0;
217                 for (i = 0; i < NDOSPART; i++) {
218                         if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
219                                 continue;
220                         if (dp[i].dp_size == 0)
221                                 continue;
222                         g_mbr_print(i, dp + i);
223                         npart++;
224                         ms->type[i] = dp[i].dp_typ;
225                         pp2 = g_slice_addslice(gp, i,
226                             ((off_t)dp[i].dp_start) << 9ULL,
227                             ((off_t)dp[i].dp_size) << 9ULL,
228                             "%ss%d", gp->name, i + 1);
229                 }
230                 break;
231         }
232         g_topology_lock();
233         error = g_access_rel(cp, -1, 0, 0);
234         if (npart > 0) {
235                 LIST_FOREACH(pp, &gp->provider, provider)
236                         g_error_provider(pp, 0);
237                 return (gp);
238         }
239         g_std_spoiled(cp);
240         return (NULL);
241 }
242
243
244 static struct g_class g_mbr_class       = {
245         MBR_CLASS_NAME,
246         g_mbr_taste,
247         g_slice_access,
248         g_slice_orphan,
249         NULL,
250         G_CLASS_INITSTUFF
251 };
252
253 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
254
255 #define NDOSEXTPART             32
256 struct g_mbrext_softc {
257         int             type [NDOSEXTPART];
258 };
259
260 static int
261 g_mbrext_start(struct bio *bp)
262 {
263         struct g_provider *pp;
264         struct g_geom *gp;
265         struct g_mbrext_softc *mp;
266         struct g_slicer *gsp;
267         int index;
268
269         pp = bp->bio_to;
270         index = pp->index;
271         gp = pp->geom;
272         gsp = gp->softc;
273         mp = gsp->softc;
274         if (bp->bio_cmd == BIO_GETATTR) {
275                 if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
276                         return (1);
277         }
278         return (0);
279 }
280
281 static void
282 g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
283 {
284         struct g_mbrext_softc *mp;
285         struct g_slicer *gsp;
286
287         g_slice_dumpconf(sb, indent, gp, cp, pp);
288         gsp = gp->softc;
289         mp = gsp->softc;
290         if (pp != NULL) {
291                 sbuf_printf(sb, "%s<type>%d</type>\n",
292                     indent, mp->type[pp->index]);
293         }
294 }
295
296 static void
297 g_mbrext_print(int i, struct dos_partition *dp)
298 {
299         g_hexdump(dp, sizeof(dp[0]));
300         printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
301         printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
302         printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
303         printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
304 }
305
306 static struct g_geom *
307 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
308 {
309         struct g_geom *gp;
310         struct g_consumer *cp;
311         struct g_provider *pp2;
312         int error, i, j, slice;
313         struct g_mbrext_softc *ms;
314         off_t off;
315         u_char *buf;
316         struct dos_partition dp[4];
317
318         g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
319         g_topology_assert();
320         if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
321                 return (NULL);
322         gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
323         if (gp == NULL)
324                 return (NULL);
325         g_topology_unlock();
326         gp->dumpconf = g_mbrext_dumpconf;
327         off = 0;
328         slice = 0;
329         while (1) {     /* a trick to allow us to use break */
330                 j = sizeof i;
331                 error = g_io_getattr("MBR::type", cp, &j, &i);
332                 if (error || i != DOSPTYP_EXT)
333                         break;
334                 for (;;) {
335                         buf = g_read_data(cp, off, DEV_BSIZE, &error);
336                         if (buf == NULL || error != 0)
337                                 break;
338                         if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
339                                 break;
340                         for (i = 0; i < NDOSPART; i++) 
341                                 g_dec_dos_partition(
342                                     buf + DOSPARTOFF + i * sizeof(struct dos_partition),
343                                     dp + i);
344                         g_free(buf);
345                         g_mbrext_print(0, dp);
346                         g_mbrext_print(1, dp + 1);
347                         if (dp[0].dp_flag == 0 && dp[0].dp_size != 0) {
348                                 pp2 = g_slice_addslice(gp, slice,
349                                     (((off_t)dp[0].dp_start) << 9ULL) + off,
350                                     ((off_t)dp[0].dp_size) << 9ULL,
351                                     "%*.*s%d",
352                                     strlen(gp->name) - 1,
353                                     strlen(gp->name) - 1,
354                                     gp->name,
355                                     slice + 5);
356                                 ms->type[slice] = dp[0].dp_typ;
357                                 slice++;
358                                 g_error_provider(pp2, 0);
359                         }
360                         if (dp[1].dp_flag != 0)
361                                 break;
362                         if (dp[1].dp_typ != DOSPTYP_EXT)
363                                 break;
364                         if (dp[1].dp_size == 0)
365                                 break;
366                         off = ((off_t)dp[1].dp_start) << 9ULL;
367                 }
368                 break;
369         }
370         g_topology_lock();
371         error = g_access_rel(cp, -1, 0, 0);
372         if (slice > 0)
373                 return (gp);
374
375         g_topology_assert();
376         g_std_spoiled(cp);
377         g_topology_assert();
378         return (NULL);
379 }
380
381
382 static struct g_class g_mbrext_class    = {
383         MBREXT_CLASS_NAME,
384         g_mbrext_taste,
385         g_slice_access,
386         g_slice_orphan,
387         NULL,
388         G_CLASS_INITSTUFF
389 };
390
391 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);