244
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys
|
|
4 import argparse
|
|
5 from hexfile import HexFile
|
|
6
|
|
7 def hex2int(s):
|
|
8 if s.startswith('0x'):
|
|
9 s = s[2:]
|
|
10 return int(s, 16)
|
|
11 raise ValueError('Hexadecimal value must begin with 0x')
|
|
12
|
|
13 parser = argparse.ArgumentParser(
|
|
14 description='hexfile manipulation tool by Windel Bouwman')
|
|
15 subparsers = parser.add_subparsers(title='commands',
|
|
16 description='possible commands', dest='command')
|
|
17
|
|
18 p = subparsers.add_parser('info', help='dump info about hexfile')
|
|
19 p.add_argument('hexfile', type=argparse.FileType('r'))
|
|
20
|
|
21 p = subparsers.add_parser('new', help='create empty hexfile')
|
|
22 p.add_argument('hexfile', type=argparse.FileType('x'))
|
|
23
|
|
24 p = subparsers.add_parser('add', help='add binary data from file to hexfile')
|
|
25 p.add_argument('hexfile', type=argparse.FileType('r+'), help="the hexfile to add the data to")
|
|
26 p.add_argument('address', type=hex2int, help="hex address")
|
|
27 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
|
|
28
|
|
29 def main(args):
|
|
30 if args.command == 'info':
|
|
31 hf = HexFile()
|
|
32 hf.load(args.hexfile)
|
|
33 print(hf)
|
|
34 for region in hf.regions:
|
|
35 print(region)
|
|
36 elif args.command == 'new':
|
|
37 hf = HexFile()
|
|
38 hf.save(args.hexfile)
|
|
39 elif args.command == 'add':
|
|
40 hf = HexFile()
|
|
41 hf.load(args.hexfile)
|
|
42 data = args.datafile.read()
|
|
43 hf.addRegion(args.address, data)
|
|
44 else:
|
|
45 raise NotImplementedError()
|
|
46
|
|
47 if __name__ == '__main__':
|
|
48 args = parser.parse_args()
|
|
49 if not args.command:
|
|
50 parser.print_usage()
|
|
51 sys.exit(1)
|
|
52 main(args)
|
|
53
|