dkwedge_gpt.c revision 1.3 1 /* $NetBSD: dkwedge_gpt.c,v 1.3 2006/08/13 18:45:08 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * EFI GUID Partition Table support for disk wedges
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.3 2006/08/13 18:45:08 martin Exp $");
45
46 #include "opt_dkwedge.h"
47 #ifdef DKWEDGE_METHOD_GPT
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52 #include <sys/errno.h>
53 #include <sys/disk.h>
54 #include <sys/vnode.h>
55 #include <sys/malloc.h>
56
57 #include <sys/disklabel_gpt.h>
58 #include <sys/uuid.h>
59
60 static const struct {
61 struct uuid ptype_guid;
62 const char *ptype_str;
63 } gpt_ptype_guid_to_str_tab[] = {
64 { GPT_ENT_TYPE_EFI, "msdos" }, /* XXX yes? */
65 #if 0
66 { GPT_ENT_TYPE_FREEBSD, ??? },
67 #endif
68 { GPT_ENT_TYPE_FREEBSD_SWAP, "swap" }, /* XXX for now */
69 { GPT_ENT_TYPE_FREEBSD_UFS, "ffs" }, /* XXX for now */
70
71 /* XXX What about the MS and Linux types? */
72
73 { { 0 }, NULL },
74 };
75
76 static const char *
77 gpt_ptype_guid_to_str(const struct uuid *guid)
78 {
79 int i;
80
81 for (i = 0; gpt_ptype_guid_to_str_tab[i].ptype_str != NULL; i++) {
82 if (memcmp(&gpt_ptype_guid_to_str_tab[i].ptype_guid,
83 guid, sizeof(*guid)) == 0)
84 return (gpt_ptype_guid_to_str_tab[i].ptype_str);
85 }
86
87 return (NULL);
88 }
89
90 static const uint32_t gpt_crc_tab[16] = {
91 0x00000000U, 0x1db71064U, 0x3b6e20c8U, 0x26d930acU,
92 0x76dc4190U, 0x6b6b51f4U, 0x4db26158U, 0x5005713cU,
93 0xedb88320U, 0xf00f9344U, 0xd6d6a3e8U, 0xcb61b38cU,
94 0x9b64c2b0U, 0x86d3d2d4U, 0xa00ae278U, 0xbdbdf21cU
95 };
96
97 static uint32_t
98 gpt_crc32(const void *vbuf, size_t len)
99 {
100 const uint8_t *buf = vbuf;
101 uint32_t crc;
102
103 crc = 0xffffffffU;
104 while (len--) {
105 crc ^= *buf++;
106 crc = (crc >> 4) ^ gpt_crc_tab[crc & 0xf];
107 crc = (crc >> 4) ^ gpt_crc_tab[crc & 0xf];
108 }
109
110 return (crc ^ 0xffffffffU);
111 }
112
113 static int
114 gpt_verify_header_crc(struct gpt_hdr *hdr)
115 {
116 uint32_t crc;
117 int rv;
118
119 crc = hdr->hdr_crc_self;
120 hdr->hdr_crc_self = 0;
121 rv = le32toh(crc) == gpt_crc32(hdr, le32toh(hdr->hdr_size));
122 hdr->hdr_crc_self = crc;
123
124 return (rv);
125 }
126
127 static int
128 dkwedge_discover_gpt(struct disk *pdk, struct vnode *vp)
129 {
130 static const struct uuid ent_type_unused = GPT_ENT_TYPE_UNUSED;
131 static const char gpt_hdr_sig[] = GPT_HDR_SIG;
132 struct dkwedge_info dkw;
133 void *buf;
134 struct gpt_hdr *hdr;
135 struct gpt_ent *ent;
136 uint32_t entries, entsz;
137 daddr_t lba_start, lba_end, lba_table;
138 uint32_t gpe_crc;
139 int error;
140 u_int i;
141
142 buf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
143
144 /*
145 * Note: We don't bother with a Legacy or Protective MBR
146 * here. If a GPT is found, then the search stops, and
147 * the GPT is authoritative.
148 */
149
150 /* Read in the GPT Header. */
151 error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO, buf, DEV_BSIZE);
152 if (error)
153 goto out;
154 hdr = buf;
155
156 /* Validate it. */
157 if (memcmp(gpt_hdr_sig, hdr->hdr_sig, sizeof(hdr->hdr_sig)) != 0) {
158 /* XXX Should check at end-of-disk. */
159 error = ESRCH;
160 goto out;
161 }
162 if (hdr->hdr_revision != htole32(GPT_HDR_REVISION)) {
163 /* XXX Should check at end-of-disk. */
164 error = ESRCH;
165 goto out;
166 }
167 if (le32toh(hdr->hdr_size) > DEV_BSIZE) {
168 /* XXX Should check at end-of-disk. */
169 error = ESRCH;
170 goto out;
171 }
172 if (gpt_verify_header_crc(hdr) == 0) {
173 /* XXX Should check at end-of-disk. */
174 error = ESRCH;
175 goto out;
176 }
177
178 /* XXX Now that we found it, should we validate the backup? */
179
180 {
181 struct uuid disk_guid;
182 char guid_str[UUID_STR_LEN];
183 uuid_dec_le(hdr->hdr_guid, &disk_guid);
184 uuid_snprintf(guid_str, sizeof(guid_str), &disk_guid);
185 aprint_verbose("%s: GPT GUID: %s\n", pdk->dk_name, guid_str);
186 }
187
188 entries = le32toh(hdr->hdr_entries);
189 entsz = roundup(le32toh(hdr->hdr_entsz), 8);
190 if (entsz > roundup(sizeof(struct gpt_ent), 8)) {
191 aprint_error("%s: bogus GPT entry size: %u\n",
192 pdk->dk_name, le32toh(hdr->hdr_entsz));
193 error = EINVAL;
194 goto out;
195 }
196 gpe_crc = le32toh(hdr->hdr_crc_table);
197
198 /* XXX Clamp entries at 128 for now. */
199 if (entries > 128) {
200 aprint_error("%s: WARNING: clamping number of GPT entries to "
201 "128 (was %u)\n", pdk->dk_name, entries);
202 entries = 128;
203 }
204
205 lba_start = le64toh(hdr->hdr_lba_start);
206 lba_end = le64toh(hdr->hdr_lba_end);
207 lba_table = le64toh(hdr->hdr_lba_table);
208 if (lba_start < 0 || lba_end < 0 || lba_table < 0) {
209 aprint_error("%s: GPT block numbers out of range\n",
210 pdk->dk_name);
211 error = EINVAL;
212 goto out;
213 }
214
215 free(buf, M_DEVBUF);
216 buf = malloc(roundup(entries * entsz, DEV_BSIZE), M_DEVBUF, M_WAITOK);
217 error = dkwedge_read(pdk, vp, lba_table, buf,
218 roundup(entries * entsz, DEV_BSIZE));
219 if (error) {
220 /* XXX Should check alternate location. */
221 aprint_error("%s: unable to read GPT partition array, "
222 "error = %d\n", pdk->dk_name, error);
223 goto out;
224 }
225
226 if (gpt_crc32(buf, entries * entsz) != gpe_crc) {
227 /* XXX Should check alternate location. */
228 aprint_error("%s: bad GPT partition array CRC\n",
229 pdk->dk_name);
230 error = EINVAL;
231 goto out;
232 }
233
234 /*
235 * Walk the partitions, adding a wedge for each type we know about.
236 */
237 for (i = 0; i < entries; i++) {
238 struct uuid ptype_guid, ent_guid;
239 const char *ptype;
240 int j;
241 char ptype_guid_str[UUID_STR_LEN], ent_guid_str[UUID_STR_LEN];
242
243 ent = (struct gpt_ent *)((caddr_t)buf + (i * entsz));
244
245 uuid_dec_le(ent->ent_type, &ptype_guid);
246 if (memcmp(&ptype_guid, &ent_type_unused,
247 sizeof(ptype_guid)) == 0)
248 continue;
249
250 uuid_dec_le(ent->ent_guid, &ent_guid);
251
252 uuid_snprintf(ptype_guid_str, sizeof(ptype_guid_str),
253 &ptype_guid);
254 uuid_snprintf(ent_guid_str, sizeof(ent_guid_str),
255 &ent_guid);
256
257 /* Skip it if we don't grok this ptype. */
258 if ((ptype = gpt_ptype_guid_to_str(&ptype_guid)) == NULL) {
259 /*
260 * XXX Should probably just add these... maybe
261 * XXX just have an empty ptype?
262 */
263 aprint_verbose("%s: skipping entry %u (%s), type %s\n",
264 pdk->dk_name, i, ent_guid_str, ptype_guid_str);
265 continue;
266 }
267 strcpy(dkw.dkw_ptype, ptype);
268
269 strcpy(dkw.dkw_parent, pdk->dk_name);
270 dkw.dkw_offset = le64toh(ent->ent_lba_start);
271 dkw.dkw_size = le64toh(ent->ent_lba_end) - dkw.dkw_offset + 1;
272
273 /* XXX Make sure it falls within the disk's data area. */
274
275 if (ent->ent_name[0] == 0x0000)
276 strcpy(dkw.dkw_wname, ent_guid_str);
277 else {
278 for (j = 0; ent->ent_name[j] != 0x0000; j++) {
279 /* XXX UTF-16 -> UTF-8 */
280 dkw.dkw_wname[j] =
281 le16toh(ent->ent_name[j]) & 0xff;
282 }
283 dkw.dkw_wname[j] = '\0';
284 }
285
286 /*
287 * Try with the partition name first. If that fails,
288 * use the GUID string. If that fails, punt.
289 */
290 if ((error = dkwedge_add(&dkw)) == EEXIST) {
291 aprint_error("%s: wedge named '%s' already exists, "
292 "trying '%s'\n", pdk->dk_name,
293 dkw.dkw_wname, /* XXX Unicode */
294 ent_guid_str);
295 strcpy(dkw.dkw_wname, ent_guid_str);
296 error = dkwedge_add(&dkw);
297 }
298 if (error == EEXIST)
299 aprint_error("%s: wedge named '%s' already exists, "
300 "manual intervention required\n", pdk->dk_name,
301 dkw.dkw_wname);
302 else if (error)
303 aprint_error("%s: error %d adding entry %u (%s), "
304 "type %s\n", pdk->dk_name, error, i, ent_guid_str,
305 ptype_guid_str);
306 }
307 error = 0;
308
309 out:
310 free(buf, M_DEVBUF);
311 return (error);
312 }
313
314 DKWEDGE_DISCOVERY_METHOD_DECL(GPT, 0, dkwedge_discover_gpt);
315
316 #endif
317
318