Safe Data API
CRC-protected (optionally redundant) data containers for Zephyr
v0.0-dev+gbb519af
Loading...
Searching...
No Matches
safe_data.c File Reference
#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/sys/crc.h>
#include <zephyr/logging/log.h>
#include <safe_data/safe_data.h>

Go to the source code of this file.

Macros

#define SAFE_TAG_SEED   0x0u
#define SAFE_DATA_KIND_COUNT   (SAFE_DATA_FAULT_KIND_MUTATOR_CLAMPED + 1)
#define SAFE_DATA_KAT_EXPECTED   0xCBF43926u

Functions

 LOG_MODULE_REGISTER (safe_data, CONFIG_SAFE_DATA_LOG_LEVEL)
static uint32_t safe_tag_update (uint32_t c, const uint8_t *p, size_t len)
static uint32_t safe_tag (const void *p, size_t len)
static uint32_t safe_tag_inverted (const void *shadow, size_t len)
static bool payload_matches_shadow (const void *payload, const void *shadow, size_t len)
void safe_data_fault_handler (const void *payload, size_t len)
 Legacy user-overridable (weak) fault handler.
int safe_data_fault_handler_register (safe_data_fault_cb_t cb)
 Register the fault handler callback.
int safe_data_stats_get (struct safe_data_stats *out)
 Snapshot the global statistics.
void safe_data_stats_reset (void)
 Reset all statistics counters to zero.
static void safe_event (enum safe_data_fault_kind kind, const void *payload, size_t len)
int safe_data_init (struct k_mutex *lock, void *payload, size_t len, uint32_t *crc, void *shadow)
 Initialise the lock and seal the current payload contents.
static int verify_quiet (const void *payload, size_t len, uint32_t crc, const void *shadow)
int safe_data_verify (const void *payload, size_t len, uint32_t crc, const void *shadow)
 Verify payload integrity. Pure: read-only and lock-free; it NEVER modifies the payload, so it is safe to call concurrently with writers (a transient mismatch may then be reported - callers needing atomicity should use the higher-level helpers).
int safe_data_verify_repair (void *payload, size_t len, uint32_t crc, void *shadow)
 Verify payload integrity and repair a recoverable fault in place.
int safe_data_read (struct k_mutex *lock, void *payload, size_t len, uint32_t *crc, void *shadow, void *out)
 Verify under lock and copy the payload out to out.
int safe_data_write (struct k_mutex *lock, void *payload, size_t len, uint32_t *crc, void *shadow, const void *in)
 Copy in into the payload under lock and reseal the integrity tag.
int safe_data_update (struct k_mutex *lock, void *payload, size_t len, uint32_t *crc, void *shadow, safe_data_mutator_t fn, void *user)
 Atomic read-modify-write: lock, verify, run fn, reseal on success.
int safe_data_commit (struct k_mutex *lock, void *payload, size_t len, uint32_t *crc, void *shadow)
 Reseal after an explicit external modification (advanced use). Lock, recompute the integrity tag, unlock.
int safe_data_selftest (void)
 Self-test of the integrity mechanism itself.
int safe_data_selftest_result (void)
 Result of the boot-time self-test (CONFIG_SAFE_DATA_SELFTEST_BOOT).
static int safe_data_boot_selftest (void)
 SYS_INIT (safe_data_boot_selftest, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT)

Variables

static safe_data_fault_cb_t fault_cb
static atomic_t stat_counters [(SAFE_DATA_FAULT_KIND_MUTATOR_CLAMPED+1)]
static volatile bool event_mute
static int boot_selftest_result = -EAGAIN

Macro Definition Documentation

◆ SAFE_DATA_KAT_EXPECTED

#define SAFE_DATA_KAT_EXPECTED   0xCBF43926u

Definition at line 487 of file safe_data.c.

Referenced by safe_data_selftest().

◆ SAFE_DATA_KIND_COUNT

#define SAFE_DATA_KIND_COUNT   (SAFE_DATA_FAULT_KIND_MUTATOR_CLAMPED + 1)

Definition at line 107 of file safe_data.c.

◆ SAFE_TAG_SEED

#define SAFE_TAG_SEED   0x0u

Definition at line 38 of file safe_data.c.

Referenced by safe_tag(), and safe_tag_inverted().

Function Documentation

◆ LOG_MODULE_REGISTER()

LOG_MODULE_REGISTER ( safe_data ,
CONFIG_SAFE_DATA_LOG_LEVEL  )

◆ payload_matches_shadow()

bool payload_matches_shadow ( const void * payload,
const void * shadow,
size_t len )
static

Definition at line 74 of file safe_data.c.

Referenced by safe_data_verify_repair(), and verify_quiet().

◆ safe_data_boot_selftest()

int safe_data_boot_selftest ( void )
static

Definition at line 556 of file safe_data.c.

References boot_selftest_result, and safe_data_selftest().

Referenced by SYS_INIT().

◆ safe_event()

◆ safe_tag()

uint32_t safe_tag ( const void * p,
size_t len )
inlinestatic

Definition at line 45 of file safe_data.c.

References SAFE_TAG_SEED, and safe_tag_update().

Referenced by safe_data_selftest(), and verify_quiet().

◆ safe_tag_inverted()

uint32_t safe_tag_inverted ( const void * shadow,
size_t len )
static

Definition at line 53 of file safe_data.c.

References SAFE_TAG_SEED, and safe_tag_update().

Referenced by verify_quiet().

◆ safe_tag_update()

uint32_t safe_tag_update ( uint32_t c,
const uint8_t * p,
size_t len )
inlinestatic

Definition at line 39 of file safe_data.c.

Referenced by safe_tag(), and safe_tag_inverted().

◆ SYS_INIT()

SYS_INIT ( safe_data_boot_selftest ,
POST_KERNEL ,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT  )

◆ verify_quiet()

int verify_quiet ( const void * payload,
size_t len,
uint32_t crc,
const void * shadow )
static

Variable Documentation

◆ boot_selftest_result

int boot_selftest_result = -EAGAIN
static

Definition at line 549 of file safe_data.c.

Referenced by safe_data_boot_selftest(), and safe_data_selftest_result().

◆ event_mute

volatile bool event_mute
static

Definition at line 159 of file safe_data.c.

Referenced by safe_data_selftest(), and safe_event().

◆ fault_cb

safe_data_fault_cb_t fault_cb
static

Definition at line 95 of file safe_data.c.

Referenced by safe_data_fault_handler_register(), and safe_event().

◆ stat_counters

atomic_t stat_counters[(SAFE_DATA_FAULT_KIND_MUTATOR_CLAMPED+1)]
static

Definition at line 110 of file safe_data.c.

Referenced by safe_data_stats_get(), safe_data_stats_reset(), and safe_event().