diff --git a/features/TESTS/filesystem/multipart_fat_filesystem/main.cpp b/features/TESTS/filesystem/multipart_fat_filesystem/main.cpp index 0b064a5acc9..47d7b485d2b 100644 --- a/features/TESTS/filesystem/multipart_fat_filesystem/main.cpp +++ b/features/TESTS/filesystem/multipart_fat_filesystem/main.cpp @@ -163,6 +163,46 @@ void test_read_write() { TEST_ASSERT_EQUAL(0, err); } +void test_single_mbr() { + int err = bd.init(); + TEST_ASSERT_EQUAL(0, err); + + const bd_addr_t MBR_OFFSET = 0; + const bd_addr_t FAT1_OFFSET = 1; + const bd_addr_t FAT2_OFFSET = BLOCK_COUNT/2; + + uint8_t *buffer = (uint8_t *)malloc(BLOCK_SIZE); + TEST_ASSERT(buffer); + + // Check that all three header blocks have the 0x55aa signature + err = bd.read(buffer, MBR_OFFSET*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE-2], "\x55\xaa", 2) == 0); + + err = bd.read(buffer, FAT1_OFFSET*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE-2], "\x55\xaa", 2) == 0); + + err = bd.read(buffer, FAT2_OFFSET*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE-2], "\x55\xaa", 2) == 0); + + // Check that the headers for both filesystems contain a jump code + // indicating they are actual FAT superblocks and not an extra MBR + err = bd.read(buffer, FAT1_OFFSET*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + TEST_ASSERT(buffer[0] == 0xe9 || buffer[0] == 0xeb || buffer[0] == 0xe8); + + err = bd.read(buffer, FAT2_OFFSET*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + TEST_ASSERT(buffer[0] == 0xe9 || buffer[0] == 0xeb || buffer[0] == 0xe8); + + free(buffer); + + bd.deinit(); + TEST_ASSERT_EQUAL(0, err); +} + // Test setup utest::v1::status_t test_setup(const size_t number_of_cases) { @@ -174,6 +214,7 @@ Case cases[] = { Case("Testing formating", test_format), Case("Testing read write < block", test_read_write), Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>), + Case("Testing for no extra MBRs", test_single_mbr), }; Specification specification(test_setup, cases); diff --git a/features/filesystem/bd/MBRBlockDevice.cpp b/features/filesystem/bd/MBRBlockDevice.cpp index 02dc35fac4b..8799e1e3fdc 100644 --- a/features/filesystem/bd/MBRBlockDevice.cpp +++ b/features/filesystem/bd/MBRBlockDevice.cpp @@ -211,7 +211,11 @@ int MBRBlockDevice::init() } // Check for valid entry - if (table->entries[_part-1].type == 0x00) { + // 0x00 = no entry + // 0x05, 0x0f = extended partitions, currently not supported + if ((table->entries[_part-1].type == 0x00 || + table->entries[_part-1].type == 0x05 || + table->entries[_part-1].type == 0x0f)) { delete[] buffer; return BD_ERROR_INVALID_PARTITION; } diff --git a/features/filesystem/fat/FATFileSystem.cpp b/features/filesystem/fat/FATFileSystem.cpp index ece5be1432e..05e48496090 100644 --- a/features/filesystem/fat/FATFileSystem.cpp +++ b/features/filesystem/fat/FATFileSystem.cpp @@ -340,7 +340,7 @@ int FATFileSystem::format(BlockDevice *bd, bd_size_t cluster_size) // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) fs.lock(); - FRESULT res = f_mkfs(fs._fsid, FM_ANY, cluster_size, NULL, 0); + FRESULT res = f_mkfs(fs._fsid, FM_ANY | FM_SFD, cluster_size, NULL, 0); fs.unlock(); if (res != FR_OK) { return fat_error_remap(res);