4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2011 Gary Mills
24 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * fsck_pcfs -- common.c
30 * All the routines in this file are being swiped directly from
31 * mkfs_pcfs. Eventually this file should only exist in one place
32 * and be part of a library that both mkfs and fsck link against.
39 #include <sys/isa_defs.h>
40 #include <sys/types.h>
42 #include <sys/fcntl.h>
43 #include <sys/dktp/fdisk.h>
44 #include <sys/fs/pc_fs.h>
45 #include <sys/fs/pc_dir.h>
46 #include <sys/fs/pc_label.h>
47 #include "fsck_pcfs.h"
48 #include "pcfs_common.h"
52 * The assumption here is that _BIG_ENDIAN implies sparc, and
53 * so in addition to swapping bytes we also have to construct
54 * packed structures by hand to avoid bus errors due to improperly
58 void swap_pack_grab32bpb(bpb_t
*wbpb
, struct _boot_sector
*bsp
);
59 void swap_pack_grabbpb(bpb_t
*wbpb
, struct _boot_sector
*bsp
);
60 #endif /* _BIG_ENDIAN */
63 * Global variables related to input questions
70 * Save the lower 16 bits of a 32 bit value (v) into the provided
71 * buffer (pointed at by *bp), and increment the buffer pointer
72 * as well. This way the routine can be called multiple times in
73 * succession to fill buffers. The value is stored in little-endian
77 store_16_bits(uchar_t
**bp
, uint32_t v
)
87 read_16_bits(uchar_t
*bp
, uint32_t *value
)
95 * Save the 32 bit value (v) into the provided buffer (pointed
96 * at by *bp), and increment the buffer pointer as well. This way
97 * the routine can be called multiple times in succession to fill
98 * buffers. The value is stored in little-endian order.
101 store_32_bits(uchar_t
**bp
, uint32_t v
)
106 for (b
= 0; b
< 4; b
++) {
114 read_32_bits(uchar_t
*bp
, uint32_t *value
)
117 *value
+= *bp
++ << 8;
118 *value
+= *bp
++ << 16;
119 *value
+= *bp
++ << 24;
123 * dump_bytes -- display bytes as hex numbers.
124 * b is the pointer to the byte buffer
125 * n is the number of bytes in the buffer
127 /* Note: BPL = bytes to display per line */
131 dump_bytes(uchar_t
*buf
, int n
)
139 /* Display offset, 16 bytes per line, and printable ascii version */
140 while (countdown
> 0) {
142 (void) fprintf(stderr
, "\n%06x: ", offset
);
144 * Print Hex value of characters in columns on left
146 for (byte
= 0; byte
< BPL
; byte
++) {
147 if (countup
+ byte
< n
) {
148 (void) fprintf(stderr
,
149 "%02x ", (buf
[countup
+ byte
] & 0xff));
152 (void) fprintf(stderr
, " ");
156 * Right side has the printable character or '.' for
157 * unprintable for each column of the left.
159 for (byte
= 0; byte
< BPL
; byte
++) {
160 if ((countup
+ byte
< n
) &&
161 ((buf
[countup
+ byte
] >= ' ') &&
162 (buf
[countup
+ byte
] <= '~'))) {
163 (void) fprintf(stderr
, "%c",
164 buf
[countup
+ byte
]);
166 (void) fprintf(stderr
, ".");
169 countup
+= printedCount
;
170 offset
+= printedCount
;
171 countdown
-= printedCount
;
173 (void) fprintf(stderr
, "\n\n");
177 * header_for_dump -- display simple header over what will be output.
180 header_for_dump(void)
184 (void) fprintf(stderr
, "\n ");
185 for (byte
= 0; byte
< BPL
; byte
++)
186 (void) fprintf(stderr
, "%02x ", byte
);
187 (void) fprintf(stderr
, "\n ");
190 (void) fprintf(stderr
, "-");
194 * We are basically (incorrectly) assuming that if you aren't running
195 * on x86 the BPB has to be packed by hand AND that the bytes must
196 * be swapped. One or both of these assumptions may one day be invalid.
197 * (if they aren't already :-))
201 * swap_pack_grab{32}bpb
202 * If not on an x86 we assume the structures making up the bpb
203 * were not packed and that longs and shorts need to be byte swapped
204 * (we've kept everything in host order up until now). A new architecture
205 * might not need to swap or might not need to pack, in which case
206 * new routines will have to be written. Of course if an architecture
207 * supports both packing and little-endian host order, it can follow the
208 * same path as the x86 code.
211 swap_pack_grabbpb(bpb_t
*wbpb
, struct _boot_sector
*bsp
)
215 grabp
= (uchar_t
*)&(bsp
->bs_filler
[ORIG_BPB_START_INDEX
]);
217 ((uchar_t
*)&(wbpb
->bpb
.bytes_per_sector
))[1] = *grabp
++;
218 ((uchar_t
*)&(wbpb
->bpb
.bytes_per_sector
))[0] = *grabp
++;
219 wbpb
->bpb
.sectors_per_cluster
= *grabp
++;
220 ((uchar_t
*)&(wbpb
->bpb
.resv_sectors
))[1] = *grabp
++;
221 ((uchar_t
*)&(wbpb
->bpb
.resv_sectors
))[0] = *grabp
++;
222 wbpb
->bpb
.num_fats
= *grabp
++;
223 ((uchar_t
*)&(wbpb
->bpb
.num_root_entries
))[1] = *grabp
++;
224 ((uchar_t
*)&(wbpb
->bpb
.num_root_entries
))[0] = *grabp
++;
225 ((uchar_t
*)&(wbpb
->bpb
.sectors_in_volume
))[1] = *grabp
++;
226 ((uchar_t
*)&(wbpb
->bpb
.sectors_in_volume
))[0] = *grabp
++;
227 wbpb
->bpb
.media
= *grabp
++;
228 ((uchar_t
*)&(wbpb
->bpb
.sectors_per_fat
))[1] = *grabp
++;
229 ((uchar_t
*)&(wbpb
->bpb
.sectors_per_fat
))[0] = *grabp
++;
230 ((uchar_t
*)&(wbpb
->bpb
.sectors_per_track
))[1] = *grabp
++;
231 ((uchar_t
*)&(wbpb
->bpb
.sectors_per_track
))[0] = *grabp
++;
232 ((uchar_t
*)&(wbpb
->bpb
.heads
))[1] = *grabp
++;
233 ((uchar_t
*)&(wbpb
->bpb
.heads
))[0] = *grabp
++;
234 ((uchar_t
*)&(wbpb
->bpb
.hidden_sectors
))[3] = *grabp
++;
235 ((uchar_t
*)&(wbpb
->bpb
.hidden_sectors
))[2] = *grabp
++;
236 ((uchar_t
*)&(wbpb
->bpb
.hidden_sectors
))[1] = *grabp
++;
237 ((uchar_t
*)&(wbpb
->bpb
.hidden_sectors
))[0] = *grabp
++;
238 ((uchar_t
*)&(wbpb
->bpb
.sectors_in_logical_volume
))[3] = *grabp
++;
239 ((uchar_t
*)&(wbpb
->bpb
.sectors_in_logical_volume
))[2] = *grabp
++;
240 ((uchar_t
*)&(wbpb
->bpb
.sectors_in_logical_volume
))[1] = *grabp
++;
241 ((uchar_t
*)&(wbpb
->bpb
.sectors_in_logical_volume
))[0] = *grabp
++;
242 wbpb
->ebpb
.phys_drive_num
= *grabp
++;
243 wbpb
->ebpb
.reserved
= *grabp
++;
244 wbpb
->ebpb
.ext_signature
= *grabp
++;
245 ((uchar_t
*)&(wbpb
->ebpb
.volume_id
))[3] = *grabp
++;
246 ((uchar_t
*)&(wbpb
->ebpb
.volume_id
))[2] = *grabp
++;
247 ((uchar_t
*)&(wbpb
->ebpb
.volume_id
))[1] = *grabp
++;
248 ((uchar_t
*)&(wbpb
->ebpb
.volume_id
))[0] = *grabp
++;
250 (void) strncpy((char *)wbpb
->ebpb
.volume_label
, (char *)grabp
, 11);
252 (void) strncpy((char *)wbpb
->ebpb
.type
, (char *)grabp
, 8);
256 swap_pack_grab32bpb(bpb_t
*wbpb
, struct _boot_sector
*bsp
)
260 grabp
= (uchar_t
*)&(bsp
->bs_filler
[BPB_32_START_INDEX
]);
262 ((uchar_t
*)&(wbpb
->bpb32
.big_sectors_per_fat
))[3] = *grabp
++;
263 ((uchar_t
*)&(wbpb
->bpb32
.big_sectors_per_fat
))[2] = *grabp
++;
264 ((uchar_t
*)&(wbpb
->bpb32
.big_sectors_per_fat
))[1] = *grabp
++;
265 ((uchar_t
*)&(wbpb
->bpb32
.big_sectors_per_fat
))[0] = *grabp
++;
266 ((uchar_t
*)&(wbpb
->bpb32
.ext_flags
))[1] = *grabp
++;
267 ((uchar_t
*)&(wbpb
->bpb32
.ext_flags
))[0] = *grabp
++;
268 wbpb
->bpb32
.fs_vers_lo
= *grabp
++;
269 wbpb
->bpb32
.fs_vers_hi
= *grabp
++;
270 ((uchar_t
*)&(wbpb
->bpb32
.root_dir_clust
))[3] = *grabp
++;
271 ((uchar_t
*)&(wbpb
->bpb32
.root_dir_clust
))[2] = *grabp
++;
272 ((uchar_t
*)&(wbpb
->bpb32
.root_dir_clust
))[1] = *grabp
++;
273 ((uchar_t
*)&(wbpb
->bpb32
.root_dir_clust
))[0] = *grabp
++;
274 ((uchar_t
*)&(wbpb
->bpb32
.fsinfosec
))[1] = *grabp
++;
275 ((uchar_t
*)&(wbpb
->bpb32
.fsinfosec
))[0] = *grabp
++;
276 ((uchar_t
*)&(wbpb
->bpb32
.backupboot
))[1] = *grabp
++;
277 ((uchar_t
*)&(wbpb
->bpb32
.backupboot
))[0] = *grabp
++;
278 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[0]))[1] = *grabp
++;
279 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[0]))[0] = *grabp
++;
280 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[1]))[1] = *grabp
++;
281 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[1]))[0] = *grabp
++;
282 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[2]))[1] = *grabp
++;
283 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[2]))[0] = *grabp
++;
284 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[3]))[1] = *grabp
++;
285 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[3]))[0] = *grabp
++;
286 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[4]))[1] = *grabp
++;
287 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[4]))[0] = *grabp
++;
288 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[5]))[1] = *grabp
++;
289 ((uchar_t
*)&(wbpb
->bpb32
.reserved
[5]))[0] = *grabp
++;
291 #endif /* _BIG_ENDIAN */
296 char *affirmative
= gettext("yY");
297 char *a
= affirmative
;
301 (void) printf("y\n");
303 } else if (AlwaysNo
) {
304 (void) printf("n\n");
307 if (fgets(input
, sizeof (input
), stdin
) == NULL
) {
309 (void) printf("n\n");
313 if (input
[0] == (int)*a
)
321 stat_actual_disk(char *diskname
, struct stat
*info
, char **suffix
)
325 if (stat(diskname
, info
)) {
327 * Device named on command line doesn't exist. That
328 * probably means there is a partition-specifying
329 * suffix attached to the actual disk name.
331 if ((actualdisk
= strdup(diskname
)) == NULL
) {
332 (void) fprintf(stderr
,
333 gettext("Out of memory for disk name.\n"));
336 if ((*suffix
= strchr(actualdisk
, ':')) != NULL
) {
341 if (stat(actualdisk
, info
)) {
346 if ((actualdisk
= strdup(diskname
)) == NULL
) {
347 (void) fprintf(stderr
,
348 gettext("Out of memory for disk name.\n"));
356 extern void usage(void);
359 bad_arg(char *option
)
361 (void) fprintf(stderr
,
362 gettext("Unrecognized option -o %s.\n"), option
);
368 missing_arg(char *option
)
370 (void) fprintf(stderr
,
371 gettext("Option %s requires a value.\n"), option
);
377 parse_drvnum(char *pn
)
382 * Determine logical drive to seek after.
384 if ((strlen(pn
) == 1) && ((*pn
>= 'c') && (*pn
<= 'z'))) {
385 drvnum
= *pn
- 'c' + 1;
386 } else if ((*pn
>= '0') && (*pn
<= '9')) {
391 while ((*d
!= '\0') && (*d
>= '0') && (*d
<= '9')) {
396 if ((*d
!= '\0') || (v
> 24)) {
397 (void) fprintf(stderr
,
398 gettext("%s: bogus logical drive specification.\n"),
403 } else if (strcmp(pn
, "boot") == 0) {
406 (void) fprintf(stderr
,
407 gettext("%s: bogus logical drive specification.\n"), pn
);
416 * Boolean function. Give it the systid field for an fdisk partition
417 * and it decides if that's a systid that describes a DOS drive. We
418 * use systid values defined in sys/dktp/fdisk.h.
421 isDosDrive(uchar_t checkMe
)
423 return ((checkMe
== DOSOS12
) || (checkMe
== DOSOS16
) ||
424 (checkMe
== DOSHUGE
) || (checkMe
== FDISK_WINDOWS
) ||
425 (checkMe
== FDISK_EXT_WIN
) || (checkMe
== FDISK_FAT95
) ||
426 (checkMe
== DIAGPART
));
431 * Boolean function. Give it the systid field for an fdisk partition
432 * and it decides if that's a systid that describes an extended DOS
436 isDosExtended(uchar_t checkMe
)
438 return ((checkMe
== EXTDOS
) || (checkMe
== FDISK_EXTLBA
));
443 * Boolean function. Give it the systid field for an fdisk partition
444 * and it decides if that's a systid that describes a Solaris boot
448 isBootPart(uchar_t checkMe
)
450 return (checkMe
== X86BOOT
);
454 findPartitionOffset(int fd
, char *ldrive
)
456 struct ipart part
[FD_NUMPART
];
457 struct mboot extmboot
;
459 diskaddr_t xstartsect
;
460 off64_t nextseek
= 0;
461 off64_t lastseek
= 0;
464 int logicalDriveCount
= 0;
465 int extendedPart
= -1;
466 int primaryPart
= -1;
468 uint32_t xnumsect
= 0;
473 * Count of drives in the current extended partition's
474 * FDISK table, and indexes of the drives themselves.
476 int extndDrives
[FD_NUMPART
];
479 * Count of drives (beyond primary) in master boot record's
480 * FDISK table, and indexes of the drives themselves.
482 int extraDrives
[FD_NUMPART
];
483 int numExtraDrives
= 0;
485 if ((drvnum
= parse_drvnum(ldrive
)) < 0)
488 if (read(fd
, &mb
, sizeof (mb
)) != sizeof (mb
)) {
489 (void) fprintf(stderr
,
490 gettext("Couldn't read a Master Boot Record\n"));
494 if (ltohs(mb
.signature
) != BOOTSECSIG
) {
495 (void) fprintf(stderr
,
496 gettext("Bad signature on master boot record (%x)\n"),
497 ltohs(mb
.signature
));
502 * Copy partition table into memory
504 (void) memcpy(part
, mb
.parts
, sizeof (part
));
507 * Get a summary of what is in the Master FDISK table.
508 * Normally we expect to find one partition marked as a DOS drive.
509 * This partition is the one Windows calls the primary dos partition.
510 * If the machine has any logical drives then we also expect
511 * to find a partition marked as an extended DOS partition.
513 * Sometimes we'll find multiple partitions marked as DOS drives.
514 * The Solaris fdisk program allows these partitions
515 * to be created, but Windows fdisk no longer does. We still need
516 * to support these, though, since Windows does. We also need to fix
517 * our fdisk to behave like the Windows version.
519 * It turns out that some off-the-shelf media have *only* an
520 * Extended partition, so we need to deal with that case as
523 * Only a single (the first) Extended or Boot Partition will
524 * be recognized. Any others will be ignored.
526 for (i
= 0; i
< FD_NUMPART
; i
++) {
527 if (isDosDrive(part
[i
].systid
)) {
528 if (primaryPart
< 0) {
532 extraDrives
[numExtraDrives
++] = i
;
536 if ((extendedPart
< 0) && isDosExtended(part
[i
].systid
)) {
540 if ((bootPart
< 0) && isBootPart(part
[i
].systid
)) {
546 if (drvnum
== BOOT_PARTITION_DRIVE
) {
548 (void) fprintf(stderr
,
549 gettext("No boot partition found on drive\n"));
552 found
= ltohi(part
[bootPart
].relsect
) * BPSEC
;
556 if (drvnum
== PRIMARY_DOS_DRIVE
&& primaryPart
>= 0) {
557 found
= ltohi(part
[primaryPart
].relsect
) * BPSEC
;
562 * We are not looking for the C: drive (or there was no primary
563 * drive found), so we had better have an extended partition or
564 * extra drives in the Master FDISK table.
566 if ((extendedPart
< 0) && (numExtraDrives
== 0)) {
567 (void) fprintf(stderr
,
568 gettext("No such logical drive "
569 "(missing extended partition entry)\n"));
573 if (extendedPart
>= 0) {
574 nextseek
= xstartsect
= ltohi(part
[extendedPart
].relsect
);
575 xnumsect
= ltohi(part
[extendedPart
].numsect
);
578 * If the seek would not cause us to change
579 * position on the drive, then we're out of
580 * extended partitions to examine.
582 if (nextseek
== lastseek
)
584 logicalDriveCount
+= numDrives
;
586 * Seek the next extended partition, and find
587 * logical drives within it.
589 if (lseek64(fd
, nextseek
* BPSEC
, SEEK_SET
) < 0 ||
590 read(fd
, &extmboot
, sizeof (extmboot
)) !=
592 perror(gettext("Unable to read extended "
593 "partition record"));
596 (void) memcpy(part
, extmboot
.parts
, sizeof (part
));
598 if (ltohs(extmboot
.signature
) != MBB_MAGIC
) {
599 (void) fprintf(stderr
,
600 gettext("Bad signature on "
601 "extended partition\n"));
605 * Count up drives, and track where the next
606 * extended partition is in case we need it. We
607 * are expecting only one extended partition. If
608 * there is more than one we'll only go to the
609 * first one we see, but warn about ignoring.
612 for (i
= 0; i
< FD_NUMPART
; i
++) {
613 if (isDosDrive(part
[i
].systid
)) {
614 extndDrives
[numDrives
++] = i
;
616 } else if (isDosExtended(part
[i
].systid
)) {
617 if (nextseek
!= lastseek
) {
619 * Already found an extended
620 * partition in this table.
622 (void) fprintf(stderr
,
624 "Ignoring unexpected "
625 "additional extended "
629 nextseek
= xstartsect
+
630 ltohi(part
[i
].relsect
);
634 } while (drvnum
> logicalDriveCount
+ numDrives
);
636 if (drvnum
<= logicalDriveCount
+ numDrives
) {
638 * The number of logical drives we've found thus
639 * far is enough to get us to the one we were
642 driveIndex
= logicalDriveCount
+ numDrives
- drvnum
;
644 ltohi(part
[extndDrives
[driveIndex
]].relsect
) +
646 if (found
> (xstartsect
+ xnumsect
)) {
647 (void) fprintf(stderr
,
648 gettext("Logical drive start sector (%d) "
649 "is not within the partition!\n"), found
);
657 * We ran out of extended dos partition
658 * drives. The only hope now is to go
659 * back to extra drives defined in the master
660 * fdisk table. But we overwrote that table
661 * already, so we must load it in again.
663 logicalDriveCount
+= numDrives
;
664 (void) memcpy(part
, mb
.parts
, sizeof (part
));
668 * Still haven't found the drive, is it an extra
669 * drive defined in the main FDISK table?
671 if (drvnum
<= logicalDriveCount
+ numExtraDrives
) {
672 driveIndex
= logicalDriveCount
+ numExtraDrives
- drvnum
;
673 found
= ltohi(part
[extraDrives
[driveIndex
]].relsect
) * BPSEC
;