-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new build rule for OEM data files which are structured compiled data. The rule provides support for MSFT, GCC, and Clang toolchains. Deletes the intermediate binary files in a module output directory after the final binary is written using the files. This is important to prevent intermediate files from potentially being used in future incremental builds if the corresponding source file is no longer present. BaseTools/OemDataFormatter: Rename to ProductDataFormatter Renames the tool to ProductDataFormatter to align with a change in data name from "OEM data" to "product data".
- Loading branch information
1 parent
fadeea6
commit 017f59f
Showing
4 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
#python `dirname $0`/RunToolFromSource.py `basename $0` $* | ||
|
||
# If a ${PYTHON_COMMAND} command is available, use it in preference to python | ||
if command -v ${PYTHON_COMMAND} >/dev/null 2>&1; then | ||
python_exe=${PYTHON_COMMAND} | ||
fi | ||
|
||
full_cmd=${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028 for a discussion of why $0 is not a good choice here | ||
dir=$(dirname "$full_cmd") | ||
exe=$(basename "$full_cmd") | ||
|
||
export PYTHONPATH="$dir/../../Source/Python${PYTHONPATH:+:"$PYTHONPATH"}" | ||
exec "${python_exe:-python}" "$dir/../../Source/Python/$exe/$exe.py" "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@setlocal | ||
@set ToolName=%~n0% | ||
@%PYTHON_COMMAND% %BASE_TOOLS_PATH%\Source\Python\%ToolName%\%ToolName%.py %* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
BaseTools/Source/Python/ProductDataFormatter/ProductDataFormatter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# @file | ||
# | ||
# Formats a collection of intermediate product data binary files | ||
# in a given directory into a single product binary file | ||
# | ||
# Copyright (C) Microsoft Corporation. | ||
# | ||
## | ||
|
||
import glob | ||
import io | ||
import os | ||
import struct | ||
from collections import namedtuple | ||
|
||
|
||
class ProductDataFormatter: | ||
STRUCT_SIGNATURE = b"__SDSH__" | ||
ITEM_SIGNATURE = b"_SDDATA_" | ||
PRODUCT_ID = b"__MSFT__" | ||
|
||
def __init__(self, directory_path): | ||
if not os.path.isdir(directory_path): | ||
raise NotADirectoryError( | ||
f"{directory_path} is an invalid directory!") | ||
|
||
self._directory_path = directory_path | ||
|
||
def _get_bins(self): | ||
return glob.glob(os.path.join( | ||
self._directory_path, '*.productdatabin.i')) | ||
|
||
def _get_signed_item(self, file_path): | ||
with open(file_path, 'rb') as bin_file: | ||
bin_data = bytearray(bin_file.read()) | ||
|
||
signed_item_pos = 0 | ||
while True: | ||
signed_item_pos = bin_data.find( | ||
self.ITEM_SIGNATURE, signed_item_pos) | ||
|
||
if signed_item_pos == -1: | ||
return | ||
|
||
ItemHeader = namedtuple('ItemHeader', 'sig header_len id len') | ||
|
||
item_header = ItemHeader._make(struct.unpack_from( | ||
'<QHHI', bin_data, signed_item_pos)) | ||
|
||
print(f"Signed data item found") | ||
print(f" Item ID: {item_header.id}") | ||
print(f" Item Length: {item_header.len} bytes") | ||
|
||
signed_item = bin_data[signed_item_pos:signed_item_pos + | ||
item_header.header_len + item_header.len] | ||
|
||
yield signed_item | ||
signed_item_pos += (item_header.header_len + item_header.len) | ||
|
||
def _get_signed_structure(self): | ||
signed_items = [i for b in self._get_bins() for i in | ||
self._get_signed_item(b)] | ||
|
||
StructureHeader = namedtuple('StructureHeader', | ||
'sig rev header_len product_id desc_count') | ||
|
||
struct_header = StructureHeader( | ||
self.STRUCT_SIGNATURE, | ||
1, | ||
26, | ||
self.PRODUCT_ID, | ||
len(signed_items)) | ||
|
||
struct_header_data = struct.pack('<8sHI8sI', *struct_header) | ||
|
||
with io.BytesIO() as out_buffer: | ||
out_buffer.write(struct_header_data) | ||
for item in signed_items: | ||
# Note: Just keep things packed tight for now | ||
# aligned_boundary = (((~out_buffer.tell()) + 1) & | ||
# self.ITEM_ALIGNMENT - 1) | ||
# out_buffer.seek(out_buffer.tell() + aligned_boundary) | ||
out_buffer.write(item) | ||
return out_buffer.getvalue() | ||
|
||
def get_file_data(self): | ||
return {f: list(self._get_signed_item(f)) for f in self._get_bins()} | ||
|
||
def write_data(self, out_file_path): | ||
with open(out_file_path, 'wb') as out_bin_file: | ||
out_bin_file.write(self._get_signed_structure()) | ||
|
||
def remove_intermediate_files(self): | ||
for binary in self._get_bins(): | ||
try: | ||
os.remove(binary) | ||
except OSError as e: | ||
print(e) | ||
print(f"File path could not be removed.") | ||
|
||
|
||
# Todo: Clean up arg parsing, documentation, logger, etc. | ||
if __name__ == '__main__': | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
description=( | ||
'Formats intermediate product data files into ' | ||
'a single consolidated binary.')) | ||
parser.add_argument( | ||
'input_directory', | ||
help='Directory of .productdatabin.i files') | ||
parser.add_argument( | ||
'output_file_path', | ||
help='Name of of the final .productdatabin file.') | ||
args = parser.parse_args() | ||
|
||
product_data = ProductDataFormatter(args.input_directory) | ||
product_data.write_data(args.output_file_path) | ||
product_data.remove_intermediate_files() |