]> cvs.zerfleddert.de Git - rsbs2/blobdiff - firmware.c
better corruption check by calculating the number of fill bytes
[rsbs2] / firmware.c
index ac83b96e4c2911f5984c0de09a646a6786ac0954..aeef48c59a0071abd85baebde44f62363fca307f 100644 (file)
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <strings.h>
 #include "rsb-crc.h"
+#include "extract.h"
 
 #define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str)))
 
@@ -45,18 +46,17 @@ struct propaction {
 
 void show_properties(unsigned char *fw, int len)
 {
-       int i;
+       struct file_entry *fent;
 
-       for (i = 0; i < (len-100 /* XXX */); i++) {
-               if (FINDSTR(fw+i, "/default/fw_prop/") ||
-                   FINDSTR(fw+i, "/default/fw_setup/") ||
-                   FINDSTR(fw+i, "/default/oem_prop/")) {
+       for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
+               if (FINDSTR(fent->name, "/default/fw_prop/") ||
+                   FINDSTR(fent->name, "/default/fw_setup/") ||
+                   FINDSTR(fent->name, "/default/oem_prop/")) {
                        struct properties *prop;
-                       unsigned char *pos = fw + i;
 
-                       printf("0x%08x: found setting: %s ", i, pos);
+                       printf("0x%08x: found setting: %s ", fent->start - fw, fent->name);
 
-                       prop = (struct properties*)(pos + strlen((char*)pos) + 1);
+                       prop = (struct properties*)fent->start;
                        
                        if (prop->magic != 0x83011111) {
                                printf("ignoring...\n");
@@ -75,35 +75,34 @@ void show_properties(unsigned char *fw, int len)
                        }
 
                        if (prop->right_rw == 0x00 && prop->rw_mask == 0x00) {
-                               printf("(R-) ");
+                               printf("(R-)");
                        } else if (prop->right_rw == 0x01) {
-                               printf("(RW mask: 0x%02x) ", prop->rw_mask);
+                               printf("(RW mask: 0x%02x)", prop->rw_mask);
                        } else {
-                               printf("(UNK 0x%02x 0x%02x) ", prop->right_rw, prop->rw_mask);
+                               printf("(UNK 0x%02x 0x%02x)", prop->right_rw, prop->rw_mask);
                        }
-                       printf("\n");
+                       printf(", length: %d\n", fent->length);
                }
        }
 }
 
 void change_properties(unsigned char *fw, int len, struct propaction *paction)
 {
-       int i;
+       struct file_entry *fent;
        struct propaction *cpaction;
 
-       for (i = 0; i < (len-100 /* XXX */); i++) {
+       for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
                cpaction = paction;
                while (cpaction != NULL) {
-                       if (FINDSTR(fw + i, cpaction->property)) {
+                       if (FINDSTR(fent->name, cpaction->property)) {
                                break;
                        }
                        cpaction = cpaction->next;
                }
                if (cpaction != NULL) {
                        struct properties *prop;
-                       unsigned char *pos = fw + i;
 
-                       prop = (struct properties*)(pos + strlen((char*)pos) + 1);
+                       prop = (struct properties*)fent->start;
 
                        if (prop->magic != 0x83011111) {
                                continue;
@@ -152,6 +151,33 @@ void change_properties(unsigned char *fw, int len, struct propaction *paction)
        }
 }
 
+#define BD_SERIAL1     0x14,0x02
+#define BD_ICMB                0x14,0x04
+#define BD_LAN         0x14,0x08
+#define BD_SERIAL2     0x14,0x10
+#define BD_SERIAL3     0x14,0x20
+#define BD_USB         0x14,0x40
+#define BD_PCI         0x15,0x03
+#define BD_LPC         0x15,0x04
+#define BD_VGA         0x15,0x08
+#define BD_BATTERY     0x15,0x10
+#define BD_ACDC                0x15,0x20
+#define BD_STANDBY     0x15,0x40
+#define BD_POWERCONN   0x15,0x70
+#define BD_DVI         0x15,0x80
+#define BD_PWRATX      0x16,0x01
+#define BD_PWRRELAY    0x16,0x02
+#define BD_PS2A                0x19,0xff
+
+#define MAGIC(fn, args...) fn(args)
+
+#define _BD_IS_SET(bd, byte, bits) (bd[byte] & bits)
+#define BD_IS_SET(bd, ident) MAGIC(_BD_IS_SET, bd, BD_##ident)
+#define BD_TEXT(bd, ident) (BD_IS_SET(bd, ident) ? "TRUE" : "FALSE")
+
+#define _BD_SET(bd, byte, bits) (bd[byte] |= bits)
+#define BD_SET(bd, ident) MAGIC(_BD_SET, bd, BD_##ident)
+
 void print_boarddescription(unsigned char *bd)
 {
        int j;
@@ -160,41 +186,73 @@ void print_boarddescription(unsigned char *bd)
                printf("%02x ", *(bd+j));
        }
        printf("\n");
+
+       /* com/agilent/rmc/amr/AmrMaster.class
+        * com/agilent/rmc/mgui/RmcPanel.class
+        * com/agilent/rmc/mgui/panels/AvrManualConfig.class
+        * com/agilent/rmc/mgui/panels/CardConf.jad
+        * com/agilent/rmc/mgui/panels/PowerMgmtConf.jad
+        * com/agilent/rmc/mgui/panels/RemoteDiskConf.jad
+        */
+       printf("\tserial1Present\t\t: %s\n", BD_TEXT(bd, SERIAL1));
+       printf("\ticmbPresent\t\t: %s\n", BD_TEXT(bd, ICMB));
+       printf("\tlanPresent\t\t: %s\n", BD_TEXT(bd, LAN));
+       printf("\tserial2Present\t\t: %s\n", BD_TEXT(bd, SERIAL2));
+       printf("\tserial3Present\t\t: %s\n", BD_TEXT(bd, SERIAL3));
+       printf("\tusbPresent\t\t: %s\n", BD_TEXT(bd, USB));
+       printf("\tpciPresent\t\t: %s\n", BD_TEXT(bd, PCI));
+       printf("\tlpcPresent\t\t: %s\n", BD_TEXT(bd, LPC));
+       printf("\tvgaPresent\t\t: %s\n", BD_TEXT(bd, VGA));
+       printf("\tbatteryPresent\t\t: %s\n", BD_TEXT(bd, BATTERY));
+       printf("\tacdcPresent\t\t: %s\n", BD_TEXT(bd, ACDC));
+       printf("\tstandbyPresent\t\t: %s\n", BD_TEXT(bd, STANDBY));
+       printf("\thasPowerConnectors\t: %s\n", BD_TEXT(bd, POWERCONN));
+       printf("\tdviPresent\t\t: %s\n", BD_TEXT(bd, DVI));
+       printf("\tpowerSwitchATX\t\t: %s\n", BD_TEXT(bd, PWRATX));
+       printf("\tpowerSwitchRelay\t: %s\n", BD_TEXT(bd, PWRRELAY));
+       /* 22 & 4 */
+       printf("\tps2aPresent\t\t: %s\n", BD_TEXT(bd, PS2A));
 }
 
 void handle_boarddescription(unsigned char *fw, int len, int patch)
 {
-       int i;
+       struct file_entry *fent;
+       unsigned char *pos;
        
-       for (i = len - (strlen("pdata")+1); i > 0; i--) {
-               if (FINDSTR(fw+i, "pdata")) {
-                       unsigned char *pos = fw + i + strlen("pdata") + 1;
+       for (fent = get_next_file(fw, len); fent != NULL; fent = get_next_file(NULL, 0)) {
+               if (!strcmp(fent->name, "pdata"))
+                       break;
+       }
 
-                       /* MAGIC? */
-                       if (*((unsigned int*)pos) != 0x00002802) {
-                               continue;
-                       }
+       if (fent == NULL) {
+               fprintf(stderr, "pdata file not found, aborting!\n");
+               exit(1);
+       }
 
-                       pos += 26;
 
-                       /* MAGIC2? */
-                       if (*((unsigned int*)pos) != 0x00500101) {
-                               continue;
-                       }
+       pos = fent->start;
+       /* MAGIC? */
+       if (*((unsigned int*)pos) != 0x00002802) {
+               fprintf(stderr, "pdata magic does not match, aborting!\n");
+               exit(1);
+       }
 
-                       printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
-                       print_boarddescription(pos);
+       pos += 26;
 
-                       if (patch) {
-                               /* Enable ATX and relay power switching */
-                               pos[22] |= 0x03;
-                               printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
-                               print_boarddescription(pos);
-                       }
+       /* MAGIC2? */
+       if (*((unsigned int*)pos) != 0x00500101) {
+               fprintf(stderr, "pdata magic2 does not match, aborting!\n");
+               exit(1);
+       }
 
-                       break;
-               }
+       if (patch) {
+               /* Enable ATX and relay power switching */
+               BD_SET(pos, PWRATX);
+               BD_SET(pos, PWRRELAY);
        }
+
+       printf("0x%08x: BOARD_DESCRIPTION: ", fent->start - fw);
+       print_boarddescription(pos);
 }
 
 void syntax(char *name)
@@ -204,6 +262,7 @@ void syntax(char *name)
        fprintf(stderr,"\t-d\t\tdisplay all properties of the image\n");
        fprintf(stderr,"\t-u\t\tupdate checksum of the image\n");
        fprintf(stderr,"\t-b\t\tmodify BOARD_DESCRIPTION for more power-switch options\n");
+       fprintf(stderr,"\t-e\t\textract files in firmware\n");
        fprintf(stderr,"\t-t property\tset 'property' to true\n");
        fprintf(stderr,"\t-f property\tset 'property' to false\n");
        fprintf(stderr,"\t-w property\tallow read-write access to 'property'\n");
@@ -286,6 +345,19 @@ int check_crc(unsigned char *fw, int len)
        return ret;
 }
 
+void check_image(unsigned char *fw, int len)
+{
+       struct file_entry *fent;
+       char *last_name = NULL;
+
+       /* get_next_file will abort on error */
+       fent = get_next_file(fw, len);
+       while (fent != NULL) {
+               last_name = fent->name;
+               fent = get_next_file(NULL, 0);
+       }
+}
+
 int main(int argc, char **argv)
 {
        struct stat statbuf;
@@ -301,11 +373,12 @@ int main(int argc, char **argv)
        int update_crc = 0;
        int patch_bd = 0;
        int patch_fw = 0;
+       int extract = 0;
 
        if (argc < 2)
                syntax(argv[0]);
 
-       while ((opt = getopt(argc, argv, "dubt:f:w:r:")) != -1) {
+       while ((opt = getopt(argc, argv, "dubet:f:w:r:")) != -1) {
                switch(opt) {
                        case 'd':
                                showall = 1;
@@ -316,6 +389,9 @@ int main(int argc, char **argv)
                        case 'b':
                                patch_bd = 1;
                                break;
+                       case 'e':
+                               extract = 1;
+                               break;
                        case 't':
                        case 'f':
                        case 'w':
@@ -367,12 +443,15 @@ int main(int argc, char **argv)
        ret = check_crc(fw, statbuf.st_size);
        if ((ret != 0) && (!update_crc)) {
                fprintf(stderr,"Checksum incorrect, aborting...\n");
+               exit(1);
        }
 
+       check_image(fw, statbuf.st_size - 4);
+
        if (patch_fw) {
                struct propaction *cpaction = paction;
 
-               change_properties(fw, statbuf.st_size, paction);
+               change_properties(fw, statbuf.st_size - 4, paction);
 
                printf("\nProperty change results:\n");
                while(cpaction != NULL) {
@@ -396,12 +475,16 @@ int main(int argc, char **argv)
        }
 
        if (patch_bd) {
-               handle_boarddescription(fw, statbuf.st_size -4, 1);
+               handle_boarddescription(fw, statbuf.st_size - 4, 1);
        }
 
        if (showall) {
                show_properties(fw, statbuf.st_size - 4);
-               handle_boarddescription(fw, statbuf.st_size -4, 0);
+               handle_boarddescription(fw, statbuf.st_size - 4, 0);
+       }
+
+       if (extract) {
+               extract_files(fw, statbuf.st_size - 4);
        }
 
        if (update_crc || patch_fw || patch_bd) {
@@ -410,6 +493,8 @@ int main(int argc, char **argv)
                        *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
                }
 
+               check_image(fw, statbuf.st_size-4);
+
                if (check_crc(fw, statbuf.st_size) == 0) {
                        char *newfile;
 
Impressum, Datenschutz