Home | History | Annotate | Line # | Download | only in scan_ffs
scan_ffs.c revision 1.24
      1  1.24  dholland /* $NetBSD: scan_ffs.c,v 1.24 2013/06/23 07:28:36 dholland Exp $ */
      2   1.1   xtraeme 
      3   1.1   xtraeme /*
      4  1.19   xtraeme  * Copyright (c) 2005-2007 Juan Romero Pardines
      5   1.1   xtraeme  * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
      6   1.1   xtraeme  * All rights reserved.
      7   1.1   xtraeme  *
      8   1.1   xtraeme  * Redistribution and use in source and binary forms, with or without
      9   1.1   xtraeme  * modification, are permitted provided that the following conditions
     10   1.1   xtraeme  * are met:
     11   1.1   xtraeme  * 1. Redistributions of source code must retain the above copyright
     12   1.1   xtraeme  *    notice, this list of conditions and the following disclaimer.
     13   1.1   xtraeme  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   xtraeme  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   xtraeme  *    documentation and/or other materials provided with the distribution.
     16   1.1   xtraeme  *
     17   1.1   xtraeme  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1   xtraeme  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1   xtraeme  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1   xtraeme  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1   xtraeme  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1   xtraeme  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1   xtraeme  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1   xtraeme  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1   xtraeme  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26   1.1   xtraeme  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1   xtraeme  */
     28   1.1   xtraeme 
     29   1.1   xtraeme /*
     30  1.14   xtraeme  * Currently it can detect FFS and LFS partitions (version 1 or 2)
     31  1.14   xtraeme  * up to 8192/65536 fragsize/blocksize.
     32   1.1   xtraeme  */
     33   1.1   xtraeme 
     34   1.1   xtraeme #include <sys/cdefs.h>
     35   1.1   xtraeme #ifndef lint
     36  1.24  dholland __RCSID("$NetBSD: scan_ffs.c,v 1.24 2013/06/23 07:28:36 dholland Exp $");
     37   1.1   xtraeme #endif /* not lint */
     38   1.1   xtraeme 
     39   1.1   xtraeme #include <sys/types.h>
     40   1.1   xtraeme #include <sys/param.h>
     41   1.1   xtraeme #include <sys/disklabel.h>
     42   1.1   xtraeme #include <sys/dkio.h>
     43   1.1   xtraeme #include <sys/ioctl.h>
     44   1.1   xtraeme #include <sys/fcntl.h>
     45   1.5   xtraeme #include <sys/mount.h>
     46   1.5   xtraeme 
     47   1.5   xtraeme #include <ufs/ufs/dinode.h>
     48   1.5   xtraeme #include <ufs/lfs/lfs.h>
     49  1.17  perseant #include <ufs/lfs/lfs_extern.h>
     50   1.8        he 
     51   1.8        he /* Undefine macros defined by both lfs/lfs.h and ffs/fs.h */
     52   1.8        he #undef fragstoblks
     53   1.8        he #undef blkstofrags
     54   1.8        he #undef fragnum
     55   1.8        he #undef blknum
     56   1.8        he 
     57   1.1   xtraeme #include <ufs/ffs/fs.h>
     58   1.5   xtraeme 
     59   1.8        he /* Undefine macros defined by both lfs/lfs.h and ffs/fs.h */
     60   1.8        he /* ...to make sure we don't later depend on their (ambigious) definition */
     61   1.8        he #undef fragstoblks
     62   1.8        he #undef blkstofrags
     63   1.8        he #undef fragnum
     64   1.8        he #undef blknum
     65   1.8        he 
     66   1.1   xtraeme #include <unistd.h>
     67   1.1   xtraeme #include <stdlib.h>
     68   1.1   xtraeme #include <stdio.h>
     69   1.1   xtraeme #include <string.h>
     70   1.1   xtraeme #include <err.h>
     71   1.1   xtraeme #include <util.h>
     72   1.1   xtraeme 
     73  1.14   xtraeme #define BLK_CNT		(blk + (n / 512))
     74  1.14   xtraeme 
     75   1.5   xtraeme /* common struct for FFS/LFS */
     76   1.5   xtraeme struct sblockinfo {
     77   1.5   xtraeme 	struct lfs	*lfs;
     78   1.5   xtraeme 	struct fs	*ffs;
     79  1.12   xtraeme 	uint64_t	lfs_off;
     80  1.12   xtraeme 	uint64_t	ffs_off;
     81   1.5   xtraeme 	char		lfs_path[MAXMNTLEN];
     82   1.5   xtraeme 	char		ffs_path[MAXMNTLEN];
     83  1.14   xtraeme };
     84   1.1   xtraeme 
     85   1.5   xtraeme static daddr_t	blk, lastblk;
     86   1.1   xtraeme 
     87   1.5   xtraeme static int	eflag = 0;
     88  1.10   xtraeme static int	fflag = 0;
     89   1.5   xtraeme static int	flags = 0;
     90  1.14   xtraeme static int	sbaddr = 0; /* counter for the LFS superblocks */
     91   1.1   xtraeme 
     92   1.1   xtraeme static char	device[MAXPATHLEN];
     93  1.16   xtraeme static const char *fstypes[] = { "NONE", "FFSv1", "FFSv2" };
     94   1.5   xtraeme 
     95   1.5   xtraeme #define FSTYPE_NONE	0
     96   1.5   xtraeme #define FSTYPE_FFSV1	1
     97   1.5   xtraeme #define FSTYPE_FFSV2	2
     98   1.1   xtraeme 
     99  1.16   xtraeme #define SBCOUNT		128 /* may be changed */
    100   1.5   xtraeme #define SBPASS		(SBCOUNT * SBLOCKSIZE / 512)
    101   1.1   xtraeme 
    102   1.5   xtraeme /* This is only useful for LFS */
    103   1.1   xtraeme 
    104   1.5   xtraeme /* first sblock address contains the correct offset */
    105   1.5   xtraeme #define FIRST_SBLOCK_ADDRESS    1
    106   1.5   xtraeme /* second and third sblock address contain lfs_fsmnt[MAXMNTLEN] */
    107   1.5   xtraeme #define SECOND_SBLOCK_ADDRESS   2
    108   1.5   xtraeme /* last sblock address in a LFS partition */
    109   1.5   xtraeme #define MAX_SBLOCK_ADDRESS      10
    110   1.1   xtraeme 
    111  1.17  perseant enum { NADA=0, VERBOSE=1, LABELS=2, BLOCKS=4 };
    112   1.3  christos 
    113   1.5   xtraeme /* FFS functions */
    114  1.14   xtraeme static void	ffs_printpart(struct sblockinfo *, int, size_t, int);
    115  1.14   xtraeme static void	ffs_scan(struct sblockinfo *, int);
    116  1.14   xtraeme static int	ffs_checkver(struct sblockinfo *);
    117   1.5   xtraeme /* LFS functions */
    118  1.14   xtraeme static void	lfs_printpart(struct sblockinfo *, int, int);
    119  1.14   xtraeme static void	lfs_scan(struct sblockinfo *, int);
    120   1.5   xtraeme /* common functions */
    121  1.20     perry static void	usage(void) __dead;
    122   1.5   xtraeme static int	scan_disk(int, daddr_t, daddr_t, int);
    123   1.3  christos 
    124   1.1   xtraeme static int
    125  1.14   xtraeme ffs_checkver(struct sblockinfo *sbi)
    126   1.1   xtraeme {
    127  1.14   xtraeme 	switch (sbi->ffs->fs_magic) {
    128   1.1   xtraeme 		case FS_UFS1_MAGIC:
    129   1.1   xtraeme 		case FS_UFS1_MAGIC_SWAPPED:
    130  1.14   xtraeme 			sbi->ffs->fs_size = sbi->ffs->fs_old_size;
    131   1.3  christos 			return FSTYPE_FFSV1;
    132   1.1   xtraeme 		case FS_UFS2_MAGIC:
    133   1.1   xtraeme 		case FS_UFS2_MAGIC_SWAPPED:
    134   1.3  christos 			return FSTYPE_FFSV2;
    135   1.1   xtraeme 		default:
    136   1.3  christos 			return FSTYPE_NONE;
    137   1.1   xtraeme 	}
    138   1.1   xtraeme }
    139   1.1   xtraeme 
    140   1.1   xtraeme static void
    141  1.14   xtraeme ffs_printpart(struct sblockinfo *sbi, int flag, size_t ffsize, int n)
    142   1.1   xtraeme {
    143  1.17  perseant 	int offset, ver;
    144   1.1   xtraeme 
    145   1.3  christos 	switch (flag) {
    146   1.3  christos 	case VERBOSE:
    147  1.16   xtraeme 		switch (ffs_checkver(sbi)) {
    148  1.14   xtraeme 		case FSTYPE_FFSV1:
    149  1.14   xtraeme 			(void)printf("offset: %" PRIu64 " n: %d "
    150  1.16   xtraeme 			    "id: %x,%x size: %" PRIu64 "\n",
    151  1.14   xtraeme 			    BLK_CNT - (2 * SBLOCKSIZE / 512), n,
    152  1.14   xtraeme 			    sbi->ffs->fs_id[0], sbi->ffs->fs_id[1],
    153  1.16   xtraeme 			    (uint64_t)sbi->ffs->fs_size *
    154  1.14   xtraeme 			    sbi->ffs->fs_fsize / 512);
    155  1.14   xtraeme 			break;
    156  1.14   xtraeme 		case FSTYPE_FFSV2:
    157  1.14   xtraeme 			(void)printf("offset: %" PRIu64 " n: %d "
    158  1.16   xtraeme 			    "id: %x,%x size: %" PRIu64 "\n",
    159  1.14   xtraeme 			    BLK_CNT - (ffsize * SBLOCKSIZE / 512+128),
    160  1.14   xtraeme 			    n, sbi->ffs->fs_id[0], sbi->ffs->fs_id[1],
    161  1.16   xtraeme 			    (uint64_t)sbi->ffs->fs_size *
    162  1.14   xtraeme 			    sbi->ffs->fs_fsize / 512);
    163  1.14   xtraeme 			break;
    164  1.14   xtraeme 		default:
    165  1.14   xtraeme 			break;
    166  1.14   xtraeme 		}
    167   1.3  christos 		break;
    168   1.3  christos 	case LABELS:
    169   1.3  christos 		(void)printf("X:  %9" PRIu64,
    170  1.16   xtraeme 			(uint64_t)(sbi->ffs->fs_size *
    171  1.14   xtraeme 			sbi->ffs->fs_fsize / 512));
    172  1.16   xtraeme 		switch (ffs_checkver(sbi)) {
    173   1.3  christos 		case FSTYPE_FFSV1:
    174   1.3  christos 			(void)printf(" %9" PRIu64,
    175  1.14   xtraeme 			    BLK_CNT - (ffsize * SBLOCKSIZE / 512));
    176   1.3  christos 			break;
    177   1.3  christos 		case FSTYPE_FFSV2:
    178   1.3  christos 			(void)printf(" %9" PRIu64,
    179  1.14   xtraeme 			    BLK_CNT - (ffsize * SBLOCKSIZE / 512 + 128));
    180   1.3  christos 			break;
    181   1.3  christos 		default:
    182   1.3  christos 			break;
    183   1.3  christos 		}
    184   1.7   xtraeme 		(void)printf(" 4.2BSD %6d %5d %7d # %s [%s]\n",
    185  1.14   xtraeme 			sbi->ffs->fs_fsize, sbi->ffs->fs_bsize,
    186  1.14   xtraeme 			sbi->ffs->fs_old_cpg,
    187  1.16   xtraeme 			sbi->ffs_path, fstypes[ffs_checkver(sbi)]);
    188   1.3  christos 		break;
    189  1.17  perseant 	case BLOCKS:
    190   1.3  christos 	default:
    191  1.16   xtraeme 		(void)printf("%s ", fstypes[ffs_checkver(sbi)]);
    192  1.17  perseant 		ver = ffs_checkver(sbi);
    193  1.17  perseant 		if (ver == FSTYPE_NONE)
    194   1.3  christos 			break;
    195  1.17  perseant 
    196  1.17  perseant 		offset = 0;
    197  1.17  perseant 		if (flag == BLOCKS)
    198  1.17  perseant 			(void)printf("sb ");
    199  1.17  perseant 		else if (ver == FSTYPE_FFSV1)
    200  1.17  perseant 			offset = (2 * SBLOCKSIZE / 512);
    201  1.17  perseant 		else if (ver == FSTYPE_FFSV2)
    202  1.17  perseant 			offset = (ffsize * SBLOCKSIZE / 512 + 128);
    203  1.17  perseant 
    204  1.17  perseant 		(void)printf("at %" PRIu64, BLK_CNT - offset);
    205   1.1   xtraeme 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    206  1.16   xtraeme 			(uint64_t)(sbi->ffs->fs_size *
    207  1.14   xtraeme 			sbi->ffs->fs_fsize / 512), sbi->ffs_path);
    208   1.3  christos 		break;
    209   1.1   xtraeme 	}
    210   1.1   xtraeme }
    211   1.1   xtraeme 
    212   1.1   xtraeme static void
    213  1.14   xtraeme ffs_scan(struct sblockinfo *sbi, int n)
    214   1.1   xtraeme {
    215  1.14   xtraeme 	size_t i = 0;
    216   1.1   xtraeme 
    217  1.17  perseant 	if (flags & BLOCKS) {
    218  1.17  perseant 		ffs_printpart(sbi, BLOCKS, 0, n);
    219  1.17  perseant 		return;
    220  1.17  perseant 	}
    221   1.1   xtraeme 	if (flags & VERBOSE)
    222  1.14   xtraeme 		ffs_printpart(sbi, VERBOSE, NADA, n);
    223  1.16   xtraeme 	switch (ffs_checkver(sbi)) {
    224   1.3  christos 	case FSTYPE_FFSV1:
    225  1.14   xtraeme 		/* fsize/bsize > 512/4096 and < 4096/32768. */
    226  1.14   xtraeme 		if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 512)) {
    227  1.14   xtraeme 			i = 2;
    228  1.14   xtraeme 		/* fsize/bsize 4096/32768. */
    229  1.14   xtraeme 		} else if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 170)) {
    230  1.14   xtraeme 			i = 4;
    231  1.14   xtraeme 		/* fsize/bsize 8192/65536 */
    232  1.14   xtraeme 		} else if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 73)) {
    233  1.14   xtraeme 			i = 8;
    234  1.14   xtraeme 		} else
    235  1.14   xtraeme 			break;
    236  1.14   xtraeme 
    237  1.14   xtraeme 		if (flags & LABELS)
    238  1.14   xtraeme 			ffs_printpart(sbi, LABELS, i, n);
    239  1.14   xtraeme 		else
    240  1.14   xtraeme 			ffs_printpart(sbi, NADA, i, n);
    241  1.14   xtraeme 
    242   1.3  christos 		break;
    243   1.3  christos 	case FSTYPE_FFSV2:
    244   1.3  christos 		/*
    245   1.3  christos 		 * That checks for FFSv2 partitions with fragsize/blocksize:
    246   1.3  christos 		 * 512/4096, 1024/8192, 2048/16384, 4096/32768 and 8192/65536.
    247   1.3  christos 		 * Really enough for now.
    248   1.3  christos 		 */
    249   1.3  christos 		for (i = 1; i < 16; i <<= 1)
    250  1.21     lukem 			if ((BLK_CNT - lastblk) == (daddr_t)(i * SBLOCKSIZE / 512)) {
    251   1.3  christos 				if (flags & LABELS)
    252  1.14   xtraeme 					ffs_printpart(sbi, LABELS, i, n);
    253   1.3  christos 				else
    254  1.14   xtraeme 					ffs_printpart(sbi, NADA, i, n);
    255   1.3  christos 			}
    256   1.5   xtraeme 		break;
    257   1.5   xtraeme 	}
    258   1.5   xtraeme }
    259   1.5   xtraeme 
    260   1.5   xtraeme static void
    261  1.14   xtraeme lfs_printpart(struct sblockinfo *sbi, int flag, int n)
    262   1.5   xtraeme {
    263   1.5   xtraeme 	if (flags & VERBOSE)
    264  1.17  perseant                	(void)printf("offset: %" PRIu64 " size %" PRIu32
    265  1.17  perseant 			" fsid %" PRIx32 "\n", sbi->lfs_off, sbi->lfs->lfs_size,
    266  1.17  perseant 			sbi->lfs->lfs_ident);
    267   1.5   xtraeme 	switch (flag) {
    268   1.5   xtraeme 	case LABELS:
    269   1.5   xtraeme 		(void)printf("X:  %9" PRIu64,
    270  1.16   xtraeme                		(uint64_t)(sbi->lfs->lfs_size *
    271   1.5   xtraeme                		sbi->lfs->lfs_fsize / 512));
    272   1.5   xtraeme 		(void)printf(" %9" PRIu64, sbi->lfs_off);
    273   1.7   xtraeme 		(void)printf(" 4.4LFS %6d %5d %7d # %s [LFSv%d]\n",
    274   1.5   xtraeme 			sbi->lfs->lfs_fsize, sbi->lfs->lfs_bsize,
    275   1.5   xtraeme 			sbi->lfs->lfs_nseg, sbi->lfs_path,
    276   1.5   xtraeme 			sbi->lfs->lfs_version);
    277   1.5   xtraeme 		break;
    278  1.17  perseant 	case BLOCKS:
    279  1.17  perseant 		(void)printf("LFSv%d", sbi->lfs->lfs_version);
    280  1.17  perseant 		(void)printf(" sb at %" PRIu64, sbi->lfs_off + btodb(LFS_LABELPAD));
    281  1.17  perseant 		(void)printf(" fsid %" PRIx32, sbi->lfs->lfs_ident);
    282  1.17  perseant 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    283  1.17  perseant 			(uint64_t)(sbi->lfs->lfs_size *
    284  1.17  perseant 			sbi->lfs->lfs_fsize / 512), sbi->lfs_path);
    285  1.17  perseant 		break;
    286   1.5   xtraeme 	default:
    287  1.14   xtraeme 		(void)printf("LFSv%d ", sbi->lfs->lfs_version);
    288  1.14   xtraeme 		(void)printf("at %" PRIu64, sbi->lfs_off);
    289   1.5   xtraeme 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    290  1.16   xtraeme 			(uint64_t)(sbi->lfs->lfs_size *
    291  1.14   xtraeme 			sbi->lfs->lfs_fsize / 512), sbi->lfs_path);
    292   1.5   xtraeme 		break;
    293   1.5   xtraeme 	}
    294   1.5   xtraeme }
    295   1.5   xtraeme 
    296   1.5   xtraeme static void
    297  1.14   xtraeme lfs_scan(struct sblockinfo *sbi, int n)
    298   1.5   xtraeme {
    299  1.17  perseant 	/* Check to see if the sb checksums correctly */
    300  1.17  perseant 	if (lfs_sb_cksum(&(sbi->lfs->lfs_dlfs)) != sbi->lfs->lfs_cksum) {
    301  1.17  perseant 		if (flags & VERBOSE)
    302  1.17  perseant 			printf("LFS bad superblock at %" PRIu64 "\n",
    303  1.17  perseant 				BLK_CNT);
    304  1.17  perseant 		return;
    305  1.17  perseant 	}
    306  1.17  perseant 
    307   1.5   xtraeme 	/* backup offset */
    308  1.14   xtraeme 	lastblk = BLK_CNT - (LFS_SBPAD / 512);
    309   1.5   xtraeme 	/* increment counter */
    310   1.5   xtraeme         ++sbaddr;
    311   1.5   xtraeme 
    312  1.17  perseant 	if (flags & BLOCKS) {
    313  1.17  perseant 		sbi->lfs_off = BLK_CNT - btodb(LFS_LABELPAD);
    314  1.17  perseant 		lfs_printpart(sbi, BLOCKS, n);
    315  1.17  perseant 		return;
    316  1.17  perseant 	}
    317  1.17  perseant 
    318   1.5   xtraeme 	switch (sbaddr) {
    319   1.5   xtraeme 	/*
    320   1.5   xtraeme 	 * first superblock contains the right offset, but lfs_fsmnt is
    321  1.17  perseant 	 * empty... fortunately the next superblock address has it.
    322   1.5   xtraeme 	 */
    323   1.5   xtraeme 	case FIRST_SBLOCK_ADDRESS:
    324   1.5   xtraeme 		/* copy partition offset */
    325  1.21     lukem 		if ((daddr_t)sbi->lfs_off != lastblk)
    326  1.17  perseant 			sbi->lfs_off = BLK_CNT - (LFS_LABELPAD / 512);
    327   1.5   xtraeme 		break;
    328   1.5   xtraeme 	case SECOND_SBLOCK_ADDRESS:
    329   1.5   xtraeme 		/* copy the path of last mount */
    330  1.14   xtraeme 		(void)memcpy(sbi->lfs_path, sbi->lfs->lfs_fsmnt, MAXMNTLEN);
    331   1.5   xtraeme 		/* print now that we have the info */
    332   1.5   xtraeme 		if (flags & LABELS)
    333  1.14   xtraeme 			lfs_printpart(sbi, LABELS, n);
    334   1.5   xtraeme 		else
    335  1.14   xtraeme 			lfs_printpart(sbi, NADA, n);
    336   1.5   xtraeme 		/* clear our struct */
    337  1.14   xtraeme 		(void)memset(sbi, 0, sizeof(*sbi));
    338   1.5   xtraeme 		break;
    339   1.5   xtraeme 	case MAX_SBLOCK_ADDRESS:
    340   1.5   xtraeme 		/*
    341   1.5   xtraeme 		 * reset the counter, this is the last superblock address,
    342   1.5   xtraeme 		 * the next one will be another partition maybe.
    343   1.5   xtraeme 		 */
    344   1.5   xtraeme 		sbaddr = 0;
    345   1.5   xtraeme 		break;
    346   1.5   xtraeme 	default:
    347   1.5   xtraeme 		break;
    348   1.1   xtraeme 	}
    349   1.1   xtraeme }
    350   1.1   xtraeme 
    351   1.1   xtraeme static int
    352   1.5   xtraeme scan_disk(int fd, daddr_t beg, daddr_t end, int fflags)
    353   1.1   xtraeme {
    354  1.14   xtraeme 	struct sblockinfo sbinfo;
    355  1.10   xtraeme 	uint8_t buf[SBLOCKSIZE * SBCOUNT];
    356  1.16   xtraeme 	int n;
    357   1.1   xtraeme 
    358  1.16   xtraeme 	n = 0;
    359   1.1   xtraeme 	lastblk = -1;
    360   1.5   xtraeme 
    361   1.5   xtraeme 	/* clear our struct before using it */
    362   1.5   xtraeme 	(void)memset(&sbinfo, 0, sizeof(sbinfo));
    363   1.1   xtraeme 
    364   1.1   xtraeme 	if (fflags & LABELS)
    365   1.3  christos 		(void)printf(
    366   1.5   xtraeme 		    "#        size    offset fstype [fsize bsize cpg/sgs]\n");
    367   1.1   xtraeme 
    368  1.16   xtraeme 	for (blk = beg; blk <= end; blk += SBPASS) {
    369  1.16   xtraeme 		if (pread(fd, buf, sizeof(buf), blk * 512) == -1) {
    370  1.16   xtraeme 			if (fflag && fd >= 0)
    371  1.16   xtraeme 				(void)close(fd);
    372   1.7   xtraeme 			err(1, "pread");
    373  1.16   xtraeme 		}
    374   1.1   xtraeme 
    375   1.1   xtraeme 		for (n = 0; n < (SBLOCKSIZE * SBCOUNT); n += 512) {
    376  1.16   xtraeme 			sbinfo.ffs = (struct fs *)&buf[n];
    377  1.16   xtraeme 			sbinfo.lfs = (struct lfs *)&buf[n];
    378  1.16   xtraeme 
    379  1.16   xtraeme 			switch (ffs_checkver(&sbinfo)) {
    380   1.5   xtraeme 			case FSTYPE_FFSV1:
    381   1.5   xtraeme 			case FSTYPE_FFSV2:
    382  1.14   xtraeme 				ffs_scan(&sbinfo, n);
    383  1.14   xtraeme 				lastblk = BLK_CNT;
    384   1.5   xtraeme 				(void)memcpy(sbinfo.ffs_path,
    385   1.5   xtraeme 					sbinfo.ffs->fs_fsmnt, MAXMNTLEN);
    386   1.5   xtraeme 				break;
    387   1.5   xtraeme 			case FSTYPE_NONE:
    388   1.5   xtraeme 				/* maybe LFS? */
    389   1.5   xtraeme 				if (sbinfo.lfs->lfs_magic == LFS_MAGIC)
    390  1.14   xtraeme 					lfs_scan(&sbinfo, n);
    391   1.5   xtraeme 				break;
    392   1.5   xtraeme 			default:
    393   1.5   xtraeme 				break;
    394   1.5   xtraeme 			}
    395   1.1   xtraeme 		}
    396   1.1   xtraeme 	}
    397  1.16   xtraeme 
    398  1.16   xtraeme 	if (fflag && fd >= 0)
    399  1.16   xtraeme 		(void)close(fd);
    400  1.16   xtraeme 
    401   1.2    kleink 	return EXIT_SUCCESS;
    402   1.1   xtraeme }
    403   1.1   xtraeme 
    404   1.1   xtraeme 
    405   1.1   xtraeme static void
    406   1.6  christos usage(void)
    407   1.1   xtraeme {
    408   1.1   xtraeme 	(void)fprintf(stderr,
    409  1.18   xtraeme 		"Usage: %s [-blv] [-e end] [-F file] [-s start] "
    410  1.10   xtraeme 		"device\n", getprogname());
    411   1.2    kleink 	exit(EXIT_FAILURE);
    412   1.1   xtraeme }
    413   1.1   xtraeme 
    414   1.1   xtraeme 
    415   1.1   xtraeme int
    416   1.1   xtraeme main(int argc, char **argv)
    417   1.1   xtraeme {
    418   1.1   xtraeme 	int ch, fd;
    419  1.10   xtraeme 	const char *fpath;
    420   1.1   xtraeme 	daddr_t end = -1, beg = 0;
    421   1.1   xtraeme 	struct disklabel dl;
    422   1.1   xtraeme 
    423  1.10   xtraeme 	fpath = NULL;
    424  1.10   xtraeme 
    425   1.6  christos 	setprogname(*argv);
    426  1.17  perseant 	while ((ch = getopt(argc, argv, "be:F:ls:v")) != -1)
    427   1.1   xtraeme 		switch(ch) {
    428  1.17  perseant 		case 'b':
    429  1.17  perseant 			flags |= BLOCKS;
    430  1.17  perseant 			flags &= ~LABELS;
    431  1.17  perseant 			break;
    432   1.1   xtraeme 		case 'e':
    433   1.1   xtraeme 			eflag = 1;
    434   1.1   xtraeme 			end = atoi(optarg);
    435   1.1   xtraeme 			break;
    436  1.11   xtraeme 		case 'F':
    437  1.10   xtraeme 			fflag = 1;
    438  1.10   xtraeme 			fpath = optarg;
    439  1.10   xtraeme 			break;
    440   1.1   xtraeme 		case 'l':
    441   1.1   xtraeme 			flags |= LABELS;
    442  1.17  perseant 			flags &= ~BLOCKS;
    443   1.1   xtraeme 			break;
    444   1.1   xtraeme 		case 's':
    445   1.1   xtraeme 			beg = atoi(optarg);
    446   1.1   xtraeme 			break;
    447   1.1   xtraeme 		case 'v':
    448   1.1   xtraeme 			flags |= VERBOSE;
    449   1.1   xtraeme 			break;
    450   1.1   xtraeme 		default:
    451   1.6  christos 			usage();
    452   1.1   xtraeme 			/* NOTREACHED */
    453   1.3  christos 		}
    454   1.3  christos 
    455   1.1   xtraeme 	argc -= optind;
    456   1.1   xtraeme 	argv += optind;
    457   1.1   xtraeme 
    458  1.10   xtraeme 	if (fflag) {
    459  1.10   xtraeme 		struct stat stp;
    460   1.1   xtraeme 
    461  1.10   xtraeme 		if (stat(fpath, &stp))
    462  1.10   xtraeme 			err(1, "Cannot stat `%s'", fpath);
    463   1.1   xtraeme 
    464  1.10   xtraeme 		if (!eflag)
    465  1.12   xtraeme 			end = (uint64_t)stp.st_size;
    466   1.1   xtraeme 
    467  1.16   xtraeme 		(void)printf("Total file size: %" PRIu64 "\n\n",
    468  1.16   xtraeme 		    (uint64_t)stp.st_size);
    469  1.16   xtraeme 
    470  1.15   xtraeme 		fd = open(fpath, O_RDONLY | O_DIRECT);
    471   1.3  christos 	} else {
    472  1.10   xtraeme 		if (argc != 1)
    473  1.10   xtraeme 			usage();
    474  1.10   xtraeme 
    475  1.10   xtraeme 		fd = opendisk(argv[0], O_RDONLY, device, sizeof(device), 0);
    476  1.10   xtraeme 
    477  1.10   xtraeme 		if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    478  1.10   xtraeme 			warn("Couldn't retrieve disklabel");
    479  1.10   xtraeme 			(void)memset(&dl, 0, sizeof(dl));
    480  1.10   xtraeme 			dl.d_secperunit = 0x7fffffff;
    481  1.10   xtraeme 		} else {
    482  1.10   xtraeme 			(void)printf("Disk: %s\n", dl.d_typename);
    483  1.10   xtraeme 			(void)printf("Total sectors on disk: %" PRIu32 "\n\n",
    484  1.10   xtraeme 			    dl.d_secperunit);
    485  1.10   xtraeme 		}
    486   1.1   xtraeme 	}
    487  1.10   xtraeme 
    488  1.10   xtraeme 	if (!eflag && !fflag)
    489   1.1   xtraeme 		end = dl.d_secperunit; /* default to max sectors */
    490   1.1   xtraeme 
    491  1.10   xtraeme 	if (fd == -1)
    492  1.10   xtraeme 		err(1, "Cannot open `%s'", device);
    493  1.10   xtraeme 		/* NOTREACHED */
    494  1.10   xtraeme 
    495   1.5   xtraeme 	return scan_disk(fd, beg, end, flags);
    496   1.1   xtraeme }
    497