xref: /aosp_15_r20/external/e2fsprogs/misc/findsuper.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * findsuper --- quick hacked up program to find ext2 superblocks.
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * This is a hack, and really shouldn't be installed anywhere.  If you
5*6a54128fSAndroid Build Coastguard Worker  * need a program which does this sort of functionality, please try
6*6a54128fSAndroid Build Coastguard Worker  * using gpart program.
7*6a54128fSAndroid Build Coastguard Worker  *
8*6a54128fSAndroid Build Coastguard Worker  * Portions Copyright 1998-2000, Theodore Ts'o.
9*6a54128fSAndroid Build Coastguard Worker  *
10*6a54128fSAndroid Build Coastguard Worker  * Well, here's my linux version of findsuper.
11*6a54128fSAndroid Build Coastguard Worker  * I'm sure you coulda done it faster.  :)
12*6a54128fSAndroid Build Coastguard Worker  * IMHO there isn't as much interesting data to print in the
13*6a54128fSAndroid Build Coastguard Worker  * linux superblock as there is in the SunOS superblock--disk geometry is
14*6a54128fSAndroid Build Coastguard Worker  * not there...and linux seems to update the dates in all the superblocks.
15*6a54128fSAndroid Build Coastguard Worker  * SunOS doesn't ever touch the backup superblocks after the fs is created,
16*6a54128fSAndroid Build Coastguard Worker  * as far as I can tell, so the date is more interesting IMHO and certainly
17*6a54128fSAndroid Build Coastguard Worker  * marks which superblocks are backup ones.
18*6a54128fSAndroid Build Coastguard Worker  *
19*6a54128fSAndroid Build Coastguard Worker  * I wanted to add msdos support, but I couldn't make heads or tails
20*6a54128fSAndroid Build Coastguard Worker  * of the kernel include files to find anything I could look for in msdos.
21*6a54128fSAndroid Build Coastguard Worker  *
22*6a54128fSAndroid Build Coastguard Worker  * Reading every block of a Sun partition is fairly quick.  Doing the
23*6a54128fSAndroid Build Coastguard Worker  * same under linux (slower hardware I suppose) just isn't the same.
24*6a54128fSAndroid Build Coastguard Worker  * It might be more useful to default to reading the first (second?) block
25*6a54128fSAndroid Build Coastguard Worker  * on each cyl; however, if the disk geometry is wrong, this is useless.
26*6a54128fSAndroid Build Coastguard Worker  * But ya could still get the cyl size to print the numbers as cyls instead
27*6a54128fSAndroid Build Coastguard Worker  * of blocks...
28*6a54128fSAndroid Build Coastguard Worker  *
29*6a54128fSAndroid Build Coastguard Worker  * run this as (for example)
30*6a54128fSAndroid Build Coastguard Worker  *   findsuper /dev/hda
31*6a54128fSAndroid Build Coastguard Worker  *   findsuper /dev/hda 437760 1024   (my disk has cyls of 855*512)
32*6a54128fSAndroid Build Coastguard Worker  *
33*6a54128fSAndroid Build Coastguard Worker  * I suppose the next step is to figure out a way to determine if
34*6a54128fSAndroid Build Coastguard Worker  * the block found is the first superblock somehow, and if so, build
35*6a54128fSAndroid Build Coastguard Worker  * a partition table from the superblocks found... but this is still
36*6a54128fSAndroid Build Coastguard Worker  * useful as is.
37*6a54128fSAndroid Build Coastguard Worker  *
38*6a54128fSAndroid Build Coastguard Worker  *		Steve
39*6a54128fSAndroid Build Coastguard Worker  * [email protected]
40*6a54128fSAndroid Build Coastguard Worker  * [email protected]
41*6a54128fSAndroid Build Coastguard Worker  *
42*6a54128fSAndroid Build Coastguard Worker  * Additional notes by Andreas Dilger <[email protected]>:
43*6a54128fSAndroid Build Coastguard Worker  * - fixed to support > 2G devices by using lseek64
44*6a54128fSAndroid Build Coastguard Worker  * - add reliability checking for the superblock to avoid random garbage
45*6a54128fSAndroid Build Coastguard Worker  * - add adaptive progress meter
46*6a54128fSAndroid Build Coastguard Worker  *
47*6a54128fSAndroid Build Coastguard Worker  * It _should_ also handle signals and tell you the ending block, so
48*6a54128fSAndroid Build Coastguard Worker  * that you can resume at a later time, but it doesn't yet...
49*6a54128fSAndroid Build Coastguard Worker  *
50*6a54128fSAndroid Build Coastguard Worker  * Note that gpart does not appear to find all superblocks that aren't aligned
51*6a54128fSAndroid Build Coastguard Worker  * with the start of a possible partition, so it is not useful in systems
52*6a54128fSAndroid Build Coastguard Worker  * with LVM or similar setups which don't use fat partition alignment.
53*6a54128fSAndroid Build Coastguard Worker  *
54*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
55*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Public
56*6a54128fSAndroid Build Coastguard Worker  * License.
57*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
58*6a54128fSAndroid Build Coastguard Worker  */
59*6a54128fSAndroid Build Coastguard Worker 
60*6a54128fSAndroid Build Coastguard Worker /*
61*6a54128fSAndroid Build Coastguard Worker  * Documentation addendum added by Andreas [email protected]/[email protected]
62*6a54128fSAndroid Build Coastguard Worker  *
63*6a54128fSAndroid Build Coastguard Worker  * The program findsuper is a utility that scans a disk and finds
64*6a54128fSAndroid Build Coastguard Worker  * copies of ext2 superblocks (by checking for the ext2 signature).
65*6a54128fSAndroid Build Coastguard Worker  *
66*6a54128fSAndroid Build Coastguard Worker  * For each superblock found, it prints the offset in bytes, the
67*6a54128fSAndroid Build Coastguard Worker  * offset in 1024-byte blocks, the size of the ext2 partition in fs
68*6a54128fSAndroid Build Coastguard Worker  * blocks, the filesystem blocksize (in bytes), the block group number
69*6a54128fSAndroid Build Coastguard Worker  * (always 0 for older ext2 systems), and a timestamp (s_mtime).
70*6a54128fSAndroid Build Coastguard Worker  *
71*6a54128fSAndroid Build Coastguard Worker  * This program can be used to retrieve partitions that have been
72*6a54128fSAndroid Build Coastguard Worker  * lost.  The superblock for block group 0 is found 1 block (2
73*6a54128fSAndroid Build Coastguard Worker  * sectors) after the partition start.
74*6a54128fSAndroid Build Coastguard Worker  *
75*6a54128fSAndroid Build Coastguard Worker  * For new systems that have a block group number in the superblock it
76*6a54128fSAndroid Build Coastguard Worker  * is immediately clear which superblock is the first of a partition.
77*6a54128fSAndroid Build Coastguard Worker  * For old systems where no group numbers are given, the first
78*6a54128fSAndroid Build Coastguard Worker  * superblock can be recognized by the timestamp: all superblock
79*6a54128fSAndroid Build Coastguard Worker  * copies have the creation time in s_mtime, except the first, which
80*6a54128fSAndroid Build Coastguard Worker  * has the last time e2fsck or tune2fs wrote to the filesystem.
81*6a54128fSAndroid Build Coastguard Worker  *
82*6a54128fSAndroid Build Coastguard Worker  */
83*6a54128fSAndroid Build Coastguard Worker 
84*6a54128fSAndroid Build Coastguard Worker #define _FILE_OFFSET_BITS 64
85*6a54128fSAndroid Build Coastguard Worker 
86*6a54128fSAndroid Build Coastguard Worker #include "config.h"
87*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
88*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
89*6a54128fSAndroid Build Coastguard Worker #include <string.h>
90*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
91*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
92*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
93*6a54128fSAndroid Build Coastguard Worker #include <time.h>
94*6a54128fSAndroid Build Coastguard Worker 
95*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2_fs.h"
96*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2fs.h"
97*6a54128fSAndroid Build Coastguard Worker #include "support/nls-enable.h"
98*6a54128fSAndroid Build Coastguard Worker 
99*6a54128fSAndroid Build Coastguard Worker #undef DEBUG
100*6a54128fSAndroid Build Coastguard Worker 
101*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
102*6a54128fSAndroid Build Coastguard Worker #define WHY(fmt, arg...) { printf("\r%Ld: " fmt, sk, ##arg) ; continue; }
103*6a54128fSAndroid Build Coastguard Worker #else
104*6a54128fSAndroid Build Coastguard Worker #define WHY(fmt, arg...) { continue; }
105*6a54128fSAndroid Build Coastguard Worker #endif
106*6a54128fSAndroid Build Coastguard Worker 
usage(void)107*6a54128fSAndroid Build Coastguard Worker static void usage(void)
108*6a54128fSAndroid Build Coastguard Worker {
109*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr,
110*6a54128fSAndroid Build Coastguard Worker 		_("Usage:  findsuper device [skipbytes [startkb]]\n"));
111*6a54128fSAndroid Build Coastguard Worker 	exit(1);
112*6a54128fSAndroid Build Coastguard Worker }
113*6a54128fSAndroid Build Coastguard Worker 
114*6a54128fSAndroid Build Coastguard Worker 
main(int argc,char * argv[])115*6a54128fSAndroid Build Coastguard Worker int main(int argc, char *argv[])
116*6a54128fSAndroid Build Coastguard Worker {
117*6a54128fSAndroid Build Coastguard Worker 	int skiprate=512;		/* one sector */
118*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t sk=0, skl=0;
119*6a54128fSAndroid Build Coastguard Worker 	int fd;
120*6a54128fSAndroid Build Coastguard Worker 	char *s;
121*6a54128fSAndroid Build Coastguard Worker 	time_t tm, last = time(0);
122*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t interval = 1024 * 1024;
123*6a54128fSAndroid Build Coastguard Worker 	int c, print_jnl_copies = 0;
124*6a54128fSAndroid Build Coastguard Worker 	const char * device_name;
125*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block ext2;
126*6a54128fSAndroid Build Coastguard Worker 	/* interesting fields: EXT2_SUPER_MAGIC
127*6a54128fSAndroid Build Coastguard Worker 	 *      s_blocks_count s_log_block_size s_mtime s_magic s_lastcheck */
128*6a54128fSAndroid Build Coastguard Worker 
129*6a54128fSAndroid Build Coastguard Worker #ifdef ENABLE_NLS
130*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_MESSAGES, "");
131*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_CTYPE, "");
132*6a54128fSAndroid Build Coastguard Worker 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
133*6a54128fSAndroid Build Coastguard Worker 	textdomain(NLS_CAT_NAME);
134*6a54128fSAndroid Build Coastguard Worker 	set_com_err_gettext(gettext);
135*6a54128fSAndroid Build Coastguard Worker #endif
136*6a54128fSAndroid Build Coastguard Worker 
137*6a54128fSAndroid Build Coastguard Worker 	while ((c = getopt (argc, argv, "j")) != EOF) {
138*6a54128fSAndroid Build Coastguard Worker 		switch (c) {
139*6a54128fSAndroid Build Coastguard Worker 		case 'j':
140*6a54128fSAndroid Build Coastguard Worker 			print_jnl_copies++;
141*6a54128fSAndroid Build Coastguard Worker 			break;
142*6a54128fSAndroid Build Coastguard Worker 		default:
143*6a54128fSAndroid Build Coastguard Worker 			usage();
144*6a54128fSAndroid Build Coastguard Worker 		}
145*6a54128fSAndroid Build Coastguard Worker 	}
146*6a54128fSAndroid Build Coastguard Worker 
147*6a54128fSAndroid Build Coastguard Worker 	if (optind == argc)
148*6a54128fSAndroid Build Coastguard Worker 		usage();
149*6a54128fSAndroid Build Coastguard Worker 
150*6a54128fSAndroid Build Coastguard Worker 	device_name = argv[optind++];
151*6a54128fSAndroid Build Coastguard Worker 
152*6a54128fSAndroid Build Coastguard Worker 	if (optind < argc) {
153*6a54128fSAndroid Build Coastguard Worker 		skiprate = strtol(argv[optind], &s, 0);
154*6a54128fSAndroid Build Coastguard Worker 		if (s == argv[optind]) {
155*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr,_("skipbytes should be a number, not %s\n"), s);
156*6a54128fSAndroid Build Coastguard Worker 			exit(1);
157*6a54128fSAndroid Build Coastguard Worker 		}
158*6a54128fSAndroid Build Coastguard Worker 		optind++;
159*6a54128fSAndroid Build Coastguard Worker 	}
160*6a54128fSAndroid Build Coastguard Worker 	if (skiprate & 0x1ff) {
161*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr,
162*6a54128fSAndroid Build Coastguard Worker 			_("skipbytes must be a multiple of the sector size\n"));
163*6a54128fSAndroid Build Coastguard Worker 		exit(2);
164*6a54128fSAndroid Build Coastguard Worker 	}
165*6a54128fSAndroid Build Coastguard Worker 	if (optind < argc) {
166*6a54128fSAndroid Build Coastguard Worker 		sk = skl = strtoll(argv[optind], &s, 0) << 10;
167*6a54128fSAndroid Build Coastguard Worker 		if (s == argv[optind]) {
168*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr,
169*6a54128fSAndroid Build Coastguard Worker 				_("startkb should be a number, not %s\n"), s);
170*6a54128fSAndroid Build Coastguard Worker 			exit(1);
171*6a54128fSAndroid Build Coastguard Worker 		}
172*6a54128fSAndroid Build Coastguard Worker 		optind++;
173*6a54128fSAndroid Build Coastguard Worker 	}
174*6a54128fSAndroid Build Coastguard Worker 	if (sk < 0) {
175*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("startkb should be positive, not %llu\n"),sk);
176*6a54128fSAndroid Build Coastguard Worker 		exit(1);
177*6a54128fSAndroid Build Coastguard Worker 	}
178*6a54128fSAndroid Build Coastguard Worker 
179*6a54128fSAndroid Build Coastguard Worker 	fd = open(device_name, O_RDONLY);
180*6a54128fSAndroid Build Coastguard Worker 	if (fd < 0) {
181*6a54128fSAndroid Build Coastguard Worker 		perror(device_name);
182*6a54128fSAndroid Build Coastguard Worker 		exit(1);
183*6a54128fSAndroid Build Coastguard Worker 	}
184*6a54128fSAndroid Build Coastguard Worker 
185*6a54128fSAndroid Build Coastguard Worker 	/* Now, go looking for the superblock! */
186*6a54128fSAndroid Build Coastguard Worker 	printf(_("starting at %llu, with %u byte increments\n"), sk, skiprate);
187*6a54128fSAndroid Build Coastguard Worker 	if (print_jnl_copies)
188*6a54128fSAndroid Build Coastguard Worker 		printf(_("[*] probably superblock written in the ext3 "
189*6a54128fSAndroid Build Coastguard Worker 			 "journal superblock,\n\tso start/end/grp wrong\n"));
190*6a54128fSAndroid Build Coastguard Worker 	printf(_("byte_offset  byte_start     byte_end  fs_blocks blksz  grp  mkfs/mount_time           sb_uuid label\n"));
191*6a54128fSAndroid Build Coastguard Worker 	for (; lseek64(fd, sk, SEEK_SET) != -1 &&
192*6a54128fSAndroid Build Coastguard Worker 	       read(fd, &ext2, 512) == 512; sk += skiprate) {
193*6a54128fSAndroid Build Coastguard Worker 		static unsigned char last_uuid[16] = "blah";
194*6a54128fSAndroid Build Coastguard Worker 		unsigned long long bsize, grpsize;
195*6a54128fSAndroid Build Coastguard Worker 		int jnl_copy, sb_offset;
196*6a54128fSAndroid Build Coastguard Worker 
197*6a54128fSAndroid Build Coastguard Worker 		if (sk && !(sk & (interval - 1))) {
198*6a54128fSAndroid Build Coastguard Worker 			time_t now, diff;
199*6a54128fSAndroid Build Coastguard Worker 
200*6a54128fSAndroid Build Coastguard Worker 			now = time(0);
201*6a54128fSAndroid Build Coastguard Worker 			diff = now - last;
202*6a54128fSAndroid Build Coastguard Worker 
203*6a54128fSAndroid Build Coastguard Worker 			if (diff > 0) {
204*6a54128fSAndroid Build Coastguard Worker 				s = ctime(&now);
205*6a54128fSAndroid Build Coastguard Worker 				s[24] = 0;
206*6a54128fSAndroid Build Coastguard Worker 				printf("\r%11Lu: %8LukB/s @ %s", sk,
207*6a54128fSAndroid Build Coastguard Worker 				       (((sk - skl)) / diff) >> 10, s);
208*6a54128fSAndroid Build Coastguard Worker 				fflush(stdout);
209*6a54128fSAndroid Build Coastguard Worker 			}
210*6a54128fSAndroid Build Coastguard Worker 			if (diff < 5)
211*6a54128fSAndroid Build Coastguard Worker 				interval <<= 1;
212*6a54128fSAndroid Build Coastguard Worker 			else if (diff > 20)
213*6a54128fSAndroid Build Coastguard Worker 				interval >>= 1;
214*6a54128fSAndroid Build Coastguard Worker 			last = now;
215*6a54128fSAndroid Build Coastguard Worker 			skl = sk;
216*6a54128fSAndroid Build Coastguard Worker 		}
217*6a54128fSAndroid Build Coastguard Worker 		if (ext2.s_magic != EXT2_SUPER_MAGIC)
218*6a54128fSAndroid Build Coastguard Worker 			continue;
219*6a54128fSAndroid Build Coastguard Worker 		if (ext2.s_log_block_size > 6)
220*6a54128fSAndroid Build Coastguard Worker 			WHY("log block size > 6 (%u)\n", ext2.s_log_block_size);
221*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_r_blocks_count(&ext2) > ext2fs_blocks_count(&ext2))
222*6a54128fSAndroid Build Coastguard Worker 			WHY("r_blocks_count > blocks_count (%u > %u)\n",
223*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_r_blocks_count(&ext2),
224*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_blocks_count(&ext2));
225*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_free_blocks_count(&ext2) > ext2fs_blocks_count(&ext2))
226*6a54128fSAndroid Build Coastguard Worker 			WHY("free_blocks_count > blocks_count\n (%u > %u)\n",
227*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_free_blocks_count(&ext2),
228*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_blocks_count(&ext2));
229*6a54128fSAndroid Build Coastguard Worker 		if (ext2.s_free_inodes_count > ext2.s_inodes_count)
230*6a54128fSAndroid Build Coastguard Worker 			WHY("free_inodes_count > inodes_count (%u > %u)\n",
231*6a54128fSAndroid Build Coastguard Worker 			    ext2.s_free_inodes_count, ext2.s_inodes_count);
232*6a54128fSAndroid Build Coastguard Worker 
233*6a54128fSAndroid Build Coastguard Worker 		if (ext2.s_mkfs_time != 0)
234*6a54128fSAndroid Build Coastguard Worker 			tm = ext2.s_mkfs_time;
235*6a54128fSAndroid Build Coastguard Worker 		else
236*6a54128fSAndroid Build Coastguard Worker 			tm = ext2.s_mtime;
237*6a54128fSAndroid Build Coastguard Worker 		s = ctime(&tm);
238*6a54128fSAndroid Build Coastguard Worker 		s[24] = 0;
239*6a54128fSAndroid Build Coastguard Worker 		bsize = 1 << (ext2.s_log_block_size + 10);
240*6a54128fSAndroid Build Coastguard Worker 		grpsize = bsize * ext2.s_blocks_per_group;
241*6a54128fSAndroid Build Coastguard Worker 		if (memcmp(ext2.s_uuid, last_uuid, sizeof(last_uuid)) == 0 &&
242*6a54128fSAndroid Build Coastguard Worker 		    ext2.s_rev_level > 0 && ext2.s_block_group_nr == 0) {
243*6a54128fSAndroid Build Coastguard Worker 			jnl_copy = 1;
244*6a54128fSAndroid Build Coastguard Worker 		} else {
245*6a54128fSAndroid Build Coastguard Worker 			jnl_copy = 0;
246*6a54128fSAndroid Build Coastguard Worker 			memcpy(last_uuid, ext2.s_uuid, sizeof(last_uuid));
247*6a54128fSAndroid Build Coastguard Worker 		}
248*6a54128fSAndroid Build Coastguard Worker 		if (ext2.s_block_group_nr == 0 || bsize == 1024)
249*6a54128fSAndroid Build Coastguard Worker 			sb_offset = 1024;
250*6a54128fSAndroid Build Coastguard Worker 		else
251*6a54128fSAndroid Build Coastguard Worker 			sb_offset = 0;
252*6a54128fSAndroid Build Coastguard Worker 		if (jnl_copy && !print_jnl_copies)
253*6a54128fSAndroid Build Coastguard Worker 			continue;
254*6a54128fSAndroid Build Coastguard Worker 		printf("\r%11Lu %11Lu%s %11Lu%s %9u %5Lu %4u%s %s %02x%02x%02x%02x %.*s\n",
255*6a54128fSAndroid Build Coastguard Worker 		       sk, sk - ext2.s_block_group_nr * grpsize - sb_offset,
256*6a54128fSAndroid Build Coastguard Worker 		       jnl_copy ? "*":" ",
257*6a54128fSAndroid Build Coastguard Worker 		       sk + ext2fs_blocks_count(&ext2) * bsize -
258*6a54128fSAndroid Build Coastguard Worker 		            ext2.s_block_group_nr * grpsize - sb_offset,
259*6a54128fSAndroid Build Coastguard Worker 		       jnl_copy ? "*" : " ", ext2fs_blocks_count(&ext2), bsize,
260*6a54128fSAndroid Build Coastguard Worker 		       ext2.s_block_group_nr, jnl_copy ? "*" : " ", s,
261*6a54128fSAndroid Build Coastguard Worker 		       ext2.s_uuid[0], ext2.s_uuid[1],
262*6a54128fSAndroid Build Coastguard Worker 		       ext2.s_uuid[2], ext2.s_uuid[3],
263*6a54128fSAndroid Build Coastguard Worker 		       EXT2_LEN_STR(ext2.s_volume_name));
264*6a54128fSAndroid Build Coastguard Worker 	}
265*6a54128fSAndroid Build Coastguard Worker 	printf(_("\n%11Lu: finished with errno %d\n"), sk, errno);
266*6a54128fSAndroid Build Coastguard Worker 	close(fd);
267*6a54128fSAndroid Build Coastguard Worker 
268*6a54128fSAndroid Build Coastguard Worker 	return errno;
269*6a54128fSAndroid Build Coastguard Worker }
270