Add data size validation

This commit is contained in:
Scott Lahteine 2018-01-04 20:01:25 -06:00
parent b91ca168fb
commit 760c912ab9
2 changed files with 37 additions and 14 deletions

View file

@ -249,6 +249,8 @@ typedef struct SettingsDataStruct {
MarlinSettings settings; MarlinSettings settings;
uint16_t MarlinSettings::datasize() { return sizeof(SettingsData); }
/** /**
* Post-process after Retrieve or Reset * Post-process after Retrieve or Reset
*/ */
@ -331,6 +333,15 @@ void MarlinSettings::postprocess() {
int16_t MarlinSettings::meshes_begin; int16_t MarlinSettings::meshes_begin;
#endif #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 * M500 - Store Configuration
*/ */
@ -750,7 +761,7 @@ void MarlinSettings::postprocess() {
#endif #endif
// //
// Validate CRC // Validate CRC and Data Size
// //
if (!eeprom_error) { if (!eeprom_error) {
const uint16_t eeprom_size = eeprom_index - (EEPROM_OFFSET), 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_ECHOPAIR(" bytes; crc ", (uint32_t)final_crc);
SERIAL_ECHOLNPGM(")"); SERIAL_ECHOLNPGM(")");
#endif #endif
eeprom_error |= size_error(eeprom_size);
} }
EEPROM_FINISH(); EEPROM_FINISH();
@ -1260,19 +1273,14 @@ void MarlinSettings::postprocess() {
for (uint8_t q = MAX_EXTRUDERS * 2; q--;) EEPROM_READ(dummy); for (uint8_t q = MAX_EXTRUDERS * 2; q--;) EEPROM_READ(dummy);
#endif #endif
if (working_crc == stored_crc) { eeprom_error = size_error(eeprom_index - (EEPROM_OFFSET));
if (!validating) { if (eeprom_error) {
postprocess(); SERIAL_ECHO_START();
#if ENABLED(EEPROM_CHITCHAT) SERIAL_ECHOPAIR("Index: ", int(eeprom_index - (EEPROM_OFFSET)));
SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" Size: ", datasize());
SERIAL_ECHO(version);
SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET));
SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)working_crc);
SERIAL_ECHOLNPGM(")");
#endif
}
} }
else { else if (working_crc != stored_crc) {
eeprom_error = true;
#if ENABLED(EEPROM_CHITCHAT) #if ENABLED(EEPROM_CHITCHAT)
SERIAL_ERROR_START(); SERIAL_ERROR_START();
SERIAL_ERRORPGM("EEPROM CRC mismatch - (stored) "); SERIAL_ERRORPGM("EEPROM CRC mismatch - (stored) ");
@ -1281,7 +1289,19 @@ void MarlinSettings::postprocess() {
SERIAL_ERROR(working_crc); SERIAL_ERROR(working_crc);
SERIAL_ERRORLNPGM(" (calculated)!"); SERIAL_ERRORLNPGM(" (calculated)!");
#endif #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) #if ENABLED(AUTO_BED_LEVELING_UBL)

View file

@ -29,6 +29,8 @@ class MarlinSettings {
public: public:
MarlinSettings() { } MarlinSettings() { }
static uint16_t datasize();
static void reset(); static void reset();
static bool save(); // Return 'true' if data was saved static bool save(); // Return 'true' if data was saved
@ -90,6 +92,7 @@ class MarlinSettings {
#endif #endif
static bool _load(); static bool _load();
static bool size_error(const uint16_t size);
#endif #endif
}; };