]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/geom/geom_mbr.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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  *
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$");
35
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/endian.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/kernel.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/bio.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/md5.h>
48
49 #include <sys/diskmbr.h>
50 #include <sys/sbuf.h>
51 #include <geom/geom.h>
52 #include <geom/geom_slice.h>
53
54 FEATURE(geom_mbr, "GEOM DOS/MBR partitioning support");
55
56 #define MBR_CLASS_NAME "MBR"
57 #define MBREXT_CLASS_NAME "MBREXT"
58
59 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
60         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
61         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
62         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
63         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
64 };
65
66 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
67         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
68         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
69         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
70         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
71 };
72
73 static void
74 g_mbr_print(int i, struct dos_partition *dp)
75 {
76
77         printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
78         printf(" s(CHS):%d/%d/%d", DPCYL(dp->dp_scyl, dp->dp_ssect),
79             dp->dp_shd, DPSECT(dp->dp_ssect));
80         printf(" e(CHS):%d/%d/%d", DPCYL(dp->dp_ecyl, dp->dp_esect),
81             dp->dp_ehd, DPSECT(dp->dp_esect));
82         printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
83 }
84
85 struct g_mbr_softc {
86         int             type [NDOSPART];
87         u_int           sectorsize;
88         u_char          sec0[512];
89         u_char          slicesum[16];
90 };
91
92 /*
93  * XXX: Add gctl_req arg and give good error msgs.
94  * XXX: Check that length argument does not bring boot code inside any slice.
95  */
96 static int
97 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0, int len __unused)
98 {
99         int i, error;
100         off_t l[NDOSPART];
101         struct dos_partition ndp[NDOSPART], *dp;
102         MD5_CTX md5sum;
103
104         g_topology_assert();
105
106         if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
107                 return (EBUSY);
108
109         dp = ndp;
110         for (i = 0; i < NDOSPART; i++) {
111                 dos_partition_dec(
112                     sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
113                     dp + i);
114         }
115         if ((!bcmp(dp, historical_bogus_partition_table,
116             sizeof historical_bogus_partition_table)) ||
117             (!bcmp(dp, historical_bogus_partition_table_fixed,
118             sizeof historical_bogus_partition_table_fixed))) {
119                 /*
120                  * We will not allow people to write these from "the inside",
121                  * Since properly selfdestructing takes too much code.  If 
122                  * people really want to do this, they cannot have any
123                  * providers of this geom open, and in that case they can just
124                  * as easily overwrite the MBR in the parent device.
125                  */
126                 return(EBUSY);
127         }
128         for (i = 0; i < NDOSPART; i++) {
129                 /* 
130                  * A Protective MBR (PMBR) has a single partition of
131                  * type 0xEE spanning the whole disk. Such a MBR
132                  * protects a GPT on the disk from MBR tools that
133                  * don't know anything about GPT. We're interpreting
134                  * it a bit more loosely: any partition of type 0xEE
135                  * is to be skipped as it doesn't contain any data
136                  * that we should care about. We still allow other
137                  * partitions to be present in the MBR. A PMBR will
138                  * be handled correctly anyway.
139                  */
140                 if (dp[i].dp_typ == DOSPTYP_PMBR)
141                         l[i] = 0;
142                 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
143                         l[i] = 0;
144                 else if (dp[i].dp_typ == 0)
145                         l[i] = 0;
146                 else
147                         l[i] = (off_t)dp[i].dp_size * ms->sectorsize;
148                 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
149                     (off_t)dp[i].dp_start * ms->sectorsize, l[i],
150                     ms->sectorsize, "%ss%d", gp->name, 1 + i);
151                 if (error)
152                         return (error);
153         }
154         for (i = 0; i < NDOSPART; i++) {
155                 ms->type[i] = dp[i].dp_typ;
156                 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
157                     (off_t)dp[i].dp_start * ms->sectorsize, l[i],
158                     ms->sectorsize, "%ss%d", gp->name, 1 + i);
159         }
160         bcopy(sec0, ms->sec0, 512);
161
162         /*
163          * Calculate MD5 from the first sector and use it for avoiding
164          * recursive slices creation.
165          */
166         MD5Init(&md5sum);
167         MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
168         MD5Final(ms->slicesum, &md5sum);
169
170         return (0);
171 }
172
173 static int
174 g_mbr_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
175 {
176         struct g_geom *gp;
177         struct g_mbr_softc *ms;
178         struct g_slicer *gsp;
179         struct g_consumer *cp;
180         int error, opened;
181
182         gp = pp->geom;
183         gsp = gp->softc;
184         ms = gsp->softc;
185
186         opened = 0;
187         error = 0;
188         switch(cmd) {
189         case DIOCSMBR: {
190                 if (!(fflag & FWRITE))
191                         return (EPERM);
192                 DROP_GIANT();
193                 g_topology_lock();
194                 cp = LIST_FIRST(&gp->consumer);
195                 if (cp->acw == 0) {
196                         error = g_access(cp, 0, 1, 0);
197                         if (error == 0)
198                                 opened = 1;
199                 }
200                 if (!error)
201                         error = g_mbr_modify(gp, ms, data, 512);
202                 if (!error)
203                         error = g_write_data(cp, 0, data, 512);
204                 if (opened)
205                         g_access(cp, 0, -1 , 0);
206                 g_topology_unlock();
207                 PICKUP_GIANT();
208                 return(error);
209         }
210         default:
211                 return (ENOIOCTL);
212         }
213 }
214
215 static int
216 g_mbr_start(struct bio *bp)
217 {
218         struct g_provider *pp;
219         struct g_geom *gp;
220         struct g_mbr_softc *mp;
221         struct g_slicer *gsp;
222         int idx;
223
224         pp = bp->bio_to;
225         idx = pp->index;
226         gp = pp->geom;
227         gsp = gp->softc;
228         mp = gsp->softc;
229         if (bp->bio_cmd == BIO_GETATTR) {
230                 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
231                         return (1);
232                 if (g_handleattr_off_t(bp, "MBR::offset",
233                     gsp->slices[idx].offset))
234                         return (1);
235                 if (g_handleattr(bp, "MBR::slicesum", mp->slicesum,
236                     sizeof(mp->slicesum)))
237                         return (1);
238         }
239
240         return (0);
241 }
242
243 static void
244 g_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
261 static struct g_geom *
262 g_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         u_char hash[16];
271         MD5_CTX md5sum;
272
273         g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
274         g_topology_assert();
275         if (!strcmp(pp->geom->class->name, MBR_CLASS_NAME))
276                 return (NULL);
277         gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
278         if (gp == NULL)
279                 return (NULL);
280         g_topology_unlock();
281         do {
282                 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
283                 if (error)
284                         fwsectors = 17;
285                 sectorsize = cp->provider->sectorsize;
286                 if (sectorsize < 512)
287                         break;
288                 ms->sectorsize = sectorsize;
289                 buf = g_read_data(cp, 0, sectorsize, NULL);
290                 if (buf == NULL)
291                         break;
292
293                 /*
294                  * Calculate MD5 from the first sector and use it for avoiding
295                  * recursive slices creation.
296                  */
297                 bcopy(buf, ms->sec0, 512);
298                 MD5Init(&md5sum);
299                 MD5Update(&md5sum, ms->sec0, sizeof(ms->sec0));
300                 MD5Final(ms->slicesum, &md5sum);
301
302                 error = g_getattr("MBR::slicesum", cp, &hash);
303                 if (!error && !bcmp(ms->slicesum, hash, sizeof(hash))) {
304                         g_free(buf);
305                         break;
306                 }
307
308                 g_topology_lock();
309                 g_mbr_modify(gp, ms, buf, 512);
310                 g_topology_unlock();
311                 g_free(buf);
312                 break;
313         } while (0);
314         g_topology_lock();
315         g_access(cp, -1, 0, 0);
316         if (LIST_EMPTY(&gp->provider)) {
317                 g_slice_spoiled(cp);
318                 return (NULL);
319         }
320         return (gp);
321 }
322
323 static void
324 g_mbr_config(struct gctl_req *req, struct g_class *mp, const char *verb)
325 {
326         struct g_geom *gp;
327         struct g_consumer *cp;
328         struct g_mbr_softc *ms;
329         struct g_slicer *gsp;
330         int opened = 0, error = 0;
331         void *data;
332         int len;
333
334         g_topology_assert();
335         gp = gctl_get_geom(req, mp, "geom");
336         if (gp == NULL)
337                 return;
338         if (strcmp(verb, "write MBR")) {
339                 gctl_error(req, "Unknown verb");
340                 return;
341         }
342         gsp = gp->softc;
343         ms = gsp->softc;
344         data = gctl_get_param(req, "data", &len);
345         if (data == NULL)
346                 return;
347         if (len < 512 || (len % 512)) {
348                 gctl_error(req, "Wrong request length");
349                 return;
350         }
351         cp = LIST_FIRST(&gp->consumer);
352         if (cp->acw == 0) {
353                 error = g_access(cp, 0, 1, 0);
354                 if (error == 0)
355                         opened = 1;
356         }
357         if (!error)
358                 error = g_mbr_modify(gp, ms, data, len);
359         if (error)
360                 gctl_error(req, "conflict with open slices");
361         if (!error)
362                 error = g_write_data(cp, 0, data, len);
363         if (error)
364                 gctl_error(req, "sector zero write failed");
365         if (opened)
366                 g_access(cp, 0, -1 , 0);
367         return;
368 }
369
370 static struct g_class g_mbr_class       = {
371         .name = MBR_CLASS_NAME,
372         .version = G_VERSION,
373         .taste = g_mbr_taste,
374         .dumpconf = g_mbr_dumpconf,
375         .ctlreq = g_mbr_config,
376         .ioctl = g_mbr_ioctl,
377 };
378
379 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
380
381 #define NDOSEXTPART             32
382 struct g_mbrext_softc {
383         int             type [NDOSEXTPART];
384 };
385
386 static int
387 g_mbrext_start(struct bio *bp)
388 {
389         struct g_provider *pp;
390         struct g_geom *gp;
391         struct g_mbrext_softc *mp;
392         struct g_slicer *gsp;
393         int idx;
394
395         pp = bp->bio_to;
396         idx = pp->index;
397         gp = pp->geom;
398         gsp = gp->softc;
399         mp = gsp->softc;
400         if (bp->bio_cmd == BIO_GETATTR) {
401                 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
402                         return (1);
403         }
404         return (0);
405 }
406
407 static void
408 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
409 {
410         struct g_mbrext_softc *mp;
411         struct g_slicer *gsp;
412
413         g_slice_dumpconf(sb, indent, gp, cp, pp);
414         gsp = gp->softc;
415         mp = gsp->softc;
416         if (pp != NULL) {
417                 if (indent == NULL)
418                         sbuf_printf(sb, " ty %d", mp->type[pp->index]);
419                 else
420                         sbuf_printf(sb, "%s<type>%d</type>\n", indent,
421                             mp->type[pp->index]);
422         }
423 }
424
425 static struct g_geom *
426 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
427 {
428         struct g_geom *gp;
429         struct g_consumer *cp;
430         int error, i, slice;
431         struct g_mbrext_softc *ms;
432         off_t off;
433         u_char *buf;
434         struct dos_partition dp[4];
435         u_int fwsectors, sectorsize;
436
437         g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
438         g_topology_assert();
439         if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
440                 return (NULL);
441         gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms,
442             g_mbrext_start);
443         if (gp == NULL)
444                 return (NULL);
445         g_topology_unlock();
446         off = 0;
447         slice = 0;
448         do {
449                 error = g_getattr("MBR::type", cp, &i);
450                 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
451                         break;
452                 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
453                 if (error)
454                         fwsectors = 17;
455                 sectorsize = cp->provider->sectorsize;
456                 if (sectorsize != 512)
457                         break;
458                 for (;;) {
459                         buf = g_read_data(cp, off, sectorsize, NULL);
460                         if (buf == NULL)
461                                 break;
462                         if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) {
463                                 g_free(buf);
464                                 break;
465                         }
466                         for (i = 0; i < NDOSPART; i++) 
467                                 dos_partition_dec(
468                                     buf + DOSPARTOFF + 
469                                     i * sizeof(struct dos_partition), dp + i);
470                         g_free(buf);
471                         if (0 && bootverbose) {
472                                 printf("MBREXT Slice %d on %s:\n",
473                                     slice + 5, gp->name);
474                                 g_mbr_print(0, dp);
475                                 g_mbr_print(1, dp + 1);
476                         }
477                         if ((dp[0].dp_flag & 0x7f) == 0 &&
478                              dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
479                                 g_topology_lock();
480                                 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
481                                     (((off_t)dp[0].dp_start) << 9ULL) + off,
482                                     ((off_t)dp[0].dp_size) << 9ULL,
483                                     sectorsize,
484                                     "%*.*s%d",
485                                     strlen(gp->name) - 1,
486                                     strlen(gp->name) - 1,
487                                     gp->name,
488                                     slice + 5);
489                                 g_topology_unlock();
490                                 ms->type[slice] = dp[0].dp_typ;
491                                 slice++;
492                         }
493                         if (dp[1].dp_flag != 0)
494                                 break;
495                         if (dp[1].dp_typ != DOSPTYP_EXT &&
496                             dp[1].dp_typ != DOSPTYP_EXTLBA)
497                                 break;
498                         if (dp[1].dp_size == 0)
499                                 break;
500                         off = ((off_t)dp[1].dp_start) << 9ULL;
501                 }
502                 break;
503         } while (0);
504         g_topology_lock();
505         g_access(cp, -1, 0, 0);
506         if (LIST_EMPTY(&gp->provider)) {
507                 g_slice_spoiled(cp);
508                 return (NULL);
509         }
510         return (gp);
511 }
512
513
514 static struct g_class g_mbrext_class    = {
515         .name = MBREXT_CLASS_NAME,
516         .version = G_VERSION,
517         .taste = g_mbrext_taste,
518         .dumpconf = g_mbrext_dumpconf,
519 };
520
521 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);