Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_gpt.c revision 1.23
      1 /*	$NetBSD: dkwedge_gpt.c,v 1.23 2019/06/22 06:45:46 maxv 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.23 2019/06/22 06:45:46 maxv 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 	{ GPT_ENT_TYPE_MS_BASIC_DATA,		DKW_PTYPE_NTFS },
     80 	{ GPT_ENT_TYPE_LINUX_DATA,		DKW_PTYPE_EXT2FS },
     81 };
     82 
     83 static const char *
     84 gpt_ptype_guid_to_str(const struct uuid *guid)
     85 {
     86 	int i;
     87 
     88 	for (i = 0; i < __arraycount(gpt_ptype_guid_to_str_tab); i++) {
     89 		if (memcmp(&gpt_ptype_guid_to_str_tab[i].ptype_guid,
     90 			   guid, sizeof(*guid)) == 0)
     91 			return (gpt_ptype_guid_to_str_tab[i].ptype_str);
     92 	}
     93 
     94 	return (DKW_PTYPE_UNKNOWN);
     95 }
     96 
     97 static int
     98 gpt_verify_header_crc(struct gpt_hdr *hdr)
     99 {
    100 	uint32_t crc;
    101 	int rv;
    102 
    103 	crc = hdr->hdr_crc_self;
    104 	hdr->hdr_crc_self = 0;
    105 	rv = le32toh(crc) == crc32(0, (void *)hdr, le32toh(hdr->hdr_size));
    106 	hdr->hdr_crc_self = crc;
    107 
    108 	return (rv);
    109 }
    110 
    111 static int
    112 dkwedge_discover_gpt(struct disk *pdk, struct vnode *vp)
    113 {
    114 	static const struct uuid ent_type_unused = GPT_ENT_TYPE_UNUSED;
    115 	static const char gpt_hdr_sig[] = GPT_HDR_SIG;
    116 	struct dkwedge_info dkw;
    117 	void *buf;
    118 	uint32_t secsize;
    119 	struct gpt_hdr *hdr;
    120 	struct gpt_ent *ent;
    121 	uint32_t entries, entsz;
    122 	daddr_t lba_start, lba_end, lba_table;
    123 	uint32_t gpe_crc;
    124 	int error;
    125 	u_int i;
    126 	size_t r, n;
    127 	uint8_t *c;
    128 
    129 	secsize = DEV_BSIZE << pdk->dk_blkshift;
    130 	buf = malloc(secsize, M_DEVBUF, M_WAITOK);
    131 
    132 	/*
    133 	 * Note: We don't bother with a Legacy or Protective MBR
    134 	 * here.  If a GPT is found, then the search stops, and
    135 	 * the GPT is authoritative.
    136 	 */
    137 
    138 	/* Read in the GPT Header. */
    139 	error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO << pdk->dk_blkshift, buf, secsize);
    140 	if (error)
    141 		goto out;
    142 	hdr = buf;
    143 
    144 	/* Validate it. */
    145 	if (memcmp(gpt_hdr_sig, hdr->hdr_sig, sizeof(hdr->hdr_sig)) != 0) {
    146 		/* XXX Should check at end-of-disk. */
    147 		error = ESRCH;
    148 		goto out;
    149 	}
    150 	if (hdr->hdr_revision != htole32(GPT_HDR_REVISION)) {
    151 		/* XXX Should check at end-of-disk. */
    152 		error = ESRCH;
    153 		goto out;
    154 	}
    155 	if (le32toh(hdr->hdr_size) > secsize) {
    156 		/* XXX Should check at end-of-disk. */
    157 		error = ESRCH;
    158 		goto out;
    159 	}
    160 	if (gpt_verify_header_crc(hdr) == 0) {
    161 		/* XXX Should check at end-of-disk. */
    162 		error = ESRCH;
    163 		goto out;
    164 	}
    165 
    166 	/* XXX Now that we found it, should we validate the backup? */
    167 
    168 	{
    169 		struct uuid disk_guid;
    170 		char guid_str[UUID_STR_LEN];
    171 		uuid_dec_le(hdr->hdr_guid, &disk_guid);
    172 		uuid_snprintf(guid_str, sizeof(guid_str), &disk_guid);
    173 		aprint_verbose("%s: GPT GUID: %s\n", pdk->dk_name, guid_str);
    174 	}
    175 
    176 	entries = le32toh(hdr->hdr_entries);
    177 	entsz = roundup(le32toh(hdr->hdr_entsz), 8);
    178 	if (entsz != sizeof(struct gpt_ent)) {
    179 		aprint_error("%s: bogus GPT entry size: %u\n",
    180 		    pdk->dk_name, le32toh(hdr->hdr_entsz));
    181 		error = EINVAL;
    182 		goto out;
    183 	}
    184 	gpe_crc = le32toh(hdr->hdr_crc_table);
    185 
    186 	/* XXX Clamp entries at 512 for now. */
    187 	if (entries > 512) {
    188 		aprint_error("%s: WARNING: clamping number of GPT entries to "
    189 		    "512 (was %u)\n", pdk->dk_name, entries);
    190 		entries = 512;
    191 	}
    192 
    193 	lba_start = le64toh(hdr->hdr_lba_start);
    194 	lba_end = le64toh(hdr->hdr_lba_end);
    195 	lba_table = le64toh(hdr->hdr_lba_table);
    196 	if (lba_start < 0 || lba_end < 0 || lba_table < 0) {
    197 		aprint_error("%s: GPT block numbers out of range\n",
    198 		    pdk->dk_name);
    199 		error = EINVAL;
    200 		goto out;
    201 	}
    202 
    203 	free(buf, M_DEVBUF);
    204 	buf = malloc(roundup(entries * entsz, secsize), M_DEVBUF, M_WAITOK);
    205 	error = dkwedge_read(pdk, vp, lba_table << pdk->dk_blkshift, buf,
    206 			     roundup(entries * entsz, secsize));
    207 	if (error) {
    208 		/* XXX Should check alternate location. */
    209 		aprint_error("%s: unable to read GPT partition array, "
    210 		    "error = %d\n", pdk->dk_name, error);
    211 		goto out;
    212 	}
    213 
    214 	if (crc32(0, buf, entries * entsz) != gpe_crc) {
    215 		/* XXX Should check alternate location. */
    216 		aprint_error("%s: bad GPT partition array CRC\n",
    217 		    pdk->dk_name);
    218 		error = EINVAL;
    219 		goto out;
    220 	}
    221 
    222 	/*
    223 	 * Walk the partitions, adding a wedge for each type we know about.
    224 	 */
    225 	for (i = 0; i < entries; i++) {
    226 		struct uuid ptype_guid, ent_guid;
    227 		const char *ptype;
    228 		int j;
    229 		char ptype_guid_str[UUID_STR_LEN], ent_guid_str[UUID_STR_LEN];
    230 
    231 		ent = (struct gpt_ent *)((char *)buf + (i * entsz));
    232 
    233 		uuid_dec_le(ent->ent_type, &ptype_guid);
    234 		if (memcmp(&ptype_guid, &ent_type_unused,
    235 			   sizeof(ptype_guid)) == 0)
    236 			continue;
    237 
    238 		uuid_dec_le(ent->ent_guid, &ent_guid);
    239 
    240 		uuid_snprintf(ptype_guid_str, sizeof(ptype_guid_str),
    241 		    &ptype_guid);
    242 		uuid_snprintf(ent_guid_str, sizeof(ent_guid_str),
    243 		    &ent_guid);
    244 
    245 		/* figure out the type */
    246 		ptype = gpt_ptype_guid_to_str(&ptype_guid);
    247 		strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
    248 
    249 		strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
    250 		dkw.dkw_offset = le64toh(ent->ent_lba_start);
    251 		dkw.dkw_size = le64toh(ent->ent_lba_end) - dkw.dkw_offset + 1;
    252 
    253 		/* XXX Make sure it falls within the disk's data area. */
    254 
    255 		if (ent->ent_name[0] == 0x0000)
    256 			strlcpy(dkw.dkw_wname, ent_guid_str, sizeof(dkw.dkw_wname));
    257 		else {
    258 			c = dkw.dkw_wname;
    259 			r = sizeof(dkw.dkw_wname) - 1;
    260 			for (j = 0; j < __arraycount(ent->ent_name)
    261 			    && ent->ent_name[j] != 0x0000; j++) {
    262 				n = wput_utf8(c, r, le16toh(ent->ent_name[j]));
    263 				if (n == 0)
    264 					break;
    265 				c += n; r -= n;
    266 			}
    267 			*c = '\0';
    268 		}
    269 
    270 		/*
    271 		 * Try with the partition name first.  If that fails,
    272 		 * use the GUID string.  If that fails, punt.
    273 		 */
    274 		if ((error = dkwedge_add(&dkw)) == EEXIST &&
    275 		    strcmp(dkw.dkw_wname, ent_guid_str) != 0) {
    276 			char orig[sizeof(dkw.dkw_wname)];
    277 			strlcpy(orig, dkw.dkw_wname, sizeof(orig));
    278 			strlcpy(dkw.dkw_wname, ent_guid_str, sizeof(dkw.dkw_wname));
    279 			error = dkwedge_add(&dkw);
    280 			if (!error)
    281 				aprint_error("%s: wedge named '%s' already "
    282 				    "existed, using '%s'\n", pdk->dk_name,
    283 				    orig, ent_guid_str);
    284 		}
    285 		if (error == EEXIST)
    286 			aprint_error("%s: wedge named '%s' already exists, "
    287 			    "manual intervention required\n", pdk->dk_name,
    288 			    dkw.dkw_wname);
    289 		else if (error)
    290 			aprint_error("%s: error %d adding entry %u (%s), "
    291 			    "type %s\n", pdk->dk_name, error, i, ent_guid_str,
    292 			    ptype_guid_str);
    293 	}
    294 	error = 0;
    295 
    296  out:
    297 	free(buf, M_DEVBUF);
    298 	return (error);
    299 }
    300 
    301 DKWEDGE_DISCOVERY_METHOD_DECL(GPT, 0, dkwedge_discover_gpt);
    302