Skip to content

Commit 2377864

Browse files
committed
Added backup options for firmware and filesystem partitions (no encryption support)
1 parent 99e655b commit 2377864

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

embed/website.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ <h5 id="help_text">
301301
<div class="shadow_container">
302302

303303
<div class="mode_switch_container" id="mode_switch_container">
304-
<h4 id="label_firmware">Firmware</h4>
304+
<h4 id="label_firmware">Firmware (<a id="label_firmware_backup" href="/backup/fw">backup</a>)</h4>
305305
<div class="toggle_switch_container">
306306
<input
307307
type="checkbox"
@@ -312,7 +312,7 @@ <h4 id="label_firmware">Firmware</h4>
312312
<span class="toggle_switch_slider"></span>
313313
</label>
314314
</div>
315-
<h4 id="label_filesystem">File System</h4>
315+
<h4 id="label_filesystem">File System (<a id="label_filesystem_backup" href="/backup/fs">backup</a>)</h4>
316316
</div>
317317

318318
<div class="drop_zone" id="drop_zone">
@@ -426,7 +426,9 @@ <h6 id="clickable_file_input">or<br />click to select (.bin) file</h6>
426426
"clickable_file_input"
427427
);
428428
const label_firmware = document.getElementById("label_firmware");
429+
const label_firmware_backup = document.getElementById("label_firmware_backup");
429430
const label_filesystem = document.getElementById("label_filesystem");
431+
const label_filesystem_backup = document.getElementById("label_filesystem_backup");
430432
const mode_container = document.getElementById("mode_switch_container");
431433
const mode_checkbox = document.getElementById("toggle_switch_checkbox");
432434
const info_text_chipspecs = document.getElementById(
@@ -482,14 +484,19 @@ <h6 id="clickable_file_input">or<br />click to select (.bin) file</h6>
482484
getInfos();
483485

484486
label_firmware.style.color = "black";
487+
label_firmware_backup.style.color = "black";
485488

486489
mode_checkbox.addEventListener("change", function () {
487490
if (mode_checkbox.checked) {
488491
label_firmware.style.color = "gray";
492+
label_firmware_backup.style.color = "gray";
489493
label_filesystem.style.color = "black";
494+
label_filesystem_backup.style.color = "black";
490495
} else {
491496
label_firmware.style.color = "black";
497+
label_firmware_backup.style.color = "black";
492498
label_filesystem.style.color = "gray";
499+
label_filesystem_backup.style.color = "gray";
493500
}
494501
});
495502

src/main.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ static String getChipIDStr() {
4343
return espId;
4444
}
4545

46+
static void sendPartition(const esp_partition_t* partition) {
47+
webServer.setContentLength(partition->size);
48+
webServer.sendHeader("Content-Disposition", "attachment; filename=" + String(partition->label) + ".bin");
49+
webServer.send(200, "application/octet-stream", "");
50+
51+
uint8_t* buffer = new uint8_t[4094];
52+
uint32_t index = 0;
53+
54+
while (index < partition->size) {
55+
size_t len = partition->size - index;
56+
if (len > 4094)
57+
len = 4094;
58+
if (esp_partition_read(partition, index, buffer, len) == ESP_OK) {
59+
webServer.client().write(buffer, len);
60+
index += len;
61+
} else {
62+
webServer.send(500, "text/plain", "Cannot read partition");
63+
delete[] buffer;
64+
return;
65+
}
66+
}
67+
68+
delete[] buffer;
69+
}
70+
4671
static void start_web_server() {
4772
webServer.onNotFound([]() {
4873
webServer.sendHeader("Location", "/");
@@ -76,6 +101,24 @@ static void start_web_server() {
76101
webServer.send_P(200, "text/html", reinterpret_cast<const char*>(update_html_start), update_html_end - update_html_start);
77102
});
78103

104+
webServer.on("/backup/fw", HTTP_GET, [&]() {
105+
const esp_partition_t* partition = esp_ota_get_running_partition();
106+
if (!partition) {
107+
webServer.send(500, "text/plain", "Cannot find running partition");
108+
return;
109+
}
110+
sendPartition(partition);
111+
});
112+
113+
webServer.on("/backup/fs", HTTP_GET, [&]() {
114+
const esp_partition_t* partition = esp_partition_find_first(esp_partition_type_t::ESP_PARTITION_TYPE_DATA, esp_partition_subtype_t::ESP_PARTITION_SUBTYPE_DATA_SPIFFS, nullptr);
115+
if (!partition) {
116+
webServer.send(500, "text/plain", "Cannot find SPIFFS partition");
117+
return;
118+
}
119+
sendPartition(partition);
120+
});
121+
79122
webServer.on(
80123
"/",
81124
HTTP_POST,

0 commit comments

Comments
 (0)