Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply formatting to the source tree #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions examples/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ecflash::{EcFlash, Flasher};
use std::{env, fs, io, process, thread, time};

fn main() {
extern {
extern "C" {
fn iopl(level: isize) -> isize;
}

Expand All @@ -26,7 +26,10 @@ fn main() {
// Get I/O Permission
unsafe {
if iopl(3) < 0 {
eprintln!("Failed to get I/O permission: {}", io::Error::last_os_error());
eprintln!(
"Failed to get I/O permission: {}",
io::Error::last_os_error()
);
process::exit(1);
}

Expand All @@ -44,7 +47,10 @@ fn main() {
if let Ok(_original) = flasher.read(|x| eprint!("\rRead: {} KB", x / 1024)) {
eprintln!();

if flasher.erase(|x| eprint!("\rErase: {} KB", x / 1024)).is_ok() {
if flasher
.erase(|x| eprint!("\rErase: {} KB", x / 1024))
.is_ok()
{
eprintln!();

if let Ok(erased) = flasher.read(|x| eprint!("\rRead: {} KB", x / 1024)) {
Expand All @@ -53,28 +59,27 @@ fn main() {
//TODO: retry erase on fail
for i in 0..erased.len() {
if erased[i] != 0xFF {
println!(
"0x{:X}: 0x{:02X} != 0xFF",
i,
erased[i]
);
println!("0x{:X}: 0x{:02X} != 0xFF", i, erased[i]);
}
}

if flasher.write(&data, |x| eprint!("\rWrite {} KB", x / 1024)).is_ok() {
if flasher
.write(&data, |x| eprint!("\rWrite {} KB", x / 1024))
.is_ok()
{
eprintln!();

if let Ok(written) = flasher.read(|x| eprint!("\rRead: {} KB", x / 1024)) {
if let Ok(written) =
flasher.read(|x| eprint!("\rRead: {} KB", x / 1024))
{
eprintln!();

success = true;
for i in 0..written.len() {
if written[i] != data[i] {
println!(
"0x{:X}: 0x{:02X} != 0x{:02X}",
i,
written[i],
data[i]
i, written[i], data[i]
);
success = false;
}
Expand Down
108 changes: 47 additions & 61 deletions examples/isp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ use std::env;
use std::fs;
use std::io::{self, Read, Write};
use std::process;
use std::time::Duration;
use std::thread;
use std::time::Duration;

use ecflash::EcFlash;

const EC_KNOWN_IDS: &[u16] = &[
0x5570,
0x8587,
];
const EC_KNOWN_IDS: &[u16] = &[0x5570, 0x8587];

#[repr(u8)]
pub enum Address {
Expand Down Expand Up @@ -111,7 +108,10 @@ pub trait Smfi {
}
}

impl<T> Smfi for T where T: Debugger {
impl<T> Smfi for T
where
T: Debugger,
{
/// Set indar1 register (special case for follow mode)
fn flash_indar1(&mut self, data: u8) -> Result<()> {
self.write_at(Address::INDAR1, &[data])?;
Expand Down Expand Up @@ -145,9 +145,7 @@ pub struct SpiBus<'a, T: Smfi> {

impl<'a, T: Smfi> SpiBus<'a, T> {
pub fn new(port: &'a mut T, eflash: bool) -> Result<Self> {
port.flash_address(
if eflash { 0x7FFF_FE00 } else { 0xFFFF_FE00 },
)?;
port.flash_address(if eflash { 0x7FFF_FE00 } else { 0xFFFF_FE00 })?;

let mut spi = Self { port, data: false };
spi.reset()?;
Expand Down Expand Up @@ -249,7 +247,7 @@ impl<'a, 't, T: Smfi> SpiRom<'a, 't, T> {
if (address & 0xFF00_0000) > 0 {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("address {:X} exceeds 24 bits", address)
format!("address {:X} exceeds 24 bits", address),
));
}

Expand All @@ -276,7 +274,7 @@ impl<'a, 't, T: Smfi> SpiRom<'a, 't, T> {
if (address & 0xFF00_0000) > 0 {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("address {:X} exceeds 24 bits", address)
format!("address {:X} exceeds 24 bits", address),
));
}

Expand All @@ -295,15 +293,15 @@ impl<'a, 't, T: Smfi> SpiRom<'a, 't, T> {
if (address & 0xFF00_0000) > 0 {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("address {:X} exceeds 24 bits", address)
format!("address {:X} exceeds 24 bits", address),
));
}

//TODO: Support programming with any length
if (data.len() % 2) != 0 {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("length {} is not a multiple of 2", data.len())
format!("length {} is not a multiple of 2", data.len()),
));
}

Expand All @@ -318,14 +316,10 @@ impl<'a, 't, T: Smfi> SpiRom<'a, 't, T> {
(address >> 8) as u8,
address as u8,
word[0],
word[1]
word[1],
])?;
} else {
self.bus.write(&[
0xAD,
word[0],
word[1]
])?;
self.bus.write(&[0xAD, word[0], word[1]])?;
}

// Poll status for busy flag
Expand Down Expand Up @@ -361,7 +355,10 @@ impl ParallelArduino {
.timeout(Duration::new(1, 0))
.open_native()?;

let mut port = Self { tty, buffer_size: 0 };
let mut port = Self {
tty,
buffer_size: 0,
};
// Wait until programmer is ready
thread::sleep(Duration::new(1, 0));
// Check that programmer is ready
Expand All @@ -373,28 +370,21 @@ impl ParallelArduino {
}

fn echo(&mut self) -> Result<()> {
self.tty.write_all(&[
b'E',
0,
0x76,
])?;
self.tty.write_all(&[b'E', 0, 0x76])?;

let mut b = [0];
self.tty.read_exact(&mut b)?;
if b[0] != 0x76 {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("received echo of {:02X} instead of {:02X}", b[0], 0x76)
format!("received echo of {:02X} instead of {:02X}", b[0], 0x76),
));
}
Ok(())
}

fn update_buffer_size(&mut self) -> Result<()> {
self.tty.write_all(&[
b'B',
0,
])?;
self.tty.write_all(&[b'B', 0])?;

let mut b = [0; 1];
self.tty.read_exact(&mut b)?;
Expand All @@ -408,21 +398,15 @@ impl ParallelArduino {

impl Debugger for ParallelArduino {
fn address(&mut self, address: u8) -> Result<()> {
self.tty.write_all(&[
b'A',
address,
])?;
self.tty.write_all(&[b'A', address])?;

Ok(())
}

fn read(&mut self, data: &mut [u8]) -> Result<usize> {
for chunk in data.chunks_mut(self.buffer_size) {
let param = (chunk.len() - 1) as u8;
self.tty.write_all(&[
b'R',
param,
])?;
self.tty.write_all(&[b'R', param])?;
self.tty.read_exact(chunk)?;
}

Expand All @@ -432,18 +416,15 @@ impl Debugger for ParallelArduino {
fn write(&mut self, data: &[u8]) -> Result<usize> {
for chunk in data.chunks(self.buffer_size) {
let param = (chunk.len() - 1) as u8;
self.tty.write_all(&[
b'W',
param,
])?;
self.tty.write_all(&[b'W', param])?;
self.tty.write_all(chunk)?;

let mut b = [0];
self.tty.read_exact(&mut b)?;
if b[0] != param {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("received ack of {:02X} instead of {:02X}", b[0], param)
format!("received ack of {:02X} instead of {:02X}", b[0], param),
));
}
}
Expand All @@ -461,9 +442,7 @@ impl I2EC {
pub fn new() -> Result<Self> {
//TODO: check EC ID using super i/o
if unsafe { libc::iopl(3) } != 0 {
return Err(Error::from(
io::Error::last_os_error()
));
return Err(Error::from(io::Error::last_os_error()));
}

Ok(Self {
Expand Down Expand Up @@ -528,19 +507,19 @@ impl Pmc {

pub unsafe fn command(&mut self, data: u8) {
//eprintln!("PMC command {:02X}", data);
while ! self.can_write() {}
while !self.can_write() {}
self.cmd.write(data);
}

pub unsafe fn read(&mut self) -> u8 {
//eprintln!("PMC read");
while ! self.can_read() {}
while !self.can_read() {}
self.data.read()
}

pub unsafe fn write(&mut self, data: u8) {
//eprintln!("PMC write {:02X}", data);
while ! self.can_write() {}
while !self.can_write() {}
self.data.write(data);
}

Expand Down Expand Up @@ -638,7 +617,7 @@ fn isp_inner<T: Any + Smfi>(port: &mut T, firmware: &[u8]) -> Result<()> {
let mut erased = true;
for &b in &rom[address..address + 1024] {
if b != 0xFF {
erased =false;
erased = false;
break;
}
}
Expand All @@ -662,7 +641,10 @@ fn isp_inner<T: Any + Smfi>(port: &mut T, firmware: &[u8]) -> Result<()> {
if rom[i] != 0xFF {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("Failed to erase: {:X} is {:X} instead of {:X}", i, rom[i], 0xFF)
format!(
"Failed to erase: {:X} is {:X} instead of {:X}",
i, rom[i], 0xFF
),
));
}
}
Expand All @@ -676,23 +658,22 @@ fn isp_inner<T: Any + Smfi>(port: &mut T, firmware: &[u8]) -> Result<()> {

{
eprintln!("SPI AAI word program (accelerated)");
let port = (spi.bus.port as &mut dyn Any).downcast_mut::<ParallelArduino>().unwrap();
let port = (spi.bus.port as &mut dyn Any)
.downcast_mut::<ParallelArduino>()
.unwrap();
for (i, chunk) in firmware.chunks(port.buffer_size).enumerate() {
eprint!(" program {} / {}\r", i * port.buffer_size, firmware.len());

let param = (chunk.len() - 1) as u8;
port.tty.write_all(&[
b'P',
param
])?;
port.tty.write_all(&[b'P', param])?;
port.tty.write_all(chunk)?;

let mut b = [0];
port.tty.read_exact(&mut b)?;
if b[0] != param {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("received ack of {:02X} instead of {:02X}", b[0], param)
format!("received ack of {:02X} instead of {:02X}", b[0], param),
));
}
}
Expand All @@ -705,7 +686,6 @@ fn isp_inner<T: Any + Smfi>(port: &mut T, firmware: &[u8]) -> Result<()> {
spi.write_at(0, firmware)?;
}


// Read entire ROM
eprintln!("SPI read");
spi.read_at(0, &mut rom)?;
Expand All @@ -716,7 +696,10 @@ fn isp_inner<T: Any + Smfi>(port: &mut T, firmware: &[u8]) -> Result<()> {
if &rom[i] != firmware.get(i).unwrap_or(&0xFF) {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("Failed to program: {:X} is {:X} instead of {:X}", i, rom[i], firmware[i])
format!(
"Failed to program: {:X} is {:X} instead of {:X}",
i, rom[i], firmware[i]
),
));
}
}
Expand Down Expand Up @@ -747,7 +730,10 @@ fn isp(internal: bool, file: &str) -> Result<()> {
if internal {
unsafe {
if libc::iopl(3) < 0 {
eprintln!("Failed to get I/O permission: {}", io::Error::last_os_error());
eprintln!(
"Failed to get I/O permission: {}",
io::Error::last_os_error()
);
process::exit(1);
}

Expand Down Expand Up @@ -792,7 +778,7 @@ fn isp(internal: bool, file: &str) -> Result<()> {
.expect("failed to run shutdown");

Ok(())
},
}
Err(err) => {
eprintln!("Failed to flash EC: {}", err);
Err(err)
Expand Down
Loading