2024-04-23 18:22:53 -04:00
|
|
|
#!/usr/bin/env python3 -B
|
|
|
|
# coding=utf-8
|
2022-06-29 18:20:21 -04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Toshiba COM Extract
|
|
|
|
Toshiba BIOS COM Extractor
|
2024-04-23 18:22:53 -04:00
|
|
|
Copyright (C) 2018-2024 Plato Mavropoulos
|
2022-06-29 18:20:21 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2022-09-12 16:09:12 -04:00
|
|
|
from common.externals import get_comextract_path
|
|
|
|
from common.path_ops import make_dirs, path_stem, path_suffixes
|
2022-06-29 18:20:21 -04:00
|
|
|
from common.patterns import PAT_TOSHIBA_COM
|
2022-09-12 16:09:12 -04:00
|
|
|
from common.system import printer
|
|
|
|
from common.templates import BIOSUtility
|
2022-06-29 18:20:21 -04:00
|
|
|
from common.text_ops import file_to_bytes
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
TITLE = 'Toshiba BIOS COM Extractor v3.0'
|
|
|
|
|
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
def is_toshiba_com(in_file):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Check if input is Toshiba BIOS COM image """
|
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
buffer = file_to_bytes(in_file)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
is_ext = path_suffixes(in_file)[-1].upper() == '.COM' if os.path.isfile(in_file) else True
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
is_com = PAT_TOSHIBA_COM.search(buffer)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
return is_ext and is_com
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-09-12 16:09:12 -04:00
|
|
|
def toshiba_com_extract(input_file, extract_path, padding=0):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Parse & Extract Toshiba BIOS COM image """
|
|
|
|
|
2022-09-12 16:09:12 -04:00
|
|
|
if not os.path.isfile(input_file):
|
|
|
|
printer('Error: Could not find input file path!', padding)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-09-12 16:09:12 -04:00
|
|
|
return 1
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
make_dirs(extract_path, delete=True)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
output_name = path_stem(input_file)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
output_file = os.path.join(extract_path, f'{output_name}.bin')
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
try:
|
|
|
|
subprocess.run([get_comextract_path(), input_file, output_file], check=True, stdout=subprocess.DEVNULL)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if not os.path.isfile(output_file):
|
2024-04-23 18:22:53 -04:00
|
|
|
raise ValueError('EXTRACT_FILE_MISSING')
|
|
|
|
except Exception as error: # pylint: disable=broad-except
|
|
|
|
printer(f'Error: ToshibaComExtractor could not extract file {input_file}: {error}!', padding)
|
|
|
|
|
2022-09-12 16:09:12 -04:00
|
|
|
return 2
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
printer(f'Succesfull {output_name} extraction via ToshibaComExtractor!', padding)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
return 0
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-29 18:20:21 -04:00
|
|
|
if __name__ == '__main__':
|
2024-04-23 18:22:53 -04:00
|
|
|
BIOSUtility(title=TITLE, check=is_toshiba_com, main=toshiba_com_extract).run_utility()
|