|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 4 | +# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors) |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: MIT |
| 7 | + |
| 8 | +import os |
| 9 | +import re |
| 10 | +import sys |
| 11 | + |
| 12 | +from elftools.elf.elffile import ELFFile |
| 13 | + |
| 14 | +print() |
| 15 | + |
| 16 | +internal_memory = [ |
| 17 | + # Name, Start, Length |
| 18 | + ("RTC Fast Memory", (0x3FF9_E000, 0x4007_0000), 8 * 1024), |
| 19 | + ("RTC Slow Memory", (0x5000_0000,), 8 * 1024), |
| 20 | + ("Internal SRAM 0", (0x3FFB_0000, 0x4002_0000), 32 * 1024), |
| 21 | + ("Internal SRAM 1", (0x3FFB_8000, 0x4002_8000), 288 * 1024), |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +def partition_size(arg): |
| 26 | + if "4MB" in arg: |
| 27 | + return 1408 * 1024 |
| 28 | + else: |
| 29 | + return 2048 * 1024 |
| 30 | + |
| 31 | + |
| 32 | +def align(n, m): |
| 33 | + return m * ((n + m - 1) // m) |
| 34 | + |
| 35 | + |
| 36 | +regions = dict((name, 0) for name, _, _ in internal_memory) |
| 37 | + |
| 38 | +# This file is the elf |
| 39 | +with open(sys.argv[1], "rb") as stream: |
| 40 | + elffile = ELFFile(stream) |
| 41 | + for section in elffile.iter_sections(): |
| 42 | + start = section["sh_addr"] |
| 43 | + size = section["sh_size"] |
| 44 | + offset = section["sh_offset"] |
| 45 | + if not size or not start: |
| 46 | + continue |
| 47 | + for name, starts, length in internal_memory: |
| 48 | + for mem_start in starts: |
| 49 | + mem_end = mem_start + length |
| 50 | + if start >= mem_start and start < mem_end: |
| 51 | + regions[name] = max(regions.get(name, 0), size) |
| 52 | + # print("# putting %s in %s (start=0x%x, size=%d)" % (section.name, name, start, size)) |
| 53 | + |
| 54 | +# This file is the sdkconfig |
| 55 | +with open(sys.argv[2], "r") as f: |
| 56 | + for line in f: |
| 57 | + line = line.strip() |
| 58 | + if line.startswith("CONFIG_PARTITION_TABLE_FILENAME"): |
| 59 | + firmware_region = int(partition_size(line.split("=")[-1])) |
| 60 | + |
| 61 | +# This file is the bin |
| 62 | +used_flash = os.stat(sys.argv[3]).st_size |
| 63 | + |
| 64 | +free_flash = firmware_region - used_flash |
| 65 | +print( |
| 66 | + "{:7} bytes used, {:7} bytes free in flash firmware space out of {} bytes ({}kB).".format( |
| 67 | + used_flash, free_flash, firmware_region, firmware_region / 1024 |
| 68 | + ) |
| 69 | +) |
| 70 | +for name, mem_start, length in internal_memory: |
| 71 | + if name in regions: |
| 72 | + print( |
| 73 | + "{:7} bytes used, {:7} bytes free in '{}' out of {} bytes ({}kB).".format( |
| 74 | + regions[name], length - regions[name], name, length, length / 1024 |
| 75 | + ) |
| 76 | + ) |
| 77 | +print() |
| 78 | + |
| 79 | +# Check that we have free flash space. GCC doesn't fail when the text + data |
| 80 | +# sections don't fit in FLASH. It only counts data in RAM. |
| 81 | +if free_flash < 0: |
| 82 | + print("Too little flash!!!") |
| 83 | + print() |
| 84 | + sys.exit(-1) |
0 commit comments