annotate python/hexutil.py @ 409:8fe299cd2d55 devel

Close devel branch
author Windel Bouwman
date Sat, 21 Feb 2015 12:20:10 +0100
parents a78b41ff6ad2
children
rev   line source
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
2
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
3 import sys
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
4 import argparse
295
917eab04b8b7 Added disasm
Windel Bouwman
parents: 290
diff changeset
5 from utils import HexFile
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
6
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
7 def hex2int(s):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
8 if s.startswith('0x'):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
9 s = s[2:]
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
10 return int(s, 16)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
11 raise ValueError('Hexadecimal value must begin with 0x')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
12
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
13 parser = argparse.ArgumentParser(
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
14 description='hexfile manipulation tool by Windel Bouwman')
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 295
diff changeset
15 subparsers = parser.add_subparsers(title='commands',
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
16 description='possible commands', dest='command')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
17
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
18 p = subparsers.add_parser('info', help='dump info about hexfile')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
19 p.add_argument('hexfile', type=argparse.FileType('r'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
20
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
21 p = subparsers.add_parser('new', help='create a hexfile')
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
22 p.add_argument('hexfile', type=argparse.FileType('w'))
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
23 p.add_argument('address', type=hex2int, help="hex address of the data")
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
24 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
25
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
26 p = subparsers.add_parser('merge', help='merge two hexfiles into a third')
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
27 p.add_argument('hexfile1', type=argparse.FileType('r'), help="hexfile 1")
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
28 p.add_argument('hexfile2', type=argparse.FileType('r'), help="hexfile 2")
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
29 p.add_argument('rhexfile', type=argparse.FileType('w'), help="resulting hexfile")
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
30
290
7b38782ed496 File moves
Windel Bouwman
parents: 246
diff changeset
31
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
32 def main(args):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
33 if args.command == 'info':
331
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 295
diff changeset
34 hexfile = HexFile()
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 295
diff changeset
35 hexfile.load(args.hexfile)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 295
diff changeset
36 print(hexfile)
a78b41ff6ad2 Added better recipe files
Windel Bouwman
parents: 295
diff changeset
37 for region in hexfile.regions:
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
38 print(region)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
39 elif args.command == 'new':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
40 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
41 data = args.datafile.read()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
42 hf.addRegion(args.address, data)
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
43 hf.save(args.hexfile)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
44 elif args.command == 'merge':
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
45 hf = HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
46 hf.load(args.hexfile1)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
47 hf2 = HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
48 hf2.load(args.hexfile2)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
49 hf.merge(hf2)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
50 hf.save(args.rhexfile)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
51 else:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
52 raise NotImplementedError()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
53
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
54 if __name__ == '__main__':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
55 args = parser.parse_args()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
56 if not args.command:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
57 parser.print_usage()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
58 sys.exit(1)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
59 main(args)