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 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 #include <sys/kernel.h>
   38 #include <sys/malloc.h>
   39 #include <sys/bio.h>
   40 #include <sys/lock.h>
   41 #include <sys/mutex.h>
   42 #include <sys/diskmbr.h>
   43 #include <sys/endian.h>
   44 #include <sys/sbuf.h>
   45 #include <sys/uuid.h>
   46 #include <sys/gpt.h>
   47 #include <geom/geom.h>
   48 #include <geom/geom_slice.h>
   49 
   50 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
   51 CTASSERT(sizeof(struct gpt_ent) == 128);
   52 
   53 /*
   54  * XXX: GEOM is not dynamic enough. We are forced to use a compile-time
   55  * limit. The minimum number of partitions (128) as required by EFI is
   56  * most of the time just a waste of space.
   57  */
   58 #define GPT_MAX_SLICES  128
   59 
   60 struct g_gpt_softc {
   61         struct gpt_ent *part[GPT_MAX_SLICES];
   62 };
   63 
   64 static int
   65 is_gpt_hdr(struct gpt_hdr *hdr)
   66 {
   67         uint32_t crc;
   68 
   69         if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)))
   70                 return (0);
   71         crc = le32toh(hdr->hdr_crc_self);
   72         hdr->hdr_crc_self = 0;
   73         if (crc32(hdr, le32toh(hdr->hdr_size)) != crc)
   74                 return (0);
   75         hdr->hdr_crc_self = htole32(crc);
   76         /* We're happy... */
   77         return (1);
   78 }
   79 
   80 static int
   81 is_pmbr(char *mbr)
   82 {
   83         uint8_t *typ;
   84         int i;
   85         uint16_t magic;
   86 
   87         magic = le16toh(*(uint16_t *)(uintptr_t)(mbr + DOSMAGICOFFSET));
   88         if (magic != DOSMAGIC)
   89                 return (0);
   90 
   91         for (i = 0; i < 4; i++) {
   92                 typ = mbr + DOSPARTOFF + i * sizeof(struct dos_partition) +
   93                     offsetof(struct dos_partition, dp_typ);
   94                 if (*typ != 0 && *typ != DOSPTYP_PMBR)
   95                         return (0);
   96         }
   97 
   98         return (1);
   99 }
  100 
  101 static int
  102 g_gpt_start(struct bio *bp)
  103 {
  104         struct g_provider *pp;
  105         struct g_geom *gp;
  106         struct g_gpt_softc *gs;
  107         struct g_slicer *gsp;
  108         struct uuid uuid;
  109 
  110         pp = bp->bio_to;
  111         gp = pp->geom;
  112         gsp = gp->softc;
  113         gs = gsp->softc;
  114 
  115         if (bp->bio_cmd == BIO_GETATTR) {
  116                 le_uuid_dec(&gs->part[pp->index]->ent_type, &uuid);
  117                 if (g_handleattr(bp, "GPT::type", &uuid, sizeof(uuid)))
  118                         return (1);
  119         }
  120         
  121         return (0);
  122 }
  123 
  124 static void
  125 g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
  126     struct g_consumer *cp, struct g_provider *pp)
  127 {
  128         struct g_slicer *gsp = gp->softc;
  129         struct g_gpt_softc *gs = gsp->softc;
  130         struct uuid uuid;
  131 
  132         g_slice_dumpconf(sb, indent, gp, cp, pp);
  133 
  134         if (pp != NULL) {
  135                 le_uuid_dec(&gs->part[pp->index]->ent_type, &uuid);
  136                 if (indent != NULL)
  137                         sbuf_printf(sb, "%s<type>", indent);
  138                 else
  139                         sbuf_printf(sb, " ty ");
  140                 sbuf_printf_uuid(sb, &uuid);
  141                 if (indent != NULL)
  142                         sbuf_printf(sb, "</type>\n");
  143         }
  144 }
  145 
  146 static struct g_geom *
  147 g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
  148 {
  149         struct uuid tmp;
  150         struct g_consumer *cp;
  151         struct g_geom *gp;
  152         struct g_gpt_softc *gs;
  153         u_char *buf, *mbr;
  154         struct gpt_ent *ent, *part;
  155         struct gpt_hdr *hdr;
  156         u_int i, secsz, tblsz;
  157         int ps;
  158         uint32_t entries, entsz;
  159 
  160         g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
  161         g_topology_assert();
  162 
  163         /*
  164          * XXX: I don't like to hardcode a maximum number of slices, since
  165          * it's wasting space most of the time and insufficient any time.
  166          * It's easier for now...
  167          */
  168         gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
  169             g_gpt_start);
  170         if (gp == NULL)
  171                 return (NULL);
  172 
  173         g_topology_unlock();
  174 
  175         do {
  176                 mbr = NULL;
  177 
  178                 secsz = cp->provider->sectorsize;
  179                 if (secsz < 512)
  180                         break;
  181 
  182                 /* XXX: we need to get the media size as well. */
  183 
  184                 /* Read both the MBR sector and the GPT sector. */
  185                 mbr = g_read_data(cp, 0, 2 * secsz, NULL);
  186                 if (mbr == NULL)
  187                         break;
  188 
  189                 if (!is_pmbr(mbr))
  190                         break;
  191 
  192                 hdr = (void*)(mbr + secsz);
  193 
  194                 /*
  195                  * XXX: if we don't have a GPT header at LBA 1, we should
  196                  * check if there's a backup GPT at the end of the medium. If
  197                  * we have a valid backup GPT, we should restore the primary
  198                  * GPT and claim this lunch.
  199                  */
  200                 if (!is_gpt_hdr(hdr))
  201                         break;
  202 
  203                 entries = le32toh(hdr->hdr_entries);
  204                 entsz = le32toh(hdr->hdr_entsz);
  205                 tblsz = (entries * entsz + secsz - 1) & ~(secsz - 1);
  206                 buf = g_read_data(cp, le64toh(hdr->hdr_lba_table) * secsz,
  207                     tblsz, NULL);
  208                 if (buf == NULL)
  209                         break;
  210 
  211                 for (i = 0; i < entries; i++) {
  212                         struct uuid unused = GPT_ENT_TYPE_UNUSED;
  213                         struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
  214 
  215                         if (i >= GPT_MAX_SLICES)
  216                                 break;
  217                         ent = (void*)(buf + i * entsz);
  218                         le_uuid_dec(&ent->ent_type, &tmp);
  219                         if (!memcmp(&tmp, &unused, sizeof(unused)))
  220                                 continue;
  221                         /* XXX: This memory leaks */
  222                         part = gs->part[i] = g_malloc(entsz, M_WAITOK);
  223                         if (part == NULL)
  224                                 break;
  225                         part->ent_type = tmp;
  226                         le_uuid_dec(&ent->ent_uuid, &part->ent_uuid);
  227                         part->ent_lba_start = le64toh(ent->ent_lba_start);
  228                         part->ent_lba_end = le64toh(ent->ent_lba_end);
  229                         part->ent_attr = le64toh(ent->ent_attr);
  230                         /* XXX do we need to byte-swap UNICODE-16? */
  231                         bcopy(ent->ent_name, part->ent_name,
  232                             sizeof(part->ent_name));
  233                         ps = (!memcmp(&tmp, &freebsd, sizeof(freebsd)))
  234                             ? 's' : 'p';
  235                         g_topology_lock();
  236                         (void)g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  237                             part->ent_lba_start * secsz,
  238                             (1 + part->ent_lba_end - part->ent_lba_start) *
  239                             secsz, secsz, "%s%c%d", gp->name, ps, i + 1);
  240                         g_topology_unlock();
  241                 }
  242                 g_free(buf);
  243         } while (0);
  244 
  245         if (mbr != NULL)
  246                 g_free(mbr);
  247 
  248         g_topology_lock();
  249         g_access(cp, -1, 0, 0);
  250         if (LIST_EMPTY(&gp->provider)) {
  251                 g_slice_spoiled(cp);
  252                 return (NULL);
  253         }
  254         return (gp);
  255 }
  256 
  257 static struct g_class g_gpt_class = {
  258         .name = "GPT",
  259         .version = G_VERSION,
  260         .taste = g_gpt_taste,
  261         .dumpconf = g_gpt_dumpconf,
  262 };
  263 
  264 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);

Cache object: 55e274eeb851c009dc7267bb07b5866a


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