-
Notifications
You must be signed in to change notification settings - Fork 47
/
lib.rs
365 lines (294 loc) · 9.26 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
// 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 std::str;
use anyhow::{bail, Result};
use clap::Command as ClapCommand;
use clap::{CommandFactory, Parser};
use hif::*;
#[derive(Parser, Debug)]
#[clap(name = "spd", about = env!("CARGO_PKG_DESCRIPTION"))]
struct SpdArgs {
/// sets timeout
#[clap(
long, short, default_value = "5000", value_name = "timeout_ms",
value_parser=parse_int::parse::<u32>,
)]
timeout: u32,
/// verbose output
#[clap(long, short)]
verbose: bool,
/// specifies an I2C controller
#[clap(long, short, value_name = "controller",
value_parser=parse_int::parse::<u8>,
)]
controller: Option<u8>,
/// specifies an I2C bus by name
#[clap(long, short, value_name = "bus",
conflicts_with_all = &["port", "controller"]
)]
bus: Option<String>,
/// 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>,
}
const SPD_SIZE: usize = 512;
fn from_bcd(val: u8) -> u8 {
(val >> 4) * 10 + (val & 0xf)
}
fn dump_spd(
subargs: &SpdArgs,
addr: u8,
buf: &[u8],
header: bool,
) -> Result<()> {
use spd::Offset;
let jep_cc = Offset::ModuleManufacturerIDCodeLSB.within(buf) & 0x7f;
let jep_id = Offset::ModuleManufacturerIDCodeMSB.within(buf) & 0x7f;
let year = from_bcd(Offset::ModuleManufacturingDateYear.within(buf));
let week = from_bcd(Offset::ModuleManufacturingDateWeek.within(buf));
let width: usize = 16;
let manufacturer = jep106::JEP106Code::new(jep_cc, jep_id);
let part = str::from_utf8(
&buf[Offset::PartNumberBase.to_usize()
..=Offset::PartNumberLimit.to_usize()],
);
if header || subargs.verbose {
println!(
"{:4} {:25} {:20} {:4} {:4}",
"ADDR", "MANUFACTURER", "PART", "WEEK", "YEAR"
)
}
println!(
"{:4} {:25} {:20} {:4} {:4}",
addr,
manufacturer.get().unwrap_or("<unknown>"),
part.unwrap_or("<unknown>"),
week,
2000 + (year as u16),
);
if !subargs.verbose {
return Ok(());
}
println!(" |");
print!(" +----> ");
for i in 0..width {
print!(" {:02x}", i);
}
println!();
for offs in (0..SPD_SIZE).step_by(width) {
print!(" 0x{:03x} | ", offs);
for i in 0..width {
print!(" {:02x}", buf[offs + i]);
}
print!(" | ");
for i in 0..width {
let c = buf[offs + i] as char;
if c.is_ascii() && !c.is_ascii_control() {
print!("{}", c);
} else {
print!(".");
}
}
println!();
}
Ok(())
}
// Assumes that we already have pushed on the stack our controller/port/mux
fn set_page(ops: &mut Vec<Op>, i2c_write: &HiffyFunction, page: u8) {
let dev =
spd::Function::PageAddress(spd::Page(page)).to_device_code().unwrap();
ops.push(Op::Push(dev)); // Device
ops.push(Op::PushNone); // Register
ops.push(Op::Push(0)); // Buffer
ops.push(Op::Push(1)); // Length
ops.push(Op::Call(i2c_write.id));
ops.push(Op::DropN(4));
}
fn spd(
hubris: &HubrisArchive,
core: &mut dyn Core,
_args: &Args,
subargs: &[String],
) -> Result<()> {
let subargs = SpdArgs::try_parse_from(subargs)?;
//
// If we have been given no device-related arguments, we will attempt
// to find the `SPD_DATA` variable.
//
if subargs.bus.is_none() && subargs.controller.is_none() {
let spd_data = match hubris.lookup_variables("SPD_DATA") {
Ok(variables) => {
if variables.len() > 1 {
bail!("more than one SPD_DATA?");
}
variables[0]
}
Err(_) => {
bail!("no bus specified and no SPD_DATA found");
}
};
if spd_data.size % SPD_SIZE != 0 {
bail!(
"SPD_DATA is {} bytes; expected even multiple of {}",
spd_data.size,
SPD_SIZE
);
}
let nspd = spd_data.size / SPD_SIZE;
let mut bytes = vec![0u8; spd_data.size];
let _info = core.halt()?;
let rval = core.read_8(spd_data.addr, &mut bytes);
core.run()?;
rval?;
let mut header = true;
for addr in 0..nspd {
let offs = addr * SPD_SIZE;
let data = &bytes[offs..offs + SPD_SIZE];
if !data.iter().any(|&datum| datum != 0) {
continue;
}
dump_spd(&subargs, addr as u8, data, header)?;
header = false;
}
if header {
humility::msg!("all SPD data is empty");
}
return Ok(());
}
if core.is_dump() {
bail!("cannot specify bus/controller on a dump");
}
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 = I2cArgs::parse(
hubris,
&subargs.bus,
subargs.controller,
&subargs.port,
&subargs.mux,
&None,
)?;
let mut ops = vec![Op::Push(hargs.controller)];
ops.push(Op::Push(hargs.port.index));
if let Some(mux) = hargs.mux {
ops.push(Op::Push(mux.0));
ops.push(Op::Push(mux.1));
} else {
ops.push(Op::PushNone);
ops.push(Op::PushNone);
}
let base = ops.clone();
//
// First, we want to have all SPDs on the specified bus flip to
// their 0 page
//
set_page(&mut ops, i2c_write, 0);
//
// Now issue single byte register reads to determine where our devices are.
//
for addr in 0..spd::MAX_DEVICES {
ops.push(Op::Push(
spd::Function::Memory(addr).to_device_code().unwrap(),
));
ops.push(Op::Push(0));
ops.push(Op::Push(1));
ops.push(Op::Call(i2c_read.id));
ops.push(Op::DropN(3));
}
ops.push(Op::Done);
let results = context.run(core, ops.as_slice(), None)?;
let mut header = true;
if let Err(err) = results[0] {
bail!("failed to set page to 0: {}", i2c_write.strerror(err));
}
for addr in 0..spd::MAX_DEVICES {
if results[addr as usize + 1].is_ok() {
let mut ops = base.clone();
//
// Issue the read for the bottom 128 bytes from the 0 page
//
let dev = spd::Function::Memory(addr).to_device_code().unwrap();
ops.push(Op::Push(dev));
ops.push(Op::Push(0));
ops.push(Op::Push(128));
ops.push(Op::Call(i2c_read.id));
//
// And now read the top 128 bytes from the 0 page...
//
ops.push(Op::DropN(2));
ops.push(Op::Push(128));
ops.push(Op::Push(128));
ops.push(Op::Call(i2c_read.id));
//
// Switch to the 1 page
//
ops.push(Op::DropN(3));
set_page(&mut ops, i2c_write, 1);
//
// Issue an identical read for the bottom 128 bytes...
//
ops.push(Op::Push(dev));
ops.push(Op::Push(0));
ops.push(Op::Push(128));
ops.push(Op::Call(i2c_read.id));
//
// ...and the top 128 bytes
//
ops.push(Op::DropN(2));
ops.push(Op::Push(128));
ops.push(Op::Push(128));
ops.push(Op::Call(i2c_read.id));
ops.push(Op::DropN(3));
//
// Finally, set ourselves back to the 0 page
//
set_page(&mut ops, i2c_write, 0);
ops.push(Op::Done);
let results = context.run(core, ops.as_slice(), None)?;
//
// If that succeeded, we'll have four buffers that should add up
// to 512 bytes.
//
let mut buf = vec![];
for result in &results {
match result {
Ok(val) => {
buf.extend_from_slice(val);
}
Err(_) => {
bail!("failed to read SPD: {:?}", results);
}
}
}
if buf.len() != SPD_SIZE {
bail!("bad SPD length ({} bytes): {:?}", buf.len(), results);
}
dump_spd(&subargs, addr, &buf, header)?;
header = false;
}
}
Ok(())
}
pub fn init() -> (Command, ClapCommand<'static>) {
(
Command::Attached {
name: "spd",
archive: Archive::Required,
attach: Attach::Any,
validate: Validate::Booted,
run: spd,
},
SpdArgs::command(),
)
}