|
5 | 5 | #
|
6 | 6 | # SPDX-License-Identifier: MIT
|
7 | 7 |
|
| 8 | +import os |
8 | 9 | import re
|
9 | 10 | import sys
|
10 | 11 |
|
11 |
| -# Handle size constants with K or M suffixes (allowed in .ld but not in Python). |
12 |
| -K_PATTERN = re.compile(r"([0-9]+)[kK]") |
13 |
| -K_REPLACE = r"(\1*1024)" |
14 |
| - |
15 |
| -M_PATTERN = re.compile(r"([0-9]+)[mM]") |
16 |
| -M_REPLACE = r"(\1*1024*1024)" |
| 12 | +from elftools.elf.elffile import ELFFile |
17 | 13 |
|
18 | 14 | print()
|
19 | 15 |
|
20 |
| -text = 0 |
21 |
| -data = 0 |
22 |
| -bss = 0 |
23 |
| -# stdin is the linker output. |
24 |
| -for line in sys.stdin: |
25 |
| - # Uncomment to see linker output. |
26 |
| - # print(line) |
27 |
| - line = line.strip() |
28 |
| - if not line.startswith("text"): |
29 |
| - text, data, bss = map(int, line.split()[:3]) |
| 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 | +] |
30 | 23 |
|
31 | 24 |
|
32 | 25 | def partition_size(arg):
|
33 | 26 | if "4MB" in arg:
|
34 |
| - return "1408K" |
| 27 | + return 1408 * 1024 |
35 | 28 | else:
|
36 |
| - return "2048K" |
| 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 | 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)) |
38 | 53 |
|
39 |
| -regions = {} |
40 |
| -# This file is the linker script. |
41 |
| -with open(sys.argv[1], "r") as f: |
| 54 | +# This file is the sdkconfig |
| 55 | +with open(sys.argv[2], "r") as f: |
42 | 56 | for line in f:
|
43 | 57 | line = line.strip()
|
44 |
| - if line.startswith("CONFIG_SPIRAM_SIZE"): |
45 |
| - regions["RAM"] = line.split("=")[-1] |
46 | 58 | if line.startswith("CONFIG_PARTITION_TABLE_FILENAME"):
|
47 |
| - regions["FLASH_FIRMWARE"] = partition_size(line.split("=")[-1]) |
| 59 | + firmware_region = int(partition_size(line.split("=")[-1])) |
48 | 60 |
|
49 |
| -for region in regions: |
50 |
| - space = regions[region] |
51 |
| - if "/*" in space: |
52 |
| - space = space.split("/*")[0] |
53 |
| - space = K_PATTERN.sub(K_REPLACE, space) |
54 |
| - space = M_PATTERN.sub(M_REPLACE, space) |
55 |
| - regions[region] = int(eval(space)) |
| 61 | +# This file is the bin |
| 62 | +used_flash = os.stat(sys.argv[3]).st_size |
56 | 63 |
|
57 |
| -firmware_region = regions["FLASH_FIRMWARE"] |
58 |
| -ram_region = regions["RAM"] |
59 |
| - |
60 |
| -used_flash = data + text |
61 | 64 | free_flash = firmware_region - used_flash
|
62 |
| -used_ram = data + bss |
63 |
| -free_ram = ram_region - used_ram |
64 | 65 | print(
|
65 |
| - "{} bytes used, {} bytes free in flash firmware space out of {} bytes ({}kB).".format( |
| 66 | + "{:7} bytes used, {:7} bytes free in flash firmware space out of {} bytes ({}kB).".format( |
66 | 67 | used_flash, free_flash, firmware_region, firmware_region / 1024
|
67 | 68 | )
|
68 | 69 | )
|
69 |
| -print( |
70 |
| - "{} bytes used, {} bytes free in ram for stack and heap out of {} bytes ({}kB).".format( |
71 |
| - used_ram, free_ram, ram_region, ram_region / 1024 |
72 |
| - ) |
73 |
| -) |
| 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 | + ) |
74 | 77 | print()
|
75 | 78 |
|
76 | 79 | # Check that we have free flash space. GCC doesn't fail when the text + data
|
|
0 commit comments