Kleidos API Search

In development. It changes without notice.

Topic

Update Policy

What a boot decides after an update: to keep the new firmware, to refuse it, or to say that the board went back to the previous one.

Action When
NONE The firmware that runs was accepted before.
MarkValid The first boot of a new one. It is accepted once the hardware has come up.
RejectImage The first boot of one that cannot read this vault's partition. It is sent back.
RollbackDetected An update was tried and the previous firmware runs: the owner is told.

A new firmware that never marks itself valid is undone by the bootloader at the next start. An update touches neither the vault nor the bootloader, and one older than the firmware that runs is refused. The upload is in Wi-Fi Admin Portal and the writer in Storage.

See also
Wi-Fi Admin Portal, Storage

Files

file src/ota/ota_state_logic.h
Side-effect-free OTA boot-time decision logic (native-testable).

Classes

class kleidos::ota::ImageConfirmation
When a fresh OTA image is confirmed: once its first loop tick has run, never in setup. More about kleidos::ota::ImageConfirmation

Enumerations

enum class kleidos::ota::ImgState : uint8_t { kleidos::ota::ImgState::NEW, kleidos::ota::ImgState::PendingVerify, kleidos::ota::ImgState::VALID, kleidos::ota::ImgState::INVALID, kleidos::ota::ImgState::ABORTED, kleidos::ota::ImgState::UNDEFINED }
Mirror of the platform OTA image states for the subset of values we care about. Kept stand-alone so the native test build does not pull in ESP-IDF OTA headers. More about kleidos::ota::ImgState
enum class kleidos::ota::BootAction : uint8_t { kleidos::ota::BootAction::NONE , kleidos::ota::BootAction::MarkValid , kleidos::ota::BootAction::RollbackDetected , kleidos::ota::BootAction::RejectImage }
Requested action following OTA boot-time checks. More about kleidos::ota::BootAction

Functions

BootActionkleidos::ota::decideOnBoot (ImgState imgState, bool attemptedFlag, const char *previousVersion, const char *currentVersion)
Decide what to do on boot.
constexpr BootActionkleidos::ota::confirmPendingImage (BootAction action, bool vaultMountAdmitted)
Confirm a fresh OTA only when its read path does not refuse the vault.
constexpr boolkleidos::ota::markersClearedAtVerdict (BootAction action)
Whether the boot's verdict clears the OTA markers at once.
boolkleidos::ota::versionIsAcceptable (const char *running, const char *candidate)
Accept or reject a candidate firmware version before staging an OTA flash.
boolkleidos::ota::projectNameIsAcceptable (const char *running, const char *candidate)
Accept or reject a candidate image built for a different board variant.

Variables

constexpr size_tkleidos::ota::kProjectNameFieldBytes

Enumeration Type Documentation

BootAction #

enum class kleidos::ota::BootAction : uint8_t

Requested action following OTA boot-time checks.

Enumerator
NONE 

No action; the running firmware is long-validated.

MarkValid 

First boot of a fresh OTA; show validated toast + mark valid.

RollbackDetected 

Bootloader reverted; show blocking rollback alert.

RejectImage 

Fresh OTA whose read path refuses the vault; roll it back.

ImgState #

enum class kleidos::ota::ImgState : uint8_t

Mirror of the platform OTA image states for the subset of values we care about. Kept stand-alone so the native test build does not pull in ESP-IDF OTA headers.

Enumerator
NEW 

New slot never used before; cold boot path.

PendingVerify 

Recently applied OTA; needs validation.

VALID 

Slot confirmed valid (mark_app_valid_cancel_rollback called).

INVALID 

Slot failed validation; bootloader will revert.

ABORTED 

Update aborted; slot invalid.

UNDEFINED 

State could not be read from the chip.

Function Documentation

confirmPendingImage() #

BootAction kleidos::ota::confirmPendingImage(BootAction action, bool vaultMountAdmitted)
nodiscardconstexpr

Confirm a fresh OTA only when its read path does not refuse the vault.

The verdict is taken before the vault is first mounted, and an image once marked valid is never rolled back. An image whose read path refuses the vault partition (a whole-vault memory map the data window cannot hold, or a mapped read over an unflagged row under Flash Encryption, Storage) would then boot for good to an unlock screen with no vault behind it, and on the hardened build, with ROM download mode off, no cable could bring the vault back. Such an image is rejected while the bootloader can still return to the previous one.

It rejects a read-path refusal only. vaultMountAdmitted comes from no mount, so a fresh image whose LittleFS mount would fail for another reason is still confirmed.

Parameters
NameMeaning
actionWhat decideOnBoot decided.
vaultMountAdmittedThe running image's read path would not refuse the vault partition (littlefs::mountAdmitted()); only consulted for MarkValid.
Return values
ValueMeaning
BootAction::RejectImageaction was MarkValid and the read path refuses the vault.
actionOtherwise, unchanged.
See also
decideOnBoot

decideOnBoot() #

BootAction kleidos::ota::decideOnBoot(ImgState imgState, bool attemptedFlag, const char * previousVersion, const char * currentVersion)

Decide what to do on boot.

Decision rules (in order)

  • If imgState == PendingVerify → MarkValid (the bootloader selected a new partition and is waiting for the running app to confirm it via esp_ota_mark_app_valid_cancel_rollback()).
  • Else if attemptedFlag is set AND currentVersion matches previousVersion → RollbackDetected (the bootloader reverted because the new partition failed to self-validate in time).
  • Else → NONE.
Parameters
NameMeaning
imgStatePartition state reported by the bootloader.
attemptedFlagNVS flag — true iff an OTA upload completed in the previous session and we expected the next boot to come from the new partition.
previousVersionVersion string of the firmware that was running at the moment the OTA was applied (stored in NVS right before a software restart). Empty/null if unknown — in that case rollback cannot be detected and the function returns NONE.
currentVersionFirmware version string baked into the running binary (compile-time).
Return values
ValueMeaning
BootAction::NONELong-validated firmware; no UI action needed.
BootAction::MarkValidFirst boot of a fresh OTA; show validated toast and call esp_ota_mark_app_valid_cancel_rollback().
BootAction::RollbackDetectedBootloader reverted; show blocking rollback alert.
See also
ImgState, BootAction

markersClearedAtVerdict() #

bool kleidos::ota::markersClearedAtVerdict(BootAction action)
nodiscardconstexpr

Whether the boot's verdict clears the OTA markers at once.

The markers are how the image that runs after a rollback knows one happened: decideOnBoot reads RollbackDetected from them. Two verdicts keep them: MarkValid, until the image is marked valid (one that faults or fails its mark first is rolled back with the markers there, and the previous image shows the rollback alert), and RejectImage, whose image goes back to the previous one, rebooting at once or, if the rollback is refused, on its next reset, and the previous image says so. RollbackDetected and NONE are final and clear them.

Parameters
NameMeaning
actionWhat decideOnBoot and confirmPendingImage decided.
Return values
ValueMeaning
trueClear the markers now.
falseLeave them: for the mark (ImageConfirmation::clearMarkersAfterMark), or for the image the bootloader returns to.

projectNameIsAcceptable() #

bool kleidos::ota::projectNameIsAcceptable(const char * running, const char * candidate)

Accept or reject a candidate image built for a different board variant.

The bootloader validates the chip ID, so a classic-ESP32 image can never boot on an ESP32-S3. It does nothing for boards that share a chip: an image built for one of them passes every check the bootloader makes on another, and then drives the wrong hardware. esp_app_desc_t::project_name is the only field in the image header that names the variant, and each board's build stamps its own, so an exact match is the guard.

Both operands are treated as fixed-width, possibly unterminated buffers of kProjectNameFieldBytes bytes, because a candidate descriptor comes from an uploaded image and must never be walked past its field. A buffer with no terminator inside the field is malformed and rejected, exactly like an unreadable one.

Parameters
NameMeaning
runningProject name of the currently running image.
candidateProject name embedded in the OTA image being staged.
Return values
ValueMeaning
trueBoth names are well-formed and identical.
falseNames differ, or either buffer is null, empty, or unterminated.

versionIsAcceptable() #

bool kleidos::ota::versionIsAcceptable(const char * running, const char * candidate)

Accept or reject a candidate firmware version before staging an OTA flash.

Compares candidate against running using semantic-version ordering (major.minor.patch, numeric per field):

  • Returns true when candidate >= running (equal or newer: acceptable).
  • Returns false when candidate < running (downgrade: rejected).
  • Returns false (fail-closed) on null, empty, or malformed input.

This is a defense-in-depth guard that runs in the application layer — the primary anti-rollback guarantee of the hardened build is the eFuse SECURE_VERSION enforced by the bootloader. This check adds a visible rejection with a logged reason before a wasted flash/boot cycle.

Parameters
NameMeaning
runningVersion string of the currently running firmware, read from its own image descriptor (e.g. "1.2.3").
candidateVersion string embedded in the OTA image being staged.
Return values
ValueMeaning
trueCandidate is equal to or newer than the running version.
falseCandidate is older, or either string is null/empty/malformed.

Variable Documentation

kProjectNameFieldBytes #

size_t kleidos::ota::kProjectNameFieldBytes
constexpr

Capacity of esp_app_desc_t::project_name, in bytes, terminator included.

Mirrored here rather than included: this header is compiled by the native test build, which has no ESP-IDF. The field is a fixed 32-byte array in the image header and a candidate image is untrusted input read back out of flash, so it may not be NUL-terminated at all — every comparison against it is bounded by this constant.