3 # b43 firmware state dumper
5 # Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
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.
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.
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/>.
22 from sys import stdout
23 from tempfile import *
27 print "b43 firmware state dumper"
29 print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
30 print "Licensed under the GNU/GPL version 3"
32 print "Usage: b43-fwdump [OPTIONS]"
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."
48 phy = None # Autodetect
49 binary = None # No instruction dump
54 (opts, args) = getopt.getopt(sys.argv[1:],
56 [ "help", "phy=", "binary=", "dasmopt=", "shm" ])
57 except getopt.GetoptError:
62 if o in ("-h", "--help"):
65 if o in ("-p", "--phy"):
67 if o in ("-b", "--binary"):
69 if o in ("-d", "--dasmopt"):
71 if o in ("-s", "--shm"):
76 def dump_regs(prefix, regs):
78 template = "%s%02u: %04X "
80 template = "%s%01u: %04X "
81 for i in range(0, len(regs)):
82 if i != 0 and i % 4 == 0:
84 stdout.write(template % (prefix, i, regs[i]))
88 def disassembleText(text):
89 input = NamedTemporaryFile()
90 output = NamedTemporaryFile()
94 os.system("b43-dasm %s %s %s --paddr" % (input.name, dasmopt, output.name))
98 def makeShortDump(dasm, pc):
99 dasm = dasm.splitlines()
102 if "/* %03X */" % pc in line:
106 return "<Could not find PC in the binary>"
109 end = min(i + 8, len(dasm) - 1)
112 if "/* %03X */" % pc in dasm[pos]:
113 ret += "\t\t<<<<<<<<<<<"
119 if char >= 32 and char <= 126:
128 # Fetch the hardware information
131 lr = b43.getLinkRegs()
132 off = b43.getOffsetRegs()
134 shm = b43.shmSharedRead()
135 dbg = b43.getPsmDebug()
136 psmcond = b43.getPsmConditions()
139 print "--- B43 microcode state dump ---"
140 print "PC: %03X PSM-COND: %04X" % (dbg.getPc(), psmcond)
141 print "Link registers:"
143 print "Offset registers:"
144 dump_regs("off", off)
145 print "General purpose registers:"
151 bintext = file(binary, "r").read()
153 print "Could not read binary file %s: %s" % (binary, e.strerror)
155 dasm = disassembleText(bintext)
156 print makeShortDump(dasm, dbg.getPc())
158 print "<No binary supplied. See --binary option>"
161 print "Shared memory:"
163 for i in range(0, len(shm)):
164 if i % 16 == 0 and i != 0:
165 stdout.write(" " + ascii + "\n")
168 stdout.write("0x%04X: " % i)
170 stdout.write("%02X" % c)
174 stdout.write(" " + ascii + "\n")