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