Skip to content

Commit

Permalink
fix: Protect & Unprotect commands
Browse files Browse the repository at this point in the history
See-also #46
  • Loading branch information
andelf committed Jan 14, 2024
1 parent dea79b3 commit 1cadff1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
8 changes: 6 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ pub enum ConfigChip {
},
}
impl ConfigChip {
pub const FLAG_PROTECTED: u8 = 0x01;
pub const FLAG_READ_PROTECTED: u8 = 0x01;
pub const FLAG_WRITE_PROTECTED: u8 = 0x11;
}
impl Command for ConfigChip {
type Response = u8;
Expand All @@ -193,10 +194,13 @@ impl Command for ConfigChip {
ConfigChip::CheckReadProtect => vec![0x01],
ConfigChip::Unprotect => vec![0x02],
ConfigChip::Protect => vec![0x03],
// ret = 0x11 protected
ConfigChip::CheckReadProtectEx => vec![0x04],
// b = 0xff ?
ConfigChip::UnprotectEx(b) => vec![0x02, b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
// [0x03, 0xff, 0xff, 0xff, WPR0, WPR1, WPR2, WPR3]
ConfigChip::ProtectEx(b) => vec![0x03, b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
ConfigChip::Config { data: _, wrp: _ } => todo!("ConfigChip"),
ConfigChip::Config { data: _, wrp: _ } => todo!("ConfigChip: config flags"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ fn main() -> Result<()> {
}
Unprotect {} => {
log::info!("Unprotect Flash");
sess.protect_flash(false)?;
sess.unprotect_flash()?;
}
Protect {} => {
log::info!("Protect Flash");
sess.protect_flash(true)?;
sess.protect_flash()?;
}
Reset { mode } => {
log::info!("Reset {:?}", mode);
Expand Down
91 changes: 61 additions & 30 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ impl ProbeSession {
}

pub fn detach_chip(&mut self) -> Result<()> {
log::debug!("Detach chip");
log::trace!("Detach chip");
self.probe.send_command(commands::control::OptEnd)?;
Ok(())
}

fn reattach_chip(&mut self) -> Result<()> {
log::debug!("Reattach chip");
self.detach_chip()?;
let _ = self.probe.send_command(commands::control::AttachChip)?;
Ok(())
Expand All @@ -108,7 +109,7 @@ impl ProbeSession {
let flash_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtect)?;
let protected = flash_protected == commands::ConfigChip::FLAG_PROTECTED;
let protected = flash_protected == commands::ConfigChip::FLAG_READ_PROTECTED;
log::info!("Flash protected: {}", protected);
if protected {
log::warn!("Flash is protected, debug access is not available");
Expand All @@ -128,43 +129,73 @@ impl ProbeSession {
Ok(())
}

pub fn protect_flash(&mut self, protect: bool) -> Result<()> {
pub fn unprotect_flash(&mut self) -> Result<()> {
// HACK: requires a fresh attach
self.reattach_chip()?;

let flash_protected_flag = self
let read_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtect)?;
let protected = flash_protected_flag == commands::ConfigChip::FLAG_PROTECTED;
if protect == protected {
log::info!(
"Flash already {}",
if protected {
"protected"
} else {
"unprotected"
}
if read_protected == commands::ConfigChip::FLAG_READ_PROTECTED {
log::info!("Flash already unprotected");
}

self.probe.send_command(commands::ConfigChip::Unprotect)?;

self.reattach_chip()?;

let read_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtect)?;
log::info!(
"Read protected: {}",
read_protected == commands::ConfigChip::FLAG_READ_PROTECTED
);

let write_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtectEx)?;
if write_protected == commands::ConfigChip::FLAG_WRITE_PROTECTED {
log::warn!("Flash is write protected!");
log::warn!("try to unprotect...");
self.probe
.send_command(commands::ConfigChip::UnprotectEx(0xff))?; // FIXME: 0xff or 0xbf

self.reattach_chip()?;

let write_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtectEx)?;
println!(
"Write protected: {}",
write_protected == commands::ConfigChip::FLAG_WRITE_PROTECTED
);
}

let use_v2 = self.probe.info.version() >= (2, 9);
let cmd = match (protect, use_v2) {
(true, true) => commands::ConfigChip::ProtectEx(0xbf),
(true, false) => commands::ConfigChip::Protect,
(false, true) => commands::ConfigChip::UnprotectEx(0xbf),
(false, false) => commands::ConfigChip::Unprotect,
};
self.probe.send_command(cmd)?;
Ok(())
}

self.probe.send_command(commands::Reset::Soft)?; // quit reset
self.probe.send_command(commands::control::AttachChip)?;
pub fn protect_flash(&mut self) -> Result<()> {
// HACK: requires a fresh attach
self.reattach_chip()?;

let read_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtect)?;
if read_protected == commands::ConfigChip::FLAG_READ_PROTECTED {
log::warn!("Flash already protected");
}

self.probe.send_command(commands::ConfigChip::Protect)?;

self.reattach_chip()?;

let flash_protected = self
let read_protected = self
.probe
.send_command(commands::ConfigChip::CheckReadProtect)?;
log::info!(
"Flash protected: {}",
flash_protected == commands::ConfigChip::FLAG_PROTECTED
"Read protected: {}",
read_protected == commands::ConfigChip::FLAG_READ_PROTECTED
);

Ok(())
Expand All @@ -178,11 +209,11 @@ impl ProbeSession {
let ret = self
.probe
.send_command(commands::ConfigChip::CheckReadProtect)?;
if ret == commands::ConfigChip::FLAG_PROTECTED {
if ret == commands::ConfigChip::FLAG_READ_PROTECTED {
log::warn!("Flash is protected, unprotecting...");
self.protect_flash(false)?;
self.unprotect_flash()?;
} else if ret == 2 {
self.protect_flash(false)?;
self.unprotect_flash()?; // FIXME: 2 is unknown
} else {
log::warn!("Unknown flash protect status: {}", ret);
}
Expand All @@ -200,7 +231,7 @@ impl ProbeSession {
let data_packet_size = chip_family.data_packet_size();

if chip_family.support_flash_protect() {
self.protect_flash(false)?;
self.unprotect_flash()?;
}

let data = data.to_vec();
Expand Down

0 comments on commit 1cadff1

Please sign in to comment.