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 * $FreeBSD: releng/5.1/sys/geom/geom_gpt.c 114519 2003-05-02 08:33:26Z phk $
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/bio.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41
42 #include <sys/endian.h>
43 #include <sys/sbuf.h>
44 #include <sys/uuid.h>
45 #include <sys/gpt.h>
46 #include <geom/geom.h>
47 #include <geom/geom_slice.h>
48
49 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
50 CTASSERT(sizeof(struct gpt_ent) == 128);
51
52 /*
53 * XXX: GEOM is not dynamic enough. We are forced to use a compile-time
54 * limit. The minimum number of partitions (128) as required by EFI is
55 * most of the time just a waste of space.
56 */
57 #define GPT_MAX_SLICES 128
58
59 struct g_gpt_softc {
60 struct gpt_ent *part[GPT_MAX_SLICES];
61 };
62
63 static int
64 is_gpt_hdr(struct gpt_hdr *hdr)
65 {
66 uint32_t crc;
67
68 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)))
69 return (0);
70 crc = hdr->hdr_crc_self;
71 hdr->hdr_crc_self = 0;
72 if (crc32(hdr, hdr->hdr_size) != crc)
73 return (0);
74 hdr->hdr_crc_self = crc;
75 /* We're happy... */
76 return (1);
77 }
78
79 static int
80 g_gpt_start(struct bio *bp)
81 {
82
83 return (0);
84 }
85
86 static void
87 g_gpt_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
88 struct g_consumer *cp, struct g_provider *pp)
89 {
90 struct g_slicer *gsp = gp->softc;
91 struct g_gpt_softc *gs = gsp->softc;
92 struct uuid *uuid;
93
94 g_slice_dumpconf(sb, indent, gp, cp, pp);
95
96 if (pp != NULL) {
97 uuid = &gs->part[pp->index]->ent_type;
98 if (indent != NULL)
99 sbuf_printf(sb, "%s<type>", indent);
100 else
101 sbuf_printf(sb, " ty ");
102 sbuf_printf_uuid(sb, uuid);
103 if (indent != NULL)
104 sbuf_printf(sb, "</type>\n");
105 }
106 }
107
108 static struct g_geom *
109 g_gpt_taste(struct g_class *mp, struct g_provider *pp, int insist)
110 {
111 struct g_consumer *cp;
112 struct g_geom *gp;
113 struct g_gpt_softc *gs;
114 struct g_slicer *gsp;
115 u_char *buf, *mbr;
116 struct gpt_ent *ent;
117 struct gpt_hdr *hdr;
118 u_int i, npart, 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 gsp = gp->softc;
135 g_topology_unlock();
136 gp->dumpconf = g_gpt_dumpconf;
137
138 do {
139
140 npart = 0;
141 mbr = NULL;
142
143 if (gp->rank != 2 && insist == 0)
144 break;
145
146 secsz = cp->provider->sectorsize;
147 if (secsz < 512)
148 break;
149
150 /* XXX: we need to get the media size as well. */
151
152 /* Read both the MBR sector and the GPT sector. */
153 mbr = g_read_data(cp, 0, 2 * secsz, &error);
154 if (mbr == NULL || error != 0)
155 break;
156 #if 0
157 /*
158 * XXX: we should ignore the GPT if there's a MBR and the MBR is
159 * not a PMBR (Protective MBR). I believe this is what the EFI
160 * spec is going to say eventually (this is hearsay :-)
161 * Currently EFI (version 1.02) accepts and uses the GPT even
162 * though there's a valid MBR. We do this too, because it allows
163 * us to test this code without first nuking the only partitioning
164 * scheme we grok until this is working.
165 */
166 if (!is_pmbr((void*)mbr))
167 goto out;
168 #endif
169
170 hdr = (void*)(mbr + secsz);
171
172 /*
173 * XXX: if we don't have a GPT header at LBA 1, we should check if
174 * there's a backup GPT at the end of the medium. If we have a valid
175 * backup GPT, we should restore the primary GPT and claim this lunch.
176 */
177 if (!is_gpt_hdr(hdr))
178 break;
179
180 tblsz = (hdr->hdr_entries * hdr->hdr_entsz + secsz - 1) &
181 ~(secsz - 1);
182 buf = g_read_data(cp, hdr->hdr_lba_table * secsz, tblsz, &error);
183 for (i = 0; i < hdr->hdr_entries; i++) {
184 struct uuid unused = GPT_ENT_TYPE_UNUSED;
185 struct uuid freebsd = GPT_ENT_TYPE_FREEBSD;
186 if (i >= GPT_MAX_SLICES)
187 break;
188 ent = (void*)(buf + i * hdr->hdr_entsz);
189 if (!memcmp(&ent->ent_type, &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(&ent->ent_type, &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 npart++;
206 }
207 g_free(buf);
208
209 } while (0);
210
211
212 if (mbr != NULL)
213 g_free(mbr);
214
215 g_topology_lock();
216 g_access_rel(cp, -1, 0, 0);
217 if (LIST_EMPTY(&gp->provider)) {
218 g_slice_spoiled(cp);
219 return (NULL);
220 }
221 return (gp);
222 }
223
224 static struct g_class g_gpt_class = {
225 .name = "GPT",
226 .taste = g_gpt_taste,
227 G_CLASS_INITIALIZER
228 };
229
230 DECLARE_GEOM_CLASS(g_gpt_class, g_gpt);
Cache object: 3392dcafa0bfff3e8721ad770527ba20
|