diff --git a/README.md b/README.md index cad53ee..3dd8da2 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/hpm_isp/src/main.rs b/hpm_isp/src/main.rs index e77a554..3623b61 100644 --- a/hpm_isp/src/main.rs +++ b/hpm_isp/src/main.rs @@ -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 { @@ -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, }, } @@ -97,19 +99,25 @@ fn main() -> Result<(), Box> { { 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