Skip to content

Commit 2e96298

Browse files
committed
feat: tbf filtering
Signed-off-by: Adrian Lungu <lunguadrian30@gmail.com>
1 parent 94e3382 commit 2e96298

1 file changed

Lines changed: 59 additions & 3 deletions

File tree

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)