Add data size validation

This commit is contained in:
Scott Lahteine 2018-01-03 20:40:28 -06:00
parent 51977c64ee
commit 5d26f88bd5
2 changed files with 34 additions and 16 deletions

View File

@ -332,7 +332,7 @@ void MarlinSettings::postprocess() {
#endif #endif
void MarlinSettings::write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) { void MarlinSettings::write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
if (eeprom_error) return; if (eeprom_error) { pos += size; return; }
while (size--) { while (size--) {
uint8_t * const p = (uint8_t * const)pos; uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value; uint8_t v = *value;
@ -354,7 +354,7 @@ void MarlinSettings::postprocess() {
} }
void MarlinSettings::read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool force/*=false*/) { void MarlinSettings::read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool force/*=false*/) {
if (eeprom_error) return; if (eeprom_error) { pos += size; return; }
do { do {
uint8_t c = eeprom_read_byte((unsigned char*)pos); uint8_t c = eeprom_read_byte((unsigned char*)pos);
if (!validating || force) *value = c; if (!validating || force) *value = c;
@ -364,6 +364,15 @@ void MarlinSettings::postprocess() {
} while (--size); } while (--size);
} }
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
*/ */
@ -780,7 +789,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),
@ -799,6 +808,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);
} }
// //
@ -1289,19 +1300,13 @@ 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();
#if ENABLED(EEPROM_CHITCHAT)
SERIAL_ECHO_START(); SERIAL_ECHO_START();
SERIAL_ECHO(version); SERIAL_ECHOPAIR("Index: ", int(eeprom_index - (EEPROM_OFFSET)));
SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET)); SERIAL_ECHOLNPAIR(" Size: ", datasize());
SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)working_crc);
SERIAL_ECHOLNPGM(")");
#endif
} }
} else if (working_crc != stored_crc) {
else {
eeprom_error = true; eeprom_error = true;
#if ENABLED(EEPROM_CHITCHAT) #if ENABLED(EEPROM_CHITCHAT)
SERIAL_ERROR_START(); SERIAL_ERROR_START();
@ -1311,7 +1316,19 @@ void MarlinSettings::postprocess() {
SERIAL_ERROR(working_crc); SERIAL_ERROR(working_crc);
SERIAL_ERRORLNPGM(" (calculated)!"); SERIAL_ERRORLNPGM(" (calculated)!");
#endif #endif
if (!validating) 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

@ -94,6 +94,7 @@ class MarlinSettings {
static bool _load(); static bool _load();
static void write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc); static void write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc);
static void read_data(int &pos, uint8_t *value, uint16_t size, uint16_t *crc, const bool force=false); static void read_data(int &pos, uint8_t *value, uint16_t size, uint16_t *crc, const bool force=false);
static bool size_error(const uint16_t size);
#endif #endif
}; };