1 /*
2  * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __FLASH_OTP_NV_COUNTERS_BACKEND_H__
9 #define __FLASH_OTP_NV_COUNTERS_BACKEND_H__
10 
11 #include "cmsis_compiler.h"
12 
13 #define FLASH_NV_COUNTER_AM 3
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 #define OTP_NV_COUNTERS_INITIALIZED 0xC0DE8112U
19 
20 __PACKED_STRUCT flash_otp_nv_counters_region_t {
21     /* Must be the first item */
22     uint32_t init_value;
23 
24 #ifdef PLATFORM_DEFAULT_OTP
25     __PACKED_STRUCT {
26         uint8_t huk[32];
27         uint8_t iak[32];
28         uint8_t iak_len[4];
29         uint8_t iak_type[4];
30         uint8_t iak_id[32];
31 
32         uint8_t boot_seed[32];
33         uint8_t lcs[4];
34         uint8_t implementation_id[32];
35         uint8_t cert_ref[32];
36         uint8_t verification_service_url[32];
37         uint8_t profile_definition[32];
38 
39 #ifdef BL2
40         uint8_t bl2_rotpk_0[32];
41         uint8_t bl2_rotpk_1[32];
42 
43         uint8_t bl2_nv_counter_0[64];
44         uint8_t bl2_nv_counter_1[64];
45         uint8_t bl2_nv_counter_2[64];
46         uint8_t bl2_nv_counter_3[64];
47 
48         uint8_t bl2_rotpk_2[32];
49         uint8_t bl2_rotpk_3[32];
50 #endif /* BL2 */
51 
52 #ifdef BL1
53         uint8_t bl1_rotpk_0[32];
54         uint8_t bl1_nv_counter_0[16];
55 #endif /* BL1 */
56 
57 #if (PLATFORM_NS_NV_COUNTERS > 0)
58         uint8_t ns_nv_counter_0[64];
59 #endif
60 #if (PLATFORM_NS_NV_COUNTERS > 1)
61         uint8_t ns_nv_counter_1[64];
62 #endif
63 #if (PLATFORM_NS_NV_COUNTERS > 2)
64         uint8_t ns_nv_counter_2[64];
65 #endif
66 
67         uint8_t entropy_seed[64];
68 
69         uint8_t secure_debug_pk[32];
70     };
71 #endif /* PLATFORM_DEFAULT_OTP */
72 
73 #ifdef PLATFORM_DEFAULT_NV_COUNTERS
74     __PACKED_STRUCT {
75         uint32_t flash_nv_counters[FLASH_NV_COUNTER_AM];
76     };
77 #endif /* PLATFORM_DEFAULT_NV_COUNTERS */
78 
79     /* Must be last item, so that it can be written separately after the main
80      * write operation has succeeded
81      */
82     uint32_t swap_count;
83 };
84 
85 /**
86  * \brief                               Initialise the OTP / NV counter flash
87  *                                      area.
88  *
89  * \return                              TFM_PLAT_ERR_SUCCESS if the
90  *                                      initialization succeeds, otherwise
91  *                                      TFM_PLAT_ERR_SYSTEM_ERR
92  */
93 enum tfm_plat_err_t init_otp_nv_counters_flash(void);
94 
95 /**
96  * \brief                               Reads the OTP / NV counter area at the
97  *                                      given offset into the buffer.
98  *
99  * \param[in]  offset                   offset at which to read from.
100  * \param[out] data                     buffer into which to copy the data.
101  * \param[in]  cnt                      number of bytes to read. Must not be
102  *                                      larger than the size of the buffer.
103  *
104  * \retval TFM_PLAT_ERR_SUCCESS         The data is read successfully.
105  * \retval TFM_PLAT_ERR_INVALID_INPUT   An input parameter has an invalid value.
106  * \retval TFM_PLAT_ERR_SYSTEM_ERR      An unspecified error occurred.
107  */
108 enum tfm_plat_err_t read_otp_nv_counters_flash(uint32_t offset, void *data, uint32_t cnt);
109 
110 /**
111  * \brief                               Writes from the given buffer into the
112  *                                      OTP / NV counter area at the given
113  *                                      offset.
114  *
115  * \param[in]  offset                   offset at which to write to.
116  * \param[out] data                     buffer from which to copy the data.
117  * \param[in]  cnt                      number of bytes to write. Should not be
118  *                                      larger than the size of the buffer.
119  *
120  * \retval TFM_PLAT_ERR_SUCCESS         The data is written successfully.
121  * \retval TFM_PLAT_ERR_INVALID_INPUT   An input parameter has an invalid value.
122  * \retval TFM_PLAT_ERR_SYSTEM_ERR      An unspecified error occurred.
123  */
124 enum tfm_plat_err_t write_otp_nv_counters_flash(uint32_t offset, const void *data, uint32_t cnt);
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif /* __FLASH_OTP_NV_COUNTERS_BACKEND_H__ */
131