comparison loadtools/flcmplboot.c @ 416:c2e14cc15c23

flash erase-program-boot: implemented CRC check before flashing
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 17 Jun 2014 07:33:25 +0000
parents b2487cfd68fd
children
comparison
equal deleted inserted replaced
415:b2487cfd68fd 416:c2e14cc15c23
13 #include <stdlib.h> 13 #include <stdlib.h>
14 #include "flash.h" 14 #include "flash.h"
15 15
16 extern struct flash_bank_info flash_bank_info[2]; 16 extern struct flash_bank_info flash_bank_info[2];
17 extern struct flash_cmdset flash_cmdset_intel; 17 extern struct flash_cmdset flash_cmdset_intel;
18
19 extern uint32_t crc32_table[];
18 20
19 static int hack_enabled; 21 static int hack_enabled;
20 static uint32_t boot_sector_size; 22 static uint32_t boot_sector_size;
21 static uint32_t ram_buffer_addr; 23 static uint32_t ram_buffer_addr;
22 24
121 size_t len; 123 size_t len;
122 char *strtoul_endp; 124 char *strtoul_endp;
123 char *targv[5], longarg[513]; 125 char *targv[5], longarg[513];
124 char shortarg1[9], shortarg2[9], shortarg3[9]; 126 char shortarg1[9], shortarg2[9], shortarg3[9];
125 u_char databuf[256]; 127 u_char databuf[256];
126 int reclen, cc; 128 int reclen, cc, i;
127 uint32_t ramaddr, remlen; 129 uint32_t ramaddr, remlen, crcaccum;
130 u_long crc_from_target;
128 131
129 if (!hack_enabled) { 132 if (!hack_enabled) {
130 fprintf(stderr, 133 fprintf(stderr,
131 "Operation not applicable to this target device\n"); 134 "Operation not applicable to this target device\n");
132 return(-1); 135 return(-1);
190 (u_long) ram_buffer_addr); 193 (u_long) ram_buffer_addr);
191 targv[0] = "ML"; 194 targv[0] = "ML";
192 targv[1] = longarg; 195 targv[1] = longarg;
193 targv[2] = 0; 196 targv[2] = 0;
194 ramaddr = ram_buffer_addr; 197 ramaddr = ram_buffer_addr;
198 crcaccum = 0xFFFFFFFF;
195 for (remlen = len; remlen; remlen -= reclen) { 199 for (remlen = len; remlen; remlen -= reclen) {
196 if (remlen >= 250) 200 if (remlen >= 250)
197 reclen = 250; 201 reclen = 250;
198 else 202 else
199 reclen = remlen; 203 reclen = remlen;
201 if (cc != reclen) { 205 if (cc != reclen) {
202 fclose(binf); 206 fclose(binf);
203 fprintf(stderr, "error reading from %s\n", argv[2]); 207 fprintf(stderr, "error reading from %s\n", argv[2]);
204 return(-1); 208 return(-1);
205 } 209 }
210 for (i = 0; i < reclen; i++) /* update running CRC */
211 crcaccum = crc32_table[crcaccum & 0xFF ^ databuf[i+5]]
212 ^ (crcaccum >> 8);
206 make_s3_record(databuf, ramaddr, reclen); 213 make_s3_record(databuf, ramaddr, reclen);
207 make_ml_arg(databuf, longarg); 214 make_ml_arg(databuf, longarg);
208 tpinterf_make_cmd(targv); 215 tpinterf_make_cmd(targv);
209 if (tpinterf_send_cmd() < 0) { 216 if (tpinterf_send_cmd() < 0) {
210 fclose(binf); 217 fclose(binf);
219 putchar('.'); 226 putchar('.');
220 fflush(stdout); 227 fflush(stdout);
221 } 228 }
222 putchar('\n'); 229 putchar('\n');
223 fclose(binf); 230 fclose(binf);
231
232 printf("Verifying CRC-32 in target RAM\n");
233 if (crc32_on_target((u_long) ram_buffer_addr, (u_long) len,
234 &crc_from_target) < 0)
235 return(-1);
236 if (crc_from_target == crcaccum)
237 printf("match (%08lX)\n", crc_from_target);
238 else {
239 fprintf(stderr, "error: CRC mismatch!\n");
240 return(-1);
241 }
224 242
225 printf("Commanding flash erase+program operation on the target\n"); 243 printf("Commanding flash erase+program operation on the target\n");
226 sprintf(shortarg1, "%lx", (u_long) ram_buffer_addr); 244 sprintf(shortarg1, "%lx", (u_long) ram_buffer_addr);
227 sprintf(shortarg2, "%lx", (u_long) flash_bank_info[0].base_addr); 245 sprintf(shortarg2, "%lx", (u_long) flash_bank_info[0].base_addr);
228 sprintf(shortarg3, "%lx", (u_long) len); 246 sprintf(shortarg3, "%lx", (u_long) len);