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