Skip to content

Commit

Permalink
Use manifest's load_addr instead of hard-coded values.
Browse files Browse the repository at this point in the history
Also avoid spinning in the test if the self-test fails.
  • Loading branch information
bluegate010 authored and jhand2 committed Feb 21, 2024
1 parent 7e6331d commit 2a4216a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
30 changes: 15 additions & 15 deletions runtime/src/fips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ pub mod fips_self_test_cmd {
use crate::RtBootStatus::{RtFipSelfTestComplete, RtFipSelfTestStarted};
use caliptra_cfi_lib_git::cfi_assert_eq_8_words;
use caliptra_common::HexBytes;
use caliptra_common::{
verifier::FirmwareImageVerificationEnv, FMC_ORG, FMC_SIZE, RUNTIME_ORG, RUNTIME_SIZE,
};
use caliptra_common::{verifier::FirmwareImageVerificationEnv, FMC_SIZE, RUNTIME_SIZE};
use caliptra_drivers::{ResetReason, ShaAccLockState};
use caliptra_image_types::RomInfo;
use caliptra_image_types::{ImageTocEntry, RomInfo};
use caliptra_image_verify::ImageVerifier;
use zerocopy::AsBytes;

// Helper function to create a slice from a memory region
unsafe fn create_slice(org: u32, size: usize) -> &'static [u8] {
let ptr = org as *mut u8;
core::slice::from_raw_parts(ptr, size)
unsafe fn create_slice(toc: &ImageTocEntry) -> &'static [u8] {
let ptr = toc.load_addr as *mut u8;
core::slice::from_raw_parts(ptr, toc.size as usize)
}
pub enum SelfTestStatus {
Idle,
Expand All @@ -89,18 +87,20 @@ pub mod fips_self_test_cmd {
env.mbox
.copy_bytes_to_mbox(env.persistent_data.get().manifest1.as_bytes())?;

let fmc_size = env.persistent_data.get().manifest1.fmc.size;
if fmc_size > FMC_SIZE {
let fmc_toc = &env.persistent_data.get().manifest1.fmc;
let rt_toc = &env.persistent_data.get().manifest1.runtime;

if fmc_toc.size > FMC_SIZE {
return Err(CaliptraError::RUNTIME_INVALID_FMC_SIZE);
}
let fmc = unsafe { create_slice(FMC_ORG, fmc_size as usize) };
env.mbox.copy_bytes_to_mbox(fmc.as_bytes())?;

let runtime_size = env.persistent_data.get().manifest1.runtime.size;
if runtime_size > RUNTIME_SIZE {
if rt_toc.size > RUNTIME_SIZE {
return Err(CaliptraError::RUNTIME_INVALID_RUNTIME_SIZE);
}
let rt = unsafe { create_slice(RUNTIME_ORG, runtime_size as usize) };

let fmc = unsafe { create_slice(&fmc_toc) };
let rt = unsafe { create_slice(&rt_toc) };

env.mbox.copy_bytes_to_mbox(fmc.as_bytes())?;
env.mbox.copy_bytes_to_mbox(rt.as_bytes())?;

let mut venv = FirmwareImageVerificationEnv {
Expand Down
16 changes: 10 additions & 6 deletions test/tests/fips_test_suite/fips_cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ fn test_fips_cmds<T: HwModel>(hw: &mut T, fmc_version: u32, app_version: u32) {
break;
}
}
_ => {
Ok(None)
| Err(ModelError::MailboxCmdFailed(0xE0015)) // RUNTIME_SELF_TEST_IN_PROGRESS
| Err(ModelError::MailboxCmdFailed(0xE0016)) // RUNTIME_SELF_TEST_NOT_STARTED
| Err(ModelError::UnableToLockMailbox) => {
// Give FW time to run
let mut cycle_count = 10000;
hw.step_until(|_| -> bool {
cycle_count -= 1;
cycle_count == 0
});
for _ in 0..10000 {
hw.step();
}
}
Err(e) => {
assert_eq!(e, ModelError::MailboxCmdFailed(0));
}
}
}
Expand Down

0 comments on commit 2a4216a

Please sign in to comment.