blob: 79a206a5c1e80e58bea409bca07762f05e4afbd8 [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
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(map(ord, data)))
out_file.write(data + bytearray(data_size - len(data)) + checksum)
if __name__ == '__main__':
main(sys.argv)