AVRISP-MKII Clone: Add EEPROM magic number validation
authorSergey Vlasov <sigprof@gmail.com>
Sun, 26 Sep 2021 09:58:48 +0000 (12:58 +0300)
committerSergey Vlasov <sigprof@gmail.com>
Sun, 26 Sep 2021 13:25:33 +0000 (16:25 +0300)
If the compiled AVRISP-MKII Clone firmware is flashed into a previously
used chip without writing the corresponding initial EEPROM data, the
code could not detect that the stored parameter data was not valid, and
blindly used whatever values were left in the EEPROM.  This was
especially problematic when the reset polarity happened to be set
wrongly, because avrdude does not have any options to set that parameter
(it sends the corresponding commands only if it detects STK500 or
STK600, and does not send that command for AVRISP-MKII).

Add a check for a 4-byte magic number in the EEPROM, so that any data
which was left there by some different code could be properly ignored
and erased before using the EEPROM storage for AVRISP-MKII Clone data.

Projects/AVRISP-MKII/Lib/V2ProtocolParams.c

index c119228..c42c6c4 100644 (file)
@@ -42,6 +42,14 @@ static uint8_t EEMEM EEPROM_Reset_Polarity = 0x01;
 /* Non-Volatile Parameter Values for EEPROM storage */
 static uint8_t EEMEM EEPROM_SCK_Duration   = 0x06;
 
+/* Randomly generated magic constant for EEPROM data validation */
+#define EEPROM_MAGIC 0xEB401412UL
+
+/* EEPROM location which should contain EEPROM_MAGIC.  If this value is not correct, all other EEPROM locations are
+ * assumed to contain invalid data, therefore all parameters will be reset to their default values.
+ */
+static uint32_t EEMEM EEPROM_Magic_Value = EEPROM_MAGIC;
+
 /* Volatile Parameter Values for RAM storage */
 static ParameterItem_t ParameterTable[] =
        {
@@ -87,9 +95,29 @@ static ParameterItem_t ParameterTable[] =
        };
 
 
+/** Check the non-volatile storage in the EEPROM for the proper magic value, and erase the parameter area if the magic
+ *  value was not correct.
+ */
+static void V2Params_CheckNonVolatileStorage(void)
+{
+       /* Read the magic value from the EEPROM */
+       uint32_t MagicValue = eeprom_read_dword(&EEPROM_Magic_Value);
+       if (MagicValue != EEPROM_MAGIC) {
+               /* If the magic value was not correct, erase the parameter values in the EEPROM */
+               eeprom_update_byte(&EEPROM_Reset_Polarity, 0xFF);
+               eeprom_update_byte(&EEPROM_SCK_Duration, 0xFF);
+
+               /* Write the correct magic value to confirm that the data stored in the EEPROM is now valid */
+               eeprom_update_dword(&EEPROM_Magic_Value, EEPROM_MAGIC);
+       }
+}
+
 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
 void V2Params_LoadNonVolatileParamValues(void)
 {
+       /* Check that the EEPROM contains valid data */
+       V2Params_CheckNonVolatileStorage();
+
        /* Read parameter values that are stored in non-volatile EEPROM */
        uint8_t ResetPolarity = eeprom_read_byte(&EEPROM_Reset_Polarity);
        uint8_t SCKDuration   = eeprom_read_byte(&EEPROM_SCK_Duration);