SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (MTB & µPython)
Loading...
Searching...
No Matches
fw_hash.h
Go to the documentation of this file.
1/*******************************************************************************
2 * File Name: fw_hash.h
3 *
4 * Description: Boot-time SHA-256 of the active firmware image. Used by the
5 * Bento Desktop Buddy firmware-update flow (SPEC §5.6) so the
6 * desktop can compare the running image against a reference and
7 * so the on-device Y/N physical-ack prompt (ISSUE-027) can
8 * display a stable hash prefix.
9 *
10 * Implementation strategy (ISSUE-022 mitigation path B):
11 *
12 * We do NOT pre-bake the hash into the hex at link time — that
13 * requires a circular reservation + post-link patch step and
14 * adds a fragile tool dependency to the build pipeline. Instead
15 * the firmware computes SHA-256 over a known flash region at
16 * boot and caches the hex string in RAM. The trade-off is a
17 * one-time ~80 ms CPU hit on boot in exchange for no build-
18 * pipeline complexity.
19 *
20 * Boundaries:
21 * Hash window covers the CM33_NS application code region only
22 * (start/end derived from symbols exported by the linker
23 * script). Bootloader + HSM regions are excluded because they
24 * sit on a different flash bank and are not part of the image
25 * the desktop can patch via OpenOCD.
26 *
27 * Call fw_hash_compute_at_boot() ONCE early in main() before the
28 * Bento Buddy UI comes up. Safe to call multiple times — second
29 * call is a no-op.
30 *
31 * Reference: TESAIoT_PLAN/2026-4/Bento_Buddy/SPEC.md §7.6 +
32 * TESAIoT_PLAN/2026-4/Bento_Buddy/ISSUES.md ISSUE-022.
33 *
34 ******************************************************************************/
35
36#ifndef FW_HASH_H
37#define FW_HASH_H
38
39#include <stddef.h>
40#include <stdint.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/* SHA-256 hex string length (64 lowercase hex digits + trailing NUL). */
47#define FW_HASH_HEX_LEN (65)
48
49/* Compute SHA-256 over the CM33_NS code region and cache the result.
50 * Idempotent. Falls back to the sentinel "unknown" if mbedtls is unavailable
51 * or the linker symbols cannot be resolved at boot. */
53
54/* Return a pointer to the cached 64-char lowercase hex digest + trailing NUL.
55 * Always non-NULL — returns "unknown" before fw_hash_compute_at_boot(). */
56const char *fw_hash_hex(void);
57
58/* Return the first 8 hex chars of the cached digest as a NUL-terminated
59 * C string into `out8` (MUST be at least 9 bytes). Useful for the LCD
60 * physical-ack prompt (ISSUE-027) which only displays a short prefix. */
61void fw_hash_prefix8(char *out9);
62
63/* Diagnostic snapshot exposed via `bento.fw.query`'s `_diag` block.
64 * Populated each time fw_hash_compute_at_boot() runs. Lets the desktop
65 * verify the SHA-256 is deterministic across boots (production support
66 * for F2.5 — surfaced after a real-hardware session showed two
67 * different hashes for one binary across two power-cycles). */
68typedef struct {
69 uint32_t compute_count; /* total times compute_at_boot() executed */
70 uintptr_t vtor_addr; /* VTOR register value seen on last compute */
71 uint8_t window_head[8]; /* first 8 bytes of the hashed flash window */
73
75
76#ifdef __cplusplus
77}
78#endif
79
80#endif /* FW_HASH_H */
void fw_hash_get_diagnostics(fw_hash_diag_t *out)
เติม snapshot ข้อมูลวินิจฉัยของ hash ที่ผู้เรียกเป็นเจ้าของ สำหรับบล็อก _diag ของ bento....
void fw_hash_prefix8(char *out9)
เขียน hex 8 อักขระแรก + NUL ลงใน out9 (ต้องมีอย่างน้อย 9 ไบต์) ไม่มีผู้เรียกที่ใดเลย
void fw_hash_compute_at_boot(void)
คำนวณและแคช digest อัตลักษณ์แบบ SHA-256 ไว้ (ราว 80 ms หนึ่งครั้ง) เป็น idempotent
const char * fw_hash_hex(void)
digest 64 อักขระที่แคชไว้ ไม่เป็น NULL เสมอ — ได้ "unknown" ก่อนการคำนวณ ห้าม free ค่านี้
Definition fw_hash.h:68
uint32_t compute_count
Definition fw_hash.h:69
uintptr_t vtor_addr
Definition fw_hash.h:70
uint8_t window_head[8]
Definition fw_hash.h:71