ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_verify_rabook.c
Go to the documentation of this file.
1
13
14#include <stddef.h>
15
16#include "book_chunked.h"
18#include "miniz.h"
19
21typedef enum : uint32_t {
22 k_rabook_chunk_bytes = 4U * 1024U * 1024U,
25 k_rabook_scratch_bytes = 4U * 1024U * 1024U,
29
31typedef struct {
33 uint64_t size_bytes;
34 uint32_t calls;
36
57static ra8_err_t
58internal_rabook_read(void* opaque, uint64_t offset, uint8_t* destination, uint32_t length)
59{
60 mdl_rabook_io_t* io = (mdl_rabook_io_t*)opaque;
61 if ((offset > io->size_bytes) || ((uint64_t)length > (io->size_bytes - offset))) {
63 }
64 ra8_err_t error = fw_fs_seek(io->file, offset);
65 uint32_t done = 0U;
66 while ((error == k_ra8_ok) && (done < length)) {
67 if (io->calls >= (uint32_t)k_rabook_read_calls) {
69 }
70 uint32_t got = 0U;
71 error = fw_fs_read(io->file, &destination[done], length - done, &got);
72 ++io->calls;
73 if ((error == k_ra8_ok) && (got == 0U)) {
75 }
76 done += got;
77 }
78 return error;
79}
80
101static ra8_err_t internal_rabook_inflate(const void* source,
102 size_t source_bytes,
103 void* destination,
104 size_t destination_bytes,
105 size_t* out_bytes)
106{
107 const size_t result = tinfl_decompress_mem_to_mem(
108 destination,
109 destination_bytes,
110 source,
111 source_bytes,
112 (int)(TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF));
113 if (result == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED) {
115 }
116 *out_bytes = result;
117 return k_ra8_ok;
118}
119
121 uint64_t size_bytes,
122 mdl_export_workspace_t* workspace,
123 mdl_verify_report_t* report)
124{
125 if ((file == nullptr) || (workspace == nullptr) || (report == nullptr)) {
127 }
128 uint64_t* table =
129 (uint64_t*)mdl_export_workspace_take(workspace,
130 (size_t)k_rabook_table_entries * sizeof(uint64_t),
131 _Alignof(uint64_t));
132 uint8_t* compressed = (uint8_t*)mdl_export_workspace_take(workspace,
134 _Alignof(max_align_t));
135 uint8_t* chunk = (uint8_t*)mdl_export_workspace_take(workspace,
136 (size_t)k_rabook_chunk_bytes,
137 _Alignof(max_align_t));
138 uint8_t* scratch = (uint8_t*)mdl_export_workspace_take(workspace,
140 _Alignof(max_align_t));
141 if ((table == nullptr) || (compressed == nullptr) || (chunk == nullptr) || (scratch == nullptr)) {
143 }
144 mdl_rabook_io_t io = {.file = file, .size_bytes = size_bytes};
145 book_chunked_t reader = {};
146 ra8_err_t error = book_chunked_open(&reader,
148 &io,
149 size_bytes,
151 table,
152 (uint32_t)k_rabook_table_entries,
153 compressed,
154 (uint32_t)k_rabook_compressed_bytes);
155 book_header_t header = {};
156 if (error == k_ra8_ok) {
157 error = book_chunked_validate_strict(&reader,
158 chunk,
159 (uint32_t)k_rabook_chunk_bytes,
160 scratch,
161 (uint32_t)k_rabook_scratch_bytes,
162 &header);
163 }
164 if (error == k_ra8_ok) {
165 report->page_count = header.chapter_count;
166 report->member_count = header.image_count;
167 report->metadata_present =
168 (header.title_off != 0U) || (header.author_off != 0U) || (header.identifier_off != 0U);
169 }
170 return error;
171}
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_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 fw_fs_read(fw_fs_file_t *file, uint8_t *dst, uint32_t cap, uint32_t *out_read)
Read up to cap bytes; zero bytes is EOF.
Definition fw_if_fs.c:699
ra8_err_t fw_fs_seek(fw_fs_file_t *file, uint64_t absolute_offset)
Seek to an absolute byte offset from the beginning.
Definition fw_if_fs.c:736
void * mdl_export_workspace_take(mdl_export_workspace_t *ws, size_t bytes, size_t alignment)
Reserve aligned bytes from an exporter arena.
struct mdl_export_workspace mdl_export_workspace_t
Caller-owned bounded arena for all exporter scratch state.
@ k_rabook_compressed_bytes
One complete zlib stream.
@ k_rabook_chunk_bytes
Independent RBKC chunk size.
@ k_rabook_read_calls
EPUB random-read ceiling.
static ra8_err_t internal_rabook_read(void *opaque, uint64_t offset, uint8_t *destination, uint32_t length)
Serve one exact positioned RBKC read through the portable file facade.
ra8_err_t priv_mdl_verify_rabook(fw_fs_file_t *file, uint64_t size_bytes, mdl_export_workspace_t *workspace, mdl_verify_report_t *report)
Strictly validate one borrowed RBKC .rabook file.
mdl_verify_rabook_limit_t
Bounded strict-reader workspace profile.
@ k_rabook_table_entries
At most 65,536 RBKC chunks.
@ k_rabook_scratch_bytes
CRC and node ownership work.
static ra8_err_t internal_rabook_inflate(const void *source, size_t source_bytes, void *destination, size_t destination_bytes, size_t *out_bytes)
Inflate one complete RFC 1950 chunk into caller storage.
Private strict RBKC validator seam for media downloader artifacts.
#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).
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
Definition ra8_err.h:337
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_err_validation_failed
Validation rule failed (caller-supplied invariant not satisfied).
Definition ra8_err.h:459
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ 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
One open chunked .rabook file: parsed geometry + caller storage.
Fixed 100-byte prologue describing every table and pool in the blob.
Definition book.h:246
uint32_t author_off
String-pool offset of the author.
Definition book.h:252
uint32_t chapter_count
Number of spine chapters.
Definition book.h:256
uint32_t image_count
Number of image descriptors.
Definition book.h:264
uint32_t title_off
String-pool offset of the book title.
Definition book.h:251
uint32_t identifier_off
String-pool offset of the unique book id.
Definition book.h:254
Caller-owned open file; fields are private to the facade.
Positioned exact-read adapter state.
uint64_t size_bytes
Immutable complete extent.
fw_fs_file_t * file
Borrowed portable file.
uint32_t calls
Bounded backend read attempts.