@@ -801,7 +801,21 @@ public string FlashMd5Sum(uint addr, uint size)
801801 return System . Text . Encoding . ASCII . GetString ( resp ) . ToUpper ( ) ;
802802 }
803803 }
804+ // Diagnostic: try raw read to see if stub sent anything unexpected
804805 addErrorLine ( $ "MD5 command returned no response (timeout after { timeout } ms)") ;
806+ try
807+ {
808+ serial . ReadTimeout = 500 ;
809+ int avail = serial . BytesToRead ;
810+ addLogLine ( $ "Serial buffer has { avail } bytes after MD5 timeout.") ;
811+ if ( avail > 0 )
812+ {
813+ byte [ ] raw = new byte [ Math . Min ( avail , 64 ) ] ;
814+ int read = serial . Read ( raw , 0 , raw . Length ) ;
815+ addLogLine ( $ "Raw bytes: { BitConverter . ToString ( raw , 0 , read ) } ") ;
816+ }
817+ }
818+ catch { }
805819 }
806820 catch ( Exception ex )
807821 {
@@ -1418,29 +1432,19 @@ public void doWrite(uint offset, byte[] data)
14181432 logger . setProgress ( ( int ) ( ( i + 1 ) * blockSize ) , data . Length ) ;
14191433 }
14201434
1421- // FLASH_END: execute (1 = run, 0 = stay)
1422- addLogLine ( "Sending FLASH_END..." ) ;
1423- sendCommand ( ESPCommand . FLASH_END , BitConverter . GetBytes ( ( uint ) 0 ) ) ;
1424- var endResp = readPacket ( 1000 , ESPCommand . FLASH_END ) ;
1425- if ( endResp == null )
1426- addWarningLine ( "FLASH_END: no response (non-fatal)." ) ;
1427- else
1428- addLogLine ( "FLASH_END OK." ) ;
1429-
14301435 swWrite . Stop ( ) ;
14311436 double secsWrite = swWrite . Elapsed . TotalSeconds ;
14321437 double kbitsWrite = ( data . Length * 8.0 / 1000.0 ) / secsWrite ;
14331438 double kbytesWrite = ( data . Length / 1024.0 ) / secsWrite ;
14341439 addLogLine ( $ "Wrote { data . Length } bytes at 0x{ offset : X8} in { secsWrite : F1} s ({ kbitsWrite : F1} kbit/s, { kbytesWrite : F1} KB/s)") ;
14351440 addSuccess ( "Flash Write Complete!" + Environment . NewLine ) ;
14361441 logger . setState ( "Write complete" , Color . LightGreen ) ;
1437-
1442+
1443+ // Verify with MD5 BEFORE sending FLASH_END (stub must still be running)
14381444 if ( isStub )
14391445 {
14401446 logger . setState ( "Verifying MD5..." , Color . LightBlue ) ;
14411447 addLogLine ( "Verifying write with MD5..." ) ;
1442- // Small delay to let stub settle after FLASH_END
1443- System . Threading . Thread . Sleep ( 100 ) ;
14441448 try
14451449 {
14461450 using ( var md5 = MD5 . Create ( ) )
@@ -1451,14 +1455,24 @@ public void doWrite(uint offset, byte[] data)
14511455 string actual = FlashMd5Sum ( offset , ( uint ) data . Length ) ;
14521456 if ( actual == null ) addErrorLine ( "Failed to get Flash MD5" ) ;
14531457 else if ( actual != expected ) addErrorLine ( $ "MD5 Mismatch! Expected { expected } , Got { actual } ") ;
1454- else { addLogLine ( "Write Verified Successfully!" ) ; logger . setState ( "Write verified" , Color . LightGreen ) ; }
1458+ else { addSuccess ( "Write Verified Successfully!" + Environment . NewLine ) ; logger . setState ( "Write verified" , Color . LightGreen ) ; }
14551459 }
14561460 }
14571461 catch ( Exception ex )
14581462 {
14591463 addErrorLine ( "Verification exception: " + ex . Message ) ;
14601464 }
14611465 }
1466+
1467+ // FLASH_END: no_entry=1 means stay in bootloader (don't reboot)
1468+ // esptool: struct.pack("<I", int(not reboot)) where reboot=False sends 1
1469+ addLogLine ( "Sending FLASH_END..." ) ;
1470+ sendCommand ( ESPCommand . FLASH_END , BitConverter . GetBytes ( ( uint ) 1 ) ) ;
1471+ var endResp = readPacket ( 1000 , ESPCommand . FLASH_END ) ;
1472+ if ( endResp == null )
1473+ addWarningLine ( "FLASH_END: no response (non-fatal)." ) ;
1474+ else
1475+ addLogLine ( "FLASH_END OK." ) ;
14621476 }
14631477 }
14641478 }
0 commit comments