diff --git a/features/storage/blockdevice/MBRBlockDevice.cpp b/features/storage/blockdevice/MBRBlockDevice.cpp index 0d7e14ca28c..7fe150e4622 100644 --- a/features/storage/blockdevice/MBRBlockDevice.cpp +++ b/features/storage/blockdevice/MBRBlockDevice.cpp @@ -97,6 +97,18 @@ static int partition_absolute( memset(table->entries, 0, sizeof(table->entries)); } + // 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. + for (int i = 1; i <= 4; i++) { + if (table->entries[i-1].status != 0x00 && + table->entries[i-1].status != 0x80) { + memset(table->entries, 0, sizeof(table->entries)); + break; + } + } + // Setup new partition MBED_ASSERT(part >= 1 && part <= 4); table->entries[part-1].status = 0x00; // inactive (not bootable) @@ -241,6 +253,14 @@ int MBRBlockDevice::init() goto fail; } + // Check for valid partition status + // Same reason as in partition_absolute regarding Windows-formatted SD card + if (table->entries[_part-1].status != 0x00 && + table->entries[_part-1].status != 0x80) { + err = BD_ERROR_INVALID_PARTITION; + goto fail; + } + // Check for valid entry // 0x00 = no entry // 0x05, 0x0f = extended partitions, currently not supported