WIP matching segments to section and error checking

This commit is contained in:
Sergey Shchelkanov 2023-05-11 13:32:57 +03:00
parent 280cc95c44
commit 1f90a39a48
2 changed files with 36 additions and 8 deletions

View File

@ -31,9 +31,8 @@ def parse_line(line: str, file_extension: str) -> Record:
def parse_hex_line(line: str) -> Record: def parse_hex_line(line: str) -> Record:
if line[0] != ':': if line[0] != ':':
print("Error: unexpected record mark on line %s, expect \':\', get \'%c\'" % ( raise Exception("Error: unexpected record mark on line %s, expect \':\', get \'%c\'" % (
line, line[0])) line, line[0]))
return ()
datalen = int(line[1:3], base=16) # Data field length datalen = int(line[1:3], base=16) # Data field length
addr = int(line[3:7], base=16) # Load offset field addr = int(line[3:7], base=16) # Load offset field
@ -186,4 +185,6 @@ def bytes2words(arr: List[int]) -> List[int]:
if bytes.__len__() == 4: if bytes.__len__() == 4:
words.append(bytes[0]+2**8*bytes[1]+2**16*bytes[2]+2**24*bytes[3]) words.append(bytes[0]+2**8*bytes[1]+2**16*bytes[2]+2**24*bytes[3])
bytes = [] bytes = []
if bytes.__len__() != 0:
print("WARNING: skipping not-word-aligned byte")
return words return words

View File

@ -42,11 +42,13 @@ class Segment:
offset: int offset: int
data: List[int] data: List[int]
class MemoryType(Enum): class MemoryType(Enum):
EEPROM = 1 EEPROM = 1
RAM = 2 RAM = 2
SPIFI = 80 SPIFI = 80
class MemorySection(NamedTuple): class MemorySection(NamedTuple):
type: MemoryType type: MemoryType
offset: int offset: int
@ -60,6 +62,15 @@ mik32v0_sections: List[MemorySection] = [
] ]
def belongs_memory_section(memory_section: MemorySection, offset: int) -> bool:
if offset < memory_section.offset:
return False
if offset >= (memory_section.offset + memory_section.length):
return False
return True
def read_file(filename: str) -> List[Segment]: def read_file(filename: str) -> List[Segment]:
segments: List[Segment] = [] segments: List[Segment] = []
lines: List[str] = [] lines: List[str] = []
@ -71,7 +82,7 @@ def read_file(filename: str) -> List[Segment]:
elif file_extension == ".bin": elif file_extension == ".bin":
with open(filename, "rb") as f: with open(filename, "rb") as f:
contents = list(f.read()) contents = list(f.read())
segments[0] = (0, contents) segments.append(Segment(offset=0, data=contents))
else: else:
raise Exception("Unsupported file format: %s" % (file_extension)) raise Exception("Unsupported file format: %s" % (file_extension))
@ -96,6 +107,8 @@ def read_file(filename: str) -> List[Segment]:
lba = record.address lba = record.address
elif record.type == RecordType.LINEARSTARTADDR: elif record.type == RecordType.LINEARSTARTADDR:
print("Start Linear Address:", record.address) print("Start Linear Address:", record.address)
elif record.type == RecordType.EOF:
break
return segments return segments
@ -105,11 +118,8 @@ def upload_file(filename: str, is_resume=True) -> int:
Write ihex or binary file into MIK32 EEPROM or external flash memory Write ihex or binary file into MIK32 EEPROM or external flash memory
@filename: full path to the file with hex or bin file format @filename: full path to the file with hex or bin file format
@boot_source: boot source, eeprom, ram or spifi, define memory block mapped to boot memory area (0x0 offset)
@return: return 0 if successful, 1 if failed @return: return 0 if successful, 1 if failed
TODO: Implement error handling
""" """
# print("Running OpenOCD...") # print("Running OpenOCD...")
@ -121,7 +131,24 @@ def upload_file(filename: str, is_resume=True) -> int:
print("ERROR: File %s does not exist" % filename) print("ERROR: File %s does not exist" % filename)
exit(1) exit(1)
print(read_file(filename)) segments: List[Segment] = read_file(filename)
# print(segments)
for segment in segments:
segment_section: None | MemorySection = None
for section in mik32v0_sections:
if belongs_memory_section(section, segment.offset):
segment_section = section
if segment_section is None:
raise Exception(
"ERROR: segment with offset %s doesn't belong to any section" % hex(segment.offset))
if (segment.offset + segment.data.__len__()) > (segment_section.offset + segment_section.length):
raise Exception("ERROR: segment with offset %s and length %s overflows section %s" % (
hex(segment.offset), segment.data.__len__(), segment_section.type.name))
if segment_section.type == MemoryType.EEPROM:
result = write_words(bytes2words(segment.data), is_resume)
# cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mcu32.cfg" % (DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False) # cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mcu32.cfg" % (DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False)
# with subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL) as proc: # with subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL) as proc: