GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/scan_ffs/scan_ffs.c Lines: 0 58 0.0 %
Date: 2016-12-06 Branches: 0 42 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: scan_ffs.c,v 1.21 2015/11/23 19:19:30 deraadt Exp $	*/
2
3
/*
4
 * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include <sys/types.h>
29
#include <sys/fcntl.h>
30
#include <ufs/ffs/fs.h>
31
#include <unistd.h>
32
#include <stdlib.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <time.h>
36
#include <limits.h>
37
#include <err.h>
38
#include <util.h>
39
40
#define SBCOUNT 64		/* XXX - Should be configurable */
41
42
/* Flags to control ourselves... */
43
#define FLAG_VERBOSE		1
44
#define FLAG_SMART		2
45
#define FLAG_LABELS		4
46
47
static void usage(void);
48
49
static int
50
ufsscan(int fd, daddr_t beg, daddr_t end, int flags)
51
{
52
	static char lastmount[MAXMNTLEN];
53
	static u_int8_t buf[SBSIZE * SBCOUNT];
54
	struct fs *sb;
55
	daddr_t blk, lastblk;
56
	int n;
57
58
	lastblk = -1;
59
	memset(lastmount, 0, MAXMNTLEN);
60
61
	for (blk = beg; blk <= ((end<0)?blk:end); blk += (SBCOUNT*SBSIZE/512)){
62
		memset(buf, 0, SBSIZE * SBCOUNT);
63
		if (lseek(fd, (off_t)blk * 512, SEEK_SET) < 0)
64
		    err(1, "lseek");
65
		if (read(fd, buf, SBSIZE * SBCOUNT) < 0)
66
			err(1, "read");
67
68
		for (n = 0; n < (SBSIZE * SBCOUNT); n += 512){
69
			sb = (struct fs*)(&buf[n]);
70
			if (sb->fs_magic == FS_MAGIC) {
71
				if (flags & FLAG_VERBOSE)
72
					printf("block %lld id %x,%x size %d\n",
73
					    (long long)(blk + (n/512)),
74
					    sb->fs_id[0], sb->fs_id[1],
75
					    sb->fs_ffs1_size);
76
77
				if (((blk+(n/512)) - lastblk) == (SBSIZE/512)) {
78
					if (flags & FLAG_LABELS ) {
79
						printf("X: %lld %lld 4.2BSD %d %d %d # %s\n",
80
						    ((off_t)sb->fs_ffs1_size *
81
						    sb->fs_fsize / 512),
82
						    (long long)(blk + (n/512) -
83
						    (2*SBSIZE/512)),
84
						    sb->fs_fsize, sb->fs_bsize,
85
						    sb->fs_cpg, lastmount);
86
					} else {
87
						/* XXX 2038 */
88
						time_t t = sb->fs_ffs1_time;
89
90
						printf("ffs at %lld size %lld "
91
						    "mount %s time %s",
92
						    (long long)(blk+(n/512) -
93
						    (2*SBSIZE/512)),
94
						    (long long)(off_t)sb->fs_ffs1_size *
95
						    sb->fs_fsize,
96
						    lastmount, ctime(&t));
97
					}
98
99
					if (flags & FLAG_SMART) {
100
						off_t size = (off_t)sb->fs_ffs1_size *
101
						    sb->fs_fsize;
102
103
						if ((n + size) < (SBSIZE * SBCOUNT))
104
							n += size;
105
						else {
106
							blk += (size/512 -
107
							    (SBCOUNT*SBCOUNT));
108
							break;
109
						}
110
					}
111
				}
112
113
				/* Update last potential FS SBs seen */
114
				lastblk = blk + (n/512);
115
				memcpy(lastmount, sb->fs_fsmnt, MAXMNTLEN);
116
			}
117
		}
118
	}
119
	return(0);
120
}
121
122
123
static void
124
usage(void)
125
{
126
	extern char *__progname;
127
128
	fprintf(stderr, "usage: %s [-lsv] [-b begin] [-e end] device\n",
129
	    __progname);
130
	exit(1);
131
}
132
133
134
int
135
main(int argc, char *argv[])
136
{
137
	int ch, fd, flags = 0;
138
	daddr_t beg = 0, end = -1;
139
	const char *errstr;
140
141
	if (pledge("stdio rpath disklabel cpath wpath", NULL) == -1)
142
		err(1, "pledge");
143
144
	while ((ch = getopt(argc, argv, "lsvb:e:")) != -1)
145
		switch(ch) {
146
		case 'b':
147
			beg = strtonum(optarg, 0, LLONG_MAX, &errstr);
148
			if (errstr)
149
				errx(1, "%s: %s", optarg, errstr);
150
			break;
151
		case 'e':
152
			end = strtonum(optarg, 0, LLONG_MAX, &errstr);
153
			if (errstr)
154
				errx(1, "%s: %s", optarg, errstr);
155
			break;
156
		case 'v':
157
			flags |= FLAG_VERBOSE;
158
			break;
159
		case 's':
160
			flags |= FLAG_SMART;
161
			break;
162
		case 'l':
163
			flags |= FLAG_LABELS;
164
			break;
165
		default:
166
			usage();
167
			/* NOTREACHED */
168
	}
169
	argc -= optind;
170
	argv += optind;
171
172
	if (argc != 1)
173
		usage();
174
175
	fd = opendev(argv[0], O_RDONLY, OPENDEV_PART, NULL);
176
	if (fd < 0)
177
		err(1, "%s", argv[0]);
178
179
	if (pledge("stdio rpath cpath wpath", NULL) == -1)
180
		err(1, "pledge");
181
182
	return (ufsscan(fd, beg, end, flags));
183
}