@@ -138,6 +138,9 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
138
138
DEBUG_UPDATER.printf (" [begin] _size: 0x%08X (%d)\n " , _size, _size);
139
139
#endif
140
140
141
+ if (!_verify) {
142
+ _md5.begin ();
143
+ }
141
144
return true ;
142
145
}
143
146
@@ -186,44 +189,19 @@ bool UpdaterClass::end(bool evenIfRemaining){
186
189
_reset ();
187
190
return false ;
188
191
}
189
- }
190
192
191
- _md5.begin ();
192
- int binSize = _size;
193
- if (_hash) {
193
+ int binSize = _size - sigLen - sizeof (uint32_t ) /* The siglen word */ ;
194
194
_hash->begin ();
195
- binSize -= sigLen + sizeof (uint32_t );
196
- }
197
195
#ifdef DEBUG_UPDATER
198
- DEBUG_UPDATER.printf (" [Updater] Adjusted binsize: %d\n " , binSize);
196
+ DEBUG_UPDATER.printf (" [Updater] Adjusted binsize: %d\n " , binSize);
199
197
#endif
200
- // Calculate the MD5 and hash using proper size
201
- uint8_t buff[128 ];
202
- for (int i = 0 ; i < binSize; i += sizeof (buff)) {
203
- ESP.flashRead (_startAddress + i, (uint32_t *)buff, sizeof (buff));
204
-
205
- size_t read = binSize - i;
206
- if (read > sizeof (buff)) {
207
- read = sizeof (buff);
208
- }
209
- _md5.add (buff, read );
210
- if (_hash) {
198
+ // Calculate the MD5 and hash using proper size
199
+ uint8_t buff[128 ];
200
+ for (int i = 0 ; i < binSize; i += sizeof (buff)) {
201
+ ESP.flashRead (_startAddress + i, (uint32_t *)buff, sizeof (buff));
202
+ size_t read = std::min ((int )sizeof (buff), binSize - i);
211
203
_hash->add (buff, read );
212
204
}
213
- }
214
- _md5.calculate ();
215
- if (_target_md5.length ()) {
216
- if (_target_md5 != _md5.toString ()){
217
- _setError (UPDATE_ERROR_MD5);
218
- _reset ();
219
- return false ;
220
- }
221
- #ifdef DEBUG_UPDATER
222
- else DEBUG_UPDATER.printf (" MD5 Success: %s\n " , _target_md5.c_str ());
223
- #endif
224
- }
225
-
226
- if (_verify && _hash) {
227
205
_hash->end ();
228
206
#ifdef DEBUG_UPDATER
229
207
unsigned char *ret = (unsigned char *)_hash->hash ();
@@ -250,6 +228,19 @@ bool UpdaterClass::end(bool evenIfRemaining){
250
228
_reset ();
251
229
return false ;
252
230
}
231
+ #ifdef DEBUG_UPDATER
232
+ DEBUG_UPDATER.printf (" [Updater] Signature matches\n " );
233
+ #endif
234
+ } else if (_target_md5.length ()) {
235
+ _md5.calculate ();
236
+ if (_target_md5 != _md5.toString ()){
237
+ _setError (UPDATE_ERROR_MD5);
238
+ _reset ();
239
+ return false ;
240
+ }
241
+ #ifdef DEBUG_UPDATER
242
+ else DEBUG_UPDATER.printf (" MD5 Success: %s\n " , _target_md5.c_str ());
243
+ #endif
253
244
}
254
245
255
246
if (!_verifyEnd ()) {
@@ -329,6 +320,9 @@ bool UpdaterClass::_writeBuffer(){
329
320
_setError (UPDATE_ERROR_WRITE);
330
321
return false ;
331
322
}
323
+ if (!_verify) {
324
+ _md5.add (_buffer, _bufferLen);
325
+ }
332
326
_currentAddress += _bufferLen;
333
327
_bufferLen = 0 ;
334
328
return true ;
0 commit comments