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