2 * Copyright (c) 2002 Marcel Moolenaar
3 * Copyright (c) 2002 Poul-Henning Kamp
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/diskmbr.h>
43 #include <sys/endian.h>
47 #include <geom/geom.h>
48 #include <geom/geom_slice.h>
50 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
51 CTASSERT(sizeof(struct gpt_ent) == 128);
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.
58 #define GPT_MAX_SLICES 128
61 struct gpt_ent *part[GPT_MAX_SLICES];
65 is_gpt_hdr(struct gpt_hdr *hdr)
69 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)))
71 crc = le32toh(hdr->hdr_crc_self);
72 hdr->hdr_crc_self = 0;
73 if (crc32(hdr, le32toh(hdr->hdr_size)) != crc)
75 hdr->hdr_crc_self = htole32(crc);
87 magic = le16toh(*(uint16_t *)(uintptr_t)(mbr + DOSMAGICOFFSET));
88 if (magic != DOSMAGIC)
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)
102 g_gpt_start(struct bio *bp)
104 struct g_provider *pp;
106 struct g_gpt_softc *gs;
107 struct g_slicer *gsp;
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)))
125 g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
126 struct g_consumer *cp, struct g_provider *pp)
128 struct g_slicer *gsp = gp->softc;
129 struct g_gpt_softc *gs = gsp->softc;
132 g_slice_dumpconf(sb, indent, gp, cp, pp);
135 le_uuid_dec(&gs->part[pp->index]->ent_type, &uuid);
137 sbuf_printf(sb, "%s<type>", indent);
139 sbuf_printf(sb, " ty ");
140 sbuf_printf_uuid(sb, &uuid);
142 sbuf_printf(sb, "</type>\n");
146 static struct g_geom *
147 g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
150 struct g_consumer *cp;
152 struct g_gpt_softc *gs;
154 struct gpt_ent *ent, *part;
156 u_int i, secsz, tblsz;
158 uint32_t entries, entsz;
160 g_trace(G_T_TOPOLOGY, "g_gpt_taste(%s,%s)", mp->name, pp->name);
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...
168 gp = g_slice_new(mp, GPT_MAX_SLICES, pp, &cp, &gs, sizeof(*gs),
178 secsz = cp->provider->sectorsize;
182 /* XXX: we need to get the media size as well. */
184 /* Read both the MBR sector and the GPT sector. */
185 mbr = g_read_data(cp, 0, 2 * secsz, NULL);
192 hdr = (void*)(mbr + secsz);
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.
200 if (!is_gpt_hdr(hdr))
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,
211 for (i = 0; i < entries; i++) {
212 struct uuid unused = GPT_ENT_TYPE_UNUSED;
213 struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
215 if (i >= GPT_MAX_SLICES)
217 ent = (void*)(buf + i * entsz);
218 le_uuid_dec(&ent->ent_type, &tmp);
219 if (!memcmp(&tmp, &unused, sizeof(unused)))
221 /* XXX: This memory leaks */
222 part = gs->part[i] = g_malloc(entsz, M_WAITOK);
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)))
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);
249 g_access(cp, -1, 0, 0);
250 if (LIST_EMPTY(&gp->provider)) {
257 static struct g_class g_gpt_class = {
259 .version = G_VERSION,
260 .taste = g_gpt_taste,
261 .dumpconf = g_gpt_dumpconf,
264 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);