Home | History | Annotate | Line # | Download | only in kern
subr_disk_mbr.c revision 1.17
      1  1.17       dsl /*	$NetBSD: subr_disk_mbr.c,v 1.17 2006/01/28 19:57:07 dsl 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.3       dsl  * is used.
     38   1.3       dsl  * If we don't find a label searching the MBR, we look at the start of the
     39   1.3       dsl  * disk, if that fails then a label is faked up from the MBR.
     40   1.1       dsl  *
     41   1.1       dsl  * If there isn't a disklabel or anything in the MBR then partition a
     42   1.1       dsl  * is set to cover the whole disk.
     43   1.1       dsl  * Useful for files that contain single filesystems (etc).
     44   1.1       dsl  *
     45   1.1       dsl  * This code will read host endian netbsd labels from little endian MBR.
     46   1.1       dsl  *
     47   1.1       dsl  * Based on the i386 disksubr.c
     48   1.1       dsl  *
     49   1.3       dsl  * Since the mbr only has 32bit fields for sector addresses, we do the same.
     50   1.3       dsl  *
     51   1.1       dsl  * XXX There are potential problems writing labels to disks where there
     52   1.1       dsl  * is only space for 8 netbsd partitions but this code has been compiled
     53   1.1       dsl  * with MAXPARTITIONS=16.
     54   1.1       dsl  */
     55   1.1       dsl 
     56   1.1       dsl #include <sys/cdefs.h>
     57  1.17       dsl __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.17 2006/01/28 19:57:07 dsl Exp $");
     58   1.1       dsl 
     59   1.1       dsl #include <sys/param.h>
     60   1.1       dsl #include <sys/systm.h>
     61   1.1       dsl #include <sys/buf.h>
     62   1.1       dsl #include <sys/disklabel.h>
     63   1.1       dsl #include <sys/disk.h>
     64   1.1       dsl #include <sys/syslog.h>
     65   1.1       dsl 
     66   1.1       dsl #include "opt_mbr.h"
     67   1.1       dsl 
     68  1.13       dsl typedef struct mbr_partition mbr_partition_t;
     69  1.13       dsl 
     70  1.17       dsl /*
     71  1.17       dsl  * We allocate a buffer 2 sectors large, and look in both....
     72  1.17       dsl  * That means we find labels written by other ports with different offsets.
     73  1.17       dsl  * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
     74  1.17       dsl  */
     75  1.17       dsl #if LABELSECTOR > 1 || LABELOFFSET > 512
     76  1.17       dsl #error Invalid LABELSECTOR or LABELOFFSET
     77  1.17       dsl #endif
     78  1.17       dsl 
     79   1.1       dsl #define MBR_LABELSECTOR	1
     80   1.1       dsl 
     81   1.1       dsl #define SCAN_CONTINUE	0
     82   1.1       dsl #define SCAN_FOUND	1
     83   1.1       dsl #define SCAN_ERROR	2
     84   1.1       dsl 
     85   1.1       dsl typedef struct mbr_args {
     86   1.1       dsl 	struct disklabel *lp;
     87   1.1       dsl 	void		(*strat)(struct buf *);
     88   1.1       dsl 	struct buf	*bp;
     89   1.1       dsl 	const char	*msg;
     90   1.1       dsl 	int		error;
     91   1.3       dsl 	int		written;	/* number of times we wrote label */
     92  1.17       dsl 	int		found_mbr;	/* set if disk has a valid mbr */
     93   1.3       dsl 	uint		label_sector;	/* where we found the label */
     94  1.17       dsl 	int		action;
     95   1.1       dsl #define READ_LABEL	1
     96   1.3       dsl #define UPDATE_LABEL	2
     97   1.3       dsl #define WRITE_LABEL	3
     98  1.17       dsl } mbr_args_t;
     99  1.17       dsl 
    100  1.17       dsl static int validate_label(mbr_args_t *, uint);
    101  1.13       dsl static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
    102  1.13       dsl static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
    103   1.1       dsl 
    104   1.1       dsl static int
    105  1.17       dsl read_sector(mbr_args_t *a, uint sector, int count)
    106   1.1       dsl {
    107   1.1       dsl 	struct buf *bp = a->bp;
    108   1.1       dsl 	int error;
    109   1.1       dsl 
    110   1.1       dsl 	bp->b_blkno = sector;
    111  1.17       dsl 	bp->b_bcount = count * a->lp->d_secsize;
    112   1.1       dsl 	bp->b_flags = (bp->b_flags & ~(B_WRITE | B_DONE)) | B_READ;
    113   1.1       dsl 	bp->b_cylinder = sector / a->lp->d_secpercyl;
    114   1.1       dsl 	(*a->strat)(bp);
    115   1.1       dsl 	error = biowait(bp);
    116   1.1       dsl 	if (error != 0)
    117   1.1       dsl 		a->error = error;
    118   1.1       dsl 	return error;
    119   1.1       dsl }
    120   1.1       dsl 
    121   1.6     perry /*
    122   1.1       dsl  * Scan MBR for partitions, call 'action' routine for each.
    123   1.1       dsl  */
    124   1.1       dsl 
    125   1.1       dsl static int
    126  1.13       dsl scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
    127   1.1       dsl {
    128  1.13       dsl 	mbr_partition_t ptns[MBR_PART_COUNT];
    129  1.13       dsl 	mbr_partition_t *dp;
    130   1.4     lukem 	struct mbr_sector *mbr;
    131   1.1       dsl 	uint ext_base, this_ext, next_ext;
    132   1.1       dsl 	int rval;
    133   1.1       dsl 	int i;
    134   1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    135   1.1       dsl 	int dp_386bsd = -1;
    136   1.1       dsl #endif
    137   1.1       dsl 
    138   1.1       dsl 	ext_base = 0;
    139   1.1       dsl 	this_ext = 0;
    140   1.1       dsl 	for (;;) {
    141  1.17       dsl 		if (read_sector(a, this_ext, 1)) {
    142   1.1       dsl 			a->msg = "dos partition I/O error";
    143   1.1       dsl 			return SCAN_ERROR;
    144   1.1       dsl 		}
    145   1.1       dsl 
    146   1.1       dsl 		/* Note: Magic number is little-endian. */
    147   1.1       dsl 		mbr = (void *)a->bp->b_data;
    148   1.4     lukem 		if (mbr->mbr_magic != htole16(MBR_MAGIC))
    149   1.3       dsl 			return SCAN_CONTINUE;
    150   1.1       dsl 
    151   1.1       dsl 		/* Copy data out of buffer so action can use bp */
    152   1.1       dsl 		memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
    153   1.1       dsl 
    154  1.14  christos 		/* Look for drivers and skip them */
    155  1.17       dsl 		if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
    156  1.16      jmmv 			/* We've found a DM6 DDO partition type (used by
    157  1.16      jmmv 			 * the Ontrack Disk Manager drivers).
    158  1.16      jmmv 			 *
    159  1.16      jmmv 			 * Ensure that there are no other partitions in the
    160  1.16      jmmv 			 * MBR and jump to the real partition table (stored
    161  1.16      jmmv 			 * in the first sector of the second track). */
    162  1.14  christos 			boolean_t ok = TRUE;
    163  1.14  christos 
    164  1.14  christos 			for (i = 1; i < MBR_PART_COUNT; i++)
    165  1.14  christos 				if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
    166  1.14  christos 					ok = FALSE;
    167  1.14  christos 
    168  1.14  christos 			if (ok) {
    169  1.15  christos 				this_ext = le32toh(a->lp->d_secpercyl /
    170  1.15  christos 				    a->lp->d_ntracks);
    171  1.14  christos 				continue;
    172  1.14  christos 			}
    173  1.14  christos 		}
    174  1.14  christos 
    175   1.1       dsl 		/* look for NetBSD partition */
    176   1.1       dsl 		next_ext = 0;
    177   1.1       dsl 		dp = ptns;
    178   1.4     lukem 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
    179   1.4     lukem 			if (dp->mbrp_type == 0)
    180   1.1       dsl 				continue;
    181  1.17       dsl 			/* Check end of partition is inside disk limits */
    182  1.17       dsl 			if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
    183  1.17       dsl 			    le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
    184  1.17       dsl 				/* This mbr doesn't look good.... */
    185  1.17       dsl 				a->msg = "mbr partition exceeds disk size";
    186  1.17       dsl 				/* ...but don't report this as an error (yet) */
    187  1.17       dsl 				return SCAN_CONTINUE;
    188  1.17       dsl 			}
    189  1.17       dsl 			a->found_mbr = 1;
    190   1.4     lukem 			if (MBR_IS_EXTENDED(dp->mbrp_type)) {
    191   1.1       dsl 				next_ext = le32toh(dp->mbrp_start);
    192   1.1       dsl 				continue;
    193   1.1       dsl 			}
    194   1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    195   1.4     lukem 			if (dp->mbrp_type == MBR_PTYPE_386BSD) {
    196   1.1       dsl 				/*
    197   1.1       dsl 				 * If more than one matches, take last,
    198   1.1       dsl 				 * as NetBSD install tool does.
    199   1.1       dsl 				 */
    200   1.1       dsl 				if (this_ext == 0)
    201   1.1       dsl 					dp_386bsd = i;
    202   1.1       dsl 				continue;
    203   1.1       dsl 			}
    204   1.1       dsl #endif
    205   1.1       dsl 			rval = (*actn)(a, dp, i, this_ext);
    206   1.1       dsl 			if (rval != SCAN_CONTINUE)
    207   1.1       dsl 				return rval;
    208   1.1       dsl 		}
    209   1.1       dsl 		if (next_ext == 0)
    210   1.1       dsl 			break;
    211   1.1       dsl 		if (ext_base == 0) {
    212   1.1       dsl 			ext_base = next_ext;
    213   1.1       dsl 			next_ext = 0;
    214   1.1       dsl 		}
    215   1.1       dsl 		next_ext += ext_base;
    216   1.1       dsl 		if (next_ext <= this_ext)
    217   1.1       dsl 			break;
    218   1.1       dsl 		this_ext = next_ext;
    219   1.1       dsl 	}
    220   1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    221   1.1       dsl 	if (this_ext == 0 && dp_386bsd != -1)
    222   1.1       dsl 		return (*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0);
    223   1.1       dsl #endif
    224   1.1       dsl 	return SCAN_CONTINUE;
    225   1.1       dsl }
    226   1.1       dsl 
    227   1.1       dsl /*
    228   1.1       dsl  * Attempt to read a disk label from a device
    229   1.1       dsl  * using the indicated strategy routine.
    230   1.1       dsl  * The label must be partly set up before this:
    231   1.1       dsl  * secpercyl, secsize and anything required for a block i/o read
    232   1.1       dsl  * operation in the driver's strategy/start routines
    233   1.1       dsl  * must be filled in before calling us.
    234   1.1       dsl  *
    235   1.1       dsl  * If dos partition table requested, attempt to load it and
    236   1.1       dsl  * find disklabel inside a DOS partition. Also, if bad block
    237   1.1       dsl  * table needed, attempt to extract it as well. Return buffer
    238   1.1       dsl  * for use in signalling errors if requested.
    239   1.1       dsl  *
    240   1.1       dsl  * Returns null on success and an error string on failure.
    241   1.1       dsl  */
    242   1.1       dsl const char *
    243  1.13       dsl readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
    244  1.13       dsl     struct cpu_disklabel *osdep)
    245   1.1       dsl {
    246   1.1       dsl 	struct dkbad *bdp;
    247   1.1       dsl 	int rval;
    248   1.1       dsl 	int i;
    249   1.1       dsl 	mbr_args_t a;
    250   1.1       dsl 
    251   1.3       dsl 	memset(&a, 0, sizeof a);
    252   1.1       dsl 	a.lp = lp;
    253   1.1       dsl 	a.strat = strat;
    254  1.17       dsl 	a.action = READ_LABEL;
    255   1.1       dsl 
    256   1.1       dsl 	/* minimal requirements for architypal disk label */
    257   1.1       dsl 	if (lp->d_secsize == 0)
    258   1.1       dsl 		lp->d_secsize = DEV_BSIZE;
    259   1.1       dsl 	if (lp->d_secperunit == 0)
    260   1.1       dsl 		lp->d_secperunit = 0x1fffffff;
    261   1.1       dsl 	lp->d_npartitions = RAW_PART + 1;
    262   1.1       dsl 	for (i = 0; i < RAW_PART; i++) {
    263   1.1       dsl 		lp->d_partitions[i].p_size = 0;
    264   1.1       dsl 		lp->d_partitions[i].p_offset = 0;
    265   1.1       dsl 	}
    266   1.1       dsl 	if (lp->d_partitions[RAW_PART].p_size == 0)
    267  1.17       dsl 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    268   1.1       dsl 	lp->d_partitions[RAW_PART].p_offset = 0;
    269   1.1       dsl 
    270   1.1       dsl 	/*
    271   1.1       dsl 	 * Set partition 'a' to be the whole disk.
    272   1.1       dsl 	 * Cleared if we find an mbr or a netbsd label.
    273   1.1       dsl 	 */
    274   1.1       dsl 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
    275   1.1       dsl 	lp->d_partitions[0].p_fstype = FS_BSDFFS;
    276   1.1       dsl 
    277   1.1       dsl 	/* get a buffer and initialize it */
    278  1.17       dsl 	a.bp = geteblk(2 * (int)lp->d_secsize);
    279   1.1       dsl 	a.bp->b_dev = dev;
    280   1.1       dsl 
    281   1.1       dsl 	if (osdep)
    282   1.1       dsl 		/*
    283   1.1       dsl 		 * Scan mbr searching for netbsd partition and saving
    284   1.1       dsl 		 * bios partition information to use if the netbsd one
    285   1.1       dsl 		 * is absent.
    286   1.1       dsl 		 */
    287   1.1       dsl 		rval = scan_mbr(&a, look_netbsd_part);
    288   1.1       dsl 	else
    289   1.1       dsl 		rval = SCAN_CONTINUE;
    290   1.1       dsl 
    291   1.3       dsl 	if (rval == SCAN_CONTINUE) {
    292   1.1       dsl 		/* Look at start of disk */
    293  1.17       dsl 		rval = validate_label(&a, 0);
    294   1.1       dsl 	}
    295   1.1       dsl 
    296   1.3       dsl #if 0
    297   1.3       dsl 	/*
    298   1.3       dsl 	 * Save sector where we found the label for the 'don't overwrite
    299   1.3       dsl 	 * the label' check in bounds_check_with_label.
    300   1.3       dsl 	 */
    301   1.3       dsl 	if (rval == SCAN_FOUND)
    302   1.3       dsl 		xxx->label_sector = a.label_sector;
    303   1.3       dsl #endif
    304   1.3       dsl 
    305   1.1       dsl 	/* Obtain bad sector table if requested and present */
    306   1.1       dsl 	if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
    307   1.1       dsl 		struct dkbad *db;
    308   1.1       dsl 		int blkno;
    309   1.1       dsl 
    310   1.1       dsl 		bdp = &osdep->bad;
    311   1.1       dsl 		i = 0;
    312   1.1       dsl 		rval = SCAN_ERROR;
    313   1.1       dsl 		do {
    314   1.1       dsl 			/* read a bad sector table */
    315   1.1       dsl 			blkno = lp->d_secperunit - lp->d_nsectors + i;
    316   1.1       dsl 			if (lp->d_secsize > DEV_BSIZE)
    317   1.1       dsl 				blkno *= lp->d_secsize / DEV_BSIZE;
    318   1.1       dsl 			else
    319   1.1       dsl 				blkno /= DEV_BSIZE / lp->d_secsize;
    320   1.1       dsl 			/* if successful, validate, otherwise try another */
    321  1.17       dsl 			if (read_sector(&a, blkno, 1)) {
    322   1.1       dsl 				a.msg = "bad sector table I/O error";
    323   1.1       dsl 				continue;
    324   1.1       dsl 			}
    325   1.1       dsl 			db = (struct dkbad *)(a.bp->b_data);
    326   1.1       dsl #define DKBAD_MAGIC 0x4321
    327   1.1       dsl 			if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
    328   1.1       dsl 				a.msg = "bad sector table corrupted";
    329   1.1       dsl 				continue;
    330   1.1       dsl 			}
    331   1.1       dsl 			rval = SCAN_FOUND;
    332   1.1       dsl 			*bdp = *db;
    333   1.1       dsl 			break;
    334   1.1       dsl 		} while ((a.bp->b_flags & B_ERROR) && (i += 2) < 10 &&
    335   1.1       dsl 			i < lp->d_nsectors);
    336   1.1       dsl 	}
    337   1.1       dsl 
    338   1.1       dsl 	brelse(a.bp);
    339  1.17       dsl 	if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
    340  1.10      cube 		return a.msg;
    341   1.8   reinoud 	return NULL;
    342   1.1       dsl }
    343   1.1       dsl 
    344   1.1       dsl static int
    345  1.13       dsl look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    346   1.1       dsl {
    347   1.1       dsl 	struct partition *pp;
    348   1.1       dsl 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    349   1.1       dsl 	int rval;
    350   1.1       dsl 
    351   1.1       dsl 	if (
    352   1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    353   1.4     lukem 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
    354   1.1       dsl #endif
    355   1.4     lukem 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
    356  1.17       dsl 		rval = validate_label(a, ptn_base);
    357   1.1       dsl 
    358  1.12       dsl #if RAW_PART == 3
    359   1.1       dsl 		/* Put actual location where we found the label into ptn 2 */
    360  1.12       dsl 		if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
    361   1.1       dsl 			a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
    362   1.1       dsl 			a->lp->d_partitions[2].p_offset = ptn_base;
    363   1.1       dsl 		}
    364  1.12       dsl #endif
    365   1.1       dsl 
    366   1.1       dsl 		/* If we got a netbsd label look no further */
    367   1.1       dsl 		if (rval == SCAN_FOUND)
    368   1.1       dsl 			return rval;
    369   1.1       dsl 	}
    370   1.1       dsl 
    371   1.1       dsl 	/* Install main partitions into e..h and extended into i+ */
    372   1.1       dsl 	if (ext_base == 0)
    373   1.1       dsl 		slot += 4;
    374   1.1       dsl 	else {
    375   1.4     lukem 		slot = 4 + MBR_PART_COUNT;
    376   1.1       dsl 		pp = &a->lp->d_partitions[slot];
    377   1.1       dsl 		for (; slot < MAXPARTITIONS; pp++, slot++) {
    378   1.1       dsl 			/* This gets called twice - avoid duplicates */
    379   1.1       dsl 			if (pp->p_offset == ptn_base &&
    380   1.1       dsl 			    pp->p_size == le32toh(dp->mbrp_size))
    381   1.1       dsl 				break;
    382   1.1       dsl 			if (pp->p_size == 0)
    383   1.1       dsl 				break;
    384   1.1       dsl 		}
    385   1.1       dsl 	}
    386   1.1       dsl 
    387   1.1       dsl 	if (slot < MAXPARTITIONS) {
    388   1.1       dsl 		/* Stop 'a' being the entire disk */
    389   1.1       dsl 		a->lp->d_partitions[0].p_size = 0;
    390   1.1       dsl 		a->lp->d_partitions[0].p_fstype = 0;
    391   1.1       dsl 
    392   1.1       dsl 		/* save partition info */
    393   1.1       dsl 		pp = &a->lp->d_partitions[slot];
    394   1.1       dsl 		pp->p_offset = ptn_base;
    395   1.1       dsl 		pp->p_size = le32toh(dp->mbrp_size);
    396   1.4     lukem 		pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
    397   1.1       dsl 
    398   1.1       dsl 		if (slot >= a->lp->d_npartitions)
    399   1.1       dsl 			a->lp->d_npartitions = slot + 1;
    400   1.1       dsl 	}
    401   1.1       dsl 
    402   1.1       dsl 	return SCAN_CONTINUE;
    403   1.1       dsl }
    404   1.1       dsl 
    405   1.1       dsl 
    406   1.1       dsl static int
    407  1.17       dsl validate_label(mbr_args_t *a, uint label_sector)
    408   1.1       dsl {
    409   1.3       dsl 	struct disklabel *dlp;
    410  1.17       dsl 	char *dlp_lim, *dlp_byte;
    411   1.1       dsl 	int error;
    412   1.1       dsl 
    413   1.1       dsl 	/* Next, dig out disk label */
    414  1.17       dsl 	if (read_sector(a, label_sector, 2)) {
    415   1.3       dsl 		a->msg = "disk label read failed";
    416   1.1       dsl 		return SCAN_ERROR;
    417   1.1       dsl 	}
    418   1.1       dsl 
    419   1.1       dsl 	/* Locate disk label within block and validate */
    420   1.1       dsl 	/*
    421   1.1       dsl 	 * XXX (dsl) This search may be a waste of time, a lot of other i386
    422   1.1       dsl 	 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
    423   1.1       dsl 	 *
    424   1.3       dsl 	 * If we want to support disks from other netbsd ports, then the
    425   1.1       dsl 	 * code should also allow for a shorter label nearer the end of
    426   1.1       dsl 	 * the disk sector, and (IIRC) labels within 8k of the disk start.
    427   1.1       dsl 	 */
    428   1.3       dsl 	dlp = (void *)a->bp->b_data;
    429  1.17       dsl 	dlp_lim = a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
    430  1.17       dsl 	for (;; dlp = (void *)((char *)dlp + sizeof(long))) {
    431  1.17       dsl 		if ((char *)dlp > dlp_lim) {
    432  1.17       dsl 			if (a->action != WRITE_LABEL)
    433   1.3       dsl 				return SCAN_CONTINUE;
    434  1.17       dsl 			/* Write at arch. dependant default location */
    435  1.17       dsl 			dlp_byte = a->bp->b_data + LABELOFFSET;
    436  1.17       dsl 			if (label_sector)
    437  1.17       dsl 				dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
    438  1.17       dsl 			else
    439  1.17       dsl 				dlp_byte += LABELSECTOR * a->lp->d_secsize;
    440  1.17       dsl 			dlp = (void *)dlp_byte;
    441   1.1       dsl 			break;
    442   1.1       dsl 		}
    443  1.17       dsl 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
    444  1.17       dsl 			continue;
    445  1.17       dsl 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
    446  1.17       dsl 			a->msg = "disk label corrupted";
    447  1.17       dsl 			continue;
    448  1.17       dsl 		}
    449  1.17       dsl 		break;
    450   1.3       dsl 	}
    451   1.3       dsl 
    452  1.17       dsl 	switch (a->action) {
    453   1.3       dsl 	case READ_LABEL:
    454   1.3       dsl 		*a->lp = *dlp;
    455   1.3       dsl 		a->label_sector = label_sector;
    456   1.1       dsl 		return SCAN_FOUND;
    457   1.3       dsl 	case UPDATE_LABEL:
    458   1.3       dsl 	case WRITE_LABEL:
    459   1.3       dsl 		*dlp = *a->lp;
    460   1.3       dsl 		a->bp->b_flags &= ~(B_READ|B_DONE);
    461   1.3       dsl 		a->bp->b_flags |= B_WRITE;
    462   1.3       dsl 		(*a->strat)(a->bp);
    463   1.3       dsl 		error = biowait(a->bp);
    464   1.3       dsl 		if (error != 0) {
    465   1.3       dsl 			a->error = error;
    466   1.3       dsl 			a->msg = "disk label write failed";
    467   1.3       dsl 			return SCAN_ERROR;
    468   1.3       dsl 		}
    469   1.3       dsl 		a->written++;
    470   1.3       dsl 		/* Write label to all mbr partitions */
    471   1.3       dsl 		return SCAN_CONTINUE;
    472   1.3       dsl 	default:
    473   1.3       dsl 		return SCAN_ERROR;
    474   1.1       dsl 	}
    475   1.1       dsl }
    476   1.1       dsl 
    477   1.1       dsl /*
    478   1.1       dsl  * Check new disk label for sensibility
    479   1.1       dsl  * before setting it.
    480   1.1       dsl  */
    481   1.1       dsl int
    482  1.13       dsl setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
    483  1.13       dsl     struct cpu_disklabel *osdep)
    484   1.1       dsl {
    485   1.1       dsl 	int i;
    486   1.1       dsl 	struct partition *opp, *npp;
    487   1.1       dsl 
    488   1.1       dsl 	/* sanity clause */
    489   1.1       dsl 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
    490   1.1       dsl 		|| (nlp->d_secsize % DEV_BSIZE) != 0)
    491   1.1       dsl 			return (EINVAL);
    492   1.1       dsl 
    493   1.1       dsl 	/* special case to allow disklabel to be invalidated */
    494   1.1       dsl 	if (nlp->d_magic == 0xffffffff) {
    495   1.1       dsl 		*olp = *nlp;
    496   1.1       dsl 		return (0);
    497   1.1       dsl 	}
    498   1.1       dsl 
    499   1.1       dsl 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    500   1.1       dsl 	    dkcksum(nlp) != 0)
    501   1.1       dsl 		return (EINVAL);
    502   1.1       dsl 
    503   1.1       dsl 	/* XXX missing check if other dos partitions will be overwritten */
    504   1.1       dsl 
    505   1.1       dsl 	while (openmask != 0) {
    506   1.1       dsl 		i = ffs(openmask) - 1;
    507   1.1       dsl 		openmask &= ~(1 << i);
    508   1.1       dsl 		if (i > nlp->d_npartitions)
    509   1.1       dsl 			return (EBUSY);
    510   1.1       dsl 		opp = &olp->d_partitions[i];
    511   1.1       dsl 		npp = &nlp->d_partitions[i];
    512   1.1       dsl 		/*
    513   1.1       dsl 		 * Copy internally-set partition information
    514   1.1       dsl 		 * if new label doesn't include it.		XXX
    515   1.1       dsl 		 */
    516   1.1       dsl 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
    517   1.1       dsl 			*npp = *opp;
    518   1.1       dsl 			continue;
    519   1.1       dsl 		}
    520   1.1       dsl 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    521   1.1       dsl 			return (EBUSY);
    522   1.1       dsl 	}
    523   1.1       dsl  	nlp->d_checksum = 0;
    524   1.1       dsl  	nlp->d_checksum = dkcksum(nlp);
    525   1.1       dsl 	*olp = *nlp;
    526   1.1       dsl 	return (0);
    527   1.1       dsl }
    528   1.1       dsl 
    529   1.1       dsl 
    530   1.1       dsl /*
    531   1.1       dsl  * Write disk label back to device after modification.
    532   1.1       dsl  */
    533   1.1       dsl int
    534  1.13       dsl writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
    535  1.13       dsl     struct cpu_disklabel *osdep)
    536   1.1       dsl {
    537   1.1       dsl 	mbr_args_t a;
    538   1.1       dsl 
    539   1.3       dsl 	memset(&a, 0, sizeof a);
    540   1.1       dsl 	a.lp = lp;
    541   1.1       dsl 	a.strat = strat;
    542   1.1       dsl 
    543   1.1       dsl 	/* get a buffer and initialize it */
    544  1.17       dsl 	a.bp = geteblk(2 * (int)lp->d_secsize);
    545   1.1       dsl 	a.bp->b_dev = dev;
    546   1.1       dsl 
    547  1.17       dsl 	/* osdep => we expect an mbr with label in netbsd ptn */
    548  1.17       dsl 	a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
    549  1.17       dsl 
    550  1.17       dsl 	/* Write/update the label to every netbsd mbr partition */
    551  1.17       dsl 	scan_mbr(&a, write_netbsd_label);
    552   1.3       dsl 
    553  1.17       dsl 	/* Old write the label at the start of the volume on disks that
    554  1.17       dsl 	 * don't have a valid mbr (always update an existing one) */
    555  1.17       dsl 	a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
    556  1.17       dsl 	validate_label(&a, 0);
    557   1.1       dsl 
    558   1.3       dsl 	if (a.written == 0 && a.error == 0)
    559   1.1       dsl 		a.error = ESRCH;
    560   1.1       dsl 
    561   1.1       dsl 	brelse(a.bp);
    562   1.1       dsl 	return a.error;
    563   1.1       dsl }
    564   1.1       dsl 
    565   1.1       dsl static int
    566  1.13       dsl write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    567   1.1       dsl {
    568   1.1       dsl 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    569   1.1       dsl 
    570   1.4     lukem 	if (dp->mbrp_type != MBR_PTYPE_NETBSD)
    571   1.1       dsl 		return SCAN_CONTINUE;
    572   1.1       dsl 
    573  1.17       dsl 	return validate_label(a, ptn_base);
    574   1.1       dsl }
    575   1.1       dsl 
    576   1.1       dsl 
    577   1.1       dsl /*
    578   1.1       dsl  * Determine the size of the transfer, and make sure it is
    579   1.1       dsl  * within the boundaries of the partition. Adjust transfer
    580   1.1       dsl  * if needed, and signal errors or early completion.
    581   1.1       dsl  */
    582   1.1       dsl int
    583  1.13       dsl bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
    584   1.1       dsl {
    585   1.1       dsl 	struct disklabel *lp = dk->dk_label;
    586   1.1       dsl 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    587  1.12       dsl 	int labelsector = LABELSECTOR;
    588   1.5      fvdl 	int64_t sz;
    589   1.1       dsl 
    590  1.12       dsl #if RAW_PART == 3
    591  1.12       dsl 	labelsector += lp->d_partitions[2].p_offset;
    592  1.12       dsl #endif
    593  1.12       dsl 
    594   1.1       dsl 	sz = howmany(bp->b_bcount, lp->d_secsize);
    595   1.1       dsl 
    596   1.1       dsl 	if (bp->b_blkno + sz > p->p_size) {
    597   1.1       dsl 		sz = p->p_size - bp->b_blkno;
    598   1.1       dsl 		if (sz == 0) {
    599   1.1       dsl 			/* If exactly at end of disk, return EOF. */
    600   1.1       dsl 			bp->b_resid = bp->b_bcount;
    601   1.1       dsl 			return (0);
    602   1.1       dsl 		}
    603   1.1       dsl 		if (sz < 0) {
    604   1.1       dsl 			/* If past end of disk, return EINVAL. */
    605   1.1       dsl 			bp->b_error = EINVAL;
    606   1.1       dsl 			goto bad;
    607   1.1       dsl 		}
    608   1.1       dsl 		/* Otherwise, truncate request. */
    609   1.1       dsl 		bp->b_bcount = sz << DEV_BSHIFT;
    610   1.1       dsl 	}
    611   1.1       dsl 
    612   1.1       dsl 	/* Overwriting disk label? */
    613   1.1       dsl 	if (bp->b_blkno + p->p_offset <= labelsector &&
    614   1.1       dsl 	    bp->b_blkno + p->p_offset + sz > labelsector &&
    615   1.1       dsl 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    616   1.1       dsl 		bp->b_error = EROFS;
    617   1.1       dsl 		goto bad;
    618   1.1       dsl 	}
    619   1.1       dsl 
    620   1.1       dsl 	/* calculate cylinder for disksort to order transfers with */
    621   1.1       dsl 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    622   1.1       dsl 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    623   1.1       dsl 	return (1);
    624   1.1       dsl 
    625   1.1       dsl bad:
    626   1.1       dsl 	bp->b_flags |= B_ERROR;
    627   1.1       dsl 	return (-1);
    628   1.1       dsl }
    629