|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Portable checksummed two-generation persistence for downloader state. More...
#include <inttypes.h>#include <stdint.h>#include <stdio.h>#include <string.h>#include "mdl_state.h"#include "mdl_state_internal.h"#include "ra8_attributes.h"Go to the source code of this file.
Data Structures | |
| struct | mdl_state_envelope_t |
| Decoded canonical journal envelope. More... | |
| struct | mdl_state_slot_t |
| One physical generation discovered under the logical path. More... | |
| struct | mdl_state_writer_t |
| Streaming serialization state for one transaction. More... | |
| struct | mdl_state_validation_t |
| Expected stage identity passed to the independent validator. More... | |
Enumerations | |
| enum | mdl_state_store_limit_t : uint32_t { k_state_magic_bytes = 8U , k_state_header_bytes = 40U , k_state_header_crc_span = 36U , k_state_envelope_v1 = 1U , k_state_alt_suffix = 4U , k_state_io_call_max = 8U * 1024U * 1024U , k_state_crc32_polynomial = 0xEDB88320UL , k_state_be32_high_shift = 24U , k_state_be64_high_shift = 56U , k_state_header_version_offset = 8U , k_state_header_size_offset = 10U , k_state_header_reserved_offset = 12U , k_state_header_sequence_offset = 16U , k_state_header_payload_size_offset = 24U , k_state_header_payload_crc_offset = 32U , k_state_header_crc_offset = 36U } |
| Canonical journal-envelope and bounded-I/O constants. More... | |
Functions | |
| static uint32_t | internal_mdl_state_crc32_update (uint32_t state, const uint8_t *bytes, uint32_t length) |
| Update an unfinalized reflected CRC-32 state. | |
| static void | internal_mdl_state_put_be16 (uint8_t *out, uint16_t value) |
| Encode one uint16 in canonical big-endian order. | |
| static void | internal_mdl_state_put_be32 (uint8_t *out, uint32_t value) |
| Encode one uint32 in canonical big-endian order. | |
| static void | internal_mdl_state_put_be64 (uint8_t *out, uint64_t value) |
| Encode one uint64 in canonical big-endian order. | |
| static uint16_t | internal_mdl_state_get_be16 (const uint8_t *in) |
| Decode one canonical big-endian uint16. | |
| static uint32_t | internal_mdl_state_get_be32 (const uint8_t *in) |
| Decode one canonical big-endian uint32. | |
| static uint64_t | internal_mdl_state_get_be64 (const uint8_t *in) |
| Decode one canonical big-endian uint64. | |
| static void | internal_mdl_state_encode_header (const mdl_state_envelope_t *envelope, uint8_t *out) |
| Encode one self-checking canonical envelope. | |
| static bool | internal_mdl_state_decode_header (const uint8_t *bytes, mdl_state_envelope_t *out) |
| Decode and authenticate one canonical envelope. | |
| static ra8_err_t | internal_mdl_state_read_all (fw_fs_file_t *file, uint8_t *out, uint32_t length, uint32_t *calls) |
| Read an exact byte count while rejecting zero progress. | |
| static ra8_err_t | internal_mdl_state_hash_payload (mdl_storage_t *storage, fw_fs_file_t *file, uint64_t length, uint32_t *out_crc) |
| Hash one exact payload extent and reject early EOF. | |
| static ra8_err_t | internal_mdl_state_validate_open (mdl_storage_t *storage, fw_fs_file_t *file, mdl_state_envelope_t *out) |
| Validate an open journal envelope, extent, and checksum. | |
| static ra8_err_t | internal_mdl_state_paths (const mdl_storage_t *storage, const char *path, char *base, char *alternate) |
| Derive and validate both physical generation paths. | |
| static void | internal_mdl_state_scan_slot (mdl_storage_t *storage, const char *path, bool allow_legacy, mdl_state_slot_t *out) |
| Scan one physical generation without parsing its payload. | |
| static bool | internal_mdl_state_same_generation (const mdl_state_slot_t *a, const mdl_state_slot_t *b) |
| Check whether two slots describe the same generation. | |
| static ra8_err_t | internal_mdl_state_order (const mdl_state_slot_t *base, const mdl_state_slot_t *alternate, const mdl_state_slot_t **newest, const mdl_state_slot_t **older) |
| Order valid generations newest first. | |
| static ra8_err_t | internal_mdl_state_load_slot (mdl_storage_t *storage, const mdl_state_slot_t *slot, mdl_state_t *st) |
| Load and revalidate one previously scanned generation. | |
| static ra8_err_t | internal_mdl_state_write_all (mdl_state_writer_t *writer, const uint8_t *bytes, uint32_t length) |
| Write all bytes to an active transaction with bounded retry. | |
| static ra8_err_t | internal_mdl_state_emit (mdl_state_writer_t *writer, const char *line, int length) |
| Append one payload record and update its identity. | |
| static ra8_err_t | internal_mdl_state_emit_kv (mdl_state_writer_t *writer, char type, const char *value) |
| Serialize one bounded key/value record. | |
| static ra8_err_t | internal_mdl_state_emit_metadata (mdl_state_writer_t *writer, const mdl_state_t *st) |
| Serialize every fixed series identity and metadata record. | |
| static ra8_err_t | internal_mdl_state_emit_chapters (mdl_state_writer_t *writer, const mdl_state_t *st) |
| Serialize every chapter with exact v3 numeric identity. | |
| static ra8_err_t | internal_mdl_state_emit_pages (mdl_state_writer_t *writer, const mdl_state_t *st) |
| Serialize every page identity and cache record. | |
| static ra8_err_t | internal_mdl_state_serialize (mdl_state_writer_t *writer, const mdl_state_t *st) |
| Serialize one complete current-schema payload. | |
| static ra8_err_t | internal_mdl_state_validate_stage (void *ctx, fw_fs_file_t *staged) |
| Independently validate the staged journal before publication. | |
| static ra8_err_t | internal_mdl_state_abort (fw_fs_transaction_t *transaction, ra8_err_t primary) |
| Abort an unpublished transaction with cleanup-error precedence. | |
| static ra8_err_t | internal_mdl_state_build_stage (mdl_storage_t *storage, const char *target, uint64_t sequence, const mdl_state_t *st, fw_fs_transaction_t *transaction, mdl_state_envelope_t *envelope) |
| Build and validate a private staged journal. | |
| static ra8_err_t | internal_mdl_state_save_plan (const mdl_state_slot_t *base, const mdl_state_slot_t *alternate, const mdl_state_slot_t **target, uint64_t *sequence) |
| Select the preserved generation, rewrite target, and next sequence. | |
| ra8_err_t | mdl_state_probe (mdl_storage_t *storage, const char *path, bool *out_exists) |
| Probe the complete two-generation state marker through portable storage. | |
| static ra8_err_t | internal_mdl_state_load_mode (mdl_storage_t *storage, const char *path, mdl_state_t *st, bool allow_legacy) |
| Load the newest usable state with an explicit legacy policy. | |
| ra8_err_t | mdl_state_load (mdl_storage_t *storage, const char *path, mdl_state_t *st) |
| Load a series' newest valid state through injected portable storage. | |
| ra8_err_t | mdl_state_load_authenticated (mdl_storage_t *storage, const char *path, mdl_state_t *st) |
| Load only an authenticated checksummed state generation. | |
| static ra8_err_t | internal_mdl_state_prepare_target (mdl_storage_t *storage, const char *path, char *base, char *alternate, const char **out_target, uint64_t *out_sequence) |
| Decide which generation slot the next save occupies, and clear it. | |
| ra8_err_t | mdl_state_save (mdl_storage_t *storage, const char *path, const mdl_state_t *st, bool *out_published) |
| Publish a checksummed successor without sacrificing the newest state. | |
Variables | |
| static const uint8_t | s_state_magic [k_state_magic_bytes] = {'M', 'D', 'L', 'S', 'T', 'J', 'N', 'L'} |
| Canonical journal magic, encoded byte-for-byte. | |
Portable checksummed two-generation persistence for downloader state.
Encodes, validates, publishes, and recovers bounded state generations through fw_fs.
Definition in file mdl_state_store.c.
| enum mdl_state_store_limit_t : uint32_t |
Canonical journal-envelope and bounded-I/O constants.
Definition at line 18 of file mdl_state_store.c.
|
static |
Abort an unpublished transaction with cleanup-error precedence.
| [in,out] | transaction | Transaction. |
| [in] | primary | Earlier error. |
| primary | Abort succeeded or no transaction was active. |
transaction is non-NULL. Definition at line 736 of file mdl_state_store.c.
References fw_fs_transaction_t::active, fw_fs_transaction_abort(), k_ra8_ok, and RA8_INTERNAL.
Referenced by mdl_state_save().
|
static |
Build and validate a private staged journal.
| [in,out] | storage | Scratch binding. |
| [in] | target | Absent target path. |
| [in] | sequence | New sequence. |
| [in] | st | Valid state. |
| [out] | transaction | Active stage. |
| [out] | envelope | Identity. |
| k_ra8_ok | Stage is independently validated. |
Definition at line 753 of file mdl_state_store.c.
References mdl_storage_t::fs, fw_fs_transaction_begin(), fw_fs_transaction_seek(), fw_fs_transaction_validate(), internal_mdl_state_encode_header(), internal_mdl_state_serialize(), internal_mdl_state_validate_stage(), internal_mdl_state_write_all(), k_fw_fs_txn_create_new, k_ra8_ok, k_state_header_bytes, mdl_state_writer_t::payload_bytes, RA8_INTERNAL, mdl_storage_t::transaction_workspace, mdl_storage_t::transaction_workspace_bytes, and fw_fs_t::transactions.
Referenced by mdl_state_save().
|
static |
Update an unfinalized reflected CRC-32 state.
| [in] | state | Prior CRC state. |
| [in] | bytes | Input bytes. |
| [in] | length | Byte count. |
| UINT32_MAX | Empty input from the initial state. |
bytes is non-NULL when length is nonzero. length bytes contribute. Definition at line 80 of file mdl_state_store.c.
References k_state_crc32_polynomial.
Referenced by internal_mdl_state_decode_header(), internal_mdl_state_emit(), internal_mdl_state_encode_header(), and internal_mdl_state_hash_payload().
|
static |
Decode and authenticate one canonical envelope.
| [in] | bytes | Header bytes. |
| [out] | out | Decoded identity. |
| false | Magic, schema, reserved field, CRC, or sequence is invalid. |
Definition at line 204 of file mdl_state_store.c.
References internal_mdl_state_crc32_update(), internal_mdl_state_get_be16(), internal_mdl_state_get_be32(), internal_mdl_state_get_be64(), k_state_envelope_v1, k_state_header_bytes, k_state_header_crc_offset, k_state_header_crc_span, k_state_header_payload_crc_offset, k_state_header_payload_size_offset, k_state_header_reserved_offset, k_state_header_sequence_offset, k_state_header_size_offset, k_state_header_version_offset, memcmp(), mdl_state_envelope_t::payload_bytes, mdl_state_envelope_t::payload_crc32, RA8_INTERNAL, s_state_magic, and mdl_state_envelope_t::sequence.
Referenced by internal_mdl_state_validate_open().
|
static |
Append one payload record and update its identity.
| [in,out] | writer | Active writer. |
| [in] | line | Serialized record. |
| [in] | length | Byte count. |
| k_ra8_err_invalid_size | Record or aggregate extent overflow. |
length describes accessible bytes. Definition at line 549 of file mdl_state_store.c.
References mdl_state_writer_t::crc_state, internal_mdl_state_crc32_update(), internal_mdl_state_write_all(), k_mdl_state_line_max, k_ra8_err_invalid_size, k_ra8_ok, and mdl_state_writer_t::payload_bytes.
Referenced by internal_mdl_state_emit_chapters(), internal_mdl_state_emit_kv(), internal_mdl_state_emit_metadata(), internal_mdl_state_emit_pages(), and internal_mdl_state_serialize().
|
static |
Serialize every chapter with exact v3 numeric identity.
| [in,out] | writer | Active writer. |
| [in] | st | Valid state. |
| k_ra8_ok | Every chapter was emitted. |
Definition at line 622 of file mdl_state_store.c.
References mdl_state_t::chapter_count, mdl_chapter_rec_t::chapter_id, mdl_state_t::chapters, mdl_chapter_rec_t::complete, mdl_chapter_rec_t::fetched_at, internal_mdl_state_emit(), k_mdl_state_line_max, k_ra8_ok, memcpy(), mdl_chapter_rec_t::number, mdl_chapter_rec_t::number_known, mdl_chapter_rec_t::page_count, mdl_chapter_rec_t::pages_done, RA8_INTERNAL, mdl_chapter_rec_t::source_url, and mdl_chapter_rec_t::title.
Referenced by internal_mdl_state_serialize().
|
static |
Serialize one bounded key/value record.
| [in,out] | writer | Active writer. |
| [in] | type | Record type. |
| [in] | value | Validated value. |
| k_ra8_err_invalid_size | Formatted record exceeds its bound. |
Definition at line 574 of file mdl_state_store.c.
References internal_mdl_state_emit(), and k_mdl_state_line_max.
Referenced by internal_mdl_state_emit_metadata().
|
static |
Serialize every fixed series identity and metadata record.
| [in,out] | writer | Active writer. |
| [in] | st | Valid state. |
| k_ra8_ok | Every metadata record was emitted. |
Definition at line 588 of file mdl_state_store.c.
References mdl_state_t::artist, mdl_state_t::config_path, mdl_state_t::cover_path, mdl_state_t::cover_url, internal_mdl_state_emit(), internal_mdl_state_emit_kv(), k_mdl_state_line_max, k_ra8_ok, mdl_state_t::language, RA8_INTERNAL, mdl_state_t::reading_direction, mdl_state_t::series_title, mdl_state_t::series_url, mdl_state_t::site_host, mdl_state_t::site_name, mdl_state_t::summary, and mdl_state_t::writer.
Referenced by internal_mdl_state_serialize().
|
static |
Serialize every page identity and cache record.
| [in,out] | writer | Active writer. |
| [in] | st | Valid state. |
| k_ra8_ok | Every page was emitted. |
Definition at line 655 of file mdl_state_store.c.
References mdl_page_rec_t::content_hash, mdl_page_rec_t::etag, mdl_page_rec_t::fetched_at, internal_mdl_state_emit(), k_mdl_state_line_max, k_ra8_ok, mdl_page_rec_t::last_modified, mdl_state_t::page_rec_count, mdl_state_t::pages, RA8_INTERNAL, mdl_page_rec_t::rel_path, mdl_page_rec_t::response_status, and mdl_page_rec_t::url_hash.
Referenced by internal_mdl_state_serialize().
|
static |
Encode one self-checking canonical envelope.
| [in] | envelope | Decoded identity. |
| [out] | out | Header bytes. |
Definition at line 182 of file mdl_state_store.c.
References internal_mdl_state_crc32_update(), internal_mdl_state_put_be16(), internal_mdl_state_put_be32(), internal_mdl_state_put_be64(), k_state_envelope_v1, k_state_header_bytes, k_state_header_crc_offset, k_state_header_crc_span, k_state_header_payload_crc_offset, k_state_header_payload_size_offset, k_state_header_sequence_offset, k_state_header_size_offset, k_state_header_version_offset, memcpy(), memset(), mdl_state_envelope_t::payload_bytes, mdl_state_envelope_t::payload_crc32, RA8_INTERNAL, s_state_magic, and mdl_state_envelope_t::sequence.
Referenced by internal_mdl_state_build_stage().
|
static |
Decode one canonical big-endian uint16.
| [in] | in | Two-byte source. |
| 0 | Both bytes encode zero. |
in is non-NULL. Definition at line 141 of file mdl_state_store.c.
References RA8_INTERNAL.
Referenced by internal_mdl_state_decode_header().
|
static |
Decode one canonical big-endian uint32.
| [in] | in | Four-byte source. |
| 0 | All bytes encode zero. |
in is non-NULL. Definition at line 153 of file mdl_state_store.c.
References k_state_be32_high_shift, and RA8_INTERNAL.
Referenced by internal_mdl_state_decode_header().
|
static |
Decode one canonical big-endian uint64.
| [in] | in | Eight-byte source. |
| 0 | All bytes encode zero. |
in is non-NULL. Definition at line 166 of file mdl_state_store.c.
References RA8_INTERNAL.
Referenced by internal_mdl_state_decode_header().
|
static |
Hash one exact payload extent and reject early EOF.
| [in,out] | storage | I/O scratch binding. |
| [in,out] | file | Open stream. |
| [in] | length | Exact extent. |
| [out] | out_crc | Final CRC. |
| k_ra8_err_invalid_state | Early zero progress. |
length bytes. Definition at line 261 of file mdl_state_store.c.
References fw_fs_read(), internal_mdl_state_crc32_update(), mdl_storage_t::io_buffer, mdl_storage_t::io_buffer_bytes, k_ra8_err_invalid_size, k_ra8_err_invalid_state, k_ra8_ok, k_state_io_call_max, and RA8_INTERNAL.
Referenced by internal_mdl_state_validate_open().
|
static |
Load the newest usable state with an explicit legacy policy.
Authenticates journal generations, optionally admits legacy base text, and retries the older valid generation after a decode error.
| [in,out] | storage | Initialized exclusive storage binding. |
| [in] | path | Canonical logical state path. |
| [out] | st | State initialized on every return. |
| [in] | allow_legacy | Whether unenveloped base text may be selected. |
st is valid on every return and empty when loading fails. | k_ra8_ok | The operation completed successfully. |
| other | The originating validation, storage, stream, or network error. |
Definition at line 881 of file mdl_state_store.c.
References mdl_state_slot_t::error, mdl_state_slot_t::exists, internal_mdl_state_load_slot(), internal_mdl_state_order(), internal_mdl_state_paths(), internal_mdl_state_scan_slot(), k_fw_fs_path_cap, k_ra8_err_invalid_arg, k_ra8_ok, mdl_state_init(), and RA8_INTERNAL.
Referenced by mdl_state_load(), and mdl_state_load_authenticated().
|
static |
Load and revalidate one previously scanned generation.
| [in,out] | storage | Scratch binding. |
| [in] | slot | Scanned slot. |
| [out] | st | Destination state. |
| k_ra8_err_invalid_state | Identity changed or payload is corrupt. |
slot was valid during scanning. st. st empty. Definition at line 476 of file mdl_state_store.c.
References mdl_state_slot_t::envelope, mdl_state_slot_t::file_bytes, mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, fw_fs_close(), fw_fs_open(), internal_mdl_state_validate_open(), k_fw_fs_open_read, k_mdl_state_version, k_mdl_state_version_v2, k_ra8_err_invalid_state, k_ra8_ok, k_state_header_bytes, mdl_state_slot_t::legacy, mdl_state_init(), mdl_state_slot_t::path, mdl_state_envelope_t::payload_bytes, mdl_state_envelope_t::payload_crc32, priv_mdl_state_parse_file(), mdl_state_envelope_t::sequence, and fw_fs_t::streams.
Referenced by internal_mdl_state_load_mode().
|
static |
Order valid generations newest first.
| [in] | base | Base slot. |
| [in] | alternate | Alternate slot. |
| [out] | newest | Newest valid slot. |
| [out] | older | Other valid slot. |
| k_ra8_err_invalid_state | Divergent equal sequences. |
Definition at line 440 of file mdl_state_store.c.
References mdl_state_slot_t::envelope, internal_mdl_state_same_generation(), k_ra8_err_invalid_state, k_ra8_ok, RA8_INTERNAL, mdl_state_envelope_t::sequence, and mdl_state_slot_t::valid.
Referenced by internal_mdl_state_load_mode(), and internal_mdl_state_save_plan().
|
static |
Derive and validate both physical generation paths.
| [in] | storage | Filesystem binding. |
| [in] | path | Logical base. |
| [out] | base | Base path. |
| [out] | alternate | .alt path. |
| k_ra8_err_invalid_size | Suffix would exceed capacity. |
Definition at line 336 of file mdl_state_store.c.
References fw_fs_t::caps, mdl_storage_t::file_workspace, mdl_storage_t::fs, fw_fs_path_validate(), mdl_storage_t::io_buffer, mdl_storage_t::io_buffer_bytes, k_fw_fs_path_cap, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_ok, k_state_alt_suffix, memcpy(), RA8_INTERNAL, strlen(), and mdl_storage_t::transaction_workspace.
Referenced by internal_mdl_state_load_mode(), internal_mdl_state_prepare_target(), and mdl_state_probe().
|
static |
Decide which generation slot the next save occupies, and clear it.
Derives both physical paths, scans both slots, asks the save plan which is next, and unlinks it when a previous generation is still there.
| [in,out] | storage | Filesystem binding. |
| [in] | path | Logical base. |
| [out] | base | Base path buffer. |
| [out] | alternate | .alt path buffer. |
| [out] | out_target | Chosen path, aliasing one of those buffers. |
| [out] | out_sequence | Sequence the save must carry. |
| k_ra8_ok | The slot is chosen and empty. |
| k_ra8_err_invalid_size | Suffix would exceed capacity. |
Definition at line 935 of file mdl_state_store.c.
References mdl_state_slot_t::exists, mdl_storage_t::fs, fw_fs_unlink(), internal_mdl_state_paths(), internal_mdl_state_save_plan(), internal_mdl_state_scan_slot(), k_ra8_ok, fw_fs_t::names, mdl_state_slot_t::path, and RA8_INTERNAL.
Referenced by mdl_state_save().
|
static |
Encode one uint16 in canonical big-endian order.
| [out] | out | Two-byte destination. |
| [in] | value | Value to encode. |
out is non-NULL. value. Definition at line 99 of file mdl_state_store.c.
References RA8_INTERNAL.
Referenced by internal_mdl_state_encode_header().
|
static |
Encode one uint32 in canonical big-endian order.
| [out] | out | Four-byte destination. |
| [in] | value | Value to encode. |
out is non-NULL. value. Definition at line 112 of file mdl_state_store.c.
References k_state_be32_high_shift, and RA8_INTERNAL.
Referenced by internal_mdl_state_encode_header().
|
static |
Encode one uint64 in canonical big-endian order.
| [out] | out | Eight-byte destination. |
| [in] | value | Value to encode. |
out is non-NULL. value. Definition at line 127 of file mdl_state_store.c.
References k_state_be64_high_shift, and RA8_INTERNAL.
Referenced by internal_mdl_state_encode_header().
|
static |
Read an exact byte count while rejecting zero progress.
| [in,out] | file | Open stream. |
| [out] | out | Destination. |
| [in] | length | Byte count. |
| [in,out] | calls | Attempt counter. |
| k_ra8_err_invalid_state | Premature zero progress. |
length. length bytes. Definition at line 233 of file mdl_state_store.c.
References fw_fs_read(), k_ra8_err_invalid_size, k_ra8_err_invalid_state, k_ra8_ok, and k_state_io_call_max.
Referenced by internal_mdl_state_scan_slot(), and internal_mdl_state_validate_open().
|
static |
Check whether two slots describe the same generation.
| [in] | a | First slot. |
| [in] | b | Second slot. |
| false | Sequence, extent, or checksum differs. |
Definition at line 425 of file mdl_state_store.c.
References mdl_state_slot_t::envelope, mdl_state_envelope_t::payload_bytes, mdl_state_envelope_t::payload_crc32, RA8_INTERNAL, and mdl_state_envelope_t::sequence.
Referenced by internal_mdl_state_order().
|
static |
Select the preserved generation, rewrite target, and next sequence.
| [in] | base | Base slot. |
| [in] | alternate | Alternate slot. |
| [out] | target | Slot to replace. |
| [out] | sequence | Next sequence. |
| k_ra8_err_invalid_size | Sequence is exhausted. |
Definition at line 802 of file mdl_state_store.c.
References mdl_state_slot_t::envelope, mdl_state_slot_t::error, internal_mdl_state_order(), k_ra8_err_invalid_size, k_ra8_err_invalid_state, k_ra8_ok, RA8_INTERNAL, and mdl_state_envelope_t::sequence.
Referenced by internal_mdl_state_prepare_target().
|
static |
Scan one physical generation without parsing its payload.
| [in,out] | storage | Scratch binding. |
| [in] | path | Physical path. |
| [in] | allow_legacy | Whether text is accepted. |
| [out] | out | Slot result. |
path passed portable validation. Definition at line 369 of file mdl_state_store.c.
References mdl_state_slot_t::envelope, mdl_state_slot_t::error, fw_fs_stat_t::exists, mdl_state_slot_t::exists, mdl_state_slot_t::file_bytes, mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, fw_fs_close(), fw_fs_open(), fw_fs_stat(), internal_mdl_state_read_all(), internal_mdl_state_validate_open(), k_fw_fs_node_file, k_fw_fs_open_read, k_ra8_err_invalid_state, k_ra8_ok, k_state_magic_bytes, mdl_state_slot_t::legacy, memcmp(), fw_fs_t::names, RA8_INTERNAL, s_state_magic, fw_fs_stat_t::size_bytes, fw_fs_t::streams, fw_fs_stat_t::type, and mdl_state_slot_t::valid.
Referenced by internal_mdl_state_load_mode(), and internal_mdl_state_prepare_target().
|
static |
Serialize one complete current-schema payload.
| [in,out] | writer | Active writer. |
| [in] | st | Valid state. |
| k_ra8_ok | Version, metadata, chapters, and pages were emitted. |
Definition at line 684 of file mdl_state_store.c.
References internal_mdl_state_emit(), internal_mdl_state_emit_chapters(), internal_mdl_state_emit_metadata(), internal_mdl_state_emit_pages(), k_mdl_state_line_max, k_mdl_state_version, k_ra8_ok, and RA8_INTERNAL.
Referenced by internal_mdl_state_build_stage().
|
static |
Validate an open journal envelope, extent, and checksum.
| [in,out] | storage | Scratch binding. |
| [in,out] | file | Open stream. |
| [out] | out | Envelope. |
| k_ra8_err_invalid_state | Any canonical identity mismatch. |
Definition at line 299 of file mdl_state_store.c.
References fw_fs_file_size(), fw_fs_seek(), internal_mdl_state_decode_header(), internal_mdl_state_hash_payload(), internal_mdl_state_read_all(), k_ra8_err_invalid_state, k_ra8_ok, k_state_header_bytes, mdl_state_envelope_t::payload_bytes, mdl_state_envelope_t::payload_crc32, and RA8_INTERNAL.
Referenced by internal_mdl_state_load_slot(), internal_mdl_state_scan_slot(), and internal_mdl_state_validate_stage().
|
static |
Independently validate the staged journal before publication.
| [in] | ctx | Expected identity context. |
| [in,out] | staged | Open staged stream. |
| k_ra8_err_protocol_error | Stage differs from expected identity. |
Definition at line 710 of file mdl_state_store.c.
References mdl_state_validation_t::envelope, internal_mdl_state_validate_open(), k_ra8_err_invalid_arg, k_ra8_err_protocol_error, k_ra8_ok, mdl_state_envelope_t::payload_bytes, mdl_state_envelope_t::payload_crc32, RA8_INTERNAL, mdl_state_envelope_t::sequence, and mdl_state_validation_t::storage.
Referenced by internal_mdl_state_build_stage().
|
static |
Write all bytes to an active transaction with bounded retry.
| [in,out] | writer | Active writer. |
| [in] | bytes | Source. |
| [in] | length | Byte count. |
| k_ra8_err_invalid_state | Successful zero progress. |
length bytes. Definition at line 519 of file mdl_state_store.c.
References mdl_state_writer_t::calls, fw_fs_transaction_write(), k_ra8_err_invalid_size, k_ra8_err_invalid_state, k_ra8_ok, k_state_io_call_max, and mdl_state_writer_t::transaction.
Referenced by internal_mdl_state_build_stage(), and internal_mdl_state_emit().
| ra8_err_t mdl_state_load | ( | mdl_storage_t * | storage, |
| const char * | path, | ||
| mdl_state_t * | st ) |
Load a series' newest valid state through injected portable storage.
Authenticates both journal generations, chooses the highest valid sequence, and falls back to the older valid generation if semantic parsing fails. A legacy unenveloped v1/v2 base file remains readable as sequence zero. When neither generation exists, st is initialized empty and success is returned. Any final failure also leaves st initialized empty.
| [in,out] | storage | Initialized, exclusively-owned storage binding. |
| [in] | path | Canonical logical state path (never NULL). |
| [out] | st | State to fill; always left in a valid (possibly empty) form. |
| k_ra8_ok | Loaded, or the file was absent (empty state). |
| k_ra8_err_invalid_arg | path or st was NULL. |
| k_ra8_err_invalid_state | The file exists but is corrupt/unsupported. |
storage was initialized successfully. storage. st is always a valid state object on return (empty on any error). storage. Definition at line 920 of file mdl_state_store.c.
References internal_mdl_state_load_mode().
Referenced by internal_run_series_paths(), and internal_verify_series_dir().
|
nodiscard |
Load only an authenticated checksummed state generation.
Scans the logical base path and its .alt peer, rejects legacy unenveloped text, selects the highest valid authenticated sequence, and falls back to the older authenticated generation when semantic decoding fails. An absent marker initializes st empty and succeeds; every failure also leaves st initialized empty.
| [in,out] | storage | Initialized, exclusively-owned storage binding. |
| [in] | path | Canonical logical state path. |
| [out] | st | State filled from one authenticated generation. |
| k_ra8_ok | An authenticated generation loaded or both peers were absent. |
| k_ra8_err_invalid_state | Existing generations failed authentication. |
| k_ra8_err_invalid_arg | A pointer, path, or binding is invalid. |
storage is initialized. storage. st is a valid state object on every return, empty on failure. Definition at line 925 of file mdl_state_store.c.
References internal_mdl_state_load_mode().
Referenced by internal_library_visit(), and internal_resolve_removal_target().
|
nodiscard |
Probe the complete two-generation state marker through portable storage.
Reports a tracked marker when either the logical base path or its bounded alternate generation exists as a regular file. Integrity and schema validation remain the responsibility of mdl_state_load.
| [in,out] | storage | Initialized, exclusively-owned storage binding. |
| [in] | path | Canonical logical state path. |
| [out] | out_exists | Whether at least one regular state generation exists. |
| k_ra8_ok | The probe completed and initialized out_exists. |
| k_ra8_err_invalid_arg | A pointer/path is invalid or a marker is not a file. |
storage was initialized successfully. storage. out_exists is initialized false unless a marker was found. storage is a non-reentrant dependency bundle. Definition at line 835 of file mdl_state_store.c.
References fw_fs_stat_t::exists, mdl_storage_t::fs, fw_fs_stat(), internal_mdl_state_paths(), k_fw_fs_node_file, k_fw_fs_path_cap, k_ra8_err_invalid_arg, k_ra8_err_invalid_state, k_ra8_ok, fw_fs_t::names, and fw_fs_stat_t::type.
Referenced by internal_library_visit(), internal_resolve_removal_target(), internal_verify_library_root(), and mdl_app_run_verify().
| ra8_err_t mdl_state_save | ( | mdl_storage_t * | storage, |
| const char * | path, | ||
| const mdl_state_t * | st, | ||
| bool * | out_published ) |
Publish a checksummed successor without sacrificing the newest state.
Selects the absent, invalid, or older physical generation; removes only that target; writes a create-new private transaction; independently validates its canonical envelope, exact length, and payload CRC; then commits it. The newest accepted generation remains intact until commit. Sequence exhaustion is reported rather than wrapped. Cleanup failure takes precedence while the transaction is unpublished. A commit-time durability error can accompany a true out_published and must not be retried as though nothing changed.
| [in,out] | storage | Initialized, exclusively-owned storage binding. |
| [in] | path | Canonical logical state path (never NULL). |
| [in] | st | State to write (never NULL). |
| [out] | out_published | False initially; true exactly when the successor became visible, including after durability failure. |
| k_ra8_ok | The validated successor was published. |
| k_ra8_err_invalid_arg | A pointer, path, or binding is invalid. |
| k_ra8_err_invalid_state | Existing generations are ambiguous/corrupt. |
| k_ra8_err_invalid_size | The monotonic sequence or I/O bound is exhausted. |
| other | A namespace, transaction, validation, durability, or cleanup error. |
storage was initialized successfully. storage. st. out_published is false, the previously newest valid generation survives. out_published is true, the successor is visible regardless of return status.storage. Definition at line 966 of file mdl_state_store.c.
References fw_fs_transaction_commit(), internal_mdl_state_abort(), internal_mdl_state_build_stage(), internal_mdl_state_prepare_target(), k_fw_fs_path_cap, k_ra8_err_invalid_arg, k_ra8_err_invalid_state, k_ra8_ok, and priv_mdl_state_valid().
Referenced by internal_save_series_state(), and priv_mdl_fetch_checkpoint().
|
static |
Canonical journal magic, encoded byte-for-byte.
Definition at line 38 of file mdl_state_store.c.
Referenced by internal_mdl_state_decode_header(), internal_mdl_state_encode_header(), and internal_mdl_state_scan_slot().