@@ -23,6 +23,7 @@ extern "C" {
23
23
}
24
24
25
25
extern " C" uint32_t _FS_start;
26
+ extern " C" uint32_t _FS_end;
26
27
27
28
UpdaterClass::UpdaterClass ()
28
29
: _async(false )
@@ -105,15 +106,17 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
105
106
106
107
wifi_set_sleep_type (NONE_SLEEP_T);
107
108
109
+ // address where we will start writing the update
108
110
uintptr_t updateStartAddress = 0 ;
111
+ // size of current sketch rounded to a sector
112
+ size_t currentSketchSize = (ESP.getSketchSize () + FLASH_SECTOR_SIZE - 1 ) & (~(FLASH_SECTOR_SIZE - 1 ));
113
+ // size of the update rounded to a sector
114
+ size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1 ) & (~(FLASH_SECTOR_SIZE - 1 ));
115
+
109
116
if (command == U_FLASH) {
110
- // size of current sketch rounded to a sector
111
- size_t currentSketchSize = (ESP.getSketchSize () + FLASH_SECTOR_SIZE - 1 ) & (~(FLASH_SECTOR_SIZE - 1 ));
112
117
// address of the end of the space available for sketch and update
113
118
uintptr_t updateEndAddress = (uintptr_t )&_FS_start - 0x40200000 ;
114
- // size of the update rounded to a sector
115
- size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1 ) & (~(FLASH_SECTOR_SIZE - 1 ));
116
- // address where we will start writing the update
119
+
117
120
updateStartAddress = (updateEndAddress > roundedSize)? (updateEndAddress - roundedSize) : 0 ;
118
121
119
122
#ifdef DEBUG_UPDATER
@@ -129,7 +132,24 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
129
132
}
130
133
}
131
134
else if (command == U_FS) {
132
- updateStartAddress = (uintptr_t )&_FS_start - 0x40200000 ;
135
+ if ((uintptr_t )&_FS_start + roundedSize > (uintptr_t )&_FS_end) {
136
+ _setError (UPDATE_ERROR_SPACE);
137
+ return false ;
138
+ }
139
+
140
+ #ifdef ATOMIC_FS_UPDATE
141
+ // address of the end of the space available for update
142
+ uintptr_t updateEndAddress = (uintptr_t )&_FS_start - 0x40200000 ;
143
+
144
+ updateStartAddress = (updateEndAddress > roundedSize)? (updateEndAddress - roundedSize) : 0 ;
145
+
146
+ if (updateStartAddress < currentSketchSize) {
147
+ _setError (UPDATE_ERROR_SPACE);
148
+ return false ;
149
+ }
150
+ #else
151
+ updateStartAddress = (uintptr_t )&_FS_start - 0x40200000 ;
152
+ #endif
133
153
}
134
154
else {
135
155
// unknown command
@@ -272,8 +292,19 @@ bool UpdaterClass::end(bool evenIfRemaining){
272
292
273
293
#ifdef DEBUG_UPDATER
274
294
DEBUG_UPDATER.printf_P (PSTR (" Staged: address:0x%08X, size:0x%08zX\n " ), _startAddress, _size);
295
+ #endif
275
296
}
276
297
else if (_command == U_FS) {
298
+ #ifdef ATOMIC_FS_UPDATE
299
+ eboot_command ebcmd;
300
+ ebcmd.action = ACTION_COPY_RAW;
301
+ ebcmd.args [0 ] = _startAddress;
302
+ ebcmd.args [1 ] = (uintptr_t )&_FS_start - 0x40200000 ;
303
+ ebcmd.args [2 ] = _size;
304
+ eboot_command_write (&ebcmd);
305
+ #endif
306
+
307
+ #ifdef DEBUG_UPDATER
277
308
DEBUG_UPDATER.printf_P (PSTR (" Filesystem: address:0x%08X, size:0x%08zX\n " ), _startAddress, _size);
278
309
#endif
279
310
}
@@ -387,7 +418,7 @@ bool UpdaterClass::_verifyHeader(uint8_t data) {
387
418
}
388
419
return true ;
389
420
} else if (_command == U_FS) {
390
- // no check of SPIFFS possible with first byte.
421
+ // no check of FS possible with first byte.
391
422
return true ;
392
423
}
393
424
return false ;
@@ -421,7 +452,7 @@ bool UpdaterClass::_verifyEnd() {
421
452
422
453
return true ;
423
454
} else if (_command == U_FS) {
424
- // SPIFFS is already over written checks make no sense any more.
455
+ // FS is already over written checks make no sense any more.
425
456
return true ;
426
457
}
427
458
return false ;
0 commit comments