MTD read-ahear/write buffering layer seems functional

This commit is contained in:
Gregory Nutt 2014-09-25 13:35:10 -06:00
parent 74972dfa59
commit 81243854d8

View File

@ -158,8 +158,6 @@ int mtdrwb_main(int argc, char *argv[])
ssize_t nbytes; ssize_t nbytes;
off_t nblocks; off_t nblocks;
off_t offset; off_t offset;
off_t check;
off_t sectoff;
off_t seekpos; off_t seekpos;
unsigned int blkpererase; unsigned int blkpererase;
int fd; int fd;
@ -280,7 +278,7 @@ int mtdrwb_main(int argc, char *argv[])
for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++) for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++)
{ {
buffer[k] = offset; buffer[k] = offset;
offset += 4; offset += sizeof(uint32_t);
} }
/* And write it using the character driver */ /* And write it using the character driver */
@ -297,9 +295,9 @@ int mtdrwb_main(int argc, char *argv[])
close(fd); close(fd);
/* Open the MTD character driver for writing */ /* Open the MTD character driver for reading */
fd = open("/dev/mtd0", O_RDWR); fd = open("/dev/mtd0", O_RDONLY);
if (fd < 0) if (fd < 0)
{ {
message("ERROR: open /dev/mtd0 failed: %d\n", errno); message("ERROR: open /dev/mtd0 failed: %d\n", errno);
@ -309,21 +307,16 @@ int mtdrwb_main(int argc, char *argv[])
/* Now verify the offset in every block */ /* Now verify the offset in every block */
check = offset; offset = 0;
sectoff = 0;
for (j = 0; j < nblocks; j++) for (j = 0; j < nblocks; j++)
{ {
#if 0 /* Too much */
message(" block=%u offset=%lu\n", j, (unsigned long) check);
#endif
/* Seek to the next read position */ /* Seek to the next read position */
seekpos = lseek(fd, sectoff, SEEK_SET); seekpos = lseek(fd, offset, SEEK_SET);
if (seekpos != sectoff) if (seekpos != offset)
{ {
message("ERROR: lseek to offset %ld failed: %d\n", message("ERROR: lseek to offset %ld failed: %d\n",
(unsigned long)sectoff, errno); (unsigned long)offset, errno);
msgflush(); msgflush();
exit(10); exit(10);
} }
@ -356,17 +349,17 @@ int mtdrwb_main(int argc, char *argv[])
* indication. * indication.
*/ */
else if (nbytes == 0) else if (nbytes == 0)
{ {
message("ERROR: Unexpected end of file on /dev/mtd0\n"); message("ERROR: Unexpected end of file on /dev/mtd0\n");
msgflush(); msgflush();
exit(14); exit(14);
} }
/* This is not expected at all */ /* This is not expected at all */
else if (nbytes != geo.blocksize) else if (nbytes != geo.blocksize)
{ {
message("ERROR: Short read from /dev/mtd0 failed: %lu\n", message("ERROR: Short read from /dev/mtd0 failed: %lu\n",
(unsigned long)nbytes); (unsigned long)nbytes);
msgflush(); msgflush();
@ -377,53 +370,16 @@ int mtdrwb_main(int argc, char *argv[])
for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++) for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++)
{ {
if (buffer[k] != check) if (buffer[k] != offset)
{ {
message("ERROR: Bad offset %lu, expected %lu\n", message("ERROR: Bad offset %lu, expected %lu\n",
(long)buffer[k], (long)check); (long)buffer[k], (long)offset);
msgflush(); msgflush();
exit(16); exit(16);
} }
/* Invert the value to indicate that we have verified offset += sizeof(uint32_t);
* this value.
*/
buffer[k] = ~check;
check += sizeof(uint32_t);
} }
/* Seek to the next write position */
seekpos = lseek(fd, sectoff, SEEK_SET);
if (seekpos != sectoff)
{
message("ERROR: lseek to offset %ld failed: %d\n",
(unsigned long)sectoff, errno);
msgflush();
exit(17);
}
/* Now write the block back to FLASH with the modified value */
nbytes = write(fd, buffer, geo.blocksize);
if (nbytes < 0)
{
message("ERROR: write to /dev/mtd0 failed: %d\n", errno);
msgflush();
exit(18);
}
else if (nbytes != geo.blocksize)
{
message("ERROR: Unexpected write size to /dev/mtd0 : %ld\n",
(unsigned long)nbytes);
msgflush();
exit(19);
}
/* Get the offset to the next block */
sectoff += geo.blocksize;
} }
/* Try reading one more time. We should get the end of file */ /* Try reading one more time. We should get the end of file */