From 21abfd4fd2380f5f156e34dcd169a5eba4876c2e Mon Sep 17 00:00:00 2001 From: Arthur Heymans Date: Fri, 25 Oct 2024 11:17:56 +0200 Subject: [PATCH] Add active flow in sw-emulator app Signed-off-by: Arthur Heymans --- rom/dev/Makefile | 13 +++++++++ sw-emulator/app/src/main.rs | 56 ++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/rom/dev/Makefile b/rom/dev/Makefile index d0263c27a4..c4100d2022 100644 --- a/rom/dev/Makefile +++ b/rom/dev/Makefile @@ -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)" \ diff --git a/sw-emulator/app/src/main.rs b/sw-emulator/app/src/main.rs index 2cae70601c..6e5a4f3939 100644 --- a/sw-emulator/app/src/main.rs +++ b/sw-emulator/app/src/main.rs @@ -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; @@ -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::("rom").unwrap(); @@ -267,6 +275,11 @@ fn main() -> io::Result<()> { }, ); + let active_mode = args.get_flag("active-mode"); + + // Clippy seems wrong about this clone not being necessary + #[allow(clippy::redundant_clone)] + let firmware_buffer = current_fw_buf.clone(); let bus_args = CaliptraRootBusArgs { rom: rom_buffer, log_dir: args_log_dir.clone(), @@ -275,12 +288,20 @@ 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 { + 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()); @@ -297,7 +318,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); + } + let soc_ifc = unsafe { caliptra_registers::soc_ifc::RegisterBlock::new_with_mmio( 0x3003_0000 as *mut u32, @@ -457,6 +483,22 @@ fn upload_fw_to_mailbox(mailbox: &mut MailboxInternal, firmware_buffer: Rc,