changeset 411:d7f409493eb6

loadagent: intel-rewrite-sector implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 17 Jun 2014 04:31:16 +0000
parents 81d387690063
children bf49e348576b
files target-utils/libload/intelflash.c target-utils/loadagent/cmdtab.c
diffstat 2 files changed, 64 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/target-utils/libload/intelflash.c	Tue Jun 17 03:40:49 2014 +0000
+++ b/target-utils/libload/intelflash.c	Tue Jun 17 04:31:16 2014 +0000
@@ -2,6 +2,10 @@
  * This module implements the INFB and INFW commands for programming
  * Intel-style flash memories.  The syntax and operation are exactly
  * the same as the AMD flash counterparts AMFB and AMFW.
+ *
+ * The intel-rewrite-sector command (erase+program with a minimum of
+ * vulnerability for brickable-boot Compal phones) is implemented
+ * here as well.
  */
 
 #include <sys/types.h>
@@ -75,3 +79,61 @@
 		}
 	}
 }
+
+void
+cmd_intel_rewrite_sector(argbulk)
+	char *argbulk;
+{
+	char *argv[4];
+	u_long srcaddr, dstaddr, len;
+	const u16 *srcptr;
+	volatile u16 *flashptr;
+	u16 stat;
+
+	if (parse_args(argbulk, 3, 3, argv, 0) < 0)
+		return;
+	if (parse_hexarg(argv[0], 8, &srcaddr) < 0) {
+invarg:		printf("ERROR: invalid argument(s)\n");
+		return;
+	}
+	if (parse_hexarg(argv[1], 8, &dstaddr) < 0)
+		goto invarg;
+	if (parse_hexarg(argv[2], 8, &len) < 0)
+		goto invarg;
+	if (srcaddr & 1 || dstaddr & 1 || len & 1) {
+		printf("ERROR: all 3 arguments must be even\n");
+		return;
+	}
+	srcptr = (const u16 *) srcaddr;
+	flashptr = (volatile u16 *) dstaddr;
+	/* unlock the flash sector first */
+	*flashptr = 0x60;
+	*flashptr = 0xD0;
+	/* clear SR */
+	*flashptr = 0x50;
+	/* erase */
+	*flashptr = 0x20;
+	*flashptr = 0xD0;
+	/* wait for erase completion */
+	for (;;) {
+		stat = *flashptr;
+		if (stat & 0x80)
+			break;
+	}
+	if (stat & 0x30) {
+		printf("ERROR: erase operation failed!\n");
+		return;
+	}
+	/* now program the new content */
+	for (; len; len -= 2) {
+		*flashptr = 0x40;
+		*flashptr = *srcptr++;
+		for (;;) {
+			stat = *flashptr;
+			if (stat & 0x80)
+				break;
+		}
+		flashptr++;
+	}
+	printf("Operation complete, final SR: %02X\n", stat & 0xFF);
+}
--- a/target-utils/loadagent/cmdtab.c	Tue Jun 17 03:40:49 2014 +0000
+++ b/target-utils/loadagent/cmdtab.c	Tue Jun 17 04:31:16 2014 +0000
@@ -17,6 +17,7 @@
 extern void cmd_w32();
 
 extern void cmd_baud_switch();
+extern void cmd_intel_rewrite_sector();
 extern void cmd_memdump_human();
 extern void cmd_memdump_machine();
 extern void cmd_memload();
@@ -38,6 +39,7 @@
 	{"blankchk", cmd_blankchk},
 	{"crc32", cmd_crc32},
 	{"dump", cmd_memdump_human},
+	{"intel-rewrite-sector", cmd_intel_rewrite_sector},
 	{"jump", cmd_jump},
 	{"poweroff", abb_power_off},
 	{"r8", cmd_r8},