1
0
Fork 0
mirror of https://github.com/platomav/BIOSUtilities.git synced 2025-05-22 11:15:26 -04:00
BIOSUtilities/biosutilities/toshiba_com_extract.py
Plato Mavropoulos eda154b0f2 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
2024-10-07 01:24:12 +03:00

69 lines
2.3 KiB
Python

#!/usr/bin/env python3 -B
# coding=utf-8
"""
Toshiba COM Extract
Toshiba BIOS COM Extractor
Copyright (C) 2018-2024 Plato Mavropoulos
"""
import os
import subprocess
from biosutilities.common.externals import comextract_path
from biosutilities.common.paths import make_dirs, path_stem, safe_name
from biosutilities.common.patterns import PAT_TOSHIBA_COM
from biosutilities.common.system import printer
from biosutilities.common.templates import BIOSUtility
from biosutilities.common.texts import file_to_bytes
class ToshibaComExtract(BIOSUtility):
""" Toshiba BIOS COM Extractor """
TITLE: str = 'Toshiba BIOS COM Extractor'
def check_format(self, input_object: str | bytes | bytearray) -> bool:
""" Check if input is Toshiba BIOS COM image """
input_buffer: bytes = file_to_bytes(in_object=input_object)
return bool(PAT_TOSHIBA_COM.search(string=input_buffer, endpos=0x100))
def parse_format(self, input_object: str | bytes | bytearray, extract_path: str, padding: int = 0) -> bool:
""" Parse & Extract Toshiba BIOS COM image """
make_dirs(in_path=extract_path, delete=True)
if isinstance(input_object, str) and os.path.isfile(path=input_object):
input_path: str = input_object
else:
input_path = os.path.join(extract_path, 'toshiba_bios.com')
with open(file=input_path, mode='wb') as input_buffer:
input_buffer.write(file_to_bytes(in_object=input_object))
output_name: str = f'{safe_name(in_name=path_stem(in_path=input_path))}_extracted.bin'
output_path: str = os.path.join(extract_path, output_name)
try:
subprocess.run([comextract_path(), input_path, output_path], check=True, stdout=subprocess.DEVNULL)
if not os.path.isfile(path=output_path):
raise FileNotFoundError('EXTRACTED_FILE_MISSING')
except Exception as error: # pylint: disable=broad-except
printer(message=f'Error: ToshibaComExtractor could not extract {input_path}: {error}!', padding=padding)
return False
if input_path != input_object:
os.remove(path=input_path)
printer(message='Successful extraction via ToshibaComExtractor!', padding=padding)
return True
if __name__ == '__main__':
ToshibaComExtract().run_utility()