Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/ardupilotwaf/embed.py
Views: 1798
#!/usr/bin/env python12'''3script to create ap_romfs_embedded.h from a set of static files45Andrew Tridgell6May 20177'''89import os, sys, zlib1011def write_encode(out, s):12out.write(s.encode())1314def embed_file(out, f, idx, embedded_name, uncompressed):15'''embed one file'''16try:17contents = open(f,'rb').read()18except Exception:19raise Exception("Failed to embed %s" % f)2021if embedded_name.endswith("bootloader.bin"):22# round size to a multiple of 32 bytes for bootloader, this ensures23# it can be flashed on a STM32H7 chip24blen = len(contents)25pad = (32 - (blen % 32)) % 3226if pad != 0:27contents += bytes([0xff]*pad)28print("Padded %u bytes for %s to %u" % (pad, embedded_name, len(contents)))2930crc = crc32(contents)31write_encode(out, '__EXTFLASHFUNC__ static const uint8_t ap_romfs_%u[] = {' % idx)3233if uncompressed:34# terminate if there's not already an existing null. we don't add it to35# the contents to avoid storing the wrong length36null_terminate = 0 not in contents37b = contents38else:39# compress it (max level, max window size, raw stream, max mem usage)40z = zlib.compressobj(level=9, method=zlib.DEFLATED, wbits=-15, memLevel=9)41b = z.compress(contents)42b += z.flush()43# decompressed data will be null terminated at runtime, nothing to do here44null_terminate = False4546write_encode(out, ",".join(str(c) for c in b))47if null_terminate:48write_encode(out, ",0")49write_encode(out, '};\n\n');50return crc, len(contents)5152def crc32(bytes, crc=0):53'''crc32 equivalent to crc32_small() from AP_Math/crc.cpp'''54for byte in bytes:55crc ^= byte56for i in range(8):57mask = (-(crc & 1)) & 0xFFFFFFFF58crc >>= 159crc ^= (0xEDB88320 & mask)60return crc6162def create_embedded_h(filename, files, uncompressed=False):63'''create a ap_romfs_embedded.h file'''6465out = open(filename, "wb")66write_encode(out, '''// generated embedded files for AP_ROMFS\n\n''')6768# remove duplicates and sort69files = sorted(list(set(files)))70crc = {}71decompressed_size = {}72for i in range(len(files)):73(name, filename) = files[i]74try:75crc[filename], decompressed_size[filename] = embed_file(out, filename, i, name, uncompressed)76except Exception as e:77print(e)78return False7980write_encode(out, '''const AP_ROMFS::embedded_file AP_ROMFS::files[] = {\n''')8182for i in range(len(files)):83(name, filename) = files[i]84if uncompressed:85ustr = ' (uncompressed)'86else:87ustr = ''88print("Embedding file %s:%s%s" % (name, filename, ustr))89write_encode(out, '{ "%s", sizeof(ap_romfs_%u), %d, 0x%08x, ap_romfs_%u },\n' % (90name, i, decompressed_size[filename], crc[filename], i))91write_encode(out, '};\n')92out.close()93return True9495if __name__ == '__main__':96import sys97flist = []98for i in range(1, len(sys.argv)):99f = sys.argv[i]100flist.append((f, f))101create_embedded_h("/tmp/ap_romfs_embedded.h", flist)102103104