CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/ardupilotwaf/embed.py
Views: 1798
1
#!/usr/bin/env python
2
3
'''
4
script to create ap_romfs_embedded.h from a set of static files
5
6
Andrew Tridgell
7
May 2017
8
'''
9
10
import os, sys, zlib
11
12
def write_encode(out, s):
13
out.write(s.encode())
14
15
def embed_file(out, f, idx, embedded_name, uncompressed):
16
'''embed one file'''
17
try:
18
contents = open(f,'rb').read()
19
except Exception:
20
raise Exception("Failed to embed %s" % f)
21
22
if embedded_name.endswith("bootloader.bin"):
23
# round size to a multiple of 32 bytes for bootloader, this ensures
24
# it can be flashed on a STM32H7 chip
25
blen = len(contents)
26
pad = (32 - (blen % 32)) % 32
27
if pad != 0:
28
contents += bytes([0xff]*pad)
29
print("Padded %u bytes for %s to %u" % (pad, embedded_name, len(contents)))
30
31
crc = crc32(contents)
32
write_encode(out, '__EXTFLASHFUNC__ static const uint8_t ap_romfs_%u[] = {' % idx)
33
34
if uncompressed:
35
# terminate if there's not already an existing null. we don't add it to
36
# the contents to avoid storing the wrong length
37
null_terminate = 0 not in contents
38
b = contents
39
else:
40
# compress it (max level, max window size, raw stream, max mem usage)
41
z = zlib.compressobj(level=9, method=zlib.DEFLATED, wbits=-15, memLevel=9)
42
b = z.compress(contents)
43
b += z.flush()
44
# decompressed data will be null terminated at runtime, nothing to do here
45
null_terminate = False
46
47
write_encode(out, ",".join(str(c) for c in b))
48
if null_terminate:
49
write_encode(out, ",0")
50
write_encode(out, '};\n\n');
51
return crc, len(contents)
52
53
def crc32(bytes, crc=0):
54
'''crc32 equivalent to crc32_small() from AP_Math/crc.cpp'''
55
for byte in bytes:
56
crc ^= byte
57
for i in range(8):
58
mask = (-(crc & 1)) & 0xFFFFFFFF
59
crc >>= 1
60
crc ^= (0xEDB88320 & mask)
61
return crc
62
63
def create_embedded_h(filename, files, uncompressed=False):
64
'''create a ap_romfs_embedded.h file'''
65
66
out = open(filename, "wb")
67
write_encode(out, '''// generated embedded files for AP_ROMFS\n\n''')
68
69
# remove duplicates and sort
70
files = sorted(list(set(files)))
71
crc = {}
72
decompressed_size = {}
73
for i in range(len(files)):
74
(name, filename) = files[i]
75
try:
76
crc[filename], decompressed_size[filename] = embed_file(out, filename, i, name, uncompressed)
77
except Exception as e:
78
print(e)
79
return False
80
81
write_encode(out, '''const AP_ROMFS::embedded_file AP_ROMFS::files[] = {\n''')
82
83
for i in range(len(files)):
84
(name, filename) = files[i]
85
if uncompressed:
86
ustr = ' (uncompressed)'
87
else:
88
ustr = ''
89
print("Embedding file %s:%s%s" % (name, filename, ustr))
90
write_encode(out, '{ "%s", sizeof(ap_romfs_%u), %d, 0x%08x, ap_romfs_%u },\n' % (
91
name, i, decompressed_size[filename], crc[filename], i))
92
write_encode(out, '};\n')
93
out.close()
94
return True
95
96
if __name__ == '__main__':
97
import sys
98
flist = []
99
for i in range(1, len(sys.argv)):
100
f = sys.argv[i]
101
flist.append((f, f))
102
create_embedded_h("/tmp/ap_romfs_embedded.h", flist)
103
104