2022-01-02 21:38:21 +00:00
|
|
|
# Copyright 2022 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
|
|
|
|
pmb.parse.kconfig.check(args, flavor)
|
|
|
|
|
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"
|
|
|
|
" sure that osk-sdl has been properly configured for your"
|
|
|
|
" 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-12-21 22:12:52 +00:00
|
|
|
method = args.flash_method or args.deviceinfo["flash_method"]
|
2017-09-28 22:05:00 +00:00
|
|
|
cfg = pmb.config.flashers[method]
|
|
|
|
|
|
|
|
# Install depends
|
|
|
|
pmb.chroot.apk.install(args, cfg["depends"])
|
|
|
|
|
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):
|
|
|
|
chroot_path = args.work + "/chroot_rootfs_" + args.device
|
|
|
|
lk2nd_path = "/boot/lk2nd.img"
|
|
|
|
if not os.path.exists(chroot_path + lk2nd_path):
|
|
|
|
raise RuntimeError(f"{chroot_path+lk2nd_path} doesn't exist. Your"
|
|
|
|
" device may not support lk2nd.")
|
|
|
|
|
|
|
|
logging.info(lk2nd_path)
|
|
|
|
logging.info("It's normal if fastboot warns"
|
|
|
|
" \"Image not signed or corrupt\" or a similar warning")
|
|
|
|
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
|
|
|
|
2018-03-30 01:11:20 +00:00
|
|
|
# Legacy alias
|
|
|
|
if action == "flash_system":
|
|
|
|
action = "flash_rootfs"
|
|
|
|
|
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)
|
2018-03-30 01:11:20 +00:00
|
|
|
if action == "flash_rootfs":
|
|
|
|
rootfs(args)
|
2020-03-03 23:51:25 +00:00
|
|
|
if action == "flash_vbmeta":
|
|
|
|
flash_vbmeta(args)
|
2021-08-27 16:35:46 +00:00
|
|
|
if action == "flash_dtbo":
|
|
|
|
flash_dtbo(args)
|
2017-05-26 20:08:45 +00:00
|
|
|
if action == "list_flavors":
|
|
|
|
list_flavors(args)
|
|
|
|
if action == "list_devices":
|
|
|
|
list_devices(args)
|
2017-08-24 21:07:36 +00:00
|
|
|
if action == "sideload":
|
|
|
|
sideload(args)
|
2021-08-03 19:37:59 +00:00
|
|
|
if action in ["flash_lk2nd"]:
|
|
|
|
flash_lk2nd(args)
|