xref: /netbsd-src/sys/dev/dkwedge/dkwedge_gpt.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: dkwedge_gpt.c,v 1.2 2005/12/11 12:21:20 christos 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.2 2005/12/11 12:21:20 christos Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/errno.h>
50 #include <sys/disk.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53 
54 #include <sys/disklabel_gpt.h>
55 #include <sys/uuid.h>
56 
57 static const struct {
58 	struct uuid ptype_guid;
59 	const char *ptype_str;
60 } gpt_ptype_guid_to_str_tab[] = {
61 	{ GPT_ENT_TYPE_EFI,		"msdos" },	/* XXX yes? */
62 #if 0
63 	{ GPT_ENT_TYPE_FREEBSD,		??? },
64 #endif
65 	{ GPT_ENT_TYPE_FREEBSD_SWAP,	"swap" },	/* XXX for now */
66 	{ GPT_ENT_TYPE_FREEBSD_UFS,	"ffs" },	/* XXX for now */
67 
68 	/* XXX What about the MS and Linux types? */
69 
70 	{ { 0 },			NULL },
71 };
72 
73 static const char *
74 gpt_ptype_guid_to_str(const struct uuid *guid)
75 {
76 	int i;
77 
78 	for (i = 0; gpt_ptype_guid_to_str_tab[i].ptype_str != NULL; i++) {
79 		if (memcmp(&gpt_ptype_guid_to_str_tab[i].ptype_guid,
80 			   guid, sizeof(*guid)) == 0)
81 			return (gpt_ptype_guid_to_str_tab[i].ptype_str);
82 	}
83 
84 	return (NULL);
85 }
86 
87 static const uint32_t gpt_crc_tab[16] = {
88 	0x00000000U, 0x1db71064U, 0x3b6e20c8U, 0x26d930acU,
89 	0x76dc4190U, 0x6b6b51f4U, 0x4db26158U, 0x5005713cU,
90 	0xedb88320U, 0xf00f9344U, 0xd6d6a3e8U, 0xcb61b38cU,
91 	0x9b64c2b0U, 0x86d3d2d4U, 0xa00ae278U, 0xbdbdf21cU
92 };
93 
94 static uint32_t
95 gpt_crc32(const void *vbuf, size_t len)
96 {
97 	const uint8_t *buf = vbuf;
98 	uint32_t crc;
99 
100 	crc = 0xffffffffU;
101 	while (len--) {
102 		crc ^= *buf++;
103 		crc = (crc >> 4) ^ gpt_crc_tab[crc & 0xf];
104 		crc = (crc >> 4) ^ gpt_crc_tab[crc & 0xf];
105 	}
106 
107 	return (crc ^ 0xffffffffU);
108 }
109 
110 static int
111 gpt_verify_header_crc(struct gpt_hdr *hdr)
112 {
113 	uint32_t crc;
114 	int rv;
115 
116 	crc = hdr->hdr_crc_self;
117 	hdr->hdr_crc_self = 0;
118 	rv = le32toh(crc) == gpt_crc32(hdr, le32toh(hdr->hdr_size));
119 	hdr->hdr_crc_self = crc;
120 
121 	return (rv);
122 }
123 
124 static int
125 dkwedge_discover_gpt(struct disk *pdk, struct vnode *vp)
126 {
127 	static const struct uuid ent_type_unused = GPT_ENT_TYPE_UNUSED;
128 	static const char gpt_hdr_sig[] = GPT_HDR_SIG;
129 	struct dkwedge_info dkw;
130 	void *buf;
131 	struct gpt_hdr *hdr;
132 	struct gpt_ent *ent;
133 	uint32_t entries, entsz;
134 	daddr_t lba_start, lba_end, lba_table;
135 	uint32_t gpe_crc;
136 	int error;
137 	u_int i;
138 
139 	buf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
140 
141 	/*
142 	 * Note: We don't bother with a Legacy or Protective MBR
143 	 * here.  If a GPT is found, then the search stops, and
144 	 * the GPT is authoritative.
145 	 */
146 
147 	/* Read in the GPT Header. */
148 	error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO, buf, DEV_BSIZE);
149 	if (error)
150 		goto out;
151 	hdr = buf;
152 
153 	/* Validate it. */
154 	if (memcmp(gpt_hdr_sig, hdr->hdr_sig, sizeof(hdr->hdr_sig)) != 0) {
155 		/* XXX Should check at end-of-disk. */
156 		error = ESRCH;
157 		goto out;
158 	}
159 	if (hdr->hdr_revision != htole32(GPT_HDR_REVISION)) {
160 		/* XXX Should check at end-of-disk. */
161 		error = ESRCH;
162 		goto out;
163 	}
164 	if (le32toh(hdr->hdr_size) > DEV_BSIZE) {
165 		/* XXX Should check at end-of-disk. */
166 		error = ESRCH;
167 		goto out;
168 	}
169 	if (gpt_verify_header_crc(hdr) == 0) {
170 		/* XXX Should check at end-of-disk. */
171 		error = ESRCH;
172 		goto out;
173 	}
174 
175 	/* XXX Now that we found it, should we validate the backup? */
176 
177 	{
178 		struct uuid disk_guid;
179 		char guid_str[UUID_STR_LEN];
180 		uuid_dec_le(hdr->hdr_guid, &disk_guid);
181 		uuid_snprintf(guid_str, sizeof(guid_str), &disk_guid);
182 		aprint_verbose("%s: GPT GUID: %s\n", pdk->dk_name, guid_str);
183 	}
184 
185 	entries = le32toh(hdr->hdr_entries);
186 	entsz = roundup(le32toh(hdr->hdr_entsz), 8);
187 	if (entsz > roundup(sizeof(struct gpt_ent), 8)) {
188 		aprint_error("%s: bogus GPT entry size: %u\n",
189 		    pdk->dk_name, le32toh(hdr->hdr_entsz));
190 		error = EINVAL;
191 		goto out;
192 	}
193 	gpe_crc = le32toh(hdr->hdr_crc_table);
194 
195 	/* XXX Clamp entries at 128 for now. */
196 	if (entries > 128) {
197 		aprint_error("%s: WARNING: clamping number of GPT entries to "
198 		    "128 (was %u)\n", pdk->dk_name, entries);
199 		entries = 128;
200 	}
201 
202 	lba_start = le64toh(hdr->hdr_lba_start);
203 	lba_end = le64toh(hdr->hdr_lba_end);
204 	lba_table = le64toh(hdr->hdr_lba_table);
205 	if (lba_start < 0 || lba_end < 0 || lba_table < 0) {
206 		aprint_error("%s: GPT block numbers out of range\n",
207 		    pdk->dk_name);
208 		error = EINVAL;
209 		goto out;
210 	}
211 
212 	free(buf, M_DEVBUF);
213 	buf = malloc(roundup(entries * entsz, DEV_BSIZE), M_DEVBUF, M_WAITOK);
214 	error = dkwedge_read(pdk, vp, lba_table, buf,
215 			     roundup(entries * entsz, DEV_BSIZE));
216 	if (error) {
217 		/* XXX Should check alternate location. */
218 		aprint_error("%s: unable to read GPT partition array, "
219 		    "error = %d\n", pdk->dk_name, error);
220 		goto out;
221 	}
222 
223 	if (gpt_crc32(buf, entries * entsz) != gpe_crc) {
224 		/* XXX Should check alternate location. */
225 		aprint_error("%s: bad GPT partition array CRC\n",
226 		    pdk->dk_name);
227 		error = EINVAL;
228 		goto out;
229 	}
230 
231 	/*
232 	 * Walk the partitions, adding a wedge for each type we know about.
233 	 */
234 	for (i = 0; i < entries; i++) {
235 		struct uuid ptype_guid, ent_guid;
236 		const char *ptype;
237 		int j;
238 		char ptype_guid_str[UUID_STR_LEN], ent_guid_str[UUID_STR_LEN];
239 
240 		ent = (struct gpt_ent *)((caddr_t)buf + (i * entsz));
241 
242 		uuid_dec_le(ent->ent_type, &ptype_guid);
243 		if (memcmp(&ptype_guid, &ent_type_unused,
244 			   sizeof(ptype_guid)) == 0)
245 			continue;
246 
247 		uuid_dec_le(ent->ent_guid, &ent_guid);
248 
249 		uuid_snprintf(ptype_guid_str, sizeof(ptype_guid_str),
250 		    &ptype_guid);
251 		uuid_snprintf(ent_guid_str, sizeof(ent_guid_str),
252 		    &ent_guid);
253 
254 		/* Skip it if we don't grok this ptype. */
255 		if ((ptype = gpt_ptype_guid_to_str(&ptype_guid)) == NULL) {
256 			/*
257 			 * XXX Should probably just add these... maybe
258 			 * XXX just have an empty ptype?
259 			 */
260 			aprint_verbose("%s: skipping entry %u (%s), type %s\n",
261 			    pdk->dk_name, i, ent_guid_str, ptype_guid_str);
262 			continue;
263 		}
264 		strcpy(dkw.dkw_ptype, ptype);
265 
266 		strcpy(dkw.dkw_parent, pdk->dk_name);
267 		dkw.dkw_offset = le64toh(ent->ent_lba_start);
268 		dkw.dkw_size = le64toh(ent->ent_lba_end) - dkw.dkw_offset + 1;
269 
270 		/* XXX Make sure it falls within the disk's data area. */
271 
272 		if (ent->ent_name[0] == 0x0000)
273 			strcpy(dkw.dkw_wname, ent_guid_str);
274 		else {
275 			for (j = 0; ent->ent_name[j] != 0x0000; j++) {
276 				/* XXX UTF-16 -> UTF-8 */
277 				dkw.dkw_wname[j] =
278 				    le16toh(ent->ent_name[j]) & 0xff;
279 			}
280 			dkw.dkw_wname[j] = '\0';
281 		}
282 
283 		/*
284 		 * Try with the partition name first.  If that fails,
285 		 * use the GUID string.  If that fails, punt.
286 		 */
287 		if ((error = dkwedge_add(&dkw)) == EEXIST) {
288 			aprint_error("%s: wedge named '%s' already exists, "
289 			    "trying '%s'\n", pdk->dk_name,
290 			    dkw.dkw_wname, /* XXX Unicode */
291 			    ent_guid_str);
292 			strcpy(dkw.dkw_wname, ent_guid_str);
293 			error = dkwedge_add(&dkw);
294 		}
295 		if (error == EEXIST)
296 			aprint_error("%s: wedge named '%s' already exists, "
297 			    "manual intervention required\n", pdk->dk_name,
298 			    dkw.dkw_wname);
299 		else if (error)
300 			aprint_error("%s: error %d adding entry %u (%s), "
301 			    "type %s\n", pdk->dk_name, error, i, ent_guid_str,
302 			    ptype_guid_str);
303 	}
304 	error = 0;
305 
306  out:
307 	free(buf, M_DEVBUF);
308 	return (error);
309 }
310 
311 DKWEDGE_DISCOVERY_METHOD_DECL(GPT, 0, dkwedge_discover_gpt);
312