BIOSUtilities v24.10.06

24.10.06

Changed BIOSUtility.parse_format() to return a boolean
Changed 7-Zip and EFI decompressors to return booleans
Apple EFI Package Extractor support for InstallAssistant
Apple EFI Image Identifier support for Apple ROM Version
Added Apple EFI Image Identifier class instance attributes
Improved flow of non-PATH external executable dependencies
Fixed crash when attempting to clear read-only attribute
Fixed incompatibility with Python versions prior to 3.12
Performance improvements when initializing BIOSUtilities
Improved argument naming and definitions of "main" script
Improved the README with new "main" and Apple EFI changes
This commit is contained in:
Plato Mavropoulos 2024-10-07 01:24:12 +03:00
parent 4175af9eb1
commit eda154b0f2
26 changed files with 346 additions and 223 deletions

View file

@ -10,9 +10,9 @@ 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, path_files,
make_dirs, path_name, path_parent, runtime_root)
from biosutilities.common.patterns import PAT_APPLE_PKG_TAR, PAT_APPLE_PKG_XAR
from biosutilities.common.paths import (copy_file, delete_dirs, extract_folder, make_dirs, path_files,
path_name, path_parent, path_suffixes, runtime_root)
from biosutilities.common.patterns import PAT_APPLE_PKG_DMG, PAT_APPLE_PKG_TAR, PAT_APPLE_PKG_XAR
from biosutilities.common.system import printer
from biosutilities.common.templates import BIOSUtility
from biosutilities.common.texts import file_to_bytes
@ -42,19 +42,22 @@ class AppleEfiPkgExtract(BIOSUtility):
with open(file=input_path, mode='wb') as input_path_object:
input_path_object.write(input_buffer)
if is_szip_supported(in_path=input_path, args=['-tXAR']):
if is_szip_supported(in_path=input_path, args=['-tXAR:s0']):
if bool(PAT_APPLE_PKG_XAR.search(string=input_buffer, endpos=4)):
is_apple_efi_pkg = True
elif is_szip_supported(in_path=input_path, args=['-tTAR']):
elif is_szip_supported(in_path=input_path, args=['-tTAR:s0']):
if bool(PAT_APPLE_PKG_TAR.search(string=input_buffer)):
is_apple_efi_pkg = True
elif is_szip_supported(in_path=input_path, args=['-tDMG:s0']):
if bool(PAT_APPLE_PKG_DMG.search(string=input_buffer, endpos=200)):
is_apple_efi_pkg = True
if input_path != input_object:
os.remove(path=input_path)
return is_apple_efi_pkg
def parse_format(self, input_object: str | bytes | bytearray, extract_path: str, padding: int = 0) -> int:
def parse_format(self, input_object: str | bytes | bytearray, extract_path: str, padding: int = 0) -> bool:
""" Parse & Extract Apple EFI PKG packages """
if isinstance(input_object, str) and os.path.isfile(path=input_object):
@ -69,13 +72,15 @@ class AppleEfiPkgExtract(BIOSUtility):
working_dir: str = os.path.join(extract_path, 'temp')
for pkg_type in ('XAR', 'TAR'):
make_dirs(in_path=working_dir, delete=True)
for pkg_type in ('XAR', 'TAR', 'DMG'):
if is_szip_supported(in_path=input_path, padding=padding, args=[f'-t{pkg_type}']):
if szip_decompress(in_path=input_path, out_path=working_dir, in_name=pkg_type, padding=padding,
args=[f'-t{pkg_type}'], check=True) == 0:
args=None if pkg_type == 'DMG' else [f'-t{pkg_type}']):
break
else:
return 1
return False
if input_path != input_object:
os.remove(path=input_path)
@ -83,22 +88,42 @@ class AppleEfiPkgExtract(BIOSUtility):
for work_file in path_files(in_path=working_dir):
self._pbzx_zip(input_path=work_file, extract_path=extract_path, padding=padding + 4)
self._gzip_cpio(input_path=work_file, extract_path=extract_path, padding=padding + 4)
self._tar_gzip(input_path=work_file, extract_path=extract_path, padding=padding + 4)
self._dmg_zip(input_path=work_file, extract_path=extract_path, padding=padding + 4)
self._xar_gzip(input_path=work_file, extract_path=extract_path, padding=padding + 4)
delete_dirs(in_path=working_dir)
return 0
return True
def _tar_gzip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
""" TAR > GZIP """
def _xar_gzip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
""" XAR/TAR > GZIP """
if is_szip_supported(in_path=input_path, padding=padding, args=['-tTAR']):
tar_path: str = extract_folder(in_path=input_path)
for pkg_type in ('XAR', 'TAR'):
if is_szip_supported(in_path=input_path, padding=padding, 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=tar_path, in_name='TAR', padding=padding,
args=['-tTAR'], check=False) == 0:
for tar_file in path_files(in_path=tar_path):
self._gzip_cpio(input_path=tar_file, extract_path=extract_path, padding=padding + 4)
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):
self._gzip_cpio(input_path=pkg_file, extract_path=extract_path, padding=padding + 4)
break
def _dmg_zip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
""" DMG > ZIP """
if is_szip_supported(in_path=input_path, padding=padding, 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_szip_supported(in_path=dmg_file, padding=padding + 4, 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)
def _pbzx_zip(self, input_path: str, extract_path: str, padding: int = 0) -> None:
""" PBZX > ZIP """
@ -108,9 +133,9 @@ class AppleEfiPkgExtract(BIOSUtility):
if pbzx_module.check_format(input_object=input_path):
printer(message=f'Extracting PBZX via {pbzx_module.title}', padding=padding)
pbzx_path: str = extract_folder(in_path=input_path)
pbzx_path: str = extract_folder(in_path=input_path, suffix='_pbzx_zip')
if pbzx_module.parse_format(input_object=input_path, extract_path=pbzx_path, padding=padding + 4) == 0:
if pbzx_module.parse_format(input_object=input_path, extract_path=pbzx_path, padding=padding + 4):
printer(message=f'Successful PBZX extraction via {pbzx_module.title}!', padding=padding)
for pbzx_file in path_files(in_path=pbzx_path):
@ -118,7 +143,7 @@ class AppleEfiPkgExtract(BIOSUtility):
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'], check=False) == 0:
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)
@ -126,16 +151,16 @@ class AppleEfiPkgExtract(BIOSUtility):
""" GZIP > CPIO """
if is_szip_supported(in_path=input_path, padding=padding, args=['-tGZIP']):
gzip_path: str = extract_folder(in_path=input_path)
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'], check=True) == 0:
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_szip_supported(in_path=gzip_file, padding=padding + 4, 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'], check=False) == 0:
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)
@ -143,6 +168,9 @@ class AppleEfiPkgExtract(BIOSUtility):
def _im4p_id(input_path: str, output_path: str, padding: int = 0) -> None:
""" Split IM4P (if applicable), identify and rename EFI """
if path_suffixes(in_path=input_path)[-1].lower() not in ('.fd', '.scap', '.im4p'):
return None
if not AppleEfiIdentify().check_format(input_object=input_path):
return None
@ -169,11 +197,9 @@ class AppleEfiPkgExtract(BIOSUtility):
if efi_id_module.check_format(input_object=efi_source):
printer(message=f'Identifying EFI via {efi_id_module.title}', padding=padding + 4)
efi_id_exit: int = efi_id_module.parse_format(
input_object=efi_source, extract_path=extract_folder(in_path=efi_source), padding=padding + 8)
if efi_id_exit == 0:
efi_dest: str = os.path.join(path_parent(in_path=efi_source), efi_id_module.efi_name_id)
if efi_id_module.parse_format(input_object=efi_source, extract_path=extract_folder(in_path=efi_source),
padding=padding + 8):
efi_dest: str = os.path.join(path_parent(in_path=efi_source), efi_id_module.efi_file_name)
os.rename(src=efi_source, dst=efi_dest)