sercomm-pid.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. """
  3. # SPDX-License-Identifier: GPL-2.0-or-later
  4. #
  5. # sercomm-pid.py: Creates Sercomm device PID
  6. #
  7. # Copyright © 2022 Mikhail Zhilkin
  8. """
  9. import argparse
  10. import binascii
  11. import struct
  12. PID_SIZE = 0x70
  13. PADDING = 0x30
  14. PADDING_TAIL = 0x0
  15. def auto_int(x):
  16. return int(x, 0)
  17. def create_pid_file(args):
  18. pid_file = open(args.pid_file, "wb")
  19. buf = get_pid(args)
  20. pid_file.write(buf)
  21. pid_file.close()
  22. def get_pid(args):
  23. buf = bytearray([PADDING] * PID_SIZE)
  24. enc = args.hw_version.rjust(8, '0').encode('ascii')
  25. struct.pack_into('>8s', buf, 0x0, enc)
  26. enc = binascii.hexlify(args.hw_id.encode())
  27. struct.pack_into('>6s', buf, 0x8, enc)
  28. enc = args.sw_version.rjust(4, '0').encode('ascii')
  29. struct.pack_into('>4s', buf, 0x64, enc)
  30. if (args.extra_padd_size):
  31. tail = bytearray([PADDING_TAIL] * args.extra_padd_size)
  32. if (args.extra_padd_byte):
  33. struct.pack_into ('<i', tail, 0x0,
  34. args.extra_padd_byte)
  35. buf += tail
  36. return buf
  37. def main():
  38. global args
  39. parser = argparse.ArgumentParser(description='This script \
  40. generates firmware PID for the Sercomm-based devices')
  41. parser.add_argument('--hw-version',
  42. dest='hw_version',
  43. action='store',
  44. type=str,
  45. help='Sercomm hardware version')
  46. parser.add_argument('--hw-id',
  47. dest='hw_id',
  48. action='store',
  49. type=str,
  50. help='Sercomm hardware ID')
  51. parser.add_argument('--sw-version',
  52. dest='sw_version',
  53. action='store',
  54. type=str,
  55. help='Sercomm software version')
  56. parser.add_argument('--pid-file',
  57. dest='pid_file',
  58. action='store',
  59. type=str,
  60. help='Output PID file')
  61. parser.add_argument('--extra-padding-size',
  62. dest='extra_padd_size',
  63. action='store',
  64. type=auto_int,
  65. help='Size of extra NULL padding at the end of the file \
  66. (optional)')
  67. parser.add_argument('--extra-padding-first-byte',
  68. dest='extra_padd_byte',
  69. action='store',
  70. type=auto_int,
  71. help='First byte of extra padding (optional)')
  72. args = parser.parse_args()
  73. if ((not args.hw_version) or
  74. (not args.hw_id) or
  75. (not args.sw_version) or
  76. (not args.pid_file)):
  77. parser.print_help()
  78. exit()
  79. create_pid_file(args)
  80. main()