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