1#!/usr/bin/env python3
2#
3# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import re
9import os
10import sys
11import operator
12
13# List of folder/map to parse
14bl_images = ['bl1', 'bl2', 'bl31']
15
16# List of symbols to search for
17blx_symbols = ['__BL1_RAM_START__', '__BL1_RAM_END__',
18                '__BL2_END__',
19                '__BL31_END__',
20                '__TEXT_START__', '__TEXT_END__',
21                '__RODATA_START__', '__RODATA_END__',
22                '__DATA_START__', '__DATA_END__',
23                '__STACKS_START__', '__STACKS_END__',
24                '__BSS_END',
25                '__COHERENT_RAM_START__', '__COHERENT_RAM_END__',
26               ]
27
28# Regex to extract address from map file
29address_pattern = re.compile(r"\b0x\w*")
30
31# List of found element: [address, symbol, file]
32address_list = []
33
34# Get the directory from command line or use a default one
35inverted_print = True
36if len(sys.argv) >= 2:
37    build_dir = sys.argv[1]
38    if len(sys.argv) >= 3:
39        inverted_print = sys.argv[2] == '0'
40else:
41    build_dir = 'build/fvp/debug'
42
43# Extract all the required symbols from the map files
44for image in bl_images:
45    file_path = os.path.join(build_dir, image, '{}.map'.format(image))
46    if os.path.isfile(file_path):
47        with open (file_path, 'rt') as mapfile:
48            for line in mapfile:
49                for symbol in blx_symbols:
50                    # Regex to find symbol definition
51                    line_pattern = re.compile(r"\b0x\w*\s*" + symbol + "\s= .")
52                    match = line_pattern.search(line)
53                    if match:
54                        # Extract address from line
55                        match = address_pattern.search(line)
56                        if match:
57                            address_list.append([match.group(0), symbol, image])
58
59# Sort by address
60address_list.sort(key=operator.itemgetter(0))
61
62# Invert list for lower address at bottom
63if inverted_print:
64    address_list = reversed(address_list)
65
66# Generate memory view
67print('{:-^93}'.format('Memory Map from: ' + build_dir))
68for address in address_list:
69    if "bl1" in address[2]:
70        print(address[0], '+{:-^22}+ |{:^22}| |{:^22}|'.format(address[1], '', ''))
71    elif "bl2" in address[2]:
72        print(address[0], '|{:^22}| +{:-^22}+ |{:^22}|'.format('', address[1], ''))
73    elif "bl31" in address[2]:
74        print(address[0], '|{:^22}| |{:^22}| +{:-^22}+'.format('', '', address[1]))
75    else:
76        print(address[0], '|{:^22}| |{:^22}| +{:-^22}+'.format('', '', address[1]))
77
78print('{:^20}{:_^22}   {:_^22}   {:_^22}'.format('', '', '', ''))
79print('{:^20}{:^22}   {:^22}   {:^22}'.format('address', 'bl1', 'bl2', 'bl31'))
80