summaryrefslogtreecommitdiffstats
path: root/src/dump.c
blob: 9fc0a863a8fdfff0fa95c3746853d9438fb83419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
 *  0xFFFF - Open Free Fiasco Firmware Flasher
 *  Copyright (C) 2007-2009  pancake <pancake@nopcode.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "main.h"
#include "hexdump.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Extracts a piece from an mtd device
 * -----------------------------------
 * This function is known to be buggy, It does not takes care about
 * badblocks and oob data. It is replaced by nanddump(), but will
 * probably be fixed in the future or it will die.
 */
int rf_extract(char *dev, off_t from, off_t to, char *file)
{
	off_t i, blk = 0xfffff;
	char buf[0xfffff];
	FILE *fs = fopen(dev, "rb");
	FILE *fd = fopen(file, "wb+");

	if (from>to) {
		printf("Bad range %lld - %lld for %s\n", (long long int)from, (long long int)to, file);
		goto __rf_extract_exit;
	}
	if (fs == NULL) { perror(dev);  goto __rf_extract_exit; }
	if (fd == NULL) { perror(file); goto __rf_extract_exit; }

	printf("Extracting %s...\n", file);
	fseek(fs, from, SEEK_SET);

	for(i=from;i<to;i+=blk) {
		int ret;
		if (i+blk>to) blk = to-i;
		ret = fread(buf, blk, 1, fs);
		progressbar(i, to);
		if (!ret || feof(fs)) break;
		fwrite(buf, blk, 1, fd);
	}

	printf("\r%s: %lld bytes dumped from %s\n",
		file, (long long int)(to-from), dev);

__rf_extract_exit:
	if (fs) fclose(fs);
	if (fd) fclose(fd);

	return 1;
}

/*
 * This function was covardly copied from nanddump.c @ mtd-utils-20060907
 */
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <asm/types.h>
#include <mtd/mtd-user.h>
#ifndef loff_t
#define loff_t off_t
#endif

#define M_RDONLY   0x00000001
#define M_RDRW     0x00000002
#define M_OMITOOB  0x00000010
#define M_OMITBAD  0x00000011
#define M_OMITECC  0x00000012

int mtd_open(char *file, mtd_info_t *meminfo, int *oobinfochanged, 
	struct nand_oobinfo *old_oobinfo, int *eccstats, int flags)
{
	int fd;
	*oobinfochanged = 0 ;
	
	fd = open(file, (flags&M_RDONLY)?O_RDONLY:O_RDWR);
	if (fd == -1) {
		perror("mtd_open");
		return -1;
	}
	/* Fill in MTD device capability structure */
	if (ioctl(fd, MEMGETINFO, meminfo) != 0) {
		perror("MEMGETINFO");
		close(fd);
		return 1;
	}

	/* Make sure device page sizes are valid */
	if (!(meminfo->oobsize == 64 && meminfo->writesize == 2048) &&
	    !(meminfo->oobsize == 16 && meminfo->writesize == 512) &&
	    !(meminfo->oobsize == 8  && meminfo->writesize == 256)) {
		fprintf(stderr, "Unknown flash (not normal NAND)\n");
		close(fd);
		return -1;
	}

	return fd;
}

int mtd_close(int fd, struct nand_oobinfo *old_oobinfo, int oobinfochanged)
{
	if (fd == -1)
		return 1;

	/* reset oobinfo */
	if (oobinfochanged == 1) {
		if (ioctl (fd, MEMSETOOBSEL, &old_oobinfo) != 0) {
			perror ("MEMSETOOBSEL");
			close(fd);
			return 1;
		}
	}
	/* Close the output file and MTD device */
	return close(fd);
}

// configuration for nanddump //
//int	noecc = 0;		// don't error correct
static int omitoob = 1;		// omit oob data
static int omitbad = 1;
// configuration for nanddump //

#define CONFIGURE_FLAGS(x) \
omitoob = x & M_OMITOOB; \
omitbad = x & M_OMITBAD;

int check_badblocks(char *mtddev)
{
	int fd;
	int oobinfochanged = 0 ;
	int badblock = 0;
	int badblocks = 1;
	int eccstats = 0;
	unsigned long int i;
	unsigned long long blockstart = 1;
	unsigned char oobbuf[64];
	struct nand_oobinfo old_oobinfo;
	struct mtd_oob_buf oob = {0, 16, oobbuf};
	/* XXX stat1 is never initialized !!! */
	struct mtd_ecc_stats stat1, stat2;
	mtd_info_t meminfo;

	fd = mtd_open(mtddev, &meminfo, &oobinfochanged, &old_oobinfo, &eccstats, M_RDONLY);

	if (fd == -1) {
		printf("cannot open mtd\n");
		return 1;
	}

        fprintf(stderr, "Block size %u, page size %u, OOB size %u\n",
                meminfo.erasesize, meminfo.writesize, meminfo.oobsize);
        fprintf(stderr, "Size %u, flags %u, type 0x%x\n",
                meminfo.size, meminfo.flags, (int)meminfo.type);

	oob.length = meminfo.oobsize;
	for(i = 0; i < meminfo.size; i+= meminfo.writesize) {

		// new eraseblock , check for bad block
		if (blockstart != (i & (~meminfo.erasesize + 1))) {
			blockstart = i & (~meminfo.erasesize + 1);
			if ((badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart)) < 0) {
				perror("ioctl(MEMGETBADBLOCK)");
				goto closeall;
			}
		}

		if (badblock) {
			if (omitbad) {
				printf("Bad block found at 0x%lx\n", i);
				continue;
			}
		} else {
			char readbuf[2048]; // XXX hardcoded like mtd-utils?? ugly!
			// dummy -- should be removed
			if (pread(fd, readbuf, meminfo.writesize, i) != meminfo.writesize) {
				perror("pread");
				goto closeall;
			}
		}

		/* ECC stats available ? */
		memset (&stat1, 0, sizeof (stat1));
		if (eccstats) {
			if (ioctl (fd, ECCGETSTATS, &stat2)) {
				perror("ioctl(ECCGETSTATS)");
				goto closeall;
			}
			if (stat1.failed != stat2.failed)
				fprintf(stderr, "ECC: %d uncorrectable bitflip(s)"
					" at offset 0x%08lx\n",
					stat2.failed - stat1.failed, i);
			if (stat1.corrected != stat2.corrected)
				fprintf(stderr, "ECC: %d corrected bitflip(s) at"
					" offset 0x%08lx\n",
					stat2.corrected - stat1.corrected, i);
			stat1 = stat2;
		}

		if (badblock) {
			printf("Oops badblock %d at 0x%lx !\n", badblocks++, i);
		} else {
			/* Read OOB data and exit on failure */
			oob.start = i;
			if (ioctl(fd, MEMREADOOB, &oob) != 0) {
				perror("ioctl(MEMREADOOB)");
				goto closeall;
			}
		}

		/* Write out OOB data */
		if (badblock)
		D dump_bytes(oobbuf, meminfo.oobsize);
	}

	mtd_close(fd, &old_oobinfo, oobinfochanged);
	return 0;

closeall:
	mtd_close(fd, &old_oobinfo, oobinfochanged);
	return 1;
}

// XXX warning ioob is not used
// isbl is used to skip bad blocks ?
int nanddump(char *mtddev, unsigned long start_addr, unsigned long length, char *dumpfile, int isbl, int ioob)
{
	unsigned char readbuf[2048];
	int oobinfochanged = 0 ;
	unsigned char oobbuf[64];
	unsigned long ofs, end_addr = 0;
	unsigned long long blockstart = 1;
	struct nand_oobinfo none_oobinfo = {
		.useecc = MTD_NANDECC_OFF,
	};
	int fd, ofd, bs, badblock = 0;
	struct mtd_oob_buf oob = {0, 16, oobbuf};
	mtd_info_t meminfo;
	int badblocks = 1;
	struct nand_oobinfo old_oobinfo;
	int eccstats = 0;
	struct mtd_ecc_stats stat1, stat2;
	int flags = M_RDONLY;

	printf("\nExtracting %s from %s...\n", dumpfile, mtddev);

	fd = mtd_open(mtddev, &meminfo, &oobinfochanged, &old_oobinfo, &eccstats, flags);

	if (meminfo.writesize == 0) {
		printf("What the fuck.. this is not an MTD device!\n");
		close(fd);
		return 1;
	}

	/* Read the real oob length */
	oob.length = meminfo.oobsize;
        fprintf(stderr, "Block size %u, page size %u, OOB size %u\n",
                meminfo.erasesize, meminfo.writesize, meminfo.oobsize);
        fprintf(stderr, "Size %u, flags %u, type 0x%x\n",
                meminfo.size, meminfo.flags, (int)meminfo.type);

	if (flags & M_OMITECC)  { // (noecc)
		switch (ioctl(fd, MTDFILEMODE, (void *) MTD_MODE_RAW)) {
		case -ENOTTY:
			if (ioctl (fd, MEMGETOOBSEL, old_oobinfo) != 0) {
				perror ("MEMGETOOBSEL");
				close (fd);
				exit (1);
			}
			if (ioctl (fd, MEMSETOOBSEL, none_oobinfo) != 0) {
				perror ("MEMSETOOBSEL");
				close (fd);
				exit (1);
			}
			oobinfochanged = 1;
			break;

		case 0:
			oobinfochanged = 2;
			break;
		default:
			perror ("MTDFILEMODE");
			close (fd);
			return 1;
		}
	} else {
		/* check if we can read ecc stats */
		if (!ioctl(fd, ECCGETSTATS, &stat1)) {
			eccstats = 1;
			fprintf(stderr, "ECC failed: %d\n", stat1.failed);
			fprintf(stderr, "ECC corrected: %d\n", stat1.corrected);    
			fprintf(stderr, "Number of bad blocks: %d\n", stat1.badblocks);    
			fprintf(stderr, "Number of bbt blocks: %d\n", stat1.bbtblocks);    
		} else
			perror("No ECC status information available");
	}

	/* Open output file for writing. If file name is "-", write to standard
	 * output. */
	if (!dumpfile) {
		ofd = STDOUT_FILENO;
	} else if ((ofd = open(dumpfile, O_WRONLY | O_TRUNC | O_CREAT, 0644))== -1) {
		perror ("open outfile");
		close(fd);
		return 1;
	}

	/* Initialize start/end addresses and block size */
	if (length)
		end_addr = start_addr + length;
	if (!length || end_addr > meminfo.size)
		end_addr = meminfo.size;

	bs = meminfo.writesize;

	fprintf(stderr,
		"Dumping data starting at 0x%08x and ending at 0x%08x...\n",
		(unsigned int) start_addr, (unsigned int) end_addr);

	/* Dump the flash contents */
	for (ofs = start_addr; ofs < end_addr ; ofs+=bs) {
		// new eraseblock , check for bad block
		if (blockstart != (ofs & (~meminfo.erasesize + 1))) {
			blockstart = ofs & (~meminfo.erasesize + 1);
			if ((badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart)) < 0) {
				perror("ioctl(MEMGETBADBLOCK)");
				goto closeall;
			}
		}

		if (!isbl && badblock) {
			if (omitbad)
				continue;
			memset (readbuf, 0xff, bs);
		} else {
			/* Read page data and exit on failure */
			if (pread(fd, readbuf, bs, ofs) != bs) {
				perror("pread");
				goto closeall;
			}
		}

		// TODO exist on n800??? // remove code?
		/* ECC stats available ? */
		if (eccstats) {
			if (ioctl(fd, ECCGETSTATS, &stat2)) {
				perror("ioctl(ECCGETSTATS)");
				goto closeall;
			}
			if (stat1.failed != stat2.failed)
				fprintf(stderr, "ECC: %d uncorrectable bitflip(s)"
					" at offset 0x%08lx\n",
					stat2.failed - stat1.failed, ofs);
			if (stat1.corrected != stat2.corrected)
				fprintf(stderr, "ECC: %d corrected bitflip(s) at"
					" offset 0x%08lx\n",
					stat2.corrected - stat1.corrected, ofs);
			stat1 = stat2;
		}

		/* Write out page data */
		//if (pretty_print) dump_bytes(readbuf, bs);
		write(ofd, readbuf, bs);
		progressbar(ofs, end_addr);

		// OOB STUFF //
		if (omitoob)
			continue;

		if (!isbl&&badblock) {
			printf("Oops badblock %d *ignored* at 0x%lx !\n", badblocks++, ofs);
			memset (readbuf, 0xff, meminfo.oobsize);
		} else {
			/* Read OOB data and exit on failure */
			oob.start = ofs;
			if (ioctl(fd, MEMREADOOB, &oob) != 0) {
				perror("ioctl(MEMREADOOB)");
				goto closeall;
			}
		}

		/* Write out OOB data */
		D dump_bytes(oobbuf, meminfo.oobsize);
		write(ofd, oobbuf, meminfo.oobsize);
	}

	mtd_close(fd, &old_oobinfo, oobinfochanged);
	close(ofd);

	return 0;

 closeall:
	mtd_close(fd, &old_oobinfo, oobinfochanged);
	close(ofd);
	return 1;
}

int rf_strip(char *file)
{
	FILE *fd = fopen(file, "rw");
	unsigned char buf[4096];
	int ret;
	off_t eof = 0;

	if (!fd) {
		fprintf(stderr, "Weird! can't open %s???\n", file);
		exit(0);
	}
	printf("Stripping padding from %s... ", file);
	fflush(stdout);
	fseek(fd, 0, SEEK_END);
	if (ftell(fd)>4096) {
		do {
			fseek(fd, -4096, SEEK_CUR);
			ret = fread(buf, 1, 4096, fd);
			fseek(fd, -4096, SEEK_CUR);
			for(ret--;ret>0;ret--)
				if (buf[ret]!=0xff) {
					fseek(fd, ret+1, SEEK_CUR);
					eof = ftell(fd);
					goto __done;
				}
		} while(1);
	}

__done:
	fclose(fd);
	if (eof) truncate(file, eof);
	printf("done at %d\n", (int)eof);

	return 1;
}

static int is_n900()
{
	int n900 = 0;
	unsigned char buf[4];
	FILE *fd = fopen("/dev/mtd2", "rb");
	if (!fd) {
		fprintf(stderr, "Cannot open /dev/mtd2.\n");
		exit(1);
	}
	fread(buf, 4, 1, fd);
	if (!memcmp("\xff\xff\xff\xff", buf, 4))
		n900 = 1;
	fclose(fd);
	return n900;
}

static int is_n800()
{
	int n800 = 0;
	unsigned char buf[4];
	FILE *fd = fopen("/dev/mtd0", "rb");
	if (!fd) {
		fprintf(stderr, "Cannot open /dev/mtd0.\n");
		exit(1);
	}
	fread(buf, 4, 1, fd);
	if (!memcmp("\xff\xff\xff\xff", buf, 4))
		n800 = 1;
	fclose(fd);
	return n800;
}

int dump_config()
{
	mtd_info_t meminfo;
	int fd = open("/dev/mtd1", O_RDONLY);
	char buf[1024];
	int i=0,j;
	int ret;


	if (fd == -1) {
		perror("open /dev/mtd1");
		return 1;
	}
	if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
		perror("MEMGETINFO");
		close(fd);
		return 1;
	}

	while(i<0x60000) {
	iniloop:
		ret = read(fd, buf, 4);
		if (ret == -1)
			break;
		if (!memcmp(buf,"ConF", 4)) {
		loop:
			read(fd, buf, 4);
			if (ret == -1) break;
			printf("\n0x%08x : ConF %02x %02x %02x %02x : ", i,
				buf[0], buf[1], buf[2], buf[3]);
			fflush(stdout);
			while(1) {
				ret = read(fd, buf, 4);
				if (ret <1) {
					printf("Unexpected eof\n");
					break;
				}
				if (buf[0]=='\0'||buf[1]=='\0'||buf[2]=='\0'||buf[3]=='\0') {
					printf("%s  \t  ", buf);
					fflush(stdout);
					while(1) {
						ret = read(fd, buf, 4);
						if (ret <1) {
							printf("Unexpected eof\n");
							break;
						}
						if (!memcmp(buf,"\xff\xff\xff\xff", 4))
							goto iniloop;
						if (!memcmp(buf,"ConF", 4))
							goto loop;
						printf ("%02x %02x %02x %02x ",
							buf[0], buf[1], buf[2], buf[3]);
					}
					break;
				} else {
					write(1, buf,4);
				}
			}
		}
		j = lseek(fd, 0, SEEK_CUR);
		if (j==i) break; i = j;
	}
	close(fd);
	printf("\n");
	return 0;
}

int reverse_extract_pieces_n900(char *dir)
{
	char reply;
	chdir(dir);
	printf("Device detected: n900\n");
	// Some of the values here are probably not correct
	// I need a firmware to identify if the sizes of the pieces are ok

	nanddump("/dev/mtd0", 0, 0, "xloader.bin", 1, 0); // fix size?
	nanddump("/dev/mtd1", 0, 0, "config.bin", 1, 0);
	nanddump("/dev/mtd3", 0, 0, "nolo.bin", 0, 0);

	printf("\n\nExtract rootfs? (y/N): "); fflush(stdout);
	read(0, &reply, 1);
	if (reply=='y'||reply=='Y') {
		//rf_extract("/dev/mtd4", 0x000000, 0x6000000, "rootfs.jffs2");
		nanddump("/dev/mtd5", 0x000000, 0, "rootfs.jffs2", 0,0);
	} else	printf("*** Ignoring rootfs\n");

	printf("\n\nStrip dumped files? (y/N): "); fflush(stdout);
	read(0, &reply, 1);
	if (reply=='y'||reply=='Y') {
		rf_strip("xloader.bin");
		rf_strip("config.bin");
		rf_strip("nolo.bin");
	} else  printf("*** Ignoring strip\n");

	printf("\nIdentifying extracted files...\n");
	printf("%s: xloader\n", fpid_file("xloader.bin"));
	printf("%s: config.bin\n", fpid_file("config.bin"));
	printf("%s: nolo.bin\n", fpid_file("nolo.bin"));
	printf("%s: rootfs.jffs2\n", fpid_file("rootfs.jffs2"));

	return 1;
}

int reverse_extract_pieces(char *dir)
{
	char reply;

	if (is_n900())
		return reverse_extract_pieces_n900(dir);

	printf("Device detected: 770/n800/n900\n");
	chdir(dir);

	// TODO: get values from /proc/mtd ???

	//rf_extract("/dev/mtd0", is_n800()?0x200:0, 0x003600, "xloader.bin");
	nanddump("/dev/mtd0", is_n800()?0x200:0, 0x4000, "xloader.bin", 1,0); // 0x3600 size?
	//rf_extract("/dev/mtd0", 0x004000, 0x01ffff,  "secondary.bin");
	nanddump("/dev/mtd0", 0x004000, 0,  "secondary.bin", 1,0);
	nanddump("/dev/mtd1", 0, 0,  "config.bin", 0,0);
	//rf_extract("/dev/mtd2", 0x000800, 0x200000,  "zImage");
	nanddump("/dev/mtd2", 0x000800, 0,  "zImage", 0,0);
	//rf_extract("/dev/mtd3", 0x000000, 0x1D00000, "initfs.jffs2");
	nanddump("/dev/mtd3", 0x000000, 0, "initfs.jffs2", 0,0);

	printf("\n\nExtract rootfs? (y/N): "); fflush(stdout);
	read(0, &reply, 1);
	if (reply=='y'||reply=='Y') {
		//rf_extract("/dev/mtd4", 0x000000, 0x6000000, "rootfs.jffs2");
		nanddump("/dev/mtd4", 0x000000, 0, "rootfs.jffs2", 0,0);
	} else	printf("*** Ignoring rootfs\n");

	printf("\n\nStrip dumped files? (y/N): "); fflush(stdout);
	read(0, &reply, 1);
	if (reply=='y'||reply=='Y') {
		rf_strip("xloader.bin");
		rf_strip("secondary.bin");
		rf_strip("zImage");
		rf_strip("initfs.jffs2");
	} else  printf("*** Ignoring strip\n");

	printf("\nIdentifying extracted files...\n");
	printf("%s: xloader\n", fpid_file("xloader.bin"));
	printf("%s: secondary.bin\n", fpid_file("secondary.bin"));
	printf("%s: config.bin\n", fpid_file("config.bin"));
	printf("%s: zImage\n", fpid_file("zImage"));
	printf("%s: initfs.jffs2\n", fpid_file("initfs.jffs2"));
	printf("%s: rootfs.jffs2\n", fpid_file("rootfs.jffs2"));

	return 1;
}