scan_ffs.c revision 1.5 1 1.5 xtraeme /* $NetBSD: scan_ffs.c,v 1.5 2005/07/31 19:26:06 xtraeme Exp $ */
2 1.5 xtraeme
3 1.5 xtraeme /*
4 1.5 xtraeme * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.5 xtraeme * All rights reserved.
6 1.5 xtraeme *
7 1.5 xtraeme * This code is derived from software contributed to The NetBSD Foundation
8 1.5 xtraeme * by Juan Romero Pardines.
9 1.5 xtraeme *
10 1.5 xtraeme * Redistribution and use in source and binary forms, with or without
11 1.5 xtraeme * modification, are permitted provided that the following conditions
12 1.5 xtraeme * are met:
13 1.5 xtraeme * 1. Redistributions of source code must retain the above copyright
14 1.5 xtraeme * notice, this list of conditions and the following disclaimer.
15 1.5 xtraeme * 2. Redistributions in binary form must reproduce the above copyright
16 1.5 xtraeme * notice, this list of conditions and the following disclaimer in the
17 1.5 xtraeme * documentation and/or other materials provided with the distribution.
18 1.5 xtraeme * 3. All advertising materials mentioning features or use of this software
19 1.5 xtraeme * must display the following acknowledgement:
20 1.5 xtraeme * This product includes software developed by Juan Romero Pardines
21 1.5 xtraeme * for the NetBSD Foundation, Inc. and its contributors.
22 1.5 xtraeme * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.5 xtraeme * contributors may be used to endorse or promote products derived
24 1.5 xtraeme * from this software without specific prior written permission.
25 1.5 xtraeme *
26 1.5 xtraeme * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.5 xtraeme * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.5 xtraeme * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.5 xtraeme * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.5 xtraeme * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.5 xtraeme * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.5 xtraeme * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.5 xtraeme * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.5 xtraeme * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.5 xtraeme * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.5 xtraeme * POSSIBILITY OF SUCH DAMAGE.
37 1.5 xtraeme */
38 1.1 xtraeme
39 1.1 xtraeme /*
40 1.1 xtraeme * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
41 1.1 xtraeme * All rights reserved.
42 1.1 xtraeme *
43 1.1 xtraeme * Redistribution and use in source and binary forms, with or without
44 1.1 xtraeme * modification, are permitted provided that the following conditions
45 1.1 xtraeme * are met:
46 1.1 xtraeme * 1. Redistributions of source code must retain the above copyright
47 1.1 xtraeme * notice, this list of conditions and the following disclaimer.
48 1.1 xtraeme * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 xtraeme * notice, this list of conditions and the following disclaimer in the
50 1.1 xtraeme * documentation and/or other materials provided with the distribution.
51 1.1 xtraeme *
52 1.1 xtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 1.1 xtraeme * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 1.1 xtraeme * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 1.1 xtraeme * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 1.1 xtraeme * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 1.1 xtraeme * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 1.1 xtraeme * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 1.1 xtraeme * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 1.1 xtraeme * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 1.1 xtraeme * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.1 xtraeme */
63 1.1 xtraeme
64 1.1 xtraeme /*
65 1.1 xtraeme * Currently it can detect:
66 1.5 xtraeme * o FFSv1 fsize/bsize: 512/4096, 1024/8192, 2048/16384.
67 1.5 xtraeme * o FFSv2 fsize/bsize: 512/4096, 1024/8192, 2048/16384,
68 1.5 xtraeme * 4096/32768, 8192/65536.
69 1.5 xtraeme * o LFSv[12] fsize/bsize: 512/4096, 1024/8192, 2048/16384,
70 1.5 xtraeme * 4096/32768, 8192/65536.
71 1.5 xtraeme *
72 1.1 xtraeme * TODO:
73 1.1 xtraeme * o Detect FFSv1 partitions with fsize/bsize > 2048/16384.
74 1.5 xtraeme *
75 1.5 xtraeme * -- xtraeme --
76 1.1 xtraeme */
77 1.1 xtraeme
78 1.1 xtraeme #include <sys/cdefs.h>
79 1.1 xtraeme #ifndef lint
80 1.5 xtraeme __RCSID("$NetBSD: scan_ffs.c,v 1.5 2005/07/31 19:26:06 xtraeme Exp $");
81 1.1 xtraeme #endif /* not lint */
82 1.1 xtraeme
83 1.1 xtraeme #include <sys/types.h>
84 1.1 xtraeme #include <sys/param.h>
85 1.1 xtraeme #include <sys/disklabel.h>
86 1.1 xtraeme #include <sys/dkio.h>
87 1.1 xtraeme #include <sys/ioctl.h>
88 1.1 xtraeme #include <sys/fcntl.h>
89 1.5 xtraeme #include <sys/queue.h>
90 1.5 xtraeme #include <sys/mount.h>
91 1.5 xtraeme
92 1.5 xtraeme #include <ufs/ufs/dinode.h>
93 1.5 xtraeme #include <ufs/lfs/lfs.h>
94 1.1 xtraeme #include <ufs/ffs/fs.h>
95 1.5 xtraeme
96 1.1 xtraeme #include <unistd.h>
97 1.1 xtraeme #include <stdlib.h>
98 1.1 xtraeme #include <stdio.h>
99 1.1 xtraeme #include <string.h>
100 1.1 xtraeme #include <err.h>
101 1.1 xtraeme #include <util.h>
102 1.1 xtraeme
103 1.5 xtraeme /* common struct for FFS/LFS */
104 1.5 xtraeme struct sblockinfo {
105 1.5 xtraeme struct lfs *lfs;
106 1.5 xtraeme struct fs *ffs;
107 1.5 xtraeme u_int64_t lfs_off;
108 1.5 xtraeme u_int64_t ffs_off;
109 1.5 xtraeme char lfs_path[MAXMNTLEN];
110 1.5 xtraeme char ffs_path[MAXMNTLEN];
111 1.5 xtraeme } sbinfo;
112 1.1 xtraeme
113 1.5 xtraeme static daddr_t blk, lastblk;
114 1.1 xtraeme
115 1.5 xtraeme static int eflag = 0;
116 1.5 xtraeme static int flags = 0;
117 1.5 xtraeme static int sbaddr = 0; /* counter for the LFS superblocks */
118 1.1 xtraeme
119 1.1 xtraeme static char device[MAXPATHLEN];
120 1.5 xtraeme static const char *fstypes[] = { "NONE", "FFSv1", "FFSv2", "LFS" };
121 1.5 xtraeme
122 1.5 xtraeme #define FSTYPE_NONE 0
123 1.5 xtraeme #define FSTYPE_FFSV1 1
124 1.5 xtraeme #define FSTYPE_FFSV2 2
125 1.1 xtraeme
126 1.5 xtraeme #define SBCOUNT 64 /* may be changed */
127 1.5 xtraeme #define SBPASS (SBCOUNT * SBLOCKSIZE / 512)
128 1.1 xtraeme
129 1.5 xtraeme /* This is only useful for LFS */
130 1.1 xtraeme
131 1.5 xtraeme /* first sblock address contains the correct offset */
132 1.5 xtraeme #define FIRST_SBLOCK_ADDRESS 1
133 1.5 xtraeme /* second and third sblock address contain lfs_fsmnt[MAXMNTLEN] */
134 1.5 xtraeme #define SECOND_SBLOCK_ADDRESS 2
135 1.5 xtraeme /* last sblock address in a LFS partition */
136 1.5 xtraeme #define MAX_SBLOCK_ADDRESS 10
137 1.1 xtraeme
138 1.5 xtraeme enum { NADA, VERBOSE, LABELS };
139 1.3 christos
140 1.5 xtraeme /* FFS functions */
141 1.5 xtraeme static void ffs_printpart(int, int, int);
142 1.5 xtraeme static void ffs_scan(int);
143 1.5 xtraeme static int ffs_checkver(void);
144 1.5 xtraeme /* LFS functions */
145 1.5 xtraeme static void lfs_printpart(int, int, struct sblockinfo *);
146 1.5 xtraeme static void lfs_scan(int);
147 1.5 xtraeme /* common functions */
148 1.5 xtraeme static void usage(const char *);
149 1.5 xtraeme static int scan_disk(int, daddr_t, daddr_t, int);
150 1.3 christos
151 1.1 xtraeme static int
152 1.5 xtraeme ffs_checkver(void)
153 1.1 xtraeme {
154 1.5 xtraeme switch (sbinfo.ffs->fs_magic) {
155 1.1 xtraeme case FS_UFS1_MAGIC:
156 1.1 xtraeme case FS_UFS1_MAGIC_SWAPPED:
157 1.5 xtraeme sbinfo.ffs->fs_size = sbinfo.ffs->fs_old_size;
158 1.3 christos return FSTYPE_FFSV1;
159 1.1 xtraeme case FS_UFS2_MAGIC:
160 1.1 xtraeme case FS_UFS2_MAGIC_SWAPPED:
161 1.3 christos return FSTYPE_FFSV2;
162 1.1 xtraeme default:
163 1.3 christos return FSTYPE_NONE;
164 1.1 xtraeme }
165 1.1 xtraeme }
166 1.1 xtraeme
167 1.1 xtraeme static void
168 1.5 xtraeme ffs_printpart(int flag, int ffsize, int n)
169 1.1 xtraeme {
170 1.1 xtraeme
171 1.5 xtraeme int fstype = ffs_checkver();
172 1.1 xtraeme
173 1.3 christos switch (flag) {
174 1.3 christos case VERBOSE:
175 1.5 xtraeme (void)printf("block: %" PRIu64 " id %x,%x size %" PRIu64 "\n",
176 1.5 xtraeme blk + (n / 512),
177 1.5 xtraeme sbinfo.ffs->fs_id[0],
178 1.5 xtraeme sbinfo.ffs->fs_id[1], sbinfo.ffs->fs_size);
179 1.3 christos break;
180 1.3 christos case LABELS:
181 1.3 christos (void)printf("X: %9" PRIu64,
182 1.5 xtraeme (uint64_t)((off_t)sbinfo.ffs->fs_size *
183 1.5 xtraeme sbinfo.ffs->fs_fsize / 512));
184 1.3 christos switch (fstype) {
185 1.3 christos case FSTYPE_FFSV1:
186 1.3 christos (void)printf(" %9" PRIu64,
187 1.3 christos blk + (n / 512) - (2 * SBLOCKSIZE / 512));
188 1.3 christos break;
189 1.3 christos case FSTYPE_FFSV2:
190 1.3 christos (void)printf(" %9" PRIu64,
191 1.3 christos blk + (n / 512) -
192 1.3 christos (ffsize * SBLOCKSIZE / 512 + 128));
193 1.3 christos break;
194 1.3 christos default:
195 1.3 christos break;
196 1.3 christos }
197 1.5 xtraeme (void)printf(" 4.2BSD %6d %5d %8d # %s [%s]\n",
198 1.5 xtraeme sbinfo.ffs->fs_fsize, sbinfo.ffs->fs_bsize,
199 1.5 xtraeme sbinfo.ffs->fs_old_cpg,
200 1.5 xtraeme sbinfo.ffs_path, fstypes[fstype]);
201 1.3 christos break;
202 1.3 christos default:
203 1.3 christos printf("%s ", fstypes[fstype]);
204 1.3 christos switch (fstype) {
205 1.3 christos case FSTYPE_FFSV1:
206 1.3 christos (void)printf("at %" PRIu64,
207 1.3 christos blk + (n / 512) - (2 * SBLOCKSIZE / 512));
208 1.3 christos break;
209 1.3 christos case FSTYPE_FFSV2:
210 1.3 christos (void)printf("at %" PRIu64,
211 1.3 christos blk + (n / 512) -
212 1.3 christos (ffsize * SBLOCKSIZE / 512 + 128));
213 1.3 christos break;
214 1.3 christos default:
215 1.3 christos break;
216 1.3 christos }
217 1.1 xtraeme (void)printf(" size %" PRIu64 ", last mounted on %s\n",
218 1.5 xtraeme (uint64_t)((off_t)sbinfo.ffs->fs_size *
219 1.5 xtraeme sbinfo.ffs->fs_fsize / 512), sbinfo.ffs_path);
220 1.3 christos break;
221 1.1 xtraeme }
222 1.1 xtraeme }
223 1.1 xtraeme
224 1.1 xtraeme static void
225 1.5 xtraeme ffs_scan(int n)
226 1.1 xtraeme {
227 1.5 xtraeme int fstype = ffs_checkver();
228 1.3 christos size_t i;
229 1.1 xtraeme
230 1.1 xtraeme /*
231 1.5 xtraeme * XXX:
232 1.1 xtraeme * It cannot find FFSv1 partitions with fsize/bsize > 2048/16384,
233 1.1 xtraeme * same problem found in the original program that comes from
234 1.5 xtraeme * OpenBSD (scan_ffs(8)).
235 1.1 xtraeme */
236 1.1 xtraeme if (flags & VERBOSE)
237 1.5 xtraeme ffs_printpart(VERBOSE, NADA, n);
238 1.3 christos switch (fstype) {
239 1.3 christos case FSTYPE_FFSV1:
240 1.1 xtraeme if (((blk + (n / 512)) - lastblk) == (SBLOCKSIZE / 512)) {
241 1.1 xtraeme if (flags & LABELS)
242 1.5 xtraeme ffs_printpart(LABELS, NADA, n);
243 1.1 xtraeme else
244 1.5 xtraeme ffs_printpart(NADA, NADA, n);
245 1.1 xtraeme }
246 1.3 christos break;
247 1.3 christos case FSTYPE_FFSV2:
248 1.3 christos /*
249 1.3 christos * That checks for FFSv2 partitions with fragsize/blocksize:
250 1.3 christos * 512/4096, 1024/8192, 2048/16384, 4096/32768 and 8192/65536.
251 1.3 christos * Really enough for now.
252 1.3 christos */
253 1.3 christos for (i = 1; i < 16; i <<= 1)
254 1.3 christos if (((blk + (n / 512)) - lastblk) ==
255 1.3 christos (i * SBLOCKSIZE / 512)) {
256 1.3 christos if (flags & LABELS)
257 1.5 xtraeme ffs_printpart(LABELS, i, n);
258 1.3 christos else
259 1.5 xtraeme ffs_printpart(NADA, i, n);
260 1.3 christos }
261 1.5 xtraeme break;
262 1.5 xtraeme }
263 1.5 xtraeme }
264 1.5 xtraeme
265 1.5 xtraeme static void
266 1.5 xtraeme lfs_printpart(int flag, int n, struct sblockinfo *sbi)
267 1.5 xtraeme {
268 1.5 xtraeme if (flags & VERBOSE)
269 1.5 xtraeme (void)printf("block: %" PRIu64 " size %" PRIu32 "\n",
270 1.5 xtraeme blk + (n / 512), sbi->lfs->lfs_size);
271 1.5 xtraeme switch (flag) {
272 1.5 xtraeme case LABELS:
273 1.5 xtraeme (void)printf("X: %9" PRIu64,
274 1.5 xtraeme (uint64_t)((off_t)sbi->lfs->lfs_size *
275 1.5 xtraeme sbi->lfs->lfs_fsize / 512));
276 1.5 xtraeme (void)printf(" %9" PRIu64, sbi->lfs_off);
277 1.5 xtraeme (void)printf(" 4.4LFS %6d %5d %8d # %s [LFSv%d]\n",
278 1.5 xtraeme sbi->lfs->lfs_fsize, sbi->lfs->lfs_bsize,
279 1.5 xtraeme sbi->lfs->lfs_nseg, sbi->lfs_path,
280 1.5 xtraeme sbi->lfs->lfs_version);
281 1.5 xtraeme break;
282 1.5 xtraeme default:
283 1.5 xtraeme (void)printf("LFSv%d ", sbinfo.lfs->lfs_version);
284 1.5 xtraeme (void)printf("at %" PRIu64, sbinfo.lfs_off);
285 1.5 xtraeme (void)printf(" size %" PRIu64 ", last mounted on %s\n",
286 1.5 xtraeme (uint64_t)((off_t)sbinfo.lfs->lfs_size *
287 1.5 xtraeme sbinfo.lfs->lfs_fsize / 512), sbinfo.lfs_path);
288 1.5 xtraeme break;
289 1.5 xtraeme }
290 1.5 xtraeme }
291 1.5 xtraeme
292 1.5 xtraeme static void
293 1.5 xtraeme lfs_scan(int n)
294 1.5 xtraeme {
295 1.5 xtraeme /* backup offset */
296 1.5 xtraeme lastblk = blk + (n / 512) - (LFS_SBPAD / 512);
297 1.5 xtraeme /* increment counter */
298 1.5 xtraeme ++sbaddr;
299 1.5 xtraeme
300 1.5 xtraeme switch (sbaddr) {
301 1.5 xtraeme /*
302 1.5 xtraeme * first superblock contains the right offset, but lfs_fsmnt is
303 1.5 xtraeme * empty... afortunately the next superblock address has it.
304 1.5 xtraeme */
305 1.5 xtraeme case FIRST_SBLOCK_ADDRESS:
306 1.5 xtraeme /* copy partition offset */
307 1.5 xtraeme if (sbinfo.lfs_off != lastblk)
308 1.5 xtraeme sbinfo.lfs_off = blk + (n / 512) - (LFS_SBPAD / 512);
309 1.5 xtraeme break;
310 1.5 xtraeme case SECOND_SBLOCK_ADDRESS:
311 1.5 xtraeme /* copy the path of last mount */
312 1.5 xtraeme (void)memcpy(sbinfo.lfs_path,
313 1.5 xtraeme sbinfo.lfs->lfs_fsmnt, MAXMNTLEN);
314 1.5 xtraeme /* print now that we have the info */
315 1.5 xtraeme if (flags & LABELS)
316 1.5 xtraeme lfs_printpart(LABELS, n, &sbinfo);
317 1.5 xtraeme else
318 1.5 xtraeme lfs_printpart(NADA, n, &sbinfo);
319 1.5 xtraeme /* clear our struct */
320 1.5 xtraeme (void)memset(&sbinfo, 0, sizeof(sbinfo));
321 1.5 xtraeme break;
322 1.5 xtraeme case MAX_SBLOCK_ADDRESS:
323 1.5 xtraeme /*
324 1.5 xtraeme * reset the counter, this is the last superblock address,
325 1.5 xtraeme * the next one will be another partition maybe.
326 1.5 xtraeme */
327 1.5 xtraeme sbaddr = 0;
328 1.5 xtraeme break;
329 1.5 xtraeme default:
330 1.5 xtraeme break;
331 1.1 xtraeme }
332 1.1 xtraeme }
333 1.1 xtraeme
334 1.1 xtraeme static int
335 1.5 xtraeme scan_disk(int fd, daddr_t beg, daddr_t end, int fflags)
336 1.1 xtraeme {
337 1.1 xtraeme u_int8_t buf[SBLOCKSIZE * SBCOUNT];
338 1.3 christos int n, fstype;
339 1.1 xtraeme
340 1.5 xtraeme n = fstype = 0;
341 1.1 xtraeme lastblk = -1;
342 1.5 xtraeme
343 1.5 xtraeme /* clear our struct before using it */
344 1.5 xtraeme (void)memset(&sbinfo, 0, sizeof(sbinfo));
345 1.1 xtraeme
346 1.1 xtraeme if (fflags & LABELS)
347 1.3 christos (void)printf(
348 1.5 xtraeme "# size offset fstype [fsize bsize cpg/sgs]\n");
349 1.1 xtraeme
350 1.5 xtraeme for (blk = beg; blk <= ((end < 0) ? blk: end); blk += SBPASS) {
351 1.3 christos (void)memset(buf, 0, sizeof(buf));
352 1.1 xtraeme
353 1.3 christos if (lseek(fd, (off_t)blk * 512, SEEK_SET) == (off_t)-1)
354 1.1 xtraeme err(1, "lseek");
355 1.1 xtraeme
356 1.3 christos if (read(fd, buf, sizeof(buf)) == -1)
357 1.1 xtraeme err(1, "read");
358 1.1 xtraeme
359 1.1 xtraeme for (n = 0; n < (SBLOCKSIZE * SBCOUNT); n += 512) {
360 1.5 xtraeme sbinfo.ffs = (struct fs *)(void *)&buf[n];
361 1.5 xtraeme sbinfo.lfs = (struct lfs *)(void *)&buf[n];
362 1.5 xtraeme fstype = ffs_checkver();
363 1.5 xtraeme switch (fstype) {
364 1.5 xtraeme case FSTYPE_FFSV1:
365 1.5 xtraeme case FSTYPE_FFSV2:
366 1.5 xtraeme ffs_scan(n);
367 1.5 xtraeme lastblk = blk + (n / 512);
368 1.5 xtraeme (void)memcpy(sbinfo.ffs_path,
369 1.5 xtraeme sbinfo.ffs->fs_fsmnt, MAXMNTLEN);
370 1.5 xtraeme break;
371 1.5 xtraeme case FSTYPE_NONE:
372 1.5 xtraeme /* maybe LFS? */
373 1.5 xtraeme if (sbinfo.lfs->lfs_magic != LFS_MAGIC)
374 1.5 xtraeme break;
375 1.5 xtraeme if (sbinfo.lfs->lfs_magic == LFS_MAGIC)
376 1.5 xtraeme lfs_scan(n);
377 1.5 xtraeme break;
378 1.5 xtraeme default:
379 1.5 xtraeme break;
380 1.5 xtraeme }
381 1.1 xtraeme }
382 1.1 xtraeme }
383 1.2 kleink return EXIT_SUCCESS;
384 1.1 xtraeme }
385 1.1 xtraeme
386 1.1 xtraeme
387 1.1 xtraeme static void
388 1.5 xtraeme usage(const char *p)
389 1.1 xtraeme {
390 1.1 xtraeme (void)fprintf(stderr,
391 1.5 xtraeme "Usage: scan_%s [-lv] [-s start] [-e end] device\n", p);
392 1.2 kleink exit(EXIT_FAILURE);
393 1.1 xtraeme }
394 1.1 xtraeme
395 1.1 xtraeme
396 1.1 xtraeme int
397 1.1 xtraeme main(int argc, char **argv)
398 1.1 xtraeme {
399 1.1 xtraeme int ch, fd;
400 1.1 xtraeme daddr_t end = -1, beg = 0;
401 1.1 xtraeme struct disklabel dl;
402 1.1 xtraeme
403 1.5 xtraeme const char *prog;
404 1.5 xtraeme
405 1.5 xtraeme if (strstr(argv[0], "lfs"))
406 1.5 xtraeme prog = "lfs";
407 1.5 xtraeme else
408 1.5 xtraeme prog = "ffs";
409 1.1 xtraeme
410 1.1 xtraeme while ((ch = getopt(argc, argv, "e:ls:v")) != -1)
411 1.1 xtraeme switch(ch) {
412 1.1 xtraeme case 'e':
413 1.1 xtraeme eflag = 1;
414 1.1 xtraeme end = atoi(optarg);
415 1.1 xtraeme break;
416 1.1 xtraeme case 'l':
417 1.1 xtraeme flags |= LABELS;
418 1.1 xtraeme break;
419 1.1 xtraeme case 's':
420 1.1 xtraeme beg = atoi(optarg);
421 1.1 xtraeme break;
422 1.1 xtraeme case 'v':
423 1.1 xtraeme flags |= VERBOSE;
424 1.1 xtraeme break;
425 1.1 xtraeme default:
426 1.5 xtraeme usage(prog);
427 1.1 xtraeme /* NOTREACHED */
428 1.3 christos }
429 1.3 christos
430 1.1 xtraeme argc -= optind;
431 1.1 xtraeme argv += optind;
432 1.1 xtraeme
433 1.1 xtraeme if (argc != 1)
434 1.5 xtraeme usage(prog);
435 1.1 xtraeme
436 1.1 xtraeme fd = opendisk(argv[0], O_RDONLY, device, sizeof(device), 0);
437 1.1 xtraeme
438 1.3 christos if (fd == -1)
439 1.3 christos err(1, "Cannot open `%s'", device);
440 1.1 xtraeme /* NOTREACHED */
441 1.1 xtraeme
442 1.3 christos if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
443 1.3 christos warn("Couldn't retrieve disklabel");
444 1.3 christos (void)memset(&dl, 0, sizeof(dl));
445 1.3 christos dl.d_secperunit = 0x7fffffff;
446 1.3 christos } else {
447 1.1 xtraeme (void)printf("Disk: %s\n", dl.d_typename);
448 1.1 xtraeme (void)printf("Total sectors on disk: %" PRIu32 "\n\n",
449 1.3 christos dl.d_secperunit);
450 1.1 xtraeme }
451 1.1 xtraeme
452 1.1 xtraeme if (!eflag)
453 1.1 xtraeme end = dl.d_secperunit; /* default to max sectors */
454 1.1 xtraeme
455 1.5 xtraeme return scan_disk(fd, beg, end, flags);
456 1.1 xtraeme }
457