|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Content-identity hashing (FNV-1a 64) for the media downloader's persistent library state. More...
#include <stddef.h>#include <stdint.h>#include "fw_if_fs.h"#include "mdl_storage.h"#include "ra8_err.h"Go to the source code of this file.
Enumerations | |
| enum | mdl_fnv_const_t : uint64_t { k_mdl_fnv_offset = 0xCBF29CE484222325ULL , k_mdl_fnv_prime = 0x00000100000001B3ULL , k_mdl_hash_max_file_bytes = 8192000000ULL } |
| FNV-1a 64-bit constants (the canonical parameters). More... | |
Functions | |
| uint64_t | mdl_hash_bytes_seed (const void *data, size_t len, uint64_t seed) |
| Continue an FNV-1a 64 fold over a byte range from a running state. | |
| uint64_t | mdl_hash_bytes (const void *data, size_t len) |
| FNV-1a 64 hash of a byte range. | |
| uint64_t | mdl_hash_str (const char *s) |
| FNV-1a 64 hash of a NUL-terminated string (excluding the NUL). | |
| ra8_err_t | mdl_hash_file (mdl_storage_t *storage, const char *path, uint64_t *out) |
| FNV-1a 64 hash of a file's full contents. | |
| ra8_err_t | mdl_hash_stream (fw_fs_file_t *file, uint64_t file_size, uint8_t *buffer, uint32_t buffer_bytes, uint64_t *out) |
| Hash exactly one snapshotted extent from an already-open stream. | |
Content-identity hashing (FNV-1a 64) for the media downloader's persistent library state.
The library-state store (mdl_state) records a stable 64-bit identity for every fetched page so a re-run can (a) skip re-downloading a byte-identical image already held – including one shared across chapters – and (b) detect a torn file left by a kill mid-write and refetch it rather than package it. The same seam URL-hashes a page's source address so dedup lookup is a cheap fixed-width key rather than a full-string scan.
FNV-1a 64 is chosen deliberately: it is a few lines, allocation-free, endian neutral over a byte stream, and already the family the firmware's golden framebuffer checks use – so a .mdl_state written on the host and one a future on-device port writes agree bit for bit. It is a NON-cryptographic identity hash: it defends against accidental corruption and duplication, not against an adversary crafting a collision, which is not part of this threat model (the bytes are already trusted enough to store on disk and read back).
Definition in file mdl_hash.h.
| enum mdl_fnv_const_t : uint64_t |
FNV-1a 64-bit constants (the canonical parameters).
| Enumerator | |
|---|---|
| k_mdl_fnv_offset | FNV-1a 64 offset basis. |
| k_mdl_fnv_prime | FNV-1a 64 prime. |
| k_mdl_hash_max_file_bytes | Exact file hash bound. |
Definition at line 34 of file mdl_hash.h.
| uint64_t mdl_hash_bytes | ( | const void * | data, |
| size_t | len ) |
FNV-1a 64 hash of a byte range.
A single-shot fold of data starting from k_mdl_fnv_offset (it delegates to mdl_hash_bytes_seed with that seed). An empty range (len == 0) hashes to the offset basis, and a NULL data with a non-zero len is treated as empty rather than dereferenced.
| [in] | data | First byte of the range, or NULL for an empty range. |
| [in] | len | Number of bytes to fold. |
| k_mdl_fnv_offset | When len is 0 or data is NULL. |
data, when non-NULL, addresses at least len readable bytes. Definition at line 35 of file mdl_hash.c.
References k_mdl_fnv_offset, and mdl_hash_bytes_seed().
Referenced by internal_cache_publish(), mdl_hash_str(), and priv_mdl_cache_read_body().
| uint64_t mdl_hash_bytes_seed | ( | const void * | data, |
| size_t | len, | ||
| uint64_t | seed ) |
Continue an FNV-1a 64 fold over a byte range from a running state.
The streaming primitive: folds data into seed and returns the new running digest, so a large file can be hashed chunk by chunk with no dynamic allocation. Passing k_mdl_fnv_offset as seed for the first chunk makes a chunked hash identical to a single-shot mdl_hash_bytes of the whole range.
| [in] | data | First byte of the chunk, or NULL for an empty chunk. |
| [in] | len | Number of bytes to fold. |
| [in] | seed | Running digest so far (k_mdl_fnv_offset to start fresh). |
data into seed. | seed | When len is 0 or data is NULL (nothing to fold). |
data, when non-NULL, addresses at least len readable bytes. seed is either k_mdl_fnv_offset or a prior result of this function. Definition at line 21 of file mdl_hash.c.
References k_mdl_fnv_prime.
Referenced by internal_build_export_metadata(), internal_cache_payload_identity(), internal_cache_read_record(), internal_copy_payload(), mdl_hash_bytes(), mdl_hash_stream(), mdl_storage_txn_write(), and priv_mdl_export_zip_read().
| ra8_err_t mdl_hash_file | ( | mdl_storage_t * | storage, |
| const char * | path, | ||
| uint64_t * | out ) |
FNV-1a 64 hash of a file's full contents.
Queries path through the injected portable filesystem, rejects non-regular objects before reading, then hashes a regular file up to the implementation's 8.192 GB safety limit through bounded reads and caller-owned scratch. The size is snapshotted before reading; short input, a trailing byte from a concurrent append, or an exhausted read-call ceiling fails rather than publishing a partial-prefix digest. Used to verify a page already on disk still matches the identity recorded in state (a torn file re-hashes to a different value and is refetched) and to record the identity of a freshly fetched page.
| [in,out] | storage | Initialized filesystem binding and file workspace. |
| [in] | path | Canonical portable file path (never NULL). |
| [out] | out | Receives the 64-bit digest on success (never NULL). |
| k_ra8_ok | File read fully and hashed; *out is set. |
| k_ra8_err_invalid_arg | path or out was NULL, or the path was a symlink or non-regular object. |
| k_ra8_err_invalid_size | The regular file exceeded the safety bound. |
| k_ra8_err_not_found | The path or a path component did not exist. |
| k_ra8_err_access_denied | The process lacked permission to open it. |
| k_ra8_fail | Other open/stat/read/close failure. |
storage, path, and out are non-NULL. path directly names a readable regular file for success. Definition at line 103 of file mdl_hash.c.
References fw_fs_stat_t::exists, mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, fw_fs_close(), fw_fs_open(), fw_fs_stat(), mdl_storage_t::io_buffer, mdl_storage_t::io_buffer_bytes, k_fw_fs_node_file, k_fw_fs_open_read, k_mdl_hash_max_file_bytes, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_found, k_ra8_ok, mdl_hash_stream(), fw_fs_t::names, fw_fs_stat_t::size_bytes, fw_fs_t::streams, and fw_fs_stat_t::type.
Referenced by internal_mdl_fetch_try_reuse(), and internal_verify_page_rec().
| uint64_t mdl_hash_str | ( | const char * | s | ) |
FNV-1a 64 hash of a NUL-terminated string (excluding the NUL).
The dedup key for a page's source URL: two runs that scrape the same URL produce the same key, so a page already held is found without re-fetching.
| [in] | s | String to hash, or NULL. |
s's bytes. | k_mdl_fnv_offset | When s is NULL or empty. |
s, when non-NULL, is NUL-terminated. s is not modified.Definition at line 40 of file mdl_hash.c.
References k_mdl_fnv_offset, mdl_hash_bytes(), and strlen().
Referenced by internal_build_export_metadata(), internal_cache_paths(), internal_cache_prepare(), internal_cache_publish(), internal_cache_record_valid(), internal_mdl_fetch_one_page(), internal_mdl_fetch_prepare_page(), internal_mdl_fetch_publish_page(), and internal_mdl_fetch_resolve_not_modified().
| ra8_err_t mdl_hash_stream | ( | fw_fs_file_t * | file, |
| uint64_t | file_size, | ||
| uint8_t * | buffer, | ||
| uint32_t | buffer_bytes, | ||
| uint64_t * | out ) |
Hash exactly one snapshotted extent from an already-open stream.
Reads the declared extent through bounded caller scratch, folds every byte into FNV-1a, then requires an immediate EOF read before publishing the digest. Short input, trailing growth, zero progress, and call-bound exhaustion all fail closed.
| [in,out] | file | Readable generic file positioned at byte zero. |
| [in] | file_size | Exact byte extent that must be followed by EOF. |
| [out] | buffer | Caller-owned read scratch. |
| [in] | buffer_bytes | Nonzero extent of buffer. |
| [out] | out | Digest written only after the exact extent and EOF are read. |
| k_ra8_ok | The exact extent and EOF were observed and hashed. |
| k_ra8_err_invalid_arg | A pointer or scratch extent is invalid. |
| k_ra8_err_invalid_size | The file/call bound is exceeded. |
| k_ra8_fail | The stream shrank, grew, or stopped making progress. |
| other | A generic stream read error propagated. |
file is open for reading and positioned at byte zero. buffer covers buffer_bytes writable bytes and out is writable. file positioned at EOF and writes out. out untouched. Hash exactly one snapshotted extent from an already-open stream.
| [in,out] | file | Open generic file positioned at byte zero. |
| [in] | file_size | Immutable size snapshot obtained from fw_fs_stat. |
| [out] | buffer | Caller-owned read scratch. |
| [in] | buffer_bytes | Nonzero extent of buffer. |
| [out] | out | Digest written only after exact EOF validation. |
file is a valid open regular file and out is non-NULL. file_size is at most k_mdl_hash_max_file_bytes. file through EOF and writes out. out untouched. Definition at line 63 of file mdl_hash.c.
References fw_fs_read(), k_hash_max_read_calls, k_mdl_fnv_offset, k_mdl_hash_max_file_bytes, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_fail, k_ra8_ok, and mdl_hash_bytes_seed().
Referenced by internal_validate_stage(), mdl_hash_file(), and priv_mdl_export_source_verify_close().