-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathlib.rs
More file actions
135 lines (123 loc) · 4.07 KB
/
Copy pathlib.rs
File metadata and controls
135 lines (123 loc) · 4.07 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
// 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 auxflash`
//!
//! Tools to interact with the auxiliary flash, described in RFD 311.
//!
//! This subcommand should be rarely used; `humility flash` will automatically
//! program auxiliary flash when needed.
use anyhow::Result;
use clap::{CommandFactory, Parser};
use colored::Colorize;
use humility_cli::ExecutionContext;
use humility_auxflash::AuxFlashHandler;
use humility_cmd::Command;
#[derive(Parser, Debug)]
#[clap(name = "auxflash", about = env!("CARGO_PKG_DESCRIPTION"))]
struct AuxFlashArgs {
/// sets timeout
#[clap(
long, short = 'T', default_value_t = 15000, value_name = "timeout_ms",
value_parser = parse_int::parse::<u32>
)]
timeout: u32,
#[clap(subcommand)]
cmd: AuxFlashCommand,
}
#[derive(Parser, Debug)]
enum AuxFlashCommand {
/// Prints the auxiliary flash status
Status {
#[clap(long, short)]
verbose: bool,
},
Erase {
#[clap(long, short)]
slot: u32,
},
Read {
#[clap(long, short)]
slot: u32,
/// Number of bytes to read (defaults to 1M)
#[clap(long, short)]
count: Option<usize>,
output: String,
},
Write {
#[clap(long, short)]
slot: u32,
#[clap(long, short = 'F')]
force: bool,
input: Option<String>,
},
}
////////////////////////////////////////////////////////////////////////////////
fn auxflash_status(mut worker: AuxFlashHandler, verbose: bool) -> Result<()> {
let slot_count = worker.slot_count()?;
let active_slot = worker.active_slot()?;
println!(" {} | {}", "slot".bold(), "status".bold());
println!("------|----------------------------");
for i in 0..slot_count {
print!(" {:>3} | ", i);
match worker.slot_status(i) {
Err(e) => {
println!("Error: {}", e.to_string().red());
}
Ok(None) => println!("{}", "Missing checksum".yellow()),
Ok(Some(v)) => {
if active_slot == Some(i) {
print!("{} (", "Active".green());
} else {
print!("{} (", "Valid ".blue());
}
if verbose {
for byte in v {
print!("{:0>2x}", byte);
}
} else {
for byte in &v[0..4] {
print!("{:0>2x}", byte);
}
print!("...");
}
println!(")");
}
}
}
Ok(())
}
fn auxflash(context: &mut ExecutionContext) -> Result<()> {
let subargs = AuxFlashArgs::try_parse_from(&context.cli.cmd)?;
let hubris = &context.cli.archive()?;
let core = &mut *context.cli.attach_live_booted(hubris)?;
let mut worker = AuxFlashHandler::new(hubris, core, subargs.timeout)?;
match subargs.cmd {
AuxFlashCommand::Status { verbose } => {
auxflash_status(worker, verbose)?;
}
AuxFlashCommand::Erase { slot } => {
worker.slot_erase(slot)?;
humility::msg!("done erasing slot {slot}");
}
AuxFlashCommand::Read { slot, output, count } => {
let data = worker.auxflash_read(slot, count)?;
std::fs::write(output, data)?;
}
AuxFlashCommand::Write { slot, input, force } => match input {
Some(input) => {
let data = std::fs::read(input)?;
worker.auxflash_write(slot, &data, force)?;
}
None => {
// If the user didn't specify an image to flash on the
// command line, then attempt to pull it from the image.
worker.auxflash_write_from_archive(slot, force)?;
}
},
}
Ok(())
}
pub fn init() -> Command {
Command { app: AuxFlashArgs::command(), name: "auxflash", run: auxflash }
}