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