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