Files
actionkamera/toolkit/env-patch.py
T
BRNSystemsandClaude Opus 5.5 7054eb974f Patch U-Boot env for a standalone full image
env-patch.py edits a U-Boot env image (CRC32 + NUL-separated vars), used
to bake mtdparts, the SD-update bootcmd and the build id into full.bin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 22:01:43 +01:00

27 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""env-patch.py IN OUT KEY=VALUE ... : set variables in a U-Boot env image
(non-redundant: CRC32 LE of the data area, then NUL-separated vars, 00-padded).
An empty VALUE deletes the variable. Size is kept; fails if it no longer fits."""
import sys, struct, zlib
src, dst, sets = sys.argv[1], sys.argv[2], sys.argv[3:]
d = open(src, 'rb').read()
size = len(d)
if struct.unpack('<I', d[:4])[0] != zlib.crc32(d[4:]):
sys.exit(f'{src}: bad env CRC (not a U-Boot env image?)')
body = d[4:]
vars_ = [v for v in body[:body.find(b'\x00\x00')].split(b'\x00') if v]
env = dict(v.split(b'=', 1) for v in vars_)
order = [v.split(b'=', 1)[0] for v in vars_]
for s in sets:
k, v = s.encode().split(b'=', 1)
if v:
if k not in env: order.append(k)
env[k] = v
else:
env.pop(k, None); order = [o for o in order if o != k]
data = b'\x00'.join(k + b'=' + env[k] for k in order) + b'\x00\x00'
if len(data) > size - 4:
sys.exit(f'env too big: {len(data)} > {size - 4}')
data += b'\x00' * (size - 4 - len(data))
open(dst, 'wb').write(struct.pack('<I', zlib.crc32(data)) + data)