Skip to content

Commit f30a766

Browse files
committed
usb/msc: erase blocks before writing to them
We could perhaps check whether they've been erased already (via unmap or something) but the default of most OSes seems to be to _not_ unmap blocks so we will have to erase in most cases anyway.
1 parent 3869f76 commit f30a766

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/machine/usb/msc/msc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func newMSC(dev machine.BlockDevice) *msc {
7373
m := &msc{
7474
// Some platforms require reads/writes to be aligned to the full underlying hardware block
7575
blockCache: make([]byte, dev.WriteBlockSize()),
76-
blockSizeUSB: 512,
76+
blockSizeUSB: max(uint32(dev.EraseBlockSize()), 512),
7777
buf: make([]byte, dev.WriteBlockSize()),
7878
cswBuf: make([]byte, csw.MsgLen),
7979
cbw: &CBW{Data: make([]byte, 31)},

src/machine/usb/msc/scsi_readwrite.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ func (m *msc) writeBlock(b []byte, lba, offset uint32) (n int, err error) {
9191
return 0, invalidWriteError
9292
}
9393

94+
// Erase the block first if needed.
95+
if uint32(blockStart)%m.blockSizeUSB == 0 {
96+
firstBlock := uint32(blockStart) / uint32(m.dev.EraseBlockSize())
97+
numBlocks := (m.blockSizeUSB + uint32(m.dev.EraseBlockSize()) - 1) / uint32(m.dev.EraseBlockSize())
98+
m.dev.EraseBlocks(int64(firstBlock), int64(numBlocks))
99+
}
100+
94101
// Write the full block to the underlying device
95102
n, err = m.dev.WriteAt(b, blockStart)
96103
n -= int(blockOffset)

0 commit comments

Comments
 (0)