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