Software APIs
flash_info_fields.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 "sw/device/silicon_creator/manuf/lib/flash_info_fields.h"
6 
7 #include <stdint.h>
8 
9 #include "sw/device/lib/base/status.h"
11 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
12 #include "sw/device/silicon_creator/lib/attestation.h"
13 
14 #include "flash_ctrl_regs.h" // Generated.
15 #include "otp_ctrl_regs.h" // Generated.
16 
17 /**
18  * Partition 0 pages and fields.
19  * Refer to sw/device/silicon_creator/lib/drivers/flash_ctrl.h for what
20  * information ROM and ROM_EXT expect to find on various pages.
21  */
22 const flash_info_field_t kFlashInfoFieldDeviceId = {
23  .partition = 0,
24  .bank = 0,
25  .page = 0,
26  .byte_offset = 0,
27 };
28 
29 const flash_info_field_t kFlashInfoFieldManufState = {
30  .partition = 0,
31  .bank = 0,
32  .page = 0,
33  .byte_offset = OTP_CTRL_PARAM_DEVICE_ID_SIZE,
34 };
35 
36 const flash_info_field_t kFlashInfoFieldAstCalibrationData = {
37  .partition = 0,
38  .bank = 0,
39  .page = 0,
40  .byte_offset =
41  OTP_CTRL_PARAM_DEVICE_ID_SIZE + OTP_CTRL_PARAM_MANUF_STATE_SIZE,
42 };
43 
44 const flash_info_field_t kFlashInfoFieldCharacterizationData = {
45  .partition = 0,
46  .bank = 0,
47  .page = 0,
48  .byte_offset = OTP_CTRL_PARAM_DEVICE_ID_SIZE +
49  OTP_CTRL_PARAM_MANUF_STATE_SIZE +
50  kFlashInfoAstCalibrationDataSizeIn32BitWords,
51 };
52 
53 const flash_info_field_t kFlashInfoFieldCreatorSeed = {
54  .partition = 0,
55  .bank = 0,
56  .page = 1,
57  .byte_offset = 0,
58 };
59 
60 const flash_info_field_t kFlashInfoFieldOwnerSeed = {
61  .partition = 0,
62  .bank = 0,
63  .page = 2,
64  .byte_offset = 0,
65 };
66 
67 const flash_info_field_t kFlashInfoFieldWaferAuthSecret = {
68  .partition = 0,
69  .bank = 0,
70  .page = 3,
71  .byte_offset = 0,
72 };
73 
74 const flash_info_field_t kFlashInfoFieldUdsAttestationKeySeed = {
75  .partition = 0,
76  .bank = 0,
77  .page = 4,
78  .byte_offset = kFlashInfoFieldUdsKeySeedIdx * kAttestationSeedBytes,
79 };
80 
81 const flash_info_field_t kFlashInfoFieldCdi0AttestationKeySeed = {
82  .partition = 0,
83  .bank = 0,
84  .page = 4,
85  .byte_offset = kFlashInfoFieldCdi0KeySeedIdx * kAttestationSeedBytes,
86 };
87 
88 const flash_info_field_t kFlashInfoFieldCdi1AttestationKeySeed = {
89  .partition = 0,
90  .bank = 0,
91  .page = 4,
92  .byte_offset = kFlashInfoFieldCdi1KeySeedIdx * kAttestationSeedBytes,
93 };
94 
95 const flash_info_field_t kFlashInfoFieldTpmEkAttestationKeySeed = {
96  .partition = 0,
97  .bank = 0,
98  .page = 4,
99  .byte_offset = kFlashInfoFieldTpmEkKeySeedIdx * kAttestationSeedBytes,
100 };
101 
102 const flash_info_field_t kFlashInfoFieldAttestationKeyGenVersion = {
103  .partition = 0,
104  .bank = 0,
105  .page = 4,
106  .byte_offset = FLASH_CTRL_PARAM_BYTES_PER_PAGE - sizeof(uint32_t),
107 };
108 
109 status_t manuf_flash_info_field_read(dif_flash_ctrl_state_t *flash_state,
110  flash_info_field_t field,
111  uint32_t *data_out, size_t num_words) {
113  uint32_t byte_address =
114  (field.page * device_info.bytes_per_page) + field.byte_offset;
115  TRY(flash_ctrl_testutils_read(flash_state, byte_address, field.partition,
116  data_out, kDifFlashCtrlPartitionTypeInfo,
117  num_words,
118  /*delay=*/0));
119  return OK_STATUS();
120 }
121 
122 status_t manuf_flash_info_field_write(dif_flash_ctrl_state_t *flash_state,
123  flash_info_field_t field,
124  uint32_t *data_in, size_t num_words,
125  bool erase_page_before_write) {
127  uint32_t byte_address =
128  (field.page * device_info.bytes_per_page) + field.byte_offset;
129  if (erase_page_before_write) {
130  TRY(flash_ctrl_testutils_erase_and_write_page(
131  flash_state, byte_address, field.partition, data_in,
132  kDifFlashCtrlPartitionTypeInfo, num_words));
133  } else {
134  TRY(flash_ctrl_testutils_write(flash_state, byte_address, field.partition,
135  data_in, kDifFlashCtrlPartitionTypeInfo,
136  num_words));
137  }
138  return OK_STATUS();
139 }