diff --git a/features/TESTS/filesystem/fopen/fopen.cpp b/features/TESTS/filesystem/fopen/fopen.cpp index f19c1c0c47..a0c95b3ae4 100644 --- a/features/TESTS/filesystem/fopen/fopen.cpp +++ b/features/TESTS/filesystem/fopen/fopen.cpp @@ -1231,6 +1231,24 @@ control_t fsfat_fopen_test_21(const size_t call_count) return CaseNext; } +/** @brief test for operation of SDFileSystem::format() + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_22(const size_t call_count) +{ + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + int32_t ret = -1; + + /* the allocation_unit of 0 means chanFS will use the default for the card (varies according to capacity). */ + ret = sd.format(0); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to format sdcard (ret=%d)\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + return CaseNext; +} + @@ -1319,6 +1337,7 @@ Case cases[] = { #ifdef FOPEN_NOT_IMPLEMENTED Case("FSFAT_FOPEN_TEST_21: todo.", FSFAT_FOPEN_TEST_21), #endif /* FOPEN_NOT_IMPLEMENTED */ + Case("FSFAT_FOPEN_TEST_22: format() test.", FSFAT_FOPEN_TEST_22), }; diff --git a/features/filesystem/fat/FATFileSystem.cpp b/features/filesystem/fat/FATFileSystem.cpp index 2e4e0ef9ff..21a6fb6b3b 100644 --- a/features/filesystem/fat/FATFileSystem.cpp +++ b/features/filesystem/fat/FATFileSystem.cpp @@ -323,6 +323,23 @@ int FATFileSystem::rename(const char *oldname, const char *newname) { return 0; } +<<<<<<< HEAD +======= +/* See http://elm-chan.org/fsw/ff/en/mkfs.html for details of f_mkfs() and + * associated arguments. */ +int FATFileSystem::format(int allocation_unit) { + lock(); + FRESULT res = f_mkfs(_fsid, 0, allocation_unit); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) + if (res) { + debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res); + unlock(); + return -1; + } + unlock(); + return 0; +} + +>>>>>>> b86fe65... STORAGE: change FATFileSystem::format() to include allocation_unit argument, to facilitate fixing of inconsistent file systems. DirHandle *FATFileSystem::opendir(const char *name) { lock(); FATFS_DIR dir; diff --git a/features/filesystem/fat/FATFileSystem.h b/features/filesystem/fat/FATFileSystem.h index a0996a30f4..c66a4e66af 100644 --- a/features/filesystem/fat/FATFileSystem.h +++ b/features/filesystem/fat/FATFileSystem.h @@ -74,6 +74,18 @@ public: */ virtual int rename(const char *oldname, const char *newname); + /** + * Formats a logical drive, FDISK partitioning rule. + * + * @param allocation_unit + * This is the number of bytes per cluster size. The valid value is N + * times the sector size. N is a power of 2 from 1 to 128 for FAT + * volume and upto 16MiB for exFAT volume. If zero is given, + * the default allocation unit size is selected depending on the volume + * size. + */ + virtual int format(int allocation_unit = 0); + /** * Opens a directory on the filesystem */