]>
cvs.zerfleddert.de Git - rsbs2/blob - src/firmware.c
12 #include "filesystem.h"
14 #define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str)))
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)
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)
45 struct propaction
*next
;
48 void show_properties(uint8_t *fw
, int32_t len
)
50 struct file_entry
*fent
;
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/")) {
56 struct properties
*prop
;
58 printf("0x%08x: found setting: %s ", (uint32_t)(fent
->start
- fw
), fent
->name
);
60 prop
= (struct properties
*)fent
->start
;
62 if (prop
->magic
!= 0x83011111) {
63 printf("ignoring...\n");
67 if (prop
->type1
== 0x00 && prop
->type2
== 0x04) {
68 printf("STRING: '%s' ", prop
->val
);
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) {
74 memcpy(&val
, prop
->val
, 4);
75 printf("VAL: 0x%x ", val
);
77 printf("0x%02x 0x%2x...ignoring\n", prop
->type1
, prop
->type2
);
81 if (prop
->right_rw
== 0x00 && prop
->rw_mask
== 0x00) {
83 } else if (prop
->right_rw
== 0x01) {
84 printf("(RW mask: 0x%02x)", prop
->rw_mask
);
86 printf("(UNK 0x%02x 0x%02x)", prop
->right_rw
, prop
->rw_mask
);
88 printf(", length: %d\n", fent
->length
);
93 void change_properties(uint8_t *fw
, int32_t len
, struct propaction
*paction
)
95 struct file_entry
*fent
;
96 struct propaction
*cpaction
;
98 for (fent
= get_next_file(fw
, len
); fent
!= NULL
; fent
= get_next_file(NULL
, 0)) {
100 while (cpaction
!= NULL
) {
101 if (FINDSTR(fent
->name
, cpaction
->property
)) {
104 cpaction
= cpaction
->next
;
106 if (cpaction
!= NULL
) {
107 struct properties
*prop
;
109 prop
= (struct properties
*)fent
->start
;
111 if (prop
->magic
!= 0x83011111) {
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) {
120 cpaction
->status
|= PROP_STATUS_SUCCESS
;
122 cpaction
->status
|= PROP_STATUS_SAMEVAL
;
125 if (*prop
->val
== 0x01) {
127 cpaction
->status
|= PROP_STATUS_SUCCESS
;
129 cpaction
->status
|= PROP_STATUS_SAMEVAL
;
133 cpaction
->status
= PROP_STATUS_WRONGTYPE
;
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
;
142 cpaction
->status
|= PROP_STATUS_WRONGRIGHTS
;
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
;
151 cpaction
->status
|= PROP_STATUS_WRONGRIGHTS
;
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
176 #define MAGIC(fn, args...) fn(args)
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")
182 #define _BD_SET(bd, byte, bits) (bd[byte] |= bits)
183 #define BD_SET(bd, ident) MAGIC(_BD_SET, bd, BD_##ident)
185 void print_boarddescription(uint8_t *bd
)
189 for (j
= 0; j
< 32; j
++) {
190 printf("%02x ", *(bd
+j
));
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
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
));
218 printf("\tps2aPresent\t\t: %s\n", BD_TEXT(bd
, PS2A
));
221 void handle_boarddescription(uint8_t *fw
, int32_t len
, int32_t patch
)
223 struct file_entry
*fent
;
226 for (fent
= get_next_file(fw
, len
); fent
!= NULL
; fent
= get_next_file(NULL
, 0)) {
227 if (!strcmp(fent
->name
, "pdata"))
232 fprintf(stderr
, "pdata file not found, aborting!\n");
239 if (*((uint32_t*)pos
) != 0x00002802) {
240 fprintf(stderr
, "pdata magic does not match, aborting!\n");
247 if (*((uint32_t*)pos
) != 0x00500101) {
248 fprintf(stderr
, "pdata magic2 does not match, aborting!\n");
253 /* Enable ATX and relay power switching */
255 BD_SET(pos
, PWRRELAY
);
258 printf("0x%08x: BOARD_DESCRIPTION: ", (uint32_t)(fent
->start
- fw
));
259 print_boarddescription(pos
);
262 void syntax(char *name
)
264 fprintf(stderr
,"Syntax: %s parameters firmware.bin\n", name
);
265 fprintf(stderr
,"parameters as follows:\n");
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");
270 fprintf(stderr
,"\t-l\t\t\tlist files in firmware\n");
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");
279 void add_action(int32_t opt
, char *optarg
, struct propaction
**paction
) {
280 struct propaction
*pos
= *paction
;
281 struct propaction
*prev
= NULL
;
283 while (pos
!= NULL
) {
284 if (!strcmp(pos
->property
, optarg
))
291 pos
= malloc(sizeof(struct propaction
));
296 bzero(pos
, sizeof(struct propaction
));
297 pos
->property
= optarg
;
308 if (pos
->action
& PROP_ACTION_FALSE
) {
309 fprintf(stderr
,"inconsistent requests for %s\n",pos
->property
);
312 pos
->action
|= PROP_ACTION_TRUE
;
315 if (pos
->action
& PROP_ACTION_TRUE
) {
316 fprintf(stderr
,"inconsistent requests for %s\n",pos
->property
);
319 pos
->action
|= PROP_ACTION_FALSE
;
322 if (pos
->action
& PROP_ACTION_RO
) {
323 fprintf(stderr
,"inconsistent requests for %s\n",pos
->property
);
326 pos
->action
|= PROP_ACTION_RW
;
329 if (pos
->action
& PROP_ACTION_RW
) {
330 fprintf(stderr
,"inconsistent requests for %s\n",pos
->property
);
333 pos
->action
|= PROP_ACTION_RO
;
338 int32_t check_crc(uint8_t *fw
, int32_t len
)
341 uint32_t crc
, oldcrc
;
343 ret
= rsb_crc2(fw
, len
, 0x55335053, &crc
);
344 oldcrc
= (uint32_t)*((uint32_t*)(fw
+ len
- 4));
346 printf("Checksum: 0x%08x (%s), should be: 0x%08x\n",
348 (ret
? "NOT OK" : "OK"),
354 void check_image(uint8_t *fw
, int32_t len
)
356 struct file_entry
*fent
;
358 /* get_next_file will abort on error */
359 fent
= get_next_file(fw
, len
);
360 while (fent
!= NULL
) {
361 fent
= get_next_file(NULL
, 0);
365 int32_t main(int32_t argc
, char **argv
)
375 struct propaction
*paction
= NULL
;
377 int32_t update_crc
= 0;
378 int32_t patch_bd
= 0;
379 int32_t patch_fw
= 0;
386 while ((opt
= getopt(argc
, argv
, "dubelt:f:w:r:x:")) != -1) {
408 add_action(opt
, optarg
, &paction
);
411 replace_add_file(NULL
, 0, NULL
, NULL
);
424 if (stat(file
, &statbuf
) == -1) {
425 fprintf(stderr
,"%s: ", file
);
430 if ((fd
= open(file
, O_RDONLY
)) == -1) {
431 fprintf(stderr
,"%s: ", file
);
436 if ((fw
= malloc(statbuf
.st_size
)) == NULL
) {
441 bzero(fw
, statbuf
.st_size
);
443 remaining
= statbuf
.st_size
;
446 if ((ret
= read(fd
, fw
+ (statbuf
.st_size
- remaining
), remaining
)) == -1) {
454 ret
= check_crc(fw
, statbuf
.st_size
);
455 if ((ret
!= 0) && (!update_crc
)) {
456 fprintf(stderr
,"Checksum incorrect, aborting...\n");
460 check_image(fw
, statbuf
.st_size
- 4);
463 struct propaction
*cpaction
= paction
;
465 change_properties(fw
, statbuf
.st_size
- 4, paction
);
467 printf("\nProperty change results:\n");
468 while(cpaction
!= NULL
) {
469 printf("%s: ", cpaction
->property
);
471 if (cpaction
->status
== PROP_STATUS_NOTFOUND
)
473 if (cpaction
->status
& PROP_STATUS_SUCCESS
)
475 if (cpaction
->status
& PROP_STATUS_SAMEVAL
)
477 if (cpaction
->status
& PROP_STATUS_WRONGTYPE
)
478 printf("WRONGTYPE ");
479 if (cpaction
->status
& PROP_STATUS_WRONGRIGHTS
)
480 printf("WRONGRIGHTS ");
483 cpaction
= cpaction
->next
;
489 handle_boarddescription(fw
, statbuf
.st_size
- 4, 1);
493 show_properties(fw
, statbuf
.st_size
- 4);
494 handle_boarddescription(fw
, statbuf
.st_size
- 4, 0);
498 list_files(fw
, statbuf
.st_size
- 4);
502 extract_files(fw
, statbuf
.st_size
- 4);
505 if (update_crc
|| patch_fw
|| patch_bd
) {
506 ret
= rsb_crc2(fw
, statbuf
.st_size
, 0x55335053, &crc
);
508 *((uint32_t*)(fw
+ statbuf
.st_size
- 4)) = crc
;
511 check_image(fw
, statbuf
.st_size
-4);
513 if (check_crc(fw
, statbuf
.st_size
) == 0) {
516 newfile
= malloc(strlen(file
) + strlen(".patched") + 1);
517 if (newfile
== NULL
) {
521 strcpy(newfile
, file
);
522 strcat(newfile
, ".patched");
525 write_file(newfile
, fw
, statbuf
.st_size
);
527 fprintf(stderr
,"Can't set correct checksum, aborting...\n");