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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
use std::process::Command;
use std::str::FromStr;
use std::{fs, io};
use tonic::{Request, Response, Status, transport::Server};
use crate::devinfod::dev_info_server::{DevInfo, DevInfoServer};
use crate::devinfod::{
GetBuildInfoRequest, GetBuildInfoResponse, GetRootRequest, GetRootResponse,
GetSwVersionsRequest, GetSwVersionsResponse, GetUbootEnvRequest, GetUbootEnvResponse,
};
pub mod devinfod {
tonic::include_proto!("unit.info.v0");
}
#[derive(Debug, Default)]
pub struct MyDevInfo {}
pub struct Distro {
name: String,
version: String,
}
impl Distro {
pub fn to_pb(&self) -> devinfod::Distro {
devinfod::Distro {
name: self.name.clone(),
version: self.version.clone(),
}
}
}
pub struct Layer {
name: String,
branch: String,
revision: String,
}
impl Layer {
pub fn to_pb(&self) -> devinfod::Layer {
devinfod::Layer {
name: self.name.clone(),
branch: self.branch.clone(),
revision: self.revision.clone(),
}
}
}
enum BuildInfoSection {
BuildConfiguration,
LayerRevisions,
}
impl BuildInfoSection {
pub fn from_line(line: &str) -> Option<Self> {
if line.starts_with("Build Configuration") {
Some(BuildInfoSection::BuildConfiguration)
} else if line.starts_with("Layer Revisions") {
Some(BuildInfoSection::LayerRevisions)
} else {
None
}
}
}
pub struct BuildInfo {
build_configuration: Distro,
layer_revisions: Vec<Layer>,
}
impl BuildInfo {
pub fn to_pb(&self) -> devinfod::BuildInfo {
let mut layers_pb = Vec::<devinfod::Layer>::new();
for layer in &self.layer_revisions {
layers_pb.push(layer.to_pb());
}
devinfod::BuildInfo {
distro: Some(self.build_configuration.to_pb()),
layers: layers_pb,
}
}
}
impl FromStr for BuildInfo {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut distro_name: Option<String> = None;
let mut distro_version: Option<String> = None;
let mut layers = Vec::new();
let mut curr_section: Option<BuildInfoSection> = None;
for line in s.lines() {
let trimmed = line.trim();
if line.ends_with("|") {
if let Some(section) = BuildInfoSection::from_line(line) {
curr_section = Some(section);
continue;
}
}
if line.starts_with("-----") {
continue;
}
if let Some(section) = &curr_section {
match section {
BuildInfoSection::BuildConfiguration => {
if let Some((key, value)) = trimmed.split_once("=") {
let key = key.trim();
let value = value.trim();
match key {
"DISTRO" => distro_name = Some(value.to_string()),
"DISTRO_VERSION" => distro_version = Some(value.to_string()),
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Distro configuration contains unexpected key: {}",
key
),
));
}
}
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Entry for build configuration has invalid format, should be of form <key> = <value>",
));
}
}
BuildInfoSection::LayerRevisions => {
if let Some((key, value)) = trimmed.split_once("=") {
let layer_name = key.trim();
let value = value.trim();
if let Some((branch, revision)) = value.split_once(":") {
layers.push(Layer {
name: layer_name.to_string(),
branch: branch.to_string(),
revision: revision.to_string(),
});
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Entry for layer {} contains invalid value format, should be of form <branch>:<revision>",
layer_name
),
));
}
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Entry for layer revisions has invalid format, should be of form <key> = <value>",
));
}
}
}
}
}
match (distro_name, distro_version) {
(Some(name), Some(version)) => Ok(BuildInfo {
build_configuration: Distro { name, version },
layer_revisions: layers,
}),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,
"Missing required build configuration information",
)),
}
}
}
pub struct SwVersion {
name: String,
version: String,
}
impl SwVersion {
pub fn to_pb(&self) -> devinfod::SwVersion {
devinfod::SwVersion {
name: self.name.clone(),
version: self.version.clone(),
}
}
}
pub struct SwVersions {
versions: Vec<SwVersion>,
}
impl SwVersions {
pub fn to_pb(&self) -> devinfod::SwVersions {
let mut versions = Vec::<devinfod::SwVersion>::new();
for sw_version in &self.versions {
versions.push(sw_version.to_pb());
}
devinfod::SwVersions { versions }
}
}
impl FromStr for SwVersions {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut versions = Vec::new();
for line in s.lines() {
let trimmed = line.trim();
if let Some((key, value)) = trimmed.split_once(" ") {
let key = key.trim();
let value = value.trim();
versions.push(SwVersion {
name: key.to_string(),
version: value.to_string(),
});
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Entry for sw versions has invalid format, should be of form <key> <value>",
));
}
}
Ok(SwVersions { versions })
}
}
fn swupdate_get_root() -> io::Result<String> {
let output = Command::new("swupdate").arg("-g").output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("swupdate stderr: {}", stderr);
return Err(io::Error::new(
io::ErrorKind::Other,
format!("swupdate failed with status: {}", output.status),
));
}
let root = String::from_utf8(output.stdout)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
.trim()
.to_string();
Ok(root)
}
struct EnvVar {
name: String,
value: String,
}
impl EnvVar {
pub fn to_pb(&self) -> devinfod::EnvVar {
devinfod::EnvVar {
name: self.name.clone(),
value: self.value.clone(),
}
}
}
struct UbootEnv {
vars: Vec<EnvVar>,
}
impl UbootEnv {
pub fn from_cmd() -> io::Result<Self> {
let output = Command::new("fw_printenv").output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("fw_printenv stderr: {}", stderr);
return Err(io::Error::new(
io::ErrorKind::Other,
format!("fw_printenv failed with status: {}", output.status),
));
}
let output_str = String::from_utf8(output.stdout)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
.trim()
.to_string();
let mut vars = Vec::<EnvVar>::new();
for line in output_str.lines() {
let trimmed = line.trim();
if let Some((key, value)) = trimmed.split_once("=") {
let key = key.trim();
if matches!(
key,
"target_partition_device_node" | "upgrade_available" | "ustate"
) {
let value = value.trim();
vars.push(EnvVar {
name: key.to_string(),
value: value.to_string(),
})
}
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Line for u-boot environment has invalid format, should be of form <key>=<value>",
));
}
}
Ok(UbootEnv { vars })
}
pub fn to_pb(&self) -> devinfod::UbootEnv {
let mut vars_pb = Vec::<devinfod::EnvVar>::new();
for var in &self.vars {
vars_pb.push(var.to_pb());
}
devinfod::UbootEnv { vars: vars_pb }
}
}
#[tonic::async_trait]
impl DevInfo for MyDevInfo {
async fn get_build_info(
&self,
_request: Request<GetBuildInfoRequest>,
) -> Result<Response<GetBuildInfoResponse>, Status> {
let build_info: BuildInfo = fs::read_to_string("/etc/buildinfo")?.parse()?;
let build_info_pb = build_info.to_pb();
let response = GetBuildInfoResponse {
info: Some(build_info_pb),
};
Ok(Response::new(response))
}
async fn get_root(
&self,
_request: Request<GetRootRequest>,
) -> Result<Response<GetRootResponse>, Status> {
let root = swupdate_get_root()?;
let response = GetRootResponse { root };
Ok(Response::new(response))
}
async fn get_sw_versions(
&self,
_request: Request<GetSwVersionsRequest>,
) -> Result<Response<GetSwVersionsResponse>, Status> {
let sw_versions: SwVersions = fs::read_to_string("/etc/sw-versions")?.parse()?;
let sw_versions_pb = sw_versions.to_pb();
let response = GetSwVersionsResponse {
versions: Some(sw_versions_pb),
};
Ok(Response::new(response))
}
async fn get_uboot_env(
&self,
_request: Request<GetUbootEnvRequest>,
) -> Result<Response<GetUbootEnvResponse>, Status> {
let env = UbootEnv::from_cmd()?;
let env_pb = env.to_pb();
let response = GetUbootEnvResponse { env: Some(env_pb) };
Ok(Response::new(response))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "0.0.0.0:60067".parse().unwrap();
let devinfo = MyDevInfo::default();
println!("Listening on {}", addr);
Server::builder()
.add_service(DevInfoServer::new(devinfo))
.serve(addr)
.await?;
Ok(())
}
|