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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
33 * from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
34 * $FreeBSD: src/sys/kern/subr_diskmbr.c,v 1.45 2000/01/28 10:22:07 bde Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/diskslice.h>
42 #define DOSPTYP_EXTENDED 5
43 #define DOSPTYP_EXTENDEDX 15
44 #define DOSPTYP_ONTRACK 84
45 #include <sys/diskmbr.h>
47 #include <sys/malloc.h>
48 #include <sys/syslog.h>
49 #include <sys/device.h>
51 #define TRACE(str) do { if (dsi_debug) kprintf str; } while (0)
53 static volatile u_char dsi_debug
;
56 * This is what we have embedded in every boot1 for supporting the bogus
57 * "Dangerously Dedicated" mode. However, the old table is broken because
58 * it has an illegal geometry in it - it specifies 256 heads (heads = end
59 * head + 1) which causes nasty stuff when that wraps to zero in bios code.
60 * eg: divide by zero etc. This caused the dead-thinkpad problem, numerous
61 * SCSI bios crashes, EFI to crash, etc.
63 * We still have to recognize the old table though, even though we stopped
64 * inflicting it upon the world.
66 static struct dos_partition historical_bogus_partition_table
[NDOSPART
] = {
67 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
68 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
69 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
70 { 0x80, 0, 1, 0, DOSPTYP_386BSD
, 255, 255, 255, 0, 50000, },
72 static struct dos_partition historical_bogus_partition_table_fixed
[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
, 254, 255, 255, 0, 50000, },
79 static int check_part (char *sname
, struct dos_partition
*dp
,
80 u_int64_t offset
, int nsectors
, int ntracks
,
81 u_int64_t mbr_offset
);
82 static void mbr_extended (cdev_t dev
, struct disk_info
*info
,
83 struct diskslices
*ssp
, u_int64_t ext_offset
,
84 u_int64_t ext_size
, u_int64_t base_ext_offset
,
85 int nsectors
, int ntracks
, u_int64_t mbr_offset
,
87 static int mbr_setslice (char *sname
, struct disk_info
*info
,
88 struct diskslice
*sp
, struct dos_partition
*dp
,
93 mbrinit(cdev_t dev
, struct disk_info
*info
, struct diskslices
**sspp
)
98 struct dos_partition
*dp
;
99 struct dos_partition
*dp0
;
100 struct dos_partition dpcopy
[NDOSPART
];
105 u_int64_t mbr_offset
;
108 char *sname
= "tempname";
109 struct diskslice
*sp
;
110 struct diskslices
*ssp
;
113 mbr_offset
= DOSBBSECTOR
;
116 * Don't bother if the block size is weird or the
117 * media size is 0 (probably means no media present).
119 if (info
->d_media_blksize
& DEV_BMASK
)
121 if (info
->d_media_size
== 0)
125 * Read master boot record.
128 bp
= geteblk((int)info
->d_media_blksize
);
129 bp
->b_bio1
.bio_offset
= (off_t
)mbr_offset
* info
->d_media_blksize
;
130 bp
->b_bio1
.bio_done
= biodone_sync
;
131 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
132 bp
->b_bcount
= info
->d_media_blksize
;
133 bp
->b_cmd
= BUF_CMD_READ
;
134 bp
->b_flags
|= B_FAILONDIS
;
135 dev_dstrategy(wdev
, &bp
->b_bio1
);
136 if (biowait(&bp
->b_bio1
, "mbrrd") != 0) {
137 if ((info
->d_dsflags
& DSO_MBRQUIET
) == 0) {
138 diskerr(&bp
->b_bio1
, wdev
,
139 "reading primary partition table: error",
147 /* Weakly verify it. */
149 sname
= dsname(dev
, 0, 0, 0, NULL
);
150 if (cp
[0x1FE] != 0x55 || cp
[0x1FF] != 0xAA) {
152 kprintf("%s: invalid primary partition table: no magic\n",
158 /* Make a copy of the partition table to avoid alignment problems. */
159 memcpy(&dpcopy
[0], cp
+ DOSPARTOFF
, sizeof(dpcopy
));
164 * Check for "Ontrack Diskmanager" or GPT. If a GPT is found in
165 * the first dos partition, ignore the rest of the MBR and go
168 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++) {
170 (dp
->dp_typ
== DOSPTYP_PMBR
|| dp
->dp_typ
== DOSPTYP_GPT
)) {
173 "%s: Found GPT in slice #%d\n", sname
, dospart
+ 1);
174 error
= gptinit(dev
, info
, sspp
);
178 if (dp
->dp_typ
== DOSPTYP_ONTRACK
) {
181 "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname
);
182 bp
->b_flags
|= B_INVAL
| B_AGE
;
189 if (bcmp(dp0
, historical_bogus_partition_table
,
190 sizeof historical_bogus_partition_table
) == 0 ||
191 bcmp(dp0
, historical_bogus_partition_table_fixed
,
192 sizeof historical_bogus_partition_table_fixed
) == 0) {
194 TRACE(("%s: invalid primary partition table: historical\n",
199 "%s: invalid primary partition table: Dangerously Dedicated (ignored)\n",
205 /* Guess the geometry. */
208 * Perhaps skip entries with 0 size.
209 * Perhaps only look at entries of type DOSPTYP_386BSD.
214 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++) {
219 ncyls
= DPCYL(dp
->dp_ecyl
, dp
->dp_esect
) + 1;
220 if (max_ncyls
< ncyls
)
222 nsectors
= DPSECT(dp
->dp_esect
);
223 if (max_nsectors
< nsectors
)
224 max_nsectors
= nsectors
;
225 ntracks
= dp
->dp_ehd
+ 1;
226 if (max_ntracks
< ntracks
)
227 max_ntracks
= ntracks
;
231 * Check that we have guessed the geometry right by checking the
237 * Check for overlaps.
238 * Check against d_secperunit if the latter is reliable.
241 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++) {
242 if (dp
->dp_scyl
== 0 && dp
->dp_shd
== 0 && dp
->dp_ssect
== 0
243 && dp
->dp_start
== 0 && dp
->dp_size
== 0)
245 //sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
246 // WHOLE_SLICE_PART, partname);
249 * Temporarily ignore errors from this check. We could
250 * simplify things by accepting the table eariler if we
251 * always ignore errors here. Perhaps we should always
252 * accept the table if the magic is right but not let
253 * bad entries affect the geometry.
255 check_part(sname
, dp
, mbr_offset
, max_nsectors
, max_ntracks
,
262 * Accept the DOS partition table.
264 * Adjust the disk information structure with updated CHS
265 * conversion parameters, but only use values extracted from
266 * the primary partition table.
268 * NOTE! Regardless of our having to deal with this old cruft,
269 * we do not screw around with the info->d_media* parameters.
271 secpercyl
= (u_long
)max_nsectors
* max_ntracks
;
272 if (secpercyl
!= 0 && mbr_offset
== DOSBBSECTOR
) {
273 info
->d_secpertrack
= max_nsectors
;
274 info
->d_nheads
= max_ntracks
;
275 info
->d_secpercyl
= secpercyl
;
276 info
->d_ncylinders
= info
->d_media_blocks
/ secpercyl
;
280 * We are passed a pointer to a suitably initialized minimal
281 * slices "struct" with no dangling pointers in it. Replace it
282 * by a maximal one. This usually oversizes the "struct", but
283 * enlarging it while searching for logical drives would be
286 kfree(*sspp
, M_DEVBUF
);
287 ssp
= dsmakeslicestruct(MAX_SLICES
, info
);
290 /* Initialize normal slices. */
291 sp
= &ssp
->dss_slices
[BASE_SLICE
];
292 for (dospart
= 0, dp
= dp0
; dospart
< NDOSPART
; dospart
++, dp
++, sp
++) {
293 sname
= dsname(dev
, dkunit(dev
), BASE_SLICE
+ dospart
,
294 WHOLE_SLICE_PART
, partname
);
295 (void)mbr_setslice(sname
, info
, sp
, dp
, mbr_offset
);
297 ssp
->dss_nslices
= BASE_SLICE
+ NDOSPART
;
299 /* Handle extended partitions. */
301 for (dospart
= 0; dospart
< NDOSPART
; dospart
++, sp
++) {
302 if (sp
->ds_type
== DOSPTYP_EXTENDED
||
303 sp
->ds_type
== DOSPTYP_EXTENDEDX
) {
304 mbr_extended(wdev
, info
, ssp
,
305 sp
->ds_offset
, sp
->ds_size
, sp
->ds_offset
,
306 max_nsectors
, max_ntracks
, mbr_offset
, 1);
311 * mbr_extended() abuses ssp->dss_nslices for the number of slices
312 * that would be found if there were no limit on the number of slices
313 * in *ssp. Cut it back now.
315 if (ssp
->dss_nslices
> MAX_SLICES
)
316 ssp
->dss_nslices
= MAX_SLICES
;
319 bp
->b_flags
|= B_INVAL
| B_AGE
;
327 check_part(char *sname
, struct dos_partition
*dp
, u_int64_t offset
,
328 int nsectors
, int ntracks
, u_int64_t mbr_offset
)
341 secpercyl
= (u_long
)nsectors
* ntracks
;
342 chs_scyl
= DPCYL(dp
->dp_scyl
, dp
->dp_ssect
);
343 chs_ssect
= DPSECT(dp
->dp_ssect
);
344 ssector
= chs_ssect
- 1 + dp
->dp_shd
* nsectors
+ chs_scyl
* secpercyl
346 ssector1
= offset
+ dp
->dp_start
;
349 * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
350 * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
351 * apart from the cylinder being reduced modulo 1024. Always allow
352 * 1023/255/63, because this is the official way to represent
353 * pure-LBA for the starting position.
355 if ((ssector
< ssector1
356 && ((chs_ssect
== nsectors
&& dp
->dp_shd
== ntracks
- 1
359 && (ssector1
- ssector
) % (1024 * secpercyl
) == 0)))
360 || (dp
->dp_scyl
== 255 && dp
->dp_shd
== 255
361 && dp
->dp_ssect
== 255)) {
362 TRACE(("%s: C/H/S start %d/%d/%d, start %llu: allow\n",
363 sname
, chs_scyl
, dp
->dp_shd
, chs_ssect
,
364 (long long)ssector1
));
368 chs_ecyl
= DPCYL(dp
->dp_ecyl
, dp
->dp_esect
);
369 chs_esect
= DPSECT(dp
->dp_esect
);
370 esector
= chs_esect
- 1 + dp
->dp_ehd
* nsectors
+ chs_ecyl
* secpercyl
372 esector1
= ssector1
+ dp
->dp_size
- 1;
375 * Allow certain bogus C/H/S values for esector, as above. However,
376 * heads == 255 isn't really legal and causes some BIOS crashes. The
377 * correct value to indicate a pure-LBA end is 1023/heads-1/sectors -
378 * usually 1023/254/63. "heads" is base 0, "sectors" is base 1.
380 if ((esector
< esector1
381 && ((chs_esect
== nsectors
&& dp
->dp_ehd
== ntracks
- 1
384 && (esector1
- esector
) % (1024 * secpercyl
) == 0)))
385 || (dp
->dp_ecyl
== 255 && dp
->dp_ehd
== 255
386 && dp
->dp_esect
== 255)) {
387 TRACE(("%s: C/H/S end %d/%d/%d, end %llu: allow\n",
388 sname
, chs_ecyl
, dp
->dp_ehd
, chs_esect
,
389 (long long)esector1
));
393 error
= (ssector
== ssector1
&& esector
== esector1
) ? 0 : EINVAL
;
395 kprintf("%s: type 0x%x, start %llu, end = %llu, size %u %s\n",
397 (long long)ssector1
, (long long)esector1
,
398 dp
->dp_size
, (error
? "" : ": OK"));
399 if (ssector
!= ssector1
&& bootverbose
)
400 kprintf("%s: C/H/S start %d/%d/%d (%llu) != start %llu: invalid\n",
401 sname
, chs_scyl
, dp
->dp_shd
, chs_ssect
,
402 (long long)ssector
, (long long)ssector1
);
403 if (esector
!= esector1
&& bootverbose
)
404 kprintf("%s: C/H/S end %d/%d/%d (%llu) != end %llu: invalid\n",
405 sname
, chs_ecyl
, dp
->dp_ehd
, chs_esect
,
406 (long long)esector
, (long long)esector1
);
412 mbr_extended(cdev_t dev
, struct disk_info
*info
, struct diskslices
*ssp
,
413 u_int64_t ext_offset
, u_int64_t ext_size
, u_int64_t base_ext_offset
,
414 int nsectors
, int ntracks
, u_int64_t mbr_offset
, int level
)
419 struct dos_partition
*dp
;
420 struct dos_partition dpcopy
[NDOSPART
];
421 u_int64_t ext_offsets
[NDOSPART
];
422 u_int64_t ext_sizes
[NDOSPART
];
426 struct diskslice
*sp
;
430 "%s: excessive recursion in search for slices; aborting search\n",
435 /* Read extended boot record. */
436 bp
= geteblk((int)info
->d_media_blksize
);
437 bp
->b_bio1
.bio_offset
= (off_t
)ext_offset
* info
->d_media_blksize
;
438 bp
->b_bio1
.bio_done
= biodone_sync
;
439 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
440 bp
->b_bcount
= info
->d_media_blksize
;
441 bp
->b_cmd
= BUF_CMD_READ
;
442 bp
->b_flags
|= B_FAILONDIS
;
443 dev_dstrategy(dev
, &bp
->b_bio1
);
444 if (biowait(&bp
->b_bio1
, "mbrrd") != 0) {
445 diskerr(&bp
->b_bio1
, dev
,
446 "reading extended partition table: error",
452 /* Weakly verify it. */
454 if (cp
[0x1FE] != 0x55 || cp
[0x1FF] != 0xAA) {
455 sname
= dsname(dev
, dkunit(dev
), WHOLE_DISK_SLICE
, WHOLE_SLICE_PART
,
458 kprintf("%s: invalid extended partition table: no magic\n",
463 /* Make a copy of the partition table to avoid alignment problems. */
464 memcpy(&dpcopy
[0], cp
+ DOSPARTOFF
, sizeof(dpcopy
));
466 slice
= ssp
->dss_nslices
;
467 for (dospart
= 0, dp
= &dpcopy
[0]; dospart
< NDOSPART
;
469 ext_sizes
[dospart
] = 0;
470 if (dp
->dp_scyl
== 0 && dp
->dp_shd
== 0 && dp
->dp_ssect
== 0
471 && dp
->dp_start
== 0 && dp
->dp_size
== 0)
473 if (dp
->dp_typ
== DOSPTYP_EXTENDED
||
474 dp
->dp_typ
== DOSPTYP_EXTENDEDX
) {
477 sname
= dsname(dev
, dkunit(dev
), WHOLE_DISK_SLICE
,
478 WHOLE_SLICE_PART
, partname
);
479 ksnprintf(buf
, sizeof(buf
), "%s", sname
);
480 if (strlen(buf
) < sizeof buf
- 11)
481 strcat(buf
, "<extended>");
482 check_part(buf
, dp
, base_ext_offset
, nsectors
,
483 ntracks
, mbr_offset
);
484 ext_offsets
[dospart
] = base_ext_offset
+ dp
->dp_start
;
485 ext_sizes
[dospart
] = dp
->dp_size
;
487 sname
= dsname(dev
, dkunit(dev
), slice
, WHOLE_SLICE_PART
,
489 check_part(sname
, dp
, ext_offset
, nsectors
, ntracks
,
491 if (slice
>= MAX_SLICES
) {
492 kprintf("%s: too many slices\n", sname
);
496 sp
= &ssp
->dss_slices
[slice
];
497 if (mbr_setslice(sname
, info
, sp
, dp
, ext_offset
) != 0)
502 ssp
->dss_nslices
= slice
;
504 /* If we found any more slices, recursively find all the subslices. */
505 for (dospart
= 0; dospart
< NDOSPART
; dospart
++) {
506 if (ext_sizes
[dospart
] != 0) {
507 mbr_extended(dev
, info
, ssp
, ext_offsets
[dospart
],
508 ext_sizes
[dospart
], base_ext_offset
,
509 nsectors
, ntracks
, mbr_offset
, ++level
);
514 bp
->b_flags
|= B_INVAL
| B_AGE
;
519 mbr_setslice(char *sname
, struct disk_info
*info
, struct diskslice
*sp
,
520 struct dos_partition
*dp
, u_int64_t br_offset
)
525 offset
= br_offset
+ dp
->dp_start
;
526 if (offset
> info
->d_media_blocks
|| offset
< br_offset
) {
528 "%s: slice starts beyond end of the disk: rejecting it\n",
532 size
= info
->d_media_blocks
- offset
;
533 if (size
>= dp
->dp_size
) {
534 if (dp
->dp_size
== 0xFFFFFFFFU
) {
535 kprintf("%s: slice >2TB, using media size instead "
536 "of slice table size\n", sname
);
541 kprintf("%s: slice extends beyond end of disk: "
542 "truncating from %u to %llu sectors\n",
543 sname
, dp
->dp_size
, (unsigned long long)size
);
545 sp
->ds_offset
= offset
;
547 sp
->ds_type
= dp
->dp_typ
;
548 bzero(&sp
->ds_type_uuid
, sizeof(sp
->ds_type_uuid
));
549 bzero(&sp
->ds_stor_uuid
, sizeof(sp
->ds_type_uuid
));
552 * Slices do not overlap with the parent (if any).