diff --git a/firmware/common/file.c b/firmware/common/file.c index 01ae17224d..c44f7af809 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c @@ -543,6 +543,17 @@ static int readwrite(int fd, void* buf, long count, bool write) file->dirty = true; } else { + /* Flush the cache first if it's dirty. */ + if (file->dirty) + { + rc = flush_cache(fd); + if ( rc < 0 ) { + errno = EIO; + return rc * 10 - 8; + } + file->cacheoffset = -1; + } + rc = fat_readwrite(&(file->fatfile), 1, &(file->cache),false); if (rc < 1 ) { DEBUGF("Failed caching sector\n");