268 lines
9.5 KiB
Python
268 lines
9.5 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Offline tests for Firehose <read> handling across 8k/2720v and FIH loaders."""
|
||
|
|
|
||
|
|
import os
|
||
|
|
import struct
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from unittest import mock
|
||
|
|
|
||
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
sys.path.insert(0, os.path.join(BASE_DIR, "vendor", "edl"))
|
||
|
|
|
||
|
|
import Library.firehose as firehose_mod # noqa: E402
|
||
|
|
import Library.usb as usb_mod # noqa: E402
|
||
|
|
from Library.firehose import qualcomm_firehose, xmlparser # noqa: E402
|
||
|
|
from Library.gpt import gpt # noqa: E402
|
||
|
|
from Library.usb import usb_class # noqa: E402
|
||
|
|
|
||
|
|
|
||
|
|
def build_synthetic_gpt(sectors=32):
|
||
|
|
"""Construct a 32-sector raw GPT image containing boot and userdata entries."""
|
||
|
|
buf = bytearray(sectors * 512)
|
||
|
|
# Protective MBR signature at LBA 0
|
||
|
|
buf[510:512] = b"\x55\xaa"
|
||
|
|
# GPT header at LBA 1 (revision 0x00010000 LE == 0x100 BE)
|
||
|
|
header = struct.pack(
|
||
|
|
"<8s4I4Q16sQ2I",
|
||
|
|
b"EFI PART",
|
||
|
|
0x00010000,
|
||
|
|
92,
|
||
|
|
0,
|
||
|
|
0,
|
||
|
|
1,
|
||
|
|
1023,
|
||
|
|
34,
|
||
|
|
1000,
|
||
|
|
b"\x11" * 16,
|
||
|
|
2,
|
||
|
|
4,
|
||
|
|
128,
|
||
|
|
)
|
||
|
|
buf[512:512 + len(header)] = header
|
||
|
|
# Partition entries starting at LBA 2 (offset 1024)
|
||
|
|
entries = [
|
||
|
|
("boot", 34, 97),
|
||
|
|
("userdata", 100, 999),
|
||
|
|
]
|
||
|
|
for idx, (name, first_lba, last_lba) in enumerate(entries):
|
||
|
|
off = 1024 + idx * 128
|
||
|
|
ptype = struct.pack("<I", 0xEBD0A0A2) + b"\x00" * 12
|
||
|
|
puniq = bytes([idx + 1]) * 16
|
||
|
|
pname = name.encode("utf-16-le").ljust(72, b"\x00")
|
||
|
|
entry = struct.pack("<16s16s3Q72s", ptype, puniq, first_lba, last_lba, 0, pname)
|
||
|
|
buf[off:off + 128] = entry
|
||
|
|
return bytes(buf)
|
||
|
|
|
||
|
|
|
||
|
|
class FakeCdc:
|
||
|
|
def __init__(self, rx_packets):
|
||
|
|
self.rx_packets = [bytearray(pkt) for pkt in rx_packets]
|
||
|
|
self.tx_packets = []
|
||
|
|
self.read_lengths = []
|
||
|
|
|
||
|
|
def write(self, data, pktsize=64, timeout=None):
|
||
|
|
self.tx_packets.append((bytes(data) if not isinstance(data, str) else data.encode("utf-8"), pktsize))
|
||
|
|
|
||
|
|
def read(self, length=0x80000, timeout=None):
|
||
|
|
self.read_lengths.append(length)
|
||
|
|
if not self.rx_packets:
|
||
|
|
return bytearray()
|
||
|
|
pkt = self.rx_packets.pop(0)
|
||
|
|
if len(pkt) <= length:
|
||
|
|
return pkt
|
||
|
|
out = pkt[:length]
|
||
|
|
self.rx_packets.insert(0, pkt[length:])
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
class TestEdlFirehoseRead(unittest.TestCase):
|
||
|
|
def _make_fh(self, rx_packets, max_payload=1048576):
|
||
|
|
cfg = qualcomm_firehose.cfg()
|
||
|
|
cfg.MemoryName = "eMMC"
|
||
|
|
cfg.SECTOR_SIZE_IN_BYTES = 512
|
||
|
|
cfg.MaxPayloadSizeToTargetInBytes = max_payload
|
||
|
|
cfg.MaxXMLSizeInBytes = 4096
|
||
|
|
cdc = FakeCdc(rx_packets)
|
||
|
|
return qualcomm_firehose(cdc, xmlparser(), cfg), cdc
|
||
|
|
|
||
|
|
def test_8k_rawmode_ack_gpt_parse(self):
|
||
|
|
gpt_bytes = build_synthetic_gpt(32)
|
||
|
|
rx = [
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><log value="start 0, num 32" /></data>',
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><response value="ACK" rawmode="true" /></data>\x00',
|
||
|
|
gpt_bytes[:8192],
|
||
|
|
gpt_bytes[8192:],
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><log value="Finished sector address 32" /></data>',
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><response value="ACK" rawmode="false" /></data>',
|
||
|
|
]
|
||
|
|
fh, cdc = self._make_fh(rx)
|
||
|
|
with mock.patch.object(firehose_mod.time, "sleep"):
|
||
|
|
got = fh.cmd_read_buffer(0, 0, 32, Display=False)
|
||
|
|
self.assertEqual(bytes(got), gpt_bytes)
|
||
|
|
table = gpt()
|
||
|
|
self.assertTrue(table.parse(got, 512))
|
||
|
|
self.assertEqual([e.name for e in table.partentries], ["boot", "userdata"])
|
||
|
|
# Check raw reads never ask for more than remaining bytes (16 KiB here).
|
||
|
|
self.assertEqual(cdc.read_lengths[0], 16384)
|
||
|
|
self.assertEqual(cdc.read_lengths[1], 16384)
|
||
|
|
self.assertEqual(cdc.read_lengths[2], 16384)
|
||
|
|
self.assertEqual(cdc.read_lengths[3], 8192)
|
||
|
|
|
||
|
|
def test_fih_immediate_raw_gpt_parse(self):
|
||
|
|
gpt_bytes = build_synthetic_gpt(32)
|
||
|
|
rx = [
|
||
|
|
gpt_bytes[:4096],
|
||
|
|
gpt_bytes[4096:12288],
|
||
|
|
gpt_bytes[12288:],
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><response value="ACK" rawmode="false" /></data>',
|
||
|
|
]
|
||
|
|
fh, cdc = self._make_fh(rx)
|
||
|
|
with mock.patch.object(firehose_mod.time, "sleep"):
|
||
|
|
got = fh.cmd_read_buffer(0, 0, 32, Display=False)
|
||
|
|
self.assertEqual(bytes(got), gpt_bytes)
|
||
|
|
table = gpt()
|
||
|
|
self.assertTrue(table.parse(got, 512))
|
||
|
|
self.assertEqual([e.name for e in table.partentries], ["boot", "userdata"])
|
||
|
|
self.assertEqual(cdc.read_lengths[:3], [16384, 12288, 4096])
|
||
|
|
|
||
|
|
def test_single_sector_cmd_read_both_variants(self):
|
||
|
|
sector = bytes([0xA5]) * 512
|
||
|
|
for name, ack_pkts in (
|
||
|
|
("8k", [b'<?xml version="1.0" encoding="UTF-8" ?><data><response value="ACK" rawmode="true" /></data>']),
|
||
|
|
("fih", []),
|
||
|
|
):
|
||
|
|
with self.subTest(loader=name):
|
||
|
|
rx = ack_pkts + [
|
||
|
|
sector,
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><response value="ACK" rawmode="false" /></data>',
|
||
|
|
]
|
||
|
|
fh, cdc = self._make_fh(rx)
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
out = os.path.join(tmp, "sec.bin")
|
||
|
|
with mock.patch.object(firehose_mod.time, "sleep"):
|
||
|
|
res = fh.cmd_read(0, 7, 1, out, Display=False)
|
||
|
|
self.assertTrue(res)
|
||
|
|
self.assertNotEqual(res, "")
|
||
|
|
with open(out, "rb") as handle:
|
||
|
|
self.assertEqual(handle.read(), sector)
|
||
|
|
self.assertEqual(cdc.read_lengths[0], 512)
|
||
|
|
|
||
|
|
def test_nak_before_raw_fails_cleanly(self):
|
||
|
|
rx = [
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><log value="Invalid physical partition 9" /></data>',
|
||
|
|
b'<?xml version="1.0" encoding="UTF-8" ?><data><response value="NAK" rawmode="false" /></data>',
|
||
|
|
]
|
||
|
|
fh, _ = self._make_fh(rx)
|
||
|
|
got = fh.cmd_read_buffer(9, 0, 1, Display=False)
|
||
|
|
self.assertEqual(got, "")
|
||
|
|
|
||
|
|
def test_usb_connect_preserves_active_configuration(self):
|
||
|
|
fake_ep_out = object()
|
||
|
|
fake_ep_in = object()
|
||
|
|
|
||
|
|
class FakeDevice:
|
||
|
|
def __init__(self, initial_cfg, raise_on_get=False):
|
||
|
|
self.initial_cfg = initial_cfg
|
||
|
|
self.raise_on_get = raise_on_get
|
||
|
|
self.set_configuration_calls = 0
|
||
|
|
|
||
|
|
def is_kernel_driver_active(self, _):
|
||
|
|
return False
|
||
|
|
|
||
|
|
def get_active_configuration(self):
|
||
|
|
if self.raise_on_get and self.set_configuration_calls == 0:
|
||
|
|
raise usb_mod.usb.core.USBError("Entity not found")
|
||
|
|
return self.initial_cfg if self.set_configuration_calls == 0 else "cfg1"
|
||
|
|
|
||
|
|
def set_configuration(self):
|
||
|
|
self.set_configuration_calls += 1
|
||
|
|
|
||
|
|
with mock.patch.object(usb_mod.usb.util, "find_descriptor", side_effect=[
|
||
|
|
"intf0", fake_ep_out, fake_ep_in,
|
||
|
|
"intf0", fake_ep_out, fake_ep_in,
|
||
|
|
"intf0", fake_ep_out, fake_ep_in,
|
||
|
|
]), mock.patch.object(usb_mod.usb.util, "claim_interface"):
|
||
|
|
dev_active = FakeDevice("cfg1")
|
||
|
|
with mock.patch.object(usb_mod.usb.core, "find", return_value=dev_active):
|
||
|
|
cdc = usb_class()
|
||
|
|
self.assertTrue(cdc.connect())
|
||
|
|
self.assertEqual(dev_active.set_configuration_calls, 0)
|
||
|
|
|
||
|
|
dev_none = FakeDevice(None)
|
||
|
|
with mock.patch.object(usb_mod.usb.core, "find", return_value=dev_none):
|
||
|
|
cdc = usb_class()
|
||
|
|
self.assertTrue(cdc.connect())
|
||
|
|
self.assertEqual(dev_none.set_configuration_calls, 1)
|
||
|
|
|
||
|
|
dev_err = FakeDevice("cfg1", raise_on_get=True)
|
||
|
|
with mock.patch.object(usb_mod.usb.core, "find", return_value=dev_err):
|
||
|
|
cdc = usb_class()
|
||
|
|
self.assertTrue(cdc.connect())
|
||
|
|
self.assertEqual(dev_err.set_configuration_calls, 1)
|
||
|
|
|
||
|
|
def test_usb_timeout_defaults_and_sahara_connect_probe(self):
|
||
|
|
self.assertEqual(usb_mod.DEFAULT_USB_TIMEOUT_MS, 30000)
|
||
|
|
|
||
|
|
class TimeoutDevice:
|
||
|
|
def __init__(self):
|
||
|
|
self.default_timeout = 1000
|
||
|
|
self.writes = []
|
||
|
|
self.reads = []
|
||
|
|
|
||
|
|
def is_kernel_driver_active(self, _):
|
||
|
|
return False
|
||
|
|
|
||
|
|
def get_active_configuration(self):
|
||
|
|
return "cfg1"
|
||
|
|
|
||
|
|
def write(self, ep, payload, timeout=None):
|
||
|
|
self.writes.append((ep, bytes(payload), timeout))
|
||
|
|
|
||
|
|
def read(self, ep, length, timeout=None):
|
||
|
|
self.reads.append((ep, length, timeout))
|
||
|
|
return b"ok"
|
||
|
|
|
||
|
|
dev = TimeoutDevice()
|
||
|
|
with mock.patch.object(usb_mod.usb.core, "find", return_value=dev), \
|
||
|
|
mock.patch.object(usb_mod.usb.util, "find_descriptor", side_effect=["intf0", "ep_out", "ep_in"]), \
|
||
|
|
mock.patch.object(usb_mod.usb.util, "claim_interface"):
|
||
|
|
cdc = usb_class()
|
||
|
|
self.assertTrue(cdc.connect())
|
||
|
|
self.assertEqual(dev.default_timeout, 30000)
|
||
|
|
cdc.write(b"abc")
|
||
|
|
cdc.read(64)
|
||
|
|
cdc.read(64, timeout=2000)
|
||
|
|
self.assertEqual(dev.writes[-1], ("ep_out", b"abc", 30000))
|
||
|
|
self.assertEqual(dev.reads, [("ep_in", 64, 30000), ("ep_in", 64, 2000)])
|
||
|
|
|
||
|
|
class ProbeCdc:
|
||
|
|
def __init__(self):
|
||
|
|
self.read_calls = []
|
||
|
|
self.write_calls = []
|
||
|
|
|
||
|
|
def read(self, length=0x80000, timeout=None):
|
||
|
|
self.read_calls.append((length, timeout))
|
||
|
|
if len(self.read_calls) == 1:
|
||
|
|
raise TimeoutError("probe timed out")
|
||
|
|
return bytearray(b'<?xml version="1.0" ?><data><response value="ACK" /></data>')
|
||
|
|
|
||
|
|
def write(self, data, pktsize=64, timeout=None):
|
||
|
|
self.write_calls.append((data, pktsize, timeout))
|
||
|
|
|
||
|
|
from Library.sahara import qualcomm_sahara
|
||
|
|
pcdc = ProbeCdc()
|
||
|
|
sahara = qualcomm_sahara(pcdc)
|
||
|
|
self.assertEqual(sahara.connect(), "Firehose")
|
||
|
|
self.assertEqual(pcdc.read_calls[0][1], 2000)
|
||
|
|
self.assertIsNone(pcdc.read_calls[1][1])
|
||
|
|
|
||
|
|
with open(os.path.join(BASE_DIR, "flash-edl.py"), "r", encoding="utf-8") as handle:
|
||
|
|
flash_src = handle.read()
|
||
|
|
self.assertIn("DEFAULT_USB_IO_TIMEOUT_SEC = 30", flash_src)
|
||
|
|
self.assertIn('"--usb-timeout"', flash_src)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|