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