1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00

build: refactor build_vendorheader

This commit is contained in:
Pavol Rusnak 2017-04-01 18:28:10 +02:00
parent e0fd890661
commit 6187a8a0c1
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
5 changed files with 45 additions and 91 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
_attic/
vendor/src/
emu.config
__pycache__

View File

@ -400,6 +400,9 @@ QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA
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
$(Q)$(OBJCOPY) -I binary -O elf32-littlearm -B arm \
--rename-section .data=.vendorheader,alloc,load,readonly,contents \

View File

@ -1 +1,2 @@
build/
vendorheader.bin

View File

@ -1,71 +1,15 @@
#!/usr/bin/env python3
from PIL import Image
import sys
import re
import struct
import zlib
import re
import binascii
import os
import imp
def process_rgb(w, h, pix):
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
png2toi = imp.load_source('png2toi', os.path.dirname(__file__) + '/png2toi')
# encode vendor name, add length byte and padding to multiple of 4
def vendorencode(vname):
def encodevendor(vname):
vbin = vname.encode('utf-8')
vbin = struct.pack('<B',len(vbin)) + vbin
vbin += b'\0' * (-len(vbin) & 3)
@ -81,10 +25,9 @@ def main():
print('Usage build_vendorheader key1hex,... m version vendorname vendorimage.png vendorimage.bin')
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])
(major,minor) = map(lambda x: int(x),
re.search('^(\d+)\.(\d+)$', sys.argv[3]).groups())
(vmajor, vminor) = [int(x) for x in sys.argv[3].split('.')]
vname = sys.argv[4]
ifn = sys.argv[5]
ofn = sys.argv[6]
@ -92,11 +35,11 @@ def main():
print('Must provide PNG file')
return 2
timeout = 0
vheader = b'TRZV' + struct.pack('<IIBBBB', 0, timeout, major, minor, m, len(keys))
expiry = 0
vheader = b'TRZV' + struct.pack('<IIBBBB', 0, expiry, vmajor, vminor, m, len(keys))
for k in keys:
vheader += k
vheader += vendorencode(vname) + process_image(ifn)
vheader += encodevendor(vname) + png2toi.process_image(ifn)
padding = 65 + (-len(vheader) - 65) & 511
vheader += b'\0' * padding

View File

@ -25,7 +25,7 @@ def process_grayscale(w, h, pix):
return data
def process_image(ifn):
def process_image(ifn, savefiles=False):
im = Image.open(ifn)
w, h = im.size
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
@ -53,28 +53,34 @@ def process_image(ifn):
zdata = z.compress(pixeldata) + z.flush()
zdata = zdata[2:-4] # strip header and checksum
with open(ofn, 'wb') as f:
if im.mode == 'RGB':
f.write(b'TOIf')
else:
f.write(b'TOIg')
f.write(struct.pack('<HH', w, h))
f.write(struct.pack('<I', len(zdata)))
f.write(zdata)
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
with open(ofn + '.h', 'wt') as f:
f.write('static const uint8_t toi_%s[] = {\n' % ifn[:-4])
if im.mode == 'RGB':
f.write(" 'T', 'O', 'I', 'f',\n")
else:
f.write(" 'T', 'O', 'I', 'g',\n")
f.write(' (uint16_t)%d, (uint16_t)%d,\n' % (w, h))
f.write(' (uint32_t)%d,\n' % len(zdata))
f.write(' ')
for b in zdata:
f.write(' 0x%02x,' % b)
f.write('\n};\n')
print('Written %s ...' % (ofn + '.h'))
data = b''
if im.mode == 'RGB':
data += b'TOIf'
else:
data += b'TOIg'
data += struct.pack('<HH', w, h)
data += struct.pack('<I', len(zdata))
data += 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:
f.write('static const uint8_t toi_%s[] = {\n' % ifn[:-4])
if im.mode == 'RGB':
f.write(" 'T', 'O', 'I', 'f',\n")
else:
f.write(" 'T', 'O', 'I', 'g',\n")
f.write(' (uint16_t)%d, (uint16_t)%d,\n' % (w, h))
f.write(' (uint32_t)%d,\n' % len(zdata))
f.write(' ')
for b in zdata:
f.write(' 0x%02x,' % b)
f.write('\n};\n')
print('Written %s ... done' % (ofn + '.h'))
return data
def main():
@ -87,7 +93,7 @@ def main():
print('Must provide PNG file')
return 2
process_image(ifn)
process_image(ifn, savefiles=True)
main()