Software APIs
immutable_rom_ext_section_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
8 #include "sw/device/lib/base/status.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
12 #include "sw/device/silicon_creator/lib/drivers/otp.h"
13 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
14 #include "sw/device/silicon_creator/lib/drivers/uart.h"
15 
17 
18 OTTF_DEFINE_TEST_CONFIG();
19 
20 enum {
21  kImmutableRomExtSectionHashSizeIn32BitWords =
22  OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_SHA256_HASH_SIZE /
23  sizeof(uint32_t),
24  kSramValuePass = 0x53534150, // PASS
25 };
26 
27 OT_USED OT_SECTION(".rom_ext_immutable") void rom_ext_non_mutable(void) {
28  // Print "Immutable" to the UART console.
29  // l b a t u m m I
30  const uint64_t kStr1 = 0x6c626174756d6d49;
31  // e
32  const uint32_t kStr2 = 0x65;
33  const uint32_t kNewline = 0x0a0d;
34  uart_write_imm(kStr1);
35  uart_write_imm(kStr2);
36  uart_write_imm(kNewline);
37 
38  // Wait until the UART is done transmitting.
39  while (!uart_tx_idle()) {
40  }
41 
42  // Set a magic value ("PASS") in retention SRAM.
43  retention_sram_get()->owner.reserved[0] = kSramValuePass;
44 
45  return;
46 }
47 
48 bool test_main(void) {
49  uint32_t immutable_rom_ext_section_enabled =
50  otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_EN_OFFSET);
51  LOG_INFO("Immutable ROM_EXT Enable OTP: 0x%08x",
52  immutable_rom_ext_section_enabled);
53  LOG_INFO(
54  "Immutable ROM_EXT Start Offset OTP: 0x%08x",
55  otp_read32(
56  OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_START_OFFSET_OFFSET));
57  LOG_INFO("Immutable ROM_EXT Length OTP: 0x%08x",
58  otp_read32(
59  OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_LENGTH_OFFSET));
60  uint32_t immutable_rom_ext_hash[kImmutableRomExtSectionHashSizeIn32BitWords];
61  otp_read(OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_SHA256_HASH_OFFSET,
62  immutable_rom_ext_hash, kImmutableRomExtSectionHashSizeIn32BitWords);
63  LOG_INFO(
64  "Immutable ROM_EXT Section Hash OTP: 0x%08x%08x%08x%08x%08x%08x%08x%08x",
65  immutable_rom_ext_hash[7], immutable_rom_ext_hash[6],
66  immutable_rom_ext_hash[5], immutable_rom_ext_hash[4],
67  immutable_rom_ext_hash[3], immutable_rom_ext_hash[2],
68  immutable_rom_ext_hash[1], immutable_rom_ext_hash[0]);
69 
70  // Check the same immutable section offset is used by every test to ensure it
71  // is portable across various ROM_EXT slot configurations.
72  if (immutable_rom_ext_section_enabled == kHardenedBoolTrue) {
73  CHECK(
74  otp_read32(
75  OTP_CTRL_PARAM_CREATOR_SW_CFG_IMMUTABLE_ROM_EXT_START_OFFSET_OFFSET) ==
76  0x400);
77  }
78 
79  // Check the immutabled section executed by reading out the retention SRAM.
80  if (immutable_rom_ext_section_enabled == kHardenedBoolTrue) {
81  CHECK(retention_sram_get()->owner.reserved[0] == kSramValuePass);
82  }
83 
84  return true;
85 }