Skip to content

Commit fc741f0

Browse files
authored
Merge pull request #8346 from OpenNuvoton/nuvoton_fix_mbr
storage: fix valid partion check with windows formatted sd card
2 parents 93a17c1 + 8ef23ff commit fc741f0

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
@@ -99,6 +99,18 @@ static int partition_absolute(
9999
memset(table->entries, 0, sizeof(table->entries));
100100
}
101101

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

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

0 commit comments

Comments
 (0)