b07d493eb04b0ab4f1bbaf4484fea808ac633b17
[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         print "The --defs directory is the \"common\" subdirectory in the"
40         print "b43-ucode assembly source tree."
41
42 def parseArgs():
43         global opt_asmfile
44         global opt_binfile
45         global opt_defsfiles
46
47         opt_asmfile = None
48         opt_binfile = None
49         opt_defsfiles = None
50
51         try:
52                 (opts, args) = getopt.getopt(sys.argv[1:],
53                         "ha:b:d:",
54                         [ "help", "asmfile=", "binfile=", "defs=" ])
55         except getopt.GetoptError:
56                 usage()
57                 sys.exit(1)
58
59         for (o, v) in opts:
60                 if o in ("-h", "--help"):
61                         usage()
62                         sys.exit(0)
63                 if o in ("-a", "--asmfile"):
64                         if opt_binfile:
65                                 print "Must not set both of --asmfile and --binfile"
66                                 sys.exit(1)
67                         opt_asmfile = v
68                 if o in ("-b", "--binfile"):
69                         if opt_asmfile:
70                                 print "Must not set both of --asmfile and --binfile"
71                                 sys.exit(1)
72                         opt_binfile = v
73                 if o in ("-d", "--defs"):
74                         opt_defsfiles = v
75
76         if not opt_asmfile and not opt_binfile:
77                 print "Must set either --asmfile or --binfile"
78                 sys.exit(1)
79         if not opt_defsfiles:
80                 print "Must set --defs"
81                 sys.exit(1)
82
83 def main():
84         parseArgs()
85
86         # Get the assembly
87         if opt_asmfile:
88                 try:
89                         asm = file(opt_asmfile).read()
90                 except IOError, e:
91                         print "Could not read asmfile %s: %s" % (e.filename, e.strerror)
92                         sys.exit(1)
93         else:
94                 try:
95                         bin = file(opt_binfile).read()
96                 except IOError, e:
97                         print "Could not read binfile %s: %s" % (e.filename, e.strerror)
98                         sys.exit(1)
99                 asm = Disassembler(bin, "").getAsm()
100
101         b = B43Beautifier(asm, opt_defsfiles)
102         sys.stdout.write(b.getAsm())
103
104 try:
105         main()
106 except B43Exception:
107         sys.exit(1)