Harden signed SD and bootloader OTA

This commit is contained in:
mikecarper committed 2026-08-21 12:15:54 -07:00
1 parent 3d78d9c846
commit d9f5dfa8a2
29 files changed
+1846 -408

No files matched your search

+539 -5
View File
@@ -18,6 +18,7 @@
#include "helpers/ota/OtaBlInfo.h"
#include "helpers/ota/OtaFlashLayout_nrf52.h"
#include "helpers/ota/OtaSdBootToken.h"
#include "helpers/ota/OtaSdAuthRecord.h"
#include "mota_vectors.h" // auto-generated by tools/mota/gen_vectors.py
extern "C" {
@@ -50,6 +51,14 @@ TEST(OtaBootResult, AcceptsOnlyOtafixApplyDiagnostics) {
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xBD), 0x00);
}
TEST(OtaPolicy, TrustedAutoInstallIsStrictlyForwardOnly) {
EXPECT_FALSE(ota_trusted_auto_version_allows(0, 0x01170104u));
EXPECT_FALSE(ota_trusted_auto_version_allows(0x01170103u, 0));
EXPECT_FALSE(ota_trusted_auto_version_allows(0x01170103u, 0x01170102u));
EXPECT_FALSE(ota_trusted_auto_version_allows(0x01170103u, 0x01170103u));
EXPECT_TRUE(ota_trusted_auto_version_allows(0x01170103u, 0x01170104u));
}
TEST(OtaBootPackage, StorageProfilesAndSdGeometryAreExact) {
EXPECT_EQ(OTA_BL_PROFILE_SD_BOOT_UPDATE, 0x09u);
EXPECT_EQ(OTA_BL_PROFILE_INTERNAL_BOOT_UPDATE, 0x0Au);
@@ -89,6 +98,251 @@ TEST(OtaBootPackage, StorageProfilesAndSdGeometryAreExact) {
0x00FFu, 0u, 0u, live_size, safe_span));
}
TEST(OtaSdAuth, RetainedRecordBindsPurposeGeometryAndContainerDigest) {
uint8_t digest[32];
for (uint8_t i = 0; i < sizeof(digest); ++i) digest[i] = (uint8_t)(0x40u + i);
uint8_t record[MOTA_SD_AUTH_LEN];
const uint32_t total = 41330u;
const uint32_t sectors = (total + 511u) / 512u;
ASSERT_TRUE(mota_sd_auth_encode(record, MOTA_SD_AUTH_PURPOSE_BOOTLOADER,
MOTA_BOOT_FORMAT_VER, 2048u, sectors,
total, 8000000u, digest));
EXPECT_EQ(0, memcmp(record, "MOTASDA2", 8));
EXPECT_EQ(mota_sd_auth_rd16(record + 8), 2u);
EXPECT_EQ(mota_sd_auth_rd16(record + 10), 72u);
EXPECT_EQ(record[12], MOTA_SD_AUTH_PURPOSE_BOOTLOADER);
EXPECT_EQ(record[13], MOTA_BOOT_FORMAT_VER);
EXPECT_EQ(mota_sd_auth_rd32(record + 16), 2048u);
EXPECT_EQ(mota_sd_auth_rd32(record + 20), sectors);
EXPECT_EQ(mota_sd_auth_rd32(record + 24), total);
EXPECT_EQ(mota_sd_auth_rd32(record + 28), 8000000u);
EXPECT_EQ(0, memcmp(record + 32, digest, sizeof(digest)));
EXPECT_TRUE(mota_sd_auth_valid(record));
record[32] ^= 1u;
EXPECT_FALSE(mota_sd_auth_valid(record));
record[32] ^= 1u;
EXPECT_FALSE(mota_sd_auth_encode(record, MOTA_SD_AUTH_PURPOSE_APP,
MOTA_BOOT_FORMAT_VER, 2048u, sectors,
total, 8000000u, digest));
EXPECT_FALSE(mota_sd_auth_encode(record, MOTA_SD_AUTH_PURPOSE_APP,
MOTA_APP_FORMAT_VER, 2048u, sectors + 1u,
total, 8000000u, digest));
}
TEST(OtaBootPackage, ContinuityExtensionIsBackwardCompatibleAndStrictlyMonotonic) {
uint8_t envelope[OTA_BOOT_ENVELOPE_SIZE] = {0};
wr_u32le(envelope, OTA_BOOT_MANIFEST_MAGIC0);
wr_u32le(envelope + 4, OTA_BOOT_MANIFEST_MAGIC1);
envelope[8] = (uint8_t)OTA_BOOT_MANIFEST_VERSION;
envelope[10] = (uint8_t)OTA_BOOT_MANIFEST_SIZE;
wr_u32le(envelope + 12, OTA_BOOT_IMAGE_START);
wr_u32le(envelope + 16, OTA_BOOT_IMAGE_SIZE);
wr_u32le(envelope + 20, 0x239A0071u);
memcpy(envelope + 24, "TOWER_V2_OTA", 12);
wr_u32le(envelope + 44, OTA_BOOT_CONTINUITY_MAGIC0);
wr_u32le(envelope + 48, OTA_BOOT_CONTINUITY_MAGIC1);
envelope[52] = (uint8_t)OTA_BOOT_CONTINUITY_VERSION;
envelope[54] = (uint8_t)OTA_BOOT_CONTINUITY_SIZE;
const uint32_t candidate_version = 0x0117010Du;
wr_u32le(envelope + 56, candidate_version);
envelope[60] = (uint8_t)OTA_BOOT_CONTINUITY_FAMILY_S140;
envelope[62] = 0xB6;
wr_u32le(envelope + 64, MOTA_NRF52_APP_BASE_S140_V6);
envelope[68] = (uint8_t)OTA_BOOT_CONTINUITY_LAYOUT_ABI;
OtaBootloaderIdentity candidate;
ASSERT_TRUE(ota_bootloader_manifest_parse(envelope, 0x100u, candidate,
sizeof(envelope)));
ASSERT_TRUE(candidate.continuity_present);
EXPECT_EQ(candidate.boot_version, candidate_version);
EXPECT_EQ(candidate.softdevice_family, 140u);
EXPECT_EQ(candidate.softdevice_fwid, 0x00B6u);
EXPECT_EQ(candidate.app_base, MOTA_NRF52_APP_BASE_S140_V6);
OtaBootloaderIdentity legacy = candidate;
legacy.continuity_present = false;
legacy.crc_ok = true;
EXPECT_FALSE(ota_bootloader_sd_retained_auth_ready(
legacy, 140u, 0x00B6u, MOTA_NRF52_APP_BASE_S140_V6, 1u));
candidate.crc_ok = true;
EXPECT_TRUE(ota_bootloader_sd_retained_auth_ready(
candidate, 140u, 0x00B6u, MOTA_NRF52_APP_BASE_S140_V6, 1u));
EXPECT_EQ(ota_bootloader_continuity_gate(
legacy, candidate, candidate_version, 140u, 0x00B6u,
MOTA_NRF52_APP_BASE_S140_V6, 1u),
OTA_BOOT_CONTINUITY_OK); // one-time v1 bootstrap
EXPECT_EQ(ota_bootloader_continuity_gate(
legacy, candidate, candidate_version + 1u, 140u, 0x00B6u,
MOTA_NRF52_APP_BASE_S140_V6, 1u),
OTA_BOOT_CONTINUITY_OUTER_VERSION);
OtaBootloaderIdentity installed = candidate;
installed.boot_version = candidate_version - 1u;
EXPECT_EQ(ota_bootloader_continuity_gate(
installed, candidate, candidate_version, 140u, 0x00B6u,
MOTA_NRF52_APP_BASE_S140_V6, 1u),
OTA_BOOT_CONTINUITY_OK);
installed.boot_version = candidate_version;
EXPECT_EQ(ota_bootloader_continuity_gate(
installed, candidate, candidate_version, 140u, 0x00B6u,
MOTA_NRF52_APP_BASE_S140_V6, 1u),
OTA_BOOT_CONTINUITY_NOT_NEWER);
EXPECT_EQ(ota_bootloader_continuity_gate(
legacy, candidate, candidate_version, 140u, 0x00B7u,
MOTA_NRF52_APP_BASE_S140_V6, 1u),
OTA_BOOT_CONTINUITY_PLATFORM);
EXPECT_FALSE(ota_bootloader_version_valid(0x02040100u));
EXPECT_FALSE(ota_bootloader_version_valid(UINT32_MAX));
EXPECT_TRUE(ota_bootloader_version_valid(0x020401FFu));
candidate.boot_version = UINT32_MAX;
EXPECT_EQ(ota_bootloader_continuity_gate(
legacy, candidate, UINT32_MAX, 140u, 0x00B6u,
MOTA_NRF52_APP_BASE_S140_V6, 1u),
OTA_BOOT_CONTINUITY_OUTER_VERSION);
candidate.boot_version = candidate_version;
envelope[48] ^= 1u;
EXPECT_FALSE(ota_bootloader_manifest_parse(envelope, 0x100u, candidate,
sizeof(envelope)));
}
TEST(OtaBootPackage, CandidateContinuityEnvelopeHasOneCanonicalFinalOffset) {
auto make_image = [](uint32_t manifest_offset) {
std::vector<uint8_t> image(OTA_BOOT_IMAGE_SIZE, 0xFF);
wr_u32le(image.data(), OTA_NRF52840_RAM_END);
wr_u32le(image.data() + 4, OTA_BOOT_IMAGE_START + 0x101u);
uint8_t* caps = image.data() + 0x400u;
memset(caps, 0, 16u);
memcpy(caps, "MOTABLDR", 8u);
caps[8] = MOTA_BOOT_FORMAT_VER;
caps[10] = (uint8_t)OTA_BL_REQUIRED_APP_CODEC_MASK;
caps[12] = OTA_BL_PROFILE_SD_BOOT_UPDATE;
uint8_t* envelope = image.data() + manifest_offset;
memset(envelope, 0, OTA_BOOT_ENVELOPE_SIZE);
wr_u32le(envelope, OTA_BOOT_MANIFEST_MAGIC0);
wr_u32le(envelope + 4, OTA_BOOT_MANIFEST_MAGIC1);
envelope[8] = (uint8_t)OTA_BOOT_MANIFEST_VERSION;
envelope[10] = (uint8_t)OTA_BOOT_MANIFEST_SIZE;
wr_u32le(envelope + 12, OTA_BOOT_IMAGE_START);
wr_u32le(envelope + 16, OTA_BOOT_IMAGE_SIZE);
wr_u32le(envelope + 20, OTA_XIAO_BOARD_ID_BASE);
memcpy(envelope + 24, OTA_XIAO_BOOT_DEVICE_NAME,
OTA_BOOT_DEVICE_NAME_SIZE);
wr_u32le(envelope + 44, OTA_BOOT_CONTINUITY_MAGIC0);
wr_u32le(envelope + 48, OTA_BOOT_CONTINUITY_MAGIC1);
envelope[52] = (uint8_t)OTA_BOOT_CONTINUITY_VERSION;
envelope[54] = (uint8_t)OTA_BOOT_CONTINUITY_SIZE;
wr_u32le(envelope + 56, 0x0117010Du);
envelope[60] = (uint8_t)OTA_BOOT_CONTINUITY_FAMILY_S140;
envelope[62] = 0x23u;
envelope[63] = 0x01u;
wr_u32le(envelope + 64, MOTA_NRF52_APP_BASE_S140_V7);
envelope[68] = (uint8_t)OTA_BOOT_CONTINUITY_LAYOUT_ABI;
wr_u32le(envelope + 40,
ota_boot_image_crc32(image.data(), image.size(), manifest_offset + 40));
return image;
};
OtaBootloaderIdentity identity;
auto canonical = make_image(OTA_BOOT_CANDIDATE_MANIFEST_OFFSET);
ASSERT_TRUE(ota_bootloader_candidate_identity_from_image(
canonical.data(), canonical.size(), identity));
EXPECT_TRUE(ota_bootloader_candidate_identity_canonical(identity));
EXPECT_EQ(identity.manifest_offset, 0x9FB4u);
auto relocated = make_image(0x8000u);
ASSERT_TRUE(ota_bootloader_identity_from_image(
relocated.data(), relocated.size(), identity)); // installed/legacy scanner remains generic
EXPECT_FALSE(ota_bootloader_candidate_identity_canonical(identity));
EXPECT_FALSE(ota_bootloader_candidate_identity_from_image(
relocated.data(), relocated.size(), identity));
OtaStoreRam<OTA_BOOT_IMAGE_SIZE> store;
OtaBootloaderCapsMarker caps;
ASSERT_TRUE(store.begin((uint32_t)canonical.size()));
ASSERT_TRUE(store.write(0, canonical.data(), (uint32_t)canonical.size()));
EXPECT_TRUE(ota_bootloader_external_image_metadata(
store, 0, OTA_BL_PROFILE_SD_BOOT_UPDATE, identity, caps));
EXPECT_EQ(identity.manifest_offset, OTA_BOOT_CANDIDATE_MANIFEST_OFFSET);
// A second CRC-valid base identity still counts when its adjacent BLM2
// extension is only half present. These coupled values are the fixed point
// for this deterministic image. Counting continuity first would wrongly
// discard the decoy and disagree with a deployed legacy updater.
auto ambiguous = canonical;
const uint32_t corrupt_decoy = 0x200u;
memcpy(ambiguous.data() + corrupt_decoy,
ambiguous.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET,
OTA_BOOT_MANIFEST_SIZE);
wr_u32le(ambiguous.data() + corrupt_decoy + 44, OTA_BOOT_CONTINUITY_MAGIC0);
wr_u32le(ambiguous.data() + corrupt_decoy + 48, 0x21444142u); // "BAD!"
wr_u32le(ambiguous.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40,
0x614E59E1u);
wr_u32le(ambiguous.data() + corrupt_decoy + 40, 0x4A03F7A2u);
ASSERT_EQ(ota_boot_image_crc32(
ambiguous.data(), ambiguous.size(),
OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40), 0x614E59E1u);
ASSERT_EQ(ota_boot_image_crc32(
ambiguous.data(), ambiguous.size(), corrupt_decoy + 40),
0x4A03F7A2u);
EXPECT_FALSE(ota_bootloader_candidate_identity_from_image(
ambiguous.data(), ambiguous.size(), identity));
ASSERT_TRUE(store.begin((uint32_t)ambiguous.size()));
ASSERT_TRUE(store.write(0, ambiguous.data(), (uint32_t)ambiguous.size()));
EXPECT_FALSE(ota_bootloader_external_image_metadata(
store, 0, OTA_BL_PROFILE_SD_BOOT_UPDATE, identity, caps));
// The same malformed extension is harmless when its base CRC is invalid;
// only the canonical CRC-valid identity is selected and interpreted.
auto bad_crc_decoy = canonical;
memcpy(bad_crc_decoy.data() + corrupt_decoy,
bad_crc_decoy.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET,
OTA_BOOT_MANIFEST_SIZE);
wr_u32le(bad_crc_decoy.data() + corrupt_decoy + 44,
OTA_BOOT_CONTINUITY_MAGIC0);
wr_u32le(bad_crc_decoy.data() + corrupt_decoy + 48, 0x21444142u);
wr_u32le(bad_crc_decoy.data() + corrupt_decoy + 40, 0xA5A5A5A5u);
wr_u32le(bad_crc_decoy.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40, 0u);
wr_u32le(bad_crc_decoy.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40,
ota_boot_image_crc32(
bad_crc_decoy.data(), bad_crc_decoy.size(),
OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40));
ASSERT_NE(ota_boot_image_crc32(
bad_crc_decoy.data(), bad_crc_decoy.size(), corrupt_decoy + 40),
0xA5A5A5A5u);
EXPECT_TRUE(ota_bootloader_candidate_identity_from_image(
bad_crc_decoy.data(), bad_crc_decoy.size(), identity));
ASSERT_TRUE(store.begin((uint32_t)bad_crc_decoy.size()));
ASSERT_TRUE(store.write(0, bad_crc_decoy.data(),
(uint32_t)bad_crc_decoy.size()));
EXPECT_TRUE(ota_bootloader_external_image_metadata(
store, 0, OTA_BL_PROFILE_SD_BOOT_UPDATE, identity, caps));
// A sole CRC-valid identity that claims a corrupt extension is not a legacy
// image; installed diagnostics and candidate validation both fail closed.
auto sole_half = canonical;
wr_u32le(sole_half.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 48,
0x21444142u);
wr_u32le(sole_half.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40, 0u);
wr_u32le(sole_half.data() + OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40,
ota_boot_image_crc32(
sole_half.data(), sole_half.size(),
OTA_BOOT_CANDIDATE_MANIFEST_OFFSET + 40));
EXPECT_FALSE(ota_bootloader_identity_from_image(
sole_half.data(), sole_half.size(), identity));
ASSERT_TRUE(store.begin((uint32_t)sole_half.size()));
ASSERT_TRUE(store.write(0, sole_half.data(), (uint32_t)sole_half.size()));
EXPECT_FALSE(ota_bootloader_external_image_metadata(
store, 0, OTA_BL_PROFILE_SD_BOOT_UPDATE, identity, caps));
ASSERT_TRUE(store.begin((uint32_t)relocated.size()));
ASSERT_TRUE(store.write(0, relocated.data(), (uint32_t)relocated.size()));
EXPECT_FALSE(ota_bootloader_external_image_metadata(
store, 0, OTA_BL_PROFILE_SD_BOOT_UPDATE, identity, caps));
}
TEST(OtaBootPackage, SdApprovalTokenBindsTheAuthenticatedImageHash) {
uint8_t hash[32];
for (uint8_t i = 0; i < sizeof(hash); i++) hash[i] = i;
@@ -279,6 +533,14 @@ TEST(OtaBootPackage, EmbeddedIdentityVectorCapsAndExplicitConfirmationGate) {
EXPECT_TRUE(ota_bootloader_caps_marker_parse(marker, caps));
marker[15] = 1;
EXPECT_FALSE(ota_bootloader_caps_marker_parse(marker, caps));
marker[15] = 0;
marker[8] = 0; marker[9] = 1; // ABI 0x0100: valid little-endian u16
ASSERT_TRUE(ota_bootloader_caps_marker_parse(marker, caps));
EXPECT_EQ(caps.apply_abi, 0x0100u);
marker[8] = 0; marker[9] = 0;
EXPECT_FALSE(ota_bootloader_caps_marker_parse(marker, caps));
marker[8] = 0xFF; marker[9] = 0xFF;
EXPECT_FALSE(ota_bootloader_caps_marker_parse(marker, caps));
std::vector<uint8_t> internal_caps_image(64, 0xFF);
const uint8_t internal_marker[16] = {
@@ -342,8 +604,8 @@ TEST(OtaBootPackage, CapabilityScannerRejectsAnOtherwiseValidUnalignedMarker) {
const uint8_t qspi_profile =
OTA_BL_STORAGE_STAGE_CEILING | OTA_BL_STORAGE_QSPI |
OTA_BL_STORAGE_BOOT_UPDATE;
const uint8_t marker[16] = {'M','O','T','A','B','L','D','R', 3,0, 5,0,
qspi_profile, 0,0,0};
uint8_t marker[16] = {'M','O','T','A','B','L','D','R', 3,0, 5,0,
qspi_profile, 0,0,0};
uint8_t image[64];
memset(image, 0xFF, sizeof(image));
memcpy(image + 1, marker, sizeof(marker));
@@ -356,6 +618,17 @@ TEST(OtaBootPackage, CapabilityScannerRejectsAnOtherwiseValidUnalignedMarker) {
EXPECT_EQ(caps.apply_abi, 3u);
EXPECT_EQ(caps.storage_flags, qspi_profile);
marker[8] = 0; marker[9] = 1;
memset(image, 0xFF, sizeof(image));
memcpy(image + 4, marker, sizeof(marker));
const OtaBlCaps high_abi =
ota_bl_caps_scan_aligned(image, sizeof(image), true, qspi_profile);
ASSERT_TRUE(high_abi.present);
EXPECT_EQ(high_abi.apply_abi, 0x0100u);
marker[8] = 3; marker[9] = 0;
memset(image, 0xFF, sizeof(image));
memcpy(image + 4, marker, sizeof(marker));
// A malformed aligned magic decoy is ignored, but a second fully valid
// privileged marker makes the installed capability identity ambiguous.
memcpy(image + 24, marker, sizeof(marker));
@@ -365,12 +638,19 @@ TEST(OtaBootPackage, CapabilityScannerRejectsAnOtherwiseValidUnalignedMarker) {
image[24 + 13] = 1;
EXPECT_TRUE(ota_bl_caps_scan_aligned(image, sizeof(image), true, qspi_profile).present);
// A structurally valid marker for the other storage profile is not an
// installed-profile candidate and cannot shadow the exact marker.
// Any second structurally valid privileged marker is ambiguous, including
// another known storage profile or the bare BOOT_UPDATE capability.
memcpy(image + 24, marker, sizeof(marker));
image[24 + 12] = OTA_BL_STORAGE_STAGE_CEILING |
OTA_BL_STORAGE_BOOT_UPDATE;
EXPECT_TRUE(ota_bl_caps_scan_aligned(image, sizeof(image), true, qspi_profile).present);
EXPECT_FALSE(ota_bl_caps_scan_aligned(image, sizeof(image), true, qspi_profile).present);
image[24 + 12] = OTA_BL_STORAGE_BOOT_UPDATE;
EXPECT_FALSE(ota_bl_caps_scan_aligned(image, sizeof(image), true, qspi_profile).present);
memset(image, 0xFF, sizeof(image));
memcpy(image + 4, marker, sizeof(marker));
image[4 + 12] = OTA_BL_STORAGE_BOOT_UPDATE;
EXPECT_FALSE(ota_bl_caps_scan_aligned(image, sizeof(image), true, qspi_profile).present);
}
class FakeMotaSeederStream : public Stream {
@@ -1764,6 +2044,50 @@ TEST(OtaTransfer, RejectsUnsupportedManifestHashAlgorithm) {
EXPECT_EQ(client.fetchState(), OtaManager::FAILED);
}
TEST(OtaTransfer, RejectsEnvelopeMidThatDoesNotMatchParsedManifestRootBeforeStaging) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
std::array<uint8_t, MOTA_MFL> bytes;
memcpy(bytes.data(), manifest.manifest_start, bytes.size());
bytes[20] ^= 0x80; // parsed merkle_root; the peer keeps the requested MID in the wire envelope
OtaManager client;
OtaStoreRam<4096> store;
CapturedMessages sent;
client.begin(SIM_TARGET_ID, capture_send, &sent);
client.set_fetch_store(&store);
ASSERT_EQ(client.pull(manifest.merkle_root, manifest.target_id), OtaManager::PULL_STARTED);
deliver_manifest_fragment(client, manifest.merkle_root, 0, bytes.data(), OTA_MF_FRAG);
deliver_manifest_fragment(client, manifest.merkle_root, 1, bytes.data() + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(client.fetchState(), OtaManager::FAILED);
EXPECT_EQ(client.fetchError(), OtaManager::FETCH_ERROR_MANIFEST);
EXPECT_EQ(store.staged_size(), 0u);
}
TEST(OtaTransfer, RejectsParsedManifestTargetThatDoesNotMatchRequestedCatalogTargetBeforeStaging) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
std::array<uint8_t, MOTA_MFL> bytes;
memcpy(bytes.data(), manifest.manifest_start, bytes.size());
wr_u32le(bytes.data() + 3, manifest.target_id ^ 0x01000000u);
OtaManager client;
OtaStoreRam<4096> store;
CapturedMessages sent;
client.begin(SIM_TARGET_ID, capture_send, &sent);
client.set_fetch_store(&store);
ASSERT_EQ(client.pull(manifest.merkle_root, manifest.target_id), OtaManager::PULL_STARTED);
deliver_manifest_fragment(client, manifest.merkle_root, 0, bytes.data(), OTA_MF_FRAG);
deliver_manifest_fragment(client, manifest.merkle_root, 1, bytes.data() + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(client.fetchState(), OtaManager::FAILED);
EXPECT_EQ(client.fetchError(), OtaManager::FETCH_ERROR_MANIFEST);
EXPECT_EQ(store.staged_size(), 0u);
}
// Same end-to-end transfer, but with 1 KB logical blocks: each block is delivered as several
// self-describing DATA fragments (frag_off), reassembled by the client, then its merkle PROOF is
// requested + verified separately before the block is committed. Exercises the multi-fragment path.
@@ -2017,6 +2341,7 @@ TEST(OtaTransfer, ResumeAfterReboot) {
SendTo to_server2{&server};
client2.begin(SIM_TARGET_ID, sim_send, &to_server2);
client2.set_fetch_store(&store);
client2.set_autofetch(OtaManager::AUTOFETCH_ANY);
ASSERT_TRUE(client2.resumeStaged(nullptr)); // adopt whatever is staged
EXPECT_EQ(client2.fetchState(), OtaManager::VERIFYING_STAGED);
finish_staged_verification(client2);
@@ -2030,6 +2355,131 @@ TEST(OtaTransfer, ResumeAfterReboot) {
EXPECT_EQ(0, std::memcmp(store.data(), SIM_MOTA_1K, SIM_MOTA_1K_LEN)); // byte-identical to the original
}
TEST(OtaTransfer, BootResumeHonorsCurrentAutofetchTargetAndVersionPolicy) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
ASSERT_TRUE(manifest.is_signed());
auto stage = [](OtaStoreRam<4096>& store, const uint8_t* bytes,
uint32_t length) {
return store.begin(length) && store.write(0, bytes, length);
};
OtaStoreRam<4096> off_store;
ASSERT_TRUE(stage(off_store, SIM_MOTA, SIM_MOTA_LEN));
OtaManager off;
off.begin(manifest.target_id, nullptr, nullptr);
off.set_fetch_store(&off_store);
EXPECT_FALSE(off.resumeStaged(nullptr));
std::vector<uint8_t> unsigned_mota(SIM_MOTA, SIM_MOTA + SIM_MOTA_LEN);
unsigned_mota[8u + 1u] &= (uint8_t)~MFLAG_SIGNED;
memset(unsigned_mota.data() + 8u + 97u, 0, 32u + 64u);
MotaManifest unsigned_manifest;
ASSERT_TRUE(mota_parse(unsigned_mota.data(),
(uint32_t)unsigned_mota.size(), unsigned_manifest));
ASSERT_FALSE(unsigned_manifest.is_signed());
OtaStoreRam<4096> unsigned_store;
ASSERT_TRUE(stage(unsigned_store, unsigned_mota.data(),
(uint32_t)unsigned_mota.size()));
OtaManager signed_only;
signed_only.begin(manifest.target_id, nullptr, nullptr);
signed_only.set_fetch_store(&unsigned_store);
signed_only.set_autofetch(OtaManager::AUTOFETCH_SIGNED);
signed_only.set_auto_version_floor(manifest.fw_version - 1u, true);
EXPECT_FALSE(signed_only.resumeStaged(nullptr));
OtaStoreRam<4096> replay_store;
ASSERT_TRUE(stage(replay_store, SIM_MOTA, SIM_MOTA_LEN));
OtaManager replay;
replay.begin(manifest.target_id, nullptr, nullptr);
replay.set_fetch_store(&replay_store);
replay.set_autofetch(OtaManager::AUTOFETCH_ANY);
replay.set_auto_version_floor(manifest.fw_version, true);
EXPECT_FALSE(replay.resumeStaged(nullptr));
OtaStoreRam<4096> wrong_target_store;
ASSERT_TRUE(stage(wrong_target_store, SIM_MOTA, SIM_MOTA_LEN));
OtaManager wrong_target;
wrong_target.begin(manifest.target_id ^ 1u, nullptr, nullptr);
wrong_target.set_fetch_store(&wrong_target_store);
wrong_target.set_autofetch(OtaManager::AUTOFETCH_ANY);
wrong_target.set_auto_version_floor(manifest.fw_version - 1u, true);
EXPECT_FALSE(wrong_target.resumeStaged(nullptr));
OtaStoreRam<4096> newer_store;
ASSERT_TRUE(stage(newer_store, SIM_MOTA, SIM_MOTA_LEN));
OtaManager newer;
newer.begin(manifest.target_id, nullptr, nullptr);
newer.set_fetch_store(&newer_store);
newer.set_autofetch(OtaManager::AUTOFETCH_SIGNED);
newer.set_auto_version_floor(manifest.fw_version - 1u, true);
EXPECT_TRUE(newer.resumeStaged(nullptr));
EXPECT_EQ(newer.fetchState(), OtaManager::VERIFYING_STAGED);
}
TEST(OtaTransfer, ExplicitResumePreservesTargetZeroWildcard) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
auto stage = [](OtaStoreRam<4096>& store) {
return store.begin(SIM_MOTA_LEN) &&
store.write(0, SIM_MOTA, SIM_MOTA_LEN);
};
OtaStoreRam<4096> manual_store;
ASSERT_TRUE(stage(manual_store));
OtaManager manual;
manual.begin(manifest.target_id ^ 1u, nullptr, nullptr);
manual.set_fetch_store(&manual_store);
EXPECT_EQ(manual.pull(manifest.merkle_root, 0), OtaManager::PULL_RESUMED);
OtaStoreRam<4096> archive_store;
ASSERT_TRUE(stage(archive_store));
OtaManager archive;
archive.begin(manifest.target_id ^ 1u, nullptr, nullptr);
archive.set_fetch_store(&archive_store);
EXPECT_EQ(archive.pull_archive(manifest.merkle_root, 0),
OtaManager::PULL_RESUMED);
OtaStoreRam<4096> mismatch_store;
ASSERT_TRUE(stage(mismatch_store));
OtaManager mismatch;
mismatch.begin(manifest.target_id, nullptr, nullptr);
mismatch.set_fetch_store(&mismatch_store);
EXPECT_EQ(mismatch.pull(manifest.merkle_root, manifest.target_id ^ 1u),
OtaManager::PULL_STARTED);
EXPECT_EQ(mismatch.fetchState(), OtaManager::WANT_MANIFEST);
}
TEST(OtaTransfer, ExplicitDebugResumeUsesManualIntentNotAutofetchPolicy) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
OtaStoreRam<4096> store;
ASSERT_TRUE(store.begin(SIM_MOTA_LEN));
ASSERT_TRUE(store.write(0, SIM_MOTA, SIM_MOTA_LEN));
// A deliberate debug/operator resume remains possible with autofetch disabled, an equal running
// version, and a cross-target wildcard. Those same properties are rejected by resumeStaged(nullptr).
OtaManager explicit_resume;
explicit_resume.begin(manifest.target_id ^ 1u, nullptr, nullptr);
explicit_resume.set_fetch_store(&store);
explicit_resume.set_autofetch(OtaManager::AUTOFETCH_OFF);
explicit_resume.set_auto_version_floor(manifest.fw_version, true);
ASSERT_TRUE(explicit_resume.resumeStagedExplicit(manifest.merkle_root, 0));
EXPECT_EQ(explicit_resume.fetchState(), OtaManager::VERIFYING_STAGED);
OtaStoreRam<4096> wrong_mid_store;
ASSERT_TRUE(wrong_mid_store.begin(SIM_MOTA_LEN));
ASSERT_TRUE(wrong_mid_store.write(0, SIM_MOTA, SIM_MOTA_LEN));
OtaManager wrong_mid;
wrong_mid.begin(manifest.target_id, nullptr, nullptr);
wrong_mid.set_fetch_store(&wrong_mid_store);
uint8_t other_mid[4];
memcpy(other_mid, manifest.merkle_root, sizeof(other_mid));
other_mid[0] ^= 1u;
EXPECT_FALSE(wrong_mid.resumeStagedExplicit(other_mid, 0));
EXPECT_EQ(wrong_mid.fetchState(), OtaManager::IDLE);
}
TEST(OtaTransfer, ResumeRehashesPayloadBeforeTrustingPresentLeaf) {
g_q.clear();
OtaManager server, client;
@@ -2056,6 +2506,7 @@ TEST(OtaTransfer, ResumeRehashesPayloadBeforeTrustingPresentLeaf) {
OtaManager resumed;
resumed.begin(SIM_TARGET_ID, nullptr, nullptr);
resumed.set_fetch_store(&store);
resumed.set_autofetch(OtaManager::AUTOFETCH_ANY);
ASSERT_TRUE(resumed.resumeStaged(nullptr));
finish_staged_verification(resumed);
EXPECT_EQ(resumed.fetchState(), OtaManager::FETCHING);
@@ -2087,6 +2538,7 @@ TEST(OtaTransfer, ResumeReadFailureCanNeverBecomeComplete) {
OtaManager resumed;
resumed.begin(SIM_TARGET_ID, nullptr, nullptr);
resumed.set_fetch_store(&store);
resumed.set_autofetch(OtaManager::AUTOFETCH_ANY);
ASSERT_TRUE(resumed.resumeStaged(nullptr));
finish_staged_verification(resumed);
EXPECT_EQ(resumed.fetchState(), OtaManager::FAILED);
@@ -2256,6 +2708,88 @@ TEST(OtaTransfer, ManualPullReportsIncompatibleManifestInsteadOfGoingIdle) {
EXPECT_EQ(store.staged_size(), 0u); // compatibility is rejected before the store is begun
}
TEST(OtaTransfer, AutomaticAdmissionRejectsReplayAndRechecksLyingHaveVersion) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
OtaManager client;
OtaStoreRam<4096> store;
CapturedMessages sent;
client.begin(SIM_TARGET_ID, capture_send, &sent);
client.set_fetch_store(&store);
client.set_autofetch(OtaManager::AUTOFETCH_ANY);
client.set_auto_version_floor(manifest.fw_version, true);
uint8_t wire[64];
client.on_message(wire, make_have1(wire, sizeof(wire), manifest.merkle_root,
manifest.target_id, manifest.fw_version,
manifest.codec_id, manifest.flags));
EXPECT_EQ(client.fetchState(), OtaManager::IDLE); // equal-version replay rejected at catalog admission
client.on_message(wire, make_have1(wire, sizeof(wire), manifest.merkle_root,
manifest.target_id, manifest.fw_version + 1u,
manifest.codec_id, manifest.flags));
ASSERT_EQ(client.fetchState(), OtaManager::WANT_MANIFEST);
deliver_manifest_fragment(client, manifest.merkle_root, 0,
manifest.manifest_start, OTA_MF_FRAG);
deliver_manifest_fragment(client, manifest.merkle_root, 1,
manifest.manifest_start + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(client.fetchState(), OtaManager::FAILED); // parsed version defeats the lying HAVE row
EXPECT_EQ(client.fetchError(), OtaManager::FETCH_ERROR_VERSION);
EXPECT_EQ(store.staged_size(), 0u);
}
TEST(OtaTransfer, SignedOnlyAutofetchRechecksTheParsedManifestFlag) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
ASSERT_TRUE(manifest.is_signed());
std::array<uint8_t, MOTA_MFL> unsigned_bytes;
memcpy(unsigned_bytes.data(), manifest.manifest_start, unsigned_bytes.size());
unsigned_bytes[1] &= (uint8_t)~MFLAG_SIGNED;
OtaManager client;
OtaStoreRam<4096> store;
CapturedMessages sent;
client.begin(SIM_TARGET_ID, capture_send, &sent);
client.set_fetch_store(&store);
client.set_autofetch(OtaManager::AUTOFETCH_SIGNED);
client.set_auto_version_floor(manifest.fw_version - 1u, true);
uint8_t wire[64];
client.on_message(wire, make_have1(wire, sizeof(wire), manifest.merkle_root,
manifest.target_id, manifest.fw_version,
manifest.codec_id,
(uint8_t)(manifest.flags | MFLAG_SIGNED)));
ASSERT_EQ(client.fetchState(), OtaManager::WANT_MANIFEST);
deliver_manifest_fragment(client, manifest.merkle_root, 0,
unsigned_bytes.data(), OTA_MF_FRAG);
deliver_manifest_fragment(client, manifest.merkle_root, 1,
unsigned_bytes.data() + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(client.fetchState(), OtaManager::FAILED);
EXPECT_EQ(client.fetchError(), OtaManager::FETCH_ERROR_MANIFEST);
EXPECT_EQ(store.staged_size(), 0u);
}
TEST(OtaTransfer, ExplicitManualPullMayOverrideAutomaticVersionFloor) {
MotaManifest manifest;
ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest));
OtaManager client;
OtaStoreRam<4096> store;
CapturedMessages sent;
client.begin(SIM_TARGET_ID, capture_send, &sent);
client.set_fetch_store(&store);
client.set_auto_version_floor(manifest.fw_version, true);
ASSERT_EQ(client.pull(manifest.merkle_root, manifest.target_id), OtaManager::PULL_STARTED);
deliver_manifest_fragment(client, manifest.merkle_root, 0,
manifest.manifest_start, OTA_MF_FRAG);
deliver_manifest_fragment(client, manifest.merkle_root, 1,
manifest.manifest_start + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(client.fetchState(), OtaManager::FETCHING);
EXPECT_GT(store.staged_size(), 0u);
}
TEST(OtaTransfer, PullAdmissionReportsNoStoreAndBusyWithoutReplacingIntent) {
g_q.clear();
OtaManager client;
-20
View File
@@ -3,7 +3,6 @@
#include "helpers/ota/OtaFlashLayout_nrf52.h"
#include "helpers/ota/OtaStoreQspiNrf52.h"
#include "helpers/ota/OtaSdHandoff.h"
using namespace mesh::ota;
@@ -325,22 +324,3 @@ TEST(OtaFlashPlan, LeavesOutputUntouchedOnReject) {
EXPECT_FALSE(mota_nrf52_stage_plan(CAP_V6 + 1, APP_V6, APP_END_V6, LEGACY, start));
EXPECT_EQ(start, 0x1234ABCDu);
}
TEST(OtaSdHandoff, EncodesChecksummedRecordAndPreservesSectorTail) {
uint8_t sector[MOTA_SD_SECTOR_SIZE];
std::memset(sector, 0xA5, sizeof(sector));
mota_sd_encode_handoff(sector, 2048, 1234, 630000, 8000000);
EXPECT_EQ(0, std::memcmp(sector, MOTA_SD_HANDOFF_MAGIC, 8));
EXPECT_EQ(mota_sd_rd32(sector + 8), MOTA_SD_HANDOFF_VERSION);
EXPECT_EQ(mota_sd_rd32(sector + 12), 2048u);
EXPECT_EQ(mota_sd_rd32(sector + 16), 1234u);
EXPECT_EQ(mota_sd_rd32(sector + 20), 630000u);
EXPECT_EQ(mota_sd_rd32(sector + 24), ~630000u);
EXPECT_EQ(mota_sd_rd32(sector + 28), 8000000u);
EXPECT_EQ(mota_sd_rd32(sector + 32), mota_sd_crc32(sector, 32));
EXPECT_EQ(sector[MOTA_SD_HANDOFF_LEN], 0xA5); // bytes outside our record are untouched
sector[20] ^= 1u;
EXPECT_NE(mota_sd_rd32(sector + 32), mota_sd_crc32(sector, 32));
}