2024-04-23 18:22:53 -04:00
|
|
|
#!/usr/bin/env python3 -B
|
|
|
|
# coding=utf-8
|
2022-04-01 10:43:22 -04:00
|
|
|
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
2024-04-23 18:22:53 -04:00
|
|
|
Copyright (C) 2022-2024 Plato Mavropoulos
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
from common.externals import get_tiano_path
|
|
|
|
from common.system import printer
|
|
|
|
|
|
|
|
|
|
|
|
def get_compress_sizes(data):
|
|
|
|
""" Get EFI compression sizes """
|
2022-04-01 10:43:22 -04:00
|
|
|
|
|
|
|
size_compress = int.from_bytes(data[0x0:0x4], 'little')
|
|
|
|
size_original = int.from_bytes(data[0x4:0x8], 'little')
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
return size_compress, size_original
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
def is_efi_compressed(data, strict=True):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Check if data is EFI compressed, controlling EOF padding """
|
|
|
|
|
|
|
|
size_comp, size_orig = get_compress_sizes(data)
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
check_diff = size_comp < size_orig
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if strict:
|
|
|
|
check_size = size_comp + 0x8 == len(data)
|
|
|
|
else:
|
|
|
|
check_size = size_comp + 0x8 <= len(data)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
return check_diff and check_size
|
|
|
|
|
|
|
|
|
2022-06-21 07:23:08 -04:00
|
|
|
def efi_decompress(in_path, out_path, padding=0, silent=False, comp_type='--uefi'):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" EFI/Tiano Decompression via TianoCompress """
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
try:
|
2024-04-23 18:22:53 -04:00
|
|
|
subprocess.run([get_tiano_path(), '-d', in_path, '-o', out_path, '-q', comp_type],
|
|
|
|
check=True, stdout=subprocess.DEVNULL)
|
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
with open(in_path, 'rb') as file:
|
2024-04-23 18:22:53 -04:00
|
|
|
_, size_orig = get_compress_sizes(file.read())
|
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if os.path.getsize(out_path) != size_orig:
|
2024-04-23 18:22:53 -04:00
|
|
|
raise OSError('EFI decompressed file & header size mismatch!')
|
|
|
|
except Exception as error: # pylint: disable=broad-except
|
2022-06-21 07:23:08 -04:00
|
|
|
if not silent:
|
2024-04-23 18:22:53 -04:00
|
|
|
printer(f'Error: TianoCompress could not extract file {in_path}: {error}!', padding)
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
return 1
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-21 07:23:08 -04:00
|
|
|
if not silent:
|
|
|
|
printer('Succesfull EFI decompression via TianoCompress!', padding)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-15 11:17:58 -04:00
|
|
|
return 0
|