Harden LoRa OTA and enable unattended targets
This commit is contained in:
1 parent
8599cf0acb
commit
11aa4bc191
17 files changed
+204
-86
No files matched your search
@@ -48,6 +48,6 @@ firmware and matches a delta's `base_hash` against its own `EndF`. Wiring (handl
|
||||
- **nRF52 / STM32** (emit `.hex` → `.uf2`): the same hook rewrites the `.hex` with the trailer at the image
|
||||
end. The byte logic is `motalib.ensure_endf`, used everywhere.
|
||||
|
||||
`target_id` = `sha2-256:4(pio_env_name)`, `hw_id` = `-D MOTA_HW_ID`, `fw_version` = parsed from
|
||||
`FIRMWARE_VERSION` — so a node (and `motatool`, reading the firmware's `EndF`) auto-discovers identity
|
||||
without relying on filenames.
|
||||
`target_id` = `sha2-256:4(pio_env_name)`, `hw_id` = explicit `-D MOTA_HW_ID` or a role-stripped hardware
|
||||
family derived from the environment name, and `fw_version` = parsed from `FIRMWARE_VERSION`. A node (and
|
||||
`motatool`, reading the firmware's `EndF`) therefore auto-discovers identity without relying on filenames.
|
||||
@@ -22,6 +22,7 @@ from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import io
|
||||
import re
|
||||
import struct
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Optional, Tuple
|
||||
@@ -53,6 +54,10 @@ APPROVAL_YES = b"APRV" # 41 50 52 56 = approved
|
||||
|
||||
DEFAULT_BLOCK_SIZE = 1024
|
||||
|
||||
# nRF52840 OTAFIX's detools in-place workspace. A firmware image (including EndF) must fit here, and
|
||||
# the staged .mota must begin above it. Keep in sync with OtaFlashLayout_nrf52.h / the OTAFIX bootloader.
|
||||
NRF52_INPLACE_MEMORY = 0x00098000
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Multihash helpers (sha2-256 truncations)
|
||||
@@ -108,6 +113,23 @@ def target_id_for_env(env_name: str) -> int:
|
||||
return int.from_bytes(d, "little")
|
||||
|
||||
|
||||
def hardware_id_for_env(env_name: str) -> str:
|
||||
"""Derive a stable hardware-family tag from a PlatformIO environment name.
|
||||
|
||||
Role/profile suffixes are removed so a deliberate role switch on the same physical board remains
|
||||
possible, while a cross-board install is rejected. Long family names retain a short hash suffix to
|
||||
avoid collisions inside EndF's fixed 32-byte field.
|
||||
"""
|
||||
role = re.search(
|
||||
r"[_-](?:repeater|repeatr|room_server|room_svr|sensor|terminal_chat|kiss_modem|"
|
||||
r"companion_radio|companion|comp_radio)(?=[_-]|$)", env_name, re.IGNORECASE)
|
||||
family = (env_name[:role.start()] if role else env_name).strip("_-") or env_name.strip("_-")
|
||||
if len(family) <= 32:
|
||||
return family
|
||||
suffix = hashlib.sha256(family.encode()).hexdigest()[:8]
|
||||
return f"{family[:23].rstrip('_-')}-{suffix}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# EndF trailer
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -104,6 +104,8 @@ def _firmware_ident():
|
||||
import re
|
||||
target_id = ml.target_id_for_env(env["PIOENV"]) # noqa: F821
|
||||
hw_id = (_cppdef("MOTA_HW_ID") or "").replace("\\", "").strip().strip('"').strip("'")
|
||||
if not hw_id:
|
||||
hw_id = ml.hardware_id_for_env(env["PIOENV"]) # noqa: F821
|
||||
ver_s = (_cppdef("FIRMWARE_VERSION") or "").replace("\\", "").strip().strip('"').strip("'")
|
||||
if not ver_s: # not a -D -> read the header MeshCore ships
|
||||
ver_s = _version_from_headers()
|
||||
@@ -138,9 +140,12 @@ def _append_endf_hex(source, target, env): # Intel-HEX path (nRF52: app f
|
||||
body = bytes(ih.tobinarray(start=app_start, size=app_end - app_start))
|
||||
ident = _firmware_ident()
|
||||
out, h8 = ml.ensure_endf(body, ident)
|
||||
if len(out) > ml.NRF52_INPLACE_MEMORY:
|
||||
raise RuntimeError(f"nRF52 OTA image is {len(out)} bytes; in-place limit is "
|
||||
f"{ml.NRF52_INPLACE_MEMORY} bytes")
|
||||
if len(out) == len(body):
|
||||
print(f"EndF: already present in {os.path.basename(path)} (no change)"); return
|
||||
trailer = out[len(body):] # the EndF trailer (60 bytes with identity)
|
||||
trailer = out[len(body):] # the EndF trailer (56 bytes with identity)
|
||||
for i, b in enumerate(trailer):
|
||||
ih[app_end + i] = b # write it right after the app's last byte
|
||||
ih.write_hex_file(path)
|
||||
|
||||
@@ -40,6 +40,18 @@ def test_target_id_for_env():
|
||||
assert ml.target_id_for_env("RAK_4631_repeater") != ml.target_id_for_env("RAK_4631_companion_radio_usb")
|
||||
|
||||
|
||||
def test_hardware_id_for_env():
|
||||
assert ml.hardware_id_for_env("RAK_4631_repeater") == "RAK_4631"
|
||||
assert ml.hardware_id_for_env("RAK_4631_companion_radio_usb") == "RAK_4631"
|
||||
assert ml.hardware_id_for_env("ThinkNode_M2_Repeater_bridge_espnow") == "ThinkNode_M2"
|
||||
assert ml.hardware_id_for_env("wio-e5-repeater_bridge_rs232") == "wio-e5"
|
||||
long_env = "ikoka_handheld_nrf_e22_30dbm_096_rotated_room_server"
|
||||
tag = ml.hardware_id_for_env(long_env)
|
||||
assert len(tag) <= 32 and tag.startswith("ikoka_handheld_nrf_e22")
|
||||
assert tag == ml.hardware_id_for_env(long_env.replace("room_server", "companion_radio_usb"))
|
||||
assert tag != ml.hardware_id_for_env("ikoka_handheld_nrf_e22_22dbm_096_rotated_room_server")
|
||||
|
||||
|
||||
# --- EndF ------------------------------------------------------------------
|
||||
|
||||
def test_endf_roundtrip_and_idempotent():
|
||||
|
||||
Reference in new issue
Block a user