#!/usr/bin/python3 """ Build a single insert for RedBend OTA filesystem updates All rights reversed """ from PyCRC import CRC32 import subprocess import os import io import struct permission_bytes = bytes.fromhex('1A 5F 72 65 64 62 65 6E 64 5F 30 34 30 37 35 35 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 30 34 31 37 37 37 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 30 34 32 37 35 35 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 31 30 30 36 34 34 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 31 30 30 37 35 35 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 31 30 30 37 37 37 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 31 30 34 37 35 35 3A 30 30 30 30 3A 30 30 30 30 00 1A 5F 72 65 64 62 65 6E 64 5F 31 32 30 37 37 37 3A 30 30 30 30 3A 30 30 30 30 00') general_bytes = bytes.fromhex('50 40 01 00 80 38 01 00 00 00 00') fs_bytes = bytes.fromhex('80 80 02 00 00 ') # initialize tables only once crc32 = CRC32.CRC32() def compress(fname): """I compress a file with LZMA and return the byte array""" lzma_fname = fname + ".lzma" print(lzma_fname) ret = subprocess.call(["lzma", "e", fname, lzma_fname]) assert ret == 0 with open(lzma_fname, "rb") as f: return f.read() def build_toc(fname, size, compr_size): """I build the table of content for a single file and insert""" wr = io.BytesIO() # name + NUL wr.write(fname.encode('utf8')) wr.write(b'\0') # permissions wr.write(permission_bytes) # FILE_SIZE + D100 0000 (offset to permission) + COMPR_SIZE off = len(wr.getvalue()) wr.write(struct.pack('