1 /* $NetBSD: dkwedge_rdb.c,v 1.4 2017/02/28 04:47:41 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.4 2017/02/28 04:47:41 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 #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 strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent)); 169 170 found = root = swap = false; 171 /* 172 * scan for partition blocks 173 */ 174 for (nextb = be32toh(rbp->partbhead); nextb != RDBNULL; 175 nextb = be32toh(pbp->next)) { 176 error = dkwedge_read(pdk, vp, ADJUST_NR(nextb), bp, bufsize); 177 if (error) { 178 aprint_error("%s: unable to read RDB partition block @ " 179 "%u, error = %d\n", pdk->dk_name, nextb, error); 180 error = ESRCH; 181 goto done; 182 } 183 pbp = (struct partblock *)bp; 184 185 if (be32toh(pbp->id) != PARTBLOCK_ID) { 186 aprint_error( 187 "%s: RDB partition block @ %u bad id\n", 188 pdk->dk_name, nextb); 189 error = ESRCH; 190 goto done; 191 } 192 if (rdbchksum(pbp)) { 193 aprint_error( 194 "%s: RDB partition block @ %u bad checksum\n", 195 pdk->dk_name, nextb); 196 error = ESRCH; 197 goto done; 198 } 199 200 tabsize = be32toh(pbp->e.tabsize); 201 if (tabsize < 11) { 202 /* 203 * not enough info, too funky for us. 204 * I don't want to skip I want it fixed. 205 */ 206 aprint_error( 207 "%s: RDB partition block @ %u bad partition info " 208 "(environ < 11)\n", 209 pdk->dk_name, nextb); 210 error = ESRCH; 211 goto done; 212 } 213 214 /* 215 * XXXX should be ">" however some vendors don't know 216 * what a table size is so, we hack for them. 217 * the other checks can fail for all I care but this 218 * is a very common value. *sigh*. 219 */ 220 if (tabsize >= 16) 221 archtype = getarchtype(be32toh(pbp->e.dostype)); 222 else 223 archtype = ADT_UNKNOWN; 224 225 switch (archtype) { 226 case ADT_NETBSDROOT: 227 if (root) { 228 aprint_error("%s: more than one root RDB " 229 "partition, ignoring\n", pdk->dk_name); 230 continue; 231 } 232 root = true; 233 break; 234 case ADT_NETBSDSWAP: 235 if (swap) { 236 aprint_error("%s: more than one swap RDB " 237 "partition , ignoring\n", pdk->dk_name); 238 continue; 239 } 240 swap = true; 241 break; 242 default: 243 break; 244 } 245 246 if (pbp->partname[0] + 1 < sizeof(pbp->partname)) 247 pbp->partname[pbp->partname[0] + 1] = 0; 248 else 249 pbp->partname[sizeof(pbp->partname) - 1] = 0; 250 strlcpy(dkw.dkw_wname, pbp->partname + 1, 251 sizeof(dkw.dkw_wname)); 252 253 ptype = archtype_to_ptype(archtype); 254 strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype)); 255 256 blk_per_cyl = 257 be32toh(pbp->e.secpertrk) * be32toh(pbp->e.numheads) 258 * ((be32toh(pbp->e.sizeblock) << 2) / secsize); 259 dkw.dkw_size = (uint64_t)(be32toh(pbp->e.highcyl) 260 - be32toh(pbp->e.lowcyl) + 1) * blk_per_cyl; 261 dkw.dkw_offset = (daddr_t)be32toh(pbp->e.lowcyl) * blk_per_cyl; 262 263 error = dkwedge_add(&dkw); 264 if (error == EEXIST) { 265 aprint_error( 266 "%s: wedge named '%s' already exists, ignoring\n", 267 pdk->dk_name, dkw.dkw_wname); 268 } else if (error) 269 aprint_error("%s: error %d adding partition %s\n", 270 pdk->dk_name, error, dkw.dkw_wname); 271 else 272 found = true; 273 } 274 275 if (found) 276 error = 0; 277 else 278 error = ESRCH; 279 done: 280 DKW_FREE(bp); 281 return error; 282 } 283 284 static unsigned 285 rdbchksum(void *bdata) 286 { 287 unsigned *blp, cnt, val; 288 289 blp = bdata; 290 cnt = be32toh(blp[1]); 291 val = 0; 292 293 while (cnt--) 294 val += be32toh(*blp++); 295 return(val); 296 } 297 298 static unsigned char 299 getarchtype(unsigned dostype) 300 { 301 unsigned t3, b1; 302 303 t3 = dostype & 0xffffff00; 304 b1 = dostype & 0x000000ff; 305 306 switch (t3) { 307 case DOST_NBR: 308 return ADT_NETBSDROOT; 309 case DOST_NBS: 310 return ADT_NETBSDSWAP; 311 case DOST_NBU: 312 return ADT_NETBSDUSER; 313 case DOST_MUFS: /* check for 'muFS'? */ 314 case DOST_DOS: 315 return ADT_AMIGADOS; 316 case DOST_AMIX: 317 return ADT_AMIX; 318 319 case DOST_XXXBSD: 320 #ifdef DIAGNOSTIC 321 aprint_verbose("deprecated dostype found: 0x%x\n", 322 dostype); 323 #endif 324 switch (b1) { 325 case 'R': 326 return ADT_NETBSDROOT; 327 case 'S': 328 return ADT_NETBSDSWAP; 329 default: 330 return ADT_NETBSDUSER; 331 } 332 333 case DOST_EXT2: 334 return ADT_EXT2; 335 case DOST_RAID: 336 return ADT_RAID; 337 default: 338 #ifdef DIAGNOSTIC 339 aprint_verbose("warning unknown dostype: 0x%x\n", 340 dostype); 341 #endif 342 return ADT_UNKNOWN; 343 } 344 } 345 346 static const char * 347 archtype_to_ptype(unsigned char archtype) 348 { 349 switch (archtype) { 350 case ADT_NETBSDROOT: 351 case ADT_NETBSDUSER: 352 return DKW_PTYPE_FFS; 353 case ADT_NETBSDSWAP: 354 return DKW_PTYPE_SWAP; 355 case ADT_AMIGADOS: 356 return DKW_PTYPE_AMIGADOS; 357 case ADT_EXT2: 358 return DKW_PTYPE_EXT2FS; 359 default: 360 return DKW_PTYPE_UNKNOWN; 361 } 362 } 363 364 #ifdef _KERNEL 365 DKWEDGE_DISCOVERY_METHOD_DECL(RDB, 15, dkwedge_discover_rdb); 366 #endif 367