src/northbridge: Remove whitespace after sizeof
[coreboot.git] / util / mtkheader / gen-bl-img.py
blob361912f637e288dde18516693134029f2ed7d0c6
1 #!/usr/bin/env python2
4 # This file is part of the coreboot project.
6 # Copyright (c) 2015 MediaTek Inc.
7 # Author: Tristan Shieh <tristan.shieh@mediatek.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
20 import struct
21 import sys
22 import hashlib
24 def read(path):
25 with open(path, "rb") as f:
26 return f.read()
28 def write(path, data):
29 with open(path, "wb") as f:
30 f.write(data)
32 def padding(data, size, pattern = '\0'):
33 return data + pattern * (size - len(data))
35 def align(data, size, pattern = '\0'):
36 return padding(data, (len(data) + (size - 1)) & ~(size - 1), pattern)
38 gfh_infos = {
39 'mt8173': struct.pack("44I",
40 0x014d4d4d, 0x00000038, 0x454c4946, 0x464e495f,
41 0x0000004f, 0x00000001, 0x01050001, 0x000C0f50,
42 0xffffffff, 0x00020000, 0x000000a8, 0x00000020,
43 0x000000B0, 0x00000001, 0x014d4d4d, 0x0001000c,
44 0x00000001, 0x034d4d4d, 0x00070064, 0x00001182,
45 0x00000000, 0x00000000, 0x00000000, 0x00000000,
46 0x00000000, 0x00000000, 0x00000000, 0x00000000,
47 0x00000000, 0x00000000, 0x00000000, 0x00000000,
48 0x00000000, 0x00000000, 0x00000000, 0x00000000,
49 0x00000000, 0x00000000, 0x00006400, 0x00001388,
50 0x00000000, 0x00000000, 0x00000000, 0x00000000,
53 def gen_emmc_header(data):
54 header = (padding(struct.pack("<12sII", "EMMC_BOOT", 1, 512), 512, '\xff') +
55 padding(struct.pack("<8sIIIIIIII", "BRLYT", 1, 2048, 2048 + len(data),
56 0x42424242, 0x00010005, 2048, 2048 + len(data), 1) + '\0' * 140, 512, '\xff') +
57 '\0' * 1024)
58 return header
60 def gen_sf_header(data):
61 header = (padding(struct.pack("<12sII", "SF_BOOT", 1, 512), 512, '\xff') +
62 padding(struct.pack("<8sIIIIIIII", "BRLYT", 1, 2048, 2048 + len(data),
63 0x42424242, 0x00010007, 2048, 2048 + len(data), 1) + '\0' * 140, 512, '\xff') +
64 '\0' * 1024)
65 return header
67 gen_dev_header = {
68 "emmc": gen_emmc_header,
69 "sf": gen_sf_header
72 def gen_preloader(chip_ver, flash_type, data):
73 gfh_info = gfh_infos[chip_ver]
74 gfh_info = gfh_info[0:32] + struct.pack("1I", len(data)+len(gfh_info)+32) + gfh_info[36:len(gfh_info)]
76 gfh_hash = hashlib.sha256(gfh_info + data).digest()
78 data = align(gfh_info + data + gfh_hash, 512, '\xff')
79 header = gen_dev_header[flash_type](data)
80 return header + data
82 def main(argv):
83 if len(argv) != 5:
84 print "Usage: %s <chip> <flash_type> <input_file> <output_file>" % argv[0]
85 print "\t flash_type: emmc/sf"
86 print "\t chip : mt8173"
88 exit(1)
89 write(argv[4], gen_preloader(argv[1], argv[2], read(argv[3])))
91 if __name__ == "__main__":
92 main(sys.argv)