]> cvs.zerfleddert.de Git - rsbs2/blame - firmware.c
add 'list all files in image' option
[rsbs2] / firmware.c
CommitLineData
972ac24b
MG
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>
7f88d2b6 8#include <string.h>
972ac24b
MG
9#include <strings.h>
10#include "rsb-crc.h"
e8563c43 11#include "filesystem.h"
972ac24b 12
7f88d2b6
MG
13#define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str)))
14
39601b0e
MG
15struct properties {
16 unsigned int magic;
17 unsigned char unknown0;
18 unsigned char unknown1;
19 unsigned char right_rw;
20 unsigned char rw_mask;
21 unsigned char type1;
22 unsigned char unknown5;
23 unsigned char unknown6;
24 unsigned char unknown7;
25 unsigned char type2;
26 unsigned char val[];
27};
28
7ac4bfad
MG
29#define PROP_ACTION_TRUE (1<<0)
30#define PROP_ACTION_FALSE (1<<1)
31#define PROP_ACTION_RO (1<<2)
32#define PROP_ACTION_RW (1<<3)
33
34#define PROP_STATUS_NOTFOUND (0)
35#define PROP_STATUS_WRONGTYPE (1<<0)
36#define PROP_STATUS_WRONGRIGHTS (1<<1)
37#define PROP_STATUS_SAMEVAL (1<<2)
38#define PROP_STATUS_SUCCESS (1<<3)
39
40struct propaction {
41 char *property;
42 unsigned int action;
43 unsigned int status;
44 struct propaction *next;
45};
46
39601b0e 47void show_properties(unsigned char *fw, int len)
7f88d2b6 48{
0a465cc0 49 struct file_entry *fent;
7f88d2b6 50
0a465cc0
MG
51 for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
52 if (FINDSTR(fent->name, "/default/fw_prop/") ||
53 FINDSTR(fent->name, "/default/fw_setup/") ||
54 FINDSTR(fent->name, "/default/oem_prop/")) {
39601b0e 55 struct properties *prop;
7f88d2b6 56
0a465cc0 57 printf("0x%08x: found setting: %s ", fent->start - fw, fent->name);
7f88d2b6 58
0a465cc0 59 prop = (struct properties*)fent->start;
39601b0e
MG
60
61 if (prop->magic != 0x83011111) {
7f88d2b6
MG
62 printf("ignoring...\n");
63 continue;
64 }
65
39601b0e 66 if (prop->type1 == 0x00 && prop->type2 == 0x04) {
7ac4bfad 67 printf("STRING: '%s' ", prop->val);
39601b0e
MG
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));
7f88d2b6 72 } else {
39601b0e 73 printf("0x%02x 0x%2x...ignoring\n", prop->type1, prop->type2);
7f88d2b6
MG
74 continue;
75 }
76
39601b0e 77 if (prop->right_rw == 0x00 && prop->rw_mask == 0x00) {
a519eca7 78 printf("(R-)");
39601b0e 79 } else if (prop->right_rw == 0x01) {
a519eca7 80 printf("(RW mask: 0x%02x)", prop->rw_mask);
7f88d2b6 81 } else {
a519eca7 82 printf("(UNK 0x%02x 0x%02x)", prop->right_rw, prop->rw_mask);
7f88d2b6 83 }
0a465cc0 84 printf(", length: %d\n", fent->length);
7f88d2b6
MG
85 }
86 }
87}
88
7ac4bfad
MG
89void change_properties(unsigned char *fw, int len, struct propaction *paction)
90{
0a465cc0 91 struct file_entry *fent;
7ac4bfad
MG
92 struct propaction *cpaction;
93
0a465cc0 94 for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
7ac4bfad
MG
95 cpaction = paction;
96 while (cpaction != NULL) {
0a465cc0 97 if (FINDSTR(fent->name, cpaction->property)) {
7ac4bfad
MG
98 break;
99 }
100 cpaction = cpaction->next;
101 }
102 if (cpaction != NULL) {
103 struct properties *prop;
7ac4bfad 104
0a465cc0 105 prop = (struct properties*)fent->start;
7ac4bfad
MG
106
107 if (prop->magic != 0x83011111) {
108 continue;
109 }
110
111 if (cpaction->action & (PROP_ACTION_TRUE|PROP_ACTION_FALSE)) {
112 if (prop->type1 == 0x01 && prop->type2 == 0x01) {
113 if (cpaction->action & PROP_ACTION_TRUE) {
114 if (*prop->val == 0x00) {
115 *prop->val = 0x01;
116 cpaction->status |= PROP_STATUS_SUCCESS;
117 } else {
118 cpaction->status |= PROP_STATUS_SAMEVAL;
119 }
120 } else {
121 if (*prop->val == 0x01) {
122 *prop->val = 0x00;
123 cpaction->status |= PROP_STATUS_SUCCESS;
124 } else {
125 cpaction->status |= PROP_STATUS_SAMEVAL;
126 }
127 }
128 } else {
129 cpaction->status = PROP_STATUS_WRONGTYPE;
130 }
131 }
132 if (cpaction->action & PROP_ACTION_RW) {
133 if (prop->right_rw == 0x00 && prop->rw_mask == 0x00) {
134 prop->right_rw = 0x01;
135 prop->rw_mask = 0x02;
136 cpaction->status |= PROP_STATUS_SUCCESS;
137 } else {
138 cpaction->status |= PROP_STATUS_WRONGRIGHTS;
139 }
140 }
141 if (cpaction->action & PROP_ACTION_RO) {
142 if (prop->right_rw == 0x01 && prop->rw_mask == 0x02) {
143 prop->right_rw = 0x00;
144 prop->rw_mask = 0x00;
145 cpaction->status |= PROP_STATUS_SUCCESS;
146 } else {
147 cpaction->status |= PROP_STATUS_WRONGRIGHTS;
148 }
149 }
150 }
151 }
152}
153
68fc92b2
MG
154#define BD_SERIAL1 0x14,0x02
155#define BD_ICMB 0x14,0x04
156#define BD_LAN 0x14,0x08
157#define BD_SERIAL2 0x14,0x10
158#define BD_SERIAL3 0x14,0x20
159#define BD_USB 0x14,0x40
160#define BD_PCI 0x15,0x03
161#define BD_LPC 0x15,0x04
162#define BD_VGA 0x15,0x08
163#define BD_BATTERY 0x15,0x10
164#define BD_ACDC 0x15,0x20
165#define BD_STANDBY 0x15,0x40
166#define BD_POWERCONN 0x15,0x70
167#define BD_DVI 0x15,0x80
168#define BD_PWRATX 0x16,0x01
169#define BD_PWRRELAY 0x16,0x02
170#define BD_PS2A 0x19,0xff
171
172#define MAGIC(fn, args...) fn(args)
173
174#define _BD_IS_SET(bd, byte, bits) (bd[byte] & bits)
175#define BD_IS_SET(bd, ident) MAGIC(_BD_IS_SET, bd, BD_##ident)
176#define BD_TEXT(bd, ident) (BD_IS_SET(bd, ident) ? "TRUE" : "FALSE")
177
178#define _BD_SET(bd, byte, bits) (bd[byte] |= bits)
179#define BD_SET(bd, ident) MAGIC(_BD_SET, bd, BD_##ident)
180
453260c6
MG
181void print_boarddescription(unsigned char *bd)
182{
183 int j;
184
185 for (j = 0; j < 32; j++) {
186 printf("%02x ", *(bd+j));
187 }
188 printf("\n");
f370a858
MG
189
190 /* com/agilent/rmc/amr/AmrMaster.class
191 * com/agilent/rmc/mgui/RmcPanel.class
192 * com/agilent/rmc/mgui/panels/AvrManualConfig.class
193 * com/agilent/rmc/mgui/panels/CardConf.jad
194 * com/agilent/rmc/mgui/panels/PowerMgmtConf.jad
195 * com/agilent/rmc/mgui/panels/RemoteDiskConf.jad
196 */
68fc92b2
MG
197 printf("\tserial1Present\t\t: %s\n", BD_TEXT(bd, SERIAL1));
198 printf("\ticmbPresent\t\t: %s\n", BD_TEXT(bd, ICMB));
199 printf("\tlanPresent\t\t: %s\n", BD_TEXT(bd, LAN));
200 printf("\tserial2Present\t\t: %s\n", BD_TEXT(bd, SERIAL2));
201 printf("\tserial3Present\t\t: %s\n", BD_TEXT(bd, SERIAL3));
202 printf("\tusbPresent\t\t: %s\n", BD_TEXT(bd, USB));
203 printf("\tpciPresent\t\t: %s\n", BD_TEXT(bd, PCI));
204 printf("\tlpcPresent\t\t: %s\n", BD_TEXT(bd, LPC));
205 printf("\tvgaPresent\t\t: %s\n", BD_TEXT(bd, VGA));
206 printf("\tbatteryPresent\t\t: %s\n", BD_TEXT(bd, BATTERY));
207 printf("\tacdcPresent\t\t: %s\n", BD_TEXT(bd, ACDC));
208 printf("\tstandbyPresent\t\t: %s\n", BD_TEXT(bd, STANDBY));
209 printf("\thasPowerConnectors\t: %s\n", BD_TEXT(bd, POWERCONN));
210 printf("\tdviPresent\t\t: %s\n", BD_TEXT(bd, DVI));
211 printf("\tpowerSwitchATX\t\t: %s\n", BD_TEXT(bd, PWRATX));
212 printf("\tpowerSwitchRelay\t: %s\n", BD_TEXT(bd, PWRRELAY));
f370a858 213 /* 22 & 4 */
68fc92b2 214 printf("\tps2aPresent\t\t: %s\n", BD_TEXT(bd, PS2A));
453260c6
MG
215}
216
7f88d2b6
MG
217void handle_boarddescription(unsigned char *fw, int len, int patch)
218{
0a465cc0
MG
219 struct file_entry *fent;
220 unsigned char *pos;
453260c6 221
0a465cc0
MG
222 for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
223 if (!strcmp(fent->name, "pdata"))
224 break;
225 }
453260c6 226
0a465cc0
MG
227 if (fent == NULL) {
228 fprintf(stderr, "pdata file not found, aborting!\n");
229 exit(1);
230 }
453260c6 231
453260c6 232
0a465cc0
MG
233 pos = fent->start;
234 /* MAGIC? */
235 if (*((unsigned int*)pos) != 0x00002802) {
236 fprintf(stderr, "pdata magic does not match, aborting!\n");
237 exit(1);
238 }
453260c6 239
0a465cc0 240 pos += 26;
453260c6 241
0a465cc0
MG
242 /* MAGIC2? */
243 if (*((unsigned int*)pos) != 0x00500101) {
244 fprintf(stderr, "pdata magic2 does not match, aborting!\n");
245 exit(1);
246 }
247
248 if (patch) {
249 /* Enable ATX and relay power switching */
250 BD_SET(pos, PWRATX);
251 BD_SET(pos, PWRRELAY);
453260c6 252 }
0a465cc0
MG
253
254 printf("0x%08x: BOARD_DESCRIPTION: ", fent->start - fw);
255 print_boarddescription(pos);
7f88d2b6
MG
256}
257
7ac4bfad
MG
258void syntax(char *name)
259{
260 fprintf(stderr,"Syntax: %s parameters firmware.bin\n", name);
261 fprintf(stderr,"parameters as follows:\n");
505ad254
MG
262 fprintf(stderr,"\t-d\t\t\tdisplay all properties of the image\n");
263 fprintf(stderr,"\t-u\t\t\tupdate checksum of the image\n");
264 fprintf(stderr,"\t-b\t\t\tmodify BOARD_DESCRIPTION for more power-switch options\n");
265 fprintf(stderr,"\t-e\t\t\textract files in firmware\n");
e5be7964 266 fprintf(stderr,"\t-l\t\t\tlist files in firmware\n");
505ad254
MG
267 fprintf(stderr,"\t-t property\t\tset 'property' to true\n");
268 fprintf(stderr,"\t-f property\t\tset 'property' to false\n");
269 fprintf(stderr,"\t-w property\t\tallow read-write access to 'property'\n");
270 fprintf(stderr,"\t-r property\t\tallow read-only access to 'property'\n");
271 fprintf(stderr,"\t-x fw_file=local_file\treplace or add fw_file with content of local_file\n");
7ac4bfad
MG
272 exit(1);
273}
274
275void add_action(int opt, char *optarg, struct propaction **paction) {
276 struct propaction *pos = *paction;
277 struct propaction *prev = NULL;
278
279 while (pos != NULL) {
280 if (!strcmp(pos->property, optarg))
281 break;
282 prev = pos;
283 pos = pos->next;
284 }
285
286 if (pos == NULL) {
287 pos = malloc(sizeof(struct propaction));
288 if (pos == NULL) {
289 perror("malloc");
290 exit(1);
291 }
292 bzero(pos, sizeof(struct propaction));
293 pos->property = optarg;
294
295 if (prev == NULL) {
296 *paction = pos;
297 } else {
298 prev->next = pos;
299 }
300 }
301
302 switch(opt) {
303 case 't':
304 if (pos->action & PROP_ACTION_FALSE) {
305 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
306 exit(1);
307 }
308 pos->action |= PROP_ACTION_TRUE;
309 break;
310 case 'f':
311 if (pos->action & PROP_ACTION_TRUE) {
312 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
313 exit(1);
314 }
315 pos->action |= PROP_ACTION_FALSE;
316 break;
317 case 'w':
318 if (pos->action & PROP_ACTION_RO) {
319 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
320 exit(1);
321 }
322 pos->action |= PROP_ACTION_RW;
323 break;
324 case 'r':
325 if (pos->action & PROP_ACTION_RW) {
326 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
327 exit(1);
328 }
329 pos->action |= PROP_ACTION_RO;
330 break;
331 }
332}
333
334int check_crc(unsigned char *fw, int len)
335{
336 int ret;
337 unsigned int crc, oldcrc;
338
339 ret = rsb_crc2(fw, len, 0x55335053, &crc);
340 oldcrc = (unsigned int)*((unsigned int*)(fw + len - 4));
341
342 printf("Checksum: 0x%08x (%s), should be: 0x%08x\n",
343 crc,
344 (ret ? "NOT OK" : "OK"),
345 oldcrc);
346
347 return ret;
348}
349
b5163e0d 350void check_image(unsigned char *fw, int len)
0a465cc0
MG
351{
352 struct file_entry *fent;
353 char *last_name = NULL;
354
b5163e0d 355 /* get_next_file will abort on error */
0a465cc0
MG
356 fent = get_next_file(fw, len);
357 while (fent != NULL) {
358 last_name = fent->name;
359 fent = get_next_file(NULL, 0);
360 }
0a465cc0
MG
361}
362
972ac24b
MG
363int main(int argc, char **argv)
364{
365 struct stat statbuf;
7ac4bfad 366 char *file = NULL;
972ac24b
MG
367 unsigned char *fw;
368 int fd;
369 int remaining;
370 int ret;
7ac4bfad
MG
371 int opt;
372 unsigned int crc;
373 struct propaction *paction = NULL;
374 int showall = 0;
375 int update_crc = 0;
453260c6 376 int patch_bd = 0;
7ac4bfad 377 int patch_fw = 0;
14ff7444 378 int extract = 0;
e5be7964 379 int list = 0;
972ac24b 380
7ac4bfad
MG
381 if (argc < 2)
382 syntax(argv[0]);
383
e5be7964 384 while ((opt = getopt(argc, argv, "dubelt:f:w:r:x:")) != -1) {
7ac4bfad
MG
385 switch(opt) {
386 case 'd':
387 showall = 1;
388 break;
389 case 'u':
390 update_crc = 1;
391 break;
453260c6
MG
392 case 'b':
393 patch_bd = 1;
394 break;
14ff7444
MG
395 case 'e':
396 extract = 1;
397 break;
e5be7964
MG
398 case 'l':
399 list = 1;
400 break;
7ac4bfad
MG
401 case 't':
402 case 'f':
403 case 'w':
404 case 'r':
405 patch_fw = 1;
406 add_action(opt, optarg, &paction);
407 break;
505ad254
MG
408 case 'x':
409 replace_add_file(NULL, 0, NULL, NULL);
410 break;
7ac4bfad
MG
411 default:
412 syntax(argv[0]);
413 }
972ac24b
MG
414 }
415
7ac4bfad
MG
416 if (argc > optind) {
417 file = argv[optind];
418 } else {
419 syntax(argv[0]);
420 }
421
422 if (stat(file, &statbuf) == -1) {
423 fprintf(stderr,"%s: ", file);
972ac24b
MG
424 perror("stat");
425 exit(1);
426 }
427
7ac4bfad
MG
428 if ((fd = open(file, O_RDONLY)) == -1) {
429 fprintf(stderr,"%s: ", file);
972ac24b
MG
430 perror("open");
431 exit(1);
432 }
433
434 if ((fw = malloc(statbuf.st_size)) == NULL) {
435 perror("malloc");
436 exit(1);
437 }
438
439 bzero(fw, statbuf.st_size);
440
441 remaining = statbuf.st_size;
442
443 while(remaining) {
444 if ((ret = read(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
445 perror("read");
446 exit(1);
447 }
448 remaining -= ret;
449 }
7ac4bfad 450 close(fd);
972ac24b 451
7ac4bfad
MG
452 ret = check_crc(fw, statbuf.st_size);
453 if ((ret != 0) && (!update_crc)) {
454 fprintf(stderr,"Checksum incorrect, aborting...\n");
9eb4b3c6 455 exit(1);
7ac4bfad 456 }
972ac24b 457
b5163e0d 458 check_image(fw, statbuf.st_size - 4);
0a465cc0 459
7ac4bfad
MG
460 if (patch_fw) {
461 struct propaction *cpaction = paction;
462
b5163e0d 463 change_properties(fw, statbuf.st_size - 4, paction);
7ac4bfad
MG
464
465 printf("\nProperty change results:\n");
466 while(cpaction != NULL) {
467 printf("%s: ", cpaction->property);
468
469 if (cpaction->status == PROP_STATUS_NOTFOUND)
470 printf("NOTFOUND ");
471 if (cpaction->status & PROP_STATUS_SUCCESS)
472 printf("SUCCESS ");
473 if (cpaction->status & PROP_STATUS_SAMEVAL)
474 printf("SAMEVAL ");
475 if (cpaction->status & PROP_STATUS_WRONGTYPE)
476 printf("WRONGTYPE ");
477 if (cpaction->status & PROP_STATUS_WRONGRIGHTS)
478 printf("WRONGRIGHTS ");
479 printf("\n");
480
481 cpaction = cpaction->next;
7f88d2b6 482 }
7ac4bfad 483 printf("\n");
7f88d2b6 484 }
972ac24b 485
453260c6 486 if (patch_bd) {
b5163e0d 487 handle_boarddescription(fw, statbuf.st_size - 4, 1);
453260c6
MG
488 }
489
490 if (showall) {
7ac4bfad 491 show_properties(fw, statbuf.st_size - 4);
b5163e0d 492 handle_boarddescription(fw, statbuf.st_size - 4, 0);
e5be7964
MG
493 }
494
495 if (list) {
496 list_files(fw, statbuf.st_size - 4);
14ff7444
MG
497 }
498
499 if (extract) {
1f1fa7b6 500 extract_files(fw, statbuf.st_size - 4);
453260c6 501 }
7ac4bfad 502
453260c6 503 if (update_crc || patch_fw || patch_bd) {
7ac4bfad
MG
504 ret = rsb_crc2(fw, statbuf.st_size, 0x55335053, &crc);
505 if (ret == 4) {
506 *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
507 }
508
b5163e0d 509 check_image(fw, statbuf.st_size-4);
79234162 510
7ac4bfad
MG
511 if (check_crc(fw, statbuf.st_size) == 0) {
512 char *newfile;
513
514 newfile = malloc(strlen(file) + strlen(".patched") + 1);
515 if (newfile == NULL) {
516 perror("malloc");
517 exit(1);
518 }
519 strcpy(newfile, file);
520 strcat(newfile, ".patched");
521
522 printf("Writing %s\n", newfile);
523 if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
7fe9e3fd 524 fprintf(stderr,"%s: ", newfile);
7ac4bfad
MG
525 perror("open");
526 exit(1);
527 }
528
529 remaining = statbuf.st_size;
530
531 while(remaining) {
532 if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
533 perror("write");
534 exit(1);
535 }
536 remaining -= ret;
537 }
538 close(fd);
539 } else {
540 fprintf(stderr,"Can't set correct checksum, aborting...\n");
541 }
542 }
543
972ac24b
MG
544 exit(0);
545}
Impressum, Datenschutz