Skip to content
Merged
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
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gateway/faux-mgs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ slog-term = "2.9"
termios = "0.3"
thiserror = "1.0"
tokio = { version = "1.20", features = ["full"] }
zip = { version = "0.6.2", default-features = false, features = ["deflate","bzip2"] }

gateway-messages = { path = "../../gateway-messages", features = ["std"] }
gateway-sp-comms = { path = "../../gateway-sp-comms" }
59 changes: 59 additions & 0 deletions gateway/faux-mgs/src/hubris_archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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/.

// Copyright 2022 Oxide Computer Company

//! Minimal parsing of Hubris archives.

use anyhow::Context;
use anyhow::Result;
use std::fs;
use std::io::Cursor;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use zip::ZipArchive;

pub struct HubrisArchive {
archive: ZipArchive<Cursor<Vec<u8>>>,
path: PathBuf,
}

impl HubrisArchive {
pub fn open(path: &Path) -> Result<Self> {
let data = fs::read(path)
.with_context(|| format!("failed to read {}", path.display()))?;
let cursor = Cursor::new(data);
let archive = ZipArchive::new(cursor).with_context(|| {
format!(
"failed to open {} as a zip file - is it a hubris archive?",
path.display()
)
})?;
Ok(Self { archive, path: path.to_owned() })
}

pub fn final_bin(&mut self) -> Result<Vec<u8>> {
self.extract_by_name("img/final.bin")
}

fn extract_by_name(&mut self, name: &str) -> Result<Vec<u8>> {
let mut f = self.archive.by_name(name).with_context(|| {
format!(
"failed to find `{}` within {} - is it a hubris archive?",
name,
self.path.display()
)
})?;
let mut data = Vec::new();
f.read_to_end(&mut data).with_context(|| {
format!(
"failed to extract `{}` within {}",
name,
self.path.display()
)
})?;
Ok(data)
}
}
15 changes: 8 additions & 7 deletions gateway/faux-mgs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ use slog::o;
use slog::Drain;
use slog::Level;
use slog::Logger;
use std::fs;
use std::net::SocketAddrV6;
use std::path::PathBuf;
use std::time::Duration;
use tokio::net::UdpSocket;

mod hubris_archive;
mod usart;

use self::hubris_archive::HubrisArchive;

/// Command line program that can send MGS messages to a single SP.
#[derive(Parser, Debug)]
struct Args {
Expand Down Expand Up @@ -103,7 +105,7 @@ enum SpCommand {
},

/// Upload a new image to the SP and have it swap banks (requires reset)
Update { image: PathBuf },
Update { hubris_archive: PathBuf },

/// Instruct the SP to reset.
Reset,
Expand Down Expand Up @@ -187,12 +189,11 @@ async fn main() -> Result<()> {
)
.await?;
}
Some(SpCommand::Update { image }) => {
let data = fs::read(&image).with_context(|| {
format!("failed to read image {}", image.display())
})?;
Some(SpCommand::Update { hubris_archive }) => {
let mut archive = HubrisArchive::open(&hubris_archive)?;
let data = archive.final_bin()?;
sp.update(data).await.with_context(|| {
format!("updating to {} failed", image.display())
format!("updating to {} failed", hubris_archive.display())
})?;
}
Some(SpCommand::Reset) => {
Expand Down