Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_bsdlabel.c revision 1.14
      1 /*	$NetBSD: dkwedge_bsdlabel.c,v 1.14 2007/06/09 02:10:30 dyoung 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Adapted from kern/subr_disk_mbr.c:
     41  *
     42  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
     43  * All rights reserved.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  *
     69  *	@(#)ufs_disksubr.c      7.16 (Berkeley) 5/4/91
     70  */
     71 
     72 /*
     73  * 4.4BSD disklabel support for disk wedges
     74  *
     75  * Here is the basic search algorithm in use here:
     76  *
     77  * For historical reasons, we scan for x86-style MBR partitions looking
     78  * for a MBR_PTYPE_NETBSD (or MBR_PTYPE_386BSD) partition.  The first
     79  * 4.4BSD disklabel found in the 2nd sector of such a partition is used.
     80  * We assume that the 4.4BSD disklabel describes all partitions on the
     81  * disk; we do not use any partition information from the MBR partition
     82  * table.
     83  *
     84  * If that fails, then we fall back on a table of known locations for
     85  * various platforms.
     86  */
     87 
     88 #include <sys/cdefs.h>
     89 __KERNEL_RCSID(0, "$NetBSD: dkwedge_bsdlabel.c,v 1.14 2007/06/09 02:10:30 dyoung Exp $");
     90 
     91 #include <sys/param.h>
     92 #ifdef _KERNEL
     93 #include <sys/systm.h>
     94 #endif
     95 #include <sys/proc.h>
     96 #include <sys/errno.h>
     97 #include <sys/disk.h>
     98 #include <sys/vnode.h>
     99 #include <sys/malloc.h>
    100 
    101 #include <sys/bootblock.h>
    102 #include <sys/disklabel.h>
    103 
    104 #define	BSD44_MBR_LABELSECTOR	1
    105 
    106 #define	DISKLABEL_SIZE(x)						\
    107 	(offsetof(struct disklabel, d_partitions) +			\
    108 	 (sizeof(struct partition) * (x)))
    109 
    110 /*
    111  * Note the smallest MAXPARTITIONS was 8, so we allow a disklabel
    112  * that size to be locted at the end of the sector.
    113  */
    114 #define	DISKLABEL_MINSIZE	DISKLABEL_SIZE(8)
    115 
    116 /*
    117  * Table of known platform-specific disklabel locations.
    118  */
    119 static const struct disklabel_location {
    120 	daddr_t		label_sector;	/* sector containing label */
    121 	size_t		label_offset;	/* byte offset of label in sector */
    122 } disklabel_locations[] = {
    123 	{ 0,	0 },	/* mvme68k, next68k */
    124 	{ 0,	64 },	/* algor, alpha, amiga, amigappc, evbmips, evbppc,
    125 			   luna68k, mac68k, macppc, news68k, newsmips,
    126 			   pc532, pdp11, pmax, vax, x68k */
    127 	{ 0,	128 },	/* sparc, sun68k */
    128 	{ 1,	0 },	/* amd64, arc, arm, bebox, cobalt, evbppc, hp700,
    129 			   hpcarm, hpcmips, i386, ibmnws, mipsco, mvmeppc,
    130 			   ofppc, playstation2, pmppc, prep, sandpoint,
    131 			   sbmips, sgimips, sh3 */
    132 	/* XXX atari is weird */
    133 	{ 2,	0 },	/* cesfic, hp300 */
    134 
    135 	{ -1,	0 },
    136 };
    137 
    138 #define	SCAN_CONTINUE	0
    139 #define	SCAN_FOUND	1
    140 #define	SCAN_ERROR	2
    141 
    142 typedef struct mbr_args {
    143 	struct disk	*pdk;
    144 	struct vnode	*vp;
    145 	void		*buf;
    146 	int		error;
    147 } mbr_args_t;
    148 
    149 static const char *
    150 bsdlabel_fstype_to_str(uint8_t fstype)
    151 {
    152 	const char *str;
    153 
    154 	switch (fstype) {
    155 	case FS_UNUSED:		str = DKW_PTYPE_UNUSED;		break;
    156 	case FS_SWAP:		str = DKW_PTYPE_SWAP;		break;
    157 	case FS_BSDFFS:		str = DKW_PTYPE_FFS;		break;
    158 	case FS_MSDOS:		str = DKW_PTYPE_FAT;		break;
    159 	case FS_BSDLFS:		str = DKW_PTYPE_LFS;		break;
    160 	case FS_ISO9660:	str = DKW_PTYPE_ISO9660;	break;
    161 	case FS_ADOS:		str = DKW_PTYPE_AMIGADOS;	break;
    162 	case FS_HFS:		str = DKW_PTYPE_APPLEHFS;	break;
    163 	case FS_FILECORE:	str = DKW_PTYPE_FILECORE;	break;
    164 	case FS_EX2FS:		str = DKW_PTYPE_EXT2FS;		break;
    165 	case FS_NTFS:		str = DKW_PTYPE_NTFS;		break;
    166 	case FS_RAID:		str = DKW_PTYPE_RAIDFRAME;	break;
    167 	case FS_CCD:		str = DKW_PTYPE_CCD;		break;
    168 	case FS_APPLEUFS:	str = DKW_PTYPE_APPLEUFS;	break;
    169 	default:		str = NULL;			break;
    170 	}
    171 
    172 	return (str);
    173 }
    174 
    175 static void
    176 swap_disklabel(struct disklabel *lp)
    177 {
    178 	int i;
    179 
    180 #define	SWAP16(x)	lp->x = bswap16(lp->x)
    181 #define	SWAP32(x)	lp->x = bswap32(lp->x)
    182 
    183 	SWAP32(d_magic);
    184 	SWAP16(d_type);
    185 	SWAP16(d_subtype);
    186 	SWAP32(d_secsize);
    187 	SWAP32(d_nsectors);
    188 	SWAP32(d_ntracks);
    189 	SWAP32(d_ncylinders);
    190 	SWAP32(d_secpercyl);
    191 	SWAP32(d_secperunit);
    192 	SWAP16(d_sparespertrack);
    193 	SWAP16(d_sparespercyl);
    194 	SWAP32(d_acylinders);
    195 	SWAP16(d_rpm);
    196 	SWAP16(d_interleave);
    197 	SWAP16(d_trackskew);
    198 	SWAP16(d_cylskew);
    199 	SWAP32(d_headswitch);
    200 	SWAP32(d_trkseek);
    201 	SWAP32(d_flags);
    202 
    203 	for (i = 0; i < NDDATA; i++)
    204 		SWAP32(d_drivedata[i]);
    205 	for (i = 0; i < NSPARE; i++)
    206 		SWAP32(d_spare[i]);
    207 
    208 	SWAP32(d_magic2);
    209 	SWAP16(d_checksum);
    210 	SWAP16(d_npartitions);
    211 	SWAP32(d_bbsize);
    212 	SWAP32(d_sbsize);
    213 
    214 	for (i = 0; i < lp->d_npartitions; i++) {
    215 		SWAP32(d_partitions[i].p_size);
    216 		SWAP32(d_partitions[i].p_offset);
    217 		SWAP32(d_partitions[i].p_fsize);
    218 		SWAP16(d_partitions[i].p_cpg);
    219 	}
    220 
    221 #undef SWAP16
    222 #undef SWAP32
    223 }
    224 
    225 /*
    226  * Add wedges for a valid NetBSD disklabel.
    227  */
    228 static void
    229 addwedges(const mbr_args_t *a, const struct disklabel *lp)
    230 {
    231 	int error, i;
    232 
    233 	for (i = 0; i < lp->d_npartitions; i++) {
    234 		struct dkwedge_info dkw;
    235 		const struct partition *p;
    236 		const char *ptype;
    237 
    238 		p = &lp->d_partitions[i];
    239 
    240 		if (p->p_fstype == FS_UNUSED)
    241 			continue;
    242 		if ((ptype = bsdlabel_fstype_to_str(p->p_fstype)) == NULL) {
    243 			/*
    244 			 * XXX Should probably just add these...
    245 			 * XXX maybe just have an empty ptype?
    246 			 */
    247 			aprint_verbose("%s: skipping partition %d, type %d\n",
    248 			    a->pdk->dk_name, i, p->p_fstype);
    249 			continue;
    250 		}
    251 		strcpy(dkw.dkw_ptype, ptype);
    252 
    253 		strcpy(dkw.dkw_parent, a->pdk->dk_name);
    254 		dkw.dkw_offset = p->p_offset;
    255 		dkw.dkw_size = p->p_size;
    256 
    257 		/*
    258 		 * These get historical disk naming style
    259 		 * wedge names.
    260 		 */
    261 		snprintf((char *)&dkw.dkw_wname, sizeof(dkw.dkw_wname),
    262 		    "%s%c", a->pdk->dk_name, 'a' + i);
    263 
    264 		error = dkwedge_add(&dkw);
    265 		if (error == EEXIST)
    266 			aprint_error("%s: wedge named '%s' already "
    267 			    "exists, manual intervention required\n",
    268 			    a->pdk->dk_name, dkw.dkw_wname);
    269 		else if (error)
    270 			aprint_error("%s: error %d adding partition "
    271 			    "%d type %d\n", a->pdk->dk_name, error,
    272 			    i, p->p_fstype);
    273 	}
    274 }
    275 
    276 static int
    277 validate_label(mbr_args_t *a, daddr_t label_sector, size_t label_offset)
    278 {
    279 	struct disklabel *lp;
    280 	void *lp_lim;
    281 	int error, swapped;
    282 	uint16_t npartitions;
    283 
    284 	error = dkwedge_read(a->pdk, a->vp, label_sector, a->buf, DEV_BSIZE);
    285 	if (error) {
    286 		aprint_error("%s: unable to read BSD disklabel @ %" PRId64
    287 		    ", error = %d\n", a->pdk->dk_name, label_sector, error);
    288 		a->error = error;
    289 		return (SCAN_ERROR);
    290 	}
    291 
    292 	/*
    293 	 * We ignore label_offset; this seems to have not been used
    294 	 * consistently in the old code, requiring us to do the search
    295 	 * in the sector.
    296 	 */
    297 	lp = a->buf;
    298 	lp_lim = (char *)a->buf + DEV_BSIZE - DISKLABEL_MINSIZE;
    299 	for (;; lp = (void *)((char *)lp + sizeof(uint32_t))) {
    300 		if ((char *)lp > (char *)lp_lim)
    301 			return (SCAN_CONTINUE);
    302 		label_offset = (size_t)((char *)lp - (char *)a->buf);
    303 		if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
    304 			if (lp->d_magic != bswap32(DISKMAGIC) ||
    305 			    lp->d_magic2 != bswap32(DISKMAGIC))
    306 				continue;
    307 			/* Label is in the other byte order. */
    308 			swapped = 1;
    309 		} else
    310 			swapped = 0;
    311 
    312 		npartitions = (swapped) ? bswap16(lp->d_npartitions)
    313 					: lp->d_npartitions;
    314 
    315 		/* Validate label length. */
    316 		if ((char *)lp + DISKLABEL_SIZE(npartitions) >
    317 		    (char *)a->buf + DEV_BSIZE) {
    318 			aprint_error("%s: BSD disklabel @ "
    319 			    "%" PRId64 "+%zd has bogus partition count (%u)\n",
    320 			    a->pdk->dk_name, label_sector, label_offset,
    321 			    npartitions);
    322 			continue;
    323 		}
    324 
    325 		/*
    326 		 * We have validated the partition count.  Checksum it.
    327 		 * Note that we purposefully checksum before swapping
    328 		 * the byte order.
    329 		 */
    330 		if (dkcksum_sized(lp, npartitions) != 0) {
    331 			aprint_error("%s: BSD disklabel @ %" PRId64
    332 			    "+%zd has bad checksum\n", a->pdk->dk_name,
    333 			    label_sector, label_offset);
    334 			continue;
    335 		}
    336 		/* Put the disklabel in the right order. */
    337 		if (swapped)
    338 			swap_disklabel(lp);
    339 		addwedges(a, lp);
    340 		return (SCAN_FOUND);
    341 	}
    342 }
    343 
    344 static int
    345 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
    346 				    int, u_int))
    347 {
    348 	struct mbr_partition ptns[MBR_PART_COUNT];
    349 	struct mbr_partition *dp;
    350 	struct mbr_sector *mbr;
    351 	u_int ext_base, this_ext, next_ext;
    352 	int i, rval;
    353 #ifdef COMPAT_386BSD_MBRPART
    354 	int dp_386bsd = -1;
    355 #endif
    356 
    357 	ext_base = 0;
    358 	this_ext = 0;
    359 	for (;;) {
    360 		a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->buf,
    361 					DEV_BSIZE);
    362 		if (a->error) {
    363 			aprint_error("%s: unable to read MBR @ %u, "
    364 			    "error = %d\n", a->pdk->dk_name, this_ext,
    365 			    a->error);
    366 			return (SCAN_ERROR);
    367 		}
    368 
    369 		mbr = a->buf;
    370 		if (mbr->mbr_magic != htole16(MBR_MAGIC))
    371 			return (SCAN_CONTINUE);
    372 
    373 		/* Copy data out of buffer so action can use the buffer. */
    374 		memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
    375 
    376 		/* Looks for NetBSD partition. */
    377 		next_ext = 0;
    378 		dp = ptns;
    379 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
    380 			if (dp->mbrp_type == 0)
    381 				continue;
    382 			if (MBR_IS_EXTENDED(dp->mbrp_type)) {
    383 				next_ext = le32toh(dp->mbrp_start);
    384 				continue;
    385 			}
    386 #ifdef COMPAT_386BSD_MBRPART
    387 			if (dp->mbrp_type == MBR_PTYPE_386BSD) {
    388 				/*
    389 				 * If more than one matches, take last,
    390 				 * as NetBSD install tool does.
    391 				 */
    392 				if (this_ext == 0)
    393 					dp_386bsd = i;
    394 				continue;
    395 			}
    396 #endif
    397 			rval = (*actn)(a, dp, i, this_ext);
    398 			if (rval != SCAN_CONTINUE)
    399 				return (rval);
    400 		}
    401 		if (next_ext == 0)
    402 			break;
    403 		if (ext_base == 0) {
    404 			ext_base = next_ext;
    405 			next_ext = 0;
    406 		}
    407 		next_ext += ext_base;
    408 		if (next_ext <= this_ext)
    409 			break;
    410 		this_ext = next_ext;
    411 	}
    412 #ifdef COMPAT_386BSD_MBRPART
    413 	if (this_ext == 0 && dp_386bsd != -1)
    414 		return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
    415 #endif
    416 	return (SCAN_CONTINUE);
    417 }
    418 
    419 static int
    420 look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
    421     u_int ext_base)
    422 {
    423 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    424 	int rval;
    425 
    426 	if (
    427 #ifdef COMPAT_386BSD_MBRPART
    428 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
    429 #endif
    430 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
    431 		rval = validate_label(a, ptn_base + BSD44_MBR_LABELSECTOR, 0);
    432 
    433 		/* If we got a NetBSD label, look no further. */
    434 		if (rval == SCAN_FOUND)
    435 			return (rval);
    436 	}
    437 
    438 	return (SCAN_CONTINUE);
    439 }
    440 
    441 #ifdef _KERNEL
    442 #define	DKW_MALLOC(SZ)	malloc((SZ), M_DEVBUF, M_WAITOK)
    443 #define	DKW_FREE(PTR)	free((PTR), M_DEVBUF)
    444 #else
    445 #define	DKW_MALLOC(SZ)	malloc((SZ))
    446 #define	DKW_FREE(PTR)	free((PTR))
    447 #endif
    448 
    449 static int
    450 dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
    451 {
    452 	mbr_args_t a;
    453 	const struct disklabel_location *dl;
    454 	int rval;
    455 
    456 	a.pdk = pdk;
    457 	a.vp = vp;
    458 	a.buf = DKW_MALLOC(DEV_BSIZE);
    459 	a.error = 0;
    460 
    461 	/* MBR search. */
    462 	rval = scan_mbr(&a, look_netbsd_part);
    463 	if (rval != SCAN_CONTINUE) {
    464 		if (rval == SCAN_FOUND)
    465 			a.error = 0;	/* found it, wedges installed */
    466 		goto out;
    467 	}
    468 
    469 	/* Known location search. */
    470 	for (dl = disklabel_locations; dl->label_sector != -1; dl++) {
    471 		rval = validate_label(&a, dl->label_sector, dl->label_offset);
    472 		if (rval != SCAN_CONTINUE) {
    473 			if (rval == SCAN_FOUND)
    474 				a.error = 0;	/* found it, wedges installed */
    475 			goto out;
    476 		}
    477 	}
    478 
    479 	/* No NetBSD disklabel found. */
    480 	a.error = ESRCH;
    481  out:
    482 	DKW_FREE(a.buf);
    483 	return (a.error);
    484 }
    485 
    486 #ifdef _KERNEL
    487 DKWEDGE_DISCOVERY_METHOD_DECL(BSD44, 5, dkwedge_discover_bsdlabel);
    488 #endif
    489