b43-fwdump: Add SHM dump
[b43-tools.git] / debug / b43-fwdump
1 #!/usr/bin/env python
2 """
3 #  b43 firmware state dumper
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 from libb43 import *
22 from sys import stdout
23 from tempfile import *
24
25
26 def usage():
27         print "b43 firmware state dumper"
28         print ""
29         print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
30         print "Licensed under the GNU/GPL version 3"
31         print ""
32         print "Usage: b43-fwdump [OPTIONS]"
33         print ""
34         print "-h|--help            Print this help text"
35         print "-p|--phy PHY         The PHY to use. For example phy0"
36         print "-b|--binary BIN      The firmware binary. This is required for"
37         print "                     an instruction dump."
38         print "-d|--dasmopt OPT     Additional options to the disassembler."
39         print "-s|--shm             Also dump SHM."
40         return
41
42 def parseArgs():
43         global phy
44         global binary
45         global dasmopt
46         global dumpShm
47
48         phy = None # Autodetect
49         binary = None # No instruction dump
50         dasmopt = ""
51         dumpShm = False
52
53         try:
54                 (opts, args) = getopt.getopt(sys.argv[1:],
55                         "hp:b:d:s",
56                         [ "help", "phy=", "binary=", "dasmopt=", "shm" ])
57         except getopt.GetoptError:
58                 usage()
59                 sys.exit(1)
60
61         for (o, v) in opts:
62                 if o in ("-h", "--help"):
63                         usage()
64                         sys.exit(0)
65                 if o in ("-p", "--phy"):
66                         phy = v
67                 if o in ("-b", "--binary"):
68                         binary = v
69                 if o in ("-d", "--dasmopt"):
70                         dasmopt = v
71                 if o in ("-s", "--shm"):
72                         dumpShm = True
73         return
74
75
76 def dump_regs(prefix, regs):
77         if len(regs) >= 10:
78                 template = "%s%02u: %04X  "
79         else:
80                 template = "%s%01u: %04X  "
81         for i in range(0, len(regs)):
82                 if i != 0 and i % 4 == 0:
83                         stdout.write("\n")
84                 stdout.write(template % (prefix, i, regs[i]))
85         stdout.write("\n")
86         return
87
88 def disassembleText(text):
89         input = NamedTemporaryFile()
90         output = NamedTemporaryFile()
91
92         input.write(text)
93         input.flush()
94         os.system("b43-dasm %s %s %s --paddr" % (input.name, dasmopt, output.name))
95
96         return output.read()
97
98 def makeShortDump(dasm, pc):
99         dasm = dasm.splitlines()
100         i = 0
101         for line in dasm:
102                 if "/* %03X */" % pc in line:
103                         break
104                 i += 1
105         if i >= len(dasm):
106                 return "<Could not find PC in the binary>"
107         ret = ""
108         pos = max(i - 8, 0)
109         end = min(i + 8, len(dasm) - 1)
110         while pos != end:
111                 ret += dasm[pos]
112                 if "/* %03X */" % pc in dasm[pos]:
113                         ret += "\t\t<<<<<<<<<<<"
114                 ret += "\n"
115                 pos += 1
116         return ret
117
118 def toAscii(char):
119         if char >= 32 and char <= 126:
120                 return chr(char)
121         return "."
122
123 def main():
124         parseArgs()
125
126         b43 = B43(phy)
127
128         # Fetch the hardware information
129         b43.ucodeStop()
130         gpr = b43.getGprs()
131         lr = b43.getLinkRegs()
132         off = b43.getOffsetRegs()
133         if dumpShm:
134                 shm = b43.shmSharedRead()
135         dbg = b43.getPsmDebug()
136         psmcond = b43.getPsmConditions()
137         b43.ucodeStart()
138
139         print "--- B43 microcode state dump ---"
140         print "PC: %03X  PSM-COND: %04X" % (dbg.getPc(), psmcond)
141         print "Link registers:"
142         dump_regs("lr", lr)
143         print "Offset registers:"
144         dump_regs("off", off)
145         print "General purpose registers:"
146         dump_regs("r", gpr)
147
148         print "Code:"
149         if binary:
150                 try:
151                         bintext = file(binary, "r").read()
152                 except IOError, e:
153                         print "Could not read binary file %s: %s" % (binary, e.strerror)
154                         sys.exit(1)
155                 dasm = disassembleText(bintext)
156                 print makeShortDump(dasm, dbg.getPc())
157         else:
158                 print "<No binary supplied. See --binary option>"
159
160         if dumpShm:
161                 print "Shared memory:"
162                 ascii = ""
163                 for i in range(0, len(shm)):
164                         if i % 16 == 0 and i != 0:
165                                 stdout.write("  " + ascii + "\n")
166                                 ascii = ""
167                         if i % 16 == 0:
168                                 stdout.write("0x%04X:  " % i)
169                         c = ord(shm[i])
170                         stdout.write("%02X" % c)
171                         if (i % 2 != 0):
172                                 stdout.write(" ")
173                         ascii += toAscii(c)
174                 stdout.write("  " + ascii + "\n")
175         return
176
177
178 try:
179         main()
180 except B43Exception:
181         sys.exit(1)