2021-08-14 13:31:04 +02:00
|
|
|
import shutil
|
2021-08-05 20:26:48 +02:00
|
|
|
import os
|
|
|
|
import click
|
2023-04-30 16:58:33 +02:00
|
|
|
import logging
|
2021-10-25 21:01:09 +02:00
|
|
|
|
2022-09-17 19:05:54 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2023-01-06 04:38:05 +01:00
|
|
|
from constants import FLASH_PARTS, LOCATIONS, FASTBOOT, JUMPDRIVE
|
2022-08-15 17:41:23 +02:00
|
|
|
from exec.cmd import run_root_cmd
|
2022-08-16 16:38:30 +02:00
|
|
|
from exec.file import get_temp_dir
|
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
|
|
|
|
2022-10-16 23:25:47 +02:00
|
|
|
from .fastboot import fastboot_flash
|
2023-01-06 04:38:05 +01:00
|
|
|
from .image import dd_image, dump_aboot, dump_lk2nd, dump_qhypstub, get_image_path, losetup_destroy, losetup_rootfs_image, partprobe, shrink_fs
|
2022-10-16 23:25:47 +02:00
|
|
|
|
2022-02-06 20:36:11 +01:00
|
|
|
ABOOT = FLASH_PARTS['ABOOT']
|
2021-09-29 02:00:59 +02:00
|
|
|
LK2ND = FLASH_PARTS['LK2ND']
|
|
|
|
QHYPSTUB = FLASH_PARTS['QHYPSTUB']
|
2023-06-12 01:10:54 +02:00
|
|
|
FULL_IMG = FLASH_PARTS['FULL']
|
2021-08-05 20:26:48 +02:00
|
|
|
|
2023-01-06 04:38:05 +01:00
|
|
|
DD = 'dd'
|
|
|
|
|
|
|
|
FLASH_METHODS = [FASTBOOT, JUMPDRIVE, DD]
|
|
|
|
|
|
|
|
|
|
|
|
def find_jumpdrive(location: str) -> str:
|
|
|
|
if location not in LOCATIONS:
|
|
|
|
raise Exception(f'Invalid location {location}. Choose one of {", ".join(LOCATIONS)}')
|
|
|
|
dir = '/dev/disk/by-id'
|
|
|
|
for file in os.listdir(dir):
|
|
|
|
sanitized_file = file.replace('-', '').replace('_', '').lower()
|
|
|
|
if f'jumpdrive{location.split("-")[0]}' in sanitized_file:
|
|
|
|
return os.path.realpath(os.path.join(dir, file))
|
|
|
|
raise Exception('Unable to discover Jumpdrive')
|
|
|
|
|
|
|
|
|
|
|
|
def test_blockdev(path: str):
|
|
|
|
partprobe(path)
|
|
|
|
result = run_root_cmd(['lsblk', path, '-o', 'SIZE'], capture_output=True)
|
|
|
|
if result.returncode != 0:
|
|
|
|
raise Exception(f'Failed to lsblk {path}')
|
|
|
|
if result.stdout == b'SIZE\n 0B\n':
|
|
|
|
raise Exception(f'Disk {path} has a size of 0B. That probably means it is not available (e.g. no'
|
|
|
|
'microSD inserted or no microSD card slot installed in the device) or corrupt or defect')
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_minimal_image(source_path: str, sector_size: int) -> str:
|
|
|
|
minimal_image_dir = get_temp_dir(register_cleanup=True)
|
|
|
|
minimal_image_path = os.path.join(minimal_image_dir, f'minimal-{os.path.basename(source_path)}')
|
2023-04-30 16:58:33 +02:00
|
|
|
logging.info(f"Copying image {os.path.basename(source_path)} to {minimal_image_dir} for shrinking")
|
2023-01-06 04:38:05 +01:00
|
|
|
shutil.copyfile(source_path, minimal_image_path)
|
|
|
|
|
|
|
|
loop_device = losetup_rootfs_image(minimal_image_path, sector_size)
|
|
|
|
partprobe(loop_device)
|
|
|
|
shrink_fs(loop_device, minimal_image_path, sector_size)
|
|
|
|
losetup_destroy(loop_device)
|
|
|
|
return minimal_image_path
|
|
|
|
|
2021-08-14 13:31:04 +02:00
|
|
|
|
2021-08-05 20:26:48 +02:00
|
|
|
@click.command(name='flash')
|
2022-09-17 19:05:54 +02:00
|
|
|
@profile_option
|
2023-01-06 04:38:05 +01:00
|
|
|
@click.option('-m', '--method', type=click.Choice(FLASH_METHODS))
|
|
|
|
@click.option('--split-size', help='Chunk size when splitting the image into sparse files via fastboot')
|
2023-04-30 03:24:17 +02:00
|
|
|
@click.option('--shrink/--no-shrink', is_flag=True, default=True, help="Copy and shrink the image file to minimal size")
|
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('--confirm', is_flag=True, help="Ask for confirmation before executing fastboot commands")
|
2021-10-25 21:01:09 +02:00
|
|
|
@click.argument('what', type=click.Choice(list(FLASH_PARTS.values())))
|
2022-05-08 17:27:58 +02:00
|
|
|
@click.argument('location', type=str, required=False)
|
2023-01-06 04:38:05 +01:00
|
|
|
def cmd_flash(
|
|
|
|
what: str,
|
|
|
|
location: str,
|
|
|
|
method: Optional[str] = None,
|
|
|
|
split_size: Optional[str] = None,
|
|
|
|
profile: Optional[str] = None,
|
|
|
|
shrink: bool = True,
|
2023-01-09 05:47:24 +01:00
|
|
|
sector_size: Optional[int] = None,
|
2023-04-30 03:24:17 +02:00
|
|
|
confirm: bool = False,
|
2023-01-06 04:38:05 +01:00
|
|
|
):
|
2023-06-12 00:59:28 +02:00
|
|
|
"""
|
|
|
|
Flash a partition onto a device.
|
|
|
|
|
2023-06-12 01:10:54 +02:00
|
|
|
The syntax of LOCATION depends on the flashing method and is usually only required for flashing "full":
|
2023-06-12 00:59:28 +02:00
|
|
|
|
|
|
|
\b
|
|
|
|
- fastboot: the regular fastboot partition identifier. Usually "userdata"
|
|
|
|
- dd: a path to a block device
|
|
|
|
- jumpdrive: one of "emmc", "sdcard" or a path to a block device
|
|
|
|
"""
|
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-02-06 23:37:40 +01:00
|
|
|
device_image_path = get_image_path(device, flavour)
|
2021-08-05 20:26:48 +02:00
|
|
|
|
2022-09-04 02:55:39 +02:00
|
|
|
deviceinfo = device.parse_deviceinfo()
|
2023-04-30 03:29:52 +02:00
|
|
|
sector_size = sector_size or device.get_image_sectorsize_default()
|
2023-01-06 04:38:05 +01:00
|
|
|
method = method or deviceinfo.flash_method
|
2021-10-22 17:07:05 +02:00
|
|
|
|
2021-09-29 02:00:59 +02:00
|
|
|
if what not in FLASH_PARTS.values():
|
|
|
|
raise Exception(f'Unknown what "{what}", must be one of {", ".join(FLASH_PARTS.values())}')
|
|
|
|
|
2023-04-30 03:24:17 +02:00
|
|
|
if location and location.startswith('aboot'):
|
2023-06-12 00:59:28 +02:00
|
|
|
raise Exception("You're trying to flash something "
|
|
|
|
f"to your aboot partition ({location!r}), "
|
2023-04-30 03:24:17 +02:00
|
|
|
"which contains the android bootloader itself.\n"
|
|
|
|
"This will brick your phone and is not what you want.\n"
|
2023-06-12 00:59:28 +02:00
|
|
|
'Aborting.\nDid you mean to flash to "boot"?')
|
2023-04-30 03:24:17 +02:00
|
|
|
|
2023-06-12 01:10:54 +02:00
|
|
|
if what == FULL_IMG:
|
2023-01-06 04:38:05 +01:00
|
|
|
path = ''
|
|
|
|
if method not in FLASH_METHODS:
|
|
|
|
raise Exception(f"Flash method {method} not supported!")
|
|
|
|
if not location:
|
2021-09-29 02:00:59 +02:00
|
|
|
raise Exception(f'You need to specify a location to flash {what} to')
|
2021-08-14 13:31:04 +02:00
|
|
|
path = ''
|
2023-01-06 04:38:05 +01:00
|
|
|
image_path = prepare_minimal_image(device_image_path, sector_size) if shrink else device_image_path
|
|
|
|
if method == FASTBOOT:
|
|
|
|
fastboot_flash(
|
|
|
|
partition=location,
|
|
|
|
file=image_path,
|
2023-04-26 13:26:16 +02:00
|
|
|
sparse_size=split_size if split_size is not None else '100M',
|
2023-04-30 03:24:17 +02:00
|
|
|
confirm=confirm,
|
2023-01-06 04:38:05 +01:00
|
|
|
)
|
|
|
|
elif method in [JUMPDRIVE, DD]:
|
|
|
|
if method == DD or location.startswith("/") or (location not in LOCATIONS and os.path.exists(location)):
|
|
|
|
path = location
|
|
|
|
elif method == JUMPDRIVE:
|
|
|
|
path = find_jumpdrive(location)
|
|
|
|
test_blockdev(path)
|
|
|
|
if dd_image(input=image_path, output=path).returncode != 0:
|
|
|
|
raise Exception(f'Failed to flash {image_path} to {path}')
|
2021-10-22 17:07:05 +02:00
|
|
|
else:
|
2023-01-06 04:38:05 +01:00
|
|
|
raise Exception(f'Unhandled flash method "{method}" for "{what}"')
|
2021-08-05 20:26:48 +02:00
|
|
|
else:
|
2023-01-06 04:38:05 +01:00
|
|
|
if method and method != FASTBOOT:
|
|
|
|
raise Exception(f'Flashing "{what}" with method "{method}" not supported, try no parameter or "{FASTBOOT}"')
|
2021-10-22 17:07:05 +02:00
|
|
|
loop_device = losetup_rootfs_image(device_image_path, sector_size)
|
2022-02-06 20:36:11 +01:00
|
|
|
if what == ABOOT:
|
|
|
|
path = dump_aboot(f'{loop_device}p1')
|
2023-04-30 03:24:17 +02:00
|
|
|
fastboot_flash(location or 'boot', path, confirm=confirm)
|
2021-10-22 17:07:05 +02:00
|
|
|
elif what == LK2ND:
|
|
|
|
path = dump_lk2nd(f'{loop_device}p1')
|
2023-04-30 03:24:17 +02:00
|
|
|
fastboot_flash(location or 'lk2nd', path, confirm=confirm)
|
2021-10-22 17:07:05 +02:00
|
|
|
elif what == QHYPSTUB:
|
|
|
|
path = dump_qhypstub(f'{loop_device}p1')
|
2023-04-30 03:24:17 +02:00
|
|
|
fastboot_flash(location or 'qhypstub', path, confirm=confirm)
|
2021-10-22 17:07:05 +02:00
|
|
|
else:
|
|
|
|
raise Exception(f'Unknown what "{what}", this must be a bug in kupferbootstrap!')
|