Пример #1
0
def test_flexspi_conf_block_fcb(data_dir) -> None:
    # default object created
    fcb = FlexSPIConfBlockFCB()
    assert fcb.info()
    assert fcb.export()
    assert fcb == FlexSPIConfBlockFCB.parse(fcb.export())
    fcb.padding_len = 10
    assert len(fcb.export()) == fcb.space
    # FCB from RT105x EVK
    fcb_path = os.path.join(data_dir, 'rt105x_flex_spi.fcb')
    fcb = FlexSPIConfBlockFCB.parse(load_binary(fcb_path))
    assert fcb.info()
    fcb.padding_len = 0
    compare_bin_files(fcb_path, fcb.export())
    fcb.enabled = False
    assert fcb.size == 0
    assert fcb.export() == b''
    # invalid tag
    with pytest.raises(ValueError):
        FlexSPIConfBlockFCB.parse(b'\x00' * 512)
    # invalid version
    with pytest.raises(ValueError):
        FlexSPIConfBlockFCB.parse(FlexSPIConfBlockFCB.TAG + b'\x00' * 512)
    # insufficient length
    with pytest.raises(ValueError):
        FlexSPIConfBlockFCB.parse(FlexSPIConfBlockFCB.TAG +
                                  FlexSPIConfBlockFCB.VERSION[::-1])
Пример #2
0
def write_sb(cpu_params: CpuParams, image_file_name: str, img: SecureBootV1) -> None:
    """Write SB image to the external flash
    The method behaviour depends on TEST_IMG_CONTENT:
    - if True (TEST MODE), the method generates the image and compare with previous version
    - if False (PRODUCTION), the method generates the image and burn into FLASH

    :param cpu_params: processor specific parameters of the test
    :param image_file_name: of the image to be written (including file extension)
    :param img: image instance to be written
    """
    path = os.path.join(cpu_params.data_dir, OUTPUT_IMAGES_SUBDIR, image_file_name)
    dbg_info = DebugInfo()
    img_data = img.export(dbg_info=dbg_info,
                          # use the following parameters only for unit test
                          header_padding8=b'\xdb\x00\x76\x7a\xf4\x81\x0b\x86',
                          auth_padding=b'\x36\x72\xf4\x99\x92\x05\x34\xd2\xd5\x17\xa0\xf7')
    write_dbg_log(cpu_params.data_dir, image_file_name, dbg_info.lines, TEST_IMG_CONTENT)
    if TEST_IMG_CONTENT:
        assert img.info()  # quick check info prints non-empty output
        compare_bin_files(path, img_data)
        img = SecureBootV1.parse(b'0' + img_data, 1)
        dbg_info2 = DebugInfo()
        img_data2 = img.export(dbg_info=dbg_info2, header_padding8=b'\xdb\x00\x76\x7a\xf4\x81\x0b\x86',
                               auth_padding=b'\x36\x72\xf4\x99\x92\x05\x34\xd2\xd5\x17\xa0\xf7')
        assert dbg_info.lines == dbg_info2.lines
        assert img_data == img_data2
    else:
        with open(path, 'wb') as f:
            f.write(img_data)

        mboot = init_flashloader(cpu_params)
        assert mboot.receive_sb_file(img_data)
        mboot.close()
Пример #3
0
def write_sb(data_dir: str, sb_file_name: str, bin_data: bytes, key_store: KeyStore) -> None:
    """ In production mode, this function should write SB file to the disk and burn info external FLASH.
    In test mode, the function just compare existing content with provided content.

    :param data_dir: absolute path of the data directory
    :param sb_file_name: of the output SB file on the disk
    :param bin_data: binary content of the SB file
    :param key_store: key-store used for SB file
    """
    path = os.path.join(data_dir, OUTPUT_IMAGES_SUBDIR, sb_file_name)
    if TEST_IMG_CONTENT:
        compare_bin_files(path, bin_data)
    else:
        with open(path, 'wb') as f:
            f.write(bin_data)
        send_sb_via_usb_into_processor(bin_data, key_store)
Пример #4
0
def write_image(data_dir: str, image_file_name: str, bin_data: bytes) -> None:
    """In production mode, this function should write image to the disk and burn info external FLASH.
    In test mode, the function just compare existing image with provided image.

    :param data_dir: absolute path of the data directory
    :param image_file_name: of the output image on the disk
    :param bin_data: binary content of the image
    """
    path = os.path.join(data_dir, OUTPUT_IMAGES_SUBDIR, image_file_name)
    if TEST_IMG_CONTENT:
        compare_bin_files(path, bin_data)
    else:
        with open(path, "wb") as f:
            f.write(bin_data)
        # burn info external flash; processor must be connected using USB
        mboot = burn_img_via_usb_into_flexspi_flash(data_dir, bin_data)
        mboot.close()
Пример #5
0
def test_srk_table(cpu_params: CpuParams) -> None:
    """Test creation of SRK table and SRK fuses"""
    srk_table = srk_table4(cpu_params)
    srk_table_path = os.path.join(cpu_params.srk_data_dir, 'SRK_hash_table.bin')
    srk_fuses_path = os.path.join(cpu_params.srk_data_dir, 'SRK_fuses.bin')

    # test valid fuse value
    assert srk_table.get_fuse(0) == 0x3B86E63F
    assert srk_table.get_fuse(7) == 0xc542ab47
    #
    if TEST_IMG_CONTENT:
        compare_bin_files(srk_table_path, srk_table.export())
        compare_bin_files(srk_fuses_path, srk_table.export_fuses())
    else:
        with open(srk_table_path, 'wb') as f:
            f.write(srk_table.export())
        with open(srk_fuses_path, 'wb') as f:
            f.write(srk_table.export_fuses())
Пример #6
0
def write_image(
        cpu_params: CpuParams,
        image_file_name: str,
        img: BootImgRT,
        otpmk_bee_regions: Tuple[BeeFacRegion, ...] = tuple(),
) -> None:
    """Write image to the external flash
    The method behaviour depends on TEST_IMG_CONTENT:
    - if True (TEST MODE), the method generates the image and compare with previous version
    - if False (PRODUCTION), the method generates the image and burn into FLASH

    :param cpu_params: processor specific parameters of the test
    :param image_file_name: of the image to be written (including file extension)
    :param img: image instance to be written
    :param otpmk_bee_regions: optional list of BEE regions for BEE OTPMK encryption
    """
    path = os.path.join(cpu_params.data_dir, OUTPUT_IMAGES_SUBDIR,
                        image_file_name)
    debug_info = DebugInfo()
    # use zulu datetime for test purposes only, to produce stable output; remove the parameter for production
    zulu = datetime(year=2020,
                    month=4,
                    day=8,
                    hour=5,
                    minute=54,
                    second=33,
                    tzinfo=timezone.utc)
    img_data = img.export(dbg_info=debug_info, zulu=zulu)
    assert len(img_data) == img.size
    write_dbg_log(cpu_params.data_dir, image_file_name, debug_info.lines,
                  TEST_IMG_CONTENT)
    if TEST_IMG_CONTENT:
        assert img.info()  # quick check info prints non-empty output
        compare_bin_files(path, img_data)
        # compare no-padding
        if (NO_PADDING and img.fcb.enabled and isinstance(img.fcb, PaddingFCB)
                and not img.bee_encrypted):
            img.fcb.enabled = False
            compare_bin_files(path.replace(".bin", "_nopadding.bin"),
                              img.export(zulu=zulu))
            img.fcb.enabled = False
        # test that parse image will return same content
        if img.fcb.enabled and not img.bee_encrypted:
            compare_bin_files(path, BootImgRT.parse(img_data).export())
            # test that size matches len of exported data
            assert img.size == len(img_data)
    else:
        with open(path, "wb") as f:
            f.write(img_data)
        if (NO_PADDING and img.fcb.enabled and isinstance(img.fcb, PaddingFCB)
                and not img.bee_encrypted):
            with open(path.replace(".bin", "_nopadding.bin"), "wb") as f:
                f.write(img_data[img.ivt_offset:])

        if img.ivt_offset == BootImgRT.IVT_OFFSET_NOR_FLASH:
            _burn_image_to_flash(cpu_params, img, img_data, otpmk_bee_regions)
        else:
            assert len(otpmk_bee_regions) == 0
            _burn_image_to_sd(cpu_params, img, img_data)