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
use crate::connection::Connection;
use crate::elf::{FirmwareImage, RomSegment};
use crate::error::Error;
use crate::flash_target::{begin_command, block_command, FlashTarget};
use crate::flasher::{get_erase_size, Command, FLASH_WRITE_SIZE};
use indicatif::{ProgressBar, ProgressStyle};
pub struct Esp8266Target;
impl Esp8266Target {
pub fn new() -> Self {
Esp8266Target
}
}
impl FlashTarget for Esp8266Target {
fn begin(&mut self, connection: &mut Connection, _image: &FirmwareImage) -> Result<(), Error> {
begin_command(
connection,
Command::FlashBegin,
0,
0,
FLASH_WRITE_SIZE as u32,
0,
false,
)
}
fn write_segment(
&mut self,
connection: &mut Connection,
segment: RomSegment,
) -> Result<(), Error> {
let addr = segment.addr;
let block_count = (segment.data.len() + FLASH_WRITE_SIZE - 1) / FLASH_WRITE_SIZE;
let erase_size = get_erase_size(addr as usize, segment.data.len()) as u32;
begin_command(
connection,
Command::FlashBegin,
erase_size,
block_count as u32,
FLASH_WRITE_SIZE as u32,
addr,
false,
)?;
let chunks = segment.data.chunks(FLASH_WRITE_SIZE);
let (_, chunk_size) = chunks.size_hint();
let chunk_size = chunk_size.unwrap_or(0) as u64;
let pb_chunk = ProgressBar::new(chunk_size);
pb_chunk.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
.progress_chars("#>-"),
);
for (i, block) in chunks.enumerate() {
pb_chunk.set_message(format!("segment 0x{:X} writing chunks", addr));
let block_padding = FLASH_WRITE_SIZE - block.len();
block_command(
connection,
Command::FlashData,
block,
block_padding,
0xff,
i as u32,
)?;
pb_chunk.inc(1);
}
pb_chunk.finish_with_message(format!("segment 0x{:X}", addr));
Ok(())
}
fn finish(&mut self, connection: &mut Connection, reboot: bool) -> Result<(), Error> {
connection.with_timeout(Command::FlashEnd.timeout(), |connection| {
connection.write_command(Command::FlashEnd as u8, &[1][..], 0)
})?;
if reboot {
connection.reset()
} else {
Ok(())
}
}
}