blob: 1627a79f17536cfa42a3b5bdfde405e0df588c6a [file] [log] [blame]
Yilin Yangf944e612020-09-16 14:20:52 +08001#!/usr/bin/env python3
Yidi Lin8e76f342015-07-31 17:10:48 +08002#
Patrick Georgi7333a112020-05-08 20:48:04 +02003# SPDX-License-Identifier: GPL-2.0-only
Yidi Lin8e76f342015-07-31 17:10:48 +08004
5import struct
6import sys
7import hashlib
8
9def read(path):
Tristan Shieh3ddf57e2018-05-31 09:20:53 +080010 with open(path, 'rb') as f:
Yidi Lin8e76f342015-07-31 17:10:48 +080011 return f.read()
12
13def write(path, data):
Tristan Shieh3ddf57e2018-05-31 09:20:53 +080014 with open(path, 'wb') as f:
Yidi Lin8e76f342015-07-31 17:10:48 +080015 f.write(data)
16
Yilin Yangf944e612020-09-16 14:20:52 +080017def padding(data, size, pattern=b'\0'):
Yidi Lin8e76f342015-07-31 17:10:48 +080018 return data + pattern * (size - len(data))
19
Yilin Yangf944e612020-09-16 14:20:52 +080020def align(data, size, pattern=b'\0'):
Yidi Lin8e76f342015-07-31 17:10:48 +080021 return padding(data, (len(data) + (size - 1)) & ~(size - 1), pattern)
22
Tristan Shieh3ddf57e2018-05-31 09:20:53 +080023def gen_gfh_info(chip, data):
24 entries = {
25 'mt8173': 0x000C1000,
26 'mt8183': 0x00201000
27 }
28
29 gfh_format = '<44I'
30 gfh_size = struct.calcsize(gfh_format)
31 load_addr = entries[chip] - gfh_size
32 load_size = gfh_size + len(data) + hashlib.sha256().digest_size
33
34 gfh = struct.pack(gfh_format,
35 0x014d4d4d, 0x00000038, 0x454c4946, 0x464e495f,
36 0x0000004f, 0x00000001, 0x01050001, load_addr,
37 load_size, 0x00020000, 0x000000a8, 0x00000020,
38 0x000000B0, 0x00000001, 0x014d4d4d, 0x0001000c,
39 0x00000001, 0x034d4d4d, 0x00070064, 0x00001182,
40 0x00000000, 0x00000000, 0x00000000, 0x00000000,
41 0x00000000, 0x00000000, 0x00000000, 0x00000000,
42 0x00000000, 0x00000000, 0x00000000, 0x00000000,
43 0x00000000, 0x00000000, 0x00000000, 0x00000000,
44 0x00000000, 0x00000000, 0x00006400, 0x00001388,
45 0x00000000, 0x00000000, 0x00000000, 0x00000000)
46
47 return gfh
Yidi Lin8e76f342015-07-31 17:10:48 +080048
49def gen_emmc_header(data):
Yilin Yangf944e612020-09-16 14:20:52 +080050 header = (padding(struct.pack('<12sII', b'EMMC_BOOT', 1, 512), 512, b'\xff') +
51 padding(struct.pack('<8sIIIIIIII', b'BRLYT', 1, 2048, 2048 + len(data),
52 0x42424242, 0x00010005, 2048, 2048 + len(data), 1) + b'\0' * 140, 512,
53 b'\xff') +
54 b'\0' * 1024)
Yidi Lin8e76f342015-07-31 17:10:48 +080055 return header
56
57def gen_sf_header(data):
Yilin Yangf944e612020-09-16 14:20:52 +080058 header = (padding(struct.pack('<12sII', b'SF_BOOT', 1, 512), 512, b'\xff') +
59 padding(struct.pack('<8sIIIIIIII', b'BRLYT', 1, 2048, 2048 + len(data),
60 0x42424242, 0x00010007, 2048, 2048 + len(data), 1) + b'\0' * 140, 512,
61 b'\xff') +
62 b'\0' * 1024)
Yidi Lin8e76f342015-07-31 17:10:48 +080063 return header
64
65gen_dev_header = {
Tristan Shieh3ddf57e2018-05-31 09:20:53 +080066 'emmc': gen_emmc_header,
67 'sf': gen_sf_header
Yidi Lin8e76f342015-07-31 17:10:48 +080068}
69
70def gen_preloader(chip_ver, flash_type, data):
Tristan Shieh3ddf57e2018-05-31 09:20:53 +080071 gfh_info = gen_gfh_info(chip_ver, data)
Yidi Lin8e76f342015-07-31 17:10:48 +080072 gfh_hash = hashlib.sha256(gfh_info + data).digest()
73
Yilin Yangf944e612020-09-16 14:20:52 +080074 data = align(gfh_info + data + gfh_hash, 512, b'\xff')
Yidi Lin8e76f342015-07-31 17:10:48 +080075 header = gen_dev_header[flash_type](data)
76 return header + data
77
78def main(argv):
79 if len(argv) != 5:
Yilin Yangf944e612020-09-16 14:20:52 +080080 print('Usage: %s <chip> <flash_type> <input_file> <output_file>' % argv[0])
81 print('\t flash_type: emmc|sf')
82 print('\t chip : mt8173|mt8183')
Yidi Lin8e76f342015-07-31 17:10:48 +080083
84 exit(1)
85 write(argv[4], gen_preloader(argv[1], argv[2], read(argv[3])))
86
Tristan Shieh3ddf57e2018-05-31 09:20:53 +080087if __name__ == '__main__':
Yidi Lin8e76f342015-07-31 17:10:48 +080088 main(sys.argv)