29static const char*
const s_tag =
"ra8_cache_store";
46typedef enum : uint32_t {
57 if (data ==
nullptr) {
64 for (uint32_t i = 0U; i < len; i++) {
67 bool lsb = (crc & 1U) != 0U;
83 UINT rc = lx_nor_flash_sector_read(store->
flash, (
ULONG)sector, out512);
84 if (rc == (
UINT)LX_SUCCESS) {
87 if (rc == (
UINT)LX_SECTOR_NOT_FOUND) {
100 UINT rc = lx_nor_flash_sector_write(store->
flash, (
ULONG)sector, (VOID*)(uintptr_t)in512);
101 if (rc != (
UINT)LX_SUCCESS) {
111 UINT rc = lx_nor_flash_sector_release(store->
flash, (
ULONG)sector);
112 if (rc != (
UINT)LX_SUCCESS) {
120 if (store ==
nullptr) {
123 if (store->
index ==
nullptr) {
126 for (uint16_t i = 0U; i < store->
index_cap; i++) {
140 uint32_t start_sector,
141 uint16_t sector_count,
145 if (store ==
nullptr) {
148 if (store->
index ==
nullptr) {
151 for (uint16_t i = 0U; i < store->
index_cap; i++) {
161 .start_sector = start_sector,
162 .byte_len = byte_len,
163 .sector_count = sector_count,
190 for (uint16_t i = 0U; i < store->
index_cap; i++) {
224 (void)
memset(out_sb, 0,
sizeof(*out_sb));
256 if (sb->
crc != want) {
289 if (store ==
nullptr) {
292 if (out512 ==
nullptr) {
296 uint32_t packed = 0U;
298 for (; *slot < store->
index_cap; (*slot)++) {
332 *out_entry_count = count;
354 const uint8_t* in512,
356 uint32_t entry_count)
358 if (store ==
nullptr) {
361 if (in512 ==
nullptr) {
365 if (*loaded >= entry_count) {
403 if (entry_count > (uint32_t)store->
index_cap) {
407 uint32_t loaded = 0U;
409 if (loaded >= entry_count) {
501 if (h->
seq > *max_seq) {
527 uint32_t max_seq = 0U;
529 for (uint32_t guard = store->
log_start; guard < store->logical_sectors; guard++) {
647 (void)lx_nor_flash_initialize();
651 CHAR* nm = (cfg->
name !=
nullptr) ? (
CHAR*)(uintptr_t)cfg->
name : (
CHAR*)(uintptr_t)
"ra8_cache";
746 if (store ==
nullptr) {
749 if (cfg ==
nullptr) {
757 for (uint16_t i = 0U; i < store->
index_cap; i++) {
static const char * s_tag
Logging / check tag.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Persistent key(CRC32)->blob cache for compiled .rabook containers,built on the vendored,...
@ k_ra8_cache_store_flag_pinned
Never-evict (open book / metadata).
@ k_ra8_cache_store_flag_in_use
Slot holds a live entry.
@ k_ra8_cache_store_sector_bytes
LevelX logical-sector size (bytes).
@ k_ra8_cache_store_overprov_pct
Default GC headroom margin (%).
@ k_ra8_cache_store_min_sectors
Minimum usable logical-sector span.
@ k_ra8_cache_store_max_overprov
Reject nonsensical margins above.
Internal on-media format + cross-TU helpers for ra8_cache_store (#201).
@ k_ra8_cs_format_version
On-flash layout revision.
@ k_ra8_cs_crc32_poly
Reflected CRC-32/ISO-HDLC polynomial.
@ k_ra8_cs_dir_per_sector
Directory entries per 512-byte sector.
@ k_ra8_cs_crc32_seed
CRC-32 pre/post conditioning.
@ k_ra8_cs_entry_magic
Entry-header tag 'R','C','S','E'.
@ k_ra8_cs_super_magic
Superblock tag 'R','C','S','1'.
@ k_ra8_cs_dir_ent_bytes
Serialized ra8_cs_dir_ent_t size.
@ k_ra8_cs_dirty
Session open or crashed -> mount must replay the log.
@ k_ra8_cs_clean
Clean shutdown -> the checkpoint directory is valid.
static ra8_err_t internal_dir_load(ra8_cache_store_t *store, uint32_t entry_count)
Load entry_count directory entries from the checkpoint into the index.
static bool internal_super_is_clean(const ra8_cs_super_t *sb)
True when sb is a valid, clean-shutdown superblock.
ra8_cs_shift_t
Small fixed quantities for CRC folding and dir packing.
@ k_ra8_cs_crc_bits
Bits folded per input byte.
@ k_ra8_cs_pct_full
Whole-percent denominator for the margin.
static bool s_cs_lx_system_inited
One-shot latch so LevelX's global open-list is initialised once.
uint32_t priv_cache_store_crc32(const uint8_t *data, uint32_t len)
Fold a byte block into a CRC-32/ISO-HDLC value (seeded + finalised).
static ra8_err_t internal_open_levelx(ra8_cache_store_t *store, const ra8_cache_store_cfg_t *cfg)
Bring the injected LevelX NOR partition up (format on request, open).
ra8_err_t ra8_cache_store_init(ra8_cache_store_t *store, const ra8_cache_store_cfg_t *cfg)
Bind a LevelX NOR flash instance and mount (or format) the store.
static ra8_err_t internal_mount(ra8_cache_store_t *store)
Mount an open partition: read sector 0, then recover the index.
static ra8_err_t internal_bringup(ra8_cache_store_t *store, const ra8_cache_store_cfg_t *cfg)
Derive geometry, open the LevelX partition, and mount the index.
static void internal_init_fields(ra8_cache_store_t *store, const ra8_cache_store_cfg_t *cfg)
Bind the caller-owned buffers into the store and reset its counters.
int32_t priv_cache_store_index_add(ra8_cache_store_t *store, uint32_t key, uint32_t start_sector, uint16_t sector_count, uint32_t byte_len, bool pinned)
Claim a free index slot and populate it for key.
static bool internal_hdr_read(ra8_cache_store_t *store, uint32_t s, ra8_cs_entry_hdr_t *out_hdr)
Read + validate the entry header at logical sector s.
static ra8_err_t internal_geometry(ra8_cache_store_t *store, const ra8_cache_store_cfg_t *cfg)
Derive the store geometry (checkpoint span, log start, budget).
ra8_err_t priv_cache_store_dir_save(ra8_cache_store_t *store, uint32_t *out_entry_count)
Serialize the live index into the on-flash checkpoint directory.
static void internal_scan_accept(ra8_cache_store_t *store, const ra8_cs_entry_hdr_t *h, uint32_t *max_seq)
Fold one validated header into the index, tracking the max sequence.
ra8_err_t priv_cache_store_sector_read(const ra8_cache_store_t *store, uint32_t sector, uint8_t *out512)
Read one LevelX logical sector into the store staging buffer region.
int32_t priv_cache_store_index_find(const ra8_cache_store_t *store, uint32_t key)
Find the index slot holding key.
static ra8_err_t internal_super_read(ra8_cache_store_t *store, ra8_cs_super_t *out_sb)
Read + parse the superblock (sector 0) into out_sb.
static ra8_err_t internal_validate_cfg(const ra8_cache_store_cfg_t *cfg)
Validate the init config's required members and sizes.
static ra8_err_t internal_recover(ra8_cache_store_t *store, const ra8_cs_super_t *sb)
Rebuild the index from a parsed superblock: checkpoint load or replay.
ra8_err_t priv_cache_store_super_write(ra8_cache_store_t *store, uint32_t clean)
Write the superblock (sector 0) with the given clean marker.
static ra8_err_t internal_scan_log(ra8_cache_store_t *store)
Replay the append log to rebuild the index after an unclean shutdown.
static void internal_dir_unpack_sector(ra8_cache_store_t *store, const uint8_t *in512, uint32_t *loaded, uint32_t entry_count)
Unpack one directory sector into the index, up to entry_count total.
static uint32_t internal_dir_pack_sector(ra8_cache_store_t *store, uint16_t *slot, uint8_t *out512)
Pack up to one sector of in-use index entries into out512.
ra8_err_t priv_cache_store_sector_write(ra8_cache_store_t *store, uint32_t sector, const uint8_t *in512)
Write one LevelX logical sector from a one-sector source buffer.
ra8_err_t priv_cache_store_sector_release(ra8_cache_store_t *store, uint32_t sector)
Release a LevelX logical sector back to the free pool.
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_RETURN_ON_ERROR(err, tag, message)
Early return on error, propagating the code upward.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_hw_init_failed
Hardware peripheral failed to initialise.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
@ k_ra8_err_not_found
Requested item not found (lookup / search missed).
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
#define ra8_log_error(tag, message)
RA8 log error.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
char CHAR
ThreadX-compatible CHAR (host stub).
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Injected dependencies + geometry for ra8_cache_store_init.
ra8_cache_store_nor_init_fn nor_driver_init
Injected physical-flash bind.
uint8_t * staging
Caller-owned >=1-sector scratch.
bool format
Format (wipe) before open.
uint32_t staging_bytes
Bytes at staging (>= sector).
struct LX_NOR_FLASH_STRUCT * nor_flash
Caller-owned LevelX control block.
const char * name
LevelX partition name (may be NULL).
ra8_cache_store_entry_t * index
Caller-owned index array.
uint16_t index_cap
Index slots (max live entries).
uint8_t overprovision_pct
GC headroom margin (0 => default).
uint32_t logical_sectors
Usable LevelX logical-sector span.
One RAM index slot: key -> {header sector, run length, payload len}.
uint32_t byte_len
Payload length in bytes.
uint32_t start_sector
Logical sector of the entry header.
uint8_t flags
ra8_cache_store_flag_t bit set.
uint16_t sector_count
Run length (header + payload sectors).
uint32_t key
Content key (source CRC-32); valid iff in-use.
Caller-owned store handle.
bool inited
True between init and close.
uint32_t data_capacity
Overprovisioned live-sector budget.
uint32_t log_start
First append-log sector.
uint16_t checkpoint_dirs
Directory sectors for the checkpoint.
uint32_t live_sectors
Sectors currently held by live entries.
uint8_t * staging
Caller-owned >=1-sector scratch.
ra8_cache_store_entry_t * index
Caller-owned index slot array.
uint16_t index_cap
Number of index slots.
uint32_t next_seq
Monotonic append sequence number.
struct LX_NOR_FLASH_STRUCT * flash
LevelX control block (borrowed).
uint8_t flash_state
RAM shadow of sector 0 (dirty/clean).
uint32_t logical_sectors
Usable LevelX logical-sector span.
One serialized directory entry in the checkpoint region (16 bytes).
uint16_t flags
Persisted flags (pinned bit).
uint32_t start_sector
Entry header logical sector.
uint32_t byte_len
Payload length in bytes.
uint16_t sector_count
Run length (header + payload).
On-flash entry header at a run's first logical sector.
uint16_t flags
Persisted flags (pinned bit).
uint32_t magic
k_ra8_cs_entry_magic.
uint32_t start_sector
This header's own logical sector (self-anchor).
uint32_t seq
Append sequence number (monotonic).
uint32_t byte_len
Payload length in bytes.
uint32_t key
Content key (source CRC-32).
uint32_t hdr_crc
CRC-32 over the six fields above.
uint16_t sector_count
Run length (header + payload sectors).
On-flash superblock at logical sector 0 (also the checkpoint commit).
uint32_t entry_count
Directory entries in the checkpoint.
uint32_t crc
CRC-32 over the ten fields above.
uint32_t clean
ra8_cs_clean_t shutdown marker.
uint32_t next_seq
Next append sequence number.
uint32_t magic
k_ra8_cs_super_magic.