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

Use hpm_isp.bin by default if it exists when using flash command #12

Merged
merged 1 commit into from
Mar 16, 2024
Merged
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ sudo udevadm control --reload-rules
# Write to flash (use default memory config)
hpm_isp flash 0 write 0x400 flash.bin
# Write to flash (use custom memory config)
# Note: if hpm_isp.bin exists in the working directory, it will be used by default.
# So you don't need to pass -c option explicitly.
hpm_isp flash -c hpm_isp.bin 0 write 0x400 flash.bin
# Read from flash
hpm_isp read -c hpm_isp.bin 0 read 0x0 0x4000 flash.bin
# Use config wizard to generate config file (save to hpm_isp.bin)
hpm_isp read 0 read 0x0 0x4000 flash.bin
# Use config wizard to generate config file (save as hpm_isp.bin)
hpm_isp wizard
```

Expand Down
34 changes: 21 additions & 13 deletions hpm_isp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use hpm_isp::{
};
use hpm_rt::XpiNorConfigurationOption;

const DEFAULT_CONFIG_FILE: &'static str = "hpm_isp.bin";

#[derive(Parser)]
#[clap(version, about)]
struct Cli {
Expand All @@ -41,7 +43,7 @@ enum Commands {
/// Command of wizard to generate memory config file
Wizard {
/// Path of memory config file
#[clap(short, long, default_value = "hpm_isp.bin")]
#[clap(short, long, default_value = DEFAULT_CONFIG_FILE)]
path: PathBuf,
},
}
Expand Down Expand Up @@ -97,19 +99,25 @@ fn main() -> Result<(), Box<dyn Error>> {
{
let device = hid::HpmDevice::open().or_else(|_| Err("can't open HPMicro usb device"))?;
let mut memory_config_bin = Vec::new();

if let Some(path) = config {
let mut config_file = fs::File::open(path)?;

memory_config_bin.resize(12, 0);
config_file.read_exact(memory_config_bin.as_mut_slice())?;
if memory_config_bin[3] != 0xFC || memory_config_bin[2] != 0xF9 {
return Err("Invalid memory config file".into());
let is_default = config.is_none();
let config_path = config.unwrap_or(DEFAULT_CONFIG_FILE.into());

match fs::File::open(config_path) {
Ok(mut config_file) => {
memory_config_bin.resize(12, 0);
config_file.read_exact(memory_config_bin.as_mut_slice())?;
if memory_config_bin[3] != 0xFC || memory_config_bin[2] != 0xF9 {
return Err("Invalid memory config file".into());
}
}
// Use default config file and it isn't exists
Err(_) if is_default => {
let xpi_config = XpiNorConfigurationOption::new();
xpi_config.write(&mut memory_config_bin)?;
}
Err(err) => {
Err(err)?;
}
} else {
let xpi_config = XpiNorConfigurationOption::new();

xpi_config.write(&mut memory_config_bin)?;
}

// Config memory
Expand Down
Loading