-
Notifications
You must be signed in to change notification settings - Fork 47
/
lib.rs
535 lines (448 loc) · 14.5 KB
/
lib.rs
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// 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/.
use humility::core::Core;
use humility::hubris::*;
use humility_cmd::hiffy::*;
use humility_cmd::i2c::I2cArgs;
use humility_cmd::{Archive, Args, Attach, Command, Validate};
use anyhow::{bail, Result};
use clap::Command as ClapCommand;
use clap::{CommandFactory, Parser};
use hif::*;
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::HashMap;
use std::fs::{self, OpenOptions};
use std::io::prelude::*;
use std::io::BufReader;
use std::io::Write;
#[derive(Parser, Debug)]
#[clap(name = "rendmp", about = env!("CARGO_PKG_DESCRIPTION"))]
struct RendmpArgs {
/// sets timeout
#[clap(
long, short, default_value = "5000", value_name = "timeout_ms",
value_parser=parse_int::parse::<u32>,
)]
timeout: u32,
/// specifies an I2C bus by name
#[clap(long, short, value_name = "bus",
conflicts_with_all = &["port", "controller"]
)]
bus: Option<String>,
/// specifies an I2C controller
#[clap(long, short, value_name = "controller",
value_parser=parse_int::parse::<u8>,
)]
controller: Option<u8>,
/// specifies an I2C controller port
#[clap(long, short, value_name = "port")]
port: Option<String>,
/// specifies I2C multiplexer and segment
#[clap(long, short, value_name = "mux:segment")]
mux: Option<String>,
/// specifies an I2C device address
#[clap(long, short = 'd', value_name = "address")]
device: Option<String>,
/// specifies a device by rail name
#[clap(long, short = 'r', value_name = "rail")]
rail: Option<String>,
/// specifies a PMBus driver
#[clap(long, short = 'D')]
driver: Option<String>,
/// dump all device memory
#[clap(long)]
dump: bool,
/// ingest a Power Navigator text file
#[clap(
long,
short = 'i',
value_name = "filename",
conflicts_with_all = &["bus", "device"],
)]
ingest: Option<String>,
}
fn all_commands(
device: pmbus::Device,
) -> HashMap<String, (u8, pmbus::Operation, pmbus::Operation)> {
let mut all = HashMap::new();
for i in 0..=255u8 {
device.command(i, |cmd| {
all.insert(
cmd.name().to_string(),
(i, cmd.read_op(), cmd.write_op()),
);
});
}
all
}
#[derive(Copy, Clone, Debug)]
enum Address<'a> {
Dma(u16),
Pmbus(u8, &'a str),
}
struct Packet<'a> {
address: Address<'a>,
payload: Vec<u8>,
}
fn rendmp_gen(
_subargs: &RendmpArgs,
device: &pmbus::Device,
packets: &[Packet],
commands: &HashMap<String, (u8, pmbus::Operation, pmbus::Operation)>,
) -> Result<()> {
println!(
r##"// 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/.
///
/// Iterate over a configuration payload for a Renesas {} digital multiphase
/// PWM controller. This code was generated by "humility rendmp -g" given
/// a .txt dump from running Renesas configuration software.
///
#[rustfmt::skip]
pub fn {}_payload<E>(
mut func: impl FnMut(&[u8]) -> Result<(), E>
) -> Result<(), E> {{
const PAYLOAD: &[&[u8]] = &["##,
device.name(),
device.name(),
);
let dmaaddr = match commands.get("DMAADDR") {
Some((code, _, write)) => {
if *write != pmbus::Operation::WriteWord {
bail!("DMAADDR mismatch: found {:?}", write);
}
*code
}
_ => {
bail!("no DMAADDR command found; is this a Renesas device?");
}
};
let dmafix = match commands.get("DMAFIX") {
Some((code, _, write)) => {
if *write != pmbus::Operation::WriteWord32 {
bail!("DMADATA mismatch: found {:?}", write);
}
*code
}
_ => {
bail!("no DMAFIX command found; is this a Renesas device?");
}
};
for packet in packets {
match packet.address {
Address::Dma(addr) => {
let p = addr.to_le_bytes();
println!(" // DMAADDR = 0x{:04x}", addr);
println!(
" &[ 0x{:02x}, 0x{:02x}, 0x{:02x} ],\n",
dmaaddr, p[0], p[1]
);
println!(" // DMAFIX = {:x?}", packet.payload);
print!(" &[ 0x{:02x}, ", dmafix);
}
Address::Pmbus(code, name) => {
println!(" // {} = {:x?}", name, packet.payload);
print!(" &[ 0x{:02x}, ", code);
}
}
for byte in &packet.payload {
print!("0x{:02x}, ", byte);
}
println!("],\n");
}
println!(
r##" ];
for chunk in PAYLOAD {{
func(chunk)?;
}}
Ok(())
}}"##
);
Ok(())
}
fn rendmp_ingest(subargs: &RendmpArgs) -> Result<()> {
let filename = subargs.ingest.as_ref().unwrap();
let file = fs::File::open(filename)?;
let lines = BufReader::new(file).lines();
let mut allcmds = HashMap::new();
let mut packets = vec![];
let device = if let Some(driver) = &subargs.driver {
match pmbus::Device::from_str(driver) {
Some(device) => device,
None => {
bail!("unknown device \"{}\"", driver);
}
}
} else {
bail!("must specify device driver");
};
for code in 0..0xffu8 {
device.command(code, |cmd| {
allcmds.insert(code, cmd.name());
});
}
for (ndx, line) in lines.enumerate() {
let line = line?;
let lineno = ndx + 1;
if line.is_empty() || line.starts_with('#') {
continue;
}
let contents = line.split_whitespace().collect::<Vec<_>>();
if contents.len() != 4 || contents[2] != "#" {
bail!("malformed line {}", lineno);
}
let payload = contents[1];
if !payload.starts_with("0x") {
bail!("bad payload prefix on line {}: {}", lineno, payload);
}
let payload = match payload.len() {
4 => match parse_int::parse::<u8>(payload) {
Ok(val) => val.to_le_bytes().to_vec(),
Err(_) => {
bail!("bad payload on line {}: {}", lineno, payload);
}
},
6 => match parse_int::parse::<u16>(payload) {
Ok(val) => val.to_le_bytes().to_vec(),
Err(_) => {
bail!("bad payload on line {}: {}", lineno, payload);
}
},
10 => match parse_int::parse::<u32>(payload) {
Ok(val) => val.to_le_bytes().to_vec(),
Err(_) => {
bail!("bad payload on line {}: {}", lineno, payload);
}
},
_ => {
bail!("badly sized payload on line {}: {}", lineno, payload);
}
};
let address = contents[3];
//
// This is lame, but the only way to differentiate PMBus writes
// (single-byte address) from DMA writes (dual-byte) is to look
// at length of the string:
//
if !address.starts_with("0x") {
bail!("bad address on line {}: {}", lineno, address);
}
let address = if address.len() > 4 {
match parse_int::parse::<u16>(address) {
Ok(dmaaddr) => Address::Dma(dmaaddr),
Err(_) => {
bail!("bad DMA address on line {}: {}", lineno, address);
}
}
} else {
match parse_int::parse::<u8>(address) {
Ok(paddr) => {
Address::Pmbus(paddr, allcmds.get(&paddr).unwrap())
}
Err(_) => {
bail!("bad PMBus address on line {}: {}", lineno, address);
}
}
};
packets.push(Packet { address, payload });
}
packets.push(Packet {
address: Address::Pmbus(0xe7, allcmds.get(&0xe7).unwrap()),
payload: vec![1, 0],
});
let commands = all_commands(device);
rendmp_gen(subargs, &device, &packets, &commands)?;
Ok(())
}
fn rendmp(
hubris: &HubrisArchive,
core: &mut dyn Core,
_args: &Args,
subargs: &[String],
) -> Result<()> {
let subargs = RendmpArgs::try_parse_from(subargs)?;
if subargs.ingest.is_some() {
return rendmp_ingest(&subargs);
}
let mut context = HiffyContext::new(hubris, core, subargs.timeout)?;
let funcs = context.functions()?;
let i2c_read = funcs.get("I2cRead", 7)?;
let i2c_write = funcs.get("I2cWrite", 8)?;
let hargs = match (&subargs.rail, &subargs.device) {
(Some(rail), None) => {
let mut found = None;
for device in &hubris.manifest.i2c_devices {
if let HubrisI2cDeviceClass::Pmbus { rails } = &device.class {
for r in rails {
if rail == r {
found = match found {
Some(_) => {
bail!("multiple devices match {}", rail);
}
None => Some(device),
}
}
}
}
}
match found {
None => {
bail!("rail {} not found", rail);
}
Some(device) => I2cArgs::from_device(device),
}
}
(_, _) => I2cArgs::parse(
hubris,
&subargs.bus,
subargs.controller,
&subargs.port,
&subargs.mux,
&subargs.device,
)?,
};
let device = if let Some(driver) = &subargs.driver {
match pmbus::Device::from_str(driver) {
Some(device) => device,
None => {
bail!("unknown device \"{}\"", driver);
}
}
} else if let Some(driver) = hargs.device {
match pmbus::Device::from_str(&driver) {
Some(device) => device,
None => pmbus::Device::Common,
}
} else {
pmbus::Device::Common
};
let all = all_commands(device);
let dmaaddr = match all.get("DMAADDR") {
Some((code, _, write)) => {
if *write != pmbus::Operation::WriteWord {
bail!("DMAADDR mismatch: found {:?}", write);
}
*code
}
_ => {
bail!("no DMAADDR command found; is this a Renesas device?");
}
};
let dmaseq = match all.get("DMASEQ") {
Some((code, read, _)) => {
if *read != pmbus::Operation::ReadWord32 {
bail!("DMASEQ mismatch: found {:?}", read);
}
*code
}
_ => {
bail!("no DMASEQ command found; is this a Renesas device?");
}
};
let mut base = vec![Op::Push(hargs.controller), Op::Push(hargs.port.index)];
if let Some(mux) = hargs.mux {
base.push(Op::Push(mux.0));
base.push(Op::Push(mux.1));
} else {
base.push(Op::PushNone);
base.push(Op::PushNone);
}
if let Some(address) = hargs.address {
base.push(Op::Push(address));
} else {
bail!("expected device");
}
if subargs.dump {
let blocksize = 128u8;
let nblocks = 8;
let memsize = 256 * 1024usize;
let laps = memsize / (blocksize as usize * nblocks);
let mut addr = 0;
let bar = ProgressBar::new(memsize as u64);
let mut filename;
let mut i = 0;
let filename = loop {
filename = format!("hubris.rendmp.dump.{}", i);
if let Ok(_f) = fs::File::open(&filename) {
i += 1;
continue;
}
break filename;
};
let mut file =
OpenOptions::new().write(true).create_new(true).open(&filename)?;
humility::msg!("dumping device memory to {}", filename);
bar.set_style(ProgressStyle::default_bar().template(
"humility: dumping device memory \
[{bar:30}] {bytes}/{total_bytes}",
));
for lap in 0..laps {
let mut ops = base.clone();
//
// If this is our first lap through, set our address to be 0
//
if lap == 0 {
ops.push(Op::Push(dmaaddr));
ops.push(Op::Push(0));
ops.push(Op::Push(0));
ops.push(Op::Push(2));
ops.push(Op::Call(i2c_write.id));
ops.push(Op::DropN(4));
}
ops.push(Op::Push(dmaseq));
ops.push(Op::Push(blocksize));
//
// Unspeakably lazy, but also much less complicated: we just
// unroll our loop here.
//
for _ in 0..nblocks {
ops.push(Op::Call(i2c_read.id));
}
//
// Kick it off
//
ops.push(Op::Done);
let results = context.run(core, ops.as_slice(), None)?;
let start = if lap == 0 {
match results[0] {
Err(err) => {
bail!(
"failed to set address: {}",
i2c_write.strerror(err)
)
}
Ok(_) => 1,
}
} else {
0
};
for result in &results[start..] {
match result {
Ok(val) => {
file.write_all(val)?;
addr += val.len();
bar.set_position(addr as u64);
}
Err(err) => {
bail!("{:?}", err);
}
}
}
}
}
Ok(())
}
pub fn init() -> (Command, ClapCommand<'static>) {
(
Command::Attached {
name: "rendmp",
archive: Archive::Required,
attach: Attach::LiveOnly,
validate: Validate::Booted,
run: rendmp,
},
RendmpArgs::command(),
)
}