Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Mar 23, 2022
1 parent b3fdcc8 commit baac55a
Show file tree
Hide file tree
Showing 21 changed files with 998 additions and 689 deletions.
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ fn main() {
fs::create_dir_all(&outdir).unwrap();
println!("{:?}", &outdir);

#[cfg(feature = "cli")] {
#[cfg(feature = "cli")]
{
use clap_complete::{generate_to, shells};

// Use clap to build completion files.
Expand Down
2 changes: 0 additions & 2 deletions src/bin/lpc55/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,5 +420,3 @@ pub fn long_version(revision_hash: Option<&str>) -> String {
};
format!("{}{}", crate_version!(), hash)
}


8 changes: 1 addition & 7 deletions src/bin/lpc55/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ impl Log for Logger {
);
}
_ => {
eprintln!(
"{}|{}: {}",
record.level(),
record.target(),
record.args()
);
eprintln!("{}|{}: {}", record.level(), record.target(), record.args());
}
}
}
Expand All @@ -68,4 +63,3 @@ impl Log for Logger {
// We use eprintln! which is flushed on every call.
}
}

207 changes: 100 additions & 107 deletions src/bin/lpc55/main.rs

Large diffs are not rendered by default.

99 changes: 74 additions & 25 deletions src/bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod command;
pub use command::{Command, KeystoreOperation, Response};
pub mod error;
pub mod property;
pub use property::{GetProperties, Property, Properties};
pub use property::{GetProperties, Properties, Property};
pub mod protocol;
pub mod provision;
use protocol::Protocol;
Expand All @@ -33,7 +33,8 @@ pub trait UuidSelectable: Sized {
///
/// Default implementation; replace for better error messages.
fn having(uuid: Uuid) -> anyhow::Result<Self> {
let mut candidates: Vec<Self> = Self::list().into_iter()
let mut candidates: Vec<Self> = Self::list()
.into_iter()
// The Err variant is preventing a simple `Ok(uuid) == entry.try_uuid()`,
// as there is no Eq on anyhow::Error.
.filter_map(|mut entry| {
Expand All @@ -46,11 +47,16 @@ pub trait UuidSelectable: Sized {
} else {
None
}
}).collect();
})
.collect();
match candidates.len() {
0 => Err(anyhow!("No candidate has UUID {:X}", uuid.to_simple())),
1 => Ok(candidates.remove(0)),
n => Err(anyhow!("Multiple ({}) candidates have UUID {:X}", n, uuid.to_simple())),
n => Err(anyhow!(
"Multiple ({}) candidates have UUID {:X}",
n,
uuid.to_simple()
)),
}
}
}
Expand All @@ -70,7 +76,7 @@ impl fmt::Debug for Bootloader {
.field("vid", &hexstr!(&self.vid.to_be_bytes()))
.field("pid", &hexstr!(&self.pid.to_be_bytes()))
.field("uuid", &hexstr!(&self.uuid.to_be_bytes()))
.finish()
.finish()
}
}

Expand All @@ -80,8 +86,6 @@ impl fmt::Display for Bootloader {
}
}



/// Bootloader commands return a "status". The non-zero statii can be split
/// as `100*group + code`. We map these groups into enum variants, containing
/// the code interpreted as an error the area.
Expand All @@ -100,18 +104,27 @@ pub enum Error {

pub type Result<T> = std::result::Result<T, Error>;


impl UuidSelectable for Bootloader {
fn try_uuid(&mut self) -> anyhow::Result<Uuid> {
Ok(self.uuid())
}

fn having(uuid: Uuid) -> anyhow::Result<Self> {
let mut candidates: Vec<Self> = Self::list().into_iter().filter(|bootloader| bootloader.uuid() == uuid).collect();
let mut candidates: Vec<Self> = Self::list()
.into_iter()
.filter(|bootloader| bootloader.uuid() == uuid)
.collect();
match candidates.len() {
0 => Err(anyhow!("No usable bootloader has UUID {:X}", uuid.to_simple())),
0 => Err(anyhow!(
"No usable bootloader has UUID {:X}",
uuid.to_simple()
)),
1 => Ok(candidates.remove(0)),
n => Err(anyhow!("Multiple ({}) bootloaders have UUID {:X}", n, uuid.to_simple())),
n => Err(anyhow!(
"Multiple ({}) bootloaders have UUID {:X}",
n,
uuid.to_simple()
)),
}
}

Expand All @@ -132,21 +145,30 @@ impl UuidSelectable for Bootloader {
if device_info.product_string() != Some("USB COMPOSITE DEVICE") {
return None;
}
device_info.open_device(&api).ok()
device_info
.open_device(&api)
.ok()
.map(|device| (device, vid, pid))
})
.filter_map(|(device, vid, pid)| {
let protocol = Protocol::new(device);
GetProperties { protocol: &protocol }.device_uuid().ok()
.map(|uuid| Self { protocol, vid, pid, uuid })
GetProperties {
protocol: &protocol,
}
.device_uuid()
.ok()
.map(|uuid| Self {
protocol,
vid,
pid,
uuid,
})
})
.collect()
}

}

impl Bootloader {

fn uuid(&self) -> Uuid {
Uuid::from_u128(self.uuid)
}
Expand All @@ -157,12 +179,18 @@ impl Bootloader {
}

/// Attempt to find a unique ROM bootloader with the given VID, PID and UUID.
pub fn try_find(vid: Option<u16>, pid: Option<u16>, uuid: Option<Uuid>) -> anyhow::Result<Self> {
pub fn try_find(
vid: Option<u16>,
pid: Option<u16>,
uuid: Option<Uuid>,
) -> anyhow::Result<Self> {
let mut bootloaders = Self::find(vid, pid, uuid);
if bootloaders.len() > 1 {
Err(anyhow!("Muliple matching bootloaders found"))
} else {
bootloaders.pop().ok_or_else(|| anyhow!("No matching bootloader found"))
bootloaders
.pop()
.ok_or_else(|| anyhow!("No matching bootloader found"))
}
}

Expand Down Expand Up @@ -193,7 +221,9 @@ impl Bootloader {
// 03000C00 A0000002 00000000 15000000 00000000 00000000 00000000 00000000 00000000 00000030 FF5F0030 00000020 FF5F0020 00000000 00000000
// second time i ran this:
// 03000C00 A0000002 00000000 15000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
self.protocol.call(&Command::Keystore(KeystoreOperation::Enroll)).expect("success");
self.protocol
.call(&Command::Keystore(KeystoreOperation::Enroll))
.expect("success");
info!("PUF enrolled");
}

Expand Down Expand Up @@ -246,7 +276,10 @@ impl Bootloader {
}

pub fn read_memory_at_most_512(&self, address: usize, length: usize) -> Vec<u8> {
let response = self.protocol.call(&Command::ReadMemory { address, length }).expect("success");
let response = self
.protocol
.call(&Command::ReadMemory { address, length })
.expect("success");
if let Response::ReadMemory(data) = response {
data
} else {
Expand All @@ -255,30 +288,46 @@ impl Bootloader {
}

pub fn receive_sb_file(&self, data: &[u8]) {
let _response = self.protocol.call(&Command::ReceiveSbFile { data: data.to_vec() }).expect("success");
let _response = self
.protocol
.call(&Command::ReceiveSbFile {
data: data.to_vec(),
})
.expect("success");
}

pub fn erase_flash(&self, address: usize, length: usize) {
let _response = self.protocol.call(&Command::EraseFlash { address, length }).expect("success");
let _response = self
.protocol
.call(&Command::EraseFlash { address, length })
.expect("success");
}

pub fn write_memory(&self, address: usize, data: Vec<u8>) {
let _response = self.protocol.call(&Command::WriteMemory { address, data }).expect("success");
let _response = self
.protocol
.call(&Command::WriteMemory { address, data })
.expect("success");
}

fn property(&self, property: property::Property) -> Result<Vec<u32>> {
self.protocol.property(property)
}

pub fn properties(&self) -> property::GetProperties<'_> {
GetProperties { protocol: &self.protocol }
GetProperties {
protocol: &self.protocol,
}
}

pub fn all_properties(&self) -> Properties {
self.properties().all()
}

pub fn run_command(&self, cmd: Command) -> std::result::Result<command::Response, protocol::Error> {
pub fn run_command(
&self,
cmd: Command,
) -> std::result::Result<command::Response, protocol::Error> {
self.protocol.call(&cmd)
}
}
Expand Down
Loading

0 comments on commit baac55a

Please sign in to comment.