Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_rdb.c revision 1.6
      1 /*	$NetBSD: dkwedge_rdb.c,v 1.6 2020/04/11 16:00:34 jdolecek Exp $	*/
      2 
      3 /*
      4  * Adapted from arch/amiga/amiga/disksubr.c:
      5  *
      6  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
     34  */
     35 
     36 /*
     37  * Copyright (c) 1994 Christian E. Hopps
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *	This product includes software developed by the University of
     50  *	California, Berkeley and its contributors.
     51  * 4. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: dkwedge_rdb.c,v 1.6 2020/04/11 16:00:34 jdolecek Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/disklabel_rdb.h>
     75 #include <sys/disk.h>
     76 #include <sys/endian.h>
     77 #include <sys/systm.h>
     78 #include <sys/buf.h>
     79 
     80 /*
     81  * In /usr/src/sys/dev/scsipi/sd.c, routine sdstart() adjusts the
     82  * block numbers, it changes from DEV_BSIZE units to physical units:
     83  * blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
     84  * As long as media with sector sizes of 512 bytes are used, this
     85  * doesn't matter (divide by 1), but for successful usage of media with
     86  * greater sector sizes (e.g. 640MB MO-media with 2048 bytes/sector)
     87  * we must multiply block numbers with (lp->d_secsize / DEV_BSIZE)
     88  * to keep "unchanged" physical block numbers.
     89  */
     90 #define	SD_C_ADJUSTS_NR
     91 #ifdef SD_C_ADJUSTS_NR
     92 #define	ADJUST_NR(x)	((x) * (secsize / DEV_BSIZE))
     93 #else
     94 #define	ADJUST_NR(x)	(x)
     95 #endif
     96 
     97 static unsigned rdbchksum(void *);
     98 static unsigned char getarchtype(unsigned);
     99 static const char *archtype_to_ptype(unsigned char);
    100 
    101 static int
    102 dkwedge_discover_rdb(struct disk *pdk, struct vnode *vp)
    103 {
    104 	struct dkwedge_info dkw;
    105 	struct partblock *pbp;
    106 	struct rdblock *rbp;
    107 	struct buf *bp;
    108 	int error;
    109 	unsigned blk_per_cyl, bufsize, newsecsize, nextb, secsize, tabsize;
    110 	const char *ptype;
    111 	unsigned char archtype;
    112 	bool found, root, swap;
    113 
    114 	secsize = DEV_BSIZE << pdk->dk_blkshift;
    115 	bufsize = roundup(MAX(sizeof(struct partblock), sizeof(struct rdblock)),
    116 	    secsize);
    117 	bp = geteblk(bufsize);
    118 
    119 	/*
    120 	 * find the RDB block
    121 	 * XXX bsdlabel should be detected by the other method
    122 	 */
    123 	for (nextb = 0; nextb < RDB_MAXBLOCKS; nextb++) {
    124 		error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp, bufsize);
    125 		if (error) {
    126 			aprint_error("%s: unable to read RDB @ %u, "
    127 			    "error = %d\n", pdk->dk_name, nextb, error);
    128 			error = ESRCH;
    129 			goto done;
    130 		}
    131 		rbp = (struct rdblock *)bp;
    132 		if (be32toh(rbp->id) == RDBLOCK_ID) {
    133 			if (rdbchksum(rbp) == 0)
    134 				break;
    135 			else
    136 				aprint_error("%s: RDB bad checksum @ %u\n",
    137 				    pdk->dk_name, nextb);
    138 		}
    139 	}
    140 
    141 	if (nextb == RDB_MAXBLOCKS) {
    142 		error = ESRCH;
    143 		goto done;
    144 	}
    145 
    146 	newsecsize = be32toh(rbp->nbytes);
    147 	if (secsize != newsecsize) {
    148 		aprint_verbose("secsize changed from %u to %u\n",
    149 		    secsize, newsecsize);
    150 		secsize = newsecsize;
    151 		bufsize = roundup(MAX(sizeof(struct partblock),
    152 		    sizeof(struct rdblock)), secsize);
    153 		brelse(bp, 0);
    154 		bp = geteblk(bufsize);
    155 	}
    156 
    157 	memset(&dkw, 0, sizeof(dkw));
    158 
    159 	strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
    160 
    161 	found = root = swap = false;
    162 	/*
    163 	 * scan for partition blocks
    164 	 */
    165 	for (nextb = be32toh(rbp->partbhead); nextb != RDBNULL;
    166 	     nextb = be32toh(pbp->next)) {
    167 		error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp, bufsize);
    168 		if (error) {
    169 			aprint_error("%s: unable to read RDB partition block @ "
    170 			    "%u, error = %d\n", pdk->dk_name, nextb, error);
    171 			error = ESRCH;
    172 			goto done;
    173 		}
    174 		pbp = (struct partblock *)bp;
    175 
    176 		if (be32toh(pbp->id) != PARTBLOCK_ID) {
    177 			aprint_error(
    178 			    "%s: RDB partition block @ %u bad id\n",
    179 			    pdk->dk_name, nextb);
    180 			error = ESRCH;
    181 			goto done;
    182 		}
    183 		if (rdbchksum(pbp)) {
    184 			aprint_error(
    185 			    "%s: RDB partition block @ %u bad checksum\n",
    186 			    pdk->dk_name, nextb);
    187 			error = ESRCH;
    188 			goto done;
    189 		}
    190 
    191 		tabsize = be32toh(pbp->e.tabsize);
    192 		if (tabsize < 11) {
    193 			/*
    194 			 * not enough info, too funky for us.
    195 			 * I don't want to skip I want it fixed.
    196 			 */
    197 			aprint_error(
    198 			    "%s: RDB partition block @ %u bad partition info "
    199 			    "(environ < 11)\n",
    200 			    pdk->dk_name, nextb);
    201 			error = ESRCH;
    202 			goto done;
    203 		}
    204 
    205 		/*
    206 		 * XXXX should be ">" however some vendors don't know
    207 		 * what a table size is so, we hack for them.
    208 		 * the other checks can fail for all I care but this
    209 		 * is a very common value. *sigh*.
    210 		 */
    211 		if (tabsize >= 16)
    212 			archtype = getarchtype(be32toh(pbp->e.dostype));
    213 		else
    214 			archtype = ADT_UNKNOWN;
    215 
    216 		switch (archtype) {
    217 		case ADT_NETBSDROOT:
    218 			if (root) {
    219 				aprint_error("%s: more than one root RDB "
    220 				    "partition, ignoring\n", pdk->dk_name);
    221 				continue;
    222 			}
    223 			root = true;
    224 			break;
    225 		case ADT_NETBSDSWAP:
    226 			if (swap) {
    227 				aprint_error("%s: more than one swap RDB "
    228 				    "partition , ignoring\n", pdk->dk_name);
    229 				continue;
    230 			}
    231 			swap = true;
    232 			break;
    233 		default:
    234 			break;
    235 		}
    236 
    237 		if (pbp->partname[0] + 1 < sizeof(pbp->partname))
    238 			pbp->partname[pbp->partname[0] + 1] = 0;
    239 		else
    240 			pbp->partname[sizeof(pbp->partname) - 1] = 0;
    241 		strlcpy(dkw.dkw_wname, pbp->partname + 1,
    242 		    sizeof(dkw.dkw_wname));
    243 
    244 		ptype = archtype_to_ptype(archtype);
    245 		strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
    246 
    247 		blk_per_cyl =
    248 		    be32toh(pbp->e.secpertrk) * be32toh(pbp->e.numheads)
    249 		    * ((be32toh(pbp->e.sizeblock) << 2) / secsize);
    250 		dkw.dkw_size = (uint64_t)(be32toh(pbp->e.highcyl)
    251 		    - be32toh(pbp->e.lowcyl) + 1) * blk_per_cyl;
    252 		dkw.dkw_offset = (daddr_t)be32toh(pbp->e.lowcyl) * blk_per_cyl;
    253 
    254 		error = dkwedge_add(&dkw);
    255 		if (error == EEXIST) {
    256 			aprint_error(
    257 			    "%s: wedge named '%s' already exists, ignoring\n",
    258 			    pdk->dk_name, dkw.dkw_wname);
    259 		} else if (error)
    260 			aprint_error("%s: error %d adding partition %s\n",
    261 			    pdk->dk_name, error, dkw.dkw_wname);
    262 		else
    263 			found = true;
    264 	}
    265 
    266 	if (found)
    267 		error = 0;
    268 	else
    269 		error = ESRCH;
    270 done:
    271 	brelse(bp, 0);
    272 	return error;
    273 }
    274 
    275 static unsigned
    276 rdbchksum(void *bdata)
    277 {
    278 	unsigned *blp, cnt, val;
    279 
    280 	blp = bdata;
    281 	cnt = be32toh(blp[1]);
    282 	val = 0;
    283 
    284 	while (cnt--)
    285 		val += be32toh(*blp++);
    286 	return(val);
    287 }
    288 
    289 static unsigned char
    290 getarchtype(unsigned dostype)
    291 {
    292 	unsigned t3, b1;
    293 
    294 	t3 = dostype & 0xffffff00;
    295 	b1 = dostype & 0x000000ff;
    296 
    297 	switch (t3) {
    298 	case DOST_NBR:
    299 		return ADT_NETBSDROOT;
    300 	case DOST_NBS:
    301 		return ADT_NETBSDSWAP;
    302 	case DOST_NBU:
    303 		return ADT_NETBSDUSER;
    304 	case DOST_MUFS: /* check for 'muFS'? */
    305 	case DOST_DOS:
    306 		return ADT_AMIGADOS;
    307 	case DOST_AMIX:
    308 		return ADT_AMIX;
    309 
    310 	case DOST_XXXBSD:
    311 #ifdef DIAGNOSTIC
    312 		aprint_verbose("deprecated dostype found: 0x%x\n",
    313 		    dostype);
    314 #endif
    315 		switch (b1) {
    316 		case 'R':
    317 			return ADT_NETBSDROOT;
    318 		case 'S':
    319 			return ADT_NETBSDSWAP;
    320 		default:
    321 			return ADT_NETBSDUSER;
    322 		}
    323 
    324 	case DOST_EXT2:
    325 		return ADT_EXT2;
    326 	case DOST_RAID:
    327 		return ADT_RAID;
    328 	default:
    329 #ifdef DIAGNOSTIC
    330 		aprint_verbose("warning unknown dostype: 0x%x\n",
    331 		    dostype);
    332 #endif
    333 		return ADT_UNKNOWN;
    334 	}
    335 }
    336 
    337 static const char *
    338 archtype_to_ptype(unsigned char archtype)
    339 {
    340 	switch (archtype) {
    341 	case ADT_NETBSDROOT:
    342 	case ADT_NETBSDUSER:
    343 		return DKW_PTYPE_FFS;
    344 	case ADT_NETBSDSWAP:
    345 		return DKW_PTYPE_SWAP;
    346 	case ADT_AMIGADOS:
    347 		return DKW_PTYPE_AMIGADOS;
    348 	case ADT_EXT2:
    349 		return DKW_PTYPE_EXT2FS;
    350 	default:
    351 		return DKW_PTYPE_UNKNOWN;
    352 	}
    353 }
    354 
    355 #ifdef _KERNEL
    356 DKWEDGE_DISCOVERY_METHOD_DECL(RDB, 15, dkwedge_discover_rdb);
    357 #endif
    358