summaryrefslogtreecommitdiffstats
path: root/util/exynos/fixed_cksum.py
blob: 0ae7e4f6f8c9cea982927f43937447c8448ac1be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/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)