]>
git.zerfleddert.de Git - rsbs2/blob - src/filesystem.c
f0465a8c621d98ea11bc90ed8d4b47784cff1bfa
14 #include "filesystem.h"
16 struct file_entry
* get_next_file(uint8_t *fw
, int32_t len
)
19 static uint8_t *start
;
21 static struct file_entry fent
;
24 if (fw
!= NULL
&& len
!= 0) {
28 printf("Start of filesystem: 0x%08x\n", *((uint32_t*)pos
));
31 pos
= fw
+ *((uint32_t*)pos
);
38 if (fent
.unknown
== 0x00) {
40 int32_t fill
= (4 - ((pos
- start
) % 4)) % 4;
43 for (i
= 0; i
< fill
; i
++) {
45 fprintf(stderr
, "Wrong fill byte after EOF: 0x%02x, aborting!\n", *pos
);
52 fprintf(stderr
, "EOF marker not at end-of-file %p <-> %p, aborting!\n", pos
, end
);
60 name_length
= *((uint32_t*)pos
);
63 fent
.length
= *((uint32_t*)pos
);
66 if ((fent
.length
> (end
- pos
)) ||
67 (name_length
> (end
- pos
))) {
68 printf("EOF reached without hitting EOF marker, aborting "
69 "(unknown: 0x%02x, namelen: 0x%08x, contentlen: 0x%08x!\n",
70 fent
.unknown
, name_length
, fent
.length
);
74 fent
.name
= (char*)pos
;
83 void extract_files(uint8_t *fw
, int32_t len
)
85 struct file_entry
*fent
;
87 fent
= get_next_file(fw
, len
);
90 printf("%s: unknown: 0x%02x, length: %d, ",
91 fent
->name
, fent
->unknown
, fent
->length
);
93 if (fent
->length
> 0) {
94 write_file(extracted_file(fent
->name
), fent
->start
, fent
->length
);
95 if (*((uint32_t*)fent
->start
) == LZ_MAGIC
) {
100 if ((lzname
= strdup(fent
->name
)) == NULL
) {
105 if (!strcmp(lzname
+ strlen(lzname
) - 4 , ".lz")) {
106 fprintf(stderr
, "Ugh, can't derive filename...\n");
109 lzname
[strlen(lzname
) - 3] = 0x00;
111 printf("%s: packed file found, ", lzname
);
113 outbuf
= extract_lz_file(fent
->start
, &outlen
, 0);
114 write_file(extracted_file((char*)lzname
), outbuf
, outlen
);
118 } else if (!strcmp(fent
->name
, "firmware")) {
122 bzero(lzname
, sizeof(lzname
));
123 strcpy(lzname
, "firmware.");
125 lzpos
= fent
->start
+ *((uint32_t*)(fent
->start
+ 0x20));
126 memcpy(lzname
+ strlen(lzname
), lzpos
- 4, 4);
128 if (*((uint32_t*)(lzpos
)) == LZ_MAGIC
) {
132 printf("%s: compressed firmware part found", lzname
);
133 outbuf
= extract_lz_file(lzpos
, &outlen
, 1);
135 write_file(extracted_file((char*)lzname
), outbuf
, outlen
);
141 printf(", ignoring...\n");
143 fent
= get_next_file(NULL
, 0);
147 void replace_add_file(uint8_t *fw
, int32_t len
, char *fwname
, char *lname
)
149 fprintf(stderr
, "%s: Implement me!\n", __func__
);
153 void list_files(uint8_t *fw
, int32_t len
)
155 struct file_entry
*fent
;
157 for (fent
= get_next_file(fw
, len
); fent
!= NULL
; fent
= get_next_file(NULL
, 0)) {
158 printf("0x%x %8d %s\n", fent
->unknown
, fent
->length
, fent
->name
);
163 void mkdir_p(char *dir
)
168 if ((dir
== NULL
) || (!strcmp(dir
, ".")))
171 tmpdir
= strdup(dir
);
172 if (tmpdir
== NULL
) {
177 parent
= strdup(dirname(tmpdir
));
179 if (parent
== NULL
) {
187 if (mkdir(dir
, 0755) == -1) {
188 if (errno
!= EEXIST
) {
189 fprintf(stderr
, "%s: ", dir
);
197 void write_file(char *fname
, uint8_t *buf
, int32_t len
)
199 char *filename_c
, *dirn
;
204 if ((filename_c
= strdup(fname
)) == NULL
) {
208 dirn
= strdup(dirname(filename_c
));
217 if ((fd
= open(fname
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
218 fprintf(stderr
, "%s: ", fname
);
226 ret
= write(fd
, buf
+ (len
- remaining
), remaining
);
234 printf("%s written.\n", fname
);
239 char *extracted_file(char *fname
)
241 static char filename
[PATH_MAX
];
243 strcpy(filename
, "extracted/");
244 strcat(filename
, fname
);