From 7eb0b3c2d776e5b35b3eec1d9f20ece05f6691f1 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 12 May 2017 12:07:59 +1000 Subject: [PATCH] gen_esp32part.py: Correctly error out for non-64KB aligned app partitions Also clean up error handling for verification errors in general. Ref https://esp32.com/viewtopic.php?f=13&t=1838&p=8685#p8659 --- components/partition_table/gen_esp32part.py | 27 ++++++++++++------- .../tests/gen_esp32part_tests.py | 17 ++++++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/components/partition_table/gen_esp32part.py b/components/partition_table/gen_esp32part.py index d1c5aac63..2600ac214 100755 --- a/components/partition_table/gen_esp32part.py +++ b/components/partition_table/gen_esp32part.py @@ -155,7 +155,7 @@ class PartitionDefinition(object): MAGIC_BYTES = b"\xAA\x50" ALIGNMENT = { - APP_TYPE : 0x1000, + APP_TYPE : 0x10000, DATA_TYPE : 0x04, } @@ -241,16 +241,16 @@ class PartitionDefinition(object): def verify(self): if self.type is None: - raise ValidationError("Type field is not set") + raise ValidationError(self, "Type field is not set") if self.subtype is None: - raise ValidationError("Subtype field is not set") + raise ValidationError(self, "Subtype field is not set") if self.offset is None: - raise ValidationError("Offset field is not set") + raise ValidationError(self, "Offset field is not set") align = self.ALIGNMENT.get(self.type, 4) if self.offset % align: - raise ValidationError("%s offset 0x%x is not aligned to 0x%x" % (self.name, self.offset, align)) + raise ValidationError(self, "Offset 0x%x is not aligned to 0x%x" % (self.offset, align)) if self.size is None: - raise ValidationError("Size field is not set") + raise ValidationError(self, "Size field is not set") STRUCT_FORMAT = "<2sBBLL16sL" @@ -311,9 +311,6 @@ class PartitionDefinition(object): addr_format(self.size, True), generate_text_flags()]) -class InputError(RuntimeError): - def __init__(self, e): - super(InputError, self).__init__(e) def parse_int(v, keywords={}): """Generic parser for integer fields - int(x,0) with provision for @@ -370,6 +367,18 @@ def main(): with sys.stdout.buffer if args.output == '-' else open(args.output, 'wb') as f: f.write(output) + +class InputError(RuntimeError): + def __init__(self, e): + super(InputError, self).__init__(e) + + +class ValidationError(InputError): + def __init__(self, partition, message): + super(ValidationError, self).__init__( + "Partition %s invalid: %s" % (partition.name, message)) + + if __name__ == '__main__': try: main() diff --git a/components/partition_table/tests/gen_esp32part_tests.py b/components/partition_table/tests/gen_esp32part_tests.py index 1591c3012..46fe45c22 100755 --- a/components/partition_table/tests/gen_esp32part_tests.py +++ b/components/partition_table/tests/gen_esp32part_tests.py @@ -111,11 +111,11 @@ myota_status, data, ota,, 0x100000 def test_unit_suffixes(self): csv = """ # Name, Type, Subtype, Offset, Size -one_megabyte, app, factory, 32k, 1M +one_megabyte, app, factory, 64k, 1M """ t = PartitionTable.from_csv(csv) t.verify() - self.assertEqual(t[0].offset, 32*1024) + self.assertEqual(t[0].offset, 64*1024) self.assertEqual(t[0].size, 1*1024*1024) def test_default_offsets(self): @@ -337,5 +337,18 @@ class CommandLineTests(unittest.TestCase): pass +class VerificationTests(unittest.TestCase): + + def test_bad_alignment(self): + csv = """ +# Name,Type, SubType,Offset,Size +app,app, factory, 32K, 1M +""" + with self.assertRaisesRegexp(ValidationError, + r"Offset.+not aligned"): + t = PartitionTable.from_csv(csv) + t.verify() + + if __name__ =="__main__": unittest.main()