]> cvs.zerfleddert.de Git - rsbs2/blob - firmware.c
human readable output of BOARD_DESCRIPTION for known fields
[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 void print_boarddescription(unsigned char *bd)
156 {
157 int j;
158
159 for (j = 0; j < 32; j++) {
160 printf("%02x ", *(bd+j));
161 }
162 printf("\n");
163
164 /* com/agilent/rmc/amr/AmrMaster.class
165 * com/agilent/rmc/mgui/RmcPanel.class
166 * com/agilent/rmc/mgui/panels/AvrManualConfig.class
167 * com/agilent/rmc/mgui/panels/CardConf.jad
168 * com/agilent/rmc/mgui/panels/PowerMgmtConf.jad
169 * com/agilent/rmc/mgui/panels/RemoteDiskConf.jad
170 */
171 printf("\tserial1Present\t\t: %s\n", ((bd[20] & 2) ? "TRUE" : "FALSE"));
172 printf("\ticmbPresent\t\t: %s\n", ((bd[20] & 4) ? "TRUE" : "FALSE"));
173 printf("\tlanPresent\t\t: %s\n", ((bd[20] & 8) ? "TRUE" : "FALSE"));
174 printf("\tserial2Present\t\t: %s\n", ((bd[20] & 0x10) ? "TRUE" : "FALSE"));
175 printf("\tserial3Present\t\t: %s\n", ((bd[20] & 0x20) ? "TRUE" : "FALSE"));
176 printf("\tusbPresent\t\t: %s\n", ((bd[20] & 0x40) ? "TRUE" : "FALSE"));
177 printf("\tpciPresent\t\t: %s\n", ((bd[21] & 3) ? "TRUE" : "FALSE"));
178 printf("\tlpcPresent\t\t: %s\n", ((bd[21] & 4) ? "TRUE" : "FALSE"));
179 printf("\tvgaPresent\t\t: %s\n", ((bd[21] & 8) ? "TRUE" : "FALSE"));
180 printf("\tbatteryPresent\t\t: %s\n", ((bd[21] & 0x10) ? "TRUE" : "FALSE"));
181 printf("\tacdcPresent\t\t: %s\n", ((bd[21] & 0x20) ? "TRUE" : "FALSE"));
182 printf("\tstandbyPresent\t\t: %s\n", ((bd[21] & 0x40) ? "TRUE" : "FALSE"));
183 printf("\thasPowerConnectors\t: %s\n", ((bd[21] & 0x70) ? "TRUE" : "FALSE"));
184 printf("\tdviPresent\t\t: %s\n", ((bd[21] & 0x80) ? "TRUE" : "FALSE"));
185 printf("\tpowerSwitchATX\t\t: %s\n", ((bd[22] & 1) ? "TRUE" : "FALSE"));
186 printf("\tpowerSwitchRelay\t: %s\n", ((bd[22] & 2) ? "TRUE" : "FALSE"));
187 /* 22 & 4 */
188 printf("\tps2aPresent\t\t: %s\n", ((bd[25]) ? "TRUE" : "FALSE"));
189 }
190
191 void handle_boarddescription(unsigned char *fw, int len, int patch)
192 {
193 int i;
194
195 for (i = len - (strlen("pdata")+1); i > 0; i--) {
196 if (FINDSTR(fw+i, "pdata")) {
197 unsigned char *pos = fw + i + strlen("pdata") + 1;
198
199 /* MAGIC? */
200 if (*((unsigned int*)pos) != 0x00002802) {
201 continue;
202 }
203
204 pos += 26;
205
206 /* MAGIC2? */
207 if (*((unsigned int*)pos) != 0x00500101) {
208 continue;
209 }
210
211 printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
212 print_boarddescription(pos);
213
214 if (patch) {
215 /* Enable ATX and relay power switching */
216 pos[22] |= 0x03;
217 printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
218 print_boarddescription(pos);
219 }
220
221 break;
222 }
223 }
224 }
225
226 void syntax(char *name)
227 {
228 fprintf(stderr,"Syntax: %s parameters firmware.bin\n", name);
229 fprintf(stderr,"parameters as follows:\n");
230 fprintf(stderr,"\t-d\t\tdisplay all properties of the image\n");
231 fprintf(stderr,"\t-u\t\tupdate checksum of the image\n");
232 fprintf(stderr,"\t-b\t\tmodify BOARD_DESCRIPTION for more power-switch options\n");
233 fprintf(stderr,"\t-t property\tset 'property' to true\n");
234 fprintf(stderr,"\t-f property\tset 'property' to false\n");
235 fprintf(stderr,"\t-w property\tallow read-write access to 'property'\n");
236 fprintf(stderr,"\t-r property\tallow read-only access to 'property'\n");
237 exit(1);
238 }
239
240 void add_action(int opt, char *optarg, struct propaction **paction) {
241 struct propaction *pos = *paction;
242 struct propaction *prev = NULL;
243
244 while (pos != NULL) {
245 if (!strcmp(pos->property, optarg))
246 break;
247 prev = pos;
248 pos = pos->next;
249 }
250
251 if (pos == NULL) {
252 pos = malloc(sizeof(struct propaction));
253 if (pos == NULL) {
254 perror("malloc");
255 exit(1);
256 }
257 bzero(pos, sizeof(struct propaction));
258 pos->property = optarg;
259
260 if (prev == NULL) {
261 *paction = pos;
262 } else {
263 prev->next = pos;
264 }
265 }
266
267 switch(opt) {
268 case 't':
269 if (pos->action & PROP_ACTION_FALSE) {
270 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
271 exit(1);
272 }
273 pos->action |= PROP_ACTION_TRUE;
274 break;
275 case 'f':
276 if (pos->action & PROP_ACTION_TRUE) {
277 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
278 exit(1);
279 }
280 pos->action |= PROP_ACTION_FALSE;
281 break;
282 case 'w':
283 if (pos->action & PROP_ACTION_RO) {
284 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
285 exit(1);
286 }
287 pos->action |= PROP_ACTION_RW;
288 break;
289 case 'r':
290 if (pos->action & PROP_ACTION_RW) {
291 fprintf(stderr,"inconsistent requests for %s\n",pos->property);
292 exit(1);
293 }
294 pos->action |= PROP_ACTION_RO;
295 break;
296 }
297 }
298
299 int check_crc(unsigned char *fw, int len)
300 {
301 int ret;
302 unsigned int crc, oldcrc;
303
304 ret = rsb_crc2(fw, len, 0x55335053, &crc);
305 oldcrc = (unsigned int)*((unsigned int*)(fw + len - 4));
306
307 printf("Checksum: 0x%08x (%s), should be: 0x%08x\n",
308 crc,
309 (ret ? "NOT OK" : "OK"),
310 oldcrc);
311
312 return ret;
313 }
314
315 int main(int argc, char **argv)
316 {
317 struct stat statbuf;
318 char *file = NULL;
319 unsigned char *fw;
320 int fd;
321 int remaining;
322 int ret;
323 int opt;
324 unsigned int crc;
325 struct propaction *paction = NULL;
326 int showall = 0;
327 int update_crc = 0;
328 int patch_bd = 0;
329 int patch_fw = 0;
330
331 if (argc < 2)
332 syntax(argv[0]);
333
334 while ((opt = getopt(argc, argv, "dubt:f:w:r:")) != -1) {
335 switch(opt) {
336 case 'd':
337 showall = 1;
338 break;
339 case 'u':
340 update_crc = 1;
341 break;
342 case 'b':
343 patch_bd = 1;
344 break;
345 case 't':
346 case 'f':
347 case 'w':
348 case 'r':
349 patch_fw = 1;
350 add_action(opt, optarg, &paction);
351 break;
352 default:
353 syntax(argv[0]);
354 }
355 }
356
357 if (argc > optind) {
358 file = argv[optind];
359 } else {
360 syntax(argv[0]);
361 }
362
363 if (stat(file, &statbuf) == -1) {
364 fprintf(stderr,"%s: ", file);
365 perror("stat");
366 exit(1);
367 }
368
369 if ((fd = open(file, O_RDONLY)) == -1) {
370 fprintf(stderr,"%s: ", file);
371 perror("open");
372 exit(1);
373 }
374
375 if ((fw = malloc(statbuf.st_size)) == NULL) {
376 perror("malloc");
377 exit(1);
378 }
379
380 bzero(fw, statbuf.st_size);
381
382 remaining = statbuf.st_size;
383
384 while(remaining) {
385 if ((ret = read(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
386 perror("read");
387 exit(1);
388 }
389 remaining -= ret;
390 }
391 close(fd);
392
393 ret = check_crc(fw, statbuf.st_size);
394 if ((ret != 0) && (!update_crc)) {
395 fprintf(stderr,"Checksum incorrect, aborting...\n");
396 }
397
398 if (patch_fw) {
399 struct propaction *cpaction = paction;
400
401 change_properties(fw, statbuf.st_size, paction);
402
403 printf("\nProperty change results:\n");
404 while(cpaction != NULL) {
405 printf("%s: ", cpaction->property);
406
407 if (cpaction->status == PROP_STATUS_NOTFOUND)
408 printf("NOTFOUND ");
409 if (cpaction->status & PROP_STATUS_SUCCESS)
410 printf("SUCCESS ");
411 if (cpaction->status & PROP_STATUS_SAMEVAL)
412 printf("SAMEVAL ");
413 if (cpaction->status & PROP_STATUS_WRONGTYPE)
414 printf("WRONGTYPE ");
415 if (cpaction->status & PROP_STATUS_WRONGRIGHTS)
416 printf("WRONGRIGHTS ");
417 printf("\n");
418
419 cpaction = cpaction->next;
420 }
421 printf("\n");
422 }
423
424 if (patch_bd) {
425 handle_boarddescription(fw, statbuf.st_size -4, 1);
426 }
427
428 if (showall) {
429 show_properties(fw, statbuf.st_size - 4);
430 handle_boarddescription(fw, statbuf.st_size -4, 0);
431 }
432
433 if (update_crc || patch_fw || patch_bd) {
434 ret = rsb_crc2(fw, statbuf.st_size, 0x55335053, &crc);
435 if (ret == 4) {
436 *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
437 }
438
439 if (check_crc(fw, statbuf.st_size) == 0) {
440 char *newfile;
441
442 newfile = malloc(strlen(file) + strlen(".patched") + 1);
443 if (newfile == NULL) {
444 perror("malloc");
445 exit(1);
446 }
447 strcpy(newfile, file);
448 strcat(newfile, ".patched");
449
450 printf("Writing %s\n", newfile);
451 if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
452 fprintf(stderr,"%s: ", file);
453 perror("open");
454 exit(1);
455 }
456
457 remaining = statbuf.st_size;
458
459 while(remaining) {
460 if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
461 perror("write");
462 exit(1);
463 }
464 remaining -= ret;
465 }
466 close(fd);
467 } else {
468 fprintf(stderr,"Can't set correct checksum, aborting...\n");
469 }
470 }
471
472 exit(0);
473 }
Impressum, Datenschutz