-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathlib.rs
More file actions
301 lines (265 loc) · 10.4 KB
/
Copy pathlib.rs
File metadata and controls
301 lines (265 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! ## `humility validate`
//!
//! `humility validate` uses the Hubris `validate` task to validate the
//! correct presence of devices as described by the application TOML. To
//! view all devices, use the `--list` option; to validate them, run
//! without any additional arguments:
//!
//! ```console
//! $ humility validate
//! humility: attached via ST-Link V3
//! ID VALIDATION C P MUX ADDR DEVICE DESCRIPTION
//! 0 removed 2 F - 0x48 tmp117 Southwest temperature sensor
//! 1 removed 2 F - 0x49 tmp117 South temperature sensor
//! 2 removed 2 F - 0x4a tmp117 Southeast temperature sensor
//! 3 present 2 F - 0x70 pca9545 U.2 ABCD mux
//! 4 present 2 F - 0x71 pca9545 U.2 EFGH mux
//! 5 present 2 F - 0x72 pca9545 U.2 IJ/FRUID mux
//! 6 timeout 2 B - 0x73 pca9545 M.2 mux
//! 7 timeout 2 B 1:4 0x4c tmp451 T6 temperature sensor
//! 8 validated 3 H - 0x24 tps546b24a A2 3.3V rail
//! 9 validated 3 H - 0x26 tps546b24a A0 3.3V rail
//! 10 validated 3 H - 0x27 tps546b24a A2 5V rail
//! 11 validated 3 H - 0x29 tps546b24a A2 1.8V rail
//! 12 present 3 H - 0x3a max5970 M.2 hot plug controller
//! 13 absent 3 H - 0x4c sbtsi CPU temperature sensor
//! 14 present 3 H - 0x58 idt8a34003 Clock generator
//! 15 validated 3 H - 0x5a raa229618 CPU power controller
//! 16 validated 3 H - 0x5b raa229618 SoC power controller
//! 17 validated 3 H - 0x5c isl68224 DIMM/SP3 1.8V A0 power controller
//! 18 validated 4 F - 0x10 adm1272 Fan hot swap controller
//! 19 validated 4 F - 0x14 adm1272 Sled hot swap controller
//! 20 validated 4 F - 0x20 max31790 Fan controller
//! 21 validated 4 F - 0x25 tps546b24a T6 power controller
//! 22 removed 4 F - 0x48 tmp117 Northeast temperature sensor
//! 23 removed 4 F - 0x49 tmp117 North temperature sensor
//! 24 validated 4 F - 0x4a tmp117 Northwest temperature sensor
//! 25 validated 4 F - 0x67 bmr491 Intermediate bus converter
//! ```
//!
use anyhow::{Result, anyhow};
use clap::Parser;
use colored::Colorize;
use hif::*;
use humility::hubris::*;
use humility_cli::{ExecutionContext, humility_cmd};
use humility_hiffy::{HiffyContext, IpcError};
use humility_i2c::I2cArgs;
use humility_idol::{self as idol, HubrisIdol};
#[derive(Parser, Debug)]
#[clap(name = "validate", about = env!("CARGO_PKG_DESCRIPTION"))]
pub struct ValidateArgs {
/// sets timeout
#[clap(
long, short = 'T', default_value_t = 5000, value_name = "timeout_ms",
value_parser = parse_int::parse::<u64>,
)]
timeout: u64,
/// list all devices to be validated
#[clap(long, short)]
list: bool,
/// specifies an I2C controller to validate
#[clap(long, short, value_name = "controller",
value_parser = parse_int::parse::<u8>,
)]
controller: Option<u8>,
/// specifies an I2C bus by name to validate
#[clap(long, short, value_name = "bus",
conflicts_with_all = &["port", "controller"]
)]
bus: Option<String>,
/// specifies an I2C controller port to validate
#[clap(long, short, value_name = "port", requires = "controller")]
port: Option<String>,
/// specifies I2C multiplexer and segment to validate
#[clap(long, short, value_name = "mux:segment", requires = "controller")]
mux: Option<String>,
/// specifies a device name to validate
#[clap(long, short = 'd', value_name = "device")]
device: Option<String>,
/// specifies a device identifier to validate
#[clap(long, short = 'i', value_name = "id",
conflicts_with_all = &["port", "controller", "bus", "device"]
)]
id: Option<usize>,
}
const REFDES_HDR: &str = "REFDES";
const DEVICE_HDR: &str = "DEVICE";
const NO_REFDES: &str = "-";
fn list(hubris: &HubrisArchive, hargs: &Option<I2cArgs>) -> Result<()> {
let refdes_len = hubris
.manifest
.fmt_meta
.max_refdes_len
.max(REFDES_HDR.len())
.max(NO_REFDES.len());
let device_len =
hubris.manifest.fmt_meta.max_device_len.max(DEVICE_HDR.len());
println!(
"{:2} {:refdes_len$} {:>2} {:2} {:3} {:4} {:device_len$} DESCRIPTION",
"ID", REFDES_HDR, "C", "P", "MUX", "ADDR", DEVICE_HDR,
);
for (ndx, device) in hubris.manifest.i2c_devices.iter().enumerate() {
if let Some(hargs) = hargs
&& !hargs.matches_device(device)
{
continue;
}
let mux = match (device.mux, device.segment) {
(Some(m), Some(s)) => format!("{}:{}", m, s),
(None, None) => "-".to_string(),
(_, _) => "?:?".to_string(),
};
println!(
"{:2} {:refdes_len$} {:2} {:2} {:3} 0x{:02x} {:device_len$} {}",
ndx,
device.refdes.as_deref().unwrap_or(NO_REFDES),
device.controller,
device.port.name,
mux,
device.address,
device.device,
device.description
);
}
Ok(())
}
fn validate(
subargs: ValidateArgs,
context: &mut ExecutionContext,
) -> Result<()> {
let hubris = &context.cli.archive()?;
let core = &mut *context.cli.attach_live_booted(hubris)?;
let hargs = if subargs.bus.is_some() || subargs.controller.is_some() {
Some(I2cArgs::parse(
hubris,
&subargs.bus,
subargs.controller,
&subargs.port,
&subargs.mux,
&None,
)?)
} else {
None
};
if subargs.list {
list(hubris, &hargs)?;
return Ok(());
}
let timeout = std::time::Duration::from_millis(subargs.timeout);
let mut context = HiffyContext::new(hubris, core, timeout)?;
let op = hubris.get_idol_command("Validate.validate_i2c")?;
let mut ops = vec![];
let mut devices = vec![];
for (ndx, device) in hubris.manifest.i2c_devices.iter().enumerate() {
if let Some(id) = subargs.id
&& ndx != id
{
continue;
}
if let Some(ref hargs) = hargs {
if !hargs.matches_device(device) {
continue;
}
} else if let Some(ref d) = subargs.device
&& device.device != *d
{
continue;
}
devices.push((ndx, device));
let payload =
op.payload(&[("index", idol::IdolArgument::Scalar(ndx as u64))])?;
context.idol_call_ops(&op, &payload, &mut ops)?;
}
ops.push(Op::Done);
let results = context.run(core, ops.as_slice(), None)?;
let fmt = HubrisPrintFormat {
newline: false,
hex: true,
..HubrisPrintFormat::default()
};
let refdes_len = hubris
.manifest
.fmt_meta
.max_refdes_len
.max(REFDES_HDR.len())
.max(NO_REFDES.len());
let device_len =
hubris.manifest.fmt_meta.max_device_len.max(DEVICE_HDR.len());
println!(
"{:2} {:refdes_len$} {:11} {:>2} {:2} {:3} {:4} {:device_len$} DESCRIPTION",
"ID", REFDES_HDR, "VALIDATION", "C", "P", "MUX", "ADDR", DEVICE_HDR,
);
let ok = hubris.lookup_enum(op.ok)?;
for (rndx, (ndx, device)) in devices.iter().enumerate() {
let result = match &results[rndx] {
Ok(val) => {
// TODO: assumes discriminant is a u8. Since this is using Hiffy
// call results instead of looking at a Rust value in memory,
// it's not clear from context what changes would be required to
// fix this.
if let Some(variant) =
ok.lookup_variant_by_tag(Tag::from(val[0]))
{
Ok(match variant.name.as_str() {
"Present" => "present".yellow(),
"Validated" => "validated".green(),
_ => format!("<{}>", variant.name).cyan(),
})
} else {
Ok(hubris.printfmt(val, op.ok, fmt)?.white())
}
}
Err(IpcError::Error(e)) => {
if let idol::IdolError::CLike(err) = op.error {
// TODO: assumes discriminant is a u8. Since this is using
// Hiffy call results instead of looking at a Rust value in
// memory, it's not clear from context what changes would be
// required to fix this.
Ok(match err.lookup_variant_by_tag(Tag::from(*e)) {
Some(variant) => match variant.name.as_str() {
"NotPresent" => {
if device.removable {
"removed".blue()
} else {
"absent".red()
}
}
"BadValidation" => "failed".red(),
"DeviceTimeout" => "timeout".red(),
"DeviceError" => "error".red(),
"Unavailable" => "unavailable".yellow(),
_ => format!("<{}>", variant.name).red(),
},
None => format!("Err(0x{:x?})", e).red(),
})
} else {
Err(anyhow!("unexpected error type {:?}", op.error))
}
}
Err(IpcError::ServerDied(_)) => Ok("server died".red()),
}?;
let mux = match (device.mux, device.segment) {
(Some(m), Some(s)) => format!("{}:{}", m, s),
(None, None) => "-".to_string(),
(_, _) => "?:?".to_string(),
};
println!(
"{:2} {:refdes_len$} {:11} {:2} {:2} {:3} 0x{:02x} {:device_len$} {}",
ndx,
device.refdes.as_deref().unwrap_or(NO_REFDES),
result,
device.controller,
device.port.name,
mux,
device.address,
device.device,
device.description
);
}
Ok(())
}
humility_cmd!(ValidateArgs, validate);