refactoring: Employ Black code formatting

This commit is contained in:
radim.karnis
2022-03-04 17:25:24 +01:00
parent b57f69bd13
commit 45f1da954e
81 changed files with 8244 additions and 3612 deletions

View File

@@ -31,7 +31,7 @@ def try_delete(path):
def segment_matches_section(segment, section):
""" segment is an ImageSegment from an esptool binary.
"""segment is an ImageSegment from an esptool binary.
section is an elftools ELF section
Returns True if they match
@@ -41,7 +41,6 @@ def segment_matches_section(segment, section):
class BaseTestCase(unittest.TestCase):
def assertEqualHex(self, expected, actual, message=None):
try:
expected = hex(expected)
@@ -66,8 +65,14 @@ class BaseTestCase(unittest.TestCase):
data = section.data()
# no section should start at the same address as the ELF section.
for seg in sorted(image.segments, key=lambda s: s.addr):
print("comparing seg 0x%x sec 0x%x len 0x%x" % (seg.addr, sh_addr, len(data)))
self.assertFalse(seg.addr == sh_addr, "%s should not be in the binary image" % section_name)
print(
"comparing seg 0x%x sec 0x%x len 0x%x"
% (seg.addr, sh_addr, len(data))
)
self.assertFalse(
seg.addr == sh_addr,
"%s should not be in the binary image" % section_name,
)
def assertImageContainsSection(self, image, elf, section_name):
"""
@@ -85,18 +90,28 @@ class BaseTestCase(unittest.TestCase):
# as we find it in the image segments. When we're done 'data' should
# all be accounted for
for seg in sorted(image.segments, key=lambda s: s.addr):
print("comparing seg 0x%x sec 0x%x len 0x%x" % (seg.addr, sh_addr, len(data)))
print(
"comparing seg 0x%x sec 0x%x len 0x%x"
% (seg.addr, sh_addr, len(data))
)
if seg.addr == sh_addr:
overlap_len = min(len(seg.data), len(data))
self.assertEqual(data[:overlap_len], seg.data[:overlap_len],
"ELF '%s' section has mis-matching binary image data" % section_name)
self.assertEqual(
data[:overlap_len],
seg.data[:overlap_len],
"ELF '%s' section has mis-matching binary image data"
% section_name,
)
sh_addr += overlap_len
data = data[overlap_len:]
# no bytes in 'data' should be left unmatched
self.assertEqual(0, len(data),
"ELF %s section '%s' has no encompassing segment(s) in binary image (image segments: %s)"
% (elf, section_name, image.segments))
self.assertEqual(
0,
len(data),
"ELF %s section '%s' has no encompassing segment(s) in binary image "
"(image segments: %s)" % (elf, section_name, image.segments),
)
def assertImageInfo(self, binpath, chip="esp8266"):
"""
@@ -111,10 +126,12 @@ class BaseTestCase(unittest.TestCase):
print(e.output)
raise
self.assertFalse("invalid" in output, "Checksum calculation should be valid")
self.assertFalse("warning" in output.lower(), "Should be no warnings in image_info output")
self.assertFalse(
"warning" in output.lower(), "Should be no warnings in image_info output"
)
def run_elf2image(self, chip, elf_path, version=None, extra_args=[]):
""" Run elf2image on elf_path """
"""Run elf2image on elf_path"""
cmd = [sys.executable, ESPTOOL_PY, "--chip", chip, "elf2image"]
if version is not None:
cmd += ["--version", str(version)]
@@ -123,7 +140,9 @@ class BaseTestCase(unittest.TestCase):
try:
output = str(subprocess.check_output(cmd))
print(output)
self.assertFalse("warning" in output.lower(), "elf2image should not output warnings")
self.assertFalse(
"warning" in output.lower(), "elf2image should not output warnings"
)
except subprocess.CalledProcessError as e:
print(e.output)
raise
@@ -145,9 +164,11 @@ class ESP8266V1ImageTests(BaseTestCase):
with open(self.ELF, "rb") as f:
e = ELFFile(f)
irom_section = e.get_section_by_name(".irom0.text")
self.assertEqual(irom_section.header.sh_size,
os.stat(self.BIN_IROM).st_size,
"IROM raw binary file should be same length as .irom0.text section")
self.assertEqual(
irom_section.header.sh_size,
os.stat(self.BIN_IROM).st_size,
"IROM raw binary file should be same length as .irom0.text section",
)
def test_loaded_sections(self):
image = esptool.bin_image.LoadFirmwareImage("esp8266", self.BIN_LOAD)
@@ -162,8 +183,9 @@ class ESP8266V1ImageTests(BaseTestCase):
class ESP8266V12SectionHeaderNotAtEnd(BaseTestCase):
""" Ref https://github.com/espressif/esptool/issues/197 -
this ELF image has the section header not at the end of the file """
"""Ref https://github.com/espressif/esptool/issues/197 -
this ELF image has the section header not at the end of the file"""
ELF = "esp8266-nonossdkv12-example.elf"
BIN_LOAD = ELF + "-0x00000.bin"
BIN_IROM = ELF + "-0x40000.bin"
@@ -182,7 +204,6 @@ class ESP8266V12SectionHeaderNotAtEnd(BaseTestCase):
class ESP8266V2ImageTests(BaseTestCase):
def _test_elf2image(self, elfpath, binpath, mergedsections=[]):
try:
self.run_elf2image("esp8266", elfpath, 2)
@@ -198,13 +219,20 @@ class ESP8266V2ImageTests(BaseTestCase):
self.assertImageDoesNotContainSection(image, elfpath, sec)
irom_segment = image.segments[0]
self.assertEqual(0, irom_segment.addr,
"IROM segment 'load address' should be zero")
self.assertEqual(
0, irom_segment.addr, "IROM segment 'load address' should be zero"
)
with open(elfpath, "rb") as f:
e = ELFFile(f)
sh_size = (e.get_section_by_name(".irom0.text").header.sh_size + 15) & ~15
self.assertEqual(len(irom_segment.data), sh_size, "irom segment (0x%x) should be same size (16 padded) as .irom0.text section (0x%x)"
% (len(irom_segment.data), sh_size))
sh_size = (
e.get_section_by_name(".irom0.text").header.sh_size + 15
) & ~15
self.assertEqual(
len(irom_segment.data),
sh_size,
"irom segment (0x%x) should be same size (16 padded) "
"as .irom0.text section (0x%x)" % (len(irom_segment.data), sh_size),
)
# check V2 CRC (for ESP8266 SDK bootloader)
with open(binpath, "rb") as f:
@@ -248,8 +276,7 @@ class ESP32ImageTests(BaseTestCase):
BIN = "esp32-bootloader.bin"
image = self._test_elf2image(ELF, BIN)
self.assertEqual(3, len(image.segments))
for section in [".iram1.text", ".iram_pool_1.text",
".dram0.rodata"]:
for section in [".iram1.text", ".iram_pool_1.text", ".dram0.rodata"]:
self.assertImageContainsSection(image, ELF, section)
def test_app_template(self):
@@ -260,8 +287,12 @@ class ESP32ImageTests(BaseTestCase):
# equal 5 (instead of 6).
self.assertEqual(5, len(image.segments))
# the other segment is a padding or merged segment
for section in [".iram0.vectors", ".dram0.data",
".flash.rodata", ".flash.text"]:
for section in [
".iram0.vectors",
".dram0.data",
".flash.rodata",
".flash.text",
]:
self.assertImageContainsSection(image, ELF, section)
# check that merged sections are not in the binary image
for mergedsection in [".iram0.text"]:
@@ -279,7 +310,8 @@ class ESP32ImageTests(BaseTestCase):
def test_use_segments(self):
ELF = "esp32-zephyr.elf"
BIN = "esp32-zephyr.bin"
# default behaviour uses ELF sections, this ELF will produce 8 segments in the bin
# default behaviour uses ELF sections,
# this ELF will produce 8 segments in the bin
image = self._test_elf2image(ELF, BIN)
# Adjacent sections are now merged, len(image.segments) should
# equal 4 (instead of 8).
@@ -295,11 +327,16 @@ class ESP8266FlashHeaderTests(BaseTestCase):
ELF = "esp8266-nonossdkv20-at-v2.elf"
BIN = "esp8266-nonossdkv20-at-v2-0x01000.bin"
try:
self.run_elf2image("esp8266", ELF, version=2, extra_args=["--flash_size", "2MB", "--flash_mode", "dio"])
self.run_elf2image(
"esp8266",
ELF,
version=2,
extra_args=["--flash_size", "2MB", "--flash_mode", "dio"],
)
with open(BIN, "rb") as f:
header = f.read(4)
print("header %r" % header)
self.assertEqualHex(0xea, header[0])
self.assertEqualHex(0xEA, header[0])
self.assertEqualHex(0x02, header[2])
self.assertEqualHex(0x30, header[3])
finally:
@@ -311,10 +348,21 @@ class ESP32FlashHeaderTests(BaseTestCase):
ELF = "esp32-app-template.elf"
BIN = "esp32-app-template.bin"
try:
self.run_elf2image("esp32", ELF, extra_args=["--flash_size", "16MB", "--flash_mode", "dio", "--min-rev", "1"])
self.run_elf2image(
"esp32",
ELF,
extra_args=[
"--flash_size",
"16MB",
"--flash_mode",
"dio",
"--min-rev",
"1",
],
)
with open(BIN, "rb") as f:
header = f.read(24)
self.assertEqualHex(0xe9, header[0])
self.assertEqualHex(0xE9, header[0])
self.assertEqualHex(0x02, header[2])
self.assertEqualHex(0x40, header[3])
self.assertEqualHex(0x01, header[14]) # chip revision
@@ -324,7 +372,7 @@ class ESP32FlashHeaderTests(BaseTestCase):
class ELFSHA256Tests(BaseTestCase):
ELF = "esp32-app-cust-ver-info.elf"
SHA_OFFS = 0xb0 # absolute offset of the SHA in the .bin file
SHA_OFFS = 0xB0 # absolute offset of the SHA in the .bin file
BIN = "esp32-app-cust-ver-info.bin"
"""
@@ -339,14 +387,17 @@ class ELFSHA256Tests(BaseTestCase):
.time = "xxxxxxxxxxxxxxx",
.date = "yyyyyyyyyyyyyyy",
.idf_ver = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
.app_elf_sha256 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
.reserv2 = {0xffffffff,0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff},
.app_elf_sha256 =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
.reserv2 = {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff},
};
This leaves zeroes only for the fiels of SHA-256 and the test will fail if the placement of zeroes are tested at
the wrong place.
This leaves zeroes only for the fiels of SHA-256 and the test will fail
if the placement of zeroes are tested at the wrong place.
00000000: e907 0020 780f 0840 ee00 0000 0000 0000 ... x..@........
00000010: 0000 0000 0000 0001 2000 403f 605a 0000 ........ .@?`Z..
@@ -359,7 +410,7 @@ class ELFSHA256Tests(BaseTestCase):
00000080: 7979 7979 7979 7979 7979 7979 7979 7900 yyyyyyyyyyyyyyy.
00000090: 7a7a 7a7a 7a7a 7a7a 7a7a 7a7a 7a7a 7a7a zzzzzzzzzzzzzzzz
000000a0: 7a7a 7a7a 7a7a 7a7a 7a7a 7a7a 7a7a 7a00 zzzzzzzzzzzzzzz.
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ SHA-256 should go here
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ SHA-256 here
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000000d0: ffff ffff ffff ffff ffff ffff ffff ffff ................
000000e0: ffff ffff ffff ffff ffff ffff ffff ffff ................
@@ -372,10 +423,16 @@ class ELFSHA256Tests(BaseTestCase):
def test_binary_patched(self):
try:
self.run_elf2image("esp32", self.ELF, extra_args=["--elf-sha256-offset", "0x%x" % self.SHA_OFFS])
self.run_elf2image(
"esp32",
self.ELF,
extra_args=["--elf-sha256-offset", "0x%x" % self.SHA_OFFS],
)
image = esptool.bin_image.LoadFirmwareImage("esp32", self.BIN)
rodata_segment = image.segments[0]
bin_sha256 = rodata_segment.data[self.SHA_OFFS - 0x20: self.SHA_OFFS - 0x20 + 32] # subtract 0x20 byte header here
bin_sha256 = rodata_segment.data[
self.SHA_OFFS - 0x20 : self.SHA_OFFS - 0x20 + 32
] # subtract 0x20 byte header here
with open(self.ELF, "rb") as f:
elf_computed_sha256 = hashlib.sha256(f.read()).digest()
@@ -391,12 +448,16 @@ class ELFSHA256Tests(BaseTestCase):
def test_no_overwrite_data(self):
with self.assertRaises(subprocess.CalledProcessError) as e:
self.run_elf2image("esp32", "esp32-bootloader.elf", extra_args=["--elf-sha256-offset", "0xb0"])
self.run_elf2image(
"esp32",
"esp32-bootloader.elf",
extra_args=["--elf-sha256-offset", "0xb0"],
)
output = e.exception.output
self.assertIn(b"SHA256", output)
self.assertIn(b"zero", output)
if __name__ == '__main__':
if __name__ == "__main__":
print("Running image generation tests...")
unittest.main(buffer=True)