]> git.zerfleddert.de Git - rsbs2/blob - firmware.c
ac83b96e4c2911f5984c0de09a646a6786ac0954
[rsbs2] / firmware.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <strings.h>
10 #include "rsb-crc.h"
11
12 #define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str)))
13
14 struct properties {
15 unsigned int magic;
16 unsigned char unknown0;
17 unsigned char unknown1;
18 unsigned char right_rw;
19 unsigned char rw_mask;
20 unsigned char type1;
21 unsigned char unknown5;
22 unsigned char unknown6;
23 unsigned char unknown7;
24 unsigned char type2;
25 unsigned char val[];
26 };
27
28 #define PROP_ACTION_TRUE (1<<0)
29 #define PROP_ACTION_FALSE (1<<1)
30 #define PROP_ACTION_RO (1<<2)
31 #define PROP_ACTION_RW (1<<3)
32
33 #define PROP_STATUS_NOTFOUND (0)
34 #define PROP_STATUS_WRONGTYPE (1<<0)
35 #define PROP_STATUS_WRONGRIGHTS (1<<1)
36 #define PROP_STATUS_SAMEVAL (1<<2)
37 #define PROP_STATUS_SUCCESS (1<<3)
38
39 struct propaction {
40 char *property;
41 unsigned int action;
42 unsigned int status;
43 struct propaction *next;
44 };
45
46 void show_properties(unsigned char *fw, int len)
47 {
48 int i;
49
50 for (i = 0; i < (len-100 /* XXX */); i++) {
51 if (FINDSTR(fw+i, "/default/fw_prop/") ||
52 FINDSTR(fw+i, "/default/fw_setup/") ||
53 FINDSTR(fw+i, "/default/oem_prop/")) {
54 struct properties *prop;
55 unsigned char *pos = fw + i;
56
57 printf("0x%08x: found setting: %s ", i, pos);
58
59 prop = (struct properties*)(pos + strlen((char*)pos) + 1);
60
61 if (prop->magic != 0x83011111) {
62 printf("ignoring...\n");
63 continue;
64 }
65
66 if (prop->type1 == 0x00 && prop->type2 == 0x04) {
67 printf("STRING: '%s' ", prop->val);
68 } else if (prop->type1 == 0x01 && prop->type2 == 0x01) {
69 printf("BOOL: %s ",(*prop->val ? "TRUE" : "FALSE"));
70 } else if (prop->type1 == 0x04 && prop->type2 == 0x02) {
71 printf("VAL: 0x%x ", *((unsigned int*)prop->val));
72 } else {
73 printf("0x%02x 0x%2x...ignoring\n", prop->type1, prop->type2);
74 continue;
75 }
76
77 if (prop->right_rw == 0x00 && prop->rw_mask == 0x00) {
78 printf("(R-) ");
79 } else if (prop->right_rw == 0x01) {
80 printf("(RW mask: 0x%02x) ", prop->rw_mask);
81 } else {
82 printf("(UNK 0x%02x 0x%02x) ", prop->right_rw, prop->rw_mask);
83 }
84 printf("\n");
85 }
86 }
87 }
88
89 void change_properties(unsigned char *fw, int len, struct propaction *paction)
90 {
91 int i;
92 struct propaction *cpaction;
93
94 for (i = 0; i < (len-100 /* XXX */); i++) {
95 cpaction = paction;
96 while (cpaction != NULL) {
97 if (FINDSTR(fw + i, cpaction->property)) {
98 break;
99 }
100 cpaction = cpaction->next;
101 }
102 if (cpaction != NULL) {
103 struct properties *prop;
104 unsigned char *pos = fw + i;
105
106 prop = (struct properties*)(pos + strlen((char*)pos) + 1);
107
108 if (prop->magic != 0x83011111) {
109 continue;
110 }
111
112 if (cpaction->action & (PROP_ACTION_TRUE|PROP_ACTION_FALSE)) {
113 if (prop->type1 == 0x01 && prop->type2 == 0x01) {
114 if (cpaction->action & PROP_ACTION_TRUE) {
115 if (*prop->val == 0x00) {
116 *prop->val = 0x01;
117 cpaction->status |= PROP_STATUS_SUCCESS;
118 } else {
119 cpaction->status |= PROP_STATUS_SAMEVAL;
120 }
121 } else {
122 if (*prop->val == 0x01) {
123 *prop->val = 0x00;
124 cpaction->status |= PROP_STATUS_SUCCESS;
125 } else {
126 cpaction->status |= PROP_STATUS_SAMEVAL;
127 }
128 }
129 } else {
130 cpaction->status = PROP_STATUS_WRONGTYPE;
131 }
132 }
133 if (cpaction->action & PROP_ACTION_RW) {
134 if (prop->right_rw == 0x00 && prop->rw_mask == 0x00) {
135 prop->right_rw = 0x01;
136 prop->rw_mask = 0x02;
137 cpaction->status |= PROP_STATUS_SUCCESS;
138 } else {
139 cpaction->status |= PROP_STATUS_WRONGRIGHTS;
140 }
141 }
142 if (cpaction->action & PROP_ACTION_RO) {
143 if (prop->right_rw == 0x01 && prop->rw_mask == 0x02) {
144 prop->right_rw = 0x00;
145 prop->rw_mask = 0x00;
146 cpaction->status |= PROP_STATUS_SUCCESS;
147 } else {
148 cpaction->status |= PROP_STATUS_WRONGRIGHTS;
149 }
150 }
151 }
152 }
153 }
154
155 void print_boarddescription(unsigned char *bd)
156 {
157 int j;
158
159 for (j = 0; j < 32; j++) {
160 printf("%02x ", *(bd+j));
161 }
162 printf("\n");
163 }
164
165 void handle_boarddescription(unsigned char *fw, int len, int patch)
166 {
167 int i;
168
169 for (i = len - (strlen("pdata")+1); i > 0; i--) {
170 if (FINDSTR(fw+i, "pdata")) {
171 unsigned char *pos = fw + i + strlen("pdata") + 1;
172
173 /* MAGIC? */
174 if (*((unsigned int*)pos) != 0x00002802) {
175 continue;
176 }
177
178 pos += 26;
179
180 /* MAGIC2? */
181 if (*((unsigned int*)pos) != 0x00500101) {
182 continue;
183 }
184
185 printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
186 print_boarddescription(pos);
187
188 if (patch) {
189 /* Enable ATX and relay power switching */
190 pos[22] |= 0x03;
191 printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
192 print_boarddescription(pos);
193 }
194
195 break;
196 }
197 }
198 }
199
200 void syntax(char *name)
201 {
202 fprintf(stderr,"Syntax: %s parameters firmware.bin\n", name);
203 fprintf(stderr,"parameters as follows:\n");
204 fprintf(stderr,"\t-d\t\tdisplay all properties of the image\n");
205 fprintf(stderr,"\t-u\t\tupdate checksum of the image\n");
206 fprintf(stderr,"\t-b\t\tmodify BOARD_DESCRIPTION for more power-switch options\n");
207 fprintf(stderr,"\t-t property\tset 'property' to true\n");
208 fprintf(stderr,"\t-f property\tset 'property' to false\n");
209 fprintf(stderr,"\t-w property\tallow read-write access to 'property'\n");
210 fprintf(stderr,"\t-r property\tallow read-only access to 'property'\n");
211 exit(1);
212 }
213
214 void add_action(int opt, char *optarg, struct propaction **paction) {
215 struct propaction *pos = *paction;
216 struct propaction *prev = NULL;
217
218 while (pos != NULL) {
219 if (!strcmp(pos->property, optarg))
220 break;
221 prev = pos;
222 pos = pos->next;
223 }
224
225 if (pos == NULL) {
226 pos = malloc(sizeof(struct propaction));
227 if (pos == NULL) {
228 perror("malloc");
229 exit(1);
230 }
231 bzero(pos, sizeof(struct propaction));
232 pos->property = optarg;
233
234 if (prev == NULL) {
235 *paction = pos;
236 } else {
237 prev->next = pos;
238 }
239 }
240
241 switch(opt) {
242 case 't':
243 if (pos->action & PROP_ACTION_FALSE) {
244 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
245 exit(1);
246 }
247 pos->action |= PROP_ACTION_TRUE;
248 break;
249 case 'f':
250 if (pos->action & PROP_ACTION_TRUE) {
251 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
252 exit(1);
253 }
254 pos->action |= PROP_ACTION_FALSE;
255 break;
256 case 'w':
257 if (pos->action & PROP_ACTION_RO) {
258 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
259 exit(1);
260 }
261 pos->action |= PROP_ACTION_RW;
262 break;
263 case 'r':
264 if (pos->action & PROP_ACTION_RW) {
265 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
266 exit(1);
267 }
268 pos->action |= PROP_ACTION_RO;
269 break;
270 }
271 }
272
273 int check_crc(unsigned char *fw, int len)
274 {
275 int ret;
276 unsigned int crc, oldcrc;
277
278 ret = rsb_crc2(fw, len, 0x55335053, &crc);
279 oldcrc = (unsigned int)*((unsigned int*)(fw + len - 4));
280
281 printf("Checksum: 0x%08x (%s), should be: 0x%08x\n",
282 crc,
283 (ret ? "NOT OK" : "OK"),
284 oldcrc);
285
286 return ret;
287 }
288
289 int main(int argc, char **argv)
290 {
291 struct stat statbuf;
292 char *file = NULL;
293 unsigned char *fw;
294 int fd;
295 int remaining;
296 int ret;
297 int opt;
298 unsigned int crc;
299 struct propaction *paction = NULL;
300 int showall = 0;
301 int update_crc = 0;
302 int patch_bd = 0;
303 int patch_fw = 0;
304
305 if (argc < 2)
306 syntax(argv[0]);
307
308 while ((opt = getopt(argc, argv, "dubt:f:w:r:")) != -1) {
309 switch(opt) {
310 case 'd':
311 showall = 1;
312 break;
313 case 'u':
314 update_crc = 1;
315 break;
316 case 'b':
317 patch_bd = 1;
318 break;
319 case 't':
320 case 'f':
321 case 'w':
322 case 'r':
323 patch_fw = 1;
324 add_action(opt, optarg, &paction);
325 break;
326 default:
327 syntax(argv[0]);
328 }
329 }
330
331 if (argc > optind) {
332 file = argv[optind];
333 } else {
334 syntax(argv[0]);
335 }
336
337 if (stat(file, &statbuf) == -1) {
338 fprintf(stderr,"%s: ", file);
339 perror("stat");
340 exit(1);
341 }
342
343 if ((fd = open(file, O_RDONLY)) == -1) {
344 fprintf(stderr,"%s: ", file);
345 perror("open");
346 exit(1);
347 }
348
349 if ((fw = malloc(statbuf.st_size)) == NULL) {
350 perror("malloc");
351 exit(1);
352 }
353
354 bzero(fw, statbuf.st_size);
355
356 remaining = statbuf.st_size;
357
358 while(remaining) {
359 if ((ret = read(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
360 perror("read");
361 exit(1);
362 }
363 remaining -= ret;
364 }
365 close(fd);
366
367 ret = check_crc(fw, statbuf.st_size);
368 if ((ret != 0) && (!update_crc)) {
369 fprintf(stderr,"Checksum incorrect, aborting...\n");
370 }
371
372 if (patch_fw) {
373 struct propaction *cpaction = paction;
374
375 change_properties(fw, statbuf.st_size, paction);
376
377 printf("\nProperty change results:\n");
378 while(cpaction != NULL) {
379 printf("%s: ", cpaction->property);
380
381 if (cpaction->status == PROP_STATUS_NOTFOUND)
382 printf("NOTFOUND ");
383 if (cpaction->status & PROP_STATUS_SUCCESS)
384 printf("SUCCESS ");
385 if (cpaction->status & PROP_STATUS_SAMEVAL)
386 printf("SAMEVAL ");
387 if (cpaction->status & PROP_STATUS_WRONGTYPE)
388 printf("WRONGTYPE ");
389 if (cpaction->status & PROP_STATUS_WRONGRIGHTS)
390 printf("WRONGRIGHTS ");
391 printf("\n");
392
393 cpaction = cpaction->next;
394 }
395 printf("\n");
396 }
397
398 if (patch_bd) {
399 handle_boarddescription(fw, statbuf.st_size -4, 1);
400 }
401
402 if (showall) {
403 show_properties(fw, statbuf.st_size - 4);
404 handle_boarddescription(fw, statbuf.st_size -4, 0);
405 }
406
407 if (update_crc || patch_fw || patch_bd) {
408 ret = rsb_crc2(fw, statbuf.st_size, 0x55335053, &crc);
409 if (ret == 4) {
410 *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
411 }
412
413 if (check_crc(fw, statbuf.st_size) == 0) {
414 char *newfile;
415
416 newfile = malloc(strlen(file) + strlen(".patched") + 1);
417 if (newfile == NULL) {
418 perror("malloc");
419 exit(1);
420 }
421 strcpy(newfile, file);
422 strcat(newfile, ".patched");
423
424 printf("Writing %s\n", newfile);
425 if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
426 fprintf(stderr,"%s: ", file);
427 perror("open");
428 exit(1);
429 }
430
431 remaining = statbuf.st_size;
432
433 while(remaining) {
434 if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
435 perror("write");
436 exit(1);
437 }
438 remaining -= ret;
439 }
440 close(fd);
441 } else {
442 fprintf(stderr,"Can't set correct checksum, aborting...\n");
443 }
444 }
445
446 exit(0);
447 }
Impressum, Datenschutz