+def hex_type(string):
+ try:
+ value = int(string, 16)
+ return value
+ except ValueError:
+ raise argparse.ArgumentTypeError(f"'{string}' is not a valid hexadecimal value.")
+
+def set_size_boundaries(dev, start_addr, size):
+
+ sector_size = dev.chip_layout[dev.sel_area]['ALIGN'] # currently only area 0 supported
+
+ if start_addr % sector_size:
+ raise ValueError(f"start addr not aligned on sector size {sector_size}")
+
+ if size < sector_size:
+ print("Warning: you are trying to write something that is less than one sector size: padding with zeroes")
+
+ blocks = (size + sector_size - 1) // sector_size
+ end_addr = blocks * sector_size + start_addr - 1
+
+ if (end_addr <= start_addr):
+ raise ValueError(f"End address smaller or equal than start_address")
+
+ if (end_addr > dev.chip_layout[dev.sel_area]['EAD']):
+ raise ValueError(f"Binary file is bigger than available ROM space")
+
+ return (start_addr, end_addr)
+
+def get_area_info(dev, output=False):
+ cfg = {}
+ for i in [0,1,2]:
+ packed = pack_pkt(ARE_CMD, [str(i)])
+ dev.send_data(packed)
+ info = dev.recv_data(23)
+ msg = unpack_pkt(info)
+ fmt = '>BIIII'
+ KOA, SAD, EAD, EAU, WAU = struct.unpack(fmt, bytes(int(x, 16) for x in msg))
+ cfg[i] = {"SAD": SAD, "EAD": EAD, "ALIGN": EAU}
+ if output:
+ print(f'Area {KOA}: {hex(SAD)}:{hex(EAD)} (erase {hex(EAU)} - write {hex(WAU)})')
+
+ return cfg