|
@@ -235,22 +235,19 @@ DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
|
|
|
size_t size = sdc.lbasize;
|
|
|
|
|
|
while (block <= last) {
|
|
|
- struct cache_block *bp = disk_cache_get(block, !!offset);
|
|
|
+ sector_t sector = block << CACHE_BLOCK_BITS;
|
|
|
+ sector_t sectors = min(CACHE_BLOCK_SECTORS, size - sector);
|
|
|
+ size_t block_bytes = sectors << SECTOR_SHIFT;
|
|
|
+ size_t bytes = min(block_bytes - offset, len);
|
|
|
+ struct cache_block *bp;
|
|
|
+
|
|
|
+ bp = disk_cache_get(block, bytes < block_bytes);
|
|
|
if (!bp)
|
|
|
return RES_ERROR;
|
|
|
|
|
|
- size_t bytes = min(CACHE_BLOCK_SIZE - offset, len);
|
|
|
-
|
|
|
memcpy(bp->data + offset, buffer, bytes);
|
|
|
|
|
|
- sector_t sector = block << CACHE_BLOCK_BITS;
|
|
|
- int sectors = CACHE_BLOCK_SECTORS;
|
|
|
-
|
|
|
- if (sector + sectors > size)
|
|
|
- sectors = size - sectors; /* Truncated final block */
|
|
|
-
|
|
|
- if (sdcard_write_sectors(bp->data, block << CACHE_BLOCK_BITS,
|
|
|
- sectors) != sectors) {
|
|
|
+ if (sdcard_write_sectors(bp->data, sector, sectors) != sectors) {
|
|
|
invalidate_block(bp);
|
|
|
return RES_ERROR;
|
|
|
}
|