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

View File

@@ -263,6 +263,16 @@ class TestMergeBin:
# verify the file itself # verify the file itself
assert source == merged_bin[0x1000:] 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): class UF2Block(object):
def __init__(self, bs): def __init__(self, bs):