]> git.zerfleddert.de Git - rsbs2/commitdiff
don't traverse the filesystem by hand in three different ways, use new accessor
authorMichael Gernoth <michael@gernoth.net>
Sun, 1 Feb 2009 12:43:33 +0000 (13:43 +0100)
committerMichael Gernoth <michael@gernoth.net>
Sun, 1 Feb 2009 12:43:33 +0000 (13:43 +0100)
firmware.c

index e73d7cc817210913817de5c6cca8d1f1ee031274..fb5b007e5b2b8e9e3968c740286b90d01f8a8ed2 100644 (file)
@@ -46,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");
@@ -82,29 +81,28 @@ void show_properties(unsigned char *fw, int len)
                        } else {
                                printf("(UNK 0x%02x 0x%02x)", prop->right_rw, prop->rw_mask);
                        }
-                       printf(", length: %d\n", *((unsigned int*)(fw + i - 4)));
+                       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;
@@ -218,34 +216,43 @@ void print_boarddescription(unsigned char *bd)
 
 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);
+       }
 
-                       if (patch) {
-                               /* Enable relay power switching */
-                               BD_SET(pos, PWRRELAY);
-                       }
-                       printf("0x%08x: BOARD_DESCRIPTION: ", pos-fw);
-                       print_boarddescription(pos);
+       pos += 26;
 
-                       break;
-               }
+       /* MAGIC2? */
+       if (*((unsigned int*)pos) != 0x00500101) {
+               fprintf(stderr, "pdata magic2 does not match, aborting!\n");
+               exit(1);
+       }
+
+       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)
@@ -338,6 +345,24 @@ int check_crc(unsigned char *fw, int len)
        return ret;
 }
 
+int check_image(unsigned char *fw, int len)
+{
+       struct file_entry *fent;
+       char *last_name = NULL;
+
+       fent = get_next_file(fw, len);
+       while (fent != NULL) {
+               last_name = fent->name;
+               fent = get_next_file(NULL, 0);
+       }
+
+       if (strcmp(last_name, "pdata")) {
+               return 1;
+       }
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        struct stat statbuf;
@@ -426,6 +451,11 @@ int main(int argc, char **argv)
                exit(1);
        }
 
+       if (check_image(fw, statbuf.st_size-4) != 0) {
+               fprintf(stderr, "corrupt firmware image found (pdata is not last entry), aborting!\n");
+               exit(1);
+       }
+
        if (patch_fw) {
                struct propaction *cpaction = paction;
 
@@ -466,20 +496,6 @@ int main(int argc, char **argv)
        }
 
        if (update_crc || patch_fw || patch_bd) {
-               struct file_entry *fent;
-               char *last_name = NULL;
-
-               fent = get_next_file(fw, statbuf.st_size - 4);
-               while (fent != NULL) {
-                       last_name = fent->name;
-                       fent = get_next_file(NULL, 0);
-               }
-
-               if (strcmp(last_name, "pdata")) {
-                       fprintf(stderr, "corrupt firmware image found (pdata is not last entry, aborting!\n");
-                       exit(1);
-               }
-
                ret = rsb_crc2(fw, statbuf.st_size, 0x55335053, &crc);
                if (ret == 4) {
                        *((unsigned int*)(fw + statbuf.st_size - 4)) = crc;
Impressum, Datenschutz