asm/dasm: Minor fixes
[b43-tools.git] / disassembler / brcm80211-ivaldump
1 #!/usr/bin/env python
2 """
3 #   A small script to dump the contents of a brcm80211 initvals section
4 #
5 #   Copyright (C) 2010  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 "brcm80211 initvals section dumper"
22         print "Prints a .initvals assembly section to stdout."
23         print ""
24         print "Copyright (C) 2010 Michael Buesch <mb@bu3sch.de>"
25         print "Licensed under the GNU/GPL version 2"
26         print ""
27         print "Usage: brcm80211-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) == 0 or len(ivals) % 8 != 0:
45         print "The input file is malformed."
46         sys.exit(1)
47
48 sectname = filename.split('/')[-1]
49 if sectname.endswith(".fw"):
50         sectname = sectname[:-3]
51 print ".initvals(%s)" % sectname
52 for idx in range(0, len(ivals), 8):
53         addr = ord(ivals[idx + 0])
54         addr |= ord(ivals[idx + 1]) << 8
55         size = ord(ivals[idx + 2])
56         size |= ord(ivals[idx + 3]) << 8
57         value = ord(ivals[idx + 4])
58         value |= ord(ivals[idx + 5]) << 8
59         value |= ord(ivals[idx + 6]) << 16
60         value |= ord(ivals[idx + 7]) << 24
61
62         if addr == 0xFFFF:
63                 break
64         if size == 4:
65                 print "\tmmio32\t0x%08X, 0x%04X" % (value, addr)
66         elif size == 2:
67                 if value & 0xFFFF0000:
68                         print "The input file is malformed (invalid value for 16bit field)"
69                         sys.exit(1)
70                 print "\tmmio16\t0x%04X, 0x%04X" % (value, addr)
71         else:
72                 print "The input file is malformed (invalid size field: 0x%04X)" % size
73                 sys.exit(1)