From b58dd2264712114c4f6d8baa3e8938641212cad0 Mon Sep 17 00:00:00 2001 From: George Cosma Date: Fri, 20 Sep 2024 13:24:49 +0300 Subject: [PATCH] chore: Convert attribute unwraps to errors (#36) Co-authored-by: Micu Ana --- .../src/attributes/system_attributes.rs | 179 ++++++++++-------- tockloader-lib/src/lib.rs | 12 +- 2 files changed, 102 insertions(+), 89 deletions(-) diff --git a/tockloader-lib/src/attributes/system_attributes.rs b/tockloader-lib/src/attributes/system_attributes.rs index 1fa61ed..d086123 100644 --- a/tockloader-lib/src/attributes/system_attributes.rs +++ b/tockloader-lib/src/attributes/system_attributes.rs @@ -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}; @@ -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 { let mut result = SystemAttributes::new(); let address = 0x600; let mut buf = [0u8; 64 * 16]; @@ -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; } } @@ -98,12 +105,11 @@ 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)); @@ -111,8 +117,13 @@ impl SystemAttributes { 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); @@ -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 { let mut result = SystemAttributes::new(); let mut pkt = (0x600_u32).to_le_bytes().to_vec(); @@ -151,8 +164,7 @@ impl SystemAttributes { 64 * 16, Response::ReadRange, ) - .await - .unwrap(); + .await?; let mut data = buf.chunks(64); @@ -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; } } @@ -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(); @@ -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); @@ -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) } } diff --git a/tockloader-lib/src/lib.rs b/tockloader-lib/src/lib.rs index f7a50a1..83a782f 100644 --- a/tockloader-lib/src/lib.rs +++ b/tockloader-lib/src/lib.rs @@ -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( @@ -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 @@ -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( @@ -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 @@ -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 @@ -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