]> git.zerfleddert.de Git - rsbs2/blob - firmware.c
0595bd9645ed973ad0ee01463c06f61de534dcc7
[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 #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
182 void 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");
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 */
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));
214 /* 22 & 4 */
215 printf("\tps2aPresent\t\t: %s\n", BD_TEXT(bd, PS2A));
216 }
217
218 void handle_boarddescription(unsigned char *fw, int len, int patch)
219 {
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
238 if (patch) {
239 /* Enable ATX and relay power switching */
240 BD_SET(pos, PWRATX);
241 BD_SET(pos, PWRRELAY);
242 /* Serial */
243 BD_SET(pos, SERIAL2);
244 /* ATX standby */
245 BD_SET(pos, STANDBY);
246 }
247 printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
248 print_boarddescription(pos);
249
250 break;
251 }
252 }
253 }
254
255 void syntax(char *name)
256 {
257 fprintf(stderr,"Syntax: %s parameters firmware.bin\n", name);
258 fprintf(stderr,"parameters as follows:\n");
259 fprintf(stderr,"\t-d\t\tdisplay all properties of the image\n");
260 fprintf(stderr,"\t-u\t\tupdate checksum of the image\n");
261 fprintf(stderr,"\t-b\t\tmodify BOARD_DESCRIPTION for more power-switch options\n");
262 fprintf(stderr,"\t-t property\tset 'property' to true\n");
263 fprintf(stderr,"\t-f property\tset 'property' to false\n");
264 fprintf(stderr,"\t-w property\tallow read-write access to 'property'\n");
265 fprintf(stderr,"\t-r property\tallow read-only access to 'property'\n");
266 exit(1);
267 }
268
269 void add_action(int opt, char *optarg, struct propaction **paction) {
270 struct propaction *pos = *paction;
271 struct propaction *prev = NULL;
272
273 while (pos != NULL) {
274 if (!strcmp(pos->property, optarg))
275 break;
276 prev = pos;
277 pos = pos->next;
278 }
279
280 if (pos == NULL) {
281 pos = malloc(sizeof(struct propaction));
282 if (pos == NULL) {
283 perror("malloc");
284 exit(1);
285 }
286 bzero(pos, sizeof(struct propaction));
287 pos->property = optarg;
288
289 if (prev == NULL) {
290 *paction = pos;
291 } else {
292 prev->next = pos;
293 }
294 }
295
296 switch(opt) {
297 case 't':
298 if (pos->action & PROP_ACTION_FALSE) {
299 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
300 exit(1);
301 }
302 pos->action |= PROP_ACTION_TRUE;
303 break;
304 case 'f':
305 if (pos->action & PROP_ACTION_TRUE) {
306 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
307 exit(1);
308 }
309 pos->action |= PROP_ACTION_FALSE;
310 break;
311 case 'w':
312 if (pos->action & PROP_ACTION_RO) {
313 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
314 exit(1);
315 }
316 pos->action |= PROP_ACTION_RW;
317 break;
318 case 'r':
319 if (pos->action & PROP_ACTION_RW) {
320 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
321 exit(1);
322 }
323 pos->action |= PROP_ACTION_RO;
324 break;
325 }
326 }
327
328 int check_crc(unsigned char *fw, int len)
329 {
330 int ret;
331 unsigned int crc, oldcrc;
332
333 ret = rsb_crc2(fw, len, 0x55335053, &crc);
334 oldcrc = (unsigned int)*((unsigned int*)(fw + len - 4));
335
336 printf("Checksum: 0x%08x (%s), should be: 0x%08x\n",
337 crc,
338 (ret ? "NOT OK" : "OK"),
339 oldcrc);
340
341 return ret;
342 }
343
344 int main(int argc, char **argv)
345 {
346 struct stat statbuf;
347 char *file = NULL;
348 unsigned char *fw;
349 int fd;
350 int remaining;
351 int ret;
352 int opt;
353 unsigned int crc;
354 struct propaction *paction = NULL;
355 int showall = 0;
356 int update_crc = 0;
357 int patch_bd = 0;
358 int patch_fw = 0;
359
360 if (argc < 2)
361 syntax(argv[0]);
362
363 while ((opt = getopt(argc, argv, "dubt:f:w:r:")) != -1) {
364 switch(opt) {
365 case 'd':
366 showall = 1;
367 break;
368 case 'u':
369 update_crc = 1;
370 break;
371 case 'b':
372 patch_bd = 1;
373 break;
374 case 't':
375 case 'f':
376 case 'w':
377 case 'r':
378 patch_fw = 1;
379 add_action(opt, optarg, &paction);
380 break;
381 default:
382 syntax(argv[0]);
383 }
384 }
385
386 if (argc > optind) {
387 file = argv[optind];
388 } else {
389 syntax(argv[0]);
390 }
391
392 if (stat(file, &statbuf) == -1) {
393 fprintf(stderr,"%s: ", file);
394 perror("stat");
395 exit(1);
396 }
397
398 if ((fd = open(file, O_RDONLY)) == -1) {
399 fprintf(stderr,"%s: ", file);
400 perror("open");
401 exit(1);
402 }
403
404 if ((fw = malloc(statbuf.st_size)) == NULL) {
405 perror("malloc");
406 exit(1);
407 }
408
409 bzero(fw, statbuf.st_size);
410
411 remaining = statbuf.st_size;
412
413 while(remaining) {
414 if ((ret = read(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
415 perror("read");
416 exit(1);
417 }
418 remaining -= ret;
419 }
420 close(fd);
421
422 ret = check_crc(fw, statbuf.st_size);
423 if ((ret != 0) && (!update_crc)) {
424 fprintf(stderr,"Checksum incorrect, aborting...\n");
425 }
426
427 if (patch_fw) {
428 struct propaction *cpaction = paction;
429
430 change_properties(fw, statbuf.st_size, paction);
431
432 printf("\nProperty change results:\n");
433 while(cpaction != NULL) {
434 printf("%s: ", cpaction->property);
435
436 if (cpaction->status == PROP_STATUS_NOTFOUND)
437 printf("NOTFOUND ");
438 if (cpaction->status & PROP_STATUS_SUCCESS)
439 printf("SUCCESS ");
440 if (cpaction->status & PROP_STATUS_SAMEVAL)
441 printf("SAMEVAL ");
442 if (cpaction->status & PROP_STATUS_WRONGTYPE)
443 printf("WRONGTYPE ");
444 if (cpaction->status & PROP_STATUS_WRONGRIGHTS)
445 printf("WRONGRIGHTS ");
446 printf("\n");
447
448 cpaction = cpaction->next;
449 }
450 printf("\n");
451 }
452
453 if (patch_bd) {
454 handle_boarddescription(fw, statbuf.st_size -4, 1);
455 }
456
457 if (showall) {
458 show_properties(fw, statbuf.st_size - 4);
459 handle_boarddescription(fw, statbuf.st_size -4, 0);
460 }
461
462 if (update_crc || patch_fw || patch_bd) {
463 ret = rsb_crc2(fw, statbuf.st_size, 0x55335053, &crc);
464 if (ret == 4) {
465 *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
466 }
467
468 if (check_crc(fw, statbuf.st_size) == 0) {
469 char *newfile;
470
471 newfile = malloc(strlen(file) + strlen(".patched") + 1);
472 if (newfile == NULL) {
473 perror("malloc");
474 exit(1);
475 }
476 strcpy(newfile, file);
477 strcat(newfile, ".patched");
478
479 printf("Writing %s\n", newfile);
480 if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
481 fprintf(stderr,"%s: ", file);
482 perror("open");
483 exit(1);
484 }
485
486 remaining = statbuf.st_size;
487
488 while(remaining) {
489 if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
490 perror("write");
491 exit(1);
492 }
493 remaining -= ret;
494 }
495 close(fd);
496 } else {
497 fprintf(stderr,"Can't set correct checksum, aborting...\n");
498 }
499 }
500
501 exit(0);
502 }
Impressum, Datenschutz