From 2589b176405ad5024f1f4d041efea5b179f152f6 Mon Sep 17 00:00:00 2001
From: MCUdude <hansibull@gmail.com>
Date: Thu, 17 Feb 2022 11:24:59 +0100
Subject: [PATCH] Add support for memory "fill" mode Syntax: write <memtype>
 <start addr> <no. byte to write> <byte to write> ...

---
 src/term.c | 48 +++++++++++++++++++++++++++++++++++++-----------
 src/term.h |  4 ++++
 2 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/src/term.c b/src/term.c
index 5c91931c..295ae612 100644
--- a/src/term.c
+++ b/src/term.c
@@ -336,11 +336,13 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
   unsigned char b;
   int rc;
   int werror;
+  int write_mode;
   AVRMEM * mem;
 
   if (argc < 4) {
-    avrdude_message(MSG_INFO, "Usage: write <memtype> <addr> <byte1> "
-            "<byte2> ... <byteN>\n");
+    avrdude_message(MSG_INFO,
+      "Usage: write <memtype> <start addr> <data1> <data2> <dataN>\n"
+      "       write <memtype> <start addr> <no. bytes> <data1> <dataN> <...>\n");
     return -1;
   }
 
@@ -368,8 +370,19 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
     return -1;
   }
 
-  /* number of bytes to write at the specified address */
-  len = argc - 3;
+  // Figure out how many bytes to write to memory
+  if(strcmp(argv[5], "...") == 0) {
+    write_mode = WRITE_MODE_FILL;
+    len = strtoul(argv[3], &e, 0);
+    if (*e || (e == argv[3])) {
+      avrdude_message(MSG_INFO, "%s (write ...): can't parse address \"%s\"\n",
+            progname, argv[3]);
+      return -1;
+    }
+  } else {
+    write_mode = WRITE_MODE_STANDARD;
+    len = argc - 3;
+  }
 
   if ((addr + len) > maxsize) {
     avrdude_message(MSG_INFO, "%s (write): selected address and # bytes exceed "
@@ -384,13 +397,26 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
     return -1;
   }
 
-  for (i=3; i<argc; i++) {
-    buf[i-3] = strtoul(argv[i], &e, 0);
-    if (*e || (e == argv[i])) {
-      avrdude_message(MSG_INFO, "%s (write): can't parse byte \"%s\"\n",
-              progname, argv[i]);
-      free(buf);
-      return -1;
+  if(write_mode == WRITE_MODE_STANDARD) {
+    for (i=3; i<argc; i++) {
+      buf[i-3] = strtoul(argv[i], &e, 0);
+      if (*e || (e == argv[i])) {
+        avrdude_message(MSG_INFO, "%s (write): can't parse byte \"%s\"\n",
+                progname, argv[i]);
+        free(buf);
+        return -1;
+      }
+    }
+  } else if(write_mode == WRITE_MODE_FILL) {
+    unsigned char fill_val = strtoul(argv[4], &e, 0);
+    if (*e || (e == argv[4])) {
+        avrdude_message(MSG_INFO, "%s (write ...): can't parse byte \"%s\"\n",
+                progname, argv[4]);
+        free(buf);
+        return -1;
+    }
+    for (i = 0; i < len; i++) {
+      buf[i] = fill_val;
     }
   }
 
diff --git a/src/term.h b/src/term.h
index fca3aac8..fc5ad4f4 100644
--- a/src/term.h
+++ b/src/term.h
@@ -27,6 +27,10 @@
 extern "C" {
 #endif
 
+// Macros for determining write mode
+#define WRITE_MODE_STANDARD 0
+#define WRITE_MODE_FILL     1
+
 int terminal_mode(PROGRAMMER * pgm, struct avrpart * p);
 char * terminal_get_input(const char *prompt);