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
use failure::{format_err, Error};
use std::process::Command;
use std::str::FromStr;
pub const VAR_KUBOS_CURR_VERSION: &str = "kubos_curr_version";
pub const VAR_KUBOS_PREV_VERSION: &str = "kubos_prev_version";
pub const VAR_KUBOS_INITIAL_DEPLOY: &str = "kubos_initial_deploy";
const PRINTENV_PATH: &str = "/usr/sbin/fw_printenv";
#[derive(Default)]
pub struct UBootVars {
cmd_path: String,
}
impl UBootVars {
pub fn new() -> Self {
Self::new_from_path(PRINTENV_PATH)
}
pub fn new_from_path(path: &str) -> Self {
Self {
cmd_path: String::from(path),
}
}
fn get(&self, name: &str) -> Result<String, Error> {
let output = match Command::new(&self.cmd_path).args(&["-n", name]).output() {
Ok(output) => output,
Err(_) => return Err(format_err!("Failed to execute: {}", self.cmd_path)),
};
if !output.status.success() {
Err(format_err!("Var not found: {}", name))
} else {
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
}
pub fn get_u32(&self, name: &str) -> Option<u32> {
match self.get(name) {
Ok(v) => match u32::from_str(&v) {
Ok(val) => Some(val),
Err(_) => None,
},
Err(_) => None,
}
}
pub fn get_str(&self, name: &str) -> Option<String> {
match self.get(name) {
Ok(v) => Some(v),
Err(_) => None,
}
}
pub fn get_bool(&self, name: &str) -> Option<bool> {
match self.get(name) {
Ok(v) => match v.to_lowercase().as_ref() {
"t" | "true" | "1" | "y" | "yes" => Some(true),
_ => Some(false),
},
Err(_) => None,
}
}
}