Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_rdb.c revision 1.4.22.1
      1 /*	$NetBSD: dkwedge_rdb.c,v 1.4.22.1 2020/04/13 08:04:19 martin 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.4.22.1 2020/04/13 08:04:19 martin 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/malloc.h>
     78 #ifdef _KERNEL
     79 #include <sys/systm.h>
     80 #endif
     81 
     82 /*
     83  * In /usr/src/sys/dev/scsipi/sd.c, routine sdstart() adjusts the
     84  * block numbers, it changes from DEV_BSIZE units to physical units:
     85  * blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
     86  * As long as media with sector sizes of 512 bytes are used, this
     87  * doesn't matter (divide by 1), but for successful usage of media with
     88  * greater sector sizes (e.g. 640MB MO-media with 2048 bytes/sector)
     89  * we must multiply block numbers with (lp->d_secsize / DEV_BSIZE)
     90  * to keep "unchanged" physical block numbers.
     91  */
     92 #define	SD_C_ADJUSTS_NR
     93 #ifdef SD_C_ADJUSTS_NR
     94 #define	ADJUST_NR(x)	((x) * (secsize / DEV_BSIZE))
     95 #else
     96 #define	ADJUST_NR(x)	(x)
     97 #endif
     98 
     99 #ifdef _KERNEL
    100 #define	DKW_MALLOC(SZ)	malloc((SZ), M_DEVBUF, M_WAITOK)
    101 #define	DKW_FREE(PTR)	free((PTR), M_DEVBUF)
    102 #define	DKW_REALLOC(PTR, NEWSZ)	realloc((PTR), (NEWSZ), M_DEVBUF, M_WAITOK)
    103 #else
    104 #define	DKW_MALLOC(SZ)	malloc((SZ))
    105 #define	DKW_FREE(PTR)	free((PTR))
    106 #define	DKW_REALLOC(PTR, NEWSZ)	realloc((PTR), (NEWSZ))
    107 #endif
    108 
    109 static unsigned rdbchksum(void *);
    110 static unsigned char getarchtype(unsigned);
    111 static const char *archtype_to_ptype(unsigned char);
    112 
    113 static int
    114 dkwedge_discover_rdb(struct disk *pdk, struct vnode *vp)
    115 {
    116 	struct dkwedge_info dkw;
    117 	struct partblock *pbp;
    118 	struct rdblock *rbp;
    119 	void *bp;
    120 	int error;
    121 	unsigned blk_per_cyl, bufsize, newsecsize, nextb, secsize, tabsize;
    122 	const char *ptype;
    123 	unsigned char archtype;
    124 	bool found, root, swap;
    125 
    126 	secsize = DEV_BSIZE << pdk->dk_blkshift;
    127 	bufsize = roundup(MAX(sizeof(struct partblock), sizeof(struct rdblock)),
    128 	    secsize);
    129 	bp = DKW_MALLOC(bufsize);
    130 
    131 	/*
    132 	 * find the RDB block
    133 	 * XXX bsdlabel should be detected by the other method
    134 	 */
    135 	for (nextb = 0; nextb < RDB_MAXBLOCKS; nextb++) {
    136 		error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp, bufsize);
    137 		if (error) {
    138 			aprint_error("%s: unable to read RDB @ %u, "
    139 			    "error = %d\n", pdk->dk_name, nextb, error);
    140 			error = ESRCH;
    141 			goto done;
    142 		}
    143 		rbp = (struct rdblock *)bp;
    144 		if (be32toh(rbp->id) == RDBLOCK_ID) {
    145 			if (rdbchksum(rbp) == 0)
    146 				break;
    147 			else
    148 				aprint_error("%s: RDB bad checksum @ %u\n",
    149 				    pdk->dk_name, nextb);
    150 		}
    151 	}
    152 
    153 	if (nextb == RDB_MAXBLOCKS) {
    154 		error = ESRCH;
    155 		goto done;
    156 	}
    157 
    158 	newsecsize = be32toh(rbp->nbytes);
    159 	if (secsize != newsecsize) {
    160 		aprint_verbose("secsize changed from %u to %u\n",
    161 		    secsize, newsecsize);
    162 		secsize = newsecsize;
    163 		bufsize = roundup(MAX(sizeof(struct partblock),
    164 		    sizeof(struct rdblock)), secsize);
    165 		bp = DKW_REALLOC(bp, bufsize);
    166 	}
    167 
    168 	memset(&dkw, 0, sizeof(dkw));
    169 
    170 	strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
    171 
    172 	found = root = swap = false;
    173 	/*
    174 	 * scan for partition blocks
    175 	 */
    176 	for (nextb = be32toh(rbp->partbhead); nextb != RDBNULL;
    177 	     nextb = be32toh(pbp->next)) {
    178 		error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp, bufsize);
    179 		if (error) {
    180 			aprint_error("%s: unable to read RDB partition block @ "
    181 			    "%u, error = %d\n", pdk->dk_name, nextb, error);
    182 			error = ESRCH;
    183 			goto done;
    184 		}
    185 		pbp = (struct partblock *)bp;
    186 
    187 		if (be32toh(pbp->id) != PARTBLOCK_ID) {
    188 			aprint_error(
    189 			    "%s: RDB partition block @ %u bad id\n",
    190 			    pdk->dk_name, nextb);
    191 			error = ESRCH;
    192 			goto done;
    193 		}
    194 		if (rdbchksum(pbp)) {
    195 			aprint_error(
    196 			    "%s: RDB partition block @ %u bad checksum\n",
    197 			    pdk->dk_name, nextb);
    198 			error = ESRCH;
    199 			goto done;
    200 		}
    201 
    202 		tabsize = be32toh(pbp->e.tabsize);
    203 		if (tabsize < 11) {
    204 			/*
    205 			 * not enough info, too funky for us.
    206 			 * I don't want to skip I want it fixed.
    207 			 */
    208 			aprint_error(
    209 			    "%s: RDB partition block @ %u bad partition info "
    210 			    "(environ < 11)\n",
    211 			    pdk->dk_name, nextb);
    212 			error = ESRCH;
    213 			goto done;
    214 		}
    215 
    216 		/*
    217 		 * XXXX should be ">" however some vendors don't know
    218 		 * what a table size is so, we hack for them.
    219 		 * the other checks can fail for all I care but this
    220 		 * is a very common value. *sigh*.
    221 		 */
    222 		if (tabsize >= 16)
    223 			archtype = getarchtype(be32toh(pbp->e.dostype));
    224 		else
    225 			archtype = ADT_UNKNOWN;
    226 
    227 		switch (archtype) {
    228 		case ADT_NETBSDROOT:
    229 			if (root) {
    230 				aprint_error("%s: more than one root RDB "
    231 				    "partition, ignoring\n", pdk->dk_name);
    232 				continue;
    233 			}
    234 			root = true;
    235 			break;
    236 		case ADT_NETBSDSWAP:
    237 			if (swap) {
    238 				aprint_error("%s: more than one swap RDB "
    239 				    "partition , ignoring\n", pdk->dk_name);
    240 				continue;
    241 			}
    242 			swap = true;
    243 			break;
    244 		default:
    245 			break;
    246 		}
    247 
    248 		if (pbp->partname[0] + 1 < sizeof(pbp->partname))
    249 			pbp->partname[pbp->partname[0] + 1] = 0;
    250 		else
    251 			pbp->partname[sizeof(pbp->partname) - 1] = 0;
    252 		strlcpy(dkw.dkw_wname, pbp->partname + 1,
    253 		    sizeof(dkw.dkw_wname));
    254 
    255 		ptype = archtype_to_ptype(archtype);
    256 		strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
    257 
    258 		blk_per_cyl =
    259 		    be32toh(pbp->e.secpertrk) * be32toh(pbp->e.numheads)
    260 		    * ((be32toh(pbp->e.sizeblock) << 2) / secsize);
    261 		dkw.dkw_size = (uint64_t)(be32toh(pbp->e.highcyl)
    262 		    - be32toh(pbp->e.lowcyl) + 1) * blk_per_cyl;
    263 		dkw.dkw_offset = (daddr_t)be32toh(pbp->e.lowcyl) * blk_per_cyl;
    264 
    265 		error = dkwedge_add(&dkw);
    266 		if (error == EEXIST) {
    267 			aprint_error(
    268 			    "%s: wedge named '%s' already exists, ignoring\n",
    269 			    pdk->dk_name, dkw.dkw_wname);
    270 		} else if (error)
    271 			aprint_error("%s: error %d adding partition %s\n",
    272 			    pdk->dk_name, error, dkw.dkw_wname);
    273 		else
    274 			found = true;
    275 	}
    276 
    277 	if (found)
    278 		error = 0;
    279 	else
    280 		error = ESRCH;
    281 done:
    282 	DKW_FREE(bp);
    283 	return error;
    284 }
    285 
    286 static unsigned
    287 rdbchksum(void *bdata)
    288 {
    289 	unsigned *blp, cnt, val;
    290 
    291 	blp = bdata;
    292 	cnt = be32toh(blp[1]);
    293 	val = 0;
    294 
    295 	while (cnt--)
    296 		val += be32toh(*blp++);
    297 	return(val);
    298 }
    299 
    300 static unsigned char
    301 getarchtype(unsigned dostype)
    302 {
    303 	unsigned t3, b1;
    304 
    305 	t3 = dostype & 0xffffff00;
    306 	b1 = dostype & 0x000000ff;
    307 
    308 	switch (t3) {
    309 	case DOST_NBR:
    310 		return ADT_NETBSDROOT;
    311 	case DOST_NBS:
    312 		return ADT_NETBSDSWAP;
    313 	case DOST_NBU:
    314 		return ADT_NETBSDUSER;
    315 	case DOST_MUFS: /* check for 'muFS'? */
    316 	case DOST_DOS:
    317 		return ADT_AMIGADOS;
    318 	case DOST_AMIX:
    319 		return ADT_AMIX;
    320 
    321 	case DOST_XXXBSD:
    322 #ifdef DIAGNOSTIC
    323 		aprint_verbose("deprecated dostype found: 0x%x\n",
    324 		    dostype);
    325 #endif
    326 		switch (b1) {
    327 		case 'R':
    328 			return ADT_NETBSDROOT;
    329 		case 'S':
    330 			return ADT_NETBSDSWAP;
    331 		default:
    332 			return ADT_NETBSDUSER;
    333 		}
    334 
    335 	case DOST_EXT2:
    336 		return ADT_EXT2;
    337 	case DOST_RAID:
    338 		return ADT_RAID;
    339 	default:
    340 #ifdef DIAGNOSTIC
    341 		aprint_verbose("warning unknown dostype: 0x%x\n",
    342 		    dostype);
    343 #endif
    344 		return ADT_UNKNOWN;
    345 	}
    346 }
    347 
    348 static const char *
    349 archtype_to_ptype(unsigned char archtype)
    350 {
    351 	switch (archtype) {
    352 	case ADT_NETBSDROOT:
    353 	case ADT_NETBSDUSER:
    354 		return DKW_PTYPE_FFS;
    355 	case ADT_NETBSDSWAP:
    356 		return DKW_PTYPE_SWAP;
    357 	case ADT_AMIGADOS:
    358 		return DKW_PTYPE_AMIGADOS;
    359 	case ADT_EXT2:
    360 		return DKW_PTYPE_EXT2FS;
    361 	default:
    362 		return DKW_PTYPE_UNKNOWN;
    363 	}
    364 }
    365 
    366 #ifdef _KERNEL
    367 DKWEDGE_DISCOVERY_METHOD_DECL(RDB, 15, dkwedge_discover_rdb);
    368 #endif
    369