1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-14 11:59:50 +03:00

[Horus] Massstorage bugfix (read cache beyond end of disk)

This commit is contained in:
Bertrand Songis 2016-07-30 23:30:41 +02:00
parent 9d492b4faf
commit fcdcccf8f5
3 changed files with 17 additions and 11 deletions

View file

@ -43,7 +43,7 @@ DiskCacheBlock::DiskCacheBlock()
{ {
} }
bool DiskCacheBlock::read(BYTE* buff, DWORD sector, UINT count) bool DiskCacheBlock::read(BYTE * buff, DWORD sector, UINT count)
{ {
if (sector >= startSector && (sector+count) <= endSector) { if (sector >= startSector && (sector+count) <= endSector) {
TRACE_DISK_CACHE("\tcache read(%u, %u) from %p", (uint32_t)sector, (uint32_t)count, this); TRACE_DISK_CACHE("\tcache read(%u, %u) from %p", (uint32_t)sector, (uint32_t)count, this);
@ -53,9 +53,8 @@ bool DiskCacheBlock::read(BYTE* buff, DWORD sector, UINT count)
return false; return false;
} }
DRESULT DiskCacheBlock::fill(BYTE drv, BYTE* buff, DWORD sector, UINT count) DRESULT DiskCacheBlock::fill(BYTE drv, BYTE * buff, DWORD sector, UINT count)
{ {
// TODO: check if trying to read beyond the end of disk
DRESULT res = __disk_read(drv, data, sector, DISK_CACHE_BLOCK_SECTORS); DRESULT res = __disk_read(drv, data, sector, DISK_CACHE_BLOCK_SECTORS);
if (res != RES_OK) { if (res != RES_OK) {
return res; return res;
@ -88,7 +87,7 @@ DiskCache::DiskCache()
blocks = new DiskCacheBlock[DISK_CACHE_BLOCKS_NUM]; blocks = new DiskCacheBlock[DISK_CACHE_BLOCKS_NUM];
} }
DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count) DRESULT DiskCache::read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
{ {
// TODO: check if not caching first sectors would improve anything // TODO: check if not caching first sectors would improve anything
// if (sector < 1000) { // if (sector < 1000) {
@ -101,8 +100,14 @@ DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count)
TRACE_DISK_CACHE("\t\t big read(%u, %u)", this, (uint32_t)sector, (uint32_t)count); TRACE_DISK_CACHE("\t\t big read(%u, %u)", this, (uint32_t)sector, (uint32_t)count);
return __disk_read(drv, buff, sector, count); return __disk_read(drv, buff, sector, count);
} }
// if block + cache block size is beyond the end of the disk, then read it directly without using cache
if (sector+DISK_CACHE_BLOCK_SECTORS > SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize) {
TRACE_DISK_CACHE("\t\t cache would be beyond end of disk(%u)", this, (uint32_t)sector);
return __disk_read(drv, buff, sector, count);
}
for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) { for (int n=0; n<DISK_CACHE_BLOCKS_NUM; ++n) {
if (blocks[n].read(buff, sector, count)) { if (blocks[n].read(buff, sector, count)) {
++stats.noHits; ++stats.noHits;
return RES_OK; return RES_OK;
@ -112,7 +117,7 @@ DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count)
++stats.noMisses; ++stats.noMisses;
// find free block // find free block
for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) { for (int n=0; n<DISK_CACHE_BLOCKS_NUM; ++n) {
if (blocks[n].empty()) { if (blocks[n].empty()) {
TRACE_DISK_CACHE("\t\t using free block"); TRACE_DISK_CACHE("\t\t using free block");
return blocks[n].fill(drv, buff, sector, count); return blocks[n].fill(drv, buff, sector, count);
@ -124,6 +129,7 @@ DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count)
if (++lastBlock >= DISK_CACHE_BLOCKS_NUM) { if (++lastBlock >= DISK_CACHE_BLOCKS_NUM) {
lastBlock = 0; lastBlock = 0;
} }
return blocks[lastBlock].fill(drv, buff, sector, count); return blocks[lastBlock].fill(drv, buff, sector, count);
} }
@ -147,13 +153,13 @@ int DiskCache::getHitRate() const
return (stats.noHits * 1000) / all; return (stats.noHits * 1000) / all;
} }
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, UINT count) DRESULT disk_read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
{ {
return diskCache.read(drv, buff, sector, count); return diskCache.read(drv, buff, sector, count);
} }
DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, UINT count) DRESULT disk_write(BYTE drv, const BYTE * buff, DWORD sector, UINT count)
{ {
return diskCache.write(drv, buff, sector, count); return diskCache.write(drv, buff, sector, count);
} }

View file

@ -21,6 +21,7 @@
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "opentx.h" #include "opentx.h"
#include "FatFs/diskio.h" #include "FatFs/diskio.h"
#include "sdio_sd.h"
#if defined(__cplusplus) && !defined(SIMU) #if defined(__cplusplus) && !defined(SIMU)
extern "C" { extern "C" {
@ -38,8 +39,7 @@ enum MassstorageLuns {
}; };
/* USB Mass storage Standard Inquiry Data */ /* USB Mass storage Standard Inquiry Data */
const unsigned char STORAGE_Inquirydata[] = {//36 const unsigned char STORAGE_Inquirydata[] = { //36
/* LUN 0 */ /* LUN 0 */
0x00, 0x00,
0x80, 0x80,

View file

@ -666,7 +666,7 @@ static int8_t SCSI_ProcessRead (uint8_t lun)
{ {
uint32_t len; uint32_t len;
len = MIN(SCSI_blk_len , MSC_MEDIA_PACKET); len = MIN(SCSI_blk_len , MSC_MEDIA_PACKET);
if( USBD_STORAGE_fops->Read(lun , if( USBD_STORAGE_fops->Read(lun ,
MSC_BOT_Data, MSC_BOT_Data,