Skip to contentSkip to conversation

misc-utils: flashcp: Bugfixes to --partition

PATCH Applied Brandon Maier 2 participants 9 messages Nov 2, 2022 – Nov 9, 2022 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

cover misc-utils: flashcp: Bugfixes to --partition

Discovered an assortment of issues with the flashcp --partition code:
- not checking lseek() for errors
- overflowing the src and dest buffers
- not verifying data is written succesfully
- silently ignoring --erase-all

The patches "add safe_*" and "simplify logging" do not fix bugs but are code cleanup.

Brandon Maier (7):
misc-utils: flashcp: add safe_write
misc-utils: flashcp: check for lseek errors
misc-utils: flashcp: add safe_memerase
misc-utils: flashcp: simplify logging
misc-utils: flashcp: fix buffer overflow
misc-utils: flashcp: verify data in --partition
misc-utils: flashcp: abort on --partition and --erase-all

 misc-utils/flashcp.c | 284 +++++++++++++++++++++----------------------
 1 file changed, 138 insertions(+), 146 deletions(-)
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#1 lore raw

PATCH 1/7 misc-utils: flashcp: add safe_write

Share the writing code between the two write implementations.

---
 misc-utils/flashcp.c | 58 +++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 33 deletions(-)
misc-utils/flashcp.c +25 -33
index 9e78831..59389db 100644
@@ -147,6 +147,29 @@ static void safe_read (int fd,const char *filename,void *buf,size_t count,bool v
 	}
 }
 
+static void safe_write (int fd,const void *buf,size_t count,size_t written,unsigned long long to_write,const char *device,bool verbose)
+{
+	ssize_t result;
+
+	/* write to device */
+	result = write (fd,buf,count);
+	if (count != result)
+	{
+		if (verbose) log_printf (LOG_NORMAL,"\n");
+		if (result < 0)
+		{
+			log_printf (LOG_ERROR,
+					"While writing data to 0x%.8lx-0x%.8lx on %s: %m\n",
+					written,written + count,device);
+			exit (EXIT_FAILURE);
+		}
+		log_printf (LOG_ERROR,
+				"Short write count returned while writing to x%.8zx-0x%.8zx on %s: %zu/%llu bytes written to flash\n",
+				written,written + count,device,written + result,to_write);
+		exit (EXIT_FAILURE);
+	}
+}
+
 static void safe_rewind (int fd,const char *filename)
 {
 	if (lseek (fd,0L,SEEK_SET) < 0)
@@ -170,7 +193,6 @@ int main (int argc,char *argv[])
 {
 	const char *filename = NULL,*device = NULL;
 	int i,flags = FLAG_NONE;
-	ssize_t result;
 	size_t size,written;
 	struct mtd_info_user mtd;
 	struct erase_info_user erase;
@@ -344,22 +366,7 @@ int main (int argc,char *argv[])
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
 
 		/* write to device */
-		result = write (dev_fd,src,i);
-		if (i != result)
-		{
-			if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"\n");
-			if (result < 0)
-			{
-				log_printf (LOG_ERROR,
-						"While writing data to 0x%.8lx-0x%.8lx on %s: %m\n",
-						written,written + i,device);
-				exit (EXIT_FAILURE);
-			}
-			log_printf (LOG_ERROR,
-					"Short write count returned while writing to x%.8zx-0x%.8zx on %s: %zu/%llu bytes written to flash\n",
-					written,written + i,device,written + result,(unsigned long long)filestat.st_size);
-			exit (EXIT_FAILURE);
-		}
+		safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device,flags & FLAG_VERBOSE);
 
 		written += i;
 		size -= i;
@@ -469,22 +476,7 @@ DIFF_BLOCKS:
 
 			/* write to device */
 			lseek(dev_fd, current_dev_block, SEEK_SET);
-			result = write (dev_fd,src,i);
-			if (i != result)
-			{
-				if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"\n");
-				if (result < 0)
-				{
-					log_printf (LOG_ERROR,
-							"While writing data to 0x%.8lx-0x%.8lx on %s: %m\n",
-							written,written + i,device);
-					exit (EXIT_FAILURE);
-				}
-				log_printf (LOG_ERROR,
-						"Short write count returned while writing to x%.8zx-0x%.8zx on %s: %zu/%llu bytes written to flash\n",
-						written,written + i,device,written + result,(unsigned long long)filestat.st_size);
-				exit (EXIT_FAILURE);
-			}
+			safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device,flags & FLAG_VERBOSE);
 		}
 
 		erase.start += i;
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#2 lore raw

PATCH 2/7 misc-utils: flashcp: check for lseek errors

In reply to Brandon Maier

Add a safe_lseek wrapper to check for lseek errors.

---
 misc-utils/flashcp.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
misc-utils/flashcp.c +16 -6
index 59389db..43a497f 100644
@@ -170,13 +170,23 @@ static void safe_write (int fd,const void *buf,size_t count,size_t written,unsig
 	}
 }
 
-static void safe_rewind (int fd,const char *filename)
+static off_t safe_lseek (int fd,off_t offset,int whence,const char *filename)
 {
-	if (lseek (fd,0L,SEEK_SET) < 0)
+	off_t off;
+
+	off = lseek (fd,offset,whence);
+	if (off < 0)
 	{
-		log_printf (LOG_ERROR,"While seeking to start of %s: %m\n",filename);
+		log_printf (LOG_ERROR,"While seeking on %s: %m\n",filename);
 		exit (EXIT_FAILURE);
 	}
+
+	return off;
+}
+
+static void safe_rewind (int fd,const char *filename)
+{
+	safe_lseek(fd,0L,SEEK_SET,filename);
 }
 
 /******************************************************************************/
@@ -456,7 +466,7 @@ DIFF_BLOCKS:
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
 
 		/* read from device */
-		current_dev_block = lseek(dev_fd, 0, SEEK_CUR);
+		current_dev_block = safe_lseek(dev_fd, 0, SEEK_CUR, device);
 		safe_read (dev_fd,device,dest,i,flags & FLAG_VERBOSE);
 
 		/* compare buffers, if not the same, erase and write the block */
@@ -464,7 +474,7 @@ DIFF_BLOCKS:
 		{
 			diffBlock++;
 			/* erase block */
-			lseek(dev_fd, current_dev_block, SEEK_SET);
+			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
 			if (ioctl (dev_fd,MEMERASE,&erase) < 0)
 			{
 				log_printf (LOG_NORMAL,"\n");
@@ -475,7 +485,7 @@ DIFF_BLOCKS:
 			}
 
 			/* write to device */
-			lseek(dev_fd, current_dev_block, SEEK_SET);
+			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
 			safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device,flags & FLAG_VERBOSE);
 		}
 
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#3 lore raw

PATCH 3/7 misc-utils: flashcp: add safe_memerase

In reply to Brandon Maier
---
 misc-utils/flashcp.c | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)
misc-utils/flashcp.c +15 -23
index 43a497f..8cb0dbb 100644
@@ -189,6 +189,18 @@ static void safe_rewind (int fd,const char *filename)
 	safe_lseek(fd,0L,SEEK_SET,filename);
 }
 
+static void safe_memerase (int fd,const char *device,struct erase_info_user *erase,bool verbose)
+{
+	if (ioctl (fd,MEMERASE,erase) < 0)
+	{
+		if (verbose) log_printf (LOG_NORMAL,"\n");
+		log_printf (LOG_ERROR,
+				"While erasing blocks 0x%.8x-0x%.8x on %s: %m\n",
+				(unsigned int) erase->start,(unsigned int) (erase->start + erase->length),device);
+		exit (EXIT_FAILURE);
+	}
+}
+
 /******************************************************************************/
 
 static int dev_fd = -1,fil_fd = -1;
@@ -330,14 +342,7 @@ int main (int argc,char *argv[])
 		for (i = 1; i <= blocks; i++)
 		{
 			log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (%d%%)",i,blocks,PERCENTAGE (i,blocks));
-			if (ioctl (dev_fd,MEMERASE,&erase) < 0)
-			{
-				log_printf (LOG_NORMAL,"\n");
-				log_printf (LOG_ERROR,
-						"While erasing blocks 0x%.8x-0x%.8x on %s: %m\n",
-						(unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device);
-				exit (EXIT_FAILURE);
-			}
+			safe_memerase(dev_fd,device,&erase,flags & FLAG_VERBOSE);
 			erase.start += mtd.erasesize;
 		}
 		log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (100%%)\n",blocks,blocks);
@@ -345,13 +350,7 @@ int main (int argc,char *argv[])
 	else
 	{
 		/* if not, erase the whole chunk in one shot */
-		if (ioctl (dev_fd,MEMERASE,&erase) < 0)
-		{
-			log_printf (LOG_ERROR,
-					"While erasing blocks from 0x%.8x-0x%.8x on %s: %m\n",
-					(unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device);
-			exit (EXIT_FAILURE);
-		}
+		safe_memerase(dev_fd,device,&erase,flags & FLAG_VERBOSE);
 	}
 	DEBUG("Erased %u / %luk bytes\n",erase.length,filestat.st_size);
 
@@ -475,14 +474,7 @@ DIFF_BLOCKS:
 			diffBlock++;
 			/* erase block */
 			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
-			if (ioctl (dev_fd,MEMERASE,&erase) < 0)
-			{
-				log_printf (LOG_NORMAL,"\n");
-				log_printf (LOG_ERROR,
-						"While erasing blocks 0x%.8x-0x%.8x on %s: %m\n",
-						(unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device);
-				exit (EXIT_FAILURE);
-			}
+			safe_memerase(dev_fd,device,&erase,flags & FLAG_VERBOSE);
 
 			/* write to device */
 			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#4 lore raw

PATCH 4/7 misc-utils: flashcp: simplify logging

In reply to Brandon Maier

Most of the uses of log_printf fall into two styles

Quoted 2 lines

if (flags & FLAG_VERBOSE)

    log_printf(LOG_NORMAL, ...);

or

Quoted 2 lines

log_printf(LOG_ERROR, ...)
exit(EXIT_FAILURE);

Replace them with log_verbose and log_failure respectively.

---
 misc-utils/flashcp.c | 173 +++++++++++++++++++------------------------
 1 file changed, 77 insertions(+), 96 deletions(-)
misc-utils/flashcp.c +77 -96
index 8cb0dbb..9e669b4 100644
@@ -48,7 +48,7 @@
 /* for debugging purposes only */
 #ifdef DEBUG
 #undef DEBUG
-#define DEBUG(fmt,args...) { log_printf (LOG_ERROR,"%d: ",__LINE__); log_printf (LOG_ERROR,fmt,## args); }
+#define DEBUG(fmt,args...) { fprintf (stderr,"%d: ",__LINE__); fprintf (stderr,fmt,## args); }
 #else
 #undef DEBUG
 #define DEBUG(fmt,args...)
@@ -62,7 +62,6 @@
 
 /* cmd-line flags */
 #define FLAG_NONE		0x00
-#define FLAG_VERBOSE	0x01
 #define FLAG_HELP		0x02
 #define FLAG_FILENAME	0x04
 #define FLAG_DEVICE		0x08
@@ -73,21 +72,34 @@
 #define LOG_NORMAL	1
 #define LOG_ERROR	2
 
-static void log_printf (int level,const char *fmt, ...)
+static NORETURN void log_failure (const char *fmt, ...)
 {
-	FILE *fp = level == LOG_NORMAL ? stdout : stderr;
 	va_list ap;
 	va_start (ap,fmt);
-	vfprintf (fp,fmt,ap);
+	vfprintf (stderr,fmt,ap);
 	va_end (ap);
-	fflush (fp);
+	fflush (stderr);
+
+	exit (EXIT_FAILURE);
 }
 
-static NORETURN void showusage(bool error)
+static int verbose = 0;
+static void log_verbose (const char *fmt, ...)
 {
-	int level = error ? LOG_ERROR : LOG_NORMAL;
+	va_list ap;
 
-	log_printf (level,
+	if (!verbose)
+		return;
+
+	va_start (ap,fmt);
+	vfprintf (stdout,fmt,ap);
+	va_end (ap);
+	fflush (stdout);
+}
+
+static NORETURN void showusage(bool error)
+{
+	fprintf (error ? stderr : stdout,
 			"\n"
 			"Flash Copy - Written by Abraham van der Merwe <[email protected]>\n"
 			"\n"
@@ -110,44 +122,42 @@ static NORETURN void showusage(bool error)
 
 static int safe_open (const char *pathname,int flags)
 {
+	const char *access = "unknown";
 	int fd;
 
 	fd = open (pathname,flags);
 	if (fd < 0)
 	{
-		log_printf (LOG_ERROR,"While trying to open %s",pathname);
 		if (flags & O_RDWR)
-			log_printf (LOG_ERROR," for read/write access");
+			access = "read/write";
 		else if (flags & O_RDONLY)
-			log_printf (LOG_ERROR," for read access");
+			access = "read";
 		else if (flags & O_WRONLY)
-			log_printf (LOG_ERROR," for write access");
-		log_printf (LOG_ERROR,": %m\n");
-		exit (EXIT_FAILURE);
+			access = "write";
+
+		log_failure ("While trying to open %s for %s access: %m\n",pathname,access);
 	}
 
 	return (fd);
 }
 
-static void safe_read (int fd,const char *filename,void *buf,size_t count,bool verbose)
+static void safe_read (int fd,const char *filename,void *buf,size_t count)
 {
 	ssize_t result;
 
 	result = read (fd,buf,count);
 	if (count != result)
 	{
-		if (verbose) log_printf (LOG_NORMAL,"\n");
+		log_verbose ("\n");
 		if (result < 0)
 		{
-			log_printf (LOG_ERROR,"While reading data from %s: %m\n",filename);
-			exit (EXIT_FAILURE);
+			log_failure("While reading data from %s: %m\n",filename);
 		}
-		log_printf (LOG_ERROR,"Short read count returned while reading from %s\n",filename);
-		exit (EXIT_FAILURE);
+		log_failure("Short read count returned while reading from %s\n",filename);
 	}
 }
 
-static void safe_write (int fd,const void *buf,size_t count,size_t written,unsigned long long to_write,const char *device,bool verbose)
+static void safe_write (int fd,const void *buf,size_t count,size_t written,unsigned long long to_write,const char *device)
 {
 	ssize_t result;
 
@@ -155,18 +165,14 @@ static void safe_write (int fd,const void *buf,size_t count,size_t written,unsig
 	result = write (fd,buf,count);
 	if (count != result)
 	{
-		if (verbose) log_printf (LOG_NORMAL,"\n");
+		log_verbose ("\n");
 		if (result < 0)
 		{
-			log_printf (LOG_ERROR,
-					"While writing data to 0x%.8lx-0x%.8lx on %s: %m\n",
+			log_failure("While writing data to 0x%.8lx-0x%.8lx on %s: %m\n",
 					written,written + count,device);
-			exit (EXIT_FAILURE);
 		}
-		log_printf (LOG_ERROR,
-				"Short write count returned while writing to x%.8zx-0x%.8zx on %s: %zu/%llu bytes written to flash\n",
+		log_failure("Short write count returned while writing to x%.8zx-0x%.8zx on %s: %zu/%llu bytes written to flash\n",
 				written,written + count,device,written + result,to_write);
-		exit (EXIT_FAILURE);
 	}
 }
 
@@ -177,8 +183,7 @@ static off_t safe_lseek (int fd,off_t offset,int whence,const char *filename)
 	off = lseek (fd,offset,whence);
 	if (off < 0)
 	{
-		log_printf (LOG_ERROR,"While seeking on %s: %m\n",filename);
-		exit (EXIT_FAILURE);
+		log_failure("While seeking on %s: %m\n",filename);
 	}
 
 	return off;
@@ -189,15 +194,13 @@ static void safe_rewind (int fd,const char *filename)
 	safe_lseek(fd,0L,SEEK_SET,filename);
 }
 
-static void safe_memerase (int fd,const char *device,struct erase_info_user *erase,bool verbose)
+static void safe_memerase (int fd,const char *device,struct erase_info_user *erase)
 {
 	if (ioctl (fd,MEMERASE,erase) < 0)
 	{
-		if (verbose) log_printf (LOG_NORMAL,"\n");
-		log_printf (LOG_ERROR,
-				"While erasing blocks 0x%.8x-0x%.8x on %s: %m\n",
+		log_verbose ("\n");
+		log_failure("While erasing blocks 0x%.8x-0x%.8x on %s: %m\n",
 				(unsigned int) erase->start,(unsigned int) (erase->start + erase->length),device);
-		exit (EXIT_FAILURE);
 	}
 }
 
@@ -250,7 +253,7 @@ int main (int argc,char *argv[])
 				DEBUG("Got FLAG_HELP\n");
 				break;
 			case 'v':
-				flags |= FLAG_VERBOSE;
+				verbose = 1;
 				DEBUG("Got FLAG_VERBOSE\n");
 				break;
 			case 'p':
@@ -290,24 +293,17 @@ int main (int argc,char *argv[])
 	if (ioctl (dev_fd,MEMGETINFO,&mtd) < 0)
 	{
 		DEBUG("ioctl(): %m\n");
-		log_printf (LOG_ERROR,"This doesn't seem to be a valid MTD flash device!\n");
-		exit (EXIT_FAILURE);
+		log_failure("This doesn't seem to be a valid MTD flash device!\n");
 	}
 
 	/* get some info about the file we want to copy */
 	fil_fd = safe_open (filename,O_RDONLY);
 	if (fstat (fil_fd,&filestat) < 0)
-	{
-		log_printf (LOG_ERROR,"While trying to get the file status of %s: %m\n",filename);
-		exit (EXIT_FAILURE);
-	}
+		log_failure("While trying to get the file status of %s: %m\n",filename);
 
 	/* does it fit into the device/partition? */
 	if (filestat.st_size > mtd.size)
-	{
-		log_printf (LOG_ERROR,"%s won't fit into %s!\n",filename,device);
-		exit (EXIT_FAILURE);
-	}
+		log_failure("%s won't fit into %s!\n",filename,device);
 
 	/* diff block flashcp */
 	if (flags & FLAG_PARTITION)
@@ -333,24 +329,24 @@ int main (int argc,char *argv[])
 		erase.length *= mtd.erasesize;
 	}
 
-	if (flags & FLAG_VERBOSE)
+	if (verbose)
 	{
 		/* if the user wants verbose output, erase 1 block at a time and show him/her what's going on */
 		int blocks = erase.length / mtd.erasesize;
 		erase.length = mtd.erasesize;
-		log_printf (LOG_NORMAL,"Erasing blocks: 0/%d (0%%)",blocks);
+		log_verbose ("Erasing blocks: 0/%d (0%%)",blocks);
 		for (i = 1; i <= blocks; i++)
 		{
-			log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (%d%%)",i,blocks,PERCENTAGE (i,blocks));
-			safe_memerase(dev_fd,device,&erase,flags & FLAG_VERBOSE);
+			log_verbose ("\rErasing blocks: %d/%d (%d%%)",i,blocks,PERCENTAGE (i,blocks));
+			safe_memerase(dev_fd,device,&erase);
 			erase.start += mtd.erasesize;
 		}
-		log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (100%%)\n",blocks,blocks);
+		log_verbose ("\rErasing blocks: %d/%d (100%%)\n",blocks,blocks);
 	}
 	else
 	{
 		/* if not, erase the whole chunk in one shot */
-		safe_memerase(dev_fd,device,&erase,flags & FLAG_VERBOSE);
+		safe_memerase(dev_fd,device,&erase);
 	}
 	DEBUG("Erased %u / %luk bytes\n",erase.length,filestat.st_size);
 
@@ -358,33 +354,30 @@ int main (int argc,char *argv[])
 	 * write the entire file to flash *
 	 **********************************/
 
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
+	log_verbose ("Writing data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	size = filestat.st_size;
 	i = BUFSIZE;
 	written = 0;
 	while (size)
 	{
 		if (size < BUFSIZE) i = size;
-		if (flags & FLAG_VERBOSE)
-			log_printf (LOG_NORMAL,"\rWriting data: %dk/%lluk (%llu%%)",
-					KB (written + i),
-					KB ((unsigned long long)filestat.st_size),
-					PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
+		log_verbose ("\rWriting data: %dk/%lluk (%llu%%)",
+				KB (written + i),
+				KB ((unsigned long long)filestat.st_size),
+				PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
 
 		/* read from filename */
-		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
+		safe_read (fil_fd,filename,src,i);
 
 		/* write to device */
-		safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device,flags & FLAG_VERBOSE);
+		safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device);
 
 		written += i;
 		size -= i;
 	}
-	if (flags & FLAG_VERBOSE)
-		log_printf (LOG_NORMAL,
-				"\rWriting data: %lluk/%lluk (100%%)\n",
-				KB ((unsigned long long)filestat.st_size),
-				KB ((unsigned long long)filestat.st_size));
+	log_verbose ("\rWriting data: %lluk/%lluk (100%%)\n",
+			KB ((unsigned long long)filestat.st_size),
+			KB ((unsigned long long)filestat.st_size));
 	DEBUG("Wrote %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size);
 
 	/**********************************
@@ -396,40 +389,32 @@ int main (int argc,char *argv[])
 	size = filestat.st_size;
 	i = BUFSIZE;
 	written = 0;
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
+	log_verbose ("Verifying data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	while (size)
 	{
 		if (size < BUFSIZE) i = size;
-		if (flags & FLAG_VERBOSE)
-			log_printf (LOG_NORMAL,
-					"\rVerifying data: %luk/%lluk (%llu%%)",
-					KB (written + i),
-					KB ((unsigned long long)filestat.st_size),
-					PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
+		log_verbose ("\rVerifying data: %luk/%lluk (%llu%%)",
+				KB (written + i),
+				KB ((unsigned long long)filestat.st_size),
+				PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
 
 		/* read from filename */
-		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
+		safe_read (fil_fd,filename,src,i);
 
 		/* read from device */
-		safe_read (dev_fd,device,dest,i,flags & FLAG_VERBOSE);
+		safe_read (dev_fd,device,dest,i);
 
 		/* compare buffers */
 		if (memcmp (src,dest,i))
-		{
-			log_printf (LOG_ERROR,
-					"File does not seem to match flash data. First mismatch at 0x%.8zx-0x%.8zx\n",
+			log_failure("File does not seem to match flash data. First mismatch at 0x%.8zx-0x%.8zx\n",
 					written,written + i);
-			exit (EXIT_FAILURE);
-		}
 
 		written += i;
 		size -= i;
 	}
-	if (flags & FLAG_VERBOSE)
-		log_printf (LOG_NORMAL,
-				"\rVerifying data: %lluk/%lluk (100%%)\n",
-				KB ((unsigned long long)filestat.st_size),
-				KB ((unsigned long long)filestat.st_size));
+	log_verbose ("\rVerifying data: %lluk/%lluk (100%%)\n",
+			KB ((unsigned long long)filestat.st_size),
+			KB ((unsigned long long)filestat.st_size));
 	DEBUG("Verified %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size);
 
 	exit (EXIT_SUCCESS);
@@ -451,22 +436,18 @@ DIFF_BLOCKS:
 	int blocks = erase.length / mtd.erasesize;
 	erase.length = mtd.erasesize;
 
-	if (flags & FLAG_VERBOSE)
-		log_printf (LOG_NORMAL,
-				"\rProcessing blocks: 0/%d (%d%%)", blocks, PERCENTAGE (0,blocks));
+	log_verbose ("\rProcessing blocks: 0/%d (%d%%)", blocks, PERCENTAGE (0,blocks));
 	for (int s = 1; s <= blocks; s++)
 	{
 		if (size < mtd.erasesize) i = size;
-		if (flags & FLAG_VERBOSE)
-			log_printf (LOG_NORMAL,
-					"\rProcessing blocks: %d/%d (%d%%)", s, blocks, PERCENTAGE (s,blocks));
+		log_verbose ("\rProcessing blocks: %d/%d (%d%%)", s, blocks, PERCENTAGE (s,blocks));
 
 		/* read from filename */
-		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
+		safe_read (fil_fd,filename,src,i);
 
 		/* read from device */
 		current_dev_block = safe_lseek(dev_fd, 0, SEEK_CUR, device);
-		safe_read (dev_fd,device,dest,i,flags & FLAG_VERBOSE);
+		safe_read (dev_fd,device,dest,i);
 
 		/* compare buffers, if not the same, erase and write the block */
 		if (memcmp (src,dest,i))
@@ -474,11 +455,11 @@ DIFF_BLOCKS:
 			diffBlock++;
 			/* erase block */
 			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
-			safe_memerase(dev_fd,device,&erase,flags & FLAG_VERBOSE);
+			safe_memerase(dev_fd,device,&erase);
 
 			/* write to device */
 			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
-			safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device,flags & FLAG_VERBOSE);
+			safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device);
 		}
 
 		erase.start += i;
@@ -486,7 +467,7 @@ DIFF_BLOCKS:
 		size -= i;
 	}
 
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL, "\ndiff blocks: %d\n", diffBlock);
+	log_verbose ("\ndiff blocks: %d\n", diffBlock);
 
 	exit (EXIT_SUCCESS);
 }
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#5 lore raw

PATCH 5/7 misc-utils: flashcp: fix buffer overflow

In reply to Brandon Maier

The DIFF_BLOCKS code requires that src and dest buffers be large enough to hold one MTD erasesize. This is because each loop operates on one eraseblock so that it can erase and write one whole sector. But the src and dest buffers are fixed at BUFSIZE, so on platforms where the MTD erasesize are larger then BUFSIZE it will overflow the buffers.

Instead allocate the buffers dynamically so that they can be sized to fit the erasesize.

---
 misc-utils/flashcp.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
misc-utils/flashcp.c +13 -8
index 9e669b4..02be081 100644
@@ -57,9 +57,6 @@
 #define KB(x) ((x) / 1024)
 #define PERCENTAGE(x,total) (((x) * 100) / (total))
 
-/* size of read/write buffer */
-#define BUFSIZE (10 * 1024)
-
 /* cmd-line flags */
 #define FLAG_NONE		0x00
 #define FLAG_HELP		0x02
@@ -222,7 +219,7 @@ int main (int argc,char *argv[])
 	struct mtd_info_user mtd;
 	struct erase_info_user erase;
 	struct stat filestat;
-	unsigned char src[BUFSIZE],dest[BUFSIZE];
+	unsigned char *src,*dest;
 
 	/*********************
 	 * parse cmd-line
@@ -305,6 +302,14 @@ int main (int argc,char *argv[])
 	if (filestat.st_size > mtd.size)
 		log_failure("%s won't fit into %s!\n",filename,device);
 
+	src = malloc(mtd.erasesize);
+	if (!src)
+		log_failure("Malloc failed");
+
+	dest = malloc(mtd.erasesize);
+	if (!dest)
+		log_failure("Malloc failed");
+
 	/* diff block flashcp */
 	if (flags & FLAG_PARTITION)
 	{
@@ -356,11 +361,11 @@ int main (int argc,char *argv[])
 
 	log_verbose ("Writing data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	size = filestat.st_size;
-	i = BUFSIZE;
+	i = mtd.erasesize;
 	written = 0;
 	while (size)
 	{
-		if (size < BUFSIZE) i = size;
+		if (size < mtd.erasesize) i = size;
 		log_verbose ("\rWriting data: %dk/%lluk (%llu%%)",
 				KB (written + i),
 				KB ((unsigned long long)filestat.st_size),
@@ -387,12 +392,12 @@ int main (int argc,char *argv[])
 	safe_rewind (fil_fd,filename);
 	safe_rewind (dev_fd,device);
 	size = filestat.st_size;
-	i = BUFSIZE;
+	i = mtd.erasesize;
 	written = 0;
 	log_verbose ("Verifying data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	while (size)
 	{
-		if (size < BUFSIZE) i = size;
+		if (size < mtd.erasesize) i = size;
 		log_verbose ("\rVerifying data: %luk/%lluk (%llu%%)",
 				KB (written + i),
 				KB ((unsigned long long)filestat.st_size),
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#6 lore raw

PATCH 6/7 misc-utils: flashcp: verify data in --partition

In reply to Brandon Maier

The --partition mode is not verifying that data is being written successfully.

---
 misc-utils/flashcp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
misc-utils/flashcp.c +9 -0
index 02be081..13ff2f1 100644
@@ -465,6 +465,15 @@ DIFF_BLOCKS:
 			/* write to device */
 			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
 			safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device);
+
+			/* read from device */
+			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
+			safe_read (dev_fd,device,dest,i);
+
+			/* compare buffers for write success */
+			if (memcmp (src,dest,i))
+				log_failure("File does not seem to match flash data. First mismatch at 0x%.8zx-0x%.8zx\n",
+						written,written + i);
 		}
 
 		erase.start += i;
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#7 lore raw

PATCH 7/7 misc-utils: flashcp: abort on --partition and --erase-all

In reply to Brandon Maier
---
 misc-utils/flashcp.c | 3 +++
 1 file changed, 3 insertions(+)
misc-utils/flashcp.c +3 -0
index 13ff2f1..80f2086 100644
@@ -283,6 +283,9 @@ int main (int argc,char *argv[])
 	if (flags & FLAG_HELP || device == NULL)
 		showusage(flags != FLAG_HELP);
 
+	if (flags & FLAG_PARTITION && flags & FLAG_ERASE_ALL)
+		log_failure("Option --partition does not support --erase-all\n");
+
 	atexit (cleanup);
 
 	/* get some info about the flash device */
Signature · 6 lines
2.38.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/