Skip to content

mbr: Added assertions for overlapping partitions #6700

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 1 commit into from
Apr 30, 2018
Merged
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
14 changes: 14 additions & 0 deletions features/filesystem/bd/MBRBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ static int partition_absolute(
table->entries[part-1].type = type;

// lba dimensions
MBED_ASSERT(bd->is_valid_erase(offset, size));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering... shouldn't the assert work on LBA? if yes, it should be moved to L103 MBED_ASSERT(bd->is_valid_erase(lba_offset, lba_size));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, lba_offset and lba_size is in units of erase blocks. The offset and size are in units of bytes. bd->is_valid_erase operates in units of bytes.

We could also have an assert on lba_offset and lba_size but I thought it would be redundant. Do you think we should have that as well?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, don't bother.

uint32_t sector = std::max<uint32_t>(bd->get_erase_size(), 512);
uint32_t lba_offset = offset / sector;
uint32_t lba_size = size / sector;
Expand All @@ -106,6 +107,19 @@ static int partition_absolute(
tochs(lba_offset, table->entries[part-1].chs_start);
tochs(lba_offset+lba_size-1, table->entries[part-1].chs_stop);

// Check that we don't overlap other entries
for (int i = 1; i <= 4; i++) {
if (i != part && table->entries[i-1].type != 0x00) {
uint32_t neighbor_lba_offset = fromle32(table->entries[i-1].lba_offset);
uint32_t neighbor_lba_size = fromle32(table->entries[i-1].lba_size);
MBED_ASSERT(
(lba_offset >= neighbor_lba_offset + neighbor_lba_size) ||
(lba_offset + lba_size <= neighbor_lba_offset));
(void)neighbor_lba_offset;
(void)neighbor_lba_size;
}
}

// Write out MBR
err = bd->erase(0, bd->get_erase_size());
if (err) {
Expand Down