From a6d6fa382f0cf9781d9dceb54e7900fe9403e98c Mon Sep 17 00:00:00 2001 From: dragonmux Date: Thu, 7 Dec 2023 10:44:02 +0000 Subject: [PATCH] sfdp: Implemented a validation step for the SFDP Basic Parameter Table Header --- src/include/sfdpInternal.hxx | 3 ++- src/sfdp.cxx | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/include/sfdpInternal.hxx b/src/include/sfdpInternal.hxx index 80fba09..b1a5d89 100644 --- a/src/include/sfdpInternal.hxx +++ b/src/include/sfdpInternal.hxx @@ -63,7 +63,8 @@ namespace bmpflash::sfdp [[nodiscard]] uint16_t jedecParameterID() const noexcept { return static_cast((jedecParameterIDHigh << 8U) | jedecParameterIDLow); } - [[nodiscard]] size_t tableLength() const noexcept { return static_cast(tableLengthInU32s) * 4U; } + [[nodiscard]] size_t tableLength() const noexcept { return static_cast(tableLengthInU32s * 4U); } + void validate() noexcept; }; struct memoryDensity_t diff --git a/src/sfdp.cxx b/src/sfdp.cxx index 86edbc7..b9f0c24 100644 --- a/src/sfdp.cxx +++ b/src/sfdp.cxx @@ -111,6 +111,7 @@ namespace bmpflash::sfdp displayTableHeader(tableHeader, idx + 1U); if (tableHeader.jedecParameterID() == basicSPIParameterTable) { + tableHeader.validate(); if (!displayBasicParameterTable(probe, tableHeader)) return false; } @@ -185,7 +186,10 @@ namespace bmpflash::sfdp return std::nullopt; if (tableHeader.jedecParameterID() == basicSPIParameterTable) + { + tableHeader.validate(); return readBasicParameterTable(probe, tableHeader); + } } return std::nullopt; } @@ -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 = 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; + } + // 21 uint32_t's -> v1.4 (assume the newer standard) + else if (expectedLength == 9U) + { + versionMajor = 1U; + versionMinor = 4U; + } + else + console.error("This should not be possible, please check sfdp.cxx for sanity"); + } + } } // namespace bmpflash::sfdp