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