Skip to content

Commit 8ef23ff

Browse files
committed
Fix mis-recognize that Windows-formatted SD card has valid partitions
For Windows-formatted SD card, it is not partitioned (no MBR), but its PBR has the same boot signature (0xaa55) as MBR. We would easily mis-recognize this SD card has valid partitions if we only check partition type. We add check by only accepting 0x00 (inactive) /0x80 (active) for valid partition status.
1 parent fd4f47d commit 8ef23ff

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

features/storage/blockdevice/MBRBlockDevice.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ static int partition_absolute(
9797
memset(table->entries, 0, sizeof(table->entries));
9898
}
9999

100+
// For Windows-formatted SD card, it is not partitioned (no MBR), but its PBR has the
101+
// same boot signature (0xaa55) as MBR. We would easily mis-recognize this SD card has valid
102+
// partitions if we only check partition type. We add check by only accepting 0x00 (inactive)
103+
// /0x80 (active) for valid partition status.
104+
for (int i = 1; i <= 4; i++) {
105+
if (table->entries[i-1].status != 0x00 &&
106+
table->entries[i-1].status != 0x80) {
107+
memset(table->entries, 0, sizeof(table->entries));
108+
break;
109+
}
110+
}
111+
100112
// Setup new partition
101113
MBED_ASSERT(part >= 1 && part <= 4);
102114
table->entries[part-1].status = 0x00; // inactive (not bootable)
@@ -241,6 +253,14 @@ int MBRBlockDevice::init()
241253
goto fail;
242254
}
243255

256+
// Check for valid partition status
257+
// Same reason as in partition_absolute regarding Windows-formatted SD card
258+
if (table->entries[_part-1].status != 0x00 &&
259+
table->entries[_part-1].status != 0x80) {
260+
err = BD_ERROR_INVALID_PARTITION;
261+
goto fail;
262+
}
263+
244264
// Check for valid entry
245265
// 0x00 = no entry
246266
// 0x05, 0x0f = extended partitions, currently not supported

0 commit comments

Comments
 (0)