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 "sram_ctrl_regs.h"
57 extern char _rom_ext_start_address[];
59 extern const char _rom_chip_info_start[];
62 lifecycle_state_t lc_state;
74 static rom_error_t rom_ext_irq_error(
void) {
82 dbg_printf(
"MCAUSE=%x MEPC=%x MTVAL=%x\r\n", mcause, mepc, mtval);
94 mcause = (mcause & 0x80000000) | ((mcause & 0x7f) << 24);
95 return kErrorRomExtInterrupt + mcause;
99 static uint32_t rom_ext_current_slot(
void) {
100 uint32_t pc = ibex_addr_remap_get(0);
104 asm(
"auipc %[pc], 0;" : [pc]
"=r"(pc));
108 const uint32_t kFlashSlotB =
110 const uint32_t kFlashSlotEnd =
113 if (pc >= kFlashSlotA && pc < kFlashSlotB) {
116 }
else if (pc >= kFlashSlotB && pc < kFlashSlotEnd) {
126 void rom_ext_check_rom_expectations(
void) {
128 SHUTDOWN_IF_ERROR(epmp_state_check());
138 lc_state = lifecycle_state_get();
144 for (int8_t i = 7; i >= 0; --i) {
145 epmp_clear((uint8_t)i);
147 HARDENED_RETURN_IF_ERROR(epmp_state_check());
150 HARDENED_RETURN_IF_ERROR(ast_patch(lc_state));
155 HARDENED_RETURN_IF_ERROR(retention_sram_check_version());
158 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state,
boot_data));
163 void rom_ext_sram_exec(owner_sram_exec_mode_t mode) {
165 case kOwnerSramExecModeEnabled:
170 SRAM_CTRL_EXEC_REG_OFFSET,
173 case kOwnerSramExecModeDisabled:
178 SRAM_CTRL_EXEC_REG_OFFSET,
179 kMultiBitBool4False);
181 case kOwnerSramExecModeDisabledLocked:
186 SRAM_CTRL_EXEC_REG_OFFSET,
187 kMultiBitBool4False);
189 SRAM_CTRL_EXEC_REGWEN_REG_OFFSET,
199 ownership_key_alg_t key_alg = kOwnershipKeyAlgEcdsaP256;
200 RETURN_IF_ERROR(owner_keyring_find_key(
202 sigverify_ecdsa_p256_key_id_get(&
manifest->ecdsa_public_key),
205 dbg_printf(
"app_verify: key=%u alg=%C domain=%C\r\n", verify_key,
207 keyring.
key[verify_key]->key_domain);
217 &usage_constraints_from_hw);
218 hmac_sha256_update(&usage_constraints_from_hw,
219 sizeof(usage_constraints_from_hw));
222 hmac_sha256_update(digest_region.
start, digest_region.
length);
225 hmac_sha256_process();
227 hmac_sha256_final(&act_digest);
230 "Unexpected BL0 digest size.");
233 uint32_t flash_exec = 0;
234 return sigverify_ecdsa_p256_verify(&
manifest->ecdsa_signature,
235 &keyring.
key[verify_key]->
data.ecdsa,
236 &act_digest, &flash_exec);
244 extern char _owner_virtual_start_address[];
245 extern char _owner_virtual_size[];
256 return (lma_addr - (uintptr_t)
manifest +
257 (uintptr_t)_owner_virtual_start_address + CHIP_ROM_EXT_SIZE_MAX);
266 owner_block_measurement(owner_block_key_page(key), &owner_measurement);
276 "Expect the keymgr binding value to be the same size as an application "
278 memcpy(&sealing_binding, key->raw_diversifier,
sizeof(sealing_binding));
282 memset(&sealing_binding, 0x55,
sizeof(sealing_binding));
286 HARDENED_RETURN_IF_ERROR(dice_chain_attestation_owner(
291 HARDENED_RETURN_IF_ERROR(dice_chain_flush_flash());
295 flash_ctrl_cert_info_page_owner_restrict(&kFlashCtrlInfoPageDiceCerts);
299 flash_ctrl_creator_info_pages_lockdown();
300 otp_creator_sw_cfg_lockdown();
302 kOtpSecMmioCreatorSwCfgLockDown);
304 epmp_clear_lock_bits();
306 HARDENED_RETURN_IF_ERROR(epmp_state_check());
316 uintptr_t entry_point = manifest_entry_point_get(
manifest);
320 ibex_addr_remap_1_set((uintptr_t)_owner_virtual_start_address,
321 (uintptr_t)
manifest, (
size_t)_owner_virtual_size);
325 HARDENED_RETURN_IF_ERROR(epmp_state_check());
328 (
epmp_region_t){.start = (uintptr_t)_owner_virtual_start_address,
329 .end = (uintptr_t)_owner_virtual_start_address +
330 (uintptr_t)_owner_virtual_size},
332 HARDENED_RETURN_IF_ERROR(epmp_state_check());
337 text_region.start = owner_vma_get(
manifest, text_region.start);
338 text_region.end = owner_vma_get(
manifest, text_region.end);
339 entry_point = owner_vma_get(
manifest, entry_point);
356 HARDENED_RETURN_IF_ERROR(epmp_state_check());
357 epmp_set_tor(2, text_region, kEpmpPermReadExecute);
362 HARDENED_RETURN_IF_ERROR(epmp_state_check());
365 ibex_addr_remap_lockdown(0);
366 ibex_addr_remap_lockdown(1);
369 HARDENED_RETURN_IF_ERROR(
381 dbg_printf(
"entry: 0x%x\r\n", (
unsigned int)entry_point);
382 ((owner_stage_entry_point *)entry_point)();
384 return kErrorRomExtBootFailed;
388 static rom_error_t boot_svc_next_boot_bl0_slot_handler(
392 uint32_t primary_slot =
boot_svc_msg->next_boot_bl0_slot_req.primary_bl0_slot;
393 rom_error_t error = kErrorOk;
397 if (active_slot != primary_slot) {
398 switch (primary_slot) {
403 HARDENED_RETURN_IF_ERROR(boot_data_write(
boot_data));
406 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state,
boot_data));
410 case kBootSlotUnspecified:
414 error = kErrorBootSvcBadSlot;
421 uint32_t next_slot =
boot_svc_msg->next_boot_bl0_slot_req.next_bl0_slot;
422 switch (launder32(next_slot)) {
429 case kBootSlotUnspecified:
433 error = kErrorBootSvcBadSlot;
436 boot_svc_next_boot_bl0_slot_res_init(error, active_slot,
447 const uint32_t requested_min_sec_ver =
452 if (launder32(requested_min_sec_ver) > current_min_sec_ver) {
453 HARDENED_CHECK_GT(requested_min_sec_ver, current_min_sec_ver);
454 uint32_t max_sec_ver = current_min_sec_ver;
464 manifest = rom_ext_boot_policy_manifest_b_get();
470 if (requested_min_sec_ver <= max_sec_ver) {
471 HARDENED_CHECK_LE(requested_min_sec_ver, max_sec_ver);
476 HARDENED_RETURN_IF_ERROR(boot_data_write(
boot_data));
478 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state,
boot_data));
489 boot_svc_min_bl0_sec_ver_res_init(current_min_sec_ver, kErrorBootSvcBadSecVer,
502 switch (launder32(msg_type)) {
503 case kBootSvcEmptyReqType:
507 case kBootSvcNextBl0SlotReqType:
511 case kBootSvcMinBl0SecVerReqType:
514 case kBootSvcOwnershipUnlockReqType:
517 case kBootSvcOwnershipActivateReqType:
520 case kBootSvcEmptyResType:
521 case kBootSvcNextBl0SlotResType:
522 case kBootSvcMinBl0SecVerResType:
523 case kBootSvcOwnershipUnlockResType:
524 case kBootSvcOwnershipActivateResType:
539 rom_ext_boot_policy_manifests_get(
boot_data);
540 rom_error_t error = kErrorRomExtBootFailed;
541 rom_error_t slot[2] = {0, 0};
545 if (error != kErrorOk) {
549 if (manifests.
ordered[i] == rom_ext_boot_policy_manifest_a_get()) {
551 }
else if (manifests.
ordered[i] == rom_ext_boot_policy_manifest_b_get()) {
554 return kErrorRomExtBootFailed;
562 return kErrorRomExtBootFailed;
569 if (error == kErrorBootPolicyBadIdentifier && error == slot[1]) {
578 HARDENED_RETURN_IF_ERROR(rom_ext_init(
boot_data));
580 dbg_printf(
"Starting ROM_EXT %u.%u\r\n", self->version_major,
581 self->version_minor);
584 HARDENED_RETURN_IF_ERROR(dice_chain_init());
588 boot_log_check_or_init(
boot_log, rom_ext_current_slot(), rom_chip_info);
600 if (error == kErrorWriteBootdataThenReboot) {
605 if (error != kErrorOk) {
606 dbg_printf(
"ownership_init: %x\r\n", error);
614 uint32_t skip_boot_svc = reset_reasons & (1 << kRstmgrReasonLowPowerExit);
615 if (skip_boot_svc == 0) {
617 if (error == kErrorWriteBootdataThenReboot) {
635 dbg_printf(
"rescue: remember to clear break\r\n");
636 uart_enable_receiver();
648 void rom_ext_main(
void) {
649 rom_ext_check_rom_expectations();
654 if (error == kErrorWriteBootdataThenReboot) {
658 shutdown_finalize(error);
662 void rom_ext_interrupt_handler(
void) { shutdown_finalize(rom_ext_irq_error()); }
668 OT_ALIAS(
"rom_ext_interrupt_handler")
669 void rom_ext_exception_handler(
void);
672 OT_ALIAS("rom_ext_interrupt_handler")
673 void rom_ext_nmi_handler(
void);