Skip to content

Commit a971c13

Browse files
Merge branch 'bugfix/secure_boot_v2_part_size_check_v4.4' into 'release/v4.4'
gen_esp32part: allow secure boot v2 based app partition size 4K aligned (v4.4) See merge request espressif/esp-idf!22286
2 parents 62340ef + 379d903 commit a971c13

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

components/partition_table/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ if(CONFIG_ESPTOOLPY_FLASHSIZE)
2525
endif()
2626

2727
if(CONFIG_SECURE_BOOT AND NOT CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION)
28-
set(partition_secure_opt --secure)
28+
if(CONFIG_SECURE_BOOT_V2_ENABLED)
29+
set(partition_secure_opt --secure v2)
30+
else()
31+
set(partition_secure_opt --secure v1)
32+
endif()
2933
else()
3034
set(partition_secure_opt "")
3135
endif()

components/partition_table/Makefile.projbuild

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ endif
2121
PARTITION_SECURE_OPT :=
2222
ifdef CONFIG_SECURE_BOOT
2323
ifndef CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION
24-
PARTITION_SECURE_OPT += --secure
24+
ifdef CONFIG_SECURE_BOOT_V2_ENABLED
25+
PARTITION_SECURE_OPT += --secure v2
26+
else
27+
PARTITION_SECURE_OPT += --secure v1
28+
endif
2529
endif
2630
endif
2731

components/partition_table/gen_esp32part.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
MIN_PARTITION_SUBTYPE_APP_OTA = 0x10
2929
NUM_PARTITION_SUBTYPE_APP_OTA = 16
3030

31+
SECURE_NONE = None
32+
SECURE_V1 = 'v1'
33+
SECURE_V2 = 'v2'
34+
3135
__version__ = '1.2'
3236

3337
APP_TYPE = 0x00
@@ -91,13 +95,26 @@ def get_subtype_as_int(ptype, subtype):
9195
STRICT_DATA_ALIGNMENT = 0x1000
9296

9397

94-
def get_alignment_for_type(ptype):
98+
def get_alignment_offset_for_type(ptype):
9599
return ALIGNMENT.get(ptype, ALIGNMENT[DATA_TYPE])
96100

97101

102+
def get_alignment_size_for_type(ptype):
103+
if ptype == APP_TYPE and secure == SECURE_V1:
104+
# For secure boot v1 case, app partition must be 64K aligned
105+
# signature block (68 bytes) lies at the very end of 64K block
106+
return 0x10000
107+
if ptype == APP_TYPE and secure == SECURE_V2:
108+
# For secure boot v2 case, app partition must be 4K aligned
109+
# signature block (4K) is kept after padding the unsigned image to 64K boundary
110+
return 0x1000
111+
# No specific size alignement requirement as such
112+
return 0x1
113+
114+
98115
quiet = False
99116
md5sum = True
100-
secure = False
117+
secure = SECURE_NONE
101118
offset_part_table = 0
102119

103120

@@ -164,7 +181,7 @@ def expand_vars(f):
164181
raise InputError('CSV Error: Partitions overlap. Partition at line %d sets offset 0x%x. Previous partition ends 0x%x'
165182
% (e.line_no, e.offset, last_end))
166183
if e.offset is None:
167-
pad_to = get_alignment_for_type(e.type)
184+
pad_to = get_alignment_offset_for_type(e.type)
168185
if last_end % pad_to != 0:
169186
last_end += pad_to - (last_end % pad_to)
170187
e.offset = last_end
@@ -397,18 +414,20 @@ def verify(self):
397414
raise ValidationError(self, 'Subtype field is not set')
398415
if self.offset is None:
399416
raise ValidationError(self, 'Offset field is not set')
400-
align = get_alignment_for_type(self.type)
401-
if self.offset % align:
402-
raise ValidationError(self, 'Offset 0x%x is not aligned to 0x%x' % (self.offset, align))
417+
if self.size is None:
418+
raise ValidationError(self, 'Size field is not set')
419+
offset_align = get_alignment_offset_for_type(self.type)
420+
if self.offset % offset_align:
421+
raise ValidationError(self, 'Offset 0x%x is not aligned to 0x%x' % (self.offset, offset_align))
403422
# The alignment requirement for non-app partition is 4 bytes, but it should be 4 kB.
404423
# Print a warning for now, make it an error in IDF 5.0 (IDF-3742).
405424
if self.type != APP_TYPE and self.offset % STRICT_DATA_ALIGNMENT:
406425
critical('WARNING: Partition %s not aligned to 0x%x.'
407426
'This is deprecated and will be considered an error in the future release.' % (self.name, STRICT_DATA_ALIGNMENT))
408-
if self.size % align and secure and self.type == APP_TYPE:
409-
raise ValidationError(self, 'Size 0x%x is not aligned to 0x%x' % (self.size, align))
410-
if self.size is None:
411-
raise ValidationError(self, 'Size field is not set')
427+
if self.type == APP_TYPE and secure is not SECURE_NONE:
428+
size_align = get_alignment_size_for_type(self.type)
429+
if self.size % size_align:
430+
raise ValidationError(self, 'Size 0x%x is not aligned to 0x%x' % (self.size, size_align))
412431

413432
if self.name in TYPES and TYPES.get(self.name, '') != self.type:
414433
critical("WARNING: Partition has name '%s' which is a partition type, but does not match this partition's "
@@ -513,7 +532,7 @@ def main():
513532
'enabled by default and this flag does nothing.', action='store_true')
514533
parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
515534
parser.add_argument('--offset', '-o', help='Set offset partition table', default='0x8000')
516-
parser.add_argument('--secure', help='Require app partitions to be suitable for secure boot', action='store_true')
535+
parser.add_argument('--secure', help='Require app partitions to be suitable for secure boot', nargs='?', const=SECURE_V1, choices=[SECURE_V1, SECURE_V2])
517536
parser.add_argument('input', help='Path to CSV or binary file to parse.', type=argparse.FileType('rb'))
518537
parser.add_argument('output', help='Path to output converted binary or CSV file. Will use stdout if omitted.',
519538
nargs='?', default='-')

components/partition_table/test_gen_esp32part_host/gen_esp32part_tests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,38 @@ def test_basic_cmdline(self):
389389

390390
class VerificationTests(Py23TestCase):
391391

392+
def _run_genesp32(self, csvcontents, args):
393+
csvpath = tempfile.mktemp()
394+
with open(csvpath, 'w') as f:
395+
f.write(csvcontents)
396+
try:
397+
output = subprocess.check_output([sys.executable, '../gen_esp32part.py', csvpath] + args, stderr=subprocess.STDOUT)
398+
return output.strip()
399+
except subprocess.CalledProcessError as e:
400+
return e.output.strip()
401+
finally:
402+
os.remove(csvpath)
403+
404+
def test_check_secure_app_size(self):
405+
sample_csv = """
406+
ota_0, app, ota_0, , 0x101000
407+
ota_1, app, ota_1, , 0x100800
408+
"""
409+
410+
def rge(args):
411+
return self._run_genesp32(sample_csv, args)
412+
413+
# Valid test that would pass with the above partition table
414+
partfile = tempfile.mktemp()
415+
self.assertEqual(rge([partfile]), b'Parsing CSV input...\nVerifying table...')
416+
os.remove(partfile)
417+
# Failure case 1, incorrect ota_0 partition size
418+
self.assertEqual(rge(['-q', '--secure', 'v1']),
419+
b'Partition ota_0 invalid: Size 0x101000 is not aligned to 0x10000')
420+
# Failure case 2, incorrect ota_1 partition size
421+
self.assertEqual(rge(['-q', '--secure', 'v2']),
422+
b'Partition ota_1 invalid: Size 0x100800 is not aligned to 0x1000')
423+
392424
def test_bad_alignment(self):
393425
csv = """
394426
# Name,Type, SubType,Offset,Size

0 commit comments

Comments
 (0)