#!/usr/bin/env python from struct import pack, unpack import subprocess,os,sys,struct,array from Crypto.PublicKey import RSA from Crypto.Util.number import long_to_bytes KeyMagicNum=b'SIMLOCK_PUBKEY_INFO' ConfigInfoMagicNum=b'SIMLOCK_CONFIG_INFO' KeyPath = "SimlocKeysV6/" ConfigInfoPath = "ConfigInfo/" KeyIDTable = [0x1011, 0x1012, 0x1013, 0x1014, 0x1015] KeysDirs = ["authstartkey", "authendkey", "simlockey", "imeikey", "reskey"] def GetKeyFiles(fdir): kf = "" fls=os.listdir(fdir) j = 0 for i in fls: if os.path.splitext(i)[1] == ".pem": #print"Find key: " + i if kf == "": kf = i j = j + 1 if j > 1: kf = "" return j, kf def GetConfigInfoFiles(fdir): cf = "" fls=os.listdir(fdir) j = 0 for i in fls: if os.path.splitext(i)[1] == ".bin": #print"Find key: " + i if cf == "": cf = i j = j + 1 if j > 1: cf = "" return j, cf def GetKeyAreaHeadOffset(tos_img,tos_len): secpos = tos_img.find(KeyMagicNum) if secpos < 0: print("there is no head!!!") return 0 else: print("Find magic num: " + KeyMagicNum.decode()) return secpos def GetConfigInfoAreaHeadOffset(tos_img,tos_len): secpos = tos_img.find(ConfigInfoMagicNum) if secpos < 0: print("there is no head!!!") return 0 else: print("Find magic num: " + ConfigInfoMagicNum.decode()) return secpos def InsertKey(kpath,kid,img): ksize = os.path.getsize(kpath) print("The length of the file '%s' is %d bytes." % (kpath, ksize)) try: f = open(kpath, 'rb') except IOError: print("Can\'t find key: \""+kpath+"\"") else: kpem = f.read() f.close img.write(struct.pack("