import shutil
import sys
import os
import json

import prottcp

BOOT_PATH ="file/checker_slave_boot_uart--1.bin"
APP_PATH ="file/checker_slave_app_uart_20230912--2.bin"
SCHEME_PATH = "file/YM硬件EJ工厂注码103101--17.json"
OUT_PATH = "file/checker_slave_boot_uart_.bin"

# 创建离线下载器的镜像


# 填充指定个数的byte
def arr_byte_copy(byte:int,num:int):
    t=bytearray()
    for i in range(num):
        t.append(byte)
    return t
# int转数组
def arr_from_int(num:int):
    return  bytearray([num&0xff,(num>>8)&0xff,(num>>16)&0xff,(num>>24)&0xff])


def creat():
    boot=BOOT_PATH
    app=APP_PATH
    scheme=SCHEME_PATH
    d=bytearray()
    with open(boot,"rb") as f:
        d+=f.read()
    d+=arr_byte_copy(0xff,0x4000-len(d))
    with open(app,"rb") as f:
        d+=f.read()
    d+=arr_byte_copy(0xff,0x3f000-len(d))
    with open(scheme,"rb") as f:
        json_obj=json.loads(f.read())
        d+=prottcp.scheme_to_byte(json_obj)
    d+=arr_byte_copy(0xff,0x3f800-len(d))
    d+=arr_from_int(0x669955aa)
    with open(OUT_PATH,"wb+") as f:
        f.write(d)
        print(OUT_PATH+" create boot file success.")
    
    

if __name__ == "__main__":
    creat()


