Пример #1
0
def test_multiple_images_with_relocation_table(data_dir):
    """Test image that contains multiple binary images and relocation table
    :param data_dir: absolute path, where test data are located
    """
    with open(os.path.join(data_dir, "multicore", "testfffffff.bin"),
              "rb") as f:
        img_data = f.read()
    with open(os.path.join(data_dir, "multicore", "normal_boot.bin"),
              "rb") as f:
        img1_data = f.read()
    with open(os.path.join(data_dir, "multicore", "special_boot.bin"),
              "rb") as f:
        img2_data = f.read()

    with open(os.path.join(data_dir, "multicore", "rt5xxA0.json"), "rb") as f:
        trust_zone_data = json.loads(f.read())["trustZonePreset"]

    table = MultipleImageTable()
    table.add_entry(MultipleImageEntry(img1_data, 0x80000))
    table.add_entry(MultipleImageEntry(img2_data, 0x80600))

    mbi = Mbi_CrcRamRtxxx(
        app=img_data,
        app_table=table,
        load_addr=0,
        trust_zone=TrustZone.custom("rt5xx", trust_zone_data),
    )

    assert _compare_image(mbi, os.path.join(data_dir, "multicore"),
                          "expected_output.bin")
Пример #2
0
def test_multiple_images_with_relocation_table(data_dir):
    """Test image that contains multiple binary images and relocation table
    :param data_dir: absolute path, where test data are located
    """
    with open(os.path.join(data_dir, 'multicore', "testfffffff.bin"),
              "rb") as f:
        img_data = f.read()
    with open(os.path.join(data_dir, 'multicore', "normal_boot.bin"),
              "rb") as f:
        img1_data = f.read()
    with open(os.path.join(data_dir, 'multicore', "special_boot.bin"),
              "rb") as f:
        img2_data = f.read()

    with open(os.path.join(data_dir, 'multicore', "rt5xxA0.json"), "rb") as f:
        trust_zone_data = json.loads(f.read())['trustZonePreset']

    table = MultipleImageTable()
    table.add_entry(MultipleImageEntry(img1_data, 0x80000))
    table.add_entry(MultipleImageEntry(img2_data, 0x80600))

    mbi = MasterBootImage(app=img_data,
                          app_table=table,
                          load_addr=0,
                          image_type=MasterBootImageType.CRC_RAM_IMAGE,
                          trust_zone=TrustZone.custom('rt5xx',
                                                      trust_zone_data))

    assert _compare_image(mbi, os.path.join(data_dir, 'multicore'),
                          'expected_output.bin')
Пример #3
0
def generate_trustzone_binary(tzm_conf: click.File) -> None:
    """Generate TrustZone binary from json configuration file."""
    config_data = json.load(tzm_conf)
    config = elftosb_helper.TrustZoneConfig(config_data)
    trustzone = TrustZone.custom(family=config.family, revision=config.revision, customizations=config.presets)
    tz_data = trustzone.export()
    with open(config.output_file, 'wb') as f:
        f.write(tz_data)
Пример #4
0
def test_errors(sample_tz_data):
    with pytest.raises(AssertionError):
        TrustZone.custom(family="totaly_legit_family",
                         customizations=sample_tz_data)
    # throw error when TZ is disabled, but tz data are present
    with pytest.raises(ValueError):
        TrustZone(tz_type=TrustZoneType.DISABLED,
                  customizations=sample_tz_data)
    # throw error when TZ is set to CUSTOM but no data and no family are provided
    with pytest.raises(AssertionError):
        TrustZone(tz_type=TrustZoneType.CUSTOM)

# throw error when TZ is set to CUSTOM but no family is provided
    with pytest.raises(AssertionError):
        TrustZone(tz_type=TrustZoneType.CUSTOM, customizations=sample_tz_data)
    # throw error when TZ is set to CUSTOM but no data are provided
    with pytest.raises(AssertionError):
        TrustZone(tz_type=TrustZoneType.CUSTOM, family="lpc55xx")
    # throw error for invalid customization data
    with pytest.raises(ValueError):
        TrustZone(family="lpc55xx", customizations={"fake": "this is fake"})
Пример #5
0
def test_errors(sample_tz_data):
    with pytest.raises(SPSDKError):
        TrustZone.custom(family="totaly_legit_family",
                         customizations=sample_tz_data)
    # throw error when TZ is disabled, but tz data are present
    with pytest.raises(SPSDKError):
        TrustZone(tz_type=TrustZoneType.DISABLED,
                  customizations=sample_tz_data)
    # throw error when TZ is set to CUSTOM but no data and no family are provided
    with pytest.raises(SPSDKError):
        TrustZone(tz_type=TrustZoneType.CUSTOM)
    # throw error when TZ is set to CUSTOM but no family is provided
    with pytest.raises(SPSDKError):
        TrustZone(tz_type=TrustZoneType.CUSTOM, customizations=sample_tz_data)
    # throw error when TZ is set to CUSTOM but no data are provided
    with pytest.raises(SPSDKError):
        TrustZone(tz_type=TrustZoneType.CUSTOM, family="lpc55xx")
    # throw error for invalid customization data
    with pytest.raises(SPSDKError):
        TrustZone(family="lpc55xx", customizations={"fake": "this is fake"})
    # throw error when TZ  type is custom and family is not set
    with pytest.raises(SPSDKError, match="Need to provide 'family' parameter"):
        TrustZone(tz_type=TrustZoneType.CUSTOM, family=None)
Пример #6
0
def _get_trustzone(config: elftosb_helper.MasterBootImageConfig) -> TrustZone:
    """Create appropriate TrustZone instance."""
    if not config.trustzone_preset_file:
        return TrustZone.disabled()
    try:
        tz_config_data = json.loads(load_file(config.trustzone_preset_file))
        tz_config = elftosb_helper.TrustZoneConfig(tz_config_data)
        return TrustZone.custom(
            family=tz_config.family, revision=tz_config.revision, customizations=tz_config.presets
        )
    except ValueError:
        tz_raw_data = load_binary(config.trustzone_preset_file)
        return TrustZone.from_binary(
            family=config.family, revision=config.revision, raw_data=tz_raw_data
        )
Пример #7
0
def generate_trustzone() -> None:
    """Generate custom trustzone presets.

    For this example we have only few settings in configuration file.
    The full set is available in `spsdk/data/tz_presets` folder
    """
    supperted_families = TrustZone().get_families()
    print("Supported families:")
    print("\n".join(supperted_families))

    with open(os.path.join(DATA_DIR, "lpc55xx_tz.json")) as config_file:
        config_data = json.load(config_file)

    tz_presets = TrustZone.custom(family="lpc55xx", customizations=config_data)
    tz_data = tz_presets.export()

    with open(os.path.join(THIS_DIR, "tz.bin"), "wb") as binary_file:
        binary_file.write(tz_data)
Пример #8
0
def test_base_info(data_dir):
    """Basic test for MasterBootImage - information """
    # plain image
    mbi = MasterBootImage(app=bytes(range(64)),
                          load_addr=0,
                          enable_hw_user_mode_keys=True)
    output = mbi.info()
    repr_strings = [
        "Master Boot Image", "Image type", "Image length", "TrustZone",
        'HW user mode keys'
    ]
    for req_string in repr_strings:
        assert req_string in output, f'string {req_string} is not in the output: {output}'
    # CRC image
    mbi = MasterBootImage(app=bytes(range(64)),
                          image_type=MasterBootImageType.CRC_RAM_IMAGE,
                          load_addr=0x1000)
    output = mbi.info()
    repr_strings = [
        "Master Boot Image", "Image type", "Image length", "TrustZone"
    ]
    for req_string in repr_strings:
        assert req_string in output, f'string {req_string} is not in the output: {output}'
    # signed image
    priv_key_pem_data = _load_private_key(data_dir, 'private_rsa4096.pem')
    cert_block = certificate_block(
        data_dir, ['selfsign_4096_v3.der.crt', 'selfsign_3072_v3.der.crt'], 0)
    mbi = MasterBootImage(app=bytes(range(64)),
                          load_addr=0x12345678,
                          image_type=MasterBootImageType.SIGNED_XIP_IMAGE,
                          trust_zone=TrustZone.custom(
                              "lpc55xx",
                              {"MPU Control Register.(cm33_mpu_ctrl)": "0x0"}),
                          cert_block=cert_block,
                          priv_key_pem_data=priv_key_pem_data)
    output = mbi.info()
    repr_strings = [
        "Master Boot Image", "Image type", "Image length", "TrustZone"
    ]
    for req_string in repr_strings:
        assert req_string in output, f'string {req_string} is not in the output: {output}'
Пример #9
0
def test_tz_types(sample_tz_data):
    # TZ is enabled by default
    tz = TrustZone()
    assert tz.type == TrustZoneType.ENABLED
    tz = TrustZone.enabled()
    assert tz.type == TrustZoneType.ENABLED
    tz = TrustZone.disabled()
    assert tz.type == TrustZoneType.DISABLED

    tz = TrustZone(family="lpc55xx", customizations=sample_tz_data)
    assert tz.type == TrustZoneType.CUSTOM
    tz = TrustZone(family="lpc55xx",
                   customizations=sample_tz_data,
                   tz_type=TrustZoneType.CUSTOM)
    assert tz.type == TrustZoneType.CUSTOM
    tz = TrustZone(family="lpc55xx",
                   customizations=sample_tz_data,
                   tz_type=TrustZoneType.ENABLED)
    assert tz.type == TrustZoneType.CUSTOM
    tz = TrustZone.custom(family="lpc55xx", customizations=sample_tz_data)
    assert tz.type == TrustZoneType.CUSTOM
    assert "TrustZone" in str(tz)