hisiflash 0.3.0

Library for flashing HiSilicon chips
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
//! WS63 flasher implementation.
//!
//! This module provides the main flasher interface for the WS63 chip.
//!
//! ## Generic Port Support
//!
//! The flasher uses a generic `Port` trait, allowing it to work with different
//! serial port implementations:
//!
//! - **Native platforms**: Uses the `serialport` crate via `NativePort`
//! - **WASM/Web**: Can use Web Serial API via `WebSerialPort` (experimental)
//!
//! ## Example
//!
//! ```rust,no_run
//! use hisiflash::{ChipFamily, Fwpkg};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create flasher using chip abstraction
//!     let mut flasher = ChipFamily::Ws63.create_flasher("/dev/ttyUSB0", 921600, false, 0)?;
//!
//!     // Connect to device
//!     flasher.connect()?;
//!
//!     // Flash firmware
//!     let fwpkg = Fwpkg::from_file("firmware.fwpkg")?;
//!     flasher.flash_fwpkg(&fwpkg, None, &mut |name, current, total| {
//!         println!("Flashing {}: {}/{}", name, current, total);
//!     })?;
//!
//!     Ok(())
//! }
//! ```

use {
    crate::{
        CancelContext,
        error::{Error, Result},
        image::fwpkg::Fwpkg,
        port::Port,
        protocol::ymodem::{YmodemConfig, YmodemTransfer},
        target::ws63::protocol::{CommandFrame, DEFAULT_BAUD, contains_handshake_ack},
    },
    log::{debug, info, trace, warn},
    std::{
        thread,
        time::{Duration, Instant},
    },
};

/// Timeout for waiting for handshake.
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(30);

/// Delay after changing baud rate.
const BAUD_CHANGE_DELAY: Duration = Duration::from_millis(100);

/// Delay between partition transfers to prevent serial data stale.
const PARTITION_DELAY: Duration = Duration::from_millis(100);

/// Timeout for waiting for SEBOOT magic response.
const MAGIC_TIMEOUT: Duration = Duration::from_secs(10);

/// Delay between connection retry attempts.
const CONNECT_RETRY_DELAY: Duration = Duration::from_millis(500);

/// Maximum number of connection attempts.
const MAX_CONNECT_ATTEMPTS: usize = 7;

/// Maximum number of download retry attempts.
const MAX_DOWNLOAD_RETRIES: usize = 3;

fn is_interrupted_error(e: &Error) -> bool {
    match e {
        Error::Io(io) => {
            io.kind() == std::io::ErrorKind::Interrupted
                || io.raw_os_error() == Some(4)
                || io
                    .to_string()
                    .to_ascii_lowercase()
                    .contains("interrupted")
        },
        Error::Serial(serial) => {
            matches!(
                serial.kind(),
                serialport::ErrorKind::Io(std::io::ErrorKind::Interrupted)
            ) || serial
                .to_string()
                .to_ascii_lowercase()
                .contains("interrupted")
        },
        _ => e
            .to_string()
            .to_ascii_lowercase()
            .contains("interrupted"),
    }
}

fn sleep_interruptible(cancel: &CancelContext, total: Duration) -> Result<()> {
    const CHUNK: Duration = Duration::from_millis(20);

    let start = Instant::now();
    while start.elapsed() < total {
        cancel.check()?;
        let elapsed = start.elapsed();
        let remain = total.saturating_sub(elapsed);
        thread::sleep(remain.min(CHUNK));
    }

    Ok(())
}

/// WS63 flasher.
///
/// Generic over the port type `P`, which must implement the `Port` trait.
/// This allows the flasher to work with different serial port implementations.
pub struct Ws63Flasher<P: Port> {
    port: P,
    target_baud: u32,
    late_baud: bool,
    verbose: u8,
    cancel: CancelContext,
}

// Implementation for any Port type
impl<P: Port> Ws63Flasher<P> {
    /// Create a new WS63 flasher with an existing port.
    ///
    /// This flasher will NOT respond to Ctrl-C interrupts.
    /// For interruptible flasher, use [`with_cancel`](Self::with_cancel).
    ///
    /// # Arguments
    ///
    /// * `port` - An opened serial port implementing the `Port` trait
    /// * `target_baud` - Target baud rate for data transfer
    #[allow(dead_code)]
    pub fn new(port: P, target_baud: u32) -> Self {
        Self {
            port,
            target_baud,
            late_baud: false,
            verbose: 0,
            cancel: CancelContext::none(),
        }
    }

    /// Create a new WS63 flasher with custom cancel context.
    ///
    /// Use this when you need custom cancellation behavior (e.g., Ctrl-C support).
    ///
    /// # Arguments
    ///
    /// * `port` - An opened serial port implementing the `Port` trait
    /// * `target_baud` - Target baud rate for data transfer
    /// * `cancel` - Cancellation context for interruptible operations
    ///
    /// # Example
    ///
    /// ```ignore
    /// use hisiflash::CancelContext;
    ///
    /// // Create with global interrupt support
    /// let cancel = hisiflash::cancel_context_from_global();
    /// let flasher = Ws63Flasher::with_cancel(port, 921600, cancel);
    /// ```
    pub fn with_cancel(port: P, target_baud: u32, cancel: CancelContext) -> Self {
        Self {
            port,
            target_baud,
            late_baud: false,
            verbose: 0,
            cancel,
        }
    }

    /// Set late baud rate change mode.
    ///
    /// In late baud mode, the baud rate is changed after LoaderBoot is loaded,
    /// which may be necessary for some firmware configurations.
    #[must_use]
    pub fn with_late_baud(mut self, late_baud: bool) -> Self {
        self.late_baud = late_baud;
        self
    }

    /// Set verbose output level.
    #[must_use]
    pub fn with_verbose(mut self, verbose: u8) -> Self {
        self.verbose = verbose;
        self
    }

    /// Connect to the device.
    ///
    /// This waits for the device to boot into download mode and performs
    /// the initial handshake with retry mechanism.
    pub fn connect(&mut self) -> Result<()> {
        info!(
            "Waiting for device on {}...",
            self.port
                .name()
        );
        info!("Please reset the device to enter download mode.");

        for attempt in 1..=MAX_CONNECT_ATTEMPTS {
            self.cancel
                .check()?;

            if attempt > 1 {
                info!("Connection attempt {attempt}/{MAX_CONNECT_ATTEMPTS}");
            }

            match self.try_connect() {
                Ok(()) => {
                    return Ok(());
                },
                Err(e) => {
                    if is_interrupted_error(&e) {
                        return Err(e);
                    }

                    if attempt < MAX_CONNECT_ATTEMPTS {
                        warn!("Connection failed (attempt {attempt}/{MAX_CONNECT_ATTEMPTS}): {e}");
                        sleep_interruptible(&self.cancel, CONNECT_RETRY_DELAY)?;
                        self.port
                            .clear_buffers()?;
                    } else {
                        return Err(e);
                    }
                },
            }
        }

        Err(Error::Timeout(format!(
            "Connection failed after {MAX_CONNECT_ATTEMPTS} attempts"
        )))
    }

    /// Single connection attempt.
    fn try_connect(&mut self) -> Result<()> {
        self.cancel
            .check()?;

        self.port
            .clear_buffers()?;

        let start = Instant::now();
        let handshake_frame = CommandFrame::handshake(self.target_baud);
        let handshake_data = handshake_frame.build();

        // Send handshake frames repeatedly until we get a response
        while start.elapsed() < HANDSHAKE_TIMEOUT {
            self.cancel
                .check()?;

            // Send handshake
            if let Err(e) = self
                .port
                .write_all(&handshake_data)
            {
                if e.kind() == std::io::ErrorKind::Interrupted {
                    return Err(Error::Io(e));
                }
                trace!("Write error (ignoring): {e}");
            }
            if let Err(e) = self
                .port
                .flush()
            {
                if e.kind() == std::io::ErrorKind::Interrupted {
                    return Err(Error::Io(e));
                }
            }

            // Small delay
            sleep_interruptible(&self.cancel, Duration::from_millis(10))?;

            // Check for response
            let mut buf = [0u8; 256];
            match self
                .port
                .read(&mut buf)
            {
                Ok(n) if n > 0 => {
                    trace!("Received {n} bytes");
                    if contains_handshake_ack(&buf[..n]) {
                        info!("Handshake successful!");

                        // Change baud rate if not in late mode
                        if !self.late_baud && self.target_baud != DEFAULT_BAUD {
                            self.change_baud_rate(self.target_baud)?;
                        }

                        return Ok(());
                    }
                },
                Ok(_) => {},
                Err(e) if e.kind() == std::io::ErrorKind::TimedOut => {},
                Err(e) => {
                    if e.kind() == std::io::ErrorKind::Interrupted {
                        return Err(Error::Io(e));
                    }
                    trace!("Read error (ignoring): {e}");
                },
            }
        }

        Err(Error::Timeout(format!(
            "No response after {} seconds",
            HANDSHAKE_TIMEOUT.as_secs()
        )))
    }

    /// Change the baud rate.
    fn change_baud_rate(&mut self, baud: u32) -> Result<()> {
        self.cancel
            .check()?;

        info!("Changing baud rate to {baud}");

        // Send baud rate change command
        let frame = CommandFrame::set_baud_rate(baud);
        self.send_command(&frame)?;

        // Wait for command to be processed
        sleep_interruptible(&self.cancel, BAUD_CHANGE_DELAY)?;

        // Change local baud rate
        self.port
            .set_baud_rate(baud)?;

        // Clear buffers
        sleep_interruptible(&self.cancel, BAUD_CHANGE_DELAY)?;
        self.port
            .clear_buffers()?;

        debug!("Baud rate changed to {baud}");
        Ok(())
    }

    /// Send a command frame.
    fn send_command(&mut self, frame: &CommandFrame) -> Result<()> {
        let data = frame.build();
        trace!(
            "Sending command {:?}: {} bytes",
            frame.command(),
            data.len()
        );

        self.port
            .write_all(&data)?;
        self.port
            .flush()?;

        Ok(())
    }

    /// Wait for SEBOOT magic (0xDEADBEEF) response from device.
    ///
    /// After LoaderBoot YMODEM transfer or after sending a download command,
    /// the device responds with a SEBOOT frame starting with the magic bytes.
    /// This function reads bytes until the magic sequence is found, then
    /// drains the remaining frame data.
    fn wait_for_magic(&mut self, timeout: Duration) -> Result<()> {
        let magic: [u8; 4] = [0xEF, 0xBE, 0xAD, 0xDE]; // Little-endian DEADBEEF
        let start = Instant::now();
        let mut match_idx = 0;

        debug!("Waiting for SEBOOT magic...");

        while start.elapsed() < timeout {
            self.cancel
                .check()?;

            let mut buf = [0u8; 1];
            match self
                .port
                .read(&mut buf)
            {
                Ok(1) => {
                    if buf[0] == magic[match_idx] {
                        match_idx += 1;
                        if match_idx == magic.len() {
                            // Found magic, drain remaining frame data
                            sleep_interruptible(&self.cancel, Duration::from_millis(50))?;
                            let mut drain = [0u8; 256];
                            let _ = self
                                .port
                                .read(&mut drain);
                            debug!("Received SEBOOT magic response");
                            return Ok(());
                        }
                    } else {
                        // Reset match, check if current byte starts a new match
                        match_idx = usize::from(buf[0] == magic[0]);
                    }
                },
                Ok(_) => {},
                Err(e) if e.kind() == std::io::ErrorKind::TimedOut => {},
                Err(e) => {
                    if e.kind() == std::io::ErrorKind::Interrupted {
                        return Err(Error::Io(e));
                    }
                    return Err(Error::Io(e));
                },
            }
        }

        Err(Error::Timeout("Timeout waiting for SEBOOT magic".into()))
    }

    /// Transfer LoaderBoot via YMODEM without sending a download command.
    ///
    /// After handshake, the device enters YMODEM mode directly for LoaderBoot.
    /// No download command (0xD2) should be sent. This matches the official
    /// fbb_burntool behavior where LOADER type partitions skip the download
    /// command and go straight to YMODEM transfer.
    fn transfer_loaderboot<F>(&mut self, name: &str, data: &[u8], progress: &mut F) -> Result<()>
    where
        F: FnMut(&str, usize, usize),
    {
        self.cancel
            .check()?;

        debug!(
            "Transferring LoaderBoot {} ({} bytes) via YMODEM",
            name,
            data.len()
        );

        let config = YmodemConfig {
            char_timeout: Duration::from_millis(1000),
            c_timeout: Duration::from_secs(30),
            max_retries: 10,
            verbose: self.verbose,
        };

        let mut ymodem = YmodemTransfer::with_config(&mut self.port, config, &self.cancel);
        ymodem.transfer(name, data, |current, total| {
            progress(name, current, total);
        })?;

        debug!("LoaderBoot transfer complete");
        Ok(())
    }

    /// Flash a FWPKG firmware package.
    ///
    /// # Arguments
    ///
    /// * `fwpkg` - The firmware package to flash
    /// * `filter` - Optional filter for partition names (None = flash all)
    /// * `progress` - Progress callback (partition_name, current_bytes,
    ///   total_bytes)
    pub fn flash_fwpkg<F>(
        &mut self,
        fwpkg: &Fwpkg,
        filter: Option<&[&str]>,
        mut progress: F,
    ) -> Result<()>
    where
        F: FnMut(&str, usize, usize),
    {
        self.cancel
            .check()?;

        // Get LoaderBoot
        let loaderboot = fwpkg
            .loaderboot()
            .ok_or_else(|| Error::InvalidFwpkg("No LoaderBoot partition found".into()))?;

        info!("Flashing LoaderBoot: {}", loaderboot.name);

        // LoaderBoot: NO download command. After handshake ACK, the device
        // enters YMODEM mode directly. This matches fbb_burntool and ws63flash.
        let lb_data = fwpkg.bin_data(loaderboot)?;
        self.transfer_loaderboot(&loaderboot.name, lb_data, &mut progress)?;

        // Wait for LoaderBoot to initialize (device sends SEBOOT magic when ready)
        self.wait_for_magic(MAGIC_TIMEOUT)?;

        // Change baud rate if in late mode
        if self.late_baud && self.target_baud != DEFAULT_BAUD {
            self.change_baud_rate(self.target_baud)?;
        }

        // Flash remaining partitions
        for bin in fwpkg.normal_bins() {
            self.cancel
                .check()?;

            // Apply filter if provided
            if let Some(names) = filter {
                if !names
                    .iter()
                    .any(|n| {
                        bin.name
                            .contains(n)
                    })
                {
                    debug!("Skipping partition: {}", bin.name);
                    continue;
                }
            }

            info!(
                "Flashing partition: {} -> 0x{:08X}",
                bin.name, bin.burn_addr
            );

            let bin_data = fwpkg.bin_data(bin)?;
            self.download_binary(&bin.name, bin_data, bin.burn_addr, &mut progress)?;

            // Inter-partition delay to prevent serial data stale
            // (MCU won't respond if next command follows immediately)
            sleep_interruptible(&self.cancel, PARTITION_DELAY)?;
        }

        info!("Flashing complete!");
        Ok(())
    }

    /// Download a single binary to flash with retry mechanism.
    #[allow(clippy::cast_possible_truncation)]
    fn download_binary<F>(
        &mut self,
        name: &str,
        data: &[u8],
        addr: u32,
        progress: &mut F,
    ) -> Result<()>
    where
        F: FnMut(&str, usize, usize),
    {
        self.cancel
            .check()?;

        let mut last_error = None;

        for attempt in 1..=MAX_DOWNLOAD_RETRIES {
            self.cancel
                .check()?;

            match self.try_download_binary(name, data, addr, progress) {
                Ok(()) => {
                    return Ok(());
                },
                Err(e) => {
                    if is_interrupted_error(&e) || crate::is_interrupted_requested() {
                        return Err(e);
                    }

                    if attempt < MAX_DOWNLOAD_RETRIES {
                        warn!(
                            "Download failed for {name} (attempt \
                             {attempt}/{MAX_DOWNLOAD_RETRIES}): {e}"
                        );
                        warn!("Retrying...");
                        last_error = Some(e);

                        // Clear buffers and wait before retry
                        let _ = self
                            .port
                            .clear_buffers();
                        sleep_interruptible(&self.cancel, CONNECT_RETRY_DELAY)?;
                    } else {
                        return Err(e);
                    }
                },
            }
        }

        // Use unwrap_or_else to ensure we never lose error information
        Err(last_error.unwrap_or_else(|| {
            Error::Protocol("Download failed after all retries (no error captured)".into())
        }))
    }

    /// Single attempt to download a binary.
    fn try_download_binary<F>(
        &mut self,
        name: &str,
        data: &[u8],
        addr: u32,
        progress: &mut F,
    ) -> Result<()>
    where
        F: FnMut(&str, usize, usize),
    {
        self.cancel
            .check()?;

        // Check for oversized data that would truncate
        let len = u32::try_from(data.len()).map_err(|_| {
            Error::Protocol(format!("Firmware too large ({} bytes > 4GB)", data.len()))
        })?;

        debug!(
            "Downloading {} ({} bytes) to 0x{:08X}",
            name,
            data.len(),
            addr
        );

        // Calculate aligned erase size (align up to 0x1000 = 4KB boundary)
        // This matches the official fbb_burntool behavior.
        let erase_size = (len + 0xFFF) & !0xFFF;

        // Send download command
        let frame = CommandFrame::download(addr, len, erase_size);
        self.send_command(&frame)?;

        // Wait for ACK frame (SEBOOT magic response) from device
        // The device responds with a SEBOOT frame after processing the download
        // command. ws63flash calls uart_read_until_magic() here.
        self.wait_for_magic(MAGIC_TIMEOUT)?;

        // Transfer using YMODEM
        // Note: ymodem.transfer() internally calls wait_for_c(), so we don't need
        // to call it here. The device sends 'C' after the ACK frame.
        let config = YmodemConfig {
            char_timeout: Duration::from_millis(1000),
            c_timeout: Duration::from_secs(30),
            max_retries: 10,
            verbose: self.verbose,
        };

        let mut ymodem = YmodemTransfer::with_config(&mut self.port, config, &self.cancel);
        ymodem.transfer(name, data, |current, total| {
            progress(name, current, total);
        })?;

        debug!("{name} transfer complete");
        Ok(())
    }

    /// Write raw binary data to flash.
    ///
    /// # Arguments
    ///
    /// * `loaderboot` - LoaderBoot binary data (required for first-stage boot)
    /// * `bins` - List of (data, address) pairs to flash
    pub fn write_bins(&mut self, loaderboot: &[u8], bins: &[(&[u8], u32)]) -> Result<()> {
        self.cancel
            .check()?;

        info!("Writing LoaderBoot ({} bytes)", loaderboot.len());

        // Transfer LoaderBoot (no download command)
        self.transfer_loaderboot("loaderboot", loaderboot, &mut |_, _, _| {})?;

        // Wait for LoaderBoot to initialize
        self.wait_for_magic(MAGIC_TIMEOUT)?;

        // Change baud rate if in late mode
        if self.late_baud && self.target_baud != DEFAULT_BAUD {
            self.change_baud_rate(self.target_baud)?;
        }

        // Download remaining binaries
        for (i, (data, addr)) in bins
            .iter()
            .enumerate()
        {
            self.cancel
                .check()?;

            let name = format!("binary_{i}");
            info!("Writing {} ({} bytes) to 0x{:08X}", name, data.len(), addr);
            self.download_binary(&name, data, *addr, &mut |_, _, _| {})?;

            // Inter-partition delay
            sleep_interruptible(&self.cancel, PARTITION_DELAY)?;
        }

        Ok(())
    }

    /// Erase entire flash.
    pub fn erase_all(&mut self) -> Result<()> {
        self.cancel
            .check()?;

        info!("Erasing entire flash...");

        let frame = CommandFrame::erase_all();
        self.send_command(&frame)?;

        // Wait for erase to complete
        sleep_interruptible(&self.cancel, Duration::from_secs(5))?;

        info!("Flash erased");
        Ok(())
    }

    /// Reset the device.
    pub fn reset(&mut self) -> Result<()> {
        self.cancel
            .check()?;

        info!("Resetting device...");

        let frame = CommandFrame::reset();
        self.send_command(&frame)?;

        Ok(())
    }
}

// Native-specific convenience functions
#[cfg(feature = "native")]
mod native_impl {
    use {
        super::{
            DEFAULT_BAUD, Duration, Error, Result, Ws63Flasher, debug, sleep_interruptible, warn,
        },
        crate::port::NativePort,
    };

    impl Ws63Flasher<NativePort> {
        /// Create a new WS63 flasher by opening a serial port.
        ///
        /// This is a convenience function for native platforms that opens
        /// the port with default settings.
        ///
        /// # Arguments
        ///
        /// * `port_name` - Serial port name (e.g., "/dev/ttyUSB0" or "COM3")
        /// * `target_baud` - Target baud rate for data transfer
        pub fn open(port_name: &str, target_baud: u32) -> Result<Self> {
            Self::open_with_retry(port_name, target_baud)
        }

        /// Open a serial port with full configuration (P0: 完整配置支持).
        ///
        /// This allows customization of all serial port parameters.
        ///
        /// # Arguments
        ///
        /// * `config` - Serial port configuration
        pub fn open_with_config(config: crate::port::SerialConfig) -> Result<Self> {
            Self::open_with_config_retry(config)
        }

        /// Open serial port with full config and retry mechanism.
        #[allow(clippy::needless_pass_by_value)]
        fn open_with_config_retry(config: crate::port::SerialConfig) -> Result<Self> {
            const MAX_OPEN_PORT_ATTEMPTS: usize = 3;
            const OPEN_RETRY_DELAY: Duration = Duration::from_millis(500);

            let mut last_error = None;

            for attempt in 1..=MAX_OPEN_PORT_ATTEMPTS {
                match NativePort::open(&config) {
                    Ok(port) => {
                        if attempt > 1 {
                            debug!("Port opened on attempt {attempt}");
                        }
                        return Ok(Self::with_cancel(
                            port,
                            config.baud_rate,
                            crate::cancel_context_from_global(),
                        ));
                    },
                    Err(e) => {
                        warn!(
                            "Failed to open port {} (attempt {}/{}): {e}",
                            config.port_name, attempt, MAX_OPEN_PORT_ATTEMPTS
                        );
                        last_error = Some(e);

                        if attempt < MAX_OPEN_PORT_ATTEMPTS {
                            sleep_interruptible(
                                &crate::cancel_context_from_global(),
                                OPEN_RETRY_DELAY,
                            )?;
                        }
                    },
                }
            }

            Err(last_error.unwrap_or_else(|| {
                Error::Config(format!(
                    "Failed to open port after {MAX_OPEN_PORT_ATTEMPTS} attempts"
                ))
            }))
        }

        /// Open serial port with retry mechanism.
        fn open_with_retry(port_name: &str, target_baud: u32) -> Result<Self> {
            const MAX_OPEN_PORT_ATTEMPTS: usize = 3;
            const OPEN_RETRY_DELAY: Duration = Duration::from_millis(500);

            let mut last_error = None;

            for attempt in 1..=MAX_OPEN_PORT_ATTEMPTS {
                let config = crate::port::SerialConfig::new(port_name, DEFAULT_BAUD);
                match NativePort::open(&config) {
                    Ok(port) => {
                        if attempt > 1 {
                            debug!("Port opened on attempt {attempt}");
                        }
                        return Ok(Self::with_cancel(
                            port,
                            target_baud,
                            crate::cancel_context_from_global(),
                        ));
                    },
                    Err(e) => {
                        warn!(
                            "Failed to open port {port_name} (attempt \
                             {attempt}/{MAX_OPEN_PORT_ATTEMPTS}): {e}"
                        );
                        last_error = Some(e);

                        if attempt < MAX_OPEN_PORT_ATTEMPTS {
                            sleep_interruptible(
                                &crate::cancel_context_from_global(),
                                OPEN_RETRY_DELAY,
                            )?;
                        }
                    },
                }
            }

            Err(last_error.unwrap_or_else(|| {
                Error::Config(format!(
                    "Failed to open port {port_name} after {MAX_OPEN_PORT_ATTEMPTS} attempts"
                ))
            }))
        }
    }
}

impl<P: Port> crate::target::Flasher for Ws63Flasher<P> {
    fn connect(&mut self) -> Result<()> {
        self.connect()
    }

    fn flash_fwpkg(
        &mut self,
        fwpkg: &Fwpkg,
        filter: Option<&[&str]>,
        progress: &mut dyn FnMut(&str, usize, usize),
    ) -> Result<()> {
        self.flash_fwpkg(fwpkg, filter, |name, current, total| {
            progress(name, current, total);
        })
    }

    fn write_bins(&mut self, loaderboot: &[u8], bins: &[(&[u8], u32)]) -> Result<()> {
        self.write_bins(loaderboot, bins)
    }

    fn erase_all(&mut self) -> Result<()> {
        self.erase_all()
    }

    fn reset(&mut self) -> Result<()> {
        self.reset()
    }

    fn connection_baud(&self) -> u32 {
        DEFAULT_BAUD
    }

    fn target_baud(&self) -> Option<u32> {
        Some(self.target_baud)
    }

    fn close(&mut self) {
        // Close the underlying port to release resources
        // This is important for proper cleanup after reset
        let _ = self
            .port
            .close();
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::port::Port,
        std::{
            io::{Read, Write},
            sync::{Arc, Mutex},
        },
    };

    /// Mock port implementation for testing without real hardware.
    ///
    /// This implementation uses an internal buffer to simulate serial port
    /// behavior, allowing unit tests to run without actual hardware.
    #[derive(Clone)]
    struct MockPort {
        name: String,
        baud_rate: u32,
        timeout: Duration,
        read_buffer: Arc<Mutex<Vec<u8>>>,
        write_buffer: Arc<Mutex<Vec<u8>>>,
        dtr: bool,
        rts: bool,
    }

    impl MockPort {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
                baud_rate: 115200,
                timeout: Duration::from_millis(1000),
                read_buffer: Arc::new(Mutex::new(Vec::new())),
                write_buffer: Arc::new(Mutex::new(Vec::new())),
                dtr: false,
                rts: false,
            }
        }

        /// Add data to the read buffer (simulates receiving data from device).
        fn add_read_data(&self, data: &[u8]) {
            let mut buf = self
                .read_buffer
                .lock()
                .unwrap();
            buf.extend_from_slice(data);
        }

        /// Get data written to the port (simulates sending data to device).
        fn get_written_data(&self) -> Vec<u8> {
            let buf = self
                .write_buffer
                .lock()
                .unwrap();
            buf.clone()
        }

        /// Clear all buffers.
        fn clear(&self) {
            let mut read_buf = self
                .read_buffer
                .lock()
                .unwrap();
            let mut write_buf = self
                .write_buffer
                .lock()
                .unwrap();
            read_buf.clear();
            write_buf.clear();
        }
    }

    impl Port for MockPort {
        fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
            self.timeout = timeout;
            Ok(())
        }

        fn timeout(&self) -> Duration {
            self.timeout
        }

        fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
            self.baud_rate = baud_rate;
            Ok(())
        }

        fn baud_rate(&self) -> u32 {
            self.baud_rate
        }

        fn clear_buffers(&mut self) -> Result<()> {
            self.clear();
            Ok(())
        }

        fn name(&self) -> &str {
            &self.name
        }

        fn set_dtr(&mut self, level: bool) -> Result<()> {
            self.dtr = level;
            Ok(())
        }

        fn set_rts(&mut self, level: bool) -> Result<()> {
            self.rts = level;
            Ok(())
        }

        fn read_cts(&mut self) -> Result<bool> {
            Ok(true) // Assume CTS is asserted
        }

        fn read_dsr(&mut self) -> Result<bool> {
            Ok(true) // Assume DSR is asserted
        }

        fn close(&mut self) -> Result<()> {
            // Clear all buffers to simulate port closure
            self.clear();
            Ok(())
        }
    }

    impl Read for MockPort {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            let mut read_buf = self
                .read_buffer
                .lock()
                .map_err(|e| std::io::Error::other(format!("mutex poisoned: {e}")))?;

            if read_buf.is_empty() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    "no data available",
                ));
            }

            let to_read = std::cmp::min(buf.len(), read_buf.len());
            buf[..to_read].copy_from_slice(&read_buf[..to_read]);
            read_buf.drain(..to_read);
            Ok(to_read)
        }
    }

    impl Write for MockPort {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            let mut write_buf = self
                .write_buffer
                .lock()
                .map_err(|e| std::io::Error::other(format!("mutex poisoned: {e}")))?;
            write_buf.extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    /// Test creating a Ws63Flasher with a mock port.
    #[test]
    fn test_flasher_new_with_mock_port() {
        let port = MockPort::new("/dev/ttyUSB0");
        let flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none());

        assert_eq!(flasher.target_baud, 921600);
        assert!(!flasher.late_baud);
        assert_eq!(flasher.verbose, 0);
    }

    /// Test builder methods on Ws63Flasher.
    #[test]
    fn test_flasher_builder_methods() {
        let port = MockPort::new("/dev/ttyUSB0");
        let flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none())
            .with_late_baud(true)
            .with_verbose(2);

        assert!(flasher.late_baud);
        assert_eq!(flasher.verbose, 2);
    }

    /// Test MockPort read/write operations.
    #[test]
    fn test_mock_port_read_write() {
        let mut port = MockPort::new("/dev/ttyUSB0");

        // Add some data to read buffer
        port.add_read_data(&[0xDE, 0xAD, 0xBE, 0xEF]);

        // Write some data
        port.write_all(b"test")
            .unwrap();
        port.flush()
            .unwrap();

        // Verify written data
        let written = port.get_written_data();
        assert_eq!(written, b"test");

        // Read data - use read_exact to handle partial reads properly
        let mut buf = [0u8; 4];
        std::io::Read::read_exact(&mut port, &mut buf).unwrap();
        assert_eq!(&buf, &[0xDE, 0xAD, 0xBE, 0xEF]);
    }

    /// Test MockPort buffer operations.
    #[test]
    fn test_mock_port_buffers() {
        let mut port = MockPort::new("/dev/ttyUSB0");

        // Clear buffers
        port.clear();
        assert!(
            port.get_written_data()
                .is_empty()
        );

        // Write and add read data
        port.write_all(b"hello")
            .unwrap();
        port.add_read_data(&[1, 2, 3]);

        // Verify
        assert_eq!(port.get_written_data(), b"hello");

        let mut buf = [0u8; 3];
        std::io::Read::read_exact(&mut port, &mut buf).unwrap();
        assert_eq!(&buf, &[1, 2, 3]);

        // Clear and verify
        port.clear();
        assert!(
            port.get_written_data()
                .is_empty()
        );
    }

    /// Test MockPort pin control.
    #[test]
    fn test_mock_port_pin_control() {
        let mut port = MockPort::new("/dev/ttyUSB0");

        assert!(!port.dtr);
        assert!(!port.rts);

        port.set_dtr(true)
            .unwrap();
        port.set_rts(true)
            .unwrap();

        assert!(port.dtr);
        assert!(port.rts);
    }

    /// Test MockPort baud rate and timeout.
    #[test]
    fn test_mock_port_baud_timeout() {
        let mut port = MockPort::new("/dev/ttyUSB0");

        assert_eq!(port.baud_rate(), 115200);
        assert_eq!(port.timeout(), Duration::from_millis(1000));

        port.set_baud_rate(921600)
            .unwrap();
        port.set_timeout(Duration::from_millis(500))
            .unwrap();

        assert_eq!(port.baud_rate(), 921600);
        assert_eq!(port.timeout(), Duration::from_millis(500));
    }

    /// Test MockPort name.
    #[test]
    fn test_mock_port_name() {
        let port = MockPort::new("/dev/ttyUSB1");
        assert_eq!(port.name(), "/dev/ttyUSB1");

        let port2 = MockPort::new("COM3");
        assert_eq!(port2.name(), "COM3");
    }

    /// Test creating flasher with mock port through
    /// ChipFamily::create_flasher_with_port.
    #[test]
    fn test_create_flasher_with_mock_port() {
        use crate::target::ChipFamily;

        let port = MockPort::new("/dev/ttyUSB0");
        let flasher = ChipFamily::Ws63.create_flasher_with_port(port, 921600, false, 0);

        assert!(flasher.is_ok());
        let flasher = flasher.unwrap();

        // Flasher should be usable (even though connect will fail without mock response
        // data)
        assert_eq!(flasher.connection_baud(), 115200); // DEFAULT_BAUD for handshake
        assert_eq!(flasher.target_baud(), Some(921600));
    }

    /// Test that Flasher trait object works correctly.
    #[test]
    fn test_flasher_trait_object() {
        use crate::target::Flasher;

        let port = MockPort::new("/dev/ttyUSB0");
        let flasher: Box<dyn Flasher> = Box::new(Ws63Flasher::with_cancel(
            port,
            921600,
            CancelContext::none(),
        ));

        assert_eq!(flasher.connection_baud(), 115200);
        assert_eq!(flasher.target_baud(), Some(921600));
    }

    /// Test multiple flasher instances with same mock port clone.
    #[test]
    fn test_multiple_flashers_same_port() {
        use crate::target::ChipFamily;

        let port = MockPort::new("/dev/ttyUSB0");
        let port_clone = port.clone();

        let flasher1 = ChipFamily::Ws63.create_flasher_with_port(port, 921600, false, 0);
        let flasher2 = ChipFamily::Ws63.create_flasher_with_port(port_clone, 115200, true, 1);

        assert!(flasher1.is_ok());
        assert!(flasher2.is_ok());

        let flasher1 = flasher1.unwrap();
        let flasher2 = flasher2.unwrap();

        assert_eq!(flasher1.target_baud(), Some(921600));
        assert_eq!(flasher2.target_baud(), Some(115200));
    }

    /// Test unsupported chip family returns error for create_flasher_with_port.
    #[test]
    fn test_create_flasher_with_port_unsupported_chip() {
        use crate::target::ChipFamily;

        let port = MockPort::new("/dev/ttyUSB0");
        let result = ChipFamily::Bs2x.create_flasher_with_port(port, 115200, false, 0);

        assert!(result.is_err());
        // Verify error is the Unsupported variant
        assert!(matches!(result, Err(crate::error::Error::Unsupported(_))));
    }

    #[test]
    fn test_is_interrupted_error_for_io_interrupted_and_message() {
        let e1 = Error::Io(std::io::Error::new(
            std::io::ErrorKind::Interrupted,
            "operation interrupted",
        ));
        assert!(is_interrupted_error(&e1));

        let e2 = Error::Io(std::io::Error::other("Interrupted system call"));
        assert!(is_interrupted_error(&e2));
    }

    #[test]
    fn test_download_binary_interrupted_short_circuits_retry() {
        crate::test_set_interrupted(true);

        let port = MockPort::new("/dev/ttyUSB0");
        let cancel = crate::cancel_context_from_global();
        let mut flasher = Ws63Flasher::with_cancel(port, 921600, cancel);
        let mut progress_calls = 0usize;

        let result = flasher.download_binary(
            "app.bin",
            &[0x01, 0x02, 0x03],
            0x0023_0000,
            &mut |_, _, _| {
                progress_calls += 1;
            },
        );

        assert!(matches!(
            result,
            Err(Error::Io(ref io)) if io.kind() == std::io::ErrorKind::Interrupted
        ));
        assert_eq!(progress_calls, 0);
        assert!(
            flasher
                .port
                .get_written_data()
                .is_empty(),
            "Interrupted download should not send frames or enter retry loop"
        );

        crate::test_set_interrupted(false);
    }

    // =====================================================================
    // Regression tests for protocol fixes (CRC fix + flash protocol fix)
    // =====================================================================

    /// Regression: erase_size must be aligned to 0x1000 (4KB) boundary.
    ///
    /// The official fbb_burntool aligns erase_size to 0x1000:
    ///   `if (eraseSize % 0x1000 != 0) eraseSize = 0x1000 * (eraseSize / 0x1000
    /// + 1)`
    ///
    /// Previously hisiflash passed `len` directly as erase_size without
    /// alignment.
    #[test]
    fn test_erase_size_alignment_4k() {
        // Already aligned values should stay the same
        assert_eq!((0x1000u32 + 0xFFF) & !0xFFF, 0x1000);
        assert_eq!((0x2000u32 + 0xFFF) & !0xFFF, 0x2000);
        assert_eq!((0x10000u32 + 0xFFF) & !0xFFF, 0x10000);

        // Non-aligned values should be rounded up to next 4KB boundary
        assert_eq!((1u32 + 0xFFF) & !0xFFF, 0x1000);
        assert_eq!((0x1001u32 + 0xFFF) & !0xFFF, 0x2000);
        assert_eq!((0x2001u32 + 0xFFF) & !0xFFF, 0x3000);
        assert_eq!((0xFFFu32 + 0xFFF) & !0xFFF, 0x1000);

        // Typical firmware sizes from ws63-liteos-app_all.fwpkg
        // root_params_sign.bin: length = 0x8F4 (2292 bytes)
        assert_eq!((0x8F4u32 + 0xFFF) & !0xFFF, 0x1000);
        // root_params_sign_b.bin: similar
        assert_eq!((0x900u32 + 0xFFF) & !0xFFF, 0x1000);
        // A larger typical partition
        assert_eq!((0x12345u32 + 0xFFF) & !0xFFF, 0x13000);
    }

    /// Regression: wait_for_magic correctly detects SEBOOT magic bytes.
    ///
    /// After LoaderBoot transfer and after each download command, the device
    /// sends a SEBOOT frame starting with 0xDEADBEEF (little-endian: EF BE AD
    /// DE). wait_for_magic must find this pattern in the byte stream.
    #[test]
    fn test_wait_for_magic_finds_magic() {
        let port = MockPort::new("/dev/ttyUSB0");

        // Simulate device response: some garbage then magic + frame data
        let mut response = vec![0x00, 0x41, 0x42]; // garbage bytes
        response.extend_from_slice(&[0xEF, 0xBE, 0xAD, 0xDE]); // magic
        response.extend_from_slice(&[0x0C, 0x00, 0xE1, 0x1E, 0x5A, 0x00, 0x00, 0x00]); // frame
        port.add_read_data(&response);

        let mut flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none());
        let result = flasher.wait_for_magic(Duration::from_millis(500));
        assert!(
            result.is_ok(),
            "wait_for_magic should succeed when magic is present"
        );
    }

    /// Regression: wait_for_magic times out when no magic present.
    #[test]
    fn test_wait_for_magic_timeout_no_magic() {
        let port = MockPort::new("/dev/ttyUSB0");
        // No data in buffer -> should timeout
        let mut flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none());
        let result = flasher.wait_for_magic(Duration::from_millis(100));
        assert!(
            result.is_err(),
            "wait_for_magic should timeout with no data"
        );
    }

    /// Regression: wait_for_magic with magic preceded by partial match.
    ///
    /// Tests the edge case where some bytes of the magic appear before the
    /// full magic sequence (e.g., 0xEF followed by garbage, then the real
    /// magic).
    #[test]
    fn test_wait_for_magic_partial_then_real() {
        let port = MockPort::new("/dev/ttyUSB0");

        // Partial magic (0xEF 0xBE) then non-magic, then real magic
        let mut response = Vec::new();
        response.extend_from_slice(&[0xEF, 0xBE, 0x00]); // partial match then break
        response.extend_from_slice(&[0xEF, 0xBE, 0xAD, 0xDE]); // real magic
        response.extend_from_slice(&[0x0C, 0x00, 0xE1, 0x1E, 0x5A, 0x00]); // frame tail
        port.add_read_data(&response);

        let mut flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none());
        let result = flasher.wait_for_magic(Duration::from_millis(500));
        assert!(
            result.is_ok(),
            "wait_for_magic should handle partial matches"
        );
    }

    /// Regression: LoaderBoot must NOT send download command (0xD2).
    ///
    /// In the official fbb_burntool, `SendBurnCmd()` skips the download payload
    /// for LOADER type: `if (GetCurrentCmdType() != BurnCtrl::LOADER)`.
    /// ws63flash also only calls ymodem_xfer() directly after handshake for
    /// LoaderBoot.
    ///
    /// Previously hisiflash called download_binary() for LoaderBoot, which sent
    /// a 0xD2 download command frame. This caused the device to misinterpret
    /// the frame as data corruption.
    #[test]
    fn test_loaderboot_no_download_command() {
        let port = MockPort::new("/dev/ttyUSB0");

        // Simulate: device sends 'C' for YMODEM, then ACKs all blocks, then magic
        let response = vec![
            b'C', // YMODEM 'C' request
            0x06, // ACK for block 0 (file info)
            0x06, // ACK for data block
            0x06, // ACK for EOT
            0x06, // ACK for finish block
        ];
        port.add_read_data(&response);

        let mut flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none());
        let result = flasher.transfer_loaderboot("test.bin", &[0xAA], &mut |_, _, _| {});

        // Transfer should succeed (or fail on mock port details, but NOT send 0xD2)
        // The key assertion: check that no download command frame was written
        let written = flasher
            .port
            .get_written_data();

        // Download command frame starts with magic + has cmd byte 0xD2
        // Scan the written data for 0xD2 command byte at the expected position
        // Frame format: [EF BE AD DE] [len_lo len_hi] [CMD] [SCMD] ...
        let has_download_cmd = written
            .windows(8)
            .any(|w| {
                w[0] == 0xEF
                    && w[1] == 0xBE
                    && w[2] == 0xAD
                    && w[3] == 0xDE
                    && w[6] == 0xD2
                    && w[7] == 0x2D
            });

        assert!(
            !has_download_cmd,
            "LoaderBoot transfer must NOT send download command (0xD2). Written data should only \
             contain YMODEM blocks, not SEBOOT command frames."
        );

        // Also verify that the YMODEM transfer actually wrote something
        assert!(
            !written.is_empty(),
            "YMODEM transfer should have written data for LoaderBoot"
        );

        // Verify the result succeeded
        assert!(
            result.is_ok(),
            "LoaderBoot transfer should succeed: {:?}",
            result.err()
        );
    }

    /// Regression: download_binary for normal partitions MUST send download
    /// command (0xD2).
    ///
    /// After LoaderBoot, all subsequent partitions require a download command
    /// with addr, len, and aligned erase_size before the YMODEM transfer.
    #[test]
    fn test_normal_partition_sends_download_command() {
        let port = MockPort::new("/dev/ttyUSB0");

        // Simulate: device sends magic ACK after download command, then 'C' for YMODEM
        let mut response = Vec::new();
        // ACK frame for download command (magic + frame data)
        response.extend_from_slice(&[0xEF, 0xBE, 0xAD, 0xDE]);
        response.extend_from_slice(&[0x0C, 0x00, 0xE1, 0x1E, 0x5A, 0x00, 0x00, 0x00]);
        // Note: wait_for_magic drains remaining bytes after the magic in one read call,
        // so YMODEM responses (C, ACKs) get consumed. This is a mock limitation.
        // We just verify the download command was sent; full flow is tested on
        // hardware.
        port.add_read_data(&response);

        let mut flasher = Ws63Flasher::with_cancel(port, 921600, CancelContext::none());
        let test_data = vec![0xBB; 100];
        // The transfer will fail because 'C' and ACKs were drained by wait_for_magic,
        // but we only care about verifying the download command was sent.
        let _result = flasher.try_download_binary(
            "test_partition.bin",
            &test_data,
            0x00800000,
            &mut |_, _, _| {},
        );

        let written = flasher
            .port
            .get_written_data();

        // Verify download command WAS sent
        let has_download_cmd = written
            .windows(8)
            .any(|w| {
                w[0] == 0xEF
                    && w[1] == 0xBE
                    && w[2] == 0xAD
                    && w[3] == 0xDE
                    && w[6] == 0xD2
                    && w[7] == 0x2D
            });

        assert!(
            has_download_cmd,
            "Normal partition download must send download command (0xD2). Written data should \
             contain a SEBOOT command frame."
        );
    }

    /// Regression: download command frame must contain properly aligned
    /// erase_size.
    ///
    /// Verifies the actual bytes written in the download command frame have
    /// the erase_size field aligned to 0x1000 (4KB).
    #[test]
    fn test_download_frame_erase_size_in_bytes() {
        // Test with a non-aligned length (100 bytes = 0x64)
        // Expected erase_size: (0x64 + 0xFFF) & !0xFFF = 0x1000
        let frame = CommandFrame::download(0x00800000, 100, (100 + 0xFFF) & !0xFFF);
        let data = frame.build();

        // Frame layout: Magic(4) + Len(2) + CMD(1) + SCMD(1) + addr(4) + len(4) +
        // erase_size(4) + const(2) + CRC(2) erase_size starts at offset 16
        let erase_size = u32::from_le_bytes([data[16], data[17], data[18], data[19]]);
        assert_eq!(
            erase_size, 0x1000,
            "erase_size for 100 bytes should be 0x1000 (4KB aligned), got 0x{erase_size:X}"
        );

        // Test with exactly 4KB
        let frame2 = CommandFrame::download(0x00800000, 0x1000, (0x1000u32 + 0xFFF) & !0xFFF);
        let data2 = frame2.build();
        let erase_size2 = u32::from_le_bytes([data2[16], data2[17], data2[18], data2[19]]);
        assert_eq!(
            erase_size2, 0x1000,
            "erase_size for exactly 4KB should remain 0x1000"
        );

        // Test with 4KB + 1
        let frame3 = CommandFrame::download(0x00800000, 0x1001, (0x1001u32 + 0xFFF) & !0xFFF);
        let data3 = frame3.build();
        let erase_size3 = u32::from_le_bytes([data3[16], data3[17], data3[18], data3[19]]);
        assert_eq!(
            erase_size3, 0x2000,
            "erase_size for 0x1001 bytes should be 0x2000 (next 4KB boundary)"
        );
    }
}