debug: Add a generic text patcher
authorMichael Buesch <mb@bu3sch.de>
Thu, 26 Jun 2008 21:06:06 +0000 (23:06 +0200)
committerMichael Buesch <mb@bu3sch.de>
Thu, 26 Jun 2008 21:06:06 +0000 (23:06 +0200)
Signed-off-by: Michael Buesch <mb@bu3sch.de>
debug/b43-fwdump
debug/libb43.py

index 0e94a3026ee383965bae73190f7a5d9e47195232..b698887a3992824bdc799cb6f3f25a12f0ad9e24 100755 (executable)
@@ -90,16 +90,6 @@ 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 makeShortDump(dasm, pc):
        dasm = dasm.splitlines()
        i = 0
@@ -162,7 +152,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>"
index f834bc76d9bbee0b28f7039f8048cd64da0a1687..00da13bfced95788004f3820cb057f533f9bdf3f 100644 (file)
@@ -19,6 +19,8 @@
 import sys
 import os
 import re
+import md5
+from tempfile import *
 
 
 # SHM routing values
@@ -290,3 +292,99 @@ class B43:
                self.maskSet32(B43_MMIO_MACCTL, ~0, B43_MACCTL_PSM_RUN)
                return
 
+class Disassembler:
+       """Disassembler for b43 firmware."""
+       def __init__(self, binaryText, b43DasmOpts):
+               input = NamedTemporaryFile()
+               output = NamedTemporaryFile()
+
+               input.write(binaryText)
+               input.flush()
+               #FIXME check b43-dasm errors
+               os.system("b43-dasm %s %s %s" % (input.name, output.name, b43DasmOpts))
+
+               self.asmText = output.read()
+
+       def getAsm(self):
+               """Returns the assembly code."""
+               return self.asmText
+
+class Assembler:
+       """Assembler for b43 firmware."""
+       def __init__(self, assemblyText, b43AsmOpts):
+               input = NamedTemporaryFile()
+               output = NamedTemporaryFile()
+
+               input.write(assemblyText)
+               input.flush()
+               #FIXME check b43-asm errors
+               os.system("b43-asm %s %s %s" % (input.name, output.name, b43AsmOpts))
+
+               self.binaryText = output.read()
+
+       def getBinary(self):
+               """Returns the binary code."""
+               return self.binaryText
+
+class TextPatcher:
+       """A textfile patcher that does not include any target context.
+       This can be used to patch b43 firmware files."""
+
+       class TextLine:
+               def __init__(self, index, line):
+                       self.index = index
+                       self.line = line
+                       self.deleted = False
+
+       def __init__(self, text, expected_md5sum):
+               sum = md5.md5(text).hexdigest()
+               if sum != expected_md5sum:
+                       print "Patcher: The text does not match the expected MD5 sum"
+                       print "Expected:   " + expected_md5sum
+                       print "Calculated: " + sum
+                       raise B43Exception
+               text = text.splitlines()
+               self.lines = []
+               i = 0
+               for line in text:
+                       self.lines.append(TextPatcher.TextLine(i, line))
+                       i += 1
+               # Add an after-last dummy. Needed for the add-before logic
+               lastDummy = TextPatcher.TextLine(i, "")
+               lastDummy.deleted = True
+               self.lines.append(lastDummy)
+
+       def getText(self):
+               """This returns the current text."""
+               textLines = []
+               for l in self.lines:
+                       if not l.deleted:
+                               textLines.append(l.line)
+               return "\n".join(textLines)
+
+       def delLine(self, linenumber):
+               """Delete a line of text. The linenumber corresponds to the
+               original unmodified text."""
+               for l in self.lines:
+                       if l.index == linenumber:
+                               l.deleted = True
+                               return
+               print "Patcher deleteLine: Did not find the line!"
+               raise B43Exception
+
+       def addText(self, beforeLineNumber, text):
+               """Add a text before the specified linenumber. The linenumber
+               corresponds to the original unmodified text."""
+               text = text.splitlines()
+               index = 0
+               for l in self.lines:
+                       if l.index == beforeLineNumber:
+                               break
+                       index += 1
+               if index >= len(self.lines):
+                       print "Patcher addText: Did not find the line!"
+                       raise B43Exception
+               for l in text:
+                       self.lines.insert(index, TextPatcher.TextLine(-1, l))
+                       index += 1
+