diff --git a/api/src/lib.rs b/api/src/lib.rs index a7c5d49b1c..2353a2d013 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -377,6 +377,21 @@ pub trait SocManager { mbox_read_fifo(self.soc_mbox(), buffer)?; Ok(cmd) } + + /// Upload firmware to the mailbox. + fn upload_fw(&mut self, firmware: &[u8]) -> Result<(), CaliptraApiError> { + let mut resp_bytes = MboxBuffer::default(); + let response = SocManager::mailbox_exec( + self, + CommandId::FIRMWARE_LOAD.into(), + firmware, + &mut resp_bytes, + )?; + if response.is_some() { + return Err(CaliptraApiError::UploadFirmwareUnexpectedResponse); + } + Ok(()) + } } #[derive(Debug, Eq, PartialEq)] pub enum CaliptraApiError { @@ -404,4 +419,5 @@ pub enum CaliptraApiError { expected_max: u32, actual: u32, }, + UploadFirmwareUnexpectedResponse, } diff --git a/api/src/prelude.rs b/api/src/prelude.rs index 639fa50501..10ade3bb8c 100644 --- a/api/src/prelude.rs +++ b/api/src/prelude.rs @@ -3,7 +3,7 @@ pub use crate::checksum::{calc_checksum, verify_checksum}; pub use crate::mailbox::{mbox_read_fifo, mbox_write_fifo, MboxBuffer}; -pub use crate::mailbox::{MailboxReqHeader, MailboxRespHeader, Request, Response}; +pub use crate::mailbox::{CommandId, MailboxReqHeader, MailboxRespHeader, Request, Response}; pub use crate::CaliptraApiError; pub use crate::MailboxRecvTxn; pub use crate::SocManager; diff --git a/hw-model/src/lib.rs b/hw-model/src/lib.rs index ebe4e741a3..24fac53a41 100644 --- a/hw-model/src/lib.rs +++ b/hw-model/src/lib.rs @@ -1,7 +1,6 @@ // Licensed under the Apache-2.0 license use caliptra_api::{self as api}; -use caliptra_api_types as api_types; use caliptra_hw_model_types::{ ErrorInjectionMode, EtrngResponse, HexBytes, HexSlice, RandomEtrngResponses, RandomNibbles, @@ -38,7 +37,6 @@ mod model_fpga_realtime; mod output; mod rv32_builder; -pub use api_types::{DeviceLifecycle, Fuses, SecurityState, U4}; pub use caliptra_emu_bus::BusMmio; use output::ExitStatus; pub use output::Output; @@ -328,6 +326,9 @@ impl From for ModelError { expected_max, actual, }, + CaliptraApiError::UploadFirmwareUnexpectedResponse => { + ModelError::UploadFirmwareUnexpectedResponse + } } } } @@ -402,9 +403,6 @@ impl Display for ModelError { } } -/// Firmware Load Command Opcode -const FW_LOAD_CMD_OPCODE: u32 = 0x4657_4C44; - /// Stash Measurement Command Opcode. const STASH_MEASUREMENT_CMD_OPCODE: u32 = 0x4D45_4153; @@ -716,7 +714,7 @@ pub trait HwModel: SocManager { SocManager::mailbox_exec(self, cmd, buf, resp_data).map_err(ModelError::from) } - fn mailbox_execute_alloc( + fn mailbox_execute_heap( &mut self, cmd: u32, buf: &[u8], @@ -743,6 +741,11 @@ pub trait HwModel: SocManager { SocManager::start_mailbox_exec(self, cmd, buf).map_err(ModelError::from) } + fn finish_mailbox_execute_heap(&mut self) -> std::result::Result>, ModelError> { + let mut resp_buffer = MboxBuffer::default(); + self.finish_mailbox_execute(&mut resp_buffer)?; + Ok(Some(resp_buffer.as_slice().to_vec())) + } /// Wait for the response to a previous call to `start_mailbox_execute()`. fn finish_mailbox_execute<'r>( &mut self, @@ -837,14 +840,7 @@ pub trait HwModel: SocManager { /// Upload firmware to the mailbox. fn upload_firmware(&mut self, firmware: &[u8]) -> Result<(), ModelError> { - let mut resp_bytes = MboxBuffer::default(); - let response = - SocManager::mailbox_exec(self, FW_LOAD_CMD_OPCODE, firmware, &mut resp_bytes) - .map_err(ModelError::from)?; - if response.is_some() { - return Err(ModelError::UploadFirmwareUnexpectedResponse); - } - Ok(()) + SocManager::upload_fw(self, firmware).map_err(ModelError::from) } fn wait_for_mailbox_receive<'a, 'b>( diff --git a/hw-model/src/prelude.rs b/hw-model/src/prelude.rs index 9fddf072b5..1a8e5bc485 100644 --- a/hw-model/src/prelude.rs +++ b/hw-model/src/prelude.rs @@ -1,4 +1,7 @@ // Licensed under the Apache-2.0 license pub use crate::model_emulated::ModelEmulated; +pub use crate::HwModel; +pub use crate::{BootParams, DefaultHwModel, InitParams, ModelError}; pub use caliptra_api::prelude::*; +pub use caliptra_api_types::{DeviceLifecycle, Fuses, SecurityState, U4}; diff --git a/rom/dev/tests/rom_integration_tests/test_rom_integrity.rs b/rom/dev/tests/rom_integration_tests/test_rom_integrity.rs index 9fddf5bf18..0208181fd6 100644 --- a/rom/dev/tests/rom_integration_tests/test_rom_integrity.rs +++ b/rom/dev/tests/rom_integration_tests/test_rom_integrity.rs @@ -79,7 +79,7 @@ fn test_read_rom_info_from_fmc() { // 0x1000_0008 is test-fmc/read_rom_info() let rom_info_from_fw = RomInfo::read_from( - hw.mailbox_execute_alloc(0x1000_0008, &[]) + hw.mailbox_execute_heap(0x1000_0008, &[]) .unwrap() .unwrap() .as_slice(),