From dfb329e10f2d0517d282cea2c0da4a1d575d854b Mon Sep 17 00:00:00 2001 From: Alain Carlucci Date: Sat, 22 Mar 2025 11:55:44 +0100 Subject: [PATCH] CS7000: added wrap script and build target --- meson.build | 12 ++++-- scripts/cs7000_wrap.py | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 scripts/cs7000_wrap.py diff --git a/meson.build b/meson.build index f3daaef6..5aa968ec 100644 --- a/meson.build +++ b/meson.build @@ -839,7 +839,7 @@ targets = [ 'name' : 'cs7000', 'opts' : cs7000_opts, 'flashable': true, - 'wrap' : ' ', + 'wrap' : 'cs7000', 'load_addr': '0x08000000' }, { @@ -866,6 +866,7 @@ dfu_util = find_program('dfu-util', required:false, d west = find_program('west', required:false, disabler:true) uf2conv = find_program('scripts/uf2conv.py', required:false, disabler:true) dfu_convert = find_program('scripts/dfu-convert.py', required:false, disabler:true) +cs7000_wrap = find_program('scripts/cs7000_wrap.py', required:false, disabler:true) foreach t : targets @@ -977,14 +978,17 @@ foreach t : targets '-D', '@INPUT@', '-s', '0x08000000']) - # For CS7000 'wrap' target prepares a .dfu file elif name == 'openrtx_cs7000' or name == 'openrtx_cs7000p' - custom_target(name+'_wrap', - output : name+'.dfu', + custom_target(name+'_dfu', + output : name+'_dfu', input : bin, command : [dfu_convert, '-b', t['load_addr']+':@INPUT@', '@OUTPUT@']) + custom_target(name+'_wrap', + output : name+'_wrap', + input : bin, + command : [cs7000_wrap, '@INPUT@', '@OUTPUT@.bin']) custom_target(name+'_flash', input : bin, diff --git a/scripts/cs7000_wrap.py b/scripts/cs7000_wrap.py new file mode 100644 index 00000000..eb275f1d --- /dev/null +++ b/scripts/cs7000_wrap.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Copyright 2025 Alain Carlucci, Silvano Seva + +import struct +import sys + +if len(sys.argv) != 3: + print("Usage: %s " % (sys.argv[0], )) + exit(-1) + +key = [0x71, 0x21, 0x3c, 0x7f, 0x28, 0x7e, 0x2b, 0x7e, 0x77, 0x64, 0x7e, 0x7d, + 0x4e, 0x37, 0x7e, 0x1d, 0x5c, 0x44, 0x5e, 0x68, 0x67, 0x22, 0x51, 0x3a, + 0x66, 0x7d, 0x0b, 0x65, 0x77, 0x0c, 0x60, 0x7c, 0x33, 0x47, 0x5c, 0x61, + 0x65, 0x5a, 0x6b, 0x6a, 0x5d, 0x68, 0x59, 0x5d, 0x7a, 0x61, 0x0b, 0x59, + 0x31, 0x57, 0x6c, 0x56, 0x08, 0x4d, 0x6b, 0x4b, 0x54, 0x45, 0x50, 0x4e, + 0x53, 0x70, 0x3a, 0x6e, 0x2d, 0x42, 0x45, 0x6a, 0x48, 0x4d, 0x51, 0x37, + 0x54, 0x56, 0x44, 0x60, 0x37, 0x36, 0x7b, 0x57, 0x6d, 0x2e, 0x47, 0x16, + 0x00, 0x2a, 0x00, 0x3a, 0x62, 0x07, 0x01, 0x01, 0x03, 0x19, 0x78, 0x33, + 0x12, 0x28, 0x1a, 0x11, 0x09, 0x2b, 0x00, 0x13, 0x04, 0x1d, 0x22, 0x24, + 0x25, 0x4a, 0x4b, 0x1c, 0x27, 0x25, 0x03, 0x1e, 0x74, 0x01, 0x09, 0x75, + 0x1a, 0x19, 0x13, 0x7e, 0x16, 0x7c, 0x30, 0x09, 0x04, 0x76, 0x06, 0x3c, + 0x09, 0x0a, 0x0d, 0x7c, 0x0e, 0x00, 0x17, 0x36, 0x37, 0x02, 0x2b, 0x7a, + 0x6c, 0x0e, 0x00, 0x7b, 0x07, 0x2a, 0x05, 0x66, 0x79, 0x02, 0x03, 0x70, + 0x5e, 0x00, 0x72, 0x5a, 0x12, 0x3c, 0x10, 0x0f, 0x06, 0x0c, 0x75, 0x05, + 0x18, 0x14, 0x1c, 0x18, 0x0a, 0x08, 0x06, 0x7b, 0x7d, 0x2b, 0x76, 0x00, + 0x30, 0x67, 0x4a, 0x48, 0x0f, 0x6d, 0x73, 0x7b, 0x78, 0x1d, 0x00, 0x28, + 0x7a, 0x00, 0x31, 0x7d, 0x02, 0x73, 0x00, 0x3c, 0x0d, 0x01, 0x7d, 0x30, + 0x00, 0x39, 0x00, 0x7e, 0x27, 0x00, 0x1e, 0x78, 0x00, 0x2d, 0x10, 0x2e, + 0x30, 0x34, 0x39, 0x3a, 0x3c, 0x3d, 0x42, 0x41, 0x3f, 0x2b, 0x3d, 0x34, + 0x2e, 0x33, 0x30, 0x2d, 0x7d, 0x02, 0x64, 0x7e, 0x17, 0x6f, 0x3c, 0x20, + 0x04, 0x24, 0x20, 0x04, 0x6e, 0x03, 0x21, 0x01, 0x62, 0x00, 0x71, 0x00, + 0x30, 0x7e, 0x75, 0x3a] + +input_file = sys.argv[1] +output_file = sys.argv[2] + +file_data = open(input_file, "rb").read() +file_len = len(file_data) + +encoded_len = struct.pack("> 8) & 0xff, checksum_int & 0xff]) + +encrypted_payload = bytes([x^y for x,y in zip(payload + checksum_data, full_key)]) + +open(output_file, "wb").write(header + encrypted_payload)