Rewrite mem_usage.awk in Python

Rewrite the memory usage script in Python. No functional change, except
that the script now takes tee.elf as an argument instead of processing
the output of readelf. The makefile (make mem_usage) is adjusted
accordingly.

This makes the script shorter and easier to call, and it is now
possible to add command line options.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Volodymyr Babchuk <vlad.babchuk@gmail.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Acked-by Etienne Carriere <etienne.carriere@linaro.org>
diff --git a/scripts/mem_usage.awk b/scripts/mem_usage.awk
deleted file mode 100644
index c5a8478..0000000
--- a/scripts/mem_usage.awk
+++ /dev/null
@@ -1,129 +0,0 @@
-#
-# Copyright (c) 2014, Linaro Limited
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are met:
-#
-# 1. Redistributions of source code must retain the above copyright notice,
-# this list of conditions and the following disclaimer.
-#
-# 2. Redistributions in binary form must reproduce the above copyright notice,
-# this list of conditions and the following disclaimer in the documentation
-# and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
-#
-
-BEGIN {
-	in_shdr = 0;
-	num_sects = 0;
-}
-
-/Section Headers:/ {
-	in_shdr = 1;
-	next;
-}
-
-/Key to Flags:/ {
-	in_shdr = 0;
-	next;
-}
-
-function add_section(_name, _addr, _offs, _size)
-{
-	sects[num_sects]["name"] = _name;
-	sects[num_sects]["addr"] = strtonum("0x" _addr);
-	sects[num_sects]["offs"] = strtonum("0x" _offs);
-	sects[num_sects]["size"] = strtonum("0x" _size);
-	num_sects++;
-}
-
-{
-	if (in_shdr) {
-		if ($1 == "[")
-			name_offs = 3;
-		else
-			name_offs = 2;
-
-		name = $(name_offs);
-		addr = $(name_offs + 2);
-		offs = $(name_offs + 3);
-		size = $(name_offs + 4);
-		flags = $(name_offs + 6);
-
-		if (flags == "AX" || flags == "WA" || flags == "A" ||
-		    flags == "AL") {
-			add_section(name, addr, offs, size);
-		}
-	}
-}
-
-function print_sect(_name, _addr, _size, _round_up, _print_num_pages,
-		#local variables
-		_size_kib, _num_pages)
-{
-	if (_size == 0) {
-		_size_kib = 0;
-		_num_pages = 0;
-	} else {
-		if (_round_up) {
-			_size_kib = (_size - 1) / 1024 + 1;
-		} else {
-			_size_kib = _size / 1024;
-		}
-		_num_pages = (_size - 1) / 4096 + 1;
-	}
-
-	
-
-	printf "%-16s", _name;
-	printf " %.8X - %.8X", _addr, _addr + _size;
-	printf " size %.8X %3d KiB", _size, _size_kib
-
-	if (_print_num_pages)
-		printf " %d pages", _num_pages;
-	printf "\n";
-}
-
-END {
-	for (i = 0; i < num_sects; i++ ) {
-		addr = sects[i]["addr"];
-
-		if (addr != 0) {
-			first_addr = addr;
-			break;
-		}
-	}
-
-	last_addr = sects[num_sects - 1]["addr"];
-	last_size = sects[num_sects - 1]["size"];
-	ram_usage = last_addr + last_size - first_addr;
-	print_sect("RAM Usage", first_addr, ram_usage, 1, 1);
-
-	last_addr = 0;
-	last_size = 0;
-
-	for (i = 0; i < num_sects; i++ ) {
-		name = sects[i]["name"];
-		addr = sects[i]["addr"];
-		size = sects[i]["size"];
-
-		if (last_addr != 0 && addr != last_addr + last_size)
-			print_sect("*hole*", last_addr + last_size,
-					addr - (last_addr + last_size), 0, 0);
-		print_sect(name, addr, size, 0, 0);
-		last_addr = addr;
-		last_size = size;
-	}
-}
diff --git a/scripts/mem_usage.py b/scripts/mem_usage.py
new file mode 100755
index 0000000..4309314
--- /dev/null
+++ b/scripts/mem_usage.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) 2014-2017, Linaro Limited
+# All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+import argparse
+import os
+import subprocess
+import sys
+
+
+def get_args():
+    parser = argparse.ArgumentParser(
+        description='Shows the memory layout of the TEE ELF file. '
+        'Size is provided for each section.')
+    parser.add_argument('tee_elf', help='The OP-TEE ELF file (tee.elf)')
+    return parser.parse_args()
+
+
+def printf(format, *args):
+    sys.stdout.write(format % args)
+
+
+def print_sect(name, addr, size, round_up=False, print_num_pages=False):
+    if size == 0:
+        size_kib = 0
+        num_pages = 0
+    else:
+        if round_up:
+            size_kib = (size - 1) / 1024 + 1
+        else:
+            size_kib = size / 1024
+        num_pages = (size - 1) / 4096 + 1
+
+    printf('%-16s %.8X - %.8X size %.8X %3d KiB', name, addr, addr + size,
+           size, size_kib)
+    if print_num_pages:
+        printf(' %d pages', num_pages)
+    printf('\n')
+
+
+def readelf_cmd():
+    return os.getenv('CROSS_COMPILE', '') + 'readelf'
+
+
+def main():
+    in_shdr = False
+    sects = []
+
+    args = get_args()
+    env = os.environ.copy()
+    env['LC_ALL'] = 'C'
+    readelf = subprocess.Popen([readelf_cmd(), '-S', '-W', args.tee_elf],
+                               stdout=subprocess.PIPE, env=env,
+                               universal_newlines=True)
+    for line in iter(readelf.stdout.readline, ''):
+        if 'Section Headers:' in line:
+            in_shdr = True
+            continue
+        if 'Key to Flags:' in line:
+            in_shdr = False
+            continue
+        if in_shdr:
+            words = line.split()
+            if words[0] == '[':
+                words.pop(0)
+            try:
+                (_, name, _, addr, offs, size, _,
+                 flags) = words[:8]
+            except:
+                continue
+            if (flags == 'AX' or flags == 'WA' or flags == 'A' or
+                    flags == 'AL'):
+                sects.append({'name': name, 'addr': addr,
+                              'offs': offs, 'size': size})
+    for sect in sects:
+        if sect['addr'] != 0:
+            first_addr = sect['addr']
+            break
+    last_addr = sects[-1]['addr']
+    last_size = sects[-1]['size']
+
+    ram_usage = int(last_addr, 16) + int(last_size, 16) - int(first_addr, 16)
+    print_sect('RAM Usage', int(first_addr, 16), ram_usage, True, True)
+
+    last_addr = 0
+    last_size = 0
+    for sect in sects:
+        name = sect['name']
+        addr = int(sect['addr'], 16)
+        size = int(sect['size'], 16)
+
+        if last_addr != 0 and addr != last_addr + last_size:
+            print_sect('*hole*', last_addr + last_size,
+                       addr - (last_addr + last_size))
+        print_sect(name, addr, size)
+        last_addr = addr
+        last_size = size
+
+
+if __name__ == "__main__":
+    main()