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