[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_mbr.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

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

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.