]>
Commit | Line | Data |
---|---|---|
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 <stdint.h> | |
8 | #include <strings.h> | |
9 | #include <string.h> | |
10 | #include <unistd.h> | |
11 | #include <errno.h> | |
12 | #include <libgen.h> | |
13 | #include "rsb-lz.h" | |
14 | #include "filesystem.h" | |
15 | ||
16 | struct file_entry* get_next_file(uint8_t *fw, int32_t len) | |
17 | { | |
18 | static uint8_t *pos; | |
19 | static uint8_t *start; | |
20 | static uint8_t *end; | |
21 | static struct file_entry fent; | |
22 | int32_t name_length; | |
23 | ||
24 | if (fw != NULL && len != 0) { | |
25 | pos = fw + 0x28; | |
26 | ||
27 | #if 0 | |
28 | printf("Start of filesystem: 0x%08x\n", *((uint32_t*)pos)); | |
29 | #endif | |
30 | start = fw; | |
31 | pos = fw + *((uint32_t*)pos); | |
32 | end = fw + len; | |
33 | } | |
34 | ||
35 | fent.unknown = *pos; | |
36 | pos++; | |
37 | ||
38 | if (fent.unknown == 0x00) { | |
39 | /* EOF */ | |
40 | int32_t fill = (4 - ((pos - start) % 4)) % 4; | |
41 | int32_t i; | |
42 | ||
43 | for (i = 0; i < fill; i++) { | |
44 | if (*pos != 0xff) { | |
45 | fprintf(stderr, "Wrong fill byte after EOF: 0x%02x, aborting!\n", *pos); | |
46 | exit(1); | |
47 | } | |
48 | pos++; | |
49 | } | |
50 | ||
51 | if (pos != end) { | |
52 | fprintf(stderr, "EOF marker not at end-of-file %p <-> %p, aborting!\n", pos, end); | |
53 | exit(1); | |
54 | } | |
55 | ||
56 | return NULL; | |
57 | } | |
58 | ||
59 | ||
60 | name_length = *((uint32_t*)pos); | |
61 | pos += 4; | |
62 | ||
63 | fent.length = *((uint32_t*)pos); | |
64 | pos += 4; | |
65 | ||
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); | |
71 | exit(1); | |
72 | } | |
73 | ||
74 | fent.name = (char*)pos; | |
75 | pos += name_length; | |
76 | ||
77 | fent.start = pos; | |
78 | pos += fent.length; | |
79 | ||
80 | return &fent; | |
81 | } | |
82 | ||
83 | void extract_files(uint8_t *fw, int32_t len) | |
84 | { | |
85 | struct file_entry *fent; | |
86 | ||
87 | fent = get_next_file(fw, len); | |
88 | ||
89 | while (fent) { | |
90 | printf("%s: unknown: 0x%02x, length: %d, ", | |
91 | fent->name, fent->unknown, fent->length); | |
92 | ||
93 | if (fent->length > 0) { | |
94 | write_file(extracted_file(fent->name), fent->start, fent->length); | |
95 | if (*((uint32_t*)fent->start) == LZ_MAGIC) { | |
96 | char *lzname; | |
97 | uint8_t *outbuf; | |
98 | uint32_t outlen; | |
99 | ||
100 | if ((lzname = strdup(fent->name)) == NULL) { | |
101 | perror("strdup"); | |
102 | exit(1); | |
103 | } | |
104 | ||
105 | if (!strcmp(lzname + strlen(lzname) - 4 , ".lz")) { | |
106 | fprintf(stderr, "Ugh, can't derive filename...\n"); | |
107 | exit(1); | |
108 | } | |
109 | lzname[strlen(lzname) - 3] = 0x00; | |
110 | ||
111 | printf("%s: packed file found, ", lzname); | |
112 | ||
113 | outbuf = extract_lz_file(fent->start, &outlen, 0); | |
114 | write_file(extracted_file((char*)lzname), outbuf, outlen); | |
115 | ||
116 | free(outbuf); | |
117 | free(lzname); | |
118 | } else if (!strcmp(fent->name, "firmware")) { | |
119 | uint8_t *lzpos; | |
120 | char lzname[128]; | |
121 | ||
122 | bzero(lzname, sizeof(lzname)); | |
123 | strcpy(lzname, "firmware."); | |
124 | ||
125 | lzpos = fent->start + *((uint32_t*)(fent->start + 0x20)); | |
126 | memcpy(lzname + strlen(lzname), lzpos - 4, 4); | |
127 | lzpos += 4; | |
128 | if (*((uint32_t*)(lzpos)) == LZ_MAGIC) { | |
129 | uint8_t *outbuf; | |
130 | uint32_t outlen; | |
131 | ||
132 | printf("%s: compressed firmware part found", lzname); | |
133 | outbuf = extract_lz_file(lzpos, &outlen, 1); | |
134 | printf(", "); | |
135 | write_file(extracted_file((char*)lzname), outbuf, outlen); | |
136 | ||
137 | free(outbuf); | |
138 | } | |
139 | } | |
140 | } else { | |
141 | printf(", ignoring...\n"); | |
142 | } | |
143 | fent = get_next_file(NULL, 0); | |
144 | } | |
145 | } | |
146 | ||
147 | void replace_add_file(uint8_t *fw, int32_t len, char *fwname, char *lname) | |
148 | { | |
149 | fprintf(stderr, "%s: Implement me!\n", __func__); | |
150 | exit(1); | |
151 | } | |
152 | ||
153 | void list_files(uint8_t *fw, int32_t len) | |
154 | { | |
155 | struct file_entry *fent; | |
156 | ||
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); | |
159 | } | |
160 | ||
161 | } | |
162 | ||
163 | void mkdir_p(char *dir) | |
164 | { | |
165 | char *parent; | |
166 | char *tmpdir; | |
167 | ||
168 | if ((dir == NULL) || (!strcmp(dir, "."))) | |
169 | return; | |
170 | ||
171 | tmpdir = strdup(dir); | |
172 | if (tmpdir == NULL) { | |
173 | perror("strdup"); | |
174 | exit(1); | |
175 | } | |
176 | ||
177 | parent = strdup(dirname(tmpdir)); | |
178 | free(tmpdir); | |
179 | if (parent == NULL) { | |
180 | perror("strdup"); | |
181 | exit(1); | |
182 | } | |
183 | ||
184 | mkdir_p(parent); | |
185 | ||
186 | errno = 0; | |
187 | if (mkdir(dir, 0755) == -1) { | |
188 | if (errno != EEXIST) { | |
189 | fprintf(stderr, "%s: ", dir); | |
190 | perror("mkdir"); | |
191 | exit(1); | |
192 | } | |
193 | } | |
194 | free(parent); | |
195 | } | |
196 | ||
197 | void write_file(char *fname, uint8_t *buf, int32_t len) | |
198 | { | |
199 | char *filename_c, *dirn; | |
200 | int32_t fd; | |
201 | int32_t remaining; | |
202 | int32_t ret; | |
203 | ||
204 | if ((filename_c = strdup(fname)) == NULL) { | |
205 | perror("strdup"); | |
206 | exit(1); | |
207 | } | |
208 | dirn = strdup(dirname(filename_c)); | |
209 | if (dirn == NULL) { | |
210 | perror("strdup"); | |
211 | exit(1); | |
212 | } | |
213 | mkdir_p(dirn); | |
214 | free(dirn); | |
215 | free(filename_c); | |
216 | ||
217 | if ((fd = open(fname, O_WRONLY|O_CREAT, 0644)) == -1) { | |
218 | fprintf(stderr, "%s: ", fname); | |
219 | perror("open"); | |
220 | exit(1); | |
221 | } | |
222 | ||
223 | remaining = len; | |
224 | ||
225 | while(remaining) { | |
226 | ret = write(fd, buf + (len - remaining), remaining); | |
227 | if (ret < 0) { | |
228 | perror("write"); | |
229 | exit(1); | |
230 | } | |
231 | remaining -= ret; | |
232 | } | |
233 | ||
234 | printf("%s written.\n", fname); | |
235 | ||
236 | close(fd); | |
237 | } | |
238 | ||
239 | char *extracted_file(char *fname) | |
240 | { | |
241 | static char filename[PATH_MAX]; | |
242 | ||
243 | strcpy(filename, "extracted/"); | |
244 | strcat(filename, fname); | |
245 | ||
246 | return filename; | |
247 | } |