fwcutter: mklist.py - Update to new library and skip some sections
[b43-tools.git] / fwcutter / mklist.py
1 #!/usr/bin/env python
2 #
3 #  Script for creating a "struct extract" list for fwcutter_list.h
4 #
5 #  Copyright (c) 2008 Michael Buesch <mb@bu3sch.de>
6 #
7 #   Redistribution and use in source and binary forms, with or without
8 #   modification, are permitted provided that the following conditions
9 #   are met:
10 #
11 #     1. Redistributions of source code must retain the above copyright
12 #        notice, this list of conditions and the following disclaimer.
13 #     2. Redistributions in binary form must reproduce the above
14 #        copyright notice, this list of conditions and the following
15 #        disclaimer in the documentation and/or other materials provided
16 #        with the distribution.
17 #
18 #   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 #   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 #   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 #   DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
22 #   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 #   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 #   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 #   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 #   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 #   OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 #   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import sys
31 import os
32 import re
33 import hashlib
34
35 if len(sys.argv) != 2:
36         print "Usage: %s path/to/wl.o" % sys.argv[0]
37         sys.exit(1)
38 fn = sys.argv[1]
39
40 pipe = os.popen("objdump -t %s" % fn)
41 syms = pipe.readlines()
42 pipe = os.popen("objdump --headers %s" % fn)
43 headers = pipe.readlines()
44
45 # Get the .rodata fileoffset
46 rodata_fileoffset = None
47 rofileoff_re = re.compile(r"\d+\s+\.rodata\s+[0-9a-fA-F]+\s+[0-9a-fA-F]+\s+[0-9a-fA-F]+\s+([0-9a-fA-F]+)\s+.")
48 for line in headers:
49         line = line.strip()
50         m = rofileoff_re.match(line)
51         if m:
52                 rodata_fileoffset = int(m.group(1), 16)
53                 break
54 if rodata_fileoffset == None:
55         print "ERROR: Could not find .rodata fileoffset"
56         sys.exit(1)
57
58 md5sum = hashlib.md5(file(fn, "r").read())
59
60 print "static struct extract _%s[] =" % md5sum.hexdigest()
61 print "{"
62
63 sym_re = re.compile(r"([0-9a-fA-F]+)\s+g\s+O\s+\.rodata\s+([0-9a-fA-F]+) d11([-_\s\w0-9]+)")
64 ucode_re = re.compile(r"ucode(\d+)")
65
66 for sym in syms:
67         sym = sym.strip()
68         m = sym_re.match(sym)
69         if not m:
70                 continue
71         pos = int(m.group(1), 16) + rodata_fileoffset
72         size = int(m.group(2), 16)
73         name = m.group(3)
74         if name[-2:] == "sz":
75                 continue
76
77         type = None
78         if "initvals" in name:
79                 type = "EXT_IV"
80                 size -= 8
81         if "pcm" in name:
82                 type = "EXT_PCM"
83         if "bommajor" in name:
84                 print "\t/* ucode major version at offset 0x%x */" % pos
85                 continue
86         if "bomminor" in name:
87                 print "\t/* ucode minor version at offset 0x%x */" % pos
88                 continue
89         if "ucode_2w" in name:
90                 continue
91         m = ucode_re.match(name)
92         if m:
93                 corerev = int(m.group(1))
94                 if corerev <= 4:
95                         type = "EXT_UCODE_1"
96                 elif corerev >= 5 and corerev <= 14:
97                         type = "EXT_UCODE_2"
98                 else:
99                         type = "EXT_UCODE_3"
100         if not type:
101                 print "\t/* ERROR: Could not guess data type for: %s */" % name
102                 continue
103
104         print "\t{ .name = \"%s\", .offset = 0x%X, .type = %s, .length = 0x%X }," % (name, pos, type, size)
105 print "\tEXTRACT_LIST_END"
106 print "};"