|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Flat, execute-in-place container for a build-time "compiled" e-book. More...
Go to the source code of this file.
Data Structures | |
| struct | book_header_t |
| Fixed 100-byte prologue describing every table and pool in the blob. More... | |
| struct | book_chapter_t |
| One spine document (a renderable chapter) plus its TOC label. More... | |
| struct | book_node_t |
| One DOM node. More... | |
| struct | book_attr_t |
| One name="value" attribute on an element. More... | |
| struct | book_stylesheet_t |
| A preserved CSS stylesheet and the chapter it scopes to. More... | |
| struct | book_image_t |
| Descriptor for one transcoded image in the image pool. More... | |
Typedefs | |
| typedef ra8_err_t(* | book_inflate_fn) (const void *src, size_t src_len, void *dst, size_t dst_cap, size_t *out_len) |
| Caller-supplied DEFLATE inflater used by book_open(). | |
Enumerations | |
| enum | book_version_t : uint32_t { k_book_format_version = 1U } |
| Wire-format version stamped into every blob header. More... | |
| enum | book_container_t : uint8_t { k_book_container_magic_len = 4U , k_book_container_header_len = 24U , k_book_container_entry_len = 8U } |
| Constants of the on-disk .rabook chunked compression container. More... | |
| enum | book_sentinel_t : uint32_t { k_book_nil = 0xFFFFFFFFU } |
| Reserved index value meaning "no such element". More... | |
| enum | book_flag_t : uint32_t { k_book_flag_rtl = 0x00000001U , k_book_flag_mask_known = 0x00000001U } |
| Feature-flag bits carried in book_header_t.flags. More... | |
| enum | book_node_kind_t : uint8_t { k_book_node_element = 0U , k_book_node_text = 1U } |
| Discriminates the two kinds of DOM node. More... | |
| enum | book_image_format_t : uint8_t { k_book_image_gray4 = 0U , k_book_image_svg = 1U } |
| Pixel encoding of an entry in the image pool. More... | |
| enum | book_image_pixfmt_t : uint8_t { k_book_pixfmt_gray4 = 0U , k_book_pixfmt_gray8 = 1U } |
| Pixel depth of a k_book_image_gray4 raster in the image pool. More... | |
| enum | book_struct_size_t : uint16_t { k_book_sizeof_header = 100U , k_book_sizeof_chapter = 12U , k_book_sizeof_node = 24U , k_book_sizeof_attr = 8U , k_book_sizeof_stylesheet = 8U , k_book_sizeof_image = 24U } |
| Pinned on-disk sizes of the blob's fixed-layout records. More... | |
Functions | |
| static const book_header_t * | book_header (const void *base) |
| View the blob base as its header. | |
| static bool | book_is_rtl (const void *base) |
| Whether the book declares right-to-left reading order (manga). | |
| static const void * | book_at (const void *base, uint32_t off) |
| Resolve a blob-relative byte offset to a pointer. | |
| static const char * | book_string (const void *base, uint32_t off) |
| Resolve a string-pool offset to a NUL-terminated UTF-8 string. | |
| static const book_chapter_t * | book_chapters (const void *base) |
| Base of the chapter table. | |
| static const book_node_t * | book_nodes (const void *base) |
| Base of the DOM node table. | |
| static const book_attr_t * | book_attrs (const void *base) |
| Base of the attribute table. | |
| static const book_stylesheet_t * | book_stylesheets (const void *base) |
| Base of the stylesheet table. | |
| static const book_image_t * | book_images (const void *base) |
| Base of the image table. | |
| static const char * | book_node_name (const void *base, const book_node_t *node) |
| Tag name of an element node. | |
| static const char * | book_node_text (const void *base, const book_node_t *node) |
| Text of a text node. | |
| static const uint8_t * | book_image_data (const void *base, const book_image_t *img) |
| Image-pool pointer to one image's compressed pixel data. | |
| static book_image_pixfmt_t | book_image_pixfmt (const book_image_t *img) |
| Declared pixel depth of one image descriptor. | |
| ra8_err_t | book_validate (const void *base, size_t size) |
| Validate that a byte buffer is a well-formed, intact .rabook blob. | |
| ra8_err_t | book_open (const void *file, size_t file_len, book_inflate_fn inflate, void *scratch, size_t scratch_cap, const void **out_base, size_t *out_size) |
| Open a .rabook file: check the container, inflate, validate the blob. | |
| ra8_err_t | book_chapter_to_xhtml (const void *base, uint32_t chapter_idx, char *out, size_t cap, size_t *out_len) |
| Serialize one chapter's DOM subtree back to XHTML for the renderer. | |
| ra8_err_t | book_chapter_text (const void *base, uint32_t chapter_idx, char *out, size_t cap, size_t *out_len) |
| Extract one chapter's readable plain text from the DOM. | |
Flat, execute-in-place container for a build-time "compiled" e-book.
book is the on-device representation of a book that has already been unzipped, XML-parsed and image-transcoded on the host by tools/epub_compile. The firmware never unzips or parses XHTML at runtime. A .rabook file is a chunked container – the magic "RBKC", a fixed header naming the chunk size / inflated total / chunk count, a flat chunk table, then one independent zlib stream per fixed-size slice of the flat blob – so it stays compact on flash / SD and stays seekable: any chunk can be inflated alone, without touching the rest of the file. book_open() inflates every chunk once into a caller-provided SDRAM scratch buffer (the resident fast path); book_chunked.h instead inflates single chunks on demand into ra8_vmem cache frames so a book far larger than RAM is read with a bounded working set. Either way the firmware then points a const book_header_t* at inflated bytes and walks them with the inline accessors below. Every internal reference is a byte offset relative to the blob base, so the inflated structure is position-independent and walked with no further parsing or copying.
Definition in file book.h.
| typedef ra8_err_t( * book_inflate_fn) (const void *src, size_t src_len, void *dst, size_t dst_cap, size_t *out_len) |
Caller-supplied DEFLATE inflater used by book_open().
Keeps book decoupled from any one decompressor: the firmware passes a miniz-backed inflater, host tests pass a zlib one.
| [in] | src | Start of the raw DEFLATE stream. |
| [in] | src_len | DEFLATE stream length in bytes. |
| [out] | dst | Destination buffer to inflate into. |
| [in] | dst_cap | Capacity of dst in bytes. |
| [out] | out_len | Receives the number of bytes written to dst. |
| enum book_container_t : uint8_t |
Constants of the on-disk .rabook chunked compression container.
A file is (all integers little-endian):
Chunk i inflates to exactly min(chunk_bytes, inflated_total - i * chunk_bytes) bytes of the flat blob described by book_header_t, so any chunk can be inflated independently – the property the ra8_vmem paged read path (book_chunked.h) relies on. Producers size chunk_bytes equal to the reader's ra8_vmem frame size so one chunk fills exactly one cache frame. book_open() inflates all chunks (resident).
| enum book_flag_t : uint32_t |
Feature-flag bits carried in book_header_t.flags.
Flags extend the v1 layout without a format-version bump: a bit may only change how content is presented, never where any table or pool lives. book_validate() rejects a blob whose flags sets any bit outside k_book_flag_mask_known, so an old firmware never silently mis-renders a book that depends on a semantic it does not implement. Kept in lockstep with FLAG_RTL in tools/epub_compile/src/epub_compile.py (emitted by the CBZ arm, tools/epub_compile/src/cbz_compile.py, under --rtl).
| Enumerator | |
|---|---|
| k_book_flag_rtl | Right-to-left reading order (manga): spine pages advance leaf-first, and the reader mirrors its page-turn zones. |
| k_book_flag_mask_known | Every bit this firmware understands; a set bit outside this mask fails book_validate(). |
| enum book_image_format_t : uint8_t |
Pixel encoding of an entry in the image pool.
Raster images use panel-native 4-bit grayscale; SVG keeps its vector source for on-device rasterization. An enum so future encodings can be added without a flag-day.
| Enumerator | |
|---|---|
| k_book_image_gray4 | 4bpp gray, 2px/byte; pixel (x,y) is at flat index y*width + x. |
| k_book_image_svg | Verbatim UTF-8 SVG source (vector; on-device rasterized). |
| enum book_image_pixfmt_t : uint8_t |
Pixel depth of a k_book_image_gray4 raster in the image pool.
A second axis, orthogonal to book_image_format_t: that field only says whether an entry is a packed-grayscale raster or verbatim SVG, while this one – modelled on JOF's bpp header field – records how DEEP that raster is. A grayscale e-reader can then rasterize at 4bpp (half the storage, and exactly right for an even 16-level e-ink panel) while a device with more headroom carries the lossless 8bpp source; the compiler picks the depth by device profile instead of baking 4bpp in for every reader.
It lives in the descriptor's former padding byte (book_image_t::pixel_format), so it costs no format growth and needs no book_version_t bump: every .rabook written before this field existed zero-filled that byte, and 0 is k_book_pixfmt_gray4 – exactly the 4bpp packing those blobs already carried. New firmware therefore reads every pre-existing blob unchanged (backward-read); an unknown depth is rejected by book_validate() rather than silently mis-rendered.
| Enumerator | |
|---|---|
| k_book_pixfmt_gray4 | 4bpp packed grayscale, 2px/byte (default; every pre-field blob). |
| k_book_pixfmt_gray8 | 8bpp grayscale, 1px/byte (lossless against any grey panel). |
| enum book_node_kind_t : uint8_t |
| enum book_sentinel_t : uint32_t |
Reserved index value meaning "no such element".
Used for first_attr, first_child, next_sibling, cover_image_index and a stylesheet's scope_chapter to mean "none" / "applies to all", since a real index can never be 0xFFFFFFFF (the table count is bounded far below that).
| Enumerator | |
|---|---|
| k_book_nil | Absent index / "applies to all chapters". |
| enum book_struct_size_t : uint16_t |
Pinned on-disk sizes of the blob's fixed-layout records.
The format is a binary wire layout shared with the host compiler, so each record's byte size is part of the contract. These named constants drive the static_asserts that guard against accidental padding or a silent field change.
| Enumerator | |
|---|---|
| k_book_sizeof_header | Bytes in book_header_t. |
| k_book_sizeof_chapter | Bytes in book_chapter_t. |
| k_book_sizeof_node | Bytes in book_node_t. |
| k_book_sizeof_attr | Bytes in book_attr_t. |
| k_book_sizeof_stylesheet | Bytes in book_stylesheet_t. |
| k_book_sizeof_image | Bytes in book_image_t. |
| enum book_version_t : uint32_t |
|
inlinestatic |
Resolve a blob-relative byte offset to a pointer.
| [in] | base | Blob base (non-NULL). |
| [in] | off | Byte offset within the blob (validated < total_size). |
Definition at line 427 of file book.h.
Referenced by book_attrs(), book_chapters(), book_image_data(), book_images(), book_nodes(), book_string(), and book_stylesheets().
|
inlinestatic |
Base of the attribute table.
| [in] | base | Blob base (non-NULL, validated). |
Definition at line 488 of file book.h.
References book_at(), and book_header().
Referenced by internal_emit_attrs().
| ra8_err_t book_chapter_text | ( | const void * | base, |
| uint32_t | chapter_idx, | ||
| char * | out, | ||
| size_t | cap, | ||
| size_t * | out_len ) |
Extract one chapter's readable plain text from the DOM.
Walks the chapter's element/text tree iteratively (no recursion, bounded stack) and writes its text runs into out, inserting a newline at each block-level element so paragraphs stay separated. Markup, attributes and inline structure are dropped – this is for a simple word-wrap reader, not rich layout. The output is NOT NUL-terminated.
| [in] | base | Validated book blob base (non-NULL). |
| [in] | chapter_idx | Spine chapter index (< header chapter_count). |
| [out] | out | Destination text buffer (non-NULL). |
| [in] | cap | Capacity of out in bytes. |
| [out] | out_len | Receives the text byte length written. |
| k_ra8_ok | Chapter text extracted. |
| k_ra8_err_null_ptr | A required pointer argument is NULL. |
| k_ra8_err_invalid_arg | chapter_idx is out of range. |
| k_ra8_err_invalid_size | Output did not fit cap, or DOM nesting exceeded the bounded walk stack. |
base was accepted by book_validate() / book_open(). chapter_idx is less than book_header(base)->chapter_count. out contents are unspecified.out. Definition at line 683 of file book_xhtml.c.
References book_chapters(), book_header(), book_header_t::chapter_count, internal_walk_text(), k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_ok, book_header_t::node_count, RA8_CHECK_NULL_PTR, book_chapter_t::root_node, and s_tag_xhtml.
Referenced by book_chapter_text_src(), and imp_walk_book().
| ra8_err_t book_chapter_to_xhtml | ( | const void * | base, |
| uint32_t | chapter_idx, | ||
| char * | out, | ||
| size_t | cap, | ||
| size_t * | out_len ) |
Serialize one chapter's DOM subtree back to XHTML for the renderer.
A bridge for feeding a compiled book into reflow_layout_chapter(), which consumes XHTML. Walks the chapter's element/text tree iteratively (no recursion, bounded stack) and writes well-formed XHTML – every tag, attribute and text run, faithfully – into out. Void elements self-close; text and attribute values are entity-escaped. The output is NOT NUL-terminated.
| [in] | base | Validated book blob base (non-NULL). |
| [in] | chapter_idx | Spine chapter index (< header chapter_count). |
| [out] | out | Destination XHTML buffer (non-NULL). |
| [in] | cap | Capacity of out in bytes. |
| [out] | out_len | Receives the XHTML byte length written. |
| k_ra8_ok | Chapter serialized; out_len bytes written. |
| k_ra8_err_null_ptr | A required pointer argument is NULL. |
| k_ra8_err_invalid_arg | chapter_idx is out of range. |
| k_ra8_err_invalid_size | Output did not fit cap, or DOM nesting exceeded the bounded walk stack. |
base was accepted by book_validate() / book_open(). cap is large enough for the chapter's serialized XHTML. out contents are unspecified.out. Definition at line 702 of file book_xhtml.c.
References book_chapters(), book_header(), book_header_t::chapter_count, internal_walk_to_xhtml(), k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_ok, book_header_t::node_count, RA8_CHECK_NULL_PTR, book_chapter_t::root_node, and s_tag_xhtml.
Referenced by erb_render_chapter().
|
inlinestatic |
Base of the chapter table.
| [in] | base | Blob base (non-NULL, validated). |
Definition at line 458 of file book.h.
References book_at(), and book_header().
Referenced by book_chapter_text(), book_chapter_to_xhtml(), and render_held_page().
|
inlinestatic |
View the blob base as its header.
| [in] | base | Pointer to the first byte of a .rabook blob (non-NULL). |
Definition at line 382 of file book.h.
Referenced by book_attrs(), book_chapter_text(), book_chapter_to_xhtml(), book_chapters(), book_image_data(), book_images(), book_is_rtl(), book_is_valid(), book_nodes(), book_string(), book_stylesheets(), erb_render_image(), imp_walk_book(), internal_check_compiled_page_count(), main(), publish_result(), render_held_page(), and verify_blob().
|
inlinestatic |
Image-pool pointer to one image's compressed pixel data.
| [in] | base | Blob base (non-NULL, validated). |
| [in] | img | Image descriptor (non-NULL). |
Definition at line 566 of file book.h.
References book_at(), book_header(), and book_image_t::data_off.
Referenced by erb_render_image().
|
inlinestatic |
Declared pixel depth of one image descriptor.
Decodes book_image_t::pixel_format into its book_image_pixfmt_t. Meaningful only for a k_book_image_gray4 raster (4bpp vs 8bpp packing); an SVG entry stores 0 and reports k_book_pixfmt_gray4, so a renderer must branch on book_image_t::format first and only consult this for a raster. Every blob written before the field existed zero-filled the byte, so an old gray4 blob reports k_book_pixfmt_gray4 unchanged (backward-read).
| [in] | img | Image descriptor (non-NULL) obtained from book_images(). |
| k_book_pixfmt_gray4 | 4bpp packed grayscale (2px/byte); also every pre-field blob and every SVG entry. |
| k_book_pixfmt_gray8 | 8bpp grayscale (1px/byte). |
Definition at line 599 of file book.h.
References book_image_t::pixel_format.
Referenced by book_src_image_rect(), and erb_render_image().
|
inlinestatic |
Base of the image table.
| [in] | base | Blob base (non-NULL, validated). |
Definition at line 518 of file book.h.
References book_at(), and book_header().
Referenced by erb_render_image(), and internal_image_pixfmts_known().
|
inlinestatic |
Whether the book declares right-to-left reading order (manga).
Reads k_book_flag_rtl out of the header's flags word. An RTL book's spine is still stored first-page-first; the flag tells the reader to mirror its presentation (page-turn zones, spread direction). Consuming the flag in the page-turn UI is the reader's job; this accessor only decodes it.
| [in] | base | Pointer to the first byte of a .rabook blob (non-NULL). |
| true | The book reads right-to-left. |
| false | The book reads left-to-right (every v1 blob before the flag). |
Definition at line 411 of file book.h.
References book_header(), and k_book_flag_rtl.
|
inlinestatic |
Tag name of an element node.
| [in] | base | Blob base (non-NULL, validated). |
| [in] | node | Element node (non-NULL, kind == k_book_node_element). |
Definition at line 534 of file book.h.
References book_string(), and book_node_t::name_off.
|
inlinestatic |
Text of a text node.
| [in] | base | Blob base (non-NULL, validated). |
| [in] | node | Text node (non-NULL, kind == k_book_node_text). |
Definition at line 550 of file book.h.
References book_string(), and book_node_t::text_off.
Referenced by collect_chapter_text().
|
inlinestatic |
Base of the DOM node table.
| [in] | base | Blob base (non-NULL, validated). |
Definition at line 473 of file book.h.
References book_at(), and book_header().
Referenced by collect_chapter_text(), internal_walk_text(), and internal_walk_to_xhtml().
| ra8_err_t book_open | ( | const void * | file, |
| size_t | file_len, | ||
| book_inflate_fn | inflate, | ||
| void * | scratch, | ||
| size_t | scratch_cap, | ||
| const void ** | out_base, | ||
| size_t * | out_size ) |
Open a .rabook file: check the container, inflate, validate the blob.
Parses the "RBKC" container header and chunk table (see book_container_t for the layout), inflates every chunk's zlib stream in order into the caller-owned scratch buffer (expected to live in SDRAM), then runs book_validate() over the reassembled flat blob. On success *out_base is the validated blob base (equal to scratch) ready for the inline accessors. This is the resident open – the whole inflated blob must fit scratch_cap; a book larger than the resident budget is instead read chunk-by-chunk through book_chunked.h + book_src_paged().
| [in] | file | Pointer to the .rabook file bytes (non-NULL). |
| [in] | file_len | Length of file in bytes. |
| [in] | inflate | Decompressor callback (see book_inflate_fn). |
| [out] | scratch | Buffer that receives the inflated blob (non-NULL). |
| [in] | scratch_cap | Capacity of scratch; must be >= the inflated total. |
| [out] | out_base | Receives the validated blob base on success. |
| [out] | out_size | Receives the inflated blob length on success. |
| k_ra8_ok | Container valid, inflated, and blob validated. |
| k_ra8_err_null_ptr | A required pointer argument is NULL. |
| k_ra8_err_invalid_arg | Container magic / header geometry / chunk table is malformed (bad magic, zero chunk size, count disagreeing with the total, non-monotonic table, table end disagreeing with the payload length). |
| k_ra8_err_invalid_size | File too short, scratch_cap too small, or a chunk inflated to a length other than its span. |
| k_ra8_err_range_check_failed | Blob CRC mismatch (from book_validate()). |
Definition at line 543 of file book.c.
References internal_open_body(), RA8_CHECK_NULL_PTR, and s_tag_book.
|
inlinestatic |
Resolve a string-pool offset to a NUL-terminated UTF-8 string.
| [in] | base | Blob base (non-NULL, validated). |
| [in] | off | String-pool offset. |
Definition at line 443 of file book.h.
References book_at(), and book_header().
Referenced by book_node_name(), book_node_text(), imp_walk_book(), internal_emit_attrs(), internal_open_element(), internal_walk_text(), and internal_walk_to_xhtml().
|
inlinestatic |
Base of the stylesheet table.
| [in] | base | Blob base (non-NULL, validated). |
Definition at line 503 of file book.h.
References book_at(), and book_header().
| ra8_err_t book_validate | ( | const void * | base, |
| size_t | size ) |
Validate that a byte buffer is a well-formed, intact .rabook blob.
Checks, in order: the magic and format_version; that flags sets no bit outside k_book_flag_mask_known (a blob relying on a presentation semantic this firmware does not implement must be rejected, not mis-read); that total_size fits in size; that every table offset plus its extent and every pool lie within total_size; that every image descriptor names a book_image_pixfmt_t this build can unpack (an unknown depth is refused rather than mis-blitted); and finally the CRC-32 of the body. Must be called once before any accessor is used on base; the accessors assume a validated blob and do no bounds checking themselves (they are pure offset arithmetic for XIP).
| [in] | base | Pointer to the candidate blob (may be NULL). |
| [in] | size | Number of readable bytes at base. |
| k_ra8_ok | Blob is well-formed and CRC matches. |
| k_ra8_err_null_ptr | base is NULL. |
| k_ra8_err_invalid_arg | Magic is wrong, the format version is unknown, flags carries an unknown feature bit, or an image declares an unknown pixel format. |
| k_ra8_err_invalid_size | size is too small or a table/pool runs past total_size. |
| k_ra8_err_range_check_failed | CRC-32 of the body does not match the header. |
Definition at line 231 of file book.c.
References book_header_t::attr_count, book_header_t::attr_off, book_header_t::chapter_count, book_header_t::chapter_off, book_header_t::crc32_val, book_header_t::flags, book_header_t::format_version, book_header_t::image_count, book_header_t::image_off, book_header_t::image_pool_off, book_header_t::image_pool_size, internal_crc32(), internal_image_pixfmts_known(), internal_magic_ok(), internal_table_fits(), k_book_flag_mask_known, k_book_format_version, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_range_check_failed, k_ra8_ok, book_header_t::node_count, book_header_t::node_off, RA8_CHECK_NULL_PTR, s_tag_book, book_header_t::string_off, book_header_t::string_size, book_header_t::stylesheet_count, book_header_t::stylesheet_off, and book_header_t::total_size.
Referenced by erb_render_image(), imp_walk_book(), internal_compile_temp(), internal_dispatch_and_cache(), internal_open_body(), main(), and verify_blob().