1 /* $OpenBSD: scan_ffs.c,v 1.7 2001/07/07 18:26:21 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Tobias Weingartner. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/fcntl.h> 36 #include <ufs/ffs/fs.h> 37 #include <unistd.h> 38 #include <stdlib.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <time.h> 42 #include <err.h> 43 #include <util.h> 44 45 #define SBCOUNT 64 /* XXX - Should be configurable */ 46 47 /* Flags to control ourselves... */ 48 #define FLAG_VERBOSE 1 49 #define FLAG_SMART 2 50 #define FLAG_LABELS 4 51 52 int 53 ufsscan(fd, beg, end, flags) 54 int fd; 55 daddr_t beg, end; 56 int flags; 57 { 58 static char lastmount[MAXMNTLEN]; 59 static u_int8_t buf[SBSIZE * SBCOUNT]; 60 struct fs *sb; 61 daddr_t blk, lastblk; 62 int n; 63 64 lastblk = -1; 65 memset(lastmount, 0, MAXMNTLEN); 66 67 for(blk = beg; blk <= ((end<0)?blk:end); blk += (SBCOUNT*SBSIZE/512)){ 68 memset(buf, 0, SBSIZE * SBCOUNT); 69 if (lseek(fd, (off_t)blk * 512, SEEK_SET) < 0) 70 err(1, "lseek"); 71 if (read(fd, buf, SBSIZE * SBCOUNT) < 0) 72 err(1, "read"); 73 74 for(n = 0; n < (SBSIZE * SBCOUNT); n += 512){ 75 sb = (struct fs*)(&buf[n]); 76 if (sb->fs_magic == FS_MAGIC) { 77 if (flags & FLAG_VERBOSE) 78 printf("block %d id %x,%x size %d\n", 79 blk + (n/512), sb->fs_id[0], 80 sb->fs_id[1], sb->fs_size); 81 82 if (((blk+(n/512)) - lastblk) == (SBSIZE/512)) { 83 if (flags & FLAG_LABELS ) { 84 printf("X: %d %d 4.2BSD %d %d %d # %s\n", 85 (daddr_t)((off_t)sb->fs_size * sb->fs_fsize / 512), 86 blk+(n/512)-(2*SBSIZE/512), 87 sb->fs_fsize, sb->fs_bsize, 88 sb->fs_cpg, lastmount); 89 } else { 90 printf("ffs at %d size %lld mount %s time %s", 91 blk+(n/512)-(2*SBSIZE/512), 92 (long long)(off_t)sb->fs_size * sb->fs_fsize, 93 lastmount, ctime(&sb->fs_time)); 94 } 95 96 if (flags & FLAG_SMART) { 97 off_t size = (off_t)sb->fs_size * sb->fs_fsize; 98 99 if ((n + size) < (SBSIZE * SBCOUNT)) 100 n += size; 101 else { 102 blk += (size/512 - 103 (SBCOUNT*SBCOUNT)); 104 break; 105 } 106 } 107 } 108 109 /* Update last potential FS SBs seen */ 110 lastblk = blk + (n/512); 111 memcpy(lastmount, sb->fs_fsmnt, MAXMNTLEN); 112 } 113 } 114 } 115 return(0); 116 } 117 118 119 void 120 usage(code) 121 int code; 122 { 123 extern char *__progname; 124 (void)fprintf(stderr, "usage: %s [-lsv] [-b begin] [-e end] device\n", __progname); 125 exit(code); 126 } 127 128 129 int 130 main(argc, argv) 131 int argc; 132 char *argv[]; 133 { 134 int ch, fd, flags = 0; 135 daddr_t beg = 0, end = -1; 136 137 while ((ch = getopt(argc, argv, "lsvb:e:")) != -1) 138 switch(ch) { 139 case 'b': 140 beg = atoi(optarg); 141 break; 142 case 'e': 143 end = atoi(optarg); 144 break; 145 case 'v': 146 flags |= FLAG_VERBOSE; 147 break; 148 case 's': 149 flags |= FLAG_SMART; 150 break; 151 case 'l': 152 flags |= FLAG_LABELS; 153 break; 154 default: 155 usage(1); 156 /* NOTREACHED */ 157 } 158 argc -= optind; 159 argv += optind; 160 161 if (argc != 1) 162 usage(1); 163 164 fd = opendev(argv[0], O_RDONLY, OPENDEV_PART, NULL); 165 if (fd < 0) 166 err(1, "%s", argv[1]); 167 168 return (ufsscan(fd, beg, end, flags)); 169 } 170