-/* $OpenBSD: disksubr.c,v 1.5 1996/09/27 19:14:08 pefo Exp $ */
+/* $OpenBSD: disksubr.c,v 1.6 1996/12/09 15:54:58 deraadt Exp $ */
+/* $NetBSD: disksubr.c,v 1.21 1996/05/03 19:42:03 christos Exp $ */
/*
- * Copyright (c) 1994, 1995 Carnegie-Mellon University.
+ * Copyright (c) 1996 Theo de Raadt
+ * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
* All rights reserved.
*
- * Authors: Keith Bostic, Chris G. Demetriou, Per Fogelstrom (R4000)
- *
- * Permission to use, copy, modify and distribute this software and
- * its documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
- * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
- * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * Carnegie Mellon requests users of this software to return to
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
*
- * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
- * School of Computer Science
- * Carnegie Mellon University
- * Pittsburgh PA 15213-3890
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
*
- * any improvements or extensions that they make and grant Carnegie the
- * rights to redistribute these changes.
+ * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
*/
#include <sys/param.h>
+#include <sys/systm.h>
#include <sys/buf.h>
-#include <sys/ioccom.h>
#include <sys/device.h>
#include <sys/disklabel.h>
+#include <sys/syslog.h>
#include <sys/disk.h>
-#include <scsi/scsi_all.h>
-#include <scsi/scsiconf.h>
+#define b_cylin b_resid
-#include <machine/cpu.h>
-#include <machine/autoconf.h>
+int fat_types[] = { DOSPTYP_FAT12, DOSPTYP_FAT16S,
+ DOSPTYP_FAT16B, DOSPTYP_FAT16C, -1 };
-extern struct device *bootdv;
-
-/* was this the boot device ? */
void
dk_establish(dk, dev)
struct disk *dk;
}
/*
- * Attempt to read a disk label from a device * using the indicated stategy routine.
+ * Attempt to read a disk label from a device
+ * using the indicated stategy routine.
* The label must be partly set up before this:
- * secpercyl and anything required in the strategy routine
- * (e.g., sector size) must be filled in before calling us.
+ * secpercyl, secsize and anything required for a block i/o read
+ * operation in the driver's strategy/start routines
+ * must be filled in before calling us.
+ *
+ * If dos partition table requested, attempt to load it and
+ * find disklabel inside a DOS partition. Also, if bad block
+ * table needed, attempt to extract it as well. Return buffer
+ * for use in signalling errors if requested.
+ *
* Returns null on success and an error string on failure.
*/
char *
-readdisklabel(dev, strat, lp, clp)
+readdisklabel(dev, strat, lp, osdep)
dev_t dev;
- void (*strat)();
- struct disklabel *lp;
- struct cpu_disklabel *clp;
+ void (*strat) __P((struct buf *));
+ register struct disklabel *lp;
+ struct cpu_disklabel *osdep;
{
+ struct dos_partition *dp = osdep->dosparts, *dp2;
+ struct dkbad *bdp = &osdep->bad;
struct buf *bp;
struct disklabel *dlp;
- struct dos_partition *dp = clp->dosparts;
char *msg = NULL;
- int dospart, dospartoff, i;
+ int dospartoff, cyl, i, *ip, ourpart = -1;
/* minimal requirements for archtypal disk label */
+ if (lp->d_secsize == 0)
+ lp->d_secsize = DEV_BSIZE;
if (lp->d_secperunit == 0)
- lp->d_secperunit = 0x1fffffff;
+ lp->d_secperunit = 0x1fffffff;
lp->d_npartitions = RAW_PART + 1;
- for(i = 0; i < RAW_PART; i++) {
+ for (i = 0; i < RAW_PART; i++) {
lp->d_partitions[i].p_size = 0;
lp->d_partitions[i].p_offset = 0;
}
- if (lp->d_partitions[RAW_PART].p_size == 0)
- lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
- lp->d_partitions[RAW_PART].p_offset = 0;
+ if (lp->d_partitions[i].p_size == 0)
+ lp->d_partitions[i].p_size = 0x1fffffff;
+ lp->d_partitions[i].p_offset = 0;
- /* obtain buffer to probe drive with */
+ /* get a buffer and initialize it */
bp = geteblk((int)lp->d_secsize);
bp->b_dev = dev;
- dospartoff = 0;
- dospart = -1;
/* do dos partitions in the process of getting disklabel? */
+ dospartoff = 0;
+ cyl = LABELSECTOR / lp->d_secpercyl;
if (dp) {
/* read master boot record */
bp->b_blkno = DOSBBSECTOR;
bp->b_bcount = lp->d_secsize;
bp->b_flags = B_BUSY | B_READ;
- bp->b_resid = 0;
+ bp->b_cylin = DOSBBSECTOR / lp->d_secpercyl;
(*strat)(bp);
/* if successful, wander through dos partition table */
msg = "dos partition I/O error";
goto done;
}
- if (*(unsigned int *)(bp->b_data) == 0x8efac033) {
- /* XXX how do we check veracity/bounds of this? */
- bcopy(bp->b_data + DOSPARTOFF, dp, NDOSPART * sizeof(*dp));
-
- for (i = 0, dp = clp->dosparts; i < NDOSPART; i++, dp++) {
- if (dp->dp_size && dp->dp_typ == DOSPTYP_OPENBSD
- && dospart < 0)
- dospart = i;
- }
- for (i = 0, dp = clp->dosparts; i < NDOSPART; i++, dp++) {
- if (dp->dp_size && dp->dp_typ == DOSPTYP_386BSD
- && dospart < 0)
- dospart = i;
- }
- if(dospart >= 0) {
- /*
- * set part a to show OpenBSD part
- */
- dp = clp->dosparts+dospart;
- dospartoff = dp->dp_start;
-
- lp->d_partitions[0].p_size = dp->dp_size;
- lp->d_partitions[0].p_offset = dp->dp_start;
- lp->d_partitions[RAW_PART].p_size = dp->dp_size;
- lp->d_partitions[RAW_PART].p_offset = dp->dp_start;
- lp->d_ntracks = dp->dp_ehd + 1;
- lp->d_nsectors = DPSECT(dp->dp_esect);
- lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
+
+ /* XXX how do we check veracity/bounds of this? */
+ bcopy(bp->b_data + DOSPARTOFF, dp, NDOSPART * sizeof(*dp));
+
+ /*
+ * Search for our MBR partition
+ */
+ for (dp2=dp, i=0; i < NDOSPART && ourpart == -1; i++, dp2++)
+ if (dp2->dp_size && dp2->dp_typ == DOSPTYP_OPENBSD)
+ ourpart = i;
+ for (dp2=dp, i=0; i < NDOSPART && ourpart == -1; i++, dp2++)
+ if (dp2->dp_size && dp2->dp_typ == DOSPTYP_386BSD)
+ ourpart = i;
+
+ if (ourpart != -1) {
+ dp2 = &dp[ourpart];
+
+ /*
+ * This is our MBR partition. need sector address
+ * for SCSI/IDE, cylinder for ESDI/ST506/RLL
+ */
+ dospartoff = dp2->dp_start;
+ cyl = DPCYL(dp2->dp_scyl, dp2->dp_ssect);
+
+ /* XXX build a temporary disklabel */
+ lp->d_partitions[0].p_size = dp2->dp_size;
+ lp->d_partitions[0].p_offset = dp2->dp_start;
+ if (lp->d_ntracks == 0)
+ lp->d_ntracks = dp2->dp_ehd + 1;
+ if (lp->d_nsectors == 0)
+ lp->d_nsectors = DPSECT(dp2->dp_esect);
+ if (lp->d_secpercyl == 0)
+ lp->d_secpercyl = lp->d_ntracks *
+ lp->d_nsectors;
+ }
+
+ /*
+ * In case the disklabel read below fails, we want to provide
+ * a fake label in which m/n/o/p are MBR partitions 0/1/2/3
+ */
+ for (dp2=dp, i=0; i < NDOSPART; i++, dp2++) {
+ lp->d_partitions[12+i].p_size = dp2->dp_size;
+ lp->d_partitions[12+i].p_offset = dp2->dp_start;
+ for (ip = fat_types; *ip != -1; ip++) {
+ if (dp2->dp_typ != *ip)
+ continue;
+ lp->d_partitions[12+i].p_fstype =
+ FS_MSDOS;
}
}
-
+ lp->d_bbsize = 8192;
+ lp->d_sbsize = 64*1024; /* XXX ? */
+ lp->d_npartitions = MAXPARTITIONS;
}
+
/* next, dig out disk label */
bp->b_blkno = dospartoff + LABELSECTOR;
- bp->b_resid = 0;
+ bp->b_cylin = cyl;
bp->b_bcount = lp->d_secsize;
bp->b_flags = B_BUSY | B_READ;
- (*strat)(bp);
+ (*strat)(bp);
/* if successful, locate disk label within block and validate */
if (biowait(bp)) {
- msg = "disk label read error";
+ /* XXX we return the faked label built so far */
+ msg = "disk label I/O error";
goto done;
}
-
- dlp = (struct disklabel *)(bp->b_un.b_addr + LABELOFFSET);
- if (dlp->d_magic == DISKMAGIC) {
- if (dkcksum(dlp)) {
- msg = "OpenBSD disk label corrupted";
- goto done;
+ for (dlp = (struct disklabel *)bp->b_data;
+ dlp <= (struct disklabel *)(bp->b_data + lp->d_secsize - sizeof(*dlp));
+ dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
+ if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
+ if (msg == NULL)
+ msg = "no disk label";
+ } else if (dlp->d_npartitions > MAXPARTITIONS ||
+ dkcksum(dlp) != 0)
+ msg = "disk label corrupted";
+ else {
+ *lp = *dlp;
+ msg = NULL;
+ break;
}
- *lp = *dlp;
+ }
+
+ if (msg) {
+#if defined(CD9660)
+ if (iso_disklabelspoof(dev, strat, lp) == 0)
+ msg = NULL;
+#endif
goto done;
}
- msg = "no disk label";
+
+ /* obtain bad sector table if requested and present */
+ if (bdp && (lp->d_flags & D_BADSECT)) {
+ struct dkbad *db;
+
+ i = 0;
+ do {
+ /* read a bad sector table */
+ bp->b_flags = B_BUSY | B_READ;
+ bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
+ if (lp->d_secsize > DEV_BSIZE)
+ bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
+ else
+ bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
+ bp->b_bcount = lp->d_secsize;
+ bp->b_cylin = lp->d_ncylinders - 1;
+ (*strat)(bp);
+
+ /* if successful, validate, otherwise try another */
+ if (biowait(bp)) {
+ msg = "bad sector table I/O error";
+ } else {
+ db = (struct dkbad *)(bp->b_data);
+#define DKBAD_MAGIC 0x4321
+ if (db->bt_mbz == 0
+ && db->bt_flag == DKBAD_MAGIC) {
+ msg = NULL;
+ *bdp = *db;
+ break;
+ } else
+ msg = "bad sector table corrupted";
+ }
+ } while ((bp->b_flags & B_ERROR) && (i += 2) < 10 &&
+ i < lp->d_nsectors);
+ }
+
done:
- bp->b_flags = B_INVAL | B_AGE | B_READ;
+ bp->b_flags |= B_INVAL;
brelse(bp);
return (msg);
}
/*
- * Check new disk label for sensibility before setting it.
+ * Check new disk label for sensibility
+ * before setting it.
*/
int
-setdisklabel(olp, nlp, openmask, clp)
+setdisklabel(olp, nlp, openmask, osdep)
register struct disklabel *olp, *nlp;
u_long openmask;
- struct cpu_disklabel *clp;
+ struct cpu_disklabel *osdep;
{
register i;
register struct partition *opp, *npp;
(nlp->d_secsize % DEV_BSIZE) != 0)
return(EINVAL);
+ /* special case to allow disklabel to be invalidated */
+ if (nlp->d_magic == 0xffffffff) {
+ *olp = *nlp;
+ return (0);
+ }
+
if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
- dkcksum(nlp) != 0)
+ dkcksum(nlp) != 0)
return (EINVAL);
- while ((i = ffs((long)openmask)) != 0) {
- i--;
+ /* XXX missing check if other dos partitions will be overwritten */
+
+ while (openmask != 0) {
+ i = ffs(openmask) - 1;
openmask &= ~(1 << i);
if (nlp->d_npartitions <= i)
return (EBUSY);
if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
return (EBUSY);
/*
- * Copy internally-set partition information
- * if new label doesn't include it. XXX
- */
+ * Copy internally-set partition information
+ * if new label doesn't include it. XXX
+ */
if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
npp->p_fstype = opp->p_fstype;
npp->p_fsize = opp->p_fsize;
npp->p_cpg = opp->p_cpg;
}
}
- nlp->d_checksum = 0;
- nlp->d_checksum = dkcksum(nlp);
+ nlp->d_checksum = 0;
+ nlp->d_checksum = dkcksum(nlp);
*olp = *nlp;
- return (0);
+ return (0);
}
+
/*
* Write disk label back to device after modification.
- * this means write out the Rigid disk blocks to represent the
- * label. Hope the user was carefull.
*/
int
-writedisklabel(dev, strat, lp, clp)
+writedisklabel(dev, strat, lp, osdep)
dev_t dev;
- void (*strat)();
+ void (*strat) __P((struct buf *));
register struct disklabel *lp;
- struct cpu_disklabel *clp;
+ struct cpu_disklabel *osdep;
{
- struct buf *bp;
+ struct dos_partition *dp = osdep->dosparts, *dp2;
+ struct buf *bp;
struct disklabel *dlp;
- struct dos_partition *dp = clp->dosparts;
- int error = 0, i;
- int dospart, dospartoff;
+ int error, dospartoff, cyl, i;
+ int ourpart = -1;
+ /* get a buffer and initialize it */
bp = geteblk((int)lp->d_secsize);
bp->b_dev = dev;
- dospart = -1;
- dospartoff = 0;
/* do dos partitions in the process of getting disklabel? */
+ dospartoff = 0;
+ cyl = LABELSECTOR / lp->d_secpercyl;
if (dp) {
/* read master boot record */
bp->b_blkno = DOSBBSECTOR;
bp->b_bcount = lp->d_secsize;
bp->b_flags = B_BUSY | B_READ;
- bp->b_resid = 0;
+ bp->b_cylin = DOSBBSECTOR / lp->d_secpercyl;
(*strat)(bp);
- if (((error = biowait(bp)) == 0)
- && *(unsigned int *)(bp->b_data) == 0x8efac033) {
- /* XXX how do we check veracity/bounds of this? */
- bcopy(bp->b_data + DOSPARTOFF, dp, NDOSPART * sizeof(*dp));
-
- for (i = 0, dp = clp->dosparts; i < NDOSPART; i++, dp++) {
- if (dp->dp_size && dp->dp_typ == DOSPTYP_OPENBSD
- && dospart < 0) {
- dospart = i;
- dospartoff = dp->dp_start;
- }
- }
- for (i = 0, dp = clp->dosparts; i < NDOSPART; i++, dp++) {
- if (dp->dp_size && dp->dp_typ == DOSPTYP_386BSD
- && dospart < 0) {
- dospart = i;
- dospartoff = dp->dp_start;
- }
- }
+ if ((error = biowait(bp)) != 0)
+ goto done;
+
+ /* XXX how do we check veracity/bounds of this? */
+ bcopy(bp->b_data + DOSPARTOFF, dp,
+ NDOSPART * sizeof(*dp));
+
+ for (dp2=dp, i=0; i < NDOSPART && ourpart == -1; i++, dp2++)
+ if (dp2->dp_size && dp2->dp_typ == DOSPTYP_OPENBSD)
+ ourpart = i;
+ for (dp2=dp, i=0; i < NDOSPART && ourpart == -1; i++, dp2++)
+ if (dp2->dp_size && dp2->dp_typ == DOSPTYP_386BSD)
+ ourpart = i;
+
+ if (ourpart != -1) {
+ dp2 = &dp[ourpart];
+
+ /*
+ * need sector address for SCSI/IDE,
+ * cylinder for ESDI/ST506/RLL
+ */
+ dospartoff = dp2->dp_start;
+ cyl = DPCYL(dp2->dp_scyl, dp2->dp_ssect);
}
-
}
+
+ /* next, dig out disk label */
bp->b_blkno = dospartoff + LABELSECTOR;
- bp->b_resid = 0;
+ bp->b_cylin = cyl;
bp->b_bcount = lp->d_secsize;
- bp->b_flags = B_READ; /* get current label */
+ bp->b_flags = B_BUSY | B_READ;
(*strat)(bp);
- if (error = biowait(bp))
- goto done;
- dlp = (struct disklabel *)(bp->b_un.b_addr + LABELOFFSET);
- *dlp = *lp; /* struct assignment */
-
- bp->b_flags = B_WRITE;
- (*strat)(bp);
- error = biowait(bp);
+ /* if successful, locate disk label within block and validate */
+ if ((error = biowait(bp)) != 0)
+ goto done;
+ for (dlp = (struct disklabel *)bp->b_data;
+ dlp <= (struct disklabel *)(bp->b_data + lp->d_secsize - sizeof(*dlp));
+ dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
+ if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
+ dkcksum(dlp) == 0) {
+ *dlp = *lp;
+ bp->b_flags = B_BUSY | B_WRITE;
+ (*strat)(bp);
+ error = biowait(bp);
+ goto done;
+ }
+ }
+ error = ESRCH;
done:
+ bp->b_flags |= B_INVAL;
brelse(bp);
- return (error);
+ return (error);
}
-
-/*
+/*
* Determine the size of the transfer, and make sure it is
* within the boundaries of the partition. Adjust transfer
* if needed, and signal errors or early completion.
struct disklabel *lp;
int wlabel;
{
-#define dkpart(dev) (minor(dev) % MAXPARTITIONS )
+ struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
+ int labelsector = lp->d_partitions[2].p_offset + LABELSECTOR;
+ int sz;
- struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
- int labelsect = lp->d_partitions[RAW_PART].p_offset;
- int maxsz = p->p_size;
- int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
+ sz = howmany(bp->b_bcount, lp->d_secsize);
- /* overwriting disk label ? */
- /* XXX should also protect bootstrap in first 8K */
- if (bp->b_blkno + p->p_offset == LABELSECTOR + labelsect &&
- (bp->b_flags & B_READ) == 0 && wlabel == 0) {
- bp->b_error = EROFS;
- goto bad;
- }
-
- /* beyond partition? */
- if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
- /* if exactly at end of disk, return an EOF */
- if (bp->b_blkno == maxsz) {
+ if (bp->b_blkno + sz > p->p_size) {
+ sz = p->p_size - bp->b_blkno;
+ if (sz == 0) {
+ /* If exactly at end of disk, return EOF. */
bp->b_resid = bp->b_bcount;
- return(0);
+ goto done;
}
- /* or truncate if part of it fits */
- sz = maxsz - bp->b_blkno;
- if (sz <= 0) {
+ if (sz < 0) {
+ /* If past end of disk, return EINVAL. */
bp->b_error = EINVAL;
goto bad;
}
+ /* Otherwise, truncate request. */
bp->b_bcount = sz << DEV_BSHIFT;
- }
+ }
+
+ /* Overwriting disk label? */
+ if (bp->b_blkno + p->p_offset <= labelsector &&
+#if LABELSECTOR != 0
+ bp->b_blkno + p->p_offset + sz > labelsector &&
+#endif
+ (bp->b_flags & B_READ) == 0 && !wlabel) {
+ bp->b_error = EROFS;
+ goto bad;
+ }
/* calculate cylinder for disksort to order transfers with */
- bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
- return(1);
+ bp->b_cylin = (bp->b_blkno + p->p_offset) /
+ (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
+ return (1);
+
bad:
bp->b_flags |= B_ERROR;
- return(-1);
+done:
+ return (0);
}