Subject: Re: disk label support?
To: Hubert Feyrer <hubert.feyrer@informatik.fh-regensburg.de>
From: Christopher SEKIYA <wileyc@rezrov.net>
List: port-sgimips
Date: 09/23/2001 23:54:46
On Sun, Sep 23, 2001 at 04:28:42PM +0200, Hubert Feyrer wrote:

> ISTR some discussion about disklabel support on the sgimips port not being
> 100% decided yet. What's the story?

Appended is a patch that appears to DTRT for disklabels.  A BSD disklabel is
derived from the SGI disklabel (if it exists) and placed in sector 1.  Slices
3 and 10 are swapped to keep things consistent.

This diff is not 100% good-to-go yet.  I intended to do more sanity checking and
the like, but my timing was really bad and I've very little time to hack
anymore :(

-- Chris

diff -urNbB /usr/local/src/netbsd-current/sys/arch/sgimips/include/disklabel.h include/disklabel.h
--- /usr/local/src/netbsd-current/sys/arch/sgimips/include/disklabel.h	Thu Jun 15 00:39:57 2000
+++ include/disklabel.h	Wed Aug 29 20:38:32 2001
@@ -51,22 +51,48 @@
  * How to sanely map partition numbers in that case?
  */
 #define MAXPARTITIONS	16
-#define RAW_PART	10
+#define RAW_PART	2
 
-#define LABELSECTOR	0
+#define LABELSECTOR	1
 #define LABELOFFSET	0
 
 struct cpu_disklabel {
 	int	cd_dummy;
 };
 
+struct devparms {
+	u_int8_t	dp_skew;
+	u_int8_t	dp_gap1;
+	u_int8_t	dp_gap2;
+	u_int8_t	dp_spares_cyl;
+	u_int16_t	dp_cyls;
+	u_int16_t	dp_shd0;
+	u_int16_t	dp_trks0;
+	u_int8_t	dp_ctq_depth;
+	u_int8_t	dp_cylshi;
+	u_int16_t	dp_unused;
+	u_int16_t	dp_secs;
+	u_int16_t	dp_secbytes;
+	u_int16_t	dp_interleave;
+	u_int32_t	dp_flags;
+	u_int32_t	dp_datarate;
+	u_int32_t	dp_nretries;
+	u_int32_t	dp_mspw;
+	u_int16_t	dp_xgap1;
+	u_int16_t	dp_xsync;
+	u_int16_t	dp_xrdly;
+	u_int16_t	dp_xgap2;
+	u_int16_t	dp_xrgate;
+	u_int16_t	dp_xwcont;
+} __attribute__((__packed__));
+
 struct sgilabel {
 #define SGILABEL_MAGIC	0xbe5a941
 	u_int32_t	magic;
 	int16_t		root;
 	int16_t		swap;
 	char		bootfile[16];
-	char		_devparms[48];
+	struct devparms dp;
 	struct {
 		char 		name[8];
 		int32_t		block;
@@ -83,6 +109,7 @@
 
 #define SGI_PTYPE_VOLHDR	0
 #define SGI_PTYPE_RAW		3
+#define SGI_PTYPE_BSD		4
 #define SGI_PTYPE_VOLUME	6
 #define SGI_PTYPE_EFS		7
 #define SGI_PTYPE_LVOL		8
--- /usr/local/src/netbsd-current/sys/arch/sgimips/sgimips/disksubr.c	Mon Nov 27 15:00:09 2000
+++ sgimips/disksubr.c	Wed Aug 29 20:39:02 2001
@@ -1,6 +1,6 @@
-/*	$NetBSD: disksubr.c,v 1.3 2000/11/27 06:00:09 soren Exp $	*/
-
 /*
+ * Copyright (c) 2001 Christopher Sekiya
+ * Copyright (c) 2001 Wayne Knowles
  * Copyright (c) 2000 Soren S. Jorvang
  * All rights reserved.
  * 
@@ -37,9 +37,19 @@
 #include <sys/buf.h>
 #include <sys/disklabel.h>
 #include <sys/disk.h>
+#include <ufs/ffs/fs.h>
 
 #include <machine/disklabel.h>
 
+static int disklabel_bsd_to_sgimips(struct disklabel *lp, struct sgilabel *vh);
+static char *disklabel_sgimips_to_bsd(struct sgilabel *vh, struct disklabel *lp);
+
+int     mipsvh_cksum(struct sgilabel *vhp);
+
+#define LABELSIZE(lp)   ((char *)&lp->d_partitions[lp->d_npartitions] - \
+                         (char *)lp)
+
+
 /*
  * Attempt to read a disk label from a device using the indicated
  * stategy routine. The label must be partly set up before this:
@@ -53,18 +63,12 @@
  */
 
 char *
-readdisklabel(dev, strat, lp, clp)
-	dev_t dev;
-	void (*strat)(struct buf *);
-	struct disklabel *lp; 
-	struct cpu_disklabel *clp;
+readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp)
 {
 	struct buf *bp;
 	struct disklabel *dlp; 
 	struct sgilabel *slp;
-	char block[512];
-	int error;
-	int i;
+	int err;
 
 	/* Minimal requirements for archetypal disk label. */
 	if (lp->d_secsize == 0)
@@ -75,86 +79,136 @@
 	/* Obtain buffer to probe drive with. */
 	bp = geteblk((int)lp->d_secsize);
 
-	/* Next, dig out the disk label. */
 	bp->b_dev = dev;
 	bp->b_blkno = LABELSECTOR;
-	bp->b_cylinder = 0;
 	bp->b_bcount = lp->d_secsize;
 	bp->b_flags |= B_READ;
+        bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
 	(*strat)(bp);
-
-	/* If successful, locate disk label within block and validate. */
-	error = biowait(bp);
-	if (error == 0) {
-		/* Save the whole block in case it has info we need. */
-		memcpy(block, bp->b_un.b_addr, sizeof(block));
-	}
+        err = biowait(bp);
 	brelse(bp);
-	if (error != 0)
+        
+        if (err)
 		return "error reading disklabel";
 
-	/* Check for a NetBSD disk label. */
-	dlp = (struct disklabel *) (block + LABELOFFSET);
-	if (dlp->d_magic == DISKMAGIC) {
-		if (dkcksum(dlp))
-			return ("NetBSD disk label corrupted");
-		*lp = *dlp;
-		return NULL;
+        /* Check for NetBSD label in second sector */
+        dlp = (struct disklabel *)(bp->b_un.b_addr + LABELOFFSET);
+        if (dlp->d_magic == DISKMAGIC)
+                if (!dkcksum(dlp)) {
+                        memcpy(lp, dlp, LABELSIZE(dlp));
+                        return NULL;    /* NetBSD label found */
 	}
 
+        bp = geteblk((int)lp->d_secsize);
+        bp->b_dev = dev;
+        bp->b_blkno = 0;
+        bp->b_bcount = lp->d_secsize;
+        bp->b_flags |= B_READ;
+        bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
+        (*strat)(bp);
+        err = biowait(bp);
+        brelse(bp);
+
+        if (err)
+                return "error reading volume header";
+
 	/* Check for a SGI label. */
-	slp = (struct sgilabel *)block;
+	slp = (struct sgilabel *)bp->b_un.b_addr;
 	if (be32toh(slp->magic) != SGILABEL_MAGIC)
 		return "no disk label";
-	/*
-	 * XXX Calculate checksum.
-	 */
-	for (i = 0; i < MAXPARTITIONS; i++) {
-	/* XXX be32toh */
-		lp->d_partitions[i].p_offset = slp->partitions[i].first;
-		lp->d_partitions[i].p_size = slp->partitions[i].blocks;
-		lp->d_partitions[i].p_fstype = FS_BSDFFS;
-		lp->d_partitions[i].p_fsize = 1024;
-		lp->d_partitions[i].p_frag = 8;
-		lp->d_partitions[i].p_cpg = 16;
-
-		if (i == RAW_PART)
-			lp->d_partitions[i].p_fstype = FS_OTHER;
-	}
-
-	lp->d_magic = DISKMAGIC;
-	lp->d_magic2 = DISKMAGIC;
-	lp->d_secsize = 512;
-	lp->d_npartitions = 16;
-
-	lp->d_checksum = 0;
-	lp->d_checksum = dkcksum(lp);
 
-	return NULL;
+        return disklabel_sgimips_to_bsd(slp, lp);
 }
 
 int
-setdisklabel(olp, nlp, openmask, clp)
-	struct disklabel *olp;
-	struct disklabel *nlp;
-	unsigned long openmask;
-	struct cpu_disklabel *clp;
+setdisklabel(struct disklabel *olp, struct disklabel *nlp, unsigned long openmask, struct cpu_disklabel *clp)
 {
-	printf("SETDISKLABEL\n");
+        register int i;
+        register struct partition *opp, *npp;
 
-	return 0;
+        if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
+            dkcksum(nlp) != 0)
+                return (EINVAL);
+        while ((i = ffs((long)openmask)) != 0) {
+                i--;
+                openmask &= ~(1 << i);
+                if (nlp->d_npartitions <= i)
+                        return (EBUSY);
+                opp = &olp->d_partitions[i];
+                npp = &nlp->d_partitions[i];
+                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
+                 */
+                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_frag = opp->p_frag;
+                        npp->p_cpg = opp->p_cpg;
+                }
+        }
+        nlp->d_checksum = 0;
+        nlp->d_checksum = dkcksum(nlp);
+        *olp = *nlp;
+        return (0);
 }
 
+#define dkpart(dev)             (minor(dev) & 07)
+#define dkminor(unit, part)     (((unit) << 3) | (part))
+
 int     
-writedisklabel(dev, strat, lp, clp)
-        dev_t dev; 
-        void (*strat)(struct buf *);
-        struct disklabel *lp;
-        struct cpu_disklabel *clp;
+writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp)
 {               
-	printf("WRITEDISKLABEL\n");
+        struct buf *bp;
+        int labelpart;
+        int error;
+
+        labelpart = dkpart(dev);
+        if (lp->d_partitions[labelpart].p_offset != 0) {
+                if (lp->d_partitions[0].p_offset != 0)
+                        return (EXDEV);                 /* not quite right */
+                labelpart = 0;
+        }
+
+        /* Read sgimips volume header before merging NetBSD partition info */
+        bp = geteblk((int)lp->d_secsize);
+
+        bp->b_dev = dev;
+        bp->b_blkno = 0;
+        bp->b_bcount = lp->d_secsize;
+        bp->b_flags |= B_READ;
+        bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
+        (*strat)(bp);
+
+        if((error = biowait(bp)) != 0)
+                goto ioerror;
+
+        if ((error = disklabel_bsd_to_sgimips(lp, (void *)bp->b_data)) != 0)
+                goto ioerror;
+
+        /* Write sgimips label to first sector */
+        bp->b_flags &= ~(B_READ|B_DONE);
+        bp->b_flags |= B_WRITE;
+        (*strat)(bp);
+        if ((error = biowait(bp)) != 0)
+                goto ioerror;
 
-	return ENODEV;
+        /* Write NetBSD disk label to second sector */
+        memset(bp->b_data, 0, lp->d_secsize);
+        memcpy(bp->b_data, lp, sizeof(*lp));
+        bp->b_blkno = LABELSECTOR;
+        bp->b_bcount = lp->d_secsize;
+        bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
+        bp->b_flags &= ~(B_READ | B_DONE);
+        bp->b_flags |= B_WRITE;
+        (*strat)(bp);
+        error = biowait(bp);
+
+ioerror:
+        brelse(bp);
+        return error;
 }
 
 
@@ -164,10 +218,7 @@
  * if needed, and signal errors or early completion.
  */
 int
-bounds_check_with_label(bp, lp, wlabel)
-	struct buf *bp;
-	struct disklabel *lp;
-	int wlabel;
+bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
 {
 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
 	int maxsz = p->p_size;
@@ -208,3 +259,156 @@
 	bp->b_flags |= B_ERROR;
 	return(-1);
 }
+
+struct partitionmap {
+        int     mips_part;      /* sgimips partition number */
+        int     mips_type;      /* sgimips partition type */
+        int     bsd_part;       /* BSD partition number */
+        int     bsd_type;       /* BSD partition type */
+};
+ 
+struct partitionmap partition_map[] = {
+     /* slice   sgimips type		BSD	BSD Type */
+        {0,     SGI_PTYPE_BSD,          0,      FS_BSDFFS},
+        {1,     SGI_PTYPE_RAW,          1,      FS_SWAP},
+        {2,     SGI_PTYPE_BSD,          10,     FS_BSDFFS},
+        {3,     SGI_PTYPE_BSD,          8,      FS_BSDFFS},
+	{4,     SGI_PTYPE_BSD,          4,      FS_BSDFFS},
+        {5,     SGI_PTYPE_BSD,          5,      FS_BSDFFS},
+        {6,     SGI_PTYPE_BSD,          6,      FS_BSDFFS},
+        {7,     SGI_PTYPE_BSD,          7,      FS_BSDFFS},
+	{8,	SGI_PTYPE_VOLHDR,	3,      FS_OTHER},
+	{9,	SGI_PTYPE_BSD,		9,      FS_BSDFFS},
+        {10,    SGI_PTYPE_VOLUME,	2,      FS_OTHER},
+	{11,	SGI_PTYPE_BSD,		11,	FS_BSDFFS},
+	{12,	SGI_PTYPE_BSD,		12,	FS_BSDFFS},
+	{13,	SGI_PTYPE_BSD,		13,	FS_BSDFFS},
+	{14,	SGI_PTYPE_BSD,		14,	FS_BSDFFS},
+	{15,	SGI_PTYPE_BSD,		15,	FS_BSDFFS}
+};
+
+#define NPARTMAP        (sizeof(partition_map)/sizeof(struct partitionmap))
+
+/*
+ * Convert a sgimips disk label into a NetBSD disk label.
+ *
+ * Returns NULL on success, otherwise an error string
+ */
+static char *
+disklabel_sgimips_to_bsd(struct sgilabel *vh, struct disklabel *lp)
+{
+        int  i, bp, mp;
+        struct partition *lpp;
+        if (mipsvh_cksum(vh))
+                return ("sgimips disk label corrupted");
+
+        lp->d_secsize    = vh->dp.dp_secbytes;
+        lp->d_nsectors   = vh->dp.dp_secs;
+        lp->d_ntracks    = vh->dp.dp_trks0;
+        lp->d_ncylinders = vh->dp.dp_cyls;
+        lp->d_interleave = vh->dp.dp_interleave;
+
+
+        lp->d_secpercyl  = lp->d_nsectors * lp->d_ntracks;
+        lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
+
+        lp->d_bbsize = BBSIZE;
+        lp->d_sbsize = SBSIZE;
+        lp->d_npartitions = MAXPARTITIONS;
+
+        for (i = 0; i < 16; i++) {
+                mp = partition_map[i].mips_part;
+                bp = partition_map[i].bsd_part;
+
+                lpp = &lp->d_partitions[bp];
+                lpp->p_offset = vh->partitions[mp].first;
+                lpp->p_size = vh->partitions[mp].blocks;
+                lpp->p_fstype = partition_map[i].bsd_type;
+                if (lpp->p_fstype == FS_BSDFFS) {
+                        lpp->p_fsize = 1024;
+                        lpp->p_frag = 8;
+                        lpp->p_cpg = 16;
+                }
+        }
+#if DIAGNOSTIC
+        printf("Warning: using sgimips disk label\n");
+#endif
+        return NULL;
+}
+
+
+/*
+ * Convert a NetBSD disk label into a sgimips disk label.
+ *
+ * Returns NULL on success, otherwise an error string
+ */
+static int
+disklabel_bsd_to_sgimips(struct disklabel *lp, struct sgilabel *vh)
+{
+        int  i, bp, mp;
+        struct partition *lpp;
+
+        if (vh->magic != SGILABEL_MAGIC || mipsvh_cksum(vh) != 0) {
+#if DIAGNOSTIC
+                printf("Warning: writing sgimips compatible label\n");
+#endif
+                memset((void *)vh, 0, sizeof *vh);
+                vh->magic = SGILABEL_MAGIC;
+                vh->root = 0;        /* a*/
+                vh->swap = 1;        /* b*/
+        }
+
+        strcpy(vh->bootfile, "/netbsd");
+        vh->dp.dp_skew = lp->d_trackskew;
+        vh->dp.dp_gap1 = 1; /* XXX */
+        vh->dp.dp_gap2 = 1; /* XXX */
+        vh->dp.dp_cyls = lp->d_ncylinders;
+        vh->dp.dp_shd0 = 0;
+        vh->dp.dp_trks0 = lp->d_ntracks;
+        vh->dp.dp_secs = lp->d_nsectors;
+        vh->dp.dp_secbytes = lp->d_secsize;
+        vh->dp.dp_interleave = lp->d_interleave;
+        vh->dp.dp_nretries = 22;
+
+        for (i = 0; i < 16; i++) {
+                mp = partition_map[i].mips_part;
+                bp = partition_map[i].bsd_part;
+
+                lpp = &lp->d_partitions[bp];
+                vh->partitions[mp].first = lpp->p_offset;
+                vh->partitions[mp].blocks = lpp->p_size;
+                vh->partitions[mp].type = partition_map[i].mips_type;
+        }
+
+        /*
+         * Create a fake partition for bootstrap code (or SASH)
+         */
+        vh->partitions[8].first = 0;
+        vh->partitions[8].blocks = vh->partitions[vh->root].first +
+                BBSIZE / vh->dp.dp_secbytes;
+        vh->partitions[8].type = SGI_PTYPE_VOLHDR;
+
+        vh->checksum = 0;
+        vh->checksum = -mipsvh_cksum(vh);
+        return 0;
+}
+
+/*
+ * Compute checksum for MIPS disk volume header
+ *
+ * Mips volume header checksum is the 32bit 2's complement sum
+ * of the entire volume header structure
+ */
+int
+mipsvh_cksum(struct sgilabel *vhp)
+{
+        int i, *ptr;
+        int cksum = 0;
+
+        ptr = (int *)vhp;
+        i = sizeof(*vhp) / sizeof(*ptr);
+        while (i--)
+                cksum += *ptr++;
+        return cksum;
+}
+