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