]> git.zerfleddert.de Git - rsbs2/blob - firmware.c
add stub for adding/replacing files in the firmware image
[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 #include "filesystem.h"
12
13 #define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str)))
14
15 struct 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
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
40 struct propaction {
41 char *property;
42 unsigned int action;
43 unsigned int status;
44 struct propaction *next;
45 };
46
47 void show_properties(unsigned char *fw, int len)
48 {
49 struct file_entry *fent;
50
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/")) {
55 struct properties *prop;
56
57 printf("0x%08x: found setting: %s ", fent->start - fw, fent->name);
58
59 prop = (struct properties*)fent->start;
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(", length: %d\n", fent->length);
85 }
86 }
87 }
88
89 void change_properties(unsigned char *fw, int len, struct propaction *paction)
90 {
91 struct file_entry *fent;
92 struct propaction *cpaction;
93
94 for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
95 cpaction = paction;
96 while (cpaction != NULL) {
97 if (FINDSTR(fent->name, cpaction->property)) {
98 break;
99 }
100 cpaction = cpaction->next;
101 }
102 if (cpaction != NULL) {
103 struct properties *prop;
104
105 prop = (struct properties*)fent->start;
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
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
181 void 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");
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 */
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));
213 /* 22 & 4 */
214 printf("\tps2aPresent\t\t: %s\n", BD_TEXT(bd, PS2A));
215 }
216
217 void handle_boarddescription(unsigned char *fw, int len, int patch)
218 {
219 struct file_entry *fent;
220 unsigned char *pos;
221
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 }
226
227 if (fent == NULL) {
228 fprintf(stderr, "pdata file not found, aborting!\n");
229 exit(1);
230 }
231
232
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 }
239
240 pos += 26;
241
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);
252 }
253
254 printf("0x%08x: BOARD_DESCRIPTION: ", fent->start - fw);
255 print_boarddescription(pos);
256 }
257
258 void syntax(char *name)
259 {
260 fprintf(stderr,"Syntax: %s parameters firmware.bin\n", name);
261 fprintf(stderr,"parameters as follows:\n");
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");
266 fprintf(stderr,"\t-t property\t\tset 'property' to true\n");
267 fprintf(stderr,"\t-f property\t\tset 'property' to false\n");
268 fprintf(stderr,"\t-w property\t\tallow read-write access to 'property'\n");
269 fprintf(stderr,"\t-r property\t\tallow read-only access to 'property'\n");
270 fprintf(stderr,"\t-x fw_file=local_file\treplace or add fw_file with content of local_file\n");
271 exit(1);
272 }
273
274 void add_action(int opt, char *optarg, struct propaction **paction) {
275 struct propaction *pos = *paction;
276 struct propaction *prev = NULL;
277
278 while (pos != NULL) {
279 if (!strcmp(pos->property, optarg))
280 break;
281 prev = pos;
282 pos = pos->next;
283 }
284
285 if (pos == NULL) {
286 pos = malloc(sizeof(struct propaction));
287 if (pos == NULL) {
288 perror("malloc");
289 exit(1);
290 }
291 bzero(pos, sizeof(struct propaction));
292 pos->property = optarg;
293
294 if (prev == NULL) {
295 *paction = pos;
296 } else {
297 prev->next = pos;
298 }
299 }
300
301 switch(opt) {
302 case 't':
303 if (pos->action & PROP_ACTION_FALSE) {
304 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
305 exit(1);
306 }
307 pos->action |= PROP_ACTION_TRUE;
308 break;
309 case 'f':
310 if (pos->action & PROP_ACTION_TRUE) {
311 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
312 exit(1);
313 }
314 pos->action |= PROP_ACTION_FALSE;
315 break;
316 case 'w':
317 if (pos->action & PROP_ACTION_RO) {
318 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
319 exit(1);
320 }
321 pos->action |= PROP_ACTION_RW;
322 break;
323 case 'r':
324 if (pos->action & PROP_ACTION_RW) {
325 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
326 exit(1);
327 }
328 pos->action |= PROP_ACTION_RO;
329 break;
330 }
331 }
332
333 int check_crc(unsigned char *fw, int len)
334 {
335 int ret;
336 unsigned int crc, oldcrc;
337
338 ret = rsb_crc2(fw, len, 0x55335053, &crc);
339 oldcrc = (unsigned int)*((unsigned int*)(fw + len - 4));
340
341 printf("Checksum: 0x%08x (%s), should be: 0x%08x\n",
342 crc,
343 (ret ? "NOT OK" : "OK"),
344 oldcrc);
345
346 return ret;
347 }
348
349 void check_image(unsigned char *fw, int len)
350 {
351 struct file_entry *fent;
352 char *last_name = NULL;
353
354 /* get_next_file will abort on error */
355 fent = get_next_file(fw, len);
356 while (fent != NULL) {
357 last_name = fent->name;
358 fent = get_next_file(NULL, 0);
359 }
360 }
361
362 int main(int argc, char **argv)
363 {
364 struct stat statbuf;
365 char *file = NULL;
366 unsigned char *fw;
367 int fd;
368 int remaining;
369 int ret;
370 int opt;
371 unsigned int crc;
372 struct propaction *paction = NULL;
373 int showall = 0;
374 int update_crc = 0;
375 int patch_bd = 0;
376 int patch_fw = 0;
377 int extract = 0;
378
379 if (argc < 2)
380 syntax(argv[0]);
381
382 while ((opt = getopt(argc, argv, "dubet:f:w:r:x:")) != -1) {
383 switch(opt) {
384 case 'd':
385 showall = 1;
386 break;
387 case 'u':
388 update_crc = 1;
389 break;
390 case 'b':
391 patch_bd = 1;
392 break;
393 case 'e':
394 extract = 1;
395 break;
396 case 't':
397 case 'f':
398 case 'w':
399 case 'r':
400 patch_fw = 1;
401 add_action(opt, optarg, &paction);
402 break;
403 case 'x':
404 replace_add_file(NULL, 0, NULL, NULL);
405 break;
406 default:
407 syntax(argv[0]);
408 }
409 }
410
411 if (argc > optind) {
412 file = argv[optind];
413 } else {
414 syntax(argv[0]);
415 }
416
417 if (stat(file, &statbuf) == -1) {
418 fprintf(stderr,"%s: ", file);
419 perror("stat");
420 exit(1);
421 }
422
423 if ((fd = open(file, O_RDONLY)) == -1) {
424 fprintf(stderr,"%s: ", file);
425 perror("open");
426 exit(1);
427 }
428
429 if ((fw = malloc(statbuf.st_size)) == NULL) {
430 perror("malloc");
431 exit(1);
432 }
433
434 bzero(fw, statbuf.st_size);
435
436 remaining = statbuf.st_size;
437
438 while(remaining) {
439 if ((ret = read(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
440 perror("read");
441 exit(1);
442 }
443 remaining -= ret;
444 }
445 close(fd);
446
447 ret = check_crc(fw, statbuf.st_size);
448 if ((ret != 0) && (!update_crc)) {
449 fprintf(stderr,"Checksum incorrect, aborting...\n");
450 exit(1);
451 }
452
453 check_image(fw, statbuf.st_size - 4);
454
455 if (patch_fw) {
456 struct propaction *cpaction = paction;
457
458 change_properties(fw, statbuf.st_size - 4, paction);
459
460 printf("\nProperty change results:\n");
461 while(cpaction != NULL) {
462 printf("%s: ", cpaction->property);
463
464 if (cpaction->status == PROP_STATUS_NOTFOUND)
465 printf("NOTFOUND ");
466 if (cpaction->status & PROP_STATUS_SUCCESS)
467 printf("SUCCESS ");
468 if (cpaction->status & PROP_STATUS_SAMEVAL)
469 printf("SAMEVAL ");
470 if (cpaction->status & PROP_STATUS_WRONGTYPE)
471 printf("WRONGTYPE ");
472 if (cpaction->status & PROP_STATUS_WRONGRIGHTS)
473 printf("WRONGRIGHTS ");
474 printf("\n");
475
476 cpaction = cpaction->next;
477 }
478 printf("\n");
479 }
480
481 if (patch_bd) {
482 handle_boarddescription(fw, statbuf.st_size - 4, 1);
483 }
484
485 if (showall) {
486 show_properties(fw, statbuf.st_size - 4);
487 handle_boarddescription(fw, statbuf.st_size - 4, 0);
488 }
489
490 if (extract) {
491 extract_files(fw, statbuf.st_size - 4);
492 }
493
494 if (update_crc || patch_fw || patch_bd) {
495 ret = rsb_crc2(fw, statbuf.st_size, 0x55335053, &crc);
496 if (ret == 4) {
497 *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
498 }
499
500 check_image(fw, statbuf.st_size-4);
501
502 if (check_crc(fw, statbuf.st_size) == 0) {
503 char *newfile;
504
505 newfile = malloc(strlen(file) + strlen(".patched") + 1);
506 if (newfile == NULL) {
507 perror("malloc");
508 exit(1);
509 }
510 strcpy(newfile, file);
511 strcat(newfile, ".patched");
512
513 printf("Writing %s\n", newfile);
514 if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
515 fprintf(stderr,"%s: ", newfile);
516 perror("open");
517 exit(1);
518 }
519
520 remaining = statbuf.st_size;
521
522 while(remaining) {
523 if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
524 perror("write");
525 exit(1);
526 }
527 remaining -= ret;
528 }
529 close(fd);
530 } else {
531 fprintf(stderr,"Can't set correct checksum, aborting...\n");
532 }
533 }
534
535 exit(0);
536 }
Impressum, Datenschutz