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
mhightower83 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
Next Next commit
Proof of concept for post OTA 1st boot erase config.
  • Loading branch information
mhightower83 committed Dec 28, 2019
commit 6fde10f0bbfc3af62418aac38d0feb0a09e7d219
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern "C" {
#include "twi.h"
#include "core_esp8266_features.h"
#include "core_esp8266_version.h"
#include "erase_config.h"

#define HIGH 0x1
#define LOW 0x0
Expand Down
6 changes: 6 additions & 0 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ struct rst_info * EspClass::getResetInfoPtr(void) {
return &resetInfo;
}

#ifdef ERASE_CONFIG_H
bool EspClass::eraseConfig(const uint32_t flash_erase_mask) {
return erase_config(flash_erase_mask);
}
#else
bool EspClass::eraseConfig(void) {
const size_t cfgSize = 0x4000;
size_t cfgAddr = ESP.getFlashChipSize() - cfgSize;
Expand All @@ -514,6 +519,7 @@ bool EspClass::eraseConfig(void) {

return true;
}
#endif

uint32_t EspClass::getSketchSize() {
static uint32_t result = 0;
Expand Down
4 changes: 4 additions & 0 deletions cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ class EspClass {
String getResetInfo();
struct rst_info * getResetInfoPtr();

#ifdef ERASE_CONFIG_H
bool eraseConfig(const uint32_t flash_erase_mask = ERASE_CONFIG_SDK_DATA);
#else
bool eraseConfig();
#endif

#ifndef CORE_MOCK
inline uint32_t getCycleCount() __attribute__((always_inline));
Expand Down
24 changes: 18 additions & 6 deletions cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Updater.h"
#include "eboot_command.h"
#include <esp8266_peri.h>
#include <erase_config.h>

//#define DEBUG_UPDATER Serial

Expand Down Expand Up @@ -34,6 +35,9 @@ UpdaterClass::UpdaterClass()
, _startAddress(0)
, _currentAddress(0)
, _command(U_FLASH)
#ifdef ERASE_CONFIG_H
, _eraseConfigOption(ERASE_CONFIG_BLANK_BIN)
#endif
, _hash(nullptr)
, _verify(nullptr)
, _progress_callback(nullptr)
Expand Down Expand Up @@ -84,7 +88,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
_setError(UPDATE_ERROR_BOOTSTRAP);
return false;
}

#ifdef DEBUG_UPDATER
if (command == U_FS) {
DEBUG_UPDATER.println(F("[begin] Update Filesystem."));
Expand Down Expand Up @@ -127,7 +131,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {

//make sure that the size of both sketches is less than the total space (updateEndAddress)
if(updateStartAddress < currentSketchSize) {
_setError(UPDATE_ERROR_SPACE);
_setError(UPDATE_ERROR_SPACE);
return false;
}
}
Expand Down Expand Up @@ -284,10 +288,17 @@ bool UpdaterClass::end(bool evenIfRemaining){

if (_command == U_FLASH) {
eboot_command ebcmd;
memset(&ebcmd, 0, sizeof(ebcmd));
ebcmd.action = ACTION_COPY_RAW;
ebcmd.args[0] = _startAddress;
ebcmd.args[1] = 0x00000;
ebcmd.args[2] = _size;
#ifdef ERASE_CONFIG_H
ebcmd.args[4] = _eraseConfigOption;
ebcmd.args[5] = ~_eraseConfigOption;
ebcmd.args[6] = _eraseConfigOption;
ebcmd.args[7] = ~_eraseConfigOption;
#endif
eboot_command_write(&ebcmd);

#ifdef DEBUG_UPDATER
Expand All @@ -297,6 +308,7 @@ bool UpdaterClass::end(bool evenIfRemaining){
else if (_command == U_FS) {
#ifdef ATOMIC_FS_UPDATE
eboot_command ebcmd;
memset(&ebcmd, 0, sizeof(ebcmd));
ebcmd.action = ACTION_COPY_RAW;
ebcmd.args[0] = _startAddress;
ebcmd.args[1] = (uintptr_t)&_FS_start - 0x40200000;
Expand Down Expand Up @@ -345,7 +357,7 @@ bool UpdaterClass::_writeBuffer(){
modifyFlashMode = true;
}
}

if (eraseResult) {
if(!_async) yield();
writeResult = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
Expand Down Expand Up @@ -431,7 +443,7 @@ bool UpdaterClass::_verifyEnd() {
uint8_t buf[4];
if(!ESP.flashRead(_startAddress, (uint32_t *) &buf[0], 4)) {
_currentAddress = (_startAddress);
_setError(UPDATE_ERROR_READ);
_setError(UPDATE_ERROR_READ);
return false;
}

Expand All @@ -443,7 +455,7 @@ bool UpdaterClass::_verifyEnd() {
return true;
} else if (buf[0] != 0xE9) {
_currentAddress = (_startAddress);
_setError(UPDATE_ERROR_MAGIC_BYTE);
_setError(UPDATE_ERROR_MAGIC_BYTE);
return false;
}

Expand All @@ -452,7 +464,7 @@ bool UpdaterClass::_verifyEnd() {
// check if new bin fits to SPI flash
if(bin_flash_size > ESP.getFlashChipRealSize()) {
_currentAddress = (_startAddress);
_setError(UPDATE_ERROR_NEW_FLASH_CONFIG);
_setError(UPDATE_ERROR_NEW_FLASH_CONFIG);
return false;
}

Expand Down
13 changes: 11 additions & 2 deletions cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class UpdaterVerifyClass {
class UpdaterClass {
public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;

UpdaterClass();

/* Optionally add a cryptographic signature verification hash and method */
Expand All @@ -63,6 +63,13 @@ class UpdaterClass {
*/
bool begin(size_t size, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);

#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.

Same #if comment

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

/*
*/
inline void setEraseConfigOption(ERASE_CONFIG_MASK_t eraseOption) {
_eraseConfigOption = eraseOption;
}
#endif
/*
Run Updater from asynchronous callbacs
*/
Expand Down Expand Up @@ -179,7 +186,7 @@ class UpdaterClass {
bool _verifyHeader(uint8_t data);
bool _verifyEnd();

void _setError(int error);
void _setError(int error);

bool _async;
uint8_t _error;
Expand All @@ -197,6 +204,8 @@ class UpdaterClass {
int _ledPin;
uint8_t _ledOn;

uint32_t _eraseConfigOption;

// Optional signed binary verification
UpdaterHashClass *_hash;
UpdaterVerifyClass *_verify;
Expand Down
60 changes: 60 additions & 0 deletions cores/esp8266/erase_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <Arduino.h>
#include <flash_utils.h>
#include <eboot_command.h>
#include <spi_flash.h>

#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"

bool 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);
uint32_t sector = flash_size/SPI_FLASH_SEC_SIZE - 1U;

for (; !!erase_mask; erase_mask >>= 1U, sector--) {
if ((erase_mask & 1U)) {
if (0 != spi_flash_erase_sector(sector)) {
return false;
}
}
}

return true;
}

bool check_and_erase_config(void) {
// This should work since each element of the structure is a word.
eboot_command volatile * ebcmd = (eboot_command volatile *)RTC_MEM;

// We want to run after an OTA has completed and the bin has been moved to its
// final resting place in flash. We want to catch the moment of the 1st boot
// of this new sketch. Then verify we have a valid erase option.
if (0U == ebcmd->magic &&
0U == ebcmd->crc32 &&
ACTION_COPY_RAW == ebcmd->action &&
ebcmd->args[4] == ebcmd->args[6] &&
ebcmd->args[5] == ebcmd->args[7] &&
ebcmd->args[4] == ~ebcmd->args[5] &&
0U == (ebcmd->args[4] & ~ERASE_CONFIG_ALL_DATA)) {

uint32_t erase_flash_option = ebcmd->args[4];

// Make sure we don't repeat
for (size_t i=4; i<=7; i++)
ebcmd->args[i] = 0U;

if (erase_flash_option) {
erase_config(erase_flash_option);
system_restart();
while(true){}
}
}
return true;
}

};

#endif // ERASE_CONFIG_H
43 changes: 43 additions & 0 deletions cores/esp8266/erase_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef ERASE_CONFIG_H
#define ERASE_CONFIG_H


#ifdef __cplusplus
extern "C" {
#endif

typedef enum ERASE_CONFIG {
// Note, The Arduino ESP8266 flash memory map differs from Espressif's.
// Espressive has separate sectors for PHY init data and RF_CAL. In Arduino
// ESP8266 RF_CAL and PHY INIT share the same sector. Between two RF init user
// calls the PHY init data is overlayed. Thus avoiding the waste of a 4K sector
// to store only 128 bytes.
//
// Mapping of Sectors at end of Flash, adapted for the Arduino ESP8266.
// Defaults to using the IDE configured flash size for placement
//_______________________________________________________________________________________
//_Bit_number_for_Mask______|_____4_____|_____3_____|_____2_____|_____1_____|_____0_____|
// | | RF_CAL | SDK Parameter Area |
// Overlay at RF init | | PHY INIT | | | |
// Persistant data | | | | SSID/PW | |
// User storage | EEPROM | | | | |
// Often shown downloaded | | BLANK.BIN | | BLANK.BIN | |
//__________________________|___________|___________|___________|___________|___________|
ERASE_CONFIG_NONE = 0, //| | | | | |
ERASE_CONFIG_EEPROM = ( BIT(4) ),
ERASE_CONFIG_RF_CAL = ( BIT(3) ),
ERASE_CONFIG_PERSISTANT = ( BIT(1) ),
ERASE_CONFIG_BLANK_BIN = ( BIT(3) | BIT(1) ),
ERASE_CONFIG_SDK_DATA = ( BIT(3) | BIT(2) | BIT(1) | BIT(0) ),
ERASE_CONFIG_ALL_DATA = ( BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0) )
//__________________________|___________|___________|___________|___________|___________|
} ERASE_CONFIG_MASK_t; // Use one of these with eraseConfig

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

#ifdef __cplusplus
};
#endif

#endif
0