-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_swupdate.py
executable file
·215 lines (166 loc) · 6.35 KB
/
gen_swupdate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
#
# Copyright (c) 2017-2020 Linutronix GmbH
#
# SPDX-License-Identifier: MIT
import libconf
import codecs
import logging
import os
import os.path
import shutil
import hashlib
from subprocess import Popen, PIPE
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from tempfile import TemporaryDirectory
import struct
def getuncompressedsize(filename):
with open(filename, 'rb') as f:
f.seek(-4, 2)
return struct.unpack('I', f.read(4))[0]
def getsha256(filename):
m = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
data = f.read(1024)
if not data:
break
m.update(data)
return m.hexdigest()
def find_and_link_file(entry, libdirs):
fname = entry.filename
for d in libdirs:
dname = os.path.join(d, fname)
if os.path.exists(dname):
try:
os.symlink(dname, fname)
except FileExistsError:
pass
return dname
def handle_image(i, opt):
if 'filename' not in i:
return
file_iv = find_and_link_file(i, opt.libdirs)
sha256 = getsha256(i.filename)
i['sha256'] = sha256
if 'volume' in i and 'compressed' in i and (
i.compressed is True or i.compressed == "zlib"):
if 'encrypted' in i:
logging.warning("""The decompressed-size cannot be calculated
for preencrypted volumes.""")
else:
unc_size = getuncompressedsize(file_iv)
if 'properties' not in i:
i['properties'] = {}
i['properties']['decompressed-size'] = str(unc_size)
def handle_script(i, opt):
if 'filename' not in i:
return
find_and_link_file(i, opt.libdirs)
sha256 = getsha256(i.filename)
i['sha256'] = sha256
def find_key(key, d):
if isinstance(d, dict):
if key in d:
for i in d[key]:
yield i
for k in d.values():
for x in find_key(key, k):
yield x
def main():
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter,
description='''Generate (signed) swu-update file,
based on information from a
template sw-description.''')
parser.add_argument("template", metavar="TEMPLATE",
help="sw-description template (sw-decription.in)")
parser.add_argument("--debug", action="store_true", dest="debug",
default=False,
help="Enable various features for debugging")
parser.add_argument("-k", "--key", dest="key",
help="""pkcs11 uri or file name of the key used for
signing the update""")
parser.add_argument("-o", "--output", dest="output",
default="firmware.swu",
help="filename of the resulting update file")
parser.add_argument("-C", "--chdir", dest="chdir",
help="""directory where the sw-update cpio archive is
built""")
parser.add_argument("-L", "--libdir", dest="libdirs", action="append",
default=['.'],
help="""add path where files (e.g. images and scripts)
are searched""")
opt = parser.parse_args()
# make all paths absolute
swdescription_in = os.path.abspath(opt.template)
if opt.key:
keyfile = os.path.abspath(opt.key)
opt.output = os.path.abspath(opt.output)
opt.libdirs = [os.path.abspath(p) for p in opt.libdirs]
fp = codecs.open(swdescription_in, 'r', 'utf-8')
cc = libconf.load(fp, filename=swdescription_in)
if not opt.chdir:
temp = TemporaryDirectory()
opt.chdir = temp.name
os.chdir(opt.chdir)
for i in find_key('images', cc.software):
handle_image(i, opt)
for i in find_key('scripts', cc.software):
handle_script(i, opt)
for i in find_key('files', cc.software):
handle_script(i, opt)
fp = codecs.open('sw-description', 'w', 'utf-8')
libconf.dump(cc, fp)
fp.close()
files = ['sw-description']
if opt.key:
if os.path.isfile(keyfile):
logging.warning("""Please consider providing a pkcs11 uri instead
of a key file.""")
sign_cmd = 'openssl dgst -sha256 \
-sign "%s" sw-description \
> sw-description.sig' % keyfile
else:
sign_cmd = 'openssl dgst -sha256 \
-engine pkcs11 \
-keyform engine \
-sign "%s" sw-description \
> sw-description.sig' % opt.key
# Preventing the malloc check works around Debian bug #923333
if os.system('MALLOC_CHECK_=0 ' + sign_cmd) != 0:
print('failed to sign sw-description')
files.append('sw-description.sig')
for i in find_key('images', cc.software):
if 'filename' in i:
files.append(i.filename)
for i in find_key('scripts', cc.software):
if 'filename' in i:
files.append(i.filename)
for i in find_key('files', cc.software):
if 'filename' in i:
files.append(i.filename)
swfp = open(opt.output, 'wb')
cpio_cmd = 'paxcpio'
cpio_opt = '-L'
if not shutil.which(cpio_cmd):
cpio_cmd = 'cpio'
cpio_opt = '--dereference'
cpio = Popen([cpio_cmd, '-ov', '-H', 'crc', cpio_opt],
stdin=PIPE, stdout=swfp)
files_for_cpio = []
for i in files:
if i not in files_for_cpio:
files_for_cpio.append(i)
for n in files_for_cpio:
if cpio_cmd == 'cpio' and os.path.getsize(n) > (2 << 30):
logging.warning('''%s is greater than 2GiB. %s will have a bad
checksum with GNU cpio. Install paxcpio or
configure SWUpdate with DISABLE_CPIO_CRC.''',
n, opt.output)
cpio.stdin.write(bytes(n+'\n', 'utf-8'))
cpio.stdin.close()
cpio.wait()
swfp.close()
print('finished')
if __name__ == "__main__":
main()