From b9acfe000ca0e55fc73d73d2ae0049c626cd903d Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Sun, 6 May 2018 18:35:31 +0200 Subject: [PATCH 1/2] Fix configuration restore regression from 5.13.1 5.13.1a * Fix configuration restore regression from 5.13.1 --- sonoff/_releasenotes.ino | 1 + sonoff/webserver.ino | 48 ++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 9b6207e39..cadfdfda2 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,5 +1,6 @@ /* 5.13.1a * Change user_config.h otaurl to http://sonoff.maddox.co.uk/tasmota/sonoff.bin (#2588, #2602) + * Fix configuration restore regression from 5.13.1 * Fix compile error when ADC is enabled and Rules are disabled (#2608) * Fix rule power trigger when no backlog command is used (#2613) * Fix several timer data input and output errors (#2597, #2620) diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino index f0c3fc6e6..6e8361624 100644 --- a/sonoff/webserver.ino +++ b/sonoff/webserver.ino @@ -330,6 +330,8 @@ uint8_t upload_file_type; uint8_t upload_progress_dot_count; uint8_t config_block_count = 0; uint8_t config_xor_on = 0; +uint8_t config_xor_on_set = CONFIG_FILE_XOR; +uint8_t *settings_new = NULL; // Helper function to avoid code duplication (saves 4k Flash) static void WebGetArg(const char* arg, char* out, size_t max) @@ -977,10 +979,10 @@ void HandleBackupConfiguration() WebServer->send(200, FPSTR(HDR_CTYPE_STREAM), ""); memcpy(buffer, &Settings, sizeof(buffer)); buffer[0] = CONFIG_FILE_SIGN; - buffer[1] = (!CONFIG_FILE_XOR)?0:1; + buffer[1] = (!config_xor_on_set) ? 0 : 1; if (buffer[1]) { for (uint16_t i = 2; i < sizeof(buffer); i++) { - buffer[i] ^= (CONFIG_FILE_XOR +i); + buffer[i] ^= (config_xor_on_set +i); } } myClient.write((const char*)buffer, sizeof(buffer)); @@ -1236,6 +1238,14 @@ void HandleUpgradeFirmwareStart() ExecuteCommand(svalue); } +void SettingsNewFree() +{ + if (settings_new != NULL) { + free(settings_new); + settings_new = NULL; + } +} + void HandleUploadDone() { if (HttpUser()) { return; } @@ -1275,6 +1285,7 @@ void HandleUploadDone() page += FPSTR(HTTP_MSG_RSTRT); restart_flag = 2; } + SettingsNewFree(); page += F("
"); page += FPSTR(HTTP_BTN_MAIN); ShowPage(page); @@ -1302,7 +1313,13 @@ void HandleUploadLoop() SettingsSave(1); // Free flash for upload snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_UPLOAD D_FILE " %s ..."), upload.filename.c_str()); AddLog(LOG_LEVEL_INFO); - if (!upload_file_type) { + if (upload_file_type) { + SettingsNewFree(); + if (!(settings_new = (uint8_t *)malloc(sizeof(Settings)))) { + upload_error = 2; + return; + } + } else { MqttRetryCounter(60); #ifdef USE_EMULATION UdpDisconnect(); @@ -1347,18 +1364,7 @@ void HandleUploadLoop() upload_error = 9; return; } - if (config_xor_on) { - for (uint16_t i = 2; i < upload.currentSize; i++) { - upload.buf[i] ^= (CONFIG_FILE_XOR +i); - } - } - if (0 == config_block_count) { - SettingsDefaultSet2(); - memcpy((char*)&Settings +16, upload.buf +16, upload.currentSize -16); - memcpy((char*)&Settings +8, upload.buf +8, 4); // Restore version and auto upgrade - } else { - memcpy((char*)&Settings +(config_block_count * HTTP_UPLOAD_BUFLEN), upload.buf, upload.currentSize); - } + memcpy(settings_new + (config_block_count * HTTP_UPLOAD_BUFLEN), upload.buf, upload.currentSize); config_block_count++; } } else { // firmware @@ -1376,7 +1382,17 @@ void HandleUploadLoop() if (_serialoutput && (upload_progress_dot_count % 80)) { Serial.println(); } - if (!upload_file_type) { + if (upload_file_type) { + if (config_xor_on) { + for (uint16_t i = 2; i < sizeof(Settings); i++) { + settings_new[i] ^= (config_xor_on_set +i); + } + } + SettingsDefaultSet2(); + memcpy((char*)&Settings +16, settings_new +16, sizeof(Settings) -16); + memcpy((char*)&Settings +8, settings_new +8, 4); // Restore version and auto upgrade + SettingsNewFree(); + } else { if (!Update.end(true)) { // true to set the size to the current progress if (_serialoutput) { Update.printError(Serial); } upload_error = 6; From 8ff8e6e69bf56f98d921038bfc6f4a81b31fa362 Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Sun, 6 May 2018 18:43:34 +0200 Subject: [PATCH 2/2] Fix configuration restore regression from 5.13.1a 5.13.1a * Fix configuration restore regression from 5.13.1 --- sonoff/webserver.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino index 6e8361624..0439ae509 100644 --- a/sonoff/webserver.ino +++ b/sonoff/webserver.ino @@ -1360,7 +1360,6 @@ void HandleUploadLoop() if (upload_file_type) { // config if (!upload_error) { if (upload.currentSize > (sizeof(Settings) - (config_block_count * HTTP_UPLOAD_BUFLEN))) { - if (config_block_count) { SettingsDefault(); } upload_error = 9; return; }