|
1 |
| -use std::{ |
2 |
| - fs::{self, read, read_to_string}, |
3 |
| - mem::swap, |
4 |
| - path::PathBuf, |
5 |
| - str::FromStr, |
6 |
| -}; |
| 1 | +use std::{fs, mem::swap, path::PathBuf, str::FromStr}; |
7 | 2 |
|
8 | 3 | use clap::{AppSettings, IntoApp, Parser};
|
9 | 4 | use espflash::{
|
10 | 5 | cli::{
|
11 | 6 | board_info,
|
12 | 7 | clap::{ConnectOpts, FlashOpts},
|
13 |
| - connect, |
| 8 | + connect, flash_elf_image, |
14 | 9 | monitor::monitor,
|
15 | 10 | save_elf_as_image,
|
16 | 11 | },
|
17 |
| - Chip, Config, Error, ImageFormatId, PartitionTable, |
| 12 | + Chip, Config, ImageFormatId, |
18 | 13 | };
|
19 | 14 | use miette::{IntoDiagnostic, Result, WrapErr};
|
20 | 15 |
|
@@ -72,68 +67,43 @@ fn main() -> Result<()> {
|
72 | 67 | use SubCommand::*;
|
73 | 68 |
|
74 | 69 | match subcommand {
|
75 |
| - BoardInfo(matches) => board_info(matches, config), |
76 |
| - SaveImage(matches) => save_image(matches), |
| 70 | + BoardInfo(opts) => board_info(opts, config), |
| 71 | + SaveImage(opts) => save_image(opts), |
77 | 72 | }
|
78 | 73 | } else {
|
79 | 74 | flash(opts, config)
|
80 | 75 | }
|
81 | 76 | }
|
82 | 77 |
|
83 | 78 | fn flash(opts: Opts, config: Config) -> Result<()> {
|
84 |
| - let ram = opts.flash_opts.ram; |
85 |
| - let bootloader_path = opts.flash_opts.bootloader; |
86 |
| - let partition_table_path = opts.flash_opts.partition_table; |
87 |
| - let image_format_string = opts.format; |
88 |
| - |
89 |
| - let elf = match opts.image { |
90 |
| - Some(elf) => elf, |
91 |
| - _ => { |
92 |
| - Opts::into_app().print_help().ok(); |
93 |
| - return Ok(()); |
94 |
| - } |
95 |
| - }; |
96 |
| - |
97 | 79 | let mut flasher = connect(&opts.connect_opts, &config)?;
|
| 80 | + flasher.board_info()?; |
98 | 81 |
|
99 |
| - let input_bytes = read(&elf) |
100 |
| - .into_diagnostic() |
101 |
| - .wrap_err_with(|| format!("Failed to open elf image \"{}\"", elf))?; |
| 82 | + let elf = if let Some(elf) = opts.image { |
| 83 | + elf |
| 84 | + } else { |
| 85 | + Opts::into_app().print_help().ok(); |
| 86 | + return Ok(()); |
| 87 | + }; |
| 88 | + |
| 89 | + // Read the ELF data from the build path and load it to the target. |
| 90 | + let elf_data = fs::read(&elf).into_diagnostic()?; |
102 | 91 |
|
103 |
| - if ram { |
104 |
| - flasher.load_elf_to_ram(&input_bytes)?; |
| 92 | + if opts.flash_opts.ram { |
| 93 | + flasher.load_elf_to_ram(&elf_data)?; |
105 | 94 | } else {
|
106 |
| - let bootloader = bootloader_path |
107 |
| - .as_deref() |
108 |
| - .map(read) |
109 |
| - .transpose() |
110 |
| - .into_diagnostic() |
111 |
| - .wrap_err_with(|| { |
112 |
| - format!( |
113 |
| - "Failed to open bootloader image \"{}\"", |
114 |
| - bootloader_path.unwrap().display() |
115 |
| - ) |
116 |
| - })?; |
117 |
| - let image_format = image_format_string |
| 95 | + let bootloader = opts.flash_opts.bootloader.as_deref(); |
| 96 | + let partition_table = opts.flash_opts.partition_table.as_deref(); |
| 97 | + |
| 98 | + let image_format = opts |
| 99 | + .format |
118 | 100 | .as_deref()
|
119 | 101 | .map(ImageFormatId::from_str)
|
120 | 102 | .transpose()?;
|
121 |
| - let partition_table = partition_table_path |
122 |
| - .as_deref() |
123 |
| - .map(|path| { |
124 |
| - let table = read_to_string(path)?; |
125 |
| - PartitionTable::try_from_str(&table).map_err(Error::from) |
126 |
| - }) |
127 |
| - .transpose() |
128 |
| - .into_diagnostic() |
129 |
| - .wrap_err_with(|| { |
130 |
| - format!( |
131 |
| - "Failed to load partition table \"{}\"", |
132 |
| - partition_table_path.unwrap().display() |
133 |
| - ) |
134 |
| - })?; |
135 |
| - flasher.load_elf_to_flash_with_format( |
136 |
| - &input_bytes, |
| 103 | + |
| 104 | + flash_elf_image( |
| 105 | + &mut flasher, |
| 106 | + &elf_data, |
137 | 107 | bootloader,
|
138 | 108 | partition_table,
|
139 | 109 | image_format,
|
|
0 commit comments