Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Bootstrap command #275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 37 additions & 2 deletions cmd/isp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! 0x00000010 | a1 01 00 00 a3 01 00 00 a5 01 00 00 a7 01 00 00 | ................
//!

use anyhow::Result;
use anyhow::{Context, Result};
use byteorder::ByteOrder;
use clap::Command as ClapCommand;
use clap::{CommandFactory, Parser};
Expand Down Expand Up @@ -89,6 +89,8 @@ enum IspCmd {
GetProperty { prop: BootloaderProperty },
/// Get information about why the chip put itself in ISP mode
LastError,
/// Run initial setup commands
Bootstrap,
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -484,6 +486,39 @@ fn ispcmd(context: &mut humility::ExecutionContext) -> Result<()> {
let result = crate::cmd::do_isp_last_error(&mut *port)?;
pretty_print_error(result);
}
IspCmd::Bootstrap => {
match context.archive.as_ref() {
Some(a) => {
let cfpa = a
.extract_file_bytes("img/CFPA.bin")
.context("Missing CFPA file in archive")?;
crate::cmd::do_isp_write_memory(&mut *port, 0x9de00, cfpa)?;
let cmpa = a
.extract_file_bytes("img/CMPA.bin")
.context("Missing CMPA file in archive")?;
crate::cmd::do_isp_write_memory(&mut *port, 0x9e400, cmpa)?;
humility::msg!("Wrote CFPA and CMPA");
let stage0 = a
.extract_file_bytes("img/final.bin")
.context("Missing hubris file in archive")?;
crate::cmd::do_isp_write_memory(&mut *port, 0x0, stage0)?;
humility::msg!("Wrote stage0");
}
None => humility::msg!("No hubris archive given!"),
}

// Step 1: Enroll
println!("Generating new activation code");
crate::cmd::do_enroll(&mut *port)?;

// Step 2: Generate UDS
println!("Generating new UDS");
crate::cmd::do_generate_uds(&mut *port)?;

println!("Writing keystore");
// Step 3: Write the keystore to persistent storage
crate::cmd::do_save_keystore(&mut *port)?;
}
};

Ok(())
Expand All @@ -493,7 +528,7 @@ pub fn init() -> (Command, ClapCommand<'static>) {
(
Command::Unattached {
name: "isp",
archive: Archive::Ignored,
archive: Archive::Optional,
run: ispcmd,
},
IspArgs::command(),
Expand Down
7 changes: 6 additions & 1 deletion humility-core/src/hubris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4490,7 +4490,7 @@ impl HubrisArchive {
Ok(())
}

pub fn extract_file_to(&self, filename: &str, target: &Path) -> Result<()> {
pub fn extract_file_bytes(&self, filename: &str) -> Result<Vec<u8>> {
let cursor = Cursor::new(self.archive.as_slice());
let mut archive = zip::ZipArchive::new(cursor)?;
let mut file = archive
Expand All @@ -4499,6 +4499,11 @@ impl HubrisArchive {
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;

Ok(buffer)
}

pub fn extract_file_to(&self, filename: &str, target: &Path) -> Result<()> {
let buffer = self.extract_file_bytes(filename)?;
std::fs::write(target, &buffer).map_err(Into::into)
}

Expand Down