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