root/tools/dumpchk.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. usage
  2. main

   1 /*
   2 Find the code normally found at the start of ROM.
   3 Default mode detects dryos or vxworks.
   4 If -fix is given, re-write the dump file after removing anything before
   5 the start sig
   6 
   7 If -vxworks or -dryos is given, just check that the corresponding sig is
   8 found at the start of the dump, and print a message/return status
   9 -quiet will suppress the message and only return status.
  10 
  11 If you have a tree full of dumps you want to check/fix, use find, e.g.
  12 */
  13 //find . -path ./*sub/* \! -path *.svn* -type d -exec dumpchk '{}/primary.bin' \;
  14 /*
  15 will check primary.bin in each of the platform/sub directories, skipping .svn,
  16 reporting missing or zero sized files. 
  17 
  18 You can use -fix with the above command if you feel lucky.
  19 
  20 Note that -fix DOES NOT MAKE ANY BACKUP, and is NOT PARTICULARLY SAFE.
  21 
  22 The two start sigs work for all dumps I have tested, which includes
  23 almost every camera supported by chdk and some that aren't*/
  24 #include <stdio.h>
  25 #include <stdlib.h>
  26 #include <string.h>
  27 void usage(void) {
  28         fprintf(stderr,"usage: dumpchk [-vxworks|-dryos|-fix] [-quiet] <dump>\n");
  29         exit(1);
  30 }
  31 
  32 // MOV R0, 2
  33 // B +...
  34 // Copyright 1992...
  35 const char vxworks_start_sig[] = {
  36         0x02, 0x00, 0xA0, 0xE3,
  37         0x12, 0x00, 0x00, 0xEA,
  38         'C', 'o', 'p', 'y', 'r', 'i', 'g', 'h'
  39 };
  40 // B +...
  41 // gaonisoy
  42 const char dryos_start_sig[] = {
  43         0x01, 0x00, 0x00, 0xEA,
  44         'g', 'a','o', 'n', 'i', 's', 'o' ,'y'
  45 };
  46 
  47 
  48 #define CHECK_FIND 0
  49 #define CHECK_VXWORKS 1
  50 #define CHECK_DRYOS 2
  51 int main(int argc, char **argv) {
  52         int retval = 0;
  53         int checkmode = CHECK_FIND;
  54         int fix = 0;
  55         int quiet = 0;
  56         char *dumpname = NULL;
  57         FILE *dumpfile;
  58         int i,size;
  59         for(i = 1; i < argc; i++) {
  60                 if ( strcmp(argv[i],"-vxworks") == 0 ) {
  61                         checkmode = CHECK_VXWORKS;
  62                 }
  63                 else if (strcmp(argv[i],"-dryos") == 0 ) {
  64                         checkmode = CHECK_DRYOS;
  65                 }
  66                 else if (strcmp(argv[i],"-fix") == 0 ) {
  67                         fix = 1;
  68                 }
  69                 else if (strcmp(argv[i],"-quiet") == 0 ) {
  70                         quiet = 1;
  71                 }
  72                 else if ( argv[i][0] == '-' ) {
  73                         fprintf(stderr,"%s unknown option %s\n",argv[0],argv[i]);
  74                         usage();
  75                 }
  76                 else {
  77                         if (!dumpname)
  78                                 dumpname=argv[i];
  79                         else {
  80                                 fprintf(stderr,"%s unexpected %s\n",argv[0],argv[i]);
  81                                 usage();
  82                         }
  83                 }
  84         }
  85         if(!dumpname) {
  86                 usage();
  87         }
  88         dumpfile = fopen(dumpname,"rb");
  89         if(!dumpfile) {
  90                 fprintf(stderr,"%s unable to open %s\n",argv[0],dumpname);
  91                 usage();
  92         }
  93     fseek(dumpfile,0,SEEK_END);
  94     size=ftell(dumpfile);
  95     fseek(dumpfile,0,SEEK_SET);
  96 
  97         if( checkmode == CHECK_FIND) {
  98                 char *buf;
  99                 const char *found=NULL;
 100                 if(size < sizeof(vxworks_start_sig) || size < sizeof(dryos_start_sig)) {
 101                         printf("%s TOO SMALL: 0x%X (%d) bytes\n",dumpname,size,size);
 102                         fclose(dumpfile);
 103                         return 1;
 104                 }
 105                 buf=malloc(size);
 106                 if(!buf) {
 107                         fprintf(stderr,"%s out of memory\n",argv[0]);
 108                         fclose(dumpfile);
 109                         exit(2);
 110                 }
 111                 fread(buf, 1, size, dumpfile);
 112                 for(i=0; i<size; i++) {
 113                         if( i+sizeof(vxworks_start_sig) < size) {
 114                                 if(memcmp(buf+i,vxworks_start_sig,sizeof(vxworks_start_sig)) == 0) {
 115                                         found = "VXWORKS";
 116                                 }
 117                         }
 118                         if( i+sizeof(dryos_start_sig) < size) {
 119                                 if(memcmp(buf+i,dryos_start_sig,sizeof(dryos_start_sig)) == 0) {
 120                                         found = "DRYOS";
 121                                 }
 122                         }
 123                         if(found) {
 124                                 printf("%s %s ",dumpname,found);
 125                                 if(i) {
 126                                         printf("@0x%X (%d) REMAINING 0x%X (%d) bytes\n",i,i,size-i,size-i);
 127                                 }
 128                                 else {
 129                                         printf("@Start SIZE 0x%X (%d)\n",size,size);
 130                                 }
 131                                 break;
 132                         }
 133                 }
 134                 if(i==size) {
 135                         printf("%s 0x%X (%d) bytes no sig found\n",dumpname,size,size);
 136                         retval = 0;
 137                 }
 138                 fclose(dumpfile);
 139                 if(fix && i && i < size) {
 140                         dumpfile = fopen(dumpname,"wb");
 141                         printf("fix: %s trim 0x%X (%d) ...",dumpname,i,i);
 142                         if(!dumpfile) {
 143                                 fprintf(stderr,"\n%s unable to open %s for write\n",argv[0],dumpname);
 144                         }
 145                         else {
 146                                 fwrite(buf+i,1,size-i,dumpfile);
 147                                 printf(" wrote 0x%X (%d)\n",size-i,size-i);
 148                         }
 149                 }
 150                 free(buf);
 151         }
 152         else {
 153                 const char *sig=vxworks_start_sig,*osname = "VXWORKS";
 154                 char *buf;
 155                 int sigsize = sizeof(vxworks_start_sig);
 156                 
 157                 if (checkmode == CHECK_DRYOS) {
 158                         sig = dryos_start_sig;
 159                         sigsize = sizeof(dryos_start_sig);
 160                         osname = "DRYOS";
 161                 }
 162                 buf = malloc(sigsize);
 163                 if(!buf) {
 164                         fprintf(stderr,"%s out of memory\n",argv[0]);
 165                         fclose(dumpfile);
 166                         exit(2);
 167                 }
 168                 if( fread(buf,1,sigsize,dumpfile) != sigsize) {
 169                         retval = 0;
 170                 }
 171                 else {
 172                         retval = (memcmp(buf,sig,sigsize) == 0);
 173                 }
 174                 free(buf);
 175 
 176                 if(!quiet) {
 177                         if(retval) {
 178                                 printf("%s valid %s 0x%X (%d) bytes\n",dumpname,osname,size,size);
 179                         }
 180                         else {
 181                                 printf("%s NOT valid %s 0x%X (%d) bytes\n",dumpname,osname,size,size);
 182                         }
 183                 }
 184         }
 185         fclose(dumpfile);
 186         return retval == 0;
 187 }

/* [<][>][^][v][top][bottom][index][help] */