2015-11-20 01:59:09 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-01-05 05:47:46 -05:00
|
|
|
# Copyright (C) 2016 Furrtek
|
2015-11-20 01:59:09 -05:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import struct
|
|
|
|
import md5
|
|
|
|
|
|
|
|
usage_message = """
|
|
|
|
Baseband processors modules package file generator
|
|
|
|
|
|
|
|
Usage: <command> <module_name> <module_name>...
|
|
|
|
"""
|
|
|
|
|
|
|
|
def read_image(path):
|
|
|
|
f = open(path, 'rb')
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
return data
|
|
|
|
|
|
|
|
def write_file(data, path):
|
|
|
|
f = open(path, 'wb')
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
print(usage_message)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
data = bytearray()
|
|
|
|
h_data = bytearray()
|
|
|
|
name = bytearray()
|
|
|
|
info = bytearray()
|
|
|
|
description = bytearray()
|
|
|
|
data_default_byte = bytearray((0,))
|
|
|
|
|
|
|
|
sys.argv = sys.argv[1:]
|
|
|
|
|
|
|
|
# Format for module file:
|
|
|
|
# Magic (4), Version (2), Length (4), Name (16), MD5 (16), Description (214)
|
2016-02-04 04:27:53 -05:00
|
|
|
# 0x00 pad bytes (256)
|
|
|
|
# Module binary (padded to 32768-16)
|
|
|
|
# MD5 (16) again, so that module code can read it (dirty...)
|
2015-11-20 01:59:09 -05:00
|
|
|
|
|
|
|
for args in sys.argv:
|
2016-04-28 08:59:14 -04:00
|
|
|
m = md5.new()
|
2015-11-20 01:59:09 -05:00
|
|
|
data = read_image(args + '/build/' + args + '.bin')
|
2016-01-05 14:17:55 -05:00
|
|
|
data_r = data
|
|
|
|
|
|
|
|
# Magic bytes
|
2015-11-20 01:59:09 -05:00
|
|
|
info = 'PPM '
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Version
|
2016-02-04 04:27:53 -05:00
|
|
|
info += struct.pack('H', 2)
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Length
|
2015-11-20 01:59:09 -05:00
|
|
|
info += struct.pack('I', len(data))
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Module name
|
2015-11-20 01:59:09 -05:00
|
|
|
name = read_image(args + '/name')
|
|
|
|
if len(name) > 16:
|
|
|
|
name = name[0:15]
|
|
|
|
pad_size = 16 - len(name)
|
|
|
|
name += (data_default_byte * pad_size)
|
|
|
|
info += name
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Module MD5 footprint
|
2015-11-20 01:59:09 -05:00
|
|
|
m.update(data)
|
|
|
|
digest = m.digest()
|
|
|
|
pad_size = 16 - len(digest)
|
|
|
|
digest += (data_default_byte * pad_size)
|
|
|
|
info += digest
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Module description
|
2015-11-20 01:59:09 -05:00
|
|
|
description = read_image(args + '/description')
|
|
|
|
if len(description) > 214:
|
|
|
|
description = description[0:213]
|
|
|
|
pad_size = 214 - len(description)
|
|
|
|
description += (data_default_byte * pad_size)
|
|
|
|
info += description
|
2016-01-05 14:17:55 -05:00
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
# Header padding to fit in SD card sector
|
|
|
|
info += (data_default_byte * 256)
|
|
|
|
|
|
|
|
# Binary padding
|
2015-11-20 01:59:09 -05:00
|
|
|
data = info + data
|
2016-02-04 04:27:53 -05:00
|
|
|
pad_size = (32768 + 512 - 16) - len(data)
|
2016-01-05 14:17:55 -05:00
|
|
|
data += (data_default_byte * pad_size)
|
|
|
|
data += digest
|
2015-11-20 01:59:09 -05:00
|
|
|
write_file(data, args + '.bin')
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Add to modules.h
|
2015-11-20 01:59:09 -05:00
|
|
|
md5sum = ''
|
|
|
|
for byte in digest:
|
|
|
|
md5sum += '0x' + format(byte, '02x') + ','
|
|
|
|
h_data += 'const char md5_' + args.replace('-','_') + '[16] = {' + md5sum + '};\n'
|
2016-01-05 14:17:55 -05:00
|
|
|
|
|
|
|
# Update original binary with MD5 footprint
|
2016-04-28 08:59:14 -04:00
|
|
|
write_file(data[512:(32768+512)], args + '/build/' + args + '_inc.bin')
|
2015-11-20 01:59:09 -05:00
|
|
|
|
2016-01-05 05:47:46 -05:00
|
|
|
write_file(h_data, 'common/modules.h')
|