dkwedge_gpt.c revision 1.21 1 /* $NetBSD: dkwedge_gpt.c,v 1.21 2018/11/06 04:04:33 mrg 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * EFI GUID Partition Table support for disk wedges
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.21 2018/11/06 04:04:33 mrg Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/errno.h>
43 #include <sys/disk.h>
44 #include <sys/vnode.h>
45 #include <sys/malloc.h>
46
47 #include <sys/disklabel_gpt.h>
48 #include <sys/uuid.h>
49
50 /* UTF-8 encoding stuff */
51 #include <fs/unicode.h>
52
53 /*
54 * GUID to dkw_ptype mapping information.
55 *
56 * GPT_ENT_TYPE_MS_BASIC_DATA is not suited to mapping. Aside from being
57 * used for multiple Microsoft file systems, Linux uses it for its own
58 * set of native file systems. Treating this GUID as unknown seems best.
59 */
60
61 static const struct {
62 struct uuid ptype_guid;
63 const char *ptype_str;
64 } gpt_ptype_guid_to_str_tab[] = {
65 { GPT_ENT_TYPE_EFI, DKW_PTYPE_FAT },
66 { GPT_ENT_TYPE_NETBSD_SWAP, DKW_PTYPE_SWAP },
67 { GPT_ENT_TYPE_FREEBSD_SWAP, DKW_PTYPE_SWAP },
68 { GPT_ENT_TYPE_NETBSD_FFS, DKW_PTYPE_FFS },
69 { GPT_ENT_TYPE_FREEBSD_UFS, DKW_PTYPE_FFS },
70 { GPT_ENT_TYPE_APPLE_UFS, DKW_PTYPE_FFS },
71 { GPT_ENT_TYPE_NETBSD_LFS, DKW_PTYPE_LFS },
72 { GPT_ENT_TYPE_NETBSD_RAIDFRAME, DKW_PTYPE_RAIDFRAME },
73 { GPT_ENT_TYPE_NETBSD_CCD, DKW_PTYPE_CCD },
74 { GPT_ENT_TYPE_NETBSD_CGD, DKW_PTYPE_CGD },
75 { GPT_ENT_TYPE_APPLE_HFS, DKW_PTYPE_APPLEHFS },
76 { GPT_ENT_TYPE_VMWARE_VMKCORE, DKW_PTYPE_VMKCORE },
77 { GPT_ENT_TYPE_VMWARE_VMFS, DKW_PTYPE_VMFS },
78 { GPT_ENT_TYPE_VMWARE_RESERVED, DKW_PTYPE_VMWRESV },
79 };
80
81 static const char *
82 gpt_ptype_guid_to_str(const struct uuid *guid)
83 {
84 int i;
85
86 for (i = 0; i < __arraycount(gpt_ptype_guid_to_str_tab); i++) {
87 if (memcmp(&gpt_ptype_guid_to_str_tab[i].ptype_guid,
88 guid, sizeof(*guid)) == 0)
89 return (gpt_ptype_guid_to_str_tab[i].ptype_str);
90 }
91
92 return (DKW_PTYPE_UNKNOWN);
93 }
94
95 static int
96 gpt_verify_header_crc(struct gpt_hdr *hdr)
97 {
98 uint32_t crc;
99 int rv;
100
101 crc = hdr->hdr_crc_self;
102 hdr->hdr_crc_self = 0;
103 rv = le32toh(crc) == crc32(0, (void *)hdr, le32toh(hdr->hdr_size));
104 hdr->hdr_crc_self = crc;
105
106 return (rv);
107 }
108
109 static int
110 dkwedge_discover_gpt(struct disk *pdk, struct vnode *vp)
111 {
112 static const struct uuid ent_type_unused = GPT_ENT_TYPE_UNUSED;
113 static const char gpt_hdr_sig[] = GPT_HDR_SIG;
114 struct dkwedge_info dkw;
115 void *buf;
116 uint32_t secsize;
117 struct gpt_hdr *hdr;
118 struct gpt_ent *ent;
119 uint32_t entries, entsz;
120 daddr_t lba_start, lba_end, lba_table;
121 uint32_t gpe_crc;
122 int error;
123 u_int i;
124 size_t r, n;
125 uint8_t *c;
126
127 secsize = DEV_BSIZE << pdk->dk_blkshift;
128 buf = malloc(secsize, M_DEVBUF, M_WAITOK);
129
130 /*
131 * Note: We don't bother with a Legacy or Protective MBR
132 * here. If a GPT is found, then the search stops, and
133 * the GPT is authoritative.
134 */
135
136 /* Read in the GPT Header. */
137 error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO << pdk->dk_blkshift, buf, secsize);
138 if (error)
139 goto out;
140 hdr = buf;
141
142 /* Validate it. */
143 if (memcmp(gpt_hdr_sig, hdr->hdr_sig, sizeof(hdr->hdr_sig)) != 0) {
144 /* XXX Should check at end-of-disk. */
145 error = ESRCH;
146 goto out;
147 }
148 if (hdr->hdr_revision != htole32(GPT_HDR_REVISION)) {
149 /* XXX Should check at end-of-disk. */
150 error = ESRCH;
151 goto out;
152 }
153 if (le32toh(hdr->hdr_size) > secsize) {
154 /* XXX Should check at end-of-disk. */
155 error = ESRCH;
156 goto out;
157 }
158 if (gpt_verify_header_crc(hdr) == 0) {
159 /* XXX Should check at end-of-disk. */
160 error = ESRCH;
161 goto out;
162 }
163
164 /* XXX Now that we found it, should we validate the backup? */
165
166 {
167 struct uuid disk_guid;
168 char guid_str[UUID_STR_LEN];
169 uuid_dec_le(hdr->hdr_guid, &disk_guid);
170 uuid_snprintf(guid_str, sizeof(guid_str), &disk_guid);
171 aprint_verbose("%s: GPT GUID: %s\n", pdk->dk_name, guid_str);
172 }
173
174 entries = le32toh(hdr->hdr_entries);
175 entsz = roundup(le32toh(hdr->hdr_entsz), 8);
176 if (entsz > roundup(sizeof(struct gpt_ent), 8)) {
177 aprint_error("%s: bogus GPT entry size: %u\n",
178 pdk->dk_name, le32toh(hdr->hdr_entsz));
179 error = EINVAL;
180 goto out;
181 }
182 gpe_crc = le32toh(hdr->hdr_crc_table);
183
184 /* XXX Clamp entries at 512 for now. */
185 if (entries > 512) {
186 aprint_error("%s: WARNING: clamping number of GPT entries to "
187 "512 (was %u)\n", pdk->dk_name, entries);
188 entries = 512;
189 }
190
191 lba_start = le64toh(hdr->hdr_lba_start);
192 lba_end = le64toh(hdr->hdr_lba_end);
193 lba_table = le64toh(hdr->hdr_lba_table);
194 if (lba_start < 0 || lba_end < 0 || lba_table < 0) {
195 aprint_error("%s: GPT block numbers out of range\n",
196 pdk->dk_name);
197 error = EINVAL;
198 goto out;
199 }
200
201 free(buf, M_DEVBUF);
202 buf = malloc(roundup(entries * entsz, secsize), M_DEVBUF, M_WAITOK);
203 error = dkwedge_read(pdk, vp, lba_table << pdk->dk_blkshift, buf,
204 roundup(entries * entsz, secsize));
205 if (error) {
206 /* XXX Should check alternate location. */
207 aprint_error("%s: unable to read GPT partition array, "
208 "error = %d\n", pdk->dk_name, error);
209 goto out;
210 }
211
212 if (crc32(0, buf, entries * entsz) != gpe_crc) {
213 /* XXX Should check alternate location. */
214 aprint_error("%s: bad GPT partition array CRC\n",
215 pdk->dk_name);
216 error = EINVAL;
217 goto out;
218 }
219
220 /*
221 * Walk the partitions, adding a wedge for each type we know about.
222 */
223 for (i = 0; i < entries; i++) {
224 struct uuid ptype_guid, ent_guid;
225 const char *ptype;
226 int j;
227 char ptype_guid_str[UUID_STR_LEN], ent_guid_str[UUID_STR_LEN];
228
229 ent = (struct gpt_ent *)((char *)buf + (i * entsz));
230
231 uuid_dec_le(ent->ent_type, &ptype_guid);
232 if (memcmp(&ptype_guid, &ent_type_unused,
233 sizeof(ptype_guid)) == 0)
234 continue;
235
236 uuid_dec_le(ent->ent_guid, &ent_guid);
237
238 uuid_snprintf(ptype_guid_str, sizeof(ptype_guid_str),
239 &ptype_guid);
240 uuid_snprintf(ent_guid_str, sizeof(ent_guid_str),
241 &ent_guid);
242
243 /* figure out the type */
244 ptype = gpt_ptype_guid_to_str(&ptype_guid);
245 strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
246
247 strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
248 dkw.dkw_offset = le64toh(ent->ent_lba_start);
249 dkw.dkw_size = le64toh(ent->ent_lba_end) - dkw.dkw_offset + 1;
250
251 /* XXX Make sure it falls within the disk's data area. */
252
253 if (ent->ent_name[0] == 0x0000)
254 strlcpy(dkw.dkw_wname, ent_guid_str, sizeof(dkw.dkw_wname));
255 else {
256 c = dkw.dkw_wname;
257 r = sizeof(dkw.dkw_wname) - 1;
258 for (j = 0; j < __arraycount(ent->ent_name)
259 && ent->ent_name[j] != 0x0000; j++) {
260 n = wput_utf8(c, r, le16toh(ent->ent_name[j]));
261 if (n == 0)
262 break;
263 c += n; r -= n;
264 }
265 *c = '\0';
266 }
267
268 /*
269 * Try with the partition name first. If that fails,
270 * use the GUID string. If that fails, punt.
271 */
272 if ((error = dkwedge_add(&dkw)) == EEXIST &&
273 strcmp(dkw.dkw_wname, ent_guid_str) != 0) {
274 char orig[sizeof(dkw.dkw_wname)];
275 strlcpy(orig, dkw.dkw_wname, sizeof(orig));
276 strlcpy(dkw.dkw_wname, ent_guid_str, sizeof(dkw.dkw_wname));
277 error = dkwedge_add(&dkw);
278 if (!error)
279 aprint_error("%s: wedge named '%s' already "
280 "existed, using '%s'\n", pdk->dk_name,
281 orig, ent_guid_str);
282 }
283 if (error == EEXIST)
284 aprint_error("%s: wedge named '%s' already exists, "
285 "manual intervention required\n", pdk->dk_name,
286 dkw.dkw_wname);
287 else if (error)
288 aprint_error("%s: error %d adding entry %u (%s), "
289 "type %s\n", pdk->dk_name, error, i, ent_guid_str,
290 ptype_guid_str);
291 }
292 error = 0;
293
294 out:
295 free(buf, M_DEVBUF);
296 return (error);
297 }
298
299 DKWEDGE_DISCOVERY_METHOD_DECL(GPT, 0, dkwedge_discover_gpt);
300