Yilin Yang | 1502494 | 2020-09-17 09:14:11 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Gabe Black | b6b1077 | 2013-12-08 12:48:45 -0800 | [diff] [blame] | 2 | # |
Patrick Georgi | 7051707 | 2020-05-10 18:47:05 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: BSD-3-Clause |
Gabe Black | b6b1077 | 2013-12-08 12:48:45 -0800 | [diff] [blame] | 4 | |
| 5 | """ |
| 6 | This utility computes and fills Exynos ROM checksum (for BL1 or BL2). |
| 7 | (Algorithm from U-Boot: tools/mkexynosspl.c) |
| 8 | |
| 9 | Input: IN OUT DATA_SIZE |
| 10 | |
| 11 | Output: |
| 12 | |
| 13 | IN padded out to DATA_SIZE, checksum at the end, written to OUT. |
| 14 | """ |
| 15 | |
| 16 | import struct |
| 17 | import sys |
| 18 | |
| 19 | def main(argv): |
| 20 | if len(argv) != 4: |
| 21 | exit('usage: %s IN OUT DATA_SIZE' % argv[0]) |
| 22 | |
| 23 | in_name, out_name = argv[1:3] |
| 24 | size = int(argv[3], 0) |
| 25 | checksum_format = "<I" |
| 26 | with open(in_name, "rb") as in_file, open(out_name, "wb") as out_file: |
| 27 | data = in_file.read() |
| 28 | checksum_size = struct.calcsize(checksum_format) |
| 29 | data_size = size - checksum_size |
| 30 | assert len(data) <= data_size |
Yilin Yang | 1502494 | 2020-09-17 09:14:11 +0800 | [diff] [blame] | 31 | checksum = struct.pack(checksum_format, sum(data)) |
Gabe Black | b6b1077 | 2013-12-08 12:48:45 -0800 | [diff] [blame] | 32 | out_file.write(data + bytearray(data_size - len(data)) + checksum) |
| 33 | |
| 34 | |
| 35 | if __name__ == '__main__': |
| 36 | main(sys.argv) |