-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_releases.py
executable file
·137 lines (102 loc) · 3.74 KB
/
check_releases.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
#!/usr/bin/python3
import os
import json
import binascii
import struct
def check_bridge():
patterns = ['trezor-bridge-%(version)s-1.i386.rpm',
'trezor-bridge-%(version)s-1.x86_64.rpm',
'trezor-bridge-%(version)s-win32-install.exe',
'trezor-bridge-%(version)s.pkg',
'trezor-bridge_%(version)s_amd64.deb',
'trezor-bridge_%(version)s_i386.deb']
print('Checking Bridge data:')
v = open('bridge/latest.txt', 'r').read().strip()
print('* expected latest version: %s' % v)
if not os.path.isdir(os.path.join('bridge', v)):
return False
ok = True
print('* checking files for version: %s' % v)
for p in patterns:
expected = p % {'version': v}
fname = os.path.join('bridge', v, expected)
print(' * %s ... ' % fname, end='')
if os.path.isfile(fname):
print('OK')
else:
ok = False
print('MISSING')
print()
return ok
def check_firmware(model):
if model not in ['1', '2']:
raise ValueError('Unknown model: %s' % model)
print('Checking Firmware (model %s) data:' % model)
ok = True
releases = json.loads(open('firmware/%s/releases.json' % model, 'r').read())
# Find out the latest firmware release
latest = [0, 0, 0]
for r in releases:
latest = max(latest, r['version'])
print('* expected latest version: %s' % '.'.join(str(x) for x in latest))
for r in releases:
# Check only latest firmware, others may not be available
if r['version'] != latest:
continue
firmware = r['url']
version = '.'.join([str(x) for x in r['version']])
if version not in firmware:
print("Missing '%s' in '%s'" % (version, firmware))
ok = False
continue
print(' * %s ... ' % firmware, end='')
if not os.path.exists(firmware[len('data/'):]):
print('MISSING')
ok = False
continue
else:
data = open(firmware[len('data/'):], 'rb').read()
if model == '1':
start = b'TRZR'
elif model == '2':
start = b'TRZV'
if not data.startswith(binascii.hexlify(start)):
print('WRONG HEADER')
ok = False
continue
if model == '1':
codelen = struct.unpack('<I', binascii.unhexlify(data[8:16]))
codelen = codelen[0]
if codelen + 256 != len(data) // 2:
print('INVALID SIZE (is %d bytes, should be %d bytes)' % (codelen + 256, len(data) // 2))
ok = False
continue
elif len(data) / 2 > 512 * 1024 - 32 * 1024: # Firmware - header - signatures
print('TOO BIG')
ok = False
continue
else:
print('OK')
if model == '2':
vendorlen = struct.unpack('<I', binascii.unhexlify(data[8:16]))[0]
headerlen = struct.unpack('<I', binascii.unhexlify(data[8 + vendorlen * 2:16 + vendorlen * 2]))[0]
codelen = struct.unpack('<I', binascii.unhexlify(data[24 + vendorlen * 2:32 + vendorlen * 2]))[0]
if codelen + vendorlen + headerlen != len(data) // 2:
print('INVALID SIZE (is %d bytes, should be %d bytes)' % (codelen + vendorlen + headerlen, len(data) // 2))
ok = False
continue
else:
print('OK')
print()
return ok
if __name__ == '__main__':
ok = True
ok &= check_bridge()
ok &= check_firmware('1')
ok &= check_firmware('2')
if ok:
print('EVERYTHING OK')
exit(0)
else:
print('PROBLEMS FOUND')
exit(1)