Skip to contentSkip to conversation

Mtd_debug patch

PATCH Stephen Beaver 2 participants 2 messages May 28, 2010 – Jun 13, 2010 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

The attached patch to the current version of mtd_debug (grabbed on5/28/2010) adds two new capabilities:

usage: mtd_debug info   <device>
       mtd_debug read   <device> <offset> <len> <dest-filename>
       mtd_debug write  <device> <offset> <len> <source-filename>

mtd_debug verify <device> <offset> <cmp-filename> mtd_debug erase <device> <offset> <len> mtd_debug blank <device> <offset> <len>

mtd_debug blank ... checks that an area of flash memory if blank (all 0xFFs) ie it verifies that an erase succeeded.
mtd_debug verify ... checks that the contents of the flash memory match the specified file. Ie it verifies that a write succeeded.

These functions allow things like:

#!/bin/sh

mtd_debug erase /dev/mtd1 0 0x200000 mtd_debug write /dev/mtd1 0 0x150000 uImage if ( mtd_debug verifiy /dev/mtd1 0 uImage ) ; then
reboot
else
# Better try again, or we will die
fi

Both have been tested on NOR and NAND flash, and are just simple modifications to the existing file_from_flash() function.

Steve

Signature · 4 lines
Stephen Beaver | ANXeBusiness | Engineering |219  232-8371 | www.anx.com |
[email protected]
Think Green...do you really need to print this email?  Reduce, reuse,
recycle!
mtd_debug.c +216 -10
@@ -30,6 +30,10 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/*
+ * Added blank check and verify functions, May 2010 - Steve Beaver, ANX ebusiness Corp
+ */
+ 
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -173,6 +177,192 @@
    return (1);
 }
 
+#define BUF_SIZE	(64 * 1024 * sizeof (u_int8_t))
+
+int flash_cmp_file (int fd, u_int32_t offset, const char *filename)
+{
+   u_int8_t *buf = NULL, *filebuf = NULL;
+   int err;
+   FILE *fp;
+   int size = BUF_SIZE;
+   int n, i; 
+   unsigned long len = 0L;
+   
+   if (offset != lseek (fd,offset,SEEK_SET))
+	 {
+		perror ("lseek()");
+		goto err0;
+	 }
+
+   if ((fp = fopen (filename,"r")) == NULL)
+	 {
+		perror ("fopen()");
+		goto err1;
+	 }
+
+retry:
+   if ((buf = (u_int8_t *) malloc (size)) == NULL)
+	 {
+
+		fprintf (stderr, "%s: malloc(%#x)\n", __FUNCTION__, size);
+		if (size != BUF_SIZE) {
+			size = BUF_SIZE;
+			fprintf (stderr, "%s: trying buffer size %#x\n", __FUNCTION__, size);
+			goto retry;
+		}
+		perror ("malloc()");
+		goto err0;
+	 }
+	 
+	 // make ourselves a buffer for the compare file
+	 
+   if ((filebuf = (u_int8_t *) malloc (size)) == NULL)
+	 {
+		fprintf (stderr, "%s: malloc(%#x)\n", __FUNCTION__, size);
+		if (size != BUF_SIZE) {
+			size = BUF_SIZE;
+			fprintf (stderr, "%s: trying file buffer size %#x\n", __FUNCTION__, size);
+			goto retry;
+		}
+		perror ("malloc()");
+		goto err0;
+	 }	 
+
+ do {
+   err = fread(filebuf,1, size, fp);
+   n = err;
+   len += (long)n;
+   
+   if (err < 0)
+	 {
+		fprintf (stderr, "%s: write, size %#x, n %#x\n", __FUNCTION__, size, n);
+		perror ("fread()");
+		goto err2;
+	 }
+	 	   
+   err = read (fd,buf,n);
+   if (err < 0)
+	 {
+		fprintf (stderr, "%s: read, size %#x, n %#x\n", __FUNCTION__, size, n);
+		perror ("read()");
+		goto err2;
+	 }
+	      
+	if(memcmp(buf, filebuf, n) != 0)
+	   goto err3;   
+
+   } while (n == size);
+
+   if (buf != NULL)
+   free (buf);
+   if (filebuf != NULL)
+   free (filebuf);
+   fclose (fp);
+   printf ("0x%.8lx bytes from address 0x%.8x verified\n",len,offset);
+   return (0);
+
+err3:
+   for(i=0;i<n;i++)
+      if(buf[i] != filebuf[i])
+         break;
+
+   
+   printf ("Verify failed at byte 0x%.8lx from offset 0x%.8x\n", (len-n)+i, offset);
+   err2:
+   fclose (fp);
+   err1:
+   if (buf != NULL)
+   free (buf);
+   if (filebuf != NULL)
+   free (filebuf);
+   err0:
+   return (1);
+}
+
+int flash_blank (int fd, u_int32_t offset, size_t len)
+{
+   u_int8_t *buf = NULL, *blankbuf = NULL;
+   int err;
+   int size = len * sizeof (u_int8_t);
+   int n = len;
+   
+   if (offset != lseek (fd,offset,SEEK_SET))
+	 {
+		perror ("lseek()");
+		goto err0;
+	 }
+
+retry:
+   if ((buf = (u_int8_t *) malloc (size)) == NULL)
+	 {
+#define BUF_SIZE	(64 * 1024 * sizeof (u_int8_t))
+		fprintf (stderr, "%s: malloc(%#x)\n", __FUNCTION__, size);
+		if (size != BUF_SIZE) {
+			size = BUF_SIZE;
+			fprintf (stderr, "%s: trying buffer size %#x\n", __FUNCTION__, size);
+			goto retry;
+		}
+		perror ("malloc()");
+		goto err0;
+	 }
+	 
+	 // make ourselves a buffer full of 0xff
+	 
+   if ((blankbuf = (u_int8_t *) malloc (size)) == NULL)
+	 {
+		fprintf (stderr, "%s: malloc(%#x)\n", __FUNCTION__, size);
+		if (size != BUF_SIZE) {
+			size = BUF_SIZE;
+			fprintf (stderr, "%s: trying blank buffer size %#x\n", __FUNCTION__, size);
+			goto retry;
+		}
+		perror ("malloc()");
+		goto err0;
+	 }
+	 
+	memset(blankbuf, 0xff, size);
+	 
+ do {
+   if (n <= size)
+	   size = n;
+	   
+   err = read (fd,buf,size);
+
+   if (err < 0)
+	 {
+		fprintf (stderr, "%s: read, size %#x, n %#x\n", __FUNCTION__, size, n);
+		perror ("read()");
+		goto err2;
+	 }
+
+	if( memcmp(buf, blankbuf, size) != 0)
+	   goto err3;
+	    
+   n -= size;
+   } while (n > 0);
+
+   if (buf != NULL)
+   free (buf);
+
+   if (blankbuf != NULL)
+   free (blankbuf);
+   printf ("%d bytes from offset 0x%.8x in flash were blank\n",len, offset);
+   return (0);
+
+	err3:
+	
+	printf("Non-blank byte(s) found\n");
+	
+   err2:
+
+   if (buf != NULL)
+    free (buf);
+   if (blankbuf != NULL)
+    free(blankbuf);
+   err0:
+   return (1);
+}
+
 int file_to_flash (int fd,u_int32_t offset,u_int32_t len,const char *filename)
 {
    u_int8_t *buf = NULL;
@@ -207,7 +397,7 @@
  do {
    if (n <= size)
 	   size = n;
-   if (fread (buf,size,1,fp) != 1 || ferror (fp))
+   if (fread (buf, size,1, fp) == -1 || ferror (fp))
 	 {
 		fprintf (stderr, "%s: fread, size %#x, n %#x\n", __FUNCTION__, size, n);
 		perror ("fread()");
@@ -359,21 +549,27 @@
 void showusage (const char *progname)
 {
    fprintf (stderr,
-			"usage: %s info <device>\n"
-			"       %s read <device> <offset> <len> <dest-filename>\n"
-			"       %s write <device> <offset> <len> <source-filename>\n"
-			"       %s erase <device> <offset> <len>\n",
+			"usage: %s info   <device>\n"
+			"       %s read   <device> <offset> <len> <dest-filename>\n"
+			"       %s write  <device> <offset> <len> <source-filename>\n"
+			"       %s verify <device> <offset> <cmp-filename>\n"
+			"       %s erase  <device> <offset> <len>\n"
+         "       %s blank  <device> <offset> <len>\n",
+			progname,
 			progname,
 			progname,
 			progname,
+			progname,                  
 			progname);
    exit (1);
 }
 
-#define OPT_INFO	1
-#define OPT_READ	2
-#define OPT_WRITE	3
-#define OPT_ERASE	4
+#define OPT_INFO	 1
+#define OPT_READ	 2
+#define OPT_WRITE	 3
+#define OPT_ERASE	 4
+#define OPT_VERIFY 5
+#define OPT_BLANK  6
 
 int main (int argc,char *argv[])
 {
@@ -391,11 +587,15 @@
 	 option = OPT_WRITE;
    else if (argc == 5 && !strcmp (argv[1],"erase"))
 	 option = OPT_ERASE;
+	else if (argc ==  5 && !strcmp (argv[1], "verify"))
+	 option = OPT_VERIFY;
+	else if (argc ==  5 && !strcmp (argv[1], "blank"))
+	 option = OPT_BLANK; 
    else
 	 showusage (progname);
 
    /* open device */
-   open_flag = (option==OPT_INFO || option==OPT_READ) ? O_RDONLY : O_RDWR;
+   open_flag = (option==OPT_INFO || option==OPT_READ || option == OPT_BLANK || option == OPT_VERIFY) ? O_RDONLY : O_RDWR;
    if ((fd = open (argv[2],O_SYNC | open_flag)) < 0)
 	 {
 		perror ("open()");
@@ -416,6 +616,12 @@
 	  case OPT_ERASE:
 		err = erase_flash (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0));
 		break;
+	  case OPT_VERIFY:
+	   err = flash_cmp_file(fd, strtol(argv[3],NULL,0), argv[4]);
+      break;
+	  case OPT_BLANK:
+	   err = flash_blank(fd, strtol(argv[3],NULL,0), strtol(argv[4],NULL,0));      
+      break;
 	 }
 
    /* close device */
#1 lore raw

Hi Stephen,

Quoted 19 lines · Stephen Beaver

The attached patch to the current version of mtd_debug (grabbed on5/28/2010) adds two new capabilities:

usage: mtd_debug info   <device>
       mtd_debug read   <device> <offset> <len> <dest-filename>
       mtd_debug write  <device> <offset> <len> <source-filename>

mtd_debug verify <device> <offset> <cmp-filename> mtd_debug erase <device> <offset> <len> mtd_debug blank <device> <offset> <len>

mtd_debug blank ... checks that an area of flash memory if blank (all 0xFFs) ie it verifies that an erase succeeded.
mtd_debug verify ... checks that the contents of the flash memory match the specified file. Ie it verifies that a write succeeded.

These functions allow things like:

#!/bin/sh

Quoted 7 lines

mtd_debug erase /dev/mtd1 0 0x200000 mtd_debug write /dev/mtd1 0 0x150000 uImage if ( mtd_debug verifiy /dev/mtd1 0 uImage ) ; then
reboot
else
# Better try again, or we will die
fi

Looks like a useful feature, but I cannot apply your patch because it is not applicable to the latest mtd-utils.git. Also, your patch has many trailing white-spaces. Could you please re-send it in inlined form - it will make it easier to review it and reply to you (it it difficult to comment attached patches).

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)