#!/usr/bin/env python3 """Boot splash for a Kupfer handset (OnePlus 6T): Plymouth with the Chandra theme. Run it as root on the phone, from a copy of this directory. docs/boot-splash.md explains it. install-plymouth.py [--package FILE] install, rebuild the initramfs, flash boot install-plymouth.py --rollback put back the boot partition and files saved at install --package FILE installs Plymouth from a downloaded package (a phone without internet). Nothing is flashed until a new initramfs has been built and checked. The first install saves the boot partition and every file it changes in /var/lib/chandra/boot-backup. """ import glob, os, re, shutil, subprocess, sys, tempfile here = os.path.dirname(os.path.abspath(__file__)) backup = "/var/lib/chandra/boot-backup" theme_src = os.path.join(here, "plymouth/chandra") theme_dst = "/usr/share/plymouth/themes/chandra" deviceinfo = "/etc/kupfer/deviceinfo" hook_conf = "/etc/kupfer/mkinitcpio.conf.d/60-chandra-plymouth.conf" daemon_conf = "/etc/plymouth/plymouthd.conf" changed = [deviceinfo, hook_conf, daemon_conf, "/etc/mkinitcpio.conf"] marker = "# chandra boot splash" # ignore-serial-consoles: console=ttyMSM0 would otherwise make Plymouth fall back to text. cmdline = "quiet splash loglevel=3 plymouth.ignore-serial-consoles vt.global_cursor_default=0" needed = ["usr/bin/plymouthd", "usr/lib/plymouth/script.so", "usr/lib/plymouth/renderers/drm.so", "usr/share/plymouth/themes/chandra/chandra.script", "usr/share/plymouth/themes/chandra/glow.png", "usr/share/plymouth/themes/chandra/moon-phases.png", "hooks/plymouth", "hooks/rootfsdetect", "hooks/rootfsresize", "etc/kupfer/deviceinfo", "a630_sqe.fw", "a630_gmu.bin", "a630_zap.mbn"] def run(*cmd, **kw): print("+", " ".join(cmd), flush=True) return subprocess.run(cmd, check=True, **kw) def boot_partition(): slot = re.search(r"androidboot\.slot_suffix=(_[ab])", open("/proc/cmdline").read()) if not slot: sys.exit("no androidboot.slot_suffix on the kernel command line: not this phone") return os.path.realpath("/dev/disk/by-partlabel/boot" + slot.group(1)) def saved(path): return os.path.join(backup, path.lstrip("/").replace("/", "__")) def write(path, text): os.makedirs(os.path.dirname(path), exist_ok=True) with open(path + ".new", "w") as f: f.write(text) os.chmod(path + ".new", 0o644) os.replace(path + ".new", path) def save_originals(part): image = os.path.join(backup, os.path.basename(os.path.realpath(part)) + ".img") done = os.path.join(backup, "complete") if not os.path.exists(done): os.makedirs(backup, mode=0o700, exist_ok=True) run("dd", "if=" + part, "of=" + image, "bs=4M", "status=none") run("cmp", part, image) shutil.copy2("/boot/initramfs-linux.img", backup) shutil.copy2("/boot/aboot.img", backup) for f in changed: if os.path.exists(f): shutil.copy2(f, saved(f)) open(done, "w").close() print("backup:", backup) def restore_files(): for f in changed: if os.path.exists(saved(f)): shutil.copy2(saved(f), f) elif os.path.exists(f): os.remove(f) def rollback(): part = boot_partition() image = os.path.join(backup, os.path.basename(part) + ".img") if not os.path.exists(os.path.join(backup, "complete")) or not os.path.exists(image): sys.exit("no backup of %s in %s" % (part, backup)) restore_files() shutil.copy2(os.path.join(backup, "initramfs-linux.img"), "/boot/initramfs-linux.img") shutil.copy2(os.path.join(backup, "aboot.img"), "/boot/aboot.img") run("dd", "if=" + image, "of=" + part, "bs=4M", "conv=fsync", "status=none") run("cmp", image, part) print("rolled back; reboot to use it") def install_package(package): if subprocess.run(["pacman", "-Q", "plymouth"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0: return # Keep pacman from building the initramfs and flashing it unchecked. with tempfile.TemporaryDirectory() as hooks: for h in ("90-linux-sdm845.hook", "90-mkinitcpio-install.hook", "91-android-bootimg-updater.hook"): os.symlink("/dev/null", os.path.join(hooks, h)) source = ["-U", package] if package else ["-S", "plymouth"] dirs = [d for d in ("/etc/pacman.d/hooks", hooks) if os.path.isdir(d)] # pacman refuses missing ones run("pacman", *[a for d in dirs for a in ("--hookdir", d)], "--noconfirm", *source) def configure(): shutil.copytree(theme_src, theme_dst, dirs_exist_ok=True) expected = set(os.listdir(theme_src)) for name in os.listdir(theme_dst): if name not in expected: path = os.path.join(theme_dst, name) if os.path.isdir(path) and not os.path.islink(path): shutil.rmtree(path) else: os.remove(path) for f in glob.glob(theme_dst + "/*"): os.chmod(f, 0o644) write(daemon_conf, "[Daemon]\nTheme=chandra\nShowDelay=0\nDeviceTimeout=8\n") # Sourced by mkinitcpio-overwrite after the base HOOKS: plymouth goes right after udev. write(hook_conf, '_h=" ${HOOKS[*]} "\nHOOKS=(${_h/ udev / udev plymouth })\nunset _h\n') text = open(deviceinfo).read() text = re.sub(r"\n*%s.*" % re.escape(marker), "", text, flags=re.S).rstrip("\n") old = re.search(r'^deviceinfo_kernel_cmdline="([^"]*)"', text, re.M) if not old: sys.exit("no deviceinfo_kernel_cmdline in " + deviceinfo) write(deviceinfo, '%s\n\n%s\ndeviceinfo_kernel_cmdline="%s %s"\n' % (text, marker, old.group(1), cmdline)) run("/usr/bin/mkinitcpio-overwrite") hooks = re.search(r"^HOOKS=\((.*)\)", open("/etc/mkinitcpio.conf").read(), re.M).group(1).split() if hooks.count("plymouth") != 1 or hooks[hooks.index("udev") + 1] != "plymouth": sys.exit("unexpected HOOKS: %s" % hooks) def check_initramfs(image): names = subprocess.run(["lsinitcpio", image], check=True, capture_output=True, text=True).stdout.splitlines() missing = [n for n in needed if not any(f.lstrip("./").endswith(n) for f in names)] if missing: sys.exit("the new initramfs lacks %s" % missing) print("initramfs ok: %d files, %.1f MB" % (len(names), os.path.getsize(image) / 1e6)) def build_and_flash(part): kver = os.uname().release trial = "/var/tmp/chandra-initramfs.img" run("mkinitcpio", "-k", kver, "-c", "/etc/mkinitcpio.conf", "-g", trial) check_initramfs(trial) vfs = os.statvfs("/boot") grow = 2 * (os.path.getsize(trial) - os.path.getsize("/boot/initramfs-linux.img")) if grow + 4 * 2**20 > vfs.f_bavail * vfs.f_frsize: sys.exit("not enough space on /boot") shutil.copyfile(trial, "/boot/initramfs-linux.img") os.chmod("/boot/initramfs-linux.img", 0o600) os.remove(trial) run("/usr/bin/update-bootimg") run("sync") size = os.path.getsize("/boot/aboot.img") run("cmp", "-n", str(size), "/boot/aboot.img", part) with tempfile.TemporaryDirectory() as d: info = subprocess.run(["unpack_bootimg", "--boot_img", part, "--out", d], check=True, capture_output=True, text=True).stdout if cmdline not in info: sys.exit("the flashed image's command line lacks the splash options; run --rollback\n" + info) run("cmp", os.path.join(d, "ramdisk"), "/boot/initramfs-linux.img") print("flashed and verified:", part) def main(): if os.geteuid() != 0: sys.exit("run as root") if "--rollback" in sys.argv: return rollback() package = sys.argv[sys.argv.index("--package") + 1] if "--package" in sys.argv else None part = boot_partition() save_originals(part) try: install_package(package) configure() build_and_flash(part) except BaseException: print("failed; see above. `%s --rollback` puts the old boot back" % sys.argv[0], file=sys.stderr) raise main()