Use Python lz4 module for LZ4

To make SBL scripts/tools more OS-agnostic the
lz4 (de)compression module can be used instead of
relying on the BaseTools LZ4 package/executable.

Need to update the azure pipeline to install
the python lz4 module before running the builds.

TEST=Confirmed that the python lz4.block.compress
     routine is compatible with Lz4DecompressLib
     during SBL runtime to decompress LZ4 binaries.

Signed-off-by: James Gutbub <james.gutbub@intel.com>
This commit is contained in:
James Gutbub 2020-12-22 10:15:11 -07:00 committed by Maurice Ma
parent 99fe66faf8
commit ab49d6c9a0
2 changed files with 31 additions and 9 deletions

View File

@ -44,6 +44,7 @@ jobs:
displayName: Install required tools
- script: |
python -m pip install lz4
python BuildLoader.py build qemu -k
displayName: 'Run QEMU build'
@ -98,6 +99,7 @@ jobs:
displayName: Install required tools
- script: |
python -m pip install lz4
python BuildLoader.py build $(Build.Name) $(Build.Arch) $(Build.Target) -k
displayName: 'Run $(Build.Name) build'
@ -166,6 +168,7 @@ jobs:
displayName: Environment configuration
- script: |
python -m pip install lz4
python BuildLoader.py build $(Build.Name) $(Build.Arch) $(Build.Target) -k
displayName: 'Run $(Build.Name) build'

View File

@ -340,13 +340,24 @@ def decompress (in_file, out_file, tool_dir = ''):
fo.write(di[offset:offset + lz_hdr.compressed_len])
fo.close()
compress_tool = "%sCompress" % alg
cmdline = [
os.path.join (tool_dir, compress_tool),
"-d",
"-o", out_file,
temp]
run_process (cmdline, False, True)
if alg == "Lz4":
try:
import lz4.block
except ImportError:
print("Could not import lz4, use 'python -m pip install lz4' to install it.")
exit(1)
decompress_data = lz4.block.decompress(get_file_data(temp))
with open(out_file, "wb") as lz4bin:
lz4bin.write(decompress_data)
lz4bin.close()
else:
compress_tool = "%sCompress" % alg
cmdline = [
os.path.join (tool_dir, compress_tool),
"-d",
"-o", out_file,
temp]
run_process (cmdline, False, True)
os.remove(temp)
def compress (in_file, alg, svn=0, out_path = '', tool_dir = ''):
@ -377,7 +388,15 @@ def compress (in_file, alg, svn=0, out_path = '', tool_dir = ''):
if in_len > 0:
if sig == "LZDM":
shutil.copy(in_file, out_file)
else:
compress_data = get_file_data(out_file)
elif sig == "LZ4 ":
try:
import lz4.block
except ImportError:
print("Could not import lz4, use 'python -m pip install lz4' to install it.")
exit(1)
compress_data = lz4.block.compress(get_file_data(in_file), mode='high_compression')
elif sig == "LZMA":
compress_tool = "%sCompress" % alg
cmdline = [
os.path.join (tool_dir, compress_tool),
@ -385,7 +404,7 @@ def compress (in_file, alg, svn=0, out_path = '', tool_dir = ''):
"-o", out_file,
in_file]
run_process (cmdline, False, True)
compress_data = get_file_data(out_file)
compress_data = get_file_data(out_file)
else:
compress_data = bytearray()