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