b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import argparse |
| 4 | import os |
| 5 | import struct |
| 6 | |
| 7 | def create_header(args, size): |
| 8 | header = struct.pack('32s32s32s32s32s', |
| 9 | args.part_name.encode('ascii'), |
| 10 | str(size).encode('ascii'), |
| 11 | args.part_version.encode('ascii'), |
| 12 | "".encode('ascii'), |
| 13 | args.rootfs_version.encode('ascii')) |
| 14 | |
| 15 | return header |
| 16 | |
| 17 | def create_output(args): |
| 18 | in_st = os.stat(args.input_file) |
| 19 | in_size = in_st.st_size |
| 20 | |
| 21 | header = create_header(args, in_size) |
| 22 | print(header) |
| 23 | |
| 24 | in_f = open(args.input_file, 'r+b') |
| 25 | in_bytes = in_f.read(in_size) |
| 26 | in_f.close() |
| 27 | |
| 28 | out_f = open(args.output_file, 'w+b') |
| 29 | out_f.write(header) |
| 30 | out_f.write(in_bytes) |
| 31 | out_f.close() |
| 32 | |
| 33 | def main(): |
| 34 | global args |
| 35 | |
| 36 | parser = argparse.ArgumentParser(description='') |
| 37 | |
| 38 | parser.add_argument('--input-file', |
| 39 | dest='input_file', |
| 40 | action='store', |
| 41 | type=str, |
| 42 | help='Input file') |
| 43 | |
| 44 | parser.add_argument('--output-file', |
| 45 | dest='output_file', |
| 46 | action='store', |
| 47 | type=str, |
| 48 | help='Output file') |
| 49 | |
| 50 | parser.add_argument('--part-name', |
| 51 | dest='part_name', |
| 52 | action='store', |
| 53 | type=str, |
| 54 | help='Partition Name') |
| 55 | |
| 56 | parser.add_argument('--part-version', |
| 57 | dest='part_version', |
| 58 | action='store', |
| 59 | type=str, |
| 60 | help='Partition Version') |
| 61 | |
| 62 | parser.add_argument('--rootfs-version', |
| 63 | dest='rootfs_version', |
| 64 | action='store', |
| 65 | type=str, |
| 66 | help='RootFS lib version') |
| 67 | |
| 68 | args = parser.parse_args() |
| 69 | |
| 70 | if not args.rootfs_version: |
| 71 | args.rootfs_version = "" |
| 72 | |
| 73 | if ((not args.input_file) or |
| 74 | (not args.output_file) or |
| 75 | (not args.part_name) or |
| 76 | (not args.part_version)): |
| 77 | parser.print_help() |
| 78 | |
| 79 | create_output(args) |
| 80 | |
| 81 | main() |