|
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,9 +76,64 @@ impl Tab { |
75 | 76 | } |
76 | 77 | } |
77 | 78 |
|
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(); |
79 | 92 | 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) { |
81 | 137 | return Ok(file.data.clone()); |
82 | 138 | } |
83 | 139 | } |
|
0 commit comments