7 #include "sw/device/lib/base/status.h"
12 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
13 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
14 #include "sw/device/lib/testing/rstmgr_testutils.h"
15 #include "sw/device/lib/testing/test_framework/check.h"
18 #include "sw/device/silicon_creator/lib/boot_data.h"
19 #include "sw/device/silicon_creator/lib/boot_log.h"
20 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
21 #include "sw/device/silicon_creator/lib/manifest.h"
22 #include "sw/device/silicon_creator/lib/manifest_def.h"
23 #include "sw/device/silicon_creator/rom/boot_policy_ptrs.h"
25 #include "flash_ctrl_regs.h"
27 #include "otp_ctrl_regs.h"
29 OTTF_DEFINE_TEST_CONFIG();
32 static dif_rstmgr_t rstmgr;
33 static dif_otp_ctrl_t otp_ctrl;
36 .
ecc_en = kMultiBitBool4True,
37 .high_endurance_en = kMultiBitBool4False,
38 .scramble_en = kMultiBitBool4True,
39 .erase_en = kMultiBitBool4True,
40 .prog_en = kMultiBitBool4True,
41 .rd_en = kMultiBitBool4True};
46 typedef enum rom_ext_slot {
58 kManifestSecurityVersionOffset = 844,
59 kManifestIdOffest = 820,
60 kManifestLengthOffset = 832,
61 kManifestManifestVersionOffset = 824,
62 kManifestSignedRegionEndOffset = 828,
63 kManifestCodeStartOffset = 892,
64 kManifestCodeEndOffset = 896,
65 kManifestEntryPointOffset = 900,
66 kManifestEcdsaPublicKeyOffset = 432,
67 kManifestUsageConstraintsSelectorBitsOffset = 384,
68 kManifestEcdsaSignatureOffset = 0,
69 kManifestExtensionsOffset = 904,
79 kCodeFirstWordOffset = 1,
80 kCodeMiddleWordOffset = 2,
81 kCodeLastWordOffset = 3,
86 kRomExtSlotAFirstPageIndex = 0,
87 kRomExtSlotBFirstPageIndex = FLASH_CTRL_PARAM_REG_PAGES_PER_BANK,
91 kRomExtSlotBFirstAddr =
95 kRomExtSlotAManifestIdAddr = kRomExtSlotAFirstAddr + kManifestIdOffest,
96 kRomExtSlotBManifestIdAddr = kRomExtSlotBFirstAddr + kManifestIdOffest,
99 kFlashBank0DataRegion = 0,
100 kFlashBank1DataRegion = 1,
104 kManifestSecurityVersionOffset);
108 kManifestManifestVersionOffset);
110 kManifestSignedRegionEndOffset);
112 kManifestCodeStartOffset);
115 kManifestEntryPointOffset);
117 kManifestEcdsaPublicKeyOffset);
119 kManifestUsageConstraintsSelectorBitsOffset);
121 kManifestEcdsaSignatureOffset);
123 kManifestExtensionsOffset);
162 {.
offset = kManifestSecurityVersionOffset,
163 .description =
"manifest_security_version"},
164 {.offset = kManifestIdOffest, .description =
"manifest_identifier"},
165 {.offset = kManifestLengthOffset, .description =
"manifest_length"},
166 {.offset = kManifestManifestVersionOffset,
167 .description =
"manifest_manifest_version"},
168 {.offset = kManifestSignedRegionEndOffset,
169 .description =
"manifest_signed_region_end"},
170 {.offset = kManifestCodeStartOffset, .description =
"manifest_code_start"},
171 {.offset = kManifestCodeEndOffset, .description =
"manifest_code_end"},
172 {.offset = kManifestEntryPointOffset,
173 .description =
"manifest_entry_point"},
174 {.offset = kManifestEcdsaPublicKeyOffset,
175 .description =
"manifest_ecdsa_public_key"},
176 {.offset = kManifestUsageConstraintsSelectorBitsOffset,
177 .description =
"manifest_usage_constraints_selector_bits"},
178 {.offset = kManifestEcdsaSignatureOffset,
179 .description =
"manifest_ecdsa_signature"},
180 {.offset = kManifestExtIdSpxKey,
182 .description =
"manifest_extension_spx_public_key"},
183 {.offset = kManifestExtIdSpxSignature,
185 .description =
"manifest_extension_spx_signature"},
188 {.offset = kCodeFirstWordOffset, .description =
"code_first_word"},
191 static void init_peripherals(
void) {
195 CHECK_DIF_OK(dif_otp_ctrl_init(
197 CHECK_DIF_OK(dif_rstmgr_init(
204 static void sw_reset(
void) {
205 rstmgr_testutils_reason_clear();
213 static uint32_t compute_flash_page_index(rom_ext_slot_t rom_ext_slot,
214 uint32_t byte_offset_in_bank) {
215 uint32_t page_idx = rom_ext_slot == kRomExtSlotA ? kRomExtSlotAFirstPageIndex
216 : kRomExtSlotBFirstPageIndex;
217 page_idx += byte_offset_in_bank / FLASH_CTRL_PARAM_BYTES_PER_PAGE;
226 static status_t corrupt_rom_ext_word(rom_ext_slot_t slot,
227 uint32_t offset_to_corrupt) {
228 uint32_t page_idx = compute_flash_page_index(slot, offset_to_corrupt);
229 uint32_t flash_data_bank =
230 slot == kRomExtSlotA ? kFlashBank0DataRegion : kFlashBank1DataRegion;
231 uint32_t first_flash_addr_in_slot =
232 slot == kRomExtSlotA ? kRomExtSlotAFirstAddr : kRomExtSlotBFirstAddr;
235 TRY(flash_ctrl_testutils_data_region_setup_properties(
236 &flash_ctrl, page_idx, flash_data_bank,
237 1, kFlashFullAccessScrambledEcc, NULL));
241 uint32_t word_to_corrupt = 0;
242 TRY(flash_ctrl_testutils_read(
243 &flash_ctrl, first_flash_addr_in_slot + offset_to_corrupt,
244 0, &word_to_corrupt, kDifFlashCtrlPartitionTypeData,
246 LOG_INFO(
"Uncorrupted Flash Word: 0x%08x", word_to_corrupt);
251 uint32_t corrupted_word = ~word_to_corrupt;
252 TRY(flash_ctrl_testutils_write(
253 &flash_ctrl, first_flash_addr_in_slot + offset_to_corrupt,
254 0, &corrupted_word, kDifFlashCtrlPartitionTypeData,
266 uint32_t cause_id = CAUSE_ID;
267 CHECK(cause_id <
ARRAYSIZE(kWords2Corrupt));
268 char *corruption_desc = (
char *)retention_sram_get()->
owner.
reserved;
269 const char *str = kWords2Corrupt[cause_id].
description;
273 corruption_desc[len] = str[len];
281 uint32_t addr_of_corruption = kWords2Corrupt[cause_id].
offset;
282 switch (addr_of_corruption) {
283 case kManifestExtIdSpxKey:
287 case kManifestExtIdSpxSignature:
289 kManifestExtIdSpxSignature);
292 case kCodeFirstWordOffset:
295 case kCodeMiddleWordOffset:
299 case kCodeLastWordOffset:
309 LOG_INFO(
"Slot A self corrupting (%s) ...", corruption_desc);
310 CHECK_STATUS_OK(corrupt_rom_ext_word(kRomExtSlotA, addr_of_corruption));
312 LOG_INFO(
"Slot B self corrupting (%s) ...", corruption_desc);
313 CHECK_STATUS_OK(corrupt_rom_ext_word(kRomExtSlotB, addr_of_corruption));
317 LOG_INFO(
"Resetting chip to trigger load access fault in the ROM ...");