Skip to content

fatfs: Remove extra MBR block #6074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions features/TESTS/filesystem/multipart_fat_filesystem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -174,6 +214,7 @@ Case cases[] = {
Case("Testing formating", test_format),
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
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);
Expand Down
6 changes: 5 additions & 1 deletion features/filesystem/bd/MBRBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion features/filesystem/fat/FATFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down