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 $
42 * PC9801 port by KATO Takenor <kato@eclogite.eps.nagoya-u.ac.jp>
45 #include "compat_atdisk.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
51 #include <sys/disklabel.h>
53 #define DOSPTYP_EXTENDED 5
54 #define DOSPTYP_EXTENDEDX 15
55 #define DOSPTYP_ONTRACK 84
57 #include <sys/diskslice.h>
58 #include <sys/malloc.h>
59 #include <sys/syslog.h>
61 #define TRACE(str) do { if (dsi_debug) printf str; } while (0)
63 static volatile u_char dsi_debug;
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, },
74 static int check_part __P((char *sname, struct dos_partition *dp,
75 u_long offset, int nsectors, int ntracks,
78 static void mbr_extended __P((dev_t dev, struct disklabel *lp,
79 struct diskslices *ssp, u_long ext_offset,
80 u_long ext_size, u_long base_ext_offset,
81 int nsectors, int ntracks, u_long mbr_offset,
86 #define DPBLKNO(cyl,hd,sect) ((cyl)*(lp->d_secpercyl))
87 #if NCOMPAT_ATDISK > 0
88 int atcompat_dsinit __P((dev_t dev,
89 struct disklabel *lp, struct diskslices **sspp));
94 check_part(sname, dp, offset, nsectors, ntracks, mbr_offset )
96 struct dos_partition *dp;
117 secpercyl = (u_long)nsectors * ntracks;
119 chs_scyl = dp->dp_scyl;
120 chs_ssect = dp->dp_ssect;
121 ssector = chs_ssect + dp->dp_shd * nsectors +
122 chs_scyl * secpercyl + mbr_offset;
124 chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
125 chs_ssect = DPSECT(dp->dp_ssect);
126 ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl
130 pc98_start = dp->dp_scyl * secpercyl;
131 pc98_size = dp->dp_ecyl ?
132 (dp->dp_ecyl + 1) * secpercyl - pc98_start : 0;
133 ssector1 = offset + pc98_start;
135 ssector1 = offset + dp->dp_start;
139 * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
140 * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
141 * apart from the cylinder being reduced modulo 1024. Always allow
144 if ((ssector < ssector1
145 && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
148 && (ssector1 - ssector) % (1024 * secpercyl) == 0)))
149 || (dp->dp_scyl == 255 && dp->dp_shd == 255
150 && dp->dp_ssect == 255)) {
151 TRACE(("%s: C/H/S start %d/%d/%d, start %lu: allow\n",
152 sname, chs_scyl, dp->dp_shd, chs_ssect, ssector1));
157 chs_ecyl = dp->dp_ecyl;
158 chs_esect = nsectors - 1;
159 esector = chs_esect + (ntracks - 1) * nsectors +
160 chs_ecyl * secpercyl + mbr_offset;
161 esector1 = ssector1 + pc98_size - 1;
163 chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect);
164 chs_esect = DPSECT(dp->dp_esect);
165 esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl
167 esector1 = ssector1 + dp->dp_size - 1;
170 /* Allow certain bogus C/H/S values for esector, as above. */
171 if ((esector < esector1
172 && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
175 && (esector1 - esector) % (1024 * secpercyl) == 0)))
176 || (dp->dp_ecyl == 255 && dp->dp_ehd == 255
177 && dp->dp_esect == 255)) {
178 TRACE(("%s: C/H/S end %d/%d/%d, end %lu: allow\n",
179 sname, chs_ecyl, dp->dp_ehd, chs_esect, esector1));
183 error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
186 printf("%s: mid 0x%x, start %lu, end = %lu, size %lu%s\n",
187 sname, dp->dp_mid, ssector1, esector1, pc98_size,
188 error ? "" : ": OK");
190 printf("%s: type 0x%x, start %lu, end = %lu, size %lu %s\n",
191 sname, dp->dp_typ, ssector1, esector1,
192 (u_long)dp->dp_size, error ? "" : ": OK");
194 if (ssector != ssector1 && bootverbose)
195 printf("%s: C/H/S start %d/%d/%d (%lu) != start %lu: invalid\n",
196 sname, chs_scyl, dp->dp_shd, chs_ssect,
198 if (esector != esector1 && bootverbose)
199 printf("%s: C/H/S end %d/%d/%d (%lu) != end %lu: invalid\n",
200 sname, chs_ecyl, dp->dp_ehd, chs_esect,
206 dsinit(dev, lp, sspp)
208 struct disklabel *lp;
209 struct diskslices **sspp;
214 struct dos_partition *dp;
215 struct dos_partition *dp0;
224 struct diskslice *sp;
225 struct diskslices *ssp;
231 mbr_offset = DOSBBSECTOR;
233 /* Read master boot record. */
234 if ((int)lp->d_secsize < 1024)
235 bp = geteblk((int)1024);
237 bp = geteblk((int)lp->d_secsize);
240 /* Read master boot record. */
241 bp = geteblk((int)lp->d_secsize);
243 bp->b_dev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
244 bp->b_blkno = mbr_offset;
245 bp->b_bcount = lp->d_secsize;
246 bp->b_flags |= B_READ;
248 if (bp->b_bcount < 1024)
252 if (biowait(bp) != 0) {
253 diskerr(bp, "reading primary partition table: error",
254 LOG_PRINTF, 0, (struct disklabel *)NULL);
260 /* Weakly verify it. */
262 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, partname);
263 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
265 printf("%s: invalid primary partition table: no magic\n",
272 * entire disk for FreeBSD
274 if ((*(cp + 512) == 0x57) && (*(cp + 513) == 0x45) &&
275 (*(cp + 514) == 0x56) && (*(cp + 515) == 0x82)) {
276 sname = dsname(dev, dkunit(dev), BASE_SLICE,
278 free(*sspp, M_DEVBUF);
279 ssp = dsmakeslicestruct(MAX_SLICES, lp);
282 /* Initialize normal slices. */
283 sp = &ssp->dss_slices[BASE_SLICE];
285 sp->ds_size = lp->d_secperunit;
286 sp->ds_type = DOSPTYP_386BSD;
287 sp->ds_subtype = 0xc4;
289 ssp->dss_nslices = BASE_SLICE + 1;
296 if ((*(cp + 0x0e) == 1) && (*(cp + 0x15) == 0xf0) &&
297 (*(cp + 0x1c) == 0x0) &&
298 ((*(cp + 512) == 0xf0) || (*(cp + 512) == 0xf8)) &&
299 (*(cp + 513) == 0xff) && (*(cp + 514) == 0xff)) {
300 sname = dsname(dev, dkunit(dev), BASE_SLICE,
302 free(*sspp, M_DEVBUF);
303 ssp = dsmakeslicestruct(MAX_SLICES, lp);
306 /* Initialize normal slices. */
307 sp = &ssp->dss_slices[BASE_SLICE];
309 sp->ds_size = lp->d_secperunit;
310 sp->ds_type = 0xa0; /* XXX */
311 sp->ds_subtype = 0x81; /* XXX */
313 ssp->dss_nslices = BASE_SLICE + 1;
316 #if NCOMPAT_ATDISK > 0
318 * Check magic number of 'extended format' for PC-9801.
319 * If no magic, it may be formatted on IBM-PC.
321 if (((cp[4] != 'I') || (cp[5] != 'P') || (cp[6] != 'L') ||
323 ((strncmp(devtoname(bp->b_dev), "da", 2) == 0) ||
324 (strncmp(devtoname(bp->b_dev), "wd", 2) == 0))) {
326 bp->b_flags |= B_INVAL | B_AGE;
328 return atcompat_dsinit(dev, lp, sspp);
331 dp0 = (struct dos_partition *)(cp + 512);
333 dp0 = (struct dos_partition *)(cp + DOSPARTOFF);
335 /* Check for "Ontrack Diskmanager". */
336 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
337 if (dp->dp_typ == DOSPTYP_ONTRACK) {
340 "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
341 bp->b_flags |= B_INVAL | B_AGE;
348 if (bcmp(dp0, historical_bogus_partition_table,
349 sizeof historical_bogus_partition_table) == 0) {
350 TRACE(("%s: invalid primary partition table: historical\n",
357 /* Guess the geometry. */
360 * Perhaps skip entries with 0 size.
361 * Perhaps only look at entries of type DOSPTYP_386BSD.
366 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
373 ncyls = lp->d_ncylinders;
375 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
377 if (max_ncyls < ncyls)
380 nsectors = lp->d_nsectors;
382 nsectors = DPSECT(dp->dp_esect);
384 if (max_nsectors < nsectors)
385 max_nsectors = nsectors;
387 ntracks = lp->d_ntracks;
389 ntracks = dp->dp_ehd + 1;
391 if (max_ntracks < ntracks)
392 max_ntracks = ntracks;
396 * Check that we have guessed the geometry right by checking the
402 * Check for overlaps.
403 * Check against d_secperunit if the latter is reliable.
407 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
408 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0)
410 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
413 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
414 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
415 && dp->dp_start == 0 && dp->dp_size == 0)
417 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
421 * Temporarily ignore errors from this check. We could
422 * simplify things by accepting the table eariler if we
423 * always ignore errors here. Perhaps we should always
424 * accept the table if the magic is right but not let
425 * bad entries affect the geometry.
427 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
434 * Accept the DOS partition table.
435 * First adjust the label (we have been careful not to change it
436 * before we can guarantee success).
438 secpercyl = (u_long)max_nsectors * max_ntracks;
439 if (secpercyl != 0) {
442 lp->d_nsectors = max_nsectors;
443 lp->d_ntracks = max_ntracks;
444 lp->d_secpercyl = secpercyl;
445 secperunit = secpercyl * max_ncyls;
446 if (lp->d_secperunit < secperunit)
447 lp->d_secperunit = secperunit;
448 lp->d_ncylinders = lp->d_secperunit / secpercyl;
452 * We are passed a pointer to a suitably initialized minimal
453 * slices "struct" with no dangling pointers in it. Replace it
454 * by a maximal one. This usually oversizes the "struct", but
455 * enlarging it while searching for logical drives would be
458 free(*sspp, M_DEVBUF);
459 ssp = dsmakeslicestruct(MAX_SLICES, lp);
462 /* Initialize normal slices. */
463 sp = &ssp->dss_slices[BASE_SLICE];
464 for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) {
466 pc98_start = DPBLKNO(dp->dp_scyl,dp->dp_shd,dp->dp_ssect);
467 pc98_size = dp->dp_ecyl ? DPBLKNO(dp->dp_ecyl+1,dp->dp_ehd,dp->dp_esect) - pc98_start : 0;
468 sp->ds_offset = pc98_start;
469 sp->ds_size = pc98_size;
470 sp->ds_type = dp->dp_mid;
471 sp->ds_subtype = dp->dp_sid;
472 strncpy(sp->ds_name, dp->dp_name, sizeof(sp->ds_name));
474 sp->ds_offset = mbr_offset + dp->dp_start;
475 sp->ds_size = dp->dp_size;
476 sp->ds_type = dp->dp_typ;
479 lp->d_subtype |= (lp->d_subtype & 3) | dospart
483 ssp->dss_nslices = BASE_SLICE + NDOSPART;
486 /* Handle extended partitions. */
488 for (dospart = 0; dospart < NDOSPART; dospart++, sp++)
489 if (sp->ds_type == DOSPTYP_EXTENDED ||
490 sp->ds_type == DOSPTYP_EXTENDEDX)
491 mbr_extended(bp->b_dev, lp, ssp,
492 sp->ds_offset, sp->ds_size, sp->ds_offset,
493 max_nsectors, max_ntracks, mbr_offset, 1);
496 * mbr_extended() abuses ssp->dss_nslices for the number of slices
497 * that would be found if there were no limit on the number of slices
498 * in *ssp. Cut it back now.
500 if (ssp->dss_nslices > MAX_SLICES)
501 ssp->dss_nslices = MAX_SLICES;
505 bp->b_flags |= B_INVAL | B_AGE;
514 mbr_extended(dev, lp, ssp, ext_offset, ext_size, base_ext_offset, nsectors,
515 ntracks, mbr_offset, level)
517 struct disklabel *lp;
518 struct diskslices *ssp;
521 u_long base_ext_offset;
530 struct dos_partition *dp;
531 struct dos_partition dpcopy[NDOSPART];
532 u_long ext_offsets[NDOSPART];
533 u_long ext_sizes[NDOSPART];
537 struct diskslice *sp;
541 "%s: excessive recursion in search for slices; aborting search\n",
546 /* Read extended boot record. */
547 bp = geteblk((int)lp->d_secsize);
549 bp->b_blkno = ext_offset;
550 bp->b_bcount = lp->d_secsize;
551 bp->b_flags |= B_READ;
553 if (biowait(bp) != 0) {
554 diskerr(bp, "reading extended partition table: error",
555 LOG_PRINTF, 0, (struct disklabel *)NULL);
560 /* Weakly verify it. */
562 if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
563 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART,
566 printf("%s: invalid extended partition table: no magic\n",
571 /* Make a copy of the partition table to avoid alignment problems. */
572 memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
574 slice = ssp->dss_nslices;
575 for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
577 ext_sizes[dospart] = 0;
578 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
579 && dp->dp_start == 0 && dp->dp_size == 0)
581 if (dp->dp_typ == DOSPTYP_EXTENDED ||
582 dp->dp_typ == DOSPTYP_EXTENDEDX) {
585 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
587 snprintf(buf, sizeof(buf), "%s", sname);
588 if (strlen(buf) < sizeof buf - 11)
589 strcat(buf, "<extended>");
590 check_part(buf, dp, base_ext_offset, nsectors,
591 ntracks, mbr_offset);
592 ext_offsets[dospart] = base_ext_offset + dp->dp_start;
593 ext_sizes[dospart] = dp->dp_size;
595 sname = dsname(dev, dkunit(dev), slice, RAW_PART,
597 check_part(sname, dp, ext_offset, nsectors, ntracks,
599 if (slice >= MAX_SLICES) {
600 printf("%s: too many slices\n", sname);
604 sp = &ssp->dss_slices[slice];
605 sp->ds_offset = ext_offset + dp->dp_start;
606 sp->ds_size = dp->dp_size;
607 sp->ds_type = dp->dp_typ;
609 /* Fake FreeBSD(98). */
610 if (sp->ds_type == DOSPTYP_386BSD)
616 ssp->dss_nslices = slice;
618 /* If we found any more slices, recursively find all the subslices. */
619 for (dospart = 0; dospart < NDOSPART; dospart++)
620 if (ext_sizes[dospart] != 0)
621 mbr_extended(dev, lp, ssp, ext_offsets[dospart],
622 ext_sizes[dospart], base_ext_offset,
623 nsectors, ntracks, mbr_offset, ++level);
626 bp->b_flags |= B_INVAL | B_AGE;
633 alpha_fix_srm_checksum(bp)
640 p = (u_int64_t *) bp->b_data;
642 for (i = 0; i < 63; i++)