def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input_bin', required=True, help='Full firmware binary file')
    parser.add_argument('--model', default='39990-TLA-A030', help='EPS part number')
    args = parser.parse_args()
    if (not (args.model in car_models)):
        print(('Car model %s not found' % args.model))
        sys.exit((- 1))
    print(('Creating rwd for model %s' % args.model))
    m = car_models[args.model]
    if (not os.path.exists(args.input_bin)):
        print(('%s not found' % args.input_bin))
        sys.exit((- 1))
    encrypt_lookup_table = {}
    for (k, v) in default_decrypt_lookup_table.items():
        encrypt_lookup_table[v] = k
    with open(args.input_bin, 'rb') as f:
        full_fw = f.read()
        patch_fw = full_fw[m['start-address']:(m['start-address'] + m['data-size'])]
        for (func_idx, off) in m['checksum-offsets']:
            old_checksum = (struct.unpack('!H', patch_fw[off:(off + 2)])[0] & 65535)
            new_checksum = (checksum_funcs[func_idx](patch_fw, 0, off) & 65535)
            print(('Update checksum at offset %s from %s to %s' % (hex(off), hex(old_checksum), hex(new_checksum))))
            patch_fw = ((patch_fw[:off] + struct.pack('!H', (new_checksum & 65535))) + patch_fw[(off + 2):])
        encrypted = bytearray()
        for b in patch_fw:
            encrypted.append(encrypt_lookup_table[b])
        out_enc_path = (args.input_bin + '.enc')
        with open(out_enc_path, 'wb') as out_f:
            out_f.write(encrypted)
            print(('Encryption done, saved to %s.' % out_enc_path))
        cur_dir = os.path.dirname(os.path.abspath(__file__))
        cmds = ['python2', 'rwd-builder.py', '--can-address', m['can-address'], '--supported-versions', *m['supported-versions'], '--security-key', *m['security-key'], '--encryption-key', m['encryption-key'], '--encrypted-file', out_enc_path, '--start-address', hex(m['start-address']), '--data-size', hex(m['data-size'])]
        subprocess.check_call(cmds, cwd=cur_dir)
        print(('RWD file %s created.' % (out_enc_path[:(- 4)] + '.rwd')))
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input_bin', required=True, help='Full firmware binary file')
    parser.add_argument('--model', default='39990-TLA-A030', help='EPS part number')
    args = parser.parse_args()
    if (not (args.model in car_models)):
        print(('Car model %s not found' % args.model))
        sys.exit((- 1))
    print(('Creating rwd for model %s' % args.model))
    m = car_models[args.model]
    if (not os.path.exists(args.input_bin)):
        print(('%s not found' % args.input_bin))
        sys.exit((- 1))
    encrypt_lookup_table = {}
    for (k, v) in default_decrypt_lookup_table.items():
        encrypt_lookup_table[v] = k
    with open(args.input_bin, 'rb') as f:
        full_fw = f.read()
        patch_fw = full_fw[m['start-address']:(m['start-address'] + m['data-size'])]
        for (func_idx, off) in m['checksum-offsets']:
            old_checksum = (struct.unpack('!H', patch_fw[off:(off + 2)])[0] & 65535)
            new_checksum = (checksum_funcs[func_idx](patch_fw, 0, off) & 65535)
            print(('Update checksum at offset %s from %s to %s' % (hex(off), hex(old_checksum), hex(new_checksum))))
            patch_fw = ((patch_fw[:off] + struct.pack('!H', (new_checksum & 65535))) + patch_fw[(off + 2):])
        encrypted = bytearray()
        for b in patch_fw:
            encrypted.append(encrypt_lookup_table[b])
        out_enc_path = (args.input_bin + '.enc')
        with open(out_enc_path, 'wb') as out_f:
            out_f.write(encrypted)
            print(('Encryption done, saved to %s.' % out_enc_path))
        cur_dir = os.path.dirname(os.path.abspath(__file__))
        cmds = ['python2', 'rwd-builder.py', '--can-address', m['can-address'], '--supported-versions', *m['supported-versions'], '--security-key', *m['security-key'], '--encryption-key', m['encryption-key'], '--encrypted-file', out_enc_path, '--start-address', hex(m['start-address']), '--data-size', hex(m['data-size'])]
        subprocess.check_call(cmds, cwd=cur_dir)
        print(('RWD file %s created.' % (out_enc_path[:(- 4)] + '.rwd')))