Skip to content

Commit 6c74dce

Browse files
authored
Merge pull request #6074 from geky/fix-fat-sfd
fatfs: Remove extra MBR block
2 parents ccff46d + 5b09daf commit 6c74dce

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

features/TESTS/filesystem/multipart_fat_filesystem/main.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,46 @@ void test_read_write() {
163163
TEST_ASSERT_EQUAL(0, err);
164164
}
165165

166+
void test_single_mbr() {
167+
int err = bd.init();
168+
TEST_ASSERT_EQUAL(0, err);
169+
170+
const bd_addr_t MBR_OFFSET = 0;
171+
const bd_addr_t FAT1_OFFSET = 1;
172+
const bd_addr_t FAT2_OFFSET = BLOCK_COUNT/2;
173+
174+
uint8_t *buffer = (uint8_t *)malloc(BLOCK_SIZE);
175+
TEST_ASSERT(buffer);
176+
177+
// Check that all three header blocks have the 0x55aa signature
178+
err = bd.read(buffer, MBR_OFFSET*BLOCK_SIZE, BLOCK_SIZE);
179+
TEST_ASSERT_EQUAL(0, err);
180+
TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE-2], "\x55\xaa", 2) == 0);
181+
182+
err = bd.read(buffer, FAT1_OFFSET*BLOCK_SIZE, BLOCK_SIZE);
183+
TEST_ASSERT_EQUAL(0, err);
184+
TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE-2], "\x55\xaa", 2) == 0);
185+
186+
err = bd.read(buffer, FAT2_OFFSET*BLOCK_SIZE, BLOCK_SIZE);
187+
TEST_ASSERT_EQUAL(0, err);
188+
TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE-2], "\x55\xaa", 2) == 0);
189+
190+
// Check that the headers for both filesystems contain a jump code
191+
// indicating they are actual FAT superblocks and not an extra MBR
192+
err = bd.read(buffer, FAT1_OFFSET*BLOCK_SIZE, BLOCK_SIZE);
193+
TEST_ASSERT_EQUAL(0, err);
194+
TEST_ASSERT(buffer[0] == 0xe9 || buffer[0] == 0xeb || buffer[0] == 0xe8);
195+
196+
err = bd.read(buffer, FAT2_OFFSET*BLOCK_SIZE, BLOCK_SIZE);
197+
TEST_ASSERT_EQUAL(0, err);
198+
TEST_ASSERT(buffer[0] == 0xe9 || buffer[0] == 0xeb || buffer[0] == 0xe8);
199+
200+
free(buffer);
201+
202+
bd.deinit();
203+
TEST_ASSERT_EQUAL(0, err);
204+
}
205+
166206

167207
// Test setup
168208
utest::v1::status_t test_setup(const size_t number_of_cases) {
@@ -174,6 +214,7 @@ Case cases[] = {
174214
Case("Testing formating", test_format),
175215
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
176216
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
217+
Case("Testing for no extra MBRs", test_single_mbr),
177218
};
178219

179220
Specification specification(test_setup, cases);

features/filesystem/bd/MBRBlockDevice.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ int MBRBlockDevice::init()
211211
}
212212

213213
// Check for valid entry
214-
if (table->entries[_part-1].type == 0x00) {
214+
// 0x00 = no entry
215+
// 0x05, 0x0f = extended partitions, currently not supported
216+
if ((table->entries[_part-1].type == 0x00 ||
217+
table->entries[_part-1].type == 0x05 ||
218+
table->entries[_part-1].type == 0x0f)) {
215219
delete[] buffer;
216220
return BD_ERROR_INVALID_PARTITION;
217221
}

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ int FATFileSystem::format(BlockDevice *bd, bd_size_t cluster_size)
340340

341341
// Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
342342
fs.lock();
343-
FRESULT res = f_mkfs(fs._fsid, FM_ANY, cluster_size, NULL, 0);
343+
FRESULT res = f_mkfs(fs._fsid, FM_ANY | FM_SFD, cluster_size, NULL, 0);
344344
fs.unlock();
345345
if (res != FR_OK) {
346346
return fat_error_remap(res);

0 commit comments

Comments
 (0)