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