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.26 2007/06/19 06:07:57 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/diskslice.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>
53 #include <sys/malloc.h>
54 #include <sys/syslog.h>
55 #include <sys/device.h>
57 #define TRACE(str) do { if (dsi_debug) kprintf str; } while (0)
59 static volatile u_char dsi_debug
;
62 * This is what we have embedded in every boot1 for supporting the bogus
63 * "Dangerously Dedicated" mode. However, the old table is broken because
64 * it has an illegal geometry in it - it specifies 256 heads (heads = end
65 * head + 1) which causes nasty stuff when that wraps to zero in bios code.
66 * eg: divide by zero etc. This caused the dead-thinkpad problem, numerous
67 * SCSI bios crashes, EFI to crash, etc.
69 * We still have to recognize the old table though, even though we stopped
70 * inflicting it upon the world.
72 static struct dos_partition historical_bogus_partition_table
[NDOSPART
] = {
73 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
74 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
75 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
76 { 0x80, 0, 1, 0, DOSPTYP_386BSD
, 255, 255, 255, 0, 50000, },
78 static struct dos_partition historical_bogus_partition_table_fixed
[NDOSPART
] = {
79 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
80 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
81 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
82 { 0x80, 0, 1, 0, DOSPTYP_386BSD
, 254, 255, 255, 0, 50000, },
85 static int check_part (char *sname
, struct dos_partition
*dp
,
86 u_int64_t offset
, int nsectors
, int ntracks
,
87 u_int64_t mbr_offset
);
88 static void mbr_extended (cdev_t dev
, struct disk_info
*info
,
89 struct diskslices
*ssp
, u_int64_t ext_offset
,
90 u_int64_t ext_size
, u_int64_t base_ext_offset
,
91 int nsectors
, int ntracks
, u_int64_t mbr_offset
,
93 static int mbr_setslice (char *sname
, struct disk_info
*info
,
94 struct diskslice
*sp
, struct dos_partition
*dp
,
99 mbrinit(cdev_t dev
, struct disk_info
*info
, struct diskslices
**sspp
)
104 struct dos_partition
*dp
;
105 struct dos_partition
*dp0
;
106 struct dos_partition dpcopy
[NDOSPART
];
111 u_int64_t mbr_offset
;
114 char *sname
= "tempname";
115 struct diskslice
*sp
;
116 struct diskslices
*ssp
;
119 mbr_offset
= DOSBBSECTOR
;
121 if (info
->d_media_blksize
& DEV_BMASK
)
123 /* Read master boot record. */
125 bp
= geteblk((int)info
->d_media_blksize
);
126 bp
->b_bio1
.bio_offset
= (off_t
)mbr_offset
* info
->d_media_blksize
;
127 bp
->b_bio1
.bio_done
= biodone_sync
;
128 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
129 bp
->b_bcount
= info
->d_media_blksize
;
130 bp
->b_cmd
= BUF_CMD_READ
;
131 dev_dstrategy(wdev
, &bp
->b_bio1
);
132 if (biowait(&bp
->b_bio1
, "mbrrd") != 0) {
133 if ((info
->d_dsflags
& DSO_MBRQUIET
) == 0) {
134 diskerr(&bp
->b_bio1
, wdev
,
135 "reading primary partition table: error",
143 /* Weakly verify it. */
145 //sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, WHOLE_SLICE_PART, partname);
146 if (cp
[0x1FE] != 0x55 || cp
[0x1FF] != 0xAA) {
148 kprintf("%s: invalid primary partition table: no magic\n",
154 /* Make a copy of the partition table to avoid alignment problems. */
155 memcpy(&dpcopy
[0], cp
+ DOSPARTOFF
, sizeof(dpcopy
));
160 * Check for "Ontrack Diskmanager" or GPT. If a GPT is found in
161 * the first dos partition, ignore the rest of the MBR and go
164 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++) {
166 (dp
->dp_typ
== DOSPTYP_PMBR
|| dp
->dp_typ
== DOSPTYP_GPT
)) {
169 "%s: Found GPT in slice #%d\n", sname
, dospart
+ 1);
170 error
= gptinit(dev
, info
, sspp
);
174 if (dp
->dp_typ
== DOSPTYP_ONTRACK
) {
177 "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname
);
178 bp
->b_flags
|= B_INVAL
| B_AGE
;
185 if (bcmp(dp0
, historical_bogus_partition_table
,
186 sizeof historical_bogus_partition_table
) == 0 ||
187 bcmp(dp0
, historical_bogus_partition_table_fixed
,
188 sizeof historical_bogus_partition_table_fixed
) == 0) {
190 TRACE(("%s: invalid primary partition table: historical\n",
195 "%s: invalid primary partition table: Dangerously Dedicated (ignored)\n",
201 /* Guess the geometry. */
204 * Perhaps skip entries with 0 size.
205 * Perhaps only look at entries of type DOSPTYP_386BSD.
210 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++) {
215 ncyls
= DPCYL(dp
->dp_ecyl
, dp
->dp_esect
) + 1;
216 if (max_ncyls
< ncyls
)
218 nsectors
= DPSECT(dp
->dp_esect
);
219 if (max_nsectors
< nsectors
)
220 max_nsectors
= nsectors
;
221 ntracks
= dp
->dp_ehd
+ 1;
222 if (max_ntracks
< ntracks
)
223 max_ntracks
= ntracks
;
227 * Check that we have guessed the geometry right by checking the
233 * Check for overlaps.
234 * Check against d_secperunit if the latter is reliable.
237 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++) {
238 if (dp
->dp_scyl
== 0 && dp
->dp_shd
== 0 && dp
->dp_ssect
== 0
239 && dp
->dp_start
== 0 && dp
->dp_size
== 0)
241 //sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
242 // WHOLE_SLICE_PART, partname);
245 * Temporarily ignore errors from this check. We could
246 * simplify things by accepting the table eariler if we
247 * always ignore errors here. Perhaps we should always
248 * accept the table if the magic is right but not let
249 * bad entries affect the geometry.
251 check_part(sname
, dp
, mbr_offset
, max_nsectors
, max_ntracks
,
258 * Accept the DOS partition table.
260 * Adjust the disk information structure with updated CHS
261 * conversion parameters, but only use values extracted from
262 * the primary partition table.
264 * NOTE! Regardless of our having to deal with this old cruft,
265 * we do not screw around with the info->d_media* parameters.
267 secpercyl
= (u_long
)max_nsectors
* max_ntracks
;
268 if (secpercyl
!= 0 && mbr_offset
== DOSBBSECTOR
) {
269 info
->d_secpertrack
= max_nsectors
;
270 info
->d_nheads
= max_ntracks
;
271 info
->d_secpercyl
= secpercyl
;
272 info
->d_ncylinders
= info
->d_media_blocks
/ secpercyl
;
276 * We are passed a pointer to a suitably initialized minimal
277 * slices "struct" with no dangling pointers in it. Replace it
278 * by a maximal one. This usually oversizes the "struct", but
279 * enlarging it while searching for logical drives would be
282 kfree(*sspp
, M_DEVBUF
);
283 ssp
= dsmakeslicestruct(MAX_SLICES
, info
);
286 /* Initialize normal slices. */
287 sp
= &ssp
->dss_slices
[BASE_SLICE
];
288 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++, sp
++) {
289 sname
= dsname(dev
, dkunit(dev
), BASE_SLICE
+ dospart
,
290 WHOLE_SLICE_PART
, partname
);
291 (void)mbr_setslice(sname
, info
, sp
, dp
, mbr_offset
);
293 ssp
->dss_nslices
= BASE_SLICE
+ NDOSPART
;
295 /* Handle extended partitions. */
297 for (dospart
= 0; dospart
< NDOSPART
; dospart
++, sp
++) {
298 if (sp
->ds_type
== DOSPTYP_EXTENDED
||
299 sp
->ds_type
== DOSPTYP_EXTENDEDX
) {
300 mbr_extended(wdev
, info
, ssp
,
301 sp
->ds_offset
, sp
->ds_size
, sp
->ds_offset
,
302 max_nsectors
, max_ntracks
, mbr_offset
, 1);
307 * mbr_extended() abuses ssp->dss_nslices for the number of slices
308 * that would be found if there were no limit on the number of slices
309 * in *ssp. Cut it back now.
311 if (ssp
->dss_nslices
> MAX_SLICES
)
312 ssp
->dss_nslices
= MAX_SLICES
;
315 bp
->b_flags
|= B_INVAL
| B_AGE
;
323 check_part(char *sname
, struct dos_partition
*dp
, u_int64_t offset
,
324 int nsectors
, int ntracks
, u_int64_t mbr_offset
)
337 secpercyl
= (u_long
)nsectors
* ntracks
;
338 chs_scyl
= DPCYL(dp
->dp_scyl
, dp
->dp_ssect
);
339 chs_ssect
= DPSECT(dp
->dp_ssect
);
340 ssector
= chs_ssect
- 1 + dp
->dp_shd
* nsectors
+ chs_scyl
* secpercyl
342 ssector1
= offset
+ dp
->dp_start
;
345 * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
346 * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
347 * apart from the cylinder being reduced modulo 1024. Always allow
348 * 1023/255/63, because this is the official way to represent
349 * pure-LBA for the starting position.
351 if ((ssector
< ssector1
352 && ((chs_ssect
== nsectors
&& dp
->dp_shd
== ntracks
- 1
355 && (ssector1
- ssector
) % (1024 * secpercyl
) == 0)))
356 || (dp
->dp_scyl
== 255 && dp
->dp_shd
== 255
357 && dp
->dp_ssect
== 255)) {
358 TRACE(("%s: C/H/S start %d/%d/%d, start %llu: allow\n",
359 sname
, chs_scyl
, dp
->dp_shd
, chs_ssect
,
360 (long long)ssector1
));
364 chs_ecyl
= DPCYL(dp
->dp_ecyl
, dp
->dp_esect
);
365 chs_esect
= DPSECT(dp
->dp_esect
);
366 esector
= chs_esect
- 1 + dp
->dp_ehd
* nsectors
+ chs_ecyl
* secpercyl
368 esector1
= ssector1
+ dp
->dp_size
- 1;
371 * Allow certain bogus C/H/S values for esector, as above. However,
372 * heads == 255 isn't really legal and causes some BIOS crashes. The
373 * correct value to indicate a pure-LBA end is 1023/heads-1/sectors -
374 * usually 1023/254/63. "heads" is base 0, "sectors" is base 1.
376 if ((esector
< esector1
377 && ((chs_esect
== nsectors
&& dp
->dp_ehd
== ntracks
- 1
380 && (esector1
- esector
) % (1024 * secpercyl
) == 0)))
381 || (dp
->dp_ecyl
== 255 && dp
->dp_ehd
== 255
382 && dp
->dp_esect
== 255)) {
383 TRACE(("%s: C/H/S end %d/%d/%d, end %llu: allow\n",
384 sname
, chs_ecyl
, dp
->dp_ehd
, chs_esect
,
385 (long long)esector1
));
389 error
= (ssector
== ssector1
&& esector
== esector1
) ? 0 : EINVAL
;
391 kprintf("%s: type 0x%x, start %llu, end = %llu, size %u %s\n",
393 (long long)ssector1
, (long long)esector1
,
394 dp
->dp_size
, (error
? "" : ": OK"));
395 if (ssector
!= ssector1
&& bootverbose
)
396 kprintf("%s: C/H/S start %d/%d/%d (%llu) != start %llu: invalid\n",
397 sname
, chs_scyl
, dp
->dp_shd
, chs_ssect
,
398 (long long)ssector
, (long long)ssector1
);
399 if (esector
!= esector1
&& bootverbose
)
400 kprintf("%s: C/H/S end %d/%d/%d (%llu) != end %llu: invalid\n",
401 sname
, chs_ecyl
, dp
->dp_ehd
, chs_esect
,
402 (long long)esector
, (long long)esector1
);
408 mbr_extended(cdev_t dev
, struct disk_info
*info
, struct diskslices
*ssp
,
409 u_int64_t ext_offset
, u_int64_t ext_size
, u_int64_t base_ext_offset
,
410 int nsectors
, int ntracks
, u_int64_t mbr_offset
, int level
)
415 struct dos_partition
*dp
;
416 struct dos_partition dpcopy
[NDOSPART
];
417 u_int64_t ext_offsets
[NDOSPART
];
418 u_int64_t ext_sizes
[NDOSPART
];
422 struct diskslice
*sp
;
426 "%s: excessive recursion in search for slices; aborting search\n",
431 /* Read extended boot record. */
432 bp
= geteblk((int)info
->d_media_blksize
);
433 bp
->b_bio1
.bio_offset
= (off_t
)ext_offset
* info
->d_media_blksize
;
434 bp
->b_bio1
.bio_done
= biodone_sync
;
435 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
436 bp
->b_bcount
= info
->d_media_blksize
;
437 bp
->b_cmd
= BUF_CMD_READ
;
438 dev_dstrategy(dev
, &bp
->b_bio1
);
439 if (biowait(&bp
->b_bio1
, "mbrrd") != 0) {
440 diskerr(&bp
->b_bio1
, dev
,
441 "reading extended partition table: error",
447 /* Weakly verify it. */
449 if (cp
[0x1FE] != 0x55 || cp
[0x1FF] != 0xAA) {
450 sname
= dsname(dev
, dkunit(dev
), WHOLE_DISK_SLICE
, WHOLE_SLICE_PART
,
453 kprintf("%s: invalid extended partition table: no magic\n",
458 /* Make a copy of the partition table to avoid alignment problems. */
459 memcpy(&dpcopy
[0], cp
+ DOSPARTOFF
, sizeof(dpcopy
));
461 slice
= ssp
->dss_nslices
;
462 for (dospart
= 0, dp
= &dpcopy
[0]; dospart
< NDOSPART
;
464 ext_sizes
[dospart
] = 0;
465 if (dp
->dp_scyl
== 0 && dp
->dp_shd
== 0 && dp
->dp_ssect
== 0
466 && dp
->dp_start
== 0 && dp
->dp_size
== 0)
468 if (dp
->dp_typ
== DOSPTYP_EXTENDED
||
469 dp
->dp_typ
== DOSPTYP_EXTENDEDX
) {
472 sname
= dsname(dev
, dkunit(dev
), WHOLE_DISK_SLICE
,
473 WHOLE_SLICE_PART
, partname
);
474 ksnprintf(buf
, sizeof(buf
), "%s", sname
);
475 if (strlen(buf
) < sizeof buf
- 11)
476 strcat(buf
, "<extended>");
477 check_part(buf
, dp
, base_ext_offset
, nsectors
,
478 ntracks
, mbr_offset
);
479 ext_offsets
[dospart
] = base_ext_offset
+ dp
->dp_start
;
480 ext_sizes
[dospart
] = dp
->dp_size
;
482 sname
= dsname(dev
, dkunit(dev
), slice
, WHOLE_SLICE_PART
,
484 check_part(sname
, dp
, ext_offset
, nsectors
, ntracks
,
486 if (slice
>= MAX_SLICES
) {
487 kprintf("%s: too many slices\n", sname
);
491 sp
= &ssp
->dss_slices
[slice
];
492 if (mbr_setslice(sname
, info
, sp
, dp
, ext_offset
) != 0)
497 ssp
->dss_nslices
= slice
;
499 /* If we found any more slices, recursively find all the subslices. */
500 for (dospart
= 0; dospart
< NDOSPART
; dospart
++) {
501 if (ext_sizes
[dospart
] != 0) {
502 mbr_extended(dev
, info
, ssp
, ext_offsets
[dospart
],
503 ext_sizes
[dospart
], base_ext_offset
,
504 nsectors
, ntracks
, mbr_offset
, ++level
);
509 bp
->b_flags
|= B_INVAL
| B_AGE
;
514 mbr_setslice(char *sname
, struct disk_info
*info
, struct diskslice
*sp
,
515 struct dos_partition
*dp
, u_int64_t br_offset
)
520 offset
= br_offset
+ dp
->dp_start
;
521 if (offset
> info
->d_media_blocks
|| offset
< br_offset
) {
523 "%s: slice starts beyond end of the disk: rejecting it\n",
527 size
= info
->d_media_blocks
- offset
;
528 if (size
>= dp
->dp_size
) {
529 if (dp
->dp_size
== 0xFFFFFFFFU
) {
530 kprintf("%s: slice >2TB, using media size instead "
531 "of slice table size\n", sname
);
536 kprintf("%s: slice extends beyond end of disk: "
537 "truncating from %u to %llu sectors\n",
538 sname
, dp
->dp_size
, (unsigned long long)size
);
540 sp
->ds_offset
= offset
;
542 sp
->ds_type
= dp
->dp_typ
;
543 bzero(&sp
->ds_type_uuid
, sizeof(sp
->ds_type_uuid
));
544 bzero(&sp
->ds_stor_uuid
, sizeof(sp
->ds_type_uuid
));
547 * Slices do not overlap with the parent (if any).