Support compatible RAK bootloader updates with adaptive app storage

This commit is contained in:
mikecarper committed 2026-09-29 03:08:53 -07:00
1 parent 793531e16b
commit 346cf2fd1f
12 files changed
+430 -22

No files matched your search

+25
View File
@@ -763,6 +763,31 @@ TEST(OtaHybridHandoff, EncodesExactGeometryAndRejectsCorruption) {
MOTA_NRF52_FLASH_PAGE, ram, hash));
}
TEST(OtaBootPackage, OptionalRakApplicationStoragePreservesLegacyBootUpdateContract) {
uint8_t image[128] = {};
const uint8_t boot[16] = {'M','O','T','A','B','L','D','R', 3,0,5,0,0x0A,0,0,0};
const uint8_t bundle[32] = {
'M','O','T','A','R','A','M','A',1,0,72,0,0,0,1,0,
'M','O','T','A','S','T','O','R',1,0,16,0,0x14,0,0,0,
};
memcpy(image, boot, sizeof(boot));
memcpy(image + 32, bundle, sizeof(bundle));
EXPECT_EQ(ota_bl_app_caps_scan(image, sizeof(image)).storage_flags, 0x1Eu);
EXPECT_EQ(ota_bl_app_caps_scan(image, sizeof(image)).optional_app_storage, 0x14u);
EXPECT_EQ(ota_bl_update_caps_scan_aligned(image, sizeof(image), 0x0A).storage_flags, 0x0Au);
for (unsigned byte : {32u, 40u, 42u, 46u, 48u, 56u, 58u, 60u, 63u}) {
image[byte] ^= 1;
EXPECT_EQ(ota_bl_app_caps_scan(image, sizeof(image)).storage_flags, 0x0Au);
image[byte] ^= 1;
}
memcpy(image + 80, bundle, sizeof(bundle));
EXPECT_EQ(ota_bl_optional_app_storage(image, sizeof(image)), 0u);
memset(image + 80, 0, sizeof(bundle));
memset(image + 48, 0, 16); // genuine old board has no optional record
EXPECT_EQ(ota_bl_app_caps_scan(image, sizeof(image)).storage_flags, 0x0Au);
EXPECT_TRUE(ota_bl_update_caps_scan_aligned(image, sizeof(image), 0x0A).present);
}
TEST(OtaBootPackage, LegacyAndCurrentBootloadersHaveSeparateCapabilityViews) {
const uint8_t internal_profile = OTA_BL_PROFILE_INTERNAL_BOOT_UPDATE;
const uint8_t legacy_marker[16] = {
+141
View File
@@ -0,0 +1,141 @@
"""Exercise the actual adaptive store with simulated internal and QSPI media."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
class AdaptiveBootloaderTest(unittest.TestCase):
def test_storage_switch_and_manual_resume(self):
header = (ROOT / "src/helpers/ota/OtaStoreAdaptiveNrf52.h").read_text()
body = header[header.index("class OtaStoreAdaptiveNrf52"):
header.index("} // namespace ota")]
source = r'''
#include <cassert>
#include <new>
#include "src/helpers/ota/OtaStore.h"
#include "src/helpers/ota/OtaBlInfo.h"
#include "src/helpers/ota/OtaRakStoragePolicy.h"
using namespace mesh::ota;
namespace mesh { namespace ota {
static bool capable = true, boot_resume = false, app_resume = false;
static unsigned detected = 2, internal_begins = 0, external_begins = 0;
static unsigned internal_live = 0, external_live = 0;
struct OtaBootloaderIdentity { bool crc_ok = true; const char* device_name = "4631_DFU"; };
bool ota_installed_bootloader_identity(OtaBootloaderIdentity&) { return true; }
struct MotaManifest { bool is_bootloader() const { return boot_resume; } };
bool mota_parse_manifest(const uint8_t*, size_t, MotaManifest&) { return true; }
OtaBlCaps simulatedCaps() {
OtaBlCaps c; c.present = true; c.apply_abi = capable ? 3 : 2;
c.codec_mask = 5; c.storage_flags = capable ? 0x1e : 0x16;
c.optional_app_storage = capable ? 0x14 : 0; return c;
}
OtaBlCaps simulatedBootCaps() {
auto c = simulatedCaps(); c.storage_flags = capable ? 0x0a : 0x02;
c.optional_app_storage = 0; return c;
}
class Store : public OtaStore {
public:
uint32_t size = 0;
bool begin(uint32_t n) override { size = n; return true; }
bool write(uint32_t, const uint8_t*, uint32_t) override { return true; }
bool read(uint32_t, uint8_t*, uint32_t) const override { return true; }
uint32_t capacity() const override { return 65536; }
uint32_t staged_size() const override { return size; }
void clear() override { size = 0; }
};
class OtaStoreFlashNrf52 : public Store {
public:
OtaStoreFlashNrf52() { ++internal_live; assert(external_live == 0); }
~OtaStoreFlashNrf52() { --internal_live; }
bool begin(uint32_t n) override { ++internal_begins; return Store::begin(n); }
bool reopenFor(const uint8_t*, uint32_t) override { return boot_resume; }
};
class OtaStoreQspiNrf52 : public Store {
public:
OtaStoreQspiNrf52() { ++external_live; assert(internal_live == 0); }
~OtaStoreQspiNrf52() { --external_live; }
static uint8_t autoDetect() { return detected; }
static bool headerW25Detected() { return true; }
uint32_t jedec_id() const { return 0xef4015; }
uint8_t status1() const { return 0; }
const char* last_stage() const { return "ok"; }
const char* last_error() const { return "ok"; }
bool begin(uint32_t n) override { ++external_begins; return Store::begin(n); }
bool reopenFor(const uint8_t*, uint32_t) override { return app_resume; }
};
#define ota_bootloader_app_caps simulatedCaps
#define ota_bootloader_update_caps simulatedBootCaps
'''
source += body
source += r'''
} }
int main() {
uint8_t mid[4] = {1, 2, 3, 4};
{
OtaStoreAdaptiveNrf52 store;
assert(store.usesExternal());
assert(store.plan_layout(true, 40960, 365, 40960, true));
assert(store.usesInternal());
assert(store.begin(41330));
assert(internal_begins == 1 && external_begins == 0);
assert(store.plan_layout(true, 100000, 365, 100000, false));
assert(store.usesExternal());
assert(store.begin(100370));
assert(external_begins == 1);
boot_resume = true;
assert(!store.reopenFor(nullptr, 0));
assert(store.usesExternal());
assert(store.reopenFor(mid, 0));
assert(store.usesInternal());
boot_resume = false;
assert(!store.reopenFor(mid, 0));
assert(store.usesExternal());
app_resume = true;
assert(store.reopenFor(mid, 0));
assert(store.usesExternal());
capable = false;
assert(!store.plan_layout(true, 40960, 365, 40960, true));
assert(store.usesExternal());
}
assert(internal_live == 0 && external_live == 0);
capable = true; detected = 0;
{
OtaStoreAdaptiveNrf52 store;
assert(store.usesInternal());
assert(store.plan_layout(true, 40960, 365, 40960, true));
assert(store.begin(41330));
assert(store.plan_layout(false, 40960, 365, 40960, false));
assert(store.usesInternal());
}
assert(internal_live == 0 && external_live == 0);
for (uint8_t flags : {uint8_t(0x0a), uint8_t(0x1e)}) {
OtaBlCaps c = simulatedCaps(); c.storage_flags = flags;
assert(ota_bootloader_self_update_caps_valid(c));
c.apply_abi = 2; assert(!ota_bootloader_self_update_caps_valid(c));
}
for (uint8_t flags : {uint8_t(0x09), uint8_t(0x0e), uint8_t(0x16), uint8_t(0x1f)}) {
OtaBlCaps c = simulatedCaps(); c.storage_flags = flags;
assert(!ota_bootloader_self_update_caps_valid(c));
}
}
'''
source = "#include <initializer_list>\n" + source
with tempfile.TemporaryDirectory() as directory:
cpp = Path(directory) / "adaptive.cpp"
exe = Path(directory) / "adaptive"
cpp.write_text(source)
subprocess.run([
"c++", "-std=c++17", "-Wall", "-Wextra", "-Werror",
"-DOTA_RAK_AUTO_STORE", "-DOTA_INTERNAL_BOOTLOADER_UPDATE",
"-I", str(ROOT), str(cpp), "-o", str(exe),
], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
unittest.main()
+8
View File
@@ -25,6 +25,14 @@ int main() {
const auto Q = RakStorageChoice::Qspi;
const auto U = RakStorageChoice::Unsafe;
for (bool rak3401 : {false, true}) {
const char* board = rak3401 ? "3401_DFU" : "4631_DFU";
assert(rak_storage_choice(0, true, true, board, rak3401, true) == I);
assert(rak_storage_choice(2, true, true, board, rak3401, true) == Q);
assert(rak_storage_choice(2, true, true, board, rak3401, false) == U);
assert(rak_storage_choice(1, true, true, board, rak3401, true) == (rak3401 ? U : Q));
assert(rak_storage_choice(3, true, true, board, rak3401, true) == U);
assert(rak_storage_choice(2, true, true,
rak3401 ? "4631_DFU" : "3401_DFU", rak3401, true) == U);
assert(rak_storage_choice(0, false, false, nullptr, rak3401) == I);
assert(rak_storage_choice(0, true, false, nullptr, rak3401) == U);
assert(rak_storage_choice(0, true, true,