ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mkfontimg.c
Go to the documentation of this file.
1
19
20#include <stdint.h>
21#include <string.h>
22
23#include "mkfontimg_internal.h"
24#include "ra8_attributes.h"
25#include "ra8_fs.h"
26
52
66RA8_INTERNAL static void internal_put16(uint8_t* sector, uint32_t offset, uint16_t value)
67{
68 sector[offset] = (uint8_t)(value & (uint16_t)k_byte_lo_mask);
69 sector[offset + 1U] = (uint8_t)((value >> (uint16_t)k_byte_shift) & (uint16_t)k_byte_lo_mask);
70}
71
84{
85 internal_put16(sector, (uint32_t)k_bpb_off_bytspersec, (uint16_t)k_block_size);
86 sector[k_bpb_off_secperclus] = (uint8_t)k_bpb_secperclus;
87 internal_put16(sector, (uint32_t)k_bpb_off_rsvdseccnt, (uint16_t)k_bpb_rsvdseccnt);
88 sector[k_bpb_off_numfats] = (uint8_t)k_bpb_numfats;
89 internal_put16(sector, (uint32_t)k_bpb_off_rootentcnt, (uint16_t)k_bpb_rootentcnt);
90 internal_put16(sector, (uint32_t)k_bpb_off_totsec16, (uint16_t)k_blocks_fat16);
91 internal_put16(sector, (uint32_t)k_bpb_off_secfat, (uint16_t)k_bpb_fatsz16);
92 sector[k_bpb_sig_off_a] = (uint8_t)k_bpb_sig_a;
93 sector[k_bpb_sig_off_b] = (uint8_t)k_bpb_sig_b;
94}
95
112RA8_INTERNAL static bool
113internal_mount_image(const char* image_out, mkfontimg_host_t* host, ra8_fs_mount_t** mount)
114{
115 if (!priv_mkfontimg_host_begin(image_out, k_blocks_fat16, k_block_size, host)) {
116 return false;
117 }
118 uint8_t sector[k_block_size] = {};
119 internal_build_fat16(sector);
120 if (!priv_mkfontimg_host_seed(host, sector, sizeof(sector)) ||
121 ra8_fs_mount(&host->backend, mount) != k_ra8_ok) {
123 return false;
124 }
125 return true;
126}
127
144{
145 if (ra8_fs_unmount(mount) != k_ra8_ok || host->disk.io_failed) {
147 return false;
148 }
149 return priv_mkfontimg_host_commit(host);
150}
151
166RA8_INTERNAL static int internal_run_blank(const char* image_out)
167{
168 mkfontimg_host_t host;
169 ra8_fs_mount_t* mount = nullptr;
170 if (!internal_mount_image(image_out, &host, &mount) || !internal_finish_image(&host, mount)) {
171 priv_mkfontimg_diag("mkfontimg: blank image generation failed; destination preserved\n");
172 return 1;
173 }
174 priv_mkfontimg_diag("mkfontimg: wrote ");
175 priv_mkfontimg_diag(image_out);
176 priv_mkfontimg_diag(" (blank FAT16, no font)\n");
177 return 0;
178}
179
196RA8_INTERNAL static int
197internal_run_font(const char* font_in, const char* image_out, const char* card_name)
198{
199 mkfontimg_host_t host;
200 ra8_fs_mount_t* mount = nullptr;
201 if (!internal_mount_image(image_out, &host, &mount)) {
202 priv_mkfontimg_diag("mkfontimg: cannot create or mount safe output\n");
203 return 1;
204 }
205 uint64_t font_bytes = 0U;
206 bool copied = priv_mkfontimg_host_copy(&host,
207 mount,
208 font_in,
209 card_name,
212 &font_bytes);
213 if (!copied) {
214 (void)ra8_fs_unmount(mount);
216 priv_mkfontimg_diag("mkfontimg: font changed, is invalid, or could not be copied exactly: ");
217 priv_mkfontimg_diag(font_in);
219 return 1;
220 }
221 if (!internal_finish_image(&host, mount)) {
222 priv_mkfontimg_diag("mkfontimg: publication failed; destination preserved\n");
223 return 1;
224 }
225 priv_mkfontimg_diag("mkfontimg: wrote ");
226 priv_mkfontimg_diag(image_out);
228 priv_mkfontimg_diag(card_name);
229 priv_mkfontimg_diag(" = ");
230 priv_mkfontimg_diag_u64(font_bytes);
231 priv_mkfontimg_diag(" bytes)\n");
232 return 0;
233}
234
250int main(int argc, char** argv)
251{
252 if ((argc >= 2) && (strcmp(argv[1], "--blank") == 0)) {
253 if (argc != 3) {
254 priv_mkfontimg_diag("usage: mkfontimg --blank <image-out>\n");
255 return 2;
256 }
257 return internal_run_blank(argv[2]);
258 }
259 if (argc < 3 || argc > 4) {
260 priv_mkfontimg_diag("usage: mkfontimg <font-in> <image-out> [dest-name]\n");
261 return 2;
262 }
263 const char* card_name = (argc == 4) ? argv[3] : "FONT.OTF";
264 return internal_run_font(argv[1], argv[2], card_name);
265}
@ k_block_size
Bytes per image sector.
mkfontimg_constant_t
FAT16 geometry, BPB fields, and accepted font limits.
Definition mkfontimg.c:28
@ k_bpb_numfats
FAT copy count.
Definition mkfontimg.c:44
@ k_byte_lo_mask
Low-byte mask.
Definition mkfontimg.c:31
@ k_bpb_fatsz16
Sectors per FAT.
Definition mkfontimg.c:46
@ k_bpb_sig_b
Signature high byte.
Definition mkfontimg.c:50
@ k_bpb_sig_off_b
Signature high offset.
Definition mkfontimg.c:48
@ k_bpb_off_bytspersec
BPB bytes/sector offset.
Definition mkfontimg.c:35
@ k_bpb_off_secfat
BPB FAT-size offset.
Definition mkfontimg.c:41
@ k_bpb_rootentcnt
Fixed root entries.
Definition mkfontimg.c:45
@ k_bpb_sig_a
Signature low byte.
Definition mkfontimg.c:49
@ k_bpb_sig_off_a
Signature low offset.
Definition mkfontimg.c:47
@ k_bpb_secperclus
Sectors per cluster.
Definition mkfontimg.c:42
@ k_font_min_bytes
Minimum accepted font bytes.
Definition mkfontimg.c:33
@ k_bpb_off_secperclus
BPB sectors/cluster offset.
Definition mkfontimg.c:36
@ k_bpb_rsvdseccnt
Reserved sectors.
Definition mkfontimg.c:43
@ k_bpb_off_rootentcnt
BPB root-entry offset.
Definition mkfontimg.c:39
@ k_bpb_off_numfats
BPB FAT-count offset.
Definition mkfontimg.c:38
@ k_bpb_off_rsvdseccnt
BPB reserved-sector offset.
Definition mkfontimg.c:37
@ k_font_cap
Maximum accepted font bytes.
Definition mkfontimg.c:34
@ k_blocks_fat16
Sectors in the image.
Definition mkfontimg.c:30
int main(int argc, char **argv)
Parse CLI mode and delegate to blank or font image generation.
Definition mkfontimg.c:250
static int internal_run_font(const char *font_in, const char *image_out, const char *card_name)
Generate a FAT16 image containing one verified streamed font.
Definition mkfontimg.c:197
static int internal_run_blank(const char *image_out)
Generate and publish a formatted empty FAT16 image.
Definition mkfontimg.c:166
static void internal_put16(uint8_t *sector, uint32_t offset, uint16_t value)
Store one little-endian 16-bit BPB field.
Definition mkfontimg.c:66
static void internal_build_fat16(uint8_t sector[k_block_size])
Build the legacy-compatible minimal FAT16 boot sector.
Definition mkfontimg.c:83
static bool internal_finish_image(mkfontimg_host_t *host, ra8_fs_mount_t *mount)
Finish filesystem state and publish the image atomically.
Definition mkfontimg.c:143
static bool internal_mount_image(const char *image_out, mkfontimg_host_t *host, ra8_fs_mount_t **mount)
Create, seed, and mount one unpublished FAT16 image.
Definition mkfontimg.c:113
Module-private bounded host-storage composition for mkfontimg.
void priv_mkfontimg_diag(const char *text)
Write one complete best-effort diagnostic fragment to standard error.
bool priv_mkfontimg_host_seed(mkfontimg_host_t *host, const uint8_t *bytes, uint32_t length)
Seed exact bytes at the beginning of the temporary image.
bool priv_mkfontimg_host_begin(const char *final_path, uint64_t block_count, uint32_t block_size, mkfontimg_host_t *host)
Create, size, and bind one private sibling-temporary image.
bool priv_mkfontimg_host_commit(mkfontimg_host_t *host)
Sync and atomically publish a complete temporary image.
void priv_mkfontimg_diag_u64(uint64_t value)
Write one unsigned decimal value to standard error without stdio.
void priv_mkfontimg_host_abort(mkfontimg_host_t *host)
Close and unlink an unpublished temporary image.
bool priv_mkfontimg_host_copy(const mkfontimg_host_t *host, ra8_fs_mount_t *mount, const char *input_path, const char *card_name, uint64_t minimum_bytes, uint64_t maximum_bytes, uint64_t *out_bytes)
Stream one stable host input into a card file and verify it by reread.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
int strcmp(const char *s1, const char *s2)
Compare two null-terminated strings.
Minimal FAT12/FAT16/FAT32 filesystem adapter (read + write).
ra8_err_t ra8_fs_mount(const ra8_fs_backend_t *backend, ra8_fs_mount_t **out_handle)
Mount a FAT volume from a block-device backend, auto-selecting the first partition.
ra8_err_t ra8_fs_unmount(ra8_fs_mount_t *handle)
Unmount a previously mounted volume and release its slot.
bool io_failed
Sticky positioned-I/O failure.
Caller-owned sibling-temporary and block-backend binding.
ra8_fs_backend_t backend
Portable FS facade.
mkfontimg_disk_t disk
Bound block state.
Cached parse of one mounted FAT volume.
@ k_byte_shift
Bits per byte for LE packing.
@ k_bpb_off_totsec16
Total sectors (16-bit).