Home | History | Annotate | Line # | Download | only in sun
disksubr.c revision 1.5
      1  1.5  christos /*	$NetBSD: disksubr.c,v 1.5 2005/05/30 22:17:11 christos Exp $ */
      2  1.1    bouyer 
      3  1.4     perry /*
      4  1.1    bouyer  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      5  1.1    bouyer  * All rights reserved.
      6  1.1    bouyer  *
      7  1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8  1.1    bouyer  * modification, are permitted provided that the following conditions
      9  1.1    bouyer  * are met:
     10  1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11  1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12  1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.1    bouyer  * 3. Neither the name of the University nor the names of its contributors
     16  1.1    bouyer  *    may be used to endorse or promote products derived from this software
     17  1.1    bouyer  *    without specific prior written permission.
     18  1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  1.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1    bouyer  * SUCH DAMAGE.
     29  1.1    bouyer  */
     30  1.1    bouyer 
     31  1.1    bouyer /*
     32  1.1    bouyer  * Copyright (c) 1994, 1995 Gordon W. Ross
     33  1.1    bouyer  * Copyright (c) 1994 Theo de Raadt
     34  1.1    bouyer  * All rights reserved.
     35  1.1    bouyer  *
     36  1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     37  1.1    bouyer  * modification, are permitted provided that the following conditions
     38  1.1    bouyer  * are met:
     39  1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     40  1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     41  1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     43  1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     44  1.1    bouyer  *
     45  1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46  1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47  1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48  1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49  1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50  1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51  1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52  1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53  1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54  1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55  1.1    bouyer  */
     56  1.1    bouyer 
     57  1.1    bouyer #include <sys/cdefs.h>
     58  1.5  christos __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.5 2005/05/30 22:17:11 christos Exp $");
     59  1.1    bouyer 
     60  1.1    bouyer #include <sys/param.h>
     61  1.1    bouyer #include <sys/systm.h>
     62  1.1    bouyer #include <sys/buf.h>
     63  1.1    bouyer #include <sys/ioccom.h>
     64  1.1    bouyer #include <sys/device.h>
     65  1.1    bouyer #include <sys/disklabel.h>
     66  1.1    bouyer #include <sys/disk.h>
     67  1.1    bouyer #include <sys/dkbad.h>
     68  1.1    bouyer 
     69  1.1    bouyer #include <dev/sun/disklabel.h>
     70  1.1    bouyer 
     71  1.1    bouyer #if LABELSECTOR != 0
     72  1.1    bouyer #error  "Default value of LABELSECTOR no longer zero?"
     73  1.1    bouyer #endif
     74  1.1    bouyer 
     75  1.3     perry static	char *disklabel_sun_to_bsd(char *, struct disklabel *);
     76  1.3     perry static	int disklabel_bsd_to_sun(struct disklabel *, char *);
     77  1.1    bouyer 
     78  1.1    bouyer /*
     79  1.1    bouyer  * Attempt to read a disk label from a device
     80  1.1    bouyer  * using the indicated strategy routine.
     81  1.1    bouyer  * The label must be partly set up before this:
     82  1.1    bouyer  * secpercyl, secsize and anything required for a block i/o read
     83  1.1    bouyer  * operation in the driver's strategy/start routines
     84  1.1    bouyer  * must be filled in before calling us.
     85  1.1    bouyer  *
     86  1.1    bouyer  * Return buffer for use in signalling errors if requested.
     87  1.1    bouyer  *
     88  1.1    bouyer  * Returns null on success and an error string on failure.
     89  1.1    bouyer  */
     90  1.1    bouyer const char *
     91  1.1    bouyer readdisklabel(dev, strat, lp, clp)
     92  1.1    bouyer 	dev_t dev;
     93  1.3     perry 	void (*strat)(struct buf *);
     94  1.1    bouyer 	struct disklabel *lp;
     95  1.1    bouyer 	struct cpu_disklabel *clp;
     96  1.1    bouyer {
     97  1.1    bouyer 	struct buf *bp;
     98  1.1    bouyer 	struct disklabel *dlp;
     99  1.1    bouyer 	struct sun_disklabel *slp;
    100  1.1    bouyer 	int error;
    101  1.1    bouyer 
    102  1.1    bouyer 	/* minimal requirements for archtypal disk label */
    103  1.1    bouyer 	if (lp->d_secperunit == 0)
    104  1.1    bouyer 		lp->d_secperunit = 0x1fffffff;
    105  1.1    bouyer 	if (lp->d_npartitions == 0) {
    106  1.1    bouyer 		lp->d_npartitions = RAW_PART + 1;
    107  1.1    bouyer 		if (lp->d_partitions[RAW_PART].p_size == 0)
    108  1.1    bouyer 			lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
    109  1.1    bouyer 		lp->d_partitions[RAW_PART].p_offset = 0;
    110  1.1    bouyer 	}
    111  1.1    bouyer 
    112  1.1    bouyer 	/* obtain buffer to probe drive with */
    113  1.1    bouyer 	bp = geteblk((int)lp->d_secsize);
    114  1.1    bouyer 
    115  1.1    bouyer 	/* next, dig out disk label */
    116  1.1    bouyer 	bp->b_dev = dev;
    117  1.1    bouyer 	bp->b_blkno = LABELSECTOR;
    118  1.1    bouyer 	bp->b_cylinder = 0;
    119  1.1    bouyer 	bp->b_bcount = lp->d_secsize;
    120  1.1    bouyer 	bp->b_flags |= B_READ;
    121  1.1    bouyer 	(*strat)(bp);
    122  1.1    bouyer 
    123  1.1    bouyer 	/* if successful, locate disk label within block and validate */
    124  1.1    bouyer 	error = biowait(bp);
    125  1.1    bouyer 	if (error == 0) {
    126  1.1    bouyer 		/* Save the whole block in case it has info we need. */
    127  1.1    bouyer 		memcpy(clp->cd_block, bp->b_data, sizeof(clp->cd_block));
    128  1.1    bouyer 	}
    129  1.1    bouyer 	brelse(bp);
    130  1.1    bouyer 	if (error)
    131  1.1    bouyer 		return ("disk label read error");
    132  1.1    bouyer 
    133  1.1    bouyer 	/* Check for a NetBSD disk label at LABELOFFSET */
    134  1.1    bouyer 	dlp = (struct disklabel *) (clp->cd_block + LABELOFFSET);
    135  1.1    bouyer 	if (dlp->d_magic == DISKMAGIC) {
    136  1.1    bouyer 		if (dkcksum(dlp))
    137  1.1    bouyer 			return ("NetBSD disk label corrupted");
    138  1.1    bouyer 		*lp = *dlp;
    139  1.1    bouyer 		return (NULL);
    140  1.1    bouyer 	}
    141  1.1    bouyer 
    142  1.1    bouyer 	/* Check for a Sun disk label (for PROM compatibility). */
    143  1.1    bouyer 	slp = (struct sun_disklabel *) clp->cd_block;
    144  1.1    bouyer 	if (slp->sl_magic == SUN_DKMAGIC)
    145  1.1    bouyer 		return (disklabel_sun_to_bsd(clp->cd_block, lp));
    146  1.1    bouyer 
    147  1.1    bouyer 	/*
    148  1.1    bouyer 	 * Check for a NetBSD disk label somewhere in LABELSECTOR
    149  1.1    bouyer 	 * (compat with others big-endian boxes)
    150  1.1    bouyer 	 */
    151  1.1    bouyer 	for (dlp = (struct disklabel *)clp->cd_block;
    152  1.1    bouyer 	    dlp <= (struct disklabel *)((char *)clp->cd_block +
    153  1.1    bouyer 	    DEV_BSIZE - sizeof(*dlp));
    154  1.1    bouyer 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
    155  1.1    bouyer 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
    156  1.1    bouyer 			continue;
    157  1.1    bouyer 		}
    158  1.1    bouyer 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0)
    159  1.1    bouyer 			return("NetBSD disk label corrupted");
    160  1.1    bouyer 		else {
    161  1.1    bouyer 			*lp = *dlp;
    162  1.1    bouyer 			return(NULL);
    163  1.1    bouyer 		}
    164  1.1    bouyer 	}
    165  1.1    bouyer 
    166  1.1    bouyer 	memset(clp->cd_block, 0, sizeof(clp->cd_block));
    167  1.1    bouyer 	return ("no disk label");
    168  1.1    bouyer }
    169  1.1    bouyer 
    170  1.1    bouyer /*
    171  1.1    bouyer  * Check new disk label for sensibility
    172  1.1    bouyer  * before setting it.
    173  1.1    bouyer  */
    174  1.1    bouyer int
    175  1.1    bouyer setdisklabel(olp, nlp, openmask, clp)
    176  1.1    bouyer 	struct disklabel *olp, *nlp;
    177  1.1    bouyer 	u_long openmask;
    178  1.1    bouyer 	struct cpu_disklabel *clp;
    179  1.1    bouyer {
    180  1.1    bouyer 	int i;
    181  1.1    bouyer 	struct partition *opp, *npp;
    182  1.1    bouyer 
    183  1.1    bouyer 	/* sanity clause */
    184  1.1    bouyer 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0 ||
    185  1.1    bouyer 	    (nlp->d_secsize % DEV_BSIZE) != 0)
    186  1.1    bouyer 		return(EINVAL);
    187  1.1    bouyer 
    188  1.1    bouyer 	/* special case to allow disklabel to be invalidated */
    189  1.1    bouyer 	if (nlp->d_magic == 0xffffffff) {
    190  1.1    bouyer 		*olp = *nlp;
    191  1.1    bouyer 		return (0);
    192  1.1    bouyer 	}
    193  1.1    bouyer 
    194  1.1    bouyer 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    195  1.1    bouyer 	    dkcksum(nlp) != 0)
    196  1.1    bouyer 		return (EINVAL);
    197  1.1    bouyer 
    198  1.1    bouyer 	while ((i = ffs(openmask)) != 0) {
    199  1.1    bouyer 		i--;
    200  1.1    bouyer 		openmask &= ~(1 << i);
    201  1.1    bouyer 		if (nlp->d_npartitions <= i)
    202  1.1    bouyer 			return (EBUSY);
    203  1.1    bouyer 		opp = &olp->d_partitions[i];
    204  1.1    bouyer 		npp = &nlp->d_partitions[i];
    205  1.1    bouyer 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    206  1.1    bouyer 			return (EBUSY);
    207  1.1    bouyer 	}
    208  1.1    bouyer 
    209  1.1    bouyer 	*olp = *nlp;
    210  1.1    bouyer 	return (0);
    211  1.1    bouyer }
    212  1.1    bouyer 
    213  1.1    bouyer /*
    214  1.1    bouyer  * Write disk label back to device after modification.
    215  1.1    bouyer  * Current label is already in clp->cd_block[]
    216  1.1    bouyer  */
    217  1.1    bouyer int
    218  1.1    bouyer writedisklabel(dev, strat, lp, clp)
    219  1.1    bouyer 	dev_t dev;
    220  1.3     perry 	void (*strat)(struct buf *);
    221  1.1    bouyer 	struct disklabel *lp;
    222  1.1    bouyer 	struct cpu_disklabel *clp;
    223  1.1    bouyer {
    224  1.1    bouyer 	struct buf *bp;
    225  1.1    bouyer 	int error;
    226  1.1    bouyer 	struct disklabel *dlp;
    227  1.1    bouyer 	struct sun_disklabel *slp;
    228  1.1    bouyer 
    229  1.1    bouyer 	/*
    230  1.1    bouyer 	 * Embed native label in a piece of wasteland.
    231  1.1    bouyer 	 */
    232  1.1    bouyer 	if (sizeof(struct disklabel) > sizeof slp->sl_bsdlabel)
    233  1.1    bouyer 		return EFBIG;
    234  1.1    bouyer 
    235  1.1    bouyer 	slp = (struct sun_disklabel *)clp->cd_block;
    236  1.1    bouyer 	memset(slp->sl_bsdlabel, 0, sizeof(slp->sl_bsdlabel));
    237  1.1    bouyer 	dlp = (struct disklabel *)slp->sl_bsdlabel;
    238  1.1    bouyer 	*dlp = *lp;
    239  1.1    bouyer 
    240  1.1    bouyer 	/* Build a SunOS compatible label around the native label */
    241  1.1    bouyer 	error = disklabel_bsd_to_sun(lp, clp->cd_block);
    242  1.1    bouyer 	if (error)
    243  1.1    bouyer 		return (error);
    244  1.1    bouyer 
    245  1.1    bouyer 	/* Get a buffer and copy the new label into it. */
    246  1.1    bouyer 	bp = geteblk((int)lp->d_secsize);
    247  1.1    bouyer 	memcpy(bp->b_data, clp->cd_block, sizeof(clp->cd_block));
    248  1.1    bouyer 
    249  1.1    bouyer 	/* Write out the updated label. */
    250  1.1    bouyer 	bp->b_dev = dev;
    251  1.1    bouyer 	bp->b_blkno = LABELSECTOR;
    252  1.1    bouyer 	bp->b_cylinder = 0;
    253  1.1    bouyer 	bp->b_bcount = lp->d_secsize;
    254  1.1    bouyer 	bp->b_flags |= B_WRITE;
    255  1.1    bouyer 	(*strat)(bp);
    256  1.1    bouyer 	error = biowait(bp);
    257  1.1    bouyer 	brelse(bp);
    258  1.1    bouyer 
    259  1.1    bouyer 	return (error);
    260  1.1    bouyer }
    261  1.1    bouyer 
    262  1.1    bouyer /*
    263  1.1    bouyer  * Determine the size of the transfer, and make sure it is
    264  1.1    bouyer  * within the boundaries of the partition. Adjust transfer
    265  1.1    bouyer  * if needed, and signal errors or early completion.
    266  1.1    bouyer  */
    267  1.1    bouyer int
    268  1.1    bouyer bounds_check_with_label(dk, bp, wlabel)
    269  1.1    bouyer 	struct disk *dk;
    270  1.1    bouyer 	struct buf *bp;
    271  1.1    bouyer 	int wlabel;
    272  1.1    bouyer {
    273  1.1    bouyer 	struct disklabel *lp = dk->dk_label;
    274  1.1    bouyer 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    275  1.1    bouyer 	int maxsz = p->p_size;
    276  1.1    bouyer 	int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
    277  1.1    bouyer 
    278  1.1    bouyer 	/*
    279  1.1    bouyer 	 * overwriting disk label ?
    280  1.1    bouyer 	 * The label is always in sector LABELSECTOR.
    281  1.1    bouyer 	 * XXX should also protect bootstrap in first 8K
    282  1.1    bouyer 	 */
    283  1.1    bouyer 	if (bp->b_blkno + p->p_offset <= LABELSECTOR &&
    284  1.1    bouyer 	    (bp->b_flags & B_READ) == 0 && wlabel == 0) {
    285  1.1    bouyer 		bp->b_error = EROFS;
    286  1.1    bouyer 		goto bad;
    287  1.1    bouyer 	}
    288  1.1    bouyer 
    289  1.1    bouyer 	/* beyond partition? */
    290  1.1    bouyer 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
    291  1.1    bouyer 		/* if exactly at end of disk, return an EOF */
    292  1.1    bouyer 		if (bp->b_blkno == maxsz) {
    293  1.1    bouyer 			bp->b_resid = bp->b_bcount;
    294  1.1    bouyer 			return(0);
    295  1.1    bouyer 		}
    296  1.1    bouyer 		/* or truncate if part of it fits */
    297  1.1    bouyer 		sz = maxsz - bp->b_blkno;
    298  1.1    bouyer 		if (sz <= 0) {
    299  1.1    bouyer 			bp->b_error = EINVAL;
    300  1.1    bouyer 			goto bad;
    301  1.1    bouyer 		}
    302  1.1    bouyer 		bp->b_bcount = sz << DEV_BSHIFT;
    303  1.1    bouyer 	}
    304  1.1    bouyer 
    305  1.1    bouyer 	/* calculate cylinder for disksort to order transfers with */
    306  1.1    bouyer 	bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
    307  1.1    bouyer 	return(1);
    308  1.1    bouyer bad:
    309  1.1    bouyer 	bp->b_flags |= B_ERROR;
    310  1.1    bouyer 	return(-1);
    311  1.1    bouyer }
    312  1.1    bouyer 
    313  1.1    bouyer /************************************************************************
    314  1.1    bouyer  *
    315  1.1    bouyer  * The rest of this was taken from arch/sparc/scsi/sun_disklabel.c
    316  1.1    bouyer  * and then substantially rewritten by Gordon W. Ross
    317  1.1    bouyer  *
    318  1.1    bouyer  ************************************************************************/
    319  1.1    bouyer 
    320  1.1    bouyer /* What partition types to assume for Sun disklabels: */
    321  1.1    bouyer static u_char
    322  1.1    bouyer sun_fstypes[8] = {
    323  1.1    bouyer 	FS_BSDFFS,	/* a */
    324  1.1    bouyer 	FS_SWAP,	/* b */
    325  1.1    bouyer 	FS_OTHER,	/* c - whole disk */
    326  1.1    bouyer 	FS_BSDFFS,	/* d */
    327  1.1    bouyer 	FS_BSDFFS,	/* e */
    328  1.1    bouyer 	FS_BSDFFS,	/* f */
    329  1.1    bouyer 	FS_BSDFFS,	/* g */
    330  1.1    bouyer 	FS_BSDFFS,	/* h */
    331  1.1    bouyer };
    332  1.1    bouyer 
    333  1.1    bouyer /*
    334  1.1    bouyer  * Given a SunOS disk label, set lp to a BSD disk label.
    335  1.1    bouyer  * Returns NULL on success, else an error string.
    336  1.1    bouyer  *
    337  1.1    bouyer  * The BSD label is cleared out before this is called.
    338  1.1    bouyer  */
    339  1.5  christos static const char *
    340  1.1    bouyer disklabel_sun_to_bsd(cp, lp)
    341  1.1    bouyer 	char *cp;
    342  1.1    bouyer 	struct disklabel *lp;
    343  1.1    bouyer {
    344  1.1    bouyer 	struct sun_disklabel *sl;
    345  1.1    bouyer 	struct partition *npp;
    346  1.1    bouyer 	struct sun_dkpart *spp;
    347  1.1    bouyer 	int i, secpercyl;
    348  1.1    bouyer 	u_short cksum, *sp1, *sp2;
    349  1.1    bouyer 
    350  1.1    bouyer 	sl = (struct sun_disklabel *)cp;
    351  1.1    bouyer 
    352  1.1    bouyer 	/* Verify the XOR check. */
    353  1.1    bouyer 	sp1 = (u_short *)sl;
    354  1.1    bouyer 	sp2 = (u_short *)(sl + 1);
    355  1.1    bouyer 	cksum = 0;
    356  1.1    bouyer 	while (sp1 < sp2)
    357  1.1    bouyer 		cksum ^= *sp1++;
    358  1.1    bouyer 	if (cksum != 0)
    359  1.1    bouyer 		return("SunOS disk label, bad checksum");
    360  1.1    bouyer 
    361  1.1    bouyer 	/* Format conversion. */
    362  1.1    bouyer 	lp->d_magic = DISKMAGIC;
    363  1.1    bouyer 	lp->d_magic2 = DISKMAGIC;
    364  1.1    bouyer 	memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
    365  1.1    bouyer 
    366  1.1    bouyer 	lp->d_secsize = 512;
    367  1.1    bouyer 	lp->d_nsectors   = sl->sl_nsectors;
    368  1.1    bouyer 	lp->d_ntracks    = sl->sl_ntracks;
    369  1.1    bouyer 	lp->d_ncylinders = sl->sl_ncylinders;
    370  1.1    bouyer 
    371  1.1    bouyer 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
    372  1.1    bouyer 	lp->d_secpercyl  = secpercyl;
    373  1.1    bouyer 	lp->d_secperunit = secpercyl * sl->sl_ncylinders;
    374  1.1    bouyer 
    375  1.1    bouyer 	lp->d_sparespercyl = sl->sl_sparespercyl;
    376  1.1    bouyer 	lp->d_acylinders   = sl->sl_acylinders;
    377  1.1    bouyer 	lp->d_rpm          = sl->sl_rpm;
    378  1.1    bouyer 	lp->d_interleave   = sl->sl_interleave;
    379  1.1    bouyer 
    380  1.1    bouyer 	lp->d_npartitions = 8;
    381  1.1    bouyer 	/* These are as defined in <ufs/ffs/fs.h> */
    382  1.1    bouyer 	lp->d_bbsize = 8192;	/* XXX */
    383  1.1    bouyer 	lp->d_sbsize = 8192;	/* XXX */
    384  1.1    bouyer 
    385  1.1    bouyer 	for (i = 0; i < 8; i++) {
    386  1.1    bouyer 		spp = &sl->sl_part[i];
    387  1.1    bouyer 		npp = &lp->d_partitions[i];
    388  1.1    bouyer 		npp->p_offset = spp->sdkp_cyloffset * secpercyl;
    389  1.1    bouyer 		npp->p_size = spp->sdkp_nsectors;
    390  1.1    bouyer 		if (npp->p_size == 0) {
    391  1.1    bouyer 			npp->p_fstype = FS_UNUSED;
    392  1.1    bouyer 		} else {
    393  1.1    bouyer 			npp->p_fstype = sun_fstypes[i];
    394  1.1    bouyer 			if (npp->p_fstype == FS_BSDFFS) {
    395  1.1    bouyer 				/*
    396  1.1    bouyer 				 * The sun label does not store the FFS fields,
    397  1.1    bouyer 				 * so just set them with default values here.
    398  1.1    bouyer 				 */
    399  1.1    bouyer 				npp->p_fsize = 1024;
    400  1.1    bouyer 				npp->p_frag = 8;
    401  1.1    bouyer 				npp->p_cpg = 16;
    402  1.1    bouyer 			}
    403  1.1    bouyer 		}
    404  1.1    bouyer 	}
    405  1.1    bouyer 
    406  1.1    bouyer 	lp->d_checksum = 0;
    407  1.1    bouyer 	lp->d_checksum = dkcksum(lp);
    408  1.1    bouyer 	return (NULL);
    409  1.1    bouyer }
    410  1.1    bouyer 
    411  1.1    bouyer /*
    412  1.1    bouyer  * Given a BSD disk label, update the Sun disklabel
    413  1.1    bouyer  * pointed to by cp with the new info.  Note that the
    414  1.1    bouyer  * Sun disklabel may have other info we need to keep.
    415  1.1    bouyer  * Returns zero or error code.
    416  1.1    bouyer  */
    417  1.1    bouyer static int
    418  1.1    bouyer disklabel_bsd_to_sun(lp, cp)
    419  1.1    bouyer 	struct disklabel *lp;
    420  1.1    bouyer 	char *cp;
    421  1.1    bouyer {
    422  1.1    bouyer 	struct sun_disklabel *sl;
    423  1.1    bouyer 	struct partition *npp;
    424  1.1    bouyer 	struct sun_dkpart *spp;
    425  1.1    bouyer 	int i, secpercyl;
    426  1.1    bouyer 	u_short cksum, *sp1, *sp2;
    427  1.1    bouyer 
    428  1.1    bouyer 	if (lp->d_secsize != 512)
    429  1.1    bouyer 		return (EINVAL);
    430  1.1    bouyer 
    431  1.1    bouyer 	sl = (struct sun_disklabel *)cp;
    432  1.1    bouyer 
    433  1.1    bouyer 	/*
    434  1.1    bouyer 	 * Format conversion.
    435  1.1    bouyer 	 */
    436  1.1    bouyer 	memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
    437  1.1    bouyer 	sl->sl_rpm = lp->d_rpm;
    438  1.1    bouyer 	sl->sl_pcylinders   = lp->d_ncylinders + lp->d_acylinders; /* XXX */
    439  1.1    bouyer 	sl->sl_sparespercyl = lp->d_sparespercyl;
    440  1.1    bouyer 	sl->sl_interleave   = lp->d_interleave;
    441  1.1    bouyer 	sl->sl_ncylinders   = lp->d_ncylinders;
    442  1.1    bouyer 	sl->sl_acylinders   = lp->d_acylinders;
    443  1.1    bouyer 	sl->sl_ntracks      = lp->d_ntracks;
    444  1.1    bouyer 	sl->sl_nsectors     = lp->d_nsectors;
    445  1.1    bouyer 
    446  1.1    bouyer 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
    447  1.1    bouyer 	for (i = 0; i < 8; i++) {
    448  1.1    bouyer 		spp = &sl->sl_part[i];
    449  1.1    bouyer 		npp = &lp->d_partitions[i];
    450  1.1    bouyer 
    451  1.1    bouyer 		/*
    452  1.1    bouyer 		 * SunOS partitions must start on a cylinder boundary.
    453  1.1    bouyer 		 * Note this restriction is forced upon NetBSD/sparc
    454  1.1    bouyer 		 * labels too, since we want to keep both labels
    455  1.1    bouyer 		 * synchronised.
    456  1.1    bouyer 		 */
    457  1.1    bouyer 		if (npp->p_offset % secpercyl)
    458  1.1    bouyer 			return (EINVAL);
    459  1.1    bouyer 		spp->sdkp_cyloffset = npp->p_offset / secpercyl;
    460  1.1    bouyer 		spp->sdkp_nsectors = npp->p_size;
    461  1.1    bouyer 	}
    462  1.1    bouyer 	sl->sl_magic = SUN_DKMAGIC;
    463  1.1    bouyer 
    464  1.1    bouyer 	/* Compute the XOR check. */
    465  1.1    bouyer 	sp1 = (u_short *)sl;
    466  1.1    bouyer 	sp2 = (u_short *)(sl + 1);
    467  1.1    bouyer 	sl->sl_cksum = cksum = 0;
    468  1.1    bouyer 	while (sp1 < sp2)
    469  1.1    bouyer 		cksum ^= *sp1++;
    470  1.1    bouyer 	sl->sl_cksum = cksum;
    471  1.1    bouyer 
    472  1.1    bouyer 	return (0);
    473  1.1    bouyer }
    474  1.1    bouyer 
    475  1.1    bouyer /*
    476  1.1    bouyer  * Search the bad sector table looking for the specified sector.
    477  1.1    bouyer  * Return index if found.
    478  1.1    bouyer  * Return -1 if not found.
    479  1.1    bouyer  */
    480  1.1    bouyer int
    481  1.1    bouyer isbad(bt, cyl, trk, sec)
    482  1.1    bouyer 	struct dkbad *bt;
    483  1.1    bouyer 	int cyl, trk, sec;
    484  1.1    bouyer {
    485  1.1    bouyer 	int i;
    486  1.1    bouyer 	long blk, bblk;
    487  1.1    bouyer 
    488  1.1    bouyer 	blk = ((long)cyl << 16) + (trk << 8) + sec;
    489  1.1    bouyer 	for (i = 0; i < 126; i++) {
    490  1.1    bouyer 		bblk = ((long)bt->bt_bad[i].bt_cyl << 16) +
    491  1.1    bouyer 			bt->bt_bad[i].bt_trksec;
    492  1.1    bouyer 		if (blk == bblk)
    493  1.1    bouyer 			return (i);
    494  1.1    bouyer 		if (blk < bblk || bblk < 0)
    495  1.1    bouyer 			break;
    496  1.1    bouyer 	}
    497  1.1    bouyer 	return (-1);
    498  1.1    bouyer }
    499