mirror of
https://github.com/platomav/BIOSUtilities.git
synced 2025-05-13 06:34:42 -04:00
BIOSUtilities v24.10.29
Added graceful exception hanlding during "main" flow Improved and cleaned 7-Zip and EFI compression logic Improved too aggressive extraction directory handling Fixed input name detection at VAIO Package Extractor Fixed Intel IBIOSI detection at Apple EFI Identifier
This commit is contained in:
parent
d8e23f9ef3
commit
f895fc208c
24 changed files with 327 additions and 380 deletions
|
@ -10,13 +10,12 @@ Copyright (C) 2019-2024 Plato Mavropoulos
|
|||
import os
|
||||
|
||||
from biosutilities.common.compression import is_szip_supported, szip_decompress
|
||||
from biosutilities.common.paths import (copy_file, delete_dirs, extract_folder, is_access, is_file, make_dirs,
|
||||
path_files, path_name, path_parent, path_suffixes, runtime_root)
|
||||
from biosutilities.common.paths import (copy_file, delete_dirs, delete_file, extract_folder, is_access, is_dir,
|
||||
is_file, make_dirs, path_files, path_name, path_suffixes, runtime_root)
|
||||
from biosutilities.common.system import printer
|
||||
from biosutilities.common.templates import BIOSUtility
|
||||
from biosutilities.common.texts import file_to_bytes
|
||||
|
||||
from biosutilities.apple_efi_id import AppleEfiIdentify
|
||||
from biosutilities.apple_efi_id import AppleEfiIdentify, EFI_EXTENSIONS
|
||||
from biosutilities.apple_efi_im4p import AppleEfiIm4pSplit
|
||||
from biosutilities.apple_efi_pbzx import AppleEfiPbzxExtract
|
||||
|
||||
|
@ -31,15 +30,13 @@ class AppleEfiPkgExtract(BIOSUtility):
|
|||
|
||||
is_apple_efi_pkg: bool = False
|
||||
|
||||
input_buffer: bytes = file_to_bytes(in_object=self.input_object)
|
||||
|
||||
if isinstance(self.input_object, str) and is_file(in_path=self.input_object):
|
||||
input_path: str = self.input_object
|
||||
else:
|
||||
input_path = os.path.join(runtime_root(), 'APPLE_EFI_PKG_INPUT_BUFFER_CHECK.bin')
|
||||
|
||||
with open(input_path, 'wb') as input_path_object:
|
||||
input_path_object.write(input_buffer)
|
||||
input_path_object.write(self.input_buffer)
|
||||
|
||||
for pkg_type in ('XAR', 'TAR', 'DMG'):
|
||||
if is_szip_supported(in_path=input_path, args=[f'-t{pkg_type}:s0']):
|
||||
|
@ -48,7 +45,7 @@ class AppleEfiPkgExtract(BIOSUtility):
|
|||
break
|
||||
|
||||
if input_path != self.input_object:
|
||||
os.remove(input_path)
|
||||
delete_file(in_path=input_path)
|
||||
|
||||
return is_apple_efi_pkg
|
||||
|
||||
|
@ -61,16 +58,14 @@ class AppleEfiPkgExtract(BIOSUtility):
|
|||
input_path = os.path.join(runtime_root(), 'APPLE_EFI_PKG_INPUT_BUFFER_PARSE.bin')
|
||||
|
||||
with open(input_path, 'wb') as input_path_object:
|
||||
input_path_object.write(file_to_bytes(in_object=self.input_object))
|
||||
|
||||
make_dirs(in_path=self.extract_path, delete=True)
|
||||
input_path_object.write(self.input_buffer)
|
||||
|
||||
working_dir: str = os.path.join(self.extract_path, 'temp')
|
||||
|
||||
make_dirs(in_path=working_dir)
|
||||
|
||||
for pkg_type in ('XAR', 'TAR', 'DMG'):
|
||||
if is_szip_supported(in_path=input_path, padding=self.padding, args=[f'-t{pkg_type}']):
|
||||
if is_szip_supported(in_path=input_path, args=[f'-t{pkg_type}']):
|
||||
if szip_decompress(in_path=input_path, out_path=working_dir, in_name=pkg_type, padding=self.padding,
|
||||
args=None if pkg_type == 'DMG' else [f'-t{pkg_type}']):
|
||||
break
|
||||
|
@ -78,52 +73,52 @@ class AppleEfiPkgExtract(BIOSUtility):
|
|||
return False
|
||||
|
||||
if input_path != self.input_object:
|
||||
os.remove(input_path)
|
||||
delete_file(in_path=input_path)
|
||||
|
||||
for work_file in path_files(in_path=working_dir):
|
||||
if is_file(in_path=work_file) and is_access(in_path=work_file):
|
||||
self._pbzx_zip(input_path=work_file, extract_path=self.extract_path, padding=self.padding + 4)
|
||||
self._gzip_cpio(input_path=work_file, extract_path=self.extract_path, padding=self.padding + 4)
|
||||
self._dmg_zip(input_path=work_file, extract_path=self.extract_path, padding=self.padding + 4)
|
||||
self._xar_gzip(input_path=work_file, extract_path=self.extract_path, padding=self.padding + 4)
|
||||
self._pbzx_zip(input_path=work_file, padding=self.padding + 4)
|
||||
self._gzip_cpio(input_path=work_file, padding=self.padding + 4)
|
||||
self._dmg_zip(input_path=work_file, padding=self.padding + 4)
|
||||
self._xar_gzip(input_path=work_file, padding=self.padding + 4)
|
||||
|
||||
delete_dirs(in_path=working_dir)
|
||||
|
||||
return True
|
||||
|
||||
def _xar_gzip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
|
||||
def _xar_gzip(self, input_path: str, padding: int = 0) -> None:
|
||||
""" XAR/TAR > GZIP """
|
||||
|
||||
for pkg_type in ('XAR', 'TAR'):
|
||||
if is_szip_supported(in_path=input_path, padding=padding, args=[f'-t{pkg_type}']):
|
||||
if is_szip_supported(in_path=input_path, args=[f'-t{pkg_type}']):
|
||||
pkg_path: str = extract_folder(in_path=input_path, suffix=f'_{pkg_type.lower()}_gzip')
|
||||
|
||||
if szip_decompress(in_path=input_path, out_path=pkg_path, in_name=pkg_type,
|
||||
padding=padding, args=[f'-t{pkg_type}']):
|
||||
for pkg_file in path_files(in_path=pkg_path):
|
||||
if is_file(in_path=pkg_file) and is_access(in_path=pkg_file):
|
||||
self._gzip_cpio(input_path=pkg_file, extract_path=extract_path, padding=padding + 4)
|
||||
self._gzip_cpio(input_path=pkg_file, padding=padding + 4)
|
||||
|
||||
break
|
||||
|
||||
def _dmg_zip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
|
||||
def _dmg_zip(self, input_path: str, padding: int = 0) -> None:
|
||||
""" DMG > ZIP """
|
||||
|
||||
if is_szip_supported(in_path=input_path, padding=padding, args=['-tDMG']):
|
||||
if is_szip_supported(in_path=input_path, args=['-tDMG']):
|
||||
dmg_path: str = extract_folder(in_path=input_path, suffix='_dmg_zip')
|
||||
|
||||
if szip_decompress(in_path=input_path, out_path=dmg_path, in_name='DMG', padding=padding, args=None):
|
||||
for dmg_file in path_files(in_path=dmg_path):
|
||||
if is_file(in_path=dmg_file) and is_access(in_path=dmg_file):
|
||||
if is_szip_supported(in_path=dmg_file, padding=padding + 4, args=['-tZIP']):
|
||||
if is_szip_supported(in_path=dmg_file, args=['-tZIP']):
|
||||
zip_path: str = extract_folder(in_path=dmg_file)
|
||||
|
||||
if szip_decompress(in_path=dmg_file, out_path=zip_path, in_name='ZIP',
|
||||
padding=padding + 4, args=['-tZIP']):
|
||||
for zip_file in path_files(in_path=zip_path):
|
||||
self._im4p_id(input_path=zip_file, output_path=extract_path, padding=padding + 8)
|
||||
self._im4p_id(input_path=zip_file, padding=padding + 8)
|
||||
|
||||
def _pbzx_zip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
|
||||
def _pbzx_zip(self, input_path: str, padding: int = 0) -> None:
|
||||
""" PBZX > ZIP """
|
||||
|
||||
pbzx_path: str = extract_folder(in_path=input_path, suffix='_pbzx_zip')
|
||||
|
@ -139,51 +134,48 @@ class AppleEfiPkgExtract(BIOSUtility):
|
|||
|
||||
for pbzx_file in path_files(in_path=pbzx_path):
|
||||
if is_file(in_path=pbzx_file) and is_access(in_path=pbzx_file):
|
||||
if is_szip_supported(in_path=pbzx_file, padding=padding + 4, args=['-tZIP']):
|
||||
if is_szip_supported(in_path=pbzx_file, args=['-tZIP']):
|
||||
zip_path: str = extract_folder(in_path=pbzx_file)
|
||||
|
||||
if szip_decompress(in_path=pbzx_file, out_path=zip_path, in_name='ZIP',
|
||||
padding=padding + 4, args=['-tZIP']):
|
||||
for zip_file in path_files(in_path=zip_path):
|
||||
self._im4p_id(input_path=zip_file, output_path=extract_path, padding=padding + 8)
|
||||
self._im4p_id(input_path=zip_file, padding=padding + 8)
|
||||
|
||||
def _gzip_cpio(self, input_path: str, extract_path: str, padding: int = 0) -> None:
|
||||
def _gzip_cpio(self, input_path: str, padding: int = 0) -> None:
|
||||
""" GZIP > CPIO """
|
||||
|
||||
if is_szip_supported(in_path=input_path, padding=padding, args=['-tGZIP']):
|
||||
if is_szip_supported(in_path=input_path, args=['-tGZIP']):
|
||||
gzip_path: str = extract_folder(in_path=input_path, suffix='_gzip_cpio')
|
||||
|
||||
if szip_decompress(in_path=input_path, out_path=gzip_path, in_name='GZIP',
|
||||
padding=padding, args=['-tGZIP']):
|
||||
for gzip_file in path_files(in_path=gzip_path):
|
||||
if is_file(in_path=gzip_file) and is_access(in_path=gzip_file):
|
||||
if is_szip_supported(in_path=gzip_file, padding=padding + 4, args=['-tCPIO']):
|
||||
if is_szip_supported(in_path=gzip_file, args=['-tCPIO']):
|
||||
cpio_path: str = extract_folder(in_path=gzip_file)
|
||||
|
||||
if szip_decompress(in_path=gzip_file, out_path=cpio_path, in_name='CPIO',
|
||||
padding=padding + 4, args=['-tCPIO']):
|
||||
for cpio_file in path_files(in_path=cpio_path):
|
||||
self._im4p_id(input_path=cpio_file, output_path=extract_path, padding=padding + 8)
|
||||
self._im4p_id(input_path=cpio_file, padding=padding + 8)
|
||||
|
||||
@staticmethod
|
||||
def _im4p_id(input_path: str, output_path: str, padding: int = 0) -> None:
|
||||
""" Split IM4P (if applicable), identify and rename EFI """
|
||||
def _im4p_id(self, input_path: str, padding: int = 0) -> None:
|
||||
""" Split IM4P (if applicable), identify and copy EFI """
|
||||
|
||||
if path_suffixes(in_path=input_path)[-1].lower() not in EFI_EXTENSIONS:
|
||||
return None
|
||||
|
||||
if not (is_file(in_path=input_path) and is_access(in_path=input_path)):
|
||||
return None
|
||||
|
||||
if path_suffixes(in_path=input_path)[-1].lower() not in ('.fd', '.scap', '.im4p'):
|
||||
return None
|
||||
|
||||
if not AppleEfiIdentify(input_object=input_path).check_format():
|
||||
return None
|
||||
|
||||
input_name: str = path_name(in_path=input_path)
|
||||
|
||||
printer(message=input_name, padding=padding)
|
||||
|
||||
working_dir: str = extract_folder(in_path=input_path)
|
||||
|
||||
if not AppleEfiIdentify(input_object=input_path, extract_path=working_dir).check_format():
|
||||
return None
|
||||
|
||||
printer(message=path_name(in_path=input_path), padding=padding)
|
||||
|
||||
im4p_module: AppleEfiIm4pSplit = AppleEfiIm4pSplit(
|
||||
input_object=input_path, extract_path=working_dir, padding=padding + 8)
|
||||
|
||||
|
@ -191,27 +183,19 @@ class AppleEfiPkgExtract(BIOSUtility):
|
|||
printer(message=f'Splitting IM4P via {im4p_module.TITLE}', padding=padding + 4)
|
||||
|
||||
im4p_module.parse_format()
|
||||
else:
|
||||
make_dirs(in_path=working_dir, delete=True)
|
||||
|
||||
copy_file(in_path=input_path, out_path=working_dir, metadata=True)
|
||||
|
||||
for efi_source in path_files(in_path=working_dir):
|
||||
if is_file(in_path=efi_source) and is_access(in_path=efi_source):
|
||||
for efi_path in path_files(in_path=working_dir) if is_dir(in_path=working_dir) else [input_path]:
|
||||
if is_file(in_path=efi_path) and is_access(in_path=efi_path):
|
||||
efi_id_module: AppleEfiIdentify = AppleEfiIdentify(
|
||||
input_object=efi_source, extract_path=extract_folder(in_path=efi_source), padding=padding + 8)
|
||||
input_object=efi_path, extract_path=extract_folder(in_path=efi_path), padding=padding + 8)
|
||||
|
||||
if efi_id_module.check_format():
|
||||
printer(message=f'Identifying EFI via {efi_id_module.TITLE}', padding=padding + 4)
|
||||
|
||||
if efi_id_module.parse_format():
|
||||
efi_dest: str = os.path.join(path_parent(in_path=efi_source), efi_id_module.efi_file_name)
|
||||
efi_path_final: str = os.path.join(self.extract_path, efi_id_module.efi_file_name)
|
||||
|
||||
os.replace(efi_source, efi_dest)
|
||||
|
||||
for efi_final in path_files(in_path=working_dir):
|
||||
if is_file(in_path=efi_final) and is_access(in_path=efi_final):
|
||||
copy_file(in_path=efi_final, out_path=output_path, metadata=True)
|
||||
copy_file(in_path=efi_path, out_path=efi_path_final, metadata=True)
|
||||
|
||||
delete_dirs(in_path=working_dir)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue