changeset 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 38c7078712ab
children 10b4bed10192
files loadtools/flprogbin.c
diffstat 1 files changed, 26 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/loadtools/flprogbin.c	Sun Oct 18 21:41:01 2015 +0000
+++ b/loadtools/flprogbin.c	Mon Oct 19 04:10:34 2015 +0000
@@ -12,18 +12,21 @@
 #include "flash.h"
 
 extern struct flash_bank_info flash_bank_info[2];
+extern uint32_t crc32_table[];
 
 flashcmd_progbin(argc, argv, bank)
 	char **argv;
 {
 	struct flash_bank_info *bi;
 	u_long flashoff, fileoff, len, origlen, bytesdone;
+	u_long crc_base_addr, crc_from_target;
+	uint32_t crcaccum;
 	char *strtoul_endp;
 	FILE *binf;
 	struct stat filestat;
 	char *targv[4], shortarg[10], longarg[513];
 	u_char databuf[256];
-	int reclen, cc;
+	int reclen, cc, i;
 	time_t curtime, last_time;
 
 	if (argc < 4 || argc > 6) {
@@ -110,6 +113,7 @@
 		fclose(binf);
 		return(-1);
 	}
+	crc_base_addr = bi->base_addr + flashoff;
 	sprintf(shortarg, "%lx", (u_long) bi->base_addr);
 	targv[0] = bi->ops->loadagent_setbase_cmd;
 	targv[1] = shortarg;
@@ -138,6 +142,7 @@
 	origlen = len;
 	bytesdone = 0;
 	last_time = 0;
+	crcaccum = 0xFFFFFFFF;
 	while (len) {
 		if (len >= 256)
 			reclen = 256;
@@ -149,6 +154,9 @@
 			fprintf(stderr, "error reading from %s\n", argv[3]);
 			return(-1);
 		}
+		for (i = 0; i < reclen; i++)	/* update running CRC */
+			crcaccum = crc32_table[crcaccum & 0xFF ^ databuf[i]]
+				^ (crcaccum >> 8);
 		sprintf(shortarg, "%lx", flashoff);
 		build_flashw_hex_string(databuf, longarg, reclen >> 1, 0);
 		tpinterf_make_cmd(targv);
@@ -156,10 +164,10 @@
 			fclose(binf);
 			return(-1);
 		}
-		cc = tpinterf_pass_output(8);	/* 8 s timeout */
-		if (cc) {
+		i = tpinterf_pass_output(8);	/* 8 s timeout */
+		if (i) {
 			fclose(binf);
-			return(cc);
+			return(i);
 		}
 		flashoff += reclen;
 		len -= reclen;
@@ -175,5 +183,18 @@
 	}
 	putchar('\n');
 	fclose(binf);
-	return(0);
+
+	/* reset flash to read mode */
+	if (bi->ops->reset_cmd(bi) < 0)
+		return(-1);
+	printf("Verifying CRC-32 of programmed flash area\n");
+	if (crc32_on_target(crc_base_addr, origlen, &crc_from_target) < 0)
+		return(-1);
+	if (crc_from_target == crcaccum) {
+		printf("match (%08lX)\n", crc_from_target);
+		return(0);
+	} else {
+		fprintf(stderr, "error: CRC mismatch!\n");
+		return(-1);
+	}
 }