5 #include "sw/device/silicon_creator/rom_ext/rom_ext.h"
13 #include "sw/device/silicon_creator/lib/base/boot_measurements.h"
16 #include "sw/device/silicon_creator/lib/boot_data.h"
17 #include "sw/device/silicon_creator/lib/boot_log.h"
18 #include "sw/device/silicon_creator/lib/boot_svc/boot_svc_empty.h"
19 #include "sw/device/silicon_creator/lib/boot_svc/boot_svc_header.h"
20 #include "sw/device/silicon_creator/lib/boot_svc/boot_svc_msg.h"
21 #include "sw/device/silicon_creator/lib/cert/dice_chain.h"
22 #include "sw/device/silicon_creator/lib/dbg_print.h"
23 #include "sw/device/silicon_creator/lib/drivers/ast.h"
24 #include "sw/device/silicon_creator/lib/drivers/epmp.h"
25 #include "sw/device/silicon_creator/lib/drivers/flash_ctrl.h"
26 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
27 #include "sw/device/silicon_creator/lib/drivers/ibex.h"
28 #include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
29 #include "sw/device/silicon_creator/lib/drivers/otp.h"
30 #include "sw/device/silicon_creator/lib/drivers/pinmux.h"
31 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
32 #include "sw/device/silicon_creator/lib/drivers/rnd.h"
33 #include "sw/device/silicon_creator/lib/drivers/rstmgr.h"
34 #include "sw/device/silicon_creator/lib/drivers/uart.h"
35 #include "sw/device/silicon_creator/lib/epmp_state.h"
36 #include "sw/device/silicon_creator/lib/manifest.h"
37 #include "sw/device/silicon_creator/lib/manifest_def.h"
38 #include "sw/device/silicon_creator/lib/ownership/ownership.h"
39 #include "sw/device/silicon_creator/lib/ownership/ownership_activate.h"
40 #include "sw/device/silicon_creator/lib/ownership/ownership_key.h"
41 #include "sw/device/silicon_creator/lib/ownership/ownership_unlock.h"
42 #include "sw/device/silicon_creator/lib/shutdown.h"
43 #include "sw/device/silicon_creator/lib/sigverify/ecdsa_p256_key.h"
44 #include "sw/device/silicon_creator/lib/sigverify/rsa_verify.h"
45 #include "sw/device/silicon_creator/lib/sigverify/sigverify.h"
46 #include "sw/device/silicon_creator/rom_ext/rescue.h"
47 #include "sw/device/silicon_creator/rom_ext/rom_ext_boot_policy.h"
48 #include "sw/device/silicon_creator/rom_ext/rom_ext_boot_policy_ptrs.h"
49 #include "sw/device/silicon_creator/rom_ext/rom_ext_manifest.h"
50 #include "sw/device/silicon_creator/rom_ext/sigverify_keys.h"
52 #include "flash_ctrl_regs.h"
54 #include "otp_ctrl_regs.h"
55 #include "sram_ctrl_regs.h"
58 extern char _rom_ext_start_address[];
60 extern const char _rom_chip_info_start[];
63 lifecycle_state_t lc_state;
75 static rom_error_t rom_ext_irq_error(
void) {
83 dbg_printf(
"MCAUSE=%x MEPC=%x MTVAL=%x\r\n", mcause, mepc, mtval);
95 mcause = (mcause & 0x80000000) | ((mcause & 0x7f) << 24);
96 return kErrorRomExtInterrupt + mcause;
100 static uint32_t rom_ext_current_slot(
void) {
101 uint32_t pc = ibex_addr_remap_get(0);
105 asm(
"auipc %[pc], 0;" : [pc]
"=r"(pc));
109 const uint32_t kFlashSlotB =
111 const uint32_t kFlashSlotEnd =
114 if (pc >= kFlashSlotA && pc < kFlashSlotB) {
117 }
else if (pc >= kFlashSlotB && pc < kFlashSlotEnd) {
127 void rom_ext_check_rom_expectations(
void) {
129 SHUTDOWN_IF_ERROR(epmp_state_check());
139 lc_state = lifecycle_state_get();
145 for (int8_t i = 7; i >= 0; --i) {
146 epmp_clear((uint8_t)i);
148 HARDENED_RETURN_IF_ERROR(epmp_state_check());
151 HARDENED_RETURN_IF_ERROR(ast_patch(lc_state));
156 HARDENED_RETURN_IF_ERROR(retention_sram_check_version());
159 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state,
boot_data));
164 void rom_ext_sram_exec(owner_sram_exec_mode_t mode) {
166 case kOwnerSramExecModeEnabled:
171 SRAM_CTRL_EXEC_REG_OFFSET,
174 case kOwnerSramExecModeDisabled:
179 SRAM_CTRL_EXEC_REG_OFFSET,
180 kMultiBitBool4False);
182 case kOwnerSramExecModeDisabledLocked:
187 SRAM_CTRL_EXEC_REG_OFFSET,
188 kMultiBitBool4False);
190 SRAM_CTRL_EXEC_REGWEN_REG_OFFSET,
200 ownership_key_alg_t key_alg = kOwnershipKeyAlgEcdsaP256;
201 RETURN_IF_ERROR(owner_keyring_find_key(
203 sigverify_ecdsa_p256_key_id_get(&
manifest->ecdsa_public_key),
206 dbg_printf(
"app_verify: key=%u alg=%C domain=%C\r\n", verify_key,
208 keyring.
key[verify_key]->key_domain);
218 &usage_constraints_from_hw);
219 hmac_sha256_update(&usage_constraints_from_hw,
220 sizeof(usage_constraints_from_hw));
223 hmac_sha256_update(digest_region.
start, digest_region.
length);
226 hmac_sha256_process();
228 hmac_sha256_final(&act_digest);
231 "Unexpected BL0 digest size.");
234 uint32_t flash_exec = 0;
235 return sigverify_ecdsa_p256_verify(&
manifest->ecdsa_signature,
236 &keyring.
key[verify_key]->
data.ecdsa,
237 &act_digest, &flash_exec);
245 extern char _owner_virtual_start_address[];
246 extern char _owner_virtual_size[];
257 return (lma_addr - (uintptr_t)
manifest +
258 (uintptr_t)_owner_virtual_start_address + CHIP_ROM_EXT_SIZE_MAX);
267 owner_block_measurement(owner_block_key_page(key), &owner_measurement);
277 "Expect the keymgr binding value to be the same size as an application "
279 memcpy(&sealing_binding, key->raw_diversifier,
sizeof(sealing_binding));
283 memset(&sealing_binding, 0x55,
sizeof(sealing_binding));
287 HARDENED_RETURN_IF_ERROR(dice_chain_attestation_owner(
292 HARDENED_RETURN_IF_ERROR(dice_chain_flush_flash());
296 flash_ctrl_cert_info_page_owner_restrict(&kFlashCtrlInfoPageDiceCerts);
300 flash_ctrl_creator_info_pages_lockdown();
301 otp_creator_sw_cfg_lockdown();
303 kOtpSecMmioCreatorSwCfgLockDown);
305 epmp_clear_lock_bits();
307 HARDENED_RETURN_IF_ERROR(epmp_state_check());
317 uintptr_t entry_point = manifest_entry_point_get(
manifest);
321 ibex_addr_remap_1_set((uintptr_t)_owner_virtual_start_address,
322 (uintptr_t)
manifest, (
size_t)_owner_virtual_size);
326 HARDENED_RETURN_IF_ERROR(epmp_state_check());
329 (
epmp_region_t){.start = (uintptr_t)_owner_virtual_start_address,
330 .end = (uintptr_t)_owner_virtual_start_address +
331 (uintptr_t)_owner_virtual_size},
333 HARDENED_RETURN_IF_ERROR(epmp_state_check());
338 text_region.start = owner_vma_get(
manifest, text_region.start);
339 text_region.end = owner_vma_get(
manifest, text_region.end);
340 entry_point = owner_vma_get(
manifest, entry_point);
357 HARDENED_RETURN_IF_ERROR(epmp_state_check());
358 epmp_set_tor(2, text_region, kEpmpPermReadExecute);
363 HARDENED_RETURN_IF_ERROR(epmp_state_check());
366 ibex_addr_remap_lockdown(0);
367 ibex_addr_remap_lockdown(1);
370 HARDENED_RETURN_IF_ERROR(
382 dbg_printf(
"entry: 0x%x\r\n", (
unsigned int)entry_point);
383 ((owner_stage_entry_point *)entry_point)();
385 return kErrorRomExtBootFailed;
389 static rom_error_t boot_svc_next_boot_bl0_slot_handler(
393 uint32_t primary_slot =
boot_svc_msg->next_boot_bl0_slot_req.primary_bl0_slot;
394 rom_error_t error = kErrorOk;
398 if (active_slot != primary_slot) {
399 switch (primary_slot) {
404 HARDENED_RETURN_IF_ERROR(boot_data_write(
boot_data));
407 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state,
boot_data));
411 case kBootSlotUnspecified:
415 error = kErrorBootSvcBadSlot;
422 uint32_t next_slot =
boot_svc_msg->next_boot_bl0_slot_req.next_bl0_slot;
423 switch (launder32(next_slot)) {
430 case kBootSlotUnspecified:
434 error = kErrorBootSvcBadSlot;
437 boot_svc_next_boot_bl0_slot_res_init(error, active_slot,
448 const uint32_t requested_min_sec_ver =
453 if (launder32(requested_min_sec_ver) > current_min_sec_ver) {
454 HARDENED_CHECK_GT(requested_min_sec_ver, current_min_sec_ver);
455 uint32_t max_sec_ver = current_min_sec_ver;
465 manifest = rom_ext_boot_policy_manifest_b_get();
471 if (requested_min_sec_ver <= max_sec_ver) {
472 HARDENED_CHECK_LE(requested_min_sec_ver, max_sec_ver);
477 HARDENED_RETURN_IF_ERROR(boot_data_write(
boot_data));
479 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state,
boot_data));
490 boot_svc_min_bl0_sec_ver_res_init(current_min_sec_ver, kErrorBootSvcBadSecVer,
503 switch (launder32(msg_type)) {
504 case kBootSvcEmptyReqType:
508 case kBootSvcNextBl0SlotReqType:
512 case kBootSvcMinBl0SecVerReqType:
515 case kBootSvcOwnershipUnlockReqType:
518 case kBootSvcOwnershipActivateReqType:
521 case kBootSvcEmptyResType:
522 case kBootSvcNextBl0SlotResType:
523 case kBootSvcMinBl0SecVerResType:
524 case kBootSvcOwnershipUnlockResType:
525 case kBootSvcOwnershipActivateResType:
540 rom_ext_boot_policy_manifests_get(
boot_data);
541 rom_error_t error = kErrorRomExtBootFailed;
542 rom_error_t slot[2] = {0, 0};
546 if (error != kErrorOk) {
550 if (manifests.
ordered[i] == rom_ext_boot_policy_manifest_a_get()) {
552 }
else if (manifests.
ordered[i] == rom_ext_boot_policy_manifest_b_get()) {
555 return kErrorRomExtBootFailed;
563 return kErrorRomExtBootFailed;
570 if (error == kErrorBootPolicyBadIdentifier && error == slot[1]) {
579 HARDENED_RETURN_IF_ERROR(rom_ext_init(
boot_data));
581 dbg_printf(
"Starting ROM_EXT %u.%u\r\n", self->version_major,
582 self->version_minor);
584 uint32_t hash_enforcement =
585 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_EN_OFFSET);
589 dbg_printf(
"info: imm_section hash unenforced\r\n");
593 HARDENED_RETURN_IF_ERROR(dice_chain_init());
597 boot_log_check_or_init(
boot_log, rom_ext_current_slot(), rom_chip_info);
609 if (error == kErrorWriteBootdataThenReboot) {
614 if (error != kErrorOk) {
615 dbg_printf(
"ownership_init: %x\r\n", error);
623 uint32_t skip_boot_svc = reset_reasons & (1 << kRstmgrReasonLowPowerExit);
624 if (skip_boot_svc == 0) {
626 if (error == kErrorWriteBootdataThenReboot) {
644 dbg_printf(
"rescue: remember to clear break\r\n");
645 uart_enable_receiver();
657 void rom_ext_main(
void) {
658 rom_ext_check_rom_expectations();
663 if (error == kErrorWriteBootdataThenReboot) {
667 shutdown_finalize(error);
671 void rom_ext_interrupt_handler(
void) { shutdown_finalize(rom_ext_irq_error()); }
677 OT_ALIAS(
"rom_ext_interrupt_handler")
678 void rom_ext_exception_handler(
void);
681 OT_ALIAS("rom_ext_interrupt_handler")
682 void rom_ext_nmi_handler(
void);