1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys

base = ''
basename = ''
compat = False

if sys.argv[1] == '-c':
    f = sys.argv[2]
    compat = True
else:
    f = sys.argv[1]
  
#print ('Analyzing', f)

for line in open(f):
    if line.startswith("#define"):
        line = line.replace('\n', '')
        line = line.replace('\t', '')

        if line.find('volatile') != -1:
            line = line[:line.find('*(volatile')] + line[line.find(' *)')+3:]

        if line.find('//') != -1:
            line = line[:line.find('//')-1]
        if line.find('/*') != -1 and line.find('*/') != -1:
            line = line[:line.find('/*')-1] + line[line.find('*/')+2:]

        chunks = [x for x in line.split(' ') if x != '']
        name = chunks[1]
     
        if name.startswith("HW_") or name.startswith("REGS_"):
            if name.endswith("_BASE"):
                basename = name.replace(' ', '')
                base = '0x%08x' % eval(''.join(chunks[2:]).replace(' ', '').replace('REGS_BASE', '0x80000000'))
            else:
                if base == '' or basename == '':
                    print ('No base defined, aborting')
                    sys.exit(0)
                # Blacklist
                if name.endswith('_BM')                 or \
                    name.startswith('HW_LRADC_CHANNEL') or \
                    name.startswith('ABP_SSP')          or \
                    name.startswith('HW_APBH_SSP')      or \
                    (compat and not name.endswith('_ADDR')):
                    continue
                # Macro parameters
                if name.find("(") != -1 and name.find(")") != -1:
                    param = name[name.find("(")+1:name.find(")")]
                    name = name.replace(name[name.find("("):name.find(")")+1], '')
                else:
                    param = ''
                # Evaluate the expression
                expr = ''.join(chunks[2:])
                expr = expr.replace(basename, base)
                expr = expr.replace('REGS_BASE', '0x80000000')
                if param != '':
                    expr = expr.replace('(' + param + ')', '0')

                try:
                    addr = eval(expr)
                except NameError:
                    sys.exit(0)

                # Haxx and sanity check
                if addr >= int(base, 0x10):
                    if compat:
                        name = name.replace('_ADDR', '')
                    print (name, '0x%08x' % addr)