@@ -32,6 +32,7 @@ void UpdaterClass::_reset() {
32
32
_currentAddress = 0 ;
33
33
_size = 0 ;
34
34
_command = U_FLASH;
35
+ _target_md5 = 0 ;
35
36
}
36
37
37
38
bool UpdaterClass::begin (size_t size, int command) {
@@ -96,10 +97,17 @@ bool UpdaterClass::begin(size_t size, int command) {
96
97
_size = size;
97
98
_buffer = new uint8_t [FLASH_SECTOR_SIZE];
98
99
_command = command;
99
-
100
+
101
+ _target_md5 = new char [64 ];
102
+ _md5.begin ();
100
103
return true ;
101
104
}
102
105
106
+ void UpdaterClass::setMD5 (const char * expected_md5){
107
+ if (strlen (expected_md5) != 32 ) return ;
108
+ strcpy (_target_md5, expected_md5);
109
+ }
110
+
103
111
bool UpdaterClass::end (bool evenIfRemaining){
104
112
if (_size == 0 ){
105
113
#ifdef DEBUG_UPDATER
@@ -123,7 +131,21 @@ bool UpdaterClass::end(bool evenIfRemaining){
123
131
}
124
132
_size = progress ();
125
133
}
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
+
127
149
if (_command == U_FLASH) {
128
150
eboot_command ebcmd;
129
151
ebcmd.action = ACTION_COPY_RAW;
@@ -157,6 +179,7 @@ bool UpdaterClass::_writeBuffer(){
157
179
#endif
158
180
return false ;
159
181
}
182
+ _md5.add (_buffer, _bufferLen);
160
183
_currentAddress += _bufferLen;
161
184
_bufferLen = 0 ;
162
185
return true ;
@@ -232,6 +255,8 @@ void UpdaterClass::printError(Stream &out){
232
255
out.println (" Bad Size Given" );
233
256
} else if (_error == UPDATE_ERROR_STREAM){
234
257
out.println (" Stream Read Timeout" );
258
+ } else if (_error == UPDATE_ERROR_MD5){
259
+ out.println (" MD5 Check Failed" );
235
260
} else {
236
261
out.println (" UNKNOWN" );
237
262
}
0 commit comments