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