mirror of
https://github.com/opentx/opentx.git
synced 2025-07-14 20:10:08 +03:00
[Horus] Massstorage bugfix (read cache beyond end of disk)
This commit is contained in:
parent
9d492b4faf
commit
fcdcccf8f5
3 changed files with 17 additions and 11 deletions
|
@ -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) {
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
if (res != RES_OK) {
|
||||
return res;
|
||||
|
@ -88,7 +87,7 @@ DiskCache::DiskCache()
|
|||
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
|
||||
// if (sector < 1000) {
|
||||
|
@ -102,7 +101,13 @@ DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count)
|
|||
return __disk_read(drv, buff, sector, count);
|
||||
}
|
||||
|
||||
for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
|
||||
// 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) {
|
||||
if (blocks[n].read(buff, sector, count)) {
|
||||
++stats.noHits;
|
||||
return RES_OK;
|
||||
|
@ -112,7 +117,7 @@ DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count)
|
|||
++stats.noMisses;
|
||||
|
||||
// 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()) {
|
||||
TRACE_DISK_CACHE("\t\t using free block");
|
||||
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) {
|
||||
lastBlock = 0;
|
||||
}
|
||||
|
||||
return blocks[lastBlock].fill(drv, buff, sector, count);
|
||||
}
|
||||
|
||||
|
@ -147,13 +153,13 @@ int DiskCache::getHitRate() const
|
|||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "opentx.h"
|
||||
#include "FatFs/diskio.h"
|
||||
#include "sdio_sd.h"
|
||||
|
||||
#if defined(__cplusplus) && !defined(SIMU)
|
||||
extern "C" {
|
||||
|
@ -38,8 +39,7 @@ enum MassstorageLuns {
|
|||
};
|
||||
|
||||
/* USB Mass storage Standard Inquiry Data */
|
||||
const unsigned char STORAGE_Inquirydata[] = {//36
|
||||
|
||||
const unsigned char STORAGE_Inquirydata[] = { //36
|
||||
/* LUN 0 */
|
||||
0x00,
|
||||
0x80,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue