Skip to content

Commit

Permalink
sfdp: Implemented a validation step for the SFDP Basic Parameter Tabl…
Browse files Browse the repository at this point in the history
…e Header
  • Loading branch information
dragonmux committed Dec 7, 2023
1 parent 2895d64 commit 63a9e9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/include/sfdpInternal.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ namespace bmpflash::sfdp

[[nodiscard]] uint16_t jedecParameterID() const noexcept
{ return static_cast<uint16_t>((jedecParameterIDHigh << 8U) | jedecParameterIDLow); }
[[nodiscard]] size_t tableLength() const noexcept { return static_cast<size_t>(tableLengthInU32s) * 4U; }
[[nodiscard]] size_t tableLength() const noexcept { return static_cast<size_t>(tableLengthInU32s * 4U); }
void validate() noexcept;
};

struct memoryDensity_t
Expand Down
47 changes: 47 additions & 0 deletions src/sfdp.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ namespace bmpflash::sfdp
displayTableHeader(tableHeader, idx + 1U);
if (tableHeader.jedecParameterID() == basicSPIParameterTable)
{
tableHeader.validate();
if (!displayBasicParameterTable(probe, tableHeader))
return false;
}
Expand Down Expand Up @@ -185,7 +186,10 @@ namespace bmpflash::sfdp
return std::nullopt;

if (tableHeader.jedecParameterID() == basicSPIParameterTable)
{
tableHeader.validate();
return readBasicParameterTable(probe, tableHeader);
}
}
return std::nullopt;
}
Expand Down Expand Up @@ -226,4 +230,47 @@ namespace bmpflash::sfdp
return tableLength();
}
}

void parameterTableHeader_t::validate() noexcept
{
const auto expectedLength{lengthForVersion()};
const auto actualLength{tableLength()};
// If the table is the proper length for the version, we're done
if (actualLength == expectedLength)
return;

// If the table is longer than it should be for the stated version, truncate it
if (actualLength > expectedLength)
tableLengthInU32s = static_cast<uint8_t>(expectedLength / 4U);
// Otherwise fix the version number to match the one for the actual length
else
{
// 24 uint32_t's -> v1.8
if (expectedLength == 96U)
{
versionMajor = 1U;
versionMinor = 8U;
}
// 21 uint32_t's -> v1.7
else if (expectedLength == 84U)
{
versionMajor = 1U;
versionMinor = 7U;
}
// 16 uint32_t's -> v1.6 (assume the newer standard)
else if (expectedLength == 64U)
{
versionMajor = 1U;
versionMinor = 6U;
}
// 9 uint32_t's -> v1.4 (assume the newer standard)
else if (expectedLength == 36U)
{
versionMajor = 1U;
versionMinor = 4U;
}
else
console.error("This should not be possible, please check sfdp.cxx for sanity");
}
}
} // namespace bmpflash::sfdp

0 comments on commit 63a9e9e

Please sign in to comment.