|
3 | 3 | // file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
4 | 4 |
|
5 | 5 | use serde::Serialize;
|
6 |
| -use std::path::{Path, PathBuf}; |
| 6 | +use std::path::Path; |
7 | 7 |
|
8 |
| -// |
9 |
| -// We allow for enough information to be put in the archive for the image to |
10 |
| -// be flashed based only on the archive (e.g., by Humility). Because flashing |
11 |
| -// is itself a bit of a mess (requiring different programs for different |
12 |
| -// targets), this is a bit gritty (e.g., any required external configuration |
13 |
| -// files must themselves put in the archive). If these structures need to |
14 |
| -// change, be sure to make corresponding changes to Humility. |
15 |
| -// |
16 |
| -#[derive(Debug, Serialize)] |
17 |
| -pub enum FlashProgram { |
18 |
| - PyOcd(Vec<FlashArgument>), |
19 |
| - OpenOcd(FlashProgramConfig), |
20 |
| -} |
21 |
| - |
22 |
| -// |
23 |
| -// Enum describing flash programs configuration (e.g., "openocd.cfg" for |
24 |
| -// OpenOCD), either as a path in the file system or with the entire contents. |
25 |
| -// |
26 |
| -#[derive(Debug, Serialize)] |
27 |
| -pub enum FlashProgramConfig { |
28 |
| - Path(PathBuf), |
29 |
| - Payload(String), |
30 |
| -} |
31 |
| - |
32 |
| -// |
33 |
| -// An enum describing a single command-line argument to the flash program. |
34 |
| -// |
35 |
| -#[derive(Debug, Serialize)] |
36 |
| -pub enum FlashArgument { |
37 |
| - // A direct string |
38 |
| - Direct(String), |
39 |
| - |
40 |
| - // The filesystem path of the binary flash payload itself |
41 |
| - Payload, |
42 |
| - |
43 |
| - // A single argument consisting of a prefix and a suffix. When the |
44 |
| - // argument is processed, a single argument should be generated consisting |
45 |
| - // of the prefix, the path of the flash, and the suffix, all joined by |
46 |
| - // spaces. |
47 |
| - FormattedPayload(String, String), |
48 |
| - |
49 |
| - // The filesystem path of the flash program configuration |
50 |
| - Config, |
51 |
| -} |
52 |
| - |
53 |
| -#[derive(Debug, Serialize)] |
| 8 | +#[derive(Debug, Serialize, Default)] |
54 | 9 | pub struct FlashConfig {
|
| 10 | + /// The name used by probe-rs to identify the chip. |
55 | 11 | chip: Option<String>,
|
56 |
| - program: FlashProgram, |
57 |
| - args: Vec<FlashArgument>, |
58 |
| -} |
59 |
| - |
60 |
| -impl FlashProgramConfig { |
61 |
| - fn new(path: PathBuf) -> Self { |
62 |
| - FlashProgramConfig::Path(path) |
63 |
| - } |
64 | 12 | }
|
65 | 13 |
|
66 | 14 | impl FlashConfig {
|
67 |
| - fn new(program: FlashProgram) -> Self { |
68 |
| - FlashConfig { |
69 |
| - chip: None, |
70 |
| - program, |
71 |
| - args: vec![], |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - // |
76 |
| - // Add a command-line argument to the flash program |
77 |
| - // |
78 |
| - fn arg<'a>(&'a mut self, val: &str) -> &'a mut Self { |
79 |
| - self.args.push(FlashArgument::Direct(val.to_string())); |
80 |
| - self |
81 |
| - } |
82 |
| - |
83 | 15 | //
|
84 | 16 | // Set the chip
|
85 | 17 | //
|
86 | 18 | fn set_chip(&mut self, val: &str) -> &mut Self {
|
87 | 19 | self.chip = Some(val.to_string());
|
88 | 20 | self
|
89 | 21 | }
|
90 |
| - |
91 |
| - // |
92 |
| - // Add the path to the payload as an argument to the flash program |
93 |
| - // |
94 |
| - fn payload(&mut self) -> &mut Self { |
95 |
| - self.args.push(FlashArgument::Payload); |
96 |
| - self |
97 |
| - } |
98 |
| - |
99 |
| - // |
100 |
| - // Add a formatted payload as a single argument to the flash program. The |
101 |
| - // argument will consists of the specified prefix, followed by the path to |
102 |
| - // the payload, followed by the specified suffix. |
103 |
| - // |
104 |
| - fn formatted_payload<'a>( |
105 |
| - &'a mut self, |
106 |
| - prefix: &str, |
107 |
| - suffix: &str, |
108 |
| - ) -> &'a mut Self { |
109 |
| - self.args.push(FlashArgument::FormattedPayload( |
110 |
| - prefix.to_string(), |
111 |
| - suffix.to_string(), |
112 |
| - )); |
113 |
| - self |
114 |
| - } |
115 |
| - |
116 |
| - // |
117 |
| - // Add a flasher configuration file as an argument to the flash program |
118 |
| - // |
119 |
| - fn config(&mut self) -> &mut Self { |
120 |
| - self.args.push(FlashArgument::Config); |
121 |
| - self |
122 |
| - } |
123 |
| - |
124 |
| - // |
125 |
| - // Slurp in any flash program configuration file and flatten it into |
126 |
| - // our overall configuration |
127 |
| - // |
128 |
| - pub fn flatten(&mut self) -> anyhow::Result<()> { |
129 |
| - if let FlashProgram::OpenOcd(FlashProgramConfig::Path(path)) = |
130 |
| - &self.program |
131 |
| - { |
132 |
| - let p: PathBuf = path.iter().collect(); |
133 |
| - let text = std::fs::read_to_string(p)?; |
134 |
| - self.program = |
135 |
| - FlashProgram::OpenOcd(FlashProgramConfig::Payload(text)); |
136 |
| - } |
137 |
| - |
138 |
| - Ok(()) |
139 |
| - } |
140 | 22 | }
|
141 | 23 |
|
142 | 24 | pub fn config(
|
143 | 25 | board: &str,
|
144 |
| - chip_dir: &Path, |
| 26 | + _chip_dir: &Path, |
145 | 27 | ) -> anyhow::Result<Option<FlashConfig>> {
|
146 |
| - let mut flash = match board { |
147 |
| - "lpcxpresso55s69" |
148 |
| - | "rot-carrier-1" |
149 |
| - | "rot-carrier-2" |
150 |
| - | "oxide-rot-1" |
151 |
| - | "oxide-rot-1-selfsigned" => { |
152 |
| - let chip = if board == "rot-carrier-1" { |
153 |
| - "lpc55s28" |
154 |
| - } else { |
155 |
| - "lpc55s69" |
156 |
| - }; |
157 |
| - |
158 |
| - let mut args = vec![]; |
159 |
| - |
160 |
| - for arg in ["reset", "-t", chip].iter() { |
161 |
| - args.push(FlashArgument::Direct(arg.to_string())); |
162 |
| - } |
163 |
| - |
164 |
| - let mut flash = FlashConfig::new(FlashProgram::PyOcd(args)); |
165 |
| - |
166 |
| - flash |
167 |
| - .arg("flash") |
168 |
| - .arg("-t") |
169 |
| - .arg(chip) |
170 |
| - .arg("--format") |
171 |
| - .arg("hex") |
172 |
| - .payload(); |
173 |
| - |
174 |
| - flash |
175 |
| - } |
176 |
| - |
177 |
| - "stm32f3-discovery" | "stm32f4-discovery" | "nucleo-h743zi2" |
178 |
| - | "nucleo-h753zi" | "gemini-bu-1" | "gimletlet-1" | "gimletlet-2" |
179 |
| - | "gimlet-b" | "gimlet-c" | "gimlet-d" | "gimlet-e" | "gimlet-f" |
180 |
| - | "psc-b" | "psc-c" | "sidecar-b" | "sidecar-c" | "sidecar-d" |
181 |
| - | "stm32g031-nucleo" | "donglet-g030" | "donglet-g031" |
182 |
| - | "oxcon2023g0" | "stm32g070-nucleo" | "stm32g0b1-nucleo" |
183 |
| - | "medusa-a" | "grapefruit" => { |
184 |
| - let cfg = FlashProgramConfig::new(chip_dir.join("openocd.cfg")); |
185 |
| - |
186 |
| - let mut flash = FlashConfig::new(FlashProgram::OpenOcd(cfg)); |
187 |
| - |
188 |
| - flash |
189 |
| - .arg("-f") |
190 |
| - .config() |
191 |
| - .arg("-c") |
192 |
| - .formatted_payload("program", "verify reset") |
193 |
| - .arg("-c") |
194 |
| - .arg("exit"); |
195 |
| - |
196 |
| - flash |
197 |
| - } |
198 |
| - _ => { |
199 |
| - eprintln!("Warning: unrecognized board, won't know how to flash."); |
200 |
| - return Ok(None); |
201 |
| - } |
202 |
| - }; |
| 28 | + let mut flash = FlashConfig::default(); |
203 | 29 |
|
204 | 30 | flash.set_chip(chip_name(board)?);
|
205 | 31 |
|
|
0 commit comments