Skip to content

Commit ef453a5

Browse files
me-no-devCopilotpre-commit-ci-lite[bot]
authored
feat(update): Allow updating any type of file system (#11856)
* feat(update): Allow updating any type of file system * fix(style): Update libraries/HTTPUpdate/src/HTTPUpdate.cpp Co-authored-by: Copilot <[email protected]> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent f4f4bc6 commit ef453a5

File tree

7 files changed

+123
-37
lines changed

7 files changed

+123
-37
lines changed

libraries/ArduinoOTA/src/ArduinoOTA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ String ArduinoOTAClass::readStringUntil(char end) {
182182
void ArduinoOTAClass::_onRx() {
183183
if (_state == OTA_IDLE) {
184184
int cmd = parseInt();
185-
if (cmd != U_FLASH && cmd != U_SPIFFS) {
185+
if (cmd != U_FLASH && cmd != U_FLASHFS) {
186186
return;
187187
}
188188
_cmd = cmd;

libraries/HTTPUpdate/src/HTTPUpdate.cpp

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,59 @@ HTTPUpdateResult HTTPUpdate::update(NetworkClient &client, const String &url, co
5252
if (!http.begin(client, url)) {
5353
return HTTP_UPDATE_FAILED;
5454
}
55-
return handleUpdate(http, currentVersion, false, requestCB);
55+
return handleUpdate(http, currentVersion, U_FLASH, requestCB);
56+
}
57+
58+
HTTPUpdateResult HTTPUpdate::updateFs(HTTPClient &httpClient, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
59+
return handleUpdate(httpClient, currentVersion, U_FLASHFS, requestCB);
5660
}
5761

5862
HTTPUpdateResult HTTPUpdate::updateSpiffs(HTTPClient &httpClient, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
59-
return handleUpdate(httpClient, currentVersion, true, requestCB);
63+
return handleUpdate(httpClient, currentVersion, U_SPIFFS, requestCB);
64+
}
65+
66+
HTTPUpdateResult HTTPUpdate::updateFatfs(HTTPClient &httpClient, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
67+
return handleUpdate(httpClient, currentVersion, U_FATFS, requestCB);
68+
}
69+
70+
HTTPUpdateResult HTTPUpdate::updateLittlefs(HTTPClient &httpClient, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
71+
return handleUpdate(httpClient, currentVersion, U_LITTLEFS, requestCB);
72+
}
73+
74+
HTTPUpdateResult HTTPUpdate::updateFs(NetworkClient &client, const String &url, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
75+
HTTPClient http;
76+
if (!http.begin(client, url)) {
77+
return HTTP_UPDATE_FAILED;
78+
}
79+
return handleUpdate(http, currentVersion, U_FLASHFS, requestCB);
6080
}
6181

6282
HTTPUpdateResult HTTPUpdate::updateSpiffs(NetworkClient &client, const String &url, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
6383
HTTPClient http;
6484
if (!http.begin(client, url)) {
6585
return HTTP_UPDATE_FAILED;
6686
}
67-
return handleUpdate(http, currentVersion, true, requestCB);
87+
return handleUpdate(http, currentVersion, U_SPIFFS, requestCB);
88+
}
89+
90+
HTTPUpdateResult HTTPUpdate::updateFatfs(NetworkClient &client, const String &url, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
91+
HTTPClient http;
92+
if (!http.begin(client, url)) {
93+
return HTTP_UPDATE_FAILED;
94+
}
95+
return handleUpdate(http, currentVersion, U_FATFS, requestCB);
96+
}
97+
98+
HTTPUpdateResult HTTPUpdate::updateLittlefs(NetworkClient &client, const String &url, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
99+
HTTPClient http;
100+
if (!http.begin(client, url)) {
101+
return HTTP_UPDATE_FAILED;
102+
}
103+
return handleUpdate(http, currentVersion, U_LITTLEFS, requestCB);
68104
}
69105

70106
HTTPUpdateResult HTTPUpdate::update(HTTPClient &httpClient, const String &currentVersion, HTTPUpdateRequestCB requestCB) {
71-
return handleUpdate(httpClient, currentVersion, false, requestCB);
107+
return handleUpdate(httpClient, currentVersion, U_FLASH, requestCB);
72108
}
73109

74110
HTTPUpdateResult
@@ -77,7 +113,7 @@ HTTPUpdateResult
77113
if (!http.begin(client, host, port, uri)) {
78114
return HTTP_UPDATE_FAILED;
79115
}
80-
return handleUpdate(http, currentVersion, false, requestCB);
116+
return handleUpdate(http, currentVersion, U_FLASH, requestCB);
81117
}
82118

83119
/**
@@ -158,7 +194,7 @@ String getSketchSHA256() {
158194
* @param currentVersion const char *
159195
* @return HTTPUpdateResult
160196
*/
161-
HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &currentVersion, bool spiffs, HTTPUpdateRequestCB requestCB) {
197+
HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &currentVersion, uint8_t type, HTTPUpdateRequestCB requestCB) {
162198

163199
HTTPUpdateResult ret = HTTP_UPDATE_FAILED;
164200

@@ -187,8 +223,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &curren
187223
http.addHeader("x-ESP32-chip-size", String(ESP.getFlashChipSize()));
188224
http.addHeader("x-ESP32-sdk-version", ESP.getSdkVersion());
189225

190-
if (spiffs) {
226+
if (type == U_SPIFFS) {
191227
http.addHeader("x-ESP32-mode", "spiffs");
228+
} else if (type == U_FATFS) {
229+
http.addHeader("x-ESP32-mode", "fatfs");
230+
} else if (type == U_LITTLEFS) {
231+
http.addHeader("x-ESP32-mode", "littlefs");
232+
} else if (type == U_FLASHFS) {
233+
http.addHeader("x-ESP32-mode", "flashfs");
192234
} else {
193235
http.addHeader("x-ESP32-mode", "sketch");
194236
}
@@ -251,8 +293,24 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &curren
251293
case HTTP_CODE_OK: ///< OK (Start Update)
252294
if (len > 0) {
253295
bool startUpdate = true;
254-
if (spiffs) {
255-
const esp_partition_t *_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
296+
if (type != U_FLASH) {
297+
const esp_partition_t *_partition = NULL;
298+
if (type == U_SPIFFS) {
299+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
300+
} else if (type == U_FATFS) {
301+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
302+
} else if (type == U_LITTLEFS) {
303+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, NULL);
304+
} else if (type == U_FLASHFS) {
305+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
306+
if (!_partition) {
307+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
308+
}
309+
if (!_partition) {
310+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, NULL);
311+
}
312+
}
313+
256314
if (!_partition) {
257315
_lastError = HTTP_UE_NO_PARTITION;
258316
return HTTP_UPDATE_FAILED;
@@ -291,17 +349,15 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &curren
291349

292350
delay(100);
293351

294-
int command;
352+
int command = type;
295353

296-
if (spiffs) {
297-
command = U_SPIFFS;
298-
log_d("runUpdate spiffs...\n");
299-
} else {
300-
command = U_FLASH;
354+
if (type == U_FLASH) {
301355
log_d("runUpdate flash...\n");
356+
} else {
357+
log_d("runUpdate file system...\n");
302358
}
303359

304-
if (!spiffs) {
360+
if (type == U_FLASH) {
305361
/* To do
306362
uint8_t buf[4];
307363
if(tcp->peekBytes(&buf[0], 4) != 4) {
@@ -341,7 +397,7 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &curren
341397
_cbEnd();
342398
}
343399

344-
if (_rebootOnUpdate && !spiffs) {
400+
if (_rebootOnUpdate && type == U_FLASH) {
345401
ESP.restart();
346402
}
347403

libraries/HTTPUpdate/src/HTTPUpdate.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,17 @@ class HTTPUpdate {
9898
NetworkClient &client, const String &host, uint16_t port, const String &uri = "/", const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL
9999
);
100100

101+
t_httpUpdate_return updateFs(NetworkClient &client, const String &url, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
101102
t_httpUpdate_return updateSpiffs(NetworkClient &client, const String &url, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
103+
t_httpUpdate_return updateFatfs(NetworkClient &client, const String &url, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
104+
t_httpUpdate_return updateLittlefs(NetworkClient &client, const String &url, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
102105

103106
t_httpUpdate_return update(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
104107

108+
t_httpUpdate_return updateFs(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
105109
t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
110+
t_httpUpdate_return updateFatfs(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
111+
t_httpUpdate_return updateLittlefs(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
106112

107113
// Notification callbacks
108114
void onStart(HTTPUpdateStartCB cbOnStart) {
@@ -122,7 +128,7 @@ class HTTPUpdate {
122128
String getLastErrorString(void);
123129

124130
protected:
125-
t_httpUpdate_return handleUpdate(HTTPClient &http, const String &currentVersion, bool spiffs = false, HTTPUpdateRequestCB requestCB = NULL);
131+
t_httpUpdate_return handleUpdate(HTTPClient &http, const String &currentVersion, uint8_t type = U_FLASH, HTTPUpdateRequestCB requestCB = NULL);
126132
bool runUpdate(Stream &in, uint32_t size, String md5, int command = U_FLASH);
127133

128134
// Set the error and potentially use a CB to notify the application

libraries/HTTPUpdateServer/src/HTTPUpdateServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class HTTPUpdateServer {
122122
Serial.printf("Update: %s\n", upload.filename.c_str());
123123
}
124124
if (upload.name == "filesystem") {
125-
if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS)) { //Instead of SPIFFS.totalBytes(). Fix https://github.com/espressif/arduino-esp32/issues/9967
125+
if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASHFS)) { //Instead of SPIFFS.totalBytes(). Fix https://github.com/espressif/arduino-esp32/issues/9967
126126
if (_serial_output) {
127127
Update.printError(Serial);
128128
}

libraries/Update/examples/HTTP_Server_AES_OTA_Update/HTTP_Server_AES_OTA_Update.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defaults:- {if not set ie. "Update.setupCrypt();" }
1818
1919
OTA_MODE options:-
2020
U_AES_DECRYPT_NONE decryption disabled, loads OTA image files as sent(plain)
21-
U_AES_DECRYPT_AUTO auto loads both plain & encrypted OTA FLASH image files, and plain OTA SPIFFS image files
21+
U_AES_DECRYPT_AUTO auto loads both plain & encrypted OTA FLASH image files, and plain OTA File System image files
2222
U_AES_DECRYPT_ON decrypts OTA image files
2323
2424
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/
@@ -36,7 +36,6 @@ espsecure.py encrypt_flash_data = runs the idf encryption function to make a en
3636

3737
#include <WiFi.h>
3838
#include <NetworkClient.h>
39-
#include <SPIFFS.h>
4039
#include <Update.h>
4140
#include <WebServer.h>
4241
#include <ESPmDNS.h>
@@ -145,7 +144,7 @@ void setupHttpUpdateServer() {
145144
if (upload.status == UPLOAD_FILE_START) {
146145
Serial.printf("Update: %s\n", upload.filename.c_str());
147146
if (upload.name == "filesystem") {
148-
if (!Update.begin(SPIFFS.totalBytes(), U_SPIFFS)) { //start with max available size
147+
if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASHFS)) { //start with max available size
149148
Update.printError(Serial);
150149
}
151150
} else {

libraries/Update/src/Update.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929

3030
#define UPDATE_SIZE_UNKNOWN 0xFFFFFFFF
3131

32-
#define U_FLASH 0
33-
#define U_SPIFFS 100
34-
#define U_AUTH 200
32+
#define U_FLASH 0
33+
#define U_FLASHFS 100
34+
#define U_SPIFFS 101
35+
#define U_FATFS 102
36+
#define U_LITTLEFS 103
37+
#define U_AUTH 200
3538

3639
#define ENCRYPTED_BLOCK_SIZE 16
3740
#define ENCRYPTED_TWEAK_BLOCK_SIZE 32
@@ -267,7 +270,6 @@ class UpdateClass {
267270
size_t _size;
268271
THandlerFunction_Progress _progress_callback;
269272
uint32_t _progress;
270-
uint32_t _paroffset;
271273
uint32_t _command;
272274
const esp_partition_t *_partition;
273275

libraries/Update/src/Updater.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ UpdateClass::UpdateClass()
7575
#ifndef UPDATE_NOCRYPT
7676
_cryptKey(0), _cryptBuffer(0),
7777
#endif /* UPDATE_NOCRYPT */
78-
_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL)
78+
_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _command(U_FLASH), _partition(NULL)
7979
#ifndef UPDATE_NOCRYPT
8080
,
8181
_cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf)
@@ -154,16 +154,39 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
154154
}
155155
log_d("OTA Partition: %s", _partition->label);
156156
} else if (command == U_SPIFFS) {
157-
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, label);
158-
_paroffset = 0;
157+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
158+
if (!_partition) {
159+
_error = UPDATE_ERROR_NO_PARTITION;
160+
return false;
161+
}
162+
log_d("SPIFFS Partition: %s", _partition->label);
163+
} else if (command == U_FATFS) {
164+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
165+
if (!_partition) {
166+
_error = UPDATE_ERROR_NO_PARTITION;
167+
return false;
168+
}
169+
log_d("FATFS Partition: %s", _partition->label);
170+
} else if (command == U_LITTLEFS) {
171+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, NULL);
172+
if (!_partition) {
173+
_error = UPDATE_ERROR_NO_PARTITION;
174+
return false;
175+
}
176+
log_d("LittleFS Partition: %s", _partition->label);
177+
} else if (command == U_FLASHFS) {
178+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
159179
if (!_partition) {
160180
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
161-
_paroffset = 0x1000; //Offset for ffat, assuming size is already corrected
162-
if (!_partition) {
163-
_error = UPDATE_ERROR_NO_PARTITION;
164-
return false;
165-
}
166181
}
182+
if (!_partition) {
183+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, NULL);
184+
}
185+
if (!_partition) {
186+
_error = UPDATE_ERROR_NO_PARTITION;
187+
return false;
188+
}
189+
log_d("FS Partition: %s", _partition->label);
167190
} else {
168191
_error = UPDATE_ERROR_BAD_ARGUMENT;
169192
log_e("bad command %u", command);
@@ -452,7 +475,7 @@ bool UpdateClass::_verifyHeader(uint8_t data) {
452475
return false;
453476
}
454477
return true;
455-
} else if (_command == U_SPIFFS) {
478+
} else {
456479
return true;
457480
}
458481
return false;
@@ -471,7 +494,7 @@ bool UpdateClass::_verifyEnd() {
471494
}
472495
_reset();
473496
return true;
474-
} else if (_command == U_SPIFFS) {
497+
} else {
475498
_reset();
476499
return true;
477500
}

0 commit comments

Comments
 (0)