@@ -32,6 +32,7 @@ void UpdaterClass::_reset() {
3232 _currentAddress = 0 ;
3333 _size = 0 ;
3434 _command = U_FLASH;
35+ _target_md5 = 0 ;
3536}
3637
3738bool UpdaterClass::begin (size_t size, int command) {
@@ -96,10 +97,17 @@ bool UpdaterClass::begin(size_t size, int command) {
9697 _size = size;
9798 _buffer = new uint8_t [FLASH_SECTOR_SIZE];
9899 _command = command;
99-
100+
101+ _target_md5 = new char [64 ];
102+ _md5.begin ();
100103 return true ;
101104}
102105
106+ void UpdaterClass::setMD5 (const char * expected_md5){
107+ if (strlen (expected_md5) != 32 ) return ;
108+ strcpy (_target_md5, expected_md5);
109+ }
110+
103111bool UpdaterClass::end (bool evenIfRemaining){
104112 if (_size == 0 ){
105113#ifdef DEBUG_UPDATER
@@ -123,7 +131,21 @@ bool UpdaterClass::end(bool evenIfRemaining){
123131 }
124132 _size = progress ();
125133 }
126-
134+
135+ _md5.calculate ();
136+ if (_target_md5 && strlen (_target_md5) == 32 ){
137+ if (strcmp (_target_md5, _md5.toString ().c_str ()) != 0 ){
138+ _error = UPDATE_ERROR_MD5;
139+ #ifdef DEBUG_UPDATER
140+ DEBUG_UPDATER.printf (" MD5 Failed: expected:%s, calculated:%s\n " , _target_md5, _md5.toString ().c_str ());
141+ #endif
142+ return false ;
143+ }
144+ #ifdef DEBUG_UPDATER
145+ else DEBUG_UPDATER.printf (" MD5 Success: %s\n " , _md5.toString ().c_str ());
146+ #endif
147+ }
148+
127149 if (_command == U_FLASH) {
128150 eboot_command ebcmd;
129151 ebcmd.action = ACTION_COPY_RAW;
@@ -157,6 +179,7 @@ bool UpdaterClass::_writeBuffer(){
157179#endif
158180 return false ;
159181 }
182+ _md5.add (_buffer, _bufferLen);
160183 _currentAddress += _bufferLen;
161184 _bufferLen = 0 ;
162185 return true ;
@@ -232,6 +255,8 @@ void UpdaterClass::printError(Stream &out){
232255 out.println (" Bad Size Given" );
233256 } else if (_error == UPDATE_ERROR_STREAM){
234257 out.println (" Stream Read Timeout" );
258+ } else if (_error == UPDATE_ERROR_MD5){
259+ out.println (" MD5 Check Failed" );
235260 } else {
236261 out.println (" UNKNOWN" );
237262 }
0 commit comments