Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32 USB MSC with multiple luns can cause read/write errors #308

Open
timschuerewegen opened this issue Feb 1, 2025 · 2 comments
Open
Assignees
Labels
bug Something isn't working internal bug tracker Issue confirmed and logged into the internal bug tracking system mw Middleware-related issue or pull-request. usb Universal Serial Bus

Comments

@timschuerewegen
Copy link

I am using STM32Cube_FW_H7_V1.11.2 and found a bug in

\Middlewares\ST\STM32_USB_Device_Library\Class\MSC\Src\usbd_msc_scsi.c

that can cause read/write errors when using multiple luns of different sizes.

This is what happened ...

I was unable to open a text file on the LUN 0 drive after Windows had detected the LUN 0 and 1 drives.

I used WireShark and saw the following:

...
SCSI: Test Unit Ready LUN: 0x00
SCSI: Response LUN: 0x00 (Test Unit Ready) (Good)
SCSI: Test Unit Ready LUN: 0x01
SCSI: Response LUN: 0x01 (Test Unit Ready) (Good)
SCSI: Read(10) LUN: 0x00 (LBA: 0x00587bf0, Len: 1)
SCSI: Response LUN: 0x00 (Read(10)) (Check Condition) with Status: Command Failed (0x01)
...

I put several breakpoints in the SCSI_Read10 function and saw that SCSI_CheckAddressRange was failing because the hmsc->scsi_blk_nbr variable contained the number of blocks for LUN 1, not LUN 0.

The SCSI_Read10, SCSI_Read12, SCSI_Write10 and SCSI_Write12 functions are all using the hmsc->scsi_blk_size and hmsc->scsi_blk_nbr variables but those values can be wrong when using multiple luns of different sizes. Those functions should first update those values via a call to GetCapacity just like the SCSI_ReadCapacity10, SCCSI_ReadCapacity16 and SCSI_ReadFormatCapacity functions do.

I added the following code to the SCSI_Read10, SCSI_Read12, SCSI_Write10 and SCSI_Write12 functions and the issue was fixed.

if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0)
{
  SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
  return -1;
}
@ALABSTM ALABSTM self-assigned this Feb 3, 2025
@ALABSTM ALABSTM added bug Something isn't working mw Middleware-related issue or pull-request. usb Universal Serial Bus labels Feb 3, 2025
@ALABSTM ALABSTM added the internal bug tracker Issue confirmed and logged into the internal bug tracking system label Feb 5, 2025
@ALABSTM ALABSTM moved this from To do to Analyzed in stm32cube-mcu-fw-dashboard Feb 5, 2025
@ALABSTM
Copy link
Contributor

ALABSTM commented Feb 5, 2025

ST Internal Reference: 202361

@ALABSTM
Copy link
Contributor

ALABSTM commented Feb 5, 2025

Hi @timschuerewegen,

You point looks relevant. It has been forwarded to our development teams. I'll keep you informed.

With regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working internal bug tracker Issue confirmed and logged into the internal bug tracking system mw Middleware-related issue or pull-request. usb Universal Serial Bus
Projects
Development

No branches or pull requests

2 participants