STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
firmware_hash.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * Firmware hash: the identity of the image this board is running.
5 *
6 * esp_partition_get_sha256() returns the SHA-256 that ESP-IDF *appended* to the
7 * app image at build time, after recomputing it once to check the image is
8 * intact. It is therefore NOT sha256(firmware.bin): hashing the whole file
9 * folds the appended digest into its own input and can never match this value.
10 * To compare a board against a build, use firmware/tools/firmware_digest.py,
11 * and to turn a digest into a commit use firmware/tools/which_firmware.py. See
12 * FIRMWARE_HASH_VERIFICATION.md.
13 *
14 * Any change to the image changes this, with or without a new commit. The build
15 * is kept free of absolute paths so the same source hashes the same on every
16 * machine -- see firmware/tools/reproducible_build.py.
17 *
18 * SHA-256 is 32 bytes. The full 64-character hex string is printed for
19 * unambiguous identification. Note print() emits uppercase hex; the registry
20 * stores lowercase, and which_firmware.py accepts either.
21 */
22
23#include <Arduino.h>
24#include <esp_ota_ops.h>
25#include <esp_partition.h>
26
27namespace FirmwareHash {
28
29// Computes SHA-256 of the running app partition once, then returns the cached
30// result on every subsequent call. Safe to call from setup() and from loop().
31inline const uint8_t* get() {
32 static uint8_t cached[32] = {};
33 static bool computed = false;
34 if (!computed) {
36 if (part && esp_partition_get_sha256(part, cached) == ESP_OK) {
37 computed = true;
38 }
39 }
40 return cached;
41}
42
43inline void print() {
44 const uint8_t* sha256 = get();
45 Serial.print("Firmware hash: ");
46 for (int i = 0; i < 32; i++) {
47 if (sha256[i] < 0x10)
48 Serial.print("0");
49 Serial.print(sha256[i], HEX);
50 }
51 Serial.println();
52 Serial.flush();
53}
54
55} // namespace FirmwareHash
static SerialStub Serial
Definition Arduino.h:128
#define HEX
Definition Arduino.h:24
const esp_partition_t * esp_ota_get_running_partition()
int esp_partition_get_sha256(const esp_partition_t *, uint8_t *out)
#define ESP_OK
Firmware hash: the identity of the image this board is running.
const uint8_t * get()