]> git.zerfleddert.de Git - rsbs2/blob - filesystem.c
2d71946d259d4b503e19518cda4052c9a4f0a29f
[rsbs2] / filesystem.c
1 #include <sys/stat.h>
2 #include <sys/types.h>
3 #include <limits.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <strings.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <libgen.h>
12 #include "rsb-lz.h"
13 #include "filesystem.h"
14
15 struct file_entry* get_next_file(unsigned char *fw, int len)
16 {
17 static unsigned char *pos;
18 static unsigned char *start;
19 static unsigned char *end;
20 static struct file_entry fent;
21 int name_length;
22
23 if (fw != NULL && len != 0) {
24 pos = fw + 0x28;
25
26 #if 0
27 printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos));
28 #endif
29 start = fw;
30 pos = fw + *((unsigned int*)pos);
31 end = fw + len;
32 }
33
34 fent.unknown = *pos;
35 pos++;
36
37 if (fent.unknown == 0x00) {
38 /* EOF */
39 int fill = (4 - ((pos - start) % 4)) % 4;
40 int i;
41
42 for (i = 0; i < fill; i++) {
43 if (*pos != 0xff) {
44 fprintf(stderr, "Wrong fill byte after EOF: 0x%02x, aborting!\n", *pos);
45 exit(1);
46 }
47 pos++;
48 }
49
50 if (pos != end) {
51 fprintf(stderr, "EOF marker not at end-of-file %p <-> %p, aborting!\n", pos, end);
52 exit(1);
53 }
54
55 return NULL;
56 }
57
58
59 name_length = *((unsigned int*)pos);
60 pos += 4;
61
62 fent.length = *((unsigned int*)pos);
63 pos += 4;
64
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);
70 exit(1);
71 }
72
73 fent.name = (char*)pos;
74 pos += name_length;
75
76 fent.start = pos;
77 pos += fent.length;
78
79 return &fent;
80 }
81
82 void extract_files(unsigned char *fw, int len)
83 {
84 struct file_entry *fent;
85
86 fent = get_next_file(fw, len);
87
88 while (fent) {
89 printf("%s: unknown: 0x%02x, length: %d",
90 fent->name, fent->unknown, fent->length);
91
92 if (fent->length > 0) {
93 write_file(fent->name, fent->start, fent->length);
94 if (*((unsigned int*)fent->start) == LZ_MAGIC) {
95 char *lzname;
96
97 if ((lzname = strdup(fent->name)) == NULL) {
98 perror("strdup");
99 exit(1);
100 }
101
102 if (!strcmp(lzname + strlen(lzname) - 4 , ".lz")) {
103 fprintf(stderr, "Ugh, can't derive filename...\n");
104 exit(1);
105 }
106 lzname[strlen(lzname) - 3] = 0x00;
107
108 printf("%s: packed file found", lzname);
109
110 extract_lz_file(fent->start, (unsigned char*)lzname);
111 free(lzname);
112 } else if (!strcmp(fent->name, "firmware")) {
113 unsigned char *lzpos;
114 char lzname[128];
115
116 bzero(lzname, sizeof(lzname));
117 strcpy(lzname, "firmware.");
118
119 lzpos = fent->start + *((unsigned int*)(fent->start + 0x20));
120 memcpy(lzname + strlen(lzname), lzpos - 4, 4);
121 lzpos += 4;
122 if (*((unsigned int*)(lzpos)) == LZ_MAGIC) {
123 printf("%s: compressed firmware part found", lzname);
124 extract_lz_file(lzpos, (unsigned char*)lzname);
125 }
126 }
127 } else {
128 printf(", ignoring...\n");
129 }
130 fent = get_next_file(NULL, 0);
131 }
132 }
133
134 void mkdir_p(char *dir)
135 {
136 char *copy, *parent;
137
138 if ((dir == NULL) || (!strcmp(dir, ".")))
139 return;
140
141 if ((copy = strdup(dir)) == NULL) {
142 perror("strdup");
143 exit(1);
144 }
145 parent = dirname(copy);
146 mkdir_p(parent);
147
148 errno = 0;
149 if (mkdir(dir, 0755) == -1) {
150 if (errno != EEXIST) {
151 fprintf(stderr, "%s: ", dir);
152 perror("mkdir");
153 exit(1);
154 }
155 }
156 free(copy);
157 }
158
159 void write_file(char *fname, unsigned char *buf, int len)
160 {
161 char filename[PATH_MAX];
162 char *filename_c, *dirn;
163 int fd;
164 int remaining;
165 int ret;
166
167 strcpy(filename, "extracted/");
168 strcat(filename, fname);
169
170 if ((filename_c = strdup(filename)) == NULL) {
171 perror("strdup");
172 exit(1);
173 }
174 dirn = dirname(filename_c);
175 mkdir_p(dirn);
176 free(filename_c);
177
178 if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) {
179 fprintf(stderr, "%s: ", filename);
180 perror("open");
181 exit(1);
182 }
183
184 remaining = len;
185
186 while(remaining) {
187 ret = write(fd, buf + (len - remaining), remaining);
188 if (ret < 0) {
189 perror("write");
190 exit(1);
191 }
192 remaining -= ret;
193 }
194
195 printf(", %s written.\n", filename);
196
197 close(fd);
198 }
Impressum, Datenschutz