From 309997a175f895f44908b6d2878c9c4813044f89 Mon Sep 17 00:00:00 2001 From: James Coleman Date: Thu, 27 Aug 2026 16:35:19 -0500 Subject: [PATCH] Initial commit Single-file Python CLI for a Qualcomm modem's EFS2 filesystem over the DIAG serial port: ls, stat, pull, push, and recursive backup. Talks DIAG over pyserial to work around EfsTools' libnserial EIO on option-driver diag ports. Auto-detects the port by probing for a DIAG response. --- .gitignore | 3 + License.txt | 19 +++ README.md | 88 +++++++++++++ efs2.py | 358 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 468 insertions(+) create mode 100644 .gitignore create mode 100644 License.txt create mode 100644 README.md create mode 100644 efs2.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b908d4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +.DS_Store diff --git a/License.txt b/License.txt new file mode 100644 index 0000000..83d4a90 --- /dev/null +++ b/License.txt @@ -0,0 +1,19 @@ +Copyright (c) 2026 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9210fad --- /dev/null +++ b/README.md @@ -0,0 +1,88 @@ +# qcdm-efs2 + +A small command-line client for a Qualcomm modem's EFS2 filesystem, over the +DIAG serial port, in one file of Python. List, stat, pull, push, and back up +files in the modem's EFS from Linux, no Windows and no QPST. + +I wrote it to back up and replace the RF band policy files (`/policyman`) on a +rooted Sony Xperia 1 VI, after finding that the usual tool, +[EfsTools](https://github.com/JohnBel/EfsTools), fails on Linux against the +`option`-driver diag port: its native serial layer sets modem control lines on +open and the driver rejects the ioctl with `EIO`. `pyserial` opens the same +port fine, so this talks DIAG over `pyserial` instead. The EFS2 packet layouts +are taken from EfsTools, so the wire format is identical; only the serial +handling differs. + +The full write-up, including the Xperia band-unlock use case, is +[here](https://mrgecko.org/blog/2026/enable-us-5g-xperia-1-vi-from-linux). + +## Requirements + +- Python 3 +- `pyserial` (`pip install pyserial`) +- A rooted phone with a Qualcomm modem, in DIAG mode +- Root on the Linux host to bind the serial driver + +## Getting the phone onto a serial port + +Put the phone into a USB composition that exposes the DIAG port (keeps adb): + +``` +adb shell su -c 'setprop sys.usb.config diag,serial_cdev,rmnet,adb' +``` + +It re-enumerates as a Qualcomm DIAG device (`05c6:9091`). The kernel does not +bind a serial driver on its own, so load `option` and hand it the USB id: + +``` +sudo modprobe option +echo 05c6 9091 | sudo tee /sys/bus/usb-serial/drivers/option1/new_id +``` + +That composition exposes a few serial nodes and only one is DIAG. You don't +have to work out which: `efs2.py` probes each candidate, sends a DIAG version +request, and keeps the one that answers. It looks at `/dev/serial/by-id/` +interface 0 (`-if00-`) first, then the rest. Set `DIAGPORT` or pass `--port` to +override. + +## Usage + +``` +efs2.py [-h] [-p PORT] {ls,stat,pull,push,backup} ... + + ls list an EFS directory + stat stat an EFS file + pull download one EFS file + push upload/overwrite one EFS file, then verify the read-back + backup recursively download an EFS directory +``` + +Examples: + +```sh +# see what is in the modem's band-policy folder +python3 efs2.py ls /policyman + +# back up a whole directory (subdirectories included) before changing anything +python3 efs2.py backup /policyman ./policyman-backup + +# download and upload single files +python3 efs2.py pull /policyman/band_set_01.xml +python3 efs2.py push band_set_01.xml /policyman/band_set_01.xml +``` + +`push` deletes the target, re-creates it, writes in 1 KB chunks, then reads it +back and reports whether it matches. That read-back is the point: a truncated +write to the modem EFS is how you get a modem that won't register. + +## Warning + +This writes to the modem's EFS. A bad write to the wrong file can stop the +modem from booting or registering. Always `backup` first, and know your +recovery path (for Sony phones, reflashing the modem `.sin` files with +Newflasher restores stock EFS). Use at your own risk. + +## License + +MIT, see `License.txt`. EFS2 protocol layouts derived from +[EfsTools](https://github.com/JohnBel/EfsTools) by JohnBel. diff --git a/efs2.py b/efs2.py new file mode 100644 index 0000000..cec4146 --- /dev/null +++ b/efs2.py @@ -0,0 +1,358 @@ +#!/usr/bin/env python3 +"""Minimal Qualcomm DIAG EFS2 client over a serial diag port. + +Command-line tool to list, pull, push and back up files in a rooted phone's +modem EFS. Packet layouts and opcodes replicate JohnBel/EfsTools exactly. +Originally written to back up and replace the policyman RF-band config on a +Sony Xperia 1 VI. Run with -h for usage. +""" +import sys, os, time, struct, glob, argparse, serial + +DIAG_SUBSYS = 0x4B +SUBSYS_EFS = 19 +# EFS opcodes (EfsTools QcdmEfsCommand) +HELLO, QUERY, OPEN, CLOSE, READ, WRITE = 0, 1, 2, 3, 4, 5 +UNLINK, MKDIR, OPENDIR, READDIR, CLOSEDIR, STAT = 8, 9, 11, 12, 13, 15 +# open flags +O_WRONLY, O_RDONLY, O_CREAT = 0o1, 0o0, 0o100 + +# CRC-16 CCITT reflected (HDLC FCS-16) +_TAB = [] +for _i in range(256): + _c = _i + for _ in range(8): + _c = (_c >> 1) ^ 0x8408 if (_c & 1) else (_c >> 1) + _TAB.append(_c) + +def fcs16(data): + fcs = 0xFFFF + for b in data: + fcs = (fcs >> 8) ^ _TAB[(fcs ^ b) & 0xFF] + return (~fcs) & 0xFFFF + +def hdlc_encode(payload): + crc = fcs16(payload) + frame = bytes(payload) + struct.pack("= 3 and dec[0] == 0x00 and \ + fcs16(dec[:-2]) == struct.unpack("= 4 and p[0] == DIAG_SUBSYS and p[1] == SUBSYS_EFS \ + and struct.unpack(" ignore + raise TimeoutError("no matching response for efs cmd %d" % efs_cmd) + + # ---- EFS operations ---- + def hello(self): + win, wbyte, ver = 0x100000, 0x100000, 1 + body = struct.pack(" %s (%d bytes)" % (args.path, local, len(data))) + + +def cmd_push(d, args): + with open(args.local, "rb") as f: + data = f.read() + n = d.write_file(args.path, data) + verified = d.read_file(args.path) == data + print("pushed %s -> %s (%d bytes), read-back %s" + % (args.local, args.path, n, "matches" if verified else "MISMATCH")) + if not verified: + raise SystemExit(2) + + +def cmd_backup(d, args): + written = [0] + + def walk(efs_dir, local_dir): + os.makedirs(local_dir, exist_ok=True) + for name, etype, size in d.listdir(efs_dir): + efs_path = efs_dir.rstrip("/") + "/" + name + local_path = os.path.join(local_dir, name) + if etype == 1: # directory + walk(efs_path, local_path) + elif etype in (0, 15): # regular file or item file + try: + data = d.read_file(efs_path) + except Exception as e: + sys.stderr.write(" skip %s: %s\n" % (efs_path, e)) + continue + with open(local_path, "wb") as f: + f.write(data) + written[0] += 1 + print(" %s (%d bytes)" % (efs_path, len(data))) + + walk(args.path, args.dest) + print("backed up %d files from %s to %s" % (written[0], args.path, args.dest)) + + +def build_parser(): + p = argparse.ArgumentParser( + prog="efs2.py", + description="Minimal Qualcomm DIAG EFS2 client: list, pull, push and " + "back up files in a rooted phone's modem EFS.", + epilog="Put the phone in diag mode first (adb shell su -c 'setprop " + "sys.usb.config diag,serial_cdev,rmnet,adb') and bind the option " + "driver. The diag port is auto-detected; override with --port or " + "the DIAGPORT env var.") + p.add_argument("-p", "--port", help="diag serial port (default: auto-detect)") + sub = p.add_subparsers(dest="command", required=True) + + s = sub.add_parser("ls", help="list an EFS directory") + s.add_argument("path") + s.set_defaults(func=cmd_ls) + + s = sub.add_parser("stat", help="stat an EFS file") + s.add_argument("path") + s.set_defaults(func=cmd_stat) + + s = sub.add_parser("pull", help="download one EFS file") + s.add_argument("path", help="EFS path, e.g. /policyman/band_set_01.xml") + s.add_argument("local", nargs="?", help="local path (default: the basename)") + s.set_defaults(func=cmd_pull) + + s = sub.add_parser("push", help="upload/overwrite one EFS file, then verify") + s.add_argument("local", help="local file to upload") + s.add_argument("path", help="destination EFS path") + s.set_defaults(func=cmd_push) + + s = sub.add_parser("backup", help="recursively download an EFS directory") + s.add_argument("path", help="EFS directory, e.g. /policyman") + s.add_argument("dest", help="local directory to write into") + s.set_defaults(func=cmd_backup) + return p + + +def main(): + args = build_parser().parse_args() + d = Diag(args.port) + sys.stderr.write("using diag port %s (efs v%d)\n" % (d.port, d.hello())) + try: + args.func(d, args) + finally: + d.s.close() + + +if __name__ == "__main__": + main()