Commit ·
86bbb81
1
Parent(s): 28334c2
refactor: try to support elf flash
Browse files- Cargo.lock +19 -0
- blflash/Cargo.toml +1 -0
- blflash/src/chip/bl602/mod.rs +19 -18
- blflash/src/chip/mod.rs +8 -0
- blflash/src/elf.rs +18 -0
- blflash/src/flasher.rs +7 -26
- blflash/src/main.rs +85 -32
Cargo.lock
CHANGED
|
@@ -95,6 +95,7 @@ dependencies = [
|
|
| 95 |
"indicatif",
|
| 96 |
"log",
|
| 97 |
"main_error",
|
|
|
|
| 98 |
"paw",
|
| 99 |
"serde",
|
| 100 |
"serial",
|
|
@@ -483,6 +484,15 @@ dependencies = [
|
|
| 483 |
"autocfg",
|
| 484 |
]
|
| 485 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
[[package]]
|
| 487 |
name = "number_prefix"
|
| 488 |
version = "0.3.0"
|
|
@@ -501,6 +511,15 @@ version = "0.3.0"
|
|
| 501 |
source = "registry+https://github.com/rust-lang/crates.io-index"
|
| 502 |
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
|
| 503 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
[[package]]
|
| 505 |
name = "paw"
|
| 506 |
version = "1.0.0"
|
|
|
|
| 95 |
"indicatif",
|
| 96 |
"log",
|
| 97 |
"main_error",
|
| 98 |
+
"parse_int",
|
| 99 |
"paw",
|
| 100 |
"serde",
|
| 101 |
"serial",
|
|
|
|
| 484 |
"autocfg",
|
| 485 |
]
|
| 486 |
|
| 487 |
+
[[package]]
|
| 488 |
+
name = "num-traits"
|
| 489 |
+
version = "0.2.14"
|
| 490 |
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
| 491 |
+
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
| 492 |
+
dependencies = [
|
| 493 |
+
"autocfg",
|
| 494 |
+
]
|
| 495 |
+
|
| 496 |
[[package]]
|
| 497 |
name = "number_prefix"
|
| 498 |
version = "0.3.0"
|
|
|
|
| 511 |
source = "registry+https://github.com/rust-lang/crates.io-index"
|
| 512 |
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
|
| 513 |
|
| 514 |
+
[[package]]
|
| 515 |
+
name = "parse_int"
|
| 516 |
+
version = "0.4.0"
|
| 517 |
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
| 518 |
+
checksum = "82db48cac18f0963b10ddad303fa88447b95bbe0e6dbe3385f98402b63d0cc48"
|
| 519 |
+
dependencies = [
|
| 520 |
+
"num-traits",
|
| 521 |
+
]
|
| 522 |
+
|
| 523 |
[[package]]
|
| 524 |
name = "paw"
|
| 525 |
version = "1.0.0"
|
blflash/Cargo.toml
CHANGED
|
@@ -32,3 +32,4 @@ structopt = { version = "0.3.21", features = ["paw"] }
|
|
| 32 |
paw = "1.0.0"
|
| 33 |
crc = "1.8.1"
|
| 34 |
hex = "0.4.2"
|
|
|
|
|
|
| 32 |
paw = "1.0.0"
|
| 33 |
crc = "1.8.1"
|
| 34 |
hex = "0.4.2"
|
| 35 |
+
parse_int = "0.4.0"
|
blflash/src/chip/bl602/mod.rs
CHANGED
|
@@ -21,7 +21,25 @@ impl Bl602 {
|
|
| 21 |
fn addr_is_flash(&self, addr: u32) -> bool {
|
| 22 |
addr >= ROM_START && addr < ROM_END
|
| 23 |
}
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
&self,
|
| 26 |
mut partition_cfg: PartitionCfg,
|
| 27 |
mut bootheader_cfg: BootHeaderCfg,
|
|
@@ -45,20 +63,3 @@ impl Bl602 {
|
|
| 45 |
Ok(segments)
|
| 46 |
}
|
| 47 |
}
|
| 48 |
-
|
| 49 |
-
impl Chip for Bl602 {
|
| 50 |
-
fn get_eflash_loader(&self) -> &[u8] {
|
| 51 |
-
EFLASH_LOADER
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
fn get_flash_segment<'a>(&self, code_segment: CodeSegment<'a>) -> Option<RomSegment<'a>> {
|
| 55 |
-
if self.addr_is_flash(code_segment.addr) {
|
| 56 |
-
Some(RomSegment::from_code_segment(
|
| 57 |
-
code_segment.addr - ROM_START,
|
| 58 |
-
code_segment,
|
| 59 |
-
))
|
| 60 |
-
} else {
|
| 61 |
-
None
|
| 62 |
-
}
|
| 63 |
-
}
|
| 64 |
-
}
|
|
|
|
| 21 |
fn addr_is_flash(&self, addr: u32) -> bool {
|
| 22 |
addr >= ROM_START && addr < ROM_END
|
| 23 |
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
impl Chip for Bl602 {
|
| 27 |
+
fn get_eflash_loader(&self) -> &[u8] {
|
| 28 |
+
EFLASH_LOADER
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
fn get_flash_segment<'a>(&self, code_segment: CodeSegment<'a>) -> Option<RomSegment<'a>> {
|
| 32 |
+
if self.addr_is_flash(code_segment.addr) {
|
| 33 |
+
Some(RomSegment::from_code_segment(
|
| 34 |
+
code_segment.addr - ROM_START,
|
| 35 |
+
code_segment,
|
| 36 |
+
))
|
| 37 |
+
} else {
|
| 38 |
+
None
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
fn with_boot2(
|
| 43 |
&self,
|
| 44 |
mut partition_cfg: PartitionCfg,
|
| 45 |
mut bootheader_cfg: BootHeaderCfg,
|
|
|
|
| 63 |
Ok(segments)
|
| 64 |
}
|
| 65 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blflash/src/chip/mod.rs
CHANGED
|
@@ -1,8 +1,16 @@
|
|
| 1 |
pub mod bl602;
|
| 2 |
pub use crate::elf::{CodeSegment, FirmwareImage, RomSegment};
|
|
|
|
|
|
|
| 3 |
pub use bl602::Bl602;
|
| 4 |
|
| 5 |
pub trait Chip {
|
| 6 |
fn get_eflash_loader(&self) -> &[u8];
|
| 7 |
fn get_flash_segment<'a>(&self, code_segment: CodeSegment<'a>) -> Option<RomSegment<'a>>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
}
|
|
|
|
| 1 |
pub mod bl602;
|
| 2 |
pub use crate::elf::{CodeSegment, FirmwareImage, RomSegment};
|
| 3 |
+
use crate::image::{BootHeaderCfg, PartitionCfg};
|
| 4 |
+
use crate::Error;
|
| 5 |
pub use bl602::Bl602;
|
| 6 |
|
| 7 |
pub trait Chip {
|
| 8 |
fn get_eflash_loader(&self) -> &[u8];
|
| 9 |
fn get_flash_segment<'a>(&self, code_segment: CodeSegment<'a>) -> Option<RomSegment<'a>>;
|
| 10 |
+
fn with_boot2(
|
| 11 |
+
&self,
|
| 12 |
+
partition_cfg: PartitionCfg,
|
| 13 |
+
bootheader_cfg: BootHeaderCfg,
|
| 14 |
+
bin: &[u8],
|
| 15 |
+
) -> Result<Vec<RomSegment>, Error>;
|
| 16 |
}
|
blflash/src/elf.rs
CHANGED
|
@@ -4,6 +4,8 @@ use std::cmp::Ordering;
|
|
| 4 |
use xmas_elf::program::{SegmentData, Type};
|
| 5 |
use xmas_elf::ElfFile;
|
| 6 |
|
|
|
|
|
|
|
| 7 |
pub struct FirmwareImage<'a> {
|
| 8 |
pub entry: u32,
|
| 9 |
pub elf: ElfFile<'a>,
|
|
@@ -41,6 +43,22 @@ impl<'a> FirmwareImage<'a> {
|
|
| 41 |
Some(CodeSegment { addr, data, size })
|
| 42 |
})
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
#[derive(Debug, Ord, Eq)]
|
|
|
|
| 4 |
use xmas_elf::program::{SegmentData, Type};
|
| 5 |
use xmas_elf::ElfFile;
|
| 6 |
|
| 7 |
+
use crate::chip::Chip;
|
| 8 |
+
|
| 9 |
pub struct FirmwareImage<'a> {
|
| 10 |
pub entry: u32,
|
| 11 |
pub elf: ElfFile<'a>,
|
|
|
|
| 43 |
Some(CodeSegment { addr, data, size })
|
| 44 |
})
|
| 45 |
}
|
| 46 |
+
pub fn to_flash_bin(&self, chip: &dyn Chip) -> Vec<u8> {
|
| 47 |
+
let segs = self
|
| 48 |
+
.segments()
|
| 49 |
+
.filter_map(|segment| chip.get_flash_segment(segment))
|
| 50 |
+
.collect::<Vec<_>>();
|
| 51 |
+
let size = segs
|
| 52 |
+
.iter()
|
| 53 |
+
.fold(0, |len, i| len.max(i.addr + i.data.len() as u32));
|
| 54 |
+
|
| 55 |
+
let mut bin = Vec::new();
|
| 56 |
+
bin.resize(size as usize, 0xFF);
|
| 57 |
+
for s in segs {
|
| 58 |
+
bin[s.addr as usize..s.addr as usize + s.data.len()].copy_from_slice(&s.data);
|
| 59 |
+
}
|
| 60 |
+
bin
|
| 61 |
+
}
|
| 62 |
}
|
| 63 |
|
| 64 |
#[derive(Debug, Ord, Eq)]
|
blflash/src/flasher.rs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
use crate::chip::Chip;
|
| 2 |
-
use crate::elf::FirmwareImage;
|
| 3 |
use crate::Error;
|
| 4 |
use crate::{connection::Connection, elf::RomSegment};
|
| 5 |
use deku::prelude::*;
|
|
@@ -143,28 +142,6 @@ impl Flasher {
|
|
| 143 |
Ok(())
|
| 144 |
}
|
| 145 |
|
| 146 |
-
pub fn load_elf_to_flash(&mut self, force: bool, elf_data: &[u8]) -> Result<(), Error> {
|
| 147 |
-
let image = FirmwareImage::from_data(elf_data).map_err(|_| Error::InvalidElf)?;
|
| 148 |
-
let segs = image
|
| 149 |
-
.segments()
|
| 150 |
-
.filter_map(|segment| self.chip.get_flash_segment(segment))
|
| 151 |
-
.collect::<Vec<_>>();
|
| 152 |
-
|
| 153 |
-
self.load_segments(force, segs.into_iter())?;
|
| 154 |
-
Ok(())
|
| 155 |
-
}
|
| 156 |
-
|
| 157 |
-
pub fn check_elf_to_flash(&mut self, elf_data: &[u8]) -> Result<(), Error> {
|
| 158 |
-
let image = FirmwareImage::from_data(elf_data).map_err(|_| Error::InvalidElf)?;
|
| 159 |
-
let segs = image
|
| 160 |
-
.segments()
|
| 161 |
-
.filter_map(|segment| self.chip.get_flash_segment(segment))
|
| 162 |
-
.collect::<Vec<_>>();
|
| 163 |
-
|
| 164 |
-
self.check_segments(segs.into_iter())?;
|
| 165 |
-
Ok(())
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
pub fn dump_flash(&mut self, range: Range<u32>, mut writer: impl Write) -> Result<(), Error> {
|
| 169 |
self.load_eflash_loader()?;
|
| 170 |
|
|
@@ -209,11 +186,12 @@ impl Flasher {
|
|
| 209 |
|
| 210 |
self.check_image()?;
|
| 211 |
self.run_image()?;
|
| 212 |
-
sleep(Duration::from_millis(
|
| 213 |
-
// TODO configurable
|
| 214 |
self.connection.set_baud(self.flash_speed)?;
|
| 215 |
self.handshake()?;
|
| 216 |
|
|
|
|
|
|
|
| 217 |
Ok(())
|
| 218 |
}
|
| 219 |
|
|
@@ -362,11 +340,14 @@ impl Flasher {
|
|
| 362 |
connection.write_all(&data)?;
|
| 363 |
connection.flush()?;
|
| 364 |
log::trace!("handshake sent elapsed {:?}", start.elapsed());
|
| 365 |
-
|
|
|
|
|
|
|
| 366 |
if connection.read_response(0).is_ok() {
|
| 367 |
return Ok(());
|
| 368 |
}
|
| 369 |
}
|
|
|
|
| 370 |
Err(Error::Timeout)
|
| 371 |
})
|
| 372 |
}
|
|
|
|
| 1 |
use crate::chip::Chip;
|
|
|
|
| 2 |
use crate::Error;
|
| 3 |
use crate::{connection::Connection, elf::RomSegment};
|
| 4 |
use deku::prelude::*;
|
|
|
|
| 142 |
Ok(())
|
| 143 |
}
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
pub fn dump_flash(&mut self, range: Range<u32>, mut writer: impl Write) -> Result<(), Error> {
|
| 146 |
self.load_eflash_loader()?;
|
| 147 |
|
|
|
|
| 186 |
|
| 187 |
self.check_image()?;
|
| 188 |
self.run_image()?;
|
| 189 |
+
sleep(Duration::from_millis(500));
|
|
|
|
| 190 |
self.connection.set_baud(self.flash_speed)?;
|
| 191 |
self.handshake()?;
|
| 192 |
|
| 193 |
+
log::info!("Entered eflash_loader");
|
| 194 |
+
|
| 195 |
Ok(())
|
| 196 |
}
|
| 197 |
|
|
|
|
| 340 |
connection.write_all(&data)?;
|
| 341 |
connection.flush()?;
|
| 342 |
log::trace!("handshake sent elapsed {:?}", start.elapsed());
|
| 343 |
+
sleep(Duration::from_millis(200));
|
| 344 |
+
|
| 345 |
+
for _ in 0..5 {
|
| 346 |
if connection.read_response(0).is_ok() {
|
| 347 |
return Ok(());
|
| 348 |
}
|
| 349 |
}
|
| 350 |
+
|
| 351 |
Err(Error::Timeout)
|
| 352 |
})
|
| 353 |
}
|
blflash/src/main.rs
CHANGED
|
@@ -3,14 +3,18 @@ use blflash::{
|
|
| 3 |
bl602::{self, Bl602},
|
| 4 |
Chip,
|
| 5 |
},
|
|
|
|
| 6 |
image::BootHeaderCfgFile,
|
| 7 |
Config, Error, Flasher,
|
| 8 |
};
|
| 9 |
use env_logger::Env;
|
| 10 |
use main_error::MainError;
|
| 11 |
use serial::{BaudRate, SerialPort};
|
| 12 |
-
use std::fs::{read, File};
|
| 13 |
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
use structopt::StructOpt;
|
| 15 |
|
| 16 |
#[derive(StructOpt)]
|
|
@@ -19,7 +23,7 @@ struct Connection {
|
|
| 19 |
#[structopt(short, long)]
|
| 20 |
port: String,
|
| 21 |
/// Flash baud rate
|
| 22 |
-
#[structopt(short, long, default_value = "
|
| 23 |
baud_rate: usize,
|
| 24 |
/// Initial baud rate
|
| 25 |
#[structopt(long, default_value = "115200")]
|
|
@@ -27,12 +31,7 @@ struct Connection {
|
|
| 27 |
}
|
| 28 |
|
| 29 |
#[derive(StructOpt)]
|
| 30 |
-
struct
|
| 31 |
-
#[structopt(flatten)]
|
| 32 |
-
conn: Connection,
|
| 33 |
-
/// Bin file
|
| 34 |
-
#[structopt(parse(from_os_str))]
|
| 35 |
-
image: PathBuf,
|
| 36 |
/// Path to partition_cfg.toml, default to be partition/partition_cfg_2M.toml
|
| 37 |
#[structopt(parse(from_os_str))]
|
| 38 |
partition_cfg: Option<PathBuf>,
|
|
@@ -42,9 +41,20 @@ struct FlashOpt {
|
|
| 42 |
/// With boot2
|
| 43 |
#[structopt(short, long)]
|
| 44 |
without_boot2: bool,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
/// Don't skip if hash matches
|
| 46 |
#[structopt(short, long)]
|
| 47 |
force: bool,
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
#[derive(StructOpt)]
|
|
@@ -54,6 +64,8 @@ struct CheckOpt {
|
|
| 54 |
/// Bin file
|
| 55 |
#[structopt(parse(from_os_str))]
|
| 56 |
image: PathBuf,
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
#[derive(StructOpt)]
|
|
@@ -64,10 +76,10 @@ struct DumpOpt {
|
|
| 64 |
#[structopt(parse(from_os_str))]
|
| 65 |
output: PathBuf,
|
| 66 |
/// start address
|
| 67 |
-
#[structopt(default_value = "0")]
|
| 68 |
start: u32,
|
| 69 |
/// end address
|
| 70 |
-
#[structopt(default_value = "
|
| 71 |
end: u32,
|
| 72 |
}
|
| 73 |
|
|
@@ -97,40 +109,79 @@ impl Connection {
|
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
.partition_cfg
|
| 107 |
.map(read)
|
| 108 |
.unwrap_or_else(|| Ok(bl602::DEFAULT_PARTITION_CFG.to_vec()))?;
|
| 109 |
-
let boot_header_cfg =
|
| 110 |
.boot_header_cfg
|
| 111 |
.map(read)
|
| 112 |
.unwrap_or_else(|| Ok(bl602::DEFAULT_BOOTHEADER_CFG.to_vec()))?;
|
| 113 |
let partition_cfg = toml::from_slice(&partition_cfg)?;
|
| 114 |
let BootHeaderCfgFile { boot_header_cfg } = toml::from_slice(&boot_header_cfg)?;
|
| 115 |
|
| 116 |
-
let
|
| 117 |
-
let segments = chip.with_boot2(partition_cfg, boot_header_cfg, &bin)?;
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
} else {
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
log::info!("Success");
|
| 136 |
|
|
@@ -138,13 +189,15 @@ fn flash(opt: FlashOpt) -> Result<(), Error> {
|
|
| 138 |
}
|
| 139 |
|
| 140 |
fn check(opt: CheckOpt) -> Result<(), Error> {
|
|
|
|
| 141 |
let mut flasher = opt.conn.create_flasher(Bl602)?;
|
| 142 |
-
|
| 143 |
log::info!("Bootrom version: {}", flasher.boot_info().bootrom_version);
|
| 144 |
log::trace!("Boot info: {:x?}", flasher.boot_info());
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
let
|
| 147 |
-
flasher.
|
| 148 |
|
| 149 |
Ok(())
|
| 150 |
}
|
|
|
|
| 3 |
bl602::{self, Bl602},
|
| 4 |
Chip,
|
| 5 |
},
|
| 6 |
+
elf::{FirmwareImage, RomSegment},
|
| 7 |
image::BootHeaderCfgFile,
|
| 8 |
Config, Error, Flasher,
|
| 9 |
};
|
| 10 |
use env_logger::Env;
|
| 11 |
use main_error::MainError;
|
| 12 |
use serial::{BaudRate, SerialPort};
|
|
|
|
| 13 |
use std::path::PathBuf;
|
| 14 |
+
use std::{
|
| 15 |
+
borrow::Cow,
|
| 16 |
+
fs::{read, File},
|
| 17 |
+
};
|
| 18 |
use structopt::StructOpt;
|
| 19 |
|
| 20 |
#[derive(StructOpt)]
|
|
|
|
| 23 |
#[structopt(short, long)]
|
| 24 |
port: String,
|
| 25 |
/// Flash baud rate
|
| 26 |
+
#[structopt(short, long, default_value = "1000000")]
|
| 27 |
baud_rate: usize,
|
| 28 |
/// Initial baud rate
|
| 29 |
#[structopt(long, default_value = "115200")]
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
#[derive(StructOpt)]
|
| 34 |
+
struct Boot2Opt {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
/// Path to partition_cfg.toml, default to be partition/partition_cfg_2M.toml
|
| 36 |
#[structopt(parse(from_os_str))]
|
| 37 |
partition_cfg: Option<PathBuf>,
|
|
|
|
| 41 |
/// With boot2
|
| 42 |
#[structopt(short, long)]
|
| 43 |
without_boot2: bool,
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
#[derive(StructOpt)]
|
| 47 |
+
struct FlashOpt {
|
| 48 |
+
#[structopt(flatten)]
|
| 49 |
+
conn: Connection,
|
| 50 |
+
/// Bin file
|
| 51 |
+
#[structopt(parse(from_os_str))]
|
| 52 |
+
image: PathBuf,
|
| 53 |
/// Don't skip if hash matches
|
| 54 |
#[structopt(short, long)]
|
| 55 |
force: bool,
|
| 56 |
+
#[structopt(flatten)]
|
| 57 |
+
boot: Boot2Opt,
|
| 58 |
}
|
| 59 |
|
| 60 |
#[derive(StructOpt)]
|
|
|
|
| 64 |
/// Bin file
|
| 65 |
#[structopt(parse(from_os_str))]
|
| 66 |
image: PathBuf,
|
| 67 |
+
#[structopt(flatten)]
|
| 68 |
+
boot: Boot2Opt,
|
| 69 |
}
|
| 70 |
|
| 71 |
#[derive(StructOpt)]
|
|
|
|
| 76 |
#[structopt(parse(from_os_str))]
|
| 77 |
output: PathBuf,
|
| 78 |
/// start address
|
| 79 |
+
#[structopt(parse(try_from_str = parse_int::parse), default_value = "0")]
|
| 80 |
start: u32,
|
| 81 |
/// end address
|
| 82 |
+
#[structopt(parse(try_from_str = parse_int::parse), default_value = "0x100000")]
|
| 83 |
end: u32,
|
| 84 |
}
|
| 85 |
|
|
|
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
+
impl Boot2Opt {
|
| 113 |
+
fn with_boot2<'a>(
|
| 114 |
+
self,
|
| 115 |
+
chip: &'a dyn Chip,
|
| 116 |
+
image: &[u8],
|
| 117 |
+
) -> Result<Vec<RomSegment<'a>>, Error> {
|
| 118 |
+
let partition_cfg = self
|
| 119 |
.partition_cfg
|
| 120 |
.map(read)
|
| 121 |
.unwrap_or_else(|| Ok(bl602::DEFAULT_PARTITION_CFG.to_vec()))?;
|
| 122 |
+
let boot_header_cfg = self
|
| 123 |
.boot_header_cfg
|
| 124 |
.map(read)
|
| 125 |
.unwrap_or_else(|| Ok(bl602::DEFAULT_BOOTHEADER_CFG.to_vec()))?;
|
| 126 |
let partition_cfg = toml::from_slice(&partition_cfg)?;
|
| 127 |
let BootHeaderCfgFile { boot_header_cfg } = toml::from_slice(&boot_header_cfg)?;
|
| 128 |
|
| 129 |
+
let segments = chip.with_boot2(partition_cfg, boot_header_cfg, image)?;
|
|
|
|
| 130 |
|
| 131 |
+
Ok(segments)
|
| 132 |
+
}
|
| 133 |
+
fn make_segment<'a>(
|
| 134 |
+
self,
|
| 135 |
+
_chip: &'a dyn Chip,
|
| 136 |
+
image: Vec<u8>,
|
| 137 |
+
) -> Result<RomSegment<'a>, Error> {
|
| 138 |
+
let boot_header_cfg = self
|
| 139 |
+
.boot_header_cfg
|
| 140 |
+
.map(read)
|
| 141 |
+
.unwrap_or_else(|| Ok(bl602::DEFAULT_BOOTHEADER_CFG.to_vec()))?;
|
| 142 |
+
let BootHeaderCfgFile {
|
| 143 |
+
mut boot_header_cfg,
|
| 144 |
+
} = toml::from_slice(&boot_header_cfg)?;
|
| 145 |
+
let img = boot_header_cfg.make_image(0x2000, image)?;
|
| 146 |
|
| 147 |
+
Ok(RomSegment::from_vec(0x0, img))
|
| 148 |
+
}
|
| 149 |
+
fn get_segments<'a>(
|
| 150 |
+
self,
|
| 151 |
+
chip: &'a dyn Chip,
|
| 152 |
+
image: Vec<u8>,
|
| 153 |
+
) -> Result<Vec<RomSegment<'a>>, Error> {
|
| 154 |
+
Ok(if self.without_boot2 {
|
| 155 |
+
vec![self.make_segment(chip, Vec::from(image))?]
|
| 156 |
+
} else {
|
| 157 |
+
self.with_boot2(chip, &image)?
|
| 158 |
+
})
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
|
| 162 |
+
fn read_image<'a>(chip: &dyn Chip, image: &'a [u8]) -> Result<Cow<'a, [u8]>, Error> {
|
| 163 |
+
Ok(if image[0..4] == [0x7f, 0x45, 0x4c, 0x46] {
|
| 164 |
+
log::trace!("Detect ELF");
|
| 165 |
+
// ELF
|
| 166 |
+
let firmware_image = FirmwareImage::from_data(image).map_err(|_| Error::InvalidElf)?;
|
| 167 |
+
Cow::Owned(firmware_image.to_flash_bin(chip))
|
| 168 |
} else {
|
| 169 |
+
// bin
|
| 170 |
+
Cow::Borrowed(image)
|
| 171 |
+
})
|
| 172 |
+
}
|
| 173 |
|
| 174 |
+
fn flash(opt: FlashOpt) -> Result<(), Error> {
|
| 175 |
+
let chip = Bl602;
|
| 176 |
+
let mut flasher = opt.conn.create_flasher(chip)?;
|
| 177 |
+
log::info!("Bootrom version: {}", flasher.boot_info().bootrom_version);
|
| 178 |
+
log::trace!("Boot info: {:x?}", flasher.boot_info());
|
| 179 |
+
let image = read(&opt.image)?;
|
| 180 |
+
let image = read_image(&chip, &image)?;
|
| 181 |
|
| 182 |
+
let segments = opt.boot.get_segments(&chip, Vec::from(image))?;
|
| 183 |
+
flasher.load_segments(opt.force, segments.into_iter())?;
|
| 184 |
+
flasher.reset()?;
|
| 185 |
|
| 186 |
log::info!("Success");
|
| 187 |
|
|
|
|
| 189 |
}
|
| 190 |
|
| 191 |
fn check(opt: CheckOpt) -> Result<(), Error> {
|
| 192 |
+
let chip = Bl602;
|
| 193 |
let mut flasher = opt.conn.create_flasher(Bl602)?;
|
|
|
|
| 194 |
log::info!("Bootrom version: {}", flasher.boot_info().bootrom_version);
|
| 195 |
log::trace!("Boot info: {:x?}", flasher.boot_info());
|
| 196 |
+
let image = read(&opt.image)?;
|
| 197 |
+
let image = read_image(&chip, &image)?;
|
| 198 |
|
| 199 |
+
let segments = opt.boot.get_segments(&chip, Vec::from(image))?;
|
| 200 |
+
flasher.check_segments(segments.into_iter())?;
|
| 201 |
|
| 202 |
Ok(())
|
| 203 |
}
|