8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / pcfs / fsck / pcfs_common.c
bloba8524712ce69eec619f16c585bdd56df894af08c
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <libintl.h>
39 #include <sys/isa_defs.h>
40 #include <sys/types.h>
41 #include <sys/stat.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"
49 #include "pcfs_bpb.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
55 * aligned pointers.
57 #ifdef _BIG_ENDIAN
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
65 extern int AlwaysYes;
66 extern int AlwaysNo;
69 * store_16_bits
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
74 * order.
76 void
77 store_16_bits(uchar_t **bp, uint32_t v)
79 uchar_t *l = *bp;
81 *l++ = v & 0xff;
82 *l = (v >> 8) & 0xff;
83 *bp += 2;
86 void
87 read_16_bits(uchar_t *bp, uint32_t *value)
89 *value = *bp++;
90 *value += *bp << 8;
94 * store_32_bits
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.
100 void
101 store_32_bits(uchar_t **bp, uint32_t v)
103 uchar_t *l = *bp;
104 int b;
106 for (b = 0; b < 4; b++) {
107 *l++ = v & 0xff;
108 v = v >> 8;
110 *bp += 4;
113 void
114 read_32_bits(uchar_t *bp, uint32_t *value)
116 *value = *bp++;
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 */
128 #define BPL 16
130 void
131 dump_bytes(uchar_t *buf, int n)
133 int printedCount;
134 int countdown = n;
135 int countup = 0;
136 int offset = 0;
137 int byte;
139 /* Display offset, 16 bytes per line, and printable ascii version */
140 while (countdown > 0) {
141 printedCount = 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));
150 printedCount++;
151 } else {
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]);
165 } else {
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.
179 void
180 header_for_dump(void)
182 int byte;
184 (void) fprintf(stderr, "\n ");
185 for (byte = 0; byte < BPL; byte++)
186 (void) fprintf(stderr, "%02x ", byte);
187 (void) fprintf(stderr, "\n ");
188 byte = 3*BPL;
189 while (byte-- > 0)
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 :-))
199 #ifdef _BIG_ENDIAN
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.
210 void
211 swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp)
213 uchar_t *grabp;
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);
251 grabp += 11;
252 (void) strncpy((char *)wbpb->ebpb.type, (char *)grabp, 8);
255 void
256 swap_pack_grab32bpb(bpb_t *wbpb, struct _boot_sector *bsp)
258 uchar_t *grabp;
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 */
294 yes(void)
296 char *affirmative = gettext("yY");
297 char *a = affirmative;
298 char input[80];
300 if (AlwaysYes) {
301 (void) printf("y\n");
302 return (1);
303 } else if (AlwaysNo) {
304 (void) printf("n\n");
305 return (0);
307 if (fgets(input, sizeof (input), stdin) == NULL) {
308 AlwaysNo = 1;
309 (void) printf("n\n");
310 return (0);
312 while (*a) {
313 if (input[0] == (int)*a)
314 break;
315 a++;
317 return ((int)*a);
320 char *
321 stat_actual_disk(char *diskname, struct stat *info, char **suffix)
323 char *actualdisk;
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"));
334 exit(2);
336 if ((*suffix = strchr(actualdisk, ':')) != NULL) {
337 **suffix = '\0';
338 (*suffix)++;
341 if (stat(actualdisk, info)) {
342 perror(actualdisk);
343 exit(2);
345 } else {
346 if ((actualdisk = strdup(diskname)) == NULL) {
347 (void) fprintf(stderr,
348 gettext("Out of memory for disk name.\n"));
349 exit(2);
353 return (actualdisk);
356 extern void usage(void);
358 void
359 bad_arg(char *option)
361 (void) fprintf(stderr,
362 gettext("Unrecognized option -o %s.\n"), option);
363 usage();
364 exit(2);
367 void
368 missing_arg(char *option)
370 (void) fprintf(stderr,
371 gettext("Option %s requires a value.\n"), option);
372 usage();
373 exit(3);
376 static int
377 parse_drvnum(char *pn)
379 int drvnum;
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')) {
387 char *d;
388 int v = 0;
390 d = pn;
391 while ((*d != '\0') && (*d >= '0') && (*d <= '9')) {
392 v *= 10;
393 v += *d - '0';
394 d++;
396 if ((*d != '\0') || (v > 24)) {
397 (void) fprintf(stderr,
398 gettext("%s: bogus logical drive specification.\n"),
399 pn);
400 return (-1);
402 drvnum = v;
403 } else if (strcmp(pn, "boot") == 0) {
404 drvnum = 99;
405 } else {
406 (void) fprintf(stderr,
407 gettext("%s: bogus logical drive specification.\n"), pn);
408 return (-1);
411 return (drvnum);
415 * isDosDrive()
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.
420 static int
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));
430 * isDosExtended()
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
433 * partition.
435 static int
436 isDosExtended(uchar_t checkMe)
438 return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
442 * isBootPart()
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
445 * partition.
447 static int
448 isBootPart(uchar_t checkMe)
450 return (checkMe == X86BOOT);
453 off64_t
454 findPartitionOffset(int fd, char *ldrive)
456 struct ipart part[FD_NUMPART];
457 struct mboot extmboot;
458 struct mboot mb;
459 diskaddr_t xstartsect;
460 off64_t nextseek = 0;
461 off64_t lastseek = 0;
462 off64_t found = 0;
463 off64_t error = -1;
464 int logicalDriveCount = 0;
465 int extendedPart = -1;
466 int primaryPart = -1;
467 int bootPart = -1;
468 uint32_t xnumsect = 0;
469 int drvnum;
470 int driveIndex;
471 int i;
473 * Count of drives in the current extended partition's
474 * FDISK table, and indexes of the drives themselves.
476 int extndDrives[FD_NUMPART];
477 int numDrives = 0;
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)
486 return (error);
488 if (read(fd, &mb, sizeof (mb)) != sizeof (mb)) {
489 (void) fprintf(stderr,
490 gettext("Couldn't read a Master Boot Record\n"));
491 return (error);
494 if (ltohs(mb.signature) != BOOTSECSIG) {
495 (void) fprintf(stderr,
496 gettext("Bad signature on master boot record (%x)\n"),
497 ltohs(mb.signature));
498 return (error);
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
521 * well.
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) {
529 logicalDriveCount++;
530 primaryPart = i;
531 } else {
532 extraDrives[numExtraDrives++] = i;
534 continue;
536 if ((extendedPart < 0) && isDosExtended(part[i].systid)) {
537 extendedPart = i;
538 continue;
540 if ((bootPart < 0) && isBootPart(part[i].systid)) {
541 bootPart = i;
542 continue;
546 if (drvnum == BOOT_PARTITION_DRIVE) {
547 if (bootPart < 0) {
548 (void) fprintf(stderr,
549 gettext("No boot partition found on drive\n"));
550 return (error);
552 found = ltohi(part[bootPart].relsect) * BPSEC;
553 return (found);
556 if (drvnum == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
557 found = ltohi(part[primaryPart].relsect) * BPSEC;
558 return (found);
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"));
570 return (error);
573 if (extendedPart >= 0) {
574 nextseek = xstartsect = ltohi(part[extendedPart].relsect);
575 xnumsect = ltohi(part[extendedPart].numsect);
576 do {
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)
583 break;
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)) !=
591 sizeof (extmboot)) {
592 perror(gettext("Unable to read extended "
593 "partition record"));
594 return (error);
596 (void) memcpy(part, extmboot.parts, sizeof (part));
597 lastseek = nextseek;
598 if (ltohs(extmboot.signature) != MBB_MAGIC) {
599 (void) fprintf(stderr,
600 gettext("Bad signature on "
601 "extended partition\n"));
602 return (error);
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.
611 numDrives = 0;
612 for (i = 0; i < FD_NUMPART; i++) {
613 if (isDosDrive(part[i].systid)) {
614 extndDrives[numDrives++] = i;
615 continue;
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,
623 gettext("WARNING: "
624 "Ignoring unexpected "
625 "additional extended "
626 "partition"));
627 continue;
629 nextseek = xstartsect +
630 ltohi(part[i].relsect);
631 continue;
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
640 * searching for.
642 driveIndex = logicalDriveCount + numDrives - drvnum;
643 found =
644 ltohi(part[extndDrives[driveIndex]].relsect) +
645 lastseek;
646 if (found > (xstartsect + xnumsect)) {
647 (void) fprintf(stderr,
648 gettext("Logical drive start sector (%d) "
649 "is not within the partition!\n"), found);
650 return (error);
651 } else {
652 found *= BPSEC;
654 return (found);
655 } else {
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;
674 return (found);
676 return (error);