Skip to content

Commit d23140b

Browse files
committed
rt: add FMC compatiblity checks
Add compatiblity check funcion to test FMC compatiblity in runtime. Signed-off-by: leongross <[email protected]>
1 parent 0d5eb33 commit d23140b

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

error/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ impl CaliptraError {
467467
pub const RUNTIME_REVOKE_EXPORTED_CDI_HANDLE_NOT_FOUND: CaliptraError =
468468
CaliptraError::new_const(0x000E005A);
469469

470+
pub const RUNTIME_FMC_NOT_COMPATIBLE: CaliptraError = CaliptraError::new_const(0x000E005B);
471+
470472
/// FMC Errors
471473
pub const FMC_GLOBAL_NMI: CaliptraError = CaliptraError::new_const(0x000F0001);
472474
pub const FMC_GLOBAL_EXCEPTION: CaliptraError = CaliptraError::new_const(0x000F0002);

runtime/src/compatibility.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*++
2+
3+
Licensed under the Apache-2.0 license.
4+
5+
File Name:
6+
7+
compatibility.rs
8+
9+
Abstract:
10+
11+
File contains compatibility functions to to check if the runtime is
12+
compatible with FMC.
13+
14+
--*/
15+
16+
use caliptra_common::{cprint, FirmwareHandoffTable};
17+
use caliptra_image_types::ImageManifest;
18+
19+
// From the official documentation:
20+
// "The Major and Minor version numbers of the Firmware Handoff Table.
21+
// All FHT versions with the same Major version number must remain backward compatible.""
22+
pub fn is_fmc_compatible(fht: &FirmwareHandoffTable, manifest: &ImageManifest) -> bool {
23+
cprint!(
24+
"[rt] FHT major: {}, Manifest FMC version: {}\n",
25+
fht.fht_major_ver,
26+
manifest.fmc.version
27+
);
28+
u32::from(fht.fht_major_ver) == manifest.fmc.version
29+
}
30+
31+
#[test]
32+
fn test_is_fmc_compatible() {
33+
let mut fht = FirmwareHandoffTable::default();
34+
let mut manifest = ImageManifest::default();
35+
36+
fht.fht_major_ver = 1;
37+
fht.fht_minor_ver = 0;
38+
manifest.fmc.version = 1;
39+
40+
assert!(is_fmc_compatible(&fht, &manifest));
41+
42+
// change minor version should not affect compatibility
43+
fht.fht_minor_ver = 1;
44+
assert!(is_fmc_compatible(&fht, &manifest));
45+
46+
fht.fht_minor_ver = 0xff;
47+
assert!(is_fmc_compatible(&fht, &manifest));
48+
49+
fht.fht_major_ver = 2;
50+
assert!(!is_fmc_compatible(&fht, &manifest));
51+
}

runtime/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Abstract:
1616
mod authorize_and_stash;
1717
mod capabilities;
1818
mod certify_key_extended;
19+
pub mod compatibility;
1920
pub mod dice;
2021
mod disable;
2122
mod dpe_crypto;

runtime/src/main.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ Abstract:
1818
core::arch::global_asm!(include_str!("ext_intr.S"));
1919

2020
use caliptra_cfi_lib_git::CfiCounter;
21-
use caliptra_common::{cprintln, handle_fatal_error};
21+
use caliptra_common::{cprintln, handle_fatal_error, FirmwareHandoffTable};
2222
use caliptra_cpu::{log_trap_record, TrapRecord};
2323
use caliptra_error::CaliptraError;
24+
use caliptra_image_types::ImageManifest;
2425
use caliptra_registers::soc_ifc::SocIfcReg;
25-
use caliptra_runtime::Drivers;
26+
use caliptra_runtime::{compatibility, Drivers};
2627
use core::hint::black_box;
2728

2829
#[cfg(feature = "std")]
@@ -76,11 +77,19 @@ pub extern "C" fn entry_point() -> ! {
7677
handle_fatal_error(e.into());
7778
});
7879

79-
if !drivers.persistent_data.get().fht.is_valid() {
80-
cprintln!("[rt] RT can't load FHT");
80+
let fht: &FirmwareHandoffTable = &drivers.persistent_data.get().fht;
81+
if !fht.is_valid() {
82+
cprintln!("[rt] Runtime can't load FHT");
8183
handle_fatal_error(caliptra_drivers::CaliptraError::RUNTIME_HANDOFF_FHT_NOT_LOADED.into());
8284
}
83-
cprintln!("[rt] RT listening for mailbox commands...");
85+
86+
let manifest: &ImageManifest = &drivers.persistent_data.get().manifest1;
87+
if !compatibility::is_fmc_compatible(fht, manifest) {
88+
cprintln!("[rt] Runtime is not compatible with FMC");
89+
handle_fatal_error(caliptra_drivers::CaliptraError::RUNTIME_FMC_NOT_COMPATIBLE.into());
90+
}
91+
92+
cprintln!("[rt] Runtime listening for mailbox commands...");
8493
if let Err(e) = caliptra_runtime::handle_mailbox_commands(&mut drivers) {
8594
handle_fatal_error(e.into());
8695
}

0 commit comments

Comments
 (0)