FastLED 3.10.8
Loading...
Searching...
No Matches
boot_guard_record.h
Go to the documentation of this file.
1#pragma once
2
7
8#include "fl/stl/stdint.h"
9#include "fl/stl/compiler_control.h" // FL_ALWAYS_INLINE
10
11namespace fl {
12
21 fl::u32 magic;
22 fl::u32 image;
23 fl::u32 boots;
24 fl::u32 crashes;
25 fl::u32 check;
26 fl::u32 reserved[3]; // pad to one 32-byte cache line
27};
28
29constexpr fl::u32 kBootGuardMagic = 0xFA57B008u;
30
31FL_ALWAYS_INLINE fl::u32 bootGuardCheck(fl::u32 image, fl::u32 boots, fl::u32 crashes) {
32 return kBootGuardMagic ^ image ^ ~boots ^ ((crashes << 16) | (crashes >> 16)) ^ 0x5A5A5A5Au;
33}
34
36FL_ALWAYS_INLINE bool bootGuardValid(const volatile BootGuardRecord& r, fl::u32 image) {
37 return r.magic == kBootGuardMagic && r.image == image &&
38 r.check == bootGuardCheck(image, r.boots, r.crashes);
39}
40
42FL_ALWAYS_INLINE fl::u32 bootGuardDecode(const volatile BootGuardRecord& r, fl::u32 image) {
43 return bootGuardValid(r, image) ? r.boots : 0;
44}
45
48 fl::u32 image) {
49 return bootGuardValid(r, image) ? r.crashes : 0;
50}
51
53FL_ALWAYS_INLINE void bootGuardStore(volatile BootGuardRecord& r, fl::u32 image,
54 fl::u32 boots, fl::u32 crashes) {
56 r.image = image;
57 r.boots = boots;
58 r.crashes = crashes;
59 r.check = bootGuardCheck(image, boots, crashes);
60 r.reserved[0] = 0;
61 r.reserved[1] = 0;
62 r.reserved[2] = 0;
63}
64
67FL_ALWAYS_INLINE void bootGuardEncode(volatile BootGuardRecord& r, fl::u32 image,
68 fl::u32 boots) {
69 bootGuardStore(r, image, boots, bootGuardDecodeCrashes(r, image));
70}
71
74 fl::u32 crashes) {
75 bootGuardStore(r, image, bootGuardDecode(r, image), crashes);
76}
77
80FL_ALWAYS_INLINE fl::u32 bootGuardHashWords(const volatile fl::u32* words, fl::u32 count,
81 fl::u32 stride = 1) {
82 fl::u32 h = 0x811C9DC5u;
83 for (fl::u32 i = 0; i < count; i += stride) {
84 h = (h ^ words[i]) * 0x01000193u;
85 }
86 return h;
87}
88
91 fl::u32 boots;
92 bool escape;
93};
94
99 fl::u32 escape_boots) {
101 const fl::u32 boots = stored_boots + 1;
102 d.escape = escape_boots != 0 && boots >= escape_boots;
103 d.boots = d.escape ? 0 : boots;
104 return d;
105}
106
107} // namespace fl
#define FL_ALWAYS_INLINE
FL_ALWAYS_INLINE BootGuardDecision bootGuardOnBoot(fl::u32 stored_boots, fl::u32 escape_boots)
Boot-guard policy.
FL_ALWAYS_INLINE void bootGuardStore(volatile BootGuardRecord &r, fl::u32 image, fl::u32 boots, fl::u32 crashes)
Store both counts in r as a valid record of image.
FL_ALWAYS_INLINE bool bootGuardValid(const volatile BootGuardRecord &r, fl::u32 image)
True when r holds a valid record written by firmware image.
FL_ALWAYS_INLINE fl::u32 bootGuardDecodeCrashes(const volatile BootGuardRecord &r, fl::u32 image)
Crash count stored in r, or 0 if r is not a valid record of image.
FL_ALWAYS_INLINE void bootGuardEncode(volatile BootGuardRecord &r, fl::u32 image, fl::u32 boots)
Store boots, keeping the crash count (0 if the record is invalid).
FL_ALWAYS_INLINE fl::u32 bootGuardDecode(const volatile BootGuardRecord &r, fl::u32 image)
Boot count stored in r, or 0 if r is not a valid record of image.
constexpr fl::u32 kBootGuardMagic
FL_ALWAYS_INLINE fl::u32 bootGuardCheck(fl::u32 image, fl::u32 boots, fl::u32 crashes)
FL_ALWAYS_INLINE void bootGuardEncodeCrashes(volatile BootGuardRecord &r, fl::u32 image, fl::u32 crashes)
Store crashes, keeping the boot count (0 if the record is invalid).
FL_ALWAYS_INLINE fl::u32 bootGuardHashWords(const volatile fl::u32 *words, fl::u32 count, fl::u32 stride=1)
FNV-1a over every stride-th of count 32-bit words; platforms hash their firmware image with it to get...
Base definition for an LED controller.
Definition crgb.hpp:179
fl::u32 boots
count to store before continuing
fl::u32 image
key of the firmware image that wrote the record
fl::u32 boots
consecutive boots that never called markBootHealthy()
fl::u32 crashes
consecutive watchdog resets (Watchdog::consecutiveCrashCount())
bool escape
reboot into the bootloader now
fl::u32 check
checksum of the fields above, rejects power-on garbage
Reset-persistent boot-guard record.
What the early-boot hook does on this boot.