image.py: factor out and fix up dump_file_from_image() from dump_lk2nd() and friends

This commit is contained in:
InsanePrawn 2022-09-27 03:54:06 +02:00
parent a3d60e768a
commit c4900cfd00

View file

@ -7,7 +7,7 @@ import click
import logging import logging
from signal import pause from signal import pause
from subprocess import CompletedProcess from subprocess import CompletedProcess
from typing import Union from typing import Optional, Union
from chroot.device import DeviceChroot, get_device_chroot from chroot.device import DeviceChroot, get_device_chroot
from constants import Arch, BASE_LOCAL_PACKAGES, BASE_PACKAGES, POST_CMDS from constants import Arch, BASE_LOCAL_PACKAGES, BASE_PACKAGES, POST_CMDS
@ -199,46 +199,32 @@ def mount_chroot(rootfs_source: str, boot_src: str, chroot: DeviceChroot):
chroot.mount(boot_src, '/boot', options=['defaults']) chroot.mount(boot_src, '/boot', options=['defaults'])
def dump_aboot(image_path: str) -> str: def dump_file_from_image(image_path: str, file_path: str, target_path: Optional[str] = None):
path = '/tmp/aboot.img' target_path = target_path or os.path.join('/tmp', os.path.basename(file_path))
result = subprocess.run([ result = run_root_cmd([
'debugfs', 'debugfs',
image_path, image_path,
'-R', '-R',
f'dump /aboot.img {path}', f'\'dump /{file_path.lstrip("/")} {target_path}\'',
]) ])
if result.returncode != 0: if result.returncode != 0:
raise Exception('Failed to dump aboot.img') raise Exception(f'Failed to dump {file_path} from /boot')
return path return target_path
def dump_aboot(image_path: str) -> str:
return dump_file_from_image(image_path, file_path='/aboot.img')
def dump_lk2nd(image_path: str) -> str: def dump_lk2nd(image_path: 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. 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' return dump_file_from_image(image_path, file_path='/lk2nd.img')
result = subprocess.run([
'debugfs',
image_path,
'-R',
f'dump /lk2nd.img {path}',
])
if result.returncode != 0:
raise Exception('Failed to dump lk2nd.img')
return path
def dump_qhypstub(image_path: str) -> str: def dump_qhypstub(image_path: str) -> str:
path = '/tmp/qhypstub.bin' return dump_file_from_image(image_path, file_path='/qhyptstub.img')
result = subprocess.run([
'debugfs',
image_path,
'-R',
f'dump /qhypstub.bin {path}',
])
if result.returncode != 0:
raise Exception('Failed to dump qhypstub.bin')
return path
def create_img_file(image_path: str, size_str: str): def create_img_file(image_path: str, size_str: str):