Skip to content

Commit

Permalink
add more types to prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty1968 committed Sep 3, 2024
1 parent 4a76d12 commit f1686cf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
16 changes: 16 additions & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -404,4 +419,5 @@ pub enum CaliptraApiError {
expected_max: u32,
actual: u32,
},
UploadFirmwareUnexpectedResponse,
}
2 changes: 1 addition & 1 deletion api/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 10 additions & 14 deletions hw-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -328,6 +326,9 @@ impl From<CaliptraApiError> for ModelError {
expected_max,
actual,
},
CaliptraApiError::UploadFirmwareUnexpectedResponse => {
ModelError::UploadFirmwareUnexpectedResponse
}
}
}
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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],
Expand All @@ -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<Option<Vec<u8>>, 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,
Expand Down Expand Up @@ -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>(
Expand Down
3 changes: 3 additions & 0 deletions hw-model/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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};
2 changes: 1 addition & 1 deletion rom/dev/tests/rom_integration_tests/test_rom_integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit f1686cf

Please sign in to comment.