1 /* $OpenBSD: scan_ffs.c,v 1.4 1998/03/28 01:18:38 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, 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 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 %d mount %s time %s", 91 blk+(n/512)-(2*SBSIZE/512), 92 sb->fs_size * sb->fs_fsize, 93 lastmount, ctime(&sb->fs_time)); 94 } 95 96 if (flags & FLAG_SMART) { 97 int size = 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() 121 { 122 fprintf(stderr, "usage: scan_ffs [-lsv] [-b begin] [-e end] <device>\n"); 123 exit(1); 124 } 125 126 127 int 128 main(argc, argv) 129 int argc; 130 char *argv[]; 131 { 132 int ch, fd, flags = 0; 133 daddr_t beg = 0, end = -1; 134 135 while ((ch = getopt(argc, argv, "lsvb:e:")) != -1) 136 switch(ch) { 137 case 'b': 138 beg = atoi(optarg); 139 break; 140 case 'e': 141 end = atoi(optarg); 142 break; 143 case 'v': 144 flags |= FLAG_VERBOSE; 145 break; 146 case 's': 147 flags |= FLAG_SMART; 148 break; 149 case 'l': 150 flags |= FLAG_LABELS; 151 break; 152 default: 153 usage(); 154 } 155 argc -= optind; 156 argv += optind; 157 158 if (argc != 1) 159 usage(); 160 161 fd = opendev(argv[0], O_RDONLY, OPENDEV_PART, NULL); 162 if (fd < 0) 163 err(1, "%s", argv[1]); 164 165 return (ufsscan(fd, beg, end, flags)); 166 } 167