8000 PoC for handling Erase WiFi Setting after OTA by mhightower83 · Pull Request #6965 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

PoC for handling Erase WiFi Setting after OTA #6965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 64 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
6fde10f
Proof of concept for post OTA 1st boot erase config.
mhightower83 Dec 28, 2019
93fb3d3
Added a 2nd method
mhightower83 Dec 30, 2019
c66ae2e
Merge branch 'master' into pr-erase-config
mhightower83 Dec 30, 2019
6d882e4
Added two_shots init to noextra4k
mhightower83 Dec 30, 2019
500b7cc
Removed cleaned up debug crud and added test example.
mhightower83 Dec 31, 2019
afd603b
Merge branch 'master' into pr-erase-config
mhightower83 Dec 31, 2019
bfb21b1
A proof of concept for handling Erase WiFi Setting after OTA
mhightower83 Dec 31, 2019
e51ab08
Addressed Tavis CI style issues.
mhightower83 Dec 31, 2019
a284b0a
Take two
mhightower83 Dec 31, 2019
3258d63
Merge branch 'master' into pr-erase-config
mhightower83 Jan 6, 2020
9d6e059
Merge branch 'master' into pr-erase-config
mhightower83 Jan 8, 2020
95eefed
Update example. Removed some debug crud. Added a Flash Info hotkey op…
mhightower83 Jan 8, 2020
815f1dc
Merge branch 'master' into pr-erase-config
mhightower83 Jan 9, 2020
660d19c
Merge branch 'master' into pr-erase-config
mhightower83 Jan 9, 2020
4554a7c
style fix
mhightower83 Jan 9, 2020
1ce4c60
Merge branch 'master' into pr-erase-config
mhightower83 Jan 16, 2020
7130877
Merge branch 'master' into pr-erase-config
mhightower83 Jan 23, 2020
73f16dc
Merge branch 'master' into pr-erase-config
mhightower83 Jan 28, 2020
9c4e44b
Added print messages to show progress.
mhightower83 Feb 6, 2020
546f0cb
Merge branch 'master' into pr-erase-config
mhightower83 Feb 6, 2020
560ddc5
Set default Method to 2, to side step travis issue of using preinit().
mhightower83 Feb 7, 2020
049d91b
Added a Third method.
mhightower83 Feb 18, 2020
4e2493a
Resolve merge conflict.
mhightower83 Feb 19, 2020
39160c8
one more :(
mhightower83 Feb 19, 2020
c0394fb
Merge branch 'master' into pr-erase-config
mhightower83 Feb 19, 2020
f1acb40
Access bin image header through ICACHE reduces IRAM usage by 16.
mhightower83 Feb 19, 2020
604b40a
Merge branch 'master' into pr-erase-config
mhightower83 Feb 21, 2020
4553009
Made function name longer to avoid possible name conflicts in the fut…
mhightower83 Feb 22, 2020
372998c
Merge branch 'master' into pr-erase-config
mhightower83 Mar 11, 2020
39b9508
CI issues
mhightower83 Mar 11, 2020
4954dcd
Merge branch 'master' into pr-erase-config
mhightower83 Mar 17, 2020
828a47d
Merge branch 'master' into pr-erase-config
mhightower83 Mar 27, 2020
b2d35ea
Merge branch 'master' into pr-erase-config
mh 8000 ightower83 Apr 9, 2020
74129c5
Merge branch 'master' into pr-erase-config
mhightower83 Apr 16, 2020
14fede0
Merge branch 'master' into pr-erase-config
mhightower83 Apr 28, 2020
be5fe6d
Merge branch 'master' into pr-erase-config
mhightower83 May 5, 2020
cfd54df
Merge branch 'master' into pr-erase-config
mhightower83 May 16, 2020
6f91be0
Merge branch 'master' into pr-erase-config
mhightower83 May 31, 2020
6b1cdb8
Merge branch 'master' into pr-erase-config
mhightower83 Jun 4, 2020
fcb8b21
Merge branch 'master' into pr-erase-config
mhightower83 Jun 19, 2020
c40a873
Merge branch 'master' into pr-erase-config
mhightower83 Jul 7, 2020
2b2def2
Corrected new errors after upgrade to GCC 10.1 toolchain.
mhightower83 Jul 10, 2020
48ed5e9
Merge branch 'master' into pr-erase-config
devyte Jul 22, 2020
b211727
Merge branch 'master' into pr-erase-config
mhightower83 Sep 2, 2020
ab9b6b2
Corrected manual merge error, with init order of class.
mhightower83 Sep 2, 2020
e5539fe
Merge branch 'master' into pr-erase-config
mhightower83 Sep 17, 2020
502d8bc
Merge branch 'master' into pr-erase-config
mhightower83 Oct 2, 2020
f50b3a6
Merge branch 'master' into pr-erase-config
mhightower83 Nov 11, 2020
94365e1
Fixed merge errors and code rot.
mhightower83 Nov 11, 2020
5234f3f
Updated wifi_health_t and WiFiDisconnectLog_t to take
mhightower83 Nov 11, 2020
025bb2f
Merge branch 'master' into pr-erase-config
mhightower83 Dec 8, 2020
edade1c
Merge branch 'master' into pr-erase-config
mhightower83 Dec 29, 2020
24a117c
Merge branch 'master' into pr-erase-config
d-a-v Feb 19, 2021
4e31c91
Merge branch 'master' into pr-erase-config
mhightower83 Mar 14, 2021
0b01518
Replaced ROM_uart_div_modify and associated logic, with the current
mhightower83 Mar 14, 2021
f9a3f95
Merge branch 'master' into pr-erase-config
devyte Apr 3, 2021
217b2d2
Cleanup merge conflict. Now use IRAM_ATTR and _NOINLINE_STATIC w/o ch…
mhightower83 Apr 3, 2021
0b7fced
Merge branch 'master' into pr-erase-config
mhightower83 Jul 28, 2021
9f0e220
Merge branch 'master' into pr-erase-config
mhightower83 Aug 8, 2021
e44e752
Merge branch 'master' into pr-erase-config
mhightower83 Aug 21, 2021
c44a3f9
Changed to comply with strict-aliasing rules. Changes based on discus…
mhightower83 Aug 22, 2021
63a2048
Merge branch 'pr-erase-config' of github.com:mhightower83/Arduino int…
mhightower83 Aug 22, 2021
21b0154
Merge branch 'master' into pr-erase-config
mhightower83 Dec 21, 2021
fb05f15
Merge branch 'master' into pr-erase-config
mhightower83 May 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added print messages to show progress.
And code to ensure the UART speed stayed at 74880 for a more complete
viewing of messages. Well at least, it stays at 74880 for a longer time than it was.
Added pinMode filter to prevent UART TX pin from being disabled during user_init().
  • Loading branch information
mhightower83 committed Feb 6, 2020
commit 9c4e44bc8406eb3b4a7eab9260765f824e8b8749
3 changes: 3 additions & 0 deletions cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ void UpdaterClass::_reset() {
}

bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
// Empty call so erase_config.cpp module to ensure it is built and linked in.
enable_erase_config_at_link_time();

if(_size > 0){
#ifdef DEBUG_UPDATER
DEBUG_UPDATER.println(F("[begin] already running"));
Expand Down
19 changes: 19 additions & 0 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ void init_done() {
https://github.com/esp8266/Arduino/pull/4889

*/
#if defined(ERASE_CONFIG_H)
extern "C" void fix_divider(void);
#define FIX_DIVIDER fix_divider
#define ETS_PRINTF(...) ets_uart_printf(__VA_ARGS__)
#define ETS_DELAY_US(a) ets_delay_us(a)
#else
#define FIX_DIVIDER() do {} while(0)
#define ETS_PRINTF(...) do {} while(0)
#define ETS_DELAY_US(a) do {} while(0)
#endif


extern "C" void app_entry_redefinable(void) __attribute__((weak));
extern "C" void app_entry_redefinable(void)
Expand All @@ -291,6 +302,8 @@ extern "C" void app_entry_redefinable(void)
cont_t s_cont __attribute__((aligned(16)));
g_pcont = &s_cont;

FIX_DIVIDER();
ETS_PRINTF("\n\ncall_user_start()\n");
/* Call the entry point of the SDK code. */
call_user_start();
}
Expand All @@ -312,7 +325,13 @@ extern "C" void user_init(void) {
struct rst_info *rtc_info_ptr = system_get_rst_info();
memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));

#if defined(ERASE_CONFIG_H)
uart_div_modify(0, UART_CLK_FREQ / (74880));
ETS_DELAY_US(150);
ETS_PRINTF("\nuser_init()\n");
#else
uart_div_modify(0, UART_CLK_FREQ / (115200));
#endif

init(); // in core_esp8266_wiring.c, inits hw regs and sdk timer

Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/core_esp8266_postmortem.cpp
Original file line num 8000 ber Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void ets_printf_P(const char *str, ...) {
vsnprintf(destStr, sizeof(destStr), str, argPtr);
va_end(argPtr);
while (*c) {
ets_putc(*(c++));
ets_uart_putc1(*(c++)); // This will send "\r\n" for each '\n'
}
}

Expand Down Expand Up @@ -147,10 +147,10 @@ void __wrap_system_restart_local() {
// (determined empirically, might break)
uint32_t offset = 0;
if (rst_info.reason == REASON_SOFT_WDT_RST) {
offset = 0x1b0;
offset = 0x190;
}
else if (rst_info.reason == REASON_EXCEPTION_RST) {
offset = 0x1a0;
offset = 0x180;
}
else if (rst_info.reason == REASON_WDT_RST) {
offset = 0x10;
Expand Down
144 changes: 136 additions & 8 deletions cores/esp8266/erase_config.cpp
8000
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ OTAEraseConfig.
#undef ERASE_CONFIG_H
#endif

#if 1
#define ETS_PRINTF(...) ets_uart_printf(__VA_ARGS__)
#define ETS_DELAY_US(a) ets_delay_us(a)
#else
#define ETS_PRINTF(...) do {} while(0)
#define ETS_DELAY_US(a) do {} while(0)
#endif

#ifdef ERASE_CONFIG_H
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always true #if

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

extern "C" {
#include "user_interface.h"
void __real_system_restart_local();

#if (ERASE_CONFIG_METHOD == 1)
#define IRAM_MAYBE
Expand All @@ -71,7 +79,15 @@ extern "C" {
#pragma GCC error "Unsupported ERASE_CONFIG_METHOD"
#endif

bool IRAM_MAYBE erase_config2(const uint32_t flash_erase_mask) {

void enable_erase_config_at_link_time(void) {
/*
This has to be called from somewhere for this module to get
linked into the build.
*/
}

bool IRAM_MAYBE erase_config(const uint32_t flash_erase_mask) {
// This is really the active configured size
uint32_t flash_size = flashchip->chip_size;
uint32_t erase_mask = (flash_erase_mask & (uint32_t)ERASE_CONFIG_ALL_DATA);
Expand All @@ -84,7 +100,10 @@ bool IRAM_MAYBE erase_config2(const uint32_t flash_erase_mask) {
#elif (ERASE_CONFIG_METHOD == 2)
if (0 != SPIEraseSector(sector)) {
#endif
ETS_PRINTF("Erase sector 0x%04X failed!\n", sector);
return false;
} else {
ETS_PRINTF("Erased sector 0x%04X\n", sector);
}
}
}
Expand Down Expand Up @@ -114,18 +133,82 @@ bool IRAM_MAYBE check_and_erase_config(void) {
ebcmd->args[i] = 0U;

if (erase_flash_option) {
ETS_PRINTF("\nerase_config(0x%03X)\n", erase_flash_option);
#if (ERASE_CONFIG_METHOD == 1)
erase_config2(erase_flash_option);
system_restart();
erase_config(erase_flash_option);
ETS_PRINTF("\n__real_system_restart_local\n\n");
ETS_DELAY_US(12000);
__real_system_restart_local();
while(true){}
#elif (ERASE_CONFIG_METHOD == 2)
return erase_config2(erase_flash_option);
return erase_config(erase_flash_option);
#endif
}
} else {
ETS_PRINTF("\nNo OTA erase flags\n");
}
return true;
}




typedef enum ROM_RST_REASON { /* Comments on the right are from RTOS SDK */
NO_MEAN = 0, /* Undefined */
POWERON_RESET = 1, /* Power on boot *//**<1, Vbat power on reset */
EXT_RESET = 2, /* External reset or wake-up from Deep-sleep */
/**<2, external system reset */
SW_RESET = 3, /* *//**<3, Software reset digital core */
OWDT_RESET = 4, /* Hardware WDT reset *//**<4, Legacy watch dog reset digital core */
DEEPSLEEP_RESET = 5, /* *//**<5, Deep Sleep reset digital core */
SDIO_RESET = 6, /* *//**<6, Reset by SLC module, reset digital core*/
} ROM_RST_REASON_t;

constexpr volatile uint32_t *RTC_SYS = (volatile uint32_t*)0x60001100;
extern uint32_t rtc_get_reset_reason(void);

bool ICACHE_RAM_ATTR is_cpu_freq_cal(void) {
const uint32_t rtc_sys_reason = RTC_SYS[0];
const uint32_t rom_api_reason = rtc_get_reset_reason();
if (1 >= rtc_sys_reason && OWDT_RESET != rom_api_reason) {
return false; // REASON_EXT_SYS_RST
}
if (REASON_EXT_SYS_RST < rtc_sys_reason) {
return false; // REASON_DEFAULT_RST
}
return true;
}

/*
* Here we use uart_div_modify in the Boot ROM. Note the Boot ROM version does
* not do any input validation.
*
* We cannot use the SDKs replacement. It is not in IRAM.
*/
#ifndef ROM_uart_div_modify
#define ROM_uart_div_modify 0x400039d8
#endif
typedef void (*fp_uart_div_modify_t)(uint32_t uart_no, uint32 DivLatchValue);
constexpr fp_uart_div_modify_t real_uart_div_modify = (fp_uart_div_modify_t)ROM_uart_div_modify;

void ICACHE_RAM_ATTR fix_divider(void) {
/*
When reset cause is not power-on or EXT_RST the CPU crystal calibration
has been done and there is no need to correct. With the exception of some
early callback(s) from the SDK like run_user_rf_pre_init(). The UART speed
appears to always be based on the precalibrated crystal frequency.
*/
uint32_t divider = UART_CLK_FREQ / 115200;
if (is_cpu_freq_cal()) {
divider = UART_CLK_FREQ / 74880;
}
real_uart_div_modify(0, divider);
ETS_DELAY_US(150);
}





#if (ERASE_CONFIG_METHOD == 1)
extern struct rst_info resetInfo;
Expand All @@ -138,16 +221,15 @@ extern "C" void preinit (void)
}
}



#elif (ERASE_CONFIG_METHOD == 2) //Another option

#include "cont.h"

int eboot_two_shots __attribute__((section(".noinit")));
extern cont_t* g_pcont;
extern "C" void call_user_start();

extern "C" void app_entry_redefinable(void)
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void)
{
/* Allocate continuation context on this SYS stack,
and save pointer to it. */
Expand All @@ -156,6 +238,10 @@ extern "C" void app_entry_redefinable(void)

eboot_two_shots = 2;

fix_divider();
ETS_PRINTF("\n\ncall_user_start()\n");
ETS_DELAY_US(12000);

/* Call the entry point of the SDK code. */
call_user_start();
}
Expand All @@ -182,15 +268,57 @@ int ICACHE_RAM_ATTR SPIRead(uint32_t addr, void *dest, size_t size) {
// config sectors.
if (eboot_two_shots) {
eboot_two_shots--;
if (0 == eboot_two_shots)
if (0 == eboot_two_shots) {
fix_divider();
check_and_erase_config();
ETS_DELAY_US(12000);
}
}

int err = real_SPIRead(addr, dest, size);
dbg_log_SPIRead(addr, dest, size, err);
return err;
}


#endif

};
#endif // ERASE_CONFIG_H


extern "C" void ICACHE_RAM_ATTR _Z22__run_user_rf_pre_initv(void) {
// fix_divider();
real_uart_div_modify(0, UART_CLK_FREQ / 115200);
ETS_DELAY_US(150);
ets_uart_printf("\n__run_user_rf_pre_init()\n");
ETS_DELAY_US(12000);
}

/*
This helps keep the UART enabled longer so we can get a few more messages
printed.
*/
#if 1
extern struct rst_info resetInfo;
extern "C" void __pinMode( uint8_t pin, uint8_t mode );

inline bool is_gpio_persistent(void) {
return REASON_EXCEPTION_RST <= resetInfo.reason &&
REASON_SOFT_RESTART >= resetInfo.reason;
}

extern "C" void pinMode( uint8_t pin, uint8_t mode ) {
static bool in_initPins = true;
if (in_initPins && (1 == pin)) {
if (!is_gpio_persistent()) {
// Restore pin to TX after Power-on and EXT_RST
__pinMode(pin, FUNCTION_0);
}
in_initPins = false;
return;
}

__pinMode( pin, mode );
}
#endif
1 change: 1 addition & 0 deletions cores/esp8266/erase_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ERASE_CONFIG_ALL_DATA = ( BIT(4) | BIT(3) | BIT(2) | BIT(1) |

bool check_and_erase_config(void);
bool erase_config(const uint32_t flash_erase_mask);
void enable_erase_config_at_link_time(void);

#ifdef __cplusplus
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const char* password = STAPSK;
ERASE_CONFIG_SDK_DATA
ERASE_CONFIG_ALL_DATA
*/
ERASE_CONFIG_MASK_t eraseConfigOption = ERASE_CONFIG_BLANK_BIN;
ERASE_CONFIG_MASK_t eraseConfigOption = ERASE_CONFIG_RF_CAL; //ERASE_CONFIG_BLANK_BIN;

#define String_F(a) String(F(a))

Expand All @@ -52,10 +52,11 @@ void setTimeTZ(String& tz) {
}

void setup() {
Serial.begin(115200);
Serial.begin(74880);
// Serial.begin(115200);
delay(20);
Serial.println();
Serial.println("Booting");
Serial.println("setup ...");

WiFi.persistent(false); // w/o this a flash write occurs at every boot
WiFi.mode(WIFI_OFF);
Expand Down Expand Up @@ -135,7 +136,7 @@ void setup() {
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.println("setup complete");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
Expand Down
0