-
Notifications
You must be signed in to change notification settings - Fork 63
Add humility-caboose and humility-cmd-caboose
#696
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
Open
mkeeter
wants to merge
1
commit into
master
Choose a base branch
from
mkeeter/humility-caboose
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "humility-cmd-caboose" | ||
| version = "0.1.0" | ||
| edition.workspace = true | ||
| description = "read from the caboose" | ||
|
|
||
| [dependencies] | ||
| anyhow.workspace = true | ||
| clap.workspace = true | ||
| tlvc-text.workspace = true | ||
|
|
||
| humility-caboose.workspace = true | ||
| humility-cli.workspace = true | ||
| humility-probes-core.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // 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/. | ||
|
|
||
| //! ## `humility caboose` | ||
| //! | ||
| //! Tools to read from the caboose (without an archive) | ||
|
|
||
| use anyhow::Result; | ||
| use clap::Parser; | ||
| use humility_cli::{ExecutionContext, humility_cmd}; | ||
|
|
||
| #[derive(Parser, Debug)] | ||
| #[clap(name = "caboose", about = env!("CARGO_PKG_DESCRIPTION"))] | ||
| pub struct CabooseArgs { | ||
| #[clap(subcommand)] | ||
| cmd: CabooseCommand, | ||
| } | ||
|
|
||
| #[derive(Parser, Debug)] | ||
| enum CabooseCommand { | ||
| Read, | ||
| } | ||
|
|
||
| fn caboose(subargs: CabooseArgs, context: &mut ExecutionContext) -> Result<()> { | ||
| let log = context.log(); | ||
| match subargs.cmd { | ||
| CabooseCommand::Read => { | ||
| let mut probe = context.cli.attach_probe(None)?; | ||
| let mut t = humility_caboose::read_tlvc_caboose(&mut probe, log)?; | ||
|
|
||
| // Strip raw bytes from the end, for pretty-printing | ||
| if let Some(tlvc_text::Piece::Bytes(bs)) = t.last() | ||
| && bs.iter().all(|c| *c == 0xFF) | ||
| { | ||
| t.pop(); | ||
| } | ||
|
|
||
| if t.is_empty() { | ||
| panic!("caboose is empty"); | ||
| } | ||
|
|
||
| let mut text = vec![]; | ||
| tlvc_text::save(&mut text, &t).unwrap(); | ||
| println!("{}", std::str::from_utf8(&text).unwrap()); | ||
| Ok(()) | ||
|
mkeeter marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| humility_cmd!(CabooseArgs, caboose); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "humility-caboose" | ||
| version = "0.1.0" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
|
|
||
| [dependencies] | ||
| anyhow.workspace = true | ||
| thiserror.workspace = true | ||
| tlvc.workspace = true | ||
| tlvc-text.workspace = true | ||
|
|
||
| humility.workspace = true | ||
| humility-arch-arm.workspace = true | ||
| humility-doppel.workspace = true | ||
| humility-probes-core.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| // 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/. | ||
|
|
||
| //! Library to get [caboose](https://hubris.oxide.computer/reference/#caboose) | ||
| //! information from an attached system | ||
| //! | ||
| //! The main entry point is [`read_tlvc_caboose`]. | ||
| #![warn(missing_docs)] | ||
| use anyhow::Result; | ||
|
mkeeter marked this conversation as resolved.
|
||
|
|
||
| use humility::{ | ||
| core::Core, | ||
| log::{Logger, info}, | ||
| }; | ||
| use humility_doppel::{ | ||
| CABOOSE_MAGIC, POSSIBLE_HEADER_MAGIC, POSSIBLE_IMAGE_HEADER_OFFSETS, | ||
| }; | ||
|
|
||
| /// Error returned when trying to read a caboose | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum CabooseError { | ||
| /// Failed when calling [`Core::halt`] | ||
| #[error("could not halt core")] | ||
| CouldNotHalt(#[source] anyhow::Error), | ||
|
|
||
| /// Failed when calling [`Core::read_reg`] on the `PC` register | ||
| #[error("could not read PC")] | ||
| CouldNotReadPc(#[source] anyhow::Error), | ||
|
|
||
| /// Failed when calling [`Core::run`] | ||
| #[error("could not run core")] | ||
| CouldNotRun(#[source] anyhow::Error), | ||
|
|
||
| /// Program counter does not match any of our expected ranges | ||
| #[error("could not find image start point based on program counter {0:#x}")] | ||
| UnknownPc(u32), | ||
|
|
||
| /// Flash read failed | ||
| #[error("flash read failed")] | ||
| FlashReadFailed(#[source] anyhow::Error), | ||
|
|
||
| /// Failed to find [`POSSIBLE_HEADER_MAGIC`] at any expected offset | ||
| #[error("failed to find header magic with base address {0:#x}")] | ||
| MissingMagic(u32), | ||
|
|
||
| /// Could not compute caboose offset | ||
| #[error("could not compute caboose offset; it is probably missing")] | ||
| MissingCaboose, | ||
|
|
||
| /// Found a magic value which is not [`CABOOSE_MAGIC`] | ||
| #[error("bad caboose magic: expected {CABOOSE_MAGIC:#x}, got {actual:#x}")] | ||
| BadCabooseMagic { | ||
| /// Magic value that was found in the system | ||
| actual: u32, | ||
| }, | ||
|
|
||
| /// Caboose exceeds [`humility::core::CORE_MAX_READSIZE`] | ||
| #[error( | ||
| "caboose size {caboose_size} exceeds max read size {}", | ||
| humility::core::CORE_MAX_READSIZE | ||
| )] | ||
| GiganticCaboose { | ||
| /// Actual size of the caboose | ||
| caboose_size: usize, | ||
| }, | ||
|
|
||
| /// Forwarded error from the TLVC library | ||
| #[error("tlvc error: {0}")] | ||
| TlvcError(String), // tlvc::Error does not implement the `Error` trait | ||
| } | ||
|
|
||
| /// Reads a TLVC-encoded caboose from an attached system | ||
| pub fn read_tlvc_caboose( | ||
| core: &mut humility_probes_core::ProbeCore, | ||
| log: &Logger, | ||
| ) -> Result<Vec<tlvc_text::Piece>, CabooseError> { | ||
| // We'll get the program counter to estimate where we should try to read the | ||
| // image header, since we don't necessarily have our chip info here. | ||
| core.halt().map_err(CabooseError::CouldNotHalt)?; | ||
| let pc = core | ||
| .read_reg(humility_arch_arm::ARMRegister::PC) | ||
| .map_err(CabooseError::CouldNotReadPc)?; | ||
| core.run().map_err(CabooseError::CouldNotRun)?; | ||
|
|
||
| // Find the flash base by looking at the program counter | ||
| // | ||
| // This assumes that we're executing from flash, which is true for all | ||
| // systems that we care about. | ||
| let base = if (0x10000..0x50000).contains(&pc) { | ||
| 0x10000 | ||
| } else if (0x50000..0x90000).contains(&pc) { | ||
| 0x50000 | ||
| } else if (0x08000000..0x08100000).contains(&pc) { | ||
| 0x08000000 | ||
| } else { | ||
| return Err(CabooseError::UnknownPc(pc)); | ||
| }; | ||
| info!(log, "found flash base at {base:#x} (pc at {pc:#x})"); | ||
|
|
||
| // Find the image header by looking for the appropriate magic word | ||
| let mut found_header = None; | ||
| for header_offset in POSSIBLE_IMAGE_HEADER_OFFSETS { | ||
| let header_magic = core | ||
| .read_word_32(base + header_offset) | ||
| .map_err(CabooseError::FlashReadFailed)?; | ||
| if POSSIBLE_HEADER_MAGIC.contains(&header_magic) { | ||
| found_header = Some(header_offset); | ||
| info!(log, "found image header at {:#x}", base + header_offset); | ||
| break; | ||
| } | ||
| } | ||
| let Some(header_offset) = found_header else { | ||
| return Err(CabooseError::MissingMagic(base)); | ||
| }; | ||
|
|
||
| // Check that the caboose exists and is valid | ||
| let image_size_addr = base | ||
| .checked_add(header_offset) | ||
| .and_then(|b| b.checked_add(4)) | ||
| .ok_or(CabooseError::MissingCaboose)?; | ||
| let image_size = core | ||
| .read_word_32(image_size_addr) | ||
| .map_err(CabooseError::FlashReadFailed)?; | ||
|
|
||
| let caboose_size_addr = base | ||
| .checked_add(image_size) | ||
| .and_then(|b| b.checked_sub(4)) | ||
| .ok_or(CabooseError::MissingCaboose)?; | ||
| let caboose_size = core | ||
| .read_word_32(caboose_size_addr) | ||
| .map_err(CabooseError::FlashReadFailed)?; | ||
|
|
||
| let caboose_magic_addr = (base + image_size) | ||
| .checked_sub(caboose_size) | ||
| .ok_or(CabooseError::MissingCaboose)?; | ||
| let caboose_magic = core | ||
| .read_word_32(caboose_magic_addr) | ||
| .map_err(CabooseError::FlashReadFailed)?; | ||
| if caboose_magic != CABOOSE_MAGIC { | ||
| return Err(CabooseError::BadCabooseMagic { actual: caboose_magic }); | ||
| } | ||
|
|
||
| // Compute start and end for the raw caboose range (including magic and len) | ||
| let raw_caboose_start = base + image_size - caboose_size; | ||
| let raw_caboose_end = base + image_size; | ||
| info!( | ||
| log, | ||
| "found caboose at {:#x}..{:#x}", raw_caboose_start, raw_caboose_end, | ||
| ); | ||
|
|
||
| // The caboose data range skips the magic word and length word | ||
| let caboose_data_range = raw_caboose_start + 4..raw_caboose_end - 4; | ||
| let caboose_data_size = caboose_data_range.len(); | ||
|
|
||
| if caboose_data_size > humility::core::CORE_MAX_READSIZE { | ||
| return Err(CabooseError::GiganticCaboose { | ||
| caboose_size: caboose_data_size, | ||
| }); | ||
| } | ||
|
|
||
| // Read the whole caboose into memory | ||
| let mut caboose_data = vec![0u8; caboose_data_size]; | ||
| core.read_8(caboose_data_range.start, &mut caboose_data) | ||
| .map_err(CabooseError::FlashReadFailed)?; | ||
|
|
||
| let reader = tlvc::TlvcReader::begin(caboose_data.as_slice()) | ||
| .map_err(|e| CabooseError::TlvcError(format!("{e:?}")))?; | ||
| Ok(tlvc_text::dump(reader)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.