0b3a09c20073272d09939fa94021bb158969ba0c
[b43-tools.git] / debug / b43-beautifier
1 #!/usr/bin/env python
2 """
3 #  b43 firmware assembly code beautifier
4 #
5 #  Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
6 #
7 #  This program is free software: you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License version 3
9 #  as published by the Free Software Foundation.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 """
19
20 import getopt
21 import sys
22 from libb43 import *
23
24
25 def usage():
26         print "b43 firmware assembly code beautifier"
27         print ""
28         print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
29         print "Licensed under the GNU/GPL version 3"
30         print ""
31         print "Usage: b43-beautifier [OPTIONS]"
32         print ""
33         print "-h|--help            Print this help text"
34         print "-a|--asmfile [FILE]  Assembly code source file"
35         print "-b|--binfile [FILE]  Binary code source file"
36         print "-d|--defs [DIR]      Directory containing the defs files"
37         print ""
38         print "The options  -d AND (-a XOR -b)  are essential"
39
40 def parseArgs():
41         global opt_asmfile
42         global opt_binfile
43         global opt_defsfiles
44
45         opt_asmfile = None
46         opt_binfile = None
47         opt_defsfiles = None
48
49         try:
50                 (opts, args) = getopt.getopt(sys.argv[1:],
51                         "ha:b:d:",
52                         [ "help", "asmfile=", "binfile=", "defs=" ])
53         except getopt.GetoptError:
54                 usage()
55                 sys.exit(1)
56
57         for (o, v) in opts:
58                 if o in ("-h", "--help"):
59                         usage()
60                         sys.exit(0)
61                 if o in ("-a", "--asmfile"):
62                         if opt_binfile:
63                                 print "Must not set both of --asmfile and --binfile"
64                                 sys.exit(1)
65                         opt_asmfile = v
66                 if o in ("-b", "--binfile"):
67                         if opt_asmfile:
68                                 print "Must not set both of --asmfile and --binfile"
69                                 sys.exit(1)
70                         opt_binfile = v
71                 if o in ("-d", "--defs"):
72                         opt_defsfiles = v
73
74         if not opt_asmfile and not opt_binfile:
75                 print "Must set either --asmfile or --binfile"
76                 sys.exit(1)
77         if not opt_defsfiles:
78                 print "Must set --defs"
79                 sys.exit(1)
80
81 def main():
82         parseArgs()
83
84         # Get the assembly
85         if opt_asmfile:
86                 try:
87                         asm = file(opt_asmfile).read()
88                 except IOError, e:
89                         print "Could not read asmfile %s: %s" % (e.filename, e.strerror)
90                         sys.exit(1)
91         else:
92                 try:
93                         bin = file(opt_binfile).read()
94                 except IOError, e:
95                         print "Could not read binfile %s: %s" % (e.filename, e.strerror)
96                         sys.exit(1)
97                 asm = Disassembler(bin, "").getAsm()
98
99         b = B43Beautifier(asm, opt_defsfiles)
100         sys.stdout.write(b.getAsm())
101
102 try:
103         main()
104 except B43Exception:
105         sys.exit(1)