xref: /netbsd-src/sys/arch/sgimips/sgimips/disksubr.c (revision 021d832a1333a7c481b64a5d4c4c4d092bd0bc20)
1*021d832aSchristos /*	$NetBSD: disksubr.c,v 1.24 2019/04/03 22:10:51 christos Exp $	*/
2c09d9a0dSwdk 
3891194e1Ssoren /*
47f5269c5Swdk  * Copyright (c) 2001 Christopher Sekiya
57f5269c5Swdk  * Copyright (c) 2001 Wayne Knowles
6891194e1Ssoren  * Copyright (c) 2000 Soren S. Jorvang
7891194e1Ssoren  * All rights reserved.
8891194e1Ssoren  *
9891194e1Ssoren  * Redistribution and use in source and binary forms, with or without
10891194e1Ssoren  * modification, are permitted provided that the following conditions
11891194e1Ssoren  * are met:
12891194e1Ssoren  * 1. Redistributions of source code must retain the above copyright
13891194e1Ssoren  *    notice, this list of conditions and the following disclaimer.
14891194e1Ssoren  * 2. Redistributions in binary form must reproduce the above copyright
15891194e1Ssoren  *    notice, this list of conditions and the following disclaimer in the
16891194e1Ssoren  *    documentation and/or other materials provided with the distribution.
17891194e1Ssoren  * 3. All advertising materials mentioning features or use of this software
18891194e1Ssoren  *    must display the following acknowledgement:
19891194e1Ssoren  *          This product includes software developed for the
2007147999Skeihan  *          NetBSD Project.  See http://www.NetBSD.org/ for
21891194e1Ssoren  *          information about NetBSD.
22891194e1Ssoren  * 4. The name of the author may not be used to endorse or promote products
23891194e1Ssoren  *    derived from this software without specific prior written permission.
24891194e1Ssoren  *
25891194e1Ssoren  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26891194e1Ssoren  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27891194e1Ssoren  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28891194e1Ssoren  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29891194e1Ssoren  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30891194e1Ssoren  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31891194e1Ssoren  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32891194e1Ssoren  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33891194e1Ssoren  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34891194e1Ssoren  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35891194e1Ssoren  */
36891194e1Ssoren 
37ed517291Slukem #include <sys/cdefs.h>
38*021d832aSchristos __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.24 2019/04/03 22:10:51 christos Exp $");
39ed517291Slukem 
40891194e1Ssoren #include <sys/param.h>
41891194e1Ssoren #include <sys/systm.h>
42891194e1Ssoren #include <sys/buf.h>
43891194e1Ssoren #include <sys/disklabel.h>
44891194e1Ssoren #include <sys/disk.h>
45c09d9a0dSwdk #include <ufs/ufs/dinode.h>
467f5269c5Swdk #include <ufs/ffs/fs.h>
47891194e1Ssoren 
48891194e1Ssoren #include <machine/disklabel.h>
49891194e1Ssoren 
50ceb4d241Sjmc static int disklabel_bsd_to_sgimips(struct disklabel *lp,
51ceb4d241Sjmc 				    struct sgi_boot_block *vh);
52bf739d01Smartin static const char *disklabel_sgimips_to_bsd(struct sgi_boot_block *vh,
53ceb4d241Sjmc 				      struct disklabel *lp);
547f5269c5Swdk 
55ceb4d241Sjmc int mipsvh_cksum(struct sgi_boot_block *vhp);
567f5269c5Swdk 
577f5269c5Swdk #define LABELSIZE(lp)	((char *)&lp->d_partitions[lp->d_npartitions] - \
587f5269c5Swdk 			 (char *)lp)
597f5269c5Swdk 
607f5269c5Swdk 
61891194e1Ssoren /*
62891194e1Ssoren  * Attempt to read a disk label from a device using the indicated
6337e458faSwiz  * strategy routine. The label must be partly set up before this:
64891194e1Ssoren  * secpercyl, secsize and anything required for a block i/o read
65891194e1Ssoren  * operation in the driver's strategy/start routines must be
66891194e1Ssoren  * filled in before calling us.
67891194e1Ssoren  *
68891194e1Ssoren  * Return buffer for use in signalling errors if requested.
69891194e1Ssoren  *
70891194e1Ssoren  * Returns null on success and an error string on failure.
71891194e1Ssoren  */
72891194e1Ssoren 
73d91455ceSdsl const char *
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * clp)747f5269c5Swdk readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp)
75891194e1Ssoren {
76891194e1Ssoren 	struct buf *bp;
77891194e1Ssoren 	struct disklabel *dlp;
78ceb4d241Sjmc 	struct sgi_boot_block *slp;
797f5269c5Swdk 	int err;
80891194e1Ssoren 
81891194e1Ssoren 	/* Minimal requirements for archetypal disk label. */
82891194e1Ssoren 	if (lp->d_secsize == 0)
83891194e1Ssoren 		lp->d_secsize = DEV_BSIZE;
84891194e1Ssoren 	if (lp->d_secperunit == 0)
85891194e1Ssoren 		lp->d_secperunit = 0x1fffffff;
86891194e1Ssoren 
87891194e1Ssoren 	/* Obtain buffer to probe drive with. */
88891194e1Ssoren 	bp = geteblk((int)lp->d_secsize);
89891194e1Ssoren 
90891194e1Ssoren 	bp->b_dev = dev;
91891194e1Ssoren 	bp->b_blkno = LABELSECTOR;
92891194e1Ssoren 	bp->b_bcount = lp->d_secsize;
93c62d17a5Schs 	bp->b_flags |= B_READ;
947f5269c5Swdk 	bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
95891194e1Ssoren 	(*strat)(bp);
967f5269c5Swdk 	err = biowait(bp);
979f56dfa5Sad 	brelse(bp, 0);
987f5269c5Swdk 
997f5269c5Swdk 	if (err)
100891194e1Ssoren 		return "error reading disklabel";
101891194e1Ssoren 
1027f5269c5Swdk 	/* Check for NetBSD label in second sector */
103aeb73b56Sad 	dlp = (struct disklabel *)((char *)bp->b_data + LABELOFFSET);
1047f5269c5Swdk 	if (dlp->d_magic == DISKMAGIC)
1057f5269c5Swdk 		if (!dkcksum(dlp)) {
1067f5269c5Swdk 			memcpy(lp, dlp, LABELSIZE(dlp));
1077f5269c5Swdk 			return NULL;	/* NetBSD label found */
108891194e1Ssoren 	}
109891194e1Ssoren 
1107f5269c5Swdk 	bp = geteblk((int)lp->d_secsize);
1117f5269c5Swdk 	bp->b_dev = dev;
1127f5269c5Swdk 	bp->b_blkno = 0;
1137f5269c5Swdk 	bp->b_bcount = lp->d_secsize;
1147f5269c5Swdk 	bp->b_flags |= B_READ;
1157f5269c5Swdk 	bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
1167f5269c5Swdk 	(*strat)(bp);
1177f5269c5Swdk 	err = biowait(bp);
1189f56dfa5Sad 	brelse(bp, 0);
1197f5269c5Swdk 
1207f5269c5Swdk 	if (err)
1217f5269c5Swdk 		return "error reading volume header";
1227f5269c5Swdk 
123891194e1Ssoren 	/* Check for a SGI label. */
124aeb73b56Sad 	slp = (struct sgi_boot_block *)bp->b_data;
125ceb4d241Sjmc 	if (be32toh(slp->magic) != SGI_BOOT_BLOCK_MAGIC)
126891194e1Ssoren 		return "no disk label";
1277f5269c5Swdk 
1287f5269c5Swdk 	return disklabel_sgimips_to_bsd(slp, lp);
1297f5269c5Swdk }
1307f5269c5Swdk 
1317f5269c5Swdk #define dkpart(dev)		(minor(dev) & 07)
1327f5269c5Swdk #define dkminor(unit, part)	(((unit) << 3) | (part))
133891194e1Ssoren 
134891194e1Ssoren int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * clp)1357f5269c5Swdk writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp)
136891194e1Ssoren {
1377f5269c5Swdk 	struct buf *bp;
1387f5269c5Swdk 	int labelpart;
1397f5269c5Swdk 	int error;
140891194e1Ssoren 
1417f5269c5Swdk 	labelpart = dkpart(dev);
1427f5269c5Swdk 	if (lp->d_partitions[labelpart].p_offset != 0) {
1437f5269c5Swdk 		if (lp->d_partitions[0].p_offset != 0)
1447f5269c5Swdk 			return (EXDEV);			/* not quite right */
1457f5269c5Swdk 		labelpart = 0;
146891194e1Ssoren 	}
147891194e1Ssoren 
1487f5269c5Swdk 	/* Read sgimips volume header before merging NetBSD partition info */
1497f5269c5Swdk 	bp = geteblk((int)lp->d_secsize);
150891194e1Ssoren 
1517f5269c5Swdk 	bp->b_dev = dev;
1527f5269c5Swdk 	bp->b_blkno = 0;
1537f5269c5Swdk 	bp->b_bcount = lp->d_secsize;
1547f5269c5Swdk 	bp->b_flags |= B_READ;
1557f5269c5Swdk 	bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
1567f5269c5Swdk 	(*strat)(bp);
1577f5269c5Swdk 
1587f5269c5Swdk 	if((error = biowait(bp)) != 0)
1597f5269c5Swdk 		goto ioerror;
1607f5269c5Swdk 
1617f5269c5Swdk 	if ((error = disklabel_bsd_to_sgimips(lp, (void *)bp->b_data)) != 0)
1627f5269c5Swdk 		goto ioerror;
1637f5269c5Swdk 
1647f5269c5Swdk 	/* Write sgimips label to first sector */
1654a780c9aSad 	bp->b_oflags &= ~(BO_DONE);
1664a780c9aSad 	bp->b_flags &= ~(B_READ);
1677f5269c5Swdk 	bp->b_flags |= B_WRITE;
1687f5269c5Swdk 	(*strat)(bp);
1697f5269c5Swdk 	if ((error = biowait(bp)) != 0)
1707f5269c5Swdk 		goto ioerror;
1717f5269c5Swdk 
1727f5269c5Swdk 	/* Write NetBSD disk label to second sector */
1737f5269c5Swdk 	memset(bp->b_data, 0, lp->d_secsize);
1747f5269c5Swdk 	memcpy(bp->b_data, lp, sizeof(*lp));
1757f5269c5Swdk 	bp->b_blkno = LABELSECTOR;
1767f5269c5Swdk 	bp->b_bcount = lp->d_secsize;
1777f5269c5Swdk 	bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
1784a780c9aSad 	bp->b_oflags &= ~(BO_DONE);
1794a780c9aSad 	bp->b_flags &= ~(B_READ);
1807f5269c5Swdk 	bp->b_flags |= B_WRITE;
1817f5269c5Swdk 	(*strat)(bp);
1827f5269c5Swdk 	error = biowait(bp);
1837f5269c5Swdk 
1847f5269c5Swdk ioerror:
1859f56dfa5Sad 	brelse(bp, 0);
1867f5269c5Swdk 	return error;
187891194e1Ssoren }
188891194e1Ssoren 
1897f5269c5Swdk struct partitionmap {
1907f5269c5Swdk 	int	mips_part;	/* sgimips partition number */
1917f5269c5Swdk 	int	mips_type;	/* sgimips partition type */
1927f5269c5Swdk 	int	bsd_part;	/* BSD partition number */
1937f5269c5Swdk 	int	bsd_type;	/* BSD partition type */
1947f5269c5Swdk };
1957f5269c5Swdk 
1967f5269c5Swdk struct partitionmap partition_map[] = {
1977f5269c5Swdk      /* slice	sgimips type		BSD	BSD Type */
1987f5269c5Swdk 	{0,	SGI_PTYPE_BSD,		0,	FS_BSDFFS},
1997f5269c5Swdk 	{1,	SGI_PTYPE_RAW,		1,	FS_SWAP},
2007f5269c5Swdk 	{2,	SGI_PTYPE_BSD,		10,	FS_BSDFFS},
2017f5269c5Swdk 	{3,	SGI_PTYPE_BSD,		3,	FS_BSDFFS},
2027f5269c5Swdk 	{4,	SGI_PTYPE_BSD,		4,	FS_BSDFFS},
2037f5269c5Swdk 	{5,	SGI_PTYPE_BSD,		5,	FS_BSDFFS},
2047f5269c5Swdk 	{6,	SGI_PTYPE_BSD,		6,	FS_BSDFFS},
2057f5269c5Swdk 	{7,	SGI_PTYPE_BSD,		7,	FS_BSDFFS},
2067f5269c5Swdk 	{8,	SGI_PTYPE_VOLHDR,	8,	FS_OTHER},
2077f5269c5Swdk 	{9,	SGI_PTYPE_BSD,		9,	FS_BSDFFS},
2087f5269c5Swdk 	{10,	SGI_PTYPE_VOLUME,	2,	FS_OTHER},
2097f5269c5Swdk 	{11,	SGI_PTYPE_BSD,		11,	FS_BSDFFS},
2107f5269c5Swdk 	{12,	SGI_PTYPE_BSD,		12,	FS_BSDFFS},
2117f5269c5Swdk 	{13,	SGI_PTYPE_BSD,		13,	FS_BSDFFS},
2127f5269c5Swdk 	{14,	SGI_PTYPE_BSD,		14,	FS_BSDFFS},
2137f5269c5Swdk 	{15,	SGI_PTYPE_BSD,		15,	FS_BSDFFS}
2147f5269c5Swdk };
2157f5269c5Swdk 
2167f5269c5Swdk #define NPARTMAP	(sizeof(partition_map)/sizeof(struct partitionmap))
2177f5269c5Swdk 
2187f5269c5Swdk /*
2197f5269c5Swdk  * Convert a sgimips disk label into a NetBSD disk label.
2207f5269c5Swdk  *
2217f5269c5Swdk  * Returns NULL on success, otherwise an error string
2227f5269c5Swdk  */
223bf739d01Smartin static const char *
disklabel_sgimips_to_bsd(struct sgi_boot_block * vh,struct disklabel * lp)224ceb4d241Sjmc disklabel_sgimips_to_bsd(struct sgi_boot_block *vh, struct disklabel *lp)
2257f5269c5Swdk {
2267f5269c5Swdk 	int  i, bp, mp;
2277f5269c5Swdk 	struct partition *lpp;
2287f5269c5Swdk 	if (mipsvh_cksum(vh))
2297f5269c5Swdk 		return ("sgimips disk label corrupted");
2307f5269c5Swdk 
23117b2c3c8Stsutsui #if 0 /* ARCS ignores dp_secbytes and it may be wrong; use default instead */
2327f5269c5Swdk 	lp->d_secsize    = vh->dp.dp_secbytes;
23317b2c3c8Stsutsui #endif
2347f5269c5Swdk 	lp->d_nsectors   = vh->dp.dp_secs;
2357f5269c5Swdk 	lp->d_ntracks    = vh->dp.dp_trks0;
2367f5269c5Swdk 	lp->d_ncylinders = vh->dp.dp_cyls;
2377f5269c5Swdk 	lp->d_interleave = vh->dp.dp_interleave;
2387f5269c5Swdk 
2397f5269c5Swdk 
2407f5269c5Swdk 	lp->d_secpercyl  = lp->d_nsectors * lp->d_ntracks;
2417f5269c5Swdk 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2427f5269c5Swdk 
2437f5269c5Swdk 	lp->d_bbsize = BBSIZE;
2446a6aa0e6She 	lp->d_sbsize = SBLOCKSIZE;
2457f5269c5Swdk 	lp->d_npartitions = MAXPARTITIONS;
2467f5269c5Swdk 
2477f5269c5Swdk 	for (i = 0; i < 16; i++) {
2487f5269c5Swdk 		mp = partition_map[i].mips_part;
2497f5269c5Swdk 		bp = partition_map[i].bsd_part;
2507f5269c5Swdk 
2517f5269c5Swdk 		lpp = &lp->d_partitions[bp];
2527f5269c5Swdk 		lpp->p_offset = vh->partitions[mp].first;
25317b2c3c8Stsutsui 		/* XXX ARCS ignores dp_secbytes on calculating offsets */
25417b2c3c8Stsutsui 		if (lp->d_secsize > DEV_BSIZE)
25517b2c3c8Stsutsui 			lpp->p_offset /= lp->d_secsize / DEV_BSIZE;
2567f5269c5Swdk 		lpp->p_size = vh->partitions[mp].blocks;
2577f5269c5Swdk 		lpp->p_fstype = partition_map[i].bsd_type;
2587f5269c5Swdk 		if (lpp->p_fstype == FS_BSDFFS) {
2597f5269c5Swdk 			lpp->p_fsize = 1024;
2607f5269c5Swdk 			lpp->p_frag = 8;
2617f5269c5Swdk 			lpp->p_cpg = 16;
2627f5269c5Swdk 		}
2637f5269c5Swdk 	}
2647f5269c5Swdk 	return NULL;
2657f5269c5Swdk }
2667f5269c5Swdk 
2677f5269c5Swdk 
2687f5269c5Swdk /*
2697f5269c5Swdk  * Convert a NetBSD disk label into a sgimips disk label.
2707f5269c5Swdk  *
2717f5269c5Swdk  * Returns NULL on success, otherwise an error string
2727f5269c5Swdk  */
2737f5269c5Swdk static int
disklabel_bsd_to_sgimips(struct disklabel * lp,struct sgi_boot_block * vh)274ceb4d241Sjmc disklabel_bsd_to_sgimips(struct disklabel *lp, struct sgi_boot_block *vh)
2757f5269c5Swdk {
2767f5269c5Swdk 	int i, bp, mp;
2777f5269c5Swdk 	struct partition *lpp;
2787f5269c5Swdk 
279ceb4d241Sjmc 	if (vh->magic != SGI_BOOT_BLOCK_MAGIC || mipsvh_cksum(vh) != 0) {
2807f5269c5Swdk 		memset((void *)vh, 0, sizeof *vh);
281ceb4d241Sjmc 		vh->magic = SGI_BOOT_BLOCK_MAGIC;
2827f5269c5Swdk 		vh->root = 0;        /* a*/
2837f5269c5Swdk 		vh->swap = 1;        /* b*/
2847f5269c5Swdk 	}
2857f5269c5Swdk 
2867f5269c5Swdk 	strcpy(vh->bootfile, "/netbsd");
2877f5269c5Swdk 	vh->dp.dp_skew = lp->d_trackskew;
2887f5269c5Swdk 	vh->dp.dp_gap1 = 1; /* XXX */
2897f5269c5Swdk 	vh->dp.dp_gap2 = 1; /* XXX */
2907f5269c5Swdk 	vh->dp.dp_cyls = lp->d_ncylinders;
2917f5269c5Swdk 	vh->dp.dp_shd0 = 0;
2927f5269c5Swdk 	vh->dp.dp_trks0 = lp->d_ntracks;
2937f5269c5Swdk 	vh->dp.dp_secs = lp->d_nsectors;
29417b2c3c8Stsutsui #if 0 /* ARCS ignores dp_secbytes; leave it default */
2957f5269c5Swdk 	vh->dp.dp_secbytes = lp->d_secsize;
29617b2c3c8Stsutsui #else
29717b2c3c8Stsutsui 	vh->dp.dp_secbytes = SGI_BOOT_BLOCK_BLOCKSIZE;
29817b2c3c8Stsutsui #endif
2997f5269c5Swdk 	vh->dp.dp_interleave = lp->d_interleave;
3007f5269c5Swdk 	vh->dp.dp_nretries = 22;
3017f5269c5Swdk 
3027f5269c5Swdk 	for (i = 0; i < 16; i++) {
3037f5269c5Swdk 		mp = partition_map[i].mips_part;
3047f5269c5Swdk 		bp = partition_map[i].bsd_part;
3057f5269c5Swdk 
3067f5269c5Swdk 		lpp = &lp->d_partitions[bp];
3077f5269c5Swdk 		vh->partitions[mp].first = lpp->p_offset;
30817b2c3c8Stsutsui 		/* XXX ARCS ignores dp_secbytes on calculating offsets */
30917b2c3c8Stsutsui 		if (lp->d_secsize > SGI_BOOT_BLOCK_BLOCKSIZE)
31017b2c3c8Stsutsui 			vh->partitions[mp].first *=
31117b2c3c8Stsutsui 			    lp->d_secsize / SGI_BOOT_BLOCK_BLOCKSIZE;
3127f5269c5Swdk 		vh->partitions[mp].blocks = lpp->p_size;
3137f5269c5Swdk 		vh->partitions[mp].type = partition_map[i].mips_type;
3147f5269c5Swdk 	}
3157f5269c5Swdk 
3167f5269c5Swdk 	/*
3177f5269c5Swdk 	 * Create a fake partition for bootstrap code (or SASH)
3187f5269c5Swdk 	 */
3197f5269c5Swdk 	vh->partitions[8].first = 0;
3207f5269c5Swdk 	vh->partitions[8].blocks = vh->partitions[vh->root].first +
3217f5269c5Swdk 		BBSIZE / vh->dp.dp_secbytes;
3227f5269c5Swdk 	vh->partitions[8].type = SGI_PTYPE_VOLHDR;
3237f5269c5Swdk 
3247f5269c5Swdk 	vh->checksum = 0;
3257f5269c5Swdk 	vh->checksum = -mipsvh_cksum(vh);
3267f5269c5Swdk 	return 0;
3277f5269c5Swdk }
3287f5269c5Swdk 
3297f5269c5Swdk /*
3307f5269c5Swdk  * Compute checksum for MIPS disk volume header
3317f5269c5Swdk  *
3327f5269c5Swdk  * Mips volume header checksum is the 32bit 2's complement sum
3337f5269c5Swdk  * of the entire volume header structure
3347f5269c5Swdk  */
3357f5269c5Swdk int
mipsvh_cksum(struct sgi_boot_block * vhp)336ceb4d241Sjmc mipsvh_cksum(struct sgi_boot_block *vhp)
3377f5269c5Swdk {
3387f5269c5Swdk 	int i, *ptr;
3397f5269c5Swdk 	int cksum = 0;
3407f5269c5Swdk 
3417f5269c5Swdk 	ptr = (int *)vhp;
3427f5269c5Swdk 	i = sizeof(*vhp) / sizeof(*ptr);
3437f5269c5Swdk 	while (i--)
3447f5269c5Swdk 		cksum += *ptr++;
3457f5269c5Swdk 	return cksum;
3467f5269c5Swdk }
3477f5269c5Swdk 
348