# HG changeset patch # User Ideenmodellierer # Date 1602330704 -7200 # Node ID b1eee27cd02b5de06cae5e6a8b21b4844805797f # Parent 0ad0b26ec56bd238336481afb40dada7c428a4e1 BugFix firmware downgrade to version with less settings: If a downgrade to a firmware version using less setting parameter were done then the settings had been reset to the default values. Root cause was that only increased number of settings was accepted by the read function. Because the layout of the setting structure is fixed (only bytes attached, no change in the order of data values) a downgrade just discarding the no longer used settings is possible => Updated read function to handle reduction of setting parameters. diff -r 0ad0b26ec56b -r b1eee27cd02b Discovery/Src/externLogbookFlash.c --- a/Discovery/Src/externLogbookFlash.c Wed Oct 07 18:07:10 2020 +0200 +++ b/Discovery/Src/externLogbookFlash.c Sat Oct 10 13:51:44 2020 +0200 @@ -568,6 +568,7 @@ uint8_t ext_flash_read_settings(void) { uint8_t returnValue = HAL_BUSY; + uint8_t exit = 0; uint8_t *pData; const uint16_t lengthStandardNow = sizeof(SSettings); uint8_t length_lo, length_hi; @@ -581,25 +582,48 @@ ext_flash_read_block(&length_lo, EF_SETTINGS); ext_flash_read_block(&length_hi, EF_SETTINGS); - while ((length_lo != 0xFF) && (length_hi != 0xFF)) /* get the latest stored setting block */ + while ((length_lo != 0xFF) && (length_hi != 0xFF) && (exit == 0)) /* get the latest stored setting block */ { lengthOnEEPROM = length_hi * 256; lengthOnEEPROM += length_lo; - if(lengthOnEEPROM <= lengthStandardNow) + if(lengthOnEEPROM <= lengthStandardNow) /* EEPROM Header size equal or smaller => settings constant or upgraded */ { ext_flash_read_block_multi(&header, 4, EF_SETTINGS); - if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader)) + if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader)) /* check to allow update of header */ { returnValue = HAL_OK; pSettings->header = header; pData = (uint8_t *)pSettings + 4; /* header */ for(uint16_t i = 0; i < (lengthOnEEPROM-4); i++) ext_flash_read_block(&pData[i], EF_SETTINGS); + if(header != pSettings->header) /* setting layout changed => no additional setting sets expected */ + { + exit = 1; + } + } + else + { + returnValue = HAL_ERROR; + exit = 1; + } + } + else /* size of settings decreased => possible downgrade of firmware */ + { + ext_flash_read_block_multi(&header, 4, EF_SETTINGS); + + if(header > 0xFFFF0014) /* verify that new (old) header should be compatible (only less bytes, no change in layout) */ + { + returnValue = HAL_OK; + pSettings->header = header; + pData = (uint8_t *)pSettings + 4; /* header */ + for(uint16_t i = 0; i < (lengthStandardNow-4); i++) /* only read the data fitting into the structure */ + ext_flash_read_block(&pData[i], EF_SETTINGS); } else { returnValue = HAL_ERROR; } + exit = 1; } ext_flash_read_block(&length_lo, EF_SETTINGS); ext_flash_read_block(&length_hi, EF_SETTINGS);