The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

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

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  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  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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  * $FreeBSD: releng/5.0/sys/geom/geom_mbr.c 108569 2003-01-02 20:16:17Z phk $
   33  */
   34 
   35 #include <sys/param.h>
   36 #include <sys/errno.h>
   37 #ifndef _KERNEL
   38 #include <stdio.h>
   39 #include <string.h>
   40 #include <signal.h>
   41 #include <sys/param.h>
   42 #include <stdlib.h>
   43 #include <err.h>
   44 #else
   45 #include <sys/systm.h>
   46 #include <sys/kernel.h>
   47 #include <sys/malloc.h>
   48 #include <sys/bio.h>
   49 #include <sys/lock.h>
   50 #include <sys/mutex.h>
   51 #endif
   52 
   53 #include <sys/diskmbr.h>
   54 #include <sys/sbuf.h>
   55 #include <geom/geom.h>
   56 #include <geom/geom_slice.h>
   57 
   58 #define MBR_CLASS_NAME "MBR"
   59 #define MBREXT_CLASS_NAME "MBREXT"
   60 
   61 static struct dos_partition historical_bogus_partition_table[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, 255, 255, 255, 0, 50000, },
   66 };
   67 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
   68         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
   69         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
   70         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
   71         { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
   72 };
   73 static void
   74 g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
   75 {
   76 
   77         d->dp_flag = ptr[0];
   78         d->dp_shd = ptr[1];
   79         d->dp_ssect = ptr[2];
   80         d->dp_scyl = ptr[3];
   81         d->dp_typ = ptr[4];
   82         d->dp_ehd = ptr[5];
   83         d->dp_esect = ptr[6];
   84         d->dp_ecyl = ptr[7];
   85         d->dp_start = g_dec_le4(ptr + 8);
   86         d->dp_size = g_dec_le4(ptr + 12);
   87 }
   88 
   89 #if 0
   90 static void
   91 g_enc_dos_partition(u_char *ptr, struct dos_partition *d)
   92 {
   93 
   94         ptr[0] = d->dp_flag;
   95         ptr[1] = d->dp_shd;
   96         ptr[2] = d->dp_ssect;
   97         ptr[3] = d->dp_scyl;
   98         ptr[4] = d->dp_typ;
   99         ptr[5] = d->dp_ehd;
  100         ptr[6] = d->dp_esect;
  101         ptr[7] = d->dp_ecyl;
  102         g_enc_le4(ptr + 8, d->dp_start);
  103         g_enc_le4(ptr + 12, d->dp_size);
  104 }
  105 #endif
  106 
  107 struct g_mbr_softc {
  108         int             type [NDOSPART];
  109         u_char          sec0[512];
  110 };
  111 
  112 static int
  113 g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0)
  114 {
  115         int i, error;
  116         off_t l[NDOSPART];
  117         struct dos_partition ndp[NDOSPART], *dp;
  118 
  119         g_topology_assert();
  120 
  121         if (sec0[0x1fe] != 0x55 && sec0[0x1ff] != 0xaa)
  122                 return (EBUSY);
  123 
  124         dp = ndp;
  125         for (i = 0; i < NDOSPART; i++) 
  126                 g_dec_dos_partition(
  127                     sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
  128                     dp + i);
  129         if ((!bcmp(dp, historical_bogus_partition_table,
  130             sizeof historical_bogus_partition_table)) ||
  131             (!bcmp(dp, historical_bogus_partition_table_fixed,
  132             sizeof historical_bogus_partition_table_fixed))) {
  133                 /*
  134                  * We will not allow people to write these from "the inside",
  135                  * Since properly selfdestructing takes too much code.  If 
  136                  * people really want to do this, they cannot have any
  137                  * providers of this geom open, and in that case they can just
  138                  * as easily overwrite the MBR in the parent device.
  139                  */
  140                 return(EBUSY);
  141         }
  142         for (i = 0; i < NDOSPART; i++) {
  143                 /* 
  144                  * A Protective MBR (PMBR) has a single partition of
  145                  * type 0xEE spanning the whole disk. Such a MBR
  146                  * protects a GPT on the disk from MBR tools that
  147                  * don't know anything about GPT. We're interpreting
  148                  * it a bit more loosely: any partition of type 0xEE
  149                  * is to be skipped as it doesn't contain any data
  150                  * that we should care about. We still allow other
  151                  * partitions to be present in the MBR. A PMBR will
  152                  * be handled correctly anyway.
  153                  */
  154                 if (dp[i].dp_typ == 0xee)
  155                         l[i] = 0;
  156                 else if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80)
  157                         l[i] = 0;
  158                 else if (dp[i].dp_typ == 0)
  159                         l[i] = 0;
  160                 else
  161                         l[i] = (off_t)dp[i].dp_size << 9ULL;
  162                 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
  163                     (off_t)dp[i].dp_start << 9ULL, l[i], 512,
  164                     "%ss%d", gp->name, 1 + i);
  165                 if (error)
  166                         return (error);
  167         }
  168         for (i = 0; i < NDOSPART; i++) {
  169                 ms->type[i] = dp[i].dp_typ;
  170                 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  171                     (off_t)dp[i].dp_start << 9ULL, l[i], 512,
  172                     "%ss%d", gp->name, 1 + i);
  173         }
  174         bcopy(sec0, ms->sec0, 512);
  175         return (0);
  176 }
  177 
  178 static void
  179 g_mbr_ioctl(void *arg)
  180 {
  181         struct bio *bp;
  182         struct g_geom *gp;
  183         struct g_slicer *gsp;
  184         struct g_mbr_softc *ms;
  185         struct g_ioctl *gio;
  186         struct g_consumer *cp;
  187         u_char *sec0;
  188         int error;
  189 
  190         /* Get hold of the interesting bits from the bio. */
  191         bp = arg;
  192         gp = bp->bio_to->geom;
  193         gsp = gp->softc;
  194         ms = gsp->softc;
  195         gio = (struct g_ioctl *)bp->bio_data;
  196 
  197         /* The disklabel to set is the ioctl argument. */
  198         sec0 = gio->data;
  199 
  200         error = g_mbr_modify(gp, ms, sec0);
  201         if (error) {
  202                 g_io_deliver(bp, error);
  203                 return;
  204         }
  205         cp = LIST_FIRST(&gp->consumer);
  206         error = g_write_data(cp, 0, sec0, 512);
  207         g_io_deliver(bp, error);
  208 }
  209 
  210 
  211 static int
  212 g_mbr_start(struct bio *bp)
  213 {
  214         struct g_provider *pp;
  215         struct g_geom *gp;
  216         struct g_mbr_softc *mp;
  217         struct g_slicer *gsp;
  218         struct g_ioctl *gio;
  219         int idx, error;
  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         }
  233 
  234         /* We only handle ioctl(2) requests of the right format. */
  235         if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
  236                 return (0);
  237         else if (bp->bio_length != sizeof(*gio))
  238                 return (0);
  239 
  240         /* Get hold of the ioctl parameters. */
  241         gio = (struct g_ioctl *)bp->bio_data;
  242 
  243         switch (gio->cmd) {
  244         case DIOCGMBR:
  245                 /* Return a copy of the disklabel to userland. */
  246                 bcopy(mp->sec0, gio->data, 512);
  247                 g_io_deliver(bp, 0);
  248                 return (1);
  249         case DIOCSMBR:
  250                 /*
  251                  * These we cannot do without the topology lock and some
  252                  * some I/O requests.  Ask the event-handler to schedule
  253                  * us in a less restricted environment.
  254                  */
  255                 error = g_call_me(g_mbr_ioctl, bp);
  256                 if (error)
  257                         g_io_deliver(bp, error);
  258                 /*
  259                  * We must return non-zero to indicate that we will deal
  260                  * with this bio, even though we have not done so yet.
  261                  */
  262                 return (1);
  263         default:
  264                 return (0);
  265         }
  266 
  267         return (0);
  268 }
  269 
  270 static void
  271 g_mbr_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
  272 {
  273         struct g_mbr_softc *mp;
  274         struct g_slicer *gsp;
  275 
  276         gsp = gp->softc;
  277         mp = gsp->softc;
  278         g_slice_dumpconf(sb, indent, gp, cp, pp);
  279         if (pp != NULL) {
  280                 if (indent == NULL)
  281                         sbuf_printf(sb, " ty %d", mp->type[pp->index]);
  282                 else
  283                         sbuf_printf(sb, "%s<type>%d</type>\n", indent,
  284                             mp->type[pp->index]);
  285         }
  286 }
  287 
  288 
  289 
  290 static void
  291 g_mbr_print(int i, struct dos_partition *dp)
  292 {
  293 
  294         g_hexdump(dp, sizeof(dp[0]));
  295         printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
  296         printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
  297         printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
  298         printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
  299 }
  300 
  301 static struct g_geom *
  302 g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist)
  303 {
  304         struct g_geom *gp;
  305         struct g_consumer *cp;
  306         int error;
  307         struct g_mbr_softc *ms;
  308         struct g_slicer *gsp;
  309         u_int fwsectors, sectorsize;
  310         u_char *buf;
  311 
  312         g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
  313         g_topology_assert();
  314         gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
  315         if (gp == NULL)
  316                 return (NULL);
  317         gsp = gp->softc;
  318         g_topology_unlock();
  319         gp->dumpconf = g_mbr_dumpconf;
  320         while (1) {     /* a trick to allow us to use break */
  321                 if (gp->rank != 2 && insist == 0)
  322                         break;
  323                 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
  324                 if (error)
  325                         fwsectors = 17;
  326                 sectorsize = cp->provider->sectorsize;
  327                 if (sectorsize != 512)
  328                         break;
  329                 gsp->frontstuff = sectorsize * fwsectors;
  330                 buf = g_read_data(cp, 0, sectorsize, &error);
  331                 if (buf == NULL || error != 0)
  332                         break;
  333                 g_topology_lock();
  334                 g_mbr_modify(gp, ms, buf);
  335                 g_topology_unlock();
  336                 g_free(buf);
  337                 break;
  338         }
  339         g_topology_lock();
  340         g_access_rel(cp, -1, 0, 0);
  341         if (LIST_EMPTY(&gp->provider)) {
  342                 g_std_spoiled(cp);
  343                 return (NULL);
  344         }
  345         return (gp);
  346 }
  347 
  348 static struct g_class g_mbr_class       = {
  349         MBR_CLASS_NAME,
  350         g_mbr_taste,
  351         NULL,
  352         G_CLASS_INITIALIZER
  353 };
  354 
  355 DECLARE_GEOM_CLASS(g_mbr_class, g_mbr);
  356 
  357 #define NDOSEXTPART             32
  358 struct g_mbrext_softc {
  359         int             type [NDOSEXTPART];
  360 };
  361 
  362 static int
  363 g_mbrext_start(struct bio *bp)
  364 {
  365         struct g_provider *pp;
  366         struct g_geom *gp;
  367         struct g_mbrext_softc *mp;
  368         struct g_slicer *gsp;
  369         int idx;
  370 
  371         pp = bp->bio_to;
  372         idx = pp->index;
  373         gp = pp->geom;
  374         gsp = gp->softc;
  375         mp = gsp->softc;
  376         if (bp->bio_cmd == BIO_GETATTR) {
  377                 if (g_handleattr_int(bp, "MBR::type", mp->type[idx]))
  378                         return (1);
  379         }
  380         return (0);
  381 }
  382 
  383 static void
  384 g_mbrext_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
  385 {
  386         struct g_mbrext_softc *mp;
  387         struct g_slicer *gsp;
  388 
  389         g_slice_dumpconf(sb, indent, gp, cp, pp);
  390         gsp = gp->softc;
  391         mp = gsp->softc;
  392         if (pp != NULL) {
  393                 if (indent == NULL)
  394                         sbuf_printf(sb, " ty %d", mp->type[pp->index]);
  395                 else
  396                         sbuf_printf(sb, "%s<type>%d</type>\n", indent,
  397                             mp->type[pp->index]);
  398         }
  399 }
  400 
  401 static struct g_geom *
  402 g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
  403 {
  404         struct g_geom *gp;
  405         struct g_consumer *cp;
  406         int error, i, slice;
  407         struct g_mbrext_softc *ms;
  408         off_t off;
  409         u_char *buf;
  410         struct dos_partition dp[4];
  411         u_int fwsectors, sectorsize;
  412         struct g_slicer *gsp;
  413 
  414         g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
  415         g_topology_assert();
  416         if (strcmp(pp->geom->class->name, MBR_CLASS_NAME))
  417                 return (NULL);
  418         gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
  419         if (gp == NULL)
  420                 return (NULL);
  421         gsp = gp->softc;
  422         g_topology_unlock();
  423         gp->dumpconf = g_mbrext_dumpconf;
  424         off = 0;
  425         slice = 0;
  426         while (1) {     /* a trick to allow us to use break */
  427                 error = g_getattr("MBR::type", cp, &i);
  428                 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA))
  429                         break;
  430                 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
  431                 if (error)
  432                         fwsectors = 17;
  433                 sectorsize = cp->provider->sectorsize;
  434                 if (sectorsize != 512)
  435                         break;
  436                 gsp->frontstuff = sectorsize * fwsectors;
  437                 for (;;) {
  438                         buf = g_read_data(cp, off, sectorsize, &error);
  439                         if (buf == NULL || error != 0)
  440                                 break;
  441                         if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
  442                                 break;
  443                         for (i = 0; i < NDOSPART; i++) 
  444                                 g_dec_dos_partition(
  445                                     buf + DOSPARTOFF + 
  446                                     i * sizeof(struct dos_partition), dp + i);
  447                         g_free(buf);
  448                         printf("MBREXT Slice %d on %s:\n", slice + 5, gp->name);
  449                         g_mbr_print(0, dp);
  450                         g_mbr_print(1, dp + 1);
  451                         if ((dp[0].dp_flag & 0x7f) == 0 &&
  452                              dp[0].dp_size != 0 && dp[0].dp_typ != 0) {
  453                                 g_topology_lock();
  454                                 g_slice_config(gp, slice, G_SLICE_CONFIG_SET,
  455                                     (((off_t)dp[0].dp_start) << 9ULL) + off,
  456                                     ((off_t)dp[0].dp_size) << 9ULL,
  457                                     sectorsize,
  458                                     "%*.*s%d",
  459                                     strlen(gp->name) - 1,
  460                                     strlen(gp->name) - 1,
  461                                     gp->name,
  462                                     slice + 5);
  463                                 g_topology_unlock();
  464                                 ms->type[slice] = dp[0].dp_typ;
  465                                 slice++;
  466                         }
  467                         if (dp[1].dp_flag != 0)
  468                                 break;
  469                         if (dp[1].dp_typ != DOSPTYP_EXT)
  470                                 break;
  471                         if (dp[1].dp_size == 0)
  472                                 break;
  473                         off = ((off_t)dp[1].dp_start) << 9ULL;
  474                 }
  475                 break;
  476         }
  477         g_topology_lock();
  478         g_access_rel(cp, -1, 0, 0);
  479         if (LIST_EMPTY(&gp->provider)) {
  480                 g_std_spoiled(cp);
  481                 return (NULL);
  482         }
  483         return (gp);
  484 }
  485 
  486 
  487 static struct g_class g_mbrext_class    = {
  488         MBREXT_CLASS_NAME,
  489         g_mbrext_taste,
  490         NULL,
  491         G_CLASS_INITIALIZER
  492 };
  493 
  494 DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);

Cache object: 24dde08011c4705db7adc843ebaf08a8


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


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