def test_CreateGenCommands(self): sub_region_desc = dscrptr.SubRegionDescriptor() sub_region_desc.parse_json_data( os.path.join("tests", "Collateral", "GoodSubRegDescExample.json")) fmp_guid = "8C8CE578-8A3D-4F1C-9935-896185C32DD3" dummy_file = "somefile.bin" dummy_ui_name = "DummyUi" ws = os.path.join(os.path.curdir, "temp") for index, ffs_file in enumerate(sub_region_desc.ffs_files): gen_sec_cmd_exp = (tools_path.GENSEC + " -o " + "tmp.raw" + " -s EFI_SECTION_RAW -c PI_NONE " + dummy_file) gen_sec_cmd = img.create_gensec_cmd(["raw", "PI_NONE"], ["somefile.bin"]) self.assertEqual(gen_sec_cmd_exp, " ".join(gen_sec_cmd)) gen_sec_cmd_exp = (tools_path.GENSEC + " -o " + "tmp.ui" + " -s EFI_SECTION_USER_INTERFACE -n " + dummy_ui_name) gen_sec_cmd = img.create_gensec_cmd(["ui", dummy_ui_name], None) self.assertEqual(gen_sec_cmd_exp, " ".join(gen_sec_cmd)) gen_ffs_cmd_exp = (tools_path.GENFFS + " -o " + "tmp.ffs" + " -t EFI_FV_FILETYPE_FREEFORM -g " + ffs_file.s_ffs_guid + " -i " + dummy_file) gen_ffs_cmd = img.create_ffs_cmd("free", ffs_file.s_ffs_guid, None, dummy_file) self.assertEqual(gen_ffs_cmd_exp, " ".join(gen_ffs_cmd)) dummy_ffs_files = [ws + "SubRegionFfs1.ffs", ws + "SubRegionFfs2.ffs"] gen_fv_cmd_exp = (tools_path.GENFV + " -o OutputFile.Fv -b 0x1000 -f " + dummy_ffs_files[0] + " -g " + fmp_guid + " --FvNameGuid " + sub_region_desc.s_fv_guid) gen_fv_cmd = img.create_gen_fv_command(sub_region_desc.s_fv_guid, "OutputFile.Fv", dummy_ffs_files[0]) self.assertEqual(gen_fv_cmd_exp, " ".join(gen_fv_cmd)) gen_fv_cmd_exp = (tools_path.GENFV + " -i OutputFile.Fv" + " -o OutputFile.Fv -b 0x1000 -f " + dummy_ffs_files[1] + " -g " + fmp_guid + " --FvNameGuid " + sub_region_desc.s_fv_guid) gen_fv_cmd = img.create_gen_fv_command(sub_region_desc.s_fv_guid, "OutputFile.Fv", dummy_ffs_files[1], "OutputFile.Fv") self.assertEqual(gen_fv_cmd_exp, " ".join(gen_fv_cmd)) #'./tempSubRegionFfs2.ffs' fv_cmd_list = [[ tools_path.GENFV, "-o", "OutputFile.Fv", "-b", "0x1000", "-f", dummy_ffs_files[0], "-g", fmp_guid, "--FvNameGuid", sub_region_desc.s_fv_guid, '-f', os.path.join(".", "tempSubRegionFfs2.ffs") ]] gen_fv_cmd_list = img.build_fv_from_ffs_files(sub_region_desc, "OutputFile.Fv", dummy_ffs_files) self.assertEqual(fv_cmd_list, gen_fv_cmd_list)
def test_CheckIfDataFieldValid(self): sub_region_desc = dscrptr.SubRegionDescriptor() good_guid = "1A803C55-F034-4E60-AD9E-9D3F32CE273C" bad_guid = "xxxxxxxx-F034-4E60-AD9E-9D3F32CE273C" data_field_good1 = ["field_1", dscrptr.data_types.DECIMAL, 1, 0] data_field_good2 = ["field_1", dscrptr.data_types.STRING, 1, "_STDIN_"] data_field_bad_field_name = [1, dscrptr.data_types.DECIMAL, 1, 0] data_field_bad_byte_size = [ "field_1", dscrptr.data_types.DECIMAL, -1, 0 ] data_field_bad_none_data = [ "field_1", dscrptr.data_types.DECIMAL, 1, None ] data_field_bad_type = ["field_1", "SOMETHING", 1, 0] data = [data_field_good1, data_field_good2] ffs_file = dscrptr.SubRegionFfsFile(good_guid, False, data) self.assertTrue(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2] ffs_file = dscrptr.SubRegionFfsFile(good_guid, True, data) self.assertTrue(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2, data_field_bad_field_name] ffs_file = dscrptr.SubRegionFfsFile(good_guid, False, data) self.assertFalse(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2, data_field_bad_byte_size] ffs_file = dscrptr.SubRegionFfsFile(good_guid, False, data) self.assertFalse(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2, data_field_bad_none_data] ffs_file = dscrptr.SubRegionFfsFile(good_guid, False, data) self.assertFalse(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2, data_field_bad_type] ffs_file = dscrptr.SubRegionFfsFile(good_guid, False, data) self.assertFalse(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2] ffs_file = dscrptr.SubRegionFfsFile(good_guid, "somethingelse", data) self.assertFalse(sub_region_desc.check_file_good(ffs_file)) data = [data_field_good1, data_field_good2] with self.assertRaises(dscrptr.SubRegionDescSyntaxError): ffs_file = dscrptr.SubRegionFfsFile(bad_guid, False, data) sub_region_desc.check_file_good(ffs_file)
def test_HandlePayloadGuids(self): sub_reg_desc = dscrptr.SubRegionDescriptor() self.assertTrue( sub_reg_desc.is_known_guid( dscrptr.FMP_CAPSULE_OOB_MANAGEABILITY_FILE_GUID)) self.assertTrue( sub_reg_desc.is_known_guid(dscrptr.FMP_CAPSULE_PSE_FW_FILE_GUID)) self.assertTrue( sub_reg_desc.is_known_guid(dscrptr.FMP_CAPSULE_TCC_ARB_FILE_GUID)) self.assertTrue( sub_reg_desc.is_known_guid( dscrptr.FMP_CAPSULE_TSN_MAC_ADDRESS_FILE_GUID)) self.assertTrue( sub_reg_desc.is_known_guid( dscrptr.FMP_CAPSULE_PSE_TSN_MAC_CONFIG_FILE_GUID)) self.assertFalse( sub_reg_desc.is_known_guid( uuid.UUID("e526c123-d3e9-41dd-af3c-59adc77cd3a5")))
my_parser.add_argument( "--signing-tool-path", dest="SigningToolPath", help="Path to signtool or OpenSSL tool. " " Optional if path to tools are already in PATH.", ) return my_parser if __name__ == "__main__": parser = create_arg_parser() args = parser.parse_args() sub_region_fv_file = os.path.join(os.path.curdir, "SubRegionFv.fv") sub_region_image_file = os.path.join(os.path.curdir, "SubRegionImage.bin") sub_region_desc = subrgn_descrptr.SubRegionDescriptor() sub_region_desc.parse_json_data(args.InputFile) generate_sub_region_fv(sub_region_image_file, sub_region_desc, sub_region_fv_file) gen_cap_cmd = ["python", EDK2_CAPSULE_TOOL] gen_cap_cmd += ["--encode"] gen_cap_cmd += ["--guid", sub_region_desc.s_fmp_guid] gen_cap_cmd += ["--fw-version", str(sub_region_desc.version)] gen_cap_cmd += ["--lsv", "0"] gen_cap_cmd += ["--capflag", "PersistAcrossReset"] gen_cap_cmd += ["--capflag", "InitiateReset"] gen_cap_cmd += ["-o", args.OutputCapsuleFile] if all([ args.OpenSslSignerPrivateCertFile, args.OpenSslOtherPublicCertFile, args.OpenSslTrustedPublicCertFile
if data_field.Type in [ subrgn_descptr.data_types.DECIMAL, subrgn_descptr.data_types.HEXADECIMAL ]: buffer = data_field.dValue.to_bytes(data_field.ByteSize, "little") return buffer def generate_sub_region_image(ffs_file, output_file="./output.bin"): with open(output_file, "wb") as out_buffer: for data_field in ffs_file.data: line_buffer = create_buffer_from_data_field(data_field) out_buffer.write(line_buffer) if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: script <json-file> <out-file-name>") sys.exit(2) json_file = sys.argv[1] outfile = sys.argv[2] desc = subrgn_descptr.SubRegionDescriptor() desc.parse_json_data(json_file) # Currently only creates the first file generate_sub_region_image(desc.ffs_files[0], output_file=outfile)
def test_ParseJsonSubRegDescriptorFiles(self): sub_region_desc = dscrptr.SubRegionDescriptor() sub_region_desc.parse_json_data( os.path.join("tests", "Collateral", "GoodSubRegDescExample.json")) self.assertEqual(dscrptr.FMP_CAPSULE_TSN_MAC_ADDRESS_FILE_GUID, sub_region_desc.fmp_guid) self.assertEqual(1, sub_region_desc.version) sample_ffs_file = sub_region_desc.ffs_files[0] self.assertNotEqual(None, sample_ffs_file.data) field_names = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ] field_values = [ 0, 524, 333, -98230498723950780, 98230984023984, 255, 819, 162364294545257138462923369967, 2106967472293113107385212910597918500865023886834641740578657241782190877997108, "abc" ] field_sizes = [1, 2, 3, 15, 20, 1, 3, 20, 21, 0] actual_sizes = [1, 2, 3, 15, 20, 1, 3, 20, 21, 3] field_types = [ dscrptr.data_types.DECIMAL, dscrptr.data_types.HEXADECIMAL, dscrptr.data_types.DECIMAL, dscrptr.data_types.DECIMAL, dscrptr.data_types.DECIMAL, dscrptr.data_types.HEXADECIMAL, dscrptr.data_types.HEXADECIMAL, dscrptr.data_types.HEXADECIMAL, dscrptr.data_types.HEXADECIMAL, dscrptr.data_types.STRING ] for i in range(10): data_field = sample_ffs_file.data[i] self.assertEqual("field_" + field_names[i], data_field.name) self.assertEqual(field_types[i], data_field.Type) self.assertEqual(field_sizes[i], data_field.ByteSize) if field_types[i] in [ dscrptr.data_types.DECIMAL, dscrptr.data_types.HEXADECIMAL ]: self.assertEqual(field_values[i], data_field.dValue) else: self.assertEqual(field_values[i], data_field.Value) self.assertEqual(actual_sizes[i], len(data_field.Value)) # Json with missing fields with self.assertRaises(dscrptr.SubRegionDescSyntaxError): sub_region_desc.parse_json_data( os.path.join("tests", "Collateral", "MissingFieldSubRegDescExample.json")) # Json with bad guid with self.assertRaises(dscrptr.SubRegionDescSyntaxError): sub_region_desc.parse_json_data( os.path.join("tests", "Collateral", "BadGuidSubRegDescExample.json")) # Json with unknown guid with self.assertRaises(dscrptr.UnknownSubRegionError): sub_region_desc.parse_json_data( os.path.join("tests", "Collateral", "UnknownGuidSubRegDescExample.json")) # Json with bad data field with self.assertRaises(dscrptr.SubRegionDescSyntaxError): sub_region_desc.parse_json_data( os.path.join("tests", "Collateral", "BadDataFieldSubRegDescExample.json"))