Skip to content

Commit 4477a41

Browse files
George Cosmaeva-cosma
authored andcommitted
fix: TBF parsing + logging
Signed-off-by: George Cosma <[email protected]>
1 parent a3ae613 commit 4477a41

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

tbf-parser/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
log = "0.4.27"
910

1011
[features]
1112
default = []
12-
std = []
13+
std = []

tbf-parser/src/parse.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ pub fn parse_tbf_header(
153153
.get(0..4)
154154
.ok_or(types::TbfParseError::NotEnoughFlash)?
155155
.try_into()?;
156+
157+
log::debug!(
158+
"TLV Entry: type {:?}, length {}",
159+
tlv_header.tipe,
160+
tlv_header.length
161+
);
162+
156163
remaining = remaining
157164
.get(4..)
158165
.ok_or(types::TbfParseError::NotEnoughFlash)?;

tockloader-cli/src/cli.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ fn get_subcommands() -> Vec<Command> {
3131
Command::new("listen")
3232
.about("Open a terminal to receive UART data")
3333
.args(get_channel_args())
34+
.args([
35+
// TODO(george-cosma): Change default to pconsole when new format is implemented and adopted into tock
36+
arg!(--protocol <PROTOCOL> "Choose between legacy and pconsole protocol")
37+
.default_value("legacy")
38+
.value_parser(["legacy", "pconsole"]),
39+
])
3440
.arg_required_else_help(false),
3541
Command::new("list")
3642
.about("List and inspect probes")
@@ -75,10 +81,6 @@ fn get_channel_args() -> Vec<clap::Arg> {
7581
.collect::<Vec<_>>();
7682

7783
vec![
78-
// TODO(george-cosma): Change default to pconsole when new format is implemented and adopted into tock
79-
arg!(--protocol <PROTOCOL> "Choose between legacy and pconsole protocol")
80-
.default_value("legacy")
81-
.value_parser(["legacy", "pconsole"]),
8284
arg!(--serial "Use the serial bootloader to flash")
8385
.action(clap::ArgAction::SetTrue)
8486
.conflicts_with_all(probe_args_ids.clone().collect::<Vec<_>>()),

tockloader-cli/src/main.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ fn get_board_settings(user_options: &ArgMatches) -> BoardSettings {
7272
}
7373

7474
fn using_serial(user_options: &ArgMatches) -> bool {
75-
// TODO: refactor this in the future
76-
*user_options.get_one::<bool>("serial").unwrap_or(&false)
77-
|| user_options
78-
.get_one::<String>("protocol")
79-
.is_some_and(|p| p == "legacy")
75+
let serial_flag = *user_options.get_one::<bool>("serial").unwrap_or(&false);
76+
if serial_flag {
77+
return true;
78+
}
79+
80+
let is_listen_command = user_options
81+
.try_get_one::<String>("protocol")
82+
.is_ok_and(|option| option.is_some_and(|val| val == "legacy"));
83+
84+
is_listen_command
8085
}
8186

8287
fn get_known_board(user_options: &ArgMatches) -> Option<Box<dyn KnownBoard>> {
@@ -147,12 +152,13 @@ async fn main() -> Result<()> {
147152
"info" => log::LevelFilter::Info,
148153
"debug" => log::LevelFilter::Debug,
149154
"trace" => log::LevelFilter::Trace,
150-
level => panic!("Unknown log level: {}", level),
155+
level => panic!("Unknown log level: {level}"),
151156
};
152157

153158
builder.filter_level(log::LevelFilter::Off);
154159
builder.filter_module("tockloader-lib", cli_level);
155160
builder.filter_module("tockloader", cli_level);
161+
builder.filter_module("tbf_parser", cli_level);
156162

157163
builder.init();
158164

tockloader-lib/src/attributes/app_attributes.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ impl AppAttributes {
9898
_ => return Ok(apps_details),
9999
};
100100

101+
log::debug!(
102+
"App #{apps_counter}: TBF version {tbf_version}, header size {header_size}, total size {total_size}",
103+
);
104+
101105
let mut header_data = vec![0u8; header_size as usize];
102106

103107
board_core.read(appaddr, &mut header_data)?;
108+
log::debug!("App #{apps_counter}: Header data: {header_data:?}");
104109
let header = parse_tbf_header(&header_data, tbf_version)
105110
.map_err(TockError::InvalidAppTbfHeader)?;
106111

@@ -118,7 +123,7 @@ impl AppAttributes {
118123
let mut footer_number = 0;
119124

120125
// Try to parse footers until we reach the end of the application.
121-
loop {
126+
while footer_offset < total_size {
122127
// We don't know the size of the current footer, so we read the
123128
// remaining bytes in the application (`footer_offset -
124129
// binary_end_offset`) , even if we overread.
@@ -135,10 +140,6 @@ impl AppAttributes {
135140
footer_number += 1;
136141
// we add 4 because that is the size of TL part of the TLV header (2 bytes type + 2 bytes length)
137142
footer_offset += footer_info.1 + 4;
138-
139-
if footer_offset == total_size {
140-
break;
141-
}
142143
}
143144

144145
let details: AppAttributes = AppAttributes::new(header, footers);
@@ -207,6 +208,10 @@ impl AppAttributes {
207208
_ => break,
208209
};
209210

211+
log::debug!(
212+
"App #{apps_counter}: TBF version {tbf_version}, header size {header_size}, total size {total_size}",
213+
);
214+
210215
let mut pkt = (appaddr as u32).to_le_bytes().to_vec();
211216
let length = (header_size).to_le_bytes().to_vec();
212217
for i in length {
@@ -224,6 +229,7 @@ impl AppAttributes {
224229
)
225230
.await?;
226231

232+
log::debug!("App #{apps_counter}: Header data: {header_data:?}");
227233
let header = parse_tbf_header(&header_data, tbf_version)
228234
.map_err(TockError::InvalidAppTbfHeader)?;
229235
let binary_end_offset = header.get_binary_end();
@@ -234,7 +240,7 @@ impl AppAttributes {
234240
let mut footer_number = 0;
235241

236242
// Try to parse footers until we reach the end of the application.
237-
loop {
243+
while footer_offset < total_size {
238244
// We don't know the size of the current footer, so we read the
239245
// remaining bytes in the application (`footer_offset -
240246
// binary_end_offset`) , even if we overread.
@@ -265,10 +271,6 @@ impl AppAttributes {
265271
footer_number += 1;
266272
// we add 4 because that is the size of TL part of the TLV header (2 bytes type + 2 bytes length)
267273
footer_offset += footer_info.1 + 4;
268-
269-
if footer_offset == total_size {
270-
break;
271-
}
272274
}
273275

274276
let details: AppAttributes = AppAttributes::new(header, footers);

0 commit comments

Comments
 (0)