Add caching heuristic for FAT partitions
This changes makes it possible to optimistically write more of the FAT
filesystem to disk rather than assume everything is unstreamed. The
beginning and end of the partition where the FAT tables are stored are
unstreamed.
// Globals since that's how the FatFS code likes to work.
static struct block_cache *output_;
static off_t block_offset_;
-static int block_count_ = 0;
+static size_t block_count_ = 0;
static char *current_file_ = NULL;
static FATFS fs_;
static FIL fil_;
@@ -473,7 +473,12 @@ DRESULT disk_write(BYTE pdrv, /* Physical drive number (0..) */
if (pdrv != 0 || output_ == NULL)
return RES_PARERR;
- if (block_cache_pwrite(output_, buff, FWUP_BLOCK_SIZE * count, FWUP_BLOCK_SIZE * (block_offset_ + sector), false) < 0)
+ // Arbitrarily tell the cache that any sector within 1 MB of the edges
+ // is a "streamed write". This means that it will be optimistically
+ // written to disk and not cached. There are two copies of the FAT,
+ // at the beginning and end of the disk, so try to keep those around.
+ bool streamed = (sector > 2048 && sector < block_count_ - 2048);
+ if (block_cache_pwrite(output_, buff, FWUP_BLOCK_SIZE * count, FWUP_BLOCK_SIZE * (block_offset_ + sector), streamed) < 0)
return RES_ERROR;
else
return 0;
