mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 10:54:13 +00:00
fix(ota): hash the running image, not the whole partition
`ota status` and OP_INFO_REPORT advertised a SHA-256 taken over the whole running partition, so it never matched the hash of the .bin an app holds — which defeats the delta base match the field exists for. The image length now comes from the ESP image header (segment walk), and the esptool-appended digest is included when it verifies, so the hash covers exactly the bytes an update wrote and equals sha256(firmware.bin). All zeros means the slot holds no readable image. Both consumers share one ensureRunningSha().
This commit is contained in:
1 parent
721eb33c58
commit
f9ea2bb11f
4 files changed
+132
-18
No files matched your search
@@ -67,7 +67,10 @@ should not need to explore beyond them before making a change.
|
||||
(`ota stage` + token), and the staged image boots only after a SHA-256
|
||||
gate; (3) `OP_INFO` is the capability handshake — answered in every state,
|
||||
and its report carries target id, slot size, max literal, feature bits, the
|
||||
running firmware version and the full running-slot SHA-256; (4) the wire
|
||||
running firmware version and the running image's SHA-256 (taken over exactly
|
||||
the bytes an update wrote — image length from the ESP image header, appended
|
||||
digest included — so it equals the hash of the app's own .bin and is the
|
||||
delta-base identity; all zeros means "cannot tell"); (4) the wire
|
||||
contract is append-only frozen — new features add new op codes/report
|
||||
bits, never change existing ones.
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ public:
|
||||
#endif // ESP32
|
||||
|
||||
// SHA-256 over `size` bytes of one slot, read in 1 KB slices. Shared by the
|
||||
// OP_END verification and `ota status` (the app's delta base match).
|
||||
// OP_END verification and hashImage() (the app's delta base match).
|
||||
static bool hashSlot(OtaSlotIo* io, bool staged, uint32_t size, uint8_t out[32]) {
|
||||
SHA256 sha;
|
||||
uint8_t buf[1024];
|
||||
@@ -98,6 +98,50 @@ static bool hashSlot(OtaSlotIo* io, bool staged, uint32_t size, uint8_t out[32])
|
||||
return true;
|
||||
}
|
||||
|
||||
// One slot read, staged or running.
|
||||
static bool slotRead(OtaSlotIo* io, bool staged, uint32_t off, uint8_t* out, size_t len) {
|
||||
return staged ? io->readStaged(off, out, len) : io->readSource(off, out, len);
|
||||
}
|
||||
|
||||
// Length of the app image stored at the start of a slot: the ESP image header
|
||||
// (24 bytes: magic, segment count, ..., chip id, hash_appended) followed by
|
||||
// each segment header + payload, a checksum byte and 16-byte padding. This is
|
||||
// exactly what an update wrote — the .bin length — not the erase-filled
|
||||
// partition. Returns 0 when the bytes are not an ESP app image.
|
||||
static uint32_t slotImageLength(OtaSlotIo* io, bool staged) {
|
||||
uint8_t h[24];
|
||||
if (!slotRead(io, staged, 0, h, sizeof(h))) return 0;
|
||||
if (h[0] != 0xE9 || h[1] < 1 || h[1] > 16) return 0; // magic, segment count
|
||||
uint32_t off = sizeof(h);
|
||||
for (uint8_t i = 0; i < h[1]; i++) {
|
||||
uint8_t seg[8];
|
||||
if (!slotRead(io, staged, off, seg, sizeof(seg))) return 0;
|
||||
uint32_t len = (uint32_t)seg[4] | ((uint32_t)seg[5] << 8) |
|
||||
((uint32_t)seg[6] << 16) | ((uint32_t)seg[7] << 24);
|
||||
off += 8 + len;
|
||||
off = (off + 3) & ~3u; // payloads are 4-byte aligned
|
||||
if (off >= io->sourceSize()) return 0; // garbage: refuse
|
||||
}
|
||||
off += 1; // trailing checksum byte
|
||||
return (off + 15) & ~15u; // images are padded to 16
|
||||
}
|
||||
|
||||
// SHA-256 of the app image in a slot, over exactly the bytes the update wrote.
|
||||
// esptool appends a SHA-256 of the image to every .bin by default; it is
|
||||
// included when it verifies against the computed hash, so this equals the hash
|
||||
// the app computes over its own image file (the delta base match). Returns
|
||||
// false when the slot does not hold a recognisable image.
|
||||
static bool hashImage(OtaSlotIo* io, bool staged, uint8_t out[32]) {
|
||||
const uint32_t len = slotImageLength(io, staged);
|
||||
if (len == 0) return false;
|
||||
if (!hashSlot(io, staged, len, out)) return false;
|
||||
uint8_t digest[32];
|
||||
if (slotRead(io, staged, len, digest, sizeof(digest)) && memcmp(digest, out, 32) == 0) {
|
||||
return hashSlot(io, staged, len + sizeof(digest), out);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Flip the boot partition to the inactive slot and reboot. Returns false when
|
||||
// the partition cannot be selected (the node keeps running the current
|
||||
// firmware); on-device reboot() does not return.
|
||||
@@ -254,11 +298,7 @@ size_t OtaUpdate::buildInfoReport(uint8_t* out) {
|
||||
memcpy(&p[OTA_INFO_RUNNING_VER_OFF], _version, n); // NUL-padded by memset
|
||||
}
|
||||
if (_io != nullptr) {
|
||||
if (!_running_sha_valid) {
|
||||
memset(_running_sha, 0, sizeof(_running_sha));
|
||||
hashSlot(_io, /*staged=*/false, _io->sourceSize(), _running_sha);
|
||||
_running_sha_valid = true;
|
||||
}
|
||||
ensureRunningSha();
|
||||
memcpy(&p[OTA_INFO_RUNNING_SHA_OFF], _running_sha, 32);
|
||||
}
|
||||
|
||||
@@ -573,17 +613,20 @@ void OtaUpdate::rollbackToPrevious(const char* why) {
|
||||
|
||||
// --------------------------------------------------------------------- CLI
|
||||
|
||||
void OtaUpdate::ensureRunningSha() {
|
||||
if (_running_sha_valid) return;
|
||||
_running_sha_valid = true;
|
||||
memset(_running_sha, 0, sizeof(_running_sha));
|
||||
if (_io != nullptr) hashImage(_io, /*staged=*/false, _running_sha); // zeros: unknown
|
||||
}
|
||||
|
||||
void OtaUpdate::formatStatus(char* reply, size_t reply_size) {
|
||||
if (_io == nullptr) {
|
||||
snprintf(reply, reply_size, "running=%s sha=0000000000000000 state=unsupported size=0 off=0 err=0",
|
||||
_version);
|
||||
return;
|
||||
}
|
||||
if (!_running_sha_valid) {
|
||||
memset(_running_sha, 0, sizeof(_running_sha));
|
||||
hashSlot(_io, /*staged=*/false, _io->sourceSize(), _running_sha);
|
||||
_running_sha_valid = true;
|
||||
}
|
||||
ensureRunningSha();
|
||||
const char* state_name = _state == State::Staging ? "staging"
|
||||
: _state == State::Ready ? "ready"
|
||||
: _state == State::Error ? "error" : "idle";
|
||||
|
||||
@@ -67,8 +67,11 @@
|
||||
// the wire version, the board's target id (before arming), the usable
|
||||
// slot size, the largest accepted OP_LITERAL, and the feature mask
|
||||
// (bit0 = OP_COPY delta, bit1 = apply/rollback), plus the RUNNING
|
||||
// image's firmware version (32 bytes, NUL-padded) and full SHA-256 —
|
||||
// which is what an app uses to pick a delta base by exact-image match.
|
||||
// image's firmware version (32 bytes, NUL-padded) and its SHA-256 —
|
||||
// taken over exactly the bytes the update wrote (image length from the
|
||||
// ESP image header, appended digest included), so it equals the hash of
|
||||
// the app's own .bin and is what the app uses to pick a delta base by
|
||||
// exact-image match. All zeros means "cannot tell".
|
||||
// New features are only ever added as new bits or new report fields
|
||||
// appended at the end.
|
||||
// - New ops get new op codes; existing ops never change meaning, field
|
||||
@@ -279,6 +282,9 @@ private:
|
||||
uint16_t _health_nonce = 0; // sha-free marker nonce
|
||||
|
||||
bool stagingArmed() const { return _state == State::Staging || _state == State::Ready; }
|
||||
// Compute _running_sha once per boot, on first use (the app's delta base
|
||||
// match). Zeros when the platform cannot read the running image back.
|
||||
void ensureRunningSha();
|
||||
// Build the OP_INFO_REPORT frame (magic..CRC) into `out`. Works in every
|
||||
// state — it is the negotiation handshake an app performs before arming.
|
||||
size_t buildInfoReport(uint8_t* out);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -560,13 +561,74 @@ TEST_F(OtaTest, InfoReportAnswersWhenIdle) {
|
||||
EXPECT_EQ(le32From(body + 5), 0x200000u); // FakeSlotIo slot size
|
||||
EXPECT_EQ(body[9] | (body[10] << 8), (uint16_t)OTA_MAX_LITERAL);
|
||||
EXPECT_EQ(le32From(body + 11), (uint32_t)(OTA_FEAT_DELTA | OTA_FEAT_APPLY));
|
||||
// running slot is all zeroes in the fake
|
||||
uint8_t expect_sha[32];
|
||||
mockSha256(std::vector<uint8_t>(slot.source.begin(), slot.source.end()), expect_sha);
|
||||
EXPECT_EQ(memcmp(body + OTA_INFO_RUNNING_SHA_OFF, expect_sha, 32), 0);
|
||||
// The fake slot holds no ESP image, so the running sha is reported as
|
||||
// unknown (all zeros) rather than as a hash of erase fill.
|
||||
for (size_t k = 0; k < 32; k++) EXPECT_EQ(body[OTA_INFO_RUNNING_SHA_OFF + k], 0u);
|
||||
EXPECT_STREQ((const char*)(body + OTA_INFO_RUNNING_VER_OFF), "test-fw-1.0.0");
|
||||
}
|
||||
|
||||
// A synthetic ESP app image: 24-byte header (magic, segment count, chip id,
|
||||
// hash_appended), one 400-byte segment, the checksum byte, padding to 16 and,
|
||||
// when `with_digest`, the appended SHA-256 that esptool writes by default.
|
||||
static std::vector<uint8_t> syntheticImage(bool with_digest, uint8_t fill = 0x5A) {
|
||||
const size_t seg_len = 400;
|
||||
std::vector<uint8_t> img(24 + 8 + seg_len, 0);
|
||||
img[0] = 0xE9;
|
||||
img[1] = 1;
|
||||
img[0x0c] = 9; // chip id: ESP32-S3
|
||||
img[0x17] = with_digest ? 1 : 0; // hash_appended
|
||||
img[24 + 4] = (uint8_t)(seg_len & 0xFF);
|
||||
img[24 + 5] = (uint8_t)((seg_len >> 8) & 0xFF);
|
||||
img[24 + 6] = (uint8_t)((seg_len >> 16) & 0xFF);
|
||||
for (size_t k = 0; k < seg_len; k++) img[24 + 8 + k] = (uint8_t)(fill + k);
|
||||
img.resize((img.size() + 1 + 15) & ~(size_t)15); // + checksum, padded to 16
|
||||
if (with_digest) {
|
||||
uint8_t digest[32];
|
||||
mockSha256(img, digest);
|
||||
img.insert(img.end(), digest, digest + 32);
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
TEST_F(OtaTest, InfoReportHashesTheRunningImageBytes) {
|
||||
auto image = syntheticImage(/*with_digest=*/true);
|
||||
std::copy(image.begin(), image.end(), slot.source.begin());
|
||||
auto report = feed(infoFrame());
|
||||
ASSERT_EQ(report.size(), OTA_INFO_REPORT_LEN);
|
||||
uint8_t expect[32];
|
||||
mockSha256(image, expect); // the whole .bin, digest included
|
||||
EXPECT_EQ(memcmp(report.data() + 3 + OTA_INFO_RUNNING_SHA_OFF, expect, 32), 0);
|
||||
}
|
||||
|
||||
TEST_F(OtaTest, InfoReportHashesImageWithoutAppendedDigest) {
|
||||
auto image = syntheticImage(/*with_digest=*/false);
|
||||
std::copy(image.begin(), image.end(), slot.source.begin());
|
||||
auto report = feed(infoFrame());
|
||||
ASSERT_EQ(report.size(), OTA_INFO_REPORT_LEN);
|
||||
uint8_t expect[32];
|
||||
mockSha256(image, expect); // ends at the padded checksum
|
||||
EXPECT_EQ(memcmp(report.data() + 3 + OTA_INFO_RUNNING_SHA_OFF, expect, 32), 0);
|
||||
}
|
||||
|
||||
TEST_F(OtaTest, StatusShaMatchesTheInfoReport) {
|
||||
auto image = syntheticImage(/*with_digest=*/true);
|
||||
std::copy(image.begin(), image.end(), slot.source.begin());
|
||||
auto report = feed(infoFrame());
|
||||
ASSERT_EQ(report.size(), OTA_INFO_REPORT_LEN);
|
||||
char expect_sha16[17];
|
||||
for (int k = 0; k < 8; k++)
|
||||
snprintf(expect_sha16 + k * 2, 3, "%02x", report[3 + OTA_INFO_RUNNING_SHA_OFF + k]);
|
||||
char reply[160];
|
||||
ota.formatStatus(reply, sizeof(reply));
|
||||
EXPECT_NE(strstr(reply, (std::string("sha=") + expect_sha16).c_str()), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(OtaTest, StatusShaIsZeroWhenTheSlotHoldsNoImage) {
|
||||
char reply[160];
|
||||
ota.formatStatus(reply, sizeof(reply));
|
||||
EXPECT_NE(strstr(reply, "sha=0000000000000000"), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(OtaTest, InfoReportAnswersDuringStaging) {
|
||||
auto image = testImage();
|
||||
arm(image);
|
||||
|
||||
Reference in new issue
Block a user