fwcutter: Update .gitignore
[b43-tools.git] / disassembler / b43-ivaldump
1 #!/usr/bin/env python
2 """
3 #   A small script to dump the contents of a b43 initvals file
4 #
5 #   Copyright (C) 2008  Michael Buesch <mb@bu3sch.de>
6 #
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 2
9 #   as published by the Free Software Foundation.
10 #
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.
15 """
16
17 import sys
18
19
20 def usage():
21         print "b43 initvals file dumper"
22         print ""
23         print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
24         print "Licensed under the GNU/GPL version 2"
25         print ""
26         print "Usage: b43-ivaldump FILE"
27         print ""
28         print "FILE is the file that is going to be dumped"
29         print ""
30         print "The dump will look like this:"
31         print "XX-bit  0xDEAD -> 0xBEEF"
32         print "This is an XX-bit write of the value 0xDEAD to register 0xBEEF"
33         return
34
35 if len(sys.argv) != 2:
36         usage()
37         sys.exit(1)
38
39 filename = sys.argv[1]
40
41 try:
42         ivals = file(filename).read()
43 except IOError, e:
44         print "Could not read the initvals file: %s" % e.strerror
45         sys.exit(1)
46
47 if len(ivals) < 8:
48         print "The file is too small. This can not be an initvals file."
49         sys.exit(1)
50
51 if ivals[0] != "i":
52         print "This is not an initvals file. (Wrong header magic)."
53         sys.exit(1)
54
55 if ord(ivals[1]) != 1:
56         print "Initvals file version %d is not supported by this program." % ord(ivals[1])
57         sys.exit(1)
58
59 idx = 8 # skip header
60
61 while idx < len(ivals):
62         off_sz = ord(ivals[idx + 0]) << 8
63         off_sz |= ord(ivals[idx + 1])
64         offset = off_sz & 0x7FFF
65         dword = (off_sz & 0x8000) != 0
66
67         if dword:
68                 data = ord(ivals[idx + 2]) << 24
69                 data |= ord(ivals[idx + 3]) << 16
70                 data |= ord(ivals[idx + 4]) << 8
71                 data |= ord(ivals[idx + 5]) << 0
72                 idx += 6
73                 print "32-bit  0x%08X -> 0x%04X" % (data, offset)
74         else:
75                 data = ord(ivals[idx + 2]) << 8
76                 data |= ord(ivals[idx + 3]) << 0
77                 idx += 4
78                 print "16-bit  0x%04X -> 0x%04X" % (data, offset)