def handle_support_routines_header_files(spt_routines): for function in spt_routines: file_content = function.elements_to_string() ##################################################################### # Modification based on settings if "TpmBuildSwitches.h" in function.name and not settings.ENABLE_TABLE_DRIVEN_DISPATCHER: file_content = file_content.replace( "#define TABLE_DRIVEN_DISPATCH", "// #define TABLE_DRIVEN_DISPATCH") if "VendorString.h" in function.name: file_content = re.sub( r"//[ ]*#define[ ]*MANUFACTURER[ ]*\"MSFT\"", '#define MANUFACTURER "' + str(settings.MANUFACTURER) + '"', file_content) file_content = re.sub( r"//[ ]*#define[ ]*VENDOR_STRING_1[ ]*\"DPA \"", '#define VENDOR_STRING_1 "' + str(settings.VENDOR_STRING_1) + '"', file_content) file_content = re.sub( r"//[ ]*#define[ ]*FIRMWARE_V1[ ]*\(.*?\)", '#define FIRMWARE_V1 "' + str(settings.FIRMWARE_V1) + '"', file_content) ##################################################################### file_path = constants.SRC_PATH + constants.TPM_PATH + function.folder_name + "/" + function.name # correct file path: save fiels to global include folder file_names = ["bits.h", "bool.h", "swap.h", "TpmError.h"] if function.name in file_names: file_path = constants.SRC_PATH + constants.TPM_PATH + "../include/" + function.name FileHandling.write_file(file_path, file_content)
def handle_support_routines(self, spt_routines): for spt_routine in spt_routines: # create the prototype file if it needs to be created if spt_routine.name.endswith(".c"): prototype_file = PrototypeFile(spt_routine.short_name) prototype_file.extract_prototype_functions(spt_routine) if settings.SPEC_VERSION_INT >= 138 and "ExecuteCommand" in spt_routine.short_name: prototype_file.file_path = prototype_file.file_path.replace( "ExecuteCommand", "ExecCommand") prototype_file.write() file_content = spt_routine.elements_to_string() # Set file path file_path = self.set_file_path(spt_routine.folder_name, spt_routine.file_name) if settings.SPEC_VERSION_INT >= 138 and spt_routine.name.endswith( ".h"): file_path = constants.SRC_PATH + constants.TPM_PATH + "include/" + spt_routine.file_name # Write file FileHandling.write_file(file_path, file_content)
def handle_annex(self, functions): for function in functions: ################################################################### # PROTOTYPES (START): check if proto files needs to be created if "../OsslCryptoEngine" in function.folder_name and function.name.endswith(".c"): prototype_file = PrototypeFile(function.short_name) prototype_file.extract_prototype_functions(function) prototype_file.write() # PROTOTYPES (END) ################################################################### file_content = function.elements_to_string() if function.name.strip().endswith("()"): function.name = function.name.strip().replace("()", "_fp.h") elif function.name.strip().endswith(")"): result = re.search('\((.*)\)', function.name.strip()) if result: function.name = result.group(1) # FIX for CpriHashData.c if "CpriHashData.c" in function.name: file_content = file_content.replace("#ifdef", "#ifdef ") # file_path = self.set_file_path(function.folder_name, function.name) FileHandling.write_file(file_path, file_content)
def handle_support_routines_header_files(spt_routines): for function in spt_routines: file_content = function.elements_to_string() ##################################################################### # Modification based on settings if "TpmBuildSwitches.h" in function.name and not settings.ENABLE_TABLE_DRIVEN_DISPATCHER: file_content = file_content.replace("#define TABLE_DRIVEN_DISPATCH", "// #define TABLE_DRIVEN_DISPATCH") if "VendorString.h" in function.name: file_content = file_content.replace('// #define MANUFACTURER "MSFT"', '#define MANUFACTURER "' + str(settings.MANUFACTURER) + '"') file_content = file_content.replace('// #define VENDOR_STRING_1 "DPA "', '#define VENDOR_STRING_1 "' + str(settings.VENDOR_STRING_1) + '"') file_content = file_content.replace('// #define FIRMWARE_V1 (0x20140711)', '#define FIRMWARE_V1 ' + str(settings.FIRMWARE_V1)) ##################################################################### file_path = constants.SRC_PATH + constants.TPM_PATH + function.folder_name + "/" + function.name # correct file path: save fiels to global include folder file_names = ["bits.h", "bool.h", "swap.h", "TpmError.h"] if function.name in file_names: file_path = constants.SRC_PATH + constants.TPM_PATH + "../include/" + function.name FileHandling.write_file(file_path, file_content)
def create_tpm_types_h_file(self, table_list): tpm_alg_ids = None self.tpm_types_h_file_content = TpmTypes.TPMTYPES_HEADER print "Writing TPM_Types.h (and BaseTypes.h):" for table in table_list: print u"\t" + table.name.replace(u"—", "") # "—" requires unicode if "Definition" not in table.name: continue if table.table_type is None: continue name = re.sub('\([^)]*\)\s*', "", table.short_name) name = re.sub('\{[^}]*\}\s*', "", name) self.tpm_types_h_file_content += "// Table 2:" + table.number + " - " + name + " (" + table.table_type + ")\n" # Base Types are a special case and will be written to BaseTypes.h (not TPM_Types.h) if "Base Types" in table.short_name: self.create_base_types_h_file(table) elif "TPM_ECC_CURVE Constants" in table.short_name: self.marshaller.handle_simple_constants(table) elif "TPM_CC Constants" in table.short_name: self.marshaller.handle_simple_constants(table) # TPM_ALG_IDs need further processing elif "TPM_ALG_ID Constants" in table.short_name: alg_id_inst = AlgorithmIDs() tpm_alg_ids = alg_id_inst.extract(table) self.marshaller.handle_simple_constants(table) elif "TypedefTable" in table.table_type: self.handle_typedef_table(table, tpm_alg_ids) elif "InterfaceTable" in table.table_type: self.handle_interface_table(table, tpm_alg_ids) elif "EnumTable" in table.table_type: self.handle_enum_table(table) elif "BitsTable" in table.table_type: self.handle_bits_table(table) elif "StructureTable" in table.table_type: self.handle_structures_table(table, tpm_alg_ids) elif "UnionTable" in table.table_type: self.handle_structures_table(table, tpm_alg_ids) self.tpm_types_h_file_content += "\n" # end of loop - for table in table_list: self.tpm_types_h_file_content += TpmTypes.TPMTYPES_FOOTER FileHandling.write_file(constants.TPM_INCLUDE_PATH + "TPM_Types.h", self.tpm_types_h_file_content)
def write(self): self.code = tpm2_part3_handle_process_templates.handle_process_template.safe_substitute( COMMAND=self.command, SELECTOR=self.selector, TABLE_DRIVEN_DISPATCH_IFNDEF=self.table_driven_dispatch_ifndef, TABLE_DRIVEN_DISPATCH_ENDIF=self.table_driven_dispatch_endif, CASES=self.code) FileHandling.write_file(self.file_path, self.code)
def write(self): ifndef = "#ifndef " + self.header_name_sup + "\n" define = "#define " + self.header_name_sup + "\n\n" endif = "#endif // " + self.header_name_sup + "\n" file_content = ifndef + define + self.functions.content + endif FileHandling.write_file(self.file_path, file_content)
def write(self): ifndef = "#ifndef " + self.header_name + "\n" define = "#define " + self.header_name + "\n\n" endif = "#endif // " + self.header_name + "\n" self.content = ifndef + define + self.content + endif FileHandling.write_file(self.file_path, self.content)
def write(self): self.code = tpm2_part3_command_dispatcher_templates.command_dispatcher_template.safe_substitute( COMMAND=self.command, COMMAND_VAR=self.command_var, SELECTOR=self.selector, CASES=self.code) FileHandling.write_file(self.file_path, self.code)
def extract(self, support_file, dict_sections_spt_routines_header_files): # extract license text license_text = LicenseExtractor.extract_license(support_file) FileHandling.set_license(license_text) spt_routines_annex = SupportRoutinesExtractor.extract_annex(support_file, dict_sections_spt_routines_header_files) self.handle_annex(spt_routines_annex) return None
def extract(self, support_file, dict_sections_spt_routines_header_files): # extract license text license_text = LicenseExtractor.extract_license(support_file) FileHandling.set_license(license_text) spt_routines_annex = SupportRoutinesExtractor.extract_annex( support_file, dict_sections_spt_routines_header_files) self.handle_annex(spt_routines_annex) return None
def handle_annex(self, functions): if settings.SPEC_VERSION_INT >= 138: prototype_file_simulator = PrototypeFile("Simulator") prototype_file_simulator.file_path = constants.SRC_PATH + "simulator/include/prototypes/Simulator_fp.h" for function in functions: ################################################################### # PROTOTYPES (START): check if proto files needs to be created if "../OsslCryptoEngine" in function.folder_name and function.name.endswith( ".c"): prototype_file = PrototypeFile(function.short_name) prototype_file.extract_prototype_functions(function) prototype_file.write() if settings.SPEC_VERSION_INT >= 138 and "../simulator" in function.folder_name and function.name.endswith( ".c"): prototype_file_simulator.extract_prototype_functions(function) # PROTOTYPES (END) ################################################################### file_content = function.elements_to_string() if function.name.strip().endswith("()"): function.name = function.name.strip().replace("()", "_fp.h") elif function.name.strip().endswith(")"): result = re.search('\((.*)\)', function.name.strip()) if result: function.name = result.group(1) # FIX for CpriHashData.c if "CpriHashData.c" in function.name: file_content = file_content.replace("#ifdef", "#ifdef ") # file_path = self.set_file_path(function.folder_name, function.name) if settings.SPEC_VERSION_INT >= 138: if "PlatformData.h" in function.name: file_path = constants.SRC_PATH + "platform/include/" + function.name if "Platform_fp.h" in function.name: file_path = constants.SRC_PATH + "platform/include/prototypes/" + function.name if "TpmTcpProtocol.h" in function.name: file_path = constants.SRC_PATH + "simulator/include/" + function.name if "TpmToOssl" in function.name and function.name.endswith( ".h"): file_path = constants.SRC_PATH + "tpm/include/ossl/" + function.name FileHandling.write_file(file_path, file_content) if settings.SPEC_VERSION_INT >= 138: prototype_file_simulator.write()
def extract(self, commands_file, folders): # extract license text license_text = LicenseExtractor.extract_license(commands_file) FileHandling.set_license(license_text) # extract CommandAttributes.h self.command_attributes_header_file = CommandAttributesHeaderFile() self.command_attributes_header_file.write() commands = CommandsExtractor.extract(commands_file, folders) self.handle_commands(commands)
def write(self): self.content = u'#include "InternalRoutines.h"\n\n' + self.content # marshal.c FileHandling.write_file(self.file_path, self.content) # marshal_fp.h self.content_fp = u'#ifndef _MARSHAL_FP_H\n\ #define _MARSHAL_FP_H\n\n'\ + self.content_fp self.content_fp += "#endif // _MARSHAL_FP_H" FileHandling.write_file(self.file_path_fp, self.content_fp)
def extract(self, structures_file): # extract license text license_text = LicenseExtractor.extract_license(structures_file) FileHandling.set_license(license_text) # extract tables from document table_list = TableExtractor.extract_structure_tables(structures_file) # create TPM_Types.h (and BaseTypes.h) self.create_tpm_types_h_file(table_list) self.marshaller.handle_arrays() self.marshaller.write()
def handle_commands(self, commands): for command in commands: if command.table_command is not None: self.list_of_tables_commands.append(command.table_command) ################################################################### # PROTOTYPES (START) prototype_file = PrototypeFile(command.short_name) prototype_file.extract_structures_and_modifiers( command.short_name, command.table_command, command.table_response, self.command_dispatcher) prototype_file.extract_prototype_functions(command) prototype_file.write() # PROTOTYPES (END) ################################################################### file_content = command.elements_to_string() file_path = self.COMMAND_PATH + command.folder_name + "/" + command.file_name # correct file path for EC_Ephemeral.c if "EC_Ephemeral.c" in command.file_name: file_path = self.COMMAND_PATH + "Asymmetric/" + command.file_name FileHandling.write_file(file_path, file_content) # add prototype header file to Commands.h if command.name.startswith("TPM2_"): if command.section_name != self.current_section: self.command_header_file.append("\n\n") self.command_header_file.append("// " + command.section_name + "\n") self.current_section = command.section_name self.command_header_file.append("#include \"" + command.short_name + "_fp.h\"\n") self.command_header_file.write() self.command_dispatcher.write() if settings.ENABLE_TABLE_DRIVEN_DISPATCHER: self.command_dispatch_data.write() # create HandleProcess.c if settings.SPEC_VERSION_INT < 138: self.create_handle_process()
def write(self): if settings.SPEC_VERSION_INT < 138: self.content = u'#include "InternalRoutines.h"\n\n' + self.content else: self.content = u'#include "Tpm.h"\n\n' + self.content # marshal.c FileHandling.write_file(self.file_path, self.content) # marshal_fp.h self.content_fp = u'#ifndef _MARSHAL_FP_H\n\ #define _MARSHAL_FP_H\n\n'\ + self.content_fp self.content_fp += "#endif // _MARSHAL_FP_H" FileHandling.write_file(self.file_path_fp, self.content_fp)
def handle_support_routines(self, spt_routines): for spt_routine in spt_routines: # create the prototype file if it needs to be created if spt_routine.name.endswith(".c"): prototype_file = PrototypeFile(spt_routine.short_name) prototype_file.extract_prototype_functions(spt_routine) prototype_file.write() file_content = spt_routine.elements_to_string() # Set file path file_path = self.set_file_path(spt_routine.folder_name, spt_routine.file_name) # Write file FileHandling.write_file(file_path, file_content)
def handle_commands(self, commands): for command in commands: if command.table_command is not None: self.list_of_tables_commands.append(command.table_command) ################################################################### # PROTOTYPES (START) prototype_file = PrototypeFile(command.short_name) prototype_file.extract_structures_and_modifiers(command.short_name, command.table_command, command.table_response, self.command_dispatcher) prototype_file.extract_prototype_functions(command) prototype_file.write() # PROTOTYPES (END) ################################################################### file_content = command.elements_to_string() file_path = self.COMMAND_PATH + command.folder_name + "/" + command.file_name # correct file path for EC_Ephemeral.c if "EC_Ephemeral.c" in command.file_name: file_path = self.COMMAND_PATH + "Asymmetric/" + command.file_name FileHandling.write_file(file_path, file_content) # add prototype header file to Commands.h if command.name.startswith("TPM2_"): if command.section_name != self.current_section: self.command_header_file.append("\n\n") self.command_header_file.append("// " + command.section_name + "\n") self.current_section = command.section_name self.command_header_file.append("#include \"" + command.short_name + "_fp.h\"\n") self.command_header_file.write() self.command_dispatcher.write() if settings.ENABLE_TABLE_DRIVEN_DISPATCHER: self.command_dispatch_data.write() # create HandleProcess.c self.create_handle_process()
def create_base_types_h_file(self, table): base_types_h_file_content = BaseTypes.BASETYPE_HEADER base_types_h_file_content += "// Part 2, " + table.name + "\n" for row in table.rows: base_type = row[0] new_type = row[1] line = "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" base_types_h_file_content += str(line) base_types_h_file_content += "\n\n" self.marshaller.handle_simple_type_structures_new(table) base_types_h_file_content += BaseTypes.BASETYPE_FOOTER FileHandling.write_file(constants.TPM_INCLUDE_PATH + "BaseTypes.h", base_types_h_file_content)
def create_base_types_h_file(self, table): base_types_h_file_content = BaseTypes.BASETYPE_HEADER base_types_h_file_content += "// Part 2, " + table.name + "\n" for row in table.rows: base_type = row[0] new_type = row[1] line = "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" base_types_h_file_content += str(line) base_types_h_file_content += "\n\n" self.marshaller.handle_simple_type_structures_new(table) base_types_h_file_content += BaseTypes.BASETYPE_FOOTER FileHandling.write_file(constants.TPM_INCLUDE_PATH + "BaseTypes.h", base_types_h_file_content)
def write(self): # create name name = self.header_name.upper() if self.name.startswith("_"): name = self.header_name.upper()[:-1] + "FP_H_" # replace "_" with "FP_H_" # IFDEF for command command_ifdef = "" if not self.name.startswith("_"): command_ifdef = tpm2_part3_commands_prototypes_templates.command_ifdef.safe_substitute( NAME=self.name ) # Modifiers modifiers = "" if self.modifiers.content is not u'': modifiers= self.modifiers.content # Functions functions = self.functions.content # ENDIF for command command_endif = "" if not self.name.startswith("_"): command_endif = tpm2_part3_commands_prototypes_templates.command_endif.safe_substitute( NAME=self.name ) # create file content content = tpm2_part3_commands_prototypes_templates.command_prototype_template.safe_substitute( COMMAND_IFDEF=command_ifdef, NAME=name, STRUCTURE_IN=self.structure_in.content, STRUCTURE_OUT=self.structure_out.content, MODIFIERS=modifiers, FUNCTIONS=functions, COMMAND_ENDIF=command_endif, ) FileHandling.write_file(self.file_path, content)
def write(self): self.code = tpm2_part3_command_dispatcher_templates.command_dispatcher_template.safe_substitute( COMMAND=self.command, COMMAND_VAR=self.command_var, SELECTOR=self.selector, CASES=self.code ) FileHandling.write_file(self.file_path, self.code)
("(informative) Simulation Environment", "../platform"), ("(informative) Remote Procedure Interface", "../simulator"), ]) # --------------------------------------------------------------------------- # # Extract code from PDF/XML files # --------------------------------------------------------------------------- # # Create class instances: s = structures.Structures() c = commands.Commands() h = spt_routines_header_files.SptRoutinesHeaderFiles() r = spt_routines.SptRoutines() a = spt_routines_annex.SptRoutinesAnnex() file_handler = FileHandling() # Extract code: print "Reading " + settings.TPM20_SPEC_STRUCTURES structures_file = file_handler.get_fd(settings.TPM20_SPEC_STRUCTURES) print "" s.extract(structures_file) print "" print "Reading " + settings.TPM20_SPEC_COMMANDS sys.stdout.flush() commands_file = file_handler.get_fd(settings.TPM20_SPEC_COMMANDS) print "" c.extract(commands_file, dict_sections_commands) print ""
if not settings.SET: print "Please check and set the values in 'settings.py'" exit(1) # --------------------------------------------------------------------------- # # Extract code from PDF/XML files # --------------------------------------------------------------------------- # # Create class instances: s = structures.Structures() c = commands.Commands() h = spt_routines_header_files.SptRoutinesHeaderFiles() r = spt_routines.SptRoutines() a = spt_routines_annex.SptRoutinesAnnex() file_handler = FileHandling() # Extract code: print "Reading " + settings.TPM20_SPEC_STRUCTURES structures_file = file_handler.get_fd(settings.TPM20_SPEC_STRUCTURES) print "" s.extract(structures_file) print "" print "Reading " + settings.TPM20_SPEC_COMMANDS commands_file = file_handler.get_fd(settings.TPM20_SPEC_COMMANDS) print "" c.extract(commands_file, dict_sections_commands) print "" print "Reading " + settings.TPM20_SPEC_SUPPORTING_ROUTINES