fix(merge_bin): treat files starting with colon as raw files

This commit is contained in:
Peter Dragun
2024-01-19 16:13:14 +01:00
committed by Radim Karniš
parent bdeec68cb6
commit 2c0a5daa7b
2 changed files with 23 additions and 9 deletions

View File

@@ -13,7 +13,7 @@ import struct
import tempfile
from typing import BinaryIO, Optional
from intelhex import IntelHex
from intelhex import HexRecordError, IntelHex
from .loader import ESPLoader
from .targets import (
@@ -47,14 +47,18 @@ def intel_hex_to_bin(file: BinaryIO, start_addr: Optional[int] = None) -> Binary
INTEL_HEX_MAGIC = b":"
magic = file.read(1)
file.seek(0)
if magic == INTEL_HEX_MAGIC:
ih = IntelHex()
ih.loadhex(file.name)
file.close()
bin = tempfile.NamedTemporaryFile(suffix=".bin", delete=False)
ih.tobinfile(bin, start=start_addr)
return bin
else:
try:
if magic == INTEL_HEX_MAGIC:
ih = IntelHex()
ih.loadhex(file.name)
file.close()
bin = tempfile.NamedTemporaryFile(suffix=".bin", delete=False)
ih.tobinfile(bin, start=start_addr)
return bin
else:
return file
except HexRecordError:
# file started with HEX magic but the rest was not according to the standard
return file

View File

@@ -263,6 +263,16 @@ class TestMergeBin:
# verify the file itself
assert source == merged_bin[0x1000:]
def test_hex_header_raw_file(self):
# use raw binary file starting with colon
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b":")
try:
merged = self.run_merge_bin("esp32", [(0x0, f.name)])
assert merged == b":"
finally:
os.unlink(f.name)
class UF2Block(object):
def __init__(self, bs):