blob: 1f8292b90a4c12cdc68d2b0e826d2918b509e539 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/usr/bin/python3
2# SPDX-License-Identifier: GPL-2.0-or-later
3#
4# Copyright (C) 2022 Luiz Angelo Daros de Luca <luizluca@gmail.com>
5#
6# Cameo Image header geneator, used by some D-Link DGS-1210 switches
7# and APRESIA ApresiaLightGS series
8#
9import argparse
10import pathlib
11import socket
12import struct
13
14MODEL_LEN = 20
15SIGNATURE_LEN = 16
16LINUXLOAD_LEN = 10
17BUFSIZE = 4096
18
19parser = argparse.ArgumentParser(description='Generate Cameo firmware header.')
20parser.add_argument('source_file', type=argparse.FileType('rb'))
21parser.add_argument('dest_file', type=argparse.FileType('wb'))
22parser.add_argument('model')
23parser.add_argument('signature')
24parser.add_argument('partition', type=int, choices=range(0,10),
25 metavar="partition=[0-9]",help="partition id")
26parser.add_argument('customer_signature', type=int, choices=range(0,10),
27 metavar="customer_signature=[0-9]",
28 help="customer signature")
29parser.add_argument('board_version', type=int, choices=range(0,2**32),
30 metavar="board_version=[0-4294967295]",
31 help="board version")
32parser.add_argument('linux_loadaddr', nargs='?',
33 help="Kernel start address in 0xFFFFFFFF format")
34args = parser.parse_args()
35
36if len(args.model) > MODEL_LEN:
37 raise ValueError(f"Model '{args.model}' is greater than {MODEL_LEN} bytes")
38
39if len(args.signature) > SIGNATURE_LEN:
40 raise ValueError(f"Signature '{args.signature}' is greater than"
41 f"{SIGNATURE_LEN} bytes")
42
43if args.signature == "os":
44 if args.linux_loadaddr:
45 if len(args.linux_loadaddr) > LINUXLOAD_LEN:
46 raise ValueError(f"linux_loadaddr '{args.linux_loadaddr}' is greater"
47 f"than {LINUXLOAD_LEN} bytes")
48 if (args.linux_loadaddr[0:2] != "0x"):
49 raise ValueError(f"linux_loadaddr '{args.linux_loadaddr}' must use"
50 f"the 0x789ABCDE format")
51 int(args.linux_loadaddr[2:],16)
52 else:
53 raise ValueError(f"linux_loadaddr is required for signature 'os'")
54else:
55 args.linux_loadaddr = ""
56
57checksum = 0
58size = 0
59while True:
60 buf = args.source_file.read(BUFSIZE)
61 if not buf:
62 break
63 checksum = sum(iter(buf),checksum) % (1<<32)
64 size += len(buf)
65
66args.dest_file.write(struct.pack('!I', checksum))
67args.dest_file.write(struct.pack(f'{MODEL_LEN}s',
68 args.model.encode("ascii")))
69args.dest_file.write(struct.pack(f'{SIGNATURE_LEN}s',
70 args.signature.encode("ascii")))
71args.dest_file.write(struct.pack('!B', args.partition))
72args.dest_file.write(struct.pack('!B', 0x40)) # ??? This header size?
73args.dest_file.write(struct.pack('!B', 0x00)) # ??? Encrypted?
74args.dest_file.write(struct.pack('!B', args.customer_signature))
75args.dest_file.write(struct.pack('!I', args.board_version))
76args.dest_file.write(struct.pack('!I', size))
77args.dest_file.write(struct.pack(f'{LINUXLOAD_LEN}s',
78 args.linux_loadaddr.encode("ascii")))
79args.dest_file.write(struct.pack('!2x'))
80
81args.source_file.seek(0)
82while True:
83 buf = args.source_file.read(BUFSIZE)
84 if not buf:
85 break
86 args.dest_file.write(buf)