244
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys
|
|
4 import argparse
|
295
|
5 from utils import HexFile
|
244
|
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')
|
331
|
15 subparsers = parser.add_subparsers(title='commands',
|
244
|
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
|
246
|
21 p = subparsers.add_parser('new', help='create a hexfile')
|
|
22 p.add_argument('hexfile', type=argparse.FileType('w'))
|
|
23 p.add_argument('address', type=hex2int, help="hex address of the data")
|
|
24 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
|
244
|
25
|
246
|
26 p = subparsers.add_parser('merge', help='merge two hexfiles into a third')
|
|
27 p.add_argument('hexfile1', type=argparse.FileType('r'), help="hexfile 1")
|
|
28 p.add_argument('hexfile2', type=argparse.FileType('r'), help="hexfile 2")
|
|
29 p.add_argument('rhexfile', type=argparse.FileType('w'), help="resulting hexfile")
|
244
|
30
|
290
|
31
|
244
|
32 def main(args):
|
|
33 if args.command == 'info':
|
331
|
34 hexfile = HexFile()
|
|
35 hexfile.load(args.hexfile)
|
|
36 print(hexfile)
|
|
37 for region in hexfile.regions:
|
244
|
38 print(region)
|
|
39 elif args.command == 'new':
|
|
40 hf = HexFile()
|
|
41 data = args.datafile.read()
|
|
42 hf.addRegion(args.address, data)
|
246
|
43 hf.save(args.hexfile)
|
|
44 elif args.command == 'merge':
|
|
45 hf = HexFile()
|
|
46 hf.load(args.hexfile1)
|
|
47 hf2 = HexFile()
|
|
48 hf2.load(args.hexfile2)
|
|
49 hf.merge(hf2)
|
|
50 hf.save(args.rhexfile)
|
244
|
51 else:
|
|
52 raise NotImplementedError()
|
|
53
|
|
54 if __name__ == '__main__':
|
|
55 args = parser.parse_args()
|
|
56 if not args.command:
|
|
57 parser.print_usage()
|
|
58 sys.exit(1)
|
|
59 main(args)
|