Skip to content

Commit 22d61aa

Browse files
author
technopolistv
committed
first commit
0 parents  commit 22d61aa

11 files changed

+687
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.pio/*
2+
.vscode/*

LICENSE.md

+427
Large diffs are not rendered by default.

README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Self-destructing Arduino ESP32 Firmware - Stage 2
2+
3+
![Arduino ESP32 Self-destructing Firmware](/self-destructing-arduino-esp32-firmware-platformio.png)
4+
5+
> Is it possible to erase the firmware of an ESP32 from within itself? Let's try to find out.
6+
7+
If you want to erase the ESP32's flash content, the easiest way would be using `esptool.py` with the `erase_flash` parameter. But how we can do this from within the running Arduino Sketch?
8+
9+
## Installation
10+
11+
![PlatformIO](/platformio.png)
12+
13+
Open the `ESP32-SelfDestruct-Payload` project. Click on the _PlatformIO Icon_ on the sidebar. Under `General` click on `Build`.
14+
15+
Copy the `firmware.bin` file under the folder `.pio/build/{BOARD}/firmware.bin` into the `data` folder of the [ESP32-SelfDestruct](https://github.com/technopolistv/ESP32-SelfDestruct/) project.
16+
17+
Do _not_ upload this code to your ESP32! Just build the project and move the `firmware.bin` into the `data` folder of the `ESP32-SelfDestruct` project and read the [instructions](https://github.com/technopolistv/ESP32-SelfDestruct/).
18+
19+
## Demo Screenshot
20+
21+
The green part highlights the main firmware (Stage 1) and the red part the stub firmware (Stage 2).
22+
23+
![Serial Monitor Arduino ESP32 Self-destructing Firmware](/self-destruct-serial-monitor.png)
24+
25+
## Partition layout of Self-Destructing Firmware
26+
27+
<table>
28+
<thead>
29+
<tr>
30+
<th>Offset</th>
31+
<th>Before Update</th>
32+
<th>After Update</th>
33+
<th>After Erase</th>
34+
</tr>
35+
</thead>
36+
<tbody>
37+
<tr>
38+
<td>0x9000</td>
39+
<td>NVS (Data)</td>
40+
<td>NVS (Data)</td>
41+
<td>NVS (Empty)</td>
42+
</tr>
43+
<tr>
44+
<td>0xe000</td>
45+
<td>OTA<br></td>
46+
<td>OTA</td>
47+
<td>OTA (Empty)</td>
48+
</tr>
49+
<tr>
50+
<td>0x10000</td>
51+
<td>APP0 (Firmware)</td>
52+
<td>APP0 (Firmware disabled)</td>
53+
<td>APP0 (Empty)</td>
54+
</tr>
55+
<tr>
56+
<td>0x150000</td>
57+
<td>APP1 (Empty)</td>
58+
<td>APP1 (Stub Firmware actived)</td>
59+
<td>APP1 (Stub Firmware)</td>
60+
</tr>
61+
<tr>
62+
<td>0x290000</td>
63+
<td>SPIFFS (Stub Firmware)</td>
64+
<td>SPIFFS (Stub Firmware)</td>
65+
<td>SPIFFS (Empty)</td>
66+
</tr>
67+
</tbody>
68+
</table>
69+
70+
## More
71+
- Stage 1 - https://github.com/technopolistv/ESP32-SelfDestruct/
72+
- Stage 2 - This Project
73+
74+
✍️ Blog: https://www.technopolis.tv/PlatformIO-Self-destructing-Arduino-ESP32-Firmware/

include/README

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

platformio.ini

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp32dev]
12+
platform = espressif32
13+
board = esp32dev
14+
framework = arduino
15+
monitor_speed = 115200
16+
monitor_filters = esp32_exception_decoder

platformio.png

36.6 KB
Loading

self-destruct-serial-monitor.png

164 KB
Loading
Loading

src/main.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
https://www.technopolis.tv/PlatformIO-Self-destructing-Arduino-ESP32-Firmware/
3+
*/
4+
5+
#include <Arduino.h>
6+
7+
#define NAME "Self Destruct Payload"
8+
#define VERSION "v1.0.0"
9+
#define BUILD NAME " " VERSION
10+
#define RED_LED 26
11+
#define BUTTON 32
12+
13+
void setup() {
14+
pinMode(RED_LED, OUTPUT);
15+
pinMode(BUTTON, INPUT_PULLUP);
16+
17+
// turn on the red LED to indicate second stage
18+
digitalWrite(RED_LED, HIGH);
19+
20+
Serial.begin(115200);
21+
Serial.println(BUILD);
22+
23+
// erase everything except the own partition (APP1)
24+
Serial.print("Erase NVS: ");
25+
if(esp_flash_erase_region(NULL, 0x9000, 0x9000) == ESP_OK) {
26+
Serial.println("OK");
27+
} else {
28+
Serial.println("ERR");
29+
}
30+
31+
Serial.print("Erase OTA Data: ");
32+
if(esp_flash_erase_region(NULL, 0xe000, 0x2000) == ESP_OK) {
33+
Serial.println("OK");
34+
} else {
35+
Serial.println("ERR");
36+
}
37+
38+
Serial.print("Erase SPIFFS: ");
39+
if(esp_flash_erase_region(NULL, 0x290000, 0x160000) == ESP_OK) {
40+
Serial.println("OK");
41+
} else {
42+
Serial.println("ERR");
43+
}
44+
45+
Serial.print("Erase APP0: ");
46+
if(esp_flash_erase_region(NULL, 0x10000, 0x140000) == ESP_OK) {
47+
Serial.println("OK");
48+
} else {
49+
Serial.println("ERR");
50+
}
51+
52+
// uncommenting this part would crash the ESP
53+
// Serial.print("Erase APP1: ");
54+
// if(esp_flash_erase_region(NULL, 0x150000, 0x140000) == ESP_OK) {
55+
// Serial.println("OK");
56+
// } else {
57+
// Serial.println("ERR");
58+
// }
59+
60+
// turn off red LED to indicate that erasing process is finished
61+
digitalWrite(RED_LED, LOW);
62+
}
63+
64+
void loop() {
65+
if (digitalRead(BUTTON) == LOW) {
66+
digitalWrite(RED_LED, !digitalRead(RED_LED));
67+
68+
Serial.println("Nothing to see here...");
69+
70+
delay(500);
71+
}
72+
}

test/README

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PlatformIO Test Runner and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PlatformIO Unit Testing:
11+
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

0 commit comments

Comments
 (0)