From fd2ff867a08fd174f0bbf7a03aa59547634e91ac Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 22 Jul 2020 23:45:08 +1000 Subject: stm32/usbdev: Fix calculation of SCSI LUN size with multiple LUNs. The SCSI driver calls GetCapacity to get the block size and number of blocks of the underlying block-device/LUN. It caches these values and uses them later on to verify that reads/writes are within the bounds of the LUN. But, prior to this commit, there was only one set of cached values for all LUNs, so the bounds checking for a LUN could use incorrect values, values from one of the other LUNs that most recently updated the cached values. This would lead to failed SCSI requests. This commit fixes this issue by having separate cached values for each LUN. Signed-off-by: Damien George --- ports/stm32/usbdev/class/src/usbd_msc_scsi.c | 34 +++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'ports/stm32/usbdev/class/src') diff --git a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c index d0413b758..2eb716ccd 100644 --- a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c +++ b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c @@ -247,7 +247,7 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_ { USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; - if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) + if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr[lun], &hmsc->scsi_blk_size[lun]) != 0) { SCSI_SenseCode(pdev, lun, @@ -258,15 +258,17 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_ else { - hmsc->bot_data[0] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 24); - hmsc->bot_data[1] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 16); - hmsc->bot_data[2] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 8); - hmsc->bot_data[3] = (uint8_t)(hmsc->scsi_blk_nbr - 1); + uint32_t blk_nbr = hmsc->scsi_blk_nbr[lun]; + hmsc->bot_data[0] = (uint8_t)((blk_nbr - 1) >> 24); + hmsc->bot_data[1] = (uint8_t)((blk_nbr - 1) >> 16); + hmsc->bot_data[2] = (uint8_t)((blk_nbr - 1) >> 8); + hmsc->bot_data[3] = (uint8_t)(blk_nbr - 1); - hmsc->bot_data[4] = (uint8_t)(hmsc->scsi_blk_size >> 24); - hmsc->bot_data[5] = (uint8_t)(hmsc->scsi_blk_size >> 16); - hmsc->bot_data[6] = (uint8_t)(hmsc->scsi_blk_size >> 8); - hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_size); + uint32_t blk_size = hmsc->scsi_blk_size[lun]; + hmsc->bot_data[4] = (uint8_t)(blk_size >> 24); + hmsc->bot_data[5] = (uint8_t)(blk_size >> 16); + hmsc->bot_data[6] = (uint8_t)(blk_size >> 8); + hmsc->bot_data[7] = (uint8_t)(blk_size); hmsc->bot_data_length = 8; return 0; @@ -516,7 +518,7 @@ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *para } hmsc->bot_state = USBD_BOT_DATA_IN; - hmsc->scsi_blk_len *= hmsc->scsi_blk_size; + hmsc->scsi_blk_len *= hmsc->scsi_blk_size[lun]; /* cases 4,5 : Hi <> Dn */ if (hmsc->cbw.dDataLength != hmsc->scsi_blk_len) @@ -596,7 +598,7 @@ static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa return -1; /* error */ } - hmsc->scsi_blk_len *= hmsc->scsi_blk_size; + hmsc->scsi_blk_len *= hmsc->scsi_blk_size[lun]; /* cases 3,11,13 : Hn,Ho <> D0 */ if (hmsc->cbw.dDataLength != hmsc->scsi_blk_len) @@ -670,7 +672,7 @@ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , u { USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; - if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr ) + if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr[lun]) { SCSI_SenseCode(pdev, lun, @@ -697,7 +699,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) if( hmsc->bdev_ops->Read(lun , hmsc->bot_data, hmsc->scsi_blk_addr_in_blks, - len / hmsc->scsi_blk_size) < 0) + len / hmsc->scsi_blk_size[lun]) < 0) { SCSI_SenseCode(pdev, @@ -714,7 +716,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) len); - hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size; + hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size[lun]; hmsc->scsi_blk_len -= len; /* case 6 : Hi = Di */ @@ -744,7 +746,7 @@ static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) if(hmsc->bdev_ops->Write(lun , hmsc->bot_data, hmsc->scsi_blk_addr_in_blks, - len / hmsc->scsi_blk_size) < 0) + len / hmsc->scsi_blk_size[lun]) < 0) { SCSI_SenseCode(pdev, lun, @@ -754,7 +756,7 @@ static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) } - hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size; + hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size[lun]; hmsc->scsi_blk_len -= len; /* case 12 : Ho = Do */ -- cgit v1.2.3