Skip to content

Commit fcf8ef1

Browse files
committed
tool for padding/checksumming ax54g-payloads
1 parent 5361981 commit fcf8ef1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

ax54g-pad.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import struct
4+
import sys
5+
import zlib
6+
CKSUM_AX54G_FMT = struct.Struct(">I28x")
7+
8+
BOOTLOADER_SIZE = 0x80000-0x2000
9+
KERNEL_SIZE = 0x180000
10+
11+
12+
def copy_into(dst, src, start):
13+
for x in range(len(src)):
14+
dst[start+x] = src[x]
15+
16+
17+
def main(args):
18+
data = open(args.input, 'rb').read()
19+
s = CKSUM_AX54G_FMT.size
20+
if args.action == 'check':
21+
exp = CKSUM_AX54G_FMT.unpack(data[-s:])[0]
22+
got = zlib.adler32(data[:-s])
23+
ok = "OK" if exp == got else "FAIL"
24+
print("Checksum: {} expected 0x{:08x} got 0x{:08x}".format(ok, exp, got))
25+
elif args.action == 'pad':
26+
if len(data) in [KERNEL_SIZE, BOOTLOADER_SIZE]:
27+
print("Already padded")
28+
return
29+
size = BOOTLOADER_SIZE if len(data) < BOOTLOADER_SIZE else KERNEL_SIZE
30+
print("Assuming size 0x{:0x}".format(size))
31+
out = bytearray([0]*size)
32+
copy_into(out, data, 0)
33+
cksum = zlib.adler32(bytes(out[:-s]))
34+
CKSUM_AX54G_FMT.pack_into(out, size-s, cksum)
35+
open(args.output, 'wb').write(out)
36+
37+
elif args.action == 'unpad':
38+
if len(data) not in [KERNEL_SIZE, BOOTLOADER_SIZE]:
39+
print("Probably not padded")
40+
for x in range(len(data)-s-1, 0, -1):
41+
if data[x] != 0x00:
42+
print("{:08x}".format(x+1))
43+
open(args.output, 'wb').write(data[:x+1])
44+
break
45+
46+
47+
if __name__ == '__main__':
48+
parser = argparse.ArgumentParser()
49+
parser.add_argument(
50+
"-i", "--input", required=True, help="input file")
51+
parser.add_argument(
52+
"-o", "--output", required="check" not in sys.argv, help="output file")
53+
parser.add_argument(
54+
"-a", "--action",
55+
choices=['pad', 'unpad', 'check'],
56+
required=True,
57+
help="output file")
58+
main(parser.parse_args())

0 commit comments

Comments
 (0)