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_gpt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2002 Marcel Moolenaar
    3  * Copyright (c) 2002 Poul-Henning Kamp
    4  * Copyright (c) 2002 Networks Associates Technology, Inc.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. The names of the authors may not be used to endorse or promote
   16  *    products derived from this software without specific prior written
   17  *    permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  * $FreeBSD: releng/5.0/sys/geom/geom_gpt.c 108141 2002-12-20 21:52:03Z phk $
   32  */
   33 
   34 #include <sys/param.h>
   35 #ifndef _KERNEL
   36 #include <stdio.h>
   37 #include <string.h>
   38 #include <signal.h>
   39 #include <sys/param.h>
   40 #include <stdlib.h>
   41 #include <err.h>
   42 #else
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/malloc.h>
   46 #include <sys/bio.h>
   47 #include <sys/lock.h>
   48 #include <sys/mutex.h>
   49 #endif
   50 
   51 #include <sys/endian.h>
   52 #include <sys/sbuf.h>
   53 #include <sys/uuid.h>
   54 #include <sys/gpt.h>
   55 #include <geom/geom.h>
   56 #include <geom/geom_slice.h>
   57 
   58 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
   59 CTASSERT(sizeof(struct gpt_ent) == 128);
   60 
   61 /*
   62  * XXX: GEOM is not dynamic enough. We are forced to use a compile-time
   63  * limit. The minimum number of partitions (128) as required by EFI is
   64  * most of the time just a waste of space.
   65  */
   66 #define GPT_MAX_SLICES  128
   67 
   68 struct g_gpt_softc {
   69         struct gpt_ent *part[GPT_MAX_SLICES];
   70 };
   71 
   72 static int
   73 is_gpt_hdr(struct gpt_hdr *hdr)
   74 {
   75         uint32_t crc;
   76 
   77         if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)))
   78                 return (0);
   79         crc = hdr->hdr_crc_self;
   80         hdr->hdr_crc_self = 0;
   81         if (crc32(hdr, hdr->hdr_size) != crc)
   82                 return (0);
   83         hdr->hdr_crc_self = crc;
   84         /* We're happy... */
   85         return (1);
   86 }
   87 
   88 static int
   89 g_gpt_start(struct bio *bp)
   90 {
   91         struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
   92         struct g_provider *pp = bp->bio_to;
   93         struct g_geom *gp = pp->geom;
   94         struct g_slicer *gsp = gp->softc;
   95         struct g_gpt_softc *gs = gsp->softc;
   96         u_int type;
   97 
   98         if (bp->bio_cmd != BIO_GETATTR)
   99                 return (0);
  100 
  101         /*
  102          * XXX: this is bogus. The BSD class has a strong dependency on
  103          * the MBR/MBREXT class, because it asks for an attribute that's
  104          * specific to the MBR/MBREXT class and the value of the attribute
  105          * is just as specific to the MBR class. In an extensible scheme
  106          * a geom would ask another geom if it could possible accomodate a
  107          * class and the answer should be yes or no. Now we're forced to
  108          * emulate a MBR class :-/
  109          */
  110         type = (memcmp(&gs->part[pp->index]->ent_type, &freebsd,
  111             sizeof(freebsd))) ? 0 : 165;
  112         return ((g_handleattr_int(bp, "MBR::type", type)) ? 1 : 0);
  113 }
  114 
  115 static void
  116 g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
  117     struct g_consumer *cp, struct g_provider *pp)
  118 {
  119         struct g_slicer *gsp = gp->softc;
  120         struct g_gpt_softc *gs = gsp->softc;
  121         struct uuid *uuid;
  122 
  123         g_slice_dumpconf(sb, indent, gp, cp, pp);
  124 
  125         if (pp != NULL) {
  126                 uuid = &gs->part[pp->index]->ent_type;
  127                 if (indent != NULL)
  128                         sbuf_printf(sb, "%s<type>", indent);
  129                 else
  130                         sbuf_printf(sb, " ty ");
  131                 sbuf_printf_uuid(sb, uuid);
  132                 if (indent != NULL)
  133                         sbuf_printf(sb, "</type>\n");
  134         }
  135 }
  136 
  137 static struct g_geom *
  138 g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist)
  139 {
  140         struct g_consumer *cp;
  141         struct g_geom *gp;
  142         struct g_gpt_softc *gs;
  143         struct g_slicer *gsp;
  144         u_char *buf, *mbr;
  145         struct gpt_ent *ent;
  146         struct gpt_hdr *hdr;
  147         u_int i, npart, secsz, tblsz;
  148         int error, ps;
  149 
  150         g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
  151         g_topology_assert();
  152 
  153         /*
  154          * XXX: I don't like to hardcode a maximum number of slices, since
  155          * it's wasting space most of the time and insufficient any time.
  156          * It's easier for now...
  157          */
  158         gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
  159             g_gpt_start);
  160         if (gp == NULL)
  161                 return (NULL);
  162 
  163         gsp = gp->softc;
  164         g_topology_unlock();
  165         gp->dumpconf = g_gpt_dumpconf;
  166 
  167         do {
  168 
  169                 npart = 0;
  170                 mbr = NULL;
  171 
  172                 if (gp->rank != 2 && insist == 0)
  173                         break;
  174 
  175                 secsz = cp->provider->sectorsize;
  176                 if (secsz < 512)
  177                         break;
  178 
  179                 /* XXX: we need to get the media size as well. */
  180 
  181                 /* Read both the MBR sector and the GPT sector. */
  182                 mbr = g_read_data(cp, 0, 2 * secsz, &error);
  183                 if (mbr == NULL || error != 0)
  184                         break;
  185 #if 0
  186         /*
  187          * XXX: we should ignore the GPT if there's a MBR and the MBR is
  188          * not a PMBR (Protective MBR). I believe this is what the EFI
  189          * spec is going to say eventually (this is hearsay :-)
  190          * Currently EFI (version 1.02) accepts and uses the GPT even
  191          * though there's a valid MBR. We do this too, because it allows
  192          * us to test this code without first nuking the only partitioning
  193          * scheme we grok until this is working.
  194          */
  195         if (!is_pmbr((void*)mbr))
  196                 goto out;
  197 #endif
  198 
  199                 hdr = (void*)(mbr + secsz);
  200 
  201         /*
  202          * XXX: if we don't have a GPT header at LBA 1, we should check if
  203          * there's a backup GPT at the end of the medium. If we have a valid
  204          * backup GPT, we should restore the primary GPT and claim this lunch.
  205          */
  206                 if (!is_gpt_hdr(hdr))
  207                         break;
  208 
  209                 tblsz = (hdr->hdr_entries * hdr->hdr_entsz + secsz - 1) &
  210                     ~(secsz - 1);
  211                 buf = g_read_data(cp, hdr->hdr_lba_table * secsz, tblsz, &error);
  212 
  213                 gsp->frontstuff = hdr->hdr_lba_start * secsz;
  214 
  215                 for (i = 0; i < hdr->hdr_entries; i++) {
  216                         struct uuid unused = GPT_ENT_TYPE_UNUSED;
  217                         struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
  218                         if (i >= GPT_MAX_SLICES)
  219                                 break;
  220                         ent = (void*)(buf + i * hdr->hdr_entsz);
  221                         if (!memcmp(&ent->ent_type, &unused, sizeof(unused)))
  222                                 continue;
  223                         gs->part[i] = g_malloc(hdr->hdr_entsz, M_WAITOK);
  224                         if (gs->part[i] == NULL)
  225                                 break;
  226                         bcopy(ent, gs->part[i], hdr->hdr_entsz);
  227                         ps = (!memcmp(&ent->ent_type, &freebsd, sizeof(freebsd)))
  228                             ? 's' : 'p';
  229                         g_topology_lock();
  230                         (void)g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  231                             ent->ent_lba_start * secsz,
  232                             (1 + ent->ent_lba_end - ent->ent_lba_start) * secsz,
  233                             secsz,
  234                             "%s%c%d", gp->name, ps, i + 1);
  235                         g_topology_unlock();
  236                         npart++;
  237                 }
  238                 g_free(buf);
  239 
  240         } while (0);
  241 
  242 
  243         if (mbr != NULL)
  244                 g_free(mbr);
  245 
  246         g_topology_lock();
  247         g_access_rel(cp, -1, 0, 0);
  248         if (LIST_EMPTY(&gp->provider)) {
  249                 g_std_spoiled(cp);
  250                 return (NULL);
  251         }
  252         return (gp);
  253 }
  254 
  255 static struct g_class g_gpt_class = {
  256         "GPT",
  257         g_gpt_taste,
  258         NULL,
  259         G_CLASS_INITIALIZER
  260 };
  261 
  262 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);

Cache object: 5a19793835d75f5aa229897a9b32b951


[ 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.