coreboot-kgpe-d16/util/exynos/fixed_cksum.py
Yilin Yang 1502494cba util/exynos: Port *_cksum.py to python3
BUG=chromium:1023662
TEST=1. Create a tiny file `in.txt` as input
     2. Run `fixed_cksum.py in.txt out.txt 20` with py2 and py3 version,
     the output is the same
     3. Run `variable_cksum.py in.txt out.txt` with py2 and py3 version,
     the output is the same

Signed-off-by: Yilin Yang <kerker@google.com>
Change-Id: I9428269dfb826a3a95fffef9ea3f7c1a7107ef84
Reviewed-on: https://review.coreboot.org/c/coreboot/+/45460
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
2020-09-18 08:00:39 +00:00

36 lines
882 B
Python
Executable file

#!/usr/bin/env python3
#
# SPDX-License-Identifier: BSD-3-Clause
"""
This utility computes and fills Exynos ROM checksum (for BL1 or BL2).
(Algorithm from U-Boot: tools/mkexynosspl.c)
Input: IN OUT DATA_SIZE
Output:
IN padded out to DATA_SIZE, checksum at the end, written to OUT.
"""
import struct
import sys
def main(argv):
if len(argv) != 4:
exit('usage: %s IN OUT DATA_SIZE' % argv[0])
in_name, out_name = argv[1:3]
size = int(argv[3], 0)
checksum_format = "<I"
with open(in_name, "rb") as in_file, open(out_name, "wb") as out_file:
data = in_file.read()
checksum_size = struct.calcsize(checksum_format)
data_size = size - checksum_size
assert len(data) <= data_size
checksum = struct.pack(checksum_format, sum(data))
out_file.write(data + bytearray(data_size - len(data)) + checksum)
if __name__ == '__main__':
main(sys.argv)