Home | History | Annotate | Line # | Download | only in kern
subr_disk_mbr.c revision 1.2
      1  1.2  agc /*	$NetBSD: subr_disk_mbr.c,v 1.2 2003/08/07 16:31:53 agc Exp $	*/
      2  1.1  dsl 
      3  1.1  dsl /*
      4  1.1  dsl  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      5  1.1  dsl  * All rights reserved.
      6  1.1  dsl  *
      7  1.1  dsl  * Redistribution and use in source and binary forms, with or without
      8  1.1  dsl  * modification, are permitted provided that the following conditions
      9  1.1  dsl  * are met:
     10  1.1  dsl  * 1. Redistributions of source code must retain the above copyright
     11  1.1  dsl  *    notice, this list of conditions and the following disclaimer.
     12  1.1  dsl  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  dsl  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  dsl  *    documentation and/or other materials provided with the distribution.
     15  1.2  agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  dsl  *    may be used to endorse or promote products derived from this software
     17  1.1  dsl  *    without specific prior written permission.
     18  1.1  dsl  *
     19  1.1  dsl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  dsl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  dsl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  dsl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  dsl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  dsl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  dsl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  dsl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  dsl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  dsl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  dsl  * SUCH DAMAGE.
     30  1.1  dsl  *
     31  1.1  dsl  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
     32  1.1  dsl  */
     33  1.1  dsl 
     34  1.1  dsl /*
     35  1.1  dsl  * Code to find a NetBSD label on a disk that contains an i386 style MBR.
     36  1.1  dsl  * The first NetBSD label found in the 2nd sector of a NetBSD partition
     37  1.1  dsl  * is used.  If there isn't a NetBSD partition then a label is faked up
     38  1.1  dsl  * from the MBR.
     39  1.1  dsl  *
     40  1.1  dsl  * If there isn't a disklabel or anything in the MBR then partition a
     41  1.1  dsl  * is set to cover the whole disk.
     42  1.1  dsl  * Useful for files that contain single filesystems (etc).
     43  1.1  dsl  *
     44  1.1  dsl  * This code will read host endian netbsd labels from little endian MBR.
     45  1.1  dsl  *
     46  1.1  dsl  * Based on the i386 disksubr.c
     47  1.1  dsl  *
     48  1.1  dsl  * XXX There are potential problems writing labels to disks where there
     49  1.1  dsl  * is only space for 8 netbsd partitions but this code has been compiled
     50  1.1  dsl  * with MAXPARTITIONS=16.
     51  1.1  dsl  */
     52  1.1  dsl 
     53  1.1  dsl #include <sys/cdefs.h>
     54  1.2  agc __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.2 2003/08/07 16:31:53 agc Exp $");
     55  1.1  dsl 
     56  1.1  dsl #include <sys/param.h>
     57  1.1  dsl #include <sys/systm.h>
     58  1.1  dsl #include <sys/buf.h>
     59  1.1  dsl #include <sys/disklabel.h>
     60  1.1  dsl #include <sys/disk.h>
     61  1.1  dsl #include <sys/syslog.h>
     62  1.1  dsl 
     63  1.1  dsl #include "opt_mbr.h"
     64  1.1  dsl 
     65  1.1  dsl #define MBR_LABELSECTOR	1
     66  1.1  dsl 
     67  1.1  dsl #define SCAN_CONTINUE	0
     68  1.1  dsl #define SCAN_FOUND	1
     69  1.1  dsl #define SCAN_ERROR	2
     70  1.1  dsl 
     71  1.1  dsl typedef struct mbr_args {
     72  1.1  dsl 	struct disklabel *lp;
     73  1.1  dsl 	void		(*strat)(struct buf *);
     74  1.1  dsl 	struct buf	*bp;
     75  1.1  dsl 	const char	*msg;
     76  1.1  dsl 	int		error;
     77  1.1  dsl } mbr_args_t;
     78  1.1  dsl 
     79  1.1  dsl #define READ_LABEL	1
     80  1.1  dsl #define WRITE_LABEL	2
     81  1.1  dsl static int validate_label(mbr_args_t *, uint, int);
     82  1.1  dsl static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
     83  1.1  dsl static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
     84  1.1  dsl 
     85  1.1  dsl static int
     86  1.1  dsl read_sector(mbr_args_t *a, uint sector)
     87  1.1  dsl {
     88  1.1  dsl 	struct buf *bp = a->bp;
     89  1.1  dsl 	int error;
     90  1.1  dsl 
     91  1.1  dsl 	bp->b_blkno = sector;
     92  1.1  dsl 	bp->b_bcount = a->lp->d_secsize;
     93  1.1  dsl 	bp->b_flags = (bp->b_flags & ~(B_WRITE | B_DONE)) | B_READ;
     94  1.1  dsl 	bp->b_cylinder = sector / a->lp->d_secpercyl;
     95  1.1  dsl 	(*a->strat)(bp);
     96  1.1  dsl 	error = biowait(bp);
     97  1.1  dsl 	if (error != 0)
     98  1.1  dsl 		a->error = error;
     99  1.1  dsl 	return error;
    100  1.1  dsl }
    101  1.1  dsl 
    102  1.1  dsl /*
    103  1.1  dsl  * Scan MBR for partitions, call 'action' routine for each.
    104  1.1  dsl  */
    105  1.1  dsl 
    106  1.1  dsl static int
    107  1.1  dsl scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
    108  1.1  dsl {
    109  1.1  dsl 	mbr_partition_t ptns[NMBRPART];
    110  1.1  dsl 	mbr_partition_t *dp;
    111  1.1  dsl 	mbr_sector_t *mbr;
    112  1.1  dsl 	uint ext_base, this_ext, next_ext;
    113  1.1  dsl 	int rval;
    114  1.1  dsl 	int i;
    115  1.1  dsl #ifdef COMPAT_386BSD_MBRPART
    116  1.1  dsl 	int dp_386bsd = -1;
    117  1.1  dsl #endif
    118  1.1  dsl 
    119  1.1  dsl 	ext_base = 0;
    120  1.1  dsl 	this_ext = 0;
    121  1.1  dsl 	for (;;) {
    122  1.1  dsl 		if (read_sector(a, this_ext)) {
    123  1.1  dsl 			a->msg = "dos partition I/O error";
    124  1.1  dsl 			return SCAN_ERROR;
    125  1.1  dsl 		}
    126  1.1  dsl 
    127  1.1  dsl 		/* Note: Magic number is little-endian. */
    128  1.1  dsl 		mbr = (void *)a->bp->b_data;
    129  1.1  dsl 		if (mbr->mbr_signature != htole16(MBR_MAGIC)) {
    130  1.1  dsl 			/* Don't set an error message here.... */
    131  1.1  dsl 			return SCAN_ERROR;
    132  1.1  dsl 		}
    133  1.1  dsl 
    134  1.1  dsl 		/* Copy data out of buffer so action can use bp */
    135  1.1  dsl 		memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
    136  1.1  dsl 
    137  1.1  dsl 		/* look for NetBSD partition */
    138  1.1  dsl 		next_ext = 0;
    139  1.1  dsl 		dp = ptns;
    140  1.1  dsl 		for (i = 0; i < NMBRPART; i++, dp++) {
    141  1.1  dsl 			if (dp->mbrp_typ == 0)
    142  1.1  dsl 				continue;
    143  1.1  dsl 			if (MBR_IS_EXTENDED(dp->mbrp_typ)) {
    144  1.1  dsl 				next_ext = le32toh(dp->mbrp_start);
    145  1.1  dsl 				continue;
    146  1.1  dsl 			}
    147  1.1  dsl #ifdef COMPAT_386BSD_MBRPART
    148  1.1  dsl 			if (dp->mbrp_typ == MBR_PTYPE_386BSD) {
    149  1.1  dsl 				/*
    150  1.1  dsl 				 * If more than one matches, take last,
    151  1.1  dsl 				 * as NetBSD install tool does.
    152  1.1  dsl 				 */
    153  1.1  dsl 				if (this_ext == 0)
    154  1.1  dsl 					dp_386bsd = i;
    155  1.1  dsl 				continue;
    156  1.1  dsl 			}
    157  1.1  dsl #endif
    158  1.1  dsl 			rval = (*actn)(a, dp, i, this_ext);
    159  1.1  dsl 			if (rval != SCAN_CONTINUE)
    160  1.1  dsl 				return rval;
    161  1.1  dsl 		}
    162  1.1  dsl 		if (next_ext == 0)
    163  1.1  dsl 			break;
    164  1.1  dsl 		if (ext_base == 0) {
    165  1.1  dsl 			ext_base = next_ext;
    166  1.1  dsl 			next_ext = 0;
    167  1.1  dsl 		}
    168  1.1  dsl 		next_ext += ext_base;
    169  1.1  dsl 		if (next_ext <= this_ext)
    170  1.1  dsl 			break;
    171  1.1  dsl 		this_ext = next_ext;
    172  1.1  dsl 	}
    173  1.1  dsl #ifdef COMPAT_386BSD_MBRPART
    174  1.1  dsl 	if (this_ext == 0 && dp_386bsd != -1)
    175  1.1  dsl 		return (*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0);
    176  1.1  dsl #endif
    177  1.1  dsl 	return SCAN_CONTINUE;
    178  1.1  dsl }
    179  1.1  dsl 
    180  1.1  dsl /*
    181  1.1  dsl  * Attempt to read a disk label from a device
    182  1.1  dsl  * using the indicated strategy routine.
    183  1.1  dsl  * The label must be partly set up before this:
    184  1.1  dsl  * secpercyl, secsize and anything required for a block i/o read
    185  1.1  dsl  * operation in the driver's strategy/start routines
    186  1.1  dsl  * must be filled in before calling us.
    187  1.1  dsl  *
    188  1.1  dsl  * If dos partition table requested, attempt to load it and
    189  1.1  dsl  * find disklabel inside a DOS partition. Also, if bad block
    190  1.1  dsl  * table needed, attempt to extract it as well. Return buffer
    191  1.1  dsl  * for use in signalling errors if requested.
    192  1.1  dsl  *
    193  1.1  dsl  * Returns null on success and an error string on failure.
    194  1.1  dsl  */
    195  1.1  dsl const char *
    196  1.1  dsl readdisklabel(dev, strat, lp, osdep)
    197  1.1  dsl 	dev_t dev;
    198  1.1  dsl 	void (*strat)(struct buf *);
    199  1.1  dsl 	struct disklabel *lp;
    200  1.1  dsl 	struct cpu_disklabel *osdep;
    201  1.1  dsl {
    202  1.1  dsl 	struct dkbad *bdp;
    203  1.1  dsl 	int rval;
    204  1.1  dsl 	int i;
    205  1.1  dsl 	mbr_args_t a;
    206  1.1  dsl 
    207  1.1  dsl 	a.lp = lp;
    208  1.1  dsl 	a.strat = strat;
    209  1.1  dsl 	a.msg = NULL;
    210  1.1  dsl 	a.error = 0;
    211  1.1  dsl 
    212  1.1  dsl 	/* minimal requirements for architypal disk label */
    213  1.1  dsl 	if (lp->d_secsize == 0)
    214  1.1  dsl 		lp->d_secsize = DEV_BSIZE;
    215  1.1  dsl 	if (lp->d_secperunit == 0)
    216  1.1  dsl 		lp->d_secperunit = 0x1fffffff;
    217  1.1  dsl 	lp->d_npartitions = RAW_PART + 1;
    218  1.1  dsl 	for (i = 0; i < RAW_PART; i++) {
    219  1.1  dsl 		lp->d_partitions[i].p_size = 0;
    220  1.1  dsl 		lp->d_partitions[i].p_offset = 0;
    221  1.1  dsl 	}
    222  1.1  dsl 	if (lp->d_partitions[RAW_PART].p_size == 0)
    223  1.1  dsl 		lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
    224  1.1  dsl 	lp->d_partitions[RAW_PART].p_offset = 0;
    225  1.1  dsl 
    226  1.1  dsl 	/*
    227  1.1  dsl 	 * Set partition 'a' to be the whole disk.
    228  1.1  dsl 	 * Cleared if we find an mbr or a netbsd label.
    229  1.1  dsl 	 */
    230  1.1  dsl 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
    231  1.1  dsl 	lp->d_partitions[0].p_fstype = FS_BSDFFS;
    232  1.1  dsl 
    233  1.1  dsl 	/* get a buffer and initialize it */
    234  1.1  dsl 	a.bp = geteblk((int)lp->d_secsize);
    235  1.1  dsl 	a.bp->b_dev = dev;
    236  1.1  dsl 
    237  1.1  dsl 	if (osdep)
    238  1.1  dsl 		/*
    239  1.1  dsl 		 * Scan mbr searching for netbsd partition and saving
    240  1.1  dsl 		 * bios partition information to use if the netbsd one
    241  1.1  dsl 		 * is absent.
    242  1.1  dsl 		 */
    243  1.1  dsl 		rval = scan_mbr(&a, look_netbsd_part);
    244  1.1  dsl 	else
    245  1.1  dsl 		rval = SCAN_CONTINUE;
    246  1.1  dsl 
    247  1.1  dsl 	if (rval == SCAN_CONTINUE || (rval == SCAN_ERROR && a.msg == NULL)) {
    248  1.1  dsl 		/* Look at start of disk */
    249  1.1  dsl 		rval = validate_label(&a, LABELSECTOR, READ_LABEL);
    250  1.1  dsl 		if (LABELSECTOR != 0 && rval == SCAN_CONTINUE)
    251  1.1  dsl 			rval = validate_label(&a, 0, READ_LABEL);
    252  1.1  dsl 	}
    253  1.1  dsl 
    254  1.1  dsl 	/* Obtain bad sector table if requested and present */
    255  1.1  dsl 	if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
    256  1.1  dsl 		struct dkbad *db;
    257  1.1  dsl 		int blkno;
    258  1.1  dsl 
    259  1.1  dsl 		bdp = &osdep->bad;
    260  1.1  dsl 		i = 0;
    261  1.1  dsl 		rval = SCAN_ERROR;
    262  1.1  dsl 		do {
    263  1.1  dsl 			/* read a bad sector table */
    264  1.1  dsl 			blkno = lp->d_secperunit - lp->d_nsectors + i;
    265  1.1  dsl 			if (lp->d_secsize > DEV_BSIZE)
    266  1.1  dsl 				blkno *= lp->d_secsize / DEV_BSIZE;
    267  1.1  dsl 			else
    268  1.1  dsl 				blkno /= DEV_BSIZE / lp->d_secsize;
    269  1.1  dsl 			/* if successful, validate, otherwise try another */
    270  1.1  dsl 			if (read_sector(&a, blkno)) {
    271  1.1  dsl 				a.msg = "bad sector table I/O error";
    272  1.1  dsl 				continue;
    273  1.1  dsl 			}
    274  1.1  dsl 			db = (struct dkbad *)(a.bp->b_data);
    275  1.1  dsl #define DKBAD_MAGIC 0x4321
    276  1.1  dsl 			if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
    277  1.1  dsl 				a.msg = "bad sector table corrupted";
    278  1.1  dsl 				continue;
    279  1.1  dsl 			}
    280  1.1  dsl 			rval = SCAN_FOUND;
    281  1.1  dsl 			*bdp = *db;
    282  1.1  dsl 			break;
    283  1.1  dsl 		} while ((a.bp->b_flags & B_ERROR) && (i += 2) < 10 &&
    284  1.1  dsl 			i < lp->d_nsectors);
    285  1.1  dsl 	}
    286  1.1  dsl 
    287  1.1  dsl 	brelse(a.bp);
    288  1.1  dsl 	if (rval != SCAN_ERROR)
    289  1.1  dsl 		return NULL;
    290  1.1  dsl 	return a.msg;
    291  1.1  dsl }
    292  1.1  dsl 
    293  1.1  dsl static int
    294  1.1  dsl look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    295  1.1  dsl {
    296  1.1  dsl 	struct partition *pp;
    297  1.1  dsl 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    298  1.1  dsl 	int rval;
    299  1.1  dsl 
    300  1.1  dsl 	if (
    301  1.1  dsl #ifdef COMPAT_386BSD_MBRPART
    302  1.1  dsl 	    dp->mbrp_typ == MBR_PTYPE_386BSD ||
    303  1.1  dsl #endif
    304  1.1  dsl 	    dp->mbrp_typ == MBR_PTYPE_NETBSD) {
    305  1.1  dsl 		rval = validate_label(a, ptn_base + MBR_LABELSECTOR, READ_LABEL);
    306  1.1  dsl 
    307  1.1  dsl 		/* Put actual location where we found the label into ptn 2 */
    308  1.1  dsl 		if (RAW_PART != 2 && (rval == SCAN_FOUND ||
    309  1.1  dsl 					a->lp->d_partitions[2].p_size == 0)) {
    310  1.1  dsl 			a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
    311  1.1  dsl 			a->lp->d_partitions[2].p_offset = ptn_base;
    312  1.1  dsl 		}
    313  1.1  dsl 
    314  1.1  dsl 		/* If we got a netbsd label look no further */
    315  1.1  dsl 		if (rval == SCAN_FOUND)
    316  1.1  dsl 			return rval;
    317  1.1  dsl 	}
    318  1.1  dsl 
    319  1.1  dsl 	/* Install main partitions into e..h and extended into i+ */
    320  1.1  dsl 	if (ext_base == 0)
    321  1.1  dsl 		slot += 4;
    322  1.1  dsl 	else {
    323  1.1  dsl 		slot = 4 + NMBRPART;
    324  1.1  dsl 		pp = &a->lp->d_partitions[slot];
    325  1.1  dsl 		for (; slot < MAXPARTITIONS; pp++, slot++) {
    326  1.1  dsl 			/* This gets called twice - avoid duplicates */
    327  1.1  dsl 			if (pp->p_offset == ptn_base &&
    328  1.1  dsl 			    pp->p_size == le32toh(dp->mbrp_size))
    329  1.1  dsl 				break;
    330  1.1  dsl 			if (pp->p_size == 0)
    331  1.1  dsl 				break;
    332  1.1  dsl 		}
    333  1.1  dsl 	}
    334  1.1  dsl 
    335  1.1  dsl 	if (slot < MAXPARTITIONS) {
    336  1.1  dsl 		/* Stop 'a' being the entire disk */
    337  1.1  dsl 		a->lp->d_partitions[0].p_size = 0;
    338  1.1  dsl 		a->lp->d_partitions[0].p_fstype = 0;
    339  1.1  dsl 
    340  1.1  dsl 		/* save partition info */
    341  1.1  dsl 		pp = &a->lp->d_partitions[slot];
    342  1.1  dsl 		pp->p_offset = ptn_base;
    343  1.1  dsl 		pp->p_size = le32toh(dp->mbrp_size);
    344  1.1  dsl 		pp->p_fstype = xlat_mbr_fstype(dp->mbrp_typ);
    345  1.1  dsl 
    346  1.1  dsl 		if (slot >= a->lp->d_npartitions)
    347  1.1  dsl 			a->lp->d_npartitions = slot + 1;
    348  1.1  dsl 	}
    349  1.1  dsl 
    350  1.1  dsl 	return SCAN_CONTINUE;
    351  1.1  dsl }
    352  1.1  dsl 
    353  1.1  dsl 
    354  1.1  dsl static int
    355  1.1  dsl validate_label(mbr_args_t *a, uint label_sector, int action)
    356  1.1  dsl {
    357  1.1  dsl 	struct disklabel *dlp, *dlp_lim;
    358  1.1  dsl 	int error;
    359  1.1  dsl 
    360  1.1  dsl 	/* Next, dig out disk label */
    361  1.1  dsl 	if (read_sector(a, label_sector)) {
    362  1.1  dsl 		a->msg =  "disk label read failed";
    363  1.1  dsl 		return SCAN_ERROR;
    364  1.1  dsl 	}
    365  1.1  dsl 
    366  1.1  dsl 	/* Locate disk label within block and validate */
    367  1.1  dsl 	/*
    368  1.1  dsl 	 * XXX (dsl) This search may be a waste of time, a lot of other i386
    369  1.1  dsl 	 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
    370  1.1  dsl 	 *
    371  1.1  dsl 	 * If we want to support diska from other netbsd ports, then the
    372  1.1  dsl 	 * code should also allow for a shorter label nearer the end of
    373  1.1  dsl 	 * the disk sector, and (IIRC) labels within 8k of the disk start.
    374  1.1  dsl 	 */
    375  1.1  dsl 	dlp_lim = (void *)(a->bp->b_data + a->lp->d_secsize - sizeof(*dlp));
    376  1.1  dsl 	for (dlp = (void *)a->bp->b_data; dlp <= dlp_lim;
    377  1.1  dsl 	    dlp = (void *)((char *)dlp + sizeof(long))) {
    378  1.1  dsl 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
    379  1.1  dsl 			continue;
    380  1.1  dsl 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
    381  1.1  dsl 			a->msg = "disk label corrupted";
    382  1.1  dsl 			continue;
    383  1.1  dsl 		}
    384  1.1  dsl 		switch (action) {
    385  1.1  dsl 		case READ_LABEL:
    386  1.1  dsl 			*a->lp = *dlp;
    387  1.1  dsl 			break;
    388  1.1  dsl 		case WRITE_LABEL:
    389  1.1  dsl 			*dlp = *a->lp;
    390  1.1  dsl 			a->bp->b_flags &= ~(B_READ|B_DONE);
    391  1.1  dsl 			a->bp->b_flags |= B_WRITE;
    392  1.1  dsl 			(*a->strat)(a->bp);
    393  1.1  dsl 			error = biowait(a->bp);
    394  1.1  dsl 			if (error != 0) {
    395  1.1  dsl 				a->error = error;
    396  1.1  dsl 				a->msg = "disk label write failed";
    397  1.1  dsl 				return SCAN_ERROR;
    398  1.1  dsl 			}
    399  1.1  dsl 			break;
    400  1.1  dsl 		}
    401  1.1  dsl 		return SCAN_FOUND;
    402  1.1  dsl 	}
    403  1.1  dsl 
    404  1.1  dsl 	return SCAN_CONTINUE;
    405  1.1  dsl }
    406  1.1  dsl 
    407  1.1  dsl /*
    408  1.1  dsl  * Check new disk label for sensibility
    409  1.1  dsl  * before setting it.
    410  1.1  dsl  */
    411  1.1  dsl int
    412  1.1  dsl setdisklabel(olp, nlp, openmask, osdep)
    413  1.1  dsl 	struct disklabel *olp, *nlp;
    414  1.1  dsl 	u_long openmask;
    415  1.1  dsl 	struct cpu_disklabel *osdep;
    416  1.1  dsl {
    417  1.1  dsl 	int i;
    418  1.1  dsl 	struct partition *opp, *npp;
    419  1.1  dsl 
    420  1.1  dsl 	/* sanity clause */
    421  1.1  dsl 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
    422  1.1  dsl 		|| (nlp->d_secsize % DEV_BSIZE) != 0)
    423  1.1  dsl 			return (EINVAL);
    424  1.1  dsl 
    425  1.1  dsl 	/* special case to allow disklabel to be invalidated */
    426  1.1  dsl 	if (nlp->d_magic == 0xffffffff) {
    427  1.1  dsl 		*olp = *nlp;
    428  1.1  dsl 		return (0);
    429  1.1  dsl 	}
    430  1.1  dsl 
    431  1.1  dsl 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    432  1.1  dsl 	    dkcksum(nlp) != 0)
    433  1.1  dsl 		return (EINVAL);
    434  1.1  dsl 
    435  1.1  dsl 	/* XXX missing check if other dos partitions will be overwritten */
    436  1.1  dsl 
    437  1.1  dsl 	while (openmask != 0) {
    438  1.1  dsl 		i = ffs(openmask) - 1;
    439  1.1  dsl 		openmask &= ~(1 << i);
    440  1.1  dsl 		if (i > nlp->d_npartitions)
    441  1.1  dsl 			return (EBUSY);
    442  1.1  dsl 		opp = &olp->d_partitions[i];
    443  1.1  dsl 		npp = &nlp->d_partitions[i];
    444  1.1  dsl 		/*
    445  1.1  dsl 		 * Copy internally-set partition information
    446  1.1  dsl 		 * if new label doesn't include it.		XXX
    447  1.1  dsl 		 */
    448  1.1  dsl 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
    449  1.1  dsl 			*npp = *opp;
    450  1.1  dsl 			continue;
    451  1.1  dsl 		}
    452  1.1  dsl 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    453  1.1  dsl 			return (EBUSY);
    454  1.1  dsl 	}
    455  1.1  dsl  	nlp->d_checksum = 0;
    456  1.1  dsl  	nlp->d_checksum = dkcksum(nlp);
    457  1.1  dsl 	*olp = *nlp;
    458  1.1  dsl 	return (0);
    459  1.1  dsl }
    460  1.1  dsl 
    461  1.1  dsl 
    462  1.1  dsl /*
    463  1.1  dsl  * Write disk label back to device after modification.
    464  1.1  dsl  */
    465  1.1  dsl int
    466  1.1  dsl writedisklabel(dev, strat, lp, osdep)
    467  1.1  dsl 	dev_t dev;
    468  1.1  dsl 	void (*strat)(struct buf *);
    469  1.1  dsl 	struct disklabel *lp;
    470  1.1  dsl 	struct cpu_disklabel *osdep;
    471  1.1  dsl {
    472  1.1  dsl 	mbr_args_t a;
    473  1.1  dsl 	int rval;
    474  1.1  dsl 
    475  1.1  dsl 	a.lp = lp;
    476  1.1  dsl 	a.strat = strat;
    477  1.1  dsl 	a.msg = NULL;
    478  1.1  dsl 	a.error = 0;
    479  1.1  dsl 
    480  1.1  dsl 	/* get a buffer and initialize it */
    481  1.1  dsl 	a.bp = geteblk((int)lp->d_secsize);
    482  1.1  dsl 	a.bp->b_dev = dev;
    483  1.1  dsl 
    484  1.1  dsl 	if (osdep)
    485  1.1  dsl 		rval = scan_mbr(&a, write_netbsd_label);
    486  1.1  dsl 	else
    487  1.1  dsl 		rval = validate_label(&a, LABELSECTOR, WRITE_LABEL);
    488  1.1  dsl 
    489  1.1  dsl 	if (rval != SCAN_FOUND && a.error == 0)
    490  1.1  dsl 		a.error = ESRCH;
    491  1.1  dsl 
    492  1.1  dsl 	brelse(a.bp);
    493  1.1  dsl 	return a.error;
    494  1.1  dsl }
    495  1.1  dsl 
    496  1.1  dsl static int
    497  1.1  dsl write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    498  1.1  dsl {
    499  1.1  dsl 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    500  1.1  dsl 	int rval;
    501  1.1  dsl 
    502  1.1  dsl 	if (dp->mbrp_typ != MBR_PTYPE_NETBSD)
    503  1.1  dsl 		return SCAN_CONTINUE;
    504  1.1  dsl 
    505  1.1  dsl 	rval = validate_label(a, ptn_base + MBR_LABELSECTOR, WRITE_LABEL);
    506  1.1  dsl #ifdef notyet 	/* because the error reporting is wrong */
    507  1.1  dsl 	/* write label to all netbsd partitions */
    508  1.1  dsl 	if (rval == SCAN_FOUND)
    509  1.1  dsl 		return SCAN_CONTINUE;
    510  1.1  dsl #endif
    511  1.1  dsl 	return rval;
    512  1.1  dsl }
    513  1.1  dsl 
    514  1.1  dsl 
    515  1.1  dsl /*
    516  1.1  dsl  * Determine the size of the transfer, and make sure it is
    517  1.1  dsl  * within the boundaries of the partition. Adjust transfer
    518  1.1  dsl  * if needed, and signal errors or early completion.
    519  1.1  dsl  */
    520  1.1  dsl int
    521  1.1  dsl bounds_check_with_label(dk, bp, wlabel)
    522  1.1  dsl 	struct disk *dk;
    523  1.1  dsl 	struct buf *bp;
    524  1.1  dsl 	int wlabel;
    525  1.1  dsl {
    526  1.1  dsl 	struct disklabel *lp = dk->dk_label;
    527  1.1  dsl 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    528  1.1  dsl 	int labelsector = lp->d_partitions[2].p_offset + LABELSECTOR;
    529  1.1  dsl 	int sz;
    530  1.1  dsl 
    531  1.1  dsl 	sz = howmany(bp->b_bcount, lp->d_secsize);
    532  1.1  dsl 
    533  1.1  dsl 	if (bp->b_blkno + sz > p->p_size) {
    534  1.1  dsl 		sz = p->p_size - bp->b_blkno;
    535  1.1  dsl 		if (sz == 0) {
    536  1.1  dsl 			/* If exactly at end of disk, return EOF. */
    537  1.1  dsl 			bp->b_resid = bp->b_bcount;
    538  1.1  dsl 			return (0);
    539  1.1  dsl 		}
    540  1.1  dsl 		if (sz < 0) {
    541  1.1  dsl 			/* If past end of disk, return EINVAL. */
    542  1.1  dsl 			bp->b_error = EINVAL;
    543  1.1  dsl 			goto bad;
    544  1.1  dsl 		}
    545  1.1  dsl 		/* Otherwise, truncate request. */
    546  1.1  dsl 		bp->b_bcount = sz << DEV_BSHIFT;
    547  1.1  dsl 	}
    548  1.1  dsl 
    549  1.1  dsl 	/* Overwriting disk label? */
    550  1.1  dsl 	if (bp->b_blkno + p->p_offset <= labelsector &&
    551  1.1  dsl 	    bp->b_blkno + p->p_offset + sz > labelsector &&
    552  1.1  dsl 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    553  1.1  dsl 		bp->b_error = EROFS;
    554  1.1  dsl 		goto bad;
    555  1.1  dsl 	}
    556  1.1  dsl 
    557  1.1  dsl 	/* calculate cylinder for disksort to order transfers with */
    558  1.1  dsl 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    559  1.1  dsl 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    560  1.1  dsl 	return (1);
    561  1.1  dsl 
    562  1.1  dsl bad:
    563  1.1  dsl 	bp->b_flags |= B_ERROR;
    564  1.1  dsl 	return (-1);
    565  1.1  dsl }
    566