11#include <zephyr/types.h>
12#include <zephyr/autoconf.h>
13#include <drivers/nrfx_common.h>
14#if defined(CONFIG_NRFX_NVMC)
16#elif defined(CONFIG_NRFX_RRAMC)
17#include <nrfx_rramc.h>
19#error "No NRFX storage technology supported backend selected"
29#if defined(CONFIG_NRFX_NVMC)
30typedef uint16_t counter_t;
31typedef uint16_t lcs_data_t;
32typedef uint16_t lcs_reserved_t;
33#elif defined(CONFIG_NRFX_RRAMC)
35typedef uint32_t counter_t;
36typedef uint32_t lcs_data_t;
37typedef uint32_t lcs_reserved_t;
42#define EINVALIDLCS 115
45#define SB_PUBLIC_KEY_HASH_LEN 16
48#define BL_MONOTONIC_COUNTERS_DESC_NSIB 0x1
54#define BL_MONOTONIC_COUNTERS_DESC_MCUBOOT_ID0 0x2
111int get_counter(uint16_t counter_desc, counter_t *counter_value,
const counter_t **free_slot);
130#define BL_STORAGE ((const volatile struct bl_storage_data *)(PM_PROVISION_ADDRESS))
133#define BL_STORAGE_IMPLEMENTATION_ID_SIZE 32
253NRFX_STATIC_INLINE
void otp_copy32(uint8_t *restrict dst, uint32_t
volatile * restrict src,
256 for (
int i = 0; i < size / 4; i++) {
260 for (
int j = 0; j < 4; j++) {
261 dst[i * 4 + j] = (val >> 8 * j) & 0xFF;
285#define STATE_ENTERED 0x0000
286#define STATE_NOT_ENTERED 0xFFFF
294#if defined(CONFIG_NRFX_RRAMC)
295NRFX_STATIC_INLINE uint32_t index_from_address(uint32_t address)
297 return ((address - (uint32_t)
BL_STORAGE)/
sizeof(uint32_t));
303#if defined(CONFIG_NRFX_NVMC)
304 return ~nrfx_nvmc_otp_halfword_read(address);
305#elif defined(CONFIG_NRFX_RRAMC)
306 return ~nrfx_rramc_otp_word_read(index_from_address(address));
312#if defined(CONFIG_NRFX_NVMC)
313 nrfx_nvmc_halfword_write((uint32_t)address, ~value);
314#elif defined(CONFIG_NRFX_RRAMC)
315 nrfx_rramc_otp_word_write(index_from_address((uint32_t)address), ~value);
321#if defined(CONFIG_NRFX_NVMC)
322 return nrfx_nvmc_uicr_word_read((uint32_t *)address);
323#elif defined(CONFIG_NRFX_RRAMC)
324 return nrfx_rramc_word_read(address);
330#if defined(CONFIG_NRFX_NVMC)
331 nrfx_nvmc_word_write(address, value);
333#elif defined(CONFIG_NRFX_RRAMC)
334 nrfx_rramc_word_write(address, value);
342#if defined(CONFIG_NRFX_NVMC)
343 halfword = nrfx_nvmc_otp_halfword_read(address);
344#elif defined(CONFIG_NRFX_RRAMC)
345 uint32_t word = nrfx_rramc_otp_word_read(index_from_address(address));
347 if (!(address & 0x3)) {
348 halfword = (uint16_t)(word & 0x0000FFFF);
350 halfword = (uint16_t)(word >> 16);
358#if defined(CONFIG_NRFX_NVMC)
359 return nrfx_nvmc_otp_halfword_read(address);
360#elif defined(CONFIG_NRFX_RRAMC)
361 return nrfx_rramc_otp_word_read(index_from_address(address));
367#if defined(CONFIG_NRFX_NVMC)
368 nrfx_nvmc_halfword_write(address,
state);
369#elif defined(CONFIG_NRFX_RRAMC)
432 enum lcs current_lcs = 0;
443 if (next_lcs < current_lcs) {
448 if (next_lcs == current_lcs) {
#define SB_PUBLIC_KEY_HASH_LEN
Definition bl_storage.h:45
#define EREADLCS
Definition bl_storage.h:41
#define BL_STORAGE
Definition bl_storage.h:130
#define EINVALIDLCS
Definition bl_storage.h:42
int get_counter(uint16_t counter_desc, counter_t *counter_value, const counter_t **free_slot)
const struct monotonic_counter * get_counter_struct(uint16_t description)
#define BL_STORAGE_IMPLEMENTATION_ID_SIZE
Definition bl_storage.h:133
void bl_storage_counter_set(uint32_t address, counter_t value)
Definition bl_storage.h:310
int set_monotonic_counter(uint16_t counter_desc, counter_t new_counter)
Set the current HW monotonic counter.
lcs_data_t bl_storage_lcs_get(uint32_t address)
Definition bl_storage.h:356
uint32_t bl_storage_word_read(uint32_t address)
Definition bl_storage.h:319
#define STATE_ENTERED
Definition bl_storage.h:285
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 reading number of public key data slots.
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.
#define STATE_NOT_ENTERED
Definition bl_storage.h:286
void otp_copy32(uint8_t *restrict dst, uint32_t volatile *restrict src, size_t size)
Definition bl_storage.h:253
int read_life_cycle_state(enum lcs *lcs)
Read the current life cycle state the device is in from OTP,.
Definition bl_storage.h:383
void invalidate_public_key(uint32_t key_idx)
Function for invalidating a public key.
uint32_t bl_storage_word_write(uint32_t address, uint32_t value)
Definition bl_storage.h:328
int num_monotonic_counter_slots(uint16_t counter_desc, uint16_t *counter_slots)
Get the number of monotonic counter slots.
int update_life_cycle_state(enum lcs next_lcs)
Update the life cycle state in OTP.
Definition bl_storage.h:429
int public_key_data_read(uint32_t key_idx, uint8_t *p_buf)
Function for reading public key hashes.
int bl_storage_lcs_set(uint32_t address, lcs_data_t state)
Definition bl_storage.h:365
uint32_t s1_address_read(void)
Function for reading address of slot 1.
uint16_t bl_storage_otp_halfword_read(uint32_t address)
Definition bl_storage.h:339
void read_implementation_id_from_otp(uint8_t *buf)
Definition bl_storage.h:270
lcs
The PSA life cycle states a device can be in.
Definition bl_storage.h:237
@ BL_STORAGE_LCS_DECOMMISSIONED
Definition bl_storage.h:242
@ BL_STORAGE_LCS_UNKNOWN
Definition bl_storage.h:238
@ BL_STORAGE_LCS_ASSEMBLY
Definition bl_storage.h:239
@ BL_STORAGE_LCS_PROVISIONING
Definition bl_storage.h:240
@ BL_STORAGE_LCS_SECURED
Definition bl_storage.h:241
counter_t bl_storage_counter_get(uint32_t address)
Definition bl_storage.h:301
uint32_t num_public_keys
Definition bl_storage.h:123
struct bl_storage_data::@151 key_data[1]
uint8_t implementation_id[32]
Definition bl_storage.h:120
uint32_t valid
Definition bl_storage.h:125
uint32_t s0_address
Definition bl_storage.h:121
uint32_t s1_address
Definition bl_storage.h:122
Definition bl_storage.h:117
uint16_t num_counters
Definition bl_storage.h:101
uint16_t type
Definition bl_storage.h:100
struct monotonic_counter counters[1]
Definition bl_storage.h:102
Definition bl_storage.h:99
lcs_reserved_t reserved_for_padding
Definition bl_storage.h:74
lcs_data_t secure
Definition bl_storage.h:68
lcs_data_t provisioning
Definition bl_storage.h:67
lcs_data_t decommissioned
Definition bl_storage.h:75
Definition bl_storage.h:66
counter_t counter_slots[1]
Definition bl_storage.h:91
uint16_t num_counter_slots
Definition bl_storage.h:90
uint16_t description
Definition bl_storage.h:88
Definition bl_storage.h:84