Skip to content

Commit 7f43c01

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

5 files changed

Lines changed: 66 additions & 6 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/command_impl/probers/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl CommandInstall for ProbeRSConnection {
5555
"architechture".to_owned(),
5656
))?;
5757

58-
let mut binary = tab_file.extract_binary(arch)?;
58+
let mut binary = tab_file.extract_binary(arch.to_string())?;
5959
let size = binary.len() as u64;
6060

6161
// Make sure the app is aligned to a multiple of its size

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: 59 additions & 3 deletions
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,9 +76,64 @@ impl Tab {
7576
}
7677
}
7778

78-
pub fn extract_binary(&self, arch: &str) -> Result<Vec<u8>, TockloaderError> {
79+
/// This function returns all the compatible binaries for a given tab
80+
///
81+
/// Vector components:
82+
/// - binary (Vec<u8>)
83+
/// - start_address: u64
84+
/// - ram_start_address: u64
85+
pub fn filter_tbfs(
86+
&self,
87+
settings: &BoardSettings,
88+
) -> Result<Vec<Option<(u64, u64)>>, TockloaderError> {
89+
// save the file
90+
// also save flash start and ram start for comparing easily later
91+
let mut compatible_tbfs: Vec<Option<(u64, u64)>> = Vec::new();
7992
for file in &self.tbf_files {
80-
if file.filename.starts_with(arch) {
93+
let (arch, flash, ram) = Self::split_arch(file.filename.to_string());
94+
// check if we have the same arch
95+
// check if flash and ram fit
96+
if flash != 0
97+
&& ram != 0
98+
&& arch.starts_with(settings.arch.as_ref().unwrap())
99+
&& flash >= settings.start_address
100+
{
101+
log::info!("rust, pushed arch {arch}, flash {flash:#x}, ram {ram:#x}");
102+
compatible_tbfs.push(Some((flash, ram)));
103+
}
104+
105+
// how about we don't do anything on else?
106+
// } else if arch.starts_with(settings.arch.as_ref().unwrap()) {
107+
// // this happens for C apps, we'll have
108+
// // arch = "cortex-m4.tbf"
109+
// // without any flash and ram values
110+
// compatible_tbfs.push(None);
111+
// }
112+
}
113+
Ok(compatible_tbfs)
114+
}
115+
116+
fn split_arch(filename: String) -> (String, u64, u64) {
117+
// filename is always formatted like this:
118+
// "cortex-m0.0x10020000.0x20004000.tab"
119+
// splitting by .0x will give us "arch", "flash start", "ram start.tab"
120+
// 3 items
121+
// log::info!("filename {filename}");
122+
let data: Vec<&str> = filename.split(".0x").collect();
123+
if data.len() == 3 {
124+
let flashaddr: u64 = u64::from_str_radix(data[1], 16).unwrap();
125+
// split the ram address again because it also contains .tab
126+
// take the first item of the tuple
127+
let ramaddr: u64 = u64::from_str_radix(data[2].split_once(".").unwrap().0, 16).unwrap();
128+
(data[0].to_string(), flashaddr, ramaddr)
129+
} else {
130+
(data[0].to_string(), 0, 0)
131+
}
132+
}
133+
134+
pub fn extract_binary(&self, arch: String) -> Result<Vec<u8>, TockloaderError> {
135+
for file in &self.tbf_files {
136+
if file.filename.starts_with(&arch) {
81137
return Ok(file.data.clone());
82138
}
83139
}

0 commit comments

Comments
 (0)