Skip to content

storage: fix valid partion check with windows formatted sd card #8346

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
Oct 26, 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
20 changes: 20 additions & 0 deletions features/storage/blockdevice/MBRBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down