Skip to content

Commit

Permalink
esp32/makeimg.py: Get bootloader and partition offset from sdkconfig.
Browse files Browse the repository at this point in the history
So that it works on ESP32C3, which has the bootloader at 0x0.

Fixes issue #7565.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Aug 9, 2021
1 parent 8645b7c commit 3835f5f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions ports/esp32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ endif
all:
idf.py $(IDFPY_FLAGS) build
@$(PYTHON) makeimg.py \
$(BUILD)/sdkconfig \
$(BUILD)/bootloader/bootloader.bin \
$(BUILD)/partition_table/partition-table.bin \
$(BUILD)/micropython.bin \
Expand Down
48 changes: 38 additions & 10 deletions ports/esp32/makeimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,65 @@

import gen_esp32part

OFFSET_BOOTLOADER = 0x1000
OFFSET_PARTITIONS = 0x8000
OFFSET_BOOTLOADER_DEFAULT = 0x1000
OFFSET_PARTITIONS_DEFAULT = 0x8000


def load_sdkconfig_hex_value(filename, value, default):
value = "CONFIG_" + value + "="
with open(filename, "r") as f:
for line in f:
if line.startswith(value):
return int(line.split("=", 1)[1], 16)
return default


def load_partition_table(filename):
with open(filename, "rb") as f:
return gen_esp32part.PartitionTable.from_binary(f.read())


partition_table = load_partition_table(sys.argv[2])
# Extract command-line arguments.
arg_sdkconfig = sys.argv[1]
arg_bootloader_bin = sys.argv[2]
arg_partitions_bin = sys.argv[3]
arg_application_bin = sys.argv[4]
arg_output_bin = sys.argv[5]

# Load required sdkconfig values.
offset_bootloader = load_sdkconfig_hex_value(
arg_sdkconfig, "BOOTLOADER_OFFSET_IN_FLASH", OFFSET_BOOTLOADER_DEFAULT
)
offset_partitions = load_sdkconfig_hex_value(
arg_sdkconfig, "PARTITION_TABLE_OFFSET", OFFSET_PARTITIONS_DEFAULT
)

# Load the partition table.
partition_table = load_partition_table(arg_partitions_bin)

max_size_bootloader = OFFSET_PARTITIONS - OFFSET_BOOTLOADER
max_size_bootloader = offset_partitions - offset_bootloader
max_size_partitions = 0
offset_application = 0
max_size_application = 0

# Inspect the partition table to find offsets and maximum sizes.
for part in partition_table:
if part.name == "nvs":
max_size_partitions = part.offset - OFFSET_PARTITIONS
max_size_partitions = part.offset - offset_partitions
elif part.type == gen_esp32part.APP_TYPE and offset_application == 0:
offset_application = part.offset
max_size_application = part.size

# Define the input files, their location and maximum size.
files_in = [
("bootloader", OFFSET_BOOTLOADER, max_size_bootloader, sys.argv[1]),
("partitions", OFFSET_PARTITIONS, max_size_partitions, sys.argv[2]),
("application", offset_application, max_size_application, sys.argv[3]),
("bootloader", offset_bootloader, max_size_bootloader, arg_bootloader_bin),
("partitions", offset_partitions, max_size_partitions, arg_partitions_bin),
("application", offset_application, max_size_application, arg_application_bin),
]
file_out = sys.argv[4]
file_out = arg_output_bin

cur_offset = OFFSET_BOOTLOADER
# Write output file with combined firmware.
cur_offset = offset_bootloader
with open(file_out, "wb") as fout:
for name, offset, max_size, file_in in files_in:
assert offset >= cur_offset
Expand Down

0 comments on commit 3835f5f

Please sign in to comment.