fwcutter/make: Avoid _DEFAULT_SOURCE warning
[b43-tools.git] / debug / libb43.py
index 671ad2c6f173a141bb2bbffa984c6200f5c929e4..c8b953372a18ba2bedb2f76fbd22e2181b486f9d 100644 (file)
@@ -1,7 +1,7 @@
 """
 #  b43 debugging library
 #
-#  Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+#  Copyright (C) 2008-2010 Michael Buesch <m@bues.ch>
 #
 #  This program is free software: you can redistribute it and/or modify
 #  it under the terms of the GNU General Public License version 3
@@ -19,7 +19,7 @@
 import sys
 import os
 import re
-import md5
+import hashlib
 from tempfile import *
 
 
@@ -62,7 +62,7 @@ class B43PsmDebug:
 class B43:
        """Hardware access layer. This accesses the hardware through the debugfs interface."""
 
-       def __init__(self, phy):
+       def __init__(self, phy=None):
                debugfs_path = self.__debugfs_find()
 
                # Construct the debugfs b43 path to the device
@@ -71,7 +71,11 @@ class B43:
                        b43_path += phy
                else:
                        # Get the PHY.
-                       phys = os.listdir(b43_path)
+                       try:
+                               phys = os.listdir(b43_path)
+                       except OSError:
+                               print "Could not find B43's debugfs directory: %s" % b43_path
+                               raise B43Exception
                        if not phys:
                                print "Could not find any b43 device"
                                raise B43Exception
@@ -339,7 +343,7 @@ class TextPatcher:
                        self.deleted = False
 
        def __init__(self, text, expected_md5sum):
-               sum = md5.md5(text).hexdigest()
+               sum = hashlib.md5(text).hexdigest()
                if sum != expected_md5sum:
                        print "Patcher: The text does not match the expected MD5 sum"
                        print "Expected:   " + expected_md5sum
@@ -529,17 +533,26 @@ class B43AsmLine:
        def getLine(self):
                return self.text
 
+       def __repr__(self):
+               return self.getLine()
+
        def isInstruction(self):
                return False
 
 class B43AsmInstruction(B43AsmLine):
        def __init__(self, opcode):
-               self.opcode = opcode
-               self.operands = []
+               self.setOpcode(opcode)
+               self.clearOperands()
 
        def getOpcode(self):
                return self.opcode
 
+       def setOpcode(self, opcode):
+               self.opcode = opcode
+
+       def clearOperands(self):
+               self.operands = []
+
        def addOperand(self, operand):
                self.operands.append(operand)
 
@@ -618,13 +631,15 @@ class B43Beautifier(B43AsmParser):
        def __init__(self, asm_code, headers_dir):
                """asm_code is the assembly code. headers_dir is a full
                path to the directory containing the symbolic SPR,SHM,etc... definitions"""
+               if headers_dir.endswith("/"):
+                       headers_dir = headers_dir[:-1]
                B43AsmParser.__init__(self, asm_code)
                self.symSpr = B43SymbolicSpr(headers_dir + "/spr.inc")
                self.symShm = B43SymbolicShm(headers_dir + "/shm.inc")
                self.symCond = B43SymbolicCondition(headers_dir + "/cond.inc")
-               self.preamble = "#include <%s/spr.inc>\n" % headers_dir
-               self.preamble += "#include <%s/shm.inc>\n" % headers_dir
-               self.preamble += "#include <%s/cond.inc>\n" % headers_dir
+               self.preamble = "#include \"%s/spr.inc\"\n" % headers_dir
+               self.preamble += "#include \"%s/shm.inc\"\n" % headers_dir
+               self.preamble += "#include \"%s/cond.inc\"\n" % headers_dir
                self.preamble += "\n"
                self.__process_code()
 
@@ -637,15 +652,38 @@ class B43Beautifier(B43AsmParser):
                                continue
                        opcode = line.getOpcode()
                        operands = line.getOperands()
+                       # Transform unconditional jump
+                       if opcode == "jext" and int(operands[0], 16) == 0x7F:
+                               label = operands[1]
+                               line.setOpcode("jmp")
+                               line.clearOperands()
+                               line.addOperand(label)
+                               continue
+                       # Transform external conditions
                        if opcode == "jext" or opcode == "jnext":
                                operands[0] = self.symCond.get(int(operands[0], 16))
                                continue
+                       # Transform orx 7,8,imm,imm,target to mov
+                       if opcode == "orx" and \
+                          int(operands[0], 16) == 7 and int(operands[1], 16) == 8 and\
+                          operands[2].startswith("0x") and operands[3].startswith("0x"):
+                               value = int(operands[3], 16) & 0xFF
+                               value |= (int(operands[2], 16) & 0xFF) << 8
+                               target = operands[4]
+                               line.setOpcode("mov")
+                               line.clearOperands()
+                               line.addOperand("0x%X" % value)
+                               line.addOperand(target)
+                               opcode = line.getOpcode()
+                               operands = line.getOperands()
                        for i in range(0, len(operands)):
                                o = operands[i]
+                               # Transform SPR operands
                                m = spr_re.match(o)
                                if m:
                                        operands[i] = self.symSpr.get(o)
                                        continue
+                               # Transform SHM operands
                                m = shm_re.match(o)
                                if m:
                                        offset = int(m.group(1), 16)
@@ -654,7 +692,7 @@ class B43Beautifier(B43AsmParser):
 
        def getAsm(self):
                """Returns the beautified asm code."""
-               ret = self.preamble
+               ret = [ self.preamble ]
                for line in self.codelines:
-                       ret += line.getLine() + "\n"
-               return ret
+                       ret.append(str(line))
+               return "\n".join(ret)