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: releng/5.2/sys/geom/geom_gpt.c 116196 2003-06-11 06:49:16Z obrien $");
   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 = hdr->hdr_crc_self;
   72         hdr->hdr_crc_self = 0;
   73         if (crc32(hdr, hdr->hdr_size) != crc)
   74                 return (0);
   75         hdr->hdr_crc_self = 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                 uuid = &gs->part[pp->index]->ent_type;
   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 g_consumer *cp;
  113         struct g_geom *gp;
  114         struct g_gpt_softc *gs;
  115         u_char *buf, *mbr;
  116         struct gpt_ent *ent;
  117         struct gpt_hdr *hdr;
  118         u_int i, secsz, tblsz;
  119         int error, ps;
  120 
  121         g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
  122         g_topology_assert();
  123 
  124         /*
  125          * XXX: I don't like to hardcode a maximum number of slices, since
  126          * it's wasting space most of the time and insufficient any time.
  127          * It's easier for now...
  128          */
  129         gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
  130             g_gpt_start);
  131         if (gp == NULL)
  132                 return (NULL);
  133 
  134         g_topology_unlock();
  135         gp->dumpconf = g_gpt_dumpconf;
  136 
  137         do {
  138 
  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 is
  157          * not a PMBR (Protective MBR). I believe this is what the EFI
  158          * 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 allows
  161          * us to test this code without first nuking the only partitioning
  162          * scheme we grok until this is working.
  163          */
  164         if (!is_pmbr((void*)mbr))
  165                 goto out;
  166 #endif
  167 
  168                 hdr = (void*)(mbr + secsz);
  169 
  170         /*
  171          * XXX: if we don't have a GPT header at LBA 1, we should check if
  172          * there's a backup GPT at the end of the medium. If we have a valid
  173          * backup GPT, we should restore the primary GPT and claim this lunch.
  174          */
  175                 if (!is_gpt_hdr(hdr))
  176                         break;
  177 
  178                 tblsz = (hdr->hdr_entries * hdr->hdr_entsz + secsz - 1) &
  179                     ~(secsz - 1);
  180                 buf = g_read_data(cp, hdr->hdr_lba_table * secsz, tblsz, &error);
  181                 for (i = 0; i < hdr->hdr_entries; i++) {
  182                         struct uuid unused = GPT_ENT_TYPE_UNUSED;
  183                         struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
  184                         struct uuid tmp;
  185                         if (i >= GPT_MAX_SLICES)
  186                                 break;
  187                         ent = (void*)(buf + i * hdr->hdr_entsz);
  188                         le_uuid_dec(&ent->ent_type, &tmp);
  189                         if (!memcmp(&tmp, &unused, sizeof(unused)))
  190                                 continue;
  191                         /* XXX: This memory leaks */
  192                         gs->part[i] = g_malloc(hdr->hdr_entsz, M_WAITOK);
  193                         if (gs->part[i] == NULL)
  194                                 break;
  195                         bcopy(ent, gs->part[i], hdr->hdr_entsz);
  196                         ps = (!memcmp(&tmp, &freebsd, sizeof(freebsd)))
  197                             ? 's' : 'p';
  198                         g_topology_lock();
  199                         (void)g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  200                             ent->ent_lba_start * secsz,
  201                             (1 + ent->ent_lba_end - ent->ent_lba_start) * secsz,
  202                             secsz,
  203                             "%s%c%d", gp->name, ps, i + 1);
  204                         g_topology_unlock();
  205                 }
  206                 g_free(buf);
  207 
  208         } while (0);
  209 
  210         if (mbr != NULL)
  211                 g_free(mbr);
  212 
  213         g_topology_lock();
  214         g_access_rel(cp, -1, 0, 0);
  215         if (LIST_EMPTY(&gp->provider)) {
  216                 g_slice_spoiled(cp);
  217                 return (NULL);
  218         }
  219         return (gp);
  220 }
  221 
  222 static struct g_class g_gpt_class = {
  223         .name = "GPT",
  224         .taste = g_gpt_taste,
  225 };
  226 
  227 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);

Cache object: c9268145eb4ade621e6e327fc5a2b690


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