From 4369df96735898702751f853e0fefdefbe9ec937 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 17 Aug 2021 20:57:31 +0200 Subject: [PATCH] Restructure building images, flashing and booting --- boot.py | 40 +++++++++--------------------- constants.py | 34 +++++++++++++++++++++++++ fastboot.py | 36 +++++++++++++++++++++++++++ flash.py | 59 +++++++++---------------------------------- image.py | 70 ++++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 146 insertions(+), 93 deletions(-) create mode 100644 constants.py create mode 100644 fastboot.py diff --git a/boot.py b/boot.py index 73b800e..8333f78 100644 --- a/boot.py +++ b/boot.py @@ -1,24 +1,13 @@ import os import urllib.request -from image import get_device_and_flavour, get_image_name -from logger import logging, setup_logging, verbose_option -from flash import dump_bootimg, erase_dtbo +from image import get_device_and_flavour, get_image_name, dump_bootimg, dump_lk2nd +from logger import setup_logging, verbose_option +from fastboot import fastboot_boot, fastboot_erase_dtbo +from constants import BOOT_STRATEGIES, FASTBOOT, JUMPDRIVE, LK2ND, JUMPDRIVE_VERSION import click -import subprocess - -FASTBOOT = 'fastboot' - -JUMPDRIVE = 'jumpdrive' -jumpdrive_version = '0.8' - -boot_strategies = { - 'oneplus-enchilada': FASTBOOT, - 'xiaomi-beryllium-ebbg': FASTBOOT, - 'xiaomi-beryllium-tianma': FASTBOOT, -} -@click.command(name='boot', help=f'Leave TYPE empty or choose \'{JUMPDRIVE}\'') +@click.command(name='boot') @verbose_option @click.argument('type', required=False) def cmd_boot(verbose, type): @@ -26,23 +15,18 @@ def cmd_boot(verbose, type): device, flavour = get_device_and_flavour() image_name = get_image_name(device, flavour) - strategy = boot_strategies[device] + strategy = BOOT_STRATEGIES[device] if strategy == FASTBOOT: if type == JUMPDRIVE: file = f'boot-{device}.img' path = os.path.join('/var/cache/jumpdrive', file) - urllib.request.urlretrieve(f'https://github.com/dreemurrs-embedded/Jumpdrive/releases/download/{jumpdrive_version}/{file}', path) + if not os.path.exists(path): + urllib.request.urlretrieve(f'https://github.com/dreemurrs-embedded/Jumpdrive/releases/download/{JUMPDRIVE_VERSION}/{file}', path) + elif type == LK2ND: + path = dump_lk2nd(image_name) else: path = dump_bootimg(image_name) - erase_dtbo() - - result = subprocess.run([ - 'fastboot', - 'boot', - path, - ]) - if result.returncode != 0: - logging.fatal(f'Failed to boot {path} using fastboot') - exit(1) + fastboot_erase_dtbo() + fastboot_boot(path) diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..4806dd1 --- /dev/null +++ b/constants.py @@ -0,0 +1,34 @@ +FASTBOOT = 'fastboot' + +ROOTFS = 'rootfs' +BOOTIMG = 'bootimg' +LK2ND = 'lk2nd' +QHYPSTUB = 'qhypstub' + +EMMC = 'emmc' +EMMCFILE = 'emmc-file' +MICROSD = 'microsd' +LOCATIONS = [EMMC, EMMCFILE, MICROSD] + +JUMPDRIVE = 'jumpdrive' +JUMPDRIVE_VERSION = '0.8' + +BOOT_STRATEGIES = { + 'oneplus-enchilada': FASTBOOT, + 'xiaomi-beryllium-ebbg': FASTBOOT, + 'xiaomi-beryllium-tianma': FASTBOOT, + 'bq-paella': FASTBOOT, +} + +DEVICES = { + 'oneplus-enchilada': ['sdm845-oneplus-enchilada'], + 'xiaomi-beryllium-ebbg': ['sdm845-xiaomi-beryllium-ebbg'], + 'xiaomi-beryllium-tianma': ['sdm845-xiaomi-beryllium-tianma'], + 'bq-paella': ['msm8916-bq-paella'], +} + +FLAVOURS = { + 'barebone': [], + 'phosh': [], + 'plasma-mobile': [], +} diff --git a/fastboot.py b/fastboot.py new file mode 100644 index 0000000..9eac4de --- /dev/null +++ b/fastboot.py @@ -0,0 +1,36 @@ +import logging +import subprocess + + +def fastboot_erase_dtbo(): + subprocess.run( + [ + 'fastboot', + 'erase', + 'dtbo', + ], + capture_output=True, + ) + + +def fastboot_flash(partition, file): + result = subprocess.run([ + 'fastboot', + 'flash', + partition, + file, + ]) + if result.returncode != 0: + logging.info(f'Failed to flash {file}') + exit(1) + + +def fastboot_boot(file): + result = subprocess.run([ + 'fastboot', + 'boot', + file, + ]) + if result.returncode != 0: + logging.fatal(f'Failed to boot {file} using fastboot') + exit(1) diff --git a/flash.py b/flash.py index e20b5d4..d393087 100644 --- a/flash.py +++ b/flash.py @@ -1,47 +1,14 @@ import atexit -from cmath import log -from email.mime import image +from constants import BOOTIMG, LK2ND, LOCATIONS, QHYPSTUB, ROOTFS +from fastboot import fastboot_flash import shutil -from image import get_device_and_flavour, get_image_name +from image import dump_bootimg, dump_lk2nd, dump_qhypstub, get_device_and_flavour, get_image_name import os import subprocess import click import tempfile from logger import logging, setup_logging, verbose_option -ROOTFS = 'rootfs' -BOOTIMG = 'bootimg' - -EMMC = 'emmc' -EMMCFILE = 'emmc-file' -MICROSD = 'microsd' -locations = [EMMC, EMMCFILE, MICROSD] - - -def dump_bootimg(image_name: str) -> str: - path = '/tmp/boot.img' - result = subprocess.run([ - 'debugfs', - image_name, - '-R', - f'dump /boot/boot.img {path}', - ]) - if result.returncode != 0: - logging.fatal(f'Faild to dump boot.img') - exit(1) - return path - - -def erase_dtbo(): - result = subprocess.run([ - 'fastboot', - 'erase', - 'dtbo', - ]) - if result.returncode != 0: - logging.info(f'Failed to erase dtbo') - exit(1) - @click.command(name='flash') @verbose_option @@ -57,8 +24,8 @@ def cmd_flash(verbose, what, location): if location == None: logging.info(f'You need to specify a location to flash {what} to') exit(1) - if location not in locations: - logging.info(f'Invalid location {location}. Choose one of {", ".join(locations)} for location') + if location not in LOCATIONS: + logging.info(f'Invalid location {location}. Choose one of {", ".join(LOCATIONS)} for location') exit(1) path = '' @@ -168,15 +135,13 @@ def cmd_flash(verbose, what, location): elif what == BOOTIMG: path = dump_bootimg(image_name) - result = subprocess.run([ - 'fastboot', - 'flash', - 'boot', - path, - ]) - if result.returncode != 0: - logging.info(f'Failed to flash boot.img') - exit(1) + fastboot_flash('boot', path) + elif what == LK2ND: + path = dump_lk2nd(image_name) + fastboot_flash('lk2nd', path) + elif what == QHYPSTUB: + path = dump_qhypstub(image_name) + fastboot_flash('qhypstub', path) else: logging.fatal(f'Unknown what {what}') exit(1) diff --git a/image.py b/image.py index c90f48e..f1f6320 100644 --- a/image.py +++ b/image.py @@ -4,18 +4,7 @@ import subprocess import click from logger import logging, setup_logging, verbose_option from chroot import create_chroot, create_chroot_user - -devices = { - 'oneplus-enchilada': ['sdm845-oneplus-enchilada'], - 'xiaomi-beryllium-ebbg': ['sdm845-xiaomi-beryllium-ebbg'], - 'xiaomi-beryllium-tianma': ['sdm845-xiaomi-beryllium-tianma'], -} - -flavours = { - 'barebone': [], - 'phosh': [], - 'plasma-mobile': [], -} +from constants import DEVICES, FLAVOURS def get_device_and_flavour() -> tuple[str, str]: @@ -69,6 +58,51 @@ def mount_rootfs_image(path): return rootfs_mount +def dump_bootimg(image_name: str) -> str: + path = '/tmp/boot.img' + result = subprocess.run([ + 'debugfs', + image_name, + '-R', + f'dump /boot/boot.img {path}', + ]) + if result.returncode != 0: + logging.fatal(f'Faild to dump boot.img') + exit(1) + return path + + +def dump_lk2nd(image_name: str) -> str: + """ + This doesn't append the image with the appended DTB which is needed for some devices, so it should get added in the future. + """ + path = '/tmp/lk2nd.img' + result = subprocess.run([ + 'debugfs', + image_name, + '-R', + f'dump /boot/lk2nd.img {path}', + ]) + if result.returncode != 0: + logging.fatal(f'Faild to dump lk2nd.img') + exit(1) + return path + + +def dump_qhypstub(image_name: str) -> str: + path = '/tmp/qhypstub.bin' + result = subprocess.run([ + 'debugfs', + image_name, + '-R', + f'dump /boot/qhypstub.bin {path}', + ]) + if result.returncode != 0: + logging.fatal(f'Faild to dump qhypstub.bin') + exit(1) + return path + + @click.group(name='image') def cmd_image(): pass @@ -80,13 +114,13 @@ def cmd_image(): def cmd_device(verbose, device): setup_logging(verbose) - for key in devices.keys(): + for key in DEVICES.keys(): if '-'.join(key.split('-')[1:]) == device: device = key break - if device not in devices: - logging.fatal(f'Unknown device {device}. Pick one from:\n{", ".join(devices.keys())}') + if device not in DEVICES: + logging.fatal(f'Unknown device {device}. Pick one from:\n{", ".join(DEVICES.keys())}') exit(1) logging.info(f'Setting device to {device}') @@ -101,8 +135,8 @@ def cmd_device(verbose, device): def cmd_flavour(verbose, flavour): setup_logging(verbose) - if flavour not in flavours: - logging.fatal(f'Unknown flavour {flavour}. Pick one from:\n{", ".join(flavours.keys())}') + if flavour not in FLAVOURS: + logging.fatal(f'Unknown flavour {flavour}. Pick one from:\n{", ".join(FLAVOURS.keys())}') exit(1) logging.info(f'Setting flavour to {flavour}') @@ -163,7 +197,7 @@ def cmd_build(verbose): create_chroot( rootfs_mount, - packages=(['base', 'base-kupfer'] + devices[device] + flavours[flavour]), + packages=['base', 'base-kupfer'] + DEVICES[device] + FLAVOURS[flavour], pacman_conf='/app/local/etc/pacman.conf', extra_repos=extra_repos, )