debug: Add a generic text patcher
[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         print "-S|--shmbin          Do a binary SHM dump, only."
41         return
42
43 def parseArgs():
44         global phy
45         global binary
46         global dasmopt
47         global dumpShm
48         global dumpShmBin
49
50         phy = None # Autodetect
51         binary = None # No instruction dump
52         dasmopt = ""
53         dumpShm = False
54         dumpShmBin = False
55
56         try:
57                 (opts, args) = getopt.getopt(sys.argv[1:],
58                         "hp:b:d:sS",
59                         [ "help", "phy=", "binary=", "dasmopt=", "shm", "shmbin" ])
60         except getopt.GetoptError:
61                 usage()
62                 sys.exit(1)
63
64         for (o, v) in opts:
65                 if o in ("-h", "--help"):
66                         usage()
67                         sys.exit(0)
68                 if o in ("-p", "--phy"):
69                         phy = v
70                 if o in ("-b", "--binary"):
71                         binary = v
72                 if o in ("-d", "--dasmopt"):
73                         dasmopt = v
74                 if o in ("-s", "--shm"):
75                         dumpShm = True
76                 if o in ("-S", "--shmbin"):
77                         dumpShmBin = True
78         return
79
80
81 def dump_regs(prefix, regs):
82         if len(regs) >= 10:
83                 template = "%s%02u: %04X  "
84         else:
85                 template = "%s%01u: %04X  "
86         for i in range(0, len(regs)):
87                 if i != 0 and i % 4 == 0:
88                         stdout.write("\n")
89                 stdout.write(template % (prefix, i, regs[i]))
90         stdout.write("\n")
91         return
92
93 def makeShortDump(dasm, pc):
94         dasm = dasm.splitlines()
95         i = 0
96         for line in dasm:
97                 if "/* %03X */" % pc in line:
98                         break
99                 i += 1
100         if i >= len(dasm):
101                 return "<Could not find PC in the binary>"
102         ret = ""
103         pos = max(i - 8, 0)
104         end = min(i + 8, len(dasm) - 1)
105         while pos != end:
106                 ret += dasm[pos]
107                 if "/* %03X */" % pc in dasm[pos]:
108                         ret += "\t\t<<<<<<<<<<<"
109                 ret += "\n"
110                 pos += 1
111         return ret
112
113 def toAscii(char):
114         if char >= 32 and char <= 126:
115                 return chr(char)
116         return "."
117
118 def main():
119         parseArgs()
120
121         b43 = B43(phy)
122
123         # Fetch the hardware information
124         b43.ucodeStop()
125         gpr = b43.getGprs()
126         lr = b43.getLinkRegs()
127         off = b43.getOffsetRegs()
128         if dumpShm or dumpShmBin:
129                 shm = b43.shmSharedRead()
130         dbg = b43.getPsmDebug()
131         psmcond = b43.getPsmConditions()
132         b43.ucodeStart()
133
134         if dumpShmBin:
135                 # Only do a binary SHM dump
136                 stdout.write(shm)
137                 sys.exit(0)
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 = Disassembler(bintext, dasmopt + "--paddr").getAsm()
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)