[ 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  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  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  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
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: src/sys/geom/geom_gpt.c,v 1.32.2.1 2005/12/29 05:59:51 sobomax Exp $");
   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         struct dos_partition *part;
   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         part = (struct dos_partition *)(uintptr_t)(mbr + DOSPARTOFF);
   92         for (i = 0; i < 4; i++) {
   93                 if (part[i].dp_typ != 0 && part[i].dp_typ != DOSPTYP_PMBR)
   94                         return (0);
   95         }
   96 
   97         return (1);
   98 }
   99 
  100 static int
  101 g_gpt_start(struct bio *bp)
  102 {
  103 
  104         return (0);
  105 }
  106 
  107 static void
  108 g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
  109     struct g_consumer *cp, struct g_provider *pp)
  110 {
  111         struct g_slicer *gsp = gp->softc;
  112         struct g_gpt_softc *gs = gsp->softc;
  113         struct uuid uuid;
  114 
  115         g_slice_dumpconf(sb, indent, gp, cp, pp);
  116 
  117         if (pp != NULL) {
  118                 le_uuid_dec(&gs->part[pp->index]->ent_type, &uuid);
  119                 if (indent != NULL)
  120                         sbuf_printf(sb, "%s<type>", indent);
  121                 else
  122                         sbuf_printf(sb, " ty ");
  123                 sbuf_printf_uuid(sb, &uuid);
  124                 if (indent != NULL)
  125                         sbuf_printf(sb, "</type>\n");
  126         }
  127 }
  128 
  129 static struct g_geom *
  130 g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
  131 {
  132         struct uuid tmp;
  133         struct g_consumer *cp;
  134         struct g_geom *gp;
  135         struct g_gpt_softc *gs;
  136         u_char *buf, *mbr;
  137         struct gpt_ent *ent, *part;
  138         struct gpt_hdr *hdr;
  139         u_int i, secsz, tblsz;
  140         int ps;
  141         uint32_t entries, entsz;
  142 
  143         g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
  144         g_topology_assert();
  145 
  146         /*
  147          * XXX: I don't like to hardcode a maximum number of slices, since
  148          * it's wasting space most of the time and insufficient any time.
  149          * It's easier for now...
  150          */
  151         gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
  152             g_gpt_start);
  153         if (gp == NULL)
  154                 return (NULL);
  155 
  156         g_topology_unlock();
  157 
  158         do {
  159                 mbr = NULL;
  160 
  161                 secsz = cp->provider->sectorsize;
  162                 if (secsz < 512)
  163                         break;
  164 
  165                 /* XXX: we need to get the media size as well. */
  166 
  167                 /* Read both the MBR sector and the GPT sector. */
  168                 mbr = g_read_data(cp, 0, 2 * secsz, NULL);
  169                 if (mbr == NULL)
  170                         break;
  171 
  172                 if (!is_pmbr(mbr))
  173                         break;
  174 
  175                 hdr = (void*)(mbr + secsz);
  176 
  177                 /*
  178                  * XXX: if we don't have a GPT header at LBA 1, we should
  179                  * check if there's a backup GPT at the end of the medium. If
  180                  * we have a valid backup GPT, we should restore the primary
  181                  * GPT and claim this lunch.
  182                  */
  183                 if (!is_gpt_hdr(hdr))
  184                         break;
  185 
  186                 entries = le32toh(hdr->hdr_entries);
  187                 entsz = le32toh(hdr->hdr_entsz);
  188                 tblsz = (entries * entsz + secsz - 1) & ~(secsz - 1);
  189                 buf = g_read_data(cp, le64toh(hdr->hdr_lba_table) * secsz,
  190                     tblsz, NULL);
  191                 if (buf == NULL)
  192                         break;
  193 
  194                 for (i = 0; i < entries; i++) {
  195                         struct uuid unused = GPT_ENT_TYPE_UNUSED;
  196                         struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
  197 
  198                         if (i >= GPT_MAX_SLICES)
  199                                 break;
  200                         ent = (void*)(buf + i * entsz);
  201                         le_uuid_dec(&ent->ent_type, &tmp);
  202                         if (!memcmp(&tmp, &unused, sizeof(unused)))
  203                                 continue;
  204                         /* XXX: This memory leaks */
  205                         part = gs->part[i] = g_malloc(entsz, M_WAITOK);
  206                         if (part == NULL)
  207                                 break;
  208                         part->ent_type = tmp;
  209                         le_uuid_dec(&ent->ent_uuid, &part->ent_uuid);
  210                         part->ent_lba_start = le64toh(ent->ent_lba_start);
  211                         part->ent_lba_end = le64toh(ent->ent_lba_end);
  212                         part->ent_attr = le64toh(ent->ent_attr);
  213                         /* XXX do we need to byte-swap UNICODE-16? */
  214                         bcopy(ent->ent_name, part->ent_name,
  215                             sizeof(part->ent_name));
  216                         ps = (!memcmp(&tmp, &freebsd, sizeof(freebsd)))
  217                             ? 's' : 'p';
  218                         g_topology_lock();
  219                         (void)g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  220                             part->ent_lba_start * secsz,
  221                             (1 + part->ent_lba_end - part->ent_lba_start) *
  222                             secsz, secsz, "%s%c%d", gp->name, ps, i + 1);
  223                         g_topology_unlock();
  224                 }
  225                 g_free(buf);
  226         } while (0);
  227 
  228         if (mbr != NULL)
  229                 g_free(mbr);
  230 
  231         g_topology_lock();
  232         g_access(cp, -1, 0, 0);
  233         if (LIST_EMPTY(&gp->provider)) {
  234                 g_slice_spoiled(cp);
  235                 return (NULL);
  236         }
  237         return (gp);
  238 }
  239 
  240 static struct g_class g_gpt_class = {
  241         .name = "GPT",
  242         .version = G_VERSION,
  243         .taste = g_gpt_taste,
  244         .dumpconf = g_gpt_dumpconf,
  245 };
  246 
  247 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);

Cache object: ada6968f99abb1bb17ffbe2671bf9c08


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