From 760c912ab98c028c5bcc99e04de4fc34744eb2a4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 4 Jan 2018 20:01:25 -0600 Subject: [PATCH] Add data size validation --- Marlin/src/module/configuration_store.cpp | 48 ++++++++++++++++------- Marlin/src/module/configuration_store.h | 3 ++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/Marlin/src/module/configuration_store.cpp b/Marlin/src/module/configuration_store.cpp index 4ced3576c..4067a6629 100644 --- a/Marlin/src/module/configuration_store.cpp +++ b/Marlin/src/module/configuration_store.cpp @@ -249,6 +249,8 @@ typedef struct SettingsDataStruct { MarlinSettings settings; +uint16_t MarlinSettings::datasize() { return sizeof(SettingsData); } + /** * Post-process after Retrieve or Reset */ @@ -331,6 +333,15 @@ void MarlinSettings::postprocess() { int16_t MarlinSettings::meshes_begin; #endif + bool MarlinSettings::size_error(const uint16_t size) { + if (size != datasize()) { + SERIAL_ERROR_START(); + SERIAL_ERRORLNPGM("EEPROM datasize error."); + return true; + } + return false; + } + /** * M500 - Store Configuration */ @@ -750,7 +761,7 @@ void MarlinSettings::postprocess() { #endif // - // Validate CRC + // Validate CRC and Data Size // if (!eeprom_error) { const uint16_t eeprom_size = eeprom_index - (EEPROM_OFFSET), @@ -769,6 +780,8 @@ void MarlinSettings::postprocess() { SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)final_crc); SERIAL_ECHOLNPGM(")"); #endif + + eeprom_error |= size_error(eeprom_size); } EEPROM_FINISH(); @@ -1260,19 +1273,14 @@ void MarlinSettings::postprocess() { for (uint8_t q = MAX_EXTRUDERS * 2; q--;) EEPROM_READ(dummy); #endif - if (working_crc == stored_crc) { - if (!validating) { - postprocess(); - #if ENABLED(EEPROM_CHITCHAT) - SERIAL_ECHO_START(); - SERIAL_ECHO(version); - SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET)); - SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)working_crc); - SERIAL_ECHOLNPGM(")"); - #endif - } + eeprom_error = size_error(eeprom_index - (EEPROM_OFFSET)); + if (eeprom_error) { + SERIAL_ECHO_START(); + SERIAL_ECHOPAIR("Index: ", int(eeprom_index - (EEPROM_OFFSET))); + SERIAL_ECHOLNPAIR(" Size: ", datasize()); } - else { + else if (working_crc != stored_crc) { + eeprom_error = true; #if ENABLED(EEPROM_CHITCHAT) SERIAL_ERROR_START(); SERIAL_ERRORPGM("EEPROM CRC mismatch - (stored) "); @@ -1281,7 +1289,19 @@ void MarlinSettings::postprocess() { SERIAL_ERROR(working_crc); SERIAL_ERRORLNPGM(" (calculated)!"); #endif - reset(); + } + else if (!validating) { + #if ENABLED(EEPROM_CHITCHAT) + SERIAL_ECHO_START(); + SERIAL_ECHO(version); + SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET)); + SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)working_crc); + SERIAL_ECHOLNPGM(")"); + #endif + } + + if (!validating) { + if (eeprom_error) reset(); else postprocess(); } #if ENABLED(AUTO_BED_LEVELING_UBL) diff --git a/Marlin/src/module/configuration_store.h b/Marlin/src/module/configuration_store.h index 56fd3c2ba..34235533f 100644 --- a/Marlin/src/module/configuration_store.h +++ b/Marlin/src/module/configuration_store.h @@ -29,6 +29,8 @@ class MarlinSettings { public: MarlinSettings() { } + static uint16_t datasize(); + static void reset(); static bool save(); // Return 'true' if data was saved @@ -90,6 +92,7 @@ class MarlinSettings { #endif static bool _load(); + static bool size_error(const uint16_t size); #endif };