Files
bananarchy/vendor/edl/Library/firehose.py
T
Luxferre d7992904bf Bring up nokia-sparkler SMP/cpufreq/lk2nd/cam/haptics, fix timer and EDL USB timeout
- Add 0013 early-SCM 4-core SMP + cpufreq kernel patch and clock-frequency DT nodes
- Complete nokia-sparkler lk2nd port, boot/X11 session startup, 180-deg cached camera, and 157 Hz haptic audio
- Replace root-only timer backend with permissionless sxmo_timer.sh and keypad/hook cancellation
- Fix EDL Firehose rawmode ACK, USB active-config preservation, *+# hint, and 30 s default USB timeout
- Sync a1120 (cocojamboo) prebuilts, docs, tests, and verified checksums in check-rebuild.sh
2026-11-16 11:45:38 +02:00

541 lines
23 KiB
Python

import xml.etree.ElementTree as ET
import binascii
import time
import os
from Library.utils import *
class xmlparser():
def _iter_roots(self, raw):
raw_bytes = bytes(raw)
first_xml = raw_bytes.find(b"<?xml")
if first_xml == -1:
return []
roots = []
for chunk in raw_bytes[first_xml:].split(b"<?xml"):
if not chunk:
continue
part = b"<?xml" + chunk
end = part.find(b"</data>")
if end != -1:
part = part[:end + len(b"</data>")]
part = part.strip(b"\x00 \t\r\n")
if not part:
continue
try:
parser = ET.XMLParser(encoding="utf-8")
roots.append(ET.fromstring(part, parser=parser))
except ET.ParseError:
continue
return roots
def getresponse(self, input):
content = {}
for root in self._iter_roots(input):
for atype in root.findall('response'):
for field in atype.attrib:
content[field] = atype.attrib[field]
return content
def getlog(self, input):
data = ''
for root in self._iter_roots(input):
for atype in root.findall('log'):
if 'value' in atype.attrib:
data += atype.attrib['value']
return data
class qualcomm_firehose:
class cfg:
MemoryName = "eMMC"
TargetName = ""
Version = ""
ZLPAwareHost = 1
SkipStorageInit = 0
SkipWrite = 0
MaxPayloadSizeToTargetInBytes = 0x100000
SECTOR_SIZE_IN_BYTES=512
MaxXMLSizeInBytes=4096
bit64=True
def __init__(self,cdc,xml,cfg):
self.cdc=cdc
self.xml=xml
self.cfg=cfg
if self.cfg.MemoryName=="UFS":
self.cfg.SECTOR_SIZE_IN_BYTES=4096
def getstatus(self,resp):
if "value" in resp:
value = resp["value"]
if value == "ACK":
return True
return False
def xmlsend(self,data, skipResponse=False):
self.cdc.write(data,self.cfg.MaxXMLSizeInBytes)
if skipResponse:
return True, {}
data=bytearray()
while b"<response" not in data:
try:
data+=self.cdc.read(self.cfg.MaxXMLSizeInBytes)
except:
break
resp = self.xml.getresponse(data)
return [self.getstatus(resp),resp,data]
def cmd_reset(self):
data = "<?xml version=\"1.0\" ?><data><power value=\"reset\"/></data>"
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="<?xml version=\"1.0\" ?><data><nop /></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"<?xml version=\"1.0\" ?><data><getsha256digest SECTOR_SIZE_IN_BYTES=\"{self.cfg.SECTOR_SIZE_IN_BYTES}\""+\
f" num_partition_sectors=\"{num_partition_sectors}\""+\
f" physical_partition_number=\"{physical_partition_number}\""+\
f" start_sector=\"{start_sector}\"/>\n</data>"
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"<?xml version=\"1.0\" ?><data>\n<setbootablestoragedrive value=\"{hex(partition_number)}\" /></data>"
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"<response" not in data:
try:
data += self.cdc.read(self.cfg.MaxXMLSizeInBytes)
except:
break
info = self.xml.getlog(data)
rsp = self.xml.getresponse(data)
return info, rsp
def cmd_write(self,physical_partition_number,start_sector,filename,Display=True):
size = os.stat(filename).st_size
with open(filename,"rb") as rf:
#Make sure we fill data up to the sector size
num_partition_sectors=size // self.cfg.SECTOR_SIZE_IN_BYTES
if (size%self.cfg.SECTOR_SIZE_IN_BYTES)!=0:
num_partition_sectors+=1
if Display:
print(
f"\nWriting to physical partition {str(physical_partition_number)}, sector {str(start_sector)}, sectors {str(num_partition_sectors)}")
data=f"<?xml version=\"1.0\" ?><data>\n"+\
f"<program SECTOR_SIZE_IN_BYTES=\"{self.cfg.SECTOR_SIZE_IN_BYTES}\""+\
f" num_partition_sectors=\"{num_partition_sectors}\""+\
f" physical_partition_number=\"{physical_partition_number}\""+\
f" start_sector=\"{start_sector}\"/>\n</data>"
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"<?xml version=\"1.0\" ?><data>\n"+\
f"<program SECTOR_SIZE_IN_BYTES=\"{self.cfg.SECTOR_SIZE_IN_BYTES}\""+\
f" num_partition_sectors=\"{num_partition_sectors}\""+\
f" physical_partition_number=\"{physical_partition_number}\""+\
f" start_sector=\"{start_sector}\"/>\n</data>"
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<wlen:
wlen=bytesToWrite
self.cdc.write(empty[0:wlen],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}")
else:
print(f"Error:{rsp}")
return False
return False
def _begin_raw_read(self, xml_cmd, bytes_to_read):
"""Send a <read> command and handle optional initial ACK XML packets.
Standard Qualcomm Firehose loaders (8k.mbn, 2720v.mbn) answer <read>
with <response value="ACK" rawmode="true" /> before streaming raw
sectors, whereas FIH programmers (8110.mbn, 2720.mbn, 800t.mbn) stream
raw sector data immediately.
"""
self.cdc.write(xml_cmd, self.cfg.MaxXMLSizeInBytes)
sector_size = self.cfg.SECTOR_SIZE_IN_BYTES
max_chunk = max(sector_size, self.cfg.MaxPayloadSizeToTargetInBytes)
read_size = min(bytes_to_read, max_chunk) if bytes_to_read > 0 else max_chunk
pending = bytearray()
while True:
if pending:
pkt = bytes(pending)
pending = bytearray()
else:
pkt = bytes(self.cdc.read(read_size))
if not pkt:
return False, bytearray(), "empty response from device"
raw = pkt.lstrip(b"\x00 \t\r\n")
end_idx = pkt.find(b"</data>")
is_xml = (
raw.startswith(b"<?xml")
and end_idx != -1
and (b"<response" in pkt[:end_idx] or b"<log" in pkt[:end_idx])
)
if not is_xml:
return True, bytearray(pkt), ""
split_at = end_idx + len(b"</data>")
xml_part = pkt[:split_at]
tail = pkt[split_at:]
if tail and (len(tail) >= sector_size or tail.strip(b"\x00 \t\r\n")):
leftover = bytearray(tail)
else:
leftover = bytearray()
log_msg = self.xml.getlog(xml_part)
resp = self.xml.getresponse(xml_part)
if not resp:
if leftover and leftover.lstrip(b"\x00 \t\r\n").startswith(b"<?xml"):
pending = leftover
continue
if self.getstatus(resp):
return True, leftover, log_msg
return False, bytearray(), log_msg or xml_part.decode("utf-8", "replace")
def _read_raw_sectors(self, physical_partition_number, start_sector, num_partition_sectors, on_chunk, 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"<?xml version=\"1.0\" ?><data><read SECTOR_SIZE_IN_BYTES=\"{self.cfg.SECTOR_SIZE_IN_BYTES}\""
f" num_partition_sectors=\"{num_partition_sectors}\""
f" physical_partition_number=\"{physical_partition_number}\""
f" start_sector=\"{start_sector}\"/>\n</data>"
)
bytes_to_read = self.cfg.SECTOR_SIZE_IN_BYTES * num_partition_sectors
ok, initial_raw, err_info = self._begin_raw_read(data, bytes_to_read)
if not ok:
print(f"Error:{err_info}")
return False
max_chunk = max(self.cfg.SECTOR_SIZE_IN_BYTES, self.cfg.MaxPayloadSizeToTargetInBytes)
total = bytes_to_read
dataread = 0
old = 0
prog = 0
if Display:
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
if initial_raw and bytes_to_read > 0:
chunk = initial_raw[:bytes_to_read]
on_chunk(chunk)
bytes_to_read -= len(chunk)
dataread += len(chunk)
if total > 0:
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
while bytes_to_read > 0:
tmp = self.cdc.read(min(bytes_to_read, max_chunk))
if not tmp:
print("Error:Short read from device")
return False
chunk = tmp[:bytes_to_read]
on_chunk(chunk)
bytes_to_read -= len(chunk)
dataread += len(chunk)
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 True
print(f"Error:{info}")
return False
def cmd_read_buffer(self, physical_partition_number, start_sector, num_partition_sectors, Display=True):
resData = bytearray()
if not self._read_raw_sectors(
physical_partition_number,
start_sector,
num_partition_sectors,
resData.extend,
Display=Display,
):
return ""
return resData
def cmd_read(self, physical_partition_number, start_sector, num_partition_sectors, filename, Display=True):
with open(filename, "wb") as wf:
if not self._read_raw_sectors(
physical_partition_number,
start_sector,
num_partition_sectors,
wf.write,
Display=Display,
):
return ""
return True
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"<?xml version =\"1.0\" ?><data>"+\
f"<configure MemoryName=\"{self.cfg.MemoryName}\" ZLPAwareHost=\"{str(self.cfg.ZLPAwareHost)}\" "+\
f"SkipStorageInit=\"{str(int(self.cfg.SkipStorageInit))}\" SkipWrite=\"{str(int(self.cfg.SkipWrite))}\" "+\
f"MaxPayloadSizeToTargetInBytes=\"{str(self.cfg.MaxPayloadSizeToTargetInBytes)}\"/>"+\
"</data>"
'''
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><data><response value=\"ACK\" MinVersionSupported=\"1\"" \
"MemoryName=\"eMMC\" MaxPayloadSizeFromTargetInBytes=\"4096\" MaxPayloadSizeToTargetInBytes=\"1048576\" " \
"MaxPayloadSizeToTargetInBytesSupported=\"1048576\" MaxXMLSizeInBytes=\"4096\" Version=\"1\" TargetName=\"8953\" />" \
"</data>"
'''
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="<?xml version=\"1.0\" ?><data><writeIMEI len=\"16\"/></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="<?xml version=\"1.0\" ?><data><getstorageinfo /></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:
'''
<?xml version="1.0" ?><data><peek address64="1048576" SizeInBytes="90112" /></data>
'''
data=f"<?xml version=\"1.0\" ?><data><peek address64=\"{address}\" SizeInBytes=\"{SizeInBytes}\" /></data>\n"
'''
<?xml version="1.0" encoding="UTF-8" ?><data><log value="Using address 00100000" /></data>
<?xml version="1.0" encoding="UTF-8" ?><data><log value="0x22 0x00 0x00 0xEA 0x70 0x00 0x00 0xEA 0x74 0x00 0x00 0xEA 0x78 0x00 0
x00 0xEA 0x7C 0x00 0x00 0xEA 0x80 0x00 0x00 0xEA 0x84 0x00 0x00 0xEA 0x88 0x00 0x00 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA
0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0x
FF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF
0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF 0xFF 0xEA 0xFE 0xFF " /></data>
'''
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"<?xml version=\"1.0\" ?><data><peek address64=\"{hex(address)}\" size_in_bytes=\"{hex(SizeInBytes)}\" /></data>"
self.cdc.write(data, self.cfg.MaxXMLSizeInBytes)
addrinfo = self.cdc.read(self.cfg.MaxXMLSizeInBytes)
if b'<response' in addrinfo and 'NAK' in addrinfo:
print(f"Error:{addrinfo}")
return
if (b"address" in addrinfo and b"can\'t" in addrinfo):
tmp=b""
while b"NAK" not in tmp and b"ACK" not in tmp:
tmp+=self.cdc.read(self.cfg.MaxXMLSizeInBytes)
print(f"Error:{addrinfo}")
return
data=bytearray()
dataread=0
old=0
print_progress(0, 100, prefix='Progress:', suffix='Complete', bar_length=50)
while (True):
tmp=self.cdc.read(self.cfg.MaxXMLSizeInBytes)
if b'<response' in tmp or b"ERROR" in tmp:
break
tmp2=binascii.unhexlify(self.xml.getlog(tmp).replace("0x","").replace(" ",""))
dataread+=len(tmp2)
wf.write(tmp2)
prog = int(float(dataread) / float(SizeInBytes) * float(100))
if (prog > old):
print_progress(prog, 100, prefix='Progress:', suffix='Complete', bar_length=50)
old=prog
if b'<response' in tmp and b'ACK' in tmp:
print(f"Bytes from {hex(address)}, bytes read {hex(dataread)}, written to {filename}.")
return True
else:
print(f"Error:{addrinfo}")
return True