]>
cvs.zerfleddert.de Git - rsbs2/blob - filesystem.c
13 #include "filesystem.h"
15 struct file_entry
* get_next_file(unsigned char *fw
, int len
)
17 static unsigned char *pos
;
18 static unsigned char *start
;
19 static unsigned char *end
;
20 static struct file_entry fent
;
23 if (fw
!= NULL
&& len
!= 0) {
27 printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos
));
30 pos
= fw
+ *((unsigned int*)pos
);
37 if (fent
.unknown
== 0x00) {
39 int fill
= (4 - ((pos
- start
) % 4)) % 4;
42 for (i
= 0; i
< fill
; i
++) {
44 fprintf(stderr
, "Wrong fill byte after EOF: 0x%02x, aborting!\n", *pos
);
51 fprintf(stderr
, "EOF marker not at end-of-file %p <-> %p, aborting!\n", pos
, end
);
59 name_length
= *((unsigned int*)pos
);
62 fent
.length
= *((unsigned int*)pos
);
65 if ((fent
.length
> (end
- pos
)) ||
66 (name_length
> (end
- pos
))) {
67 printf("EOF reached without hitting EOF marker, aborting "
68 "(unknown: 0x%02x, namelen: 0x%08x, contentlen: 0x%08x!\n",
69 fent
.unknown
, name_length
, fent
.length
);
73 fent
.name
= (char*)pos
;
82 void extract_files(unsigned char *fw
, int len
)
84 struct file_entry
*fent
;
86 fent
= get_next_file(fw
, len
);
89 printf("%s: unknown: 0x%02x, length: %d, ",
90 fent
->name
, fent
->unknown
, fent
->length
);
92 if (fent
->length
> 0) {
93 write_file(extracted_file(fent
->name
), fent
->start
, fent
->length
);
94 if (*((unsigned int*)fent
->start
) == LZ_MAGIC
) {
96 unsigned char *outbuf
;
99 if ((lzname
= strdup(fent
->name
)) == NULL
) {
104 if (!strcmp(lzname
+ strlen(lzname
) - 4 , ".lz")) {
105 fprintf(stderr
, "Ugh, can't derive filename...\n");
108 lzname
[strlen(lzname
) - 3] = 0x00;
110 printf("%s: packed file found, ", lzname
);
112 outbuf
= extract_lz_file(fent
->start
, &outlen
, 0);
113 write_file(extracted_file((char*)lzname
), outbuf
, outlen
);
117 } else if (!strcmp(fent
->name
, "firmware")) {
118 unsigned char *lzpos
;
121 bzero(lzname
, sizeof(lzname
));
122 strcpy(lzname
, "firmware.");
124 lzpos
= fent
->start
+ *((unsigned int*)(fent
->start
+ 0x20));
125 memcpy(lzname
+ strlen(lzname
), lzpos
- 4, 4);
127 if (*((unsigned int*)(lzpos
)) == LZ_MAGIC
) {
128 unsigned char *outbuf
;
131 printf("%s: compressed firmware part found", lzname
);
132 outbuf
= extract_lz_file(lzpos
, &outlen
, 1);
134 write_file(extracted_file((char*)lzname
), outbuf
, outlen
);
140 printf(", ignoring...\n");
142 fent
= get_next_file(NULL
, 0);
146 void replace_add_file(unsigned char *fw
, int len
, char *fwname
, char *lname
)
148 fprintf(stderr
, "%s: Implement me!\n", __func__
);
152 void list_files(unsigned char *fw
, int len
)
154 struct file_entry
*fent
;
156 for (fent
= get_next_file(fw
, len
); fent
!= NULL
; fent
= get_next_file(NULL
, 0)) {
157 printf("0x%x %8d %s\n", fent
->unknown
, fent
->length
, fent
->name
);
162 void mkdir_p(char *dir
)
166 if ((dir
== NULL
) || (!strcmp(dir
, ".")))
169 if ((copy
= strdup(dir
)) == NULL
) {
173 parent
= dirname(copy
);
177 if (mkdir(dir
, 0755) == -1) {
178 if (errno
!= EEXIST
) {
179 fprintf(stderr
, "%s: ", dir
);
187 void write_file(char *fname
, unsigned char *buf
, int len
)
189 char *filename_c
, *dirn
;
194 if ((filename_c
= strdup(fname
)) == NULL
) {
198 dirn
= dirname(filename_c
);
202 if ((fd
= open(fname
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
203 fprintf(stderr
, "%s: ", fname
);
211 ret
= write(fd
, buf
+ (len
- remaining
), remaining
);
219 printf("%s written.\n", fname
);
224 char *extracted_file(char *fname
)
226 static char filename
[PATH_MAX
];
228 strcpy(filename
, "extracted/");
229 strcat(filename
, fname
);