Home | History | Annotate | Line # | Download | only in scan_ffs
scan_ffs.c revision 1.36.2.1
      1  1.36.2.1    martin /* $NetBSD: scan_ffs.c,v 1.36.2.1 2023/02/22 13:11:37 martin 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.36.2.1    martin __RCSID("$NetBSD: scan_ffs.c,v 1.36.2.1 2023/02/22 13:11:37 martin 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/lfs/lfs.h>
     48      1.28  dholland #include <ufs/lfs/lfs_accessors.h>
     49      1.17  perseant #include <ufs/lfs/lfs_extern.h>
     50       1.8        he 
     51      1.25  dholland #include <ufs/ufs/dinode.h>
     52       1.1   xtraeme #include <ufs/ffs/fs.h>
     53  1.36.2.1    martin #include <ufs/ffs/ffs_extern.h>
     54       1.5   xtraeme 
     55       1.1   xtraeme #include <unistd.h>
     56       1.1   xtraeme #include <stdlib.h>
     57       1.1   xtraeme #include <stdio.h>
     58       1.1   xtraeme #include <string.h>
     59       1.1   xtraeme #include <err.h>
     60       1.1   xtraeme #include <util.h>
     61      1.34  christos #include <paths.h>
     62       1.1   xtraeme 
     63      1.14   xtraeme #define BLK_CNT		(blk + (n / 512))
     64      1.14   xtraeme 
     65       1.5   xtraeme /* common struct for FFS/LFS */
     66       1.5   xtraeme struct sblockinfo {
     67       1.5   xtraeme 	struct lfs	*lfs;
     68       1.5   xtraeme 	struct fs	*ffs;
     69      1.12   xtraeme 	uint64_t	lfs_off;
     70      1.12   xtraeme 	uint64_t	ffs_off;
     71       1.5   xtraeme 	char		lfs_path[MAXMNTLEN];
     72       1.5   xtraeme 	char		ffs_path[MAXMNTLEN];
     73      1.14   xtraeme };
     74       1.1   xtraeme 
     75       1.5   xtraeme static daddr_t	blk, lastblk;
     76       1.1   xtraeme 
     77       1.5   xtraeme static int	eflag = 0;
     78      1.10   xtraeme static int	fflag = 0;
     79       1.5   xtraeme static int	flags = 0;
     80      1.14   xtraeme static int	sbaddr = 0; /* counter for the LFS superblocks */
     81       1.1   xtraeme 
     82       1.1   xtraeme static char	device[MAXPATHLEN];
     83      1.16   xtraeme static const char *fstypes[] = { "NONE", "FFSv1", "FFSv2" };
     84       1.5   xtraeme 
     85      1.33       mrg static sig_atomic_t print_info = 0;
     86      1.33       mrg 
     87       1.5   xtraeme #define FSTYPE_NONE	0
     88       1.5   xtraeme #define FSTYPE_FFSV1	1
     89       1.5   xtraeme #define FSTYPE_FFSV2	2
     90       1.1   xtraeme 
     91      1.16   xtraeme #define SBCOUNT		128 /* may be changed */
     92       1.5   xtraeme #define SBPASS		(SBCOUNT * SBLOCKSIZE / 512)
     93       1.1   xtraeme 
     94       1.5   xtraeme /* This is only useful for LFS */
     95       1.1   xtraeme 
     96       1.5   xtraeme /* first sblock address contains the correct offset */
     97       1.5   xtraeme #define FIRST_SBLOCK_ADDRESS    1
     98       1.5   xtraeme /* second and third sblock address contain lfs_fsmnt[MAXMNTLEN] */
     99       1.5   xtraeme #define SECOND_SBLOCK_ADDRESS   2
    100       1.5   xtraeme /* last sblock address in a LFS partition */
    101       1.5   xtraeme #define MAX_SBLOCK_ADDRESS      10
    102       1.1   xtraeme 
    103      1.17  perseant enum { NADA=0, VERBOSE=1, LABELS=2, BLOCKS=4 };
    104       1.3  christos 
    105       1.5   xtraeme /* FFS functions */
    106      1.14   xtraeme static void	ffs_printpart(struct sblockinfo *, int, size_t, int);
    107      1.14   xtraeme static void	ffs_scan(struct sblockinfo *, int);
    108      1.14   xtraeme static int	ffs_checkver(struct sblockinfo *);
    109       1.5   xtraeme /* LFS functions */
    110      1.14   xtraeme static void	lfs_printpart(struct sblockinfo *, int, int);
    111      1.14   xtraeme static void	lfs_scan(struct sblockinfo *, int);
    112       1.5   xtraeme /* common functions */
    113      1.20     perry static void	usage(void) __dead;
    114       1.5   xtraeme static int	scan_disk(int, daddr_t, daddr_t, int);
    115       1.3  christos 
    116      1.33       mrg static void
    117      1.33       mrg got_siginfo(int signo)
    118      1.33       mrg {
    119      1.33       mrg 
    120      1.33       mrg 	print_info = 1;
    121      1.33       mrg }
    122      1.33       mrg 
    123       1.1   xtraeme static int
    124      1.14   xtraeme ffs_checkver(struct sblockinfo *sbi)
    125       1.1   xtraeme {
    126      1.14   xtraeme 	switch (sbi->ffs->fs_magic) {
    127  1.36.2.1    martin 		case FS_UFS1_MAGIC_SWAPPED:
    128  1.36.2.1    martin 		case FS_UFS2_MAGIC_SWAPPED:
    129  1.36.2.1    martin 		case FS_UFS2EA_MAGIC_SWAPPED:
    130  1.36.2.1    martin 			ffs_sb_swap(sbi->ffs, sbi->ffs);
    131  1.36.2.1    martin 			break;
    132  1.36.2.1    martin 	}
    133  1.36.2.1    martin 
    134  1.36.2.1    martin 	switch (sbi->ffs->fs_magic) {
    135       1.1   xtraeme 		case FS_UFS1_MAGIC:
    136       1.1   xtraeme 		case FS_UFS1_MAGIC_SWAPPED:
    137      1.14   xtraeme 			sbi->ffs->fs_size = sbi->ffs->fs_old_size;
    138       1.3  christos 			return FSTYPE_FFSV1;
    139       1.1   xtraeme 		case FS_UFS2_MAGIC:
    140      1.36       chs 		case FS_UFS2EA_MAGIC:
    141       1.1   xtraeme 		case FS_UFS2_MAGIC_SWAPPED:
    142      1.36       chs 		case FS_UFS2EA_MAGIC_SWAPPED:
    143       1.3  christos 			return FSTYPE_FFSV2;
    144       1.1   xtraeme 		default:
    145       1.3  christos 			return FSTYPE_NONE;
    146       1.1   xtraeme 	}
    147       1.1   xtraeme }
    148       1.1   xtraeme 
    149       1.1   xtraeme static void
    150      1.14   xtraeme ffs_printpart(struct sblockinfo *sbi, int flag, size_t ffsize, int n)
    151       1.1   xtraeme {
    152      1.17  perseant 	int offset, ver;
    153       1.1   xtraeme 
    154       1.3  christos 	switch (flag) {
    155       1.3  christos 	case VERBOSE:
    156      1.16   xtraeme 		switch (ffs_checkver(sbi)) {
    157      1.14   xtraeme 		case FSTYPE_FFSV1:
    158      1.14   xtraeme 			(void)printf("offset: %" PRIu64 " n: %d "
    159      1.16   xtraeme 			    "id: %x,%x size: %" PRIu64 "\n",
    160      1.14   xtraeme 			    BLK_CNT - (2 * SBLOCKSIZE / 512), n,
    161      1.14   xtraeme 			    sbi->ffs->fs_id[0], sbi->ffs->fs_id[1],
    162      1.16   xtraeme 			    (uint64_t)sbi->ffs->fs_size *
    163      1.14   xtraeme 			    sbi->ffs->fs_fsize / 512);
    164      1.14   xtraeme 			break;
    165      1.14   xtraeme 		case FSTYPE_FFSV2:
    166      1.14   xtraeme 			(void)printf("offset: %" PRIu64 " n: %d "
    167      1.16   xtraeme 			    "id: %x,%x size: %" PRIu64 "\n",
    168      1.14   xtraeme 			    BLK_CNT - (ffsize * SBLOCKSIZE / 512+128),
    169      1.14   xtraeme 			    n, sbi->ffs->fs_id[0], sbi->ffs->fs_id[1],
    170      1.16   xtraeme 			    (uint64_t)sbi->ffs->fs_size *
    171      1.14   xtraeme 			    sbi->ffs->fs_fsize / 512);
    172      1.14   xtraeme 			break;
    173      1.14   xtraeme 		default:
    174      1.14   xtraeme 			break;
    175      1.14   xtraeme 		}
    176       1.3  christos 		break;
    177       1.3  christos 	case LABELS:
    178       1.3  christos 		(void)printf("X:  %9" PRIu64,
    179      1.16   xtraeme 			(uint64_t)(sbi->ffs->fs_size *
    180      1.14   xtraeme 			sbi->ffs->fs_fsize / 512));
    181      1.16   xtraeme 		switch (ffs_checkver(sbi)) {
    182       1.3  christos 		case FSTYPE_FFSV1:
    183       1.3  christos 			(void)printf(" %9" PRIu64,
    184      1.14   xtraeme 			    BLK_CNT - (ffsize * SBLOCKSIZE / 512));
    185       1.3  christos 			break;
    186       1.3  christos 		case FSTYPE_FFSV2:
    187       1.3  christos 			(void)printf(" %9" PRIu64,
    188      1.14   xtraeme 			    BLK_CNT - (ffsize * SBLOCKSIZE / 512 + 128));
    189       1.3  christos 			break;
    190       1.3  christos 		default:
    191       1.3  christos 			break;
    192       1.3  christos 		}
    193       1.7   xtraeme 		(void)printf(" 4.2BSD %6d %5d %7d # %s [%s]\n",
    194      1.14   xtraeme 			sbi->ffs->fs_fsize, sbi->ffs->fs_bsize,
    195      1.14   xtraeme 			sbi->ffs->fs_old_cpg,
    196      1.16   xtraeme 			sbi->ffs_path, fstypes[ffs_checkver(sbi)]);
    197       1.3  christos 		break;
    198      1.17  perseant 	case BLOCKS:
    199       1.3  christos 	default:
    200      1.16   xtraeme 		(void)printf("%s ", fstypes[ffs_checkver(sbi)]);
    201      1.17  perseant 		ver = ffs_checkver(sbi);
    202      1.17  perseant 		if (ver == FSTYPE_NONE)
    203       1.3  christos 			break;
    204      1.17  perseant 
    205      1.17  perseant 		offset = 0;
    206      1.17  perseant 		if (flag == BLOCKS)
    207      1.17  perseant 			(void)printf("sb ");
    208      1.17  perseant 		else if (ver == FSTYPE_FFSV1)
    209      1.17  perseant 			offset = (2 * SBLOCKSIZE / 512);
    210      1.17  perseant 		else if (ver == FSTYPE_FFSV2)
    211      1.17  perseant 			offset = (ffsize * SBLOCKSIZE / 512 + 128);
    212      1.17  perseant 
    213      1.17  perseant 		(void)printf("at %" PRIu64, BLK_CNT - offset);
    214       1.1   xtraeme 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    215      1.16   xtraeme 			(uint64_t)(sbi->ffs->fs_size *
    216      1.14   xtraeme 			sbi->ffs->fs_fsize / 512), sbi->ffs_path);
    217       1.3  christos 		break;
    218       1.1   xtraeme 	}
    219       1.1   xtraeme }
    220       1.1   xtraeme 
    221       1.1   xtraeme static void
    222      1.14   xtraeme ffs_scan(struct sblockinfo *sbi, int n)
    223       1.1   xtraeme {
    224      1.14   xtraeme 	size_t i = 0;
    225       1.1   xtraeme 
    226      1.17  perseant 	if (flags & BLOCKS) {
    227      1.17  perseant 		ffs_printpart(sbi, BLOCKS, 0, n);
    228      1.17  perseant 		return;
    229      1.17  perseant 	}
    230       1.1   xtraeme 	if (flags & VERBOSE)
    231      1.14   xtraeme 		ffs_printpart(sbi, VERBOSE, NADA, n);
    232      1.16   xtraeme 	switch (ffs_checkver(sbi)) {
    233       1.3  christos 	case FSTYPE_FFSV1:
    234      1.14   xtraeme 		/* fsize/bsize > 512/4096 and < 4096/32768. */
    235      1.14   xtraeme 		if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 512)) {
    236      1.14   xtraeme 			i = 2;
    237      1.14   xtraeme 		/* fsize/bsize 4096/32768. */
    238      1.14   xtraeme 		} else if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 170)) {
    239      1.14   xtraeme 			i = 4;
    240      1.14   xtraeme 		/* fsize/bsize 8192/65536 */
    241      1.14   xtraeme 		} else if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 73)) {
    242      1.14   xtraeme 			i = 8;
    243      1.14   xtraeme 		} else
    244      1.14   xtraeme 			break;
    245      1.14   xtraeme 
    246      1.14   xtraeme 		if (flags & LABELS)
    247      1.14   xtraeme 			ffs_printpart(sbi, LABELS, i, n);
    248      1.14   xtraeme 		else
    249      1.14   xtraeme 			ffs_printpart(sbi, NADA, i, n);
    250      1.14   xtraeme 
    251       1.3  christos 		break;
    252       1.3  christos 	case FSTYPE_FFSV2:
    253       1.3  christos 		/*
    254       1.3  christos 		 * That checks for FFSv2 partitions with fragsize/blocksize:
    255       1.3  christos 		 * 512/4096, 1024/8192, 2048/16384, 4096/32768 and 8192/65536.
    256       1.3  christos 		 * Really enough for now.
    257       1.3  christos 		 */
    258       1.3  christos 		for (i = 1; i < 16; i <<= 1)
    259      1.21     lukem 			if ((BLK_CNT - lastblk) == (daddr_t)(i * SBLOCKSIZE / 512)) {
    260       1.3  christos 				if (flags & LABELS)
    261      1.14   xtraeme 					ffs_printpart(sbi, LABELS, i, n);
    262       1.3  christos 				else
    263      1.14   xtraeme 					ffs_printpart(sbi, NADA, i, n);
    264       1.3  christos 			}
    265       1.5   xtraeme 		break;
    266       1.5   xtraeme 	}
    267       1.5   xtraeme }
    268       1.5   xtraeme 
    269       1.5   xtraeme static void
    270      1.14   xtraeme lfs_printpart(struct sblockinfo *sbi, int flag, int n)
    271       1.5   xtraeme {
    272       1.5   xtraeme 	if (flags & VERBOSE)
    273      1.29  dholland                	(void)printf("offset: %" PRIu64 " size %" PRIu64
    274      1.26  dholland 			" fsid %" PRIx32 "\n", sbi->lfs_off,
    275      1.26  dholland 			lfs_sb_getsize(sbi->lfs),
    276      1.27  dholland 			lfs_sb_getident(sbi->lfs));
    277       1.5   xtraeme 	switch (flag) {
    278       1.5   xtraeme 	case LABELS:
    279       1.5   xtraeme 		(void)printf("X:  %9" PRIu64,
    280      1.29  dholland                		(lfs_sb_getsize(sbi->lfs) *
    281      1.26  dholland                		lfs_sb_getfsize(sbi->lfs) / 512));
    282       1.5   xtraeme 		(void)printf(" %9" PRIu64, sbi->lfs_off);
    283      1.32  dholland 		(void)printf(" 4.4LFS %6d %5d %7d # %s [LFS%d v%d]\n",
    284      1.26  dholland 			lfs_sb_getfsize(sbi->lfs), lfs_sb_getbsize(sbi->lfs),
    285      1.26  dholland 			lfs_sb_getnseg(sbi->lfs), sbi->lfs_path,
    286      1.32  dholland 			sbi->lfs->lfs_is64 ? 64 : 32,
    287      1.30  dholland 			lfs_sb_getversion(sbi->lfs));
    288       1.5   xtraeme 		break;
    289      1.17  perseant 	case BLOCKS:
    290      1.32  dholland 		(void)printf("LFS%d v%d", sbi->lfs->lfs_is64 ? 64 : 32,
    291      1.32  dholland 			lfs_sb_getversion(sbi->lfs));
    292      1.17  perseant 		(void)printf(" sb at %" PRIu64, sbi->lfs_off + btodb(LFS_LABELPAD));
    293      1.27  dholland 		(void)printf(" fsid %" PRIx32, lfs_sb_getident(sbi->lfs));
    294      1.17  perseant 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    295      1.29  dholland 			(lfs_sb_getsize(sbi->lfs) *
    296      1.26  dholland 			lfs_sb_getfsize(sbi->lfs) / 512), sbi->lfs_path);
    297      1.17  perseant 		break;
    298       1.5   xtraeme 	default:
    299      1.32  dholland 		(void)printf("LFS%d v%d ", sbi->lfs->lfs_is64 ? 64 : 32,
    300      1.32  dholland 			lfs_sb_getversion(sbi->lfs));
    301      1.14   xtraeme 		(void)printf("at %" PRIu64, sbi->lfs_off);
    302       1.5   xtraeme 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    303      1.29  dholland 			(lfs_sb_getsize(sbi->lfs) *
    304      1.26  dholland 			lfs_sb_getfsize(sbi->lfs) / 512), sbi->lfs_path);
    305       1.5   xtraeme 		break;
    306       1.5   xtraeme 	}
    307       1.5   xtraeme }
    308       1.5   xtraeme 
    309       1.5   xtraeme static void
    310      1.14   xtraeme lfs_scan(struct sblockinfo *sbi, int n)
    311       1.5   xtraeme {
    312      1.27  dholland 	size_t namesize;
    313      1.27  dholland 
    314      1.17  perseant 	/* Check to see if the sb checksums correctly */
    315      1.31  dholland 	if (lfs_sb_cksum(sbi->lfs) != lfs_sb_getcksum(sbi->lfs)) {
    316      1.17  perseant 		if (flags & VERBOSE)
    317      1.17  perseant 			printf("LFS bad superblock at %" PRIu64 "\n",
    318      1.17  perseant 				BLK_CNT);
    319      1.17  perseant 		return;
    320      1.17  perseant 	}
    321      1.17  perseant 
    322       1.5   xtraeme 	/* backup offset */
    323      1.14   xtraeme 	lastblk = BLK_CNT - (LFS_SBPAD / 512);
    324       1.5   xtraeme 	/* increment counter */
    325       1.5   xtraeme         ++sbaddr;
    326       1.5   xtraeme 
    327      1.17  perseant 	if (flags & BLOCKS) {
    328      1.17  perseant 		sbi->lfs_off = BLK_CNT - btodb(LFS_LABELPAD);
    329      1.17  perseant 		lfs_printpart(sbi, BLOCKS, n);
    330      1.17  perseant 		return;
    331      1.17  perseant 	}
    332      1.17  perseant 
    333       1.5   xtraeme 	switch (sbaddr) {
    334       1.5   xtraeme 	/*
    335       1.5   xtraeme 	 * first superblock contains the right offset, but lfs_fsmnt is
    336      1.17  perseant 	 * empty... fortunately the next superblock address has it.
    337       1.5   xtraeme 	 */
    338       1.5   xtraeme 	case FIRST_SBLOCK_ADDRESS:
    339       1.5   xtraeme 		/* copy partition offset */
    340      1.21     lukem 		if ((daddr_t)sbi->lfs_off != lastblk)
    341      1.17  perseant 			sbi->lfs_off = BLK_CNT - (LFS_LABELPAD / 512);
    342       1.5   xtraeme 		break;
    343       1.5   xtraeme 	case SECOND_SBLOCK_ADDRESS:
    344       1.5   xtraeme 		/* copy the path of last mount */
    345      1.31  dholland 		namesize = MIN(sizeof(sbi->lfs_path), MIN(
    346      1.31  dholland 			       sizeof(sbi->lfs->lfs_dlfs_u.u_32.dlfs_fsmnt),
    347      1.31  dholland 			       sizeof(sbi->lfs->lfs_dlfs_u.u_64.dlfs_fsmnt)));
    348      1.31  dholland 		(void)memcpy(sbi->lfs_path, lfs_sb_getfsmnt(sbi->lfs),
    349      1.27  dholland 			     namesize);
    350      1.27  dholland 		sbi->lfs_path[namesize - 1] = '\0';
    351       1.5   xtraeme 		/* print now that we have the info */
    352       1.5   xtraeme 		if (flags & LABELS)
    353      1.14   xtraeme 			lfs_printpart(sbi, LABELS, n);
    354       1.5   xtraeme 		else
    355      1.14   xtraeme 			lfs_printpart(sbi, NADA, n);
    356       1.5   xtraeme 		/* clear our struct */
    357      1.14   xtraeme 		(void)memset(sbi, 0, sizeof(*sbi));
    358       1.5   xtraeme 		break;
    359       1.5   xtraeme 	case MAX_SBLOCK_ADDRESS:
    360       1.5   xtraeme 		/*
    361       1.5   xtraeme 		 * reset the counter, this is the last superblock address,
    362       1.5   xtraeme 		 * the next one will be another partition maybe.
    363       1.5   xtraeme 		 */
    364       1.5   xtraeme 		sbaddr = 0;
    365       1.5   xtraeme 		break;
    366       1.5   xtraeme 	default:
    367       1.5   xtraeme 		break;
    368       1.1   xtraeme 	}
    369       1.1   xtraeme }
    370       1.1   xtraeme 
    371       1.1   xtraeme static int
    372      1.32  dholland lfs_checkmagic(struct sblockinfo *sbinfo)
    373      1.32  dholland {
    374      1.32  dholland 	switch (sbinfo->lfs->lfs_dlfs_u.u_32.dlfs_magic) {
    375      1.32  dholland 	case LFS_MAGIC:
    376      1.32  dholland 		sbinfo->lfs->lfs_is64 = false;
    377      1.32  dholland 		sbinfo->lfs->lfs_dobyteswap = false;
    378      1.32  dholland 		return 1;
    379      1.32  dholland 	case LFS_MAGIC_SWAPPED:
    380      1.32  dholland 		sbinfo->lfs->lfs_is64 = false;
    381      1.32  dholland 		sbinfo->lfs->lfs_dobyteswap = true;
    382      1.32  dholland 		return 1;
    383      1.32  dholland 	case LFS64_MAGIC:
    384      1.32  dholland 		sbinfo->lfs->lfs_is64 = true;
    385      1.32  dholland 		sbinfo->lfs->lfs_dobyteswap = false;
    386      1.32  dholland 		return 1;
    387      1.32  dholland 	case LFS64_MAGIC_SWAPPED:
    388      1.32  dholland 		sbinfo->lfs->lfs_is64 = true;
    389      1.32  dholland 		sbinfo->lfs->lfs_dobyteswap = true;
    390      1.32  dholland 		return 1;
    391      1.32  dholland 	default:
    392      1.32  dholland 		return 0;
    393      1.32  dholland 	}
    394      1.32  dholland }
    395      1.32  dholland 
    396      1.34  christos static void
    397      1.34  christos show_status(uintmax_t beg, uintmax_t total)
    398      1.34  christos {
    399      1.34  christos 	static int ttyfd = -2;
    400      1.34  christos 	char buf[2048];
    401      1.34  christos 	const uintmax_t done = blk - beg;
    402      1.34  christos 	const double pcent = (100.0 * done) / total;
    403      1.34  christos 	int n;
    404      1.34  christos 
    405      1.34  christos 	if (ttyfd == -2)
    406      1.35  christos 		ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC);
    407      1.34  christos 
    408      1.34  christos 	if (ttyfd == -1)
    409      1.34  christos 		return;
    410      1.34  christos 
    411      1.34  christos 	n = snprintf(buf, sizeof(buf), "%s: done %ju of %ju blocks (%3.2f%%)\n",
    412      1.34  christos 	    getprogname(), done, total, pcent);
    413      1.34  christos 
    414      1.34  christos 	if (n <= 0)
    415      1.34  christos 		return;
    416      1.34  christos 	write(ttyfd, buf, (size_t)n);
    417      1.34  christos }
    418      1.34  christos 
    419      1.32  dholland static int
    420       1.5   xtraeme scan_disk(int fd, daddr_t beg, daddr_t end, int fflags)
    421       1.1   xtraeme {
    422      1.14   xtraeme 	struct sblockinfo sbinfo;
    423      1.10   xtraeme 	uint8_t buf[SBLOCKSIZE * SBCOUNT];
    424      1.16   xtraeme 	int n;
    425       1.1   xtraeme 
    426      1.16   xtraeme 	n = 0;
    427       1.1   xtraeme 	lastblk = -1;
    428       1.5   xtraeme 
    429       1.5   xtraeme 	/* clear our struct before using it */
    430       1.5   xtraeme 	(void)memset(&sbinfo, 0, sizeof(sbinfo));
    431       1.1   xtraeme 
    432       1.1   xtraeme 	if (fflags & LABELS)
    433       1.3  christos 		(void)printf(
    434       1.5   xtraeme 		    "#        size    offset fstype [fsize bsize cpg/sgs]\n");
    435       1.1   xtraeme 
    436      1.33       mrg 	const daddr_t total = end - beg;
    437      1.16   xtraeme 	for (blk = beg; blk <= end; blk += SBPASS) {
    438      1.33       mrg 		if (print_info) {
    439      1.34  christos 			show_status(beg, total);
    440      1.33       mrg 
    441      1.33       mrg 			print_info = 0;
    442      1.33       mrg 		}
    443      1.16   xtraeme 		if (pread(fd, buf, sizeof(buf), blk * 512) == -1) {
    444      1.16   xtraeme 			if (fflag && fd >= 0)
    445      1.16   xtraeme 				(void)close(fd);
    446       1.7   xtraeme 			err(1, "pread");
    447      1.16   xtraeme 		}
    448       1.1   xtraeme 
    449       1.1   xtraeme 		for (n = 0; n < (SBLOCKSIZE * SBCOUNT); n += 512) {
    450      1.16   xtraeme 			sbinfo.ffs = (struct fs *)&buf[n];
    451      1.16   xtraeme 			sbinfo.lfs = (struct lfs *)&buf[n];
    452      1.16   xtraeme 
    453      1.16   xtraeme 			switch (ffs_checkver(&sbinfo)) {
    454       1.5   xtraeme 			case FSTYPE_FFSV1:
    455       1.5   xtraeme 			case FSTYPE_FFSV2:
    456      1.14   xtraeme 				ffs_scan(&sbinfo, n);
    457      1.14   xtraeme 				lastblk = BLK_CNT;
    458       1.5   xtraeme 				(void)memcpy(sbinfo.ffs_path,
    459       1.5   xtraeme 					sbinfo.ffs->fs_fsmnt, MAXMNTLEN);
    460       1.5   xtraeme 				break;
    461       1.5   xtraeme 			case FSTYPE_NONE:
    462       1.5   xtraeme 				/* maybe LFS? */
    463      1.32  dholland 				if (lfs_checkmagic(&sbinfo))
    464      1.14   xtraeme 					lfs_scan(&sbinfo, n);
    465       1.5   xtraeme 				break;
    466       1.5   xtraeme 			default:
    467       1.5   xtraeme 				break;
    468       1.5   xtraeme 			}
    469       1.1   xtraeme 		}
    470       1.1   xtraeme 	}
    471      1.16   xtraeme 
    472      1.16   xtraeme 	if (fflag && fd >= 0)
    473      1.16   xtraeme 		(void)close(fd);
    474      1.16   xtraeme 
    475       1.2    kleink 	return EXIT_SUCCESS;
    476       1.1   xtraeme }
    477       1.1   xtraeme 
    478       1.1   xtraeme 
    479       1.1   xtraeme static void
    480       1.6  christos usage(void)
    481       1.1   xtraeme {
    482       1.1   xtraeme 	(void)fprintf(stderr,
    483      1.18   xtraeme 		"Usage: %s [-blv] [-e end] [-F file] [-s start] "
    484      1.10   xtraeme 		"device\n", getprogname());
    485       1.2    kleink 	exit(EXIT_FAILURE);
    486       1.1   xtraeme }
    487       1.1   xtraeme 
    488       1.1   xtraeme 
    489       1.1   xtraeme int
    490       1.1   xtraeme main(int argc, char **argv)
    491       1.1   xtraeme {
    492       1.1   xtraeme 	int ch, fd;
    493      1.10   xtraeme 	const char *fpath;
    494       1.1   xtraeme 	daddr_t end = -1, beg = 0;
    495       1.1   xtraeme 	struct disklabel dl;
    496       1.1   xtraeme 
    497      1.10   xtraeme 	fpath = NULL;
    498      1.10   xtraeme 
    499       1.6  christos 	setprogname(*argv);
    500      1.17  perseant 	while ((ch = getopt(argc, argv, "be:F:ls:v")) != -1)
    501       1.1   xtraeme 		switch(ch) {
    502      1.17  perseant 		case 'b':
    503      1.17  perseant 			flags |= BLOCKS;
    504      1.17  perseant 			flags &= ~LABELS;
    505      1.17  perseant 			break;
    506       1.1   xtraeme 		case 'e':
    507       1.1   xtraeme 			eflag = 1;
    508       1.1   xtraeme 			end = atoi(optarg);
    509       1.1   xtraeme 			break;
    510      1.11   xtraeme 		case 'F':
    511      1.10   xtraeme 			fflag = 1;
    512      1.10   xtraeme 			fpath = optarg;
    513      1.10   xtraeme 			break;
    514       1.1   xtraeme 		case 'l':
    515       1.1   xtraeme 			flags |= LABELS;
    516      1.17  perseant 			flags &= ~BLOCKS;
    517       1.1   xtraeme 			break;
    518       1.1   xtraeme 		case 's':
    519       1.1   xtraeme 			beg = atoi(optarg);
    520       1.1   xtraeme 			break;
    521       1.1   xtraeme 		case 'v':
    522       1.1   xtraeme 			flags |= VERBOSE;
    523       1.1   xtraeme 			break;
    524       1.1   xtraeme 		default:
    525       1.6  christos 			usage();
    526       1.1   xtraeme 			/* NOTREACHED */
    527       1.3  christos 		}
    528       1.3  christos 
    529       1.1   xtraeme 	argc -= optind;
    530       1.1   xtraeme 	argv += optind;
    531       1.1   xtraeme 
    532      1.33       mrg 	signal(SIGINFO, got_siginfo);
    533      1.33       mrg 
    534      1.10   xtraeme 	if (fflag) {
    535      1.10   xtraeme 		struct stat stp;
    536       1.1   xtraeme 
    537      1.10   xtraeme 		if (stat(fpath, &stp))
    538      1.10   xtraeme 			err(1, "Cannot stat `%s'", fpath);
    539       1.1   xtraeme 
    540      1.10   xtraeme 		if (!eflag)
    541      1.12   xtraeme 			end = (uint64_t)stp.st_size;
    542       1.1   xtraeme 
    543      1.16   xtraeme 		(void)printf("Total file size: %" PRIu64 "\n\n",
    544      1.16   xtraeme 		    (uint64_t)stp.st_size);
    545      1.16   xtraeme 
    546      1.15   xtraeme 		fd = open(fpath, O_RDONLY | O_DIRECT);
    547       1.3  christos 	} else {
    548      1.10   xtraeme 		if (argc != 1)
    549      1.10   xtraeme 			usage();
    550      1.10   xtraeme 
    551      1.10   xtraeme 		fd = opendisk(argv[0], O_RDONLY, device, sizeof(device), 0);
    552      1.10   xtraeme 
    553      1.10   xtraeme 		if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    554      1.10   xtraeme 			warn("Couldn't retrieve disklabel");
    555      1.10   xtraeme 			(void)memset(&dl, 0, sizeof(dl));
    556      1.10   xtraeme 			dl.d_secperunit = 0x7fffffff;
    557      1.10   xtraeme 		} else {
    558      1.10   xtraeme 			(void)printf("Disk: %s\n", dl.d_typename);
    559      1.10   xtraeme 			(void)printf("Total sectors on disk: %" PRIu32 "\n\n",
    560      1.10   xtraeme 			    dl.d_secperunit);
    561      1.10   xtraeme 		}
    562       1.1   xtraeme 	}
    563      1.10   xtraeme 
    564      1.10   xtraeme 	if (!eflag && !fflag)
    565       1.1   xtraeme 		end = dl.d_secperunit; /* default to max sectors */
    566       1.1   xtraeme 
    567      1.10   xtraeme 	if (fd == -1)
    568      1.10   xtraeme 		err(1, "Cannot open `%s'", device);
    569      1.10   xtraeme 		/* NOTREACHED */
    570      1.10   xtraeme 
    571       1.5   xtraeme 	return scan_disk(fd, beg, end, flags);
    572       1.1   xtraeme }
    573