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