From ed38456f83ac84ab1f3facc52de83f4e58a78f18 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Fri, 15 Apr 2022 20:46:40 +0100
Subject: [PATCH 1/7] Piggy-back 'Do not remove trailing 0xff' onto option -D

---
 src/avr.c    | 10 ++++++++++
 src/fileio.c |  5 ++++-
 src/main.c   |  1 +
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/avr.c b/src/avr.c
index d6e78bb5..9d2bf503 100644
--- a/src/avr.c
+++ b/src/avr.c
@@ -282,6 +282,16 @@ int avr_read_byte_default(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
 int avr_mem_hiaddr(AVRMEM * mem)
 {
   int i, n;
+  static int disableffopt;
+
+  /* calling once with NULL disables any future trailing-0xff optimisation */
+  if(!mem) {
+    disableffopt = 1;
+    return 0;
+  }
+
+  if(disableffopt)
+    return mem->size;
 
   /* return the highest non-0xff address regardless of how much
      memory was read */
diff --git a/src/fileio.c b/src/fileio.c
index 7c021ce4..429a200e 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -1594,7 +1594,10 @@ int fileio(int op, char * filename, FILEFMT format,
        * if we are reading flash, just mark the size as being the
        * highest non-0xff byte
        */
-      rc = avr_mem_hiaddr(mem);
+      int hiaddr = avr_mem_hiaddr(mem);
+
+      if(hiaddr < rc)           /* if trailing-0xff not disabled */
+        rc = hiaddr;
     }
   }
   if (format != FMT_IMM && !using_stdio) {
diff --git a/src/main.c b/src/main.c
index 253c6e51..c3bc0381 100644
--- a/src/main.c
+++ b/src/main.c
@@ -528,6 +528,7 @@ int main(int argc, char * argv [])
 
       case 'D': /* disable auto erase */
         uflags &= ~UF_AUTO_ERASE;
+        avr_mem_hiaddr(NULL); /* disable trailing 0xff optimisation */
         break;
 
       case 'e': /* perform a chip erase */

From e18d436f88e575afcee68343842660b44aa9ce30 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Fri, 15 Apr 2022 20:48:46 +0100
Subject: [PATCH 2/7] Move evaluating 'is flash' from caller to callee
 avr_mem_hiaddr()

---
 src/avr.c    | 27 +++++++++++----------------
 src/fileio.c | 19 ++++++-------------
 2 files changed, 17 insertions(+), 29 deletions(-)

diff --git a/src/avr.c b/src/avr.c
index 9d2bf503..a6d621b7 100644
--- a/src/avr.c
+++ b/src/avr.c
@@ -278,6 +278,7 @@ int avr_read_byte_default(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
  * value. This is useful for determining where to stop when dealing
  * with "flash" memory, since writing 0xff to flash is typically a
  * no-op. Always return an even number since flash is word addressed.
+ * Only apply this optimisation on flash-type memory.
  */
 int avr_mem_hiaddr(AVRMEM * mem)
 {
@@ -293,6 +294,13 @@ int avr_mem_hiaddr(AVRMEM * mem)
   if(disableffopt)
     return mem->size;
 
+  /* if the memory is not a flash-type memory do not remove trailing 0xff */
+  if(strcasecmp(mem->desc, "flash") &&
+     strcasecmp(mem->desc, "application") &&
+     strcasecmp(mem->desc, "apptable") &&
+     strcasecmp(mem->desc, "boot"))
+    return mem->size;
+
   /* return the highest non-0xff address regardless of how much
      memory was read */
   for (i=mem->size-1; i>0; i--) {
@@ -426,15 +434,8 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
       nread++;
       report_progress(nread, npages, NULL);
     }
-    if (!failure) {
-      if (strcasecmp(mem->desc, "flash") == 0 ||
-          strcasecmp(mem->desc, "application") == 0 ||
-          strcasecmp(mem->desc, "apptable") == 0 ||
-          strcasecmp(mem->desc, "boot") == 0)
-        return avr_mem_hiaddr(mem);
-      else
-        return mem->size;
-    }
+    if (!failure)
+      return avr_mem_hiaddr(mem);
     /* else: fall back to byte-at-a-time write, for historical reasons */
   }
 
@@ -464,13 +465,7 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
     report_progress(i, mem->size, NULL);
   }
 
-  if (strcasecmp(mem->desc, "flash") == 0 ||
-      strcasecmp(mem->desc, "application") == 0 ||
-      strcasecmp(mem->desc, "apptable") == 0 ||
-      strcasecmp(mem->desc, "boot") == 0)
-    return avr_mem_hiaddr(mem);
-  else
-    return i;
+  return avr_mem_hiaddr(mem);
 }
 
 
diff --git a/src/fileio.c b/src/fileio.c
index 429a200e..754e267b 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -1585,21 +1585,14 @@ int fileio(int op, char * filename, FILEFMT format,
       return -1;
   }
 
-  if (rc > 0) {
-    if ((op == FIO_READ) && (strcasecmp(mem->desc, "flash") == 0 ||
-                             strcasecmp(mem->desc, "application") == 0 ||
-                             strcasecmp(mem->desc, "apptable") == 0 ||
-                             strcasecmp(mem->desc, "boot") == 0)) {
-      /*
-       * if we are reading flash, just mark the size as being the
-       * highest non-0xff byte
-       */
-      int hiaddr = avr_mem_hiaddr(mem);
+  /* on reading flash set the size to location of highest non-0xff byte */
+  if (rc > 0 && op == FIO_READ) {
+    int hiaddr = avr_mem_hiaddr(mem);
 
-      if(hiaddr < rc)           /* if trailing-0xff not disabled */
-        rc = hiaddr;
-    }
+    if(hiaddr < rc)             /* if trailing-0xff not disabled */
+      rc = hiaddr;
   }
+
   if (format != FMT_IMM && !using_stdio) {
     fclose(f);
   }

From 2397984d2b13b885fee4100f79af6612dca33599 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Wed, 27 Apr 2022 18:54:13 +0100
Subject: [PATCH 3/7] Disable trailing-0xff removal when invoking arduino
 programmer

---
 src/arduino.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/arduino.c b/src/arduino.c
index dbaafef2..7f030a13 100644
--- a/src/arduino.c
+++ b/src/arduino.c
@@ -130,4 +130,7 @@ void arduino_initpgm(PROGRAMMER * pgm)
   pgm->read_sig_bytes = arduino_read_sig_bytes;
   pgm->open = arduino_open;
   pgm->close = arduino_close;
+
+  /* disable trailing-0xff removal when reading input files and avr flash */
+  avr_mem_hiaddr(NULL);
 }

From 52b20f4a2808dc6732fd9d185a9f0b3c1122b7e0 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Thu, 28 Apr 2022 17:26:09 +0100
Subject: [PATCH 4/7] Provide self-documenting API for disabling trailing-0xff
 removal

---
 src/arduino.c    | 3 +--
 src/libavrdude.h | 1 +
 src/main.c       | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/arduino.c b/src/arduino.c
index 7f030a13..f7b60246 100644
--- a/src/arduino.c
+++ b/src/arduino.c
@@ -131,6 +131,5 @@ void arduino_initpgm(PROGRAMMER * pgm)
   pgm->open = arduino_open;
   pgm->close = arduino_close;
 
-  /* disable trailing-0xff removal when reading input files and avr flash */
-  avr_mem_hiaddr(NULL);
+  disable_trailing_ff_removal(); /* so that arduino bootloader can ignore chip erase */
 }
diff --git a/src/libavrdude.h b/src/libavrdude.h
index ddb72b48..d8d5739a 100644
--- a/src/libavrdude.h
+++ b/src/libavrdude.h
@@ -787,6 +787,7 @@ int avr_get_cycle_count(PROGRAMMER * pgm, AVRPART * p, int * cycles);
 
 int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles);
 
+#define disable_trailing_ff_removal() avr_mem_hiaddr(NULL)
 int avr_mem_hiaddr(AVRMEM * mem);
 
 int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p);
diff --git a/src/main.c b/src/main.c
index c3bc0381..526ae35f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -528,7 +528,7 @@ int main(int argc, char * argv [])
 
       case 'D': /* disable auto erase */
         uflags &= ~UF_AUTO_ERASE;
-        avr_mem_hiaddr(NULL); /* disable trailing 0xff optimisation */
+        disable_trailing_ff_removal();
         break;
 
       case 'e': /* perform a chip erase */

From 52734bafc6af7cfc4b72824629159900ee88f6a6 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Thu, 28 Apr 2022 17:29:06 +0100
Subject: [PATCH 5/7] Add option -A to separately disable trailing-0xff removal

---
 src/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index 526ae35f..f5381834 100644
--- a/src/main.c
+++ b/src/main.c
@@ -110,7 +110,8 @@ static void usage(void)
  "  -B <bitclock>              Specify JTAG/STK500v2 bit clock period (us).\n"
  "  -C <config-file>           Specify location of configuration file.\n"
  "  -c <programmer>            Specify programmer type.\n"
- "  -D                         Disable auto erase for flash memory\n"
+ "  -A                         Disable trailing-0xff removal from file and AVR read.\n"
+ "  -D                         Disable auto erase for flash memory; implies -A.\n"
  "  -i <delay>                 ISP Clock Delay [in microseconds]\n"
  "  -P <port>                  Specify connection port.\n"
  "  -F                         Override invalid signature check.\n"
@@ -442,7 +443,7 @@ int main(int argc, char * argv [])
   /*
    * process command line arguments
    */
-  while ((ch = getopt(argc,argv,"?b:B:c:C:DeE:Fi:l:np:OP:qstU:uvVx:yY:")) != -1) {
+  while ((ch = getopt(argc,argv,"?Ab:B:c:C:DeE:Fi:l:np:OP:qstU:uvVx:yY:")) != -1) {
 
     switch (ch) {
       case 'b': /* override default programmer baud rate */
@@ -528,6 +529,9 @@ int main(int argc, char * argv [])
 
       case 'D': /* disable auto erase */
         uflags &= ~UF_AUTO_ERASE;
+        /* fall through */
+
+      case 'A': /* explicit disabling of trailing-0xff removal */
         disable_trailing_ff_removal();
         break;
 

From f47ec634f81a0f9bd89aa6ce0616b147f69da5d9 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Thu, 28 Apr 2022 17:53:10 +0100
Subject: [PATCH 6/7] On verify always verify full input file

---
 src/fileio.c     | 9 +++++----
 src/libavrdude.h | 5 +++--
 src/update.c     | 2 +-
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/fileio.c b/src/fileio.c
index 754e267b..1c81682d 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -1446,16 +1446,17 @@ static int fmt_autodetect(char * fname)
 
 
 
-int fileio(int op, char * filename, FILEFMT format, 
+int fileio(int oprwv, char * filename, FILEFMT format,
              struct avrpart * p, char * memtype, int size)
 {
-  int rc;
+  int op, rc;
   FILE * f;
   char * fname;
   struct fioparms fio;
   AVRMEM * mem;
   int using_stdio;
 
+  op = oprwv == FIO_READ_FOR_VERIFY? FIO_READ: oprwv;
   mem = avr_locate_mem(p, memtype);
   if (mem == NULL) {
     avrdude_message(MSG_INFO, "fileio(): memory type \"%s\" not configured for device \"%s\"\n",
@@ -1585,8 +1586,8 @@ int fileio(int op, char * filename, FILEFMT format,
       return -1;
   }
 
-  /* on reading flash set the size to location of highest non-0xff byte */
-  if (rc > 0 && op == FIO_READ) {
+  /* on reading flash other than for verify set the size to location of highest non-0xff byte */
+  if (rc > 0 && oprwv == FIO_READ) {
     int hiaddr = avr_mem_hiaddr(mem);
 
     if(hiaddr < rc)             /* if trailing-0xff not disabled */
diff --git a/src/libavrdude.h b/src/libavrdude.h
index d8d5739a..73083932 100644
--- a/src/libavrdude.h
+++ b/src/libavrdude.h
@@ -826,7 +826,8 @@ struct fioparms {
 
 enum {
   FIO_READ,
-  FIO_WRITE
+  FIO_WRITE,
+  FIO_READ_FOR_VERIFY,
 };
 
 #ifdef __cplusplus
@@ -835,7 +836,7 @@ extern "C" {
 
 char * fmtstr(FILEFMT format);
 
-int fileio(int op, char * filename, FILEFMT format,
+int fileio(int oprwv, char * filename, FILEFMT format,
            struct avrpart * p, char * memtype, int size);
 
 #ifdef __cplusplus
diff --git a/src/update.c b/src/update.c
index 15002549..d3c208fc 100644
--- a/src/update.c
+++ b/src/update.c
@@ -341,7 +341,7 @@ int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags f
             progname, mem->desc, alias_mem_desc, upd->filename);
     }
 
-    rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1);
+    rc = fileio(FIO_READ_FOR_VERIFY, upd->filename, upd->format, p, upd->memtype, -1);
     if (rc < 0) {
       avrdude_message(MSG_INFO, "%s: read from file '%s' failed\n",
               progname, upd->filename);

From 580c37fbfefcff83131ea767b176cbf27f9a39a8 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@open.ac.uk>
Date: Fri, 29 Apr 2022 00:14:45 +0100
Subject: [PATCH 7/7] Describe -A in the man and .texi documentation

---
 src/avrdude.1        | 25 ++++++++++++++++++++++---
 src/doc/avrdude.texi | 15 +++++++++++++--
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/src/avrdude.1 b/src/avrdude.1
index cc05cf4c..48082d00 100644
--- a/src/avrdude.1
+++ b/src/avrdude.1
@@ -18,7 +18,7 @@
 .\"
 .\" $Id$
 .\"
-.Dd DATE November 22, 2021
+.Dd DATE April 28, 2022
 .Os
 .Dt AVRDUDE 1
 .Sh NAME
@@ -31,6 +31,7 @@
 .Op Fl B Ar bitclock
 .Op Fl c Ar programmer-id
 .Op Fl C Ar config-file
+.Op Fl A
 .Op Fl D
 .Op Fl e
 .Oo Fl E Ar exitspec Ns
@@ -102,7 +103,7 @@ available (like almost all embedded Linux boards) you can do without
 any additional hardware - just connect them to the MOSI, MISO, RESET 
 and SCK pins on the AVR and use the linuxgpio programmer type. It bitbangs
 the lines using the Linux sysfs GPIO interface. Of course, care should
-be taken about voltage level compatibility. Also, although not strictrly 
+be taken about voltage level compatibility. Also, although not strictly
 required, it is strongly advisable to protect the GPIO pins from 
 overcurrent situations in some way. The simplest would be to just put
 some resistors in series or better yet use a 3-state buffer driver like
@@ -253,7 +254,7 @@ The Teensy bootloader is supported for all AVR boards.
 As the bootloader does not support reading from flash memory,
 use the
 .Fl V
-option to prevent AVRDUDE from verifing the flash memory.
+option to prevent AVRDUDE from verifying the flash memory.
 See the section on
 .Em extended parameters
 for Teensy specific options.
@@ -376,6 +377,20 @@ files. This can be used to add entries to the configuration
 without patching your system wide configuration file. It can be used
 several times, the files are read in same order as given on the command
 line.
+.It Fl A
+Disable the automatic removal of trailing-0xFF sequences in file
+input that is to be programmed to flash and in AVR reads from
+flash memory. Normally, trailing 0xFFs can be discarded, as flash
+programming requires the memory be erased to 0xFF beforehand.
+.Fl A
+should be used when the programmer hardware, or bootloader
+software for that matter, does not carry out chip erase and
+instead handles the memory erase on a page level. The popular
+Arduino bootloader exhibits this behaviour; for this reason
+.Fl A
+is engaged by default when specifying
+. Fl c
+arduino.
 .It Fl D
 Disable auto erase for flash.  When the
 .Fl U
@@ -389,6 +404,10 @@ use page erase before writing each page so no explicit chip erase
 is required.
 Note however that any page not affected by the current operation
 will retain its previous contents.
+Setting
+.Fl D
+implies
+.Fl A.
 .It Fl e
 Causes a chip erase to be executed.  This will reset the contents of the
 flash ROM and EEPROM to the value
diff --git a/src/doc/avrdude.texi b/src/doc/avrdude.texi
index ad4a6598..a7901361 100644
--- a/src/doc/avrdude.texi
+++ b/src/doc/avrdude.texi
@@ -321,13 +321,13 @@ via a serial link (@url{https://github.com/ElTangas/jtag2updi}).
 
 The Micronucleus bootloader is supported for both protocol version V1
 and V2. As the bootloader does not support reading from flash memory,
-use the @code{-V} option to prevent AVRDUDE from verifing the flash memory.
+use the @code{-V} option to prevent AVRDUDE from verifying the flash memory.
 See the section on @emph{extended parameters}
 below for Micronucleus specific options.
 
 The Teensy bootloader is supported for all AVR boards.
 As the bootloader does not support reading from flash memory,
-use the @code{-V} option to prevent AVRDUDE from verifing the flash memory.
+use the @code{-V} option to prevent AVRDUDE from verifying the flash memory.
 See the section on @emph{extended parameters}
 below for Teensy specific options.
 
@@ -495,6 +495,16 @@ without patching your system wide configuration file. It can be used
 several times, the files are read in same order as given on the command 
 line.
 
+@item -A
+Disable the automatic removal of trailing-0xFF sequences in file
+input that is to be programmed to flash and in AVR reads from
+flash memory. Normally, trailing 0xFFs can be discarded, as flash
+programming requires the memory be erased to 0xFF beforehand. -A
+should be used when the programmer hardware, or bootloader
+software for that matter, does not carry out chip erase and
+instead handles the memory erase on a page level. The popular
+Arduino bootloader exhibits this behaviour; for this reason -A is
+engaged by default when specifying -c arduino.
 
 @item -D
 Disable auto erase for flash.  When the -U option with flash memory is 
@@ -506,6 +516,7 @@ use page erase before writing each page so no explicit chip erase
 is required.
 Note however that any page not affected by the current operation
 will retain its previous contents.
+Setting -D implies -A.
 
 @item -e
 Causes a chip erase to be executed.  This will reset the contents of the