1 /* $OpenBSD: scan_ffs.c,v 1.24 2024/05/09 08:35:40 florian 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 void 51 print_info(int flags, struct fs *sb, long long at, char* lastmount) 52 { 53 if (flags & FLAG_LABELS ) { 54 printf("X: %lld %lld 4.2BSD %d %d %d # %s\n", 55 ((off_t)sb->fs_ffs1_size * sb->fs_fsize / 512), at, 56 sb->fs_fsize, sb->fs_bsize, sb->fs_cpg, lastmount); 57 } else { 58 /* XXX 2038 */ 59 time_t t = sb->fs_ffs1_time; 60 char *ct = ctime(&t); 61 if (ct) 62 printf("ffs at %lld size %lld mount %s time %s", at, 63 (long long)(off_t) sb->fs_ffs1_size * sb->fs_fsize, 64 lastmount, ct); 65 else 66 printf("ffs at %lld size %lld mount %s time %lld\n", at, 67 (long long)(off_t) sb->fs_ffs1_size * sb->fs_fsize, 68 lastmount, t); 69 } 70 } 71 72 static int 73 ufsscan(int fd, daddr_t beg, daddr_t end, int flags) 74 { 75 static char lastmount[MAXMNTLEN]; 76 static u_int8_t buf[SBSIZE * SBCOUNT]; 77 struct fs *sb; 78 daddr_t blk, lastblk; 79 int n; 80 81 lastblk = -1; 82 memset(lastmount, 0, MAXMNTLEN); 83 84 for (blk = beg; blk <= ((end<0)?blk:end); blk += (SBCOUNT*SBSIZE/512)){ 85 memset(buf, 0, SBSIZE * SBCOUNT); 86 if (lseek(fd, (off_t)blk * 512, SEEK_SET) == -1) 87 err(1, "lseek"); 88 if (read(fd, buf, SBSIZE * SBCOUNT) == -1) 89 err(1, "read"); 90 91 for (n = 0; n < (SBSIZE * SBCOUNT); n += 512){ 92 sb = (struct fs*)(&buf[n]); 93 if (sb->fs_magic == FS_MAGIC) { 94 if (flags & FLAG_VERBOSE) 95 printf("block %lld id %x,%x size %d\n", 96 (long long)(blk + (n/512)), 97 sb->fs_id[0], sb->fs_id[1], 98 sb->fs_ffs1_size); 99 100 if (((blk+(n/512)) - lastblk) == (SBSIZE/512)) { 101 print_info(flags, sb, 102 (long long)(blk + (n/512) - 103 (2*SBSIZE/512)), lastmount); 104 if (flags & FLAG_SMART) { 105 off_t size = (off_t)sb->fs_ffs1_size * 106 sb->fs_fsize; 107 108 if ((n + size) < (SBSIZE * SBCOUNT)) 109 n += size; 110 else { 111 blk += (size/512 - 112 (SBCOUNT*SBCOUNT)); 113 break; 114 } 115 } 116 } 117 118 /* Update last potential FS SBs seen */ 119 lastblk = blk + (n/512); 120 memcpy(lastmount, sb->fs_fsmnt, MAXMNTLEN); 121 } 122 } 123 } 124 return(0); 125 } 126 127 128 static void 129 usage(void) 130 { 131 extern char *__progname; 132 133 fprintf(stderr, "usage: %s [-lsv] [-b begin] [-e end] device\n", 134 __progname); 135 exit(1); 136 } 137 138 139 int 140 main(int argc, char *argv[]) 141 { 142 int ch, fd, flags = 0; 143 daddr_t beg = 0, end = -1; 144 const char *errstr; 145 146 if (pledge("stdio rpath disklabel", NULL) == -1) 147 err(1, "pledge"); 148 149 while ((ch = getopt(argc, argv, "lsvb:e:")) != -1) 150 switch(ch) { 151 case 'b': 152 beg = strtonum(optarg, 0, LLONG_MAX, &errstr); 153 if (errstr) 154 errx(1, "%s: %s", optarg, errstr); 155 break; 156 case 'e': 157 end = strtonum(optarg, 0, LLONG_MAX, &errstr); 158 if (errstr) 159 errx(1, "%s: %s", optarg, errstr); 160 break; 161 case 'v': 162 flags |= FLAG_VERBOSE; 163 break; 164 case 's': 165 flags |= FLAG_SMART; 166 break; 167 case 'l': 168 flags |= FLAG_LABELS; 169 break; 170 default: 171 usage(); 172 /* NOTREACHED */ 173 } 174 argc -= optind; 175 argv += optind; 176 177 if (argc != 1) 178 usage(); 179 180 fd = opendev(argv[0], O_RDONLY, OPENDEV_PART, NULL); 181 if (fd == -1) 182 err(1, "%s", argv[0]); 183 184 if (pledge("stdio", NULL) == -1) 185 err(1, "pledge"); 186 187 return (ufsscan(fd, beg, end, flags)); 188 } 189