FreeCalypso > hg > freecalypso-sw
comparison loadtools/flprogbin.c @ 922:fb3f04a62f71
fc-loadtool: flash program-bin now performs a CRC-32 verification
| author | Mychaela Falconia <falcon@ivan.Harhan.ORG> |
|---|---|
| date | Mon, 19 Oct 2015 04:10:34 +0000 |
| parents | 81d387690063 |
| children |
comparison
equal
deleted
inserted
replaced
| 921:38c7078712ab | 922:fb3f04a62f71 |
|---|---|
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 #include <time.h> | 11 #include <time.h> |
| 12 #include "flash.h" | 12 #include "flash.h" |
| 13 | 13 |
| 14 extern struct flash_bank_info flash_bank_info[2]; | 14 extern struct flash_bank_info flash_bank_info[2]; |
| 15 extern uint32_t crc32_table[]; | |
| 15 | 16 |
| 16 flashcmd_progbin(argc, argv, bank) | 17 flashcmd_progbin(argc, argv, bank) |
| 17 char **argv; | 18 char **argv; |
| 18 { | 19 { |
| 19 struct flash_bank_info *bi; | 20 struct flash_bank_info *bi; |
| 20 u_long flashoff, fileoff, len, origlen, bytesdone; | 21 u_long flashoff, fileoff, len, origlen, bytesdone; |
| 22 u_long crc_base_addr, crc_from_target; | |
| 23 uint32_t crcaccum; | |
| 21 char *strtoul_endp; | 24 char *strtoul_endp; |
| 22 FILE *binf; | 25 FILE *binf; |
| 23 struct stat filestat; | 26 struct stat filestat; |
| 24 char *targv[4], shortarg[10], longarg[513]; | 27 char *targv[4], shortarg[10], longarg[513]; |
| 25 u_char databuf[256]; | 28 u_char databuf[256]; |
| 26 int reclen, cc; | 29 int reclen, cc, i; |
| 27 time_t curtime, last_time; | 30 time_t curtime, last_time; |
| 28 | 31 |
| 29 if (argc < 4 || argc > 6) { | 32 if (argc < 4 || argc > 6) { |
| 30 inv: fprintf(stderr, | 33 inv: fprintf(stderr, |
| 31 "usage: %s %s flash-offset binfile [file-offset [length]]\n", | 34 "usage: %s %s flash-offset binfile [file-offset [length]]\n", |
| 108 /* finally done with the arg parsing etc, can get to work now */ | 111 /* finally done with the arg parsing etc, can get to work now */ |
| 109 if (flash_id_check(bank, 0) < 0) { | 112 if (flash_id_check(bank, 0) < 0) { |
| 110 fclose(binf); | 113 fclose(binf); |
| 111 return(-1); | 114 return(-1); |
| 112 } | 115 } |
| 116 crc_base_addr = bi->base_addr + flashoff; | |
| 113 sprintf(shortarg, "%lx", (u_long) bi->base_addr); | 117 sprintf(shortarg, "%lx", (u_long) bi->base_addr); |
| 114 targv[0] = bi->ops->loadagent_setbase_cmd; | 118 targv[0] = bi->ops->loadagent_setbase_cmd; |
| 115 targv[1] = shortarg; | 119 targv[1] = shortarg; |
| 116 targv[2] = 0; | 120 targv[2] = 0; |
| 117 printf("Setting flash base address: %s %s\n", targv[0], targv[1]); | 121 printf("Setting flash base address: %s %s\n", targv[0], targv[1]); |
| 136 targv[3] = 0; | 140 targv[3] = 0; |
| 137 printf("Programming flash: %lu (0x%lx) bytes\n", len, len); | 141 printf("Programming flash: %lu (0x%lx) bytes\n", len, len); |
| 138 origlen = len; | 142 origlen = len; |
| 139 bytesdone = 0; | 143 bytesdone = 0; |
| 140 last_time = 0; | 144 last_time = 0; |
| 145 crcaccum = 0xFFFFFFFF; | |
| 141 while (len) { | 146 while (len) { |
| 142 if (len >= 256) | 147 if (len >= 256) |
| 143 reclen = 256; | 148 reclen = 256; |
| 144 else | 149 else |
| 145 reclen = len; | 150 reclen = len; |
| 147 if (cc != reclen) { | 152 if (cc != reclen) { |
| 148 fclose(binf); | 153 fclose(binf); |
| 149 fprintf(stderr, "error reading from %s\n", argv[3]); | 154 fprintf(stderr, "error reading from %s\n", argv[3]); |
| 150 return(-1); | 155 return(-1); |
| 151 } | 156 } |
| 157 for (i = 0; i < reclen; i++) /* update running CRC */ | |
| 158 crcaccum = crc32_table[crcaccum & 0xFF ^ databuf[i]] | |
| 159 ^ (crcaccum >> 8); | |
| 152 sprintf(shortarg, "%lx", flashoff); | 160 sprintf(shortarg, "%lx", flashoff); |
| 153 build_flashw_hex_string(databuf, longarg, reclen >> 1, 0); | 161 build_flashw_hex_string(databuf, longarg, reclen >> 1, 0); |
| 154 tpinterf_make_cmd(targv); | 162 tpinterf_make_cmd(targv); |
| 155 if (tpinterf_send_cmd() < 0) { | 163 if (tpinterf_send_cmd() < 0) { |
| 156 fclose(binf); | 164 fclose(binf); |
| 157 return(-1); | 165 return(-1); |
| 158 } | 166 } |
| 159 cc = tpinterf_pass_output(8); /* 8 s timeout */ | 167 i = tpinterf_pass_output(8); /* 8 s timeout */ |
| 160 if (cc) { | 168 if (i) { |
| 161 fclose(binf); | 169 fclose(binf); |
| 162 return(cc); | 170 return(i); |
| 163 } | 171 } |
| 164 flashoff += reclen; | 172 flashoff += reclen; |
| 165 len -= reclen; | 173 len -= reclen; |
| 166 bytesdone += reclen; | 174 bytesdone += reclen; |
| 167 cc = bytesdone * 100 / origlen; | 175 cc = bytesdone * 100 / origlen; |
| 173 } | 181 } |
| 174 last_time = curtime; | 182 last_time = curtime; |
| 175 } | 183 } |
| 176 putchar('\n'); | 184 putchar('\n'); |
| 177 fclose(binf); | 185 fclose(binf); |
| 178 return(0); | 186 |
| 187 /* reset flash to read mode */ | |
| 188 if (bi->ops->reset_cmd(bi) < 0) | |
| 189 return(-1); | |
| 190 printf("Verifying CRC-32 of programmed flash area\n"); | |
| 191 if (crc32_on_target(crc_base_addr, origlen, &crc_from_target) < 0) | |
| 192 return(-1); | |
| 193 if (crc_from_target == crcaccum) { | |
| 194 printf("match (%08lX)\n", crc_from_target); | |
| 195 return(0); | |
| 196 } else { | |
| 197 fprintf(stderr, "error: CRC mismatch!\n"); | |
| 198 return(-1); | |
| 199 } | |
| 179 } | 200 } |
