From e41dd6db735d02338c1d5bb6ae84d3b492c37fc4 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Thu, 26 Jun 2008 23:06:06 +0200 Subject: [PATCH] debug: Add a generic text patcher Signed-off-by: Michael Buesch --- debug/b43-fwdump | 12 +----- debug/libb43.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 11 deletions(-) diff --git a/debug/b43-fwdump b/debug/b43-fwdump index 0e94a30..b698887 100755 --- a/debug/b43-fwdump +++ b/debug/b43-fwdump @@ -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 "" diff --git a/debug/libb43.py b/debug/libb43.py index f834bc7..00da13b 100644 --- a/debug/libb43.py +++ b/debug/libb43.py @@ -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 + -- 2.31.1