2018-04-14 05:29:46 -03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
'''
|
2018-07-01 20:34:44 -03:00
|
|
|
script to create ap_romfs_embedded.h from a set of static files
|
2018-04-14 05:29:46 -03:00
|
|
|
|
|
|
|
Andrew Tridgell
|
|
|
|
May 2017
|
|
|
|
'''
|
|
|
|
|
2024-01-15 17:57:28 -04:00
|
|
|
import os, sys, zlib
|
2018-04-14 05:29:46 -03:00
|
|
|
|
2018-04-14 21:34:46 -03:00
|
|
|
def write_encode(out, s):
|
|
|
|
out.write(s.encode())
|
|
|
|
|
2019-10-23 06:52:51 -03:00
|
|
|
def embed_file(out, f, idx, embedded_name, uncompressed):
|
2018-04-14 05:29:46 -03:00
|
|
|
'''embed one file'''
|
2018-07-01 20:34:44 -03:00
|
|
|
try:
|
|
|
|
contents = open(f,'rb').read()
|
|
|
|
except Exception:
|
2021-02-25 13:26:59 -04:00
|
|
|
raise Exception("Failed to embed %s" % f)
|
2019-02-16 21:29:14 -04:00
|
|
|
|
|
|
|
if embedded_name.endswith("bootloader.bin"):
|
|
|
|
# round size to a multiple of 32 bytes for bootloader, this ensures
|
|
|
|
# it can be flashed on a STM32H7 chip
|
|
|
|
blen = len(contents)
|
|
|
|
pad = (32 - (blen % 32)) % 32
|
|
|
|
if pad != 0:
|
2024-01-15 17:36:58 -04:00
|
|
|
contents += bytes([0xff]*pad)
|
2019-12-23 02:29:18 -04:00
|
|
|
print("Padded %u bytes for %s to %u" % (pad, embedded_name, len(contents)))
|
2019-02-16 21:29:14 -04:00
|
|
|
|
2024-01-15 17:57:28 -04:00
|
|
|
crc = crc32(contents)
|
2023-01-18 00:36:52 -04:00
|
|
|
write_encode(out, '__EXTFLASHFUNC__ static const uint8_t ap_romfs_%u[] = {' % idx)
|
2018-04-14 05:29:46 -03:00
|
|
|
|
2019-10-23 06:52:51 -03:00
|
|
|
if uncompressed:
|
2024-01-24 14:16:01 -04:00
|
|
|
# terminate if there's not already an existing null. we don't add it to
|
|
|
|
# the contents to avoid storing the wrong length
|
|
|
|
null_terminate = 0 not in contents
|
2024-01-15 17:57:28 -04:00
|
|
|
b = contents
|
2019-10-23 06:52:51 -03:00
|
|
|
else:
|
2024-01-15 17:57:28 -04:00
|
|
|
# compress it (max level, max window size, raw stream, max mem usage)
|
|
|
|
z = zlib.compressobj(level=9, method=zlib.DEFLATED, wbits=-15, memLevel=9)
|
|
|
|
b = z.compress(contents)
|
|
|
|
b += z.flush()
|
2024-01-24 14:16:01 -04:00
|
|
|
# decompressed data will be null terminated at runtime, nothing to do here
|
|
|
|
null_terminate = False
|
2018-07-09 03:30:49 -03:00
|
|
|
|
2024-01-15 17:57:28 -04:00
|
|
|
write_encode(out, ",".join(str(c) for c in b))
|
2024-01-24 14:16:01 -04:00
|
|
|
if null_terminate:
|
|
|
|
write_encode(out, ",0")
|
2018-04-14 21:34:46 -03:00
|
|
|
write_encode(out, '};\n\n');
|
2023-12-30 15:53:15 -04:00
|
|
|
return crc, len(contents)
|
2018-04-14 05:29:46 -03:00
|
|
|
|
2021-02-22 22:56:09 -04:00
|
|
|
def crc32(bytes, crc=0):
|
|
|
|
'''crc32 equivalent to crc32_small() from AP_Math/crc.cpp'''
|
|
|
|
for byte in bytes:
|
|
|
|
crc ^= byte
|
|
|
|
for i in range(8):
|
|
|
|
mask = (-(crc & 1)) & 0xFFFFFFFF
|
|
|
|
crc >>= 1
|
|
|
|
crc ^= (0xEDB88320 & mask)
|
|
|
|
return crc
|
|
|
|
|
2019-10-23 06:52:51 -03:00
|
|
|
def create_embedded_h(filename, files, uncompressed=False):
|
2018-04-14 05:29:46 -03:00
|
|
|
'''create a ap_romfs_embedded.h file'''
|
|
|
|
|
2018-04-14 21:34:46 -03:00
|
|
|
out = open(filename, "wb")
|
|
|
|
write_encode(out, '''// generated embedded files for AP_ROMFS\n\n''')
|
2018-04-14 05:29:46 -03:00
|
|
|
|
2020-03-12 20:25:10 -03:00
|
|
|
# remove duplicates and sort
|
|
|
|
files = sorted(list(set(files)))
|
2021-02-25 13:26:59 -04:00
|
|
|
crc = {}
|
2023-12-30 15:53:15 -04:00
|
|
|
decompressed_size = {}
|
2018-04-14 05:29:46 -03:00
|
|
|
for i in range(len(files)):
|
|
|
|
(name, filename) = files[i]
|
2021-02-25 13:26:59 -04:00
|
|
|
try:
|
2023-12-30 15:53:15 -04:00
|
|
|
crc[filename], decompressed_size[filename] = embed_file(out, filename, i, name, uncompressed)
|
2021-02-25 13:26:59 -04:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2018-07-01 20:34:44 -03:00
|
|
|
return False
|
2018-04-14 05:29:46 -03:00
|
|
|
|
2018-04-14 21:34:46 -03:00
|
|
|
write_encode(out, '''const AP_ROMFS::embedded_file AP_ROMFS::files[] = {\n''')
|
2018-04-14 05:29:46 -03:00
|
|
|
|
|
|
|
for i in range(len(files)):
|
|
|
|
(name, filename) = files[i]
|
2019-10-23 06:52:51 -03:00
|
|
|
if uncompressed:
|
|
|
|
ustr = ' (uncompressed)'
|
|
|
|
else:
|
|
|
|
ustr = ''
|
|
|
|
print("Embedding file %s:%s%s" % (name, filename, ustr))
|
2023-12-30 15:53:15 -04:00
|
|
|
write_encode(out, '{ "%s", sizeof(ap_romfs_%u), %d, 0x%08x, ap_romfs_%u },\n' % (
|
|
|
|
name, i, decompressed_size[filename], crc[filename], i))
|
2018-04-14 21:34:46 -03:00
|
|
|
write_encode(out, '};\n')
|
2018-04-14 05:29:46 -03:00
|
|
|
out.close()
|
2018-07-01 20:34:44 -03:00
|
|
|
return True
|
2018-04-14 05:29:46 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
flist = []
|
|
|
|
for i in range(1, len(sys.argv)):
|
|
|
|
f = sys.argv[i]
|
|
|
|
flist.append((f, f))
|
|
|
|
create_embedded_h("/tmp/ap_romfs_embedded.h", flist)
|