From c3d58bcd7af11ff611b98144122f86c22828b318 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Thu, 15 Sep 2011 13:04:36 +0000
Subject: [PATCH] * stk500v2.c (stk600_xprog_paged_load,
 stk600_xprog_paged_write): Fix regression in the AVRISPmkII/STK600 TPI
 handling introduced by the USBasp's TPI implementation which added a pagesize
 even for the minor memory regions of TPI devices.  Also fix wrong offset
 introduced by the memory tagging patch.

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1009 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog  | 44 ++++++++++++++++++++++++++++++++++++++++++++
 stk500v2.c | 31 ++++++++++++++++++++++++++++---
 2 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 180dd397..8ff761bc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-09-15  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	* stk500v2.c (stk600_xprog_paged_load, stk600_xprog_paged_write):
+	Fix regression in the AVRISPmkII/STK600 TPI handling introduced
+	by the USBasp's TPI implementation which added a pagesize even for
+	the minor memory regions of TPI devices.  Also fix wrong offset
+	introduced by the memory tagging patch.
+
 2011-09-15  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	* avr.c (avr_read, avr_write): Don't bail out on TPI parts if
@@ -7,6 +15,42 @@
 	This fixes a regression where the recent bitbang-TPI implementation
 	broke TPI handling of STK600/AVRISPmkII.
 
+2011-09-14  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	Mega-commit to bring in memory tagging.
+	Each memory image byte is now tagged as it's being read from a file.
+	Only bytes read from a file will be written or verified (modulo page
+	granularity requirements).
+	* avrpart.h: Add memory tags.
+	* avrpart.c: Allocate and initialize tag area.
+	* update.h: Drop unused parameter "verify" from do_op().
+	* pgm.h: Add parameter base_addr to the paged_load and paged_write
+	methods, respectively.
+	* avr.h: New parameter to avr_read: second AVRPART to verify against.
+	* fileio.c: Track all memory regions that have been read from an
+	input file by tagging them.
+	* update.c: Call avr_read() with the new parameter list.
+	* main.c: Call avr_initmem() to initialize the memory regions, rather
+	than trying to duplicate an unitialized part, and then let the
+	original part rot away.
+	* avr.c: Implement the heart of the new featureset.  For paged memory
+	areas, when writing or verifying, call the paged_write and paged_load
+	methods, respectively, once per page instead of on the entire memory.
+	When writing, only write bytes or pages that have content read from a
+	file.  Whe verifying, only read memory bytes or pages where the
+	verification data have been read from a file.  Only verify those bytes
+	that have been read from a file.
+	* avrftdi.c: Implement the new API for paged_load and paged_write,
+	respectively.
+	* jtagmkII.c: (Ditto.)
+	* butterfly.c: (Ditto.)
+	* jtagmkI.c: (Ditto.)
+	* avr910.c: (Ditto.)
+	* stk500.c: (Ditto.)
+	* usbasp.c: (Ditto.)
+	* stk500v2.c: (Ditto.)
+	* usbtiny.c: (Ditto.)
+
 2011-09-13  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	* stk500v2.c (stk500v2_command): Treat warnings as errors rather than
diff --git a/stk500v2.c b/stk500v2.c
index 239ccf02..8f102f6f 100644
--- a/stk500v2.c
+++ b/stk500v2.c
@@ -3317,13 +3317,24 @@ static int stk600_xprog_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
             use_ext_addr = (1UL << 31);
     } else if (strcmp(mem->desc, "eeprom") == 0) {
         memtype = XPRG_MEM_TYPE_EEPROM;
+    } else if (strcmp(mem->desc, "signature") == 0) {
+        memtype = XPRG_MEM_TYPE_APPL;
+    } else if (strncmp(mem->desc, "fuse", strlen("fuse")) == 0) {
+        memtype = XPRG_MEM_TYPE_FUSE;
+    } else if (strcmp(mem->desc, "lockbits") == 0) {
+        memtype = XPRG_MEM_TYPE_LOCKBITS;
+    } else if (strcmp(mem->desc, "calibration") == 0) {
+        memtype = XPRG_MEM_TYPE_FACTORY_CALIBRATION;
+    } else if (strcmp(mem->desc, "usersig") == 0) {
+        memtype = XPRG_MEM_TYPE_USERSIG;
     } else {
         fprintf(stderr,
                 "%s: stk600_xprog_paged_load(): unknown paged memory \"%s\"\n",
                 progname, mem->desc);
         return -1;
     }
-    addr = mem->offset;
+    offset = addr;
+    addr += mem->offset;
 
     if ((b = malloc(page_size + 2)) == NULL) {
 	fprintf(stderr,
@@ -3335,7 +3346,6 @@ static int stk600_xprog_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     if (stk500v2_loadaddr(pgm, use_ext_addr) < 0)
         return -1;
 
-    offset = 0;
     while (n_bytes != 0) {
 	b[0] = XPRG_CMD_READ_MEM;
 	b[1] = memtype;
@@ -3407,12 +3417,28 @@ static int stk600_xprog_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     } else if (strcmp(mem->desc, "eeprom") == 0) {
         memtype = XPRG_MEM_TYPE_EEPROM;
         writemode = (1 << XPRG_MEM_WRITE_WRITE) | (1 << XPRG_MEM_WRITE_ERASE);
+    } else if (strcmp(mem->desc, "signature") == 0) {
+        memtype = XPRG_MEM_TYPE_APPL;
+        writemode = (1 << XPRG_MEM_WRITE_WRITE);
+    } else if (strncmp(mem->desc, "fuse", strlen("fuse")) == 0) {
+        memtype = XPRG_MEM_TYPE_FUSE;
+        writemode = (1 << XPRG_MEM_WRITE_WRITE);
+    } else if (strcmp(mem->desc, "lockbits") == 0) {
+        memtype = XPRG_MEM_TYPE_LOCKBITS;
+        writemode = (1 << XPRG_MEM_WRITE_WRITE);
+    } else if (strcmp(mem->desc, "calibration") == 0) {
+        memtype = XPRG_MEM_TYPE_FACTORY_CALIBRATION;
+        writemode = (1 << XPRG_MEM_WRITE_WRITE);
+    } else if (strcmp(mem->desc, "usersig") == 0) {
+        memtype = XPRG_MEM_TYPE_USERSIG;
+        writemode = (1 << XPRG_MEM_WRITE_WRITE);
     } else {
         fprintf(stderr,
                 "%s: stk600_xprog_paged_write(): unknown paged memory \"%s\"\n",
                 progname, mem->desc);
         return -1;
     }
+    offset = addr;
     addr += mem->offset;
 
     if ((b = malloc(page_size + 9)) == NULL) {
@@ -3425,7 +3451,6 @@ static int stk600_xprog_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
     if (stk500v2_loadaddr(pgm, use_ext_addr) < 0)
         return -1;
 
-    offset = 0;
     while (n_bytes != 0) {
 	if (page_size > 256) {
 	    /*