aboutsummaryrefslogtreecommitdiff
path: root/jffsX-utils/jffs-dump.c
blob: 3176469bbdca4b23160f730e532ae9d65395bc8b (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
/*
 * Dump JFFS filesystem.
 * Useful when it buggers up.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <linux/types.h>
#include <asm/byteorder.h>

#include "common.h"

#define BLOCK_SIZE 1024
#define JFFS_MAGIC 0x34383931 /* "1984" */
#define JFFS_MAX_NAME_LEN 256
#define JFFS_MIN_INO 1
#define JFFS_TRACE_INDENT 4
#define JFFS_ALIGN_SIZE 4
#define MAX_CHUNK_SIZE 32768

/* How many padding bytes should be inserted between two chunks of data
   on the flash?  */
#define JFFS_GET_PAD_BYTES(size) ((JFFS_ALIGN_SIZE                     \
			- ((uint32_t)(size) % JFFS_ALIGN_SIZE)) \
		% JFFS_ALIGN_SIZE)

#define JFFS_EMPTY_BITMASK 0xffffffff
#define JFFS_MAGIC_BITMASK 0x34383931
#define JFFS_DIRTY_BITMASK 0x00000000

struct jffs_raw_inode
{
	uint32_t magic;    /* A constant magic number.  */
	uint32_t ino;      /* Inode number.  */
	uint32_t pino;     /* Parent's inode number.  */
	uint32_t version;  /* Version number.  */
	uint32_t mode;     /* file_type, mode  */
	uint16_t uid;
	uint16_t gid;
	uint32_t atime;
	uint32_t mtime;
	uint32_t ctime;
	uint32_t offset;     /* Where to begin to write.  */
	uint32_t dsize;      /* Size of the file data.  */
	uint32_t rsize;      /* How much are going to be replaced?  */
	uint8_t nsize;       /* Name length.  */
	uint8_t nlink;       /* Number of links.  */
	uint8_t spare : 6;   /* For future use.  */
	uint8_t rename : 1;  /* Is this a special rename?  */
	uint8_t deleted : 1; /* Has this file been deleted?  */
	uint8_t accurate;    /* The inode is obsolete if accurate == 0.  */
	uint32_t dchksum;    /* Checksum for the data.  */
	uint16_t nchksum;    /* Checksum for the name.  */
	uint16_t chksum;     /* Checksum for the raw_inode.  */
};


struct jffs_file
{
	struct jffs_raw_inode inode;
	char *name;
	unsigned char *data;
};


char *root_directory_name = NULL;
int fs_pos = 0;
int verbose = 0;

#define ENDIAN_HOST   0
#define ENDIAN_BIG    1
#define ENDIAN_LITTLE 2
int endian = ENDIAN_HOST;

static uint32_t jffs_checksum(void *data, int size);
void jffs_print_trace(const char *path, int depth);
int make_root_dir(FILE *fs, int first_ino, const char *root_dir_path,
		int depth);
void write_file(struct jffs_file *f, FILE *fs, struct stat st);
void read_data(struct jffs_file *f, const char *path, int offset);
int mkfs(FILE *fs, const char *path, int ino, int parent, int depth);


	static uint32_t
jffs_checksum(void *data, int size)
{
	uint32_t sum = 0;
	uint8_t *ptr = (uint8_t *)data;

	while (size-- > 0)
	{
		sum += *ptr++;
	}

	return sum;
}


	void
jffs_print_trace(const char *path, int depth)
{
	int path_len = strlen(path);
	int out_pos = depth * JFFS_TRACE_INDENT;
	int pos = path_len - 1;
	char *out = (char *)alloca(depth * JFFS_TRACE_INDENT + path_len + 1);

	if (verbose >= 2)
	{
		fprintf(stderr, "jffs_print_trace(): path: \"%s\"\n", path);
	}

	if (!out) {
		fprintf(stderr, "jffs_print_trace(): Allocation failed.\n");
		fprintf(stderr, " path: \"%s\"\n", path);
		fprintf(stderr, "depth: %d\n", depth);
		exit(1);
	}

	memset(out, ' ', depth * JFFS_TRACE_INDENT);

	if (path[pos] == '/')
	{
		pos--;
	}
	while (path[pos] && (path[pos] != '/'))
	{
		pos--;
	}
	for (pos++; path[pos] && (path[pos] != '/'); pos++)
	{
		out[out_pos++] = path[pos];
	}
	out[out_pos] = '\0';
	fprintf(stderr, "%s\n", out);
}


/* Print the contents of a raw inode.  */
	void
jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
{
	fprintf(stdout, "jffs_raw_inode: inode number: %u, version %u\n", raw_inode->ino, raw_inode->version);
	fprintf(stdout, "{\n");
	fprintf(stdout, "        0x%08x, /* magic  */\n", raw_inode->magic);
	fprintf(stdout, "        0x%08x, /* ino  */\n", raw_inode->ino);
	fprintf(stdout, "        0x%08x, /* pino  */\n", raw_inode->pino);
	fprintf(stdout, "        0x%08x, /* version  */\n", raw_inode->version);
	fprintf(stdout, "        0x%08x, /* mode  */\n", raw_inode->mode);
	fprintf(stdout, "        0x%04x,     /* uid  */\n", raw_inode->uid);
	fprintf(stdout, "        0x%04x,     /* gid  */\n", raw_inode->gid);
	fprintf(stdout, "        0x%08x, /* atime  */\n", raw_inode->atime);
	fprintf(stdout, "        0x%08x, /* mtime  */\n", raw_inode->mtime);
	fprintf(stdout, "        0x%08x, /* ctime  */\n", raw_inode->ctime);
	fprintf(stdout, "        0x%08x, /* offset  */\n", raw_inode->offset);
	fprintf(stdout, "        0x%08x, /* dsize  */\n", raw_inode->dsize);
	fprintf(stdout, "        0x%08x, /* rsize  */\n", raw_inode->rsize);
	fprintf(stdout, "        0x%02x,       /* nsize  */\n", raw_inode->nsize);
	fprintf(stdout, "        0x%02x,       /* nlink  */\n", raw_inode->nlink);
	fprintf(stdout, "        0x%02x,       /* spare  */\n",
			raw_inode->spare);
	fprintf(stdout, "        %u,          /* rename  */\n",
			raw_inode->rename);
	fprintf(stdout, "        %u,          /* deleted  */\n",
			raw_inode->deleted);
	fprintf(stdout, "        0x%02x,       /* accurate  */\n",
			raw_inode->accurate);
	fprintf(stdout, "        0x%08x, /* dchksum  */\n", raw_inode->dchksum);
	fprintf(stdout, "        0x%04x,     /* nchksum  */\n", raw_inode->nchksum);
	fprintf(stdout, "        0x%04x,     /* chksum  */\n", raw_inode->chksum);
	fprintf(stdout, "}\n");
}

static void write_val32(uint32_t *adr, uint32_t val)
{
	switch(endian) {
		case ENDIAN_HOST:
			*adr = val;
			break;
		case ENDIAN_LITTLE:
			*adr = __cpu_to_le32(val);
			break;
		case ENDIAN_BIG:
			*adr = __cpu_to_be32(val);
			break;
	}
}

static void write_val16(uint16_t *adr, uint16_t val)
{
	switch(endian) {
		case ENDIAN_HOST:
			*adr = val;
			break;
		case ENDIAN_LITTLE:
			*adr = __cpu_to_le16(val);
			break;
		case ENDIAN_BIG:
			*adr = __cpu_to_be16(val);
			break;
	}
}

static uint32_t read_val32(uint32_t *adr)
{
	uint32_t val;

	switch(endian) {
		case ENDIAN_HOST:
			val = *adr;
			break;
		case ENDIAN_LITTLE:
			val = __le32_to_cpu(*adr);
			break;
		case ENDIAN_BIG:
			val = __be32_to_cpu(*adr);
			break;
	}
	return val;
}

static uint16_t read_val16(uint16_t *adr)
{
	uint16_t val;

	switch(endian) {
		case ENDIAN_HOST:
			val = *adr;
			break;
		case ENDIAN_LITTLE:
			val = __le16_to_cpu(*adr);
			break;
		case ENDIAN_BIG:
			val = __be16_to_cpu(*adr);
			break;
	}
	return val;
}

	int
main(int argc, char **argv)
{
	int fs;
	struct stat sb;
	uint32_t wordbuf;
	off_t pos = 0;
	off_t end;
	struct jffs_raw_inode ino;
	unsigned char namebuf[4096];
	int myino = -1;

	if (argc < 2) {
		printf("no filesystem given\n");
		exit(1);
	}

	fs = open(argv[1], O_RDONLY);
	if (fs < 0) {
		perror("open");
		exit(1);
	}

	if (argc > 2) {
		myino = atol(argv[2]);
		printf("Printing ino #%d\n" , myino);
	}

	if (fstat(fs, &sb) < 0) {
		perror("stat");
		close(fs);
		exit(1);
	}
	end = sb.st_size;

	while (pos < end) {
		if (pread(fs, &wordbuf, 4, pos) < 0) {
			perror("pread");
			exit(1);
		}

		switch(wordbuf) {
			case JFFS_EMPTY_BITMASK:
				//			printf("0xff started at 0x%lx\n", pos);
				for (; pos < end && wordbuf == JFFS_EMPTY_BITMASK; pos += 4) {
					if (pread(fs, &wordbuf, 4, pos) < 0) {
						perror("pread");
						exit(1);
					}
				}
				if (pos < end)
					pos -= 4;
				//			printf("0xff ended at 0x%lx\n", pos);
				continue;

			case JFFS_DIRTY_BITMASK:
				//			printf("0x00 started at 0x%lx\n", pos);
				for (; pos < end && wordbuf == JFFS_DIRTY_BITMASK; pos += 4) {
					if (pread(fs, &wordbuf, 4, pos) < 0) {
						perror("pread");
						exit(1);
					}
				}
				if (pos < end)
					pos -=4;
				//			printf("0x00 ended at 0x%lx\n", pos);
				continue;

			default:
				printf("Argh. Dirty memory at 0x%lx\n", pos);
				//			file_hexdump(fs, pos, 128);
				for (pos += 4; pos < end; pos += 4) {
					if (pread(fs, &wordbuf, 4, pos) < 0) {
						perror("pread");
						exit(1);
					}
					if (wordbuf == JFFS_MAGIC_BITMASK)
						break;
				}

			case JFFS_MAGIC_BITMASK:
				if (pread(fs, &ino, sizeof(ino), pos) < 0) {
					perror("pread");
					exit(1);
				}
				if (myino == -1 || ino.ino == myino) {
					printf("Magic found at 0x%lx\n", pos);
					jffs_print_raw_inode(&ino);
				}
				pos += sizeof(ino);

				if (myino == -1 || ino.ino == myino) {
					if (ino.nsize) {
						if (pread(fs, namebuf, min(ino.nsize, 4095), pos) < 0) {
							perror("pread");
							exit(1);
						}
						if (ino.nsize < 4095)
							namebuf[ino.nsize] = 0;
						else
							namebuf[4095] = 0;
						printf("Name: \"%s\"\n", namebuf);
					} else {
						printf("No Name\n");
					}
				}
				pos += (ino.nsize + 3) & ~3;

				pos += (ino.dsize + 3) & ~3;
		}



	}
}