b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | |
| 10 | import argparse |
| 11 | import binascii |
| 12 | import struct |
| 13 | |
| 14 | PID_SIZE = 0x70 |
| 15 | PADDING = 0x30 |
| 16 | PADDING_TAIL = 0x0 |
| 17 | |
| 18 | def auto_int(x): |
| 19 | return int(x, 0) |
| 20 | |
| 21 | def create_pid_file(args): |
| 22 | pid_file = open(args.pid_file, "wb") |
| 23 | buf = get_pid(args) |
| 24 | pid_file.write(buf) |
| 25 | pid_file.close() |
| 26 | |
| 27 | def get_pid(args): |
| 28 | buf = bytearray([PADDING] * PID_SIZE) |
| 29 | |
| 30 | if not args.hw_id: |
| 31 | enc = args.hw_version.rjust(14, '0').encode('ascii') |
| 32 | struct.pack_into('>14s', buf, 0x0, enc) |
| 33 | else: |
| 34 | enc = args.hw_version.rjust(8, '0').encode('ascii') |
| 35 | struct.pack_into('>8s', buf, 0x0, enc) |
| 36 | |
| 37 | enc = binascii.hexlify(args.hw_id.encode()).upper() |
| 38 | struct.pack_into('>6s', buf, 0x8, enc) |
| 39 | |
| 40 | enc = args.sw_version.rjust(4, '0').encode('ascii') |
| 41 | struct.pack_into('>4s', buf, 0x64, enc) |
| 42 | |
| 43 | if (args.extra_padd_size): |
| 44 | tail = bytearray([PADDING_TAIL] * args.extra_padd_size) |
| 45 | if (args.extra_padd_byte): |
| 46 | struct.pack_into ('<i', tail, 0x0, |
| 47 | args.extra_padd_byte) |
| 48 | elif not args.hw_id: |
| 49 | tail[0] = 0x0D |
| 50 | tail[1] = 0x0A |
| 51 | buf += tail |
| 52 | |
| 53 | return buf |
| 54 | |
| 55 | def main(): |
| 56 | global args |
| 57 | |
| 58 | parser = argparse.ArgumentParser(description='This script \ |
| 59 | generates firmware PID for the Sercomm-based devices') |
| 60 | |
| 61 | parser.add_argument('--hw-version', |
| 62 | dest='hw_version', |
| 63 | action='store', |
| 64 | type=str, |
| 65 | help='Sercomm hardware version') |
| 66 | |
| 67 | parser.add_argument('--hw-id', |
| 68 | dest='hw_id', |
| 69 | action='store', |
| 70 | type=str, |
| 71 | help='Sercomm hardware ID') |
| 72 | |
| 73 | parser.add_argument('--sw-version', |
| 74 | dest='sw_version', |
| 75 | action='store', |
| 76 | type=str, |
| 77 | help='Sercomm software version') |
| 78 | |
| 79 | parser.add_argument('--pid-file', |
| 80 | dest='pid_file', |
| 81 | action='store', |
| 82 | type=str, |
| 83 | help='Output PID file') |
| 84 | |
| 85 | parser.add_argument('--extra-padding-size', |
| 86 | dest='extra_padd_size', |
| 87 | action='store', |
| 88 | type=auto_int, |
| 89 | help='Size of extra NULL padding at the end of the file \ |
| 90 | (optional)') |
| 91 | |
| 92 | parser.add_argument('--extra-padding-first-byte', |
| 93 | dest='extra_padd_byte', |
| 94 | action='store', |
| 95 | type=auto_int, |
| 96 | help='First byte of extra padding (optional)') |
| 97 | |
| 98 | args = parser.parse_args() |
| 99 | |
| 100 | if ((not args.hw_version) or |
| 101 | (not args.sw_version) or |
| 102 | (not args.pid_file)): |
| 103 | parser.print_help() |
| 104 | exit() |
| 105 | |
| 106 | create_pid_file(args) |
| 107 | |
| 108 | main() |