fwcutter/make: Avoid _DEFAULT_SOURCE warning
[b43-tools.git] / debug / b43-fwdump
index 4308d60704fb0828c02ba28d6628e6df4601db7c..a24c61466d83195c590e3ca0ad74b72da0697ee0 100755 (executable)
@@ -2,7 +2,7 @@
 """
 #  b43 firmware state dumper
 #
-#  Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+#  Copyright (C) 2008 Michael Buesch <m@bues.ch>
 #
 #  This program is free software: you can redistribute it and/or modify
 #  it under the terms of the GNU General Public License version 3
@@ -21,22 +21,25 @@ import getopt
 from libb43 import *
 from sys import stdout
 from tempfile import *
+import re
 
 
 def usage():
        print "b43 firmware state dumper"
        print ""
-       print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
+       print "Copyright (C) 2008 Michael Buesch <m@bues.ch>"
        print "Licensed under the GNU/GPL version 3"
        print ""
        print "Usage: b43-fwdump [OPTIONS]"
        print ""
        print "-h|--help            Print this help text"
-       print "-p|--phy PHY         The PHY to use. For example phy0"
+       print "-p|--phy WIPHY       The WIPHY to use. For example phy0."
+       print "                     Can be omitted, if there is only one device in the system."
        print "-b|--binary BIN      The firmware binary. This is required for"
        print "                     an instruction dump."
        print "-d|--dasmopt OPT     Additional options to the disassembler."
        print "-s|--shm             Also dump SHM."
+       print "-S|--shmbin          Do a binary SHM dump, only."
        return
 
 def parseArgs():
@@ -44,16 +47,18 @@ def parseArgs():
        global binary
        global dasmopt
        global dumpShm
+       global dumpShmBin
 
        phy = None # Autodetect
        binary = None # No instruction dump
        dasmopt = ""
        dumpShm = False
+       dumpShmBin = False
 
        try:
                (opts, args) = getopt.getopt(sys.argv[1:],
-                       "hp:b:d:s",
-                       [ "help", "phy=", "binary=", "dasmopt=", "shm" ])
+                       "hp:b:d:sS",
+                       [ "help", "phy=", "binary=", "dasmopt=", "shm", "shmbin" ])
        except getopt.GetoptError:
                usage()
                sys.exit(1)
@@ -70,6 +75,8 @@ def parseArgs():
                        dasmopt = v
                if o in ("-s", "--shm"):
                        dumpShm = True
+               if o in ("-S", "--shmbin"):
+                       dumpShmBin = True
        return
 
 
@@ -85,31 +92,28 @@ def dump_regs(prefix, regs):
        stdout.write("\n")
        return
 
-def disassembleText(text):
-       input = NamedTemporaryFile()
-       output = NamedTemporaryFile()
-
-       input.write(text)
-       input.flush()
-       os.system("b43-dasm %s %s %s --paddr" % (input.name, dasmopt, output.name))
-
-       return output.read()
+def dasmLineIsPC(line, pc):
+       m = re.match(r'.*/\*\s+([0-9a-fA-F]+)\s+\*/.*', line, re.DOTALL)
+       if not m:
+               return False
+       linePC = int(m.group(1), 16)
+       return pc == linePC
 
 def makeShortDump(dasm, pc):
        dasm = dasm.splitlines()
        i = 0
        for line in dasm:
-               if "/* %03X */" % pc in line:
+               if dasmLineIsPC(line, pc):
                        break
                i += 1
-       if i >= len(dasm):
+       else:
                return "<Could not find PC in the binary>"
        ret = ""
        pos = max(i - 8, 0)
        end = min(i + 8, len(dasm) - 1)
        while pos != end:
                ret += dasm[pos]
-               if "/* %03X */" % pc in dasm[pos]:
+               if dasmLineIsPC(dasm[pos], pc):
                        ret += "\t\t<<<<<<<<<<<"
                ret += "\n"
                pos += 1
@@ -130,12 +134,17 @@ def main():
        gpr = b43.getGprs()
        lr = b43.getLinkRegs()
        off = b43.getOffsetRegs()
-       if dumpShm:
+       if dumpShm or dumpShmBin:
                shm = b43.shmSharedRead()
        dbg = b43.getPsmDebug()
        psmcond = b43.getPsmConditions()
        b43.ucodeStart()
 
+       if dumpShmBin:
+               # Only do a binary SHM dump
+               stdout.write(shm)
+               sys.exit(0)
+
        print "--- B43 microcode state dump ---"
        print "PC: %03X  PSM-COND: %04X" % (dbg.getPc(), psmcond)
        print "Link registers:"
@@ -152,7 +161,7 @@ def main():
                except IOError, e:
                        print "Could not read binary file %s: %s" % (binary, e.strerror)
                        sys.exit(1)
-               dasm = disassembleText(bintext)
+               dasm = Disassembler(bintext, dasmopt + "--paddr").getAsm()
                print makeShortDump(dasm, dbg.getPc())
        else:
                print "<No binary supplied. See --binary option>"