Nico Huber | ca41a6a | 2016-01-23 21:29:47 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # This file is part of the coreboot project. |
| 5 | # |
| 6 | # Copyright (c) 2015 MediaTek Inc. |
| 7 | # Author: Tristan Shieh <tristan.shieh@mediatek.com> |
| 8 | # |
| 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. |
| 12 | # |
| 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. |
| 17 | # |
| 18 | |
| 19 | |
| 20 | import struct |
| 21 | import sys |
| 22 | import hashlib |
| 23 | |
| 24 | def read(path): |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 25 | with open(path, 'rb') as f: |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 26 | return f.read() |
| 27 | |
| 28 | def write(path, data): |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 29 | with open(path, 'wb') as f: |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 30 | f.write(data) |
| 31 | |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 32 | def padding(data, size, pattern='\0'): |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 33 | return data + pattern * (size - len(data)) |
| 34 | |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 35 | def align(data, size, pattern='\0'): |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 36 | return padding(data, (len(data) + (size - 1)) & ~(size - 1), pattern) |
| 37 | |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 38 | def gen_gfh_info(chip, data): |
| 39 | entries = { |
| 40 | 'mt8173': 0x000C1000, |
| 41 | 'mt8183': 0x00201000 |
| 42 | } |
| 43 | |
| 44 | gfh_format = '<44I' |
| 45 | gfh_size = struct.calcsize(gfh_format) |
| 46 | load_addr = entries[chip] - gfh_size |
| 47 | load_size = gfh_size + len(data) + hashlib.sha256().digest_size |
| 48 | |
| 49 | gfh = struct.pack(gfh_format, |
| 50 | 0x014d4d4d, 0x00000038, 0x454c4946, 0x464e495f, |
| 51 | 0x0000004f, 0x00000001, 0x01050001, load_addr, |
| 52 | load_size, 0x00020000, 0x000000a8, 0x00000020, |
| 53 | 0x000000B0, 0x00000001, 0x014d4d4d, 0x0001000c, |
| 54 | 0x00000001, 0x034d4d4d, 0x00070064, 0x00001182, |
| 55 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 56 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 57 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 58 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 59 | 0x00000000, 0x00000000, 0x00006400, 0x00001388, |
| 60 | 0x00000000, 0x00000000, 0x00000000, 0x00000000) |
| 61 | |
| 62 | return gfh |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 63 | |
| 64 | def gen_emmc_header(data): |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 65 | header = (padding(struct.pack('<12sII', 'EMMC_BOOT', 1, 512), 512, '\xff') + |
| 66 | padding(struct.pack('<8sIIIIIIII', 'BRLYT', 1, 2048, 2048 + len(data), |
| 67 | 0x42424242, 0x00010005, 2048, 2048 + len(data), 1) + '\0' * 140, 512, |
| 68 | '\xff') + |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 69 | '\0' * 1024) |
| 70 | return header |
| 71 | |
| 72 | def gen_sf_header(data): |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 73 | header = (padding(struct.pack('<12sII', 'SF_BOOT', 1, 512), 512, '\xff') + |
| 74 | padding(struct.pack('<8sIIIIIIII', 'BRLYT', 1, 2048, 2048 + len(data), |
| 75 | 0x42424242, 0x00010007, 2048, 2048 + len(data), 1) + '\0' * 140, 512, |
| 76 | '\xff') + |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 77 | '\0' * 1024) |
| 78 | return header |
| 79 | |
| 80 | gen_dev_header = { |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 81 | 'emmc': gen_emmc_header, |
| 82 | 'sf': gen_sf_header |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | def gen_preloader(chip_ver, flash_type, data): |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 86 | gfh_info = gen_gfh_info(chip_ver, data) |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 87 | gfh_hash = hashlib.sha256(gfh_info + data).digest() |
| 88 | |
| 89 | data = align(gfh_info + data + gfh_hash, 512, '\xff') |
| 90 | header = gen_dev_header[flash_type](data) |
| 91 | return header + data |
| 92 | |
| 93 | def main(argv): |
| 94 | if len(argv) != 5: |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 95 | print 'Usage: %s <chip> <flash_type> <input_file> <output_file>' % argv[0] |
| 96 | print '\t flash_type: emmc|sf' |
| 97 | print '\t chip : mt8173|mt8183' |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 98 | |
| 99 | exit(1) |
| 100 | write(argv[4], gen_preloader(argv[1], argv[2], read(argv[3]))) |
| 101 | |
Tristan Shieh | 3ddf57e | 2018-05-31 09:20:53 +0800 | [diff] [blame] | 102 | if __name__ == '__main__': |
Yidi Lin | 8e76f34 | 2015-07-31 17:10:48 +0800 | [diff] [blame] | 103 | main(sys.argv) |