2021-08-05 20:26:48 +02:00
|
|
|
import os
|
|
|
|
import urllib.request
|
|
|
|
import click
|
2021-10-25 17:42:31 +02:00
|
|
|
|
2022-09-17 19:05:54 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-10-08 04:04:27 +02:00
|
|
|
from config.state import config
|
2022-09-27 03:53:01 +02:00
|
|
|
from constants import FLASH_PARTS, FASTBOOT, JUMPDRIVE, JUMPDRIVE_VERSION
|
2022-08-16 02:14:19 +02:00
|
|
|
from exec.file import makedir
|
2022-10-08 02:17:04 +02:00
|
|
|
from devices.device import get_profile_device
|
2022-10-08 03:25:50 +02:00
|
|
|
from flavours.flavour import get_profile_flavour
|
|
|
|
from flavours.cli import profile_option
|
2021-09-29 23:18:12 +02:00
|
|
|
from wrapper import enforce_wrap
|
2021-09-29 02:00:59 +02:00
|
|
|
|
2023-04-30 03:24:17 +02:00
|
|
|
from .fastboot import fastboot_boot, fastboot_erase
|
2022-10-16 23:25:47 +02:00
|
|
|
from .image import get_device_name, losetup_rootfs_image, get_image_path, dump_aboot, dump_lk2nd
|
|
|
|
|
2021-09-29 02:00:59 +02:00
|
|
|
LK2ND = FLASH_PARTS['LK2ND']
|
2022-02-06 20:36:11 +01:00
|
|
|
ABOOT = FLASH_PARTS['ABOOT']
|
2021-09-29 02:00:59 +02:00
|
|
|
|
2023-06-12 00:59:28 +02:00
|
|
|
BOOT_TYPES = [ABOOT, LK2ND, JUMPDRIVE]
|
2021-08-05 20:26:48 +02:00
|
|
|
|
|
|
|
|
2021-08-17 20:57:31 +02:00
|
|
|
@click.command(name='boot')
|
2022-09-17 19:05:54 +02:00
|
|
|
@profile_option
|
2023-04-30 03:24:17 +02:00
|
|
|
@click.argument('type', required=False, default=ABOOT, type=click.Choice(BOOT_TYPES))
|
2023-01-09 05:47:24 +01:00
|
|
|
@click.option('-b', '--sector-size', type=int, help="Override the device's sector size", default=None)
|
2023-04-30 03:24:17 +02:00
|
|
|
@click.option(
|
|
|
|
'--erase-dtbo/--no-erase-dtbo',
|
|
|
|
is_flag=True,
|
|
|
|
default=True,
|
|
|
|
show_default=True,
|
|
|
|
help="Erase the DTBO partition before flashing",
|
|
|
|
)
|
|
|
|
@click.option('--confirm', is_flag=True, help="Ask for confirmation before executing fastboot commands")
|
|
|
|
def cmd_boot(
|
|
|
|
type: str,
|
|
|
|
profile: Optional[str] = None,
|
|
|
|
sector_size: Optional[int] = None,
|
|
|
|
erase_dtbo: bool = True,
|
|
|
|
confirm: bool = False,
|
|
|
|
):
|
2022-02-13 19:57:04 +01:00
|
|
|
"""Boot JumpDrive or the Kupfer aboot image. Erases Android DTBO in the process."""
|
2021-09-29 23:18:12 +02:00
|
|
|
enforce_wrap()
|
2022-09-17 19:05:54 +02:00
|
|
|
device = get_profile_device(profile)
|
|
|
|
flavour = get_profile_flavour(profile).name
|
2022-09-04 04:47:13 +02:00
|
|
|
deviceinfo = device.parse_deviceinfo()
|
2023-04-30 03:24:17 +02:00
|
|
|
sector_size = sector_size or device.get_image_sectorsize_default()
|
2022-09-04 04:47:13 +02:00
|
|
|
if not sector_size:
|
2023-04-30 03:24:17 +02:00
|
|
|
raise Exception(f"Device {device.name} has no rootfs_image_sector_size specified")
|
2022-02-13 20:00:59 +01:00
|
|
|
image_path = get_image_path(device, flavour)
|
2022-09-27 03:53:01 +02:00
|
|
|
strategy = deviceinfo.flash_method
|
|
|
|
if not strategy:
|
|
|
|
raise Exception(f"Device {device.name} has no flash strategy defined")
|
2021-08-05 20:26:48 +02:00
|
|
|
|
|
|
|
if strategy == FASTBOOT:
|
|
|
|
if type == JUMPDRIVE:
|
2022-08-29 23:30:00 +02:00
|
|
|
file = f'boot-{get_device_name(device)}.img'
|
2021-10-25 17:42:31 +02:00
|
|
|
path = os.path.join(config.get_path('jumpdrive'), file)
|
2022-08-16 02:14:19 +02:00
|
|
|
makedir(os.path.dirname(path))
|
2021-08-17 20:57:31 +02:00
|
|
|
if not os.path.exists(path):
|
|
|
|
urllib.request.urlretrieve(f'https://github.com/dreemurrs-embedded/Jumpdrive/releases/download/{JUMPDRIVE_VERSION}/{file}', path)
|
2021-09-29 02:00:59 +02:00
|
|
|
else:
|
2022-02-13 20:00:59 +01:00
|
|
|
loop_device = losetup_rootfs_image(image_path, sector_size)
|
|
|
|
if type == LK2ND:
|
|
|
|
path = dump_lk2nd(loop_device + 'p1')
|
|
|
|
elif type == ABOOT:
|
|
|
|
path = dump_aboot(loop_device + 'p1')
|
|
|
|
else:
|
|
|
|
raise Exception(f'Unknown boot image type {type}')
|
2023-04-30 03:24:17 +02:00
|
|
|
if erase_dtbo:
|
|
|
|
fastboot_erase('dtbo', confirm=confirm)
|
|
|
|
fastboot_boot(path, confirm=confirm)
|
2022-09-27 03:53:01 +02:00
|
|
|
else:
|
2023-04-30 03:24:17 +02:00
|
|
|
raise Exception(f'Unsupported flash strategy "{strategy}" for device {device.name}')
|