pmbootstrap/pmb/flasher/frontend.py

157 lines
5.2 KiB
Python
Raw Normal View History

2022-01-02 21:38:21 +00:00
# Copyright 2022 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
2017-05-26 20:08:45 +00:00
import logging
import os
import pmb.config
2017-05-26 20:08:45 +00:00
import pmb.flasher
import pmb.install
import pmb.chroot.apk
import pmb.chroot.initfs
2017-05-26 20:08:45 +00:00
import pmb.chroot.other
import pmb.helpers.frontend
import pmb.parse.kconfig
2017-05-26 20:08:45 +00:00
def kernel(args):
# Rebuild the initramfs, just to make sure (see #69)
flavor = pmb.helpers.frontend._parse_flavor(args, args.autoinstall)
if args.autoinstall:
pmb.chroot.initfs.build(args, flavor, "rootfs_" + args.device)
# 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")
pmb.flasher.run(args, "boot", flavor)
2017-05-26 20:08:45 +00:00
else:
logging.info("(native) flash kernel " + flavor)
pmb.flasher.run(args, "flash_kernel", flavor)
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:")
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:")
logging.info("* " + pmb.chroot.other.kernel_flavor_installed(args, suffix))
2017-05-26 20:08:45 +00:00
def rootfs(args):
add "fastboot-bootpart" flasher to flash split images with fastboot (!1871) asus-me176c has a Fastboot interface that can be used for flashing, but in postmarketOS we do not use Android boot images for it. This is because is it not very practical - the boot partition is quite small and there is a (custom) EFI bootloader that can boot directly from any other FAT32 partition. At the moment the installation process is manual: 1. pmbootstrap install --split to have separated boot (FAT32) and rootfs images 2. pmbootstrap export 3. Flash boot and rootfs images manually using Fastboot The "fastboot-bootpart" flasher implements that process in a more convenient way. When a device uses the "fastboot-bootpart" flasher: - We generate --split images on "pmbootstrap install" by default. (This can be disabled using --no-split instead.) - pmbootstrap flasher flash_kernel flashes the raw boot partition (not an Android boot image) using Fastboot, just like the rootfs. There are some limitations that could be improved in the future: - "fastboot-bootpart" is not offered in the device wizard. I think it is special enough that no-one will be starting with it, and the difference to normal "fastboot" might be confusing. - Support "pmbootstrap flasher boot". asus-me176c does not support "fastboot boot" properly, but theoretically we could still generate Android boot images to use when booting an image directly. - At the moment the boot partition image is not regenerated when using "pmbootstrap flasher flash_kernel" (unlike when using Android boot images). "pmbootstrap install" needs to be run manually first.
2020-02-04 10:30:28 +00:00
method = args.flash_method or args.deviceinfo["flash_method"]
# Generate rootfs, install flasher
add "fastboot-bootpart" flasher to flash split images with fastboot (!1871) asus-me176c has a Fastboot interface that can be used for flashing, but in postmarketOS we do not use Android boot images for it. This is because is it not very practical - the boot partition is quite small and there is a (custom) EFI bootloader that can boot directly from any other FAT32 partition. At the moment the installation process is manual: 1. pmbootstrap install --split to have separated boot (FAT32) and rootfs images 2. pmbootstrap export 3. Flash boot and rootfs images manually using Fastboot The "fastboot-bootpart" flasher implements that process in a more convenient way. When a device uses the "fastboot-bootpart" flasher: - We generate --split images on "pmbootstrap install" by default. (This can be disabled using --no-split instead.) - pmbootstrap flasher flash_kernel flashes the raw boot partition (not an Android boot image) using Fastboot, just like the rootfs. There are some limitations that could be improved in the future: - "fastboot-bootpart" is not offered in the device wizard. I think it is special enough that no-one will be starting with it, and the difference to normal "fastboot" might be confusing. - Support "pmbootstrap flasher boot". asus-me176c does not support "fastboot boot" properly, but theoretically we could still generate Android boot images to use when booting an image directly. - At the moment the boot partition image is not regenerated when using "pmbootstrap flasher flash_kernel" (unlike when using Android boot images). "pmbootstrap install" needs to be run manually first.
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}"
add "fastboot-bootpart" flasher to flash split images with fastboot (!1871) asus-me176c has a Fastboot interface that can be used for flashing, but in postmarketOS we do not use Android boot images for it. This is because is it not very practical - the boot partition is quite small and there is a (custom) EFI bootloader that can boot directly from any other FAT32 partition. At the moment the installation process is manual: 1. pmbootstrap install --split to have separated boot (FAT32) and rootfs images 2. pmbootstrap export 3. Flash boot and rootfs images manually using Fastboot The "fastboot-bootpart" flasher implements that process in a more convenient way. When a device uses the "fastboot-bootpart" flasher: - We generate --split images on "pmbootstrap install" by default. (This can be disabled using --no-split instead.) - pmbootstrap flasher flash_kernel flashes the raw boot partition (not an Android boot image) using Fastboot, just like the rootfs. There are some limitations that could be improved in the future: - "fastboot-bootpart" is not offered in the device wizard. I think it is special enough that no-one will be starting with it, and the difference to normal "fastboot" might be confusing. - Support "pmbootstrap flasher boot". asus-me176c does not support "fastboot boot" properly, but theoretically we could still generate Android boot images to use when booting an image directly. - At the moment the boot partition image is not regenerated when using "pmbootstrap flasher flash_kernel" (unlike when using Android boot images). "pmbootstrap install" needs to be run manually first.
2020-02-04 10:30:28 +00:00
if not os.path.exists(img_path):
raise RuntimeError("The rootfs has not been generated yet, please run"
" 'pmbootstrap install' first.")
2017-05-26 20:08:45 +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"]:
add "fastboot-bootpart" flasher to flash split images with fastboot (!1871) asus-me176c has a Fastboot interface that can be used for flashing, but in postmarketOS we do not use Android boot images for it. This is because is it not very practical - the boot partition is quite small and there is a (custom) EFI bootloader that can boot directly from any other FAT32 partition. At the moment the installation process is manual: 1. pmbootstrap install --split to have separated boot (FAT32) and rootfs images 2. pmbootstrap export 3. Flash boot and rootfs images manually using Fastboot The "fastboot-bootpart" flasher implements that process in a more convenient way. When a device uses the "fastboot-bootpart" flasher: - We generate --split images on "pmbootstrap install" by default. (This can be disabled using --no-split instead.) - pmbootstrap flasher flash_kernel flashes the raw boot partition (not an Android boot image) using Fastboot, just like the rootfs. There are some limitations that could be improved in the future: - "fastboot-bootpart" is not offered in the device wizard. I think it is special enough that no-one will be starting with it, and the difference to normal "fastboot" might be confusing. - Support "pmbootstrap flasher boot". asus-me176c does not support "fastboot boot" properly, but theoretically we could still generate Android boot images to use when booting an image directly. - At the moment the boot partition image is not regenerated when using "pmbootstrap flasher flash_kernel" (unlike when using Android boot images). "pmbootstrap install" needs to be run manually first.
2020-02-04 10:30:28 +00:00
img_size = os.path.getsize(img_path) / 1024**2
max_size = int(args.deviceinfo["flash_fastboot_max_size"])
if img_size > max_size:
raise RuntimeError("The rootfs is too large for fastboot to"
" flash.")
2017-05-26 20:08:45 +00:00
# Run the flasher
logging.info("(native) flash rootfs image")
pmb.flasher.run(args, "flash_rootfs")
2017-05-26 20:08:45 +00:00
def flash_vbmeta(args):
logging.info("(native) flash vbmeta.img with verity disabled flag")
pmb.flasher.run(args, "flash_vbmeta")
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")
def sideload(args):
method = args.flash_method or args.deviceinfo["flash_method"]
cfg = pmb.config.flashers[method]
# Install depends
pmb.chroot.apk.install(args, cfg["depends"])
# 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")
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
method = args.flash_method or args.deviceinfo["flash_method"]
# Legacy alias
if action == "flash_system":
action = "flash_rootfs"
if method == "none" and action in ["boot", "flash_kernel", "flash_rootfs",
"flash_lk2nd"]:
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)
if action == "flash_rootfs":
rootfs(args)
if action == "flash_vbmeta":
flash_vbmeta(args)
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)
if action == "sideload":
sideload(args)
if action in ["flash_lk2nd"]:
flash_lk2nd(args)