Skip to content

Commit 82e19cd

Browse files
committed
feat: tbf filtering
Signed-off-by: addrian-77 <lunguadrian30@gmail.com>
1 parent bd0ded3 commit 82e19cd

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

tockloader-lib/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ edition = "2021"
99

1010
[dependencies]
1111
tokio = { version = "1.32.0", features = ["full"] }
12-
tokio-serial = {version = "5.4.4", features = ["libudev"]}
12+
tokio-serial = { version = "5.4.4", features = ["libudev"] }
1313
probe-rs = "0.24.0"
14-
tbf-parser = { path = "../tbf-parser"}
14+
tbf-parser = { path = "../tbf-parser" }
1515
utf8-decode = "1.0.1"
1616
byteorder = "1.5.0"
1717
tar = "0.4.41"

tockloader-lib/src/board_settings.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub struct BoardSettings {
22
pub arch: Option<String>,
33
pub start_address: u64,
4+
pub ram_start_address: u64,
45
}
56

67
// TODO(george-cosma): Does a default implementation make sense for this? Is a
@@ -10,6 +11,7 @@ impl Default for BoardSettings {
1011
Self {
1112
arch: None,
1213
start_address: 0x30000,
14+
ram_start_address: 0x20000000,
1315
}
1416
}
1517
}

tockloader-lib/src/known_boards.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl KnownBoard for NucleoF4 {
2525
BoardSettings {
2626
arch: Some("cortex-m4".to_string()),
2727
start_address: 0x08040000,
28+
ram_start_address: 0x20000000,
2829
}
2930
}
3031
}
@@ -47,6 +48,7 @@ impl KnownBoard for MicrobitV2 {
4748
BoardSettings {
4849
arch: Some("cortex-m4".to_string()),
4950
start_address: 0x00040000,
51+
ram_start_address: 0x20000000,
5052
}
5153
}
5254
}

tockloader-lib/src/tabs/tab.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OXIDOS AUTOMOTIVE 2024.
44

5+
use crate::board_settings::BoardSettings;
56
use crate::errors::{TabError, TockloaderError};
67
use crate::tabs::metadata::Metadata;
78
use std::fs::File;
89
use std::io::Read;
910
use tar::Archive;
1011

11-
struct TbfFile {
12+
pub struct TbfFile {
1213
pub filename: String,
1314
pub data: Vec<u8>,
1415
}
@@ -75,6 +76,52 @@ impl Tab {
7576
}
7677
}
7778

79+
pub fn filter_tbfs(
80+
&self,
81+
settings: &BoardSettings,
82+
) -> Result<Vec<(&TbfFile, u64, u64)>, TockloaderError> {
83+
// save the file
84+
// also save flash start and ram start for comparing easily later
85+
let mut compatible_tbfs: Vec<(&TbfFile, u64, u64)> = Vec::new();
86+
for file in &self.tbf_files {
87+
let (arch, flash, ram) = Self::split_arch(file.filename.to_string());
88+
// check if we have the same arch
89+
// check if flash and ram fit
90+
if flash != 0 && ram != 0 {
91+
if arch.starts_with(settings.arch.as_ref().unwrap())
92+
&& flash >= settings.start_address
93+
&& ram >= settings.ram_start_address
94+
{
95+
compatible_tbfs.push((file, flash, ram));
96+
}
97+
} else if arch.starts_with(settings.arch.as_ref().unwrap()) {
98+
// this happens for C apps, we'll have
99+
// arch = "cortex-m4.tbf"
100+
// without any flash and ram values
101+
compatible_tbfs.push((file, flash, ram));
102+
}
103+
}
104+
Ok(compatible_tbfs)
105+
}
106+
107+
fn split_arch(filename: String) -> (String, u64, u64) {
108+
// filename is always formatted like this:
109+
// "cortex-m0.0x10020000.0x20004000.tab"
110+
// splitting by .0x will give us "arch", "flash start", "ram start.tab"
111+
// 3 items
112+
log::info!("filename {filename}");
113+
let data: Vec<&str> = filename.split(".0x").collect();
114+
if data.len() == 3 {
115+
let flashaddr: u64 = u64::from_str_radix(data[1], 16).unwrap();
116+
// split the ram address again because it also contains .tab
117+
// take the first item of the tuple
118+
let ramaddr: u64 = u64::from_str_radix(data[2].split_once(".").unwrap().0, 16).unwrap();
119+
(data[0].to_string(), flashaddr, ramaddr)
120+
} else {
121+
(data[0].to_string(), 0, 0)
122+
}
123+
}
124+
78125
pub fn extract_binary(&self, arch: &str) -> Result<Vec<u8>, TockloaderError> {
79126
for file in &self.tbf_files {
80127
if file.filename.starts_with(arch) {

0 commit comments

Comments
 (0)