Add b43 firmware assembly beautifier
[b43-tools.git] / debug / b43-beautifier
diff --git a/debug/b43-beautifier b/debug/b43-beautifier
new file mode 100755 (executable)
index 0000000..0b3a09c
--- /dev/null
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+"""
+#  b43 firmware assembly code beautifier
+#
+#  Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License version 3
+#  as published by the Free Software Foundation.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+import getopt
+import sys
+from libb43 import *
+
+
+def usage():
+       print "b43 firmware assembly code beautifier"
+       print ""
+       print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
+       print "Licensed under the GNU/GPL version 3"
+       print ""
+       print "Usage: b43-beautifier [OPTIONS]"
+       print ""
+       print "-h|--help            Print this help text"
+       print "-a|--asmfile [FILE]  Assembly code source file"
+       print "-b|--binfile [FILE]  Binary code source file"
+       print "-d|--defs [DIR]      Directory containing the defs files"
+       print ""
+       print "The options  -d AND (-a XOR -b)  are essential"
+
+def parseArgs():
+       global opt_asmfile
+       global opt_binfile
+       global opt_defsfiles
+
+       opt_asmfile = None
+       opt_binfile = None
+       opt_defsfiles = None
+
+       try:
+               (opts, args) = getopt.getopt(sys.argv[1:],
+                       "ha:b:d:",
+                       [ "help", "asmfile=", "binfile=", "defs=" ])
+       except getopt.GetoptError:
+               usage()
+               sys.exit(1)
+
+       for (o, v) in opts:
+               if o in ("-h", "--help"):
+                       usage()
+                       sys.exit(0)
+               if o in ("-a", "--asmfile"):
+                       if opt_binfile:
+                               print "Must not set both of --asmfile and --binfile"
+                               sys.exit(1)
+                       opt_asmfile = v
+               if o in ("-b", "--binfile"):
+                       if opt_asmfile:
+                               print "Must not set both of --asmfile and --binfile"
+                               sys.exit(1)
+                       opt_binfile = v
+               if o in ("-d", "--defs"):
+                       opt_defsfiles = v
+
+       if not opt_asmfile and not opt_binfile:
+               print "Must set either --asmfile or --binfile"
+               sys.exit(1)
+       if not opt_defsfiles:
+               print "Must set --defs"
+               sys.exit(1)
+
+def main():
+       parseArgs()
+
+       # Get the assembly
+       if opt_asmfile:
+               try:
+                       asm = file(opt_asmfile).read()
+               except IOError, e:
+                       print "Could not read asmfile %s: %s" % (e.filename, e.strerror)
+                       sys.exit(1)
+       else:
+               try:
+                       bin = file(opt_binfile).read()
+               except IOError, e:
+                       print "Could not read binfile %s: %s" % (e.filename, e.strerror)
+                       sys.exit(1)
+               asm = Disassembler(bin, "").getAsm()
+
+       b = B43Beautifier(asm, opt_defsfiles)
+       sys.stdout.write(b.getAsm())
+
+try:
+       main()
+except B43Exception:
+       sys.exit(1)