Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_bsdlabel.c revision 1.10
      1 /*	$NetBSD: dkwedge_bsdlabel.c,v 1.10 2007/03/01 21:30:50 martin 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.10 2007/03/01 21:30:50 martin Exp $");
     90 
     91 #include <sys/param.h>
     92 #include <sys/systm.h>
     93 #include <sys/proc.h>
     94 #include <sys/errno.h>
     95 #include <sys/disk.h>
     96 #include <sys/vnode.h>
     97 #include <sys/malloc.h>
     98 
     99 #include <sys/bootblock.h>
    100 #include <sys/disklabel.h>
    101 
    102 #define	BSD44_MBR_LABELSECTOR	1
    103 
    104 #define	DISKLABEL_SIZE(x)						\
    105 	(offsetof(struct disklabel, d_partitions) +			\
    106 	 (sizeof(struct partition) * (x)))
    107 
    108 /*
    109  * Note the smallest MAXPARTITIONS was 8, so we allow a disklabel
    110  * that size to be locted at the end of the sector.
    111  */
    112 #define	DISKLABEL_MINSIZE	DISKLABEL_SIZE(8)
    113 
    114 /*
    115  * Table of known platform-specific disklabel locations.
    116  */
    117 static const struct disklabel_location {
    118 	daddr_t		label_sector;	/* sector containing label */
    119 	size_t		label_offset;	/* byte offset of label in sector */
    120 } disklabel_locations[] = {
    121 	{ 0,	0 },	/* mvme68k, next68k */
    122 	{ 0,	64 },	/* algor, alpha, amiga, amigappc, evbmips, evbppc,
    123 			   luna68k, mac68k, macppc, news68k, newsmips,
    124 			   pc532, pdp11, pmax, vax, x68k */
    125 	{ 0,	128 },	/* sparc, sun68k */
    126 	{ 1,	0 },	/* amd64, arc, arm, bebox, cobalt, evbppc, hp700,
    127 			   hpcarm, hpcmips, i386, ibmnws, mipsco, mvmeppc,
    128 			   ofppc, playstation2, pmppc, prep, sandpoint,
    129 			   sbmips, sgimips, sh3, sh5 */
    130 	/* XXX atari is weird */
    131 	{ 2,	0 },	/* cesfic, hp300 */
    132 
    133 	{ -1,	0 },
    134 };
    135 
    136 #define	SCAN_CONTINUE	0
    137 #define	SCAN_FOUND	1
    138 #define	SCAN_ERROR	2
    139 
    140 typedef struct mbr_args {
    141 	struct disk	*pdk;
    142 	struct vnode	*vp;
    143 	void		*buf;
    144 	int		error;
    145 } mbr_args_t;
    146 
    147 static const char *
    148 bsdlabel_fstype_to_str(uint8_t fstype)
    149 {
    150 	const char *str;
    151 
    152 	switch (fstype) {
    153 	case FS_UNUSED:		str = DKW_PTYPE_UNUSED;		break;
    154 	case FS_SWAP:		str = DKW_PTYPE_SWAP;		break;
    155 	case FS_BSDFFS:		str = DKW_PTYPE_FFS;		break;
    156 	case FS_MSDOS:		str = DKW_PTYPE_FAT;		break;
    157 	case FS_BSDLFS:		str = DKW_PTYPE_LFS;		break;
    158 	case FS_ISO9660:	str = DKW_PTYPE_ISO9660;	break;
    159 	case FS_ADOS:		str = DKW_PTYPE_AMIGADOS;	break;
    160 	case FS_HFS:		str = DKW_PTYPE_APPLEHFS;	break;
    161 	case FS_FILECORE:	str = DKW_PTYPE_FILECORE;	break;
    162 	case FS_EX2FS:		str = DKW_PTYPE_EXT2FS;		break;
    163 	case FS_NTFS:		str = DKW_PTYPE_NTFS;		break;
    164 	case FS_RAID:		str = DKW_PTYPE_RAIDFRAME;	break;
    165 	case FS_CCD:		str = DKW_PTYPE_CCD;		break;
    166 	case FS_APPLEUFS:	str = DKW_PTYPE_APPLEUFS;	break;
    167 	default:		str = NULL;			break;
    168 	}
    169 
    170 	return (str);
    171 }
    172 
    173 static void
    174 swap_disklabel(struct disklabel *lp)
    175 {
    176 	int i;
    177 
    178 #define	SWAP16(x)	lp->x = bswap16(lp->x)
    179 #define	SWAP32(x)	lp->x = bswap32(lp->x)
    180 
    181 	SWAP32(d_magic);
    182 	SWAP16(d_type);
    183 	SWAP16(d_subtype);
    184 	SWAP32(d_secsize);
    185 	SWAP32(d_nsectors);
    186 	SWAP32(d_ntracks);
    187 	SWAP32(d_ncylinders);
    188 	SWAP32(d_secpercyl);
    189 	SWAP32(d_secperunit);
    190 	SWAP16(d_sparespertrack);
    191 	SWAP16(d_sparespercyl);
    192 	SWAP32(d_acylinders);
    193 	SWAP16(d_rpm);
    194 	SWAP16(d_interleave);
    195 	SWAP16(d_trackskew);
    196 	SWAP16(d_cylskew);
    197 	SWAP32(d_headswitch);
    198 	SWAP32(d_trkseek);
    199 	SWAP32(d_flags);
    200 
    201 	for (i = 0; i < NDDATA; i++)
    202 		SWAP32(d_drivedata[i]);
    203 	for (i = 0; i < NSPARE; i++)
    204 		SWAP32(d_spare[i]);
    205 
    206 	SWAP32(d_magic2);
    207 	SWAP16(d_checksum);
    208 	SWAP16(d_npartitions);
    209 	SWAP32(d_bbsize);
    210 	SWAP32(d_sbsize);
    211 
    212 	for (i = 0; i < lp->d_npartitions; i++) {
    213 		SWAP32(d_partitions[i].p_size);
    214 		SWAP32(d_partitions[i].p_offset);
    215 		SWAP32(d_partitions[i].p_fsize);
    216 		SWAP16(d_partitions[i].p_cpg);
    217 	}
    218 
    219 #undef SWAP16
    220 #undef SWAP32
    221 }
    222 
    223 static int
    224 validate_label(mbr_args_t *a, daddr_t label_sector, size_t label_offset)
    225 {
    226 	struct disklabel *lp;
    227 	caddr_t lp_lim;
    228 	int i, error;
    229 	u_int checksum;
    230 
    231 	error = dkwedge_read(a->pdk, a->vp, label_sector, a->buf, DEV_BSIZE);
    232 	if (error) {
    233 		aprint_error("%s: unable to read BSD disklabel @ %" PRId64
    234 		    ", error = %d\n", a->pdk->dk_name, label_sector, error);
    235 		a->error = error;
    236 		return (SCAN_ERROR);
    237 	}
    238 
    239 	/*
    240 	 * We ignore label_offset; this seems to have not been used
    241 	 * consistently in the old code, requiring us to do the search
    242 	 * in the sector.
    243 	 */
    244 	lp = a->buf;
    245 	lp_lim = (caddr_t)a->buf + DEV_BSIZE - DISKLABEL_MINSIZE;
    246 	for (;; lp = (void *)((caddr_t)lp + sizeof(uint32_t))) {
    247 		if ((caddr_t)lp > lp_lim)
    248 			return (SCAN_CONTINUE);
    249 		label_offset = (size_t)((caddr_t)lp - (caddr_t)a->buf);
    250 		if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
    251 			if (lp->d_magic == bswap32(DISKMAGIC) &&
    252 			    lp->d_magic2 == bswap32(DISKMAGIC)) {
    253 				/*
    254 				 * Label is in the other byte order; validate
    255 				 * its length, then byte-swap it.
    256 				 */
    257 				if ((caddr_t)lp +
    258 				    DISKLABEL_SIZE(bswap16(lp->d_npartitions)) >
    259 				    (caddr_t)a->buf + DEV_BSIZE) {
    260 					aprint_error("%s: BSD disklabel @ "
    261 					    "%" PRId64
    262 					    "+%zd has bogus partition "
    263 					    "count (%u)\n", a->pdk->dk_name,
    264 					    label_sector, label_offset,
    265 					    bswap16(lp->d_npartitions));
    266 					continue;
    267 				}
    268 				checksum = dkcksum_sized(lp,
    269 				    bswap16(lp->d_npartitions));
    270 				swap_disklabel(lp);
    271 			} else
    272 				continue;
    273 		} else {
    274 			/*
    275 			 * Validate the disklabel length.
    276 			 */
    277 			if ((caddr_t)lp + DISKLABEL_SIZE(lp->d_npartitions) >
    278 			    (caddr_t)a->buf + DEV_BSIZE) {
    279 				aprint_error("%s: BSD disklabel @ %" PRId64
    280 				    "+%zd has bogus partition count (%u)\n",
    281 				    a->pdk->dk_name, label_sector,
    282 				    label_offset, lp->d_npartitions);
    283 				continue;
    284 			}
    285 			checksum = dkcksum(lp);
    286 		}
    287 
    288 		/*
    289 		 * Disklabel is now in the right order and we have validated
    290 		 * the partition count, checksum it as the final check.
    291 		 */
    292 		if (checksum != 0) {
    293 			aprint_error("%s: BSD disklabel @ %" PRId64
    294 			    "+%zd has bad checksum\n", a->pdk->dk_name,
    295 			    label_sector, label_offset);
    296 			continue;
    297 		}
    298 
    299 		/*
    300 		 * Ok, we have a valid NetBSD disklabel, add wedges for it.
    301 		 */
    302 		for (i = 0; i < lp->d_npartitions; i++) {
    303 			struct dkwedge_info dkw;
    304 			struct partition *p;
    305 			const char *ptype;
    306 
    307 			p = &lp->d_partitions[i];
    308 
    309 			if (p->p_fstype == FS_UNUSED)
    310 				continue;
    311 			if ((ptype =
    312 			     bsdlabel_fstype_to_str(p->p_fstype)) == NULL) {
    313 				/*
    314 				 * XXX Should probably just add these...
    315 				 * XXX maybe just have an empty ptype?
    316 				 */
    317 				aprint_verbose("%s: skipping partition %d, "
    318 				    "type %d\n", a->pdk->dk_name, i,
    319 				    p->p_fstype);
    320 				continue;
    321 			}
    322 			strcpy(dkw.dkw_ptype, ptype);
    323 
    324 			strcpy(dkw.dkw_parent, a->pdk->dk_name);
    325 			dkw.dkw_offset = p->p_offset;
    326 			dkw.dkw_size = p->p_size;
    327 
    328 			/*
    329 			 * These get historical disk naming style
    330 			 * wedge names.
    331 			 */
    332 			snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname),
    333 			    "%s%c", a->pdk->dk_name, 'a' + i);
    334 
    335 			error = dkwedge_add(&dkw);
    336 			if (error == EEXIST)
    337 				aprint_error("%s: wedge named '%s' already "
    338 				    "exists, manual intervention required\n",
    339 				    a->pdk->dk_name, dkw.dkw_wname);
    340 			else if (error)
    341 				aprint_error("%s: error %d adding partition "
    342 				    "%d type %d\n", a->pdk->dk_name, error,
    343 				    i, p->p_fstype);
    344 		}
    345 		return (SCAN_FOUND);
    346 	}
    347 }
    348 
    349 static int
    350 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
    351 				    int, u_int))
    352 {
    353 	struct mbr_partition ptns[MBR_PART_COUNT];
    354 	struct mbr_partition *dp;
    355 	struct mbr_sector *mbr;
    356 	u_int ext_base, this_ext, next_ext;
    357 	int i, rval;
    358 #ifdef COMPAT_386BSD_MBRPART
    359 	int dp_386bsd = -1;
    360 #endif
    361 
    362 	ext_base = 0;
    363 	this_ext = 0;
    364 	for (;;) {
    365 		a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->buf,
    366 					DEV_BSIZE);
    367 		if (a->error) {
    368 			aprint_error("%s: unable to read MBR @ %u, "
    369 			    "error = %d\n", a->pdk->dk_name, this_ext,
    370 			    a->error);
    371 			return (SCAN_ERROR);
    372 		}
    373 
    374 		mbr = a->buf;
    375 		if (mbr->mbr_magic != htole16(MBR_MAGIC))
    376 			return (SCAN_CONTINUE);
    377 
    378 		/* Copy data out of buffer so action can use the buffer. */
    379 		memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
    380 
    381 		/* Looks for NetBSD partition. */
    382 		next_ext = 0;
    383 		dp = ptns;
    384 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
    385 			if (dp->mbrp_type == 0)
    386 				continue;
    387 			if (MBR_IS_EXTENDED(dp->mbrp_type)) {
    388 				next_ext = le32toh(dp->mbrp_start);
    389 				continue;
    390 			}
    391 #ifdef COMPAT_386BSD_MBRPART
    392 			if (dp->mbrp_type == MBR_PTYPE_386BSD) {
    393 				/*
    394 				 * If more than one matches, take last,
    395 				 * as NetBSD install tool does.
    396 				 */
    397 				if (this_ext == 0)
    398 					dp_386bsd = i;
    399 				continue;
    400 			}
    401 #endif
    402 			rval = (*actn)(a, dp, i, this_ext);
    403 			if (rval != SCAN_CONTINUE)
    404 				return (rval);
    405 		}
    406 		if (next_ext == 0)
    407 			break;
    408 		if (ext_base == 0) {
    409 			ext_base = next_ext;
    410 			next_ext = 0;
    411 		}
    412 		next_ext += ext_base;
    413 		if (next_ext <= this_ext)
    414 			break;
    415 		this_ext = next_ext;
    416 	}
    417 #ifdef COMPAT_386BSD_MBRPART
    418 	if (this_ext == 0 && dp_386bsd != -1)
    419 		return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
    420 #endif
    421 	return (SCAN_CONTINUE);
    422 }
    423 
    424 static int
    425 look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
    426     u_int ext_base)
    427 {
    428 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    429 	int rval;
    430 
    431 	if (
    432 #ifdef COMPAT_386BSD_MBRPART
    433 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
    434 #endif
    435 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
    436 		rval = validate_label(a, ptn_base + BSD44_MBR_LABELSECTOR, 0);
    437 
    438 		/* If we got a NetBSD label, look no further. */
    439 		if (rval == SCAN_FOUND)
    440 			return (rval);
    441 	}
    442 
    443 	return (SCAN_CONTINUE);
    444 }
    445 
    446 static int
    447 dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
    448 {
    449 	mbr_args_t a;
    450 	const struct disklabel_location *dl;
    451 	int rval;
    452 
    453 	a.pdk = pdk;
    454 	a.vp = vp;
    455 	a.buf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
    456 	a.error = 0;
    457 
    458 	/* MBR search. */
    459 	rval = scan_mbr(&a, look_netbsd_part);
    460 	if (rval != SCAN_CONTINUE) {
    461 		if (rval == SCAN_FOUND)
    462 			a.error = 0;	/* found it, wedges installed */
    463 		goto out;
    464 	}
    465 
    466 	/* Known location search. */
    467 	for (dl = disklabel_locations; dl->label_sector != -1; dl++) {
    468 		rval = validate_label(&a, dl->label_sector, dl->label_offset);
    469 		if (rval != SCAN_CONTINUE) {
    470 			if (rval == SCAN_FOUND)
    471 				a.error = 0;	/* found it, wedges installed */
    472 			goto out;
    473 		}
    474 	}
    475 
    476 	/* No NetBSD disklabel found. */
    477 	a.error = ESRCH;
    478  out:
    479 	free(a.buf, M_DEVBUF);
    480 	return (a.error);
    481 }
    482 
    483 DKWEDGE_DISCOVERY_METHOD_DECL(BSD44, 5, dkwedge_discover_bsdlabel);
    484