ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_hash.c
Go to the documentation of this file.
1
9#include "mdl_hash.h"
10
11#include <stdint.h>
12#include <string.h>
13
14#include "ra8_attributes.h"
15
17typedef enum : uint64_t {
18 k_hash_max_read_calls = 2000001ULL,
20
21uint64_t mdl_hash_bytes_seed(const void* data, size_t len, uint64_t seed)
22{
23 if ((data == nullptr) || (len == 0U)) {
24 return seed;
25 }
26 const uint8_t* p = (const uint8_t*)data;
27 uint64_t h = seed;
28 for (size_t i = 0U; i < len; ++i) {
29 h ^= (uint64_t)p[i];
30 h *= (uint64_t)k_mdl_fnv_prime;
31 }
32 return h;
33}
34
35uint64_t mdl_hash_bytes(const void* data, size_t len)
36{
37 return mdl_hash_bytes_seed(data, len, (uint64_t)k_mdl_fnv_offset);
38}
39
40uint64_t mdl_hash_str(const char* s)
41{
42 if (s == nullptr) {
43 return (uint64_t)k_mdl_fnv_offset;
44 }
45 return mdl_hash_bytes(s, strlen(s));
46}
47
64 uint64_t file_size,
65 uint8_t* buffer,
66 uint32_t buffer_bytes,
67 uint64_t* out)
68{
69 if ((file == nullptr) || (buffer == nullptr) || (buffer_bytes == 0U) || (out == nullptr)) {
71 }
72 if (file_size > (uint64_t)k_mdl_hash_max_file_bytes) {
74 }
75 uint64_t remaining = file_size;
76 uint64_t hash = (uint64_t)k_mdl_fnv_offset;
77 for (uint64_t call = 0U; call < (uint64_t)k_hash_max_read_calls; ++call) {
78 uint32_t want = 1U;
79 if (remaining != 0U) {
80 want = (remaining < (uint64_t)buffer_bytes) ? (uint32_t)remaining : buffer_bytes;
81 }
82 uint32_t got = 0U;
83 const ra8_err_t read = fw_fs_read(file, buffer, want, &got);
84 if (read != k_ra8_ok) {
85 return read;
86 }
87 if (remaining == 0U) {
88 if (got != 0U) {
89 return k_ra8_fail;
90 }
91 *out = hash;
92 return k_ra8_ok;
93 }
94 if ((got == 0U) || ((uint64_t)got > remaining)) {
95 return k_ra8_fail;
96 }
97 hash = mdl_hash_bytes_seed(buffer, (size_t)got, hash);
98 remaining -= (uint64_t)got;
99 }
101}
102
103ra8_err_t mdl_hash_file(mdl_storage_t* storage, const char* path, uint64_t* out)
104{
105 if ((storage == nullptr) || (storage->fs == nullptr) || (path == nullptr) || (out == nullptr)) {
107 }
108 fw_fs_stat_t stat = {};
109 ra8_err_t err = fw_fs_stat(&storage->fs->names, path, &stat);
110 if (err != k_ra8_ok) {
111 return err;
112 }
113 if (!stat.exists) {
114 return k_ra8_err_not_found;
115 }
116 if (stat.type != k_fw_fs_node_file) {
118 }
119 if (stat.size_bytes > (uint64_t)k_mdl_hash_max_file_bytes) {
121 }
122 fw_fs_file_t file = {};
123 err = fw_fs_open(&storage->fs->streams,
124 path,
126 &file,
127 storage->file_workspace,
128 storage->file_workspace_bytes);
129 if (err != k_ra8_ok) {
130 return err;
131 }
132 uint64_t digest = 0U;
133 err =
134 mdl_hash_stream(&file, stat.size_bytes, storage->io_buffer, storage->io_buffer_bytes, &digest);
135 const ra8_err_t closed = fw_fs_close(&file);
136 if (err == k_ra8_ok) {
137 err = closed;
138 }
139 if (err == k_ra8_ok) {
140 *out = digest;
141 }
142 return err;
143}
ra8_err_t fw_fs_open(const fw_fs_stream_port_t *streams, const char *path, fw_fs_open_mode_t mode, fw_fs_file_t *file, void *workspace, uint32_t workspace_size)
Open a file into a caller-owned handle and backend workspace.
Definition fw_if_fs.c:648
ra8_err_t fw_fs_stat(const fw_fs_namespace_t *names, const char *path, fw_fs_stat_t *out)
Query a path; a miss is success with out->exists == false.
Definition fw_if_fs.c:480
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_close(fw_fs_file_t *file)
Close and consume an open handle.
Definition fw_if_fs.c:783
@ k_fw_fs_node_file
Regular byte stream.
@ k_fw_fs_open_read
Existing file, read-only.
mdl_hash_bound_t
Exact regular-file bound accepted by mdl_hash_file.
Definition mdl_hash.c:17
@ k_hash_max_read_calls
Short-read + EOF call ceiling.
Definition mdl_hash.c:18
uint64_t mdl_hash_bytes(const void *data, size_t len)
FNV-1a 64 hash of a byte range.
Definition mdl_hash.c:35
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.
Definition mdl_hash.c:103
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)
Fold an exact regular-file extent into a running FNV state.
Definition mdl_hash.c:63
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.
Definition mdl_hash.c:21
uint64_t mdl_hash_str(const char *s)
FNV-1a 64 hash of a NUL-terminated string (excluding the NUL).
Definition mdl_hash.c:40
Content-identity hashing (FNV-1a 64) for the media downloader's persistent library state.
@ k_mdl_fnv_prime
FNV-1a 64 prime.
Definition mdl_hash.h:36
@ k_mdl_hash_max_file_bytes
Exact file hash bound.
Definition mdl_hash.h:37
@ k_mdl_fnv_offset
FNV-1a 64 offset basis.
Definition mdl_hash.h:35
Annotation-attribute framework macros for ra8-firmware.
@ k_ra8_fail
Generic unspecified failure.
Definition ra8_err.h:133
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_not_found
Requested item not found (lookup / search missed).
Definition ra8_err.h:173
@ 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
size_t strlen(const char *s)
Calculate string length.
Caller-owned open file; fields are private to the facade.
Result of a portable metadata query.
uint64_t size_bytes
File length; zero for a directory.
bool exists
False means a clean lookup miss.
fw_fs_node_type_t type
Kind of node at the path.
fw_fs_stream_port_t streams
Byte-stream operations.
fw_fs_namespace_t names
Namespace operations.
One non-reentrant downloader filesystem dependency bundle.
Definition mdl_storage.h:40
uint32_t file_workspace_bytes
File workspace extent.
Definition mdl_storage.h:45
uint8_t * io_buffer
Caller-owned stream scratch.
Definition mdl_storage.h:44
const fw_fs_t * fs
Injected portable filesystem.
Definition mdl_storage.h:41
void * file_workspace
Open-file backend state.
Definition mdl_storage.h:42
uint32_t io_buffer_bytes
Stream scratch extent.
Definition mdl_storage.h:47