30static const char*
const s_tag =
"ra8_ftl_checkpoint";
39typedef enum : uint32_t {
79 return (uint16_t)((uint16_t)in[0] | ((uint16_t)in[1] << 8U));
98 return (uint32_t)in[0] | ((uint32_t)in[1] << 8U) | ((uint32_t)in[2] << 16U) |
116 out[0] = (uint8_t)value;
117 out[1] = (uint8_t)(value >> 8U);
134 out[0] = (uint8_t)value;
135 out[1] = (uint8_t)(value >> 8U);
136 out[2] = (uint8_t)(value >> 16U);
159 for (uint32_t i = 0U; i < length; ++i) {
162 const uint32_t mask = (uint32_t)(0U - (crc & 1U));
188 uint32_t first_bytes,
190 uint32_t second_bytes)
192 const uintptr_t first_start = (uintptr_t)first;
193 const uintptr_t second_start = (uintptr_t)second;
194 if (first_bytes == 0U) {
197 if (second_bytes == 0U) {
200 if (first_start > UINTPTR_MAX - ((uintptr_t)first_bytes - 1U)) {
203 if (second_start > UINTPTR_MAX - ((uintptr_t)second_bytes - 1U)) {
206 const uintptr_t first_end = first_start + (uintptr_t)first_bytes - 1U;
207 const uintptr_t second_end = second_start + (uintptr_t)second_bytes - 1U;
208 if (first_start > second_end) {
211 return second_start <= first_end;
231 if (ftl->
raw ==
nullptr) {
234 if (ftl->
map ==
nullptr) {
263 if (logical_blocks == 0U) {
269 if (physical_blocks <= logical_blocks) {
344 const bool set = (bitmap[byte] & mask) != 0U;
345 bitmap[byte] |= mask;
368 return (bitmap[
byte] & mask) != 0U;
397 if ((uint32_t)phys < base) {
400 if ((uint32_t)phys - base >= count) {
431 const uint16_t phys = ftl->
map[lbn];
440 for (uint32_t rel = 0U; rel < count; ++rel) {
471 const uint32_t count =
520 for (uint32_t rel = 0U; rel < count; ++rel) {
555 const uint32_t count =
615 if (buf_len != need) {
718 if (buf_len < need) {
static const char * s_tag
Logging / check tag.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#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_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
@ k_ra8_err_crc_mismatch
CRC mismatch detected on received data.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ k_ra8_err_not_initialized
Module not initialized – _init() not yet called successfully.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ 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.
Flash Translation Layer – free overwrite over erase-before-write media.
@ k_ra8_ftl_unmapped
map[] sentinel: logical block unwritten.
@ k_ra8_ftl_max_pblocks
Max physical blocks an FTL may wrap.
@ k_ra8_ftl_pstate_live
Holds current data for one logical block.
@ k_ra8_ftl_pstate_stale
Superseded; reclaimable by erase to FREE.
ra8_err_t ra8_ftl_checkpoint_load(ra8_ftl_t *ftl, const uint8_t *buf, uint32_t buf_len)
Restore FTL mapping state from a checkpoint produced by save.
static ra8_err_t internal_ready(const ra8_ftl_t *ftl)
Validate that a checkpoint-capable FTL handle is fully bound.
ra8_err_t ra8_ftl_checkpoint_save(const ra8_ftl_t *ftl, uint8_t *buf, uint32_t buf_len)
Serialise the FTL's mapping state into a caller buffer.
static uint32_t internal_get_le32(const uint8_t *in)
Decode one canonical little-endian 32-bit field.
static void internal_put_le16(uint8_t *out, uint16_t value)
Encode one canonical little-endian 16-bit field.
static ra8_err_t internal_wire_window(const ra8_ftl_t *ftl, const uint8_t *buf, uint32_t map_offset, uint32_t pb_offset, uint32_t base, uint32_t count)
Validate one canonical wire-map window into caller scratch.
static void internal_decode_commit(ra8_ftl_t *ftl, const uint8_t *buf)
Commit an already validated canonical payload to both live tables.
static ra8_err_t internal_validate_wire(const ra8_ftl_t *ftl, const uint8_t *buf)
Validate every canonical payload invariant without live mutation.
static ra8_err_t internal_native_window(const ra8_ftl_t *ftl, uint32_t base, uint32_t count)
Validate one window of the live native mapping into scratch.
static bool internal_ranges_overlap(const void *first, uint32_t first_bytes, const void *second, uint32_t second_bytes)
Fail closed when two byte ranges overlap or their endpoints wrap.
static ra8_err_t internal_size_values(uint32_t logical_blocks, uint32_t physical_blocks, uint32_t *out)
Calculate the exact wire length with checked 32-bit arithmetic.
static ra8_err_t internal_validate_header(const ra8_ftl_t *ftl, const uint8_t *buf, uint32_t buf_len, uint32_t need)
Validate the canonical header, exact length, geometry, and CRC.
ra8_err_t ra8_ftl_checkpoint_size(const ra8_ftl_t *ftl, uint32_t *size_out)
Report the buffer size a checkpoint of this FTL requires, in bytes.
static void internal_put_le32(uint8_t *out, uint32_t value)
Encode one canonical little-endian 32-bit field.
static uint32_t internal_crc32(const uint8_t *data, uint32_t length)
Compute CRC-32/ISO-HDLC over a bounded byte span.
static uint16_t internal_get_le16(const uint8_t *in)
Decode one canonical little-endian 16-bit field.
static bool internal_bit_is_set(const uint8_t *bitmap, uint32_t bit)
Read one bit from the bounded scratch bitmap.
static void internal_encode(const ra8_ftl_t *ftl, uint8_t *buf, uint32_t need)
Encode a validated live state into the canonical byte layout.
static ra8_err_t internal_window_mark(const ra8_ftl_t *ftl, uint32_t base, uint32_t count, uint16_t phys)
Mark one mapped physical index against a bounded scratch window.
ra8_ftl_checkpoint_const_t
Canonical wire-layout and validation constants.
@ k_ck_byte_3_shift
Shift of byte three in a LE32.
@ k_ck_off_total_bytes
LE32 exact blob length.
@ k_ck_scratch_bytes
Caller workspace guaranteed by FTL.
@ k_ck_map_entry_bytes
One LE16 physical-map entry.
@ k_ck_pblock_state_offset
State byte within one pblock record.
@ k_ck_pblock_entry_bytes
LE32 erase count plus state byte.
@ k_ck_crc_poly
Reflected ISO-HDLC polynomial.
@ k_ck_legacy_magic_swapped
Legacy native BE FTL1 bytes.
@ k_ck_crc_seed
CRC initial/final XOR.
@ k_ck_bitmap_blocks
Physical indices per scratch window.
@ k_ck_legacy_magic_le
Legacy native LE FTL1 value.
@ k_ck_header_bytes
Fixed version-1 header bytes.
@ k_ck_off_physical_blocks
LE32 physical geometry.
@ k_ck_magic
LE bytes RFTL.
@ k_ck_off_logical_blocks
LE32 logical geometry.
@ k_ck_version
Canonical format version.
@ k_ck_bits_per_byte
Bitmap packing factor.
@ k_ck_crc_bytes
CRC-32 trailer bytes.
@ k_ck_off_version
LE16 format version.
@ k_ck_off_header_bytes
LE16 fixed-header size.
@ k_ck_fixed_bytes
Header plus trailer.
static ra8_err_t internal_validate_native(const ra8_ftl_t *ftl)
Validate every live map/state invariant using bounded scratch windows.
static ra8_err_t internal_disjoint(const ra8_ftl_t *ftl, const void *buffer, uint32_t buffer_bytes)
Validate workspace and checkpoint-buffer non-aliasing.
static bool internal_bit_was_set(uint8_t *bitmap, uint32_t bit)
Set one scratch-bitmap bit and report whether it was already set.
uint32_t erase_count
Cumulative erases of this block (wear metric).
uint8_t state
ra8_ftl_pstate_t lifecycle state (private).
Caller-allocated FTL handle binding the wrapper to the raw device.
ra8_ftl_pblock_t * pblocks
Per-physical metadata (private).
uint32_t physical_blocks
Blocks in the raw dev (private).
uint8_t * scratch
512-byte copy scratch (private).
uint16_t * map
logical->physical map (private).
const ra8_io_blockdev_t * raw
Underlying erase-before-write dev.
uint32_t logical_blocks
Blocks presented to FAT (private).