34static const char*
const s_tag =
"ra8_ftl";
45typedef enum : uint32_t {
251 const uint16_t phys = ftl->
map[lbn];
302 const uint16_t old = ftl->
map[lbn];
307 ftl->
map[lbn] = (uint16_t)phys;
389 for (uint32_t i = 0; i < count; ++i) {
437 for (uint32_t i = 0; i < count; ++i) {
483 for (uint32_t i = 0; i < count; ++i) {
484 const uint16_t old = ftl->
map[lba + i];
610 uint32_t logical_blocks,
611 uint32_t physical_blocks,
706 const uint8_t* scratch,
707 uint32_t logical_blocks,
708 uint32_t physical_blocks)
715 if (logical_blocks == 0U) {
727 uint32_t logical_blocks,
729 uint32_t physical_blocks,
742 uint8_t erase_value = 0;
761 if (ftl->
raw ==
nullptr) {
797 if (ftl->
map ==
nullptr) {
803 *phys_out = ftl->
map[lbn];
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_no_mem
Static buffer exhausted (no dynamic memory on this project).
@ k_ra8_err_no_data
No application data available (e.g.
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ 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.
static ra8_err_t internal_validate_init_args(const ra8_ftl_t *bd, const ra8_io_blockdev_t *raw, const uint16_t *map, const ra8_ftl_pblock_t *pblocks, const uint8_t *scratch, uint32_t logical_blocks, uint32_t physical_blocks)
Validate the pointer + sizing arguments to ra8_ftl_init.
static ra8_err_t internal_dev_write(void *ctx, uint32_t lba, uint32_t count, const uint8_t *buf)
Presented vtable: write count logical blocks at lba.
static const ra8_io_blockdev_iface_t s_ftl_iface
Presented FTL vtable – a clean free-overwrite block device.
static ra8_err_t internal_check_caps(const ra8_io_blockdev_caps_t *caps, uint32_t logical_blocks, uint32_t physical_blocks, uint8_t *erase_out)
Validate the underlying device's capabilities for FTL use.
static ra8_err_t internal_read_one(const ra8_ftl_t *ftl, uint32_t lbn, uint8_t *dst)
Read one logical block, resolving the map (or erase value if unmapped).
static ra8_err_t internal_write_one(ra8_ftl_t *ftl, uint32_t lbn, const uint8_t *src)
Write one logical block via copy-on-write relocation.
ra8_err_t ra8_ftl_wear_stats(const ra8_ftl_t *ftl, uint32_t *max_out, uint32_t *min_out)
Report the highest per-physical-block erase count seen so far.
static ra8_err_t internal_dev_read(void *ctx, uint32_t lba, uint32_t count, uint8_t *buf)
Presented vtable: read count logical blocks at lba.
static ra8_err_t internal_pick_free(const ra8_ftl_t *ftl, uint32_t *out)
Select the least-erased FREE physical block (wear-levelling).
ra8_err_t ra8_ftl_init(ra8_ftl_t *bd, const ra8_io_blockdev_t *raw, uint16_t *map, uint32_t logical_blocks, ra8_ftl_pblock_t *pblocks, uint32_t physical_blocks, uint8_t *scratch)
Initialise an FTL over an erase-before-write block device.
static ra8_err_t internal_dev_sync(void *ctx)
Presented vtable: flush the underlying device.
static ra8_err_t internal_dev_get_caps(const void *ctx, ra8_io_blockdev_caps_t *out)
Presented vtable: report the FTL's free-overwrite capabilities.
static ra8_err_t internal_dev_erase(void *ctx, uint32_t lba, uint32_t count)
Presented vtable: erase count logical blocks at lba.
ra8_err_t ra8_ftl_as_blockdev(ra8_ftl_t *ftl, ra8_io_blockdev_t *out)
Expose an initialised FTL as a free-overwrite ra8_io_blockdev_t.
static ra8_err_t internal_alloc_blank(ra8_ftl_t *ftl, uint32_t *out)
Allocate a blank physical block, reclaiming stale blocks if needed.
static ra8_err_t internal_reset_tables(ra8_ftl_t *ftl)
Cold-start the FTL bookkeeping tables.
static ra8_err_t internal_bounds(const ra8_ftl_t *ftl, uint32_t lba, uint32_t count)
Reject an out-of-range presented [lba, lba+count) range.
static ra8_err_t internal_reclaim_stale(ra8_ftl_t *ftl)
Reclaim every STALE physical block by erasing it back to FREE.
ra8_err_t ra8_ftl_phys_of(const ra8_ftl_t *ftl, uint32_t lbn, uint16_t *phys_out)
Report the physical block currently backing a logical block.
ra8_ftl_impl_const_t
Implementation-private layout constants.
@ k_ra8_ftl_req_erase_unit
Required raw erase_unit_blocks value.
@ k_ra8_ftl_one_block
Count for a single-block raw transfer.
@ k_ra8_ftl_count_max
Sentinel: highest possible erase count.
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_min_spare
Minimum spare blocks (P - L >= 1).
@ k_ra8_ftl_pstate_free
Blank (reads as erase value); allocatable.
@ k_ra8_ftl_pstate_live
Holds current data for one logical block.
@ k_ra8_ftl_pstate_stale
Superseded; reclaimable by erase to FREE.
ra8_io block-device fabric – one LBA vtable across every storage medium.
ra8_err_t ra8_io_blockdev_sync(const ra8_io_blockdev_t *bd)
Flush any backend write buffering to the medium.
ra8_err_t ra8_io_blockdev_read(const ra8_io_blockdev_t *bd, uint32_t lba, uint32_t count, uint8_t *buf)
Read count logical blocks starting at lba into buf.
struct ra8_io_blockdev_iface ra8_io_blockdev_iface_t
@ k_ra8_io_block_size_bytes
Bytes per logical block (the only size).
ra8_err_t ra8_io_blockdev_write(const ra8_io_blockdev_t *bd, uint32_t lba, uint32_t count, const uint8_t *buf)
Write count logical blocks from buf starting at lba.
ra8_err_t ra8_io_blockdev_get_caps(const ra8_io_blockdev_t *bd, ra8_io_blockdev_caps_t *out)
Report the bound backend's medium capabilities.
ra8_err_t ra8_io_blockdev_erase(const ra8_io_blockdev_t *bd, uint32_t lba, uint32_t count)
Erase count blocks starting at lba to the medium's erase value.
Backend implementation contract for the ra8_io block-device fabric.
Caller-owned metadata for one physical erase block.
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).
uint8_t erase_value
Raw medium erase byte (private).
const ra8_io_blockdev_t * raw
Underlying erase-before-write dev.
uint32_t logical_blocks
Blocks presented to FAT (private).
Static properties a backend reports about its medium.
uint32_t erase_unit_blocks
Erase granularity, in logical blocks.
uint32_t block_count
Total addressable logical blocks.
bool must_erase_before_write
true => program needs a prior erase.
bool read_only
true => writes/erases are rejected.
uint8_t erase_value
Post-erase byte (ra8_io_erase_value_t).
uint16_t logical_block_bytes
Logical block size (always 512).
uint32_t program_size_bytes
Minimum program granularity in bytes.
Caller-allocated block-device handle binding a backend to its context.
const ra8_io_blockdev_iface_t * iface
Bound backend vtable (private).
void * ctx
Backend-private context (private).