There could be one of numerous things that could be in error.
I would check:
1) Make sure that the file is actually written to the disk correctly, before hand. "Physically" dump the first few clusters from the file/disk to see that they are actually there to begin with. Your code might be correct, it might be the (emulated) disk that is in error.
2) Print the cluster number within your loop above, making sure that it is the correct next cluster number.
3) Does your Read28() function read 512 bytes or 512 sectors/clusters? Usually, you need to separate file system sizes and block sizes. For example, reading from a hard drive (or any other media) should use a count of blocks since this is the only form of data transfer; count of blocks. The file system may have a different size of "block", case in point, cluster size. In my opinion, your Read28() should expect a count of blocks to read, not bytes.
4) If you follow 3) above, your Read28() can read the whole cluster/file at once.
The next comment(s) don't pertain to your current issue, but you might want to keep this in mind for future development.
Keep media, file system,
and current buffer/task sizes separate. i.e.:
1) The Media Read (Read28() in your case) should have no idea, nor care at all, what size a cluster is. It only cares about block sizes.
2) The File System Read should have no idea, nor care at all, what size a media block is. It only cares about cluster size.
3) The current task/library read should have no idea, nor care at all, what size a cluster or media block is, it should only care about bytes.
Therefore:
1) The current task/library (usually fread()) uses a count of bytes, which then calls the file system driver:
- 1a) One should also note that the fread() library might cache a set amount of bytes, only reading a set amount of bytes at a time, but this is for a later project.
2) The file system converts the passed count of bytes to cluster sizes, which then calls the media driver:
- 2a) ditto 1a.
3) The media driver converts the passed count of clusters to block sizes, which then reads from the media.
- 3a) ditto 2a
(Note, somewhere your conversion routine(s) will need to know both cluster size and block size to be able to convert correctly. It is up to you where this is done. I do it within the file system driver, just before calling the media device driver.)
Note that if the count of bytes to read is less than the current block (for media devices) or cluster (for file system drivers), it is up to that driver to extract only the requested amount of bytes. For example, (excluding any fread() caches), if the fread() requests 128 bytes, the file system still reads a cluster or a count of clusters to read at least 128 bytes, which then calls the media device driver which converts that amount to blocks, reading a block or count of blocks to satisfy the count of clusters.
As for fread() caches, you can now see why the library might want to use a cache if the fread() function was used with single byte reads (such as fgetch()). The library cache initialization code could query the file system driver for a cluster size, setting the library's cache to that size. Then again, the file system driver, if it wishes to have a cache, could query the block device for a block size and cache that amount of data.
Anyway, enough for future development notes. However, please keep this in mind since early development dictates what future development might look like.
Ben
-
http://www.fysnet.net/osdesign_book_series.htm