Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_gpt.c revision 1.3
      1  1.3   martin /*	$NetBSD: dkwedge_gpt.c,v 1.3 2006/08/13 18:45:08 martin 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej /*
     40  1.1  thorpej  * EFI GUID Partition Table support for disk wedges
     41  1.1  thorpej  */
     42  1.1  thorpej 
     43  1.1  thorpej #include <sys/cdefs.h>
     44  1.3   martin __KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.3 2006/08/13 18:45:08 martin Exp $");
     45  1.3   martin 
     46  1.3   martin #include "opt_dkwedge.h"
     47  1.3   martin #ifdef DKWEDGE_METHOD_GPT
     48  1.1  thorpej 
     49  1.1  thorpej #include <sys/param.h>
     50  1.1  thorpej #include <sys/systm.h>
     51  1.1  thorpej #include <sys/proc.h>
     52  1.1  thorpej #include <sys/errno.h>
     53  1.1  thorpej #include <sys/disk.h>
     54  1.1  thorpej #include <sys/vnode.h>
     55  1.1  thorpej #include <sys/malloc.h>
     56  1.1  thorpej 
     57  1.1  thorpej #include <sys/disklabel_gpt.h>
     58  1.1  thorpej #include <sys/uuid.h>
     59  1.1  thorpej 
     60  1.1  thorpej static const struct {
     61  1.1  thorpej 	struct uuid ptype_guid;
     62  1.1  thorpej 	const char *ptype_str;
     63  1.1  thorpej } gpt_ptype_guid_to_str_tab[] = {
     64  1.1  thorpej 	{ GPT_ENT_TYPE_EFI,		"msdos" },	/* XXX yes? */
     65  1.1  thorpej #if 0
     66  1.1  thorpej 	{ GPT_ENT_TYPE_FREEBSD,		??? },
     67  1.1  thorpej #endif
     68  1.1  thorpej 	{ GPT_ENT_TYPE_FREEBSD_SWAP,	"swap" },	/* XXX for now */
     69  1.1  thorpej 	{ GPT_ENT_TYPE_FREEBSD_UFS,	"ffs" },	/* XXX for now */
     70  1.1  thorpej 
     71  1.1  thorpej 	/* XXX What about the MS and Linux types? */
     72  1.1  thorpej 
     73  1.1  thorpej 	{ { 0 },			NULL },
     74  1.1  thorpej };
     75  1.1  thorpej 
     76  1.1  thorpej static const char *
     77  1.1  thorpej gpt_ptype_guid_to_str(const struct uuid *guid)
     78  1.1  thorpej {
     79  1.1  thorpej 	int i;
     80  1.1  thorpej 
     81  1.1  thorpej 	for (i = 0; gpt_ptype_guid_to_str_tab[i].ptype_str != NULL; i++) {
     82  1.1  thorpej 		if (memcmp(&gpt_ptype_guid_to_str_tab[i].ptype_guid,
     83  1.1  thorpej 			   guid, sizeof(*guid)) == 0)
     84  1.1  thorpej 			return (gpt_ptype_guid_to_str_tab[i].ptype_str);
     85  1.1  thorpej 	}
     86  1.1  thorpej 
     87  1.1  thorpej 	return (NULL);
     88  1.1  thorpej }
     89  1.1  thorpej 
     90  1.1  thorpej static const uint32_t gpt_crc_tab[16] = {
     91  1.1  thorpej 	0x00000000U, 0x1db71064U, 0x3b6e20c8U, 0x26d930acU,
     92  1.1  thorpej 	0x76dc4190U, 0x6b6b51f4U, 0x4db26158U, 0x5005713cU,
     93  1.1  thorpej 	0xedb88320U, 0xf00f9344U, 0xd6d6a3e8U, 0xcb61b38cU,
     94  1.1  thorpej 	0x9b64c2b0U, 0x86d3d2d4U, 0xa00ae278U, 0xbdbdf21cU
     95  1.1  thorpej };
     96  1.1  thorpej 
     97  1.1  thorpej static uint32_t
     98  1.1  thorpej gpt_crc32(const void *vbuf, size_t len)
     99  1.1  thorpej {
    100  1.1  thorpej 	const uint8_t *buf = vbuf;
    101  1.1  thorpej 	uint32_t crc;
    102  1.1  thorpej 
    103  1.1  thorpej 	crc = 0xffffffffU;
    104  1.1  thorpej 	while (len--) {
    105  1.1  thorpej 		crc ^= *buf++;
    106  1.1  thorpej 		crc = (crc >> 4) ^ gpt_crc_tab[crc & 0xf];
    107  1.1  thorpej 		crc = (crc >> 4) ^ gpt_crc_tab[crc & 0xf];
    108  1.1  thorpej 	}
    109  1.1  thorpej 
    110  1.1  thorpej 	return (crc ^ 0xffffffffU);
    111  1.1  thorpej }
    112  1.1  thorpej 
    113  1.1  thorpej static int
    114  1.1  thorpej gpt_verify_header_crc(struct gpt_hdr *hdr)
    115  1.1  thorpej {
    116  1.1  thorpej 	uint32_t crc;
    117  1.1  thorpej 	int rv;
    118  1.1  thorpej 
    119  1.1  thorpej 	crc = hdr->hdr_crc_self;
    120  1.1  thorpej 	hdr->hdr_crc_self = 0;
    121  1.1  thorpej 	rv = le32toh(crc) == gpt_crc32(hdr, le32toh(hdr->hdr_size));
    122  1.1  thorpej 	hdr->hdr_crc_self = crc;
    123  1.1  thorpej 
    124  1.1  thorpej 	return (rv);
    125  1.1  thorpej }
    126  1.1  thorpej 
    127  1.1  thorpej static int
    128  1.1  thorpej dkwedge_discover_gpt(struct disk *pdk, struct vnode *vp)
    129  1.1  thorpej {
    130  1.1  thorpej 	static const struct uuid ent_type_unused = GPT_ENT_TYPE_UNUSED;
    131  1.1  thorpej 	static const char gpt_hdr_sig[] = GPT_HDR_SIG;
    132  1.1  thorpej 	struct dkwedge_info dkw;
    133  1.1  thorpej 	void *buf;
    134  1.1  thorpej 	struct gpt_hdr *hdr;
    135  1.1  thorpej 	struct gpt_ent *ent;
    136  1.1  thorpej 	uint32_t entries, entsz;
    137  1.1  thorpej 	daddr_t lba_start, lba_end, lba_table;
    138  1.1  thorpej 	uint32_t gpe_crc;
    139  1.1  thorpej 	int error;
    140  1.1  thorpej 	u_int i;
    141  1.1  thorpej 
    142  1.1  thorpej 	buf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
    143  1.1  thorpej 
    144  1.1  thorpej 	/*
    145  1.1  thorpej 	 * Note: We don't bother with a Legacy or Protective MBR
    146  1.1  thorpej 	 * here.  If a GPT is found, then the search stops, and
    147  1.1  thorpej 	 * the GPT is authoritative.
    148  1.1  thorpej 	 */
    149  1.1  thorpej 
    150  1.1  thorpej 	/* Read in the GPT Header. */
    151  1.1  thorpej 	error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO, buf, DEV_BSIZE);
    152  1.1  thorpej 	if (error)
    153  1.1  thorpej 		goto out;
    154  1.1  thorpej 	hdr = buf;
    155  1.1  thorpej 
    156  1.1  thorpej 	/* Validate it. */
    157  1.1  thorpej 	if (memcmp(gpt_hdr_sig, hdr->hdr_sig, sizeof(hdr->hdr_sig)) != 0) {
    158  1.1  thorpej 		/* XXX Should check at end-of-disk. */
    159  1.1  thorpej 		error = ESRCH;
    160  1.1  thorpej 		goto out;
    161  1.1  thorpej 	}
    162  1.1  thorpej 	if (hdr->hdr_revision != htole32(GPT_HDR_REVISION)) {
    163  1.1  thorpej 		/* XXX Should check at end-of-disk. */
    164  1.1  thorpej 		error = ESRCH;
    165  1.1  thorpej 		goto out;
    166  1.1  thorpej 	}
    167  1.1  thorpej 	if (le32toh(hdr->hdr_size) > DEV_BSIZE) {
    168  1.1  thorpej 		/* XXX Should check at end-of-disk. */
    169  1.1  thorpej 		error = ESRCH;
    170  1.1  thorpej 		goto out;
    171  1.1  thorpej 	}
    172  1.1  thorpej 	if (gpt_verify_header_crc(hdr) == 0) {
    173  1.1  thorpej 		/* XXX Should check at end-of-disk. */
    174  1.1  thorpej 		error = ESRCH;
    175  1.1  thorpej 		goto out;
    176  1.1  thorpej 	}
    177  1.1  thorpej 
    178  1.1  thorpej 	/* XXX Now that we found it, should we validate the backup? */
    179  1.1  thorpej 
    180  1.1  thorpej 	{
    181  1.1  thorpej 		struct uuid disk_guid;
    182  1.1  thorpej 		char guid_str[UUID_STR_LEN];
    183  1.1  thorpej 		uuid_dec_le(hdr->hdr_guid, &disk_guid);
    184  1.1  thorpej 		uuid_snprintf(guid_str, sizeof(guid_str), &disk_guid);
    185  1.1  thorpej 		aprint_verbose("%s: GPT GUID: %s\n", pdk->dk_name, guid_str);
    186  1.1  thorpej 	}
    187  1.1  thorpej 
    188  1.1  thorpej 	entries = le32toh(hdr->hdr_entries);
    189  1.1  thorpej 	entsz = roundup(le32toh(hdr->hdr_entsz), 8);
    190  1.1  thorpej 	if (entsz > roundup(sizeof(struct gpt_ent), 8)) {
    191  1.1  thorpej 		aprint_error("%s: bogus GPT entry size: %u\n",
    192  1.1  thorpej 		    pdk->dk_name, le32toh(hdr->hdr_entsz));
    193  1.1  thorpej 		error = EINVAL;
    194  1.1  thorpej 		goto out;
    195  1.1  thorpej 	}
    196  1.1  thorpej 	gpe_crc = le32toh(hdr->hdr_crc_table);
    197  1.1  thorpej 
    198  1.1  thorpej 	/* XXX Clamp entries at 128 for now. */
    199  1.1  thorpej 	if (entries > 128) {
    200  1.1  thorpej 		aprint_error("%s: WARNING: clamping number of GPT entries to "
    201  1.1  thorpej 		    "128 (was %u)\n", pdk->dk_name, entries);
    202  1.1  thorpej 		entries = 128;
    203  1.1  thorpej 	}
    204  1.1  thorpej 
    205  1.1  thorpej 	lba_start = le64toh(hdr->hdr_lba_start);
    206  1.1  thorpej 	lba_end = le64toh(hdr->hdr_lba_end);
    207  1.1  thorpej 	lba_table = le64toh(hdr->hdr_lba_table);
    208  1.1  thorpej 	if (lba_start < 0 || lba_end < 0 || lba_table < 0) {
    209  1.1  thorpej 		aprint_error("%s: GPT block numbers out of range\n",
    210  1.1  thorpej 		    pdk->dk_name);
    211  1.1  thorpej 		error = EINVAL;
    212  1.1  thorpej 		goto out;
    213  1.1  thorpej 	}
    214  1.1  thorpej 
    215  1.1  thorpej 	free(buf, M_DEVBUF);
    216  1.1  thorpej 	buf = malloc(roundup(entries * entsz, DEV_BSIZE), M_DEVBUF, M_WAITOK);
    217  1.1  thorpej 	error = dkwedge_read(pdk, vp, lba_table, buf,
    218  1.1  thorpej 			     roundup(entries * entsz, DEV_BSIZE));
    219  1.1  thorpej 	if (error) {
    220  1.1  thorpej 		/* XXX Should check alternate location. */
    221  1.1  thorpej 		aprint_error("%s: unable to read GPT partition array, "
    222  1.1  thorpej 		    "error = %d\n", pdk->dk_name, error);
    223  1.1  thorpej 		goto out;
    224  1.1  thorpej 	}
    225  1.1  thorpej 
    226  1.1  thorpej 	if (gpt_crc32(buf, entries * entsz) != gpe_crc) {
    227  1.1  thorpej 		/* XXX Should check alternate location. */
    228  1.1  thorpej 		aprint_error("%s: bad GPT partition array CRC\n",
    229  1.1  thorpej 		    pdk->dk_name);
    230  1.1  thorpej 		error = EINVAL;
    231  1.1  thorpej 		goto out;
    232  1.1  thorpej 	}
    233  1.1  thorpej 
    234  1.1  thorpej 	/*
    235  1.1  thorpej 	 * Walk the partitions, adding a wedge for each type we know about.
    236  1.1  thorpej 	 */
    237  1.1  thorpej 	for (i = 0; i < entries; i++) {
    238  1.1  thorpej 		struct uuid ptype_guid, ent_guid;
    239  1.1  thorpej 		const char *ptype;
    240  1.1  thorpej 		int j;
    241  1.1  thorpej 		char ptype_guid_str[UUID_STR_LEN], ent_guid_str[UUID_STR_LEN];
    242  1.1  thorpej 
    243  1.1  thorpej 		ent = (struct gpt_ent *)((caddr_t)buf + (i * entsz));
    244  1.1  thorpej 
    245  1.1  thorpej 		uuid_dec_le(ent->ent_type, &ptype_guid);
    246  1.1  thorpej 		if (memcmp(&ptype_guid, &ent_type_unused,
    247  1.1  thorpej 			   sizeof(ptype_guid)) == 0)
    248  1.1  thorpej 			continue;
    249  1.1  thorpej 
    250  1.1  thorpej 		uuid_dec_le(ent->ent_guid, &ent_guid);
    251  1.1  thorpej 
    252  1.1  thorpej 		uuid_snprintf(ptype_guid_str, sizeof(ptype_guid_str),
    253  1.1  thorpej 		    &ptype_guid);
    254  1.1  thorpej 		uuid_snprintf(ent_guid_str, sizeof(ent_guid_str),
    255  1.1  thorpej 		    &ent_guid);
    256  1.1  thorpej 
    257  1.1  thorpej 		/* Skip it if we don't grok this ptype. */
    258  1.1  thorpej 		if ((ptype = gpt_ptype_guid_to_str(&ptype_guid)) == NULL) {
    259  1.1  thorpej 			/*
    260  1.1  thorpej 			 * XXX Should probably just add these... maybe
    261  1.1  thorpej 			 * XXX just have an empty ptype?
    262  1.1  thorpej 			 */
    263  1.1  thorpej 			aprint_verbose("%s: skipping entry %u (%s), type %s\n",
    264  1.1  thorpej 			    pdk->dk_name, i, ent_guid_str, ptype_guid_str);
    265  1.1  thorpej 			continue;
    266  1.1  thorpej 		}
    267  1.1  thorpej 		strcpy(dkw.dkw_ptype, ptype);
    268  1.1  thorpej 
    269  1.1  thorpej 		strcpy(dkw.dkw_parent, pdk->dk_name);
    270  1.1  thorpej 		dkw.dkw_offset = le64toh(ent->ent_lba_start);
    271  1.1  thorpej 		dkw.dkw_size = le64toh(ent->ent_lba_end) - dkw.dkw_offset + 1;
    272  1.1  thorpej 
    273  1.1  thorpej 		/* XXX Make sure it falls within the disk's data area. */
    274  1.1  thorpej 
    275  1.1  thorpej 		if (ent->ent_name[0] == 0x0000)
    276  1.1  thorpej 			strcpy(dkw.dkw_wname, ent_guid_str);
    277  1.1  thorpej 		else {
    278  1.1  thorpej 			for (j = 0; ent->ent_name[j] != 0x0000; j++) {
    279  1.1  thorpej 				/* XXX UTF-16 -> UTF-8 */
    280  1.1  thorpej 				dkw.dkw_wname[j] =
    281  1.1  thorpej 				    le16toh(ent->ent_name[j]) & 0xff;
    282  1.1  thorpej 			}
    283  1.1  thorpej 			dkw.dkw_wname[j] = '\0';
    284  1.1  thorpej 		}
    285  1.1  thorpej 
    286  1.1  thorpej 		/*
    287  1.1  thorpej 		 * Try with the partition name first.  If that fails,
    288  1.1  thorpej 		 * use the GUID string.  If that fails, punt.
    289  1.1  thorpej 		 */
    290  1.1  thorpej 		if ((error = dkwedge_add(&dkw)) == EEXIST) {
    291  1.1  thorpej 			aprint_error("%s: wedge named '%s' already exists, "
    292  1.1  thorpej 			    "trying '%s'\n", pdk->dk_name,
    293  1.1  thorpej 			    dkw.dkw_wname, /* XXX Unicode */
    294  1.1  thorpej 			    ent_guid_str);
    295  1.1  thorpej 			strcpy(dkw.dkw_wname, ent_guid_str);
    296  1.1  thorpej 			error = dkwedge_add(&dkw);
    297  1.1  thorpej 		}
    298  1.1  thorpej 		if (error == EEXIST)
    299  1.1  thorpej 			aprint_error("%s: wedge named '%s' already exists, "
    300  1.1  thorpej 			    "manual intervention required\n", pdk->dk_name,
    301  1.1  thorpej 			    dkw.dkw_wname);
    302  1.1  thorpej 		else if (error)
    303  1.1  thorpej 			aprint_error("%s: error %d adding entry %u (%s), "
    304  1.1  thorpej 			    "type %s\n", pdk->dk_name, error, i, ent_guid_str,
    305  1.1  thorpej 			    ptype_guid_str);
    306  1.1  thorpej 	}
    307  1.1  thorpej 	error = 0;
    308  1.1  thorpej 
    309  1.1  thorpej  out:
    310  1.1  thorpej 	free(buf, M_DEVBUF);
    311  1.1  thorpej 	return (error);
    312  1.1  thorpej }
    313  1.1  thorpej 
    314  1.1  thorpej DKWEDGE_DISCOVERY_METHOD_DECL(GPT, 0, dkwedge_discover_gpt);
    315  1.3   martin 
    316  1.3   martin #endif
    317  1.3   martin 
    318