2002-05-28 09:04:48 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Marcel Moolenaar
|
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 06:49:16 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-05-28 09:04:48 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/sbuf.h>
|
|
|
|
#include <sys/uuid.h>
|
|
|
|
#include <sys/gpt.h>
|
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/geom_slice.h>
|
|
|
|
|
2002-05-30 06:33:07 +00:00
|
|
|
CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
|
|
|
|
CTASSERT(sizeof(struct gpt_ent) == 128);
|
|
|
|
|
2002-05-28 09:04:48 +00:00
|
|
|
/*
|
|
|
|
* XXX: GEOM is not dynamic enough. We are forced to use a compile-time
|
|
|
|
* limit. The minimum number of partitions (128) as required by EFI is
|
|
|
|
* most of the time just a waste of space.
|
|
|
|
*/
|
|
|
|
#define GPT_MAX_SLICES 128
|
|
|
|
|
|
|
|
struct g_gpt_softc {
|
|
|
|
struct gpt_ent *part[GPT_MAX_SLICES];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_gpt_hdr(struct gpt_hdr *hdr)
|
|
|
|
{
|
|
|
|
uint32_t crc;
|
|
|
|
|
|
|
|
if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)))
|
|
|
|
return (0);
|
2004-04-30 05:05:39 +00:00
|
|
|
crc = le32toh(hdr->hdr_crc_self);
|
2002-05-28 09:04:48 +00:00
|
|
|
hdr->hdr_crc_self = 0;
|
2004-04-30 05:05:39 +00:00
|
|
|
if (crc32(hdr, le32toh(hdr->hdr_size)) != crc)
|
2002-05-28 09:04:48 +00:00
|
|
|
return (0);
|
2004-04-30 05:05:39 +00:00
|
|
|
hdr->hdr_crc_self = htole32(crc);
|
2002-05-28 09:04:48 +00:00
|
|
|
/* We're happy... */
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_gpt_start(struct bio *bp)
|
|
|
|
{
|
|
|
|
|
2003-05-01 01:51:28 +00:00
|
|
|
return (0);
|
2002-05-28 09:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-12-17 07:22:48 +00:00
|
|
|
g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
|
2002-05-28 09:04:48 +00:00
|
|
|
struct g_consumer *cp, struct g_provider *pp)
|
|
|
|
{
|
|
|
|
struct g_slicer *gsp = gp->softc;
|
|
|
|
struct g_gpt_softc *gs = gsp->softc;
|
2004-04-30 05:05:39 +00:00
|
|
|
struct uuid uuid;
|
2002-05-28 09:04:48 +00:00
|
|
|
|
|
|
|
g_slice_dumpconf(sb, indent, gp, cp, pp);
|
|
|
|
|
|
|
|
if (pp != NULL) {
|
2004-04-30 05:05:39 +00:00
|
|
|
le_uuid_dec(&gs->part[pp->index]->ent_type, &uuid);
|
2002-11-02 11:45:54 +00:00
|
|
|
if (indent != NULL)
|
|
|
|
sbuf_printf(sb, "%s<type>", indent);
|
|
|
|
else
|
|
|
|
sbuf_printf(sb, " ty ");
|
2004-04-30 05:05:39 +00:00
|
|
|
sbuf_printf_uuid(sb, &uuid);
|
2002-11-02 11:45:54 +00:00
|
|
|
if (indent != NULL)
|
|
|
|
sbuf_printf(sb, "</type>\n");
|
2002-05-28 09:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct g_geom *
|
|
|
|
g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist)
|
|
|
|
{
|
2004-04-30 05:05:39 +00:00
|
|
|
struct uuid tmp;
|
2002-05-28 09:04:48 +00:00
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_gpt_softc *gs;
|
|
|
|
u_char *buf, *mbr;
|
2004-04-30 05:05:39 +00:00
|
|
|
struct gpt_ent *ent, *part;
|
2002-05-28 09:04:48 +00:00
|
|
|
struct gpt_hdr *hdr;
|
2003-05-31 19:28:53 +00:00
|
|
|
u_int i, secsz, tblsz;
|
2002-08-24 22:42:16 +00:00
|
|
|
int error, ps;
|
2004-04-30 05:05:39 +00:00
|
|
|
uint32_t entries, entsz;
|
2002-05-28 09:04:48 +00:00
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
|
|
|
|
g_topology_assert();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: I don't like to hardcode a maximum number of slices, since
|
|
|
|
* it's wasting space most of the time and insufficient any time.
|
|
|
|
* It's easier for now...
|
|
|
|
*/
|
|
|
|
gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
|
|
|
|
g_gpt_start);
|
|
|
|
if (gp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
g_topology_unlock();
|
|
|
|
gp->dumpconf = g_gpt_dumpconf;
|
|
|
|
|
2002-09-28 11:57:20 +00:00
|
|
|
do {
|
|
|
|
mbr = NULL;
|
2002-05-28 09:04:48 +00:00
|
|
|
|
2002-09-28 11:57:20 +00:00
|
|
|
if (gp->rank != 2 && insist == 0)
|
|
|
|
break;
|
2002-05-28 09:04:48 +00:00
|
|
|
|
2002-10-20 20:28:24 +00:00
|
|
|
secsz = cp->provider->sectorsize;
|
|
|
|
if (secsz < 512)
|
2002-09-28 11:57:20 +00:00
|
|
|
break;
|
2002-05-28 09:04:48 +00:00
|
|
|
|
2002-09-28 11:57:20 +00:00
|
|
|
/* XXX: we need to get the media size as well. */
|
|
|
|
|
|
|
|
/* Read both the MBR sector and the GPT sector. */
|
|
|
|
mbr = g_read_data(cp, 0, 2 * secsz, &error);
|
|
|
|
if (mbr == NULL || error != 0)
|
|
|
|
break;
|
2002-05-28 09:04:48 +00:00
|
|
|
#if 0
|
2004-04-30 05:05:39 +00:00
|
|
|
/*
|
|
|
|
* XXX: we should ignore the GPT if there's a MBR and the MBR
|
|
|
|
* is not a PMBR (Protective MBR). I believe this is what the
|
|
|
|
* EFI spec is going to say eventually (this is hearsay :-)
|
|
|
|
* Currently EFI (version 1.02) accepts and uses the GPT even
|
|
|
|
* though there's a valid MBR. We do this too, because it
|
|
|
|
* allows us to test this code without first nuking the only
|
|
|
|
* partitioning scheme we grok until this is working.
|
|
|
|
*/
|
|
|
|
if (!is_pmbr((void*)mbr))
|
|
|
|
break;
|
2002-05-28 09:04:48 +00:00
|
|
|
#endif
|
2002-09-28 11:57:20 +00:00
|
|
|
hdr = (void*)(mbr + secsz);
|
2002-05-28 09:04:48 +00:00
|
|
|
|
2004-04-30 05:05:39 +00:00
|
|
|
/*
|
|
|
|
* XXX: if we don't have a GPT header at LBA 1, we should
|
|
|
|
* check if there's a backup GPT at the end of the medium. If
|
|
|
|
* we have a valid backup GPT, we should restore the primary
|
|
|
|
* GPT and claim this lunch.
|
|
|
|
*/
|
2002-09-28 11:57:20 +00:00
|
|
|
if (!is_gpt_hdr(hdr))
|
2002-05-28 09:04:48 +00:00
|
|
|
break;
|
|
|
|
|
2004-04-30 05:05:39 +00:00
|
|
|
entries = le32toh(hdr->hdr_entries);
|
|
|
|
entsz = le32toh(hdr->hdr_entsz);
|
|
|
|
tblsz = (entries * entsz + secsz - 1) & ~(secsz - 1);
|
|
|
|
buf = g_read_data(cp, le64toh(hdr->hdr_lba_table) * secsz,
|
|
|
|
tblsz, &error);
|
|
|
|
|
|
|
|
for (i = 0; i < entries; i++) {
|
2002-09-28 11:57:20 +00:00
|
|
|
struct uuid unused = GPT_ENT_TYPE_UNUSED;
|
|
|
|
struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
|
2004-04-30 05:05:39 +00:00
|
|
|
|
2002-09-28 11:57:20 +00:00
|
|
|
if (i >= GPT_MAX_SLICES)
|
|
|
|
break;
|
2004-04-30 05:05:39 +00:00
|
|
|
ent = (void*)(buf + i * entsz);
|
2003-05-31 16:48:43 +00:00
|
|
|
le_uuid_dec(&ent->ent_type, &tmp);
|
|
|
|
if (!memcmp(&tmp, &unused, sizeof(unused)))
|
2002-09-28 11:57:20 +00:00
|
|
|
continue;
|
2003-05-02 08:33:26 +00:00
|
|
|
/* XXX: This memory leaks */
|
2004-04-30 05:05:39 +00:00
|
|
|
part = gs->part[i] = g_malloc(entsz, M_WAITOK);
|
|
|
|
if (part == NULL)
|
2002-09-28 11:57:20 +00:00
|
|
|
break;
|
2004-04-30 05:05:39 +00:00
|
|
|
part->ent_type = tmp;
|
|
|
|
le_uuid_dec(&ent->ent_uuid, &part->ent_uuid);
|
|
|
|
part->ent_lba_start = le64toh(ent->ent_lba_start);
|
|
|
|
part->ent_lba_end = le64toh(ent->ent_lba_end);
|
|
|
|
part->ent_attr = le64toh(ent->ent_attr);
|
|
|
|
/* XXX do we need to byte-swap UNICODE-16? */
|
|
|
|
bcopy(ent->ent_name, part->ent_name,
|
|
|
|
sizeof(part->ent_name));
|
2003-05-31 16:48:43 +00:00
|
|
|
ps = (!memcmp(&tmp, &freebsd, sizeof(freebsd)))
|
2002-09-28 11:57:20 +00:00
|
|
|
? 's' : 'p';
|
|
|
|
g_topology_lock();
|
2002-12-16 23:08:48 +00:00
|
|
|
(void)g_slice_config(gp, i, G_SLICE_CONFIG_SET,
|
2004-04-30 05:05:39 +00:00
|
|
|
part->ent_lba_start * secsz,
|
|
|
|
(1 + part->ent_lba_end - part->ent_lba_start) *
|
|
|
|
secsz, secsz, "%s%c%d", gp->name, ps, i + 1);
|
2002-09-28 11:57:20 +00:00
|
|
|
g_topology_unlock();
|
|
|
|
}
|
|
|
|
g_free(buf);
|
|
|
|
} while (0);
|
|
|
|
|
2002-05-28 09:04:48 +00:00
|
|
|
if (mbr != NULL)
|
|
|
|
g_free(mbr);
|
|
|
|
|
|
|
|
g_topology_lock();
|
2004-02-12 22:42:11 +00:00
|
|
|
g_access(cp, -1, 0, 0);
|
2002-12-16 23:08:48 +00:00
|
|
|
if (LIST_EMPTY(&gp->provider)) {
|
2003-05-02 08:33:26 +00:00
|
|
|
g_slice_spoiled(cp);
|
2002-12-16 23:08:48 +00:00
|
|
|
return (NULL);
|
2002-05-28 09:04:48 +00:00
|
|
|
}
|
2002-12-16 23:08:48 +00:00
|
|
|
return (gp);
|
2002-05-28 09:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct g_class g_gpt_class = {
|
2003-03-24 19:30:15 +00:00
|
|
|
.name = "GPT",
|
|
|
|
.taste = g_gpt_taste,
|
2002-05-28 09:04:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);
|