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
|
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
|
|
31 def main(args):
|
|
32 if args.command == 'info':
|
|
33 hf = HexFile()
|
|
34 hf.load(args.hexfile)
|
|
35 print(hf)
|
|
36 for region in hf.regions:
|
|
37 print(region)
|
|
38 elif args.command == 'new':
|
|
39 hf = HexFile()
|
|
40 data = args.datafile.read()
|
|
41 hf.addRegion(args.address, data)
|
246
|
42 hf.save(args.hexfile)
|
|
43 elif args.command == 'merge':
|
|
44 hf = HexFile()
|
|
45 hf.load(args.hexfile1)
|
|
46 hf2 = HexFile()
|
|
47 hf2.load(args.hexfile2)
|
|
48 hf.merge(hf2)
|
|
49 hf.save(args.rhexfile)
|
244
|
50 else:
|
|
51 raise NotImplementedError()
|
|
52
|
|
53 if __name__ == '__main__':
|
|
54 args = parser.parse_args()
|
|
55 if not args.command:
|
|
56 parser.print_usage()
|
|
57 sys.exit(1)
|
|
58 main(args)
|
|
59
|