Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / kern / subr_diskmbr.c
CommitLineData
984263bc
MD
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 $
6ea70f76 39 * $DragonFly: src/sys/kern/subr_diskmbr.c,v 1.18 2006/12/23 00:35:04 swildner Exp $
984263bc
MD
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/buf.h>
45#include <sys/conf.h>
984263bc
MD
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>
dc62b251 51#include <sys/diskmbr.h>
984263bc
MD
52#include <sys/malloc.h>
53#include <sys/syslog.h>
335dda38 54#include <sys/device.h>
984263bc 55
6ea70f76 56#define TRACE(str) do { if (dsi_debug) kprintf str; } while (0)
984263bc
MD
57
58static volatile u_char dsi_debug;
59
4ac7c869
MD
60/*
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.
67 *
68 * We still have to recognize the old table though, even though we stopped
69 * inflicting it upon the world.
70 */
984263bc
MD
71static 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, },
76};
4ac7c869
MD
77static 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, },
82};
984263bc 83
402ed7e1 84static int check_part (char *sname, struct dos_partition *dp,
984263bc 85 u_long offset, int nsectors, int ntracks,
402ed7e1 86 u_long mbr_offset);
b13267a5 87static void mbr_extended (cdev_t dev, struct disklabel *lp,
984263bc
MD
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,
402ed7e1
RG
91 int level);
92static int mbr_setslice (char *sname, struct disklabel *lp,
984263bc 93 struct diskslice *sp, struct dos_partition *dp,
402ed7e1 94 u_long br_offset);
984263bc
MD
95
96static int
a0c19955
MD
97check_part(char *sname, struct dos_partition *dp, u_long offset,
98 int nsectors, int ntracks, u_long mbr_offset)
984263bc
MD
99{
100 int chs_ecyl;
101 int chs_esect;
102 int chs_scyl;
103 int chs_ssect;
104 int error;
105 u_long esector;
106 u_long esector1;
107 u_long secpercyl;
108 u_long ssector;
109 u_long ssector1;
110
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
115 + mbr_offset;
116 ssector1 = offset + dp->dp_start;
117
118 /*
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
4ac7c869
MD
122 * 1023/255/63, because this is the official way to represent
123 * pure-LBA for the starting position.
984263bc
MD
124 */
125 if ((ssector < ssector1
126 && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
127 && chs_scyl == 1023)
128 || (secpercyl != 0
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));
134 ssector = ssector1;
135 }
136
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
140 + mbr_offset;
141 esector1 = ssector1 + dp->dp_size - 1;
142
4ac7c869
MD
143 /*
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.
148 */
984263bc
MD
149 if ((esector < esector1
150 && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
151 && chs_ecyl == 1023)
152 || (secpercyl != 0
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));
158 esector = esector1;
159 }
160
161 error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
162 if (bootverbose)
6ea70f76 163 kprintf("%s: type 0x%x, start %lu, end = %lu, size %lu %s\n",
984263bc
MD
164 sname, dp->dp_typ, ssector1, esector1,
165 (u_long)dp->dp_size, error ? "" : ": OK");
166 if (ssector != ssector1 && bootverbose)
6ea70f76 167 kprintf("%s: C/H/S start %d/%d/%d (%lu) != start %lu: invalid\n",
984263bc
MD
168 sname, chs_scyl, dp->dp_shd, chs_ssect,
169 ssector, ssector1);
170 if (esector != esector1 && bootverbose)
6ea70f76 171 kprintf("%s: C/H/S end %d/%d/%d (%lu) != end %lu: invalid\n",
984263bc
MD
172 sname, chs_ecyl, dp->dp_ehd, chs_esect,
173 esector, esector1);
174 return (error);
175}
176
177int
b13267a5 178dsinit(cdev_t dev, struct disklabel *lp, struct diskslices **sspp)
984263bc
MD
179{
180 struct buf *bp;
181 u_char *cp;
182 int dospart;
183 struct dos_partition *dp;
184 struct dos_partition *dp0;
185 struct dos_partition dpcopy[NDOSPART];
186 int error;
187 int max_ncyls;
188 int max_nsectors;
189 int max_ntracks;
190 u_long mbr_offset;
191 char partname[2];
192 u_long secpercyl;
193 char *sname;
194 struct diskslice *sp;
195 struct diskslices *ssp;
b13267a5 196 cdev_t wdev;
984263bc
MD
197
198 mbr_offset = DOSBBSECTOR;
199reread_mbr:
200 /* Read master boot record. */
7843d540 201 wdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
984263bc 202 bp = geteblk((int)lp->d_secsize);
54078292 203 bp->b_bio1.bio_offset = (off_t)mbr_offset * lp->d_secsize;
984263bc 204 bp->b_bcount = lp->d_secsize;
10f3fee5 205 bp->b_cmd = BUF_CMD_READ;
81b5c339 206 dev_dstrategy(wdev, &bp->b_bio1);
984263bc 207 if (biowait(bp) != 0) {
81b5c339
MD
208 diskerr(&bp->b_bio1, wdev,
209 "reading primary partition table: error",
54078292 210 LOG_PRINTF, 0, NULL);
6ea70f76 211 kprintf("\n");
984263bc
MD
212 error = EIO;
213 goto done;
214 }
215
216 /* Weakly verify it. */
217 cp = bp->b_data;
218 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, partname);
219 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
220 if (bootverbose)
6ea70f76 221 kprintf("%s: invalid primary partition table: no magic\n",
984263bc
MD
222 sname);
223 error = EINVAL;
224 goto done;
225 }
226
227 /* Make a copy of the partition table to avoid alignment problems. */
228 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
229
230 dp0 = &dpcopy[0];
231
232 /* Check for "Ontrack Diskmanager". */
233 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
234 if (dp->dp_typ == DOSPTYP_ONTRACK) {
235 if (bootverbose)
6ea70f76 236 kprintf(
984263bc
MD
237 "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
238 bp->b_flags |= B_INVAL | B_AGE;
239 brelse(bp);
240 mbr_offset = 63;
241 goto reread_mbr;
242 }
243 }
244
245 if (bcmp(dp0, historical_bogus_partition_table,
4ac7c869
MD
246 sizeof historical_bogus_partition_table) == 0 ||
247 bcmp(dp0, historical_bogus_partition_table_fixed,
248 sizeof historical_bogus_partition_table_fixed) == 0) {
249#if 0
984263bc
MD
250 TRACE(("%s: invalid primary partition table: historical\n",
251 sname));
4ac7c869
MD
252#endif /* 0 */
253 if (bootverbose)
6ea70f76 254 kprintf(
4ac7c869
MD
255 "%s: invalid primary partition table: Dangerously Dedicated (ignored)\n",
256 sname);
984263bc
MD
257 error = EINVAL;
258 goto done;
259 }
260
261 /* Guess the geometry. */
262 /*
263 * TODO:
264 * Perhaps skip entries with 0 size.
265 * Perhaps only look at entries of type DOSPTYP_386BSD.
266 */
267 max_ncyls = 0;
268 max_nsectors = 0;
269 max_ntracks = 0;
270 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
271 int ncyls;
272 int nsectors;
273 int ntracks;
274
275 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
276 if (max_ncyls < ncyls)
277 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;
284 }
285
286 /*
287 * Check that we have guessed the geometry right by checking the
288 * partition entries.
289 */
290 /*
291 * TODO:
292 * As above.
293 * Check for overlaps.
294 * Check against d_secperunit if the latter is reliable.
295 */
296 error = 0;
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)
300 continue;
301 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
302 RAW_PART, partname);
303
304 /*
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.
310 */
311 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
312 mbr_offset);
313 }
314 if (error != 0)
315 goto done;
316
317 /*
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).
321 */
322 secpercyl = (u_long)max_nsectors * max_ntracks;
323 if (secpercyl != 0) {
324#if 0
325 u_long secperunit;
326#endif
327
328 lp->d_nsectors = max_nsectors;
329 lp->d_ntracks = max_ntracks;
330 lp->d_secpercyl = secpercyl;
331 /*
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.
338 */
339#if 0
340 secperunit = secpercyl * max_ncyls;
341 if (lp->d_secperunit < secperunit)
342 lp->d_secperunit = secperunit;
343#endif
344 lp->d_ncylinders = lp->d_secperunit / secpercyl;
345 }
346
347 /*
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
352 * inconvenient.
353 */
efda3bd0 354 kfree(*sspp, M_DEVBUF);
984263bc
MD
355 ssp = dsmakeslicestruct(MAX_SLICES, lp);
356 *sspp = ssp;
357
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,
362 RAW_PART, partname);
363 (void)mbr_setslice(sname, lp, sp, dp, mbr_offset);
364 }
365 ssp->dss_nslices = BASE_SLICE + NDOSPART;
366
367 /* Handle extended partitions. */
368 sp -= NDOSPART;
a0c19955 369 for (dospart = 0; dospart < NDOSPART; dospart++, sp++) {
984263bc 370 if (sp->ds_type == DOSPTYP_EXTENDED ||
a0c19955 371 sp->ds_type == DOSPTYP_EXTENDEDX) {
7843d540 372 mbr_extended(wdev, lp, ssp,
984263bc
MD
373 sp->ds_offset, sp->ds_size, sp->ds_offset,
374 max_nsectors, max_ntracks, mbr_offset, 1);
a0c19955
MD
375 }
376 }
984263bc
MD
377
378 /*
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.
382 */
383 if (ssp->dss_nslices > MAX_SLICES)
384 ssp->dss_nslices = MAX_SLICES;
385
386done:
387 bp->b_flags |= B_INVAL | B_AGE;
388 brelse(bp);
389 if (error == EINVAL)
390 error = 0;
391 return (error);
392}
393
394void
b13267a5 395mbr_extended(cdev_t dev, struct disklabel *lp, struct diskslices *ssp,
a0c19955
MD
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)
984263bc
MD
398{
399 struct buf *bp;
400 u_char *cp;
401 int dospart;
402 struct dos_partition *dp;
403 struct dos_partition dpcopy[NDOSPART];
404 u_long ext_offsets[NDOSPART];
405 u_long ext_sizes[NDOSPART];
406 char partname[2];
407 int slice;
408 char *sname;
409 struct diskslice *sp;
410
411 if (level >= 16) {
6ea70f76 412 kprintf(
984263bc
MD
413 "%s: excessive recursion in search for slices; aborting search\n",
414 devtoname(dev));
415 return;
416 }
417
418 /* Read extended boot record. */
419 bp = geteblk((int)lp->d_secsize);
54078292 420 bp->b_bio1.bio_offset = (off_t)ext_offset * lp->d_secsize;
984263bc 421 bp->b_bcount = lp->d_secsize;
10f3fee5 422 bp->b_cmd = BUF_CMD_READ;
81b5c339 423 dev_dstrategy(dev, &bp->b_bio1);
984263bc 424 if (biowait(bp) != 0) {
81b5c339
MD
425 diskerr(&bp->b_bio1, dev,
426 "reading extended partition table: error",
54078292 427 LOG_PRINTF, 0, NULL);
6ea70f76 428 kprintf("\n");
984263bc
MD
429 goto done;
430 }
431
432 /* Weakly verify it. */
433 cp = bp->b_data;
434 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
435 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART,
436 partname);
437 if (bootverbose)
6ea70f76 438 kprintf("%s: invalid extended partition table: no magic\n",
984263bc
MD
439 sname);
440 goto done;
441 }
442
443 /* Make a copy of the partition table to avoid alignment problems. */
444 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
445
446 slice = ssp->dss_nslices;
447 for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
448 dospart++, dp++) {
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)
452 continue;
453 if (dp->dp_typ == DOSPTYP_EXTENDED ||
454 dp->dp_typ == DOSPTYP_EXTENDEDX) {
455 static char buf[32];
456
457 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
458 RAW_PART, partname);
f8c7a42d 459 ksnprintf(buf, sizeof(buf), "%s", sname);
984263bc
MD
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;
466 } else {
467 sname = dsname(dev, dkunit(dev), slice, RAW_PART,
468 partname);
469 check_part(sname, dp, ext_offset, nsectors, ntracks,
470 mbr_offset);
471 if (slice >= MAX_SLICES) {
6ea70f76 472 kprintf("%s: too many slices\n", sname);
984263bc
MD
473 slice++;
474 continue;
475 }
476 sp = &ssp->dss_slices[slice];
477 if (mbr_setslice(sname, lp, sp, dp, ext_offset) != 0)
478 continue;
479 slice++;
480 }
481 }
482 ssp->dss_nslices = slice;
483
484 /* If we found any more slices, recursively find all the subslices. */
a0c19955
MD
485 for (dospart = 0; dospart < NDOSPART; dospart++) {
486 if (ext_sizes[dospart] != 0) {
984263bc
MD
487 mbr_extended(dev, lp, ssp, ext_offsets[dospart],
488 ext_sizes[dospart], base_ext_offset,
489 nsectors, ntracks, mbr_offset, ++level);
a0c19955
MD
490 }
491 }
984263bc
MD
492
493done:
494 bp->b_flags |= B_INVAL | B_AGE;
495 brelse(bp);
496}
497
498static int
a0c19955
MD
499mbr_setslice(char *sname, struct disklabel *lp, struct diskslice *sp,
500 struct dos_partition *dp, u_long br_offset)
984263bc
MD
501{
502 u_long offset;
503 u_long size;
504
505 offset = br_offset + dp->dp_start;
506 if (offset > lp->d_secperunit || offset < br_offset) {
6ea70f76 507 kprintf(
984263bc
MD
508 "%s: slice starts beyond end of the disk: rejecting it\n",
509 sname);
510 return (1);
511 }
512 size = lp->d_secperunit - offset;
513 if (size >= dp->dp_size)
514 size = dp->dp_size;
515 else
6ea70f76 516 kprintf(
984263bc
MD
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;
520 sp->ds_size = size;
521 sp->ds_type = dp->dp_typ;
984263bc
MD
522#if 0
523 lp->d_subtype |= (lp->d_subtype & 3) | dospart | DSTYPE_INDOSPART;
524#endif
525 return (0);
526}