Commit e41dd6db authored by Michael Büsch's avatar Michael Büsch

debug: Add a generic text patcher

Signed-off-by: Michael Büsch's avatarMichael Buesch <>
parent aafbdf92
......@@ -90,16 +90,6 @@ def dump_regs(prefix, regs):
def disassembleText(text):
input = NamedTemporaryFile()
output = NamedTemporaryFile()
os.system("b43-dasm %s %s %s --paddr" % (, dasmopt,
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)
dasm = disassembleText(bintext)
dasm = Disassembler(bintext, dasmopt + "--paddr").getAsm()
print makeShortDump(dasm, dbg.getPc())
print "<No binary supplied. See --binary option>"
......@@ -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)
class Disassembler:
"""Disassembler for b43 firmware."""
def __init__(self, binaryText, b43DasmOpts):
input = NamedTemporaryFile()
output = NamedTemporaryFile()
#FIXME check b43-dasm errors
os.system("b43-dasm %s %s %s" % (,, b43DasmOpts))
self.asmText =
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()
#FIXME check b43-asm errors
os.system("b43-asm %s %s %s" % (,, b43AsmOpts))
self.binaryText =
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
def getText(self):
"""This returns the current text."""
textLines = []
for l in self.lines:
if not l.deleted:
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
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:
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment