1import sys, hashlib, os 2import struct 3 4 5# changed by yanxiaoyong.yxy 6# https://blog.csdn.net/qq_43192819/article/details/108981008 7# https://blog.csdn.net/whatday/article/details/107769032 8def MD5(input): 9 md5 = hashlib.md5() 10 md5.update(input) 11 return bytes.fromhex(md5.hexdigest()) 12 #return md5.hexdigest().decode('hex') 13 14input_bin = sys.argv[1] 15ota_bin = sys.argv[3] 16version = int(sys.argv[2], 16) 17b = struct.pack('<L', version) 18filelen = os.path.getsize(input_bin) 19sum = 0 20i=0 21with open(input_bin, 'rb') as file: 22 ota_md5_bytes = MD5(file.read()) 23output = open(ota_bin, 'wb') 24output.write(b) 25b = struct.pack('<L', 0x01) 26output.write(b) 27#output.write("OTA1") 28output.write("OTA1".encode()) 29b = struct.pack('<L', 0x18) 30output.write(b) 31input = open(input_bin, 'rb') 32for i in range(filelen): 33 data = input.read(1) 34 sum += struct.unpack("B", data)[0] 35input.close 36b = struct.pack('<L', sum) #CRC 37output.write(b) 38b = struct.pack('<L', filelen) 39output.write(b) 40b = struct.pack('<L', 0x20) 41output.write(b) 42b = struct.pack('<L', 0) 43output.write(b) 44input = open(input_bin, 'rb') 45output.write(input.read()) 46input.close() 47output.close()