Skip to content

Commit 7aa1357

Browse files
committed
fix: macos hang and sector 0 corruption
1 parent a1ce6ba commit 7aa1357

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/machine/usb/msc/msc.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (m *msc) run(b []byte, isEpOut bool) bool {
288288
m.sentBytes = 0
289289
m.respStatus = csw.StatusPassed
290290

291-
m.scsiCmdBegin(b)
291+
m.scsiCmdBegin()
292292

293293
case mscStateData:
294294
// Transfer data
@@ -322,9 +322,8 @@ func (m *msc) run(b []byte, isEpOut bool) bool {
322322
if m.totalBytes > m.sentBytes && m.cbw.isIn() {
323323
// 6.7.2 The Thirteen Cases - Case 5 (Hi > Di): STALL before status
324324
m.stallEndpoint(usb.MSC_ENDPOINT_IN)
325-
} else if m.sendZLP || m.queuedBytes == m.maxPacketSize {
326-
// If the last packet is wMaxPacketSize we need to first send a zero-length packet
327-
// to indicate the end of the transfer before we can send a CSW
325+
} else if m.sendZLP {
326+
// Send a zero-length packet to force the end of the transfer before we send a CSW
328327
m.queuedBytes = 0
329328
m.sendZLP = false
330329
m.sendUSBPacket(m.buf[:0])

src/machine/usb/msc/scsi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"machine/usb/msc/scsi"
88
)
99

10-
func (m *msc) scsiCmdBegin(b []byte) {
10+
func (m *msc) scsiCmdBegin() {
1111
cmd := m.cbw.SCSICmd()
1212
cmdType := cmd.CmdType()
1313

1414
// Handle multi-packet commands
1515
switch cmdType {
1616
case scsi.CmdRead, scsi.CmdWrite:
17-
m.scsiCmdReadWrite(cmd, b)
17+
m.scsiCmdReadWrite(cmd)
1818
return
1919
case scsi.CmdUnmap:
2020
m.scsiCmdUnmap(cmd)

src/machine/usb/msc/scsi_readwrite.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
var invalidWriteError = errors.New("invalid write offset or length")
1010

11-
func (m *msc) scsiCmdReadWrite(cmd scsi.Cmd, b []byte) {
11+
func (m *msc) scsiCmdReadWrite(cmd scsi.Cmd) {
1212
status := m.validateScsiReadWrite(cmd)
1313
if status != csw.StatusPassed {
1414
m.sendScsiError(status, scsi.SenseIllegalRequest, scsi.SenseCodeInvalidCmdOpCode)
@@ -62,8 +62,9 @@ func (m *msc) validateScsiReadWrite(cmd scsi.Cmd) csw.Status {
6262

6363
func (m *msc) usbToRawOffset(lba, offset uint32) (int64, int64) {
6464
// Convert the emulated block address to the underlying hardware block's start and offset
65-
byteOffset := lba*m.blockSizeUSB + m.blockOffset + offset
66-
return int64(byteOffset), int64(byteOffset % m.blockSizeRaw)
65+
rawLBA := (lba*m.blockSizeUSB + offset) / m.blockSizeRaw
66+
rawBlockOffset := int64((lba*m.blockSizeUSB + offset) % m.blockSizeRaw)
67+
return int64(m.blockOffset + rawLBA*m.blockSizeRaw), rawBlockOffset
6768
}
6869

6970
func (m *msc) readBlock(b []byte, lba, offset uint32) (n int, err error) {

0 commit comments

Comments
 (0)