nRF Connect SDK API 3.3.99
Loading...
Searching...
No Matches
bl_storage.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef BL_STORAGE_H_
8#define BL_STORAGE_H_
9
10#include <string.h>
11#include <zephyr/types.h>
12#include <zephyr/autoconf.h>
13#include <nrfx.h>
14#if defined(CONFIG_NRFX_NVMC)
15#include <nrfx_nvmc.h>
16#elif defined(CONFIG_NRFX_RRAMC)
17#include <nrfx_rramc.h>
18#else
19#error "No NRFX storage technology supported backend selected"
20#endif
21#include <errno.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#if defined(CONFIG_NRFX_NVMC)
28typedef uint16_t counter_t;
29typedef uint16_t lcs_data_t;
30typedef uint16_t lcs_reserved_t;
31#elif defined(CONFIG_NRFX_RRAMC)
32/* nRF54L15 only supports word writes */
33typedef uint32_t counter_t;
34typedef uint32_t lcs_data_t;
35typedef uint32_t lcs_reserved_t;
36#endif
37
38#define EHASHFF 113 /* A hash contains too many 0xFs. */
39#define EREADLCS 114 /* LCS field of OTP is in an invalid state */
40#define EINVALIDLCS 115 /* Invalid LCS*/
41
42/* We truncate the 32 byte sha256 down to 16 bytes before storing it */
43#define SB_PUBLIC_KEY_HASH_LEN 16
44
45/* Supported collection types. */
50
51/* Counter used by NSIB to check the firmware version */
52#define BL_MONOTONIC_COUNTERS_DESC_NSIB 0x1
53
54/* Counter used by MCUBOOT to check the firmware version. Suffixed
55 * with ID0 as we might support checking the version of multiple
56 * images in the future.
57 */
58#define BL_MONOTONIC_COUNTERS_DESC_MCUBOOT_ID0 0x2
59
71 lcs_data_t provisioning;
72 lcs_data_t secure;
73 /* Pad to end the alignment at a 4-byte boundary as some devices
74 * are only supporting 4-byte UICR->OTP reads. We place the reserved
75 * padding in the middle of the struct in case we ever need to support
76 * another state.
77 */
78 lcs_reserved_t reserved_for_padding;
79 lcs_data_t decommissioned;
80};
81
89 /* Counter description. What the counter is used for. See
90 * BL_MONOTONIC_COUNTERS_DESC_x.
91 */
92 uint16_t description;
93 /* Number of entries in 'counter_slots' list. */
95 counter_t counter_slots[];
96};
97
99struct collection {
100 uint16_t type;
101 uint16_t count;
102};
103
110 struct collection collection; /* Type must be BL_COLLECTION_TYPE_MONOTONIC_COUNTERS */
112};
113
114/* Variable data types. */
119 uint8_t type;
120 uint8_t length;
121 uint8_t data[];
122};
123
124/* The third data structure in the provision page. It has unknown length since
125 * 'variable_data' is repeated. The collection starts immediately after the
126 * counter collection. As the counter collection has unknown length, the start
127 * of the variable data collection must be calculated dynamically. Similarly,
128 * the entries in the variable data collection have unknown length, so they
129 * cannot be accessed through array indices.
130 */
132 struct collection collection; /* Type must be BL_COLLECTION_TYPE_VARIABLE_DATA */
134};
135
141 /* NB: When placed in OTP, reads must be 4 bytes and 4 byte aligned */
143 uint8_t implementation_id[32];
144 uint32_t s0_address;
145 uint32_t s1_address;
146 uint32_t num_public_keys; /* Number of entries in 'key_data' list. */
147 struct {
148 uint32_t valid;
151
152 /* Monotonic counter collection:
153 * uint16_t type;
154 * uint16_t count;
155 * struct {
156 * uint16_t description;
157 * uint16_t num_counter_slots;
158 * counter_t counter_slots[];
159 * } counters[];
160 */
161
162 /* Variable data collection:
163 * uint16_t type;
164 * uint16_t count;
165 * struct {
166 * uint8_t type;
167 * uint8_t length;
168 * uint8_t data[];
169 * } variable_data[];
170 * uint8_t padding[]; // Padding to align to 4 bytes
171 */
172};
173
174extern const volatile struct bl_storage_data *const BL_STORAGE;
175
176/* This must be 32 bytes according to the IETF PSA token specification */
177#define BL_STORAGE_IMPLEMENTATION_ID_SIZE 32
178
188uint32_t s0_address_read(void);
189
195uint32_t s1_address_read(void);
196
202uint32_t num_public_keys_read(void);
203
211
222int public_key_data_read(uint32_t key_idx, uint8_t *p_buf);
223
231void invalidate_public_key(uint32_t key_idx);
232
243int num_monotonic_counter_slots(uint16_t counter_desc, uint16_t *counter_slots);
244
255int get_monotonic_counter(uint16_t counter_desc, counter_t *counter_value);
256
274int set_monotonic_counter(uint16_t counter_desc, counter_t new_counter);
275
287int is_monotonic_counter_update_possible(uint16_t counter_desc);
288
301
311
323
330
349int read_variable_data(enum variable_data_type data_type, uint8_t *buf, uint32_t *buf_len);
350
353#ifdef __cplusplus
354}
355#endif
356
357#endif /* BL_STORAGE_H_ */
#define SB_PUBLIC_KEY_HASH_LEN
Definition bl_storage.h:43
collection_type
Definition bl_storage.h:46
@ BL_COLLECTION_TYPE_MONOTONIC_COUNTERS
Definition bl_storage.h:47
@ BL_COLLECTION_TYPE_VARIABLE_DATA
Definition bl_storage.h:48
variable_data_type
Definition bl_storage.h:115
@ BL_VARIABLE_DATA_TYPE_PSA_CERTIFICATION_REFERENCE
Definition bl_storage.h:116
const volatile struct bl_storage_data *const BL_STORAGE
int set_monotonic_counter(uint16_t counter_desc, counter_t new_counter)
Set the current HW monotonic counter.
int update_life_cycle_state(enum bl_storage_lcs next_lcs)
Update the life cycle state in OTP.
int get_monotonic_counter(uint16_t counter_desc, counter_t *counter_value)
Get the current HW monotonic counter.
int verify_public_keys(void)
Function for verifying public keys.
uint32_t s0_address_read(void)
Function for reading address of slot 0.
uint32_t num_public_keys_read(void)
Function for reading number of public key data slots.
bl_storage_lcs
The PSA life cycle states a device can be in.
Definition bl_storage.h:294
@ BL_STORAGE_LCS_DECOMMISSIONED
Definition bl_storage.h:299
@ BL_STORAGE_LCS_UNKNOWN
Definition bl_storage.h:295
@ BL_STORAGE_LCS_ASSEMBLY
Definition bl_storage.h:296
@ BL_STORAGE_LCS_PROVISIONING
Definition bl_storage.h:297
@ BL_STORAGE_LCS_SECURED
Definition bl_storage.h:298
void invalidate_public_key(uint32_t key_idx)
Function for invalidating a public key.
int num_monotonic_counter_slots(uint16_t counter_desc, uint16_t *counter_slots)
Get the number of monotonic counter slots.
int public_key_data_read(uint32_t key_idx, uint8_t *p_buf)
Function for reading public key hashes.
uint32_t s1_address_read(void)
Function for reading address of slot 1.
int is_monotonic_counter_update_possible(uint16_t counter_desc)
Checks whether it is possible to update the monotonic counter to a new value.
void read_implementation_id_from_otp(uint8_t *buf)
int read_variable_data(enum variable_data_type data_type, uint8_t *buf, uint32_t *buf_len)
Read variable data from OTP.
int read_life_cycle_state(enum bl_storage_lcs *lcs)
Read the current life cycle state the device is in from OTP,.
uint32_t num_public_keys
Definition bl_storage.h:146
uint8_t implementation_id[32]
Definition bl_storage.h:143
uint32_t valid
Definition bl_storage.h:148
struct life_cycle_state_data lcs
Definition bl_storage.h:142
struct bl_storage_data::@137 key_data[]
uint32_t s0_address
Definition bl_storage.h:144
uint32_t s1_address
Definition bl_storage.h:145
Definition bl_storage.h:140
uint16_t type
Definition bl_storage.h:100
uint16_t count
Definition bl_storage.h:101
Definition bl_storage.h:99
struct monotonic_counter counters[]
Definition bl_storage.h:111
Definition bl_storage.h:109
lcs_reserved_t reserved_for_padding
Definition bl_storage.h:78
lcs_data_t secure
Definition bl_storage.h:72
lcs_data_t provisioning
Definition bl_storage.h:71
lcs_data_t decommissioned
Definition bl_storage.h:79
Definition bl_storage.h:70
counter_t counter_slots[]
Definition bl_storage.h:95
uint16_t num_counter_slots
Definition bl_storage.h:94
uint16_t description
Definition bl_storage.h:92
Definition bl_storage.h:88
Definition bl_storage.h:131
uint8_t length
Definition bl_storage.h:120
uint8_t type
Definition bl_storage.h:119
uint8_t data[]
Definition bl_storage.h:121
Definition bl_storage.h:118