mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-27 08:38:07 +00:00
build: refactor build_vendorheader
This commit is contained in:
parent
e0fd890661
commit
6187a8a0c1
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
_attic/
|
_attic/
|
||||||
vendor/src/
|
vendor/src/
|
||||||
emu.config
|
emu.config
|
||||||
|
__pycache__
|
||||||
|
@ -400,6 +400,9 @@ QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA
|
|||||||
|
|
||||||
all: $(BUILD)/$(TARGET).bin
|
all: $(BUILD)/$(TARGET).bin
|
||||||
|
|
||||||
|
$(SRCDIR_FW)/firmware/vendorheader.bin: assets/satoshilabs.png
|
||||||
|
./tools/build_vendorheader '0000000000000000000000000000000000000000000000000000000000000000,0000000000000000000000000000000000000000000000000000000000000000,0000000000000000000000000000000000000000000000000000000000000000' 2 1.1 SatoshiLabs assets/satoshilabs.png micropython/firmware/vendorheader.bin
|
||||||
|
|
||||||
$(BUILD_FW)/firmware/vendorheader.o: $(SRCDIR_FW)/firmware/vendorheader.bin
|
$(BUILD_FW)/firmware/vendorheader.o: $(SRCDIR_FW)/firmware/vendorheader.bin
|
||||||
$(Q)$(OBJCOPY) -I binary -O elf32-littlearm -B arm \
|
$(Q)$(OBJCOPY) -I binary -O elf32-littlearm -B arm \
|
||||||
--rename-section .data=.vendorheader,alloc,load,readonly,contents \
|
--rename-section .data=.vendorheader,alloc,load,readonly,contents \
|
||||||
|
1
micropython/firmware/.gitignore
vendored
1
micropython/firmware/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
build/
|
build/
|
||||||
|
vendorheader.bin
|
||||||
|
@ -1,71 +1,15 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from PIL import Image
|
|
||||||
import sys
|
import sys
|
||||||
import re
|
|
||||||
import struct
|
import struct
|
||||||
import zlib
|
import re
|
||||||
import binascii
|
import binascii
|
||||||
|
import os
|
||||||
|
import imp
|
||||||
|
|
||||||
def process_rgb(w, h, pix):
|
png2toi = imp.load_source('png2toi', os.path.dirname(__file__) + '/png2toi')
|
||||||
data = bytes()
|
|
||||||
for j in range(h):
|
|
||||||
for i in range(w):
|
|
||||||
r, g, b = pix[i, j]
|
|
||||||
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
|
|
||||||
data += struct.pack('>H', c)
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def process_grayscale(w, h, pix):
|
|
||||||
data = bytes()
|
|
||||||
for j in range(h):
|
|
||||||
for i in range(w // 2):
|
|
||||||
l1, l2 = pix[i * 2, j], pix[i * 2 + 1, j]
|
|
||||||
c = (l1 & 0xF0) | (l2 >> 4)
|
|
||||||
data += struct.pack('>B', c)
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def process_image(ifn):
|
|
||||||
im = Image.open(ifn)
|
|
||||||
w, h = im.size
|
|
||||||
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
|
|
||||||
|
|
||||||
if im.mode == 'RGB':
|
|
||||||
print('Detected RGB mode')
|
|
||||||
elif im.mode == 'L':
|
|
||||||
if w % 2 > 0:
|
|
||||||
print('PNG file must have width divisible by 2')
|
|
||||||
return 3
|
|
||||||
print('Detected GRAYSCALE mode')
|
|
||||||
else:
|
|
||||||
print('Unknown mode:', im.mode)
|
|
||||||
return 4
|
|
||||||
|
|
||||||
pix = im.load()
|
|
||||||
|
|
||||||
if im.mode == 'RGB':
|
|
||||||
ofn = '%s.toif' % ifn[:-4]
|
|
||||||
pixeldata = process_rgb(w, h, pix)
|
|
||||||
else:
|
|
||||||
ofn = '%s.toig' % ifn[:-4]
|
|
||||||
pixeldata = process_grayscale(w, h, pix)
|
|
||||||
z = zlib.compressobj(level=9, wbits=10)
|
|
||||||
zdata = z.compress(pixeldata) + z.flush()
|
|
||||||
zdata = zdata[2:-4] # strip header and checksum
|
|
||||||
|
|
||||||
toif = b''
|
|
||||||
if im.mode == 'RGB':
|
|
||||||
toif += b'TOIf'
|
|
||||||
else:
|
|
||||||
toif += b'TOIg'
|
|
||||||
toif += struct.pack('<HH', w, h)
|
|
||||||
toif += struct.pack('<I', len(zdata))
|
|
||||||
toif += zdata
|
|
||||||
return toif
|
|
||||||
|
|
||||||
# encode vendor name, add length byte and padding to multiple of 4
|
# encode vendor name, add length byte and padding to multiple of 4
|
||||||
def vendorencode(vname):
|
def encodevendor(vname):
|
||||||
vbin = vname.encode('utf-8')
|
vbin = vname.encode('utf-8')
|
||||||
vbin = struct.pack('<B',len(vbin)) + vbin
|
vbin = struct.pack('<B',len(vbin)) + vbin
|
||||||
vbin += b'\0' * (-len(vbin) & 3)
|
vbin += b'\0' * (-len(vbin) & 3)
|
||||||
@ -81,10 +25,9 @@ def main():
|
|||||||
print('Usage build_vendorheader key1hex,... m version vendorname vendorimage.png vendorimage.bin')
|
print('Usage build_vendorheader key1hex,... m version vendorname vendorimage.png vendorimage.bin')
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
keys = list(map(encodekey, sys.argv[1].split(',')))
|
keys = [encodekey(x) for x in sys.argv[1].split(',')]
|
||||||
m = int(sys.argv[2])
|
m = int(sys.argv[2])
|
||||||
(major,minor) = map(lambda x: int(x),
|
(vmajor, vminor) = [int(x) for x in sys.argv[3].split('.')]
|
||||||
re.search('^(\d+)\.(\d+)$', sys.argv[3]).groups())
|
|
||||||
vname = sys.argv[4]
|
vname = sys.argv[4]
|
||||||
ifn = sys.argv[5]
|
ifn = sys.argv[5]
|
||||||
ofn = sys.argv[6]
|
ofn = sys.argv[6]
|
||||||
@ -92,11 +35,11 @@ def main():
|
|||||||
print('Must provide PNG file')
|
print('Must provide PNG file')
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
timeout = 0
|
expiry = 0
|
||||||
vheader = b'TRZV' + struct.pack('<IIBBBB', 0, timeout, major, minor, m, len(keys))
|
vheader = b'TRZV' + struct.pack('<IIBBBB', 0, expiry, vmajor, vminor, m, len(keys))
|
||||||
for k in keys:
|
for k in keys:
|
||||||
vheader += k
|
vheader += k
|
||||||
vheader += vendorencode(vname) + process_image(ifn)
|
vheader += encodevendor(vname) + png2toi.process_image(ifn)
|
||||||
padding = 65 + (-len(vheader) - 65) & 511
|
padding = 65 + (-len(vheader) - 65) & 511
|
||||||
vheader += b'\0' * padding
|
vheader += b'\0' * padding
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ def process_grayscale(w, h, pix):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def process_image(ifn):
|
def process_image(ifn, savefiles=False):
|
||||||
im = Image.open(ifn)
|
im = Image.open(ifn)
|
||||||
w, h = im.size
|
w, h = im.size
|
||||||
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
|
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
|
||||||
@ -53,15 +53,19 @@ def process_image(ifn):
|
|||||||
zdata = z.compress(pixeldata) + z.flush()
|
zdata = z.compress(pixeldata) + z.flush()
|
||||||
zdata = zdata[2:-4] # strip header and checksum
|
zdata = zdata[2:-4] # strip header and checksum
|
||||||
|
|
||||||
with open(ofn, 'wb') as f:
|
data = b''
|
||||||
if im.mode == 'RGB':
|
if im.mode == 'RGB':
|
||||||
f.write(b'TOIf')
|
data += b'TOIf'
|
||||||
else:
|
else:
|
||||||
f.write(b'TOIg')
|
data += b'TOIg'
|
||||||
f.write(struct.pack('<HH', w, h))
|
data += struct.pack('<HH', w, h)
|
||||||
f.write(struct.pack('<I', len(zdata)))
|
data += struct.pack('<I', len(zdata))
|
||||||
f.write(zdata)
|
data += zdata
|
||||||
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
|
|
||||||
|
if savefiles:
|
||||||
|
with open(ofn, 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
print('Written %s ... %d bytes' % (ofn, len(data)))
|
||||||
with open(ofn + '.h', 'wt') as f:
|
with open(ofn + '.h', 'wt') as f:
|
||||||
f.write('static const uint8_t toi_%s[] = {\n' % ifn[:-4])
|
f.write('static const uint8_t toi_%s[] = {\n' % ifn[:-4])
|
||||||
if im.mode == 'RGB':
|
if im.mode == 'RGB':
|
||||||
@ -74,7 +78,9 @@ def process_image(ifn):
|
|||||||
for b in zdata:
|
for b in zdata:
|
||||||
f.write(' 0x%02x,' % b)
|
f.write(' 0x%02x,' % b)
|
||||||
f.write('\n};\n')
|
f.write('\n};\n')
|
||||||
print('Written %s ...' % (ofn + '.h'))
|
print('Written %s ... done' % (ofn + '.h'))
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -87,7 +93,7 @@ def main():
|
|||||||
print('Must provide PNG file')
|
print('Must provide PNG file')
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
process_image(ifn)
|
process_image(ifn, savefiles=True)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user