diff --git a/fs/littlefs/Kconfig b/fs/littlefs/Kconfig index 86b841c5194..0e9a46fe321 100644 --- a/fs/littlefs/Kconfig +++ b/fs/littlefs/Kconfig @@ -187,4 +187,10 @@ config FS_LITTLEFS_DISK_VERSION 0x00020000 means 2.0. 0x00020001 means 2.1. +config FS_LITTLEFS_DEBUG + bool "LITTLEFS file system read/write double check" + depends on DEBUG_FS_ERROR + ---help--- + Enable LITTLEFS file system read/write double check. + endif diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index 3396635663e..780c0d8a10f 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -1153,6 +1154,49 @@ static int littlefs_rewinddir(FAR struct inode *mountpt, return ret; } +/**************************************************************************** + * Name: littlefs_check + ****************************************************************************/ + +#ifdef CONFIG_FS_LITTLEFS_DEBUG +static void littlefs_check(FAR struct inode *drv, lfs_block_t block, + lfs_size_t size, FAR const void *buffer, + FAR struct mtd_geometry_s *geo) +{ + uint16_t crc16_check; + uint16_t crc16_ori; + int ret; + + crc16_ori = crc16(buffer, geo->blocksize * size); + + if (INODE_IS_MTD(drv)) + { + ret = MTD_BREAD(drv->u.i_mtd, block, size, (FAR void *)buffer); + } + else + { + ret = drv->u.i_bops->read(drv, (FAR void *)buffer, block, size); + } + + if (ret < 0) + { + ferr("[%s] read block failed!\n", __func__); + lib_dumpbuffer("buffer:", buffer, geo->blocksize * size); + DEBUGASSERT(0); + } + else + { + crc16_check = crc16(buffer, geo->blocksize * size); + if (crc16_ori != crc16_check) + { + ferr("CRC16 check failed\n"); + lib_dumpbuffer("check:", buffer, geo->blocksize * size); + DEBUGASSERT(0); + } + } +} +#endif + /**************************************************************************** * Name: littlefs_read_block ****************************************************************************/ @@ -1178,6 +1222,9 @@ static int littlefs_read_block(FAR const struct lfs_config *c, ret = drv->u.i_bops->read(drv, buffer, block, size); } +#ifdef CONFIG_FS_LITTLEFS_DEBUG + littlefs_check(drv, block, size, buffer, geo); +#endif return ret >= 0 ? OK : ret; } @@ -1211,6 +1258,9 @@ static int littlefs_write_block(FAR const struct lfs_config *c, ret = drv->u.i_bops->write(drv, buffer, block, size); } +#ifdef CONFIG_FS_LITTLEFS_DEBUG + littlefs_check(drv, block, size, buffer, geo); +#endif return ret >= 0 ? OK : ret; } diff --git a/fs/romfs/Kconfig b/fs/romfs/Kconfig index 427cf3bf853..e5eaf251eb5 100644 --- a/fs/romfs/Kconfig +++ b/fs/romfs/Kconfig @@ -27,4 +27,10 @@ config FS_ROMFS_CACHE_FILE_NSECTORS ---help--- The number of file cache sector +config FS_ROMFS_DEBUG + bool "ROMFS file system read/write double check" + depends on DEBUG_FS_ERROR + ---help--- + Enable ROMFS file system read/write double check. + endif diff --git a/fs/romfs/fs_romfsutil.c b/fs/romfs/fs_romfsutil.c index 58f1a664283..009bc1e52c1 100644 --- a/fs/romfs/fs_romfsutil.c +++ b/fs/romfs/fs_romfsutil.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -508,6 +509,41 @@ static int romfs_cachenode(FAR struct romfs_mountpt_s *rm, } #endif +/**************************************************************************** + * Name: romfs_check + ****************************************************************************/ + +#ifdef CONFIG_FS_ROMFS_DEBUG +static void romfs_check(FAR struct inode *inode, uint32_t sector, + unsigned int nsectors, FAR uint8_t *buffer, + uint16_t sectorsize) +{ + uint16_t crc16_check; + uint16_t crc16_ori; + ssize_t size; + + crc16_ori = crc16(buffer, sectorsize * nsectors); + + size = inode->u.i_bops->read(inode, buffer, sector, nsectors); + if (size != (ssize_t)nsectors) + { + ferr("Read failed: %zd\n", size); + lib_dumpbuffer("buffer:", buffer, sectorsize * nsectors); + DEBUGASSERT(0); + } + else + { + crc16_check = crc16(buffer, sectorsize * nsectors); + if (crc16_ori != crc16_check) + { + ferr("CRC16 check failed\n"); + lib_dumpbuffer("check:", buffer, sectorsize * nsectors); + DEBUGASSERT(0); + } + } +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -550,6 +586,12 @@ int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer, { ret = -EINVAL; } +#ifdef CONFIG_FS_ROMFS_DEBUG + else + { + romfs_check(inode, sector, nsectors, buffer, rm->rm_hwsectorsize); + } +#endif } return ret;