Ejemplo n.º 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 = 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')
Ejemplo n.º 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 = 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")
Ejemplo n.º 3
0
def test_invalid_master_boot_image_params(data_dir):
    with pytest.raises(TypeError):
        # noinspection PyTypeChecker
        MasterBootImage(app=5)
    # load_addr must not be negative
    with pytest.raises(AssertionError):
        MasterBootImage(app=bytes(range(64)), load_addr=-1, image_type=MasterBootImageType.CRC_XIP_IMAGE)

    # certificate block is supported for signed images
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_RAM_IMAGE)
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_XIP_IMAGE)

    # hmac_key must be provided for load-to-ram image
    cert_block = certificate_block(data_dir, ['selfsign_2048_v3.der.crt'])
    priv_key_pem_data = _load_private_key(data_dir, 'selfsign_privatekey_rsa2048.pem')
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_RAM_IMAGE,
                        cert_block=cert_block, priv_key_pem_data=priv_key_pem_data)
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_RAM_IMAGE,
                        cert_block=cert_block, priv_key_pem_data=priv_key_pem_data,
                        key_store=KeyStore(KeySourceType.OTP))
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_RAM_IMAGE,
                        cert_block=cert_block, priv_key_pem_data=priv_key_pem_data,
                        key_store=KeyStore(KeySourceType.KEYSTORE))
    # hmac_key (or key_store) cannot be provided for XIP image
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_XIP_IMAGE,
                        cert_block=cert_block, priv_key_pem_data=priv_key_pem_data,
                        hmac_key=bytes(range(32)))
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.SIGNED_XIP_IMAGE,
                        cert_block=cert_block, priv_key_pem_data=priv_key_pem_data,
                        key_store=KeyStore(KeySourceType.KEYSTORE))

    # app_table can be specified only for RAM images
    with pytest.raises(ValueError):
        app_table = MultipleImageTable()
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.CRC_XIP_IMAGE,
                        app_table=app_table)
    # app_table is empty
    with pytest.raises(ValueError):
        app_table = MultipleImageTable()
        MasterBootImage(app=bytes(range(64)), load_addr=0, image_type=MasterBootImageType.CRC_RAM_IMAGE,
                        app_table=app_table)

    # error if image len is not enough
    with pytest.raises(ValueError):
        MasterBootImage(app=bytes(range(32)), load_addr=0).export()
Ejemplo n.º 4
0
def test_multiple_image_table_invalid():
    with pytest.raises(SPSDKError,
                       match="There must be at least one entry for export"):
        img_table = MultipleImageTable()
        img_table._entries = None
        img_table.export(start_addr=0xFFF)