import xml.etree.ElementTree as ET
import binascii
import time
import os
from Library.utils import *
class xmlparser():
def getresponse(self,input):
lines=input.split(b""
val=self.xmlsend(data)
if val[0]==True:
print("Reset succeeded.")
return True
else:
print("Reset failed.")
return False
def cmd_xml(self,data):
val=self.xmlsend(data)
if val[0]==True:
print("Command succeeded.")
print(val[2])
return val[2]
else:
print("Command failed:")
print(val[2])
return val[2]
def cmd_nop(self):
data=""
val=self.xmlsend(data)
if val[0]==True:
print("Nop succeeded.")
return True
else:
print("Nop failed.")
return False
def cmd_getsha256digest(self,physical_partition_number,start_sector,num_partition_sectors):
data=f"\n"
val=self.xmlsend(data)
if val[0]==True:
res = self.xml.getlog(val[2])
print(res)
if "Digest " in res:
return res.split("Digest ")[1]
else:
return res
else:
print("GetSha256Digest failed.")
return False
def cmd_setbootablestoragedrive(self,partition_number):
data=f"\n"
val=self.xmlsend(data)
if val[0]==True:
print("Setbootablestoragedrive succeeded.")
return True
else:
print("Setbootablestoragedrive failed.")
return False
def wait_response(self):
data = bytearray()
while b"\n"+\
f"\n"
rsp = self.xmlsend(data)
pos=0
prog=0
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
if (rsp[0]) == True:
bytesToWrite = self.cfg.SECTOR_SIZE_IN_BYTES * num_partition_sectors
total=self.cfg.SECTOR_SIZE_IN_BYTES * num_partition_sectors
old=0
while (bytesToWrite>0):
wdata=rf.read(self.cfg.MaxPayloadSizeToTargetInBytes)
wlen=len(wdata)
if (wlen % self.cfg.SECTOR_SIZE_IN_BYTES) != 0:
filllen=(wlen//self.cfg.SECTOR_SIZE_IN_BYTES*self.cfg.SECTOR_SIZE_IN_BYTES)+self.cfg.SECTOR_SIZE_IN_BYTES
wdata+=b"\x00"*(filllen-wlen)
wlen=len(wdata)
self.cdc.write(wdata,self.cfg.MaxPayloadSizeToTargetInBytes)
prog = int(float(pos) / float(total) * float(100))
if (prog > old):
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
old = prog
bytesToWrite -= wlen
pos+=wlen
#time.sleep(0.05)
if Display and prog!=100:
print_progress(100, 100, prefix='Progress:', suffix='Complete', bar_length=50)
self.cdc.write(b'',self.cfg.MaxPayloadSizeToTargetInBytes)
time.sleep(0.2)
info, rsp = self.wait_response()
if rsp.get("value") == "ACK":
return True
else:
print(f"Error:{info}")
return False
else:
print(f"Error:{rsp}")
return False
return False
def cmd_erase(self,physical_partition_number,start_sector,num_partition_sectors,Display=True):
if Display:
print(f"\nErasing from physical partition {str(physical_partition_number)}, sector {str(start_sector)}, sectors {str(num_partition_sectors)}")
data=f"\n"+\
f"\n"
rsp = self.xmlsend(data)
empty = b"\x00" * self.cfg.MaxPayloadSizeToTargetInBytes
pos=0
prog=0
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
if (rsp[0]) == True:
bytesToWrite = self.cfg.SECTOR_SIZE_IN_BYTES * num_partition_sectors
total=self.cfg.SECTOR_SIZE_IN_BYTES * num_partition_sectors
old=0
while (bytesToWrite>0):
wlen=self.cfg.MaxPayloadSizeToTargetInBytes
if bytesToWrite old):
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
old = prog
bytesToWrite -= wlen
pos+=wlen
#time.sleep(0.05)
if Display and prog!=100:
print_progress(100, 100, prefix='Progress:', suffix='Complete', bar_length=50)
self.cdc.write(b'',self.cfg.MaxPayloadSizeToTargetInBytes)
time.sleep(0.2)
info, rsp = self.wait_response()
if rsp.get("value")=="ACK":
return True
else:
print(f"Error:{info}")
else:
print(f"Error:{rsp}")
return False
return False
def cmd_read_buffer(self,physical_partition_number,start_sector,num_partition_sectors,Display=True):
if Display:
print(f"\nReading from physical partition {str(physical_partition_number)}, sector {str(start_sector)}, sectors {str(num_partition_sectors)}")
data=f"\n"
rsp=self.xmlsend(data, True)
resData=bytearray()
if (rsp[0])==True:
bytesToRead=self.cfg.SECTOR_SIZE_IN_BYTES*num_partition_sectors
total=bytesToRead
dataread=0
old=0
prog=0
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
while(bytesToRead>0):
tmp=self.cdc.read(self.cfg.MaxPayloadSizeToTargetInBytes)
bytesToRead-=len(tmp)
dataread+=len(tmp)
resData+=tmp
prog = int(float(dataread) / float(total) * float(100))
if (prog > old):
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
old = prog
if Display and prog!=100:
print_progress(100, 100, prefix='Progress:', suffix='Complete', bar_length=50)
time.sleep(0.2)
info, rsp = self.wait_response()
if rsp.get("value")=="ACK":
return resData
else:
print(f"Error:{info}")
return ""
else:
print(f"Error:{rsp[2]}")
return ""
return ""
def cmd_read(self,physical_partition_number,start_sector,num_partition_sectors,filename,Display=True):
if Display:
print(f"\nReading from physical partition {str(physical_partition_number)}, sector {str(start_sector)}, sectors {str(num_partition_sectors)}")
with open(filename,"wb") as wf:
data=f"\n"
rsp=self.xmlsend(data, True)
if (rsp[0])==True:
bytesToRead=self.cfg.SECTOR_SIZE_IN_BYTES*num_partition_sectors
total=bytesToRead
dataread=0
old=0
prog=0
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
while(bytesToRead>0):
tmp=self.cdc.read(self.cfg.MaxPayloadSizeToTargetInBytes)
bytesToRead-=len(tmp)
dataread+=len(tmp)
#resData+=tmp
wf.write(tmp)
prog = int(float(dataread) / float(total) * float(100))
if (prog > old):
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
old = prog
#time.sleep(0.05)
if Display and prog!=100:
print_progress(100, 100, prefix='Progress:', suffix='Complete', bar_length=50)
time.sleep(0.2)
info, rsp = self.wait_response()
if rsp.get("value")=="ACK":
return tmp
else:
print(f"Error:{info}")
return ""
else:
print(f"Error:{rsp[1]}")
return ""
return ""
def connect(self,lvl):
v = b'-1'
try:
while v != b'':
v = self.cdc.read()
info=self.xml.getlog(v)
print(info)
except:
lvl=lvl
data=f""+\
f""+\
""
'''
"" \
""
'''
rsp=self.xmlsend(data)
try:
self.cdc.read()
except:
lvl=lvl
if rsp[0]==True:
self.cfg.MemoryName=rsp[1]["MemoryName"]
self.cfg.MaxPayloadSizeToTargetInBytes=int(rsp[1]["MaxPayloadSizeToTargetInBytes"])
self.cfg.MaxPayloadSizeToTargetInBytesSupported=int(rsp[1]["MaxPayloadSizeToTargetInBytesSupported"])
self.cfg.MaxXMLSizeInBytes=int(rsp[1]["MaxXMLSizeInBytes"])
if "MaxPayloadSizeFromTargetInBytes" in rsp[1]:
self.cfg.MaxPayloadSizeFromTargetInBytes=int(rsp[1]["MaxPayloadSizeFromTargetInBytes"])
else:
#print("Unknown cmd structure, please issue this to github: "+str(rsp[1]))
self.cfg.MaxPayloadSizeFromTargetInBytes=self.cfg.MaxXMLSizeInBytes
if "TargetName" in rsp[1]:
self.cfg.TargetName=rsp[1]["TargetName"]
if "MSM" not in self.cfg.TargetName:
self.cfg.TargetName="MSM"+self.cfg.TargetName
else:
self.cfg.TargetName="Unknown"
if "Version" in rsp[1]:
self.cfg.Version=rsp[1]["Version"]
else:
self.cfg.Version="0"
else:
if "MaxPayloadSizeToTargetInBytes" in rsp[1]:
self.cfg.MemoryName = rsp[1]["MemoryName"]
self.cfg.MaxPayloadSizeToTargetInBytes = int(rsp[1]["MaxPayloadSizeToTargetInBytes"])
self.cfg.MaxPayloadSizeToTargetInBytesSupported = int(rsp[1]["MaxPayloadSizeToTargetInBytesSupported"])
self.cfg.MaxXMLSizeInBytes = int(rsp[1]["MaxXMLSizeInBytes"])
self.cfg.MaxPayloadSizeFromTargetInBytes = int(rsp[1]["MaxPayloadSizeFromTargetInBytes"])
self.cfg.TargetName = rsp[1]["TargetName"]
if "MSM" not in self.cfg.TargetName:
self.cfg.TargetName = "MSM" + self.cfg.TargetName
self.cfg.Version = rsp[1]["Version"]
if lvl==0:
return self.connect(lvl+1)
else:
print(f"Error:{rsp}")
exit(0)
print(f"TargetName={self.cfg.TargetName}")
print(f"MemoryName={self.cfg.MemoryName}")
print(f"Version={self.cfg.Version}")
if self.cfg.MemoryName.lower()=="emmc":
self.cfg.SECTOR_SIZE_IN_BYTES=512
elif self.cfg.MemoryName.lower()=="ufs":
self.cfg.SECTOR_SIZE_IN_BYTES = 4096
return True
# OEM Stuff here below --------------------------------------------------
def cmd_writeimei(self,imei):
if len(imei)!=16:
print("IMEI must be 16 digits")
return False
data=""
val=self.xmlsend(data)
if val[0]==True:
print("writeIMEI succeeded.")
return True
else:
print("writeIMEI failed.")
return False
def cmd_getstorageinfo(self):
data=""
val=self.xmlsend(data)
if val[0]==True:
print(f"GetStorageInfo:\n--------------------\n")
print(val[1])
return True
else:
print("GetStorageInfo command isn't supported.")
return False
def cmd_peek(self,address,SizeInBytes,filename):
print(f"Peek: Address({hex(address)}),Size({hex(SizeInBytes)})")
with open(filename,"wb") as wf:
'''
'''
data=f"\n"
'''
'''
try:
self.cdc.write(data, self.cfg.MaxXMLSizeInBytes)
except:
data=data
addrinfo=self.cdc.read(self.cfg.MaxXMLSizeInBytes)
if (b"size_in_bytes" in addrinfo):
tmp=b""
while b"NAK" not in tmp and b"ACK" not in tmp:
tmp+=self.cdc.read(self.cfg.MaxXMLSizeInBytes)
data = f""
self.cdc.write(data, self.cfg.MaxXMLSizeInBytes)
addrinfo = self.cdc.read(self.cfg.MaxXMLSizeInBytes)
if b' old):
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
old=prog
if b'