Add signed XIAO bootloader updates over LoRa

This commit is contained in:
mikecarper committed 2026-08-20 22:05:11 -07:00
1 parent eb45e9d4f8
commit b7c9e37d09
34 files changed
+1604 -78

No files matched your search

+1 -1
View File
@@ -70,7 +70,7 @@ does not reflect the GoogleTest count -- run the built binary directly
| `test_user_gpio` | `src/helpers/UserGpio.cpp`, `UserGpioReplyTracker.h` | board-approved pins, get/set/reset, timed nonblocking transitions, duplicate suppression, rollover, and completion-reply routing |
| `test_utf8_helpers` | `src/helpers/UTF8Helpers.h` | byte-limit truncation at complete code-point boundaries and rejection of malformed or truncated UTF-8 |
| `test_wifi_ota_seeder_policy` | `src/helpers/WiFiOtaSeederPolicy.h`, `WiFiOtaSeederStatus.h` | listener state versus network availability, serial/TCP folder ownership, detach detection, and bounded status formatting |
| `test_ota` | `src/helpers/ota/` | container and EndF integrity; protocol codecs; transfer, resume, and apply safety; adaptive 2-to-4 block-request window growth and stall contraction; active-transfer priority classification |
| `test_ota` | `src/helpers/ota/` | v2 application/v3 bootloader parser separation; XIAO identity, vector, capability, explicit-confirmation and no-autofetch gates; container and EndF integrity; protocol codecs; transfer, resume, and layered apply safety; adaptive 2-to-4 block-request window growth and stall contraction; active-transfer priority classification |
| `test_trace_retry` | `src/Mesh.cpp` retry and relay policy | opaque OTA relay behavior during TempRadio; background discovery priority; immediate primary transfer relay, receive-delay bypass, fast CAD retry, and no generic flood retry; trace and non-OTA flood retry timing |
| `test_utils` | `src/Utils.cpp` | `Utils::toHex` (upstream) |
+201
View File
@@ -15,6 +15,7 @@
#include "helpers/ota/OtaProtocol.h"
#include "helpers/ota/OtaManager.h"
#include "helpers/ota/OtaApply.h"
#include "helpers/ota/OtaBlInfo.h"
#include "mota_vectors.h" // auto-generated by tools/mota/gen_vectors.py
extern "C" {
@@ -29,6 +30,11 @@ TEST(OtaBootResult, AcceptsOnlyOtafixApplyDiagnostics) {
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xB0), 0xB0);
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xB8), 0xB8);
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xBC), 0xBC);
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xC0), 0xC0);
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xC8), 0xC8);
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xCF), 0xCF);
EXPECT_TRUE(ota_nrf52_boot_update_result(0xC8));
EXPECT_FALSE(ota_nrf52_boot_update_result(0xB8));
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0x00), 0x00);
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0x4C), 0x00); // shutdown reason
@@ -38,6 +44,154 @@ TEST(OtaBootResult, AcceptsOnlyOtafixApplyDiagnostics) {
EXPECT_EQ(ota_nrf52_boot_result_or_zero(0xBD), 0x00);
}
static std::vector<uint8_t> boot_manifest_bytes() {
std::vector<uint8_t> m(MOTA_MFL, 0);
m[0] = MOTA_BOOT_FORMAT_VER;
m[1] = MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER;
m[2] = HASH_ALGO_SHA256;
wr_u32le(m.data() + 3, OTA_XIAO_BOARD_ID_BASE);
wr_u32le(m.data() + 7, 1);
wr_u32le(m.data() + 11, OTA_BOOT_IMAGE_SIZE);
wr_u32le(m.data() + 15, OTA_BOOT_IMAGE_SIZE);
m[19] = 10;
m[20] = 0x11; m[21] = 0x22; m[22] = 0x33; m[23] = 0x44;
for (uint8_t i = 0; i < 32; i++) m[24 + i] = (uint8_t)(0x80 + i);
m[56] = CODEC_FULL;
uint8_t hw[32]; EXPECT_TRUE(ota_xiao_bootloader_hw_id(OTA_XIAO_BOARD_ID_BASE, hw));
memcpy(m.data() + 57, hw, sizeof(hw));
memset(m.data() + MOTA_OFF_APPROVAL, 0xFF, 4);
return m;
}
TEST(OtaBootPackage, ParserSeparatesV2ApplicationsFromStrictV3Bootloader) {
MotaManifest parsed;
auto m = boot_manifest_bytes();
ASSERT_TRUE(mota_parse_manifest(m.data(), m.size(), parsed));
EXPECT_TRUE(parsed.is_bootloader());
EXPECT_EQ(parsed.block_count, 40u);
m[0] = MOTA_APP_FORMAT_VER;
EXPECT_FALSE(mota_parse_manifest(m.data(), m.size(), parsed));
m = boot_manifest_bytes(); m[1] &= ~MFLAG_BOOTLOADER;
EXPECT_FALSE(mota_parse_manifest(m.data(), m.size(), parsed));
m = boot_manifest_bytes(); m[19] = 9;
EXPECT_FALSE(mota_parse_manifest(m.data(), m.size(), parsed));
m = boot_manifest_bytes(); memset(m.data() + 7, 0, 4);
EXPECT_FALSE(mota_parse_manifest(m.data(), m.size(), parsed));
m = boot_manifest_bytes(); m[MOTA_OFF_BASE_HASH] = 1;
EXPECT_FALSE(mota_parse_manifest(m.data(), m.size(), parsed));
}
TEST(OtaBootPackage, EmbeddedIdentityVectorCapsAndExplicitConfirmationGate) {
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);
ASSERT_TRUE(ota_bootloader_vector_sane(image.data()));
image[4] &= 0xFE;
EXPECT_FALSE(ota_bootloader_vector_sane(image.data()));
wr_u32le(image.data() + 4, OTA_BOOT_IMAGE_START + 0x101u);
const uint32_t moff = 0x100;
memset(image.data() + moff, 0, OTA_BOOT_MANIFEST_SIZE);
wr_u32le(image.data() + moff, OTA_BOOT_MANIFEST_MAGIC0);
wr_u32le(image.data() + moff + 4, OTA_BOOT_MANIFEST_MAGIC1);
image[moff + 8] = OTA_BOOT_MANIFEST_VERSION;
image[moff + 10] = OTA_BOOT_MANIFEST_SIZE;
wr_u32le(image.data() + moff + 12, OTA_BOOT_IMAGE_START);
wr_u32le(image.data() + moff + 16, OTA_BOOT_IMAGE_SIZE);
wr_u32le(image.data() + moff + 20, OTA_XIAO_BOARD_ID_BASE);
memcpy(image.data() + moff + 24, OTA_XIAO_BOOT_DEVICE_NAME,
OTA_BOOT_DEVICE_NAME_SIZE);
wr_u32le(image.data() + moff + 40,
ota_boot_image_crc32(image.data(), image.size(), moff + 40));
const std::vector<uint8_t> canonical_image = image;
OtaBootloaderIdentity identity;
ASSERT_TRUE(ota_bootloader_identity_from_image(image.data(), image.size(), identity));
EXPECT_TRUE(identity.crc_ok);
EXPECT_EQ(identity.board_id, OTA_XIAO_BOARD_ID_BASE);
// A structurally complete bad-CRC decoy before the real manifest must not
// shadow the later valid identity.
const uint32_t decoy = 0x20;
memcpy(image.data() + decoy, image.data() + moff, OTA_BOOT_MANIFEST_SIZE);
wr_u32le(image.data() + decoy + 40, 0xA5A5A5A5u);
wr_u32le(image.data() + moff + 40, 0);
wr_u32le(image.data() + moff + 40,
ota_boot_image_crc32(image.data(), image.size(), moff + 40));
ASSERT_NE(ota_boot_image_crc32(image.data(), image.size(), decoy + 40), 0xA5A5A5A5u);
ASSERT_TRUE(ota_bootloader_identity_from_image(image.data(), image.size(), identity));
EXPECT_EQ(identity.manifest_offset, moff);
// These coupled CRC values make both otherwise identical manifests valid
// in this deterministic fixture. Two privileged identities are ambiguous
// and must fail closed in the helper shared with the staged-QSPI scanner.
image = canonical_image;
const uint32_t second = 0x200;
memcpy(image.data() + second, image.data() + moff, OTA_BOOT_MANIFEST_SIZE);
wr_u32le(image.data() + moff + 40, 0x07033138u);
wr_u32le(image.data() + second + 40, 0x0998508Cu);
ASSERT_EQ(ota_boot_image_crc32(image.data(), image.size(), moff + 40), 0x07033138u);
ASSERT_EQ(ota_boot_image_crc32(image.data(), image.size(), second + 40), 0x0998508Cu);
EXPECT_FALSE(ota_bootloader_identity_from_image(image.data(), image.size(), identity));
image = canonical_image;
image[0x300] ^= 1;
EXPECT_FALSE(ota_bootloader_identity_from_image(image.data(), image.size(), identity));
uint8_t marker[16] = {'M','O','T','A','B','L','D','R', 3,0, 1,0,
(uint8_t)(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE), 0,0,0};
OtaBootloaderCapsMarker caps;
EXPECT_TRUE(ota_bootloader_caps_marker_parse(marker, caps));
marker[15] = 1;
EXPECT_FALSE(ota_bootloader_caps_marker_parse(marker, caps));
OtaBlCaps selected;
selected.present = true;
selected.apply_abi = 3;
selected.storage_flags = OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE;
EXPECT_FALSE(ota_bl_caps_prefer(selected, 99, OTA_BL_STORAGE_QSPI, true));
EXPECT_TRUE(ota_bl_caps_prefer(selected, 4,
OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE, true));
auto mf = boot_manifest_bytes();
MotaManifest m;
ASSERT_TRUE(mota_parse_manifest(mf.data(), mf.size(), m));
OtaBootloaderIdentity installed;
installed.present = installed.crc_ok = true;
installed.image_start = OTA_BOOT_IMAGE_START;
installed.image_size = OTA_BOOT_IMAGE_SIZE;
installed.board_id = OTA_XIAO_BOARD_ID_BASE;
memcpy(installed.device_name, OTA_XIAO_BOOT_DEVICE_NAME,
OTA_BOOT_DEVICE_NAME_SIZE);
uint8_t mid[4] = {0x11,0x22,0x33,0x44};
uint8_t hash8[8]; memcpy(hash8, m.image_hash, sizeof(hash8));
EXPECT_EQ(ota_bootloader_confirmation_gate(m, installed, mid, mid, hash8), OTA_BOOT_CONFIRM_OK);
uint8_t bad_mid[4] = {0};
EXPECT_EQ(ota_bootloader_confirmation_gate(m, installed, mid, bad_mid, hash8),
OTA_BOOT_CONFIRM_MID);
hash8[0] ^= 1;
EXPECT_EQ(ota_bootloader_confirmation_gate(m, installed, mid, mid, hash8),
OTA_BOOT_CONFIRM_IMAGE_HASH);
}
TEST(OtaBootPackage, CapabilityScannerRejectsAnOtherwiseValidUnalignedMarker) {
const uint8_t marker[16] = {'M','O','T','A','B','L','D','R', 3,0, 1,0,
(uint8_t)(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE), 0,0,0};
uint8_t image[64];
memset(image, 0xFF, sizeof(image));
memcpy(image + 1, marker, sizeof(marker));
EXPECT_FALSE(ota_bl_caps_scan_aligned(image, sizeof(image), true).present);
memset(image, 0xFF, sizeof(image));
memcpy(image + 4, marker, sizeof(marker));
const OtaBlCaps caps = ota_bl_caps_scan_aligned(image, sizeof(image), true);
ASSERT_TRUE(caps.present);
EXPECT_EQ(caps.apply_abi, 3u);
EXPECT_EQ(caps.storage_flags,
(uint8_t)(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE));
}
class FakeMotaSeederStream : public Stream {
public:
using Stream::write;
@@ -1812,6 +1966,53 @@ static uint16_t make_have1(uint8_t* buf, uint16_t cap, const uint8_t mid[4],
return encode_have(buf, cap, hv);
}
TEST(OtaTransfer, BootloaderPackageRequiresCapableExplicitMidPull) {
const auto manifest = boot_manifest_bytes();
const uint8_t mid[4] = {0x11, 0x22, 0x33, 0x44};
const uint8_t boot_flags = MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER;
uint8_t wire[64];
// Even a capable XIAO must never autofetch a bootloader package. The target is intentionally
// its exact installed-bootloader board ID, so this proves the manual-MID gate rather than a
// coincidental target mismatch.
OtaManager capable;
OtaStoreRam<45000> capable_store;
capable.begin(OTA_XIAO_BOARD_ID_BASE, nullptr, nullptr);
capable.set_fetch_store(&capable_store);
capable.set_accept_full(true);
capable.set_accept_bootloader(true);
capable.set_autofetch(OtaManager::AUTOFETCH_ANY);
capable.on_message(
wire, make_have1(wire, sizeof(wire), mid, OTA_XIAO_BOARD_ID_BASE, 1,
CODEC_FULL, boot_flags));
EXPECT_EQ(capable.fetchState(), OtaManager::IDLE);
EXPECT_EQ(capable_store.staged_size(), 0u);
// An exact explicit pull admits the strict v3 manifest and opens the store for the 40 KiB payload.
EXPECT_EQ(capable.pull(mid, OTA_XIAO_BOARD_ID_BASE), OtaManager::PULL_STARTED);
deliver_manifest_fragment(capable, mid, 0, manifest.data(), OTA_MF_FRAG);
deliver_manifest_fragment(capable, mid, 1, manifest.data() + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(capable.fetchState(), OtaManager::FETCHING);
EXPECT_TRUE(capable.fetched_is_bootloader());
EXPECT_GT(capable_store.staged_size(), OTA_BOOT_IMAGE_SIZE);
// The same explicit pull is rejected before storage begin when this firmware lacks the privileged
// capability. This is the manager-layer guard; every apply backend has an independent rejection too.
OtaManager incapable;
OtaStoreRam<45000> incapable_store;
incapable.begin(OTA_XIAO_BOARD_ID_BASE, nullptr, nullptr);
incapable.set_fetch_store(&incapable_store);
incapable.set_accept_full(true);
EXPECT_EQ(incapable.pull(mid, OTA_XIAO_BOARD_ID_BASE), OtaManager::PULL_STARTED);
deliver_manifest_fragment(incapable, mid, 0, manifest.data(), OTA_MF_FRAG);
deliver_manifest_fragment(incapable, mid, 1, manifest.data() + OTA_MF_FRAG,
(uint16_t)(MOTA_MFL - OTA_MF_FRAG));
EXPECT_EQ(incapable.fetchState(), OtaManager::FAILED);
EXPECT_EQ(incapable.fetchError(), OtaManager::FETCH_ERROR_CODEC);
EXPECT_EQ(incapable_store.staged_size(), 0u);
}
// A node must not fetch firmware it can't apply: a catalog row whose codec the platform can't decode is
// not fetched. Full-image acceptance is platform-selectable (nRF52 single-slot disables it).
TEST(OtaTransfer, RejectsIncompatibleCodec) {
+3 -1
View File
@@ -69,8 +69,10 @@ TEST(OtaFlashPlan, SelectsCeilingFromLinkedLayoutAndStorage) {
// Standard or ExtraFS linker without an internal secondary filesystem reclaims the unused 100 KiB.
EXPECT_EQ(mota_nrf52_stage_ceiling_for_layout(EXPANDED, false), EXPANDED);
EXPECT_EQ(mota_nrf52_stage_ceiling_for_layout(LEGACY, false), EXPANDED);
// The explicit XIAO boot-update linker leaves 40 KiB scratch at 0xE0000 and still stages externally.
EXPECT_EQ(mota_nrf52_stage_ceiling_for_layout(MOTA_NRF52_BOOT_SCRATCH_START, false), EXPANDED);
// An unrecognized linker region is never permission to erase a larger window.
EXPECT_EQ(mota_nrf52_stage_ceiling_for_layout(0xE0000u, false), LEGACY);
EXPECT_EQ(mota_nrf52_stage_ceiling_for_layout(0xE1000u, false), LEGACY);
}
TEST(OtaFlashPlan, ValidatesExternalInplacePatchGeometryBeforeHandoff) {