feat: 刷机程序按型号配置工作并在 devinfo 记录芯片号
This commit is contained in:
+145
-52
@@ -2,38 +2,20 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
EDL = os.path.join(HERE, "edl")
|
||||
LOADER = os.path.join(
|
||||
EDL, "Loaders", "cyanogen", "007050e100000000_4614048173062ae4_fhprg_peek.bin"
|
||||
)
|
||||
IMAGES = os.path.join(os.path.dirname(HERE), "images")
|
||||
|
||||
WRITES = [
|
||||
("gpt_primary.bin", 0),
|
||||
("gpt_backup.bin", 7471038),
|
||||
("cdt.bin", 131072),
|
||||
("sbl1.mbn", 262144),
|
||||
("rpm.mbn", 263168),
|
||||
("tz.mbn", 264192),
|
||||
("hyp.mbn", 266240),
|
||||
("aboot.mbn", 524288),
|
||||
("boot.img", 526336),
|
||||
("rootfs.img", 659456),
|
||||
]
|
||||
EFS = [
|
||||
("modemst1.bin", "modemst1", 267296),
|
||||
("modemst2.bin", "modemst2", 271392),
|
||||
("fsc.bin", "fsc", 275488),
|
||||
("fsg.bin", "fsg", 393216),
|
||||
]
|
||||
EFS_BYTES = {
|
||||
"modemst1": 2 * 1024 * 1024,
|
||||
"modemst2": 2 * 1024 * 1024,
|
||||
"fsc": 1024,
|
||||
"fsg": 2 * 1024 * 1024,
|
||||
}
|
||||
LOADER = ""
|
||||
IMAGES = ""
|
||||
MEMORY = "eMMC"
|
||||
MAXPAYLOAD = "16384"
|
||||
REQUIRED = []
|
||||
BACKUPS = []
|
||||
FLASHES = []
|
||||
DEVINFO_LBA = 0
|
||||
DEVINFO_SECTORS = 0
|
||||
MODEL_ID = ""
|
||||
|
||||
|
||||
def emit(key, value):
|
||||
@@ -45,6 +27,64 @@ def fail(message):
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def load_profile(path):
|
||||
# 型号资料是唯一的分区和镜像来源。images 相对资料目录,loader 相对程序目录。
|
||||
global LOADER, IMAGES, MEMORY, MAXPAYLOAD, REQUIRED, BACKUPS, FLASHES
|
||||
global DEVINFO_LBA, DEVINFO_SECTORS, MODEL_ID
|
||||
profile_dir = os.path.dirname(os.path.abspath(path))
|
||||
app_dir = os.path.dirname(os.path.dirname(profile_dir))
|
||||
values = {"partition": [], "backup": [], "flash": []}
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
for raw in handle:
|
||||
line = raw.split("#", 1)[0].strip()
|
||||
if not line or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
if key in values:
|
||||
values[key].append(value)
|
||||
else:
|
||||
values[key] = value
|
||||
images = values.get("images") or ""
|
||||
loader = values.get("loader") or ""
|
||||
if not images or not loader:
|
||||
fail("型号配置缺少镜像目录或引导文件")
|
||||
IMAGES = images if os.path.isabs(images) else os.path.join(profile_dir, images)
|
||||
LOADER = loader if os.path.isabs(loader) else os.path.join(app_dir, loader)
|
||||
MEMORY = values.get("memory") or "eMMC"
|
||||
MAXPAYLOAD = str(values.get("maxpayload") or "16384")
|
||||
REQUIRED = values["partition"]
|
||||
BACKUPS = []
|
||||
for item in values["backup"]:
|
||||
name, lba, size = [part.strip() for part in item.split(",")]
|
||||
BACKUPS.append((name, int(lba), int(size)))
|
||||
FLASHES = []
|
||||
for item in values["flash"]:
|
||||
filename, lba = [part.strip() for part in item.split(",")]
|
||||
FLASHES.append((filename, int(lba)))
|
||||
if not REQUIRED:
|
||||
fail("型号配置没有用于核对的分区")
|
||||
if not os.path.isfile(LOADER):
|
||||
fail("找不到引导文件")
|
||||
MODEL_ID = values.get("id") or ""
|
||||
devinfo = values.get("devinfo") or ""
|
||||
if devinfo:
|
||||
lba, count = [part.strip() for part in devinfo.split(",")]
|
||||
DEVINFO_LBA = int(lba)
|
||||
DEVINFO_SECTORS = int(count)
|
||||
|
||||
|
||||
def edl_argv():
|
||||
return [
|
||||
"edl.py",
|
||||
"printgpt",
|
||||
"--memory=" + MEMORY,
|
||||
"--maxpayload=" + MAXPAYLOAD,
|
||||
"--loader=" + LOADER,
|
||||
]
|
||||
|
||||
|
||||
def format_size(sectors):
|
||||
size = int(sectors) * 512
|
||||
gib = 1024 ** 3
|
||||
@@ -76,13 +116,7 @@ def open_link():
|
||||
os.chdir(EDL)
|
||||
if EDL not in sys.path:
|
||||
sys.path.insert(0, EDL)
|
||||
sys.argv = [
|
||||
"edl.py",
|
||||
"printgpt",
|
||||
"--memory=eMMC",
|
||||
"--maxpayload=16384",
|
||||
"--loader=" + LOADER,
|
||||
]
|
||||
sys.argv = edl_argv()
|
||||
import edl
|
||||
fill_edl_args(edl.args)
|
||||
from edlclient.Config.usb_ids import default_ids
|
||||
@@ -134,13 +168,7 @@ def connect():
|
||||
os.chdir(EDL)
|
||||
if EDL not in sys.path:
|
||||
sys.path.insert(0, EDL)
|
||||
sys.argv = [
|
||||
"edl.py",
|
||||
"printgpt",
|
||||
"--memory=eMMC",
|
||||
"--maxpayload=16384",
|
||||
"--loader=" + LOADER,
|
||||
]
|
||||
sys.argv = edl_argv()
|
||||
import edl
|
||||
fill_edl_args(edl.args)
|
||||
|
||||
@@ -273,12 +301,33 @@ def chip_name(app):
|
||||
return hwid
|
||||
|
||||
|
||||
def gpt_names(app):
|
||||
try:
|
||||
_data, gpt = app.fh.firehose.get_gpt(0, 0, 0, 0)
|
||||
except SystemExit:
|
||||
raise
|
||||
except Exception as exc:
|
||||
fail(f"没有读到分区表:{exc}")
|
||||
entries = getattr(gpt, "partentries", None) if gpt is not None else None
|
||||
if not entries:
|
||||
fail("没有读到分区表")
|
||||
return {str(name).lower() for name in entries}
|
||||
|
||||
|
||||
def ensure_layout(app):
|
||||
# 已在本次 Firehose 会话中。不符合就退出,不写盘,也不发复位。
|
||||
names = gpt_names(app)
|
||||
missing = [name for name in REQUIRED if name.lower() not in names]
|
||||
if missing:
|
||||
fail("分区不符,已停止:" + "、".join(missing) + "。请拔出并按住复位键后重新插入")
|
||||
|
||||
|
||||
def read_part(app, name, path):
|
||||
options = {
|
||||
"<partitionname>": name,
|
||||
"<filename>": path,
|
||||
"--lun": None,
|
||||
"--memory": "eMMC",
|
||||
"--memory": MEMORY,
|
||||
}
|
||||
if not app.fh.handle_firehose("r", options):
|
||||
fail(f"读取 {name} 失败")
|
||||
@@ -294,12 +343,37 @@ def fit_partition(path, size):
|
||||
handle.write(b"\x00" * (size - len(data)))
|
||||
|
||||
|
||||
def write_board_id(app, chip):
|
||||
# 写在 devinfo 最后一个扇区,保留分区开头给引导程序。
|
||||
if DEVINFO_LBA <= 0 or DEVINFO_SECTORS <= 0:
|
||||
return
|
||||
if not chip:
|
||||
fail("没有芯片号,不能写入 devinfo")
|
||||
text = f"FLASHER1\nchip_serial={chip}\nmodel={MODEL_ID}\n"
|
||||
blob = text.encode("ascii")
|
||||
if len(blob) > 512:
|
||||
fail("devinfo 记录过长")
|
||||
blob += b"\x00" * (512 - len(blob))
|
||||
fd, path = tempfile.mkstemp(prefix="devinfo-", suffix=".bin")
|
||||
try:
|
||||
os.write(fd, blob)
|
||||
os.close(fd)
|
||||
fd = -1
|
||||
emit("STATUS", "正在写入 devinfo")
|
||||
write_file(app, DEVINFO_LBA + DEVINFO_SECTORS - 1, path)
|
||||
finally:
|
||||
if fd >= 0:
|
||||
os.close(fd)
|
||||
if os.path.isfile(path):
|
||||
os.remove(path)
|
||||
|
||||
|
||||
def write_file(app, start, path):
|
||||
options = {
|
||||
"<start_sector>": str(start),
|
||||
"<filename>": path,
|
||||
"--lun": None,
|
||||
"--memory": "eMMC",
|
||||
"--memory": MEMORY,
|
||||
"--skipwrite": False,
|
||||
}
|
||||
if not app.fh.handle_firehose("ws", options):
|
||||
@@ -314,6 +388,8 @@ def main():
|
||||
dest = ""
|
||||
full = False
|
||||
skip_efs = False
|
||||
profile = ""
|
||||
chip_arg = ""
|
||||
args = sys.argv[2:]
|
||||
i = 0
|
||||
while i < len(args):
|
||||
@@ -325,6 +401,14 @@ def main():
|
||||
dest = args[i + 1]
|
||||
i += 2
|
||||
continue
|
||||
if args[i] == "--profile" and i + 1 < len(args):
|
||||
profile = args[i + 1]
|
||||
i += 2
|
||||
continue
|
||||
if args[i] == "--chip" and i + 1 < len(args):
|
||||
chip_arg = args[i + 1]
|
||||
i += 2
|
||||
continue
|
||||
if args[i] == "--full":
|
||||
full = True
|
||||
i += 1
|
||||
@@ -334,6 +418,9 @@ def main():
|
||||
i += 1
|
||||
continue
|
||||
i += 1
|
||||
if not profile:
|
||||
fail("没有型号配置")
|
||||
load_profile(profile)
|
||||
if port:
|
||||
os.environ["EDL_USB_PORT"] = port
|
||||
if command == "info":
|
||||
@@ -354,6 +441,7 @@ def main():
|
||||
sectors = storage_sectors(app)
|
||||
emit("DISK", format_size(sectors))
|
||||
emit("SECTORS", str(sectors))
|
||||
ensure_layout(app)
|
||||
if command == "info":
|
||||
emit("DONE", "info")
|
||||
return
|
||||
@@ -362,11 +450,13 @@ def main():
|
||||
fail("没有备份目录")
|
||||
if skip_efs and not full:
|
||||
fail("没有要备份的内容")
|
||||
if not skip_efs and not BACKUPS:
|
||||
fail("型号配置没有备份分区")
|
||||
os.makedirs(dest, exist_ok=True)
|
||||
if not skip_efs:
|
||||
span = 60 if full else 80
|
||||
for index, (_file, name, _lba) in enumerate(EFS):
|
||||
emit("PROGRESS", str(int(index * span / len(EFS))))
|
||||
for index, (name, _lba, _size) in enumerate(BACKUPS):
|
||||
emit("PROGRESS", str(int(index * span / max(len(BACKUPS), 1))))
|
||||
read_part(app, name, os.path.join(dest, name + ".bin"))
|
||||
if full:
|
||||
emit("STATUS", "正在备份整盘")
|
||||
@@ -399,12 +489,14 @@ def main():
|
||||
if command == "flash":
|
||||
restore = bool(dest)
|
||||
if restore:
|
||||
for name in ("modemst1", "modemst2", "fsc", "fsg"):
|
||||
for name, _lba, _size in BACKUPS:
|
||||
if not os.path.isfile(os.path.join(dest, name + ".bin")):
|
||||
fail(f"缺少 {name} 备份")
|
||||
total = len(WRITES) + (len(EFS) if restore else 0)
|
||||
total = len(FLASHES) + (len(BACKUPS) if restore else 0)
|
||||
if total == 0:
|
||||
fail("型号配置没有写入步骤")
|
||||
step = 0
|
||||
for filename, start in WRITES:
|
||||
for filename, start in FLASHES:
|
||||
path = os.path.join(IMAGES, filename)
|
||||
if not os.path.isfile(path):
|
||||
fail(f"镜像缺少 {filename}")
|
||||
@@ -413,13 +505,14 @@ def main():
|
||||
step += 1
|
||||
emit("PROGRESS", str(int(step * 90 / total)))
|
||||
if restore:
|
||||
for filename, name, start in EFS:
|
||||
for name, start, size in BACKUPS:
|
||||
path = os.path.join(dest, name + ".bin")
|
||||
fit_partition(path, EFS_BYTES[name])
|
||||
fit_partition(path, size)
|
||||
emit("STATUS", f"正在写回 {name}")
|
||||
write_file(app, start, path)
|
||||
step += 1
|
||||
emit("PROGRESS", str(int(step * 90 / total)))
|
||||
write_board_id(app, chip_arg)
|
||||
try:
|
||||
app.cdc.timeout = 1500
|
||||
app.fh.firehose.cmd_reset("reset")
|
||||
|
||||
Reference in New Issue
Block a user