Restructure building images, flashing and booting

This commit is contained in:
jld3103 2021-08-17 20:57:31 +02:00
parent 4df7e93655
commit 4369df9673
5 changed files with 146 additions and 93 deletions

40
boot.py
View file

@ -1,24 +1,13 @@
import os import os
import urllib.request import urllib.request
from image import get_device_and_flavour, get_image_name from image import get_device_and_flavour, get_image_name, dump_bootimg, dump_lk2nd
from logger import logging, setup_logging, verbose_option from logger import setup_logging, verbose_option
from flash import dump_bootimg, erase_dtbo from fastboot import fastboot_boot, fastboot_erase_dtbo
from constants import BOOT_STRATEGIES, FASTBOOT, JUMPDRIVE, LK2ND, JUMPDRIVE_VERSION
import click 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 @verbose_option
@click.argument('type', required=False) @click.argument('type', required=False)
def cmd_boot(verbose, type): def cmd_boot(verbose, type):
@ -26,23 +15,18 @@ def cmd_boot(verbose, type):
device, flavour = get_device_and_flavour() device, flavour = get_device_and_flavour()
image_name = get_image_name(device, flavour) image_name = get_image_name(device, flavour)
strategy = boot_strategies[device] strategy = BOOT_STRATEGIES[device]
if strategy == FASTBOOT: if strategy == FASTBOOT:
if type == JUMPDRIVE: if type == JUMPDRIVE:
file = f'boot-{device}.img' file = f'boot-{device}.img'
path = os.path.join('/var/cache/jumpdrive', file) 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: else:
path = dump_bootimg(image_name) path = dump_bootimg(image_name)
erase_dtbo() fastboot_erase_dtbo()
fastboot_boot(path)
result = subprocess.run([
'fastboot',
'boot',
path,
])
if result.returncode != 0:
logging.fatal(f'Failed to boot {path} using fastboot')
exit(1)

34
constants.py Normal file
View file

@ -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': [],
}

36
fastboot.py Normal file
View file

@ -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)

View file

@ -1,47 +1,14 @@
import atexit import atexit
from cmath import log from constants import BOOTIMG, LK2ND, LOCATIONS, QHYPSTUB, ROOTFS
from email.mime import image from fastboot import fastboot_flash
import shutil 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 os
import subprocess import subprocess
import click import click
import tempfile import tempfile
from logger import logging, setup_logging, verbose_option 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') @click.command(name='flash')
@verbose_option @verbose_option
@ -57,8 +24,8 @@ def cmd_flash(verbose, what, location):
if location == None: if location == None:
logging.info(f'You need to specify a location to flash {what} to') logging.info(f'You need to specify a location to flash {what} to')
exit(1) exit(1)
if location not in locations: if location not in LOCATIONS:
logging.info(f'Invalid location {location}. Choose one of {", ".join(locations)} for location') logging.info(f'Invalid location {location}. Choose one of {", ".join(LOCATIONS)} for location')
exit(1) exit(1)
path = '' path = ''
@ -168,15 +135,13 @@ def cmd_flash(verbose, what, location):
elif what == BOOTIMG: elif what == BOOTIMG:
path = dump_bootimg(image_name) path = dump_bootimg(image_name)
result = subprocess.run([ fastboot_flash('boot', path)
'fastboot', elif what == LK2ND:
'flash', path = dump_lk2nd(image_name)
'boot', fastboot_flash('lk2nd', path)
path, elif what == QHYPSTUB:
]) path = dump_qhypstub(image_name)
if result.returncode != 0: fastboot_flash('qhypstub', path)
logging.info(f'Failed to flash boot.img')
exit(1)
else: else:
logging.fatal(f'Unknown what {what}') logging.fatal(f'Unknown what {what}')
exit(1) exit(1)

View file

@ -4,18 +4,7 @@ import subprocess
import click import click
from logger import logging, setup_logging, verbose_option from logger import logging, setup_logging, verbose_option
from chroot import create_chroot, create_chroot_user from chroot import create_chroot, create_chroot_user
from constants import DEVICES, FLAVOURS
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': [],
}
def get_device_and_flavour() -> tuple[str, str]: def get_device_and_flavour() -> tuple[str, str]:
@ -69,6 +58,51 @@ def mount_rootfs_image(path):
return rootfs_mount 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') @click.group(name='image')
def cmd_image(): def cmd_image():
pass pass
@ -80,13 +114,13 @@ def cmd_image():
def cmd_device(verbose, device): def cmd_device(verbose, device):
setup_logging(verbose) setup_logging(verbose)
for key in devices.keys(): for key in DEVICES.keys():
if '-'.join(key.split('-')[1:]) == device: if '-'.join(key.split('-')[1:]) == device:
device = key device = key
break break
if device not in devices: if device not in DEVICES:
logging.fatal(f'Unknown device {device}. Pick one from:\n{", ".join(devices.keys())}') logging.fatal(f'Unknown device {device}. Pick one from:\n{", ".join(DEVICES.keys())}')
exit(1) exit(1)
logging.info(f'Setting device to {device}') logging.info(f'Setting device to {device}')
@ -101,8 +135,8 @@ def cmd_device(verbose, device):
def cmd_flavour(verbose, flavour): def cmd_flavour(verbose, flavour):
setup_logging(verbose) setup_logging(verbose)
if flavour not in flavours: if flavour not in FLAVOURS:
logging.fatal(f'Unknown flavour {flavour}. Pick one from:\n{", ".join(flavours.keys())}') logging.fatal(f'Unknown flavour {flavour}. Pick one from:\n{", ".join(FLAVOURS.keys())}')
exit(1) exit(1)
logging.info(f'Setting flavour to {flavour}') logging.info(f'Setting flavour to {flavour}')
@ -163,7 +197,7 @@ def cmd_build(verbose):
create_chroot( create_chroot(
rootfs_mount, 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', pacman_conf='/app/local/etc/pacman.conf',
extra_repos=extra_repos, extra_repos=extra_repos,
) )