2023-01-22 18:11:10 +00:00
|
|
|
# Copyright 2023 Oliver Smith
|
2020-02-20 20:07:28 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-05-26 20:08:45 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2017-07-19 18:05:34 +00:00
|
|
|
import pmb.config
|
2017-05-26 20:08:45 +00:00
|
|
|
import pmb.flasher
|
|
|
|
import pmb.install
|
2017-06-09 17:22:25 +00:00
|
|
|
import pmb.chroot.apk
|
|
|
|
import pmb.chroot.initfs
|
2017-05-26 20:08:45 +00:00
|
|
|
import pmb.chroot.other
|
2017-09-02 03:53:58 +00:00
|
|
|
import pmb.helpers.frontend
|
2017-09-18 21:36:54 +00:00
|
|
|
import pmb.parse.kconfig
|
2017-05-26 20:08:45 +00:00
|
|
|
|
2017-07-04 21:03:51 +00:00
|
|
|
|
|
|
|
def kernel(args):
|
2017-06-09 17:22:25 +00:00
|
|
|
# Rebuild the initramfs, just to make sure (see #69)
|
2020-01-22 18:30:43 +00:00
|
|
|
flavor = pmb.helpers.frontend._parse_flavor(args, args.autoinstall)
|
|
|
|
if args.autoinstall:
|
|
|
|
pmb.chroot.initfs.build(args, flavor, "rootfs_" + args.device)
|
2017-06-09 17:22:25 +00:00
|
|
|
|
2017-09-18 21:36:54 +00:00
|
|
|
# Check kernel config
|
2022-05-28 11:53:51 +00:00
|
|
|
pmb.parse.kconfig.check(args, flavor, must_exist=False)
|
2017-09-18 21:36:54 +00:00
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
# Generate the paths and run the flasher
|
|
|
|
if args.action_flasher == "boot":
|
|
|
|
logging.info("(native) boot " + flavor + " kernel")
|
2017-07-04 20:47:03 +00:00
|
|
|
pmb.flasher.run(args, "boot", flavor)
|
2017-05-26 20:08:45 +00:00
|
|
|
else:
|
2017-06-09 17:22:25 +00:00
|
|
|
logging.info("(native) flash kernel " + flavor)
|
2017-07-04 20:47:03 +00:00
|
|
|
pmb.flasher.run(args, "flash_kernel", flavor)
|
2017-07-19 18:05:34 +00:00
|
|
|
logging.info("You will get an IP automatically assigned to your "
|
|
|
|
"USB interface shortly.")
|
2021-05-19 18:55:40 +00:00
|
|
|
logging.info("Then you can connect to your device using ssh after pmOS has"
|
|
|
|
" booted:")
|
2017-10-17 16:24:34 +00:00
|
|
|
logging.info("ssh {}@{}".format(args.user, pmb.config.default_ip))
|
2021-05-19 18:55:40 +00:00
|
|
|
logging.info("NOTE: If you enabled full disk encryption, you should make"
|
2024-04-03 18:43:40 +00:00
|
|
|
" sure that Unl0kr has been properly configured for your"
|
2021-05-19 18:55:40 +00:00
|
|
|
" device")
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def list_flavors(args):
|
|
|
|
suffix = "rootfs_" + args.device
|
|
|
|
logging.info("(" + suffix + ") installed kernel flavors:")
|
2021-08-18 23:31:59 +00:00
|
|
|
logging.info("* " + pmb.chroot.other.kernel_flavor_installed(args, suffix))
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
|
2018-03-30 01:11:20 +00:00
|
|
|
def rootfs(args):
|
2020-02-04 10:30:28 +00:00
|
|
|
method = args.flash_method or args.deviceinfo["flash_method"]
|
|
|
|
|
2018-07-15 21:41:31 +00:00
|
|
|
# Generate rootfs, install flasher
|
2020-02-04 10:30:28 +00:00
|
|
|
suffix = ".img"
|
|
|
|
if pmb.config.flashers.get(method, {}).get("split", False):
|
|
|
|
suffix = "-root.img"
|
|
|
|
|
2021-05-19 18:55:40 +00:00
|
|
|
img_path = f"{args.work}/chroot_native/home/pmos/rootfs/{args.device}"\
|
|
|
|
f"{suffix}"
|
2020-02-04 10:30:28 +00:00
|
|
|
if not os.path.exists(img_path):
|
2018-07-15 21:41:31 +00:00
|
|
|
raise RuntimeError("The rootfs has not been generated yet, please run"
|
|
|
|
" 'pmbootstrap install' first.")
|
2017-05-26 20:08:45 +00:00
|
|
|
|
2017-11-11 22:00:22 +00:00
|
|
|
# Do not flash if using fastboot & image is too large
|
2021-05-19 18:55:40 +00:00
|
|
|
if method.startswith("fastboot") \
|
|
|
|
and args.deviceinfo["flash_fastboot_max_size"]:
|
2020-02-04 10:30:28 +00:00
|
|
|
img_size = os.path.getsize(img_path) / 1024**2
|
2017-11-11 22:00:22 +00:00
|
|
|
max_size = int(args.deviceinfo["flash_fastboot_max_size"])
|
|
|
|
if img_size > max_size:
|
2018-07-15 21:41:31 +00:00
|
|
|
raise RuntimeError("The rootfs is too large for fastboot to"
|
|
|
|
" flash.")
|
2017-11-11 22:00:22 +00:00
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
# Run the flasher
|
2018-03-30 01:11:20 +00:00
|
|
|
logging.info("(native) flash rootfs image")
|
|
|
|
pmb.flasher.run(args, "flash_rootfs")
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
|
2020-03-03 23:51:25 +00:00
|
|
|
def flash_vbmeta(args):
|
|
|
|
logging.info("(native) flash vbmeta.img with verity disabled flag")
|
|
|
|
pmb.flasher.run(args, "flash_vbmeta")
|
|
|
|
|
|
|
|
|
2021-08-27 16:35:46 +00:00
|
|
|
def flash_dtbo(args):
|
|
|
|
logging.info("(native) flash dtbo image")
|
|
|
|
pmb.flasher.run(args, "flash_dtbo")
|
|
|
|
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
def list_devices(args):
|
|
|
|
pmb.flasher.run(args, "list_devices")
|
|
|
|
|
|
|
|
|
2017-08-24 21:07:36 +00:00
|
|
|
def sideload(args):
|
2017-09-28 22:05:00 +00:00
|
|
|
# Install depends
|
2022-11-09 07:14:59 +00:00
|
|
|
pmb.flasher.install_depends(args)
|
2017-09-28 22:05:00 +00:00
|
|
|
|
2017-08-24 21:07:36 +00:00
|
|
|
# Mount the buildroot
|
|
|
|
suffix = "buildroot_" + args.deviceinfo["arch"]
|
|
|
|
mountpoint = "/mnt/" + suffix
|
|
|
|
pmb.helpers.mount.bind(args, args.work + "/chroot_" + suffix,
|
|
|
|
args.work + "/chroot_native/" + mountpoint)
|
|
|
|
|
|
|
|
# Missing recovery zip error
|
|
|
|
zip_path = ("/var/lib/postmarketos-android-recovery-installer/pmos-" +
|
|
|
|
args.device + ".zip")
|
|
|
|
if not os.path.exists(args.work + "/chroot_native" + mountpoint +
|
|
|
|
zip_path):
|
|
|
|
raise RuntimeError("The recovery zip has not been generated yet,"
|
|
|
|
" please run 'pmbootstrap install' with the"
|
|
|
|
" '--android-recovery-zip' parameter first!")
|
|
|
|
|
|
|
|
pmb.flasher.run(args, "sideload")
|
|
|
|
|
|
|
|
|
2021-08-03 19:37:59 +00:00
|
|
|
def flash_lk2nd(args):
|
2023-06-05 10:40:27 +00:00
|
|
|
method = args.flash_method or args.deviceinfo["flash_method"]
|
|
|
|
if method == "fastboot":
|
|
|
|
# In the future this could be expanded to use "fastboot flash lk2nd $img"
|
|
|
|
# which reflashes/updates lk2nd from itself. For now let the user handle this
|
|
|
|
# manually since supporting the codepath with heimdall requires more effort.
|
|
|
|
pmb.flasher.init(args)
|
|
|
|
logging.info("(native) checking current fastboot product")
|
|
|
|
output = pmb.chroot.root(args, ["fastboot", "getvar", "product"],
|
|
|
|
output="interactive", output_return=True)
|
|
|
|
# Variable "product" is e.g. "LK2ND_MSM8974" or "lk2nd-msm8226" depending
|
|
|
|
# on the lk2nd version.
|
|
|
|
if "lk2nd" in output.lower():
|
|
|
|
raise RuntimeError("You are currently running lk2nd. Please reboot into the regular"
|
|
|
|
" bootloader mode to re-flash lk2nd.")
|
|
|
|
|
|
|
|
# Get the lk2nd package (which is a dependency of the device package)
|
|
|
|
device_pkg = f"device-{args.device}"
|
|
|
|
apkbuild = pmb.helpers.pmaports.get(args, device_pkg)
|
|
|
|
lk2nd_pkg = None
|
|
|
|
for dep in apkbuild["depends"]:
|
|
|
|
if dep.startswith("lk2nd"):
|
|
|
|
lk2nd_pkg = dep
|
|
|
|
break
|
|
|
|
|
|
|
|
if not lk2nd_pkg:
|
|
|
|
raise RuntimeError(f"{device_pkg} does not depend on any lk2nd package")
|
|
|
|
|
|
|
|
suffix = "rootfs_" + args.device
|
|
|
|
pmb.chroot.apk.install(args, [lk2nd_pkg], suffix)
|
|
|
|
|
|
|
|
logging.info("(native) flash lk2nd image")
|
2021-08-03 19:37:59 +00:00
|
|
|
pmb.flasher.run(args, "flash_lk2nd")
|
|
|
|
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
def frontend(args):
|
|
|
|
action = args.action_flasher
|
2017-12-21 22:12:52 +00:00
|
|
|
method = args.flash_method or args.deviceinfo["flash_method"]
|
2017-11-25 02:57:58 +00:00
|
|
|
|
2021-08-03 19:37:59 +00:00
|
|
|
if method == "none" and action in ["boot", "flash_kernel", "flash_rootfs",
|
|
|
|
"flash_lk2nd"]:
|
2017-11-25 02:57:58 +00:00
|
|
|
logging.info("This device doesn't support any flash method.")
|
|
|
|
return
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
if action in ["boot", "flash_kernel"]:
|
|
|
|
kernel(args)
|
2023-05-13 12:06:24 +00:00
|
|
|
elif action == "flash_rootfs":
|
2018-03-30 01:11:20 +00:00
|
|
|
rootfs(args)
|
2023-05-13 12:06:24 +00:00
|
|
|
elif action == "flash_vbmeta":
|
2020-03-03 23:51:25 +00:00
|
|
|
flash_vbmeta(args)
|
2023-05-13 12:06:24 +00:00
|
|
|
elif action == "flash_dtbo":
|
2021-08-27 16:35:46 +00:00
|
|
|
flash_dtbo(args)
|
2023-06-05 10:40:27 +00:00
|
|
|
elif action == "flash_lk2nd":
|
|
|
|
flash_lk2nd(args)
|
2023-05-13 12:06:24 +00:00
|
|
|
elif action == "list_flavors":
|
2017-05-26 20:08:45 +00:00
|
|
|
list_flavors(args)
|
2023-05-13 12:06:24 +00:00
|
|
|
elif action == "list_devices":
|
2017-05-26 20:08:45 +00:00
|
|
|
list_devices(args)
|
2023-05-13 12:06:24 +00:00
|
|
|
elif action == "sideload":
|
2017-08-24 21:07:36 +00:00
|
|
|
sideload(args)
|