Skip to content

Commit 865abba

Browse files
committed
esp32/makeimg.py: Load sizes from partition table and verify data fits.
Changes introduced are: - the application offset is now loaded from the partition table instead of being hard-coded to 0x10000 - maximum size of all sections is computed using the partition table - an error is generated if any section overflows its allocated space - remaining bytes are printed for each section Signed-off-by: Damien George <[email protected]>
1 parent 61f91de commit 865abba

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

ports/esp32/makeimg.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,59 @@
1-
import sys
1+
# Combine bootloader, partition table and application into a final binary.
2+
3+
import os, sys
4+
5+
sys.path.append(os.getenv("IDF_PATH") + "/components/partition_table")
6+
7+
import gen_esp32part
28

39
OFFSET_BOOTLOADER = 0x1000
410
OFFSET_PARTITIONS = 0x8000
5-
OFFSET_APPLICATION = 0x10000
11+
12+
13+
def load_partition_table(filename):
14+
with open(filename, "rb") as f:
15+
return gen_esp32part.PartitionTable.from_binary(f.read())
16+
17+
18+
partition_table = load_partition_table(sys.argv[2])
19+
20+
max_size_bootloader = OFFSET_PARTITIONS - OFFSET_BOOTLOADER
21+
max_size_partitions = 0
22+
offset_application = 0
23+
max_size_application = 0
24+
25+
for part in partition_table:
26+
if part.name == "nvs":
27+
max_size_partitions = part.offset - OFFSET_PARTITIONS
28+
elif part.type == gen_esp32part.APP_TYPE and offset_application == 0:
29+
offset_application = part.offset
30+
max_size_application = part.size
631

732
files_in = [
8-
("bootloader", OFFSET_BOOTLOADER, sys.argv[1]),
9-
("partitions", OFFSET_PARTITIONS, sys.argv[2]),
10-
("application", OFFSET_APPLICATION, sys.argv[3]),
33+
("bootloader", OFFSET_BOOTLOADER, max_size_bootloader, sys.argv[1]),
34+
("partitions", OFFSET_PARTITIONS, max_size_partitions, sys.argv[2]),
35+
("application", offset_application, max_size_application, sys.argv[3]),
1136
]
1237
file_out = sys.argv[4]
1338

1439
cur_offset = OFFSET_BOOTLOADER
1540
with open(file_out, "wb") as fout:
16-
for name, offset, file_in in files_in:
41+
for name, offset, max_size, file_in in files_in:
1742
assert offset >= cur_offset
1843
fout.write(b"\xff" * (offset - cur_offset))
1944
cur_offset = offset
2045
with open(file_in, "rb") as fin:
2146
data = fin.read()
2247
fout.write(data)
2348
cur_offset += len(data)
24-
print("%-12s% 8d" % (name, len(data)))
25-
print("%-12s% 8d" % ("total", cur_offset))
49+
print(
50+
"%-12s@0x%06x % 8d (% 8d remaining)"
51+
% (name, offset, len(data), max_size - len(data))
52+
)
53+
if len(data) > max_size:
54+
print(
55+
"ERROR: %s overflows allocated space of %d bytes by %d bytes"
56+
% (name, max_size, len(data) - max_size)
57+
)
58+
sys.exit(1)
59+
print("%-22s% 8d" % ("total", cur_offset))

ports/esp32/partitions-16MiB.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Notes: the offset of the partition table itself is set in
2-
# $ESPIDF/components/partition_table/Kconfig.projbuild and the
3-
# offset of the factory/ota_0 partition is set in makeimg.py
2+
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
43
# Name, Type, SubType, Offset, Size, Flags
54
nvs, data, nvs, 0x9000, 0x6000,
65
phy_init, data, phy, 0xf000, 0x1000,

ports/esp32/partitions-2MiB.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Notes: the offset of the partition table itself is set in
2-
# $ESPIDF/components/partition_table/Kconfig.projbuild and the
3-
# offset of the factory/ota_0 partition is set in makeimg.py
2+
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
43
# Name, Type, SubType, Offset, Size, Flags
54
nvs, data, nvs, 0x9000, 0x6000,
65
phy_init, data, phy, 0xf000, 0x1000,

ports/esp32/partitions-ota.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Partition table for MicroPython with OTA support using 4MB flash
22
# Notes: the offset of the partition table itself is set in
3-
# $ESPIDF/components/partition_table/Kconfig.projbuild and the
4-
# offset of the factory/ota_0 partition is set in makeimg.py
3+
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
54
# Name, Type, SubType, Offset, Size, Flags
65
nvs, data, nvs, 0x9000, 0x4000,
76
otadata, data, ota, 0xd000, 0x2000,

ports/esp32/partitions.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Notes: the offset of the partition table itself is set in
2-
# $ESPIDF/components/partition_table/Kconfig.projbuild and the
3-
# offset of the factory/ota_0 partition is set in makeimg.py
2+
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
43
# Name, Type, SubType, Offset, Size, Flags
54
nvs, data, nvs, 0x9000, 0x6000,
65
phy_init, data, phy, 0xf000, 0x1000,

0 commit comments

Comments
 (0)