ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_rabook_vfs.c
Go to the documentation of this file.
1
14
15#include "mdl_rabook_vfs.h"
16
17#include <stdint.h>
18#include <string.h>
19
20#include "book_chunked.h"
21#include "ra8_attributes.h"
22#include "ra8_io_vfs.h"
23
44static ra8_err_t
45internal_rabook_read_exact(void* opaque, uint64_t offset, uint8_t* dst, uint32_t len)
46{
47 mdl_rabook_vfs_t* const ctx = opaque;
48 if (ctx == nullptr) {
50 }
51 if (ctx->file == nullptr) {
53 }
54 ra8_err_t err = ra8_io_vfs_file_seek(ctx->file, offset);
55 uint32_t done = 0U;
56 RA8_LOOP_BOUND(UINT32_MAX);
57 while (done < len) {
58 if (err != k_ra8_ok) {
59 break;
60 }
61 uint32_t got = 0U;
62 err = ra8_io_vfs_file_read(ctx->file, &dst[done], len - done, &got);
63 if (err == k_ra8_ok) {
64 if (got == 0U) {
66 continue;
67 }
68 }
69 if (got > (len - done)) {
71 } else {
72 done += got;
73 }
74 }
75 return err;
76}
77
92{
93 ctx->reader = (book_chunked_t){};
94 ctx->header = (book_header_t){};
95 ctx->file = nullptr;
96 ctx->file_size = 0U;
97 ctx->open = false;
98}
99
116{
117 ra8_err_t err = k_ra8_ok;
118 if (ctx->file != nullptr) {
119 err = ra8_io_vfs_file_close(ctx->file);
120 }
122 return err;
123}
124
146 const char* path,
147 uint64_t expected_size,
148 bool enforce_size)
149{
151 if (err != k_ra8_ok) {
152 return err;
153 }
154 err = ra8_io_vfs_file_size(ctx->file, &ctx->file_size);
155 if (err == k_ra8_ok) {
156 if (enforce_size) {
157 if (ctx->file_size != expected_size) {
159 }
160 }
161 }
162 if (err == k_ra8_ok) {
163 err = book_chunked_open(&ctx->reader,
165 ctx,
166 ctx->file_size,
167 ctx->inflate_cb,
168 ctx->table,
169 ctx->table_cap,
170 ctx->compressed,
171 ctx->compressed_cap);
172 }
173 if (err != k_ra8_ok) {
174 const ra8_err_t close_err = internal_rabook_close(ctx);
175 (void)close_err;
176 }
177 return err;
178}
179
198{
200 ctx->chunk,
201 ctx->chunk_cap,
202 ctx->scratch,
203 ctx->scratch_cap,
204 out_header);
205}
206
208{
209 if (ctx == nullptr) {
210 return k_ra8_err_null_ptr;
211 }
212 if (config == nullptr) {
213 return k_ra8_err_null_ptr;
214 }
215 if (config->inflate_cb == nullptr) {
216 return k_ra8_err_null_ptr;
217 }
218 if (config->table == nullptr) {
219 return k_ra8_err_null_ptr;
220 }
221 if (config->compressed == nullptr) {
222 return k_ra8_err_null_ptr;
223 }
224 if (config->chunk == nullptr) {
225 return k_ra8_err_null_ptr;
226 }
227 if (config->scratch == nullptr) {
228 return k_ra8_err_null_ptr;
229 }
230 if (config->table_cap < 2U) {
232 }
233 if (config->compressed_cap == 0U) {
235 }
236 if (config->chunk_cap == 0U) {
238 }
239 if (config->scratch_cap == 0U) {
241 }
242 const mdl_rabook_vfs_t initialized = {
243 .inflate_cb = config->inflate_cb,
244 .table = config->table,
245 .compressed = config->compressed,
246 .chunk = config->chunk,
247 .scratch = config->scratch,
248 .table_cap = config->table_cap,
249 .compressed_cap = config->compressed_cap,
250 .chunk_cap = config->chunk_cap,
251 .scratch_cap = config->scratch_cap,
252 };
253 *ctx = initialized;
254 return k_ra8_ok;
255}
256
258 const char* staging_path,
259 uint64_t total_bytes,
260 const uint8_t sha256[k_ra8_mdl_sha256_bytes])
261{
262 if (opaque == nullptr) {
263 return k_ra8_err_null_ptr;
264 }
265 if (staging_path == nullptr) {
266 return k_ra8_err_null_ptr;
267 }
268 if (sha256 == nullptr) {
269 return k_ra8_err_null_ptr;
270 }
271 mdl_rabook_vfs_t* const ctx = opaque;
272 if (ctx->open) {
274 }
275 if (ctx->file != nullptr) {
277 }
278 ctx->transfer_validated = false;
279 (void)memset(ctx->digest, 0, sizeof(ctx->digest));
280 ra8_err_t err = internal_rabook_open_reader(ctx, staging_path, total_bytes, true);
281 book_header_t validated = {};
282 if (err == k_ra8_ok) {
283 err = internal_rabook_validate_open(ctx, &validated);
284 }
285 const ra8_err_t close_err = internal_rabook_close(ctx);
286 if (err == k_ra8_ok) {
287 err = close_err;
288 }
289 if (err == k_ra8_ok) {
290 ctx->header = validated;
291 ctx->file_size = total_bytes;
292 ctx->transfer_validated = true;
293 (void)memcpy(ctx->digest, sha256, sizeof(ctx->digest));
294 }
295 return err;
296}
297
299{
300 if (ctx == nullptr) {
301 return k_ra8_err_null_ptr;
302 }
303 if (path == nullptr) {
304 return k_ra8_err_null_ptr;
305 }
306 if (ctx->open) {
308 }
309 if (ctx->file != nullptr) {
311 }
312 ra8_err_t err = internal_rabook_open_reader(ctx, path, 0U, false);
313 book_header_t validated = {};
314 if (err == k_ra8_ok) {
315 err = internal_rabook_validate_open(ctx, &validated);
316 }
317 if (err == k_ra8_ok) {
318 ctx->header = validated;
319 ctx->open = true;
320 } else {
321 const ra8_err_t close_err = internal_rabook_close(ctx);
322 (void)close_err;
323 }
324 return err;
325}
326
328mdl_rabook_vfs_read_chunk(mdl_rabook_vfs_t* ctx, uint64_t offset, uint8_t* dst, uint32_t len)
329{
330 if (ctx == nullptr) {
331 return k_ra8_err_null_ptr;
332 }
333 if (dst == nullptr) {
334 return k_ra8_err_null_ptr;
335 }
336 if (!ctx->open) {
338 }
339 if (ctx->file == nullptr) {
341 }
342 return book_chunked_read(&ctx->reader, offset, dst, len);
343}
344
346mdl_rabook_vfs_info(const mdl_rabook_vfs_t* ctx, book_header_t* out_header, uint64_t* out_flat_size)
347{
348 if (ctx == nullptr) {
349 return k_ra8_err_null_ptr;
350 }
351 if (out_header == nullptr) {
352 return k_ra8_err_null_ptr;
353 }
354 if (out_flat_size == nullptr) {
355 return k_ra8_err_null_ptr;
356 }
357 if (!ctx->open) {
359 }
360 if (ctx->file == nullptr) {
362 }
363 *out_header = ctx->header;
364 *out_flat_size = ctx->reader.inflated_total;
365 return k_ra8_ok;
366}
367
369{
370 if (ctx == nullptr) {
371 return k_ra8_err_null_ptr;
372 }
373 return internal_rabook_close(ctx);
374}
Demand-paged chunk reader for the "RBKC" .rabook container.
ra8_err_t book_chunked_validate_strict(book_chunked_t *rd, uint8_t *chunk, uint32_t chunk_cap, uint8_t *scratch, uint32_t scratch_cap, book_header_t *out_header)
Strictly validate the complete flat blob behind an open RBKC reader.
ra8_err_t book_chunked_read(void *ctx, uint64_t offset, uint8_t *buf, uint32_t len)
Serve one chunk-aligned read of the inflated flat blob.
ra8_err_t book_chunked_open(book_chunked_t *rd, ra8_vsource_read_fn file_read, void *file_ctx, uint64_t file_len, book_inflate_fn inflate, uint64_t *table_buf, uint32_t table_cap_entries, uint8_t *staging, uint32_t staging_cap)
Open a chunked .rabook container for demand-paged chunk reads.
ra8_err_t mdl_rabook_vfs_read_chunk(mdl_rabook_vfs_t *ctx, uint64_t offset, uint8_t *dst, uint32_t len)
Read one exact chunk-aligned flat-blob span from an open book.
ra8_err_t mdl_rabook_vfs_close(mdl_rabook_vfs_t *ctx)
Close and release an open VFS reader facade.
static ra8_err_t internal_rabook_close(mdl_rabook_vfs_t *ctx)
Close a possibly live facade and clear open-derived state.
ra8_err_t mdl_rabook_vfs_validate(void *opaque, const char *staging_path, uint64_t total_bytes, const uint8_t sha256[k_ra8_mdl_sha256_bytes])
Strictly validate one closed private RBKC staging object.
static ra8_err_t internal_rabook_read_exact(void *opaque, uint64_t offset, uint8_t *dst, uint32_t len)
Read one exact random range through an already-open VFS stream.
ra8_err_t mdl_rabook_vfs_info(const mdl_rabook_vfs_t *ctx, book_header_t *out_header, uint64_t *out_flat_size)
Copy metadata from an open strictly validated book.
static void internal_rabook_clear_open(mdl_rabook_vfs_t *ctx)
Clear all state derived from an opened artifact.
ra8_err_t mdl_rabook_vfs_open(mdl_rabook_vfs_t *ctx, const char *path)
Open and strictly revalidate a published RBKC object for consumption.
ra8_err_t mdl_rabook_vfs_init(mdl_rabook_vfs_t *ctx, const mdl_rabook_vfs_config_t *config)
Initialize one strict RBKC VFS workspace binding.
static ra8_err_t internal_rabook_validate_open(mdl_rabook_vfs_t *ctx, book_header_t *out_header)
Strictly validate the complete inner flat blob of an open reader.
static ra8_err_t internal_rabook_open_reader(mdl_rabook_vfs_t *ctx, const char *path, uint64_t expected_size, bool enforce_size)
Open and parse one exact-size RBKC container without strict payload validation.
Strict no-heap RBKC validator and reader over named RA8 VFS mounts.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_LOOP_BOUND(ceiling)
NASA Power-of-10 Rule 2: bind ONE loop to a compile-time ceiling.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
@ k_ra8_err_protocol_error
Protocol-level error (e.g.
Definition ra8_err.h:429
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
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.
@ k_ra8_fs_mode_read
Read-only, must exist.
ra8_io virtual filesystem – mount many volumes, address them by name.
ra8_err_t ra8_io_vfs_file_close(ra8_io_vfs_file_t *file)
Close and always release one format-neutral stream facade.
Definition ra8_io_vfs.c:625
ra8_err_t ra8_io_vfs_file_size(const ra8_io_vfs_file_t *file, uint64_t *out_bytes)
Report a format-neutral stream's size.
Definition ra8_io_vfs.c:686
ra8_err_t ra8_io_vfs_file_open(const char *path, ra8_fs_mode_t mode, ra8_io_vfs_file_t **out_file)
Open a format-neutral stream through a mounted format's ops.
Definition ra8_io_vfs.c:598
ra8_err_t ra8_io_vfs_file_read(ra8_io_vfs_file_t *file, void *buf, uint32_t bytes, uint32_t *out_read)
Read bytes through a format-neutral stream.
Definition ra8_io_vfs.c:637
ra8_err_t ra8_io_vfs_file_seek(ra8_io_vfs_file_t *file, uint64_t offset_bytes)
Seek a format-neutral stream.
Definition ra8_io_vfs.c:667
@ k_ra8_mdl_sha256_bytes
SHA-256 digest size in bytes.
One open chunked .rabook file: parsed geometry + caller storage.
uint64_t inflated_total
Served flat-blob length.
Fixed 100-byte prologue describing every table and pool in the blob.
Definition book.h:246
Caller-owned decompression and strict-validation workspace binding.
uint32_t table_cap
Entries available at table.
uint32_t compressed_cap
Bytes available at compressed.
uint32_t chunk_cap
Bytes available at chunk.
uint8_t * scratch
Strict semantic/CRC workspace.
uint64_t * table
Parsed RBKC offset-table workspace.
uint32_t scratch_cap
Bytes available at scratch.
uint8_t * compressed
One compressed-chunk staging span.
book_inflate_fn inflate_cb
Heap-free RFC 1950 inflater.
uint8_t * chunk
One complete inflated-chunk span.
One initialized strict validator or one open validated RBKC reader.
uint8_t * scratch
Caller strict workspace.
uint32_t table_cap
Table entry capacity.
uint8_t * chunk
Caller inflated chunk.
ra8_io_vfs_file_t * file
Borrowed VFS facade slot.
uint64_t * table
Caller table workspace.
uint8_t digest[k_ra8_mdl_sha256_bytes]
Transfer digest at validate.
book_header_t header
Strict decoded header.
uint8_t * compressed
Caller compressed staging.
uint32_t scratch_cap
Strict workspace bytes.
bool transfer_validated
Stage validation succeeded.
book_inflate_fn inflate_cb
Caller inflater.
uint64_t file_size
Validated RBKC byte length.
uint32_t chunk_cap
Inflated chunk bytes.
book_chunked_t reader
Bound RBKC reader.
uint32_t compressed_cap
Compressed staging bytes.
bool open
Live final-file reader.