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