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