From 70cfef8630a4e6e0d8429649b8bcee5aeccae3f4 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 12 Feb 2018 17:12:16 -0600 Subject: [PATCH 1/3] fatfs: Removed extra MBR block Regression after ChanFS update: Due to parameter changes in the f_mkfs function, the option to use a separate block for MBR (FDISK) was turned back on. This should be off as it conflicts with an explicit MBR when using the MBRBlockDevice. --- features/filesystem/fat/FATFileSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/filesystem/fat/FATFileSystem.cpp b/features/filesystem/fat/FATFileSystem.cpp index ece5be1432..05e4849609 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); From fd9e4c12fdfe971bbb4a89afbf8222e8e1a23e06 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 12 Feb 2018 18:57:41 -0600 Subject: [PATCH 2/3] Added test to catch multiple nested MBRs --- .../multipart_fat_filesystem/main.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/features/TESTS/filesystem/multipart_fat_filesystem/main.cpp b/features/TESTS/filesystem/multipart_fat_filesystem/main.cpp index 0b064a5acc..47d7b485d2 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); From 5b09daf0f9691665dc3dcbd37a67ff8f2f3a71c4 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 13 Feb 2018 14:03:02 -0600 Subject: [PATCH 3/3] mbr: Added checks for extended partitions --- features/filesystem/bd/MBRBlockDevice.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/features/filesystem/bd/MBRBlockDevice.cpp b/features/filesystem/bd/MBRBlockDevice.cpp index 02dc35fac4..8799e1e3fd 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; }