Software APIs
rom_e2e_self_hash_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 
5 #include <stdbool.h>
6 
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/chip_info.h"
13 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
14 
17 
18 OTTF_DEFINE_TEST_CONFIG(.silence_console_prints = true);
19 
20 enum {
21  // Hash size.
22  kSha256HashSizeInBits = 256,
23  kSha256HashSizeInBytes = kSha256HashSizeInBits / 8,
24  kSha256HashSizeIn32BitWords = kSha256HashSizeInBytes / 4,
25 };
26 
27 /**
28  * The golden ROM size and hashes expected below are generated using the
29  * following instructions. If the ROM is updated, these values must also be
30  * updated to prevent CI failures.
31  *
32  * 1. Build the ROM and query the ROM hashes:
33  * bazel build //sw/device/silicon_creator/rom:rom_hashes
34  * cat bazel-bin/sw/device/silicon_creator/rom/rom_hashes.txt
35  *
36  * 2. Update the size and golden ROM hashes below (`k*GoldenRomHash`) by
37  * copying the little-endian-32 value arrays from the `rom_hashes.txt`
38  * report.
39  */
40 
41 const size_t kGoldenRomSizeBytes = 32652 - sizeof(chip_info_t);
42 const uint32_t kSimDvGoldenRomHash[kSha256HashSizeIn32BitWords] = {
43  0xc16e04d6, 0x2e94b881, 0x0759b405, 0xd0a28cde,
44  0xa8c900f3, 0x57b8c7f6, 0xacc910b0, 0x43000c0a,
45 };
46 const uint32_t kFpgaCw310GoldenRomHash[kSha256HashSizeIn32BitWords] = {
47  0xf3508c51, 0xef65a542, 0xc20e55d9, 0xada4c934,
48  0x8015bbca, 0xa863db5a, 0xd1ead827, 0x968d94cb,
49 };
50 const uint32_t kSiliconGoldenRomHash[kSha256HashSizeIn32BitWords] = {
51  0x43b60e89, 0xbfa80347, 0xeeceb60a, 0x356bc7f1,
52  0xbd023b8a, 0xe5a4ddfc, 0xf66b45b5, 0x5b2ba0ba,
53 };
54 
55 extern const char _rom_chip_info_start[];
56 
57 // We hash the ROM using the SHA256 algorithm and print the hash to the console.
58 status_t hash_rom(void) {
59  hmac_digest_t rom_hash;
60  hmac_sha256((void *)TOP_EARLGREY_ROM_BASE_ADDR, kGoldenRomSizeBytes,
61  &rom_hash);
62  // Use printf directly here instead of the `LOG()` macros which print extra
63  // filenames and line numbers which bloat DV and GLS runtimes.
64  // DO NOT MODIFY the printf immediately below without modifying the check in
65  // `hw/top_earlgrey/dv/env/seq_lib/chip_sw_rom_e2e_self_hash_gls_vseq.sv`
66  base_printf("ROM Hash: 0x%08x%08x%08x%08x%08x%08x%08x%08x\r\n",
67  rom_hash.digest[7], rom_hash.digest[6], rom_hash.digest[5],
68  rom_hash.digest[4], rom_hash.digest[3], rom_hash.digest[2],
69  rom_hash.digest[1], rom_hash.digest[0]);
70  chip_info_t *rom_chip_info = (chip_info_t *)_rom_chip_info_start;
71  LOG_INFO("rom_chip_info @ %p:", rom_chip_info);
72  LOG_INFO("scm_revision = %08x%08x",
73  rom_chip_info->scm_revision.scm_revision_high,
74  rom_chip_info->scm_revision.scm_revision_low);
75  LOG_INFO("version = %08x", rom_chip_info->version);
76 
77  // TODO(#18868) Add checks for the chip_info values we expect to see in the
78  // released ROM binary.
79 
80  if (kDeviceType == kDeviceSimDV) {
81  TRY_CHECK_ARRAYS_EQ(rom_hash.digest, kSimDvGoldenRomHash,
82  ARRAYSIZE(kSimDvGoldenRomHash));
83  } else if (kDeviceType == kDeviceFpgaCw310) {
84  TRY_CHECK_ARRAYS_EQ(rom_hash.digest, kFpgaCw310GoldenRomHash,
85  ARRAYSIZE(kFpgaCw310GoldenRomHash));
86  } else if (kDeviceType == kDeviceSilicon) {
87  TRY_CHECK_ARRAYS_EQ(rom_hash.digest, kSiliconGoldenRomHash,
88  ARRAYSIZE(kSiliconGoldenRomHash));
89  } else {
90  LOG_ERROR("ROM hash not self-checked for this device type: 0x%x",
91  kDeviceType);
92  return INTERNAL();
93  }
94 
95  return OK_STATUS();
96 };
97 
98 bool test_main(void) { return status_ok(hash_rom()); }