#!/usr/bin/env python3 """ Create a U-Boot environment image from a text environment file This is required because the Anbernic environment format is not compatible with the binary format produced by the mkenvimage tool from the uboot-tools package """ import argparse import struct import sys import zlib ENCODING = "utf-8" DEFAULT_IMG_SIZE = 128 * 1024 DEFAULT_IMG_FLAGS = 0 class EnvError(Exception): pass def makebinenv(infile, outfile, size, flags): """ Create a U-Boot environment image from a text environment file """ buf = b"" for line in infile: clean_line = line.strip() if clean_line and not clean_line.startswith("#"): buf += bytes(clean_line, ENCODING) + b"\0" infile.close() buf += b"\0" if flags is not None: buf = bytes([flags]) + buf envsize = 4 + len(buf) if envsize > size: raise EnvError(f"Insufficient environment space: need at least {envsize} bytes") buf += bytes(size - envsize) crc = zlib.crc32(buf, 0) buf = struct.pack("