From 43241c26795bdcf79fd9fa3e89d914893cee5139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 22 Sep 2017 00:22:46 +0200 Subject: [PATCH] admin pw now lives in a separate config block. BREAKING CHANGE! will wipe old settings :( --- CMakeLists.txt | 1 + esphttpdconfig.mk | 3 ++- user/cgi_persist.c | 4 +--- user/persist.c | 32 +++++++++++++++++++++++++------- user/persist.h | 17 ++++++++++++++--- user/serial.c | 9 ++++----- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea9d6ac..12dea35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,7 @@ include_directories(esp_iot_sdk_v1.5.2/include) add_definitions( -D__ets__ -DICACHE_FLASH + -DDEBUG_LOGBUF_SIZE=2048 -DUSE_OPTIMIZE_PRINTF=1 -DHTTPD_MAX_CONNECTIONS=5 -DHTTPD_STACKSIZE=1000 diff --git a/esphttpdconfig.mk b/esphttpdconfig.mk index cd2a8ad..d9b9e46 100644 --- a/esphttpdconfig.mk +++ b/esphttpdconfig.mk @@ -46,7 +46,7 @@ GLOBAL_CFLAGS = \ -DDEBUG_CAPTDNS=0 \ -DDEBUG_HTTP=0 \ -DDEBUG_ESPFS=0 \ - -DDEBUG_PERSIST=0 \ + -DDEBUG_PERSIST=1 \ -DDEBUG_UTFCACHE=0 \ -DDEBUG_CGI=0 \ -DDEBUG_WIFI=0 \ @@ -59,5 +59,6 @@ GLOBAL_CFLAGS = \ -DHTTPD_MAX_BACKLOG_SIZE=8192 \ -DHTTPD_MAX_HEAD_LEN=1024 \ -DHTTPD_MAX_POST_LEN=512 \ + -DDEBUG_LOGBUF_SIZE=2048 \ -mforce-l32 \ -DUSE_OPTIMIZE_PRINTF=1 diff --git a/user/cgi_persist.c b/user/cgi_persist.c index 4828b8a..61dee19 100644 --- a/user/cgi_persist.c +++ b/user/cgi_persist.c @@ -13,9 +13,7 @@ Cgi/template routines for configuring non-wifi settings static bool ICACHE_FLASH_ATTR verify_admin_pw(const char *pw) { - // This is not really for security, but to prevent someone who - // shouldn't touch those settings from fucking it up. - return streq(pw, STR(ADMIN_PASSWORD)); // the PW comes from the makefile + return streq(pw, persist.admin.pw); } httpd_cgi_state ICACHE_FLASH_ATTR diff --git a/user/persist.c b/user/persist.c index 5bb22d3..c9ea315 100644 --- a/user/persist.c +++ b/user/persist.c @@ -97,19 +97,27 @@ compute_checksum(AppConfigBundle *bundle) return calculateCRC32((uint8_t *) bundle, sizeof(AppConfigBundle) - 4); } +static void ICACHE_FLASH_ATTR +set_admin_block_defaults(void) +{ + persist_info("[Persist] Initing admin config block"); + strcpy(persist.admin.pw, STR(ADMIN_PASSWORD)); + persist.admin.version = ADMINCONF_VERSION; +} + /** * Load, verify and apply persistent config */ void ICACHE_FLASH_ATTR persist_load(void) { - persist_info("[Persist] Loading stored settings from FLASH..."); + persist_info("[Persist] Loading settings from FLASH..."); - persist_dbg("AppConfigBundle memory map:"); - persist_dbg("> WiFiConfigBundle at %4d (error %2d)", wconf_at, wconf_at - 0); - persist_dbg("> SystemConfigBundle at %4d (error %2d)", sconf_at, sconf_at - WIFICONF_SIZE); - persist_dbg("> TerminalConfigBundle at %4d (error %2d)", tconf_at, tconf_at - WIFICONF_SIZE - SYSCONF_SIZE); - persist_dbg("> Checksum at %4d (error %2d)", cksum_at, cksum_at - (APPCONF_SIZE - 4)); + persist_dbg("Persist memory map:"); + persist_dbg("> wifi at %4d (error %2d)", wconf_at, wconf_at - 0); + persist_dbg("> sys at %4d (error %2d)", sconf_at, sconf_at - WIFICONF_SIZE); + persist_dbg("> term at %4d (error %2d)", tconf_at, tconf_at - WIFICONF_SIZE - SYSCONF_SIZE); + persist_dbg("> cksum at %4d (error %2d)", cksum_at, cksum_at - (APPCONF_SIZE - 4)); persist_dbg("> Total size = %d bytes (error %d)", sizeof(AppConfigBundle), APPCONF_SIZE - sizeof(AppConfigBundle)); bool hard_reset = false; @@ -120,7 +128,8 @@ persist_load(void) // Verify checksums if (hard_reset || (compute_checksum(&persist.defaults) != persist.defaults.checksum) || - (compute_checksum(&persist.current) != persist.current.checksum)) { + (compute_checksum(&persist.current) != persist.current.checksum) || + (persist.admin.version != 0 && (calculateCRC32((uint8_t *) &persist.admin, sizeof(AdminConfigBlock) - 4) != persist.admin.checksum))) { error("[Persist] Checksum verification: FAILED"); hard_reset = true; } else { @@ -135,10 +144,18 @@ persist_load(void) // write them also as defaults memcpy(&persist.defaults, &persist.current, sizeof(AppConfigBundle)); + + // reset admin pw + set_admin_block_defaults(); persist_store(); // this also stores them to flash and applies to modules } else { + if (persist.admin.version == 0) { + set_admin_block_defaults(); + persist_store(); + } + apply_live_settings(); } @@ -153,6 +170,7 @@ persist_store(void) // Update checksums before write persist.current.checksum = compute_checksum(&persist.current); persist.defaults.checksum = compute_checksum(&persist.defaults); + persist.admin.checksum = calculateCRC32((uint8_t *) &persist.admin, sizeof(AdminConfigBlock) - 4); if (!system_param_save_with_protect(PERSIST_SECTOR_ID, &persist, sizeof(PersistBlock))) { error("[Persist] Store to flash failed!"); diff --git a/user/persist.h b/user/persist.h index 1cc8a67..d85c964 100644 --- a/user/persist.h +++ b/user/persist.h @@ -16,9 +16,9 @@ // Changing this could be used to force-erase the config area // after a firmware upgrade -#define CHECKSUM_SALT 3 +#define CHECKSUM_SALT 4 -#define APPCONF_SIZE 2048 +#define APPCONF_SIZE 1900 /** Struct for current or default settings */ typedef struct { // the entire block should be 1024 bytes long (for compatibility across upgrades) @@ -41,7 +41,7 @@ typedef struct { // the entire block should be 1024 bytes long (for compatibilit // it grew to a different memory area. uint8_t _filler_end[ APPCONF_SIZE - - sizeof(uint32_t) // checksum + - 4 // checksum - WIFICONF_SIZE - SYSCONF_SIZE - TERMCONF_SIZE @@ -50,10 +50,21 @@ typedef struct { // the entire block should be 1024 bytes long (for compatibilit uint32_t checksum; // computed before write and tested on load. If it doesn't match, values are reset to hard defaults. } AppConfigBundle; +#define ADMINCONF_VERSION 1 +#define ADMINCONF_SIZE 256 + +typedef struct { + u8 version; + char pw[64]; + uint8_t _filler[ADMINCONF_SIZE-64-4]; + uint32_t checksum; +} AdminConfigBlock; + /** This is the entire data block stored in FLASH */ typedef struct { AppConfigBundle defaults; // defaults are stored here AppConfigBundle current; // active settings adjusted by the user + AdminConfigBlock admin; } PersistBlock; // Persist holds the data currently loaded from the flash diff --git a/user/serial.c b/user/serial.c index 4ecb128..bcbd54c 100644 --- a/user/serial.c +++ b/user/serial.c @@ -4,8 +4,7 @@ #include "ansi_parser.h" #include "syscfg.h" -#define LOGBUF_SIZE 512 -static char logbuf[LOGBUF_SIZE]; +static char logbuf[DEBUG_LOGBUF_SIZE]; static u32 lb_nw = 1; static u32 lb_ls = 0; static ETSTimer flushLogTimer; @@ -14,7 +13,7 @@ static void buf_putc(char c) { if (lb_ls != lb_nw) { logbuf[lb_nw++] = c; - if (lb_nw >= LOGBUF_SIZE) lb_nw = 0; + if (lb_nw >= DEBUG_LOGBUF_SIZE) lb_nw = 0; } } @@ -25,11 +24,11 @@ buf_pop(void *unused) u32 old_ls; while (quantity > 0) { // stop when done - if ((lb_ls == lb_nw-1) || (lb_ls == LOGBUF_SIZE-1 && lb_nw == 0)) break; + if ((lb_ls == lb_nw-1) || (lb_ls == DEBUG_LOGBUF_SIZE-1 && lb_nw == 0)) break; old_ls = lb_ls; lb_ls++; - if (lb_ls >= LOGBUF_SIZE) lb_ls = 0; + if (lb_ls >= DEBUG_LOGBUF_SIZE) lb_ls = 0; if (OK == UART_WriteCharCRLF(UART1, logbuf[lb_ls], 5)) { quantity--;