b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/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 | # |
| 9 | import argparse |
| 10 | import pathlib |
| 11 | import socket |
| 12 | import struct |
| 13 | |
| 14 | MODEL_LEN = 20 |
| 15 | SIGNATURE_LEN = 16 |
| 16 | LINUXLOAD_LEN = 10 |
| 17 | BUFSIZE = 4096 |
| 18 | |
| 19 | parser = argparse.ArgumentParser(description='Generate Cameo firmware header.') |
| 20 | parser.add_argument('source_file', type=argparse.FileType('rb')) |
| 21 | parser.add_argument('dest_file', type=argparse.FileType('wb')) |
| 22 | parser.add_argument('model') |
| 23 | parser.add_argument('signature') |
| 24 | parser.add_argument('partition', type=int, choices=range(0,10), |
| 25 | metavar="partition=[0-9]",help="partition id") |
| 26 | parser.add_argument('customer_signature', type=int, choices=range(0,10), |
| 27 | metavar="customer_signature=[0-9]", |
| 28 | help="customer signature") |
| 29 | parser.add_argument('board_version', type=int, choices=range(0,2**32), |
| 30 | metavar="board_version=[0-4294967295]", |
| 31 | help="board version") |
| 32 | parser.add_argument('linux_loadaddr', nargs='?', |
| 33 | help="Kernel start address in 0xFFFFFFFF format") |
| 34 | args = parser.parse_args() |
| 35 | |
| 36 | if len(args.model) > MODEL_LEN: |
| 37 | raise ValueError(f"Model '{args.model}' is greater than {MODEL_LEN} bytes") |
| 38 | |
| 39 | if len(args.signature) > SIGNATURE_LEN: |
| 40 | raise ValueError(f"Signature '{args.signature}' is greater than" |
| 41 | f"{SIGNATURE_LEN} bytes") |
| 42 | |
| 43 | if 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'") |
| 54 | else: |
| 55 | args.linux_loadaddr = "" |
| 56 | |
| 57 | checksum = 0 |
| 58 | size = 0 |
| 59 | while 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 | |
| 66 | args.dest_file.write(struct.pack('!I', checksum)) |
| 67 | args.dest_file.write(struct.pack(f'{MODEL_LEN}s', |
| 68 | args.model.encode("ascii"))) |
| 69 | args.dest_file.write(struct.pack(f'{SIGNATURE_LEN}s', |
| 70 | args.signature.encode("ascii"))) |
| 71 | args.dest_file.write(struct.pack('!B', args.partition)) |
| 72 | args.dest_file.write(struct.pack('!B', 0x40)) # ??? This header size? |
| 73 | args.dest_file.write(struct.pack('!B', 0x00)) # ??? Encrypted? |
| 74 | args.dest_file.write(struct.pack('!B', args.customer_signature)) |
| 75 | args.dest_file.write(struct.pack('!I', args.board_version)) |
| 76 | args.dest_file.write(struct.pack('!I', size)) |
| 77 | args.dest_file.write(struct.pack(f'{LINUXLOAD_LEN}s', |
| 78 | args.linux_loadaddr.encode("ascii"))) |
| 79 | args.dest_file.write(struct.pack('!2x')) |
| 80 | |
| 81 | args.source_file.seek(0) |
| 82 | while True: |
| 83 | buf = args.source_file.read(BUFSIZE) |
| 84 | if not buf: |
| 85 | break |
| 86 | args.dest_file.write(buf) |