Skip to content

Commit 4017cf3

Browse files
committed
partition_table: Optionally disable the MD5 checksum in partition tables
1 parent 63ddae5 commit 4017cf3

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

components/partition_table/Kconfig.projbuild

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ config PHY_DATA_OFFSET
6262
default PARTITION_TABLE_CUSTOM_PHY_DATA_OFFSET if PARTITION_TABLE_CUSTOM
6363
default 0xf000 # this is the factory app offset used by the default tables
6464

65+
config PARTITION_TABLE_MD5
66+
bool "Generate an MD5 checksum for the partition table"
67+
default y
68+
help
69+
Generate an MD5 checksum for the partition table for protecting the
70+
integrity of the table. The generation should be turned off for legacy
71+
bootloaders which cannot recognize the MD5 checksum in the partition
72+
table.
73+
6574
endmenu
6675

6776

components/partition_table/Makefile.projbuild

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
#
99
.PHONY: partition_table partition_table-flash partition_table-clean
1010

11+
ifneq ("$(CONFIG_PARTITION_TABLE_MD5)", "y")
12+
MD5_OPT ?= "--disable-md5sum"
13+
endif
14+
1115
# NB: gen_esp32part.py lives in the sdk/bin/ dir not component dir
12-
GEN_ESP32PART := $(PYTHON) $(COMPONENT_PATH)/gen_esp32part.py -q
16+
GEN_ESP32PART := $(PYTHON) $(COMPONENT_PATH)/gen_esp32part.py -q $(MD5_OPT)
1317

1418
# Has a matching value in bootloader_support esp_flash_partitions.h
1519
PARTITION_TABLE_OFFSET := 0x8000

components/partition_table/gen_esp32part.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
__version__ = '1.0'
3636

3737
quiet = False
38+
md5sum = True
3839

3940
def status(msg):
4041
""" Print status message to stderr """
@@ -123,7 +124,7 @@ def from_binary(cls, b):
123124
raise InputError("Partition table length must be a multiple of 32 bytes")
124125
if data == b'\xFF'*32:
125126
return result # got end marker
126-
if data[:2] == MD5_PARTITION_BEGIN[:2]: #check only the magic number part
127+
if md5sum and data[:2] == MD5_PARTITION_BEGIN[:2]: #check only the magic number part
127128
if data[16:] == md5.digest():
128129
continue # the next iteration will check for the end marker
129130
else:
@@ -135,7 +136,8 @@ def from_binary(cls, b):
135136

136137
def to_binary(self):
137138
result = b"".join(e.to_binary() for e in self)
138-
result += MD5_PARTITION_BEGIN + hashlib.md5(result).digest()
139+
if md5sum:
140+
result += MD5_PARTITION_BEGIN + hashlib.md5(result).digest()
139141
if len(result )>= MAX_PARTITION_LENGTH:
140142
raise InputError("Binary partition table length (%d) longer than max" % len(result))
141143
result += b"\xFF" * (MAX_PARTITION_LENGTH - len(result)) # pad the sector, for signing
@@ -345,8 +347,10 @@ def parse_int(v, keywords={}):
345347

346348
def main():
347349
global quiet
350+
global md5sum
348351
parser = argparse.ArgumentParser(description='ESP32 partition table utility')
349352

353+
parser.add_argument('--disable-md5sum', help='Disable md5 checksum for the partition table', default=False, action='store_true')
350354
parser.add_argument('--verify', '-v', help='Verify partition table fields', default=True, action='store_false')
351355
parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true')
352356

@@ -358,6 +362,7 @@ def main():
358362
args = parser.parse_args()
359363

360364
quiet = args.quiet
365+
md5sum = not args.disable_md5sum
361366
input = args.input.read()
362367
input_is_binary = input[0:2] == PartitionDefinition.MAGIC_BYTES
363368
if input_is_binary:

docs/api-guides/partition-tables.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ MD5 checksum
153153

154154
The binary format of the partition table contains an MD5 checksum computed based on the partition table. This checksum is used for checking the integrity of the partition table during the boot.
155155

156+
The MD5 checksum generation can be disabled by the ``--disable-md5sum`` option of ``gen_esp32part.py`` or by the :ref:`CONFIG_PARTITION_TABLE_MD5` option. This is useful for example when one uses a legacy bootloader which cannot process MD5 checksums and the boot fails with the error message ``invalid magic number 0xebeb``.
157+
156158
Flashing the partition table
157159
----------------------------
158160

0 commit comments

Comments
 (0)