Skip to content

Commit

Permalink
Add active flow in sw-emulator app
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Heymans <[email protected]>
  • Loading branch information
ArthurHeymans committed Oct 25, 2024
1 parent 881c7f8 commit ffb2f2f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
13 changes: 13 additions & 0 deletions rom/dev/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ run: build-emu build-fw-image build-rom
--recovery-image-fw $(TARGET_DIR)/caliptra-rom-test-fw \
--device-lifecycle unprovisioned \

run-active: build-emu build-fw-image build-rom
cargo \
"--config=$(EXTRA_CARGO_CONFIG)" \
run \
-p caliptra-emu \
-- \
--req-idevid-csr \
--idevid-key-id-algo sha1 \
--rom $(TARGET_DIR)/caliptra-rom.bin \
--recovery-image-fw $(TARGET_DIR)/caliptra-rom-test-fw \
--device-lifecycle unprovisioned \
--active-mode \

run-update: build-emu build-fw-image build-rom
cargo \
"--config=$(EXTRA_CARGO_CONFIG)" \
Expand Down
55 changes: 48 additions & 7 deletions sw-emulator/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ use tock_registers::register_bitfields;
/// Firmware Load Command Opcode
const FW_LOAD_CMD_OPCODE: u32 = 0x4657_4C44;

/// Recovery register interface download Command Opcode
const RI_DOWNLOAD_FIRMWARE: u32 = 0x5249_4644;

/// The number of CPU clock cycles it takes to write the firmware to the mailbox.
const FW_WRITE_TICKS: u64 = 1000;

Expand Down Expand Up @@ -162,6 +165,11 @@ fn main() -> io::Result<()> {
.value_parser(value_parser!(u64))
.default_value(&(EXPECTED_CALIPTRA_BOOT_TIME_IN_CYCLES.to_string()))
)
.arg(
arg!(--"active-mode" ... "Active mode: get image update via recovery register interface")
.required(false)
.action(ArgAction::SetTrue)
)
.get_matches();

let args_rom = args.get_one::<PathBuf>("rom").unwrap();
Expand Down Expand Up @@ -267,6 +275,9 @@ fn main() -> io::Result<()> {
},
);

let active_mode = args.get_flag("active-mode");

let firmware_buffer = current_fw_buf.clone();
let bus_args = CaliptraRootBusArgs {
rom: rom_buffer,
log_dir: args_log_dir.clone(),
Expand All @@ -275,12 +286,21 @@ fn main() -> io::Result<()> {
0xFF => exit(0x00),
_ => print!("{}", val as char),
}),
ready_for_fw_cb: ReadyForFwCb::new(move |args| {
let firmware_buffer = current_fw_buf.clone();
args.schedule_later(FW_WRITE_TICKS, move |mailbox: &mut MailboxInternal| {
upload_fw_to_mailbox(mailbox, firmware_buffer);
});
}),
ready_for_fw_cb: if active_mode {
ReadyForFwCb::new(move |args| {
args.schedule_later(FW_WRITE_TICKS, move |mailbox: &mut MailboxInternal| {
rri_download(mailbox)
})
})
} else {
let firmware_buffer = firmware_buffer.clone();
ReadyForFwCb::new(move |args| {
let firmware_buffer = firmware_buffer.clone();
args.schedule_later(FW_WRITE_TICKS, move |mailbox: &mut MailboxInternal| {
upload_fw_to_mailbox(mailbox, firmware_buffer);
});
})
},
security_state,
upload_update_fw: UploadUpdateFwCb::new(move |mailbox: &mut MailboxInternal| {
upload_fw_to_mailbox(mailbox, update_fw_buf.clone());
Expand All @@ -297,7 +317,12 @@ fn main() -> io::Result<()> {
..Default::default()
};

let root_bus = CaliptraRootBus::new(&clock, bus_args);
let mut root_bus = CaliptraRootBus::new(&clock, bus_args);
// Populate the RRI data
if active_mode {
root_bus.recovery.cms_data = Some(current_fw_buf.clone());
}

let soc_ifc = unsafe {
caliptra_registers::soc_ifc::RegisterBlock::new_with_mmio(
0x3003_0000 as *mut u32,
Expand Down Expand Up @@ -457,6 +482,22 @@ fn upload_fw_to_mailbox(mailbox: &mut MailboxInternal, firmware_buffer: Rc<Vec<u
soc_mbox.execute().write(|w| w.execute(true));
}

fn rri_download(mailbox: &mut MailboxInternal) {
let soc_mbox = mailbox.as_external().regs();
// Write the cmd to mailbox.

assert!(!soc_mbox.lock().read().lock());

// Set command
soc_mbox.cmd().write(|_| RI_DOWNLOAD_FIRMWARE);

// No data to send so set len to 0
soc_mbox.dlen().write(|_| 0);

// Execute
soc_mbox.execute().write(|w| w.execute(true));
}

fn download_idev_id_csr(
mailbox: &mut MailboxInternal,
path: Rc<PathBuf>,
Expand Down

0 comments on commit ffb2f2f

Please sign in to comment.