Skip to content

Commit 3b5c6ca

Browse files
authored
Fix bootloop if config is reset (#4852)
* Fix bootloop if config missing/reset Can't reset the config if there's nothing to reset! * ESP8266: Commit ACTIONT_TRACKER * Use consistent naming for backups and reset cfgs Use 'rst.cfg.json' instead of 'cfg.json.rst.json' for configs that were reset. * Add a little more PSTR to bootloop handling
1 parent dcc1fbc commit 3b5c6ca

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

wled00/cfg.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,15 @@ bool verifyConfig() {
787787
}
788788

789789
// rename config file and reboot
790+
// if the cfg file doesn't exist, such as after a reset, do nothing
790791
void resetConfig() {
791-
DEBUG_PRINTLN(F("Reset config"));
792-
char backupname[32];
793-
strcpy(backupname, s_cfg_json);
794-
strcat(backupname, ".rst.json");
795-
WLED_FS.rename(s_cfg_json, backupname);
796-
doReboot = true;
792+
if (WLED_FS.exists(s_cfg_json)) {
793+
DEBUG_PRINTLN(F("Reset config"));
794+
char backupname[32];
795+
snprintf_P(backupname, sizeof(backupname), PSTR("/rst.%s"), &s_cfg_json[1]);
796+
WLED_FS.rename(s_cfg_json, backupname);
797+
doReboot = true;
798+
}
797799
}
798800

799801
bool deserializeConfigFromFS() {

wled00/file.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ bool compareFiles(const char* path1, const char* path2) {
515515
return identical;
516516
}
517517

518-
static const char s_backup_json[] PROGMEM = "/bkp.";
518+
static const char s_backup_fmt[] PROGMEM = "/bkp.%s";
519519

520520
bool backupFile(const char* filename) {
521521
DEBUG_PRINTF("backup %s \n", filename);
@@ -524,7 +524,7 @@ bool backupFile(const char* filename) {
524524
return false;
525525
}
526526
char backupname[32];
527-
snprintf(backupname, sizeof(backupname), "%s%s", s_backup_json, filename + 1); // skip leading '/' in filename
527+
snprintf_P(backupname, sizeof(backupname), s_backup_fmt, filename + 1); // skip leading '/' in filename
528528

529529
if (copyFile(filename, backupname)) {
530530
DEBUG_PRINTLN(F("backup ok"));
@@ -537,7 +537,7 @@ bool backupFile(const char* filename) {
537537
bool restoreFile(const char* filename) {
538538
DEBUG_PRINTF("restore %s \n", filename);
539539
char backupname[32];
540-
snprintf(backupname, sizeof(backupname), "%s%s", s_backup_json, filename + 1); // skip leading '/' in filename
540+
snprintf_P(backupname, sizeof(backupname), s_backup_fmt, filename + 1); // skip leading '/' in filename
541541

542542
if (!WLED_FS.exists(backupname)) {
543543
DEBUG_PRINTLN(F("no backup found"));
@@ -565,9 +565,9 @@ bool validateJsonFile(const char* filename) {
565565
bool result = deserializeJson(doc, file, DeserializationOption::Filter(filter)) == DeserializationError::Ok;
566566
file.close();
567567
if (!result) {
568-
DEBUG_PRINTF("Invalid JSON file %s\n", filename);
568+
DEBUG_PRINTF_P(PSTR("Invalid JSON file %s\n"), filename);
569569
} else {
570-
DEBUG_PRINTF("Valid JSON file %s\n", filename);
570+
DEBUG_PRINTF_P(PSTR("Valid JSON file %s\n"), filename);
571571
}
572572
return result;
573573
}

wled00/util.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,8 @@ void *realloc_malloc(void *ptr, size_t size) {
721721
// if a bootloop is detected: restore settings from backup, then reset settings, then switch boot image (and repeat)
722722

723723
#define BOOTLOOP_THRESHOLD 5 // number of consecutive crashes to trigger bootloop detection
724-
#define BOOTLOOP_ACTION_RESTORE 0 // default action: restore config from /cfg.bak
725-
#define BOOTLOOP_ACTION_RESET 1 // if restore does not work, reset config (rename /cfg.json to /cfg.fault)
724+
#define BOOTLOOP_ACTION_RESTORE 0 // default action: restore config from /bak.cfg.json
725+
#define BOOTLOOP_ACTION_RESET 1 // if restore does not work, reset config (rename /cfg.json to /rst.cfg.json)
726726
#define BOOTLOOP_ACTION_OTA 2 // swap the boot partition
727727
#define BOOTLOOP_ACTION_DUMP 3 // nothing seems to help, dump files to serial and reboot (until hardware reset)
728728
#ifdef ESP8266
@@ -836,6 +836,9 @@ void handleBootLoop() {
836836
#endif
837837
else
838838
dumpFilesToSerial();
839+
#ifdef ESP8266
840+
ESP.rtcUserMemoryWrite(ACTIONT_TRACKER_IDX, &bl_actiontracker, sizeof(uint32_t));
841+
#endif
839842
ESP.restart(); // restart cleanly and don't wait for another crash
840843
}
841844

0 commit comments

Comments
 (0)