|
| 1 | +use crate::connection::Connection; |
| 2 | +use crate::elf::{FirmwareImage, RomSegment}; |
| 3 | +use crate::error::Error; |
| 4 | +use crate::flash_target::{begin_command, block_command_with_timeout, FlashTarget}; |
| 5 | +use crate::flasher::{Command, SpiAttachParams, FLASH_SECTOR_SIZE, FLASH_WRITE_SIZE}; |
| 6 | +use crate::Chip; |
| 7 | +use flate2::write::{ZlibDecoder, ZlibEncoder}; |
| 8 | +use flate2::Compression; |
| 9 | +use indicatif::{ProgressBar, ProgressStyle}; |
| 10 | +use std::io::Write; |
| 11 | + |
| 12 | +pub struct Esp32Target { |
| 13 | + chip: Chip, |
| 14 | + spi_attach_params: SpiAttachParams, |
| 15 | +} |
| 16 | + |
| 17 | +impl Esp32Target { |
| 18 | + pub fn new(chip: Chip, spi_attach_params: SpiAttachParams) -> Self { |
| 19 | + Esp32Target { |
| 20 | + chip, |
| 21 | + spi_attach_params, |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl FlashTarget for Esp32Target { |
| 27 | + fn begin(&mut self, connection: &mut Connection, _image: &FirmwareImage) -> Result<(), Error> { |
| 28 | + let spi_params = self.spi_attach_params.encode(); |
| 29 | + connection.with_timeout(Command::SpiAttach.timeout(), |connection| { |
| 30 | + connection.command(Command::SpiAttach as u8, spi_params.as_slice(), 0) |
| 31 | + })?; |
| 32 | + Ok(()) |
| 33 | + } |
| 34 | + |
| 35 | + fn write_segment( |
| 36 | + &mut self, |
| 37 | + connection: &mut Connection, |
| 38 | + segment: RomSegment, |
| 39 | + ) -> Result<(), Error> { |
| 40 | + let addr = segment.addr; |
| 41 | + let mut encoder = ZlibEncoder::new(Vec::new(), Compression::best()); |
| 42 | + encoder.write_all(&segment.data)?; |
| 43 | + let compressed = encoder.finish()?; |
| 44 | + let block_count = (compressed.len() + FLASH_WRITE_SIZE - 1) / FLASH_WRITE_SIZE; |
| 45 | + let erase_count = (segment.data.len() + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE; |
| 46 | + |
| 47 | + // round up to sector size |
| 48 | + let erase_size = (erase_count * FLASH_SECTOR_SIZE) as u32; |
| 49 | + |
| 50 | + begin_command( |
| 51 | + connection, |
| 52 | + Command::FlashDeflateBegin, |
| 53 | + erase_size, |
| 54 | + block_count as u32, |
| 55 | + FLASH_WRITE_SIZE as u32, |
| 56 | + addr, |
| 57 | + self.chip != Chip::Esp32, |
| 58 | + )?; |
| 59 | + |
| 60 | + let chunks = compressed.chunks(FLASH_WRITE_SIZE); |
| 61 | + |
| 62 | + let (_, chunk_size) = chunks.size_hint(); |
| 63 | + let chunk_size = chunk_size.unwrap_or(0) as u64; |
| 64 | + let pb_chunk = ProgressBar::new(chunk_size); |
| 65 | + pb_chunk.set_style( |
| 66 | + ProgressStyle::default_bar() |
| 67 | + .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}") |
| 68 | + .progress_chars("#>-"), |
| 69 | + ); |
| 70 | + |
| 71 | + // decode the chunks to see how much data the device will have to save |
| 72 | + let mut decoder = ZlibDecoder::new(Vec::new()); |
| 73 | + let mut decoded_size = 0; |
| 74 | + |
| 75 | + for (i, block) in chunks.enumerate() { |
| 76 | + decoder.write_all(block)?; |
| 77 | + decoder.flush()?; |
| 78 | + let size = decoder.get_ref().len() - decoded_size; |
| 79 | + decoded_size = decoder.get_ref().len(); |
| 80 | + |
| 81 | + pb_chunk.set_message(format!("segment 0x{:X} writing chunks", addr)); |
| 82 | + block_command_with_timeout( |
| 83 | + connection, |
| 84 | + Command::FlashDeflateData, |
| 85 | + block, |
| 86 | + 0, |
| 87 | + 0xff, |
| 88 | + i as u32, |
| 89 | + Command::FlashDeflateData.timeout_for_size(size as u32), |
| 90 | + )?; |
| 91 | + pb_chunk.inc(1); |
| 92 | + } |
| 93 | + |
| 94 | + pb_chunk.finish_with_message(format!("segment 0x{:X}", addr)); |
| 95 | + |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | + |
| 99 | + fn finish(&mut self, connection: &mut Connection, reboot: bool) -> Result<(), Error> { |
| 100 | + connection.with_timeout(Command::FlashDeflateEnd.timeout(), |connection| { |
| 101 | + connection.write_command(Command::FlashDeflateEnd as u8, &[1][..], 0) |
| 102 | + })?; |
| 103 | + if reboot { |
| 104 | + connection.reset() |
| 105 | + } else { |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments