Home | History | Annotate | Line # | Download | only in kern
subr_disk_mbr.c revision 1.31.10.2
      1  1.31.10.2      yamt /*	$NetBSD: subr_disk_mbr.c,v 1.31.10.2 2009/06/20 07:20:31 yamt 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.31.10.1      yamt  * If there isn't a disklabel or anything in the MBR then the disc is searched
     42  1.31.10.1      yamt  * for ecma-167/iso9660/udf style partition indicators.
     43  1.31.10.1      yamt  * Useful for media or 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.31.10.2      yamt __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.31.10.2 2009/06/20 07:20:31 yamt 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.27    dyoung #include <sys/bootblock.h>
     63        1.1       dsl #include <sys/disklabel.h>
     64        1.1       dsl #include <sys/disk.h>
     65        1.1       dsl #include <sys/syslog.h>
     66  1.31.10.1      yamt #include <sys/vnode.h>
     67  1.31.10.1      yamt #include <sys/fcntl.h>
     68  1.31.10.1      yamt #include <sys/conf.h>
     69  1.31.10.1      yamt #include <sys/cdio.h>
     70  1.31.10.1      yamt #include <fs/udf/ecma167-udf.h>
     71  1.31.10.1      yamt 
     72  1.31.10.1      yamt #include <sys/kauth.h>
     73        1.1       dsl 
     74  1.31.10.2      yamt #ifdef _KERNEL_OPT
     75        1.1       dsl #include "opt_mbr.h"
     76  1.31.10.2      yamt #endif /* _KERNEL_OPT */
     77        1.1       dsl 
     78       1.13       dsl typedef struct mbr_partition mbr_partition_t;
     79       1.13       dsl 
     80       1.17       dsl /*
     81       1.17       dsl  * We allocate a buffer 2 sectors large, and look in both....
     82       1.17       dsl  * That means we find labels written by other ports with different offsets.
     83       1.17       dsl  * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
     84       1.17       dsl  */
     85       1.17       dsl #if LABELSECTOR > 1 || LABELOFFSET > 512
     86       1.17       dsl #error Invalid LABELSECTOR or LABELOFFSET
     87       1.17       dsl #endif
     88       1.17       dsl 
     89        1.1       dsl #define MBR_LABELSECTOR	1
     90        1.1       dsl 
     91        1.1       dsl #define SCAN_CONTINUE	0
     92        1.1       dsl #define SCAN_FOUND	1
     93        1.1       dsl #define SCAN_ERROR	2
     94        1.1       dsl 
     95        1.1       dsl typedef struct mbr_args {
     96        1.1       dsl 	struct disklabel *lp;
     97        1.1       dsl 	void		(*strat)(struct buf *);
     98        1.1       dsl 	struct buf	*bp;
     99        1.1       dsl 	const char	*msg;
    100        1.1       dsl 	int		error;
    101        1.3       dsl 	int		written;	/* number of times we wrote label */
    102       1.17       dsl 	int		found_mbr;	/* set if disk has a valid mbr */
    103        1.3       dsl 	uint		label_sector;	/* where we found the label */
    104       1.17       dsl 	int		action;
    105       1.28    dyoung 	uint32_t	secperunit;
    106        1.1       dsl #define READ_LABEL	1
    107        1.3       dsl #define UPDATE_LABEL	2
    108        1.3       dsl #define WRITE_LABEL	3
    109       1.17       dsl } mbr_args_t;
    110       1.17       dsl 
    111       1.17       dsl static int validate_label(mbr_args_t *, uint);
    112       1.13       dsl static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
    113       1.13       dsl static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
    114        1.1       dsl 
    115        1.1       dsl static int
    116       1.17       dsl read_sector(mbr_args_t *a, uint sector, int count)
    117        1.1       dsl {
    118        1.1       dsl 	int error;
    119        1.1       dsl 
    120       1.28    dyoung 	error = disk_read_sectors(a->strat, a->lp, a->bp, sector, count);
    121        1.1       dsl 	if (error != 0)
    122        1.1       dsl 		a->error = error;
    123        1.1       dsl 	return error;
    124        1.1       dsl }
    125        1.1       dsl 
    126        1.6     perry /*
    127        1.1       dsl  * Scan MBR for partitions, call 'action' routine for each.
    128        1.1       dsl  */
    129        1.1       dsl 
    130        1.1       dsl static int
    131       1.13       dsl scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
    132        1.1       dsl {
    133       1.13       dsl 	mbr_partition_t ptns[MBR_PART_COUNT];
    134       1.13       dsl 	mbr_partition_t *dp;
    135        1.4     lukem 	struct mbr_sector *mbr;
    136        1.1       dsl 	uint ext_base, this_ext, next_ext;
    137        1.1       dsl 	int rval;
    138        1.1       dsl 	int i;
    139       1.18  christos 	int j;
    140        1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    141        1.1       dsl 	int dp_386bsd = -1;
    142       1.18  christos 	int ap_386bsd = -1;
    143        1.1       dsl #endif
    144        1.1       dsl 
    145        1.1       dsl 	ext_base = 0;
    146        1.1       dsl 	this_ext = 0;
    147        1.1       dsl 	for (;;) {
    148       1.17       dsl 		if (read_sector(a, this_ext, 1)) {
    149        1.1       dsl 			a->msg = "dos partition I/O error";
    150        1.1       dsl 			return SCAN_ERROR;
    151        1.1       dsl 		}
    152        1.1       dsl 
    153        1.1       dsl 		/* Note: Magic number is little-endian. */
    154        1.1       dsl 		mbr = (void *)a->bp->b_data;
    155        1.4     lukem 		if (mbr->mbr_magic != htole16(MBR_MAGIC))
    156        1.3       dsl 			return SCAN_CONTINUE;
    157        1.1       dsl 
    158        1.1       dsl 		/* Copy data out of buffer so action can use bp */
    159        1.1       dsl 		memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
    160        1.1       dsl 
    161       1.14  christos 		/* Look for drivers and skip them */
    162       1.17       dsl 		if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
    163       1.16      jmmv 			/* We've found a DM6 DDO partition type (used by
    164       1.16      jmmv 			 * the Ontrack Disk Manager drivers).
    165       1.16      jmmv 			 *
    166       1.16      jmmv 			 * Ensure that there are no other partitions in the
    167       1.16      jmmv 			 * MBR and jump to the real partition table (stored
    168       1.16      jmmv 			 * in the first sector of the second track). */
    169       1.25   thorpej 			bool ok = true;
    170       1.14  christos 
    171       1.14  christos 			for (i = 1; i < MBR_PART_COUNT; i++)
    172       1.14  christos 				if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
    173       1.25   thorpej 					ok = false;
    174       1.14  christos 
    175       1.14  christos 			if (ok) {
    176       1.15  christos 				this_ext = le32toh(a->lp->d_secpercyl /
    177       1.15  christos 				    a->lp->d_ntracks);
    178       1.14  christos 				continue;
    179       1.14  christos 			}
    180       1.14  christos 		}
    181       1.14  christos 
    182        1.1       dsl 		/* look for NetBSD partition */
    183        1.1       dsl 		next_ext = 0;
    184        1.1       dsl 		dp = ptns;
    185       1.18  christos 		j = 0;
    186        1.4     lukem 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
    187       1.18  christos 			if (dp->mbrp_type == MBR_PTYPE_UNUSED)
    188        1.1       dsl 				continue;
    189       1.17       dsl 			/* Check end of partition is inside disk limits */
    190       1.17       dsl 			if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
    191       1.17       dsl 			    le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
    192       1.17       dsl 				/* This mbr doesn't look good.... */
    193       1.17       dsl 				a->msg = "mbr partition exceeds disk size";
    194       1.17       dsl 				/* ...but don't report this as an error (yet) */
    195       1.17       dsl 				return SCAN_CONTINUE;
    196       1.17       dsl 			}
    197       1.17       dsl 			a->found_mbr = 1;
    198        1.4     lukem 			if (MBR_IS_EXTENDED(dp->mbrp_type)) {
    199        1.1       dsl 				next_ext = le32toh(dp->mbrp_start);
    200        1.1       dsl 				continue;
    201        1.1       dsl 			}
    202        1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    203        1.4     lukem 			if (dp->mbrp_type == MBR_PTYPE_386BSD) {
    204        1.1       dsl 				/*
    205        1.1       dsl 				 * If more than one matches, take last,
    206        1.1       dsl 				 * as NetBSD install tool does.
    207        1.1       dsl 				 */
    208       1.18  christos 				if (this_ext == 0) {
    209        1.1       dsl 					dp_386bsd = i;
    210       1.18  christos 					ap_386bsd = j;
    211       1.18  christos 				}
    212        1.1       dsl 				continue;
    213        1.1       dsl 			}
    214        1.1       dsl #endif
    215       1.18  christos 			rval = (*actn)(a, dp, j, this_ext);
    216        1.1       dsl 			if (rval != SCAN_CONTINUE)
    217        1.1       dsl 				return rval;
    218       1.18  christos 			j++;
    219        1.1       dsl 		}
    220        1.1       dsl 		if (next_ext == 0)
    221        1.1       dsl 			break;
    222        1.1       dsl 		if (ext_base == 0) {
    223        1.1       dsl 			ext_base = next_ext;
    224        1.1       dsl 			next_ext = 0;
    225        1.1       dsl 		}
    226        1.1       dsl 		next_ext += ext_base;
    227        1.1       dsl 		if (next_ext <= this_ext)
    228        1.1       dsl 			break;
    229        1.1       dsl 		this_ext = next_ext;
    230        1.1       dsl 	}
    231        1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    232        1.1       dsl 	if (this_ext == 0 && dp_386bsd != -1)
    233       1.18  christos 		return (*actn)(a, &ptns[dp_386bsd], ap_386bsd, 0);
    234        1.1       dsl #endif
    235        1.1       dsl 	return SCAN_CONTINUE;
    236        1.1       dsl }
    237        1.1       dsl 
    238  1.31.10.1      yamt 
    239  1.31.10.1      yamt static void
    240  1.31.10.1      yamt scan_iso_vrs_session(mbr_args_t *a, uint32_t first_sector,
    241  1.31.10.1      yamt 	int *is_iso9660, int *is_udf)
    242  1.31.10.1      yamt {
    243  1.31.10.1      yamt 	struct vrs_desc *vrsd;
    244  1.31.10.1      yamt 	uint64_t vrs;
    245  1.31.10.1      yamt 	int sector_size;
    246  1.31.10.1      yamt 	int blks, inc;
    247  1.31.10.1      yamt 
    248  1.31.10.1      yamt 	sector_size = a->lp->d_secsize;
    249  1.31.10.1      yamt 	blks = sector_size / DEV_BSIZE;
    250  1.31.10.1      yamt 	inc  = MAX(1, 2048 / sector_size);
    251  1.31.10.1      yamt 
    252  1.31.10.1      yamt 	/* by definition */
    253  1.31.10.1      yamt 	vrs = ((32*1024 + sector_size - 1) / sector_size)
    254  1.31.10.1      yamt 	        + first_sector;
    255  1.31.10.1      yamt 
    256  1.31.10.1      yamt 	/* read first vrs sector */
    257  1.31.10.1      yamt 	if (read_sector(a, vrs * blks, 1))
    258  1.31.10.1      yamt 		return;
    259  1.31.10.1      yamt 
    260  1.31.10.1      yamt 	/* skip all CD001 records */
    261  1.31.10.1      yamt 	vrsd = a->bp->b_data;
    262  1.31.10.1      yamt 	/* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
    263  1.31.10.1      yamt 	while (memcmp(vrsd->identifier, "CD001", 5) == 0) {
    264  1.31.10.1      yamt 		/* for sure */
    265  1.31.10.1      yamt 		*is_iso9660 = first_sector;
    266  1.31.10.1      yamt 
    267  1.31.10.1      yamt 		vrs += inc;
    268  1.31.10.1      yamt 		if (read_sector(a, vrs * blks, 1))
    269  1.31.10.1      yamt 			return;
    270  1.31.10.1      yamt 	}
    271  1.31.10.1      yamt 
    272  1.31.10.1      yamt 	/* search for BEA01 */
    273  1.31.10.1      yamt 	vrsd = a->bp->b_data;
    274  1.31.10.1      yamt 	/* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
    275  1.31.10.1      yamt 	if (memcmp(vrsd->identifier, "BEA01", 5))
    276  1.31.10.1      yamt 		return;
    277  1.31.10.1      yamt 
    278  1.31.10.1      yamt 	/* read successor */
    279  1.31.10.1      yamt 	vrs += inc;
    280  1.31.10.1      yamt 	if (read_sector(a, vrs * blks, 1))
    281  1.31.10.1      yamt 		return;
    282  1.31.10.1      yamt 
    283  1.31.10.1      yamt 	/* check for NSR[23] */
    284  1.31.10.1      yamt 	vrsd = a->bp->b_data;
    285  1.31.10.1      yamt 	/* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
    286  1.31.10.1      yamt 	if (memcmp(vrsd->identifier, "NSR0", 4))
    287  1.31.10.1      yamt 		return;
    288  1.31.10.1      yamt 
    289  1.31.10.1      yamt 	*is_udf = first_sector;
    290  1.31.10.1      yamt }
    291  1.31.10.1      yamt 
    292  1.31.10.1      yamt 
    293  1.31.10.1      yamt /*
    294  1.31.10.1      yamt  * Scan for ISO Volume Recognition Sequences
    295  1.31.10.1      yamt  */
    296  1.31.10.1      yamt 
    297  1.31.10.1      yamt static int
    298  1.31.10.1      yamt scan_iso_vrs(mbr_args_t *a)
    299  1.31.10.1      yamt {
    300  1.31.10.1      yamt 	struct mmc_discinfo  di;
    301  1.31.10.1      yamt 	struct mmc_trackinfo ti;
    302  1.31.10.1      yamt 	dev_t dev;
    303  1.31.10.1      yamt 	uint64_t sector;
    304  1.31.10.1      yamt 	int is_iso9660, is_udf;
    305  1.31.10.1      yamt 	int tracknr, sessionnr;
    306  1.31.10.1      yamt 	int new_session, error;
    307  1.31.10.1      yamt 
    308  1.31.10.1      yamt 	is_iso9660 = is_udf = -1;
    309  1.31.10.1      yamt 
    310  1.31.10.1      yamt 	/* parse all sessions of disc if we're on a SCSI MMC device */
    311  1.31.10.1      yamt 	if (a->lp->d_flags & D_SCSI_MMC) {
    312  1.31.10.1      yamt 		/* get disc info */
    313  1.31.10.1      yamt 		dev = a->bp->b_dev;
    314  1.31.10.1      yamt 		error = bdev_ioctl(dev, MMCGETDISCINFO, &di, FKIOCTL, curlwp);
    315  1.31.10.1      yamt 		if (error)
    316  1.31.10.1      yamt 			return SCAN_CONTINUE;
    317  1.31.10.1      yamt 
    318  1.31.10.1      yamt 		/* go trough all (data) tracks */
    319  1.31.10.1      yamt 		sessionnr = -1;
    320  1.31.10.1      yamt 		for (tracknr = di.first_track;
    321  1.31.10.1      yamt 		    tracknr <= di.first_track_last_session; tracknr++)
    322  1.31.10.1      yamt 		{
    323  1.31.10.1      yamt 			ti.tracknr = tracknr;
    324  1.31.10.1      yamt 			error = bdev_ioctl(dev, MMCGETTRACKINFO, &ti,
    325  1.31.10.1      yamt 					FKIOCTL, curlwp);
    326  1.31.10.1      yamt 			if (error)
    327  1.31.10.1      yamt 				return SCAN_CONTINUE;
    328  1.31.10.1      yamt 			new_session = (ti.sessionnr != sessionnr);
    329  1.31.10.1      yamt 			sessionnr = ti.sessionnr;
    330  1.31.10.1      yamt 			if (new_session) {
    331  1.31.10.1      yamt 				if (ti.flags & MMC_TRACKINFO_BLANK)
    332  1.31.10.1      yamt 					continue;
    333  1.31.10.1      yamt 				if (!(ti.flags & MMC_TRACKINFO_DATA))
    334  1.31.10.1      yamt 					continue;
    335  1.31.10.1      yamt 				sector = ti.track_start;
    336  1.31.10.1      yamt 				scan_iso_vrs_session(a, sector,
    337  1.31.10.1      yamt 					&is_iso9660, &is_udf);
    338  1.31.10.1      yamt 			}
    339  1.31.10.1      yamt 		}
    340  1.31.10.1      yamt 		if (is_udf < 0) {
    341  1.31.10.1      yamt 			/* defaulting udf on the RAW partition */
    342  1.31.10.1      yamt 			is_udf = 0;
    343  1.31.10.1      yamt 		}
    344  1.31.10.1      yamt 	} else {
    345  1.31.10.1      yamt 		/* try start of disc */
    346  1.31.10.1      yamt 		sector = 0;
    347  1.31.10.1      yamt 		scan_iso_vrs_session(a, sector, &is_iso9660, &is_udf);
    348  1.31.10.1      yamt 	}
    349  1.31.10.1      yamt 
    350  1.31.10.1      yamt 	if ((is_iso9660 < 0) && (is_udf < 0))
    351  1.31.10.1      yamt 		return SCAN_CONTINUE;
    352  1.31.10.1      yamt 
    353  1.31.10.1      yamt 	strncpy(a->lp->d_typename, "iso partition", 16);
    354  1.31.10.1      yamt 
    355  1.31.10.1      yamt 	/* add iso9660 partition if found */
    356  1.31.10.1      yamt 	if (is_iso9660 >= 0) {
    357  1.31.10.1      yamt 		/* set 'a' partition to iso9660 */
    358  1.31.10.1      yamt 		a->lp->d_partitions[0].p_offset = 0;
    359  1.31.10.1      yamt 		a->lp->d_partitions[0].p_size   = a->lp->d_secperunit;
    360  1.31.10.1      yamt 		a->lp->d_partitions[0].p_cdsession = is_iso9660;
    361  1.31.10.1      yamt 		a->lp->d_partitions[0].p_fstype = FS_ISO9660;
    362  1.31.10.1      yamt 	} else {
    363  1.31.10.1      yamt 		a->lp->d_partitions[0].p_size   = 0;
    364  1.31.10.1      yamt 		a->lp->d_partitions[0].p_fstype = FS_UNUSED;
    365  1.31.10.1      yamt 	}
    366  1.31.10.1      yamt 
    367  1.31.10.1      yamt 	/* add udf partition if found */
    368  1.31.10.1      yamt 	if (is_udf >= 0) {
    369  1.31.10.1      yamt 		/* set the RAW partion to UDF for CD/USB stick etc */
    370  1.31.10.1      yamt 		a->lp->d_partitions[RAW_PART].p_fstype = FS_UDF;
    371  1.31.10.1      yamt 		/* UDF doesn't care about the cd session specified here */
    372  1.31.10.1      yamt 	}
    373  1.31.10.1      yamt 
    374  1.31.10.1      yamt 	return SCAN_FOUND;
    375  1.31.10.1      yamt }
    376  1.31.10.1      yamt 
    377  1.31.10.1      yamt 
    378        1.1       dsl /*
    379        1.1       dsl  * Attempt to read a disk label from a device
    380        1.1       dsl  * using the indicated strategy routine.
    381        1.1       dsl  * The label must be partly set up before this:
    382        1.1       dsl  * secpercyl, secsize and anything required for a block i/o read
    383        1.1       dsl  * operation in the driver's strategy/start routines
    384        1.1       dsl  * must be filled in before calling us.
    385        1.1       dsl  *
    386        1.1       dsl  * If dos partition table requested, attempt to load it and
    387        1.1       dsl  * find disklabel inside a DOS partition. Also, if bad block
    388        1.1       dsl  * table needed, attempt to extract it as well. Return buffer
    389        1.1       dsl  * for use in signalling errors if requested.
    390        1.1       dsl  *
    391        1.1       dsl  * Returns null on success and an error string on failure.
    392        1.1       dsl  */
    393        1.1       dsl const char *
    394       1.13       dsl readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
    395       1.13       dsl     struct cpu_disklabel *osdep)
    396        1.1       dsl {
    397        1.1       dsl 	struct dkbad *bdp;
    398        1.1       dsl 	int rval;
    399        1.1       dsl 	int i;
    400        1.1       dsl 	mbr_args_t a;
    401        1.1       dsl 
    402        1.3       dsl 	memset(&a, 0, sizeof a);
    403        1.1       dsl 	a.lp = lp;
    404        1.1       dsl 	a.strat = strat;
    405       1.17       dsl 	a.action = READ_LABEL;
    406        1.1       dsl 
    407        1.1       dsl 	/* minimal requirements for architypal disk label */
    408        1.1       dsl 	if (lp->d_secsize == 0)
    409        1.1       dsl 		lp->d_secsize = DEV_BSIZE;
    410        1.1       dsl 	if (lp->d_secperunit == 0)
    411        1.1       dsl 		lp->d_secperunit = 0x1fffffff;
    412       1.28    dyoung 	a.secperunit = lp->d_secperunit;
    413        1.1       dsl 	lp->d_npartitions = RAW_PART + 1;
    414        1.1       dsl 	for (i = 0; i < RAW_PART; i++) {
    415        1.1       dsl 		lp->d_partitions[i].p_size = 0;
    416        1.1       dsl 		lp->d_partitions[i].p_offset = 0;
    417        1.1       dsl 	}
    418        1.1       dsl 	if (lp->d_partitions[RAW_PART].p_size == 0)
    419       1.17       dsl 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    420        1.1       dsl 	lp->d_partitions[RAW_PART].p_offset = 0;
    421        1.1       dsl 
    422        1.1       dsl 	/*
    423        1.1       dsl 	 * Set partition 'a' to be the whole disk.
    424        1.1       dsl 	 * Cleared if we find an mbr or a netbsd label.
    425        1.1       dsl 	 */
    426        1.1       dsl 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
    427        1.1       dsl 	lp->d_partitions[0].p_fstype = FS_BSDFFS;
    428        1.1       dsl 
    429  1.31.10.1      yamt 	/*
    430  1.31.10.1      yamt 	 * Get a buffer big enough to read a disklabel in and initialize it
    431  1.31.10.1      yamt 	 * make it two sectors long for the validate_label(); see comment at
    432  1.31.10.1      yamt 	 * start of file.
    433  1.31.10.1      yamt 	 */
    434       1.17       dsl 	a.bp = geteblk(2 * (int)lp->d_secsize);
    435        1.1       dsl 	a.bp->b_dev = dev;
    436        1.1       dsl 
    437        1.1       dsl 	if (osdep)
    438        1.1       dsl 		/*
    439        1.1       dsl 		 * Scan mbr searching for netbsd partition and saving
    440        1.1       dsl 		 * bios partition information to use if the netbsd one
    441        1.1       dsl 		 * is absent.
    442        1.1       dsl 		 */
    443        1.1       dsl 		rval = scan_mbr(&a, look_netbsd_part);
    444        1.1       dsl 	else
    445        1.1       dsl 		rval = SCAN_CONTINUE;
    446        1.1       dsl 
    447        1.3       dsl 	if (rval == SCAN_CONTINUE) {
    448        1.1       dsl 		/* Look at start of disk */
    449       1.17       dsl 		rval = validate_label(&a, 0);
    450        1.1       dsl 	}
    451        1.1       dsl 
    452  1.31.10.1      yamt 	if (rval == SCAN_CONTINUE) {
    453  1.31.10.1      yamt 		rval = scan_iso_vrs(&a);
    454  1.31.10.1      yamt 	}
    455        1.3       dsl #if 0
    456        1.3       dsl 	/*
    457        1.3       dsl 	 * Save sector where we found the label for the 'don't overwrite
    458        1.3       dsl 	 * the label' check in bounds_check_with_label.
    459        1.3       dsl 	 */
    460        1.3       dsl 	if (rval == SCAN_FOUND)
    461        1.3       dsl 		xxx->label_sector = a.label_sector;
    462        1.3       dsl #endif
    463        1.3       dsl 
    464        1.1       dsl 	/* Obtain bad sector table if requested and present */
    465        1.1       dsl 	if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
    466        1.1       dsl 		struct dkbad *db;
    467        1.1       dsl 		int blkno;
    468        1.1       dsl 
    469        1.1       dsl 		bdp = &osdep->bad;
    470        1.1       dsl 		i = 0;
    471        1.1       dsl 		rval = SCAN_ERROR;
    472        1.1       dsl 		do {
    473        1.1       dsl 			/* read a bad sector table */
    474        1.1       dsl 			blkno = lp->d_secperunit - lp->d_nsectors + i;
    475        1.1       dsl 			if (lp->d_secsize > DEV_BSIZE)
    476        1.1       dsl 				blkno *= lp->d_secsize / DEV_BSIZE;
    477        1.1       dsl 			else
    478        1.1       dsl 				blkno /= DEV_BSIZE / lp->d_secsize;
    479        1.1       dsl 			/* if successful, validate, otherwise try another */
    480       1.17       dsl 			if (read_sector(&a, blkno, 1)) {
    481        1.1       dsl 				a.msg = "bad sector table I/O error";
    482        1.1       dsl 				continue;
    483        1.1       dsl 			}
    484        1.1       dsl 			db = (struct dkbad *)(a.bp->b_data);
    485        1.1       dsl #define DKBAD_MAGIC 0x4321
    486        1.1       dsl 			if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
    487        1.1       dsl 				a.msg = "bad sector table corrupted";
    488        1.1       dsl 				continue;
    489        1.1       dsl 			}
    490        1.1       dsl 			rval = SCAN_FOUND;
    491        1.1       dsl 			*bdp = *db;
    492        1.1       dsl 			break;
    493       1.29        ad 		} while (a.bp->b_error && (i += 2) < 10 &&
    494        1.1       dsl 			i < lp->d_nsectors);
    495        1.1       dsl 	}
    496        1.1       dsl 
    497       1.30        ad 	brelse(a.bp, 0);
    498       1.17       dsl 	if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
    499       1.10      cube 		return a.msg;
    500        1.8   reinoud 	return NULL;
    501        1.1       dsl }
    502        1.1       dsl 
    503        1.1       dsl static int
    504       1.13       dsl look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    505        1.1       dsl {
    506        1.1       dsl 	struct partition *pp;
    507        1.1       dsl 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    508        1.1       dsl 	int rval;
    509        1.1       dsl 
    510        1.1       dsl 	if (
    511        1.1       dsl #ifdef COMPAT_386BSD_MBRPART
    512        1.4     lukem 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
    513        1.1       dsl #endif
    514        1.4     lukem 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
    515       1.17       dsl 		rval = validate_label(a, ptn_base);
    516        1.1       dsl 
    517       1.12       dsl #if RAW_PART == 3
    518        1.1       dsl 		/* Put actual location where we found the label into ptn 2 */
    519       1.12       dsl 		if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
    520        1.1       dsl 			a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
    521        1.1       dsl 			a->lp->d_partitions[2].p_offset = ptn_base;
    522        1.1       dsl 		}
    523       1.12       dsl #endif
    524        1.1       dsl 
    525        1.1       dsl 		/* If we got a netbsd label look no further */
    526        1.1       dsl 		if (rval == SCAN_FOUND)
    527        1.1       dsl 			return rval;
    528        1.1       dsl 	}
    529        1.1       dsl 
    530        1.1       dsl 	/* Install main partitions into e..h and extended into i+ */
    531        1.1       dsl 	if (ext_base == 0)
    532        1.1       dsl 		slot += 4;
    533        1.1       dsl 	else {
    534        1.4     lukem 		slot = 4 + MBR_PART_COUNT;
    535        1.1       dsl 		pp = &a->lp->d_partitions[slot];
    536        1.1       dsl 		for (; slot < MAXPARTITIONS; pp++, slot++) {
    537        1.1       dsl 			/* This gets called twice - avoid duplicates */
    538        1.1       dsl 			if (pp->p_offset == ptn_base &&
    539        1.1       dsl 			    pp->p_size == le32toh(dp->mbrp_size))
    540        1.1       dsl 				break;
    541        1.1       dsl 			if (pp->p_size == 0)
    542        1.1       dsl 				break;
    543        1.1       dsl 		}
    544        1.1       dsl 	}
    545        1.1       dsl 
    546        1.1       dsl 	if (slot < MAXPARTITIONS) {
    547        1.1       dsl 		/* Stop 'a' being the entire disk */
    548        1.1       dsl 		a->lp->d_partitions[0].p_size = 0;
    549        1.1       dsl 		a->lp->d_partitions[0].p_fstype = 0;
    550        1.1       dsl 
    551        1.1       dsl 		/* save partition info */
    552        1.1       dsl 		pp = &a->lp->d_partitions[slot];
    553        1.1       dsl 		pp->p_offset = ptn_base;
    554        1.1       dsl 		pp->p_size = le32toh(dp->mbrp_size);
    555        1.4     lukem 		pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
    556        1.1       dsl 
    557        1.1       dsl 		if (slot >= a->lp->d_npartitions)
    558        1.1       dsl 			a->lp->d_npartitions = slot + 1;
    559        1.1       dsl 	}
    560        1.1       dsl 
    561        1.1       dsl 	return SCAN_CONTINUE;
    562        1.1       dsl }
    563        1.1       dsl 
    564        1.1       dsl 
    565        1.1       dsl static int
    566       1.17       dsl validate_label(mbr_args_t *a, uint label_sector)
    567        1.1       dsl {
    568        1.3       dsl 	struct disklabel *dlp;
    569       1.17       dsl 	char *dlp_lim, *dlp_byte;
    570        1.1       dsl 	int error;
    571        1.1       dsl 
    572        1.1       dsl 	/* Next, dig out disk label */
    573       1.17       dsl 	if (read_sector(a, label_sector, 2)) {
    574        1.3       dsl 		a->msg = "disk label read failed";
    575        1.1       dsl 		return SCAN_ERROR;
    576        1.1       dsl 	}
    577        1.1       dsl 
    578        1.1       dsl 	/* Locate disk label within block and validate */
    579        1.1       dsl 	/*
    580        1.1       dsl 	 * XXX (dsl) This search may be a waste of time, a lot of other i386
    581        1.1       dsl 	 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
    582        1.1       dsl 	 *
    583        1.3       dsl 	 * If we want to support disks from other netbsd ports, then the
    584        1.1       dsl 	 * code should also allow for a shorter label nearer the end of
    585        1.1       dsl 	 * the disk sector, and (IIRC) labels within 8k of the disk start.
    586        1.1       dsl 	 */
    587        1.3       dsl 	dlp = (void *)a->bp->b_data;
    588       1.26  christos 	dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
    589       1.17       dsl 	for (;; dlp = (void *)((char *)dlp + sizeof(long))) {
    590       1.17       dsl 		if ((char *)dlp > dlp_lim) {
    591       1.17       dsl 			if (a->action != WRITE_LABEL)
    592        1.3       dsl 				return SCAN_CONTINUE;
    593       1.17       dsl 			/* Write at arch. dependant default location */
    594       1.26  christos 			dlp_byte = (char *)a->bp->b_data + LABELOFFSET;
    595       1.17       dsl 			if (label_sector)
    596       1.17       dsl 				dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
    597       1.17       dsl 			else
    598       1.17       dsl 				dlp_byte += LABELSECTOR * a->lp->d_secsize;
    599       1.17       dsl 			dlp = (void *)dlp_byte;
    600        1.1       dsl 			break;
    601        1.1       dsl 		}
    602       1.17       dsl 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
    603       1.17       dsl 			continue;
    604       1.17       dsl 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
    605       1.17       dsl 			a->msg = "disk label corrupted";
    606       1.17       dsl 			continue;
    607       1.17       dsl 		}
    608       1.17       dsl 		break;
    609        1.3       dsl 	}
    610        1.3       dsl 
    611       1.17       dsl 	switch (a->action) {
    612        1.3       dsl 	case READ_LABEL:
    613        1.3       dsl 		*a->lp = *dlp;
    614       1.28    dyoung 		if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp,
    615       1.28    dyoung 		                              a->secperunit)) != NULL)
    616       1.28    dyoung 			return SCAN_ERROR;
    617        1.3       dsl 		a->label_sector = label_sector;
    618        1.1       dsl 		return SCAN_FOUND;
    619        1.3       dsl 	case UPDATE_LABEL:
    620        1.3       dsl 	case WRITE_LABEL:
    621        1.3       dsl 		*dlp = *a->lp;
    622       1.31        ad 		a->bp->b_oflags &= ~BO_DONE;
    623       1.31        ad 		a->bp->b_flags &= ~B_READ;
    624        1.3       dsl 		a->bp->b_flags |= B_WRITE;
    625        1.3       dsl 		(*a->strat)(a->bp);
    626        1.3       dsl 		error = biowait(a->bp);
    627        1.3       dsl 		if (error != 0) {
    628        1.3       dsl 			a->error = error;
    629        1.3       dsl 			a->msg = "disk label write failed";
    630        1.3       dsl 			return SCAN_ERROR;
    631        1.3       dsl 		}
    632        1.3       dsl 		a->written++;
    633        1.3       dsl 		/* Write label to all mbr partitions */
    634        1.3       dsl 		return SCAN_CONTINUE;
    635        1.3       dsl 	default:
    636        1.3       dsl 		return SCAN_ERROR;
    637        1.1       dsl 	}
    638        1.1       dsl }
    639        1.1       dsl 
    640        1.1       dsl /*
    641        1.1       dsl  * Check new disk label for sensibility
    642        1.1       dsl  * before setting it.
    643        1.1       dsl  */
    644        1.1       dsl int
    645       1.13       dsl setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
    646       1.21      yamt     struct cpu_disklabel *osdep)
    647        1.1       dsl {
    648        1.1       dsl 	int i;
    649        1.1       dsl 	struct partition *opp, *npp;
    650        1.1       dsl 
    651        1.1       dsl 	/* sanity clause */
    652        1.1       dsl 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
    653        1.1       dsl 		|| (nlp->d_secsize % DEV_BSIZE) != 0)
    654        1.1       dsl 			return (EINVAL);
    655        1.1       dsl 
    656        1.1       dsl 	/* special case to allow disklabel to be invalidated */
    657        1.1       dsl 	if (nlp->d_magic == 0xffffffff) {
    658        1.1       dsl 		*olp = *nlp;
    659        1.1       dsl 		return (0);
    660        1.1       dsl 	}
    661        1.1       dsl 
    662        1.1       dsl 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    663        1.1       dsl 	    dkcksum(nlp) != 0)
    664        1.1       dsl 		return (EINVAL);
    665        1.1       dsl 
    666        1.1       dsl 	/* XXX missing check if other dos partitions will be overwritten */
    667        1.1       dsl 
    668        1.1       dsl 	while (openmask != 0) {
    669        1.1       dsl 		i = ffs(openmask) - 1;
    670        1.1       dsl 		openmask &= ~(1 << i);
    671        1.1       dsl 		if (i > nlp->d_npartitions)
    672        1.1       dsl 			return (EBUSY);
    673        1.1       dsl 		opp = &olp->d_partitions[i];
    674        1.1       dsl 		npp = &nlp->d_partitions[i];
    675        1.1       dsl 		/*
    676        1.1       dsl 		 * Copy internally-set partition information
    677        1.1       dsl 		 * if new label doesn't include it.		XXX
    678        1.1       dsl 		 */
    679        1.1       dsl 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
    680        1.1       dsl 			*npp = *opp;
    681        1.1       dsl 			continue;
    682        1.1       dsl 		}
    683        1.1       dsl 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    684        1.1       dsl 			return (EBUSY);
    685        1.1       dsl 	}
    686        1.1       dsl  	nlp->d_checksum = 0;
    687        1.1       dsl  	nlp->d_checksum = dkcksum(nlp);
    688        1.1       dsl 	*olp = *nlp;
    689        1.1       dsl 	return (0);
    690        1.1       dsl }
    691        1.1       dsl 
    692        1.1       dsl 
    693        1.1       dsl /*
    694        1.1       dsl  * Write disk label back to device after modification.
    695        1.1       dsl  */
    696        1.1       dsl int
    697       1.13       dsl writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
    698       1.13       dsl     struct cpu_disklabel *osdep)
    699        1.1       dsl {
    700        1.1       dsl 	mbr_args_t a;
    701        1.1       dsl 
    702        1.3       dsl 	memset(&a, 0, sizeof a);
    703        1.1       dsl 	a.lp = lp;
    704        1.1       dsl 	a.strat = strat;
    705        1.1       dsl 
    706        1.1       dsl 	/* get a buffer and initialize it */
    707       1.17       dsl 	a.bp = geteblk(2 * (int)lp->d_secsize);
    708        1.1       dsl 	a.bp->b_dev = dev;
    709        1.1       dsl 
    710       1.17       dsl 	/* osdep => we expect an mbr with label in netbsd ptn */
    711       1.17       dsl 	a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
    712       1.17       dsl 
    713       1.17       dsl 	/* Write/update the label to every netbsd mbr partition */
    714       1.17       dsl 	scan_mbr(&a, write_netbsd_label);
    715        1.3       dsl 
    716       1.17       dsl 	/* Old write the label at the start of the volume on disks that
    717       1.17       dsl 	 * don't have a valid mbr (always update an existing one) */
    718       1.17       dsl 	a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
    719       1.17       dsl 	validate_label(&a, 0);
    720        1.1       dsl 
    721        1.3       dsl 	if (a.written == 0 && a.error == 0)
    722        1.1       dsl 		a.error = ESRCH;
    723        1.1       dsl 
    724       1.30        ad 	brelse(a.bp, 0);
    725        1.1       dsl 	return a.error;
    726        1.1       dsl }
    727        1.1       dsl 
    728        1.1       dsl static int
    729       1.21      yamt write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    730        1.1       dsl {
    731        1.1       dsl 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    732        1.1       dsl 
    733        1.4     lukem 	if (dp->mbrp_type != MBR_PTYPE_NETBSD)
    734        1.1       dsl 		return SCAN_CONTINUE;
    735        1.1       dsl 
    736       1.17       dsl 	return validate_label(a, ptn_base);
    737        1.1       dsl }
    738