2020-09-16 08:20:52 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-07-31 11:10:48 +02:00
|
|
|
#
|
util/: Replace GPLv2 boiler plate with SPDX header
Used commands:
perl -i -p0e 's|\/\*[\s*]*.*is free software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and\/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-2.0-only */|' $(cat filelist)
perl -i -p0e 's|This[\s*]*program[\s*]*is[\s*]*free[\s*]*software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*either[\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License,[\s*]*or[\s*]*.at[\s*]*your[\s*]*option.*[\s*]*any[\s*]*later[\s*]*version.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-2.0-or-later */|' $(cat filelist)
perl -i -p0e 's|\/\*[\s*]*.*This[\s*#]*program[\s*#]*is[\s*#]*free[\s*#]*software[;:,][\s*#]*you[\s*#]*can[\s*#]*redistribute[\s*#]*it[\s*#]*and/or[\s*#]*modify[\s*#]*it[\s*#]*under[\s*#]*the[\s*#]*terms[\s*#]*of[\s*#]*the[\s*#]*GNU[\s*#]*General[\s*#]*Public[\s*#]*License[\s*#]*as[\s*#]*published[\s*#]*by[\s*#]*the[\s*#]*Free[\s*#]*Software[\s*#]*Foundation[;:,][\s*#]*either[\s*#]*version[\s*#]*3[\s*#]*of[\s*#]*the[\s*#]*License[;:,][\s*#]*or[\s*#]*.at[\s*#]*your[\s*#]*option.*[\s*#]*any[\s*#]*later[\s*#]*version.[\s*#]*This[\s*#]*program[\s*#]*is[\s*#]*distributed[\s*#]*in[\s*#]*the[\s*#]*hope[\s*#]*that[\s*#]*it[\s*#]*will[\s*#]*be[\s*#]*useful[;:,][\s*#]*but[\s*#]*WITHOUT[\s*#]*ANY[\s*#]*WARRANTY[;:,][\s*#]*without[\s*#]*even[\s*#]*the[\s*#]*implied[\s*#]*warranty[\s*#]*of[\s*#]*MERCHANTABILITY[\s*#]*or[\s*#]*FITNESS[\s*#]*FOR[\s*#]*A[\s*#]*PARTICULAR[\s*#]*PURPOSE.[\s*#]*See[\s*#]*the[\s*#]*GNU[\s*#]*General[\s*#]*Public[\s*#]*License[\s*#]*for[\s*#]*more[\s*#]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-3.0-or-later */|' $(cat filelist)
perl -i -p0e 's|(\#\#*)[\w]*.*is free software[:;][\#\s]*you[\#\s]*can[\#\s]*redistribute[\#\s]*it[\#\s]*and\/or[\#\s]*modify[\#\s]*it[\s\#]*under[\s \#]*the[\s\#]*terms[\s\#]*of[\s\#]*the[\s\#]*GNU[\s\#]*General[\s\#]*Public[\s\#]*License[\s\#]*as[\s\#]*published[\s\#]*by[\s\#]*the[\s\#]*Free[\s\#]*Software[\s\#]*Foundation[;,][\s\#]*version[\s\#]*2[\s\#]*of[\s\#]*the[\s\#]*License.*[\s\#]*This[\s\#]*program[\s\#]*is[\s\#]*distributed[\s\#]*in[\s\#]*the[\s\#]*hope[\s\#]*that[\s\#]*it[\s\#]*will[\#\s]*be[\#\s]*useful,[\#\s]*but[\#\s]*WITHOUT[\#\s]*ANY[\#\s]*WARRANTY;[\#\s]*without[\#\s]*even[\#\s]*the[\#\s]*implied[\#\s]*warranty[\#\s]*of[\#\s]*MERCHANTABILITY[\#\s]*or[\#\s]*FITNESS[\#\s]*FOR[\#\s]*A[\#\s]*PARTICULAR[\#\s]*PURPOSE.[\#\s]*See[\#\s]*the[\#\s]*GNU[\#\s]*General[\#\s]*Public[\#\s]*License[\#\s]*for[\#\s]*more[\#\s]*details.\s(#* *\n)*|\1 SPDX-License-Identifier: GPL-2.0-only\n\n|' $(cat filelist)
perl -i -p0e 's|(\#\#*)[\w*]*.*is free software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and\/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.\s(#* *\n)*|\1 SPDX-License-Identifier: GPL-2.0-only\n\n|' $(cat filelist)
Change-Id: I1008a63b804f355a916221ac994701d7584f60ff
Signed-off-by: Patrick Georgi <pgeorgi@google.com>
Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/41177
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2020-05-08 20:48:04 +02:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
2015-07-31 11:10:48 +02:00
|
|
|
|
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
def read(path):
|
2018-05-31 03:20:53 +02:00
|
|
|
with open(path, 'rb') as f:
|
2015-07-31 11:10:48 +02:00
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def write(path, data):
|
2018-05-31 03:20:53 +02:00
|
|
|
with open(path, 'wb') as f:
|
2015-07-31 11:10:48 +02:00
|
|
|
f.write(data)
|
|
|
|
|
2020-09-16 08:20:52 +02:00
|
|
|
def padding(data, size, pattern=b'\0'):
|
2015-07-31 11:10:48 +02:00
|
|
|
return data + pattern * (size - len(data))
|
|
|
|
|
2020-09-16 08:20:52 +02:00
|
|
|
def align(data, size, pattern=b'\0'):
|
2015-07-31 11:10:48 +02:00
|
|
|
return padding(data, (len(data) + (size - 1)) & ~(size - 1), pattern)
|
|
|
|
|
2018-05-31 03:20:53 +02:00
|
|
|
def gen_gfh_info(chip, data):
|
|
|
|
entries = {
|
|
|
|
'mt8173': 0x000C1000,
|
|
|
|
'mt8183': 0x00201000
|
|
|
|
}
|
|
|
|
|
|
|
|
gfh_format = '<44I'
|
|
|
|
gfh_size = struct.calcsize(gfh_format)
|
|
|
|
load_addr = entries[chip] - gfh_size
|
|
|
|
load_size = gfh_size + len(data) + hashlib.sha256().digest_size
|
|
|
|
|
|
|
|
gfh = struct.pack(gfh_format,
|
|
|
|
0x014d4d4d, 0x00000038, 0x454c4946, 0x464e495f,
|
|
|
|
0x0000004f, 0x00000001, 0x01050001, load_addr,
|
|
|
|
load_size, 0x00020000, 0x000000a8, 0x00000020,
|
|
|
|
0x000000B0, 0x00000001, 0x014d4d4d, 0x0001000c,
|
|
|
|
0x00000001, 0x034d4d4d, 0x00070064, 0x00001182,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00006400, 0x00001388,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000)
|
|
|
|
|
|
|
|
return gfh
|
2015-07-31 11:10:48 +02:00
|
|
|
|
|
|
|
def gen_emmc_header(data):
|
2020-09-16 08:20:52 +02:00
|
|
|
header = (padding(struct.pack('<12sII', b'EMMC_BOOT', 1, 512), 512, b'\xff') +
|
|
|
|
padding(struct.pack('<8sIIIIIIII', b'BRLYT', 1, 2048, 2048 + len(data),
|
|
|
|
0x42424242, 0x00010005, 2048, 2048 + len(data), 1) + b'\0' * 140, 512,
|
|
|
|
b'\xff') +
|
|
|
|
b'\0' * 1024)
|
2015-07-31 11:10:48 +02:00
|
|
|
return header
|
|
|
|
|
|
|
|
def gen_sf_header(data):
|
2020-09-16 08:20:52 +02:00
|
|
|
header = (padding(struct.pack('<12sII', b'SF_BOOT', 1, 512), 512, b'\xff') +
|
|
|
|
padding(struct.pack('<8sIIIIIIII', b'BRLYT', 1, 2048, 2048 + len(data),
|
|
|
|
0x42424242, 0x00010007, 2048, 2048 + len(data), 1) + b'\0' * 140, 512,
|
|
|
|
b'\xff') +
|
|
|
|
b'\0' * 1024)
|
2015-07-31 11:10:48 +02:00
|
|
|
return header
|
|
|
|
|
|
|
|
gen_dev_header = {
|
2018-05-31 03:20:53 +02:00
|
|
|
'emmc': gen_emmc_header,
|
|
|
|
'sf': gen_sf_header
|
2015-07-31 11:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def gen_preloader(chip_ver, flash_type, data):
|
2018-05-31 03:20:53 +02:00
|
|
|
gfh_info = gen_gfh_info(chip_ver, data)
|
2015-07-31 11:10:48 +02:00
|
|
|
gfh_hash = hashlib.sha256(gfh_info + data).digest()
|
|
|
|
|
2020-09-16 08:20:52 +02:00
|
|
|
data = align(gfh_info + data + gfh_hash, 512, b'\xff')
|
2015-07-31 11:10:48 +02:00
|
|
|
header = gen_dev_header[flash_type](data)
|
|
|
|
return header + data
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
if len(argv) != 5:
|
2020-09-16 08:20:52 +02:00
|
|
|
print('Usage: %s <chip> <flash_type> <input_file> <output_file>' % argv[0])
|
|
|
|
print('\t flash_type: emmc|sf')
|
|
|
|
print('\t chip : mt8173|mt8183')
|
2015-07-31 11:10:48 +02:00
|
|
|
|
|
|
|
exit(1)
|
|
|
|
write(argv[4], gen_preloader(argv[1], argv[2], read(argv[3])))
|
|
|
|
|
2018-05-31 03:20:53 +02:00
|
|
|
if __name__ == '__main__':
|
2015-07-31 11:10:48 +02:00
|
|
|
main(sys.argv)
|