1
0

cameo-imghdr.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. MODEL_LEN = 20
  14. SIGNATURE_LEN = 16
  15. LINUXLOAD_LEN = 10
  16. BUFSIZE = 4096
  17. parser = argparse.ArgumentParser(description='Generate Cameo firmware header.')
  18. parser.add_argument('source_file', type=argparse.FileType('rb'))
  19. parser.add_argument('dest_file', type=argparse.FileType('wb'))
  20. parser.add_argument('model')
  21. parser.add_argument('signature')
  22. parser.add_argument('partition', type=int, choices=range(0,10),
  23. metavar="partition=[0-9]",help="partition id")
  24. parser.add_argument('customer_signature', type=int, choices=range(0,10),
  25. metavar="customer_signature=[0-9]",
  26. help="customer signature")
  27. parser.add_argument('board_version', type=int, choices=range(0,2**32),
  28. metavar="board_version=[0-4294967295]",
  29. help="board version")
  30. parser.add_argument('linux_loadaddr', nargs='?',
  31. help="Kernel start address in 0xFFFFFFFF format")
  32. args = parser.parse_args()
  33. if len(args.model) > MODEL_LEN:
  34. raise ValueError(f"Model '{args.model}' is greater than {MODEL_LEN} bytes")
  35. if len(args.signature) > SIGNATURE_LEN:
  36. raise ValueError(f"Signature '{args.signature}' is greater than"
  37. f"{SIGNATURE_LEN} bytes")
  38. if args.signature == "os":
  39. if args.linux_loadaddr:
  40. if len(args.linux_loadaddr) > LINUXLOAD_LEN:
  41. raise ValueError(f"linux_loadaddr '{args.linux_loadaddr}' is greater"
  42. f"than {LINUXLOAD_LEN} bytes")
  43. if (args.linux_loadaddr[0:2] != "0x"):
  44. raise ValueError(f"linux_loadaddr '{args.linux_loadaddr}' must use"
  45. f"the 0x789ABCDE format")
  46. int(args.linux_loadaddr[2:],16)
  47. else:
  48. raise ValueError(f"linux_loadaddr is required for signature 'os'")
  49. else:
  50. args.linux_loadaddr = ""
  51. checksum = 0
  52. size = 0
  53. while True:
  54. buf = args.source_file.read(BUFSIZE)
  55. if not buf:
  56. break
  57. checksum = sum(iter(buf),checksum) % (1<<32)
  58. size += len(buf)
  59. args.dest_file.write(struct.pack('!I', checksum))
  60. args.dest_file.write(struct.pack(f'{MODEL_LEN}s',
  61. args.model.encode("ascii")))
  62. args.dest_file.write(struct.pack(f'{SIGNATURE_LEN}s',
  63. args.signature.encode("ascii")))
  64. args.dest_file.write(struct.pack('!B', args.partition))
  65. args.dest_file.write(struct.pack('!B', 0x40)) # ??? This header size?
  66. args.dest_file.write(struct.pack('!B', 0x00)) # ??? Encrypted?
  67. args.dest_file.write(struct.pack('!B', args.customer_signature))
  68. args.dest_file.write(struct.pack('!I', args.board_version))
  69. args.dest_file.write(struct.pack('!I', size))
  70. args.dest_file.write(struct.pack(f'{LINUXLOAD_LEN}s',
  71. args.linux_loadaddr.encode("ascii")))
  72. args.dest_file.write(struct.pack('!2x'))
  73. args.source_file.seek(0)
  74. while True:
  75. buf = args.source_file.read(BUFSIZE)
  76. if not buf:
  77. break
  78. args.dest_file.write(buf)