def main() -> None: """Main function.""" # Create Boot Image instance img = BootImgRT(address=0x20000000, version=0x40) # Add DCD segment with open(f"{DATA_DIR}/dcd.txt", "r") as f_txt: img.dcd = SegDCD.parse_txt(f_txt.read()) # Add application segment with open(f"{DATA_DIR}/plain_load_ivt_flashloader.bin", "rb") as f_bin: img.add_image(data=f_bin.read()) # Print image info print(img.info()) # Save into file with open(f"{DATA_DIR}/flashloader.imx", "wb") as f: f.write(img.export())
def dcd_from_txt() -> SegDCD: """Create DCD from a text (text file).""" data = """ WriteValue 4 0x30340004 0x4F400005 WriteValue 4 0x30340004 0x4F400005 WriteValue 4 0x30340004 0x4F400005 WriteValue 4 0x30340004 0x4F400005 ClearBitMask 4 0x307900C4 0x00000001 SetBitMask 4 0x307900C4 0x00000001 CheckAllClear 4 0x307900C4 0x00000001 CheckAllClear 4 0x307900C4 0x00000001 5 CheckAnyClear 4 0x307900C4 0x00000001 CheckAnyClear 4 0x307900C4 0x00000001 5 CheckAllSet 4 0x307900C4 0x00000001 CheckAllSet 4 0x307900C4 0x00000001 5 CheckAnySet 4 0x307900C4 0x00000001 CheckAnySet 4 0x307900C4 0x00000001 5 Nop """ return SegDCD.parse_txt(data)
def test_txt_parser_for_empty_input(): assert SegDCD.parse_txt('') == SegDCD(enabled=True)
def test_txt_parser_from_cfg_tools(data_dir, ref_dcd_obj): with open(os.path.join(data_dir, 'dcd.txt'), 'r') as f: dcd_data = f.read() dcd_obj = SegDCD.parse_txt(dcd_data) # compare with reference DCD assert dcd_obj == ref_dcd_obj
def ref_dcd_obj(): # Prepare reference DCD object obj = SegDCD(enabled=True) obj.append( CmdWriteData(ops=EnumWriteOps.WRITE_VALUE, data=[(0x400FC010, 0x4F400005), (0x400FC010, 0x4F463645), (0x400FC010, 0xA54EF14A)])) obj.append( CmdWriteData(ops=EnumWriteOps.WRITE_CLEAR_BITS, data=[(0x402F0008, 0x00000001), (0x402F0008, 0x00000002), (0x402F0008, 0x00000003)])) obj.append( CmdWriteData(ops=EnumWriteOps.SET_BITMASK, data=[(0x400D8158, 0x00000009), (0x400D8158, 0x0000000A), (0x400D8158, 0x000000B4)])) obj.append( CmdCheckData(ops=EnumCheckOps.ALL_CLEAR, address=0x401F83F4, mask=0x000000AF, count=5)) obj.append( CmdCheckData(ops=EnumCheckOps.ALL_CLEAR, address=0x401F83F4, mask=0x000000A1)) obj.append( CmdCheckData(ops=EnumCheckOps.ANY_CLEAR, address=0x400D8150, mask=0x000000FF, count=3)) obj.append( CmdCheckData(ops=EnumCheckOps.ANY_CLEAR, address=0x400D8150, mask=0x00000004)) obj.append( CmdCheckData(ops=EnumCheckOps.ALL_SET, address=0x402F0008, mask=0x00000006)) obj.append( CmdCheckData(ops=EnumCheckOps.ALL_SET, address=0x402F0008, mask=0x00000AF3, count=2)) obj.append( CmdCheckData(ops=EnumCheckOps.ANY_SET, address=0x400D8158, mask=0x00000009)) obj.append( CmdCheckData(ops=EnumCheckOps.ANY_SET, address=0x400D8158, mask=0xB00E60E1)) obj.append( CmdCheckData(ops=EnumCheckOps.ANY_CLEAR, address=0x400D8158, mask=0x000F00A2)) obj.append( CmdCheckData(ops=EnumCheckOps.ANY_CLEAR, address=0x400D8158, mask=0xA00F60C1)) obj.append( CmdWriteData(ops=EnumWriteOps.WRITE_CLEAR_BITS, data=[(0x401F8400, 0x00000001)])) obj.append( CmdWriteData(ops=EnumWriteOps.SET_BITMASK, data=[(0x401F8400, 0x00000000)])) obj.append( CmdWriteData(ops=EnumWriteOps.WRITE_CLEAR_BITS, data=[(0x401F8400, 0x00000000)])) obj.append( CmdWriteData(ops=EnumWriteOps.SET_BITMASK, data=[(0x401F8400, 0x00000001)])) obj.append(CmdNop()) return obj
def test_bin_export_from_cfg_tools(data_dir, ref_dcd_obj): with open(os.path.join(data_dir, 'dcd.bin'), 'rb') as f: dcd_obj = f.read() dcd_bin_exported = SegDCD.export(ref_dcd_obj) # compare with reference DCD assert dcd_obj == dcd_bin_exported
def test_txt_parser_for_invalid_input(): assert SegDCD.parse_txt('InvalidCmd\\\nNextLine') == SegDCD( enabled=True) # test invalid commands are ignored
def dcd_from_bin() -> SegDCD: """Create DCD from binary data (from binary file).""" # fmt: off data = bytes([ 0xd2, 0x00, 0xb4, 0x41, 0xcc, 0x00, 0x24, 0x04, 0x30, 0x34, 0x00, 0x04, 0x4f, 0x40, 0x00, 0x05, 0x30, 0x34, 0x00, 0x04, 0x4f, 0x40, 0x00, 0x05, 0x30, 0x34, 0x00, 0x04, 0x4f, 0x40, 0x00, 0x05, 0x30, 0x34, 0x00, 0x04, 0x4f, 0x40, 0x00, 0x05, 0xcc, 0x00, 0x0c, 0x14, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0xcc, 0x00, 0x0c, 0x1c, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x00, 0x0c, 0x04, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x00, 0x10, 0x04, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0xcf, 0x00, 0x0c, 0x14, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x00, 0x10, 0x14, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0xcf, 0x00, 0x0c, 0x0c, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x00, 0x10, 0x0c, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0xcf, 0x00, 0x0c, 0x1c, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x00, 0x10, 0x1c, 0x30, 0x79, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0xc0, 0x00, 0x04, 0x00, ]) # fmt: on return SegDCD.parse(data)
def dcd_in_python() -> SegDCD: """Create DCD in python code.""" dcd = SegDCD(enabled=True) dcd.append( CmdWriteData( ops=EnumWriteOps.WRITE_VALUE, data=( (0x30340004, 0x4F400005), (0x30340004, 0x4F400005), (0x30340004, 0x4F400005), (0x30340004, 0x4F400005), ), )) dcd.append( CmdWriteData(ops=EnumWriteOps.CLEAR_BITMASK, data=((0x307900C4, 0x00000001), ))) dcd.append( CmdWriteData(ops=EnumWriteOps.SET_BITMASK, data=((0x307900C4, 0x00000001), ))) dcd.append( CmdCheckData(ops=EnumCheckOps.ALL_CLEAR, address=0x307900C4, mask=0x00000001)) dcd.append( CmdCheckData(ops=EnumCheckOps.ALL_CLEAR, address=0x307900C4, mask=0x00000001, count=5)) dcd.append( CmdCheckData(ops=EnumCheckOps.ANY_CLEAR, address=0x307900C4, mask=0x00000001)) dcd.append( CmdCheckData(ops=EnumCheckOps.ANY_CLEAR, address=0x307900C4, mask=0x00000001, count=5)) dcd.append( CmdCheckData(ops=EnumCheckOps.ALL_SET, address=0x307900C4, mask=0x00000001)) dcd.append( CmdCheckData(ops=EnumCheckOps.ALL_SET, address=0x307900C4, mask=0x00000001, count=5)) dcd.append( CmdCheckData(ops=EnumCheckOps.ANY_SET, address=0x307900C4, mask=0x00000001)) dcd.append( CmdCheckData(ops=EnumCheckOps.ANY_SET, address=0x307900C4, mask=0x00000001, count=5)) dcd.append(CmdNop()) return dcd
def test_bin_parser_from_cfg_tools(data_dir, ref_dcd_obj): with open(os.path.join(data_dir, "dcd.bin"), "rb") as f: dcd_data = f.read() dcd_obj = SegDCD.parse(dcd_data) # compare with reference DCD assert dcd_obj == ref_dcd_obj
def test_txt_export_from_cfg_tools(data_dir, ref_dcd_obj): with open(os.path.join(data_dir, "dcd.txt"), "r") as f: dcd_obj = f.read() dcd_bin_exported = SegDCD.export_txt(ref_dcd_obj) # compare with reference DCD assert dcd_obj == dcd_bin_exported