]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_mbr.c
This commit was generated by cvs2svn to compensate for changes in r93982,
[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         NULL,
248         G_CLASS_INITSTUFF
249 };
250
251 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
252
253 #define NDOSEXTPART             32
254 struct g_mbrext_softc {
255         int             type [NDOSEXTPART];
256 };
257
258 static int
259 g_mbrext_start(struct bio *bp)
260 {
261         struct g_provider *pp;
262         struct g_geom *gp;
263         struct g_mbrext_softc *mp;
264         struct g_slicer *gsp;
265         int index;
266
267         pp = bp->bio_to;
268         index = pp->index;
269         gp = pp->geom;
270         gsp = gp->softc;
271         mp = gsp->softc;
272         if (bp->bio_cmd == BIO_GETATTR) {
273                 if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
274                         return (1);
275         }
276         return (0);
277 }
278
279 static void
280 g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
281 {
282         struct g_mbrext_softc *mp;
283         struct g_slicer *gsp;
284
285         g_slice_dumpconf(sb, indent, gp, cp, pp);
286         gsp = gp->softc;
287         mp = gsp->softc;
288         if (pp != NULL) {
289                 sbuf_printf(sb, "%s<type>%d</type>\n",
290                     indent, mp->type[pp->index]);
291         }
292 }
293
294 static void
295 g_mbrext_print(int i, struct dos_partition *dp)
296 {
297         g_hexdump(dp, sizeof(dp[0]));
298         printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
299         printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
300         printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
301         printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
302 }
303
304 static struct g_geom *
305 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
306 {
307         struct g_geom *gp;
308         struct g_consumer *cp;
309         struct g_provider *pp2;
310         int error, i, j, slice;
311         struct g_mbrext_softc *ms;
312         off_t off;
313         u_char *buf;
314         struct dos_partition dp[4];
315
316         g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
317         g_topology_assert();
318         if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
319                 return (NULL);
320         gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
321         if (gp == NULL)
322                 return (NULL);
323         g_topology_unlock();
324         gp->dumpconf = g_mbrext_dumpconf;
325         off = 0;
326         slice = 0;
327         while (1) {     /* a trick to allow us to use break */
328                 j = sizeof i;
329                 error = g_io_getattr("MBR::type", cp, &j, &i);
330                 if (error || i != DOSPTYP_EXT)
331                         break;
332                 for (;;) {
333                         buf = g_read_data(cp, off, DEV_BSIZE, &error);
334                         if (buf == NULL || error != 0)
335                                 break;
336                         if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
337                                 break;
338                         for (i = 0; i < NDOSPART; i++) 
339                                 g_dec_dos_partition(
340                                     buf + DOSPARTOFF + i * sizeof(struct dos_partition),
341                                     dp + i);
342                         g_free(buf);
343                         g_mbrext_print(0, dp);
344                         g_mbrext_print(1, dp + 1);
345                         if (dp[0].dp_flag == 0 && dp[0].dp_size != 0) {
346                                 pp2 = g_slice_addslice(gp, slice,
347                                     (((off_t)dp[0].dp_start) << 9ULL) + off,
348                                     ((off_t)dp[0].dp_size) << 9ULL,
349                                     "%*.*s%d",
350                                     strlen(gp->name) - 1,
351                                     strlen(gp->name) - 1,
352                                     gp->name,
353                                     slice + 5);
354                                 ms->type[slice] = dp[0].dp_typ;
355                                 slice++;
356                                 g_error_provider(pp2, 0);
357                         }
358                         if (dp[1].dp_flag != 0)
359                                 break;
360                         if (dp[1].dp_typ != DOSPTYP_EXT)
361                                 break;
362                         if (dp[1].dp_size == 0)
363                                 break;
364                         off = ((off_t)dp[1].dp_start) << 9ULL;
365                 }
366                 break;
367         }
368         g_topology_lock();
369         error = g_access_rel(cp, -1, 0, 0);
370         if (slice > 0)
371                 return (gp);
372
373         g_topology_assert();
374         g_std_spoiled(cp);
375         g_topology_assert();
376         return (NULL);
377 }
378
379
380 static struct g_class g_mbrext_class    = {
381         MBREXT_CLASS_NAME,
382         g_mbrext_taste,
383         NULL,
384         G_CLASS_INITSTUFF
385 };
386
387 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);