Skip to content

Commit fd2ff86

Browse files
committed
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 <[email protected]>
1 parent 37e1b5c commit fd2ff86

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

ports/stm32/usbd_msc_interface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#ifndef MICROPY_INCLUDED_STM32_USBD_MSC_INTERFACE_H
2727
#define MICROPY_INCLUDED_STM32_USBD_MSC_INTERFACE_H
2828

29-
#define USBD_MSC_MAX_LUN (2)
30-
3129
extern const USBD_StorageTypeDef usbd_msc_fops;
3230

3331
void usbd_msc_init_lu(size_t lu_n, const void *lu_data);

ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define MSC_MEDIA_PACKET (2048) // was 8192; how low can it go whilst still working?
3535
#define HID_DATA_FS_MAX_PACKET_SIZE (64) // endpoint IN & OUT packet size
3636

37+
// Maximum number of LUN that can be exposed on the MSC interface
38+
#define USBD_MSC_MAX_LUN (2)
39+
3740
// Need to define here for BOT and SCSI layers
3841
#define MSC_IN_EP (0x81)
3942
#define MSC_OUT_EP (0x01)
@@ -78,8 +81,8 @@ typedef struct {
7881
uint8_t scsi_sense_head;
7982
uint8_t scsi_sense_tail;
8083

81-
uint16_t scsi_blk_size;
82-
uint32_t scsi_blk_nbr;
84+
uint16_t scsi_blk_size[USBD_MSC_MAX_LUN];
85+
uint32_t scsi_blk_nbr[USBD_MSC_MAX_LUN];
8386

8487
uint32_t scsi_blk_addr_in_blks;
8588
uint32_t scsi_blk_len;

ports/stm32/usbdev/class/src/usbd_msc_scsi.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_
247247
{
248248
USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData;
249249

250-
if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0)
250+
if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr[lun], &hmsc->scsi_blk_size[lun]) != 0)
251251
{
252252
SCSI_SenseCode(pdev,
253253
lun,
@@ -258,15 +258,17 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_
258258
else
259259
{
260260

261-
hmsc->bot_data[0] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 24);
262-
hmsc->bot_data[1] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 16);
263-
hmsc->bot_data[2] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 8);
264-
hmsc->bot_data[3] = (uint8_t)(hmsc->scsi_blk_nbr - 1);
261+
uint32_t blk_nbr = hmsc->scsi_blk_nbr[lun];
262+
hmsc->bot_data[0] = (uint8_t)((blk_nbr - 1) >> 24);
263+
hmsc->bot_data[1] = (uint8_t)((blk_nbr - 1) >> 16);
264+
hmsc->bot_data[2] = (uint8_t)((blk_nbr - 1) >> 8);
265+
hmsc->bot_data[3] = (uint8_t)(blk_nbr - 1);
265266

266-
hmsc->bot_data[4] = (uint8_t)(hmsc->scsi_blk_size >> 24);
267-
hmsc->bot_data[5] = (uint8_t)(hmsc->scsi_blk_size >> 16);
268-
hmsc->bot_data[6] = (uint8_t)(hmsc->scsi_blk_size >> 8);
269-
hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_size);
267+
uint32_t blk_size = hmsc->scsi_blk_size[lun];
268+
hmsc->bot_data[4] = (uint8_t)(blk_size >> 24);
269+
hmsc->bot_data[5] = (uint8_t)(blk_size >> 16);
270+
hmsc->bot_data[6] = (uint8_t)(blk_size >> 8);
271+
hmsc->bot_data[7] = (uint8_t)(blk_size);
270272

271273
hmsc->bot_data_length = 8;
272274
return 0;
@@ -516,7 +518,7 @@ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *para
516518
}
517519

518520
hmsc->bot_state = USBD_BOT_DATA_IN;
519-
hmsc->scsi_blk_len *= hmsc->scsi_blk_size;
521+
hmsc->scsi_blk_len *= hmsc->scsi_blk_size[lun];
520522

521523
/* cases 4,5 : Hi <> Dn */
522524
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
596598
return -1; /* error */
597599
}
598600

599-
hmsc->scsi_blk_len *= hmsc->scsi_blk_size;
601+
hmsc->scsi_blk_len *= hmsc->scsi_blk_size[lun];
600602

601603
/* cases 3,11,13 : Hn,Ho <> D0 */
602604
if (hmsc->cbw.dDataLength != hmsc->scsi_blk_len)
@@ -670,7 +672,7 @@ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , u
670672
{
671673
USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData;
672674

673-
if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr )
675+
if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr[lun])
674676
{
675677
SCSI_SenseCode(pdev,
676678
lun,
@@ -697,7 +699,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun)
697699
if( hmsc->bdev_ops->Read(lun ,
698700
hmsc->bot_data,
699701
hmsc->scsi_blk_addr_in_blks,
700-
len / hmsc->scsi_blk_size) < 0)
702+
len / hmsc->scsi_blk_size[lun]) < 0)
701703
{
702704

703705
SCSI_SenseCode(pdev,
@@ -714,7 +716,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun)
714716
len);
715717

716718

717-
hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size;
719+
hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size[lun];
718720
hmsc->scsi_blk_len -= len;
719721

720722
/* case 6 : Hi = Di */
@@ -744,7 +746,7 @@ static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun)
744746
if(hmsc->bdev_ops->Write(lun ,
745747
hmsc->bot_data,
746748
hmsc->scsi_blk_addr_in_blks,
747-
len / hmsc->scsi_blk_size) < 0)
749+
len / hmsc->scsi_blk_size[lun]) < 0)
748750
{
749751
SCSI_SenseCode(pdev,
750752
lun,
@@ -754,7 +756,7 @@ static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun)
754756
}
755757

756758

757-
hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size;
759+
hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size[lun];
758760
hmsc->scsi_blk_len -= len;
759761

760762
/* case 12 : Ho = Do */

0 commit comments

Comments
 (0)