Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 265 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ hubtools = { git = "https://github.com/oxidecomputer/hubtools.git", branch = "ma
slog-error-chain = { git = "https://github.com/oxidecomputer/slog-error-chain.git", branch = "main", features = ["derive"] }

anyhow = "1.0"
async-recursion = "1.1.0"
async-trait = "0.1"
backoff = { version = "0.4.0", features = ["tokio"] }
base64 = "0.22.1"
Expand All @@ -38,6 +39,8 @@ glob = "0.3.2"
hex = "0.4.3"
hubpack = "0.1.2"
humantime = "2.2.0"
lpc55_areas = { git = "https://github.com/oxidecomputer/lpc55_support/", version = "0.2.3" }
lpc55_sign = { git = "https://github.com/oxidecomputer/lpc55_support/", version = "0.3.0" }
lru-cache = "0.1.2"
lzss = "0.8"
nix = { version = "0.27.1", features = ["net"] }
Expand All @@ -46,6 +49,10 @@ once_cell = "1.21.3"
paste = "1.0.15"
parse_int = "0.6"
rand = "0.8.5"
rhai-chrono = { version = "^0" }
rhai-env = "0.1.2"
rhai-fs = { version = "0.1.3", features = ["metadata"] }
rhai = { version = "1.21.0", features = ["serde", "metadata", "debugging"]}
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-big-array = "0.5.1"
serde_bytes = "0.11.17"
Expand All @@ -65,10 +72,11 @@ strum = { version = "0.27.1", default-features = false }
strum_macros = "0.27.1"
string_cache = "0.8.9"
termios = "0.3"
thiserror = "1.0.69"
thiserror = { version = "1.0.69", default-features = false }
tokio = { version = "1.29", features = ["full"] }
tokio-stream = { version = "0.1", features = ["fs"] }
tokio-util = { version = "0.7", features = ["compat"] }
toml = { version = "0.7", default-features = false, features = ["parse", "display"] }
usdt = "0.5.0"
uuid = { version = "1.16", default-features = false }
version_check = "0.9.5"
Expand Down
15 changes: 15 additions & 0 deletions faux-mgs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ zerocopy.workspace = true

gateway-messages = { workspace = true, features = ["std"] }
gateway-sp-comms.workspace = true

async-recursion = { workspace = true, optional = true }
hubtools = { workspace = true, optional = true }
lpc55_areas = { workspace = true, optional = true }
lpc55_sign= { workspace = true, optional = true }
rhai-chrono = { workspace = true, optional = true }
rhai-env = { workspace = true, optional = true }
rhai-fs = { workspace = true, optional = true }
rhai = { workspace = true, optional = true }
thiserror = { workspace = true, optional = true }
toml = { workspace = true, optional = true }

[features]
default = ["rhaiscript"]
rhaiscript = [ "dep:async-recursion", "dep:hubtools", "dep:lpc55_areas", "dep:lpc55_sign", "dep:rhai", "dep:rhai-chrono", "dep:rhai-env", "dep:rhai-fs", "dep:thiserror", "dep:toml"]
62 changes: 50 additions & 12 deletions faux-mgs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ use uuid::Uuid;
use zerocopy::IntoBytes;

mod picocom_map;
#[cfg(feature = "rhaiscript")]
mod rhaiscript;
mod usart;

/// Command line program that can send MGS messages to a single SP.
Expand Down Expand Up @@ -137,6 +139,14 @@ struct Args {
command: Command,
}

/// Rhai program that can send MGS messages to a single SP.
#[cfg(feature = "rhaiscript")]
#[derive(Parser, Debug)]
struct RhaiArgs {
#[clap(subcommand)]
command: Command,
}

fn level_from_str(s: &str) -> Result<Level> {
if let Ok(level) = s.parse() {
Ok(level)
Expand Down Expand Up @@ -407,6 +417,16 @@ enum Command {
disable_watchdog: bool,
},

/// Run a Rhai script within faux-mgs
#[cfg(feature = "rhaiscript")]
Rhai {
/// Path to Rhia script
script: PathBuf,
/// Additional arguments passed to Rhia scripe
#[clap(trailing_var_arg = true, allow_hyphen_values = true)]
script_args: Vec<String>,
},

/// Controls the system LED
SystemLed {
#[clap(subcommand)]
Expand Down Expand Up @@ -891,7 +911,7 @@ async fn main() -> Result<()> {
.into_iter()
.map(|sp| {
let interface = sp.interface().to_string();
run_command(
run_any_command(
sp,
args.command.clone(),
args.json.is_some(),
Expand Down Expand Up @@ -965,11 +985,31 @@ fn ssh_list_keys(socket: &PathBuf) -> Result<Vec<ssh_key::PublicKey>> {
client.list_identities().context("failed to list identities")
}

async fn run_command(
/// This function exists to break recursive calls to the Rhai interpreter.
/// the faux-mgs main function calls here. However, the `rhai` subcommand
/// calls run_command() which does not include any calls to the
/// rhai subcommand.
async fn run_any_command(
sp: SingleSp,
command: Command,
json: bool,
log: Logger,
) -> Result<Output> {
match command {
#[cfg(feature = "rhaiscript")]
Command::Rhai { script, script_args } => {
rhaiscript::interpreter(&sp, log, script, script_args).await
}
_ => run_command(&sp, command, json, log).await,
}
}

/// Run faux-mgs commands except for the `rhai` subcommand.
async fn run_command(
sp: &SingleSp,
command: Command,
json: bool,
log: Logger,
) -> Result<Output> {
match command {
// Skip special commands handled by `main()` above.
Expand Down Expand Up @@ -1328,7 +1368,7 @@ async fn run_command(
let data = fs::read(&image).with_context(|| {
format!("failed to read {}", image.display())
})?;
update(&log, &sp, component, slot, data).await.with_context(
update(&log, sp, component, slot, data).await.with_context(
|| {
format!(
"updating {} slot {} to {} failed",
Expand Down Expand Up @@ -1465,7 +1505,10 @@ async fn run_command(
Ok(Output::Lines(vec!["reset complete".to_string()]))
}
}

#[cfg(feature = "rhaiscript")]
Command::Rhai { script, script_args } => {
rhaiscript::interpreter(sp, log, script, script_args).await
}
Command::ResetComponent { component, disable_watchdog } => {
sp.reset_component_prepare(component).await?;
info!(log, "SP is prepared to reset component {component}",);
Expand Down Expand Up @@ -1554,14 +1597,8 @@ async fn run_command(
if time_sec == 0 {
bail!("--time must be >= 1 second");
}
monorail_unlock(
&log,
&sp,
time_sec,
ssh_auth_sock,
key,
)
.await?;
monorail_unlock(&log, sp, time_sec, ssh_auth_sock, key)
.await?;
}
}
}
Expand Down Expand Up @@ -2062,6 +2099,7 @@ async fn populate_phase2_images(
Ok(())
}

#[derive(Clone)]
enum Output {
Json(serde_json::Value),
Lines(Vec<String>),
Expand Down
Loading
Loading