Skip to content

Commit

Permalink
refactor: memory dump to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Nov 19, 2023
1 parent 7302a56 commit 0f6f25e
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::File, io::Write, path::Path, thread::sleep, time::Duration};
use std::{thread::sleep, time::Duration};

use anyhow::Result;
use wlink::{
Expand Down Expand Up @@ -58,12 +58,8 @@ enum Commands {
length: u32,

/// Write the dumped memory region to a file
#[arg(long, default_value = "", hide_default_value = true)]
file: String,

/// If --file argument is specified and the file already exists, overwrite it
#[arg(long, default_value = "false")]
overwrite: bool,
#[arg(short = 'o', long = "out")]
filename: Option<String>,
},
/// Dump registers
Regs {},
Expand Down Expand Up @@ -267,11 +263,10 @@ fn main() -> Result<()> {
Dump {
address,
length,
file,
overwrite,
filename,
} => {
log::info!(
"Read memory from 0x{:08x} to 0x{:08x}, file: {file}",
"Read memory from 0x{:08x} to 0x{:08x}",
address,
address + length
);
Expand All @@ -280,28 +275,23 @@ fn main() -> Result<()> {
let mut algo = wlink::dmi::Algorigthm::new(&mut probe);
let out = algo.read_memory(address, length)?;

if file != "" {
if !overwrite && Path::new(file.as_str()).exists() {
log::error!("File {file} already exists and --overwrite option is not specified");
} else {
let mut file_h = File::create(&file).expect("Unable to create file");
file_h.write_all(&out).expect("Unable to write data");
log::info!("{} bytes written to file {}", out.len(), &file);
}
if let Some(fname) = filename {
std::fs::write(&fname, &out)?;
log::info!("{} bytes written to file {}", length, &fname);
} else {
println!(
"{}",
nu_pretty_hex::config_hex(
&out,
nu_pretty_hex::HexConfig {
title: true,
ascii: true,
address_offset: address as _,
..Default::default()
},
)
);
}

println!(
"{}",
nu_pretty_hex::config_hex(
&out,
nu_pretty_hex::HexConfig {
title: true,
ascii: true,
address_offset: address as _,
..Default::default()
},
)
);
}
Regs {} => {
log::info!("Dump GPRs");
Expand Down

0 comments on commit 0f6f25e

Please sign in to comment.