2 * Copyright (c) 1994 Bruce D. Evans.
5 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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.18 2006/12/23 00:35:04 swildner Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/disklabel.h>
47 #define DOSPTYP_EXTENDED 5
48 #define DOSPTYP_EXTENDEDX 15
49 #define DOSPTYP_ONTRACK 84
50 #include <sys/diskslice.h>
51 #include <sys/diskmbr.h>
52 #include <sys/malloc.h>
53 #include <sys/syslog.h>
54 #include <sys/device.h>
56 #define TRACE(str) do { if (dsi_debug) kprintf str; } while (0)
58 static volatile u_char dsi_debug;
61 * This is what we have embedded in every boot1 for supporting the bogus
62 * "Dangerously Dedicated" mode. However, the old table is broken because
63 * it has an illegal geometry in it - it specifies 256 heads (heads = end
64 * head + 1) which causes nasty stuff when that wraps to zero in bios code.
65 * eg: divide by zero etc. This caused the dead-thinkpad problem, numerous
66 * SCSI bios crashes, EFI to crash, etc.
68 * We still have to recognize the old table though, even though we stopped
69 * inflicting it upon the world.
71 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
72 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
73 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
74 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
75 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
77 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
78 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
79 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
80 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
81 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, },
84 static int check_part (char *sname, struct dos_partition *dp,
85 u_long offset, int nsectors, int ntracks,
87 static void mbr_extended (cdev_t dev, struct disklabel *lp,
88 struct diskslices *ssp, u_long ext_offset,
89 u_long ext_size, u_long base_ext_offset,
90 int nsectors, int ntracks, u_long mbr_offset,
92 static int mbr_setslice (char *sname, struct disklabel *lp,
93 struct diskslice *sp, struct dos_partition *dp,
97 check_part(char *sname, struct dos_partition *dp, u_long offset,
98 int nsectors, int ntracks, u_long mbr_offset)
111 secpercyl = (u_long)nsectors * ntracks;
112 chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
113 chs_ssect = DPSECT(dp->dp_ssect);
114 ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl
116 ssector1 = offset + dp->dp_start;
119 * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
120 * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
121 * apart from the cylinder being reduced modulo 1024. Always allow
122 * 1023/255/63, because this is the official way to represent
123 * pure-LBA for the starting position.
125 if ((ssector < ssector1
126 && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
129 && (ssector1 - ssector) % (1024 * secpercyl) == 0)))
130 || (dp->dp_scyl == 255 && dp->dp_shd == 255
131 && dp->dp_ssect == 255)) {
132 TRACE(("%s: C/H/S start %d/%d/%d, start %lu: allow\n",
133 sname, chs_scyl, dp->dp_shd, chs_ssect, ssector1));
137 chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect);
138 chs_esect = DPSECT(dp->dp_esect);
139 esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl
141 esector1 = ssector1 + dp->dp_size - 1;
144 * Allow certain bogus C/H/S values for esector, as above. However,
145 * heads == 255 isn't really legal and causes some BIOS crashes. The
146 * correct value to indicate a pure-LBA end is 1023/heads-1/sectors -
147 * usually 1023/254/63. "heads" is base 0, "sectors" is base 1.
149 if ((esector < esector1
150 && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
153 && (esector1 - esector) % (1024 * secpercyl) == 0)))
154 || (dp->dp_ecyl == 255 && dp->dp_ehd == 255
155 && dp->dp_esect == 255)) {
156 TRACE(("%s: C/H/S end %d/%d/%d, end %lu: allow\n",
157 sname, chs_ecyl, dp->dp_ehd, chs_esect, esector1));
161 error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
163 kprintf("%s: type 0x%x, start %lu, end = %lu, size %lu %s\n",
164 sname, dp->dp_typ, ssector1, esector1,
165 (u_long)dp->dp_size, error ? "" : ": OK");
166 if (ssector != ssector1 && bootverbose)
167 kprintf("%s: C/H/S start %d/%d/%d (%lu) != start %lu: invalid\n",
168 sname, chs_scyl, dp->dp_shd, chs_ssect,
170 if (esector != esector1 && bootverbose)
171 kprintf("%s: C/H/S end %d/%d/%d (%lu) != end %lu: invalid\n",
172 sname, chs_ecyl, dp->dp_ehd, chs_esect,
178 dsinit(cdev_t dev, struct disklabel *lp, struct diskslices **sspp)
183 struct dos_partition *dp;
184 struct dos_partition *dp0;
185 struct dos_partition dpcopy[NDOSPART];
194 struct diskslice *sp;
195 struct diskslices *ssp;
198 mbr_offset = DOSBBSECTOR;
200 /* Read master boot record. */
201 wdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
202 bp = geteblk((int)lp->d_secsize);
203 bp->b_bio1.bio_offset = (off_t)mbr_offset * lp->d_secsize;
204 bp->b_bcount = lp->d_secsize;
205 bp->b_cmd = BUF_CMD_READ;
206 dev_dstrategy(wdev, &bp->b_bio1);
207 if (biowait(bp) != 0) {
208 diskerr(&bp->b_bio1, wdev,
209 "reading primary partition table: error",
210 LOG_PRINTF, 0, NULL);
216 /* Weakly verify it. */
218 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, partname);
219 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
221 kprintf("%s: invalid primary partition table: no magic\n",
227 /* Make a copy of the partition table to avoid alignment problems. */
228 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
232 /* Check for "Ontrack Diskmanager". */
233 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
234 if (dp->dp_typ == DOSPTYP_ONTRACK) {
237 "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
238 bp->b_flags |= B_INVAL | B_AGE;
245 if (bcmp(dp0, historical_bogus_partition_table,
246 sizeof historical_bogus_partition_table) == 0 ||
247 bcmp(dp0, historical_bogus_partition_table_fixed,
248 sizeof historical_bogus_partition_table_fixed) == 0) {
250 TRACE(("%s: invalid primary partition table: historical\n",
255 "%s: invalid primary partition table: Dangerously Dedicated (ignored)\n",
261 /* Guess the geometry. */
264 * Perhaps skip entries with 0 size.
265 * Perhaps only look at entries of type DOSPTYP_386BSD.
270 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
275 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
276 if (max_ncyls < ncyls)
278 nsectors = DPSECT(dp->dp_esect);
279 if (max_nsectors < nsectors)
280 max_nsectors = nsectors;
281 ntracks = dp->dp_ehd + 1;
282 if (max_ntracks < ntracks)
283 max_ntracks = ntracks;
287 * Check that we have guessed the geometry right by checking the
293 * Check for overlaps.
294 * Check against d_secperunit if the latter is reliable.
297 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
298 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
299 && dp->dp_start == 0 && dp->dp_size == 0)
301 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
305 * Temporarily ignore errors from this check. We could
306 * simplify things by accepting the table eariler if we
307 * always ignore errors here. Perhaps we should always
308 * accept the table if the magic is right but not let
309 * bad entries affect the geometry.
311 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
318 * Accept the DOS partition table.
319 * First adjust the label (we have been careful not to change it
320 * before we can guarantee success).
322 secpercyl = (u_long)max_nsectors * max_ntracks;
323 if (secpercyl != 0) {
328 lp->d_nsectors = max_nsectors;
329 lp->d_ntracks = max_ntracks;
330 lp->d_secpercyl = secpercyl;
332 * Temporarily, don't even consider adjusting the drive's
333 * size, since the adjusted size may exceed the hardware's
334 * addressing capabilities. The adjustment helped mainly
335 * for ancient MFM drives with > 1024 cylinders, but now
336 * breaks at least IDE drives with 63*16*65536 sectors if
337 * they are controlled by the wd driver in CHS mode.
340 secperunit = secpercyl * max_ncyls;
341 if (lp->d_secperunit < secperunit)
342 lp->d_secperunit = secperunit;
344 lp->d_ncylinders = lp->d_secperunit / secpercyl;
348 * We are passed a pointer to a suitably initialized minimal
349 * slices "struct" with no dangling pointers in it. Replace it
350 * by a maximal one. This usually oversizes the "struct", but
351 * enlarging it while searching for logical drives would be
354 kfree(*sspp, M_DEVBUF);
355 ssp = dsmakeslicestruct(MAX_SLICES, lp);
358 /* Initialize normal slices. */
359 sp = &ssp->dss_slices[BASE_SLICE];
360 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) {
361 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
363 (void)mbr_setslice(sname, lp, sp, dp, mbr_offset);
365 ssp->dss_nslices = BASE_SLICE + NDOSPART;
367 /* Handle extended partitions. */
369 for (dospart = 0; dospart < NDOSPART; dospart++, sp++) {
370 if (sp->ds_type == DOSPTYP_EXTENDED ||
371 sp->ds_type == DOSPTYP_EXTENDEDX) {
372 mbr_extended(wdev, lp, ssp,
373 sp->ds_offset, sp->ds_size, sp->ds_offset,
374 max_nsectors, max_ntracks, mbr_offset, 1);
379 * mbr_extended() abuses ssp->dss_nslices for the number of slices
380 * that would be found if there were no limit on the number of slices
381 * in *ssp. Cut it back now.
383 if (ssp->dss_nslices > MAX_SLICES)
384 ssp->dss_nslices = MAX_SLICES;
387 bp->b_flags |= B_INVAL | B_AGE;
395 mbr_extended(cdev_t dev, struct disklabel *lp, struct diskslices *ssp,
396 u_long ext_offset, u_long ext_size, u_long base_ext_offset,
397 int nsectors, int ntracks, u_long mbr_offset, int level)
402 struct dos_partition *dp;
403 struct dos_partition dpcopy[NDOSPART];
404 u_long ext_offsets[NDOSPART];
405 u_long ext_sizes[NDOSPART];
409 struct diskslice *sp;
413 "%s: excessive recursion in search for slices; aborting search\n",
418 /* Read extended boot record. */
419 bp = geteblk((int)lp->d_secsize);
420 bp->b_bio1.bio_offset = (off_t)ext_offset * lp->d_secsize;
421 bp->b_bcount = lp->d_secsize;
422 bp->b_cmd = BUF_CMD_READ;
423 dev_dstrategy(dev, &bp->b_bio1);
424 if (biowait(bp) != 0) {
425 diskerr(&bp->b_bio1, dev,
426 "reading extended partition table: error",
427 LOG_PRINTF, 0, NULL);
432 /* Weakly verify it. */
434 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
435 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART,
438 kprintf("%s: invalid extended partition table: no magic\n",
443 /* Make a copy of the partition table to avoid alignment problems. */
444 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
446 slice = ssp->dss_nslices;
447 for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
449 ext_sizes[dospart] = 0;
450 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
451 && dp->dp_start == 0 && dp->dp_size == 0)
453 if (dp->dp_typ == DOSPTYP_EXTENDED ||
454 dp->dp_typ == DOSPTYP_EXTENDEDX) {
457 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
459 ksnprintf(buf, sizeof(buf), "%s", sname);
460 if (strlen(buf) < sizeof buf - 11)
461 strcat(buf, "<extended>");
462 check_part(buf, dp, base_ext_offset, nsectors,
463 ntracks, mbr_offset);
464 ext_offsets[dospart] = base_ext_offset + dp->dp_start;
465 ext_sizes[dospart] = dp->dp_size;
467 sname = dsname(dev, dkunit(dev), slice, RAW_PART,
469 check_part(sname, dp, ext_offset, nsectors, ntracks,
471 if (slice >= MAX_SLICES) {
472 kprintf("%s: too many slices\n", sname);
476 sp = &ssp->dss_slices[slice];
477 if (mbr_setslice(sname, lp, sp, dp, ext_offset) != 0)
482 ssp->dss_nslices = slice;
484 /* If we found any more slices, recursively find all the subslices. */
485 for (dospart = 0; dospart < NDOSPART; dospart++) {
486 if (ext_sizes[dospart] != 0) {
487 mbr_extended(dev, lp, ssp, ext_offsets[dospart],
488 ext_sizes[dospart], base_ext_offset,
489 nsectors, ntracks, mbr_offset, ++level);
494 bp->b_flags |= B_INVAL | B_AGE;
499 mbr_setslice(char *sname, struct disklabel *lp, struct diskslice *sp,
500 struct dos_partition *dp, u_long br_offset)
505 offset = br_offset + dp->dp_start;
506 if (offset > lp->d_secperunit || offset < br_offset) {
508 "%s: slice starts beyond end of the disk: rejecting it\n",
512 size = lp->d_secperunit - offset;
513 if (size >= dp->dp_size)
517 "%s: slice extends beyond end of disk: truncating from %lu to %lu sectors\n",
518 sname, (u_long)dp->dp_size, size);
519 sp->ds_offset = offset;
521 sp->ds_type = dp->dp_typ;
523 lp->d_subtype |= (lp->d_subtype & 3) | dospart | DSTYPE_INDOSPART;