SHARE
TWEET

nuvoton ec crc calculator

a guest May 21st, 2019 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. #pragma pack(push)
  6. #pragma pack(1)
  7. struct HFCGP
  8. {
  9.     uint8_t CPRED:4;
  10.     uint8_t FPRED:4;
  11.    
  12. };
  13. struct header_data
  14. {
  15.     uint16_t signature;
  16.     uint16_t reserved_0x02;
  17.     uint8_t HCBAL_DAT;
  18.     uint8_t HCBAH_DAT;
  19.     uint16_t reserved_0x06;
  20.     uint8_t HFCGML_DAT;
  21.     uint8_t HFCGMH_DAT;
  22.     uint8_t HFCGN_DAT;
  23.     struct HFCGP HFCGP_DAT;
  24.     uint16_t reserved_0x0c;
  25.     uint8_t FL_SIZE_DAT;
  26.     uint8_t BURST_DAT;
  27.     uint8_t SPI_FL_DAT;
  28.     uint8_t MISC_CTL;
  29.     uint16_t reserved_0x12;
  30.     uint32_t FIRMW_START;
  31.     uint32_t FIRMW_SIZE;
  32.     uint16_t reserved_0x1c[(0x40 - 0x1c)/2];
  33.     uint16_t HEADER_CKSM;
  34.     uint16_t reserved_0x42;
  35.     uint32_t FIRMW_CKSM;
  36. };
  37. #pragma pack(pop)
  38.  
  39.  
  40.  
  41. void main(int argc,char ** argv)
  42. {
  43.  
  44.     uint16_t csum=0;
  45.     uint8_t buf[0x100];
  46.     uint32_t csum2=0;
  47.     size_t sz,k;
  48.     uint8_t * buf2;
  49.  
  50.     if(argc!=2)
  51.     {
  52.         printf("usage:\n%s filename\n",argv[0]);
  53.         exit(0);
  54.     }
  55.     FILE * fd=fopen(argv[1],"r+");
  56.     if(!fd)
  57.     {
  58.         printf("failed to open a file %s\n",argv[1]);
  59.         exit(1);
  60.     }
  61.     struct header_data *hd=(struct header_data *)&buf[0];
  62.     sz=fread(buf,0x100,1,fd);
  63.     if(sz!=1)
  64.     {
  65.         printf("failed to read a file %s\n",argv[1]);
  66.         exit(1);
  67.     }
  68.     for(k=0;k<0x3f;k++)
  69.         csum+=buf[k];
  70.     printf("size=%ld\n\n",sizeof(struct header_data));
  71.    
  72.     printf(
  73.         "signature=%04x\n"
  74.         "reserved_0x02=%04x\n"
  75.         "HCBAL_DAT=%02x\n"
  76.         "HCBAH_DAT=%02x\n"
  77.         "reserved_0x06=%04x\n"
  78.         "HFCGML_DAT=%02x\n"
  79.         "HFCGMH_DAT=%02x\n"
  80.         "HFCGN_DAT=%02x\n"
  81.         "HFCGP_DAT.CPRED=%02x\n"
  82.         "HFCGP_DAT.FPRED=%02x\n"
  83.         "reserved_0x0c=%04x\n"
  84.         "FL_SIZE_DAT=%02x\n"
  85.         "BURST_DAT=%02x\n"
  86.         "SPI_FL_DAT=%02x\n"
  87.         "MISC_CTL=%02x\n"
  88.         "reserved_0x12=%04x\n"
  89.         "FIRMW_START=%08x\n"
  90.         "FIRMW_SIZE=%08x\n"
  91.         "HEADER_CKSM=%04x\n"
  92.         "reserved_0x42=%04x\n"
  93.         "FIRMW_CKSM=%08x\n",
  94.         (unsigned int)hd->signature,(unsigned int)hd->reserved_0x02,(unsigned int)hd->HCBAL_DAT,(unsigned int)hd->HCBAH_DAT,(unsigned int)hd->reserved_0x06,
  95.         (unsigned int)hd->HFCGML_DAT,(unsigned int)hd->HFCGMH_DAT,(unsigned int)hd->HFCGN_DAT,(unsigned int)hd->HFCGP_DAT.CPRED,(unsigned int)hd->HFCGP_DAT.FPRED,
  96.         (unsigned int)hd->reserved_0x0c,
  97.         (unsigned int)hd->FL_SIZE_DAT,(unsigned int)hd->BURST_DAT,(unsigned int)hd->SPI_FL_DAT,(unsigned int)hd->MISC_CTL,(unsigned int)hd->reserved_0x12,(unsigned int)hd->FIRMW_START,
  98.         (unsigned int)hd->FIRMW_SIZE,(unsigned int)hd->HEADER_CKSM,(unsigned int)hd->reserved_0x42,(unsigned int)hd->FIRMW_CKSM);
  99.     printf("Calculated hearder crc=%04x\n",csum);
  100.     fseek(fd,hd->FIRMW_START-0x020000,SEEK_SET);
  101.     buf2=malloc(hd->FIRMW_SIZE*2);
  102.     if(buf2==NULL)
  103.     {
  104.         printf("failed to allocate memory %u\n",hd->FIRMW_SIZE);
  105.         exit(1);
  106.     }
  107.     sz=fread(buf2,hd->FIRMW_SIZE*2,1,fd);
  108.     if(sz!=1)
  109.     {
  110.         printf("failed to read a file %s\n",argv[1]);
  111.         free(buf2);
  112.         exit(1);
  113.     }
  114.     for(k=0;k<hd->FIRMW_SIZE*2;k++)
  115.         csum2+=buf2[k];
  116.     printf("Calculated firmware crc=%08x\n",csum2);
  117.     if( (csum == hd->HEADER_CKSM) && (csum2 == hd->FIRMW_CKSM))
  118.         printf("Checksums OK\n");
  119.     else{
  120.         hd->HEADER_CKSM=csum;
  121.         hd->FIRMW_CKSM=csum2;
  122.         fseek(fd,0,SEEK_SET);
  123.         if(fwrite(hd,sizeof(struct header_data),1,fd)==1)
  124.             printf("Checksums Updated\n");
  125.         else
  126.             printf("Checksum update failed.\n");
  127.     }
  128.     free(buf2);
  129.    
  130.     fclose(fd);
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top