|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
3 | 3 | // Copyright OXIDOS AUTOMOTIVE 2024. |
4 | 4 |
|
| 5 | +use crate::board_settings::BoardSettings; |
5 | 6 | use crate::errors::{TabError, TockloaderError}; |
6 | 7 | use crate::tabs::metadata::Metadata; |
7 | 8 | use std::fs::File; |
8 | 9 | use std::io::Read; |
9 | 10 | use tar::Archive; |
10 | 11 |
|
11 | | -struct TbfFile { |
| 12 | +pub struct TbfFile { |
12 | 13 | pub filename: String, |
13 | 14 | pub data: Vec<u8>, |
14 | 15 | } |
@@ -75,6 +76,52 @@ impl Tab { |
75 | 76 | } |
76 | 77 | } |
77 | 78 |
|
| 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 | + |
78 | 125 | pub fn extract_binary(&self, arch: &str) -> Result<Vec<u8>, TockloaderError> { |
79 | 126 | for file in &self.tbf_files { |
80 | 127 | if file.filename.starts_with(arch) { |
|
0 commit comments