xref: /netbsd-src/sys/dev/dkwedge/dkwedge_rdb.c (revision 55b56938ed6d4b41323615770c1cafc2658becd9)
1 /*	$NetBSD: dkwedge_rdb.c,v 1.8 2021/02/20 09:51:20 rin 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.8 2021/02/20 09:51:20 rin Exp $");
72 
73 #include <sys/param.h>
74 #include <sys/buf.h>
75 #include <sys/disklabel_rdb.h>
76 #include <sys/disk.h>
77 #include <sys/endian.h>
78 #include <sys/systm.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(uint32_t);
99 static const char *archtype_to_ptype(uint8_t);
100 
101 static int
dkwedge_discover_rdb(struct disk * pdk,struct vnode * vp)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 	uint32_t blk_per_cyl, bufsize, newsecsize, nextb, secsize, tabsize;
110 	const char *ptype;
111 	uint8_t 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 retry:
120 	/*
121 	 * find the RDB block
122 	 * XXX bsdlabel should be detected by the other method
123 	 */
124 	for (nextb = 0; nextb < RDB_MAXBLOCKS; nextb++) {
125 		error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp->b_data,
126 		    bufsize);
127 		if (error) {
128 			aprint_error("%s: unable to read RDB @ %u, "
129 			    "error = %d\n", pdk->dk_name, nextb, error);
130 			error = ESRCH;
131 			goto done;
132 		}
133 		rbp = (struct rdblock *)bp->b_data;
134 		if (be32toh(rbp->id) == RDBLOCK_ID) {
135 			if (rdbchksum(rbp) == 0)
136 				break;
137 			else
138 				aprint_error("%s: RDB bad checksum @ %u\n",
139 				    pdk->dk_name, nextb);
140 		}
141 	}
142 
143 	if (nextb == RDB_MAXBLOCKS) {
144 		error = ESRCH;
145 		goto done;
146 	}
147 
148 	newsecsize = be32toh(rbp->nbytes);
149 	if (secsize != newsecsize) {
150 		aprint_verbose("secsize changed from %u to %u\n",
151 		    secsize, newsecsize);
152 		secsize = newsecsize;
153 		bufsize = roundup(MAX(sizeof(struct partblock),
154 		    sizeof(struct rdblock)), secsize);
155 		brelse(bp, 0);
156 		bp = geteblk(bufsize);
157 		goto retry;
158 	}
159 
160 	memset(&dkw, 0, sizeof(dkw));
161 
162 	strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
163 
164 	found = root = swap = false;
165 	/*
166 	 * scan for partition blocks
167 	 */
168 	for (nextb = be32toh(rbp->partbhead); nextb != RDBNULL;
169 	     nextb = be32toh(pbp->next)) {
170 		error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp->b_data,
171 		    bufsize);
172 		if (error) {
173 			aprint_error("%s: unable to read RDB partition block @ "
174 			    "%u, error = %d\n", pdk->dk_name, nextb, error);
175 			error = ESRCH;
176 			goto done;
177 		}
178 		pbp = (struct partblock *)bp->b_data;
179 
180 		if (be32toh(pbp->id) != PARTBLOCK_ID) {
181 			aprint_error(
182 			    "%s: RDB partition block @ %u bad id\n",
183 			    pdk->dk_name, nextb);
184 			error = ESRCH;
185 			goto done;
186 		}
187 		if (rdbchksum(pbp)) {
188 			aprint_error(
189 			    "%s: RDB partition block @ %u bad checksum\n",
190 			    pdk->dk_name, nextb);
191 			error = ESRCH;
192 			goto done;
193 		}
194 
195 		tabsize = be32toh(pbp->e.tabsize);
196 		if (tabsize < 11) {
197 			/*
198 			 * not enough info, too funky for us.
199 			 * I don't want to skip I want it fixed.
200 			 */
201 			aprint_error(
202 			    "%s: RDB partition block @ %u bad partition info "
203 			    "(environ < 11)\n",
204 			    pdk->dk_name, nextb);
205 			error = ESRCH;
206 			goto done;
207 		}
208 
209 		/*
210 		 * XXXX should be ">" however some vendors don't know
211 		 * what a table size is so, we hack for them.
212 		 * the other checks can fail for all I care but this
213 		 * is a very common value. *sigh*.
214 		 */
215 		if (tabsize >= 16)
216 			archtype = getarchtype(be32toh(pbp->e.dostype));
217 		else
218 			archtype = ADT_UNKNOWN;
219 
220 		switch (archtype) {
221 		case ADT_NETBSDROOT:
222 			if (root) {
223 				aprint_error("%s: more than one root RDB "
224 				    "partition, ignoring\n", pdk->dk_name);
225 				continue;
226 			}
227 			root = true;
228 			break;
229 		case ADT_NETBSDSWAP:
230 			if (swap) {
231 				aprint_error("%s: more than one swap RDB "
232 				    "partition , ignoring\n", pdk->dk_name);
233 				continue;
234 			}
235 			swap = true;
236 			break;
237 		default:
238 			break;
239 		}
240 
241 		if (pbp->partname[0] + 1 < sizeof(pbp->partname))
242 			pbp->partname[pbp->partname[0] + 1] = 0;
243 		else
244 			pbp->partname[sizeof(pbp->partname) - 1] = 0;
245 		strlcpy(dkw.dkw_wname, pbp->partname + 1,
246 		    sizeof(dkw.dkw_wname));
247 
248 		ptype = archtype_to_ptype(archtype);
249 		strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
250 
251 		blk_per_cyl =
252 		    be32toh(pbp->e.secpertrk) * be32toh(pbp->e.numheads)
253 		    * ((be32toh(pbp->e.sizeblock) << 2) / secsize);
254 		dkw.dkw_size = (uint64_t)(be32toh(pbp->e.highcyl)
255 		    - be32toh(pbp->e.lowcyl) + 1) * blk_per_cyl;
256 		dkw.dkw_offset = (daddr_t)be32toh(pbp->e.lowcyl) * blk_per_cyl;
257 
258 		error = dkwedge_add(&dkw);
259 		if (error == EEXIST) {
260 			aprint_error(
261 			    "%s: wedge named '%s' already exists, ignoring\n",
262 			    pdk->dk_name, dkw.dkw_wname);
263 		} else if (error)
264 			aprint_error("%s: error %d adding partition %s\n",
265 			    pdk->dk_name, error, dkw.dkw_wname);
266 		else
267 			found = true;
268 	}
269 
270 	if (found)
271 		error = 0;
272 	else
273 		error = ESRCH;
274 done:
275 	brelse(bp, 0);
276 	return error;
277 }
278 
279 static uint32_t
rdbchksum(void * bdata)280 rdbchksum(void *bdata)
281 {
282 	uint32_t *blp, cnt, val;
283 
284 	blp = bdata;
285 	cnt = be32toh(blp[1]);
286 	val = 0;
287 
288 	while (cnt--)
289 		val += be32toh(*blp++);
290 	return val;
291 }
292 
293 static uint8_t
getarchtype(uint32_t dostype)294 getarchtype(uint32_t dostype)
295 {
296 	uint32_t t3, b1;
297 
298 	t3 = dostype & 0xffffff00;
299 	b1 = dostype & 0x000000ff;
300 
301 	switch (t3) {
302 	case DOST_NBR:
303 		return ADT_NETBSDROOT;
304 	case DOST_NBS:
305 		return ADT_NETBSDSWAP;
306 	case DOST_NBU:
307 		return ADT_NETBSDUSER;
308 	case DOST_MUFS: /* check for 'muFS'? */
309 	case DOST_DOS:
310 		return ADT_AMIGADOS;
311 	case DOST_AMIX:
312 		return ADT_AMIX;
313 
314 	case DOST_XXXBSD:
315 #ifdef DIAGNOSTIC
316 		aprint_verbose("deprecated dostype found: 0x%x\n",
317 		    dostype);
318 #endif
319 		switch (b1) {
320 		case 'R':
321 			return ADT_NETBSDROOT;
322 		case 'S':
323 			return ADT_NETBSDSWAP;
324 		default:
325 			return ADT_NETBSDUSER;
326 		}
327 
328 	case DOST_EXT2:
329 		return ADT_EXT2;
330 	case DOST_RAID:
331 		return ADT_RAID;
332 	default:
333 #ifdef DIAGNOSTIC
334 		aprint_verbose("warning unknown dostype: 0x%x\n",
335 		    dostype);
336 #endif
337 		return ADT_UNKNOWN;
338 	}
339 }
340 
341 static const char *
archtype_to_ptype(unsigned char archtype)342 archtype_to_ptype(unsigned char archtype)
343 {
344 
345 	switch (archtype) {
346 	case ADT_NETBSDROOT:
347 	case ADT_NETBSDUSER:
348 		return DKW_PTYPE_FFS;
349 	case ADT_NETBSDSWAP:
350 		return DKW_PTYPE_SWAP;
351 	case ADT_AMIGADOS:
352 		return DKW_PTYPE_AMIGADOS;
353 	case ADT_EXT2:
354 		return DKW_PTYPE_EXT2FS;
355 	default:
356 		return DKW_PTYPE_UNKNOWN;
357 	}
358 }
359 
360 #ifdef _KERNEL
361 DKWEDGE_DISCOVERY_METHOD_DECL(RDB, 15, dkwedge_discover_rdb);
362 #endif
363