Skip to content

Commit

Permalink
Add capabilities command to ROM.
Browse files Browse the repository at this point in the history
Currently reports just a single bit representing functionality present in v1.0 of ROM.
  • Loading branch information
bluegate010 authored and jhand2 committed Aug 24, 2023
1 parent 98f9a0d commit c76b3ed
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 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 common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"

[dependencies]
bitfield.workspace = true
bitflags.workspace = true
caliptra-drivers.workspace = true
caliptra-image-types = { workspace = true, default-features = false }
caliptra-registers.workspace = true
Expand Down
40 changes: 40 additions & 0 deletions common/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*++
Licensed under the Apache-2.0 license.
File Name:
capabilities.rs
Abstract:
Capability bits
--*/

bitflags::bitflags! {
#[derive(Default, Copy, Clone, Debug)]
pub struct Capabilities : u128 {
// Represents base capabilities present in Caliptra ROM v1.0
const ROM_BASE = 0b0001;
}
}

impl Capabilities {
pub fn to_bytes(&self) -> [u8; 16] {
self.bits().to_be_bytes()
}
}

impl TryFrom<&[u8]> for Capabilities {
type Error = ();
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != 16 {
Err(())
} else {
let capabilities = u128::from_be_bytes(value.try_into().unwrap());
let caps = Capabilities::from_bits(capabilities);
caps.ok_or(())
}
}
}
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod boot_status;
pub mod capabilities;
pub mod checksum;
pub mod crypto;
pub mod dice;
Expand Down
3 changes: 3 additions & 0 deletions common/src/mailbox_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ impl CommandId {
pub const SELF_TEST: Self = Self(0x4650_4C54); // "FPST"
/// The shutdown command.
pub const SHUTDOWN: Self = Self(0x4650_5344); // "FPSD"

// The capabilities command.
pub const CAPABILITIES: Self = Self(0x4341_5053); // "CAPS"
}

impl From<u32> for CommandId {
Expand Down
8 changes: 8 additions & 0 deletions rom/dev/src/flow/cold_reset/fw_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{cprintln, verifier::RomImageVerificationEnv};
use crate::{pcr, wdt};
use caliptra_cfi_derive::cfi_impl_fn;
use caliptra_cfi_lib::{cfi_assert, cfi_assert_eq, cfi_launder};
use caliptra_common::capabilities::Capabilities;
use caliptra_common::mailbox_api::CommandId;
use caliptra_common::{cprint, memory_layout::MAN1_ORG, FuseLogEntryId, RomBootStatus::*};
use caliptra_drivers::*;
Expand Down Expand Up @@ -137,6 +138,13 @@ impl FirmwareProcessor {
txn.start_txn().complete(false)?;
continue;
}
CommandId::CAPABILITIES => {
let mut capabilities = Capabilities::default();
capabilities |= Capabilities::ROM_BASE;

txn.start_txn().send_response(&capabilities.to_bytes())?;
continue;
}
CommandId::FIRMWARE_LOAD => {
// Re-borrow mailbox to work around https://github.com/rust-lang/rust/issues/54663
let txn = mbox
Expand Down
23 changes: 23 additions & 0 deletions rom/dev/tests/test_capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed under the Apache-2.0 license

use caliptra_builder::ImageOptions;
use caliptra_common::{capabilities::Capabilities, mailbox_api::CommandId};
use caliptra_hw_model::{Fuses, HwModel};
use zerocopy::AsBytes;

pub mod helpers;

#[test]
fn test_capabilities() {
let (mut hw, _image_bundle) =
helpers::build_hw_model_and_image_bundle(Fuses::default(), ImageOptions::default());

let response = hw
.mailbox_execute(CommandId::CAPABILITIES.into(), &[])
.unwrap()
.unwrap();

let caps = Capabilities::try_from(response.as_bytes()).unwrap();

assert!(caps.contains(Capabilities::ROM_BASE));
}

0 comments on commit c76b3ed

Please sign in to comment.