Skip to content

Commit d9c0012

Browse files
addrian-77eva-cosma
authored andcommitted
refactor: separated IO Commands
Signed-off-by: addrian-77 <lunguadrian30@gmail.com>
1 parent ed89811 commit d9c0012

File tree

12 files changed

+49
-74
lines changed

12 files changed

+49
-74
lines changed

tbf-parser/src/parse.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,9 @@ pub fn parse_tbf_header(
203203
}
204204
types::TbfHeaderTypes::TbfHeaderWriteableFlashRegions => {
205205
// Length must be a multiple of the size of a region definition.
206-
if (tlv_header.length as usize)
207-
.is_multiple_of(mem::size_of::<
208-
types::TbfHeaderV2WriteableFlashRegion,
209-
>())
206+
if tlv_header.length as usize
207+
% mem::size_of::<types::TbfHeaderV2WriteableFlashRegion>()
208+
== 0
210209
{
211210
// Calculate how many writeable flash regions
212211
// there are specified in this header.

tockloader-lib/src/attributes/app_attributes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tokio_serial::SerialStream;
1111

1212
use crate::bootloader_serial::{issue_command, Command, Response};
1313
use crate::errors::{TockError, TockloaderError};
14-
use crate::IOCommands;
14+
use crate::IO;
1515

1616
/// This structure contains all relevant information about a tock application.
1717
///
@@ -326,7 +326,8 @@ impl AppAttributes {
326326
Ok(apps_details)
327327
}
328328

329-
pub async fn read(&mut self, conn: &mut dyn IOCommands) -> Result<Vec<u8>, TockloaderError> {
329+
/// This function reads the full binary of a given app
330+
pub async fn read_binary(&mut self, conn: &mut dyn IO) -> Result<Vec<u8>, TockloaderError> {
330331
conn.read(self.address, self.size as usize).await
331332
}
332333
}

tockloader-lib/src/command_impl/erase_apps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use async_trait::async_trait;
33
use crate::board_settings::BoardSettings;
44
use crate::connection::TockloaderConnection;
55
use crate::errors::TockloaderError;
6-
use crate::{CommandEraseApps, IOCommands};
6+
use crate::{CommandEraseApps, IO};
77

88
#[async_trait]
99
impl CommandEraseApps for TockloaderConnection {
1010
async fn erase_apps(&mut self, settings: &BoardSettings) -> Result<(), TockloaderError> {
11-
self.write(settings.start_address, [0x0].to_vec()).await
11+
self.write(settings.start_address, vec![0u8]).await
1212
}
1313
}

tockloader-lib/src/command_impl/generalized.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use crate::attributes::system_attributes::SystemAttributes;
55
use crate::board_settings::BoardSettings;
66
use crate::connection::TockloaderConnection;
77
use crate::errors::TockloaderError;
8-
use crate::IOCommands;
8+
use crate::{IOCommands, IO};
99

1010
#[async_trait]
11-
impl IOCommands for TockloaderConnection {
11+
impl IO for TockloaderConnection {
1212
async fn read(&mut self, address: u64, size: usize) -> Result<Vec<u8>, TockloaderError> {
1313
match self {
1414
TockloaderConnection::ProbeRS(conn) => conn.read(address, size).await,
@@ -22,14 +22,17 @@ impl IOCommands for TockloaderConnection {
2222
TockloaderConnection::Serial(conn) => conn.write(address, pkt).await,
2323
}
2424
}
25+
}
2526

26-
async fn list_apps(
27+
#[async_trait]
28+
impl IOCommands for TockloaderConnection {
29+
async fn read_installed_apps(
2730
&mut self,
2831
settings: &BoardSettings,
2932
) -> Result<Vec<AppAttributes>, TockloaderError> {
3033
match self {
31-
TockloaderConnection::ProbeRS(conn) => conn.list_apps(settings).await,
32-
TockloaderConnection::Serial(conn) => conn.list_apps(settings).await,
34+
TockloaderConnection::ProbeRS(conn) => conn.read_installed_apps(settings).await,
35+
TockloaderConnection::Serial(conn) => conn.read_installed_apps(settings).await,
3336
}
3437
}
3538

tockloader-lib/src/command_impl/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl CommandInfo for TockloaderConnection {
1212
&mut self,
1313
settings: &BoardSettings,
1414
) -> Result<GeneralAttributes, TockloaderError> {
15-
let installed_apps = self.list_apps(settings).await.unwrap();
15+
let installed_apps = self.read_installed_apps(settings).await.unwrap();
1616
let system_atributes = self.read_system_attributes().await.unwrap();
1717
Ok(GeneralAttributes::new(system_atributes, installed_apps))
1818
}

tockloader-lib/src/command_impl/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::command_impl::reshuffle_apps::{create_pkt, reconstruct_app, reshuffle
66
use crate::connection::TockloaderConnection;
77
use crate::errors::TockloaderError;
88
use crate::tabs::tab::Tab;
9-
use crate::{CommandInstall, CommandList, IOCommands};
9+
use crate::{CommandInstall, CommandList, IO};
1010

1111
#[async_trait]
1212
impl CommandInstall for TockloaderConnection {
@@ -30,7 +30,7 @@ impl CommandInstall for TockloaderConnection {
3030
for app in installed_apps.iter() {
3131
match app.installed {
3232
true => {
33-
app_binaries.push(app.clone().read(self).await.unwrap());
33+
app_binaries.push(app.clone().read_binary(self).await.unwrap());
3434
}
3535
false => {
3636
// TODO(adi): change this when TBF Filtering will get merged

tockloader-lib/src/command_impl/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ impl CommandList for TockloaderConnection {
1212
&mut self,
1313
settings: &BoardSettings,
1414
) -> Result<Vec<AppAttributes>, TockloaderError> {
15-
self.list_apps(settings).await
15+
self.read_installed_apps(settings).await
1616
}
1717
}

tockloader-lib/src/command_impl/probers/io.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{
66
board_settings::BoardSettings,
77
connection::{Connection, ProbeRSConnection},
88
errors::{InternalError, TockloaderError},
9-
IOCommands,
9+
IOCommands, IO,
1010
};
1111

1212
#[async_trait]
13-
impl IOCommands for ProbeRSConnection {
13+
impl IO for ProbeRSConnection {
1414
async fn read(&mut self, address: u64, size: usize) -> Result<Vec<u8>, TockloaderError> {
1515
if !self.is_open() {
1616
return Err(InternalError::ConnectionNotOpen.into());
@@ -38,8 +38,11 @@ impl IOCommands for ProbeRSConnection {
3838
loader.commit(session, options)?;
3939
Ok(())
4040
}
41+
}
4142

42-
async fn list_apps(
43+
#[async_trait]
44+
impl IOCommands for ProbeRSConnection {
45+
async fn read_installed_apps(
4346
&mut self,
4447
settings: &BoardSettings,
4548
) -> Result<Vec<AppAttributes>, TockloaderError> {
@@ -60,7 +63,6 @@ impl IOCommands for ProbeRSConnection {
6063

6164
let mut core = session.core(self.target_info.core)?;
6265

63-
// TODO(george-cosma): extract these informations without bootloader
6466
let system_attributes = SystemAttributes::read_system_attributes_probe(&mut core)?;
6567
Ok(system_attributes)
6668
}

tockloader-lib/src/command_impl/serial/erase_apps.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

tockloader-lib/src/command_impl/serial/io.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::{
77
command_impl::reshuffle_apps::PAGE_SIZE,
88
connection::{Connection, SerialConnection},
99
errors::{InternalError, TockloaderError},
10-
IOCommands,
10+
IOCommands, IO,
1111
};
1212

1313
#[async_trait]
14-
impl IOCommands for SerialConnection {
14+
impl IO for SerialConnection {
1515
async fn read(&mut self, address: u64, size: usize) -> Result<Vec<u8>, TockloaderError> {
1616
let mut pkt = (address as u32).to_le_bytes().to_vec();
1717
let length = (size as u16).to_le_bytes().to_vec();
@@ -30,34 +30,35 @@ impl IOCommands for SerialConnection {
3030
)
3131
.await?;
3232

33-
// this might be very specific, but when I was testing, i got 8191 bytes instead of 8192
34-
// this is because of the consecutive ESCAPE_CHAR rule inside issue_command function
35-
3633
if appdata.len() < size {
37-
panic!("Appdata was not read correctly?? We should not reach here");
34+
// Sanity check that we wrote everything. This was previously failing
35+
// due to not reading enough when encountering double ESCAPE_CHAR.
36+
panic!("Internal Error: When reading from a Serial connection, we read less bytes than requested despite previous checks.");
3837
}
3938
Ok(appdata)
4039
}
4140

4241
async fn write(&mut self, address: u64, pkt: Vec<u8>) -> Result<(), TockloaderError> {
4342
let stream = self.stream.as_mut().expect("Board must be open");
4443
let mut binary = pkt.clone();
45-
while !binary.len().is_multiple_of(PAGE_SIZE as usize) {
46-
binary.push(0u8);
44+
45+
if !binary.len().is_multiple_of(PAGE_SIZE as usize) {
46+
binary.extend(vec![
47+
0u8;
48+
PAGE_SIZE as usize - (binary.len() % PAGE_SIZE as usize)
49+
]);
4750
}
4851

49-
let mut page_number = 0;
50-
while (page_number + 1) * PAGE_SIZE <= binary.len() as u32 {
51-
let mut pkt = (address as u32 + page_number * PAGE_SIZE)
52+
for page_number in 0..(binary.len() / PAGE_SIZE as usize) {
53+
let mut pkt = (address as u32 + page_number as u32 * PAGE_SIZE)
5254
.to_le_bytes()
5355
.to_vec();
5456
pkt.append(
5557
&mut binary
56-
[(page_number * PAGE_SIZE) as usize..((page_number + 1) * PAGE_SIZE) as usize]
58+
[(page_number * PAGE_SIZE as usize)..((page_number + 1) * PAGE_SIZE as usize)]
5759
.to_vec(),
5860
);
5961
let _ = issue_command(stream, Command::WritePage, pkt, true, 0, Response::OK).await?;
60-
page_number += 1;
6162
}
6263

6364
let pkt = (address as u32 + binary.len() as u32)
@@ -67,8 +68,11 @@ impl IOCommands for SerialConnection {
6768
let _ = issue_command(stream, Command::ErasePage, pkt, true, 0, Response::OK).await?;
6869
Ok(())
6970
}
71+
}
7072

71-
async fn list_apps(
73+
#[async_trait]
74+
impl IOCommands for SerialConnection {
75+
async fn read_installed_apps(
7276
&mut self,
7377
settings: &BoardSettings,
7478
) -> Result<Vec<AppAttributes>, TockloaderError> {

0 commit comments

Comments
 (0)