def main():
    global debug_print
    args = parseargs()
    if args.verbose:
        debug_print = print
    debug_print('Begin MEC17xx pack_ec.py script')
    print_args(args)
    spi_size = (args.spi_size * 1024)
    debug_print('SPI Flash image size in bytes =', hex(spi_size))
    spi_list = []
    debug_print('args.input = ', args.input)
    debug_print('args.loader_file = ', args.loader_file)
    debug_print('args.image_size = ', hex(args.image_size))
    rorofile = PacklfwRoImage(args.input, args.loader_file, args.image_size)
    payload = GetPayload(rorofile)
    payload_len = len(payload)
    debug_print('EC_LFW + EC_RO length = ', hex(payload_len))
    if (args.test_spi == True):
        crc = zlib.crc32(bytes(payload[LFW_SIZE:(payload_len - 4)]))
        crc_ofs = (payload_len - 4)
        debug_print('EC_RO CRC32 = 0x{0:08x} @ 0x{1:08x}'.format(crc, crc_ofs))
        for i in range(4):
            payload[(crc_ofs + i)] = (crc & 255)
            crc = (crc >> 8)
    payload_signature = SignByteArray(payload)
    printByteArrayAsHex(payload_signature, 'LFW + EC_RO payload_signature')
    header = BuildHeader(args, payload_len, LOAD_ADDR, rorofile)
    printByteArrayAsHex(header, 'Header LFW + EC_RO')
    header_signature = SignByteArray(header)
    printByteArrayAsHex(header_signature, 'header_signature')
    tag = BuildTag(args)
    debug_print('Call to GetPayloadFromOffset')
    debug_print('args.input = ', args.input)
    debug_print('args.image_size = ', hex(args.image_size))
    payload_rw = GetPayloadFromOffset(args.input, args.image_size)
    debug_print('type(payload_rw) is ', type(payload_rw))
    debug_print('len(payload_rw) is ', hex(len(payload_rw)))
    rw_len = args.image_size
    payload_rw = payload_rw[:rw_len]
    payload_rw_len = len(payload_rw)
    debug_print('Truncated size of EC_RW = ', hex(payload_rw_len))
    payload_entry_tuple = struct.unpack_from('<I', payload_rw, 4)
    debug_print('payload_entry_tuple = ', payload_entry_tuple)
    payload_entry = payload_entry_tuple[0]
    debug_print('payload_entry = ', hex(payload_entry))
    if args.test_ecrw:
        gen_test_ecrw(payload_rw)
    if (args.test_spi == True):
        crc = zlib.crc32(bytes(payload_rw[:(payload_rw_len - 32)]))
        crc_ofs = (payload_rw_len - 4)
        debug_print('EC_RW CRC32 = 0x{0:08x} at offset 0x{1:08x}'.format(crc, crc_ofs))
        for i in range(4):
            payload_rw[(crc_ofs + i)] = (crc & 255)
            crc = (crc >> 8)
    payload_rw_sig = SignByteArray(payload_rw)
    printByteArrayAsHex(payload_rw_sig, 'payload_rw_sig')
    os.remove(rorofile)
    spi_list.append((0, tag, 'tag'))
    spi_list.append((args.header_loc, header, 'header(lwf + ro)'))
    spi_list.append(((args.header_loc + HEADER_SIZE), header_signature, 'header(lwf + ro) signature'))
    spi_list.append(((args.header_loc + args.payload_offset), payload, 'payload(lfw + ro)'))
    offset = ((args.header_loc + args.payload_offset) + payload_len)
    spi_list.append((offset, payload_signature, 'payload(lfw_ro) signature'))
    rw_offset = int((spi_size // 2))
    if (args.rw_loc >= 0):
        rw_offset = args.rw_loc
    debug_print('rw_offset = 0x{0:08x}'.format(rw_offset))
    if (rw_offset < (offset + len(payload_signature))):
        print('ERROR: EC_RW overlaps EC_RO')
    spi_list.append((rw_offset, payload_rw, 'payload(rw)'))
    spi_list = sorted(spi_list)
    dumpsects(spi_list)
    with open(args.output, 'wb') as f:
        debug_print('Write spi list to file', args.output)
        addr = 0
        for s in spi_list:
            if (addr < s[0]):
                debug_print('Offset ', hex(addr), ' Length', hex((s[0] - addr)), 'fill with 0xff')
                f.write((b'\xff' * (s[0] - addr)))
                addr = s[0]
                debug_print('Offset ', hex(addr), ' Length', hex(len(s[1])), 'write data')
            f.write(s[1])
            addr += len(s[1])
        if (addr < spi_size):
            debug_print('Offset ', hex(addr), ' Length', hex((spi_size - addr)), 'fill with 0xff')
            f.write((b'\xff' * (spi_size - addr)))
        f.flush()
def main():
    global debug_print
    args = parseargs()
    if args.verbose:
        debug_print = print
    debug_print('Begin MEC17xx pack_ec.py script')
    print_args(args)
    spi_size = (args.spi_size * 1024)
    debug_print('SPI Flash image size in bytes =', hex(spi_size))
    spi_list = []
    debug_print('args.input = ', args.input)
    debug_print('args.loader_file = ', args.loader_file)
    debug_print('args.image_size = ', hex(args.image_size))
    rorofile = PacklfwRoImage(args.input, args.loader_file, args.image_size)
    payload = GetPayload(rorofile)
    payload_len = len(payload)
    debug_print('EC_LFW + EC_RO length = ', hex(payload_len))
    if (args.test_spi == True):
        crc = zlib.crc32(bytes(payload[LFW_SIZE:(payload_len - 4)]))
        crc_ofs = (payload_len - 4)
        debug_print('EC_RO CRC32 = 0x{0:08x} @ 0x{1:08x}'.format(crc, crc_ofs))
        for i in range(4):
            payload[(crc_ofs + i)] = (crc & 255)
            crc = (crc >> 8)
    payload_signature = SignByteArray(payload)
    printByteArrayAsHex(payload_signature, 'LFW + EC_RO payload_signature')
    header = BuildHeader(args, payload_len, LOAD_ADDR, rorofile)
    printByteArrayAsHex(header, 'Header LFW + EC_RO')
    header_signature = SignByteArray(header)
    printByteArrayAsHex(header_signature, 'header_signature')
    tag = BuildTag(args)
    debug_print('Call to GetPayloadFromOffset')
    debug_print('args.input = ', args.input)
    debug_print('args.image_size = ', hex(args.image_size))
    payload_rw = GetPayloadFromOffset(args.input, args.image_size)
    debug_print('type(payload_rw) is ', type(payload_rw))
    debug_print('len(payload_rw) is ', hex(len(payload_rw)))
    rw_len = args.image_size
    payload_rw = payload_rw[:rw_len]
    payload_rw_len = len(payload_rw)
    debug_print('Truncated size of EC_RW = ', hex(payload_rw_len))
    payload_entry_tuple = struct.unpack_from('<I', payload_rw, 4)
    debug_print('payload_entry_tuple = ', payload_entry_tuple)
    payload_entry = payload_entry_tuple[0]
    debug_print('payload_entry = ', hex(payload_entry))
    if args.test_ecrw:
        gen_test_ecrw(payload_rw)
    if (args.test_spi == True):
        crc = zlib.crc32(bytes(payload_rw[:(payload_rw_len - 32)]))
        crc_ofs = (payload_rw_len - 4)
        debug_print('EC_RW CRC32 = 0x{0:08x} at offset 0x{1:08x}'.format(crc, crc_ofs))
        for i in range(4):
            payload_rw[(crc_ofs + i)] = (crc & 255)
            crc = (crc >> 8)
    payload_rw_sig = SignByteArray(payload_rw)
    printByteArrayAsHex(payload_rw_sig, 'payload_rw_sig')
    os.remove(rorofile)
    spi_list.append((0, tag, 'tag'))
    spi_list.append((args.header_loc, header, 'header(lwf + ro)'))
    spi_list.append(((args.header_loc + HEADER_SIZE), header_signature, 'header(lwf + ro) signature'))
    spi_list.append(((args.header_loc + args.payload_offset), payload, 'payload(lfw + ro)'))
    offset = ((args.header_loc + args.payload_offset) + payload_len)
    spi_list.append((offset, payload_signature, 'payload(lfw_ro) signature'))
    rw_offset = int((spi_size // 2))
    if (args.rw_loc >= 0):
        rw_offset = args.rw_loc
    debug_print('rw_offset = 0x{0:08x}'.format(rw_offset))
    if (rw_offset < (offset + len(payload_signature))):
        print('ERROR: EC_RW overlaps EC_RO')
    spi_list.append((rw_offset, payload_rw, 'payload(rw)'))
    spi_list = sorted(spi_list)
    dumpsects(spi_list)
    with open(args.output, 'wb') as f:
        debug_print('Write spi list to file', args.output)
        addr = 0
        for s in spi_list:
            if (addr < s[0]):
                debug_print('Offset ', hex(addr), ' Length', hex((s[0] - addr)), 'fill with 0xff')
                f.write((b'\xff' * (s[0] - addr)))
                addr = s[0]
                debug_print('Offset ', hex(addr), ' Length', hex(len(s[1])), 'write data')
            f.write(s[1])
            addr += len(s[1])
        if (addr < spi_size):
            debug_print('Offset ', hex(addr), ' Length', hex((spi_size - addr)), 'fill with 0xff')
            f.write((b'\xff' * (spi_size - addr)))
        f.flush()