"""RAK4631 application-only UF2 validation and USB-bound destination lookup. UF2 format: https://github.com/microsoft/uf2 Board identity: meshtastic/nRF52_Bootloader, wiscore_rak4631_board/board.h. No caller-selected mount, filename, block device, erase, or bootloader image. """ import hashlib import json import os from pathlib import Path import re import stat import struct MAX_IMAGE = 2 * 1024 * 1024 FAMILY_NRF52840_S140_V6 = 0xADA52840 APPLICATION_START = 0x26000 APPLICATION_END = 0xF4000 def require(value, message): if not value: raise ValueError(message) def validate_image(raw): require(type(raw) is bytes and 512 <= len(raw) <= MAX_IMAGE and len(raw) % 512 == 0, 'invalid RAK4631 UF2 image size') count = len(raw) // 512 addresses, numbers = set(), set() for offset in range(0, len(raw), 512): block = raw[offset:offset + 512] first, second, flags, address, size, number, total, family = struct.unpack('<8I', block[:32]) require((first, second, struct.unpack(' 0, 'UF2 transfer stopped before completion') block = block[written:] os.fsync(destination_fd) return {**receipt, 'contract': 1, 'board': 'rak4631', 'state': 'written-awaiting-device-verification'}