asm/dasm: Minor fixes
[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 "Prints a .initvals assembly section to stdout."
23         print ""
24         print "Copyright (C) 2008-2010 Michael Buesch <mb@bu3sch.de>"
25         print "Licensed under the GNU/GPL version 2"
26         print ""
27         print "Usage: b43-ivaldump FILE"
28         print ""
29         print "FILE is the file that is going to be dumped"
30         return
31
32 if len(sys.argv) != 2:
33         usage()
34         sys.exit(1)
35
36 filename = sys.argv[1]
37
38 try:
39         ivals = file(filename).read()
40 except IOError, e:
41         print "Could not read the initvals file: %s" % e.strerror
42         sys.exit(1)
43
44 if len(ivals) < 8:
45         print "The file is too small. This can not be an initvals file."
46         sys.exit(1)
47
48 if ivals[0] != "i":
49         print "This is not an initvals file. (Wrong header magic)."
50         sys.exit(1)
51
52 if ord(ivals[1]) != 1:
53         print "Initvals file version %d is not supported by this program." % ord(ivals[1])
54         sys.exit(1)
55
56 sectname = filename.split('/')[-1]
57 if sectname.endswith(".fw"):
58         sectname = sectname[:-3]
59 print ".initvals(%s)" % sectname
60 idx = 8 # skip header
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 "\tmmio32\t0x%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 "\tmmio16\t0x%04X, 0x%04X" % (data, offset)