Selaa lähdekoodia

Add data size validation

Scott Lahteine 7 vuotta sitten
vanhempi
commit
760c912ab9

+ 34
- 14
Marlin/src/module/configuration_store.cpp Näytä tiedosto

249
 
249
 
250
 MarlinSettings settings;
250
 MarlinSettings settings;
251
 
251
 
252
+uint16_t MarlinSettings::datasize() { return sizeof(SettingsData); }
253
+
252
 /**
254
 /**
253
  * Post-process after Retrieve or Reset
255
  * Post-process after Retrieve or Reset
254
  */
256
  */
331
     int16_t MarlinSettings::meshes_begin;
333
     int16_t MarlinSettings::meshes_begin;
332
   #endif
334
   #endif
333
 
335
 
336
+  bool MarlinSettings::size_error(const uint16_t size) {
337
+    if (size != datasize()) {
338
+      SERIAL_ERROR_START();
339
+      SERIAL_ERRORLNPGM("EEPROM datasize error.");
340
+      return true;
341
+    }
342
+    return false;
343
+  }
344
+
334
   /**
345
   /**
335
    * M500 - Store Configuration
346
    * M500 - Store Configuration
336
    */
347
    */
750
     #endif
761
     #endif
751
 
762
 
752
     //
763
     //
753
-    // Validate CRC
764
+    // Validate CRC and Data Size
754
     //
765
     //
755
     if (!eeprom_error) {
766
     if (!eeprom_error) {
756
       const uint16_t eeprom_size = eeprom_index - (EEPROM_OFFSET),
767
       const uint16_t eeprom_size = eeprom_index - (EEPROM_OFFSET),
769
         SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)final_crc);
780
         SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)final_crc);
770
         SERIAL_ECHOLNPGM(")");
781
         SERIAL_ECHOLNPGM(")");
771
       #endif
782
       #endif
783
+
784
+      eeprom_error |= size_error(eeprom_size);
772
     }
785
     }
773
     EEPROM_FINISH();
786
     EEPROM_FINISH();
774
 
787
 
1260
         for (uint8_t q = MAX_EXTRUDERS * 2; q--;) EEPROM_READ(dummy);
1273
         for (uint8_t q = MAX_EXTRUDERS * 2; q--;) EEPROM_READ(dummy);
1261
       #endif
1274
       #endif
1262
 
1275
 
1263
-      if (working_crc == stored_crc) {
1264
-        if (!validating) {
1265
-          postprocess();
1266
-          #if ENABLED(EEPROM_CHITCHAT)
1267
-            SERIAL_ECHO_START();
1268
-            SERIAL_ECHO(version);
1269
-            SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET));
1270
-            SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)working_crc);
1271
-            SERIAL_ECHOLNPGM(")");
1272
-          #endif
1273
-        }
1276
+      eeprom_error = size_error(eeprom_index - (EEPROM_OFFSET));
1277
+      if (eeprom_error) {
1278
+        SERIAL_ECHO_START();
1279
+        SERIAL_ECHOPAIR("Index: ", int(eeprom_index - (EEPROM_OFFSET)));
1280
+        SERIAL_ECHOLNPAIR(" Size: ", datasize());
1274
       }
1281
       }
1275
-      else {
1282
+      else if (working_crc != stored_crc) {
1283
+        eeprom_error = true;
1276
         #if ENABLED(EEPROM_CHITCHAT)
1284
         #if ENABLED(EEPROM_CHITCHAT)
1277
           SERIAL_ERROR_START();
1285
           SERIAL_ERROR_START();
1278
           SERIAL_ERRORPGM("EEPROM CRC mismatch - (stored) ");
1286
           SERIAL_ERRORPGM("EEPROM CRC mismatch - (stored) ");
1281
           SERIAL_ERROR(working_crc);
1289
           SERIAL_ERROR(working_crc);
1282
           SERIAL_ERRORLNPGM(" (calculated)!");
1290
           SERIAL_ERRORLNPGM(" (calculated)!");
1283
         #endif
1291
         #endif
1284
-        reset();
1292
+      }
1293
+      else if (!validating) {
1294
+        #if ENABLED(EEPROM_CHITCHAT)
1295
+          SERIAL_ECHO_START();
1296
+          SERIAL_ECHO(version);
1297
+          SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET));
1298
+          SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)working_crc);
1299
+          SERIAL_ECHOLNPGM(")");
1300
+        #endif
1301
+      }
1302
+
1303
+      if (!validating) {
1304
+        if (eeprom_error) reset(); else postprocess();
1285
       }
1305
       }
1286
 
1306
 
1287
       #if ENABLED(AUTO_BED_LEVELING_UBL)
1307
       #if ENABLED(AUTO_BED_LEVELING_UBL)

+ 3
- 0
Marlin/src/module/configuration_store.h Näytä tiedosto

29
   public:
29
   public:
30
     MarlinSettings() { }
30
     MarlinSettings() { }
31
 
31
 
32
+    static uint16_t datasize();
33
+
32
     static void reset();
34
     static void reset();
33
     static bool save();   // Return 'true' if data was saved
35
     static bool save();   // Return 'true' if data was saved
34
 
36
 
90
       #endif
92
       #endif
91
 
93
 
92
       static bool _load();
94
       static bool _load();
95
+      static bool size_error(const uint16_t size);
93
     #endif
96
     #endif
94
 };
97
 };
95
 
98
 

Loading…
Peruuta
Tallenna