-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrcfs.c
389 lines (350 loc) · 11.2 KB
/
rcfs.c
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
/*
Copyright (c) 2013 by Chris Wade (cmw)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define RCFS_MAGIC "r-c-f-s"
#define RCFS_SUPERBLOCK_OFF 0x1020
#define RCFS_COMPRESSED 0x800000
#define EXTRACT_FILE 1
#define LIST_FILE 2
#define PRINT_INFO 3
#define DUMP_FILES 4
extern int lzo1x_decompress_safe(const unsigned char *in, size_t in_len, unsigned char *out, size_t *out_len);
static char timestamp[200] = {0};
char *print_timestamp(unsigned int tvalue)
{
struct tm *tm;
time_t time = tvalue;
tm = gmtime(&time);
sprintf(timestamp, "%s", asctime(tm));
timestamp[strlen(timestamp) - 1] = '\0';
return timestamp;
}
typedef struct inode_ptr {
unsigned short int unkn0;
unsigned short int flags;
mode_t perms; /* file permissions */
unsigned int fname_addr; /* filename offset */
unsigned int phyoff; /* file data offset */
unsigned int size; /* uncompressed file size */
unsigned int timestamp; /* modification timestamp */
unsigned int unkn3;
unsigned int unkn4;
} inode_ptr_s;
typedef struct file
{
__uint32_t header_size;
__uint32_t *boff;
__uint32_t num_chunks;
} file_s;
typedef struct rcfs
{
__uint32_t magic[2]; /* RCFS magic */
__uint32_t unkn1;
__uint32_t unkn2;
__uint32_t size; /* Raw disk size */
__uint32_t ninodes; /* Total number of inodes */
__uint32_t inode_off; /* Offset to inode block */
__uint32_t inode_len; /* Length of inode block */
__uint32_t fname_off; /* Offset to filename index block */
__uint32_t fname_len; /* Length of filename index block */
__uint32_t data_off; /* File data block start offset */
__uint32_t data_len; /* Length of file data block */
__uint32_t cbuflen; /* Cache buffer length */
FILE *fp;
inode_ptr_s *inodes;
} rcfs_s;
/*
* Open rcfs image
*/
rcfs_s *rcfs_open(char *filename)
{
FILE *fp;
rcfs_s *p;
fp = fopen(filename, "r");
if(!fp)
return NULL;
p = calloc(1, sizeof(rcfs_s));
if(!p)
{
fclose(fp);
return NULL;
}
// Read super block
fseek(fp, RCFS_SUPERBLOCK_OFF, SEEK_SET);
if(fread(p, 1, sizeof(rcfs_s), fp) != sizeof(rcfs_s))
goto err;
if(memcmp(p->magic, RCFS_MAGIC, 7) != 0)
goto err;
// Read inode block
fseek(fp, p->inode_off, SEEK_SET);
p->inodes = malloc(p->ninodes * sizeof(inode_ptr_s));
if(fread(p->inodes, 1, p->ninodes * sizeof(inode_ptr_s), fp) != (p->ninodes * sizeof(inode_ptr_s)))
goto err;
p->fp = fp;
return p;
err:
fclose(fp);
free(p->inodes);
free(p);
return NULL;
}
/*
* Close rcfs image
*/
void rcfs_close(rcfs_s *p)
{
fclose(p->fp);
free(p->inodes);
free(p);
}
/*
* Read a file junk and decompress
*/
size_t rcfs_read_chunk(rcfs_s *p, __uint8_t *buffer, size_t size, __uint32_t offset)
{
unsigned char *inbuffer = malloc(size);
size_t outlen = 0x4000;
int ret;
ret = fread(inbuffer, 1, size, p->fp);
ret = lzo1x_decompress_safe((const unsigned char *)inbuffer, offset, (unsigned char *)buffer, &outlen);
free(inbuffer);
if(ret < 0)
return 0;
return outlen;
}
/*
* Read and decompress each chunk of file
*/
size_t rcfs_read_file(rcfs_s *p, __uint32_t inode_index, __uint8_t *buffer, int compressed)
{
int readBytes, len, i;
__uint32_t num_chunks, header_size;
__uint32_t chunk_size, posOff = 0;
fseek(p->fp, p->inodes[inode_index].phyoff, SEEK_SET);
if(compressed) {
fread(&header_size, 1, sizeof(__uint32_t), p->fp);
num_chunks = ((header_size + (sizeof(__uint32_t) - 1)) / sizeof(__uint32_t)) - 1;
readBytes = 0;
for(i=0;i < num_chunks;i++)
{
fseek(p->fp, p->inodes[inode_index].phyoff+sizeof(__uint32_t)+i*4, SEEK_SET);
fread(&chunk_size, 1, sizeof(chunk_size), p->fp);
fseek(p->fp, p->inodes[inode_index].phyoff+header_size+posOff, SEEK_SET);
len = rcfs_read_chunk(p, (buffer+readBytes), chunk_size, (chunk_size-posOff-header_size));
if(!len)
break;
readBytes += len;
posOff += chunk_size-posOff-header_size;
}
}
else {
readBytes = fread(buffer, 1, p->inodes[inode_index].size, p->fp);
}
return readBytes;
}
/*
* Lookup inode number from filename
*/
__uint32_t rcfs_inode_lookup(rcfs_s *p, char *_fname)
{
int i;
char filename[1024] = {0};
for(i=0;i < p->ninodes; i++)
{
// We probably don't want a symbolic link
if(p->inodes[i].perms & S_IFCHR)
continue;
fseek(p->fp, p->inodes[i].fname_addr, SEEK_SET);
fread(filename, 1, 100, p->fp);
if(strncmp(_fname, filename, strlen(_fname)) == 0)
return i;
}
return 0;
}
/*
* Recursivly parse a directory inode
*/
void rcfs_directory_inode(rcfs_s *p, inode_ptr_s inode, char *folder, int cmd) {
int offinodes = (inode.phyoff - p->inode_off) / sizeof(inode_ptr_s);
int ninodes = inode.size / sizeof(inode_ptr_s);
int i,len;
char* ext;
char filename[1024] = {0};
char destination[2048] = {0};
__uint8_t *fdata = NULL;
FILE *outfile;
mkdir(folder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
for(i=offinodes; i<offinodes+ninodes; i++) {
if(p->inodes[i].perms & S_IFCHR)
continue;
fseek(p->fp, p->inodes[i].fname_addr, SEEK_SET);
fread(filename, 1, 100, p->fp);
sprintf(destination, "%s/%s", folder, filename);
if(p->inodes[i].perms & S_IFDIR)
{
ext = strrchr(folder,'/');
if (!ext)
ext = filename;
if (strlen(ext) > 1 && strcmp(ext,".") != 0 && strcmp(ext,"..") != 0)
{
if(cmd == DUMP_FILES) {
mkdir(destination, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
printf("Creating %s\n", filename);
}
rcfs_directory_inode(p, p->inodes[i], destination, cmd);
}
continue;
}
switch(cmd) {
case DUMP_FILES:
printf("Extracting %s...\n", filename);
fdata = malloc(p->inodes[i].size);
len = rcfs_read_file(p, i, fdata, p->inodes[i].perms & RCFS_COMPRESSED);
if(len > 0)
{
outfile = fopen(destination, "w");
fwrite(fdata, 1, len, outfile);
fclose(outfile);
}
else {
outfile = fopen(destination, "w");
fclose(outfile);
printf("Error extracting file\n");
}
free(fdata);
break;
case LIST_FILE:
printf("%c%c%c%c%c%c%c%c%c%c %s %6i %s\n",
(p->inodes[i].perms & S_IFDIR) ? 'd' : '-',
(p->inodes[i].perms & S_IRUSR) ? 'r' : '-',
(p->inodes[i].perms & S_IWUSR) ? 'w' : '-',
(p->inodes[i].perms & S_IXUSR) ? 'x' : '-',
(p->inodes[i].perms & S_IRGRP) ? 'r' : '-',
(p->inodes[i].perms & S_IWGRP) ? 'w' : '-',
(p->inodes[i].perms & S_ISGID) ? 's' : ((p->inodes[i].perms & S_IXGRP) ? 'x' : '-'),
(p->inodes[i].perms & S_IROTH) ? 'r' : '-',
(p->inodes[i].perms & S_IWOTH) ? 'w' : '-',
(p->inodes[i].perms & S_IXOTH) ? 'x' : '-',
print_timestamp(p->inodes[i].timestamp),
p->inodes[i].size,
destination);
break;
}
}
}
int main(int argc, char *argv[])
{
rcfs_s *p;
int cmd = 0;
int argsvalid = 0;
char *filename;
if(argc >= 3)
{
if ((strcmp(argv[1], "-e") == 0) && argc == 4) {
cmd = EXTRACT_FILE;
filename = strdup(argv[3]);
}
if (strcmp(argv[1], "-l") == 0 )
cmd = LIST_FILE;
if (strcmp(argv[1], "-p") == 0 )
cmd = PRINT_INFO;
if (strcmp(argv[1], "-d") == 0 )
cmd = DUMP_FILES;
if(cmd)
argsvalid = 1;
}
if ( !argsvalid ) {
printf("Usage: \n");
printf(" %s {-e|-l|-p|-d} image.bin {filename}\n", argv[0]);
printf(" %s -e image.bin filename - extracts filename\n", argv[0]);
printf(" %s -l image.bin - lists all files and directories\n", argv[0]);
printf(" %s -p image.bin - prints superblock information\n", argv[0]);
printf(" %s -d image.bin - dumps all files and directories\n", argv[0]);
exit(1);
}
p = rcfs_open(argv[2]);
if(!p)
{
printf("rcfs: Invalid rcfs image file %s\n", argv[2]);
return 1;
}
switch(cmd) {
case PRINT_INFO:
printf("fs-rcfs: -------- superblock information--------\n");
printf("size: 0x%08x\n", p->size);
printf("cbuflen: 0x%08x\n", p->cbuflen);
printf("data_off: 0x%08x\n", p->data_off);
printf("data_len: 0x%08x\n", p->data_len);
printf("inode_off: 0x%08x\n", p->inode_off);
printf("inode_len: 0x%08x\n", p->inode_len);
printf("fname_off: 0x%08x\n", p->fname_off);
printf("fname_len: 0x%08x\n", p->fname_len);
printf("inodes: 0x%08x\n", p->ninodes);
break;
case LIST_FILE:
rcfs_directory_inode(p, p->inodes[0], "", cmd);
break;
case DUMP_FILES:
{
char* ext;
char folder[1024] = {0};
strcpy(folder, argv[2]);
ext = strrchr(folder,'.');
if (ext)
folder[ext-folder] = '\0';
rcfs_directory_inode(p, p->inodes[0], folder, cmd);
break;
}
case EXTRACT_FILE:
{
int len = 0;
int inode = rcfs_inode_lookup(p, filename);
__uint8_t *fdata = NULL;
FILE *outfile;
if(!inode)
{
printf("Unable to find %s in image\n", filename);
free(filename);
free(fdata);
break;
}
fdata = malloc(p->inodes[inode].size);
len = rcfs_read_file(p, inode, fdata, p->inodes[inode].perms & RCFS_COMPRESSED);
if(len > 0)
{
outfile = fopen(filename, "w");
fwrite(fdata, 1, len, outfile);
fclose(outfile);
printf("successfully extracted %s\n", filename);
} else {
printf("Failed to decompress file\n");
}
free(fdata);
free(filename);
break;
}
}
rcfs_close(p);
return 0;
}