Skip to content

Commit 4e66f21

Browse files
author
Cruz Monrreal
authored
Merge pull request #6700 from geky/mbr-add-partition-asserts
mbr: Added assertions for overlapping partitions
2 parents a1307d4 + a97a479 commit 4e66f21

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

features/filesystem/bd/MBRBlockDevice.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static int partition_absolute(
9696
table->entries[part-1].type = type;
9797

9898
// lba dimensions
99+
MBED_ASSERT(bd->is_valid_erase(offset, size));
99100
uint32_t sector = std::max<uint32_t>(bd->get_erase_size(), 512);
100101
uint32_t lba_offset = offset / sector;
101102
uint32_t lba_size = size / sector;
@@ -106,6 +107,19 @@ static int partition_absolute(
106107
tochs(lba_offset, table->entries[part-1].chs_start);
107108
tochs(lba_offset+lba_size-1, table->entries[part-1].chs_stop);
108109

110+
// Check that we don't overlap other entries
111+
for (int i = 1; i <= 4; i++) {
112+
if (i != part && table->entries[i-1].type != 0x00) {
113+
uint32_t neighbor_lba_offset = fromle32(table->entries[i-1].lba_offset);
114+
uint32_t neighbor_lba_size = fromle32(table->entries[i-1].lba_size);
115+
MBED_ASSERT(
116+
(lba_offset >= neighbor_lba_offset + neighbor_lba_size) ||
117+
(lba_offset + lba_size <= neighbor_lba_offset));
118+
(void)neighbor_lba_offset;
119+
(void)neighbor_lba_size;
120+
}
121+
}
122+
109123
// Write out MBR
110124
err = bd->erase(0, bd->get_erase_size());
111125
if (err) {

0 commit comments

Comments
 (0)