Skip to content

Commit bd9f51f

Browse files
committed
fix: corrected tbf parser values
Signed-off-by: Adrian Lungu <lunguadrian30@gmail.com>
1 parent 478bbb9 commit bd9f51f

5 files changed

Lines changed: 99 additions & 43 deletions

File tree

tbf-parser/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# tbf-parser
2+
3+
This document is a list of changes made to the original crate.
4+
5+
## Changes:
6+
- `get_application_flags`
7+
- Return flags of the application
8+
- DELTA: Originally did not exist
9+
- `get_protected_trailer_size`
10+
- Get the size of the protected trailer. This only returns the trailer size, WITHOUT the header. The app cannot write to this region.
11+
- DELTA: Originally did not exist
12+
- `get_protected_region_size`
13+
- Get the size in bytes of the protected region from the beginning of the process binary (start of the TBF header). The returned size includes the TBF Header. Only valid if this is an app.
14+
- DELTA: Originally named `get_protected_size`, renamed to remove ambiguity.
15+
- `get_tbf_version`
16+
- Return the version of the Tock Binary Format
17+
- DELTA: Originally did not exist

tbf-parser/src/types.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,16 @@ impl TbfHeader {
928928
}
929929
}
930930

931+
/// Return flags of the application
932+
///
933+
/// DELTA: Originally did not exist
934+
pub fn get_application_flags(&self) -> u32 {
935+
match *self {
936+
TbfHeader::TbfHeaderV2(hd) => hd.base.flags,
937+
_ => 0,
938+
}
939+
}
940+
931941
/// Return header size of the application.
932942
pub fn header_size(&self) -> u16 {
933943
match *self {
@@ -954,19 +964,17 @@ impl TbfHeader {
954964
}
955965
}
956966

957-
/// Get the number of bytes from the start of the app's region in flash that
958-
/// is for kernel use only. The app cannot write this region.
959-
pub fn get_protected_size(&self) -> u32 {
967+
/// Get the size of the protected trailer. This only returns only the
968+
/// trailer size, WITHOUT the header. The app cannot write to this region.
969+
///
970+
/// DELTA: Originally did not exist
971+
pub fn get_protected_trailer_size(&self) -> u32 {
960972
match *self {
961973
TbfHeader::TbfHeaderV2(hd) => {
962974
if hd.program.is_some() {
963-
hd.program.map_or(0, |p| {
964-
(hd.base.header_size as u32) + p.protected_trailer_size
965-
})
975+
hd.program.map_or(0, |p| p.protected_trailer_size)
966976
} else if hd.main.is_some() {
967-
hd.main.map_or(0, |m| {
968-
(hd.base.header_size as u32) + m.protected_trailer_size
969-
})
977+
hd.main.map_or(0, |m| m.protected_trailer_size)
970978
} else {
971979
0
972980
}
@@ -981,7 +989,18 @@ impl TbfHeader {
981989
pub fn get_app_start_offset(&self) -> u32 {
982990
// The application binary starts after the header plus any
983991
// additional protected space.
984-
self.get_protected_size()
992+
self.get_protected_region_size()
993+
}
994+
995+
/// Get the size in bytes of the protected region from the beginning
996+
/// of the process binary (start of the TBF header). The returned size
997+
/// includes the TBF Header, Only valid if this is an app.
998+
//
999+
// DELTA: Originally named get_protected_size, renamed to remove ambiguity
1000+
pub fn get_protected_region_size(&self) -> u32 {
1001+
// The application binary starts after the header plus any
1002+
// additional protected space.
1003+
self.header_size() as u32 + self.get_protected_trailer_size()
9851004
}
9861005

9871006
/// Get the offset from the beginning of the app's flash region where the
@@ -990,11 +1009,9 @@ impl TbfHeader {
9901009
match *self {
9911010
TbfHeader::TbfHeaderV2(hd) => {
9921011
if hd.program.is_some() {
993-
hd.program
994-
.map_or(0, |p| p.init_fn_offset + (hd.base.header_size as u32))
1012+
hd.program.map_or(0, |p| p.init_fn_offset)
9951013
} else if hd.main.is_some() {
996-
hd.main
997-
.map_or(0, |m| m.init_fn_offset + (hd.base.header_size as u32))
1014+
hd.main.map_or(0, |m| m.init_fn_offset)
9981015
} else {
9991016
0
10001017
}
@@ -1175,6 +1192,16 @@ impl TbfHeader {
11751192
}
11761193
}
11771194

1195+
/// Return the version of the Tock Binary Format
1196+
///
1197+
/// DELTA: originally did not exist
1198+
pub fn get_tbf_version(&self) -> u16 {
1199+
match self {
1200+
TbfHeader::TbfHeaderV2(hd) => hd.base.version,
1201+
_ => 0,
1202+
}
1203+
}
1204+
11781205
/// Return the fixed ShortId of the application if it was specified in the
11791206
/// TBF header.
11801207
pub fn get_fixed_short_id(&self) -> Option<core::num::NonZeroU32> {

tbf-parser/tests/parse.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ fn simple_tbf() {
1616
dbg!(&header);
1717
assert!(header.enabled());
1818
assert_eq!(header.get_minimum_app_ram_size(), 4848);
19-
assert_eq!(header.get_init_function_offset(), 41 + header_len as u32);
20-
assert_eq!(header.get_protected_size(), header_len as u32);
19+
assert_eq!(header.get_init_function_offset(), 41);
20+
assert_eq!(header.get_protected_trailer_size(), 0);
21+
assert_eq!(header.get_application_flags(), 1);
2122
assert_eq!(header.get_package_name().unwrap(), "_heart");
2223
assert_eq!(header.get_kernel_version().unwrap(), (2, 0));
2324
}
@@ -37,8 +38,9 @@ fn footer_sha256() {
3738
dbg!(&header);
3839
assert!(header.enabled());
3940
assert_eq!(header.get_minimum_app_ram_size(), 4848);
40-
assert_eq!(header.get_init_function_offset(), 41 + header_len as u32);
41-
assert_eq!(header.get_protected_size(), header_len as u32);
41+
assert_eq!(header.get_init_function_offset(), 41);
42+
assert_eq!(header.get_protected_trailer_size(), 0);
43+
assert_eq!(header.get_application_flags(), 1);
4244
assert_eq!(header.get_package_name().unwrap(), "_heart");
4345
assert_eq!(header.get_kernel_version().unwrap(), (2, 0));
4446
let binary_offset = header.get_binary_end() as usize;
@@ -87,8 +89,9 @@ fn footer_rsa4096() {
8789
dbg!(&header);
8890
assert!(header.enabled());
8991
assert_eq!(header.get_minimum_app_ram_size(), 4612);
90-
assert_eq!(header.get_init_function_offset(), 41 + header_len as u32);
91-
assert_eq!(header.get_protected_size(), header_len as u32);
92+
assert_eq!(header.get_init_function_offset(), 41);
93+
assert_eq!(header.get_protected_trailer_size(), 0);
94+
assert_eq!(header.get_application_flags(), 1);
9295
assert_eq!(header.get_package_name().unwrap(), "c_hello");
9396
assert_eq!(header.get_kernel_version().unwrap(), (2, 0));
9497
let binary_offset = header.get_binary_end() as usize;

tockloader-cli/src/display.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ pub async fn print_info(app_details: &mut [AppAttributes], system_details: &mut
7676
);
7777

7878
println!(
79-
" {BOLD_GREEN} Address in Flash: {RESET}{}",
80-
system_details.appaddr.unwrap(),
79+
" {BOLD_GREEN} Address in Flash: {RESET}{:#x}",
80+
details.address,
8181
);
8282

8383
println!(
8484
" {BOLD_GREEN} TBF version: {RESET}{}",
85-
details.tbf_header.get_binary_version(),
85+
details.tbf_header.get_tbf_version(),
8686
);
8787

8888
println!(
@@ -96,11 +96,14 @@ pub async fn print_info(app_details: &mut [AppAttributes], system_details: &mut
9696
);
9797

9898
println!(
99-
" {BOLD_GREEN} checksum: {RESET}{}",
99+
" {BOLD_GREEN} checksum: {RESET}{:#x}",
100100
details.tbf_header.checksum(),
101101
);
102102

103-
println!(" {BOLD_GREEN} flags:{RESET}");
103+
println!(
104+
" {BOLD_GREEN} flags: {RESET}{:#b}",
105+
details.tbf_header.get_application_flags(),
106+
);
104107
println!(
105108
" {BOLD_GREEN} enabled: {RESET}{}",
106109
details.tbf_header.enabled(),
@@ -111,55 +114,55 @@ pub async fn print_info(app_details: &mut [AppAttributes], system_details: &mut
111114
details.tbf_header.sticky(),
112115
);
113116

114-
println!(" {BOLD_GREEN} TVL: Main (1){RESET}");
117+
println!(" {BOLD_GREEN} TLV: Main (1){RESET}");
115118
println!(
116-
" {BOLD_GREEN} init_fn_offset: {RESET}{}",
119+
" {BOLD_GREEN} init_fn_offset: {RESET}{}",
117120
details.tbf_header.get_init_function_offset(),
118121
);
119122

120123
println!(
121-
" {BOLD_GREEN} protected_size: {RESET}{}",
122-
details.tbf_header.get_protected_size(),
124+
" {BOLD_GREEN} protected_trailer_size: {RESET}{}",
125+
details.tbf_header.get_protected_trailer_size(),
123126
);
124127

125128
println!(
126-
" {BOLD_GREEN} minimum_ram_size: {RESET}{}",
129+
" {BOLD_GREEN} minimum_ram_size: {RESET}{}",
127130
details.tbf_header.get_minimum_app_ram_size(),
128131
);
129132

130-
println!(" {BOLD_GREEN} TVL: Program (9){RESET}");
133+
println!(" {BOLD_GREEN} TLV: Program (9){RESET}");
131134
println!(
132-
" {BOLD_GREEN} init_fn_offset: {RESET}{}",
135+
" {BOLD_GREEN} init_fn_offset: {RESET}{}",
133136
details.tbf_header.get_init_function_offset(),
134137
);
135138

136139
println!(
137-
" {BOLD_GREEN} protected_size: {RESET}{}",
138-
details.tbf_header.get_protected_size(),
140+
" {BOLD_GREEN} protected_trailer_size: {RESET}{}",
141+
details.tbf_header.get_protected_trailer_size(),
139142
);
140143

141144
println!(
142-
" {BOLD_GREEN} minimum_ram_size: {RESET}{}",
145+
" {BOLD_GREEN} minimum_ram_size: {RESET}{}",
143146
details.tbf_header.get_minimum_app_ram_size(),
144147
);
145148

146149
println!(
147-
" {BOLD_GREEN} binary_end_offset: {RESET}{}",
150+
" {BOLD_GREEN} binary_end_offset: {RESET}{}",
148151
details.tbf_header.get_binary_end(),
149152
);
150153

151154
println!(
152-
" {BOLD_GREEN} app_version: {RESET}{}",
155+
" {BOLD_GREEN} app_version: {RESET}{}",
153156
details.tbf_header.get_binary_version(),
154157
);
155158

156-
println!(" {BOLD_GREEN} TVL: Package Name (3){RESET}");
159+
println!(" {BOLD_GREEN} TLV: Package Name (3){RESET}");
157160
println!(
158161
" {BOLD_GREEN} package_name: {RESET}{}",
159162
details.tbf_header.get_package_name().unwrap(),
160163
);
161164

162-
println!(" {BOLD_GREEN} TVL: Kernel Version (8){RESET}");
165+
println!(" {BOLD_GREEN} TLV: Kernel Version (8){RESET}");
163166
println!(
164167
" {BOLD_GREEN} kernel_major: {RESET}{}",
165168
details.tbf_header.get_kernel_version().unwrap().0,
@@ -183,7 +186,7 @@ pub async fn print_info(app_details: &mut [AppAttributes], system_details: &mut
183186
println!(" {BOLD_GREEN} footer_size: {RESET}{total_footer_size}");
184187

185188
for (j, footer_details) in details.tbf_footers.iter().enumerate() {
186-
println!(" {BOLD_GREEN} Footer [{j}] TVL: Credentials{RESET}");
189+
println!(" {BOLD_GREEN} Footer [{j}] TLV: Credentials{RESET}");
187190

188191
println!(
189192
" {BOLD_GREEN} Type: {RESET}{}",

tockloader-lib/src/attributes/app_attributes.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::errors::{TockError, TockloaderError};
1919
/// See also <https://book.tockos.org/doc/tock_binary_format>
2020
#[derive(Debug)]
2121
pub struct AppAttributes {
22+
pub address: u64,
2223
pub tbf_header: TbfHeader,
2324
pub tbf_footers: Vec<TbfFooter>,
2425
}
@@ -41,8 +42,13 @@ impl TbfFooter {
4142
// TODO(george-cosma): Could take advantages of the trait rework
4243

4344
impl AppAttributes {
44-
pub(crate) fn new(header_data: TbfHeader, footers_data: Vec<TbfFooter>) -> AppAttributes {
45+
pub(crate) fn new(
46+
address: u64,
47+
header_data: TbfHeader,
48+
footers_data: Vec<TbfFooter>,
49+
) -> AppAttributes {
4550
AppAttributes {
51+
address,
4652
tbf_header: header_data,
4753
tbf_footers: footers_data,
4854
}
@@ -150,7 +156,7 @@ impl AppAttributes {
150156
footer_offset += footer_info.1 + 4;
151157
}
152158

153-
let details: AppAttributes = AppAttributes::new(header, footers);
159+
let details: AppAttributes = AppAttributes::new(appaddr, header, footers);
154160

155161
apps_details.insert(apps_counter, details);
156162
apps_counter += 1;
@@ -289,7 +295,7 @@ impl AppAttributes {
289295
footer_offset += footer_info.1 + 4;
290296
}
291297

292-
let details: AppAttributes = AppAttributes::new(header, footers);
298+
let details: AppAttributes = AppAttributes::new(appaddr, header, footers);
293299

294300
apps_details.insert(apps_counter, details);
295301
apps_counter += 1;

0 commit comments

Comments
 (0)