Skip to content

Commit f36c41d

Browse files
committed
readme updates and verfication code rework
1 parent 81ec8a8 commit f36c41d

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = """
44
A utility for reading and writing flash contents on Sinowealth 8051-based devices
55
"""
66
repository = "https://github.com/carlossless/sinowealth-kb-tool"
7-
version = "0.0.1"
7+
version = "0.0.2"
88
edition = "2021"
99
license = "MIT"
1010

src/isp.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use log::*;
22
use std::{thread, time};
33

44
use crate::HidDevice;
5+
use crate::VerificationError;
56

67
use super::part::*;
78
use super::util;
@@ -30,6 +31,8 @@ const CMD_ERASE: u8 = 0x45;
3031
const XFER_READ_PAGE: u8 = 0x72;
3132
const XFER_WRITE_PAGE: u8 = 0x77;
3233

34+
const LJMP_OPCODE: u8 = 0x02;
35+
3336
#[derive(Debug, Clone)]
3437
pub enum ReadType {
3538
Normal,
@@ -92,7 +95,7 @@ impl ISPDevice<'static> {
9295
};
9396
}
9497

95-
pub fn write_cycle(&self, firmware: &mut Vec<u8>) {
98+
pub fn write_cycle(&self, firmware: &mut Vec<u8>) -> Result<(), VerificationError> {
9699
let length = firmware.len();
97100

98101
assert_eq!(
@@ -101,23 +104,22 @@ impl ISPDevice<'static> {
101104
self.part.flash_size, length
102105
);
103106

104-
// this is a bit of an arcane part and I'm not certain why this happens
105-
firmware.copy_within(0..3, length - 5);
106-
firmware[(length - 5)..(length - 2)].fill(0);
107-
108107
self.erase();
109108
self.write(&firmware);
110109
let written = self.read(0, self.part.flash_size);
111110

111+
// ARCANE: the ISP will copy the LJMP instruction (if existing) from the end to the very start of memory.
112+
// We need to make the modifications to the expected payload to account for this.
113+
if firmware[length - 5] == LJMP_OPCODE {
114+
firmware[0] = LJMP_OPCODE;
115+
}
116+
firmware.copy_within((length - 4)..(length - 2), 1); // Copy LJMP address
117+
firmware[(length - 5)..(length - 2)].fill(0); // Cleanup
118+
112119
info!("Verifying...");
113-
match util::verify(&firmware, &written) {
114-
Err(e) => {
115-
error!("{}", e.to_message());
116-
return;
117-
}
118-
Ok(_) => {}
119-
};
120+
util::verify(&firmware, &written)?;
120121
self.finalize();
122+
return Ok(());
121123
}
122124

123125
pub fn erase_cycle(&self) {
@@ -217,7 +219,6 @@ impl ISPDevice<'static> {
217219
thread::sleep(time::Duration::from_millis(2000));
218220
}
219221

220-
// TODO: verify what this command does and if it's actually needed
221222
fn finalize(&self) {
222223
info!("Finalizing...");
223224
let cmd: [u8; COMMAND_LENGTH] = [

src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clap::*;
22
use ihex::*;
33
use ihex_ext::*;
4+
use log::*;
45
use simple_logger::SimpleLogger;
5-
use std::fs;
6+
use std::{fs, process};
67

78
mod part;
89
pub use part::*;
@@ -26,7 +27,7 @@ fn cli() -> Command {
2627
.subcommand(
2728
Command::new("read")
2829
.short_flag('r')
29-
.about("Read flash contents. (ihex)")
30+
.about("Read flash contents. (Intel HEX)")
3031
.arg(arg!(output_file: <OUTPUT_FILE> "file to write flash contents to"))
3132
.arg(
3233
arg!(-p --part <PART>)
@@ -42,7 +43,7 @@ fn cli() -> Command {
4243
.subcommand(
4344
Command::new("write")
4445
.short_flag('w')
45-
.about("Write file (ihex) into flash.")
46+
.about("Write file (Intel HEX) into flash.")
4647
.arg(arg!(input_file: <INPUT_FILE> "payload to write into flash"))
4748
.arg(
4849
arg!(-p --part <PART>)
@@ -107,7 +108,13 @@ fn main() {
107108

108109
let (mut firmware, _) = load_file_vec(input_file, part.flash_size, 0).unwrap();
109110

110-
ISPDevice::new(part).write_cycle(&mut firmware);
111+
match ISPDevice::new(part).write_cycle(&mut firmware) {
112+
Err(e) => {
113+
error!("{}", e.to_message());
114+
process::exit(1);
115+
}
116+
Ok(_) => {}
117+
};
111118
}
112119
Some(("erase", sub_matches)) => {
113120
let part_name = sub_matches

0 commit comments

Comments
 (0)