Skip to content

Commit

Permalink
chore: Convert attribute unwraps to errors (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Micu Ana <[email protected]>
  • Loading branch information
george-cosma and Micu Ana authored Sep 20, 2024
1 parent 534d99b commit b58dd22
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 89 deletions.
179 changes: 96 additions & 83 deletions tockloader-lib/src/attributes/system_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use byteorder::{ByteOrder, LittleEndian};
use probe_rs::{Core, MemoryInterface};
use tokio_serial::SerialStream;

use crate::bootloader_serial::{issue_command, Command, Response};
use crate::{
bootloader_serial::{issue_command, Command, Response},
errors::TockloaderError,
};

use super::decode::{bytes_to_string, decode_attribute};

Expand Down Expand Up @@ -43,7 +46,9 @@ impl SystemAttributes {
}

// TODO: explain what is happening here
pub(crate) fn read_system_attributes_probe(board_core: &mut Core) -> Self {
pub(crate) fn read_system_attributes_probe(
board_core: &mut Core,
) -> Result<Self, TockloaderError> {
let mut result = SystemAttributes::new();
let address = 0x600;
let mut buf = [0u8; 64 * 16];
Expand All @@ -60,35 +65,37 @@ impl SystemAttributes {

let step_option = decode_attribute(step);

if step_option.is_none() {
continue;
}

let decoded_attributes = step_option.unwrap();

match index_data {
0 => {
result.board = Some(decoded_attributes.value.to_string());
}
1 => {
result.arch = Some(decoded_attributes.value.to_string());
if let Some(decoded_attributes) = step_option {
match index_data {
0 => {
result.board = Some(decoded_attributes.value.to_string());
}
1 => {
result.arch = Some(decoded_attributes.value.to_string());
}
2 => {
result.appaddr = Some(
u64::from_str_radix(
decoded_attributes
.value
.to_string()
.trim_start_matches("0x"),
16,
)
.map_err(|_| {
TockloaderError::MisconfiguredBoard(
"Invalid start address.".to_owned(),
)
})?,
);
}
3 => {
result.boothash = Some(decoded_attributes.value.to_string());
}
_ => {}
}
2 => {
result.appaddr = Some(
u64::from_str_radix(
decoded_attributes
.value
.to_string()
.trim_start_matches("0x"),
16,
)
.unwrap(),
);
}
3 => {
result.boothash = Some(decoded_attributes.value.to_string());
}
_ => panic!("Board data not found!"),
} else {
continue;
}
}

Expand All @@ -98,21 +105,25 @@ impl SystemAttributes {

let _ = board_core.read_8(address, &mut buf);

let decoder = utf8_decode::Decoder::new(buf.iter().cloned());

let mut string = String::new();
for n in decoder {
string.push(n.expect("Error decoding bootloader version"));
}
let string = String::from_utf8(buf.to_vec()).map_err(|_| {
TockloaderError::MisconfiguredBoard(
"Data may be corrupted. System attribure is not UTF-8.".to_owned(),
)
})?;

let string = string.trim_matches(char::from(0));

result.bootloader_version = Some(string.to_owned());

let mut kernel_attr_binary = [0u8; 100];
board_core
.read(result.appaddr.unwrap() - 100, &mut kernel_attr_binary)
.unwrap();
.read(
result.appaddr.ok_or(TockloaderError::MisconfiguredBoard(
"No start address found.".to_owned(),
))? - 100,
&mut kernel_attr_binary,
)
.map_err(TockloaderError::ProbeRsReadError)?;

let sentinel = bytes_to_string(&kernel_attr_binary[96..100]);
let kernel_version = LittleEndian::read_uint(&kernel_attr_binary[95..96], 1);
Expand All @@ -130,11 +141,13 @@ impl SystemAttributes {
result.kernel_bin_start = Some(kernel_binary_start);
result.kernel_bin_len = Some(kernel_binary_len);

result
Ok(result)
}

// TODO: explain what is happening here
pub(crate) async fn read_system_attributes_serial(port: &mut SerialStream) -> Self {
pub(crate) async fn read_system_attributes_serial(
port: &mut SerialStream,
) -> Result<Self, TockloaderError> {
let mut result = SystemAttributes::new();

let mut pkt = (0x600_u32).to_le_bytes().to_vec();
Expand All @@ -151,8 +164,7 @@ impl SystemAttributes {
64 * 16,
Response::ReadRange,
)
.await
.unwrap();
.await?;

let mut data = buf.chunks(64);

Expand All @@ -164,35 +176,37 @@ impl SystemAttributes {

let step_option = decode_attribute(step);

if step_option.is_none() {
continue;
}

let decoded_attributes = step_option.unwrap();

match index_data {
0 => {
result.board = Some(decoded_attributes.value.to_string());
}
1 => {
result.arch = Some(decoded_attributes.value.to_string());
if let Some(decoded_attributes) = step_option {
match index_data {
0 => {
result.board = Some(decoded_attributes.value.to_string());
}
1 => {
result.arch = Some(decoded_attributes.value.to_string());
}
2 => {
result.appaddr = Some(
u64::from_str_radix(
decoded_attributes
.value
.to_string()
.trim_start_matches("0x"),
16,
)
.map_err(|_| {
TockloaderError::MisconfiguredBoard(
"Invalid start address.".to_owned(),
)
})?,
);
}
3 => {
result.boothash = Some(decoded_attributes.value.to_string());
}
_ => {}
}
2 => {
result.appaddr = Some(
u64::from_str_radix(
decoded_attributes
.value
.to_string()
.trim_start_matches("0x"),
16,
)
.unwrap(),
);
}
3 => {
result.boothash = Some(decoded_attributes.value.to_string());
}
_ => panic!("Board data not found!"),
} else {
continue;
}
}

Expand All @@ -202,22 +216,22 @@ impl SystemAttributes {
pkt.push(i);
}

let (_, buf) = issue_command(port, Command::ReadRange, pkt, true, 8, Response::ReadRange)
.await
.unwrap();
let (_, buf) =
issue_command(port, Command::ReadRange, pkt, true, 8, Response::ReadRange).await?;

let decoder = utf8_decode::Decoder::new(buf.iter().cloned());

let mut string = String::new();
for n in decoder {
string.push(n.expect("Error decoding bootloader version"));
}
let string = String::from_utf8(buf).map_err(|_| {
TockloaderError::MisconfiguredBoard(
"Data may be corrupted. System attribure is not UTF-8.".to_owned(),
)
})?;

let string = string.trim_matches(char::from(0));

result.bootloader_version = Some(string.to_owned());

let mut pkt = ((result.appaddr.unwrap() - 100) as u32)
let mut pkt = ((result.appaddr.ok_or(TockloaderError::MisconfiguredBoard(
"No start address found.".to_owned(),
))? - 100) as u32)
.to_le_bytes()
.to_vec();
let length = (100_u16).to_le_bytes().to_vec();
Expand All @@ -233,8 +247,7 @@ impl SystemAttributes {
100,
Response::ReadRange,
)
.await
.unwrap();
.await?;

let sentinel = bytes_to_string(&kernel_attr_binary[96..100]);
let kernel_version = LittleEndian::read_uint(&kernel_attr_binary[95..96], 1);
Expand All @@ -252,6 +265,6 @@ impl SystemAttributes {
result.kernel_bin_start = Some(kernel_binary_start);
result.kernel_bin_len = Some(kernel_binary_len);

result
Ok(result)
}
}
12 changes: 6 additions & 6 deletions tockloader-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn list(
let mut core = session
.core(*core_index.unwrap())
.map_err(|e| TockloaderError::CoreAccessError(*core_index.unwrap(), e))?;
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core);
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core)?;
let appaddr = system_attributes
.appaddr
.ok_or(TockloaderError::MisconfiguredBoard(
Expand All @@ -62,7 +62,7 @@ pub async fn list(
}

let system_attributes =
SystemAttributes::read_system_attributes_serial(&mut port).await;
SystemAttributes::read_system_attributes_serial(&mut port).await?;

let appaddr = system_attributes
.appaddr
Expand All @@ -86,7 +86,7 @@ pub async fn info(
let mut core = session
.core(*core_index.unwrap())
.map_err(|e| TockloaderError::CoreAccessError(*core_index.unwrap(), e))?;
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core);
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core)?;
let appaddr = system_attributes
.appaddr
.ok_or(TockloaderError::MisconfiguredBoard(
Expand All @@ -106,7 +106,7 @@ pub async fn info(
}

let system_attributes =
SystemAttributes::read_system_attributes_serial(&mut port).await;
SystemAttributes::read_system_attributes_serial(&mut port).await?;

let appaddr = system_attributes
.appaddr
Expand All @@ -133,7 +133,7 @@ pub async fn install_app(
.core(*core_index.unwrap())
.map_err(|e| TockloaderError::CoreAccessError(*core_index.unwrap(), e))?;
// Get board data
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core);
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core)?;

let board = system_attributes
.board
Expand Down Expand Up @@ -306,7 +306,7 @@ pub async fn install_app(
}

let system_attributes =
SystemAttributes::read_system_attributes_serial(&mut port).await;
SystemAttributes::read_system_attributes_serial(&mut port).await?;

let board = system_attributes
.board
Expand Down

0 comments on commit b58dd22

Please sign in to comment.