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: src/sys/geom/geom_gpt.c,v 1.30 2004/08/08 07:57:51 phk 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 
   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 g_gpt_start(struct bio *bp)
   82 {
   83 
   84         return (0);
   85 }
   86 
   87 static void
   88 g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
   89     struct g_consumer *cp, struct g_provider *pp)
   90 {
   91         struct g_slicer *gsp = gp->softc;
   92         struct g_gpt_softc *gs = gsp->softc;
   93         struct uuid uuid;
   94 
   95         g_slice_dumpconf(sb, indent, gp, cp, pp);
   96 
   97         if (pp != NULL) {
   98                 le_uuid_dec(&gs->part[pp->index]->ent_type, &uuid);
   99                 if (indent != NULL)
  100                         sbuf_printf(sb, "%s<type>", indent);
  101                 else
  102                         sbuf_printf(sb, " ty ");
  103                 sbuf_printf_uuid(sb, &uuid);
  104                 if (indent != NULL)
  105                         sbuf_printf(sb, "</type>\n");
  106         }
  107 }
  108 
  109 static struct g_geom *
  110 g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist)
  111 {
  112         struct uuid tmp;
  113         struct g_consumer *cp;
  114         struct g_geom *gp;
  115         struct g_gpt_softc *gs;
  116         u_char *buf, *mbr;
  117         struct gpt_ent *ent, *part;
  118         struct gpt_hdr *hdr;
  119         u_int i, secsz, tblsz;
  120         int error, ps;
  121         uint32_t entries, entsz;
  122 
  123         g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
  124         g_topology_assert();
  125 
  126         /*
  127          * XXX: I don't like to hardcode a maximum number of slices, since
  128          * it's wasting space most of the time and insufficient any time.
  129          * It's easier for now...
  130          */
  131         gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
  132             g_gpt_start);
  133         if (gp == NULL)
  134                 return (NULL);
  135 
  136         g_topology_unlock();
  137 
  138         do {
  139                 mbr = NULL;
  140 
  141                 if (gp->rank != 2 && insist == 0)
  142                         break;
  143 
  144                 secsz = cp->provider->sectorsize;
  145                 if (secsz < 512)
  146                         break;
  147 
  148                 /* XXX: we need to get the media size as well. */
  149 
  150                 /* Read both the MBR sector and the GPT sector. */
  151                 mbr = g_read_data(cp, 0, 2 * secsz, &error);
  152                 if (mbr == NULL || error != 0)
  153                         break;
  154 #if 0
  155                 /*
  156                  * XXX: we should ignore the GPT if there's a MBR and the MBR
  157                  * is not a PMBR (Protective MBR). I believe this is what the
  158                  * EFI spec is going to say eventually (this is hearsay :-)
  159                  * Currently EFI (version 1.02) accepts and uses the GPT even
  160                  * though there's a valid MBR. We do this too, because it
  161                  * allows us to test this code without first nuking the only
  162                  * partitioning scheme we grok until this is working.
  163                  */
  164                 if (!is_pmbr((void*)mbr))
  165                         break;
  166 #endif
  167                 hdr = (void*)(mbr + secsz);
  168 
  169                 /*
  170                  * XXX: if we don't have a GPT header at LBA 1, we should
  171                  * check if there's a backup GPT at the end of the medium. If
  172                  * we have a valid backup GPT, we should restore the primary
  173                  * GPT and claim this lunch.
  174                  */
  175                 if (!is_gpt_hdr(hdr))
  176                         break;
  177 
  178                 entries = le32toh(hdr->hdr_entries);
  179                 entsz = le32toh(hdr->hdr_entsz);
  180                 tblsz = (entries * entsz + secsz - 1) & ~(secsz - 1);
  181                 buf = g_read_data(cp, le64toh(hdr->hdr_lba_table) * secsz,
  182                     tblsz, &error);
  183 
  184                 for (i = 0; i < entries; i++) {
  185                         struct uuid unused = GPT_ENT_TYPE_UNUSED;
  186                         struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
  187 
  188                         if (i >= GPT_MAX_SLICES)
  189                                 break;
  190                         ent = (void*)(buf + i * entsz);
  191                         le_uuid_dec(&ent->ent_type, &tmp);
  192                         if (!memcmp(&tmp, &unused, sizeof(unused)))
  193                                 continue;
  194                         /* XXX: This memory leaks */
  195                         part = gs->part[i] = g_malloc(entsz, M_WAITOK);
  196                         if (part == NULL)
  197                                 break;
  198                         part->ent_type = tmp;
  199                         le_uuid_dec(&ent->ent_uuid, &part->ent_uuid);
  200                         part->ent_lba_start = le64toh(ent->ent_lba_start);
  201                         part->ent_lba_end = le64toh(ent->ent_lba_end);
  202                         part->ent_attr = le64toh(ent->ent_attr);
  203                         /* XXX do we need to byte-swap UNICODE-16? */
  204                         bcopy(ent->ent_name, part->ent_name,
  205                             sizeof(part->ent_name));
  206                         ps = (!memcmp(&tmp, &freebsd, sizeof(freebsd)))
  207                             ? 's' : 'p';
  208                         g_topology_lock();
  209                         (void)g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  210                             part->ent_lba_start * secsz,
  211                             (1 + part->ent_lba_end - part->ent_lba_start) *
  212                             secsz, secsz, "%s%c%d", gp->name, ps, i + 1);
  213                         g_topology_unlock();
  214                 }
  215                 g_free(buf);
  216         } while (0);
  217 
  218         if (mbr != NULL)
  219                 g_free(mbr);
  220 
  221         g_topology_lock();
  222         g_access(cp, -1, 0, 0);
  223         if (LIST_EMPTY(&gp->provider)) {
  224                 g_slice_spoiled(cp);
  225                 return (NULL);
  226         }
  227         return (gp);
  228 }
  229 
  230 static struct g_class g_gpt_class = {
  231         .name = "GPT",
  232         .version = G_VERSION,
  233         .taste = g_gpt_taste,
  234         .dumpconf = g_gpt_dumpconf,
  235 };
  236 
  237 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);

Cache object: dccaafe0aad132624cab86c688ea6a4b


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