1 /*- 2 * Copyright (c) 1994 Bruce D. Evans. 3 * All rights reserved. 4 * 5 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 37 * from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $ 38 * $FreeBSD: src/sys/kern/subr_diskmbr.c,v 1.45 2000/01/28 10:22:07 bde Exp $ 39 * $DragonFly: src/sys/kern/subr_diskmbr.c,v 1.8 2004/06/02 19:31:02 dillon Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/buf.h> 45 #include <sys/conf.h> 46 #ifdef PC98 47 #define PC98_ATCOMPAT 48 #define dsinit atcompat_dsinit 49 #endif 50 #include <sys/disklabel.h> 51 #define DOSPTYP_EXTENDED 5 52 #define DOSPTYP_EXTENDEDX 15 53 #define DOSPTYP_ONTRACK 84 54 #include <sys/diskslice.h> 55 #include <sys/diskmbr.h> 56 #include <sys/malloc.h> 57 #include <sys/syslog.h> 58 #include <sys/device.h> 59 60 #define TRACE(str) do { if (dsi_debug) printf str; } while (0) 61 62 static volatile u_char dsi_debug; 63 64 static struct dos_partition historical_bogus_partition_table[NDOSPART] = { 65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 66 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 67 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 68 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, }, 69 }; 70 71 static int check_part (char *sname, struct dos_partition *dp, 72 u_long offset, int nsectors, int ntracks, 73 u_long mbr_offset); 74 static void mbr_extended (dev_t dev, struct disklabel *lp, 75 struct diskslices *ssp, u_long ext_offset, 76 u_long ext_size, u_long base_ext_offset, 77 int nsectors, int ntracks, u_long mbr_offset, 78 int level); 79 static int mbr_setslice (char *sname, struct disklabel *lp, 80 struct diskslice *sp, struct dos_partition *dp, 81 u_long br_offset); 82 83 static int 84 check_part(char *sname, struct dos_partition *dp, u_long offset, 85 int nsectors, int ntracks, u_long mbr_offset) 86 { 87 int chs_ecyl; 88 int chs_esect; 89 int chs_scyl; 90 int chs_ssect; 91 int error; 92 u_long esector; 93 u_long esector1; 94 u_long secpercyl; 95 u_long ssector; 96 u_long ssector1; 97 98 secpercyl = (u_long)nsectors * ntracks; 99 chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect); 100 chs_ssect = DPSECT(dp->dp_ssect); 101 ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl 102 + mbr_offset; 103 ssector1 = offset + dp->dp_start; 104 105 /* 106 * If ssector1 is on a cylinder >= 1024, then ssector can't be right. 107 * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct 108 * apart from the cylinder being reduced modulo 1024. Always allow 109 * 1023/255/63. 110 */ 111 if ((ssector < ssector1 112 && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1 113 && chs_scyl == 1023) 114 || (secpercyl != 0 115 && (ssector1 - ssector) % (1024 * secpercyl) == 0))) 116 || (dp->dp_scyl == 255 && dp->dp_shd == 255 117 && dp->dp_ssect == 255)) { 118 TRACE(("%s: C/H/S start %d/%d/%d, start %lu: allow\n", 119 sname, chs_scyl, dp->dp_shd, chs_ssect, ssector1)); 120 ssector = ssector1; 121 } 122 123 chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect); 124 chs_esect = DPSECT(dp->dp_esect); 125 esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl 126 + mbr_offset; 127 esector1 = ssector1 + dp->dp_size - 1; 128 129 /* Allow certain bogus C/H/S values for esector, as above. */ 130 if ((esector < esector1 131 && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1 132 && chs_ecyl == 1023) 133 || (secpercyl != 0 134 && (esector1 - esector) % (1024 * secpercyl) == 0))) 135 || (dp->dp_ecyl == 255 && dp->dp_ehd == 255 136 && dp->dp_esect == 255)) { 137 TRACE(("%s: C/H/S end %d/%d/%d, end %lu: allow\n", 138 sname, chs_ecyl, dp->dp_ehd, chs_esect, esector1)); 139 esector = esector1; 140 } 141 142 error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL; 143 if (bootverbose) 144 printf("%s: type 0x%x, start %lu, end = %lu, size %lu %s\n", 145 sname, dp->dp_typ, ssector1, esector1, 146 (u_long)dp->dp_size, error ? "" : ": OK"); 147 if (ssector != ssector1 && bootverbose) 148 printf("%s: C/H/S start %d/%d/%d (%lu) != start %lu: invalid\n", 149 sname, chs_scyl, dp->dp_shd, chs_ssect, 150 ssector, ssector1); 151 if (esector != esector1 && bootverbose) 152 printf("%s: C/H/S end %d/%d/%d (%lu) != end %lu: invalid\n", 153 sname, chs_ecyl, dp->dp_ehd, chs_esect, 154 esector, esector1); 155 return (error); 156 } 157 158 int 159 dsinit(dev_t dev, struct disklabel *lp, struct diskslices **sspp) 160 { 161 struct buf *bp; 162 u_char *cp; 163 int dospart; 164 struct dos_partition *dp; 165 struct dos_partition *dp0; 166 struct dos_partition dpcopy[NDOSPART]; 167 int error; 168 int max_ncyls; 169 int max_nsectors; 170 int max_ntracks; 171 u_long mbr_offset; 172 char partname[2]; 173 u_long secpercyl; 174 char *sname; 175 struct diskslice *sp; 176 struct diskslices *ssp; 177 dev_t wdev; 178 179 mbr_offset = DOSBBSECTOR; 180 reread_mbr: 181 /* Read master boot record. */ 182 wdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART); 183 bp = geteblk((int)lp->d_secsize); 184 bp->b_dev = wdev; 185 bp->b_blkno = mbr_offset; 186 bp->b_bcount = lp->d_secsize; 187 bp->b_flags |= B_READ; 188 BUF_STRATEGY(bp, 1); 189 if (biowait(bp) != 0) { 190 diskerr(bp, wdev, "reading primary partition table: error", 191 LOG_PRINTF, 0, (struct disklabel *)NULL); 192 printf("\n"); 193 error = EIO; 194 goto done; 195 } 196 197 /* Weakly verify it. */ 198 cp = bp->b_data; 199 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, partname); 200 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) { 201 if (bootverbose) 202 printf("%s: invalid primary partition table: no magic\n", 203 sname); 204 error = EINVAL; 205 goto done; 206 } 207 208 /* Make a copy of the partition table to avoid alignment problems. */ 209 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy)); 210 211 dp0 = &dpcopy[0]; 212 213 /* Check for "Ontrack Diskmanager". */ 214 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) { 215 if (dp->dp_typ == DOSPTYP_ONTRACK) { 216 if (bootverbose) 217 printf( 218 "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname); 219 bp->b_flags |= B_INVAL | B_AGE; 220 brelse(bp); 221 mbr_offset = 63; 222 goto reread_mbr; 223 } 224 } 225 226 if (bcmp(dp0, historical_bogus_partition_table, 227 sizeof historical_bogus_partition_table) == 0) { 228 TRACE(("%s: invalid primary partition table: historical\n", 229 sname)); 230 error = EINVAL; 231 goto done; 232 } 233 234 /* Guess the geometry. */ 235 /* 236 * TODO: 237 * Perhaps skip entries with 0 size. 238 * Perhaps only look at entries of type DOSPTYP_386BSD. 239 */ 240 max_ncyls = 0; 241 max_nsectors = 0; 242 max_ntracks = 0; 243 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) { 244 int ncyls; 245 int nsectors; 246 int ntracks; 247 248 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1; 249 if (max_ncyls < ncyls) 250 max_ncyls = ncyls; 251 nsectors = DPSECT(dp->dp_esect); 252 if (max_nsectors < nsectors) 253 max_nsectors = nsectors; 254 ntracks = dp->dp_ehd + 1; 255 if (max_ntracks < ntracks) 256 max_ntracks = ntracks; 257 } 258 259 /* 260 * Check that we have guessed the geometry right by checking the 261 * partition entries. 262 */ 263 /* 264 * TODO: 265 * As above. 266 * Check for overlaps. 267 * Check against d_secperunit if the latter is reliable. 268 */ 269 error = 0; 270 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) { 271 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0 272 && dp->dp_start == 0 && dp->dp_size == 0) 273 continue; 274 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart, 275 RAW_PART, partname); 276 277 /* 278 * Temporarily ignore errors from this check. We could 279 * simplify things by accepting the table eariler if we 280 * always ignore errors here. Perhaps we should always 281 * accept the table if the magic is right but not let 282 * bad entries affect the geometry. 283 */ 284 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks, 285 mbr_offset); 286 } 287 if (error != 0) 288 goto done; 289 290 /* 291 * Accept the DOS partition table. 292 * First adjust the label (we have been careful not to change it 293 * before we can guarantee success). 294 */ 295 secpercyl = (u_long)max_nsectors * max_ntracks; 296 if (secpercyl != 0) { 297 #if 0 298 u_long secperunit; 299 #endif 300 301 lp->d_nsectors = max_nsectors; 302 lp->d_ntracks = max_ntracks; 303 lp->d_secpercyl = secpercyl; 304 /* 305 * Temporarily, don't even consider adjusting the drive's 306 * size, since the adjusted size may exceed the hardware's 307 * addressing capabilities. The adjustment helped mainly 308 * for ancient MFM drives with > 1024 cylinders, but now 309 * breaks at least IDE drives with 63*16*65536 sectors if 310 * they are controlled by the wd driver in CHS mode. 311 */ 312 #if 0 313 secperunit = secpercyl * max_ncyls; 314 if (lp->d_secperunit < secperunit) 315 lp->d_secperunit = secperunit; 316 #endif 317 lp->d_ncylinders = lp->d_secperunit / secpercyl; 318 } 319 320 /* 321 * We are passed a pointer to a suitably initialized minimal 322 * slices "struct" with no dangling pointers in it. Replace it 323 * by a maximal one. This usually oversizes the "struct", but 324 * enlarging it while searching for logical drives would be 325 * inconvenient. 326 */ 327 free(*sspp, M_DEVBUF); 328 ssp = dsmakeslicestruct(MAX_SLICES, lp); 329 *sspp = ssp; 330 331 /* Initialize normal slices. */ 332 sp = &ssp->dss_slices[BASE_SLICE]; 333 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) { 334 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart, 335 RAW_PART, partname); 336 (void)mbr_setslice(sname, lp, sp, dp, mbr_offset); 337 } 338 ssp->dss_nslices = BASE_SLICE + NDOSPART; 339 340 /* Handle extended partitions. */ 341 sp -= NDOSPART; 342 for (dospart = 0; dospart < NDOSPART; dospart++, sp++) { 343 if (sp->ds_type == DOSPTYP_EXTENDED || 344 sp->ds_type == DOSPTYP_EXTENDEDX) { 345 mbr_extended(wdev, lp, ssp, 346 sp->ds_offset, sp->ds_size, sp->ds_offset, 347 max_nsectors, max_ntracks, mbr_offset, 1); 348 } 349 } 350 351 /* 352 * mbr_extended() abuses ssp->dss_nslices for the number of slices 353 * that would be found if there were no limit on the number of slices 354 * in *ssp. Cut it back now. 355 */ 356 if (ssp->dss_nslices > MAX_SLICES) 357 ssp->dss_nslices = MAX_SLICES; 358 359 done: 360 bp->b_flags |= B_INVAL | B_AGE; 361 brelse(bp); 362 if (error == EINVAL) 363 error = 0; 364 return (error); 365 } 366 367 void 368 mbr_extended(dev_t dev, struct disklabel *lp, struct diskslices *ssp, 369 u_long ext_offset, u_long ext_size, u_long base_ext_offset, 370 int nsectors, int ntracks, u_long mbr_offset, int level) 371 { 372 struct buf *bp; 373 u_char *cp; 374 int dospart; 375 struct dos_partition *dp; 376 struct dos_partition dpcopy[NDOSPART]; 377 u_long ext_offsets[NDOSPART]; 378 u_long ext_sizes[NDOSPART]; 379 char partname[2]; 380 int slice; 381 char *sname; 382 struct diskslice *sp; 383 384 if (level >= 16) { 385 printf( 386 "%s: excessive recursion in search for slices; aborting search\n", 387 devtoname(dev)); 388 return; 389 } 390 391 /* Read extended boot record. */ 392 bp = geteblk((int)lp->d_secsize); 393 bp->b_dev = dev; 394 bp->b_blkno = ext_offset; 395 bp->b_bcount = lp->d_secsize; 396 bp->b_flags |= B_READ; 397 BUF_STRATEGY(bp, 1); 398 if (biowait(bp) != 0) { 399 diskerr(bp, dev, "reading extended partition table: error", 400 LOG_PRINTF, 0, (struct disklabel *)NULL); 401 printf("\n"); 402 goto done; 403 } 404 405 /* Weakly verify it. */ 406 cp = bp->b_data; 407 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) { 408 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, 409 partname); 410 if (bootverbose) 411 printf("%s: invalid extended partition table: no magic\n", 412 sname); 413 goto done; 414 } 415 416 /* Make a copy of the partition table to avoid alignment problems. */ 417 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy)); 418 419 slice = ssp->dss_nslices; 420 for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART; 421 dospart++, dp++) { 422 ext_sizes[dospart] = 0; 423 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0 424 && dp->dp_start == 0 && dp->dp_size == 0) 425 continue; 426 if (dp->dp_typ == DOSPTYP_EXTENDED || 427 dp->dp_typ == DOSPTYP_EXTENDEDX) { 428 static char buf[32]; 429 430 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, 431 RAW_PART, partname); 432 snprintf(buf, sizeof(buf), "%s", sname); 433 if (strlen(buf) < sizeof buf - 11) 434 strcat(buf, "<extended>"); 435 check_part(buf, dp, base_ext_offset, nsectors, 436 ntracks, mbr_offset); 437 ext_offsets[dospart] = base_ext_offset + dp->dp_start; 438 ext_sizes[dospart] = dp->dp_size; 439 } else { 440 sname = dsname(dev, dkunit(dev), slice, RAW_PART, 441 partname); 442 check_part(sname, dp, ext_offset, nsectors, ntracks, 443 mbr_offset); 444 if (slice >= MAX_SLICES) { 445 printf("%s: too many slices\n", sname); 446 slice++; 447 continue; 448 } 449 sp = &ssp->dss_slices[slice]; 450 if (mbr_setslice(sname, lp, sp, dp, ext_offset) != 0) 451 continue; 452 slice++; 453 } 454 } 455 ssp->dss_nslices = slice; 456 457 /* If we found any more slices, recursively find all the subslices. */ 458 for (dospart = 0; dospart < NDOSPART; dospart++) { 459 if (ext_sizes[dospart] != 0) { 460 mbr_extended(dev, lp, ssp, ext_offsets[dospart], 461 ext_sizes[dospart], base_ext_offset, 462 nsectors, ntracks, mbr_offset, ++level); 463 } 464 } 465 466 done: 467 bp->b_flags |= B_INVAL | B_AGE; 468 brelse(bp); 469 } 470 471 static int 472 mbr_setslice(char *sname, struct disklabel *lp, struct diskslice *sp, 473 struct dos_partition *dp, u_long br_offset) 474 { 475 u_long offset; 476 u_long size; 477 478 offset = br_offset + dp->dp_start; 479 if (offset > lp->d_secperunit || offset < br_offset) { 480 printf( 481 "%s: slice starts beyond end of the disk: rejecting it\n", 482 sname); 483 return (1); 484 } 485 size = lp->d_secperunit - offset; 486 if (size >= dp->dp_size) 487 size = dp->dp_size; 488 else 489 printf( 490 "%s: slice extends beyond end of disk: truncating from %lu to %lu sectors\n", 491 sname, (u_long)dp->dp_size, size); 492 sp->ds_offset = offset; 493 sp->ds_size = size; 494 sp->ds_type = dp->dp_typ; 495 #ifdef PC98_ATCOMPAT 496 /* Fake FreeBSD(98). */ 497 if (sp->ds_type == DOSPTYP_386BSD) 498 sp->ds_type = 0x94; 499 #endif 500 #if 0 501 lp->d_subtype |= (lp->d_subtype & 3) | dospart | DSTYPE_INDOSPART; 502 #endif 503 return (0); 504 } 505 506 #ifdef __alpha__ 507 void 508 alpha_fix_srm_checksum(struct buf *bp) 509 { 510 u_int64_t *p; 511 u_int64_t sum; 512 int i; 513 514 p = (u_int64_t *) bp->b_data; 515 sum = 0; 516 for (i = 0; i < 63; i++) 517 sum += p[i]; 518 p[63] = sum; 519 } 520 #endif 521