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
27
namespace
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().
31
inline
const
uint8_t*
get
() {
32
static
uint8_t cached[32] = {};
33
static
bool
computed =
false
;
34
if
(!computed) {
35
const
esp_partition_t
* part =
esp_ota_get_running_partition
();
36
if
(part &&
esp_partition_get_sha256
(part, cached) ==
ESP_OK
) {
37
computed =
true
;
38
}
39
}
40
return
cached;
41
}
42
43
inline
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
Arduino.h
Serial
static SerialStub Serial
Definition
Arduino.h:128
HEX
#define HEX
Definition
Arduino.h:24
esp_ota_ops.h
esp_partition.h
esp_ota_get_running_partition
const esp_partition_t * esp_ota_get_running_partition()
Definition
esp_partition.h:12
esp_partition_get_sha256
int esp_partition_get_sha256(const esp_partition_t *, uint8_t *out)
Definition
esp_partition.h:17
ESP_OK
#define ESP_OK
Definition
esp_partition.h:10
FirmwareHash
Firmware hash: the identity of the image this board is running.
Definition
firmware_hash.h:27
FirmwareHash::get
const uint8_t * get()
Definition
firmware_hash.h:31
FirmwareHash::print
void print()
Definition
firmware_hash.h:43
esp_partition_t
Definition
esp_partition.h:5
firmware
Hotfire_Code
common
firmware_hash.h
Generated by
1.15.0