]> cvs.zerfleddert.de Git - rsbs2/blobdiff - firmware.c
implement command line parsing for rsbs2.pl, now it's useable
[rsbs2] / firmware.c
index cd7927b8f9854d86c20696adcfaabd8285f18dbf..80e12e54f9ff73aaaa15449524dde10055601a7d 100644 (file)
@@ -8,7 +8,7 @@
 #include <string.h>
 #include <strings.h>
 #include "rsb-crc.h"
-#include "extract.h"
+#include "filesystem.h"
 
 #define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str)))
 
@@ -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,48 +216,59 @@ 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)
 {
        fprintf(stderr,"Syntax: %s parameters firmware.bin\n", name);
        fprintf(stderr,"parameters as follows:\n");
-       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");
-       fprintf(stderr,"\t-r property\tallow read-only access to 'property'\n");
+       fprintf(stderr,"\t-d\t\t\tdisplay all properties of the image\n");
+       fprintf(stderr,"\t-u\t\t\tupdate checksum of the image\n");
+       fprintf(stderr,"\t-b\t\t\tmodify BOARD_DESCRIPTION for more power-switch options\n");
+       fprintf(stderr,"\t-e\t\t\textract files in firmware\n");
+       fprintf(stderr,"\t-l\t\t\tlist files in firmware\n");
+       fprintf(stderr,"\t-t property\t\tset 'property' to true\n");
+       fprintf(stderr,"\t-f property\t\tset 'property' to false\n");
+       fprintf(stderr,"\t-w property\t\tallow read-write access to 'property'\n");
+       fprintf(stderr,"\t-r property\t\tallow read-only access to 'property'\n");
+       fprintf(stderr,"\t-x fw_file=local_file\treplace or add fw_file with content of local_file\n");
        exit(1);
 }
 
@@ -338,6 +347,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;
@@ -354,11 +376,12 @@ int main(int argc, char **argv)
        int patch_bd = 0;
        int patch_fw = 0;
        int extract = 0;
+       int list = 0;
 
        if (argc < 2)
                syntax(argv[0]);
 
-       while ((opt = getopt(argc, argv, "dubet:f:w:r:")) != -1) {
+       while ((opt = getopt(argc, argv, "dubelt:f:w:r:x:")) != -1) {
                switch(opt) {
                        case 'd':
                                showall = 1;
@@ -372,6 +395,9 @@ int main(int argc, char **argv)
                        case 'e':
                                extract = 1;
                                break;
+                       case 'l':
+                               list = 1;
+                               break;
                        case 't':
                        case 'f':
                        case 'w':
@@ -379,6 +405,9 @@ int main(int argc, char **argv)
                                patch_fw = 1;
                                add_action(opt, optarg, &paction);
                                break;
+                       case 'x':
+                               replace_add_file(NULL, 0, NULL, NULL);
+                               break;
                        default:
                                syntax(argv[0]);
                }
@@ -426,10 +455,12 @@ int main(int argc, char **argv)
                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) {
@@ -453,12 +484,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 (list) {
+               list_files(fw, statbuf.st_size - 4);
        }
 
        if (extract) {
@@ -471,6 +506,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;
 
@@ -484,7 +521,7 @@ int main(int argc, char **argv)
 
                        printf("Writing %s\n", newfile);
                        if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
-                               fprintf(stderr,"%s: ", file);
+                               fprintf(stderr,"%s: ", newfile);
                                perror("open");
                                exit(1);
                        }
Impressum, Datenschutz