1 /* $NetBSD: disksubr.c,v 1.17 2002/02/19 17:09:50 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 36 */ 37 38 #include "opt_compat_netbsd.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/buf.h> 43 #include <sys/disklabel.h> 44 #include <sys/syslog.h> 45 #include <sys/disk.h> 46 47 /* get rid of DEV_BSIZE dependency */ 48 #define DEF_BSIZE DEV_BSIZE /* default sector size = 512 */ 49 50 static void parttbl_consistency_check(struct disklabel *, 51 struct dos_partition *); 52 53 54 /* 55 * Attempt to read a disk label from a device 56 * using the indicated strategy routine. 57 * The label must be partly set up before this: 58 * secpercyl, secsize and anything required for a block i/o read 59 * operation in the driver's strategy/start routines 60 * must be filled in before calling us. 61 * 62 * Returns null on success and an error string on failure. 63 */ 64 char * 65 readdisklabel(dev, strat, lp, osdep) 66 dev_t dev; 67 void (*strat) __P((struct buf *)); 68 struct disklabel *lp; 69 struct cpu_disklabel *osdep; 70 { 71 struct dos_partition *dp = 0; 72 struct dkbad *bdp = &osdep->bad; 73 struct buf *bp; 74 struct disklabel *dlp; 75 char *msg = NULL; 76 int i, labelsz; 77 78 if (osdep) 79 dp = osdep->dosparts; 80 /* minimal requirements for archtypal disk label */ 81 if (lp->d_secsize == 0) 82 lp->d_secsize = DEF_BSIZE; 83 if (lp->d_secperunit == 0) 84 lp->d_secperunit = 0x1fffffff; 85 if (lp->d_secpercyl == 0) 86 lp->d_secpercyl = 0x1fffffff; 87 lp->d_npartitions = RAW_PART + 1; 88 for (i = 0; i < RAW_PART; i++) { 89 lp->d_partitions[i].p_size = 0; 90 lp->d_partitions[i].p_offset = 0; 91 } 92 if (lp->d_partitions[0].p_size == 0) 93 lp->d_partitions[0].p_size = 0x1fffffff; 94 lp->d_partitions[0].p_offset = 0; 95 96 /* get a buffer and initialize it */ 97 bp = geteblk((int)lp->d_secsize); 98 bp->b_dev = dev; 99 100 /* read BSD disklabel first */ 101 bp->b_blkno = LABELSECTOR; 102 bp->b_cylinder = LABELSECTOR/lp->d_secpercyl; 103 labelsz = howmany(LABELOFFSET+sizeof(struct disklabel), lp->d_secsize) 104 * lp->d_secsize; 105 bp->b_bcount = labelsz; /* to support < 512B/sector disks */ 106 bp->b_flags |= B_READ; 107 (*strat)(bp); 108 109 /* if successful, locate disk label within block and validate */ 110 if (biowait(bp)) { 111 msg = "disk label I/O error"; 112 goto dodospart; 113 } 114 for (dlp = (struct disklabel *)bp->b_data; 115 dlp <= (struct disklabel *)(bp->b_data + labelsz - sizeof(*dlp)); 116 dlp = (struct disklabel *)((uint8_t *)dlp + sizeof(long))) { 117 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { 118 if (msg == NULL) 119 msg = "no disk label"; 120 } else if (dlp->d_npartitions > MAXPARTITIONS || 121 dkcksum(dlp) != 0) 122 msg = "disk label corrupted"; 123 else { 124 *lp = *dlp; 125 msg = NULL; 126 break; 127 } 128 } 129 130 dodospart: 131 /* next do the Human68k-style partition table */ 132 /* Human68k does not support > 2048B/sector devices (?) */ 133 if (lp->d_secsize >= 2048) { 134 if (msg) 135 goto done; 136 goto dobadsect; 137 } 138 bp->b_blkno = DOSPARTOFF * DEF_BSIZE / lp->d_secsize; 139 /* DOSPARTOFF in DEV_BSIZE unit */ 140 bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl; 141 labelsz = howmany(sizeof(struct cpu_disklabel), 142 lp->d_secsize) * lp->d_secsize; 143 bp->b_bcount = labelsz; /* to support < 512B/sector disks */ 144 bp->b_flags &= ~(B_DONE); 145 (*strat)(bp); 146 147 /* if successful, wander through Human68k partition table */ 148 if (biowait(bp)) 149 goto done; 150 if (strncmp(bp->b_data, "X68K", 4) != 0) { 151 /* Human68k-style partition table does not exist */ 152 if (msg) 153 goto done; 154 goto dobadsect; 155 } 156 157 /* XXX how do we check veracity/bounds of this? */ 158 if (dp) 159 memcpy(dp, bp->b_data + sizeof(*dp) /*DOSPARTOFF*/, 160 NDOSPART * sizeof(*dp)); 161 else 162 dp = (void*) (bp->b_data + sizeof(*dp) /*DOSPARTOFF*/); 163 164 /* if BSD disklabel does not exist, fall back to Human68k partition */ 165 if (msg != NULL) { 166 msg = NULL; 167 lp->d_bbsize = 8192; 168 lp->d_sbsize = 2048; 169 for (i = 0; i < NDOSPART; i++, dp++) 170 /* is this ours? */ 171 if (dp->dp_size) { 172 u_char fstype; 173 int part = i + (i < RAW_PART ? 0 : 1); 174 int start = dp->dp_start * 2; 175 int size = dp->dp_size * 2; 176 177 /* update disklabel with details */ 178 lp->d_partitions[part].p_size = size; 179 lp->d_partitions[part].p_offset = start; 180 /* get partition type */ 181 #ifndef COMPAT_10 182 if (dp->dp_flag == 1) 183 fstype = FS_UNUSED; 184 else 185 #endif 186 if (!memcmp(dp->dp_typname, "Human68k", 8)) 187 fstype = FS_MSDOS; 188 else if (!memcmp(dp->dp_typname, 189 "BSD ffs ", 8)) 190 fstype = FS_BSDFFS; 191 else if (!memcmp(dp->dp_typname, 192 "BSD lfs ", 8)) 193 fstype = FS_BSDLFS; 194 else if (!memcmp(dp->dp_typname, 195 "BSD swap", 8)) 196 fstype = FS_SWAP; 197 #ifndef COMPAT_14 198 else if (part == 1) 199 fstype = FS_SWAP; 200 #endif 201 else 202 fstype = FS_BSDFFS; /* XXX */ 203 lp->d_partitions[part].p_fstype = fstype; /* XXX */ 204 if (lp->d_npartitions <= part) 205 lp->d_npartitions = part + 1; 206 } 207 } else { 208 parttbl_consistency_check(lp, dp); 209 } 210 211 dobadsect: 212 /* obtain bad sector table if requested and present */ 213 if (bdp && (lp->d_flags & D_BADSECT)) { 214 struct dkbad *db; 215 216 i = 0; 217 do { 218 /* read a bad sector table */ 219 bp->b_flags &= ~(B_DONE); 220 bp->b_flags |= B_READ; 221 bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i; 222 if (lp->d_secsize > DEF_BSIZE) 223 bp->b_blkno *= lp->d_secsize / DEF_BSIZE; 224 else 225 bp->b_blkno /= DEF_BSIZE / lp->d_secsize; 226 bp->b_bcount = lp->d_secsize; 227 bp->b_cylinder = lp->d_ncylinders - 1; 228 (*strat)(bp); 229 230 /* if successful, validate, otherwise try another */ 231 if (biowait(bp)) { 232 msg = "bad sector table I/O error"; 233 } else { 234 db = (struct dkbad *)(bp->b_data); 235 #define DKBAD_MAGIC 0x4321 236 if (db->bt_mbz == 0 237 && db->bt_flag == DKBAD_MAGIC) { 238 msg = NULL; 239 *bdp = *db; 240 break; 241 } else 242 msg = "bad sector table corrupted"; 243 } 244 } while ((bp->b_flags & B_ERROR) && (i += 2) < 10 && 245 i < lp->d_nsectors); 246 } 247 248 done: 249 brelse(bp); 250 return (msg); 251 } 252 253 /* 254 * Check new disk label for sensibility 255 * before setting it. 256 */ 257 int 258 setdisklabel(olp, nlp, openmask, osdep) 259 struct disklabel *olp, *nlp; 260 u_long openmask; 261 struct cpu_disklabel *osdep; 262 { 263 int i; 264 struct partition *opp, *npp; 265 266 /* sanity clause */ 267 if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0 268 /*|| (nlp->d_secsize % DEV_BSIZE) != 0*/) 269 return(EINVAL); 270 271 /* special case to allow disklabel to be invalidated */ 272 if (nlp->d_magic == 0xffffffff) { 273 *olp = *nlp; 274 return (0); 275 } 276 277 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC || 278 dkcksum(nlp) != 0) 279 return (EINVAL); 280 281 if (osdep) 282 parttbl_consistency_check(nlp, osdep->dosparts); 283 /* XXX missing check if other dos partitions will be overwritten */ 284 285 while (openmask != 0) { 286 i = ffs(openmask) - 1; 287 openmask &= ~(1 << i); 288 if (nlp->d_npartitions <= i) 289 return (EBUSY); 290 opp = &olp->d_partitions[i]; 291 npp = &nlp->d_partitions[i]; 292 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size) 293 return (EBUSY); 294 /* 295 * Copy internally-set partition information 296 * if new label doesn't include it. XXX 297 */ 298 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) { 299 npp->p_fstype = opp->p_fstype; 300 npp->p_fsize = opp->p_fsize; 301 npp->p_frag = opp->p_frag; 302 npp->p_cpg = opp->p_cpg; 303 } 304 } 305 nlp->d_checksum = 0; 306 nlp->d_checksum = dkcksum(nlp); 307 *olp = *nlp; 308 return (0); 309 } 310 311 /* 312 * Write disk label back to device after modification. 313 */ 314 int 315 writedisklabel(dev, strat, lp, osdep) 316 dev_t dev; 317 void (*strat) __P((struct buf *)); 318 struct disklabel *lp; 319 struct cpu_disklabel *osdep; 320 { 321 struct dos_partition *dp = 0; 322 struct buf *bp; 323 struct disklabel *dlp; 324 int error, labelsz, i; 325 char *np; 326 327 if (osdep) 328 dp = osdep->dosparts; 329 /* sanity clause */ 330 if (lp->d_secpercyl == 0 || lp->d_secsize == 0 331 /*|| (lp->d_secsize % DEF_BSIZE) != 0*/) 332 return(EINVAL); 333 if (dp) 334 parttbl_consistency_check(lp, dp); 335 336 /* get a buffer and initialize it */ 337 bp = geteblk((int)lp->d_secsize); 338 bp->b_dev = dev; 339 340 /* attempt to write BSD disklabel first */ 341 bp->b_blkno = LABELSECTOR; 342 bp->b_cylinder = LABELSECTOR / lp->d_secpercyl; 343 labelsz = howmany(LABELOFFSET+sizeof(struct disklabel), lp->d_secsize) 344 * lp->d_secsize; 345 bp->b_bcount = labelsz; /* to support < 512B/sector disks */ 346 bp->b_flags |= B_READ; 347 (*strat)(bp); 348 349 /* if successful, locate disk label within block and validate */ 350 if (biowait(bp)) 351 goto dodospart; 352 error = ESRCH; 353 for (dlp = (struct disklabel *)bp->b_data; 354 dlp <= (struct disklabel *)(bp->b_data + labelsz - sizeof(*dlp)); 355 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { 356 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC && 357 dkcksum(dlp) == 0) { 358 *dlp = *lp; 359 bp->b_flags &= ~(B_READ|B_DONE); 360 bp->b_flags |= B_WRITE; 361 (*strat)(bp); 362 error = biowait(bp); 363 break; 364 } 365 } 366 367 dodospart: 368 /* do dos partitions in the process of getting disklabel? */ 369 if (error) { 370 if (lp->d_secsize >= 2048) { 371 error = ESRCH; 372 goto done; 373 } 374 #if 0 /* there is no mark on floppies */ 375 /* read the x68k disk magic */ 376 bp->b_blkno = DOSBBSECTOR; 377 bp->b_bcount = lp->d_secsize; 378 bp->b_flags &= ~(B_WRITE|B_DONE); 379 bp->b_flags |= B_READ; 380 bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl; 381 (*strat)(bp); 382 if ((error = biowait(bp)) || memcmp(bp->b_data, "X68SCSI1", 8)) 383 printf("warning: disk not marked for x68k"); 384 #endif 385 386 /* read the partition table */ 387 bp->b_blkno = DOSPARTOFF; 388 labelsz = howmany(sizeof(struct cpu_disklabel), 389 lp->d_secsize) * lp->d_secsize; 390 bp->b_bcount = labelsz; 391 bp->b_flags &= ~(B_WRITE|B_DONE); 392 bp->b_flags |= B_READ; 393 bp->b_cylinder = DOSPARTOFF / lp->d_secpercyl; 394 (*strat)(bp); 395 396 if ((error = biowait(bp)) == 0) { 397 /* XXX how do we check veracity/bounds of this? */ 398 dp = (struct dos_partition *)bp->b_data + 1; 399 for (i = 0; i < NDOSPART; i++, dp++) { 400 int part = i + (i < RAW_PART ? 0 : 1); 401 int start, size; 402 403 start = lp->d_partitions[part].p_offset >> 1; 404 size = lp->d_partitions[part].p_size >> 1; 405 406 switch (lp->d_partitions[part].p_fstype) { 407 case FS_MSDOS: 408 np = "Human68k"; 409 dp->dp_flag = 0; /* autoboot */ 410 break; 411 412 case FS_SWAP: 413 np = "BSD swap"; 414 dp->dp_flag = 2; /* in use */ 415 break; 416 417 case FS_BSDFFS: 418 np = "BSD ffs "; 419 if (part == 0) 420 dp->dp_flag = 0; /* autoboot */ 421 else 422 dp->dp_flag = 2; /* in use */ 423 break; 424 425 case FS_BSDLFS: 426 np = "BSD lfs "; 427 if (part == 0) 428 dp->dp_flag = 0; /* autoboot */ 429 else 430 dp->dp_flag = 2; /* in use */ 431 break; 432 433 case FS_UNUSED: 434 np = "\0\0\0\0\0\0\0\0"; 435 start = size = 0; 436 if (part < lp->d_npartitions) { 437 dp->dp_flag = 1; 438 } else { 439 dp->dp_flag = 0; 440 } 441 break; 442 443 default: 444 /* XXX OS-9, MINIX etc. */ 445 continue; 446 } 447 memcpy(dp->dp_typname, np, 8); 448 dp->dp_start = start; 449 dp->dp_size = size; 450 } 451 bp->b_flags &= ~(B_READ|B_DONE); 452 bp->b_flags |= B_WRITE; 453 (*strat)(bp); 454 error = biowait(bp); 455 } 456 } 457 458 #ifdef maybe 459 /* disklabel in appropriate location? */ 460 if (lp->d_partitions[0].p_offset != 0 461 && lp->d_partitions[0].p_offset != dospartoff) { 462 error = EXDEV; 463 goto done; 464 } 465 #endif 466 467 done: 468 brelse(bp); 469 return (error); 470 } 471 472 /* 473 * Determine the size of the transfer, and make sure it is 474 * within the boundaries of the partition. Adjust transfer 475 * if needed, and signal errors or early completion. 476 */ 477 int 478 bounds_check_with_label(bp, lp, wlabel) 479 struct buf *bp; 480 struct disklabel *lp; 481 int wlabel; 482 { 483 struct partition *p = lp->d_partitions + DISKPART(bp->b_dev); 484 int labelsector = lp->d_partitions[RAW_PART].p_offset + LABELSECTOR; 485 int sz; 486 487 sz = howmany(bp->b_bcount, lp->d_secsize); 488 489 if (bp->b_blkno + sz > p->p_size) { 490 sz = p->p_size - bp->b_blkno; 491 if (sz == 0) { 492 /* If exactly at end of disk, return EOF. */ 493 bp->b_resid = bp->b_bcount; 494 goto done; 495 } 496 if (sz < 0) { 497 /* If past end of disk, return EINVAL. */ 498 bp->b_error = EINVAL; 499 goto bad; 500 } 501 /* Otherwise, truncate request. */ 502 bp->b_bcount = sz << DEV_BSHIFT; 503 } 504 505 /* Overwriting disk label? */ 506 if (bp->b_blkno + p->p_offset <= labelsector && 507 #if LABELSECTOR != 0 508 bp->b_blkno + p->p_offset + sz > labelsector && 509 #endif 510 (bp->b_flags & B_READ) == 0 && !wlabel) { 511 bp->b_error = EROFS; 512 goto bad; 513 } 514 515 /* calculate cylinder for disksort to order transfers with */ 516 bp->b_cylinder = (bp->b_blkno + p->p_offset) / 517 (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl; 518 return (1); 519 520 bad: 521 bp->b_flags |= B_ERROR; 522 done: 523 return (0); 524 } 525 526 static void 527 parttbl_consistency_check(lp, dp) 528 struct disklabel *lp; 529 struct dos_partition *dp; 530 { 531 int i, j; 532 int f = (lp->d_secsize >= 1024) ? lp->d_secsize/1024 : 1; 533 int g = (lp->d_secsize >= 1024) ? 1 : 1024/lp->d_secsize; 534 535 /* 1. overlapping check on partition table */ 536 for (i = 0; i < NDOSPART; i++) { 537 if (dp[i].dp_size == 0) 538 continue; 539 for (j = i+1; j < NDOSPART; j++) { 540 if (dp[j].dp_size == 0) 541 continue; 542 if (((dp[i].dp_start <= dp[j].dp_start) && 543 (dp[i].dp_start + dp[i].dp_size > dp[j].dp_start))|| 544 ((dp[j].dp_start <= dp[i].dp_start) && 545 (dp[j].dp_start + dp[j].dp_size > dp[i].dp_start))) { 546 printf("warning: Human68k partition %d and %d" 547 " are overlapping\n", i+1, j+1); 548 return; 549 } 550 } 551 } 552 553 /* 2. scan disklabel partitions */ 554 #define bp lp->d_partitions 555 for (i = 0; i < lp->d_npartitions; i++) { 556 int c = 0; 557 558 if (lp->d_partitions[i].p_fstype == FS_UNUSED || 559 lp->d_partitions[i].p_size == 0) 560 continue; 561 for (j = 0; j < NDOSPART; j++) { 562 if (dp[j].dp_size == 0) 563 continue; 564 if ((bp[i].p_offset * f < (dp[j].dp_start + dp[j].dp_size) * g) && 565 ((bp[i].p_offset + bp[i].p_size) * f >= (dp[j].dp_start + dp[j].dp_size) * g)) 566 c++; 567 if ((bp[i].p_offset * f > dp[j].dp_start * g) && 568 ((bp[i].p_offset + bp[i].p_size) * f < (dp[j].dp_start + dp[j].dp_size) * g)) 569 c++; 570 if ((bp[i].p_offset * f >= dp[j].dp_start * g) && 571 ((bp[i].p_offset + bp[i].p_size) * f < dp[j].dp_start * g)) 572 c++; 573 } 574 if (c > 1) 575 printf ("warning: partition %c spans for 2 or more" 576 " partitions in Human68k partition table.\n", 577 i+'a'); 578 } 579 #undef bp 580 581 /* more checks? */ 582 } 583