[En-Nut-Discussion] PHAT: Bug when writing full sectors

Philipp Burch phip at hb9etc.ch
Sat Feb 2 12:50:31 CET 2013


Hi everyone,

some time ago, I noticed that the FTP server on my Stellaris board 
trashed file writes. It looked very suspicious that byte errors always 
occured from byte 513 onwards. Today I found some time to dig into it 
and it looks to me that the FAT driver incorrectly increases the buffer 
pointer when writing data. The offending code is in phatfs.c, lines 650 
.. 672. The number of full sectors to write is calculated, but the 
number is not propagated back into the 'step' variable. I've set my 
FTP's MSS to 536 bytes, which results in block writes of 536 bytes. This 
now has the effect that 512 bytes (one sector) is written, but the 
remaining 24 bytes are discarded because the buffer pointer then 
immediately increases by the full 536 bytes.

I've now introduced a line which updates the 'step' variable according 
to the real number of sectors. With this change, writes work correctly. 
Tests with a file of 1MiB did not show any differing content.
The patch is attached below. I'll commit it into the devnut-lm3s branch, 
but it should probably be included in trunk before merging the port.

Regards,
Philipp


--- nut/fs/phatfs.c	(revision 4962)
+++ nut/fs/phatfs.c	(working copy)
@@ -657,7 +657,11 @@
                  step = len - rc;
              }
              sect = PhatClusterSector(nfp, fcb->f_clust) + 
fcb->f_clust_pos;
+            /* Calculate number of sectors. */
              cnt = step / vol->vol_sectsz;
+            /* Update step size to sector size multiple. */
+            step = cnt * vol->vol_sectsz;
+
              if (PhatSectorWrite(nfp->nf_dev, sect, &buf[rc], cnt)) {
                  rc = -1;
                  break;


More information about the En-Nut-Discussion mailing list