5 #include "sw/device/silicon_creator/rom/rom.h"
18 #include "sw/device/silicon_creator/lib/base/boot_measurements.h"
20 #include "sw/device/silicon_creator/lib/base/static_critical_version.h"
21 #include "sw/device/silicon_creator/lib/boot_data.h"
22 #include "sw/device/silicon_creator/lib/boot_log.h"
23 #include "sw/device/silicon_creator/lib/cfi.h"
24 #include "sw/device/silicon_creator/lib/chip_info.h"
25 #include "sw/device/silicon_creator/lib/drivers/alert.h"
26 #include "sw/device/silicon_creator/lib/drivers/ast.h"
27 #include "sw/device/silicon_creator/lib/drivers/flash_ctrl.h"
28 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
29 #include "sw/device/silicon_creator/lib/drivers/ibex.h"
30 #include "sw/device/silicon_creator/lib/drivers/keymgr.h"
31 #include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
32 #include "sw/device/silicon_creator/lib/drivers/otp.h"
33 #include "sw/device/silicon_creator/lib/drivers/pinmux.h"
34 #include "sw/device/silicon_creator/lib/drivers/pwrmgr.h"
35 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
36 #include "sw/device/silicon_creator/lib/drivers/rnd.h"
37 #include "sw/device/silicon_creator/lib/drivers/rstmgr.h"
38 #include "sw/device/silicon_creator/lib/drivers/sensor_ctrl.h"
39 #include "sw/device/silicon_creator/lib/drivers/uart.h"
40 #include "sw/device/silicon_creator/lib/drivers/watchdog.h"
41 #include "sw/device/silicon_creator/lib/error.h"
42 #include "sw/device/silicon_creator/lib/otbn_boot_services.h"
43 #include "sw/device/silicon_creator/lib/shutdown.h"
44 #include "sw/device/silicon_creator/lib/sigverify/sigverify.h"
45 #include "sw/device/silicon_creator/lib/stack_utilization.h"
46 #include "sw/device/silicon_creator/rom/boot_policy.h"
47 #include "sw/device/silicon_creator/rom/boot_policy_ptrs.h"
48 #include "sw/device/silicon_creator/rom/bootstrap.h"
49 #include "sw/device/silicon_creator/rom/rom_epmp.h"
50 #include "sw/device/silicon_creator/rom/sigverify_keys_ecdsa_p256.h"
51 #include "sw/device/silicon_creator/rom/sigverify_keys_spx.h"
52 #include "sw/device/silicon_creator/rom/sigverify_otp_keys.h"
54 #include "hmac_regs.h"
56 #include "otp_ctrl_regs.h"
57 #include "rstmgr_regs.h"
78 #define ROM_CFI_FUNC_COUNTERS_TABLE(X) \
79 X(kCfiRomMain, 0x14b) \
80 X(kCfiRomInit, 0x7dc) \
81 X(kCfiRomVerify, 0x5a7) \
82 X(kCfiRomTryBoot, 0x235) \
83 X(kCfiRomPreBootCheck, 0x43a) \
88 CFI_DEFINE_COUNTERS(rom_counters, ROM_CFI_FUNC_COUNTERS_TABLE);
91 lifecycle_state_t lc_state = (lifecycle_state_t)0;
99 uint32_t flash_ecc_exc_handler_en;
101 uint32_t reset_reason_check;
103 static inline bool rom_console_enabled(
void) {
104 return otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_BANNER_EN_OFFSET) !=
118 static void rom_banner(
void) {
119 if (!rom_console_enabled()) {
123 const uint64_t kTitle1 = 0x617469546e65704f;
125 const uint32_t kTitle2 = 0x3a6e;
126 const uint32_t kNewline = 0x0a0d;
128 lifecycle_hw_rev_get(&hw);
129 uart_write_imm(kTitle1);
130 uart_write_imm(kTitle2);
131 uart_write_hex(hw.silicon_creator_id,
sizeof(hw.silicon_creator_id),
'-');
132 uart_write_hex(hw.product_id,
sizeof(hw.product_id),
'-');
133 uart_write_hex(hw.revision_id,
sizeof(hw.revision_id), kNewline);
139 static void rom_bootstrap_message(
void) {
141 const uint64_t kBootstrap1 = 0x61727473746f6f62;
143 const uint64_t kBootstrap2 = 0x0a0d313a70;
144 uart_write_imm(kBootstrap1);
145 uart_write_imm(kBootstrap2);
152 static rom_error_t rom_init(
void) {
153 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomInit, 1);
155 uint32_t reset_reasons = rstmgr_reason_get();
159 OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_RESET_REASON_CHECK_VALUE_OFFSET) &
161 if (reset_reasons != (1U << RSTMGR_RESET_INFO_LOW_POWER_EXIT_BIT)) {
181 static_critical_version = kStaticCriticalVersion2;
186 CSR_WRITE(CSR_REG_SECURESEED, rnd_uint32());
189 uint32_t cpuctrl_csr;
190 CSR_READ(CSR_REG_CPUCTRL, &cpuctrl_csr);
193 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_CPUCTRL_OFFSET));
196 lc_state = lifecycle_state_get();
199 rom_epmp_config_debug_rom(lc_state);
206 watchdog_init(lc_state);
210 HARDENED_RETURN_IF_ERROR(sensor_ctrl_configure(lc_state));
211 pwrmgr_cdc_sync(kSensorCtrlSyncCycles);
217 HARDENED_RETURN_IF_ERROR(shutdown_init(lc_state));
221 flash_ecc_exc_handler_en = otp_read32(
222 OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_FLASH_ECC_EXC_HANDLER_EN_OFFSET);
225 rom_epmp_state_init(lc_state);
228 HARDENED_RETURN_IF_ERROR(ast_check(lc_state));
232 uint32_t reset_mask =
233 (1 << kRstmgrReasonPowerOn) |
234 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_RET_RAM_RESET_MASK_OFFSET);
235 if ((reset_reasons & reset_mask) != 0) {
236 retention_sram_init();
238 retention_sram_readback_enable(
239 otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_SRAM_READBACK_EN_OFFSET) >>
254 retention_sram_get()->
version = kRetentionSramVersion4;
267 reset_reason_check = launder32(reset_reason_check) ^ rstmgr_reason_get();
270 OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_RESET_REASON_CHECK_VALUE_OFFSET) >>
274 if (launder32(check_val) == reset_reason_check) {
278 return kErrorRomResetReasonFault;
287 OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_PRESERVE_RESET_REASON_EN_OFFSET) !=
289 rstmgr_reason_clear(reset_reasons);
295 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomInit, 2);
311 uint32_t *flash_exec) {
317 const uint32_t extra_word = UINT32_MAX;
318 const uint32_t *anti_rollback = NULL;
319 size_t anti_rollback_len = 0;
322 anti_rollback = &extra_word;
323 anti_rollback_len =
sizeof(extra_word);
331 HARDENED_RETURN_IF_ERROR(otbn_boot_app_load());
332 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomVerify, 1);
335 HARDENED_RETURN_IF_ERROR(sigverify_otp_keys_init(&sigverify_ctx));
338 HARDENED_RETURN_IF_ERROR(sigverify_ecdsa_p256_key_get(
340 sigverify_ecdsa_p256_key_id_get(&
manifest->ecdsa_public_key), lc_state,
344 sigverify_spx_config_id_t spx_config = 0;
346 uint32_t sigverify_spx_en = sigverify_spx_verify_enabled(lc_state);
347 if (launder32(sigverify_spx_en) != kSigverifySpxDisabledOtp) {
349 HARDENED_RETURN_IF_ERROR(manifest_ext_get_spx_key(
manifest, &ext_spx_key));
350 HARDENED_RETURN_IF_ERROR(sigverify_spx_key_get(
351 &sigverify_ctx, sigverify_spx_key_id_get(&ext_spx_key->
key), lc_state,
352 &spx_key, &spx_config));
354 HARDENED_RETURN_IF_ERROR(
355 manifest_ext_get_spx_signature(
manifest, &ext_spx_signature));
356 spx_signature = &ext_spx_signature->
signature;
367 hmac_sha256_update(anti_rollback, anti_rollback_len);
373 &usage_constraints_from_hw);
374 hmac_sha256_update(&usage_constraints_from_hw,
375 sizeof(usage_constraints_from_hw));
379 hmac_sha256_update(digest_region.
start, digest_region.
length);
380 hmac_sha256_process();
382 hmac_sha256_final(&act_digest);
385 "Unexpected ROM_EXT digest size.");
389 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomVerify, 2);
397 if (rnd_uint32() < 0x80000000) {
398 HARDENED_RETURN_IF_ERROR(sigverify_ecdsa_p256_verify(
399 &
manifest->ecdsa_signature, ecdsa_key, &act_digest, flash_exec));
401 return sigverify_spx_verify(
402 spx_signature, spx_key, spx_config, lc_state,
403 &usage_constraints_from_hw,
sizeof(usage_constraints_from_hw),
404 anti_rollback, anti_rollback_len, digest_region.
start,
405 digest_region.
length, &act_digest, flash_exec);
407 HARDENED_RETURN_IF_ERROR(sigverify_spx_verify(
408 spx_signature, spx_key, spx_config, lc_state,
409 &usage_constraints_from_hw,
sizeof(usage_constraints_from_hw),
410 anti_rollback, anti_rollback_len, digest_region.
start,
411 digest_region.
length, &act_digest, flash_exec));
413 return sigverify_ecdsa_p256_verify(&
manifest->ecdsa_signature, ecdsa_key,
414 &act_digest, flash_exec);
422 extern char _rom_ext_virtual_start_address[];
423 extern char _rom_ext_virtual_size[];
433 uintptr_t lma_addr) {
434 return (lma_addr - (uintptr_t)
manifest +
435 (uintptr_t)_rom_ext_virtual_start_address);
444 static void rom_pre_boot_check(
void) {
445 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 1);
448 SHUTDOWN_IF_ERROR(alert_config_check(lc_state));
449 SHUTDOWN_IF_ERROR(rnd_health_config_check(lc_state));
450 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 2);
453 lifecycle_state_t lc_state_check = lifecycle_state_get();
454 if (launder32(lc_state_check) != lc_state) {
458 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 3);
461 rom_error_t boot_data_ok = boot_data_check(&
boot_data);
462 if (launder32(boot_data_ok) != kErrorOk) {
466 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 4);
469 SHUTDOWN_IF_ERROR(epmp_state_check());
470 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 5);
473 uint32_t cpuctrl_csr;
474 uint32_t cpuctrl_otp =
475 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_CPUCTRL_OFFSET);
476 CSR_READ(CSR_REG_CPUCTRL, &cpuctrl_csr);
480 if (launder32(cpuctrl_csr) != cpuctrl_otp) {
486 rstmgr_info_en_check(retention_sram_get()->creator.reset_reasons));
487 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 6);
490 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomPreBootCheck, 7);
500 static rom_error_t rom_measure_otp_partitions(
508 (OTP_CTRL_CREATOR_SW_CFG_DIGEST_CREATOR_SW_CFG_DIGEST_FIELD_WIDTH *
509 OTP_CTRL_CREATOR_SW_CFG_DIGEST_MULTIREG_COUNT / 8) ==
sizeof(uint64_t),
510 "CreatorSwCfg OTP partition digest no longer 64 bits.");
512 (OTP_CTRL_OWNER_SW_CFG_DIGEST_OWNER_SW_CFG_DIGEST_FIELD_WIDTH *
513 OTP_CTRL_OWNER_SW_CFG_DIGEST_MULTIREG_COUNT / 8) ==
sizeof(uint64_t),
514 "OwnerSwCfg OTP partition digest no longer 64 bits.");
517 OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
518 OTP_CTRL_CREATOR_SW_CFG_DIGEST_0_REG_OFFSET),
522 OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
523 OTP_CTRL_OWNER_SW_CFG_DIGEST_0_REG_OFFSET),
526 kHmacDigestNumBytes);
527 hmac_sha256_process();
529 hmac_sha256_final(&otp_measurement);
530 memcpy(measurement->data, otp_measurement.digest, kHmacDigestNumBytes);
546 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomBoot, 1);
547 HARDENED_RETURN_IF_ERROR(sc_keymgr_state_check(kScKeymgrStateReset));
551 manifest == boot_policy_manifest_a_get() ? kBootSlotA : kBootSlotB;
557 uint32_t use_otp_measurement =
558 otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_KEYMGR_OTP_MEAS_EN_OFFSET);
561 rom_measure_otp_partitions(&otp_measurement);
562 attestation_measurement = &otp_measurement;
569 kScKeymgrSecMmioCreatorMaxVerSet);
578 uintptr_t entry_point = manifest_entry_point_get(
manifest);
582 ibex_addr_remap_0_set((uintptr_t)_rom_ext_virtual_start_address,
583 (uintptr_t)
manifest, (
size_t)_rom_ext_virtual_size);
587 HARDENED_RETURN_IF_ERROR(epmp_state_check());
588 rom_epmp_unlock_rom_ext_r(
589 (
epmp_region_t){.start = (uintptr_t)_rom_ext_virtual_start_address,
590 .end = (uintptr_t)_rom_ext_virtual_start_address +
591 (uintptr_t)_rom_ext_virtual_size});
595 text_region.start = rom_ext_vma_get(
manifest, text_region.start);
596 text_region.end = rom_ext_vma_get(
manifest, text_region.end);
597 entry_point = rom_ext_vma_get(
manifest, entry_point);
607 HARDENED_RETURN_IF_ERROR(epmp_state_check());
608 rom_epmp_unlock_rom_ext_rx(text_region);
610 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomBoot, 2, kCfiRomPreBootCheck);
611 rom_pre_boot_check();
612 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomBoot, 4);
613 CFI_FUNC_COUNTER_CHECK(rom_counters, kCfiRomPreBootCheck, 8);
616 flash_ctrl_exec_set(flash_exec);
628 kCfiRomTryBootManifest0Val = 3 * kCfiIncrement + kCfiRomTryBootVal0,
633 kCfiRomTryBootManifest1Val = 10 * kCfiIncrement + kCfiRomTryBootVal0,
636 switch (launder32(rom_counters[kCfiRomTryBoot])) {
637 case kCfiRomTryBootManifest0Val:
639 kCfiRomTryBootManifest0Val);
640 manifest_check = boot_policy_manifests_get().
ordered[0];
642 case kCfiRomTryBootManifest1Val:
644 kCfiRomTryBootManifest1Val);
645 manifest_check = boot_policy_manifests_get().
ordered[1];
652 #if OT_BUILD_FOR_STATIC_ANALYZER
653 assert(manifest_check != NULL);
659 manifest_entry_point_get(manifest_check)),
665 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomBoot, 5);
668 stack_utilization_print();
671 uint32_t rom_ext_immutable_section_enabled =
672 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_EN_OFFSET);
676 uintptr_t immutable_rom_ext_start_offset = (uintptr_t)otp_read32(
677 OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_START_OFFSET_OFFSET);
678 size_t immutable_rom_ext_length = (size_t)otp_read32(
679 OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_LENGTH_OFFSET);
680 uintptr_t immutable_rom_ext_entry_point =
681 (uintptr_t)
manifest + immutable_rom_ext_start_offset;
685 immutable_rom_ext_entry_point =
686 rom_ext_vma_get(
manifest, immutable_rom_ext_entry_point);
694 hmac_sha256_update(&immutable_rom_ext_start_offset,
696 hmac_sha256_update(&immutable_rom_ext_length,
sizeof(
size_t));
697 hmac_sha256_update((
const void *)immutable_rom_ext_entry_point,
698 immutable_rom_ext_length);
699 hmac_sha256_process();
701 hmac_sha256_final(&actual_immutable_section_digest);
706 otp_read(OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_SHA256_HASH_OFFSET,
707 immutable_rom_ext_hash.digest, kHmacDigestNumWords);
708 for (
size_t i = 0; i < kHmacDigestNumWords; ++i) {
710 actual_immutable_section_digest.digest[i]);
712 ((rom_ext_entry_point *)immutable_rom_ext_entry_point)();
718 ((rom_ext_entry_point *)entry_point)();
719 return kErrorRomBootFailed;
728 static rom_error_t rom_try_boot(
void) {
729 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomTryBoot, 1);
732 HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state, &
boot_data));
735 uint32_t flash_exec = 0;
737 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomTryBoot, 2, kCfiRomVerify);
738 rom_error_t error = rom_verify(manifests.
ordered[0], &flash_exec);
739 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomTryBoot, 4);
741 if (launder32(error) == kErrorOk) {
743 CFI_FUNC_COUNTER_CHECK(rom_counters, kCfiRomVerify, 3);
744 CFI_FUNC_COUNTER_INIT(rom_counters, kCfiRomTryBoot);
745 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomTryBoot, 1, kCfiRomBoot);
746 HARDENED_RETURN_IF_ERROR(rom_boot(manifests.
ordered[0], flash_exec));
747 return kErrorRomBootFailed;
750 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomTryBoot, 5, kCfiRomVerify);
751 HARDENED_RETURN_IF_ERROR(rom_verify(manifests.
ordered[1], &flash_exec));
752 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomTryBoot, 7);
753 CFI_FUNC_COUNTER_CHECK(rom_counters, kCfiRomVerify, 3);
755 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomTryBoot, 8, kCfiRomBoot);
756 HARDENED_RETURN_IF_ERROR(rom_boot(manifests.
ordered[1], flash_exec));
757 return kErrorRomBootFailed;
760 void rom_main(
void) {
761 CFI_FUNC_COUNTER_INIT(rom_counters, kCfiRomMain);
763 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomMain, 1, kCfiRomInit);
764 SHUTDOWN_IF_ERROR(rom_init());
765 CFI_FUNC_COUNTER_INCREMENT(rom_counters, kCfiRomMain, 3);
766 CFI_FUNC_COUNTER_CHECK(rom_counters, kCfiRomInit, 3);
773 rom_bootstrap_message();
775 shutdown_finalize(bootstrap());
780 CFI_FUNC_COUNTER_PREPCALL(rom_counters, kCfiRomMain, 4, kCfiRomTryBoot);
781 shutdown_finalize(rom_try_boot());