Phase 3 of the device sim, all verified on QEMU 10.0.11: - qemu/rs485-bridge/: std-only crate bridging a QEMU serial chardev (unix socket) to warden_sim::ModbusSlave — gap-based RTU framing (CRC failures degrade to real-slave silence), line-protocol control socket for register seeding and fault injection (drop/exception/clear), bounds-checked so a scenario typo answers err instead of panicking the bus. 7 unit tests, bench in the sim_bench pattern. Verified end-to-end: guest master frame on /dev/ttyS4 (pci-serial) answered from the sim slave, CRC-correct. - virt machine gains highmem=off: the 32-bit non-LPAE kernel cannot reach virt's default 40-bit PCIe ECAM (pci-host-generic EOVERFLOW); with it the full PCI set probes (16550A ttyS0, i6300esb). - Watchdog scenario verified: guest arms /dev/watchdog, no petting, i6300esb resets the VM ~30s later (first environment where this arm is testable). - qemu/tests/portal-scenario.sh: the real static-musl warden-flared inside the VM against flare-edge's mock portal on the host — authenticated check-in, firmware desired-state pull, and download of a real signed tier-1 .wfw offer, asserted from the portal log. Found and filed flare-edge#106 (fatal SIGBUS in the HPMCU boot-loaded probe on non-RV1106 memory maps); runs against a flared built from the qemu-vm-support fix branch. - stage-2 init: WARDEN_FLARE_INSECURE=1 + WARDEN_HPMCU=0 (documented VM deviations), firmware-version stamp, newline-terminated state seeds. - CI: rs485-bridge joins the test loop and bench job; new qemu-tools job (shellcheck + initramfs + disk image on hosted runners); kernel-build gains a fail-closed qemu boot-smoke step. All qemu scripts shellcheck-clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018HUayid7W5w7jBdb9Rrj1K
106 lines
3.9 KiB
Rust
106 lines
3.9 KiB
Rust
//! CLI wiring for the RS-485 bridge. All behavior lives in the lib (tested
|
|
//! there); this file only parses arguments, connects sockets, and spawns the
|
|
//! control listener.
|
|
//!
|
|
//! Typical use (matches qemu/run.sh --rs485):
|
|
//!
|
|
//! qemu/run.sh --kernel ... --rs485 /tmp/warden-rs485.sock &
|
|
//! rs485-bridge --serial /tmp/warden-rs485.sock --control /tmp/warden-rs485-ctl.sock
|
|
|
|
use std::io::{BufRead, BufReader, Write};
|
|
use std::os::unix::net::{UnixListener, UnixStream};
|
|
use std::process::exit;
|
|
use std::time::{Duration, Instant};
|
|
use warden_rs485_bridge::{handle_control_line, pump_serial, Bus, DEFAULT_GAP};
|
|
|
|
fn usage() -> ! {
|
|
eprintln!(
|
|
"usage: rs485-bridge --serial <sock> [--control <sock>] [--address N] \
|
|
[--regs N] [--bits N] [--gap-ms N]"
|
|
);
|
|
exit(2);
|
|
}
|
|
|
|
fn main() {
|
|
let mut serial: Option<String> = None;
|
|
let mut control: Option<String> = None;
|
|
let mut address: u8 = 1;
|
|
let mut regs: usize = 128;
|
|
let mut bits: usize = 64;
|
|
let mut gap = DEFAULT_GAP;
|
|
|
|
let mut args = std::env::args().skip(1);
|
|
while let Some(a) = args.next() {
|
|
let mut val = |name: &str| args.next().unwrap_or_else(|| {
|
|
eprintln!("{name} needs a value");
|
|
usage()
|
|
});
|
|
match a.as_str() {
|
|
"--serial" => serial = Some(val("--serial")),
|
|
"--control" => control = Some(val("--control")),
|
|
"--address" => address = val("--address").parse().unwrap_or_else(|_| usage()),
|
|
"--regs" => regs = val("--regs").parse().unwrap_or_else(|_| usage()),
|
|
"--bits" => bits = val("--bits").parse().unwrap_or_else(|_| usage()),
|
|
"--gap-ms" => {
|
|
gap = Duration::from_millis(val("--gap-ms").parse().unwrap_or_else(|_| usage()))
|
|
}
|
|
_ => usage(),
|
|
}
|
|
}
|
|
let serial = serial.unwrap_or_else(|| usage());
|
|
|
|
// The bus is shared between the serial pump and the control channel.
|
|
// 'static so the control thread needs no scoped lifetime: the bridge runs
|
|
// until killed.
|
|
let bus: &'static Bus = Box::leak(Box::new(Bus::new(address, regs, bits)));
|
|
|
|
if let Some(path) = control {
|
|
let _ = std::fs::remove_file(&path); // stale socket from a previous run
|
|
let listener = UnixListener::bind(&path).unwrap_or_else(|e| {
|
|
eprintln!("FATAL: cannot bind control socket {path}: {e}");
|
|
exit(1);
|
|
});
|
|
eprintln!("rs485: control socket at {path}");
|
|
std::thread::spawn(move || {
|
|
for conn in listener.incoming().flatten() {
|
|
let reader = BufReader::new(conn.try_clone().expect("clone control conn"));
|
|
let mut writer = conn;
|
|
for line in reader.lines() {
|
|
let line = match line {
|
|
Ok(l) => l,
|
|
Err(_) => break,
|
|
};
|
|
let reply = handle_control_line(&line, bus);
|
|
if writeln!(writer, "{reply}").is_err() {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// QEMU (chardev server=on) may come up after us: retry the connect briefly
|
|
// instead of racing the VM launch.
|
|
let deadline = Instant::now() + Duration::from_secs(15);
|
|
let stream = loop {
|
|
match UnixStream::connect(&serial) {
|
|
Ok(s) => break s,
|
|
Err(e) if Instant::now() < deadline => {
|
|
eprintln!("rs485: waiting for {serial} ({e})");
|
|
std::thread::sleep(Duration::from_millis(500));
|
|
}
|
|
Err(e) => {
|
|
eprintln!("FATAL: cannot connect serial socket {serial}: {e}");
|
|
exit(1);
|
|
}
|
|
}
|
|
};
|
|
eprintln!("rs485: connected to {serial}, slave address {address}, gap {gap:?}");
|
|
|
|
if let Err(e) = pump_serial(&stream, &bus.slave, gap) {
|
|
eprintln!("FATAL: serial pump: {e}");
|
|
exit(1);
|
|
}
|
|
eprintln!("rs485: serial closed (VM gone), exiting");
|
|
}
|