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

+9 -2
View File
@@ -1089,12 +1089,19 @@ def compatible_mota(info: MotaInfo, target: TargetInfo) -> tuple[bool, str]:
return False, "bootloader self-update requires installed ABI 3 or newer"
if target.bootloader_codecs != 0x5:
return False, "bootloader self-update requires FULL|INPLACE codecs 0x5"
expected_storage = 0x09 if target.nrf_sd else 0x0E if target.nrf_qspi else 0x0A
adaptive_bootloader = target.boot_hw_id in (
"NRF_BL_239A0029_3401_AUTO_DFU", "NRF_BL_239A0029_4631_AUTO_DFU",
)
internal_rak_bootloader = target.boot_hw_id in (
"NRF_BL_239A0029_3401_DFU", "NRF_BL_239A0029_4631_DFU",
)
expected_storage = (0x0A if internal_rak_bootloader else 0x1E if adaptive_bootloader else
0x09 if target.nrf_sd else 0x0E if target.nrf_qspi else 0x0A)
if (
target.boot_storage != expected_storage
or info.bootloader_storage != expected_storage
):
return False, "bootloader package, installed capabilities, and application storage differ"
return False, "bootloader package and installed capabilities differ"
if info.block_size > target.max_block_size:
return False, f"bootloader block exceeds destination maxblk:{target.max_block_size}"
return True, ""
+48
View File
@@ -0,0 +1,48 @@
"""Adaptive bootloader packages use internal flash regardless of app storage."""
from dataclasses import replace
import unittest
import lora_ota as ota
class RakBootloaderCompatibilityTest(unittest.TestCase):
def fixture(self, name, target_id, external):
hw = f"NRF_BL_239A0029_{name}_DFU"
package = ota.MotaInfo(
path=None, blob=b"", flags=ota.MOTA_FLAG_FULL | ota.MOTA_FLAG_BOOTLOADER,
target_id=target_id, fw_version=0x02040A02, image_size=40960,
payload_size=40960, block_size=1024, merkle_root=b"\0" * 4,
image_hash=b"\0" * 8, codec_id=0, hw_id=hw,
base_hash=b"\0" * 8, payload_offset=365, bootloader_storage=0x0A,
)
target = ota.TargetInfo(
name=name, target_id=1, base_hash=b"\0" * 8, platform="nrf52",
nrf_sd=False, nrf_qspi=external, hw_id=name,
bootloader_version="OTAFIX2.4.10-preview.1", bootloader_abi=3,
bootloader_codecs=5, status="", self_status="",
boot_target_id=target_id, boot_hw_id=hw, boot_storage=0x0A,
)
return package, target
def test_internal_boot_update_with_either_application_backend(self):
for name, target_id in (("3401", 0x23818A80), ("4631", 0x2D0DF000)):
for external in (False, True):
with self.subTest(board=name, external=external):
package, target = self.fixture(name, target_id, external)
self.assertTrue(ota.compatible_mota(package, target)[0])
for flags in (0x1E, 0x0E, 0x16):
self.assertFalse(ota.compatible_mota(
replace(package, bootloader_storage=flags), target)[0])
self.assertFalse(ota.compatible_mota(
package, replace(target, boot_storage=flags))[0])
self.assertFalse(ota.compatible_mota(
package, replace(target, bootloader_abi=2))[0])
self.assertFalse(ota.compatible_mota(
package, replace(target, boot_hw_id="NRF_BL_239A0029_4631_AUTO_DFU"))[0])
self.assertFalse(ota.compatible_mota(
package, replace(target, boot_target_id=target_id ^ 1))[0])
if __name__ == "__main__":
unittest.main()
+36 -3
View File
@@ -108,11 +108,17 @@ BOOT_STORAGE_SD = 0x01
BOOT_STORAGE_STAGE_CEILING = 0x02
XIAO_BOOT_STORAGE_QSPI = 0x04
XIAO_BOOT_STORAGE_UPDATE = 0x08
BOOT_STORAGE_KNOWN = 0x0F
BOOT_STORAGE_HEADER_W25 = 0x10
BOOT_STORAGE_KNOWN = 0x1F
BOOT_STORAGE_SD_UPDATE = BOOT_STORAGE_SD | XIAO_BOOT_STORAGE_UPDATE
BOOT_STORAGE_QSPI_UPDATE = (BOOT_STORAGE_STAGE_CEILING | XIAO_BOOT_STORAGE_QSPI |
XIAO_BOOT_STORAGE_UPDATE)
BOOT_STORAGE_INTERNAL_UPDATE = BOOT_STORAGE_STAGE_CEILING | XIAO_BOOT_STORAGE_UPDATE
BOOT_STORAGE_RAK_AUTO_UPDATE = BOOT_STORAGE_QSPI_UPDATE | BOOT_STORAGE_HEADER_W25
RAK_AUTO_BOOTLOADER_IDENTITIES = (
(0x239A0029, "3401_AUTO_DFU"),
(0x239A0029, "4631_AUTO_DFU"),
)
BOOT_REQUIRED_APP_CODEC_MASK = ((1 << CODEC_FULL) |
(1 << CODEC_DETOOLS_INPLACE))
@@ -696,7 +702,7 @@ def audit_bootloader_target_inventory(application_target_ids=()) -> dict:
identities = (
(XIAO_BOOT_BOARD_ID_BASE, "XIAO_DFU"),
(XIAO_BOOT_BOARD_ID_SENSE, "XIAO_DFU"),
) + INTERNAL_BOOTLOADER_IDENTITIES
) + INTERNAL_BOOTLOADER_IDENTITIES + RAK_AUTO_BOOTLOADER_IDENTITIES
app_ids = set(application_target_ids)
targets = {}
for identity in identities:
@@ -714,6 +720,7 @@ def audit_bootloader_target_inventory(application_target_ids=()) -> dict:
def bootloader_identity_is_buildable(board_id: int, device_name: str) -> bool:
return ((board_id, device_name) in INTERNAL_BOOTLOADER_IDENTITIES or
(board_id, device_name) in RAK_AUTO_BOOTLOADER_IDENTITIES or
(board_id, device_name) in (
(XIAO_BOOT_BOARD_ID_BASE, "XIAO_DFU"),
(XIAO_BOOT_BOARD_ID_SENSE, "XIAO_DFU")))
@@ -734,6 +741,8 @@ def bootloader_qualified_platform_profile(board_id: int, device_name: str):
def bootloader_qualified_storage_profiles(board_id: int, device_name: str):
"""Return the exact allowed capability-marker profiles for one identity."""
identity = (board_id, device_name)
if identity in RAK_AUTO_BOOTLOADER_IDENTITIES:
return (BOOT_STORAGE_RAK_AUTO_UPDATE,)
if identity in ((XIAO_BOOT_BOARD_ID_BASE, "XIAO_DFU"),
(XIAO_BOOT_BOARD_ID_SENSE, "XIAO_DFU")):
return (BOOT_STORAGE_QSPI_UPDATE,)
@@ -836,7 +845,7 @@ def bootloader_caps_storage(image: bytes) -> Optional[int]:
if valid_count != 1:
return None
if storage in (BOOT_STORAGE_SD_UPDATE, BOOT_STORAGE_QSPI_UPDATE,
BOOT_STORAGE_INTERNAL_UPDATE):
BOOT_STORAGE_INTERNAL_UPDATE, BOOT_STORAGE_RAK_AUTO_UPDATE):
found = storage
return found if valid_count == 1 else None
@@ -845,6 +854,19 @@ def xiao_bootloader_caps_ok(image: bytes) -> bool:
return bootloader_caps_storage(image) == BOOT_STORAGE_QSPI_UPDATE
def bootloader_optional_app_storage(image: bytes) -> int:
record = (b"MOTARAMA" + struct.pack("<HHI", 1, 72, 65536) +
b"MOTASTOR" + struct.pack("<HHBBBB", 1, 16, 0x14, 0, 0, 0))
offsets = [off for off in range(0, len(image) - 7, 4)
if image[off:off + 8] == b"MOTASTOR"]
if not offsets:
return 0
if (len(offsets) != 1 or offsets[0] < 16 or
image[offsets[0] - 16:offsets[0] + 16] != record):
raise ValueError("optional RAK application storage marker is invalid or ambiguous")
return 0x14
def validate_bootloader_image(image: bytes, target_id: Optional[int] = None,
signed_hw_id: Optional[bytes] = None) -> BootloaderIdentity:
if len(image) != XIAO_BOOT_IMAGE_SIZE:
@@ -876,6 +898,17 @@ def validate_bootloader_image(image: bytes, target_id: Optional[int] = None,
if actual_storage not in expected_storage:
expected = "/".join(f"0x{value:02X}" for value in expected_storage)
raise ValueError(f"bootloader lacks exact ABI 3 self-update capabilities {expected}")
optional_storage = bootloader_optional_app_storage(image)
if optional_storage and (actual_storage != BOOT_STORAGE_INTERNAL_UPDATE or
identity.board_id != 0x239A0029 or
identity.device_name not in ("3401_DFU", "4631_DFU")):
raise ValueError("optional RAK application storage requires the deployed board identity")
if actual_storage == BOOT_STORAGE_RAK_AUTO_UPDATE or optional_storage:
ram_marker = b"MOTARAMA" + struct.pack("<HHI", 1, 72, 65536)
count = sum(image[off:off + 16] == ram_marker
for off in range(0, len(image) - 15, 4))
if count != 1 or struct.unpack_from("<I", image)[0] > 0x20030000:
raise ValueError("adaptive bootloader must retain its 64 KiB hybrid RAM arena")
expected_platform = bootloader_qualified_platform_profile(
identity.board_id, identity.device_name)
if expected_platform is not None:
+55 -2
View File
@@ -1500,7 +1500,7 @@ def test_xiao_bootloader_caps_rejects_malformed_or_unaligned_markers():
malformed_decoy = bytearray(caps())
struct.pack_into("<8sHHB3s", malformed_decoy, 24,
ml.XIAO_BOOT_CAPS_MAGIC, ml.BOOT_FORMAT_VER,
ml.BOOT_REQUIRED_APP_CODEC_MASK, 0x1C, b"\0\0\0")
ml.BOOT_REQUIRED_APP_CODEC_MASK, 0x2C, b"\0\0\0")
assert ml.bootloader_caps_storage(bytes(malformed_decoy)) == \
ml.BOOT_STORAGE_QSPI_UPDATE
@@ -1672,7 +1672,9 @@ def test_bootloader_build_inventory_is_unique_and_disjoint_from_app_targets():
for value in re.findall(r"\{ 0x([0-9a-fA-F]{8}),", target_header)
}
audited = ml.audit_bootloader_target_inventory(app_targets)
assert len(audited) == len(expected) + 2 # generic internal plus two legacy XIAOs
assert len(audited) == len(expected) + 4 # internal, two XIAOs, two adaptive RAKs
assert audited[0xD04AB3AB] == (0x239A0029, "3401_AUTO_DFU")
assert audited[0xFEEAFD1B] == (0x239A0029, "4631_AUTO_DFU")
# Generic parsing remains useful for inspecting a future candidate, but a
# signing/build call fails closed until its exact identity is qualified.
@@ -1762,6 +1764,57 @@ def test_qualified_bootloader_platform_and_storage_profiles_are_exact():
pass
def test_adaptive_bootloader_preserves_update_and_hybrid_capabilities():
for name in ("3401_AUTO_DFU", "4631_AUTO_DFU"):
image = _generic_bootloader_image(
device_name=name, storage=ml.BOOT_STORAGE_RAK_AUTO_UPDATE)
marker = b"MOTARAMA" + struct.pack("<HHI", 1, 72, 65536)
def add_ram(data):
struct.pack_into("<I", data, 0, 0x20030000)
data[0x90:0xA0] = marker
image = _rewrite_boot_image(image, add_ram)
assert ml.validate_bootloader_image(image).device_name == name
assert ml.bootloader_caps_storage(image) == 0x1E
mutations = (
lambda data: data.__setitem__(slice(0x90, 0xA0), b"\xff" * 16),
lambda data: data.__setitem__(slice(0xA0, 0xB0), marker),
lambda data: struct.pack_into("<I", data, 0, 0x20040000),
lambda data: data.__setitem__(0x8C, 0x16),
lambda data: data.__setitem__(0x88, 2),
)
for mutate in mutations:
try:
ml.validate_bootloader_image(_rewrite_boot_image(image, mutate))
assert False, "invalid adaptive successor was accepted"
except ValueError:
pass
def test_compatible_rak_keeps_legacy_contract_and_optional_application_storage():
marker = b"MOTARAMA" + struct.pack("<HHI", 1, 72, 65536)
optional = b"MOTASTOR" + struct.pack("<HHB3x", 1, 16, 0x14)
for name in ("3401_DFU", "4631_DFU"):
def add_storage(data):
struct.pack_into("<I", data, 0, 0x20030000)
data[0x90:0xB0] = marker + optional
image = _rewrite_boot_image(_generic_bootloader_image(device_name=name), add_storage)
assert ml.validate_bootloader_image(image).device_name == name
assert ml.bootloader_caps_storage(image) == 0x0A
assert ml.bootloader_optional_app_storage(image) == 0x14
for mutate in (
lambda raw: raw.__setitem__(0xAC, 0x04),
lambda raw: raw.__setitem__(0xAD, 1),
lambda raw: raw.__setitem__(slice(0xB0, 0xC0), optional),
lambda raw: raw.__setitem__(slice(0x90, 0xA0), b"\xff" * 16),
lambda raw: struct.pack_into("<I", raw, 0, 0x20040000),
):
try:
ml.validate_bootloader_image(_rewrite_boot_image(image, mutate))
assert False, "malformed optional application storage accepted"
except ValueError:
pass
def test_bootloader_builder_rejects_wrong_identity_geometry_and_continuity():
priv = Ed25519PrivateKey.generate()
image = _xiao_bootloader_image()